blob: ec42b164cb8fa1bf3c438b81a60dbc8d0557c6a9 [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 McLauchlanac7c1542018-03-21 15:53:24 -07008As a simple example, let's say you wanted to fail all mounts. While we cannot
9fail the mount() syscall directly (a patch is in the works), we can easily
10fail do_mount() calls like so:
Howard McLauchlanef4154b2018-03-16 16:50:26 -070011
Howard McLauchlanac7c1542018-03-21 15:53:24 -070012# ./inject.py kmalloc -v -I 'linux/fs.h' 'do_mount()'
Howard McLauchlanef4154b2018-03-16 16:50:26 -070013
Howard McLauchlanac7c1542018-03-21 15:53:24 -070014The first argument indicates the mode (or what to fail). Appropriate headers are
15specified. The verbosity flag prints the generated program.
Howard McLauchlanef4154b2018-03-16 16:50:26 -070016
Howard McLauchlanac7c1542018-03-21 15:53:24 -070017Trying to mount various filesystems will fail and report an inability to
18allocate memory, as expected.
19
20Whenever a predicate is missing, an implicit "(true)" is inserted. The example
21above can be explicitly written as:
22
23# ./inject.py kmalloc -v -I 'linux/fs.h' '(true) => do_mount()(true)'
24
25The "(true)" without an associated function is a predicate for the error
26injection mechanism of the current mode. In the case of kmalloc, the predicate
27would have access to the arguments of:
28
29 int should_failslab(struct kmem_cache *s, gfp_t gfpflags);
30
31The bio mode works similarly, with access to the arguments of:
32
33 static noinline int should_fail_bio(struct bio *bio)
34
35We also note that it's unnecessary to state the arguments of the function if you
36have no intention to reference them in the associated predicate.
37
38Now let's say we want to be a bit more specific; suppose you want to fail
39kmalloc() from mount_subtree() when called from btrfs_mount(). This will fail
40only btrfs mounts:
41
42# ./inject.py kmalloc -I 'linux/fs.h' -v 'mount_subtree() => btrfs_mount()'
43
44Attempting to mount btrfs filesystem during the execution of this command will
45yield an error, but other filesystems will be fine.
Howard McLauchlanef4154b2018-03-16 16:50:26 -070046
47Next, lets say we want to hit one of the BUG_ONs in fs/btrfs. As of 4.16-rc3,
48there is a BUG_ON in btrfs_prepare_close_one_device() at fs/btrfs/volumes.c:1002
49
50To hit this, we can use the following:
51
Howard McLauchlanac7c1542018-03-21 15:53:24 -070052# ./inject.py kmalloc -v -I 'linux/mm.h' 'btrfs_alloc_device()
53 => btrfs_close_devices()'
Howard McLauchlanef4154b2018-03-16 16:50:26 -070054
55While the script was executing, I mounted and unmounted btrfs, causing a
56segfault on umount(since that satisfied the call path indicated). A look at
Howard McLauchlanac7c1542018-03-21 15:53:24 -070057dmesg will confirm that the erroneous return value injected by the script
58tripped the BUG_ON, causing a segfault down the line.
Howard McLauchlanef4154b2018-03-16 16:50:26 -070059
60In general, it's worth noting that the required specificity of the call chain is
61dependent on how much granularity you need. The example above might have
62performed as expected without the intermediate btrfs_alloc_device, but might
63have also done something unexpected(an earlier kmalloc could have failed before
64the one we were targetting).
65
66For hot paths, the approach outlined above isn't enough. If a path is traversed
67very often, we can distinguish distinct calls with function arguments. Let's say
68we want to fail the dentry allocation of a file creatively named 'bananas'. We
69can do the following:
70
Howard McLauchlanac7c1542018-03-21 15:53:24 -070071# ./inject.py kmalloc -v -I 'linux/fs.h' 'd_alloc_parallel(struct dentry
72*parent, const struct qstr *name)(STRCMP(name->name, 'bananas'))'
Howard McLauchlanef4154b2018-03-16 16:50:26 -070073
74While this script is executing, any operation that would cause a dentry
75allocation where the name is 'bananas' fails, as expected.
76
Howard McLauchlanac7c1542018-03-21 15:53:24 -070077Here, since we're referencing a function argument in our predicate, we need to
78provide the function signature up to the argument we're using.
79
Howard McLauchlanef4154b2018-03-16 16:50:26 -070080To note, STRCMP is a workaround for some rewriter issues. It will take input of
81the form (x->...->z, 'literal'), and generate some equivalent code that the
82verifier is more friendly about. It's not horribly robust, but works for the
83purposes of making string comparisons a bit easier.
84
85Finally, we briefly demonstrate how to inject bio failures. The mechanism is
86identical, so any information from above will apply.
87
88Let's say we want to fail bio requests when the request is to some specific
89sector. An example use case would be to fail superblock writes in btrfs. For
90btrfs, we know that there must be a superblock at 65536 bytes, or sector 128.
91This allows us to run the following:
92
93# ./inject.py bio -v -I 'linux/blkdev.h' '(({struct gendisk *d = bio->bi_disk;
94struct disk_part_tbl *tbl = d->part_tbl; struct hd_struct **parts = (void *)tbl +
95sizeof(struct disk_part_tbl); struct hd_struct **partp = parts + bio->bi_partno;
96struct hd_struct *p = *partp; dev_t disk = p->__dev.devt; disk ==
97MKDEV(254,16);}) && bio->bi_iter.bi_sector == 128)'
98
99The predicate in the command above has two parts. The first is a compound
100statement which shortens to "only if the system is btrfs", but is long due
101to rewriter/verifier shenanigans. The major/minor information can be found
102however; I used Python. The second part simply checks the starting
103address of bi_iter. While executing, this script effectively fails superblock
104writes to the superblock at sector 128 without affecting other filesystems.
105
106As an extension to the above, one could easily fail all btrfs superblock writes
107(we only fail the primary) by calculating the sector number of the mirrors and
108amending the predicate accordingly.
Howard McLauchlanac7c1542018-03-21 15:53:24 -0700109
110USAGE message:
111
112usage: inject.py [-h] [-I header] [-v] mode spec
113
114Fail specified kernel functionality when call chain and predicates are met
115
116positional arguments:
117 mode indicate which base kernel function to fail
118 spec specify call chain
119
120optional arguments:
121 -h, --help show this help message and exit
122 -I header, --include header
123 additional header files to include in the BPF program
124 -v, --verbose print BPF program
125