blob: 101a39db6dbb658394ad6a282c1e8cc8864b253e [file] [log] [blame]
Howard McLauchlanef4154b2018-03-16 16:50:26 -07001Some examples for inject
2
3inject guarantees the appropriate erroneous return of the specified injection
Howard McLauchlanac7c1542018-03-21 15:53:24 -07004mode (kmalloc,bio,etc) given a call chain and an optional set of predicates. You
Howard McLauchlanef4154b2018-03-16 16:50:26 -07005can also optionally print out the generated BPF program for
6modification/debugging purposes.
7
Howard McLauchlan4c9305c2018-04-10 13:22:00 -07008As a simple example, let's say you wanted to fail all mounts. As of 4.17 we can
9fail syscalls directly, so let's do that:
Howard McLauchlanef4154b2018-03-16 16:50:26 -070010
Howard McLauchlan4c9305c2018-04-10 13:22:00 -070011# ./inject.py kmalloc -v 'SyS_mount()'
Howard McLauchlanef4154b2018-03-16 16:50:26 -070012
Howard McLauchlanac7c1542018-03-21 15:53:24 -070013The first argument indicates the mode (or what to fail). Appropriate headers are
Howard McLauchlan4c9305c2018-04-10 13:22:00 -070014specified, if necessary. The verbosity flag prints the generated program. Note
15that some syscalls will be available as 'SyS_xyz' and some will be available as
16'sys_xyz'. This is largely dependent on the number of arguments each syscall
17takes.
Howard McLauchlanef4154b2018-03-16 16:50:26 -070018
Howard McLauchlanac7c1542018-03-21 15:53:24 -070019Trying to mount various filesystems will fail and report an inability to
20allocate memory, as expected.
21
22Whenever a predicate is missing, an implicit "(true)" is inserted. The example
23above can be explicitly written as:
24
Howard McLauchlan4c9305c2018-04-10 13:22:00 -070025# ./inject.py kmalloc -v '(true) => SyS_mount()(true)'
Howard McLauchlanac7c1542018-03-21 15:53:24 -070026
27The "(true)" without an associated function is a predicate for the error
28injection mechanism of the current mode. In the case of kmalloc, the predicate
29would have access to the arguments of:
30
31 int should_failslab(struct kmem_cache *s, gfp_t gfpflags);
32
33The bio mode works similarly, with access to the arguments of:
34
35 static noinline int should_fail_bio(struct bio *bio)
36
37We also note that it's unnecessary to state the arguments of the function if you
38have no intention to reference them in the associated predicate.
39
40Now let's say we want to be a bit more specific; suppose you want to fail
41kmalloc() from mount_subtree() when called from btrfs_mount(). This will fail
42only btrfs mounts:
43
Howard McLauchlan601d75d2018-03-21 16:27:18 -070044# ./inject.py kmalloc -v 'mount_subtree() => btrfs_mount()'
Howard McLauchlanac7c1542018-03-21 15:53:24 -070045
46Attempting to mount btrfs filesystem during the execution of this command will
47yield an error, but other filesystems will be fine.
Howard McLauchlanef4154b2018-03-16 16:50:26 -070048
49Next, lets say we want to hit one of the BUG_ONs in fs/btrfs. As of 4.16-rc3,
50there is a BUG_ON in btrfs_prepare_close_one_device() at fs/btrfs/volumes.c:1002
51
52To hit this, we can use the following:
53
Howard McLauchlan601d75d2018-03-21 16:27:18 -070054# ./inject.py kmalloc -v 'btrfs_alloc_device() => btrfs_close_devices()'
Howard McLauchlanef4154b2018-03-16 16:50:26 -070055
56While the script was executing, I mounted and unmounted btrfs, causing a
57segfault on umount(since that satisfied the call path indicated). A look at
Howard McLauchlanac7c1542018-03-21 15:53:24 -070058dmesg will confirm that the erroneous return value injected by the script
59tripped the BUG_ON, causing a segfault down the line.
Howard McLauchlanef4154b2018-03-16 16:50:26 -070060
61In general, it's worth noting that the required specificity of the call chain is
62dependent on how much granularity you need. The example above might have
63performed as expected without the intermediate btrfs_alloc_device, but might
64have also done something unexpected(an earlier kmalloc could have failed before
65the one we were targetting).
66
67For hot paths, the approach outlined above isn't enough. If a path is traversed
68very often, we can distinguish distinct calls with function arguments. Let's say
69we want to fail the dentry allocation of a file creatively named 'bananas'. We
70can do the following:
71
Howard McLauchlan601d75d2018-03-21 16:27:18 -070072# ./inject.py kmalloc -v 'd_alloc_parallel(struct dentry *parent, const struct
73qstr *name)(STRCMP(name->name, 'bananas'))'
Howard McLauchlanef4154b2018-03-16 16:50:26 -070074
75While this script is executing, any operation that would cause a dentry
76allocation where the name is 'bananas' fails, as expected.
77
Howard McLauchlanac7c1542018-03-21 15:53:24 -070078Here, since we're referencing a function argument in our predicate, we need to
79provide the function signature up to the argument we're using.
80
Howard McLauchlanef4154b2018-03-16 16:50:26 -070081To note, STRCMP is a workaround for some rewriter issues. It will take input of
82the form (x->...->z, 'literal'), and generate some equivalent code that the
83verifier is more friendly about. It's not horribly robust, but works for the
84purposes of making string comparisons a bit easier.
85
86Finally, we briefly demonstrate how to inject bio failures. The mechanism is
87identical, so any information from above will apply.
88
89Let's say we want to fail bio requests when the request is to some specific
90sector. An example use case would be to fail superblock writes in btrfs. For
91btrfs, we know that there must be a superblock at 65536 bytes, or sector 128.
92This allows us to run the following:
93
94# ./inject.py bio -v -I 'linux/blkdev.h' '(({struct gendisk *d = bio->bi_disk;
95struct disk_part_tbl *tbl = d->part_tbl; struct hd_struct **parts = (void *)tbl +
96sizeof(struct disk_part_tbl); struct hd_struct **partp = parts + bio->bi_partno;
97struct hd_struct *p = *partp; dev_t disk = p->__dev.devt; disk ==
98MKDEV(254,16);}) && bio->bi_iter.bi_sector == 128)'
99
100The predicate in the command above has two parts. The first is a compound
101statement which shortens to "only if the system is btrfs", but is long due
102to rewriter/verifier shenanigans. The major/minor information can be found
103however; I used Python. The second part simply checks the starting
104address of bi_iter. While executing, this script effectively fails superblock
105writes to the superblock at sector 128 without affecting other filesystems.
106
107As an extension to the above, one could easily fail all btrfs superblock writes
108(we only fail the primary) by calculating the sector number of the mirrors and
109amending the predicate accordingly.
Howard McLauchlanac7c1542018-03-21 15:53:24 -0700110
Howard McLauchlan4c9305c2018-04-10 13:22:00 -0700111Inject also provides a probability option; this allows you to fail the
112path+predicates some percentage of the time. For example, let's say we want to
113fail our mounts half the time:
Howard McLauchlanac7c1542018-03-21 15:53:24 -0700114
Howard McLauchlan4c9305c2018-04-10 13:22:00 -0700115# ./inject.py kmalloc -v -P 0.01 'SyS_mount()'
116
117USAGE message:
Howard McLauchlan45bcfb72018-04-13 14:19:15 -0700118usage: inject.py [-h] [-I header] [-P probability] [-v] {kmalloc,bio} spec
Howard McLauchlanac7c1542018-03-21 15:53:24 -0700119
120Fail specified kernel functionality when call chain and predicates are met
121
122positional arguments:
Howard McLauchlan45bcfb72018-04-13 14:19:15 -0700123 {kmalloc,bio} indicate which base kernel function to fail
Howard McLauchlanac7c1542018-03-21 15:53:24 -0700124 spec specify call chain
125
126optional arguments:
127 -h, --help show this help message and exit
128 -I header, --include header
129 additional header files to include in the BPF program
Howard McLauchlan4c9305c2018-04-10 13:22:00 -0700130 -P probability, --probability probability
131 probability that this call chain will fail
Howard McLauchlanac7c1542018-03-21 15:53:24 -0700132 -v, --verbose print BPF program
Howard McLauchlan45bcfb72018-04-13 14:19:15 -0700133
134EXAMPLES:
135# ./inject.py kmalloc -v 'SyS_mount()'
136 Fails all calls to syscall mount
137# ./inject.py kmalloc -v '(true) => SyS_mount()(true)'
138 Explicit rewriting of above
139# ./inject.py kmalloc -v 'mount_subtree() => btrfs_mount()'
140 Fails btrfs mounts only
141# ./inject.py kmalloc -v 'd_alloc_parallel(struct dentry *parent, const struct \
142 qstr *name)(STRCMP(name->name, 'bananas'))'
143 Fails dentry allocations of files named 'bananas'
144# ./inject.py kmalloc -v -P 0.01 'SyS_mount()'
145 Fails calls to syscall mount with 1% probability