blob: 1d6b97be79e1dd6000e626a74bf001e6cab5835d [file] [log] [blame]
Alexei Starovoitov51580e72014-09-26 00:17:02 -07001/* 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
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700128/* types of values stored in eBPF registers */
129enum 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
141struct 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
154enum bpf_stack_slot_type {
155 STACK_INVALID, /* nothing was stored in this stack slot */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700156 STACK_SPILL, /* register spilled into stack */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700157 STACK_MISC /* BPF program wrote some data into this slot */
158};
159
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700160#define BPF_REG_SIZE 8 /* size of eBPF register in bytes */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700161
162/* state of the program:
163 * type of all registers and stack info
164 */
165struct verifier_state {
166 struct reg_state regs[MAX_BPF_REG];
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700167 u8 stack_slot_type[MAX_BPF_STACK];
168 struct reg_state spilled_regs[MAX_BPF_STACK / BPF_REG_SIZE];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700169};
170
171/* linked list of verifier states used to prune search */
172struct verifier_state_list {
173 struct verifier_state state;
174 struct verifier_state_list *next;
175};
176
177/* verifier_state + insn_idx are pushed to stack when branch is encountered */
178struct verifier_stack_elem {
179 /* verifer state is 'st'
180 * before processing instruction 'insn_idx'
181 * and after processing instruction 'prev_insn_idx'
182 */
183 struct verifier_state st;
184 int insn_idx;
185 int prev_insn_idx;
186 struct verifier_stack_elem *next;
187};
188
Alexei Starovoitov0246e642014-09-26 00:17:04 -0700189#define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
190
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700191/* single container for all structs
192 * one verifier_env per bpf_check() call
193 */
194struct verifier_env {
Alexei Starovoitov0246e642014-09-26 00:17:04 -0700195 struct bpf_prog *prog; /* eBPF program being verified */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700196 struct verifier_stack_elem *head; /* stack of verifier states to be processed */
197 int stack_size; /* number of states to be processed */
198 struct verifier_state cur_state; /* current verifier state */
Alexei Starovoitovf1bca822014-09-29 18:50:01 -0700199 struct verifier_state_list **explored_states; /* search pruning optimization */
Alexei Starovoitov0246e642014-09-26 00:17:04 -0700200 struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
201 u32 used_map_cnt; /* number of used maps */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700202 bool allow_ptr_leaks;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700203};
204
205/* verbose verifier prints what it's seeing
206 * bpf_check() is called under lock, so no race to access these global vars
207 */
208static u32 log_level, log_size, log_len;
209static char *log_buf;
210
211static DEFINE_MUTEX(bpf_verifier_lock);
212
213/* log_level controls verbosity level of eBPF verifier.
214 * verbose() is used to dump the verification trace to the log, so the user
215 * can figure out what's wrong with the program
216 */
217static void verbose(const char *fmt, ...)
218{
219 va_list args;
220
221 if (log_level == 0 || log_len >= log_size - 1)
222 return;
223
224 va_start(args, fmt);
225 log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args);
226 va_end(args);
227}
228
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700229/* string representation of 'enum bpf_reg_type' */
230static const char * const reg_type_str[] = {
231 [NOT_INIT] = "?",
232 [UNKNOWN_VALUE] = "inv",
233 [PTR_TO_CTX] = "ctx",
234 [CONST_PTR_TO_MAP] = "map_ptr",
235 [PTR_TO_MAP_VALUE] = "map_value",
236 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
237 [FRAME_PTR] = "fp",
238 [PTR_TO_STACK] = "fp",
239 [CONST_IMM] = "imm",
240};
241
Kaixu Xia35578d72015-08-06 07:02:35 +0000242static const struct {
243 int map_type;
244 int func_id;
245} func_limit[] = {
246 {BPF_MAP_TYPE_PROG_ARRAY, BPF_FUNC_tail_call},
247 {BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_FUNC_perf_event_read},
248};
249
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700250static void print_verifier_state(struct verifier_env *env)
251{
252 enum bpf_reg_type t;
253 int i;
254
255 for (i = 0; i < MAX_BPF_REG; i++) {
256 t = env->cur_state.regs[i].type;
257 if (t == NOT_INIT)
258 continue;
259 verbose(" R%d=%s", i, reg_type_str[t]);
260 if (t == CONST_IMM || t == PTR_TO_STACK)
261 verbose("%d", env->cur_state.regs[i].imm);
262 else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE ||
263 t == PTR_TO_MAP_VALUE_OR_NULL)
264 verbose("(ks=%d,vs=%d)",
265 env->cur_state.regs[i].map_ptr->key_size,
266 env->cur_state.regs[i].map_ptr->value_size);
267 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700268 for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
269 if (env->cur_state.stack_slot_type[i] == STACK_SPILL)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700270 verbose(" fp%d=%s", -MAX_BPF_STACK + i,
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700271 reg_type_str[env->cur_state.spilled_regs[i / BPF_REG_SIZE].type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700272 }
273 verbose("\n");
274}
275
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700276static const char *const bpf_class_string[] = {
277 [BPF_LD] = "ld",
278 [BPF_LDX] = "ldx",
279 [BPF_ST] = "st",
280 [BPF_STX] = "stx",
281 [BPF_ALU] = "alu",
282 [BPF_JMP] = "jmp",
283 [BPF_RET] = "BUG",
284 [BPF_ALU64] = "alu64",
285};
286
Alexei Starovoitov687f0712015-09-08 13:40:01 -0700287static const char *const bpf_alu_string[16] = {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700288 [BPF_ADD >> 4] = "+=",
289 [BPF_SUB >> 4] = "-=",
290 [BPF_MUL >> 4] = "*=",
291 [BPF_DIV >> 4] = "/=",
292 [BPF_OR >> 4] = "|=",
293 [BPF_AND >> 4] = "&=",
294 [BPF_LSH >> 4] = "<<=",
295 [BPF_RSH >> 4] = ">>=",
296 [BPF_NEG >> 4] = "neg",
297 [BPF_MOD >> 4] = "%=",
298 [BPF_XOR >> 4] = "^=",
299 [BPF_MOV >> 4] = "=",
300 [BPF_ARSH >> 4] = "s>>=",
301 [BPF_END >> 4] = "endian",
302};
303
304static const char *const bpf_ldst_string[] = {
305 [BPF_W >> 3] = "u32",
306 [BPF_H >> 3] = "u16",
307 [BPF_B >> 3] = "u8",
308 [BPF_DW >> 3] = "u64",
309};
310
Alexei Starovoitov687f0712015-09-08 13:40:01 -0700311static const char *const bpf_jmp_string[16] = {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700312 [BPF_JA >> 4] = "jmp",
313 [BPF_JEQ >> 4] = "==",
314 [BPF_JGT >> 4] = ">",
315 [BPF_JGE >> 4] = ">=",
316 [BPF_JSET >> 4] = "&",
317 [BPF_JNE >> 4] = "!=",
318 [BPF_JSGT >> 4] = "s>",
319 [BPF_JSGE >> 4] = "s>=",
320 [BPF_CALL >> 4] = "call",
321 [BPF_EXIT >> 4] = "exit",
322};
323
324static void print_bpf_insn(struct bpf_insn *insn)
325{
326 u8 class = BPF_CLASS(insn->code);
327
328 if (class == BPF_ALU || class == BPF_ALU64) {
329 if (BPF_SRC(insn->code) == BPF_X)
330 verbose("(%02x) %sr%d %s %sr%d\n",
331 insn->code, class == BPF_ALU ? "(u32) " : "",
332 insn->dst_reg,
333 bpf_alu_string[BPF_OP(insn->code) >> 4],
334 class == BPF_ALU ? "(u32) " : "",
335 insn->src_reg);
336 else
337 verbose("(%02x) %sr%d %s %s%d\n",
338 insn->code, class == BPF_ALU ? "(u32) " : "",
339 insn->dst_reg,
340 bpf_alu_string[BPF_OP(insn->code) >> 4],
341 class == BPF_ALU ? "(u32) " : "",
342 insn->imm);
343 } else if (class == BPF_STX) {
344 if (BPF_MODE(insn->code) == BPF_MEM)
345 verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
346 insn->code,
347 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
348 insn->dst_reg,
349 insn->off, insn->src_reg);
350 else if (BPF_MODE(insn->code) == BPF_XADD)
351 verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
352 insn->code,
353 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
354 insn->dst_reg, insn->off,
355 insn->src_reg);
356 else
357 verbose("BUG_%02x\n", insn->code);
358 } else if (class == BPF_ST) {
359 if (BPF_MODE(insn->code) != BPF_MEM) {
360 verbose("BUG_st_%02x\n", insn->code);
361 return;
362 }
363 verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
364 insn->code,
365 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
366 insn->dst_reg,
367 insn->off, insn->imm);
368 } else if (class == BPF_LDX) {
369 if (BPF_MODE(insn->code) != BPF_MEM) {
370 verbose("BUG_ldx_%02x\n", insn->code);
371 return;
372 }
373 verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
374 insn->code, insn->dst_reg,
375 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
376 insn->src_reg, insn->off);
377 } else if (class == BPF_LD) {
378 if (BPF_MODE(insn->code) == BPF_ABS) {
379 verbose("(%02x) r0 = *(%s *)skb[%d]\n",
380 insn->code,
381 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
382 insn->imm);
383 } else if (BPF_MODE(insn->code) == BPF_IND) {
384 verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
385 insn->code,
386 bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
387 insn->src_reg, insn->imm);
388 } else if (BPF_MODE(insn->code) == BPF_IMM) {
389 verbose("(%02x) r%d = 0x%x\n",
390 insn->code, insn->dst_reg, insn->imm);
391 } else {
392 verbose("BUG_ld_%02x\n", insn->code);
393 return;
394 }
395 } else if (class == BPF_JMP) {
396 u8 opcode = BPF_OP(insn->code);
397
398 if (opcode == BPF_CALL) {
399 verbose("(%02x) call %d\n", insn->code, insn->imm);
400 } else if (insn->code == (BPF_JMP | BPF_JA)) {
401 verbose("(%02x) goto pc%+d\n",
402 insn->code, insn->off);
403 } else if (insn->code == (BPF_JMP | BPF_EXIT)) {
404 verbose("(%02x) exit\n", insn->code);
405 } else if (BPF_SRC(insn->code) == BPF_X) {
406 verbose("(%02x) if r%d %s r%d goto pc%+d\n",
407 insn->code, insn->dst_reg,
408 bpf_jmp_string[BPF_OP(insn->code) >> 4],
409 insn->src_reg, insn->off);
410 } else {
411 verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
412 insn->code, insn->dst_reg,
413 bpf_jmp_string[BPF_OP(insn->code) >> 4],
414 insn->imm, insn->off);
415 }
416 } else {
417 verbose("(%02x) %s\n", insn->code, bpf_class_string[class]);
418 }
419}
420
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700421static int pop_stack(struct verifier_env *env, int *prev_insn_idx)
422{
423 struct verifier_stack_elem *elem;
424 int insn_idx;
425
426 if (env->head == NULL)
427 return -1;
428
429 memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
430 insn_idx = env->head->insn_idx;
431 if (prev_insn_idx)
432 *prev_insn_idx = env->head->prev_insn_idx;
433 elem = env->head->next;
434 kfree(env->head);
435 env->head = elem;
436 env->stack_size--;
437 return insn_idx;
438}
439
440static struct verifier_state *push_stack(struct verifier_env *env, int insn_idx,
441 int prev_insn_idx)
442{
443 struct verifier_stack_elem *elem;
444
445 elem = kmalloc(sizeof(struct verifier_stack_elem), GFP_KERNEL);
446 if (!elem)
447 goto err;
448
449 memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state));
450 elem->insn_idx = insn_idx;
451 elem->prev_insn_idx = prev_insn_idx;
452 elem->next = env->head;
453 env->head = elem;
454 env->stack_size++;
455 if (env->stack_size > 1024) {
456 verbose("BPF program is too complex\n");
457 goto err;
458 }
459 return &elem->st;
460err:
461 /* pop all elements and return */
462 while (pop_stack(env, NULL) >= 0);
463 return NULL;
464}
465
466#define CALLER_SAVED_REGS 6
467static const int caller_saved[CALLER_SAVED_REGS] = {
468 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
469};
470
471static void init_reg_state(struct reg_state *regs)
472{
473 int i;
474
475 for (i = 0; i < MAX_BPF_REG; i++) {
476 regs[i].type = NOT_INIT;
477 regs[i].imm = 0;
478 regs[i].map_ptr = NULL;
479 }
480
481 /* frame pointer */
482 regs[BPF_REG_FP].type = FRAME_PTR;
483
484 /* 1st arg to a function */
485 regs[BPF_REG_1].type = PTR_TO_CTX;
486}
487
488static void mark_reg_unknown_value(struct reg_state *regs, u32 regno)
489{
490 BUG_ON(regno >= MAX_BPF_REG);
491 regs[regno].type = UNKNOWN_VALUE;
492 regs[regno].imm = 0;
493 regs[regno].map_ptr = NULL;
494}
495
496enum reg_arg_type {
497 SRC_OP, /* register is used as source operand */
498 DST_OP, /* register is used as destination operand */
499 DST_OP_NO_MARK /* same as above, check only, don't mark */
500};
501
502static int check_reg_arg(struct reg_state *regs, u32 regno,
503 enum reg_arg_type t)
504{
505 if (regno >= MAX_BPF_REG) {
506 verbose("R%d is invalid\n", regno);
507 return -EINVAL;
508 }
509
510 if (t == SRC_OP) {
511 /* check whether register used as source operand can be read */
512 if (regs[regno].type == NOT_INIT) {
513 verbose("R%d !read_ok\n", regno);
514 return -EACCES;
515 }
516 } else {
517 /* check whether register used as dest operand can be written to */
518 if (regno == BPF_REG_FP) {
519 verbose("frame pointer is read only\n");
520 return -EACCES;
521 }
522 if (t == DST_OP)
523 mark_reg_unknown_value(regs, regno);
524 }
525 return 0;
526}
527
528static int bpf_size_to_bytes(int bpf_size)
529{
530 if (bpf_size == BPF_W)
531 return 4;
532 else if (bpf_size == BPF_H)
533 return 2;
534 else if (bpf_size == BPF_B)
535 return 1;
536 else if (bpf_size == BPF_DW)
537 return 8;
538 else
539 return -EINVAL;
540}
541
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700542static bool is_spillable_regtype(enum bpf_reg_type type)
543{
544 switch (type) {
545 case PTR_TO_MAP_VALUE:
546 case PTR_TO_MAP_VALUE_OR_NULL:
547 case PTR_TO_STACK:
548 case PTR_TO_CTX:
549 case FRAME_PTR:
550 case CONST_PTR_TO_MAP:
551 return true;
552 default:
553 return false;
554 }
555}
556
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700557/* check_stack_read/write functions track spill/fill of registers,
558 * stack boundary and alignment are checked in check_mem_access()
559 */
560static int check_stack_write(struct verifier_state *state, int off, int size,
561 int value_regno)
562{
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700563 int i;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700564 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
565 * so it's aligned access and [off, off + size) are within stack limits
566 */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700567
568 if (value_regno >= 0 &&
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700569 is_spillable_regtype(state->regs[value_regno].type)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700570
571 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700572 if (size != BPF_REG_SIZE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700573 verbose("invalid size of register spill\n");
574 return -EACCES;
575 }
576
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700577 /* save register state */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700578 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
579 state->regs[value_regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700580
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700581 for (i = 0; i < BPF_REG_SIZE; i++)
582 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL;
583 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700584 /* regular write of data into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700585 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] =
586 (struct reg_state) {};
587
588 for (i = 0; i < size; i++)
589 state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700590 }
591 return 0;
592}
593
594static int check_stack_read(struct verifier_state *state, int off, int size,
595 int value_regno)
596{
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700597 u8 *slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700598 int i;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700599
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700600 slot_type = &state->stack_slot_type[MAX_BPF_STACK + off];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700601
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700602 if (slot_type[0] == STACK_SPILL) {
603 if (size != BPF_REG_SIZE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700604 verbose("invalid size of register spill\n");
605 return -EACCES;
606 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700607 for (i = 1; i < BPF_REG_SIZE; i++) {
608 if (slot_type[i] != STACK_SPILL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700609 verbose("corrupted spill memory\n");
610 return -EACCES;
611 }
612 }
613
614 if (value_regno >= 0)
615 /* restore register state from stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700616 state->regs[value_regno] =
617 state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE];
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700618 return 0;
619 } else {
620 for (i = 0; i < size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700621 if (slot_type[i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700622 verbose("invalid read from stack off %d+%d size %d\n",
623 off, i, size);
624 return -EACCES;
625 }
626 }
627 if (value_regno >= 0)
628 /* have read misc data from the stack */
629 mark_reg_unknown_value(state->regs, value_regno);
630 return 0;
631 }
632}
633
634/* check read/write into map element returned by bpf_map_lookup_elem() */
635static int check_map_access(struct verifier_env *env, u32 regno, int off,
636 int size)
637{
638 struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
639
640 if (off < 0 || off + size > map->value_size) {
641 verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
642 map->value_size, off, size);
643 return -EACCES;
644 }
645 return 0;
646}
647
648/* check access to 'struct bpf_context' fields */
649static int check_ctx_access(struct verifier_env *env, int off, int size,
650 enum bpf_access_type t)
651{
652 if (env->prog->aux->ops->is_valid_access &&
653 env->prog->aux->ops->is_valid_access(off, size, t))
654 return 0;
655
656 verbose("invalid bpf_context access off=%d size=%d\n", off, size);
657 return -EACCES;
658}
659
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700660static bool is_pointer_value(struct verifier_env *env, int regno)
661{
662 if (env->allow_ptr_leaks)
663 return false;
664
665 switch (env->cur_state.regs[regno].type) {
666 case UNKNOWN_VALUE:
667 case CONST_IMM:
668 return false;
669 default:
670 return true;
671 }
672}
673
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700674/* check whether memory at (regno + off) is accessible for t = (read | write)
675 * if t==write, value_regno is a register which value is stored into memory
676 * if t==read, value_regno is a register which will receive the value from memory
677 * if t==write && value_regno==-1, some unknown value is stored into memory
678 * if t==read && value_regno==-1, don't care what we read from memory
679 */
680static int check_mem_access(struct verifier_env *env, u32 regno, int off,
681 int bpf_size, enum bpf_access_type t,
682 int value_regno)
683{
684 struct verifier_state *state = &env->cur_state;
685 int size, err = 0;
686
Alex Gartrell24b4d2a2015-07-23 14:24:40 -0700687 if (state->regs[regno].type == PTR_TO_STACK)
688 off += state->regs[regno].imm;
689
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700690 size = bpf_size_to_bytes(bpf_size);
691 if (size < 0)
692 return size;
693
694 if (off % size != 0) {
695 verbose("misaligned access off %d size %d\n", off, size);
696 return -EACCES;
697 }
698
699 if (state->regs[regno].type == PTR_TO_MAP_VALUE) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700700 if (t == BPF_WRITE && value_regno >= 0 &&
701 is_pointer_value(env, value_regno)) {
702 verbose("R%d leaks addr into map\n", value_regno);
703 return -EACCES;
704 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700705 err = check_map_access(env, regno, off, size);
706 if (!err && t == BPF_READ && value_regno >= 0)
707 mark_reg_unknown_value(state->regs, value_regno);
708
709 } else if (state->regs[regno].type == PTR_TO_CTX) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700710 if (t == BPF_WRITE && value_regno >= 0 &&
711 is_pointer_value(env, value_regno)) {
712 verbose("R%d leaks addr into ctx\n", value_regno);
713 return -EACCES;
714 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700715 err = check_ctx_access(env, off, size, t);
716 if (!err && t == BPF_READ && value_regno >= 0)
717 mark_reg_unknown_value(state->regs, value_regno);
718
Alex Gartrell24b4d2a2015-07-23 14:24:40 -0700719 } else if (state->regs[regno].type == FRAME_PTR ||
720 state->regs[regno].type == PTR_TO_STACK) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700721 if (off >= 0 || off < -MAX_BPF_STACK) {
722 verbose("invalid stack off=%d size=%d\n", off, size);
723 return -EACCES;
724 }
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700725 if (t == BPF_WRITE) {
726 if (!env->allow_ptr_leaks &&
727 state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
728 size != BPF_REG_SIZE) {
729 verbose("attempt to corrupt spilled pointer on stack\n");
730 return -EACCES;
731 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700732 err = check_stack_write(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700733 } else {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700734 err = check_stack_read(state, off, size, value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700735 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700736 } else {
737 verbose("R%d invalid mem access '%s'\n",
738 regno, reg_type_str[state->regs[regno].type]);
739 return -EACCES;
740 }
741 return err;
742}
743
744static int check_xadd(struct verifier_env *env, struct bpf_insn *insn)
745{
746 struct reg_state *regs = env->cur_state.regs;
747 int err;
748
749 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
750 insn->imm != 0) {
751 verbose("BPF_XADD uses reserved fields\n");
752 return -EINVAL;
753 }
754
755 /* check src1 operand */
756 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
757 if (err)
758 return err;
759
760 /* check src2 operand */
761 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
762 if (err)
763 return err;
764
765 /* check whether atomic_add can read the memory */
766 err = check_mem_access(env, insn->dst_reg, insn->off,
767 BPF_SIZE(insn->code), BPF_READ, -1);
768 if (err)
769 return err;
770
771 /* check whether atomic_add can write into the same memory */
772 return check_mem_access(env, insn->dst_reg, insn->off,
773 BPF_SIZE(insn->code), BPF_WRITE, -1);
774}
775
776/* when register 'regno' is passed into function that will read 'access_size'
777 * bytes from that pointer, make sure that it's within stack boundary
778 * and all elements of stack are initialized
779 */
780static int check_stack_boundary(struct verifier_env *env,
781 int regno, int access_size)
782{
783 struct verifier_state *state = &env->cur_state;
784 struct reg_state *regs = state->regs;
785 int off, i;
786
787 if (regs[regno].type != PTR_TO_STACK)
788 return -EACCES;
789
790 off = regs[regno].imm;
791 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
792 access_size <= 0) {
793 verbose("invalid stack type R%d off=%d access_size=%d\n",
794 regno, off, access_size);
795 return -EACCES;
796 }
797
798 for (i = 0; i < access_size; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -0700799 if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700800 verbose("invalid indirect read from stack off %d+%d size %d\n",
801 off, i, access_size);
802 return -EACCES;
803 }
804 }
805 return 0;
806}
807
808static int check_func_arg(struct verifier_env *env, u32 regno,
809 enum bpf_arg_type arg_type, struct bpf_map **mapp)
810{
811 struct reg_state *reg = env->cur_state.regs + regno;
812 enum bpf_reg_type expected_type;
813 int err = 0;
814
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100815 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700816 return 0;
817
818 if (reg->type == NOT_INIT) {
819 verbose("R%d !read_ok\n", regno);
820 return -EACCES;
821 }
822
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700823 if (arg_type == ARG_ANYTHING) {
824 if (is_pointer_value(env, regno)) {
825 verbose("R%d leaks addr into helper function\n", regno);
826 return -EACCES;
827 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100828 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700829 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100830
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700831 if (arg_type == ARG_PTR_TO_STACK || arg_type == ARG_PTR_TO_MAP_KEY ||
832 arg_type == ARG_PTR_TO_MAP_VALUE) {
833 expected_type = PTR_TO_STACK;
834 } else if (arg_type == ARG_CONST_STACK_SIZE) {
835 expected_type = CONST_IMM;
836 } else if (arg_type == ARG_CONST_MAP_PTR) {
837 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov608cd712015-03-26 19:53:57 -0700838 } else if (arg_type == ARG_PTR_TO_CTX) {
839 expected_type = PTR_TO_CTX;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700840 } else {
841 verbose("unsupported arg_type %d\n", arg_type);
842 return -EFAULT;
843 }
844
845 if (reg->type != expected_type) {
846 verbose("R%d type=%s expected=%s\n", regno,
847 reg_type_str[reg->type], reg_type_str[expected_type]);
848 return -EACCES;
849 }
850
851 if (arg_type == ARG_CONST_MAP_PTR) {
852 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
853 *mapp = reg->map_ptr;
854
855 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
856 /* bpf_map_xxx(..., map_ptr, ..., key) call:
857 * check that [key, key + map->key_size) are within
858 * stack limits and initialized
859 */
860 if (!*mapp) {
861 /* in function declaration map_ptr must come before
862 * map_key, so that it's verified and known before
863 * we have to check map_key here. Otherwise it means
864 * that kernel subsystem misconfigured verifier
865 */
866 verbose("invalid map_ptr to access map->key\n");
867 return -EACCES;
868 }
869 err = check_stack_boundary(env, regno, (*mapp)->key_size);
870
871 } else if (arg_type == ARG_PTR_TO_MAP_VALUE) {
872 /* bpf_map_xxx(..., map_ptr, ..., value) call:
873 * check [value, value + map->value_size) validity
874 */
875 if (!*mapp) {
876 /* kernel subsystem misconfigured verifier */
877 verbose("invalid map_ptr to access map->value\n");
878 return -EACCES;
879 }
880 err = check_stack_boundary(env, regno, (*mapp)->value_size);
881
882 } else if (arg_type == ARG_CONST_STACK_SIZE) {
883 /* bpf_xxx(..., buf, len) call will access 'len' bytes
884 * from stack pointer 'buf'. Check it
885 * note: regno == len, regno - 1 == buf
886 */
887 if (regno == 0) {
888 /* kernel subsystem misconfigured verifier */
889 verbose("ARG_CONST_STACK_SIZE cannot be first argument\n");
890 return -EACCES;
891 }
892 err = check_stack_boundary(env, regno - 1, reg->imm);
893 }
894
895 return err;
896}
897
Kaixu Xia35578d72015-08-06 07:02:35 +0000898static int check_map_func_compatibility(struct bpf_map *map, int func_id)
899{
900 bool bool_map, bool_func;
901 int i;
902
903 if (!map)
904 return 0;
905
Wei-Chun Chao140d8b32015-08-12 07:57:12 -0700906 for (i = 0; i < ARRAY_SIZE(func_limit); i++) {
Kaixu Xia35578d72015-08-06 07:02:35 +0000907 bool_map = (map->map_type == func_limit[i].map_type);
908 bool_func = (func_id == func_limit[i].func_id);
909 /* only when map & func pair match it can continue.
910 * don't allow any other map type to be passed into
911 * the special func;
912 */
913 if (bool_map != bool_func)
914 return -EINVAL;
915 }
916
917 return 0;
918}
919
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700920static int check_call(struct verifier_env *env, int func_id)
921{
922 struct verifier_state *state = &env->cur_state;
923 const struct bpf_func_proto *fn = NULL;
924 struct reg_state *regs = state->regs;
925 struct bpf_map *map = NULL;
926 struct reg_state *reg;
927 int i, err;
928
929 /* find function prototype */
930 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
931 verbose("invalid func %d\n", func_id);
932 return -EINVAL;
933 }
934
935 if (env->prog->aux->ops->get_func_proto)
936 fn = env->prog->aux->ops->get_func_proto(func_id);
937
938 if (!fn) {
939 verbose("unknown func %d\n", func_id);
940 return -EINVAL;
941 }
942
943 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100944 if (!env->prog->gpl_compatible && fn->gpl_only) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700945 verbose("cannot call GPL only function from proprietary program\n");
946 return -EINVAL;
947 }
948
949 /* check args */
950 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &map);
951 if (err)
952 return err;
953 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &map);
954 if (err)
955 return err;
956 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &map);
957 if (err)
958 return err;
959 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &map);
960 if (err)
961 return err;
962 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &map);
963 if (err)
964 return err;
965
966 /* reset caller saved regs */
967 for (i = 0; i < CALLER_SAVED_REGS; i++) {
968 reg = regs + caller_saved[i];
969 reg->type = NOT_INIT;
970 reg->imm = 0;
971 }
972
973 /* update return register */
974 if (fn->ret_type == RET_INTEGER) {
975 regs[BPF_REG_0].type = UNKNOWN_VALUE;
976 } else if (fn->ret_type == RET_VOID) {
977 regs[BPF_REG_0].type = NOT_INIT;
978 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
979 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
980 /* remember map_ptr, so that check_map_access()
981 * can check 'value_size' boundary of memory access
982 * to map element returned from bpf_map_lookup_elem()
983 */
984 if (map == NULL) {
985 verbose("kernel subsystem misconfigured verifier\n");
986 return -EINVAL;
987 }
988 regs[BPF_REG_0].map_ptr = map;
989 } else {
990 verbose("unknown return type %d of func %d\n",
991 fn->ret_type, func_id);
992 return -EINVAL;
993 }
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700994
Kaixu Xia35578d72015-08-06 07:02:35 +0000995 err = check_map_func_compatibility(map, func_id);
996 if (err)
997 return err;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700998
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700999 return 0;
1000}
1001
1002/* check validity of 32-bit and 64-bit arithmetic operations */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001003static int check_alu_op(struct verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001004{
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001005 struct reg_state *regs = env->cur_state.regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001006 u8 opcode = BPF_OP(insn->code);
1007 int err;
1008
1009 if (opcode == BPF_END || opcode == BPF_NEG) {
1010 if (opcode == BPF_NEG) {
1011 if (BPF_SRC(insn->code) != 0 ||
1012 insn->src_reg != BPF_REG_0 ||
1013 insn->off != 0 || insn->imm != 0) {
1014 verbose("BPF_NEG uses reserved fields\n");
1015 return -EINVAL;
1016 }
1017 } else {
1018 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
1019 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) {
1020 verbose("BPF_END uses reserved fields\n");
1021 return -EINVAL;
1022 }
1023 }
1024
1025 /* check src operand */
1026 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1027 if (err)
1028 return err;
1029
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001030 if (is_pointer_value(env, insn->dst_reg)) {
1031 verbose("R%d pointer arithmetic prohibited\n",
1032 insn->dst_reg);
1033 return -EACCES;
1034 }
1035
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001036 /* check dest operand */
1037 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1038 if (err)
1039 return err;
1040
1041 } else if (opcode == BPF_MOV) {
1042
1043 if (BPF_SRC(insn->code) == BPF_X) {
1044 if (insn->imm != 0 || insn->off != 0) {
1045 verbose("BPF_MOV uses reserved fields\n");
1046 return -EINVAL;
1047 }
1048
1049 /* check src operand */
1050 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1051 if (err)
1052 return err;
1053 } else {
1054 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1055 verbose("BPF_MOV uses reserved fields\n");
1056 return -EINVAL;
1057 }
1058 }
1059
1060 /* check dest operand */
1061 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1062 if (err)
1063 return err;
1064
1065 if (BPF_SRC(insn->code) == BPF_X) {
1066 if (BPF_CLASS(insn->code) == BPF_ALU64) {
1067 /* case: R1 = R2
1068 * copy register state to dest reg
1069 */
1070 regs[insn->dst_reg] = regs[insn->src_reg];
1071 } else {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001072 if (is_pointer_value(env, insn->src_reg)) {
1073 verbose("R%d partial copy of pointer\n",
1074 insn->src_reg);
1075 return -EACCES;
1076 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001077 regs[insn->dst_reg].type = UNKNOWN_VALUE;
1078 regs[insn->dst_reg].map_ptr = NULL;
1079 }
1080 } else {
1081 /* case: R = imm
1082 * remember the value we stored into this reg
1083 */
1084 regs[insn->dst_reg].type = CONST_IMM;
1085 regs[insn->dst_reg].imm = insn->imm;
1086 }
1087
1088 } else if (opcode > BPF_END) {
1089 verbose("invalid BPF_ALU opcode %x\n", opcode);
1090 return -EINVAL;
1091
1092 } else { /* all other ALU ops: and, sub, xor, add, ... */
1093
1094 bool stack_relative = false;
1095
1096 if (BPF_SRC(insn->code) == BPF_X) {
1097 if (insn->imm != 0 || insn->off != 0) {
1098 verbose("BPF_ALU uses reserved fields\n");
1099 return -EINVAL;
1100 }
1101 /* check src1 operand */
1102 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1103 if (err)
1104 return err;
1105 } else {
1106 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
1107 verbose("BPF_ALU uses reserved fields\n");
1108 return -EINVAL;
1109 }
1110 }
1111
1112 /* check src2 operand */
1113 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1114 if (err)
1115 return err;
1116
1117 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
1118 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
1119 verbose("div by zero\n");
1120 return -EINVAL;
1121 }
1122
1123 /* pattern match 'bpf_add Rx, imm' instruction */
1124 if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 &&
1125 regs[insn->dst_reg].type == FRAME_PTR &&
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001126 BPF_SRC(insn->code) == BPF_K) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001127 stack_relative = true;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001128 } else if (is_pointer_value(env, insn->dst_reg)) {
1129 verbose("R%d pointer arithmetic prohibited\n",
1130 insn->dst_reg);
1131 return -EACCES;
1132 } else if (BPF_SRC(insn->code) == BPF_X &&
1133 is_pointer_value(env, insn->src_reg)) {
1134 verbose("R%d pointer arithmetic prohibited\n",
1135 insn->src_reg);
1136 return -EACCES;
1137 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001138
1139 /* check dest operand */
1140 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1141 if (err)
1142 return err;
1143
1144 if (stack_relative) {
1145 regs[insn->dst_reg].type = PTR_TO_STACK;
1146 regs[insn->dst_reg].imm = insn->imm;
1147 }
1148 }
1149
1150 return 0;
1151}
1152
1153static int check_cond_jmp_op(struct verifier_env *env,
1154 struct bpf_insn *insn, int *insn_idx)
1155{
1156 struct reg_state *regs = env->cur_state.regs;
1157 struct verifier_state *other_branch;
1158 u8 opcode = BPF_OP(insn->code);
1159 int err;
1160
1161 if (opcode > BPF_EXIT) {
1162 verbose("invalid BPF_JMP opcode %x\n", opcode);
1163 return -EINVAL;
1164 }
1165
1166 if (BPF_SRC(insn->code) == BPF_X) {
1167 if (insn->imm != 0) {
1168 verbose("BPF_JMP uses reserved fields\n");
1169 return -EINVAL;
1170 }
1171
1172 /* check src1 operand */
1173 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1174 if (err)
1175 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001176
1177 if (is_pointer_value(env, insn->src_reg)) {
1178 verbose("R%d pointer comparison prohibited\n",
1179 insn->src_reg);
1180 return -EACCES;
1181 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001182 } else {
1183 if (insn->src_reg != BPF_REG_0) {
1184 verbose("BPF_JMP uses reserved fields\n");
1185 return -EINVAL;
1186 }
1187 }
1188
1189 /* check src2 operand */
1190 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1191 if (err)
1192 return err;
1193
1194 /* detect if R == 0 where R was initialized to zero earlier */
1195 if (BPF_SRC(insn->code) == BPF_K &&
1196 (opcode == BPF_JEQ || opcode == BPF_JNE) &&
1197 regs[insn->dst_reg].type == CONST_IMM &&
1198 regs[insn->dst_reg].imm == insn->imm) {
1199 if (opcode == BPF_JEQ) {
1200 /* if (imm == imm) goto pc+off;
1201 * only follow the goto, ignore fall-through
1202 */
1203 *insn_idx += insn->off;
1204 return 0;
1205 } else {
1206 /* if (imm != imm) goto pc+off;
1207 * only follow fall-through branch, since
1208 * that's where the program will go
1209 */
1210 return 0;
1211 }
1212 }
1213
1214 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx);
1215 if (!other_branch)
1216 return -EFAULT;
1217
1218 /* detect if R == 0 where R is returned value from bpf_map_lookup_elem() */
1219 if (BPF_SRC(insn->code) == BPF_K &&
1220 insn->imm == 0 && (opcode == BPF_JEQ ||
1221 opcode == BPF_JNE) &&
1222 regs[insn->dst_reg].type == PTR_TO_MAP_VALUE_OR_NULL) {
1223 if (opcode == BPF_JEQ) {
1224 /* next fallthrough insn can access memory via
1225 * this register
1226 */
1227 regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
1228 /* branch targer cannot access it, since reg == 0 */
1229 other_branch->regs[insn->dst_reg].type = CONST_IMM;
1230 other_branch->regs[insn->dst_reg].imm = 0;
1231 } else {
1232 other_branch->regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
1233 regs[insn->dst_reg].type = CONST_IMM;
1234 regs[insn->dst_reg].imm = 0;
1235 }
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001236 } else if (is_pointer_value(env, insn->dst_reg)) {
1237 verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
1238 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001239 } else if (BPF_SRC(insn->code) == BPF_K &&
1240 (opcode == BPF_JEQ || opcode == BPF_JNE)) {
1241
1242 if (opcode == BPF_JEQ) {
1243 /* detect if (R == imm) goto
1244 * and in the target state recognize that R = imm
1245 */
1246 other_branch->regs[insn->dst_reg].type = CONST_IMM;
1247 other_branch->regs[insn->dst_reg].imm = insn->imm;
1248 } else {
1249 /* detect if (R != imm) goto
1250 * and in the fall-through state recognize that R = imm
1251 */
1252 regs[insn->dst_reg].type = CONST_IMM;
1253 regs[insn->dst_reg].imm = insn->imm;
1254 }
1255 }
1256 if (log_level)
1257 print_verifier_state(env);
1258 return 0;
1259}
1260
Alexei Starovoitov0246e642014-09-26 00:17:04 -07001261/* return the map pointer stored inside BPF_LD_IMM64 instruction */
1262static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
1263{
1264 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
1265
1266 return (struct bpf_map *) (unsigned long) imm64;
1267}
1268
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001269/* verify BPF_LD_IMM64 instruction */
1270static int check_ld_imm(struct verifier_env *env, struct bpf_insn *insn)
1271{
1272 struct reg_state *regs = env->cur_state.regs;
1273 int err;
1274
1275 if (BPF_SIZE(insn->code) != BPF_DW) {
1276 verbose("invalid BPF_LD_IMM insn\n");
1277 return -EINVAL;
1278 }
1279 if (insn->off != 0) {
1280 verbose("BPF_LD_IMM64 uses reserved fields\n");
1281 return -EINVAL;
1282 }
1283
1284 err = check_reg_arg(regs, insn->dst_reg, DST_OP);
1285 if (err)
1286 return err;
1287
1288 if (insn->src_reg == 0)
1289 /* generic move 64-bit immediate into a register */
1290 return 0;
1291
1292 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
1293 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
1294
1295 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
1296 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
1297 return 0;
1298}
1299
Daniel Borkmann96be4322015-03-01 12:31:46 +01001300static bool may_access_skb(enum bpf_prog_type type)
1301{
1302 switch (type) {
1303 case BPF_PROG_TYPE_SOCKET_FILTER:
1304 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee82015-03-20 15:11:11 +01001305 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01001306 return true;
1307 default:
1308 return false;
1309 }
1310}
1311
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08001312/* verify safety of LD_ABS|LD_IND instructions:
1313 * - they can only appear in the programs where ctx == skb
1314 * - since they are wrappers of function calls, they scratch R1-R5 registers,
1315 * preserve R6-R9, and store return value into R0
1316 *
1317 * Implicit input:
1318 * ctx == skb == R6 == CTX
1319 *
1320 * Explicit input:
1321 * SRC == any register
1322 * IMM == 32-bit immediate
1323 *
1324 * Output:
1325 * R0 - 8/16/32-bit skb data converted to cpu endianness
1326 */
1327static int check_ld_abs(struct verifier_env *env, struct bpf_insn *insn)
1328{
1329 struct reg_state *regs = env->cur_state.regs;
1330 u8 mode = BPF_MODE(insn->code);
1331 struct reg_state *reg;
1332 int i, err;
1333
Daniel Borkmann24701ec2015-03-01 12:31:47 +01001334 if (!may_access_skb(env->prog->type)) {
Daniel Borkmann96be4322015-03-01 12:31:46 +01001335 verbose("BPF_LD_ABS|IND instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08001336 return -EINVAL;
1337 }
1338
1339 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
1340 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
1341 verbose("BPF_LD_ABS uses reserved fields\n");
1342 return -EINVAL;
1343 }
1344
1345 /* check whether implicit source operand (register R6) is readable */
1346 err = check_reg_arg(regs, BPF_REG_6, SRC_OP);
1347 if (err)
1348 return err;
1349
1350 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
1351 verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
1352 return -EINVAL;
1353 }
1354
1355 if (mode == BPF_IND) {
1356 /* check explicit source operand */
1357 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1358 if (err)
1359 return err;
1360 }
1361
1362 /* reset caller saved regs to unreadable */
1363 for (i = 0; i < CALLER_SAVED_REGS; i++) {
1364 reg = regs + caller_saved[i];
1365 reg->type = NOT_INIT;
1366 reg->imm = 0;
1367 }
1368
1369 /* mark destination R0 register as readable, since it contains
1370 * the value fetched from the packet
1371 */
1372 regs[BPF_REG_0].type = UNKNOWN_VALUE;
1373 return 0;
1374}
1375
Alexei Starovoitov475fb782014-09-26 00:17:05 -07001376/* non-recursive DFS pseudo code
1377 * 1 procedure DFS-iterative(G,v):
1378 * 2 label v as discovered
1379 * 3 let S be a stack
1380 * 4 S.push(v)
1381 * 5 while S is not empty
1382 * 6 t <- S.pop()
1383 * 7 if t is what we're looking for:
1384 * 8 return t
1385 * 9 for all edges e in G.adjacentEdges(t) do
1386 * 10 if edge e is already labelled
1387 * 11 continue with the next edge
1388 * 12 w <- G.adjacentVertex(t,e)
1389 * 13 if vertex w is not discovered and not explored
1390 * 14 label e as tree-edge
1391 * 15 label w as discovered
1392 * 16 S.push(w)
1393 * 17 continue at 5
1394 * 18 else if vertex w is discovered
1395 * 19 label e as back-edge
1396 * 20 else
1397 * 21 // vertex w is explored
1398 * 22 label e as forward- or cross-edge
1399 * 23 label t as explored
1400 * 24 S.pop()
1401 *
1402 * convention:
1403 * 0x10 - discovered
1404 * 0x11 - discovered and fall-through edge labelled
1405 * 0x12 - discovered and fall-through and branch edges labelled
1406 * 0x20 - explored
1407 */
1408
1409enum {
1410 DISCOVERED = 0x10,
1411 EXPLORED = 0x20,
1412 FALLTHROUGH = 1,
1413 BRANCH = 2,
1414};
1415
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07001416#define STATE_LIST_MARK ((struct verifier_state_list *) -1L)
1417
Alexei Starovoitov475fb782014-09-26 00:17:05 -07001418static int *insn_stack; /* stack of insns to process */
1419static int cur_stack; /* current stack index */
1420static int *insn_state;
1421
1422/* t, w, e - match pseudo-code above:
1423 * t - index of current instruction
1424 * w - next instruction
1425 * e - edge
1426 */
1427static int push_insn(int t, int w, int e, struct verifier_env *env)
1428{
1429 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
1430 return 0;
1431
1432 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
1433 return 0;
1434
1435 if (w < 0 || w >= env->prog->len) {
1436 verbose("jump out of range from insn %d to %d\n", t, w);
1437 return -EINVAL;
1438 }
1439
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07001440 if (e == BRANCH)
1441 /* mark branch target for state pruning */
1442 env->explored_states[w] = STATE_LIST_MARK;
1443
Alexei Starovoitov475fb782014-09-26 00:17:05 -07001444 if (insn_state[w] == 0) {
1445 /* tree-edge */
1446 insn_state[t] = DISCOVERED | e;
1447 insn_state[w] = DISCOVERED;
1448 if (cur_stack >= env->prog->len)
1449 return -E2BIG;
1450 insn_stack[cur_stack++] = w;
1451 return 1;
1452 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
1453 verbose("back-edge from insn %d to %d\n", t, w);
1454 return -EINVAL;
1455 } else if (insn_state[w] == EXPLORED) {
1456 /* forward- or cross-edge */
1457 insn_state[t] = DISCOVERED | e;
1458 } else {
1459 verbose("insn state internal bug\n");
1460 return -EFAULT;
1461 }
1462 return 0;
1463}
1464
1465/* non-recursive depth-first-search to detect loops in BPF program
1466 * loop == back-edge in directed graph
1467 */
1468static int check_cfg(struct verifier_env *env)
1469{
1470 struct bpf_insn *insns = env->prog->insnsi;
1471 int insn_cnt = env->prog->len;
1472 int ret = 0;
1473 int i, t;
1474
1475 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
1476 if (!insn_state)
1477 return -ENOMEM;
1478
1479 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
1480 if (!insn_stack) {
1481 kfree(insn_state);
1482 return -ENOMEM;
1483 }
1484
1485 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
1486 insn_stack[0] = 0; /* 0 is the first instruction */
1487 cur_stack = 1;
1488
1489peek_stack:
1490 if (cur_stack == 0)
1491 goto check_state;
1492 t = insn_stack[cur_stack - 1];
1493
1494 if (BPF_CLASS(insns[t].code) == BPF_JMP) {
1495 u8 opcode = BPF_OP(insns[t].code);
1496
1497 if (opcode == BPF_EXIT) {
1498 goto mark_explored;
1499 } else if (opcode == BPF_CALL) {
1500 ret = push_insn(t, t + 1, FALLTHROUGH, env);
1501 if (ret == 1)
1502 goto peek_stack;
1503 else if (ret < 0)
1504 goto err_free;
1505 } else if (opcode == BPF_JA) {
1506 if (BPF_SRC(insns[t].code) != BPF_K) {
1507 ret = -EINVAL;
1508 goto err_free;
1509 }
1510 /* unconditional jump with single edge */
1511 ret = push_insn(t, t + insns[t].off + 1,
1512 FALLTHROUGH, env);
1513 if (ret == 1)
1514 goto peek_stack;
1515 else if (ret < 0)
1516 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07001517 /* tell verifier to check for equivalent states
1518 * after every call and jump
1519 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07001520 if (t + 1 < insn_cnt)
1521 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07001522 } else {
1523 /* conditional jump with two edges */
1524 ret = push_insn(t, t + 1, FALLTHROUGH, env);
1525 if (ret == 1)
1526 goto peek_stack;
1527 else if (ret < 0)
1528 goto err_free;
1529
1530 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
1531 if (ret == 1)
1532 goto peek_stack;
1533 else if (ret < 0)
1534 goto err_free;
1535 }
1536 } else {
1537 /* all other non-branch instructions with single
1538 * fall-through edge
1539 */
1540 ret = push_insn(t, t + 1, FALLTHROUGH, env);
1541 if (ret == 1)
1542 goto peek_stack;
1543 else if (ret < 0)
1544 goto err_free;
1545 }
1546
1547mark_explored:
1548 insn_state[t] = EXPLORED;
1549 if (cur_stack-- <= 0) {
1550 verbose("pop stack internal bug\n");
1551 ret = -EFAULT;
1552 goto err_free;
1553 }
1554 goto peek_stack;
1555
1556check_state:
1557 for (i = 0; i < insn_cnt; i++) {
1558 if (insn_state[i] != EXPLORED) {
1559 verbose("unreachable insn %d\n", i);
1560 ret = -EINVAL;
1561 goto err_free;
1562 }
1563 }
1564 ret = 0; /* cfg looks good */
1565
1566err_free:
1567 kfree(insn_state);
1568 kfree(insn_stack);
1569 return ret;
1570}
1571
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07001572/* compare two verifier states
1573 *
1574 * all states stored in state_list are known to be valid, since
1575 * verifier reached 'bpf_exit' instruction through them
1576 *
1577 * this function is called when verifier exploring different branches of
1578 * execution popped from the state stack. If it sees an old state that has
1579 * more strict register state and more strict stack state then this execution
1580 * branch doesn't need to be explored further, since verifier already
1581 * concluded that more strict state leads to valid finish.
1582 *
1583 * Therefore two states are equivalent if register state is more conservative
1584 * and explored stack state is more conservative than the current one.
1585 * Example:
1586 * explored current
1587 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
1588 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
1589 *
1590 * In other words if current stack state (one being explored) has more
1591 * valid slots than old one that already passed validation, it means
1592 * the verifier can stop exploring and conclude that current state is valid too
1593 *
1594 * Similarly with registers. If explored state has register type as invalid
1595 * whereas register type in current state is meaningful, it means that
1596 * the current state will reach 'bpf_exit' instruction safely
1597 */
1598static bool states_equal(struct verifier_state *old, struct verifier_state *cur)
1599{
1600 int i;
1601
1602 for (i = 0; i < MAX_BPF_REG; i++) {
1603 if (memcmp(&old->regs[i], &cur->regs[i],
1604 sizeof(old->regs[0])) != 0) {
1605 if (old->regs[i].type == NOT_INIT ||
Alexei Starovoitov32bf08a2014-10-20 14:54:57 -07001606 (old->regs[i].type == UNKNOWN_VALUE &&
1607 cur->regs[i].type != NOT_INIT))
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07001608 continue;
1609 return false;
1610 }
1611 }
1612
1613 for (i = 0; i < MAX_BPF_STACK; i++) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001614 if (old->stack_slot_type[i] == STACK_INVALID)
1615 continue;
1616 if (old->stack_slot_type[i] != cur->stack_slot_type[i])
1617 /* Ex: old explored (safe) state has STACK_SPILL in
1618 * this stack slot, but current has has STACK_MISC ->
1619 * this verifier states are not equivalent,
1620 * return false to continue verification of this path
1621 */
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07001622 return false;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001623 if (i % BPF_REG_SIZE)
1624 continue;
1625 if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE],
1626 &cur->spilled_regs[i / BPF_REG_SIZE],
1627 sizeof(old->spilled_regs[0])))
1628 /* when explored and current stack slot types are
1629 * the same, check that stored pointers types
1630 * are the same as well.
1631 * Ex: explored safe path could have stored
1632 * (struct reg_state) {.type = PTR_TO_STACK, .imm = -8}
1633 * but current path has stored:
1634 * (struct reg_state) {.type = PTR_TO_STACK, .imm = -16}
1635 * such verifier states are not equivalent.
1636 * return false to continue verification of this path
1637 */
1638 return false;
1639 else
1640 continue;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07001641 }
1642 return true;
1643}
1644
1645static int is_state_visited(struct verifier_env *env, int insn_idx)
1646{
1647 struct verifier_state_list *new_sl;
1648 struct verifier_state_list *sl;
1649
1650 sl = env->explored_states[insn_idx];
1651 if (!sl)
1652 /* this 'insn_idx' instruction wasn't marked, so we will not
1653 * be doing state search here
1654 */
1655 return 0;
1656
1657 while (sl != STATE_LIST_MARK) {
1658 if (states_equal(&sl->state, &env->cur_state))
1659 /* reached equivalent register/stack state,
1660 * prune the search
1661 */
1662 return 1;
1663 sl = sl->next;
1664 }
1665
1666 /* there were no equivalent states, remember current one.
1667 * technically the current state is not proven to be safe yet,
1668 * but it will either reach bpf_exit (which means it's safe) or
1669 * it will be rejected. Since there are no loops, we won't be
1670 * seeing this 'insn_idx' instruction again on the way to bpf_exit
1671 */
1672 new_sl = kmalloc(sizeof(struct verifier_state_list), GFP_USER);
1673 if (!new_sl)
1674 return -ENOMEM;
1675
1676 /* add new state to the head of linked list */
1677 memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
1678 new_sl->next = env->explored_states[insn_idx];
1679 env->explored_states[insn_idx] = new_sl;
1680 return 0;
1681}
1682
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001683static int do_check(struct verifier_env *env)
1684{
1685 struct verifier_state *state = &env->cur_state;
1686 struct bpf_insn *insns = env->prog->insnsi;
1687 struct reg_state *regs = state->regs;
1688 int insn_cnt = env->prog->len;
1689 int insn_idx, prev_insn_idx = 0;
1690 int insn_processed = 0;
1691 bool do_print_state = false;
1692
1693 init_reg_state(regs);
1694 insn_idx = 0;
1695 for (;;) {
1696 struct bpf_insn *insn;
1697 u8 class;
1698 int err;
1699
1700 if (insn_idx >= insn_cnt) {
1701 verbose("invalid insn idx %d insn_cnt %d\n",
1702 insn_idx, insn_cnt);
1703 return -EFAULT;
1704 }
1705
1706 insn = &insns[insn_idx];
1707 class = BPF_CLASS(insn->code);
1708
1709 if (++insn_processed > 32768) {
1710 verbose("BPF program is too large. Proccessed %d insn\n",
1711 insn_processed);
1712 return -E2BIG;
1713 }
1714
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07001715 err = is_state_visited(env, insn_idx);
1716 if (err < 0)
1717 return err;
1718 if (err == 1) {
1719 /* found equivalent state, can prune the search */
1720 if (log_level) {
1721 if (do_print_state)
1722 verbose("\nfrom %d to %d: safe\n",
1723 prev_insn_idx, insn_idx);
1724 else
1725 verbose("%d: safe\n", insn_idx);
1726 }
1727 goto process_bpf_exit;
1728 }
1729
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001730 if (log_level && do_print_state) {
1731 verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx);
1732 print_verifier_state(env);
1733 do_print_state = false;
1734 }
1735
1736 if (log_level) {
1737 verbose("%d: ", insn_idx);
1738 print_bpf_insn(insn);
1739 }
1740
1741 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001742 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001743 if (err)
1744 return err;
1745
1746 } else if (class == BPF_LDX) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001747 enum bpf_reg_type src_reg_type;
1748
1749 /* check for reserved fields is already done */
1750
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001751 /* check src operand */
1752 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1753 if (err)
1754 return err;
1755
1756 err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK);
1757 if (err)
1758 return err;
1759
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07001760 src_reg_type = regs[insn->src_reg].type;
1761
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001762 /* check that memory (src_reg + off) is readable,
1763 * the state of dst_reg will be updated by this func
1764 */
1765 err = check_mem_access(env, insn->src_reg, insn->off,
1766 BPF_SIZE(insn->code), BPF_READ,
1767 insn->dst_reg);
1768 if (err)
1769 return err;
1770
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07001771 if (BPF_SIZE(insn->code) != BPF_W) {
1772 insn_idx++;
1773 continue;
1774 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001775
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07001776 if (insn->imm == 0) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001777 /* saw a valid insn
1778 * dst_reg = *(u32 *)(src_reg + off)
1779 * use reserved 'imm' field to mark this insn
1780 */
1781 insn->imm = src_reg_type;
1782
1783 } else if (src_reg_type != insn->imm &&
1784 (src_reg_type == PTR_TO_CTX ||
1785 insn->imm == PTR_TO_CTX)) {
1786 /* ABuser program is trying to use the same insn
1787 * dst_reg = *(u32*) (src_reg + off)
1788 * with different pointer types:
1789 * src_reg == ctx in one branch and
1790 * src_reg == stack|map in some other branch.
1791 * Reject it.
1792 */
1793 verbose("same insn cannot be used with different pointers\n");
1794 return -EINVAL;
1795 }
1796
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001797 } else if (class == BPF_STX) {
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001798 enum bpf_reg_type dst_reg_type;
1799
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001800 if (BPF_MODE(insn->code) == BPF_XADD) {
1801 err = check_xadd(env, insn);
1802 if (err)
1803 return err;
1804 insn_idx++;
1805 continue;
1806 }
1807
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001808 /* check src1 operand */
1809 err = check_reg_arg(regs, insn->src_reg, SRC_OP);
1810 if (err)
1811 return err;
1812 /* check src2 operand */
1813 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1814 if (err)
1815 return err;
1816
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001817 dst_reg_type = regs[insn->dst_reg].type;
1818
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001819 /* check that memory (dst_reg + off) is writeable */
1820 err = check_mem_access(env, insn->dst_reg, insn->off,
1821 BPF_SIZE(insn->code), BPF_WRITE,
1822 insn->src_reg);
1823 if (err)
1824 return err;
1825
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001826 if (insn->imm == 0) {
1827 insn->imm = dst_reg_type;
1828 } else if (dst_reg_type != insn->imm &&
1829 (dst_reg_type == PTR_TO_CTX ||
1830 insn->imm == PTR_TO_CTX)) {
1831 verbose("same insn cannot be used with different pointers\n");
1832 return -EINVAL;
1833 }
1834
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001835 } else if (class == BPF_ST) {
1836 if (BPF_MODE(insn->code) != BPF_MEM ||
1837 insn->src_reg != BPF_REG_0) {
1838 verbose("BPF_ST uses reserved fields\n");
1839 return -EINVAL;
1840 }
1841 /* check src operand */
1842 err = check_reg_arg(regs, insn->dst_reg, SRC_OP);
1843 if (err)
1844 return err;
1845
1846 /* check that memory (dst_reg + off) is writeable */
1847 err = check_mem_access(env, insn->dst_reg, insn->off,
1848 BPF_SIZE(insn->code), BPF_WRITE,
1849 -1);
1850 if (err)
1851 return err;
1852
1853 } else if (class == BPF_JMP) {
1854 u8 opcode = BPF_OP(insn->code);
1855
1856 if (opcode == BPF_CALL) {
1857 if (BPF_SRC(insn->code) != BPF_K ||
1858 insn->off != 0 ||
1859 insn->src_reg != BPF_REG_0 ||
1860 insn->dst_reg != BPF_REG_0) {
1861 verbose("BPF_CALL uses reserved fields\n");
1862 return -EINVAL;
1863 }
1864
1865 err = check_call(env, insn->imm);
1866 if (err)
1867 return err;
1868
1869 } else if (opcode == BPF_JA) {
1870 if (BPF_SRC(insn->code) != BPF_K ||
1871 insn->imm != 0 ||
1872 insn->src_reg != BPF_REG_0 ||
1873 insn->dst_reg != BPF_REG_0) {
1874 verbose("BPF_JA uses reserved fields\n");
1875 return -EINVAL;
1876 }
1877
1878 insn_idx += insn->off + 1;
1879 continue;
1880
1881 } else if (opcode == BPF_EXIT) {
1882 if (BPF_SRC(insn->code) != BPF_K ||
1883 insn->imm != 0 ||
1884 insn->src_reg != BPF_REG_0 ||
1885 insn->dst_reg != BPF_REG_0) {
1886 verbose("BPF_EXIT uses reserved fields\n");
1887 return -EINVAL;
1888 }
1889
1890 /* eBPF calling convetion is such that R0 is used
1891 * to return the value from eBPF program.
1892 * Make sure that it's readable at this time
1893 * of bpf_exit, which means that program wrote
1894 * something into it earlier
1895 */
1896 err = check_reg_arg(regs, BPF_REG_0, SRC_OP);
1897 if (err)
1898 return err;
1899
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001900 if (is_pointer_value(env, BPF_REG_0)) {
1901 verbose("R0 leaks addr as return value\n");
1902 return -EACCES;
1903 }
1904
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07001905process_bpf_exit:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001906 insn_idx = pop_stack(env, &prev_insn_idx);
1907 if (insn_idx < 0) {
1908 break;
1909 } else {
1910 do_print_state = true;
1911 continue;
1912 }
1913 } else {
1914 err = check_cond_jmp_op(env, insn, &insn_idx);
1915 if (err)
1916 return err;
1917 }
1918 } else if (class == BPF_LD) {
1919 u8 mode = BPF_MODE(insn->code);
1920
1921 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08001922 err = check_ld_abs(env, insn);
1923 if (err)
1924 return err;
1925
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001926 } else if (mode == BPF_IMM) {
1927 err = check_ld_imm(env, insn);
1928 if (err)
1929 return err;
1930
1931 insn_idx++;
1932 } else {
1933 verbose("invalid BPF_LD mode\n");
1934 return -EINVAL;
1935 }
1936 } else {
1937 verbose("unknown insn class %d\n", class);
1938 return -EINVAL;
1939 }
1940
1941 insn_idx++;
1942 }
1943
1944 return 0;
1945}
1946
Alexei Starovoitov0246e642014-09-26 00:17:04 -07001947/* look for pseudo eBPF instructions that access map FDs and
1948 * replace them with actual map pointers
1949 */
1950static int replace_map_fd_with_map_ptr(struct verifier_env *env)
1951{
1952 struct bpf_insn *insn = env->prog->insnsi;
1953 int insn_cnt = env->prog->len;
1954 int i, j;
1955
1956 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001957 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001958 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001959 verbose("BPF_LDX uses reserved fields\n");
1960 return -EINVAL;
1961 }
1962
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001963 if (BPF_CLASS(insn->code) == BPF_STX &&
1964 ((BPF_MODE(insn->code) != BPF_MEM &&
1965 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
1966 verbose("BPF_STX uses reserved fields\n");
1967 return -EINVAL;
1968 }
1969
Alexei Starovoitov0246e642014-09-26 00:17:04 -07001970 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
1971 struct bpf_map *map;
1972 struct fd f;
1973
1974 if (i == insn_cnt - 1 || insn[1].code != 0 ||
1975 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
1976 insn[1].off != 0) {
1977 verbose("invalid bpf_ld_imm64 insn\n");
1978 return -EINVAL;
1979 }
1980
1981 if (insn->src_reg == 0)
1982 /* valid generic load 64-bit imm */
1983 goto next_insn;
1984
1985 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
1986 verbose("unrecognized bpf_ld_imm64 insn\n");
1987 return -EINVAL;
1988 }
1989
1990 f = fdget(insn->imm);
1991
1992 map = bpf_map_get(f);
1993 if (IS_ERR(map)) {
1994 verbose("fd %d is not pointing to valid bpf_map\n",
1995 insn->imm);
1996 fdput(f);
1997 return PTR_ERR(map);
1998 }
1999
2000 /* store map pointer inside BPF_LD_IMM64 instruction */
2001 insn[0].imm = (u32) (unsigned long) map;
2002 insn[1].imm = ((u64) (unsigned long) map) >> 32;
2003
2004 /* check whether we recorded this map already */
2005 for (j = 0; j < env->used_map_cnt; j++)
2006 if (env->used_maps[j] == map) {
2007 fdput(f);
2008 goto next_insn;
2009 }
2010
2011 if (env->used_map_cnt >= MAX_USED_MAPS) {
2012 fdput(f);
2013 return -E2BIG;
2014 }
2015
2016 /* remember this map */
2017 env->used_maps[env->used_map_cnt++] = map;
2018
2019 /* hold the map. If the program is rejected by verifier,
2020 * the map will be released by release_maps() or it
2021 * will be used by the valid program until it's unloaded
2022 * and all maps are released in free_bpf_prog_info()
2023 */
2024 atomic_inc(&map->refcnt);
2025
2026 fdput(f);
2027next_insn:
2028 insn++;
2029 i++;
2030 }
2031 }
2032
2033 /* now all pseudo BPF_LD_IMM64 instructions load valid
2034 * 'struct bpf_map *' into a register instead of user map_fd.
2035 * These pointers will be used later by verifier to validate map access.
2036 */
2037 return 0;
2038}
2039
2040/* drop refcnt of maps used by the rejected program */
2041static void release_maps(struct verifier_env *env)
2042{
2043 int i;
2044
2045 for (i = 0; i < env->used_map_cnt; i++)
2046 bpf_map_put(env->used_maps[i]);
2047}
2048
2049/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
2050static void convert_pseudo_ld_imm64(struct verifier_env *env)
2051{
2052 struct bpf_insn *insn = env->prog->insnsi;
2053 int insn_cnt = env->prog->len;
2054 int i;
2055
2056 for (i = 0; i < insn_cnt; i++, insn++)
2057 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
2058 insn->src_reg = 0;
2059}
2060
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002061static void adjust_branches(struct bpf_prog *prog, int pos, int delta)
2062{
2063 struct bpf_insn *insn = prog->insnsi;
2064 int insn_cnt = prog->len;
2065 int i;
2066
2067 for (i = 0; i < insn_cnt; i++, insn++) {
2068 if (BPF_CLASS(insn->code) != BPF_JMP ||
2069 BPF_OP(insn->code) == BPF_CALL ||
2070 BPF_OP(insn->code) == BPF_EXIT)
2071 continue;
2072
2073 /* adjust offset of jmps if necessary */
2074 if (i < pos && i + insn->off + 1 > pos)
2075 insn->off += delta;
2076 else if (i > pos && i + insn->off + 1 < pos)
2077 insn->off -= delta;
2078 }
2079}
2080
2081/* convert load instructions that access fields of 'struct __sk_buff'
2082 * into sequence of instructions that access fields of 'struct sk_buff'
2083 */
2084static int convert_ctx_accesses(struct verifier_env *env)
2085{
2086 struct bpf_insn *insn = env->prog->insnsi;
2087 int insn_cnt = env->prog->len;
2088 struct bpf_insn insn_buf[16];
2089 struct bpf_prog *new_prog;
2090 u32 cnt;
2091 int i;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002092 enum bpf_access_type type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002093
2094 if (!env->prog->aux->ops->convert_ctx_access)
2095 return 0;
2096
2097 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002098 if (insn->code == (BPF_LDX | BPF_MEM | BPF_W))
2099 type = BPF_READ;
2100 else if (insn->code == (BPF_STX | BPF_MEM | BPF_W))
2101 type = BPF_WRITE;
2102 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002103 continue;
2104
2105 if (insn->imm != PTR_TO_CTX) {
2106 /* clear internal mark */
2107 insn->imm = 0;
2108 continue;
2109 }
2110
2111 cnt = env->prog->aux->ops->
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002112 convert_ctx_access(type, insn->dst_reg, insn->src_reg,
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002113 insn->off, insn_buf, env->prog);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002114 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
2115 verbose("bpf verifier is misconfigured\n");
2116 return -EINVAL;
2117 }
2118
2119 if (cnt == 1) {
2120 memcpy(insn, insn_buf, sizeof(*insn));
2121 continue;
2122 }
2123
2124 /* several new insns need to be inserted. Make room for them */
2125 insn_cnt += cnt - 1;
2126 new_prog = bpf_prog_realloc(env->prog,
2127 bpf_prog_size(insn_cnt),
2128 GFP_USER);
2129 if (!new_prog)
2130 return -ENOMEM;
2131
2132 new_prog->len = insn_cnt;
2133
2134 memmove(new_prog->insnsi + i + cnt, new_prog->insns + i + 1,
2135 sizeof(*insn) * (insn_cnt - i - cnt));
2136
2137 /* copy substitute insns in place of load instruction */
2138 memcpy(new_prog->insnsi + i, insn_buf, sizeof(*insn) * cnt);
2139
2140 /* adjust branches in the whole program */
2141 adjust_branches(new_prog, i, cnt - 1);
2142
2143 /* keep walking new program and skip insns we just inserted */
2144 env->prog = new_prog;
2145 insn = new_prog->insnsi + i + cnt - 1;
2146 i += cnt - 1;
2147 }
2148
2149 return 0;
2150}
2151
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002152static void free_states(struct verifier_env *env)
2153{
2154 struct verifier_state_list *sl, *sln;
2155 int i;
2156
2157 if (!env->explored_states)
2158 return;
2159
2160 for (i = 0; i < env->prog->len; i++) {
2161 sl = env->explored_states[i];
2162
2163 if (sl)
2164 while (sl != STATE_LIST_MARK) {
2165 sln = sl->next;
2166 kfree(sl);
2167 sl = sln;
2168 }
2169 }
2170
2171 kfree(env->explored_states);
2172}
2173
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002174int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07002175{
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07002176 char __user *log_ubuf = NULL;
2177 struct verifier_env *env;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07002178 int ret = -EINVAL;
2179
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002180 if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07002181 return -E2BIG;
2182
2183 /* 'struct verifier_env' can be global, but since it's not small,
2184 * allocate/free it every time bpf_check() is called
2185 */
2186 env = kzalloc(sizeof(struct verifier_env), GFP_KERNEL);
2187 if (!env)
2188 return -ENOMEM;
2189
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002190 env->prog = *prog;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002191
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07002192 /* grab the mutex to protect few globals used by verifier */
2193 mutex_lock(&bpf_verifier_lock);
2194
2195 if (attr->log_level || attr->log_buf || attr->log_size) {
2196 /* user requested verbose verifier output
2197 * and supplied buffer to store the verification trace
2198 */
2199 log_level = attr->log_level;
2200 log_ubuf = (char __user *) (unsigned long) attr->log_buf;
2201 log_size = attr->log_size;
2202 log_len = 0;
2203
2204 ret = -EINVAL;
2205 /* log_* values have to be sane */
2206 if (log_size < 128 || log_size > UINT_MAX >> 8 ||
2207 log_level == 0 || log_ubuf == NULL)
2208 goto free_env;
2209
2210 ret = -ENOMEM;
2211 log_buf = vmalloc(log_size);
2212 if (!log_buf)
2213 goto free_env;
2214 } else {
2215 log_level = 0;
2216 }
2217
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002218 ret = replace_map_fd_with_map_ptr(env);
2219 if (ret < 0)
2220 goto skip_full_check;
2221
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002222 env->explored_states = kcalloc(env->prog->len,
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002223 sizeof(struct verifier_state_list *),
2224 GFP_USER);
2225 ret = -ENOMEM;
2226 if (!env->explored_states)
2227 goto skip_full_check;
2228
Alexei Starovoitov475fb782014-09-26 00:17:05 -07002229 ret = check_cfg(env);
2230 if (ret < 0)
2231 goto skip_full_check;
2232
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002233 env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
2234
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002235 ret = do_check(env);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07002236
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002237skip_full_check:
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002238 while (pop_stack(env, NULL) >= 0);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07002239 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002240
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002241 if (ret == 0)
2242 /* program is valid, convert *(u32*)(ctx + off) accesses */
2243 ret = convert_ctx_accesses(env);
2244
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07002245 if (log_level && log_len >= log_size - 1) {
2246 BUG_ON(log_len >= log_size);
2247 /* verifier log exceeded user supplied buffer */
2248 ret = -ENOSPC;
2249 /* fall through to return what was recorded */
2250 }
2251
2252 /* copy verifier log back to user space including trailing zero */
2253 if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
2254 ret = -EFAULT;
2255 goto free_log_buf;
2256 }
2257
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002258 if (ret == 0 && env->used_map_cnt) {
2259 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002260 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
2261 sizeof(env->used_maps[0]),
2262 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002263
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002264 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002265 ret = -ENOMEM;
2266 goto free_log_buf;
2267 }
2268
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002269 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002270 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002271 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002272
2273 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
2274 * bpf_ld_imm64 instructions
2275 */
2276 convert_pseudo_ld_imm64(env);
2277 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07002278
2279free_log_buf:
2280 if (log_level)
2281 vfree(log_buf);
2282free_env:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002283 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07002284 /* if we didn't copy map pointers into bpf_prog_info, release
2285 * them now. Otherwise free_bpf_prog_info() will release them.
2286 */
2287 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002288 *prog = env->prog;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07002289 kfree(env);
2290 mutex_unlock(&bpf_verifier_lock);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07002291 return ret;
2292}