1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 */
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/bpf.h>
16 #include <linux/filter.h>
17 #include <net/netlink.h>
18 #include <linux/file.h>
19 #include <linux/vmalloc.h>
20
21 /* bpf_check() is a static code analyzer that walks eBPF program
22 * instruction by instruction and updates register/stack state.
23 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
24 *
25 * The first pass is depth-first-search to check that the program is a DAG.
26 * It rejects the following programs:
27 * - larger than BPF_MAXINSNS insns
28 * - if loop is present (detected via back-edge)
29 * - unreachable insns exist (shouldn't be a forest. program = one function)
30 * - out of bounds or malformed jumps
31 * The second pass is all possible path descent from the 1st insn.
32 * Since it's analyzing all pathes through the program, the length of the
33 * analysis is limited to 32k insn, which may be hit even if total number of
34 * insn is less then 4K, but there are too many branches that change stack/regs.
35 * Number of 'branches to be analyzed' is limited to 1k
36 *
37 * On entry to each instruction, each register has a type, and the instruction
38 * changes the types of the registers depending on instruction semantics.
39 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
40 * copied to R1.
41 *
42 * All registers are 64-bit.
43 * R0 - return register
44 * R1-R5 argument passing registers
45 * R6-R9 callee saved registers
46 * R10 - frame pointer read-only
47 *
48 * At the start of BPF program the register R1 contains a pointer to bpf_context
49 * and has type PTR_TO_CTX.
50 *
51 * Verifier tracks arithmetic operations on pointers in case:
52 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
53 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
54 * 1st insn copies R10 (which has FRAME_PTR) type into R1
55 * and 2nd arithmetic instruction is pattern matched to recognize
56 * that it wants to construct a pointer to some element within stack.
57 * So after 2nd insn, the register R1 has type PTR_TO_STACK
58 * (and -20 constant is saved for further stack bounds checking).
59 * Meaning that this reg is a pointer to stack plus known immediate constant.
60 *
61 * Most of the time the registers have UNKNOWN_VALUE type, which
62 * means the register has some value, but it's not a valid pointer.
63 * (like pointer plus pointer becomes UNKNOWN_VALUE type)
64 *
65 * When verifier sees load or store instructions the type of base register
66 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, FRAME_PTR. These are three pointer
67 * types recognized by check_mem_access() function.
68 *
69 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
70 * and the range of [ptr, ptr + map's value_size) is accessible.
71 *
72 * registers used to pass values to function calls are checked against
73 * function argument constraints.
74 *
75 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
76 * It means that the register type passed to this function must be
77 * PTR_TO_STACK and it will be used inside the function as
78 * 'pointer to map element key'
79 *
80 * For example the argument constraints for bpf_map_lookup_elem():
81 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
82 * .arg1_type = ARG_CONST_MAP_PTR,
83 * .arg2_type = ARG_PTR_TO_MAP_KEY,
84 *
85 * ret_type says that this function returns 'pointer to map elem value or null'
86 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
87 * 2nd argument should be a pointer to stack, which will be used inside
88 * the helper function as a pointer to map element key.
89 *
90 * On the kernel side the helper function looks like:
91 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
92 * {
93 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
94 * void *key = (void *) (unsigned long) r2;
95 * void *value;
96 *
97 * here kernel can access 'key' and 'map' pointers safely, knowing that
98 * [key, key + map->key_size) bytes are valid and were initialized on
99 * the stack of eBPF program.
100 * }
101 *
102 * Corresponding eBPF program may look like:
103 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
104 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
105 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
106 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
107 * here verifier looks at prototype of map_lookup_elem() and sees:
108 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
109 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
110 *
111 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
112 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
113 * and were initialized prior to this call.
114 * If it's ok, then verifier allows this BPF_CALL insn and looks at
115 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
116 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
117 * returns ether pointer to map value or NULL.
118 *
119 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
120 * insn, the register holding that pointer in the true branch changes state to
121 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
122 * branch. See check_cond_jmp_op().
123 *
124 * After the call R0 is set to return type of the function and registers R1-R5
125 * are set to NOT_INIT to indicate that they are no longer readable.
126 */
127
128 /* types of values stored in eBPF registers */
129 enum bpf_reg_type {
130 NOT_INIT = 0, /* nothing was written into register */
131 UNKNOWN_VALUE, /* reg doesn't contain a valid pointer */
132 PTR_TO_CTX, /* reg points to bpf_context */
133 CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
134 PTR_TO_MAP_VALUE, /* reg points to map element value */
135 PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
136 FRAME_PTR, /* reg == frame_pointer */
137 PTR_TO_STACK, /* reg == frame_pointer + imm */
138 CONST_IMM, /* constant integer value */
139 };
140
141 struct reg_state {
142 enum bpf_reg_type type;
143 union {
144 /* valid when type == CONST_IMM | PTR_TO_STACK */
145 int imm;
146
147 /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
148 * PTR_TO_MAP_VALUE_OR_NULL
149 */
150 struct bpf_map *map_ptr;
151 };
152 };
153
154 enum bpf_stack_slot_type {
155 STACK_INVALID, /* nothing was stored in this stack slot */
156 STACK_SPILL, /* 1st byte of register spilled into stack */
157 STACK_SPILL_PART, /* other 7 bytes of register spill */
158 STACK_MISC /* BPF program wrote some data into this slot */
159 };
160
161 struct bpf_stack_slot {
162 enum bpf_stack_slot_type stype;
163 struct reg_state reg_st;
164 };
165
166 /* state of the program:
167 * type of all registers and stack info
168 */
169 struct verifier_state {
170 struct reg_state regs[MAX_BPF_REG];
171 struct bpf_stack_slot stack[MAX_BPF_STACK];
172 };
173
174 /* linked list of verifier states used to prune search */
175 struct verifier_state_list {
176 struct verifier_state state;
177 struct verifier_state_list *next;
178 };
179
180 /* verifier_state + insn_idx are pushed to stack when branch is encountered */
181 struct verifier_stack_elem {
182 /* verifer state is 'st'
183 * before processing instruction 'insn_idx'
184 * and after processing instruction 'prev_insn_idx'
185 */
186 struct verifier_state st;
187 int insn_idx;
188 int prev_insn_idx;
189 struct verifier_stack_elem *next;
190 };
191
192 #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
193
194 /* single container for all structs
195 * one verifier_env per bpf_check() call
196 */
197 struct verifier_env {
198 struct bpf_prog *prog; /* eBPF program being verified */
199 struct verifier_stack_elem *head; /* stack of verifier states to be processed */
200 int stack_size; /* number of states to be processed */
201 struct verifier_state cur_state; /* current verifier state */
202 struct verifier_state_list **explored_states; /* search pruning optimization */
203 struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
204 u32 used_map_cnt; /* number of used maps */
205 };
206
207 /* verbose verifier prints what it's seeing
208 * bpf_check() is called under lock, so no race to access these global vars
209 */
210 static u32 log_level, log_size, log_len;
211 static char *log_buf;
212
213 static DEFINE_MUTEX(bpf_verifier_lock);
214
215 /* log_level controls verbosity level of eBPF verifier.
216 * verbose() is used to dump the verification trace to the log, so the user
217 * can figure out what's wrong with the program
218 */
verbose(const char * fmt,...)219 static void verbose(const char *fmt, ...)
220 {
221 va_list args;
222
223 if (log_level == 0 || log_len >= log_size - 1)
224 return;
225
226 va_start(args, fmt);
227 log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args);
228 va_end(args);
229 }
230
231 /* string representation of 'enum bpf_reg_type' */
232 static const char * const reg_type_str[] = {
233 [NOT_INIT] = "?",
234 [UNKNOWN_VALUE] = "inv",
235 [PTR_TO_CTX] = "ctx",
236 [CONST_PTR_TO_MAP] = "map_ptr",
237 [PTR_TO_MAP_VALUE] = "map_value",
238 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
239 [FRAME_PTR] = "fp",
240 [PTR_TO_STACK] = "fp",
241 [CONST_IMM] = "imm",
242 };
243
print_verifier_state(struct verifier_env * env)244 static void print_verifier_state(struct verifier_env *env)
245 {
246 enum bpf_reg_type t;
247 int i;
248
249 for (i = 0; i < MAX_BPF_REG; i++) {
250 t = env->cur_state.regs[i].type;
251 if (t == NOT_INIT)
252 continue;
253 verbose(" R%d=%s", i, reg_type_str[t]);
254 if (t == CONST_IMM || t == PTR_TO_STACK)
255 verbose("%d", env->cur_state.regs[i].imm);
256 else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE ||
257 t == PTR_TO_MAP_VALUE_OR_NULL)
258 verbose("(ks=%d,vs=%d)",
259 env->cur_state.regs[i].map_ptr->key_size,
260 env->cur_state.regs[i].map_ptr->value_size);
261 }
262 for (i = 0; i < MAX_BPF_STACK; i++) {
263 if (env->cur_state.stack[i].stype == STACK_SPILL)
264 verbose(" fp%d=%s", -MAX_BPF_STACK + i,
265 reg_type_str[env->cur_state.stack[i].reg_st.type]);
266 }
267 verbose("\n");
268 }
269
270 static const char *const bpf_class_string[] = {
271 [BPF_LD] = "ld",
272 [BPF_LDX] = "ldx",
273 [BPF_ST] = "st",
274 [BPF_STX] = "stx",
275 [BPF_ALU] = "alu",
276 [BPF_JMP] = "jmp",
277 [BPF_RET] = "BUG",
278 [BPF_ALU64] = "alu64",
279 };
280
281 static const char *const bpf_alu_string[] = {
282 [BPF_ADD >> 4] = "+=",
283 [BPF_SUB >> 4] = "-=",
284 [BPF_MUL >> 4] = "*=",
285 [BPF_DIV >> 4] = "/=",
286 [BPF_OR >> 4] = "|=",
287 [BPF_AND >> 4] = "&=",
288 [BPF_LSH >> 4] = "<<=",
289 [BPF_RSH >> 4] = ">>=",
290 [BPF_NEG >> 4] = "neg",
291 [BPF_MOD >> 4] = "%=",
292 [BPF_XOR >> 4] = "^=",
293 [BPF_MOV >> 4] = "=",
294 [BPF_ARSH >> 4] = "s>>=",
295 [BPF_END >> 4] = "endian",
296 };
297
298 static const char *const bpf_ldst_string[] = {
299 [BPF_W >> 3] = "u32",
300 [BPF_H >> 3] = "u16",
301 [BPF_B >> 3] = "u8",
302 [BPF_DW >> 3] = "u64",
303 };
304
305 static const char *const bpf_jmp_string[] = {
306 [BPF_JA >> 4] = "jmp",
307 [BPF_JEQ >> 4] = "==",
308 [BPF_JGT >> 4] = ">",
309 [BPF_JGE >> 4] = ">=",
310 [BPF_JSET >> 4] = "&",
311 [BPF_JNE >> 4] = "!=",
312 [BPF_JSGT >> 4] = "s>",
313 [BPF_JSGE >> 4] = "s>=",
314 [BPF_CALL >> 4] = "call",
315 [BPF_EXIT >> 4] = "exit",
316 };
317
print_bpf_insn(struct bpf_insn * insn)318 static void print_bpf_insn(struct bpf_insn *insn)
319 {
320 u8 class = BPF_CLASS(insn->code);
321
322 if (class == BPF_ALU || class == BPF_ALU64) {
323 if (BPF_SRC(insn->code) == BPF_X)
324 verbose("(%02x) %sr%d %s %sr%d\n",
325 insn->code, class == BPF_ALU ? "(u32) " : "",
326 insn->dst_reg,
327 bpf_alu_string[BPF_OP(insn->code) >> 4],
328 class == BPF_ALU ? "(u32) " : "",
329 insn->src_reg);
330 else
331 verbose("(%02x) %sr%d %s %s%d\n",
332 insn->code, class == BPF_ALU ? "(u32) " : "",
333 insn->dst_reg,
334 bpf_alu_string[BPF_OP(insn->code) >> 4],
335 class == BPF_ALU ? "(u32) " : "",
336 insn->imm);
337 } else if (class == BPF_STX) {
338 if (BPF_MODE(insn->code) == BPF_MEM)
339 verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
340 insn->code,
341 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
342 insn->dst_reg,
343 insn->off, insn->src_reg);
344 else if (BPF_MODE(insn->code) == BPF_XADD)
345 verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
346 insn->code,
347 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
348 insn->dst_reg, insn->off,
349 insn->src_reg);
350 else
351 verbose("BUG_%02x\n", insn->code);
352 } else if (class == BPF_ST) {
353 if (BPF_MODE(insn->code) != BPF_MEM) {
354 verbose("BUG_st_%02x\n", insn->code);
355 return;
356 }
357 verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
358 insn->code,
359 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
360 insn->dst_reg,
361 insn->off, insn->imm);
362 } else if (class == BPF_LDX) {
363 if (BPF_MODE(insn->code) != BPF_MEM) {
364 verbose("BUG_ldx_%02x\n", insn->code);
365 return;
366 }
367 verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
368 insn->code, insn->dst_reg,
369 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
370 insn->src_reg, insn->off);
371 } else if (class == BPF_LD) {
372 if (BPF_MODE(insn->code) == BPF_ABS) {
373 verbose("(%02x) r0 = *(%s *)skb[%d]\n",
374 insn->code,
375 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
376 insn->imm);
377 } else if (BPF_MODE(insn->code) == BPF_IND) {
378 verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
379 insn->code,
380 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
381 insn->src_reg, insn->imm);
382 } else if (BPF_MODE(insn->code) == BPF_IMM) {
383 verbose("(%02x) r%d = 0x%x\n",
384 insn->code, insn->dst_reg, insn->imm);
385 } else {
386 verbose("BUG_ld_%02x\n", insn->code);
387 return;
388 }
389 } else if (class == BPF_JMP) {
390 u8 opcode = BPF_OP(insn->code);
391
392 if (opcode == BPF_CALL) {
393 verbose("(%02x) call %d\n", insn->code, insn->imm);
394 } else if (insn->code == (BPF_JMP | BPF_JA)) {
395 verbose("(%02x) goto pc%+d\n",
396 insn->code, insn->off);
397 } else if (insn->code == (BPF_JMP | BPF_EXIT)) {
398 verbose("(%02x) exit\n", insn->code);
399 } else if (BPF_SRC(insn->code) == BPF_X) {
400 verbose("(%02x) if r%d %s r%d goto pc%+d\n",
401 insn->code, insn->dst_reg,
402 bpf_jmp_string[BPF_OP(insn->code) >> 4],
403 insn->src_reg, insn->off);
404 } else {
405 verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
406 insn->code, insn->dst_reg,
407 bpf_jmp_string[BPF_OP(insn->code) >> 4],
408 insn->imm, insn->off);
409 }
410 } else {
411 verbose("(%02x) %s\n", insn->code, bpf_class_string[class]);
412 }
413 }
414
pop_stack(struct verifier_env * env,int * prev_insn_idx)415 static int pop_stack(struct verifier_env *env, int *prev_insn_idx)
416 {
417 struct verifier_stack_elem *elem;
418 int insn_idx;
419
420 if (env->head == NULL)
421 return -1;
422
423 memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
424 insn_idx = env->head->insn_idx;
425 if (prev_insn_idx)
426 *prev_insn_idx = env->head->prev_insn_idx;
427 elem = env->head->next;
428 kfree(env->head);
429 env->head = elem;
430 env->stack_size--;
431 return insn_idx;
432 }
433
push_stack(struct verifier_env * env,int insn_idx,int prev_insn_idx)434 static struct verifier_state *push_stack(struct verifier_env *env, int insn_idx,
435 int prev_insn_idx)
436 {
437 struct verifier_stack_elem *elem;
438
439 elem = kmalloc(sizeof(struct verifier_stack_elem), GFP_KERNEL);
440 if (!elem)
441 goto err;
442
443 memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state));
444 elem->insn_idx = insn_idx;
445 elem->prev_insn_idx = prev_insn_idx;
446 elem->next = env->head;
447 env->head = elem;
448 env->stack_size++;
449 if (env->stack_size > 1024) {
450 verbose("BPF program is too complex\n");
451 goto err;
452 }
453 return &elem->st;
454 err:
455 /* pop all elements and return */
456 while (pop_stack(env, NULL) >= 0);
457 return NULL;
458 }
459
460 #define CALLER_SAVED_REGS 6
461 static const int caller_saved[CALLER_SAVED_REGS] = {
462 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
463 };
464
init_reg_state(struct reg_state * regs)465 static void init_reg_state(struct reg_state *regs)
466 {
467 int i;
468
469 for (i = 0; i < MAX_BPF_REG; i++) {
470 regs[i].type = NOT_INIT;
471 regs[i].imm = 0;
472 regs[i].map_ptr = NULL;
473 }
474
475 /* frame pointer */
476 regs[BPF_REG_FP].type = FRAME_PTR;
477
478 /* 1st arg to a function */
479 regs[BPF_REG_1].type = PTR_TO_CTX;
480 }
481
mark_reg_unknown_value(struct reg_state * regs,u32 regno)482 static void mark_reg_unknown_value(struct reg_state *regs, u32 regno)
483 {
484 BUG_ON(regno >= MAX_BPF_REG);
485 regs[regno].type = UNKNOWN_VALUE;
486 regs[regno].imm = 0;
487 regs[regno].map_ptr = NULL;
488 }
489
490 enum reg_arg_type {
491 SRC_OP, /* register is used as source operand */
492 DST_OP, /* register is used as destination operand */
493 DST_OP_NO_MARK /* same as above, check only, don't mark */
494 };
495
check_reg_arg(struct reg_state * regs,u32 regno,enum reg_arg_type t)496 static int check_reg_arg(struct reg_state *regs, u32 regno,
497 enum reg_arg_type t)
498 {
499 if (regno >= MAX_BPF_REG) {
500 verbose("R%d is invalid\n", regno);
501 return -EINVAL;
502 }
503
504 if (t == SRC_OP) {
505 /* check whether register used as source operand can be read */
506 if (regs[regno].type == NOT_INIT) {
507 verbose("R%d !read_ok\n", regno);
508 return -EACCES;
509 }
510 } else {
511 /* check whether register used as dest operand can be written to */
512 if (regno == BPF_REG_FP) {
513 verbose("frame pointer is read only\n");
514 return -EACCES;
515 }
516 if (t == DST_OP)
517 mark_reg_unknown_value(regs, regno);
518 }
519 return 0;
520 }
521
bpf_size_to_bytes(int bpf_size)522 static int bpf_size_to_bytes(int bpf_size)
523 {
524 if (bpf_size == BPF_W)
525 return 4;
526 else if (bpf_size == BPF_H)
527 return 2;
528 else if (bpf_size == BPF_B)
529 return 1;
530 else if (bpf_size == BPF_DW)
531 return 8;
532 else
533 return -EINVAL;
534 }
535
536 /* check_stack_read/write functions track spill/fill of registers,
537 * stack boundary and alignment are checked in check_mem_access()
538 */
check_stack_write(struct verifier_state * state,int off,int size,int value_regno)539 static int check_stack_write(struct verifier_state *state, int off, int size,
540 int value_regno)
541 {
542 struct bpf_stack_slot *slot;
543 int i;
544
545 if (value_regno >= 0 &&
546 (state->regs[value_regno].type == PTR_TO_MAP_VALUE ||
547 state->regs[value_regno].type == PTR_TO_STACK ||
548 state->regs[value_regno].type == PTR_TO_CTX)) {
549
550 /* register containing pointer is being spilled into stack */
551 if (size != 8) {
552 verbose("invalid size of register spill\n");
553 return -EACCES;
554 }
555
556 slot = &state->stack[MAX_BPF_STACK + off];
557 slot->stype = STACK_SPILL;
558 /* save register state */
559 slot->reg_st = state->regs[value_regno];
560 for (i = 1; i < 8; i++) {
561 slot = &state->stack[MAX_BPF_STACK + off + i];
562 slot->stype = STACK_SPILL_PART;
563 slot->reg_st.type = UNKNOWN_VALUE;
564 slot->reg_st.map_ptr = NULL;
565 }
566 } else {
567
568 /* regular write of data into stack */
569 for (i = 0; i < size; i++) {
570 slot = &state->stack[MAX_BPF_STACK + off + i];
571 slot->stype = STACK_MISC;
572 slot->reg_st.type = UNKNOWN_VALUE;
573 slot->reg_st.map_ptr = NULL;
574 }
575 }
576 return 0;
577 }
578
check_stack_read(struct verifier_state * state,int off,int size,int value_regno)579 static int check_stack_read(struct verifier_state *state, int off, int size,
580 int value_regno)
581 {
582 int i;
583 struct bpf_stack_slot *slot;
584
585 slot = &state->stack[MAX_BPF_STACK + off];
586
587 if (slot->stype == STACK_SPILL) {
588 if (size != 8) {
589 verbose("invalid size of register spill\n");
590 return -EACCES;
591 }
592 for (i = 1; i < 8; i++) {
593 if (state->stack[MAX_BPF_STACK + off + i].stype !=
594 STACK_SPILL_PART) {
595 verbose("corrupted spill memory\n");
596 return -EACCES;
597 }
598 }
599
600 if (value_regno >= 0)
601 /* restore register state from stack */
602 state->regs[value_regno] = slot->reg_st;
603 return 0;
604 } else {
605 for (i = 0; i < size; i++) {
606 if (state->stack[MAX_BPF_STACK + off + i].stype !=
607 STACK_MISC) {
608 verbose("invalid read from stack off %d+%d size %d\n",
609 off, i, size);
610 return -EACCES;
611 }
612 }
613 if (value_regno >= 0)
614 /* have read misc data from the stack */
615 mark_reg_unknown_value(state->regs, value_regno);
616 return 0;
617 }
618 }
619
620 /* check read/write into map element returned by bpf_map_lookup_elem() */
check_map_access(struct verifier_env * env,u32 regno,int off,int size)621 static int check_map_access(struct verifier_env *env, u32 regno, int off,
622 int size)
623 {
624 struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
625
626 if (off < 0 || off + size > map->value_size) {
627 verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
628 map->value_size, off, size);
629 return -EACCES;
630 }
631 return 0;
632 }
633
634 /* check access to 'struct bpf_context' fields */
check_ctx_access(struct verifier_env * env,int off,int size,enum bpf_access_type t)635 static int check_ctx_access(struct verifier_env *env, int off, int size,
636 enum bpf_access_type t)
637 {
638 if (env->prog->aux->ops->is_valid_access &&
639 env->prog->aux->ops->is_valid_access(off, size, t))
640 return 0;
641
642 verbose("invalid bpf_context access off=%d size=%d\n", off, size);
643 return -EACCES;
644 }
645
646 /* check whether memory at (regno + off) is accessible for t = (read | write)
647 * if t==write, value_regno is a register which value is stored into memory
648 * if t==read, value_regno is a register which will receive the value from memory
649 * if t==write && value_regno==-1, some unknown value is stored into memory
650 * if t==read && value_regno==-1, don't care what we read from memory
651 */
check_mem_access(struct verifier_env * env,u32 regno,int off,int bpf_size,enum bpf_access_type t,int value_regno)652 static int check_mem_access(struct verifier_env *env, u32 regno, int off,
653 int bpf_size, enum bpf_access_type t,
654 int value_regno)
655 {
656 struct verifier_state *state = &env->cur_state;
657 int size, err = 0;
658
659 size = bpf_size_to_bytes(bpf_size);
660 if (size < 0)
661 return size;
662
663 if (off % size != 0) {
664 verbose("misaligned access off %d size %d\n", off, size);
665 return -EACCES;
666 }
667
668 if (state->regs[regno].type == PTR_TO_MAP_VALUE) {
669 err = check_map_access(env, regno, off, size);
670 if (!err && t == BPF_READ && value_regno >= 0)
671 mark_reg_unknown_value(state->regs, value_regno);
672
673 } else if (state->regs[regno].type == PTR_TO_CTX) {
674 err = check_ctx_access(env, off, size, t);
675 if (!err && t == BPF_READ && value_regno >= 0)
676 mark_reg_unknown_value(state->regs, value_regno);
677
678 } else if (state->regs[regno].type == FRAME_PTR) {
679 if (off >= 0 || off < -MAX_BPF_STACK) {
680 verbose("invalid stack off=%d size=%d\n", off, size);
681 return -EACCES;
682 }
683 if (t == BPF_WRITE)
684 err = check_stack_write(state, off, size, value_regno);
685 else
686 err = check_stack_read(state, off, size, value_regno);
687 } else {
688 verbose("R%d invalid mem access '%s'\n",
689 regno, reg_type_str[state->regs[regno].type]);
690 return -EACCES;
691 }
692 return err;
693 }
694
check_xadd(struct verifier_env * env,struct bpf_insn * insn)695 static int check_xadd(struct verifier_env *env, struct bpf_insn *insn)
696 {
697 struct reg_state *regs = env->cur_state.regs;
698 int err;
699
700 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
701 insn->imm != 0) {
702 verbose("BPF_XADD uses reserved fields\n");
703 return -EINVAL;
704 }
705
706 /* check src1 operand */
707 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
708 if (err)
709 return err;
710
711 /* check src2 operand */
712 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
713 if (err)
714 return err;
715
716 /* check whether atomic_add can read the memory */
717 err = check_mem_access(env, insn->dst_reg, insn->off,
718 BPF_SIZE(insn->code), BPF_READ, -1);
719 if (err)
720 return err;
721
722 /* check whether atomic_add can write into the same memory */
723 return check_mem_access(env, insn->dst_reg, insn->off,
724 BPF_SIZE(insn->code), BPF_WRITE, -1);
725 }
726
727 /* when register 'regno' is passed into function that will read 'access_size'
728 * bytes from that pointer, make sure that it's within stack boundary
729 * and all elements of stack are initialized
730 */
check_stack_boundary(struct verifier_env * env,int regno,int access_size)731 static int check_stack_boundary(struct verifier_env *env,
732 int regno, int access_size)
733 {
734 struct verifier_state *state = &env->cur_state;
735 struct reg_state *regs = state->regs;
736 int off, i;
737
738 if (regs[regno].type != PTR_TO_STACK)
739 return -EACCES;
740
741 off = regs[regno].imm;
742 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
743 access_size <= 0) {
744 verbose("invalid stack type R%d off=%d access_size=%d\n",
745 regno, off, access_size);
746 return -EACCES;
747 }
748
749 for (i = 0; i < access_size; i++) {
750 if (state->stack[MAX_BPF_STACK + off + i].stype != STACK_MISC) {
751 verbose("invalid indirect read from stack off %d+%d size %d\n",
752 off, i, access_size);
753 return -EACCES;
754 }
755 }
756 return 0;
757 }
758
check_func_arg(struct verifier_env * env,u32 regno,enum bpf_arg_type arg_type,struct bpf_map ** mapp)759 static int check_func_arg(struct verifier_env *env, u32 regno,
760 enum bpf_arg_type arg_type, struct bpf_map **mapp)
761 {
762 struct reg_state *reg = env->cur_state.regs + regno;
763 enum bpf_reg_type expected_type;
764 int err = 0;
765
766 if (arg_type == ARG_DONTCARE)
767 return 0;
768
769 if (reg->type == NOT_INIT) {
770 verbose("R%d !read_ok\n", regno);
771 return -EACCES;
772 }
773
774 if (arg_type == ARG_ANYTHING)
775 return 0;
776
777 if (arg_type == ARG_PTR_TO_STACK || arg_type == ARG_PTR_TO_MAP_KEY ||
778 arg_type == ARG_PTR_TO_MAP_VALUE) {
779 expected_type = PTR_TO_STACK;
780 } else if (arg_type == ARG_CONST_STACK_SIZE) {
781 expected_type = CONST_IMM;
782 } else if (arg_type == ARG_CONST_MAP_PTR) {
783 expected_type = CONST_PTR_TO_MAP;
784 } else {
785 verbose("unsupported arg_type %d\n", arg_type);
786 return -EFAULT;
787 }
788
789 if (reg->type != expected_type) {
790 verbose("R%d type=%s expected=%s\n", regno,
791 reg_type_str[reg->type], reg_type_str[expected_type]);
792 return -EACCES;
793 }
794
795 if (arg_type == ARG_CONST_MAP_PTR) {
796 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
797 *mapp = reg->map_ptr;
798
799 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
800 /* bpf_map_xxx(..., map_ptr, ..., key) call:
801 * check that [key, key + map->key_size) are within
802 * stack limits and initialized
803 */
804 if (!*mapp) {
805 /* in function declaration map_ptr must come before
806 * map_key, so that it's verified and known before
807 * we have to check map_key here. Otherwise it means
808 * that kernel subsystem misconfigured verifier
809 */
810 verbose("invalid map_ptr to access map->key\n");
811 return -EACCES;
812 }
813 err = check_stack_boundary(env, regno, (*mapp)->key_size);
814
815 } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
816 /* bpf_map_xxx(..., map_ptr, ..., value) call:
817 * check [value, value + map->value_size) validity
818 */
819 if (!*mapp) {
820 /* kernel subsystem misconfigured verifier */
821 verbose("invalid map_ptr to access map->value\n");
822 return -EACCES;
823 }
824 err = check_stack_boundary(env, regno, (*mapp)->value_size);
825
826 } else if (arg_type == ARG_CONST_STACK_SIZE) {
827 /* bpf_xxx(..., buf, len) call will access 'len' bytes
828 * from stack pointer 'buf'. Check it
829 * note: regno == len, regno - 1 == buf
830 */
831 if (regno == 0) {
832 /* kernel subsystem misconfigured verifier */
833 verbose("ARG_CONST_STACK_SIZE cannot be first argument\n");
834 return -EACCES;
835 }
836 err = check_stack_boundary(env, regno - 1, reg->imm);
837 }
838
839 return err;
840 }
841
check_call(struct verifier_env * env,int func_id)842 static int check_call(struct verifier_env *env, int func_id)
843 {
844 struct verifier_state *state = &env->cur_state;
845 const struct bpf_func_proto *fn = NULL;
846 struct reg_state *regs = state->regs;
847 struct bpf_map *map = NULL;
848 struct reg_state *reg;
849 int i, err;
850
851 /* find function prototype */
852 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
853 verbose("invalid func %d\n", func_id);
854 return -EINVAL;
855 }
856
857 if (env->prog->aux->ops->get_func_proto)
858 fn = env->prog->aux->ops->get_func_proto(func_id);
859
860 if (!fn) {
861 verbose("unknown func %d\n", func_id);
862 return -EINVAL;
863 }
864
865 /* eBPF programs must be GPL compatible to use GPL-ed functions */
866 if (!env->prog->aux->is_gpl_compatible && fn->gpl_only) {
867 verbose("cannot call GPL only function from proprietary program\n");
868 return -EINVAL;
869 }
870
871 /* check args */
872 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &map);
873 if (err)
874 return err;
875 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &map);
876 if (err)
877 return err;
878 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &map);
879 if (err)
880 return err;
881 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &map);
882 if (err)
883 return err;
884 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &map);
885 if (err)
886 return err;
887
888 /* reset caller saved regs */
889 for (i = 0; i < CALLER_SAVED_REGS; i++) {
890 reg = regs + caller_saved[i];
891 reg->type = NOT_INIT;
892 reg->imm = 0;
893 }
894
895 /* update return register */
896 if (fn->ret_type == RET_INTEGER) {
897 regs[BPF_REG_0].type = UNKNOWN_VALUE;
898 } else if (fn->ret_type == RET_VOID) {
899 regs[BPF_REG_0].type = NOT_INIT;
900 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
901 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
902 /* remember map_ptr, so that check_map_access()
903 * can check 'value_size' boundary of memory access
904 * to map element returned from bpf_map_lookup_elem()
905 */
906 if (map == NULL) {
907 verbose("kernel subsystem misconfigured verifier\n");
908 return -EINVAL;
909 }
910 regs[BPF_REG_0].map_ptr = map;
911 } else {
912 verbose("unknown return type %d of func %d\n",
913 fn->ret_type, func_id);
914 return -EINVAL;
915 }
916 return 0;
917 }
918
919 /* check validity of 32-bit and 64-bit arithmetic operations */
check_alu_op(struct reg_state * regs,struct bpf_insn * insn)920 static int check_alu_op(struct reg_state *regs, struct bpf_insn *insn)
921 {
922 u8 opcode = BPF_OP(insn->code);
923 int err;
924
925 if (opcode == BPF_END || opcode == BPF_NEG) {
926 if (opcode == BPF_NEG) {
927 if (BPF_SRC(insn->code) != 0 ||
928 insn->src_reg != BPF_REG_0 ||
929 insn->off != 0 || insn->imm != 0) {
930 verbose("BPF_NEG uses reserved fields\n");
931 return -EINVAL;
932 }
933 } else {
934 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
935 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
936 BPF_CLASS(insn->code) == BPF_ALU64) {
937 verbose("BPF_END uses reserved fields\n");
938 return -EINVAL;
939 }
940 }
941
942 /* check src operand */
943 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
944 if (err)
945 return err;
946
947 /* check dest operand */
948 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
949 if (err)
950 return err;
951
952 } else if (opcode == BPF_MOV) {
953
954 if (BPF_SRC(insn->code) == BPF_X) {
955 if (insn->imm != 0 || insn->off != 0) {
956 verbose("BPF_MOV uses reserved fields\n");
957 return -EINVAL;
958 }
959
960 /* check src operand */
961 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
962 if (err)
963 return err;
964 } else {
965 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
966 verbose("BPF_MOV uses reserved fields\n");
967 return -EINVAL;
968 }
969 }
970
971 /* check dest operand */
972 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
973 if (err)
974 return err;
975
976 if (BPF_SRC(insn->code) == BPF_X) {
977 if (BPF_CLASS(insn->code) == BPF_ALU64) {
978 /* case: R1 = R2
979 * copy register state to dest reg
980 */
981 regs[insn->dst_reg] = regs[insn->src_reg];
982 } else {
983 regs[insn->dst_reg].type = UNKNOWN_VALUE;
984 regs[insn->dst_reg].map_ptr = NULL;
985 }
986 } else {
987 /* case: R = imm
988 * remember the value we stored into this reg
989 */
990 regs[insn->dst_reg].type = CONST_IMM;
991 regs[insn->dst_reg].imm = insn->imm;
992 }
993
994 } else if (opcode > BPF_END) {
995 verbose("invalid BPF_ALU opcode %x\n", opcode);
996 return -EINVAL;
997
998 } else { /* all other ALU ops: and, sub, xor, add, ... */
999
1000 bool stack_relative = false;
1001
1002 if (BPF_SRC(insn->code) == BPF_X) {
1003 if (insn->imm != 0 || insn->off != 0) {
1004 verbose("BPF_ALU uses reserved fields\n");
1005 return -EINVAL;
1006 }
1007 /* check src1 operand */
1008 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1009 if (err)
1010 return err;
1011 } else {
1012 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1013 verbose("BPF_ALU uses reserved fields\n");
1014 return -EINVAL;
1015 }
1016 }
1017
1018 /* check src2 operand */
1019 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1020 if (err)
1021 return err;
1022
1023 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
1024 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
1025 verbose("div by zero\n");
1026 return -EINVAL;
1027 }
1028
1029 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
1030 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
1031 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
1032
1033 if (insn->imm < 0 || insn->imm >= size) {
1034 verbose("invalid shift %d\n", insn->imm);
1035 return -EINVAL;
1036 }
1037 }
1038
1039 /* pattern match 'bpf_add Rx, imm' instruction */
1040 if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
1041 regs[insn->dst_reg].type == FRAME_PTR &&
1042 BPF_SRC(insn->code) == BPF_K)
1043 stack_relative = true;
1044
1045 /* check dest operand */
1046 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1047 if (err)
1048 return err;
1049
1050 if (stack_relative) {
1051 regs[insn->dst_reg].type = PTR_TO_STACK;
1052 regs[insn->dst_reg].imm = insn->imm;
1053 }
1054 }
1055
1056 return 0;
1057 }
1058
check_cond_jmp_op(struct verifier_env * env,struct bpf_insn * insn,int * insn_idx)1059 static int check_cond_jmp_op(struct verifier_env *env,
1060 struct bpf_insn *insn, int *insn_idx)
1061 {
1062 struct reg_state *regs = env->cur_state.regs;
1063 struct verifier_state *other_branch;
1064 u8 opcode = BPF_OP(insn->code);
1065 int err;
1066
1067 if (opcode > BPF_EXIT) {
1068 verbose("invalid BPF_JMP opcode %x\n", opcode);
1069 return -EINVAL;
1070 }
1071
1072 if (BPF_SRC(insn->code) == BPF_X) {
1073 if (insn->imm != 0) {
1074 verbose("BPF_JMP uses reserved fields\n");
1075 return -EINVAL;
1076 }
1077
1078 /* check src1 operand */
1079 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1080 if (err)
1081 return err;
1082 } else {
1083 if (insn->src_reg != BPF_REG_0) {
1084 verbose("BPF_JMP uses reserved fields\n");
1085 return -EINVAL;
1086 }
1087 }
1088
1089 /* check src2 operand */
1090 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1091 if (err)
1092 return err;
1093
1094 /* detect if R == 0 where R was initialized to zero earlier */
1095 if (BPF_SRC(insn->code) == BPF_K &&
1096 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
1097 regs[insn->dst_reg].type == CONST_IMM &&
1098 regs[insn->dst_reg].imm == insn->imm) {
1099 if (opcode == BPF_JEQ) {
1100 /* if (imm == imm) goto pc+off;
1101 * only follow the goto, ignore fall-through
1102 */
1103 *insn_idx += insn->off;
1104 return 0;
1105 } else {
1106 /* if (imm != imm) goto pc+off;
1107 * only follow fall-through branch, since
1108 * that's where the program will go
1109 */
1110 return 0;
1111 }
1112 }
1113
1114 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
1115 if (!other_branch)
1116 return -EFAULT;
1117
1118 /* detect if R == 0 where R is returned value from bpf_map_lookup_elem() */
1119 if (BPF_SRC(insn->code) == BPF_K &&
1120 insn->imm == 0 && (opcode == BPF_JEQ ||
1121 opcode == BPF_JNE) &&
1122 regs[insn->dst_reg].type == PTR_TO_MAP_VALUE_OR_NULL) {
1123 if (opcode == BPF_JEQ) {
1124 /* next fallthrough insn can access memory via
1125 * this register
1126 */
1127 regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
1128 /* branch targer cannot access it, since reg == 0 */
1129 other_branch->regs[insn->dst_reg].type = CONST_IMM;
1130 other_branch->regs[insn->dst_reg].imm = 0;
1131 } else {
1132 other_branch->regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
1133 regs[insn->dst_reg].type = CONST_IMM;
1134 regs[insn->dst_reg].imm = 0;
1135 }
1136 } else if (BPF_SRC(insn->code) == BPF_K &&
1137 (opcode == BPF_JEQ || opcode == BPF_JNE)) {
1138
1139 if (opcode == BPF_JEQ) {
1140 /* detect if (R == imm) goto
1141 * and in the target state recognize that R = imm
1142 */
1143 other_branch->regs[insn->dst_reg].type = CONST_IMM;
1144 other_branch->regs[insn->dst_reg].imm = insn->imm;
1145 } else {
1146 /* detect if (R != imm) goto
1147 * and in the fall-through state recognize that R = imm
1148 */
1149 regs[insn->dst_reg].type = CONST_IMM;
1150 regs[insn->dst_reg].imm = insn->imm;
1151 }
1152 }
1153 if (log_level)
1154 print_verifier_state(env);
1155 return 0;
1156 }
1157
1158 /* return the map pointer stored inside BPF_LD_IMM64 instruction */
ld_imm64_to_map_ptr(struct bpf_insn * insn)1159 static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
1160 {
1161 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
1162
1163 return (struct bpf_map *) (unsigned long) imm64;
1164 }
1165
1166 /* verify BPF_LD_IMM64 instruction */
check_ld_imm(struct verifier_env * env,struct bpf_insn * insn)1167 static int check_ld_imm(struct verifier_env *env, struct bpf_insn *insn)
1168 {
1169 struct reg_state *regs = env->cur_state.regs;
1170 int err;
1171
1172 if (BPF_SIZE(insn->code) != BPF_DW) {
1173 verbose("invalid BPF_LD_IMM insn\n");
1174 return -EINVAL;
1175 }
1176 if (insn->off != 0) {
1177 verbose("BPF_LD_IMM64 uses reserved fields\n");
1178 return -EINVAL;
1179 }
1180
1181 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1182 if (err)
1183 return err;
1184
1185 if (insn->src_reg == 0)
1186 /* generic move 64-bit immediate into a register */
1187 return 0;
1188
1189 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
1190 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
1191
1192 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
1193 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
1194 return 0;
1195 }
1196
1197 /* non-recursive DFS pseudo code
1198 * 1 procedure DFS-iterative(G,v):
1199 * 2 label v as discovered
1200 * 3 let S be a stack
1201 * 4 S.push(v)
1202 * 5 while S is not empty
1203 * 6 t <- S.pop()
1204 * 7 if t is what we're looking for:
1205 * 8 return t
1206 * 9 for all edges e in G.adjacentEdges(t) do
1207 * 10 if edge e is already labelled
1208 * 11 continue with the next edge
1209 * 12 w <- G.adjacentVertex(t,e)
1210 * 13 if vertex w is not discovered and not explored
1211 * 14 label e as tree-edge
1212 * 15 label w as discovered
1213 * 16 S.push(w)
1214 * 17 continue at 5
1215 * 18 else if vertex w is discovered
1216 * 19 label e as back-edge
1217 * 20 else
1218 * 21 // vertex w is explored
1219 * 22 label e as forward- or cross-edge
1220 * 23 label t as explored
1221 * 24 S.pop()
1222 *
1223 * convention:
1224 * 0x10 - discovered
1225 * 0x11 - discovered and fall-through edge labelled
1226 * 0x12 - discovered and fall-through and branch edges labelled
1227 * 0x20 - explored
1228 */
1229
1230 enum {
1231 DISCOVERED = 0x10,
1232 EXPLORED = 0x20,
1233 FALLTHROUGH = 1,
1234 BRANCH = 2,
1235 };
1236
1237 #define STATE_LIST_MARK ((struct verifier_state_list *) -1L)
1238
1239 static int *insn_stack; /* stack of insns to process */
1240 static int cur_stack; /* current stack index */
1241 static int *insn_state;
1242
1243 /* t, w, e - match pseudo-code above:
1244 * t - index of current instruction
1245 * w - next instruction
1246 * e - edge
1247 */
push_insn(int t,int w,int e,struct verifier_env * env)1248 static int push_insn(int t, int w, int e, struct verifier_env *env)
1249 {
1250 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
1251 return 0;
1252
1253 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
1254 return 0;
1255
1256 if (w < 0 || w >= env->prog->len) {
1257 verbose("jump out of range from insn %d to %d\n", t, w);
1258 return -EINVAL;
1259 }
1260
1261 if (e == BRANCH)
1262 /* mark branch target for state pruning */
1263 env->explored_states[w] = STATE_LIST_MARK;
1264
1265 if (insn_state[w] == 0) {
1266 /* tree-edge */
1267 insn_state[t] = DISCOVERED | e;
1268 insn_state[w] = DISCOVERED;
1269 if (cur_stack >= env->prog->len)
1270 return -E2BIG;
1271 insn_stack[cur_stack++] = w;
1272 return 1;
1273 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
1274 verbose("back-edge from insn %d to %d\n", t, w);
1275 return -EINVAL;
1276 } else if (insn_state[w] == EXPLORED) {
1277 /* forward- or cross-edge */
1278 insn_state[t] = DISCOVERED | e;
1279 } else {
1280 verbose("insn state internal bug\n");
1281 return -EFAULT;
1282 }
1283 return 0;
1284 }
1285
1286 /* non-recursive depth-first-search to detect loops in BPF program
1287 * loop == back-edge in directed graph
1288 */
check_cfg(struct verifier_env * env)1289 static int check_cfg(struct verifier_env *env)
1290 {
1291 struct bpf_insn *insns = env->prog->insnsi;
1292 int insn_cnt = env->prog->len;
1293 int ret = 0;
1294 int i, t;
1295
1296 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
1297 if (!insn_state)
1298 return -ENOMEM;
1299
1300 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
1301 if (!insn_stack) {
1302 kfree(insn_state);
1303 return -ENOMEM;
1304 }
1305
1306 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
1307 insn_stack[0] = 0; /* 0 is the first instruction */
1308 cur_stack = 1;
1309
1310 peek_stack:
1311 if (cur_stack == 0)
1312 goto check_state;
1313 t = insn_stack[cur_stack - 1];
1314
1315 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
1316 u8 opcode = BPF_OP(insns[t].code);
1317
1318 if (opcode == BPF_EXIT) {
1319 goto mark_explored;
1320 } else if (opcode == BPF_CALL) {
1321 ret = push_insn(t, t + 1, FALLTHROUGH, env);
1322 if (ret == 1)
1323 goto peek_stack;
1324 else if (ret < 0)
1325 goto err_free;
1326 } else if (opcode == BPF_JA) {
1327 if (BPF_SRC(insns[t].code) != BPF_K) {
1328 ret = -EINVAL;
1329 goto err_free;
1330 }
1331 /* unconditional jump with single edge */
1332 ret = push_insn(t, t + insns[t].off + 1,
1333 FALLTHROUGH, env);
1334 if (ret == 1)
1335 goto peek_stack;
1336 else if (ret < 0)
1337 goto err_free;
1338 /* tell verifier to check for equivalent states
1339 * after every call and jump
1340 */
1341 if (t + 1 < insn_cnt)
1342 env->explored_states[t + 1] = STATE_LIST_MARK;
1343 } else {
1344 /* conditional jump with two edges */
1345 ret = push_insn(t, t + 1, FALLTHROUGH, env);
1346 if (ret == 1)
1347 goto peek_stack;
1348 else if (ret < 0)
1349 goto err_free;
1350
1351 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
1352 if (ret == 1)
1353 goto peek_stack;
1354 else if (ret < 0)
1355 goto err_free;
1356 }
1357 } else {
1358 /* all other non-branch instructions with single
1359 * fall-through edge
1360 */
1361 ret = push_insn(t, t + 1, FALLTHROUGH, env);
1362 if (ret == 1)
1363 goto peek_stack;
1364 else if (ret < 0)
1365 goto err_free;
1366 }
1367
1368 mark_explored:
1369 insn_state[t] = EXPLORED;
1370 if (cur_stack-- <= 0) {
1371 verbose("pop stack internal bug\n");
1372 ret = -EFAULT;
1373 goto err_free;
1374 }
1375 goto peek_stack;
1376
1377 check_state:
1378 for (i = 0; i < insn_cnt; i++) {
1379 if (insn_state[i] != EXPLORED) {
1380 verbose("unreachable insn %d\n", i);
1381 ret = -EINVAL;
1382 goto err_free;
1383 }
1384 }
1385 ret = 0; /* cfg looks good */
1386
1387 err_free:
1388 kfree(insn_state);
1389 kfree(insn_stack);
1390 return ret;
1391 }
1392
1393 /* compare two verifier states
1394 *
1395 * all states stored in state_list are known to be valid, since
1396 * verifier reached 'bpf_exit' instruction through them
1397 *
1398 * this function is called when verifier exploring different branches of
1399 * execution popped from the state stack. If it sees an old state that has
1400 * more strict register state and more strict stack state then this execution
1401 * branch doesn't need to be explored further, since verifier already
1402 * concluded that more strict state leads to valid finish.
1403 *
1404 * Therefore two states are equivalent if register state is more conservative
1405 * and explored stack state is more conservative than the current one.
1406 * Example:
1407 * explored current
1408 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
1409 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
1410 *
1411 * In other words if current stack state (one being explored) has more
1412 * valid slots than old one that already passed validation, it means
1413 * the verifier can stop exploring and conclude that current state is valid too
1414 *
1415 * Similarly with registers. If explored state has register type as invalid
1416 * whereas register type in current state is meaningful, it means that
1417 * the current state will reach 'bpf_exit' instruction safely
1418 */
states_equal(struct verifier_state * old,struct verifier_state * cur)1419 static bool states_equal(struct verifier_state *old, struct verifier_state *cur)
1420 {
1421 int i;
1422
1423 for (i = 0; i < MAX_BPF_REG; i++) {
1424 if (memcmp(&old->regs[i], &cur->regs[i],
1425 sizeof(old->regs[0])) != 0) {
1426 if (old->regs[i].type == NOT_INIT ||
1427 (old->regs[i].type == UNKNOWN_VALUE &&
1428 cur->regs[i].type != NOT_INIT))
1429 continue;
1430 return false;
1431 }
1432 }
1433
1434 for (i = 0; i < MAX_BPF_STACK; i++) {
1435 if (memcmp(&old->stack[i], &cur->stack[i],
1436 sizeof(old->stack[0])) != 0) {
1437 if (old->stack[i].stype == STACK_INVALID)
1438 continue;
1439 return false;
1440 }
1441 }
1442 return true;
1443 }
1444
is_state_visited(struct verifier_env * env,int insn_idx)1445 static int is_state_visited(struct verifier_env *env, int insn_idx)
1446 {
1447 struct verifier_state_list *new_sl;
1448 struct verifier_state_list *sl;
1449
1450 sl = env->explored_states[insn_idx];
1451 if (!sl)
1452 /* this 'insn_idx' instruction wasn't marked, so we will not
1453 * be doing state search here
1454 */
1455 return 0;
1456
1457 while (sl != STATE_LIST_MARK) {
1458 if (states_equal(&sl->state, &env->cur_state))
1459 /* reached equivalent register/stack state,
1460 * prune the search
1461 */
1462 return 1;
1463 sl = sl->next;
1464 }
1465
1466 /* there were no equivalent states, remember current one.
1467 * technically the current state is not proven to be safe yet,
1468 * but it will either reach bpf_exit (which means it's safe) or
1469 * it will be rejected. Since there are no loops, we won't be
1470 * seeing this 'insn_idx' instruction again on the way to bpf_exit
1471 */
1472 new_sl = kmalloc(sizeof(struct verifier_state_list), GFP_USER);
1473 if (!new_sl)
1474 return -ENOMEM;
1475
1476 /* add new state to the head of linked list */
1477 memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
1478 new_sl->next = env->explored_states[insn_idx];
1479 env->explored_states[insn_idx] = new_sl;
1480 return 0;
1481 }
1482
do_check(struct verifier_env * env)1483 static int do_check(struct verifier_env *env)
1484 {
1485 struct verifier_state *state = &env->cur_state;
1486 struct bpf_insn *insns = env->prog->insnsi;
1487 struct reg_state *regs = state->regs;
1488 int insn_cnt = env->prog->len;
1489 int insn_idx, prev_insn_idx = 0;
1490 int insn_processed = 0;
1491 bool do_print_state = false;
1492
1493 init_reg_state(regs);
1494 insn_idx = 0;
1495 for (;;) {
1496 struct bpf_insn *insn;
1497 u8 class;
1498 int err;
1499
1500 if (insn_idx >= insn_cnt) {
1501 verbose("invalid insn idx %d insn_cnt %d\n",
1502 insn_idx, insn_cnt);
1503 return -EFAULT;
1504 }
1505
1506 insn = &insns[insn_idx];
1507 class = BPF_CLASS(insn->code);
1508
1509 if (++insn_processed > 32768) {
1510 verbose("BPF program is too large. Proccessed %d insn\n",
1511 insn_processed);
1512 return -E2BIG;
1513 }
1514
1515 err = is_state_visited(env, insn_idx);
1516 if (err < 0)
1517 return err;
1518 if (err == 1) {
1519 /* found equivalent state, can prune the search */
1520 if (log_level) {
1521 if (do_print_state)
1522 verbose("\nfrom %d to %d: safe\n",
1523 prev_insn_idx, insn_idx);
1524 else
1525 verbose("%d: safe\n", insn_idx);
1526 }
1527 goto process_bpf_exit;
1528 }
1529
1530 if (log_level && do_print_state) {
1531 verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
1532 print_verifier_state(env);
1533 do_print_state = false;
1534 }
1535
1536 if (log_level) {
1537 verbose("%d: ", insn_idx);
1538 print_bpf_insn(insn);
1539 }
1540
1541 if (class == BPF_ALU || class == BPF_ALU64) {
1542 err = check_alu_op(regs, insn);
1543 if (err)
1544 return err;
1545
1546 } else if (class == BPF_LDX) {
1547 if (BPF_MODE(insn->code) != BPF_MEM ||
1548 insn->imm != 0) {
1549 verbose("BPF_LDX uses reserved fields\n");
1550 return -EINVAL;
1551 }
1552 /* check src operand */
1553 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1554 if (err)
1555 return err;
1556
1557 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
1558 if (err)
1559 return err;
1560
1561 /* check that memory (src_reg + off) is readable,
1562 * the state of dst_reg will be updated by this func
1563 */
1564 err = check_mem_access(env, insn->src_reg, insn->off,
1565 BPF_SIZE(insn->code), BPF_READ,
1566 insn->dst_reg);
1567 if (err)
1568 return err;
1569
1570 } else if (class == BPF_STX) {
1571 if (BPF_MODE(insn->code) == BPF_XADD) {
1572 err = check_xadd(env, insn);
1573 if (err)
1574 return err;
1575 insn_idx++;
1576 continue;
1577 }
1578
1579 if (BPF_MODE(insn->code) != BPF_MEM ||
1580 insn->imm != 0) {
1581 verbose("BPF_STX uses reserved fields\n");
1582 return -EINVAL;
1583 }
1584 /* check src1 operand */
1585 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1586 if (err)
1587 return err;
1588 /* check src2 operand */
1589 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1590 if (err)
1591 return err;
1592
1593 /* check that memory (dst_reg + off) is writeable */
1594 err = check_mem_access(env, insn->dst_reg, insn->off,
1595 BPF_SIZE(insn->code), BPF_WRITE,
1596 insn->src_reg);
1597 if (err)
1598 return err;
1599
1600 } else if (class == BPF_ST) {
1601 if (BPF_MODE(insn->code) != BPF_MEM ||
1602 insn->src_reg != BPF_REG_0) {
1603 verbose("BPF_ST uses reserved fields\n");
1604 return -EINVAL;
1605 }
1606 /* check src operand */
1607 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1608 if (err)
1609 return err;
1610
1611 /* check that memory (dst_reg + off) is writeable */
1612 err = check_mem_access(env, insn->dst_reg, insn->off,
1613 BPF_SIZE(insn->code), BPF_WRITE,
1614 -1);
1615 if (err)
1616 return err;
1617
1618 } else if (class == BPF_JMP) {
1619 u8 opcode = BPF_OP(insn->code);
1620
1621 if (opcode == BPF_CALL) {
1622 if (BPF_SRC(insn->code) != BPF_K ||
1623 insn->off != 0 ||
1624 insn->src_reg != BPF_REG_0 ||
1625 insn->dst_reg != BPF_REG_0) {
1626 verbose("BPF_CALL uses reserved fields\n");
1627 return -EINVAL;
1628 }
1629
1630 err = check_call(env, insn->imm);
1631 if (err)
1632 return err;
1633
1634 } else if (opcode == BPF_JA) {
1635 if (BPF_SRC(insn->code) != BPF_K ||
1636 insn->imm != 0 ||
1637 insn->src_reg != BPF_REG_0 ||
1638 insn->dst_reg != BPF_REG_0) {
1639 verbose("BPF_JA uses reserved fields\n");
1640 return -EINVAL;
1641 }
1642
1643 insn_idx += insn->off + 1;
1644 continue;
1645
1646 } else if (opcode == BPF_EXIT) {
1647 if (BPF_SRC(insn->code) != BPF_K ||
1648 insn->imm != 0 ||
1649 insn->src_reg != BPF_REG_0 ||
1650 insn->dst_reg != BPF_REG_0) {
1651 verbose("BPF_EXIT uses reserved fields\n");
1652 return -EINVAL;
1653 }
1654
1655 /* eBPF calling convetion is such that R0 is used
1656 * to return the value from eBPF program.
1657 * Make sure that it's readable at this time
1658 * of bpf_exit, which means that program wrote
1659 * something into it earlier
1660 */
1661 err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
1662 if (err)
1663 return err;
1664
1665 process_bpf_exit:
1666 insn_idx = pop_stack(env, &prev_insn_idx);
1667 if (insn_idx < 0) {
1668 break;
1669 } else {
1670 do_print_state = true;
1671 continue;
1672 }
1673 } else {
1674 err = check_cond_jmp_op(env, insn, &insn_idx);
1675 if (err)
1676 return err;
1677 }
1678 } else if (class == BPF_LD) {
1679 u8 mode = BPF_MODE(insn->code);
1680
1681 if (mode == BPF_ABS || mode == BPF_IND) {
1682 verbose("LD_ABS is not supported yet\n");
1683 return -EINVAL;
1684 } else if (mode == BPF_IMM) {
1685 err = check_ld_imm(env, insn);
1686 if (err)
1687 return err;
1688
1689 insn_idx++;
1690 } else {
1691 verbose("invalid BPF_LD mode\n");
1692 return -EINVAL;
1693 }
1694 } else {
1695 verbose("unknown insn class %d\n", class);
1696 return -EINVAL;
1697 }
1698
1699 insn_idx++;
1700 }
1701
1702 return 0;
1703 }
1704
1705 /* look for pseudo eBPF instructions that access map FDs and
1706 * replace them with actual map pointers
1707 */
replace_map_fd_with_map_ptr(struct verifier_env * env)1708 static int replace_map_fd_with_map_ptr(struct verifier_env *env)
1709 {
1710 struct bpf_insn *insn = env->prog->insnsi;
1711 int insn_cnt = env->prog->len;
1712 int i, j;
1713
1714 for (i = 0; i < insn_cnt; i++, insn++) {
1715 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
1716 struct bpf_map *map;
1717 struct fd f;
1718
1719 if (i == insn_cnt - 1 || insn[1].code != 0 ||
1720 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
1721 insn[1].off != 0) {
1722 verbose("invalid bpf_ld_imm64 insn\n");
1723 return -EINVAL;
1724 }
1725
1726 if (insn->src_reg == 0)
1727 /* valid generic load 64-bit imm */
1728 goto next_insn;
1729
1730 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
1731 verbose("unrecognized bpf_ld_imm64 insn\n");
1732 return -EINVAL;
1733 }
1734
1735 f = fdget(insn->imm);
1736
1737 map = bpf_map_get(f);
1738 if (IS_ERR(map)) {
1739 verbose("fd %d is not pointing to valid bpf_map\n",
1740 insn->imm);
1741 return PTR_ERR(map);
1742 }
1743
1744 /* store map pointer inside BPF_LD_IMM64 instruction */
1745 insn[0].imm = (u32) (unsigned long) map;
1746 insn[1].imm = ((u64) (unsigned long) map) >> 32;
1747
1748 /* check whether we recorded this map already */
1749 for (j = 0; j < env->used_map_cnt; j++)
1750 if (env->used_maps[j] == map) {
1751 fdput(f);
1752 goto next_insn;
1753 }
1754
1755 if (env->used_map_cnt >= MAX_USED_MAPS) {
1756 fdput(f);
1757 return -E2BIG;
1758 }
1759
1760 /* remember this map */
1761 env->used_maps[env->used_map_cnt++] = map;
1762
1763 /* hold the map. If the program is rejected by verifier,
1764 * the map will be released by release_maps() or it
1765 * will be used by the valid program until it's unloaded
1766 * and all maps are released in free_bpf_prog_info()
1767 */
1768 atomic_inc(&map->refcnt);
1769
1770 fdput(f);
1771 next_insn:
1772 insn++;
1773 i++;
1774 }
1775 }
1776
1777 /* now all pseudo BPF_LD_IMM64 instructions load valid
1778 * 'struct bpf_map *' into a register instead of user map_fd.
1779 * These pointers will be used later by verifier to validate map access.
1780 */
1781 return 0;
1782 }
1783
1784 /* drop refcnt of maps used by the rejected program */
release_maps(struct verifier_env * env)1785 static void release_maps(struct verifier_env *env)
1786 {
1787 int i;
1788
1789 for (i = 0; i < env->used_map_cnt; i++)
1790 bpf_map_put(env->used_maps[i]);
1791 }
1792
1793 /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
convert_pseudo_ld_imm64(struct verifier_env * env)1794 static void convert_pseudo_ld_imm64(struct verifier_env *env)
1795 {
1796 struct bpf_insn *insn = env->prog->insnsi;
1797 int insn_cnt = env->prog->len;
1798 int i;
1799
1800 for (i = 0; i < insn_cnt; i++, insn++)
1801 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
1802 insn->src_reg = 0;
1803 }
1804
free_states(struct verifier_env * env)1805 static void free_states(struct verifier_env *env)
1806 {
1807 struct verifier_state_list *sl, *sln;
1808 int i;
1809
1810 if (!env->explored_states)
1811 return;
1812
1813 for (i = 0; i < env->prog->len; i++) {
1814 sl = env->explored_states[i];
1815
1816 if (sl)
1817 while (sl != STATE_LIST_MARK) {
1818 sln = sl->next;
1819 kfree(sl);
1820 sl = sln;
1821 }
1822 }
1823
1824 kfree(env->explored_states);
1825 }
1826
bpf_check(struct bpf_prog * prog,union bpf_attr * attr)1827 int bpf_check(struct bpf_prog *prog, union bpf_attr *attr)
1828 {
1829 char __user *log_ubuf = NULL;
1830 struct verifier_env *env;
1831 int ret = -EINVAL;
1832
1833 if (prog->len <= 0 || prog->len > BPF_MAXINSNS)
1834 return -E2BIG;
1835
1836 /* 'struct verifier_env' can be global, but since it's not small,
1837 * allocate/free it every time bpf_check() is called
1838 */
1839 env = kzalloc(sizeof(struct verifier_env), GFP_KERNEL);
1840 if (!env)
1841 return -ENOMEM;
1842
1843 env->prog = prog;
1844
1845 /* grab the mutex to protect few globals used by verifier */
1846 mutex_lock(&bpf_verifier_lock);
1847
1848 if (attr->log_level || attr->log_buf || attr->log_size) {
1849 /* user requested verbose verifier output
1850 * and supplied buffer to store the verification trace
1851 */
1852 log_level = attr->log_level;
1853 log_ubuf = (char __user *) (unsigned long) attr->log_buf;
1854 log_size = attr->log_size;
1855 log_len = 0;
1856
1857 ret = -EINVAL;
1858 /* log_* values have to be sane */
1859 if (log_size < 128 || log_size > UINT_MAX >> 8 ||
1860 log_level == 0 || log_ubuf == NULL)
1861 goto free_env;
1862
1863 ret = -ENOMEM;
1864 log_buf = vmalloc(log_size);
1865 if (!log_buf)
1866 goto free_env;
1867 } else {
1868 log_level = 0;
1869 }
1870
1871 ret = replace_map_fd_with_map_ptr(env);
1872 if (ret < 0)
1873 goto skip_full_check;
1874
1875 env->explored_states = kcalloc(prog->len,
1876 sizeof(struct verifier_state_list *),
1877 GFP_USER);
1878 ret = -ENOMEM;
1879 if (!env->explored_states)
1880 goto skip_full_check;
1881
1882 ret = check_cfg(env);
1883 if (ret < 0)
1884 goto skip_full_check;
1885
1886 ret = do_check(env);
1887
1888 skip_full_check:
1889 while (pop_stack(env, NULL) >= 0);
1890 free_states(env);
1891
1892 if (log_level && log_len >= log_size - 1) {
1893 BUG_ON(log_len >= log_size);
1894 /* verifier log exceeded user supplied buffer */
1895 ret = -ENOSPC;
1896 /* fall through to return what was recorded */
1897 }
1898
1899 /* copy verifier log back to user space including trailing zero */
1900 if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
1901 ret = -EFAULT;
1902 goto free_log_buf;
1903 }
1904
1905 if (ret == 0 && env->used_map_cnt) {
1906 /* if program passed verifier, update used_maps in bpf_prog_info */
1907 prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
1908 sizeof(env->used_maps[0]),
1909 GFP_KERNEL);
1910
1911 if (!prog->aux->used_maps) {
1912 ret = -ENOMEM;
1913 goto free_log_buf;
1914 }
1915
1916 memcpy(prog->aux->used_maps, env->used_maps,
1917 sizeof(env->used_maps[0]) * env->used_map_cnt);
1918 prog->aux->used_map_cnt = env->used_map_cnt;
1919
1920 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
1921 * bpf_ld_imm64 instructions
1922 */
1923 convert_pseudo_ld_imm64(env);
1924 }
1925
1926 free_log_buf:
1927 if (log_level)
1928 vfree(log_buf);
1929 free_env:
1930 if (!prog->aux->used_maps)
1931 /* if we didn't copy map pointers into bpf_prog_info, release
1932 * them now. Otherwise free_bpf_prog_info() will release them.
1933 */
1934 release_maps(env);
1935 kfree(env);
1936 mutex_unlock(&bpf_verifier_lock);
1937 return ret;
1938 }
1939