blob: 741a0fb10d306001a07da84bbf5998bbfd8a3fe7 [file] [log] [blame]
Brenden Blancoe8001c32018-07-23 08:15:56 -07001#!/usr/bin/env python
Howard McLauchlanef4154b2018-03-16 16:50:26 -07002#
3# This script generates a BPF program with structure inspired by trace.py. The
4# generated program operates on PID-indexed stacks. Generally speaking,
5# bookkeeping is done at every intermediate function kprobe/kretprobe to enforce
6# the goal of "fail iff this call chain and these predicates".
7#
8# Top level functions(the ones at the end of the call chain) are responsible for
9# creating the pid_struct and deleting it from the map in kprobe and kretprobe
10# respectively.
11#
12# Intermediate functions(between should_fail_whatever and the top level
13# functions) are responsible for updating the stack to indicate "I have been
14# called and one of my predicate(s) passed" in their entry probes. In their exit
15# probes, they do the opposite, popping their stack to maintain correctness.
16# This implementation aims to ensure correctness in edge cases like recursive
17# calls, so there's some additional information stored in pid_struct for that.
18#
19# At the bottom level function(should_fail_whatever), we do a simple check to
20# ensure all necessary calls/predicates have passed before error injection.
21#
22# Note: presently there are a few hacks to get around various rewriter/verifier
23# issues.
24#
Howard McLauchlanb222f002018-04-10 13:05:47 -070025# Note: this tool requires:
Howard McLauchlanef4154b2018-03-16 16:50:26 -070026# - CONFIG_BPF_KPROBE_OVERRIDE
27#
Howard McLauchlanb222f002018-04-10 13:05:47 -070028# USAGE: inject [-h] [-I header] [-P probability] [-v] mode spec
Howard McLauchlanef4154b2018-03-16 16:50:26 -070029#
30# Copyright (c) 2018 Facebook, Inc.
31# Licensed under the Apache License, Version 2.0 (the "License")
32#
33# 16-Mar-2018 Howard McLauchlan Created this.
34
35import argparse
Howard McLauchlanfb3c0a72018-04-13 14:00:15 -070036import re
Howard McLauchlanef4154b2018-03-16 16:50:26 -070037from bcc import BPF
38
39
40class Probe:
41 errno_mapping = {
42 "kmalloc": "-ENOMEM",
43 "bio": "-EIO",
44 }
45
46 @classmethod
Nicolas Saenz Julienned51219a2018-11-15 03:14:25 +010047 def configure(cls, mode, probability, count):
Howard McLauchlanef4154b2018-03-16 16:50:26 -070048 cls.mode = mode
Howard McLauchlanb222f002018-04-10 13:05:47 -070049 cls.probability = probability
Nicolas Saenz Julienned51219a2018-11-15 03:14:25 +010050 cls.count = count
Howard McLauchlanef4154b2018-03-16 16:50:26 -070051
52 def __init__(self, func, preds, length, entry):
53 # length of call chain
54 self.length = length
55 self.func = func
56 self.preds = preds
57 self.is_entry = entry
58
59 def _bail(self, err):
60 raise ValueError("error in probe '%s': %s" %
61 (self.spec, err))
62
63 def _get_err(self):
64 return Probe.errno_mapping[Probe.mode]
65
66 def _get_if_top(self):
67 # ordering guarantees that if this function is top, the last tup is top
68 chk = self.preds[0][1] == 0
69 if not chk:
70 return ""
71
Howard McLauchlanb222f002018-04-10 13:05:47 -070072 if Probe.probability == 1:
73 early_pred = "false"
74 else:
75 early_pred = "bpf_get_prandom_u32() > %s" % str(int((1<<32)*Probe.probability))
Howard McLauchlanef4154b2018-03-16 16:50:26 -070076 # init the map
77 # dont do an early exit here so the singular case works automatically
Howard McLauchlanb222f002018-04-10 13:05:47 -070078 # have an early exit for probability option
Howard McLauchlanef4154b2018-03-16 16:50:26 -070079 enter = """
80 /*
Howard McLauchlanb222f002018-04-10 13:05:47 -070081 * Early exit for probability case
82 */
83 if (%s)
84 return 0;
85 /*
Howard McLauchlanef4154b2018-03-16 16:50:26 -070086 * Top level function init map
87 */
88 struct pid_struct p_struct = {0, 0};
89 m.insert(&pid, &p_struct);
Howard McLauchlanb222f002018-04-10 13:05:47 -070090 """ % early_pred
Howard McLauchlanef4154b2018-03-16 16:50:26 -070091
92 # kill the entry
93 exit = """
94 /*
95 * Top level function clean up map
96 */
97 m.delete(&pid);
98 """
99
100 return enter if self.is_entry else exit
101
102 def _get_heading(self):
103
104 # we need to insert identifier and ctx into self.func
105 # gonna make a lot of formatting assumptions to make this work
106 left = self.func.find("(")
107 right = self.func.rfind(")")
108
109 # self.event and self.func_name need to be accessible
110 self.event = self.func[0:left]
111 self.func_name = self.event + ("_entry" if self.is_entry else "_exit")
112 func_sig = "struct pt_regs *ctx"
113
114 # assume theres something in there, no guarantee its well formed
115 if right > left + 1 and self.is_entry:
116 func_sig += ", " + self.func[left + 1:right]
117
118 return "int %s(%s)" % (self.func_name, func_sig)
119
120 def _get_entry_logic(self):
121 # there is at least one tup(pred, place) for this function
122 text = """
123
124 if (p->conds_met >= %s)
125 return 0;
126 if (p->conds_met == %s && %s) {
127 p->stack[%s] = p->curr_call;
128 p->conds_met++;
129 }"""
130 text = text % (self.length, self.preds[0][1], self.preds[0][0],
131 self.preds[0][1])
132
133 # for each additional pred
134 for tup in self.preds[1:]:
135 text += """
136 else if (p->conds_met == %s && %s) {
137 p->stack[%s] = p->curr_call;
138 p->conds_met++;
139 }
140 """ % (tup[1], tup[0], tup[1])
141 return text
142
143 def _generate_entry(self):
144 prog = self._get_heading() + """
145{
146 u32 pid = bpf_get_current_pid_tgid();
147 %s
148
149 struct pid_struct *p = m.lookup(&pid);
150
151 if (!p)
152 return 0;
153
154 /*
155 * preparation for predicate, if necessary
156 */
157 %s
158 /*
159 * Generate entry logic
160 */
161 %s
162
163 p->curr_call++;
164
165 return 0;
166}"""
167
168 prog = prog % (self._get_if_top(), self.prep, self._get_entry_logic())
169 return prog
170
171 # only need to check top of stack
172 def _get_exit_logic(self):
173 text = """
174 if (p->conds_met < 1 || p->conds_met >= %s)
175 return 0;
176
177 if (p->stack[p->conds_met - 1] == p->curr_call)
178 p->conds_met--;
179 """
180 return text % str(self.length + 1)
181
182 def _generate_exit(self):
183 prog = self._get_heading() + """
184{
185 u32 pid = bpf_get_current_pid_tgid();
186
187 struct pid_struct *p = m.lookup(&pid);
188
189 if (!p)
190 return 0;
191
192 p->curr_call--;
193
194 /*
195 * Generate exit logic
196 */
197 %s
198 %s
199 return 0;
200}"""
201
202 prog = prog % (self._get_exit_logic(), self._get_if_top())
203
204 return prog
205
206 # Special case for should_fail_whatever
207 def _generate_bottom(self):
208 pred = self.preds[0][0]
209 text = self._get_heading() + """
210{
Nicolas Saenz Julienned51219a2018-11-15 03:14:25 +0100211 u32 overriden = 0;
212 int zero = 0;
213 u32* val;
214
215 val = count.lookup(&zero);
216 if (val)
217 overriden = *val;
218
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700219 /*
220 * preparation for predicate, if necessary
221 */
222 %s
223 /*
224 * If this is the only call in the chain and predicate passes
225 */
Nicolas Saenz Julienned51219a2018-11-15 03:14:25 +0100226 if (%s == 1 && %s && overriden < %s) {
227 count.increment(zero);
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700228 bpf_override_return(ctx, %s);
229 return 0;
230 }
231 u32 pid = bpf_get_current_pid_tgid();
232
233 struct pid_struct *p = m.lookup(&pid);
234
235 if (!p)
236 return 0;
237
238 /*
239 * If all conds have been met and predicate passes
240 */
Nicolas Saenz Julienned51219a2018-11-15 03:14:25 +0100241 if (p->conds_met == %s && %s && overriden < %s) {
242 count.increment(zero);
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700243 bpf_override_return(ctx, %s);
Nicolas Saenz Julienned51219a2018-11-15 03:14:25 +0100244 }
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700245 return 0;
Howard McLauchlanb222f002018-04-10 13:05:47 -0700246}"""
Nicolas Saenz Julienned51219a2018-11-15 03:14:25 +0100247 return text % (self.prep, self.length, pred, Probe.count,
248 self._get_err(), self.length - 1, pred, Probe.count,
249 self._get_err())
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700250
251 # presently parses and replaces STRCMP
252 # STRCMP exists because string comparison is inconvenient and somewhat buggy
253 # https://github.com/iovisor/bcc/issues/1617
254 def _prepare_pred(self):
255 self.prep = ""
256 for i in range(len(self.preds)):
257 new_pred = ""
258 pred = self.preds[i][0]
259 place = self.preds[i][1]
260 start, ind = 0, 0
261 while start < len(pred):
262 ind = pred.find("STRCMP(", start)
263 if ind == -1:
264 break
265 new_pred += pred[start:ind]
266 # 7 is len("STRCMP(")
267 start = pred.find(")", start + 7) + 1
268
269 # then ind ... start is STRCMP(...)
270 ptr, literal = pred[ind + 7:start - 1].split(",")
271 literal = literal.strip()
272
273 # x->y->z, some string literal
274 # we make unique id with place_ind
275 uuid = "%s_%s" % (place, ind)
276 unique_bool = "is_true_%s" % uuid
277 self.prep += """
278 char *str_%s = %s;
279 bool %s = true;\n""" % (uuid, ptr.strip(), unique_bool)
280
281 check = "\t%s &= *(str_%s++) == '%%s';\n" % (unique_bool, uuid)
282
283 for ch in literal:
284 self.prep += check % ch
285 self.prep += check % r'\0'
286 new_pred += unique_bool
287
288 new_pred += pred[start:]
289 self.preds[i] = (new_pred, place)
290
291 def generate_program(self):
292 # generate code to work around various rewriter issues
293 self._prepare_pred()
294
295 # special case for bottom
296 if self.preds[-1][1] == self.length - 1:
297 return self._generate_bottom()
298
299 return self._generate_entry() if self.is_entry else self._generate_exit()
300
301 def attach(self, bpf):
302 if self.is_entry:
303 bpf.attach_kprobe(event=self.event,
304 fn_name=self.func_name)
305 else:
306 bpf.attach_kretprobe(event=self.event,
307 fn_name=self.func_name)
308
309
310class Tool:
Howard McLauchlan45bcfb72018-04-13 14:19:15 -0700311
312 examples ="""
313EXAMPLES:
314# ./inject.py kmalloc -v 'SyS_mount()'
315 Fails all calls to syscall mount
316# ./inject.py kmalloc -v '(true) => SyS_mount()(true)'
317 Explicit rewriting of above
318# ./inject.py kmalloc -v 'mount_subtree() => btrfs_mount()'
319 Fails btrfs mounts only
320# ./inject.py kmalloc -v 'd_alloc_parallel(struct dentry *parent, const struct \\
321 qstr *name)(STRCMP(name->name, 'bananas'))'
322 Fails dentry allocations of files named 'bananas'
323# ./inject.py kmalloc -v -P 0.01 'SyS_mount()'
324 Fails calls to syscall mount with 1% probability
325 """
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700326 # add cases as necessary
327 error_injection_mapping = {
328 "kmalloc": "should_failslab(struct kmem_cache *s, gfp_t gfpflags)",
329 "bio": "should_fail_bio(struct bio *bio)",
330 }
331
332 def __init__(self):
333 parser = argparse.ArgumentParser(description="Fail specified kernel" +
334 " functionality when call chain and predicates are met",
Howard McLauchlan45bcfb72018-04-13 14:19:15 -0700335 formatter_class=argparse.RawDescriptionHelpFormatter,
336 epilog=Tool.examples)
337 parser.add_argument(dest="mode", choices=['kmalloc','bio'],
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700338 help="indicate which base kernel function to fail")
339 parser.add_argument(metavar="spec", dest="spec",
340 help="specify call chain")
341 parser.add_argument("-I", "--include", action="append",
342 metavar="header",
343 help="additional header files to include in the BPF program")
Howard McLauchlanb222f002018-04-10 13:05:47 -0700344 parser.add_argument("-P", "--probability", default=1,
345 metavar="probability", type=float,
346 help="probability that this call chain will fail")
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700347 parser.add_argument("-v", "--verbose", action="store_true",
Howard McLauchlan45bcfb72018-04-13 14:19:15 -0700348 help="print BPF program")
Nicolas Saenz Julienned51219a2018-11-15 03:14:25 +0100349 parser.add_argument("-c", "--count", action="store", default=-1,
350 help="Number of fails before bypassing the override")
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700351 self.args = parser.parse_args()
352
353 self.program = ""
354 self.spec = self.args.spec
355 self.map = {}
356 self.probes = []
357 self.key = Tool.error_injection_mapping[self.args.mode]
358
359 # create_probes and associated stuff
360 def _create_probes(self):
361 self._parse_spec()
Nicolas Saenz Julienned51219a2018-11-15 03:14:25 +0100362 Probe.configure(self.args.mode, self.args.probability, self.args.count)
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700363 # self, func, preds, total, entry
364
365 # create all the pair probes
366 for fx, preds in self.map.items():
367
368 # do the enter
369 self.probes.append(Probe(fx, preds, self.length, True))
370
371 if self.key == fx:
372 continue
373
374 # do the exit
375 self.probes.append(Probe(fx, preds, self.length, False))
376
377 def _parse_frames(self):
378 # sentinel
379 data = self.spec + '\0'
380 start, count = 0, 0
381
382 frames = []
383 cur_frame = []
384 i = 0
Howard McLauchlanfb3c0a72018-04-13 14:00:15 -0700385 last_frame_added = 0
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700386
387 while i < len(data):
388 # improper input
389 if count < 0:
390 raise Exception("Check your parentheses")
391 c = data[i]
392 count += c == '('
393 count -= c == ')'
394 if not count:
Howard McLauchlan26882342018-03-21 15:29:39 -0700395 if c == '\0' or (c == '=' and data[i + 1] == '>'):
Howard McLauchlanfb3c0a72018-04-13 14:00:15 -0700396 # This block is closing a chunk. This means cur_frame must
397 # have something in it.
398 if not cur_frame:
399 raise Exception("Cannot parse spec, missing parens")
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700400 if len(cur_frame) == 2:
401 frame = tuple(cur_frame)
402 elif cur_frame[0][0] == '(':
403 frame = self.key, cur_frame[0]
404 else:
405 frame = cur_frame[0], '(true)'
406 frames.append(frame)
407 del cur_frame[:]
408 i += 1
409 start = i + 1
410 elif c == ')':
411 cur_frame.append(data[start:i + 1].strip())
412 start = i + 1
Howard McLauchlanfb3c0a72018-04-13 14:00:15 -0700413 last_frame_added = start
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700414 i += 1
Howard McLauchlanfb3c0a72018-04-13 14:00:15 -0700415
416 # We only permit spaces after the last frame
417 if self.spec[last_frame_added:].strip():
418 raise Exception("Invalid characters found after last frame");
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700419 # improper input
420 if count:
421 raise Exception("Check your parentheses")
422 return frames
423
424 def _parse_spec(self):
425 frames = self._parse_frames()
426 frames.reverse()
427
428 absolute_order = 0
429 for f in frames:
430 # default case
431 func, pred = f[0], f[1]
432
433 if not self._validate_predicate(pred):
Howard McLauchlanfb3c0a72018-04-13 14:00:15 -0700434 raise Exception("Invalid predicate")
435 if not self._validate_identifier(func):
436 raise Exception("Invalid function identifier")
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700437 tup = (pred, absolute_order)
438
439 if func not in self.map:
440 self.map[func] = [tup]
441 else:
442 self.map[func].append(tup)
443
444 absolute_order += 1
445
446 if self.key not in self.map:
447 self.map[self.key] = [('(true)', absolute_order)]
448 absolute_order += 1
449
450 self.length = absolute_order
451
Howard McLauchlanfb3c0a72018-04-13 14:00:15 -0700452 def _validate_identifier(self, func):
453 # We've already established paren balancing. We will only look for
454 # identifier validity here.
455 paren_index = func.find("(")
456 potential_id = func[:paren_index]
457 pattern = '[_a-zA-z][_a-zA-Z0-9]*$'
458 if re.match(pattern, potential_id):
459 return True
460 return False
461
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700462 def _validate_predicate(self, pred):
463
464 if len(pred) > 0 and pred[0] == "(":
465 open = 1
466 for i in range(1, len(pred)):
467 if pred[i] == "(":
468 open += 1
469 elif pred[i] == ")":
470 open -= 1
471 if open != 0:
472 # not well formed, break
473 return False
474
475 return True
476
477 def _def_pid_struct(self):
478 text = """
479struct pid_struct {
480 u64 curr_call; /* book keeping to handle recursion */
481 u64 conds_met; /* stack pointer */
482 u64 stack[%s];
483};
484""" % self.length
485 return text
486
487 def _attach_probes(self):
488 self.bpf = BPF(text=self.program)
489 for p in self.probes:
490 p.attach(self.bpf)
491
492 def _generate_program(self):
493 # leave out auto includes for now
Howard McLauchlanb222f002018-04-10 13:05:47 -0700494 self.program += '#include <linux/mm.h>\n'
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700495 for include in (self.args.include or []):
496 self.program += "#include <%s>\n" % include
497
498 self.program += self._def_pid_struct()
499 self.program += "BPF_HASH(m, u32, struct pid_struct);\n"
Nicolas Saenz Julienned51219a2018-11-15 03:14:25 +0100500 self.program += "BPF_ARRAY(count, u32, 1);\n"
501
Howard McLauchlanef4154b2018-03-16 16:50:26 -0700502 for p in self.probes:
503 self.program += p.generate_program() + "\n"
504
505 if self.args.verbose:
506 print(self.program)
507
508 def _main_loop(self):
509 while True:
510 self.bpf.perf_buffer_poll()
511
512 def run(self):
513 self._create_probes()
514 self._generate_program()
515 self._attach_probes()
516 self._main_loop()
517
518
519if __name__ == "__main__":
520 Tool().run()