blob: 6ca37564aa62e98d7b594db2caa97b7edabb9686 [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 McLauchlan601d75d2018-03-21 16:27:18 -070012# ./inject.py kmalloc -v '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
Howard McLauchlan601d75d2018-03-21 16:27:18 -070023# ./inject.py kmalloc -v '(true) => do_mount()(true)'
Howard McLauchlanac7c1542018-03-21 15:53:24 -070024
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
Howard McLauchlan601d75d2018-03-21 16:27:18 -070042# ./inject.py kmalloc -v 'mount_subtree() => btrfs_mount()'
Howard McLauchlanac7c1542018-03-21 15:53:24 -070043
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 McLauchlan601d75d2018-03-21 16:27:18 -070052# ./inject.py kmalloc -v 'btrfs_alloc_device() => btrfs_close_devices()'
Howard McLauchlanef4154b2018-03-16 16:50:26 -070053
54While the script was executing, I mounted and unmounted btrfs, causing a
55segfault on umount(since that satisfied the call path indicated). A look at
Howard McLauchlanac7c1542018-03-21 15:53:24 -070056dmesg will confirm that the erroneous return value injected by the script
57tripped the BUG_ON, causing a segfault down the line.
Howard McLauchlanef4154b2018-03-16 16:50:26 -070058
59In general, it's worth noting that the required specificity of the call chain is
60dependent on how much granularity you need. The example above might have
61performed as expected without the intermediate btrfs_alloc_device, but might
62have also done something unexpected(an earlier kmalloc could have failed before
63the one we were targetting).
64
65For hot paths, the approach outlined above isn't enough. If a path is traversed
66very often, we can distinguish distinct calls with function arguments. Let's say
67we want to fail the dentry allocation of a file creatively named 'bananas'. We
68can do the following:
69
Howard McLauchlan601d75d2018-03-21 16:27:18 -070070# ./inject.py kmalloc -v 'd_alloc_parallel(struct dentry *parent, const struct
71qstr *name)(STRCMP(name->name, 'bananas'))'
Howard McLauchlanef4154b2018-03-16 16:50:26 -070072
73While this script is executing, any operation that would cause a dentry
74allocation where the name is 'bananas' fails, as expected.
75
Howard McLauchlanac7c1542018-03-21 15:53:24 -070076Here, since we're referencing a function argument in our predicate, we need to
77provide the function signature up to the argument we're using.
78
Howard McLauchlanef4154b2018-03-16 16:50:26 -070079To note, STRCMP is a workaround for some rewriter issues. It will take input of
80the form (x->...->z, 'literal'), and generate some equivalent code that the
81verifier is more friendly about. It's not horribly robust, but works for the
82purposes of making string comparisons a bit easier.
83
84Finally, we briefly demonstrate how to inject bio failures. The mechanism is
85identical, so any information from above will apply.
86
87Let's say we want to fail bio requests when the request is to some specific
88sector. An example use case would be to fail superblock writes in btrfs. For
89btrfs, we know that there must be a superblock at 65536 bytes, or sector 128.
90This allows us to run the following:
91
92# ./inject.py bio -v -I 'linux/blkdev.h' '(({struct gendisk *d = bio->bi_disk;
93struct disk_part_tbl *tbl = d->part_tbl; struct hd_struct **parts = (void *)tbl +
94sizeof(struct disk_part_tbl); struct hd_struct **partp = parts + bio->bi_partno;
95struct hd_struct *p = *partp; dev_t disk = p->__dev.devt; disk ==
96MKDEV(254,16);}) && bio->bi_iter.bi_sector == 128)'
97
98The predicate in the command above has two parts. The first is a compound
99statement which shortens to "only if the system is btrfs", but is long due
100to rewriter/verifier shenanigans. The major/minor information can be found
101however; I used Python. The second part simply checks the starting
102address of bi_iter. While executing, this script effectively fails superblock
103writes to the superblock at sector 128 without affecting other filesystems.
104
105As an extension to the above, one could easily fail all btrfs superblock writes
106(we only fail the primary) by calculating the sector number of the mirrors and
107amending the predicate accordingly.
Howard McLauchlanac7c1542018-03-21 15:53:24 -0700108
109USAGE message:
110
111usage: inject.py [-h] [-I header] [-v] mode spec
112
113Fail specified kernel functionality when call chain and predicates are met
114
115positional arguments:
116 mode indicate which base kernel function to fail
117 spec specify call chain
118
119optional arguments:
120 -h, --help show this help message and exit
121 -I header, --include header
122 additional header files to include in the BPF program
123 -v, --verbose print BPF program
124