1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 * Copyright (c) 2016 Facebook
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/bpf.h>
17 #include <linux/bpf_verifier.h>
18 #include <linux/filter.h>
19 #include <net/netlink.h>
20 #include <linux/file.h>
21 #include <linux/vmalloc.h>
22 #include <linux/stringify.h>
24 /* bpf_check() is a static code analyzer that walks eBPF program
25 * instruction by instruction and updates register/stack state.
26 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
28 * The first pass is depth-first-search to check that the program is a DAG.
29 * It rejects the following programs:
30 * - larger than BPF_MAXINSNS insns
31 * - if loop is present (detected via back-edge)
32 * - unreachable insns exist (shouldn't be a forest. program = one function)
33 * - out of bounds or malformed jumps
34 * The second pass is all possible path descent from the 1st insn.
35 * Since it's analyzing all pathes through the program, the length of the
36 * analysis is limited to 64k insn, which may be hit even if total number of
37 * insn is less then 4K, but there are too many branches that change stack/regs.
38 * Number of 'branches to be analyzed' is limited to 1k
40 * On entry to each instruction, each register has a type, and the instruction
41 * changes the types of the registers depending on instruction semantics.
42 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
45 * All registers are 64-bit.
46 * R0 - return register
47 * R1-R5 argument passing registers
48 * R6-R9 callee saved registers
49 * R10 - frame pointer read-only
51 * At the start of BPF program the register R1 contains a pointer to bpf_context
52 * and has type PTR_TO_CTX.
54 * Verifier tracks arithmetic operations on pointers in case:
55 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
56 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
57 * 1st insn copies R10 (which has FRAME_PTR) type into R1
58 * and 2nd arithmetic instruction is pattern matched to recognize
59 * that it wants to construct a pointer to some element within stack.
60 * So after 2nd insn, the register R1 has type PTR_TO_STACK
61 * (and -20 constant is saved for further stack bounds checking).
62 * Meaning that this reg is a pointer to stack plus known immediate constant.
64 * Most of the time the registers have UNKNOWN_VALUE type, which
65 * means the register has some value, but it's not a valid pointer.
66 * (like pointer plus pointer becomes UNKNOWN_VALUE type)
68 * When verifier sees load or store instructions the type of base register
69 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, FRAME_PTR. These are three pointer
70 * types recognized by check_mem_access() function.
72 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
73 * and the range of [ptr, ptr + map's value_size) is accessible.
75 * registers used to pass values to function calls are checked against
76 * function argument constraints.
78 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
79 * It means that the register type passed to this function must be
80 * PTR_TO_STACK and it will be used inside the function as
81 * 'pointer to map element key'
83 * For example the argument constraints for bpf_map_lookup_elem():
84 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
85 * .arg1_type = ARG_CONST_MAP_PTR,
86 * .arg2_type = ARG_PTR_TO_MAP_KEY,
88 * ret_type says that this function returns 'pointer to map elem value or null'
89 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
90 * 2nd argument should be a pointer to stack, which will be used inside
91 * the helper function as a pointer to map element key.
93 * On the kernel side the helper function looks like:
94 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
96 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
97 * void *key = (void *) (unsigned long) r2;
100 * here kernel can access 'key' and 'map' pointers safely, knowing that
101 * [key, key + map->key_size) bytes are valid and were initialized on
102 * the stack of eBPF program.
105 * Corresponding eBPF program may look like:
106 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
107 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
108 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
109 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
110 * here verifier looks at prototype of map_lookup_elem() and sees:
111 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
112 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
114 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
115 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
116 * and were initialized prior to this call.
117 * If it's ok, then verifier allows this BPF_CALL insn and looks at
118 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
119 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
120 * returns ether pointer to map value or NULL.
122 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
123 * insn, the register holding that pointer in the true branch changes state to
124 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
125 * branch. See check_cond_jmp_op().
127 * After the call R0 is set to return type of the function and registers R1-R5
128 * are set to NOT_INIT to indicate that they are no longer readable.
131 /* verifier_state + insn_idx are pushed to stack when branch is encountered */
132 struct bpf_verifier_stack_elem
{
133 /* verifer state is 'st'
134 * before processing instruction 'insn_idx'
135 * and after processing instruction 'prev_insn_idx'
137 struct bpf_verifier_state st
;
140 struct bpf_verifier_stack_elem
*next
;
143 #define BPF_COMPLEXITY_LIMIT_INSNS 65536
144 #define BPF_COMPLEXITY_LIMIT_STACK 1024
146 #define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA)
148 struct bpf_call_arg_meta
{
149 struct bpf_map
*map_ptr
;
156 /* verbose verifier prints what it's seeing
157 * bpf_check() is called under lock, so no race to access these global vars
159 static u32 log_level
, log_size
, log_len
;
160 static char *log_buf
;
162 static DEFINE_MUTEX(bpf_verifier_lock
);
164 /* log_level controls verbosity level of eBPF verifier.
165 * verbose() is used to dump the verification trace to the log, so the user
166 * can figure out what's wrong with the program
168 static __printf(1, 2) void verbose(const char *fmt
, ...)
172 if (log_level
== 0 || log_len
>= log_size
- 1)
176 log_len
+= vscnprintf(log_buf
+ log_len
, log_size
- log_len
, fmt
, args
);
180 /* string representation of 'enum bpf_reg_type' */
181 static const char * const reg_type_str
[] = {
183 [UNKNOWN_VALUE
] = "inv",
184 [PTR_TO_CTX
] = "ctx",
185 [CONST_PTR_TO_MAP
] = "map_ptr",
186 [PTR_TO_MAP_VALUE
] = "map_value",
187 [PTR_TO_MAP_VALUE_OR_NULL
] = "map_value_or_null",
188 [PTR_TO_MAP_VALUE_ADJ
] = "map_value_adj",
190 [PTR_TO_STACK
] = "fp",
192 [PTR_TO_PACKET
] = "pkt",
193 [PTR_TO_PACKET_END
] = "pkt_end",
196 #define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x)
197 static const char * const func_id_str
[] = {
198 __BPF_FUNC_MAPPER(__BPF_FUNC_STR_FN
)
200 #undef __BPF_FUNC_STR_FN
202 static const char *func_id_name(int id
)
204 BUILD_BUG_ON(ARRAY_SIZE(func_id_str
) != __BPF_FUNC_MAX_ID
);
206 if (id
>= 0 && id
< __BPF_FUNC_MAX_ID
&& func_id_str
[id
])
207 return func_id_str
[id
];
212 static void print_verifier_state(struct bpf_verifier_state
*state
)
214 struct bpf_reg_state
*reg
;
218 for (i
= 0; i
< MAX_BPF_REG
; i
++) {
219 reg
= &state
->regs
[i
];
223 verbose(" R%d=%s", i
, reg_type_str
[t
]);
224 if (t
== CONST_IMM
|| t
== PTR_TO_STACK
)
225 verbose("%lld", reg
->imm
);
226 else if (t
== PTR_TO_PACKET
)
227 verbose("(id=%d,off=%d,r=%d)",
228 reg
->id
, reg
->off
, reg
->range
);
229 else if (t
== UNKNOWN_VALUE
&& reg
->imm
)
230 verbose("%lld", reg
->imm
);
231 else if (t
== CONST_PTR_TO_MAP
|| t
== PTR_TO_MAP_VALUE
||
232 t
== PTR_TO_MAP_VALUE_OR_NULL
||
233 t
== PTR_TO_MAP_VALUE_ADJ
)
234 verbose("(ks=%d,vs=%d,id=%u)",
235 reg
->map_ptr
->key_size
,
236 reg
->map_ptr
->value_size
,
238 if (reg
->min_value
!= BPF_REGISTER_MIN_RANGE
)
239 verbose(",min_value=%lld",
240 (long long)reg
->min_value
);
241 if (reg
->max_value
!= BPF_REGISTER_MAX_RANGE
)
242 verbose(",max_value=%llu",
243 (unsigned long long)reg
->max_value
);
245 for (i
= 0; i
< MAX_BPF_STACK
; i
+= BPF_REG_SIZE
) {
246 if (state
->stack_slot_type
[i
] == STACK_SPILL
)
247 verbose(" fp%d=%s", -MAX_BPF_STACK
+ i
,
248 reg_type_str
[state
->spilled_regs
[i
/ BPF_REG_SIZE
].type
]);
253 static const char *const bpf_class_string
[] = {
261 [BPF_ALU64
] = "alu64",
264 static const char *const bpf_alu_string
[16] = {
265 [BPF_ADD
>> 4] = "+=",
266 [BPF_SUB
>> 4] = "-=",
267 [BPF_MUL
>> 4] = "*=",
268 [BPF_DIV
>> 4] = "/=",
269 [BPF_OR
>> 4] = "|=",
270 [BPF_AND
>> 4] = "&=",
271 [BPF_LSH
>> 4] = "<<=",
272 [BPF_RSH
>> 4] = ">>=",
273 [BPF_NEG
>> 4] = "neg",
274 [BPF_MOD
>> 4] = "%=",
275 [BPF_XOR
>> 4] = "^=",
276 [BPF_MOV
>> 4] = "=",
277 [BPF_ARSH
>> 4] = "s>>=",
278 [BPF_END
>> 4] = "endian",
281 static const char *const bpf_ldst_string
[] = {
282 [BPF_W
>> 3] = "u32",
283 [BPF_H
>> 3] = "u16",
285 [BPF_DW
>> 3] = "u64",
288 static const char *const bpf_jmp_string
[16] = {
289 [BPF_JA
>> 4] = "jmp",
290 [BPF_JEQ
>> 4] = "==",
291 [BPF_JGT
>> 4] = ">",
292 [BPF_JGE
>> 4] = ">=",
293 [BPF_JSET
>> 4] = "&",
294 [BPF_JNE
>> 4] = "!=",
295 [BPF_JSGT
>> 4] = "s>",
296 [BPF_JSGE
>> 4] = "s>=",
297 [BPF_CALL
>> 4] = "call",
298 [BPF_EXIT
>> 4] = "exit",
301 static void print_bpf_insn(const struct bpf_verifier_env
*env
,
302 const struct bpf_insn
*insn
)
304 u8
class = BPF_CLASS(insn
->code
);
306 if (class == BPF_ALU
|| class == BPF_ALU64
) {
307 if (BPF_SRC(insn
->code
) == BPF_X
)
308 verbose("(%02x) %sr%d %s %sr%d\n",
309 insn
->code
, class == BPF_ALU
? "(u32) " : "",
311 bpf_alu_string
[BPF_OP(insn
->code
) >> 4],
312 class == BPF_ALU
? "(u32) " : "",
315 verbose("(%02x) %sr%d %s %s%d\n",
316 insn
->code
, class == BPF_ALU
? "(u32) " : "",
318 bpf_alu_string
[BPF_OP(insn
->code
) >> 4],
319 class == BPF_ALU
? "(u32) " : "",
321 } else if (class == BPF_STX
) {
322 if (BPF_MODE(insn
->code
) == BPF_MEM
)
323 verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
325 bpf_ldst_string
[BPF_SIZE(insn
->code
) >> 3],
327 insn
->off
, insn
->src_reg
);
328 else if (BPF_MODE(insn
->code
) == BPF_XADD
)
329 verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
331 bpf_ldst_string
[BPF_SIZE(insn
->code
) >> 3],
332 insn
->dst_reg
, insn
->off
,
335 verbose("BUG_%02x\n", insn
->code
);
336 } else if (class == BPF_ST
) {
337 if (BPF_MODE(insn
->code
) != BPF_MEM
) {
338 verbose("BUG_st_%02x\n", insn
->code
);
341 verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
343 bpf_ldst_string
[BPF_SIZE(insn
->code
) >> 3],
345 insn
->off
, insn
->imm
);
346 } else if (class == BPF_LDX
) {
347 if (BPF_MODE(insn
->code
) != BPF_MEM
) {
348 verbose("BUG_ldx_%02x\n", insn
->code
);
351 verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
352 insn
->code
, insn
->dst_reg
,
353 bpf_ldst_string
[BPF_SIZE(insn
->code
) >> 3],
354 insn
->src_reg
, insn
->off
);
355 } else if (class == BPF_LD
) {
356 if (BPF_MODE(insn
->code
) == BPF_ABS
) {
357 verbose("(%02x) r0 = *(%s *)skb[%d]\n",
359 bpf_ldst_string
[BPF_SIZE(insn
->code
) >> 3],
361 } else if (BPF_MODE(insn
->code
) == BPF_IND
) {
362 verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
364 bpf_ldst_string
[BPF_SIZE(insn
->code
) >> 3],
365 insn
->src_reg
, insn
->imm
);
366 } else if (BPF_MODE(insn
->code
) == BPF_IMM
&&
367 BPF_SIZE(insn
->code
) == BPF_DW
) {
368 /* At this point, we already made sure that the second
369 * part of the ldimm64 insn is accessible.
371 u64 imm
= ((u64
)(insn
+ 1)->imm
<< 32) | (u32
)insn
->imm
;
372 bool map_ptr
= insn
->src_reg
== BPF_PSEUDO_MAP_FD
;
374 if (map_ptr
&& !env
->allow_ptr_leaks
)
377 verbose("(%02x) r%d = 0x%llx\n", insn
->code
,
378 insn
->dst_reg
, (unsigned long long)imm
);
380 verbose("BUG_ld_%02x\n", insn
->code
);
383 } else if (class == BPF_JMP
) {
384 u8 opcode
= BPF_OP(insn
->code
);
386 if (opcode
== BPF_CALL
) {
387 verbose("(%02x) call %s#%d\n", insn
->code
,
388 func_id_name(insn
->imm
), insn
->imm
);
389 } else if (insn
->code
== (BPF_JMP
| BPF_JA
)) {
390 verbose("(%02x) goto pc%+d\n",
391 insn
->code
, insn
->off
);
392 } else if (insn
->code
== (BPF_JMP
| BPF_EXIT
)) {
393 verbose("(%02x) exit\n", insn
->code
);
394 } else if (BPF_SRC(insn
->code
) == BPF_X
) {
395 verbose("(%02x) if r%d %s r%d goto pc%+d\n",
396 insn
->code
, insn
->dst_reg
,
397 bpf_jmp_string
[BPF_OP(insn
->code
) >> 4],
398 insn
->src_reg
, insn
->off
);
400 verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
401 insn
->code
, insn
->dst_reg
,
402 bpf_jmp_string
[BPF_OP(insn
->code
) >> 4],
403 insn
->imm
, insn
->off
);
406 verbose("(%02x) %s\n", insn
->code
, bpf_class_string
[class]);
410 static int pop_stack(struct bpf_verifier_env
*env
, int *prev_insn_idx
)
412 struct bpf_verifier_stack_elem
*elem
;
415 if (env
->head
== NULL
)
418 memcpy(&env
->cur_state
, &env
->head
->st
, sizeof(env
->cur_state
));
419 insn_idx
= env
->head
->insn_idx
;
421 *prev_insn_idx
= env
->head
->prev_insn_idx
;
422 elem
= env
->head
->next
;
429 static struct bpf_verifier_state
*push_stack(struct bpf_verifier_env
*env
,
430 int insn_idx
, int prev_insn_idx
)
432 struct bpf_verifier_stack_elem
*elem
;
434 elem
= kmalloc(sizeof(struct bpf_verifier_stack_elem
), GFP_KERNEL
);
438 memcpy(&elem
->st
, &env
->cur_state
, sizeof(env
->cur_state
));
439 elem
->insn_idx
= insn_idx
;
440 elem
->prev_insn_idx
= prev_insn_idx
;
441 elem
->next
= env
->head
;
444 if (env
->stack_size
> BPF_COMPLEXITY_LIMIT_STACK
) {
445 verbose("BPF program is too complex\n");
450 /* pop all elements and return */
451 while (pop_stack(env
, NULL
) >= 0);
455 #define CALLER_SAVED_REGS 6
456 static const int caller_saved
[CALLER_SAVED_REGS
] = {
457 BPF_REG_0
, BPF_REG_1
, BPF_REG_2
, BPF_REG_3
, BPF_REG_4
, BPF_REG_5
460 static void init_reg_state(struct bpf_reg_state
*regs
)
464 for (i
= 0; i
< MAX_BPF_REG
; i
++) {
465 regs
[i
].type
= NOT_INIT
;
467 regs
[i
].min_value
= BPF_REGISTER_MIN_RANGE
;
468 regs
[i
].max_value
= BPF_REGISTER_MAX_RANGE
;
472 regs
[BPF_REG_FP
].type
= FRAME_PTR
;
474 /* 1st arg to a function */
475 regs
[BPF_REG_1
].type
= PTR_TO_CTX
;
478 static void __mark_reg_unknown_value(struct bpf_reg_state
*regs
, u32 regno
)
480 regs
[regno
].type
= UNKNOWN_VALUE
;
485 static void mark_reg_unknown_value(struct bpf_reg_state
*regs
, u32 regno
)
487 BUG_ON(regno
>= MAX_BPF_REG
);
488 __mark_reg_unknown_value(regs
, regno
);
491 static void reset_reg_range_values(struct bpf_reg_state
*regs
, u32 regno
)
493 regs
[regno
].min_value
= BPF_REGISTER_MIN_RANGE
;
494 regs
[regno
].max_value
= BPF_REGISTER_MAX_RANGE
;
497 static void mark_reg_unknown_value_and_range(struct bpf_reg_state
*regs
,
500 mark_reg_unknown_value(regs
, regno
);
501 reset_reg_range_values(regs
, regno
);
505 SRC_OP
, /* register is used as source operand */
506 DST_OP
, /* register is used as destination operand */
507 DST_OP_NO_MARK
/* same as above, check only, don't mark */
510 static int check_reg_arg(struct bpf_reg_state
*regs
, u32 regno
,
513 if (regno
>= MAX_BPF_REG
) {
514 verbose("R%d is invalid\n", regno
);
519 /* check whether register used as source operand can be read */
520 if (regs
[regno
].type
== NOT_INIT
) {
521 verbose("R%d !read_ok\n", regno
);
525 /* check whether register used as dest operand can be written to */
526 if (regno
== BPF_REG_FP
) {
527 verbose("frame pointer is read only\n");
531 mark_reg_unknown_value(regs
, regno
);
536 static int bpf_size_to_bytes(int bpf_size
)
538 if (bpf_size
== BPF_W
)
540 else if (bpf_size
== BPF_H
)
542 else if (bpf_size
== BPF_B
)
544 else if (bpf_size
== BPF_DW
)
550 static bool is_spillable_regtype(enum bpf_reg_type type
)
553 case PTR_TO_MAP_VALUE
:
554 case PTR_TO_MAP_VALUE_OR_NULL
:
555 case PTR_TO_MAP_VALUE_ADJ
:
559 case PTR_TO_PACKET_END
:
561 case CONST_PTR_TO_MAP
:
568 /* check_stack_read/write functions track spill/fill of registers,
569 * stack boundary and alignment are checked in check_mem_access()
571 static int check_stack_write(struct bpf_verifier_state
*state
, int off
,
572 int size
, int value_regno
)
575 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
576 * so it's aligned access and [off, off + size) are within stack limits
579 if (value_regno
>= 0 &&
580 is_spillable_regtype(state
->regs
[value_regno
].type
)) {
582 /* register containing pointer is being spilled into stack */
583 if (size
!= BPF_REG_SIZE
) {
584 verbose("invalid size of register spill\n");
588 /* save register state */
589 state
->spilled_regs
[(MAX_BPF_STACK
+ off
) / BPF_REG_SIZE
] =
590 state
->regs
[value_regno
];
592 for (i
= 0; i
< BPF_REG_SIZE
; i
++)
593 state
->stack_slot_type
[MAX_BPF_STACK
+ off
+ i
] = STACK_SPILL
;
595 /* regular write of data into stack */
596 state
->spilled_regs
[(MAX_BPF_STACK
+ off
) / BPF_REG_SIZE
] =
597 (struct bpf_reg_state
) {};
599 for (i
= 0; i
< size
; i
++)
600 state
->stack_slot_type
[MAX_BPF_STACK
+ off
+ i
] = STACK_MISC
;
605 static int check_stack_read(struct bpf_verifier_state
*state
, int off
, int size
,
611 slot_type
= &state
->stack_slot_type
[MAX_BPF_STACK
+ off
];
613 if (slot_type
[0] == STACK_SPILL
) {
614 if (size
!= BPF_REG_SIZE
) {
615 verbose("invalid size of register spill\n");
618 for (i
= 1; i
< BPF_REG_SIZE
; i
++) {
619 if (slot_type
[i
] != STACK_SPILL
) {
620 verbose("corrupted spill memory\n");
625 if (value_regno
>= 0)
626 /* restore register state from stack */
627 state
->regs
[value_regno
] =
628 state
->spilled_regs
[(MAX_BPF_STACK
+ off
) / BPF_REG_SIZE
];
631 for (i
= 0; i
< size
; i
++) {
632 if (slot_type
[i
] != STACK_MISC
) {
633 verbose("invalid read from stack off %d+%d size %d\n",
638 if (value_regno
>= 0)
639 /* have read misc data from the stack */
640 mark_reg_unknown_value_and_range(state
->regs
,
646 /* check read/write into map element returned by bpf_map_lookup_elem() */
647 static int check_map_access(struct bpf_verifier_env
*env
, u32 regno
, int off
,
650 struct bpf_map
*map
= env
->cur_state
.regs
[regno
].map_ptr
;
652 if (off
< 0 || size
<= 0 || off
+ size
> map
->value_size
) {
653 verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
654 map
->value_size
, off
, size
);
660 /* check read/write into an adjusted map element */
661 static int check_map_access_adj(struct bpf_verifier_env
*env
, u32 regno
,
664 struct bpf_verifier_state
*state
= &env
->cur_state
;
665 struct bpf_reg_state
*reg
= &state
->regs
[regno
];
668 /* We adjusted the register to this map value, so we
669 * need to change off and size to min_value and max_value
670 * respectively to make sure our theoretical access will be
674 print_verifier_state(state
);
675 env
->varlen_map_value_access
= true;
676 /* The minimum value is only important with signed
677 * comparisons where we can't assume the floor of a
678 * value is 0. If we are using signed variables for our
679 * index'es we need to make sure that whatever we use
680 * will have a set floor within our range.
682 if (reg
->min_value
< 0) {
683 verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
687 err
= check_map_access(env
, regno
, reg
->min_value
+ off
, size
);
689 verbose("R%d min value is outside of the array range\n",
694 /* If we haven't set a max value then we need to bail
695 * since we can't be sure we won't do bad things.
697 if (reg
->max_value
== BPF_REGISTER_MAX_RANGE
) {
698 verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
702 return check_map_access(env
, regno
, reg
->max_value
+ off
, size
);
705 #define MAX_PACKET_OFF 0xffff
707 static bool may_access_direct_pkt_data(struct bpf_verifier_env
*env
,
708 const struct bpf_call_arg_meta
*meta
,
709 enum bpf_access_type t
)
711 switch (env
->prog
->type
) {
712 case BPF_PROG_TYPE_LWT_IN
:
713 case BPF_PROG_TYPE_LWT_OUT
:
714 /* dst_input() and dst_output() can't write for now */
718 case BPF_PROG_TYPE_SCHED_CLS
:
719 case BPF_PROG_TYPE_SCHED_ACT
:
720 case BPF_PROG_TYPE_XDP
:
721 case BPF_PROG_TYPE_LWT_XMIT
:
723 return meta
->pkt_access
;
725 env
->seen_direct_write
= true;
732 static int check_packet_access(struct bpf_verifier_env
*env
, u32 regno
, int off
,
735 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
736 struct bpf_reg_state
*reg
= ®s
[regno
];
739 if (off
< 0 || size
<= 0 || off
+ size
> reg
->range
) {
740 verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
741 off
, size
, regno
, reg
->id
, reg
->off
, reg
->range
);
747 /* check access to 'struct bpf_context' fields */
748 static int check_ctx_access(struct bpf_verifier_env
*env
, int off
, int size
,
749 enum bpf_access_type t
, enum bpf_reg_type
*reg_type
)
751 /* for analyzer ctx accesses are already validated and converted */
752 if (env
->analyzer_ops
)
755 if (env
->prog
->aux
->ops
->is_valid_access
&&
756 env
->prog
->aux
->ops
->is_valid_access(off
, size
, t
, reg_type
)) {
757 /* remember the offset of last byte accessed in ctx */
758 if (env
->prog
->aux
->max_ctx_offset
< off
+ size
)
759 env
->prog
->aux
->max_ctx_offset
= off
+ size
;
763 verbose("invalid bpf_context access off=%d size=%d\n", off
, size
);
767 static bool is_pointer_value(struct bpf_verifier_env
*env
, int regno
)
769 if (env
->allow_ptr_leaks
)
772 switch (env
->cur_state
.regs
[regno
].type
) {
781 static int check_pkt_ptr_alignment(const struct bpf_reg_state
*reg
,
784 if (reg
->id
&& size
!= 1) {
785 verbose("Unknown alignment. Only byte-sized access allowed in packet access.\n");
789 /* skb->data is NET_IP_ALIGN-ed */
790 if ((NET_IP_ALIGN
+ reg
->off
+ off
) % size
!= 0) {
791 verbose("misaligned packet access off %d+%d+%d size %d\n",
792 NET_IP_ALIGN
, reg
->off
, off
, size
);
799 static int check_val_ptr_alignment(const struct bpf_reg_state
*reg
,
803 verbose("Unknown alignment. Only byte-sized access allowed in value access.\n");
810 static int check_ptr_alignment(const struct bpf_reg_state
*reg
,
815 return IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
) ? 0 :
816 check_pkt_ptr_alignment(reg
, off
, size
);
817 case PTR_TO_MAP_VALUE_ADJ
:
818 return IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
) ? 0 :
819 check_val_ptr_alignment(reg
, size
);
821 if (off
% size
!= 0) {
822 verbose("misaligned access off %d size %d\n",
831 /* check whether memory at (regno + off) is accessible for t = (read | write)
832 * if t==write, value_regno is a register which value is stored into memory
833 * if t==read, value_regno is a register which will receive the value from memory
834 * if t==write && value_regno==-1, some unknown value is stored into memory
835 * if t==read && value_regno==-1, don't care what we read from memory
837 static int check_mem_access(struct bpf_verifier_env
*env
, u32 regno
, int off
,
838 int bpf_size
, enum bpf_access_type t
,
841 struct bpf_verifier_state
*state
= &env
->cur_state
;
842 struct bpf_reg_state
*reg
= &state
->regs
[regno
];
845 if (reg
->type
== PTR_TO_STACK
)
848 size
= bpf_size_to_bytes(bpf_size
);
852 err
= check_ptr_alignment(reg
, off
, size
);
856 if (reg
->type
== PTR_TO_MAP_VALUE
||
857 reg
->type
== PTR_TO_MAP_VALUE_ADJ
) {
858 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
859 is_pointer_value(env
, value_regno
)) {
860 verbose("R%d leaks addr into map\n", value_regno
);
864 if (reg
->type
== PTR_TO_MAP_VALUE_ADJ
)
865 err
= check_map_access_adj(env
, regno
, off
, size
);
867 err
= check_map_access(env
, regno
, off
, size
);
868 if (!err
&& t
== BPF_READ
&& value_regno
>= 0)
869 mark_reg_unknown_value_and_range(state
->regs
,
872 } else if (reg
->type
== PTR_TO_CTX
) {
873 enum bpf_reg_type reg_type
= UNKNOWN_VALUE
;
875 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
876 is_pointer_value(env
, value_regno
)) {
877 verbose("R%d leaks addr into ctx\n", value_regno
);
880 err
= check_ctx_access(env
, off
, size
, t
, ®_type
);
881 if (!err
&& t
== BPF_READ
&& value_regno
>= 0) {
882 mark_reg_unknown_value_and_range(state
->regs
,
884 /* note that reg.[id|off|range] == 0 */
885 state
->regs
[value_regno
].type
= reg_type
;
888 } else if (reg
->type
== FRAME_PTR
|| reg
->type
== PTR_TO_STACK
) {
889 if (off
>= 0 || off
< -MAX_BPF_STACK
) {
890 verbose("invalid stack off=%d size=%d\n", off
, size
);
893 if (t
== BPF_WRITE
) {
894 if (!env
->allow_ptr_leaks
&&
895 state
->stack_slot_type
[MAX_BPF_STACK
+ off
] == STACK_SPILL
&&
896 size
!= BPF_REG_SIZE
) {
897 verbose("attempt to corrupt spilled pointer on stack\n");
900 err
= check_stack_write(state
, off
, size
, value_regno
);
902 err
= check_stack_read(state
, off
, size
, value_regno
);
904 } else if (state
->regs
[regno
].type
== PTR_TO_PACKET
) {
905 if (t
== BPF_WRITE
&& !may_access_direct_pkt_data(env
, NULL
, t
)) {
906 verbose("cannot write into packet\n");
909 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
910 is_pointer_value(env
, value_regno
)) {
911 verbose("R%d leaks addr into packet\n", value_regno
);
914 err
= check_packet_access(env
, regno
, off
, size
);
915 if (!err
&& t
== BPF_READ
&& value_regno
>= 0)
916 mark_reg_unknown_value_and_range(state
->regs
,
919 verbose("R%d invalid mem access '%s'\n",
920 regno
, reg_type_str
[reg
->type
]);
924 if (!err
&& size
<= 2 && value_regno
>= 0 && env
->allow_ptr_leaks
&&
925 state
->regs
[value_regno
].type
== UNKNOWN_VALUE
) {
926 /* 1 or 2 byte load zero-extends, determine the number of
927 * zero upper bits. Not doing it fo 4 byte load, since
928 * such values cannot be added to ptr_to_packet anyway.
930 state
->regs
[value_regno
].imm
= 64 - size
* 8;
935 static int check_xadd(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
937 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
940 if ((BPF_SIZE(insn
->code
) != BPF_W
&& BPF_SIZE(insn
->code
) != BPF_DW
) ||
942 verbose("BPF_XADD uses reserved fields\n");
946 /* check src1 operand */
947 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
951 /* check src2 operand */
952 err
= check_reg_arg(regs
, insn
->dst_reg
, SRC_OP
);
956 /* check whether atomic_add can read the memory */
957 err
= check_mem_access(env
, insn
->dst_reg
, insn
->off
,
958 BPF_SIZE(insn
->code
), BPF_READ
, -1);
962 /* check whether atomic_add can write into the same memory */
963 return check_mem_access(env
, insn
->dst_reg
, insn
->off
,
964 BPF_SIZE(insn
->code
), BPF_WRITE
, -1);
967 /* when register 'regno' is passed into function that will read 'access_size'
968 * bytes from that pointer, make sure that it's within stack boundary
969 * and all elements of stack are initialized
971 static int check_stack_boundary(struct bpf_verifier_env
*env
, int regno
,
972 int access_size
, bool zero_size_allowed
,
973 struct bpf_call_arg_meta
*meta
)
975 struct bpf_verifier_state
*state
= &env
->cur_state
;
976 struct bpf_reg_state
*regs
= state
->regs
;
979 if (regs
[regno
].type
!= PTR_TO_STACK
) {
980 if (zero_size_allowed
&& access_size
== 0 &&
981 regs
[regno
].type
== CONST_IMM
&&
982 regs
[regno
].imm
== 0)
985 verbose("R%d type=%s expected=%s\n", regno
,
986 reg_type_str
[regs
[regno
].type
],
987 reg_type_str
[PTR_TO_STACK
]);
991 off
= regs
[regno
].imm
;
992 if (off
>= 0 || off
< -MAX_BPF_STACK
|| off
+ access_size
> 0 ||
994 verbose("invalid stack type R%d off=%d access_size=%d\n",
995 regno
, off
, access_size
);
999 if (meta
&& meta
->raw_mode
) {
1000 meta
->access_size
= access_size
;
1001 meta
->regno
= regno
;
1005 for (i
= 0; i
< access_size
; i
++) {
1006 if (state
->stack_slot_type
[MAX_BPF_STACK
+ off
+ i
] != STACK_MISC
) {
1007 verbose("invalid indirect read from stack off %d+%d size %d\n",
1008 off
, i
, access_size
);
1015 static int check_helper_mem_access(struct bpf_verifier_env
*env
, int regno
,
1016 int access_size
, bool zero_size_allowed
,
1017 struct bpf_call_arg_meta
*meta
)
1019 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
1021 switch (regs
[regno
].type
) {
1023 return check_packet_access(env
, regno
, 0, access_size
);
1024 case PTR_TO_MAP_VALUE
:
1025 return check_map_access(env
, regno
, 0, access_size
);
1026 case PTR_TO_MAP_VALUE_ADJ
:
1027 return check_map_access_adj(env
, regno
, 0, access_size
);
1028 default: /* const_imm|ptr_to_stack or invalid ptr */
1029 return check_stack_boundary(env
, regno
, access_size
,
1030 zero_size_allowed
, meta
);
1034 static int check_func_arg(struct bpf_verifier_env
*env
, u32 regno
,
1035 enum bpf_arg_type arg_type
,
1036 struct bpf_call_arg_meta
*meta
)
1038 struct bpf_reg_state
*regs
= env
->cur_state
.regs
, *reg
= ®s
[regno
];
1039 enum bpf_reg_type expected_type
, type
= reg
->type
;
1042 if (arg_type
== ARG_DONTCARE
)
1045 if (type
== NOT_INIT
) {
1046 verbose("R%d !read_ok\n", regno
);
1050 if (arg_type
== ARG_ANYTHING
) {
1051 if (is_pointer_value(env
, regno
)) {
1052 verbose("R%d leaks addr into helper function\n", regno
);
1058 if (type
== PTR_TO_PACKET
&&
1059 !may_access_direct_pkt_data(env
, meta
, BPF_READ
)) {
1060 verbose("helper access to the packet is not allowed\n");
1064 if (arg_type
== ARG_PTR_TO_MAP_KEY
||
1065 arg_type
== ARG_PTR_TO_MAP_VALUE
) {
1066 expected_type
= PTR_TO_STACK
;
1067 if (type
!= PTR_TO_PACKET
&& type
!= expected_type
)
1069 } else if (arg_type
== ARG_CONST_SIZE
||
1070 arg_type
== ARG_CONST_SIZE_OR_ZERO
) {
1071 expected_type
= CONST_IMM
;
1072 /* One exception. Allow UNKNOWN_VALUE registers when the
1073 * boundaries are known and don't cause unsafe memory accesses
1075 if (type
!= UNKNOWN_VALUE
&& type
!= expected_type
)
1077 } else if (arg_type
== ARG_CONST_MAP_PTR
) {
1078 expected_type
= CONST_PTR_TO_MAP
;
1079 if (type
!= expected_type
)
1081 } else if (arg_type
== ARG_PTR_TO_CTX
) {
1082 expected_type
= PTR_TO_CTX
;
1083 if (type
!= expected_type
)
1085 } else if (arg_type
== ARG_PTR_TO_MEM
||
1086 arg_type
== ARG_PTR_TO_UNINIT_MEM
) {
1087 expected_type
= PTR_TO_STACK
;
1088 /* One exception here. In case function allows for NULL to be
1089 * passed in as argument, it's a CONST_IMM type. Final test
1090 * happens during stack boundary checking.
1092 if (type
== CONST_IMM
&& reg
->imm
== 0)
1093 /* final test in check_stack_boundary() */;
1094 else if (type
!= PTR_TO_PACKET
&& type
!= PTR_TO_MAP_VALUE
&&
1095 type
!= PTR_TO_MAP_VALUE_ADJ
&& type
!= expected_type
)
1097 meta
->raw_mode
= arg_type
== ARG_PTR_TO_UNINIT_MEM
;
1099 verbose("unsupported arg_type %d\n", arg_type
);
1103 if (arg_type
== ARG_CONST_MAP_PTR
) {
1104 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
1105 meta
->map_ptr
= reg
->map_ptr
;
1106 } else if (arg_type
== ARG_PTR_TO_MAP_KEY
) {
1107 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1108 * check that [key, key + map->key_size) are within
1109 * stack limits and initialized
1111 if (!meta
->map_ptr
) {
1112 /* in function declaration map_ptr must come before
1113 * map_key, so that it's verified and known before
1114 * we have to check map_key here. Otherwise it means
1115 * that kernel subsystem misconfigured verifier
1117 verbose("invalid map_ptr to access map->key\n");
1120 if (type
== PTR_TO_PACKET
)
1121 err
= check_packet_access(env
, regno
, 0,
1122 meta
->map_ptr
->key_size
);
1124 err
= check_stack_boundary(env
, regno
,
1125 meta
->map_ptr
->key_size
,
1127 } else if (arg_type
== ARG_PTR_TO_MAP_VALUE
) {
1128 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1129 * check [value, value + map->value_size) validity
1131 if (!meta
->map_ptr
) {
1132 /* kernel subsystem misconfigured verifier */
1133 verbose("invalid map_ptr to access map->value\n");
1136 if (type
== PTR_TO_PACKET
)
1137 err
= check_packet_access(env
, regno
, 0,
1138 meta
->map_ptr
->value_size
);
1140 err
= check_stack_boundary(env
, regno
,
1141 meta
->map_ptr
->value_size
,
1143 } else if (arg_type
== ARG_CONST_SIZE
||
1144 arg_type
== ARG_CONST_SIZE_OR_ZERO
) {
1145 bool zero_size_allowed
= (arg_type
== ARG_CONST_SIZE_OR_ZERO
);
1147 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1148 * from stack pointer 'buf'. Check it
1149 * note: regno == len, regno - 1 == buf
1152 /* kernel subsystem misconfigured verifier */
1153 verbose("ARG_CONST_SIZE cannot be first argument\n");
1157 /* If the register is UNKNOWN_VALUE, the access check happens
1158 * using its boundaries. Otherwise, just use its imm
1160 if (type
== UNKNOWN_VALUE
) {
1161 /* For unprivileged variable accesses, disable raw
1162 * mode so that the program is required to
1163 * initialize all the memory that the helper could
1164 * just partially fill up.
1168 if (reg
->min_value
< 0) {
1169 verbose("R%d min value is negative, either use unsigned or 'var &= const'\n",
1174 if (reg
->min_value
== 0) {
1175 err
= check_helper_mem_access(env
, regno
- 1, 0,
1182 if (reg
->max_value
== BPF_REGISTER_MAX_RANGE
) {
1183 verbose("R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
1187 err
= check_helper_mem_access(env
, regno
- 1,
1189 zero_size_allowed
, meta
);
1193 /* register is CONST_IMM */
1194 err
= check_helper_mem_access(env
, regno
- 1, reg
->imm
,
1195 zero_size_allowed
, meta
);
1201 verbose("R%d type=%s expected=%s\n", regno
,
1202 reg_type_str
[type
], reg_type_str
[expected_type
]);
1206 static int check_map_func_compatibility(struct bpf_map
*map
, int func_id
)
1211 /* We need a two way check, first is from map perspective ... */
1212 switch (map
->map_type
) {
1213 case BPF_MAP_TYPE_PROG_ARRAY
:
1214 if (func_id
!= BPF_FUNC_tail_call
)
1217 case BPF_MAP_TYPE_PERF_EVENT_ARRAY
:
1218 if (func_id
!= BPF_FUNC_perf_event_read
&&
1219 func_id
!= BPF_FUNC_perf_event_output
)
1222 case BPF_MAP_TYPE_STACK_TRACE
:
1223 if (func_id
!= BPF_FUNC_get_stackid
)
1226 case BPF_MAP_TYPE_CGROUP_ARRAY
:
1227 if (func_id
!= BPF_FUNC_skb_under_cgroup
&&
1228 func_id
!= BPF_FUNC_current_task_under_cgroup
)
1231 case BPF_MAP_TYPE_ARRAY_OF_MAPS
:
1232 case BPF_MAP_TYPE_HASH_OF_MAPS
:
1233 if (func_id
!= BPF_FUNC_map_lookup_elem
)
1239 /* ... and second from the function itself. */
1241 case BPF_FUNC_tail_call
:
1242 if (map
->map_type
!= BPF_MAP_TYPE_PROG_ARRAY
)
1245 case BPF_FUNC_perf_event_read
:
1246 case BPF_FUNC_perf_event_output
:
1247 if (map
->map_type
!= BPF_MAP_TYPE_PERF_EVENT_ARRAY
)
1250 case BPF_FUNC_get_stackid
:
1251 if (map
->map_type
!= BPF_MAP_TYPE_STACK_TRACE
)
1254 case BPF_FUNC_current_task_under_cgroup
:
1255 case BPF_FUNC_skb_under_cgroup
:
1256 if (map
->map_type
!= BPF_MAP_TYPE_CGROUP_ARRAY
)
1265 verbose("cannot pass map_type %d into func %s#%d\n",
1266 map
->map_type
, func_id_name(func_id
), func_id
);
1270 static int check_raw_mode(const struct bpf_func_proto
*fn
)
1274 if (fn
->arg1_type
== ARG_PTR_TO_UNINIT_MEM
)
1276 if (fn
->arg2_type
== ARG_PTR_TO_UNINIT_MEM
)
1278 if (fn
->arg3_type
== ARG_PTR_TO_UNINIT_MEM
)
1280 if (fn
->arg4_type
== ARG_PTR_TO_UNINIT_MEM
)
1282 if (fn
->arg5_type
== ARG_PTR_TO_UNINIT_MEM
)
1285 return count
> 1 ? -EINVAL
: 0;
1288 static void clear_all_pkt_pointers(struct bpf_verifier_env
*env
)
1290 struct bpf_verifier_state
*state
= &env
->cur_state
;
1291 struct bpf_reg_state
*regs
= state
->regs
, *reg
;
1294 for (i
= 0; i
< MAX_BPF_REG
; i
++)
1295 if (regs
[i
].type
== PTR_TO_PACKET
||
1296 regs
[i
].type
== PTR_TO_PACKET_END
)
1297 mark_reg_unknown_value(regs
, i
);
1299 for (i
= 0; i
< MAX_BPF_STACK
; i
+= BPF_REG_SIZE
) {
1300 if (state
->stack_slot_type
[i
] != STACK_SPILL
)
1302 reg
= &state
->spilled_regs
[i
/ BPF_REG_SIZE
];
1303 if (reg
->type
!= PTR_TO_PACKET
&&
1304 reg
->type
!= PTR_TO_PACKET_END
)
1306 reg
->type
= UNKNOWN_VALUE
;
1311 static int check_call(struct bpf_verifier_env
*env
, int func_id
, int insn_idx
)
1313 struct bpf_verifier_state
*state
= &env
->cur_state
;
1314 const struct bpf_func_proto
*fn
= NULL
;
1315 struct bpf_reg_state
*regs
= state
->regs
;
1316 struct bpf_reg_state
*reg
;
1317 struct bpf_call_arg_meta meta
;
1321 /* find function prototype */
1322 if (func_id
< 0 || func_id
>= __BPF_FUNC_MAX_ID
) {
1323 verbose("invalid func %s#%d\n", func_id_name(func_id
), func_id
);
1327 if (env
->prog
->aux
->ops
->get_func_proto
)
1328 fn
= env
->prog
->aux
->ops
->get_func_proto(func_id
);
1331 verbose("unknown func %s#%d\n", func_id_name(func_id
), func_id
);
1335 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1336 if (!env
->prog
->gpl_compatible
&& fn
->gpl_only
) {
1337 verbose("cannot call GPL only function from proprietary program\n");
1341 changes_data
= bpf_helper_changes_pkt_data(fn
->func
);
1343 memset(&meta
, 0, sizeof(meta
));
1344 meta
.pkt_access
= fn
->pkt_access
;
1346 /* We only support one arg being in raw mode at the moment, which
1347 * is sufficient for the helper functions we have right now.
1349 err
= check_raw_mode(fn
);
1351 verbose("kernel subsystem misconfigured func %s#%d\n",
1352 func_id_name(func_id
), func_id
);
1357 err
= check_func_arg(env
, BPF_REG_1
, fn
->arg1_type
, &meta
);
1360 err
= check_func_arg(env
, BPF_REG_2
, fn
->arg2_type
, &meta
);
1363 err
= check_func_arg(env
, BPF_REG_3
, fn
->arg3_type
, &meta
);
1366 err
= check_func_arg(env
, BPF_REG_4
, fn
->arg4_type
, &meta
);
1369 err
= check_func_arg(env
, BPF_REG_5
, fn
->arg5_type
, &meta
);
1373 /* Mark slots with STACK_MISC in case of raw mode, stack offset
1374 * is inferred from register state.
1376 for (i
= 0; i
< meta
.access_size
; i
++) {
1377 err
= check_mem_access(env
, meta
.regno
, i
, BPF_B
, BPF_WRITE
, -1);
1382 /* reset caller saved regs */
1383 for (i
= 0; i
< CALLER_SAVED_REGS
; i
++) {
1384 reg
= regs
+ caller_saved
[i
];
1385 reg
->type
= NOT_INIT
;
1389 /* update return register */
1390 if (fn
->ret_type
== RET_INTEGER
) {
1391 regs
[BPF_REG_0
].type
= UNKNOWN_VALUE
;
1392 } else if (fn
->ret_type
== RET_VOID
) {
1393 regs
[BPF_REG_0
].type
= NOT_INIT
;
1394 } else if (fn
->ret_type
== RET_PTR_TO_MAP_VALUE_OR_NULL
) {
1395 struct bpf_insn_aux_data
*insn_aux
;
1397 regs
[BPF_REG_0
].type
= PTR_TO_MAP_VALUE_OR_NULL
;
1398 regs
[BPF_REG_0
].max_value
= regs
[BPF_REG_0
].min_value
= 0;
1399 /* remember map_ptr, so that check_map_access()
1400 * can check 'value_size' boundary of memory access
1401 * to map element returned from bpf_map_lookup_elem()
1403 if (meta
.map_ptr
== NULL
) {
1404 verbose("kernel subsystem misconfigured verifier\n");
1407 regs
[BPF_REG_0
].map_ptr
= meta
.map_ptr
;
1408 regs
[BPF_REG_0
].id
= ++env
->id_gen
;
1409 insn_aux
= &env
->insn_aux_data
[insn_idx
];
1410 if (!insn_aux
->map_ptr
)
1411 insn_aux
->map_ptr
= meta
.map_ptr
;
1412 else if (insn_aux
->map_ptr
!= meta
.map_ptr
)
1413 insn_aux
->map_ptr
= BPF_MAP_PTR_POISON
;
1415 verbose("unknown return type %d of func %s#%d\n",
1416 fn
->ret_type
, func_id_name(func_id
), func_id
);
1420 err
= check_map_func_compatibility(meta
.map_ptr
, func_id
);
1425 clear_all_pkt_pointers(env
);
1429 static int check_packet_ptr_add(struct bpf_verifier_env
*env
,
1430 struct bpf_insn
*insn
)
1432 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
1433 struct bpf_reg_state
*dst_reg
= ®s
[insn
->dst_reg
];
1434 struct bpf_reg_state
*src_reg
= ®s
[insn
->src_reg
];
1435 struct bpf_reg_state tmp_reg
;
1438 if (BPF_SRC(insn
->code
) == BPF_K
) {
1439 /* pkt_ptr += imm */
1444 verbose("addition of negative constant to packet pointer is not allowed\n");
1447 if (imm
>= MAX_PACKET_OFF
||
1448 imm
+ dst_reg
->off
>= MAX_PACKET_OFF
) {
1449 verbose("constant %d is too large to add to packet pointer\n",
1453 /* a constant was added to pkt_ptr.
1454 * Remember it while keeping the same 'id'
1456 dst_reg
->off
+= imm
;
1458 if (src_reg
->type
== PTR_TO_PACKET
) {
1459 /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */
1460 tmp_reg
= *dst_reg
; /* save r7 state */
1461 *dst_reg
= *src_reg
; /* copy pkt_ptr state r6 into r7 */
1462 src_reg
= &tmp_reg
; /* pretend it's src_reg state */
1463 /* if the checks below reject it, the copy won't matter,
1464 * since we're rejecting the whole program. If all ok,
1465 * then imm22 state will be added to r7
1466 * and r7 will be pkt(id=0,off=22,r=62) while
1467 * r6 will stay as pkt(id=0,off=0,r=62)
1471 if (src_reg
->type
== CONST_IMM
) {
1472 /* pkt_ptr += reg where reg is known constant */
1476 /* disallow pkt_ptr += reg
1477 * if reg is not uknown_value with guaranteed zero upper bits
1478 * otherwise pkt_ptr may overflow and addition will become
1479 * subtraction which is not allowed
1481 if (src_reg
->type
!= UNKNOWN_VALUE
) {
1482 verbose("cannot add '%s' to ptr_to_packet\n",
1483 reg_type_str
[src_reg
->type
]);
1486 if (src_reg
->imm
< 48) {
1487 verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n",
1491 /* dst_reg stays as pkt_ptr type and since some positive
1492 * integer value was added to the pointer, increment its 'id'
1494 dst_reg
->id
= ++env
->id_gen
;
1496 /* something was added to pkt_ptr, set range and off to zero */
1503 static int evaluate_reg_alu(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
1505 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
1506 struct bpf_reg_state
*dst_reg
= ®s
[insn
->dst_reg
];
1507 u8 opcode
= BPF_OP(insn
->code
);
1510 /* for type == UNKNOWN_VALUE:
1511 * imm > 0 -> number of zero upper bits
1512 * imm == 0 -> don't track which is the same as all bits can be non-zero
1515 if (BPF_SRC(insn
->code
) == BPF_X
) {
1516 struct bpf_reg_state
*src_reg
= ®s
[insn
->src_reg
];
1518 if (src_reg
->type
== UNKNOWN_VALUE
&& src_reg
->imm
> 0 &&
1519 dst_reg
->imm
&& opcode
== BPF_ADD
) {
1521 * where both have zero upper bits. Adding them
1522 * can only result making one more bit non-zero
1523 * in the larger value.
1524 * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
1525 * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
1527 dst_reg
->imm
= min(dst_reg
->imm
, src_reg
->imm
);
1531 if (src_reg
->type
== CONST_IMM
&& src_reg
->imm
> 0 &&
1532 dst_reg
->imm
&& opcode
== BPF_ADD
) {
1534 * where dreg has zero upper bits and sreg is const.
1535 * Adding them can only result making one more bit
1536 * non-zero in the larger value.
1538 imm_log2
= __ilog2_u64((long long)src_reg
->imm
);
1539 dst_reg
->imm
= min(dst_reg
->imm
, 63 - imm_log2
);
1543 /* all other cases non supported yet, just mark dst_reg */
1548 /* sign extend 32-bit imm into 64-bit to make sure that
1549 * negative values occupy bit 63. Note ilog2() would have
1550 * been incorrect, since sizeof(insn->imm) == 4
1552 imm_log2
= __ilog2_u64((long long)insn
->imm
);
1554 if (dst_reg
->imm
&& opcode
== BPF_LSH
) {
1556 * if reg was a result of 2 byte load, then its imm == 48
1557 * which means that upper 48 bits are zero and shifting this reg
1558 * left by 4 would mean that upper 44 bits are still zero
1560 dst_reg
->imm
-= insn
->imm
;
1561 } else if (dst_reg
->imm
&& opcode
== BPF_MUL
) {
1563 * if multiplying by 14 subtract 4
1564 * This is conservative calculation of upper zero bits.
1565 * It's not trying to special case insn->imm == 1 or 0 cases
1567 dst_reg
->imm
-= imm_log2
+ 1;
1568 } else if (opcode
== BPF_AND
) {
1570 dst_reg
->imm
= 63 - imm_log2
;
1571 } else if (dst_reg
->imm
&& opcode
== BPF_ADD
) {
1573 dst_reg
->imm
= min(dst_reg
->imm
, 63 - imm_log2
);
1575 } else if (opcode
== BPF_RSH
) {
1577 * which means that after right shift, upper bits will be zero
1578 * note that verifier already checked that
1579 * 0 <= imm < 64 for shift insn
1581 dst_reg
->imm
+= insn
->imm
;
1582 if (unlikely(dst_reg
->imm
> 64))
1583 /* some dumb code did:
1586 * and all bits are zero now */
1589 /* all other alu ops, means that we don't know what will
1590 * happen to the value, mark it with unknown number of zero bits
1595 if (dst_reg
->imm
< 0) {
1596 /* all 64 bits of the register can contain non-zero bits
1597 * and such value cannot be added to ptr_to_packet, since it
1598 * may overflow, mark it as unknown to avoid further eval
1605 static int evaluate_reg_imm_alu(struct bpf_verifier_env
*env
,
1606 struct bpf_insn
*insn
)
1608 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
1609 struct bpf_reg_state
*dst_reg
= ®s
[insn
->dst_reg
];
1610 struct bpf_reg_state
*src_reg
= ®s
[insn
->src_reg
];
1611 u8 opcode
= BPF_OP(insn
->code
);
1612 u64 dst_imm
= dst_reg
->imm
;
1614 /* dst_reg->type == CONST_IMM here. Simulate execution of insns
1615 * containing ALU ops. Don't care about overflow or negative
1616 * values, just add/sub/... them; registers are in u64.
1618 if (opcode
== BPF_ADD
&& BPF_SRC(insn
->code
) == BPF_K
) {
1619 dst_imm
+= insn
->imm
;
1620 } else if (opcode
== BPF_ADD
&& BPF_SRC(insn
->code
) == BPF_X
&&
1621 src_reg
->type
== CONST_IMM
) {
1622 dst_imm
+= src_reg
->imm
;
1623 } else if (opcode
== BPF_SUB
&& BPF_SRC(insn
->code
) == BPF_K
) {
1624 dst_imm
-= insn
->imm
;
1625 } else if (opcode
== BPF_SUB
&& BPF_SRC(insn
->code
) == BPF_X
&&
1626 src_reg
->type
== CONST_IMM
) {
1627 dst_imm
-= src_reg
->imm
;
1628 } else if (opcode
== BPF_MUL
&& BPF_SRC(insn
->code
) == BPF_K
) {
1629 dst_imm
*= insn
->imm
;
1630 } else if (opcode
== BPF_MUL
&& BPF_SRC(insn
->code
) == BPF_X
&&
1631 src_reg
->type
== CONST_IMM
) {
1632 dst_imm
*= src_reg
->imm
;
1633 } else if (opcode
== BPF_OR
&& BPF_SRC(insn
->code
) == BPF_K
) {
1634 dst_imm
|= insn
->imm
;
1635 } else if (opcode
== BPF_OR
&& BPF_SRC(insn
->code
) == BPF_X
&&
1636 src_reg
->type
== CONST_IMM
) {
1637 dst_imm
|= src_reg
->imm
;
1638 } else if (opcode
== BPF_AND
&& BPF_SRC(insn
->code
) == BPF_K
) {
1639 dst_imm
&= insn
->imm
;
1640 } else if (opcode
== BPF_AND
&& BPF_SRC(insn
->code
) == BPF_X
&&
1641 src_reg
->type
== CONST_IMM
) {
1642 dst_imm
&= src_reg
->imm
;
1643 } else if (opcode
== BPF_RSH
&& BPF_SRC(insn
->code
) == BPF_K
) {
1644 dst_imm
>>= insn
->imm
;
1645 } else if (opcode
== BPF_RSH
&& BPF_SRC(insn
->code
) == BPF_X
&&
1646 src_reg
->type
== CONST_IMM
) {
1647 dst_imm
>>= src_reg
->imm
;
1648 } else if (opcode
== BPF_LSH
&& BPF_SRC(insn
->code
) == BPF_K
) {
1649 dst_imm
<<= insn
->imm
;
1650 } else if (opcode
== BPF_LSH
&& BPF_SRC(insn
->code
) == BPF_X
&&
1651 src_reg
->type
== CONST_IMM
) {
1652 dst_imm
<<= src_reg
->imm
;
1654 mark_reg_unknown_value(regs
, insn
->dst_reg
);
1658 dst_reg
->imm
= dst_imm
;
1663 static void check_reg_overflow(struct bpf_reg_state
*reg
)
1665 if (reg
->max_value
> BPF_REGISTER_MAX_RANGE
)
1666 reg
->max_value
= BPF_REGISTER_MAX_RANGE
;
1667 if (reg
->min_value
< BPF_REGISTER_MIN_RANGE
||
1668 reg
->min_value
> BPF_REGISTER_MAX_RANGE
)
1669 reg
->min_value
= BPF_REGISTER_MIN_RANGE
;
1672 static void adjust_reg_min_max_vals(struct bpf_verifier_env
*env
,
1673 struct bpf_insn
*insn
)
1675 struct bpf_reg_state
*regs
= env
->cur_state
.regs
, *dst_reg
;
1676 s64 min_val
= BPF_REGISTER_MIN_RANGE
;
1677 u64 max_val
= BPF_REGISTER_MAX_RANGE
;
1678 u8 opcode
= BPF_OP(insn
->code
);
1680 dst_reg
= ®s
[insn
->dst_reg
];
1681 if (BPF_SRC(insn
->code
) == BPF_X
) {
1682 check_reg_overflow(®s
[insn
->src_reg
]);
1683 min_val
= regs
[insn
->src_reg
].min_value
;
1684 max_val
= regs
[insn
->src_reg
].max_value
;
1686 /* If the source register is a random pointer then the
1687 * min_value/max_value values represent the range of the known
1688 * accesses into that value, not the actual min/max value of the
1689 * register itself. In this case we have to reset the reg range
1690 * values so we know it is not safe to look at.
1692 if (regs
[insn
->src_reg
].type
!= CONST_IMM
&&
1693 regs
[insn
->src_reg
].type
!= UNKNOWN_VALUE
) {
1694 min_val
= BPF_REGISTER_MIN_RANGE
;
1695 max_val
= BPF_REGISTER_MAX_RANGE
;
1697 } else if (insn
->imm
< BPF_REGISTER_MAX_RANGE
&&
1698 (s64
)insn
->imm
> BPF_REGISTER_MIN_RANGE
) {
1699 min_val
= max_val
= insn
->imm
;
1702 /* We don't know anything about what was done to this register, mark it
1705 if (min_val
== BPF_REGISTER_MIN_RANGE
&&
1706 max_val
== BPF_REGISTER_MAX_RANGE
) {
1707 reset_reg_range_values(regs
, insn
->dst_reg
);
1711 /* If one of our values was at the end of our ranges then we can't just
1712 * do our normal operations to the register, we need to set the values
1713 * to the min/max since they are undefined.
1715 if (min_val
== BPF_REGISTER_MIN_RANGE
)
1716 dst_reg
->min_value
= BPF_REGISTER_MIN_RANGE
;
1717 if (max_val
== BPF_REGISTER_MAX_RANGE
)
1718 dst_reg
->max_value
= BPF_REGISTER_MAX_RANGE
;
1722 if (dst_reg
->min_value
!= BPF_REGISTER_MIN_RANGE
)
1723 dst_reg
->min_value
+= min_val
;
1724 if (dst_reg
->max_value
!= BPF_REGISTER_MAX_RANGE
)
1725 dst_reg
->max_value
+= max_val
;
1728 if (dst_reg
->min_value
!= BPF_REGISTER_MIN_RANGE
)
1729 dst_reg
->min_value
-= min_val
;
1730 if (dst_reg
->max_value
!= BPF_REGISTER_MAX_RANGE
)
1731 dst_reg
->max_value
-= max_val
;
1734 if (dst_reg
->min_value
!= BPF_REGISTER_MIN_RANGE
)
1735 dst_reg
->min_value
*= min_val
;
1736 if (dst_reg
->max_value
!= BPF_REGISTER_MAX_RANGE
)
1737 dst_reg
->max_value
*= max_val
;
1740 /* Disallow AND'ing of negative numbers, ain't nobody got time
1741 * for that. Otherwise the minimum is 0 and the max is the max
1742 * value we could AND against.
1745 dst_reg
->min_value
= BPF_REGISTER_MIN_RANGE
;
1747 dst_reg
->min_value
= 0;
1748 dst_reg
->max_value
= max_val
;
1751 /* Gotta have special overflow logic here, if we're shifting
1752 * more than MAX_RANGE then just assume we have an invalid
1755 if (min_val
> ilog2(BPF_REGISTER_MAX_RANGE
))
1756 dst_reg
->min_value
= BPF_REGISTER_MIN_RANGE
;
1757 else if (dst_reg
->min_value
!= BPF_REGISTER_MIN_RANGE
)
1758 dst_reg
->min_value
<<= min_val
;
1760 if (max_val
> ilog2(BPF_REGISTER_MAX_RANGE
))
1761 dst_reg
->max_value
= BPF_REGISTER_MAX_RANGE
;
1762 else if (dst_reg
->max_value
!= BPF_REGISTER_MAX_RANGE
)
1763 dst_reg
->max_value
<<= max_val
;
1766 /* RSH by a negative number is undefined, and the BPF_RSH is an
1767 * unsigned shift, so make the appropriate casts.
1769 if (min_val
< 0 || dst_reg
->min_value
< 0)
1770 dst_reg
->min_value
= BPF_REGISTER_MIN_RANGE
;
1772 dst_reg
->min_value
=
1773 (u64
)(dst_reg
->min_value
) >> min_val
;
1774 if (dst_reg
->max_value
!= BPF_REGISTER_MAX_RANGE
)
1775 dst_reg
->max_value
>>= max_val
;
1778 reset_reg_range_values(regs
, insn
->dst_reg
);
1782 check_reg_overflow(dst_reg
);
1785 /* check validity of 32-bit and 64-bit arithmetic operations */
1786 static int check_alu_op(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
1788 struct bpf_reg_state
*regs
= env
->cur_state
.regs
, *dst_reg
;
1789 u8 opcode
= BPF_OP(insn
->code
);
1792 if (opcode
== BPF_END
|| opcode
== BPF_NEG
) {
1793 if (opcode
== BPF_NEG
) {
1794 if (BPF_SRC(insn
->code
) != 0 ||
1795 insn
->src_reg
!= BPF_REG_0
||
1796 insn
->off
!= 0 || insn
->imm
!= 0) {
1797 verbose("BPF_NEG uses reserved fields\n");
1801 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0 ||
1802 (insn
->imm
!= 16 && insn
->imm
!= 32 && insn
->imm
!= 64)) {
1803 verbose("BPF_END uses reserved fields\n");
1808 /* check src operand */
1809 err
= check_reg_arg(regs
, insn
->dst_reg
, SRC_OP
);
1813 if (is_pointer_value(env
, insn
->dst_reg
)) {
1814 verbose("R%d pointer arithmetic prohibited\n",
1819 /* check dest operand */
1820 err
= check_reg_arg(regs
, insn
->dst_reg
, DST_OP
);
1824 } else if (opcode
== BPF_MOV
) {
1826 if (BPF_SRC(insn
->code
) == BPF_X
) {
1827 if (insn
->imm
!= 0 || insn
->off
!= 0) {
1828 verbose("BPF_MOV uses reserved fields\n");
1832 /* check src operand */
1833 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
1837 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0) {
1838 verbose("BPF_MOV uses reserved fields\n");
1843 /* check dest operand */
1844 err
= check_reg_arg(regs
, insn
->dst_reg
, DST_OP
);
1848 /* we are setting our register to something new, we need to
1849 * reset its range values.
1851 reset_reg_range_values(regs
, insn
->dst_reg
);
1853 if (BPF_SRC(insn
->code
) == BPF_X
) {
1854 if (BPF_CLASS(insn
->code
) == BPF_ALU64
) {
1856 * copy register state to dest reg
1858 regs
[insn
->dst_reg
] = regs
[insn
->src_reg
];
1860 if (is_pointer_value(env
, insn
->src_reg
)) {
1861 verbose("R%d partial copy of pointer\n",
1865 mark_reg_unknown_value(regs
, insn
->dst_reg
);
1869 * remember the value we stored into this reg
1871 regs
[insn
->dst_reg
].type
= CONST_IMM
;
1872 regs
[insn
->dst_reg
].imm
= insn
->imm
;
1873 regs
[insn
->dst_reg
].max_value
= insn
->imm
;
1874 regs
[insn
->dst_reg
].min_value
= insn
->imm
;
1877 } else if (opcode
> BPF_END
) {
1878 verbose("invalid BPF_ALU opcode %x\n", opcode
);
1881 } else { /* all other ALU ops: and, sub, xor, add, ... */
1883 if (BPF_SRC(insn
->code
) == BPF_X
) {
1884 if (insn
->imm
!= 0 || insn
->off
!= 0) {
1885 verbose("BPF_ALU uses reserved fields\n");
1888 /* check src1 operand */
1889 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
1893 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0) {
1894 verbose("BPF_ALU uses reserved fields\n");
1899 /* check src2 operand */
1900 err
= check_reg_arg(regs
, insn
->dst_reg
, SRC_OP
);
1904 if ((opcode
== BPF_MOD
|| opcode
== BPF_DIV
) &&
1905 BPF_SRC(insn
->code
) == BPF_K
&& insn
->imm
== 0) {
1906 verbose("div by zero\n");
1910 if ((opcode
== BPF_LSH
|| opcode
== BPF_RSH
||
1911 opcode
== BPF_ARSH
) && BPF_SRC(insn
->code
) == BPF_K
) {
1912 int size
= BPF_CLASS(insn
->code
) == BPF_ALU64
? 64 : 32;
1914 if (insn
->imm
< 0 || insn
->imm
>= size
) {
1915 verbose("invalid shift %d\n", insn
->imm
);
1920 /* check dest operand */
1921 err
= check_reg_arg(regs
, insn
->dst_reg
, DST_OP_NO_MARK
);
1925 dst_reg
= ®s
[insn
->dst_reg
];
1927 /* first we want to adjust our ranges. */
1928 adjust_reg_min_max_vals(env
, insn
);
1930 /* pattern match 'bpf_add Rx, imm' instruction */
1931 if (opcode
== BPF_ADD
&& BPF_CLASS(insn
->code
) == BPF_ALU64
&&
1932 dst_reg
->type
== FRAME_PTR
&& BPF_SRC(insn
->code
) == BPF_K
) {
1933 dst_reg
->type
= PTR_TO_STACK
;
1934 dst_reg
->imm
= insn
->imm
;
1936 } else if (opcode
== BPF_ADD
&&
1937 BPF_CLASS(insn
->code
) == BPF_ALU64
&&
1938 dst_reg
->type
== PTR_TO_STACK
&&
1939 ((BPF_SRC(insn
->code
) == BPF_X
&&
1940 regs
[insn
->src_reg
].type
== CONST_IMM
) ||
1941 BPF_SRC(insn
->code
) == BPF_K
)) {
1942 if (BPF_SRC(insn
->code
) == BPF_X
)
1943 dst_reg
->imm
+= regs
[insn
->src_reg
].imm
;
1945 dst_reg
->imm
+= insn
->imm
;
1947 } else if (opcode
== BPF_ADD
&&
1948 BPF_CLASS(insn
->code
) == BPF_ALU64
&&
1949 (dst_reg
->type
== PTR_TO_PACKET
||
1950 (BPF_SRC(insn
->code
) == BPF_X
&&
1951 regs
[insn
->src_reg
].type
== PTR_TO_PACKET
))) {
1952 /* ptr_to_packet += K|X */
1953 return check_packet_ptr_add(env
, insn
);
1954 } else if (BPF_CLASS(insn
->code
) == BPF_ALU64
&&
1955 dst_reg
->type
== UNKNOWN_VALUE
&&
1956 env
->allow_ptr_leaks
) {
1957 /* unknown += K|X */
1958 return evaluate_reg_alu(env
, insn
);
1959 } else if (BPF_CLASS(insn
->code
) == BPF_ALU64
&&
1960 dst_reg
->type
== CONST_IMM
&&
1961 env
->allow_ptr_leaks
) {
1962 /* reg_imm += K|X */
1963 return evaluate_reg_imm_alu(env
, insn
);
1964 } else if (is_pointer_value(env
, insn
->dst_reg
)) {
1965 verbose("R%d pointer arithmetic prohibited\n",
1968 } else if (BPF_SRC(insn
->code
) == BPF_X
&&
1969 is_pointer_value(env
, insn
->src_reg
)) {
1970 verbose("R%d pointer arithmetic prohibited\n",
1975 /* If we did pointer math on a map value then just set it to our
1976 * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or
1977 * loads to this register appropriately, otherwise just mark the
1978 * register as unknown.
1980 if (env
->allow_ptr_leaks
&&
1981 BPF_CLASS(insn
->code
) == BPF_ALU64
&& opcode
== BPF_ADD
&&
1982 (dst_reg
->type
== PTR_TO_MAP_VALUE
||
1983 dst_reg
->type
== PTR_TO_MAP_VALUE_ADJ
))
1984 dst_reg
->type
= PTR_TO_MAP_VALUE_ADJ
;
1986 mark_reg_unknown_value(regs
, insn
->dst_reg
);
1992 static void find_good_pkt_pointers(struct bpf_verifier_state
*state
,
1993 struct bpf_reg_state
*dst_reg
)
1995 struct bpf_reg_state
*regs
= state
->regs
, *reg
;
1998 /* LLVM can generate two kind of checks:
2004 * if (r2 > pkt_end) goto <handle exception>
2008 * r2 == dst_reg, pkt_end == src_reg
2009 * r2=pkt(id=n,off=8,r=0)
2010 * r3=pkt(id=n,off=0,r=0)
2016 * if (pkt_end >= r2) goto <access okay>
2017 * <handle exception>
2020 * pkt_end == dst_reg, r2 == src_reg
2021 * r2=pkt(id=n,off=8,r=0)
2022 * r3=pkt(id=n,off=0,r=0)
2024 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
2025 * so that range of bytes [r3, r3 + 8) is safe to access.
2028 for (i
= 0; i
< MAX_BPF_REG
; i
++)
2029 if (regs
[i
].type
== PTR_TO_PACKET
&& regs
[i
].id
== dst_reg
->id
)
2030 /* keep the maximum range already checked */
2031 regs
[i
].range
= max(regs
[i
].range
, dst_reg
->off
);
2033 for (i
= 0; i
< MAX_BPF_STACK
; i
+= BPF_REG_SIZE
) {
2034 if (state
->stack_slot_type
[i
] != STACK_SPILL
)
2036 reg
= &state
->spilled_regs
[i
/ BPF_REG_SIZE
];
2037 if (reg
->type
== PTR_TO_PACKET
&& reg
->id
== dst_reg
->id
)
2038 reg
->range
= max(reg
->range
, dst_reg
->off
);
2042 /* Adjusts the register min/max values in the case that the dst_reg is the
2043 * variable register that we are working on, and src_reg is a constant or we're
2044 * simply doing a BPF_K check.
2046 static void reg_set_min_max(struct bpf_reg_state
*true_reg
,
2047 struct bpf_reg_state
*false_reg
, u64 val
,
2052 /* If this is false then we know nothing Jon Snow, but if it is
2053 * true then we know for sure.
2055 true_reg
->max_value
= true_reg
->min_value
= val
;
2058 /* If this is true we know nothing Jon Snow, but if it is false
2059 * we know the value for sure;
2061 false_reg
->max_value
= false_reg
->min_value
= val
;
2064 /* Unsigned comparison, the minimum value is 0. */
2065 false_reg
->min_value
= 0;
2068 /* If this is false then we know the maximum val is val,
2069 * otherwise we know the min val is val+1.
2071 false_reg
->max_value
= val
;
2072 true_reg
->min_value
= val
+ 1;
2075 /* Unsigned comparison, the minimum value is 0. */
2076 false_reg
->min_value
= 0;
2079 /* If this is false then we know the maximum value is val - 1,
2080 * otherwise we know the mimimum value is val.
2082 false_reg
->max_value
= val
- 1;
2083 true_reg
->min_value
= val
;
2089 check_reg_overflow(false_reg
);
2090 check_reg_overflow(true_reg
);
2093 /* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg
2094 * is the variable reg.
2096 static void reg_set_min_max_inv(struct bpf_reg_state
*true_reg
,
2097 struct bpf_reg_state
*false_reg
, u64 val
,
2102 /* If this is false then we know nothing Jon Snow, but if it is
2103 * true then we know for sure.
2105 true_reg
->max_value
= true_reg
->min_value
= val
;
2108 /* If this is true we know nothing Jon Snow, but if it is false
2109 * we know the value for sure;
2111 false_reg
->max_value
= false_reg
->min_value
= val
;
2114 /* Unsigned comparison, the minimum value is 0. */
2115 true_reg
->min_value
= 0;
2119 * If this is false, then the val is <= the register, if it is
2120 * true the register <= to the val.
2122 false_reg
->min_value
= val
;
2123 true_reg
->max_value
= val
- 1;
2126 /* Unsigned comparison, the minimum value is 0. */
2127 true_reg
->min_value
= 0;
2130 /* If this is false then constant < register, if it is true then
2131 * the register < constant.
2133 false_reg
->min_value
= val
+ 1;
2134 true_reg
->max_value
= val
;
2140 check_reg_overflow(false_reg
);
2141 check_reg_overflow(true_reg
);
2144 static void mark_map_reg(struct bpf_reg_state
*regs
, u32 regno
, u32 id
,
2145 enum bpf_reg_type type
)
2147 struct bpf_reg_state
*reg
= ®s
[regno
];
2149 if (reg
->type
== PTR_TO_MAP_VALUE_OR_NULL
&& reg
->id
== id
) {
2150 if (type
== UNKNOWN_VALUE
) {
2151 __mark_reg_unknown_value(regs
, regno
);
2152 } else if (reg
->map_ptr
->inner_map_meta
) {
2153 reg
->type
= CONST_PTR_TO_MAP
;
2154 reg
->map_ptr
= reg
->map_ptr
->inner_map_meta
;
2158 /* We don't need id from this point onwards anymore, thus we
2159 * should better reset it, so that state pruning has chances
2166 /* The logic is similar to find_good_pkt_pointers(), both could eventually
2167 * be folded together at some point.
2169 static void mark_map_regs(struct bpf_verifier_state
*state
, u32 regno
,
2170 enum bpf_reg_type type
)
2172 struct bpf_reg_state
*regs
= state
->regs
;
2173 u32 id
= regs
[regno
].id
;
2176 for (i
= 0; i
< MAX_BPF_REG
; i
++)
2177 mark_map_reg(regs
, i
, id
, type
);
2179 for (i
= 0; i
< MAX_BPF_STACK
; i
+= BPF_REG_SIZE
) {
2180 if (state
->stack_slot_type
[i
] != STACK_SPILL
)
2182 mark_map_reg(state
->spilled_regs
, i
/ BPF_REG_SIZE
, id
, type
);
2186 static int check_cond_jmp_op(struct bpf_verifier_env
*env
,
2187 struct bpf_insn
*insn
, int *insn_idx
)
2189 struct bpf_verifier_state
*other_branch
, *this_branch
= &env
->cur_state
;
2190 struct bpf_reg_state
*regs
= this_branch
->regs
, *dst_reg
;
2191 u8 opcode
= BPF_OP(insn
->code
);
2194 if (opcode
> BPF_EXIT
) {
2195 verbose("invalid BPF_JMP opcode %x\n", opcode
);
2199 if (BPF_SRC(insn
->code
) == BPF_X
) {
2200 if (insn
->imm
!= 0) {
2201 verbose("BPF_JMP uses reserved fields\n");
2205 /* check src1 operand */
2206 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
2210 if (is_pointer_value(env
, insn
->src_reg
)) {
2211 verbose("R%d pointer comparison prohibited\n",
2216 if (insn
->src_reg
!= BPF_REG_0
) {
2217 verbose("BPF_JMP uses reserved fields\n");
2222 /* check src2 operand */
2223 err
= check_reg_arg(regs
, insn
->dst_reg
, SRC_OP
);
2227 dst_reg
= ®s
[insn
->dst_reg
];
2229 /* detect if R == 0 where R was initialized to zero earlier */
2230 if (BPF_SRC(insn
->code
) == BPF_K
&&
2231 (opcode
== BPF_JEQ
|| opcode
== BPF_JNE
) &&
2232 dst_reg
->type
== CONST_IMM
&& dst_reg
->imm
== insn
->imm
) {
2233 if (opcode
== BPF_JEQ
) {
2234 /* if (imm == imm) goto pc+off;
2235 * only follow the goto, ignore fall-through
2237 *insn_idx
+= insn
->off
;
2240 /* if (imm != imm) goto pc+off;
2241 * only follow fall-through branch, since
2242 * that's where the program will go
2248 other_branch
= push_stack(env
, *insn_idx
+ insn
->off
+ 1, *insn_idx
);
2252 /* detect if we are comparing against a constant value so we can adjust
2253 * our min/max values for our dst register.
2255 if (BPF_SRC(insn
->code
) == BPF_X
) {
2256 if (regs
[insn
->src_reg
].type
== CONST_IMM
)
2257 reg_set_min_max(&other_branch
->regs
[insn
->dst_reg
],
2258 dst_reg
, regs
[insn
->src_reg
].imm
,
2260 else if (dst_reg
->type
== CONST_IMM
)
2261 reg_set_min_max_inv(&other_branch
->regs
[insn
->src_reg
],
2262 ®s
[insn
->src_reg
], dst_reg
->imm
,
2265 reg_set_min_max(&other_branch
->regs
[insn
->dst_reg
],
2266 dst_reg
, insn
->imm
, opcode
);
2269 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
2270 if (BPF_SRC(insn
->code
) == BPF_K
&&
2271 insn
->imm
== 0 && (opcode
== BPF_JEQ
|| opcode
== BPF_JNE
) &&
2272 dst_reg
->type
== PTR_TO_MAP_VALUE_OR_NULL
) {
2273 /* Mark all identical map registers in each branch as either
2274 * safe or unknown depending R == 0 or R != 0 conditional.
2276 mark_map_regs(this_branch
, insn
->dst_reg
,
2277 opcode
== BPF_JEQ
? PTR_TO_MAP_VALUE
: UNKNOWN_VALUE
);
2278 mark_map_regs(other_branch
, insn
->dst_reg
,
2279 opcode
== BPF_JEQ
? UNKNOWN_VALUE
: PTR_TO_MAP_VALUE
);
2280 } else if (BPF_SRC(insn
->code
) == BPF_X
&& opcode
== BPF_JGT
&&
2281 dst_reg
->type
== PTR_TO_PACKET
&&
2282 regs
[insn
->src_reg
].type
== PTR_TO_PACKET_END
) {
2283 find_good_pkt_pointers(this_branch
, dst_reg
);
2284 } else if (BPF_SRC(insn
->code
) == BPF_X
&& opcode
== BPF_JGE
&&
2285 dst_reg
->type
== PTR_TO_PACKET_END
&&
2286 regs
[insn
->src_reg
].type
== PTR_TO_PACKET
) {
2287 find_good_pkt_pointers(other_branch
, ®s
[insn
->src_reg
]);
2288 } else if (is_pointer_value(env
, insn
->dst_reg
)) {
2289 verbose("R%d pointer comparison prohibited\n", insn
->dst_reg
);
2293 print_verifier_state(this_branch
);
2297 /* return the map pointer stored inside BPF_LD_IMM64 instruction */
2298 static struct bpf_map
*ld_imm64_to_map_ptr(struct bpf_insn
*insn
)
2300 u64 imm64
= ((u64
) (u32
) insn
[0].imm
) | ((u64
) (u32
) insn
[1].imm
) << 32;
2302 return (struct bpf_map
*) (unsigned long) imm64
;
2305 /* verify BPF_LD_IMM64 instruction */
2306 static int check_ld_imm(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
2308 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
2311 if (BPF_SIZE(insn
->code
) != BPF_DW
) {
2312 verbose("invalid BPF_LD_IMM insn\n");
2315 if (insn
->off
!= 0) {
2316 verbose("BPF_LD_IMM64 uses reserved fields\n");
2320 err
= check_reg_arg(regs
, insn
->dst_reg
, DST_OP
);
2324 if (insn
->src_reg
== 0) {
2325 u64 imm
= ((u64
)(insn
+ 1)->imm
<< 32) | (u32
)insn
->imm
;
2327 regs
[insn
->dst_reg
].type
= CONST_IMM
;
2328 regs
[insn
->dst_reg
].imm
= imm
;
2332 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
2333 BUG_ON(insn
->src_reg
!= BPF_PSEUDO_MAP_FD
);
2335 regs
[insn
->dst_reg
].type
= CONST_PTR_TO_MAP
;
2336 regs
[insn
->dst_reg
].map_ptr
= ld_imm64_to_map_ptr(insn
);
2340 static bool may_access_skb(enum bpf_prog_type type
)
2343 case BPF_PROG_TYPE_SOCKET_FILTER
:
2344 case BPF_PROG_TYPE_SCHED_CLS
:
2345 case BPF_PROG_TYPE_SCHED_ACT
:
2352 /* verify safety of LD_ABS|LD_IND instructions:
2353 * - they can only appear in the programs where ctx == skb
2354 * - since they are wrappers of function calls, they scratch R1-R5 registers,
2355 * preserve R6-R9, and store return value into R0
2358 * ctx == skb == R6 == CTX
2361 * SRC == any register
2362 * IMM == 32-bit immediate
2365 * R0 - 8/16/32-bit skb data converted to cpu endianness
2367 static int check_ld_abs(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
2369 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
2370 u8 mode
= BPF_MODE(insn
->code
);
2371 struct bpf_reg_state
*reg
;
2374 if (!may_access_skb(env
->prog
->type
)) {
2375 verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
2379 if (insn
->dst_reg
!= BPF_REG_0
|| insn
->off
!= 0 ||
2380 BPF_SIZE(insn
->code
) == BPF_DW
||
2381 (mode
== BPF_ABS
&& insn
->src_reg
!= BPF_REG_0
)) {
2382 verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
2386 /* check whether implicit source operand (register R6) is readable */
2387 err
= check_reg_arg(regs
, BPF_REG_6
, SRC_OP
);
2391 if (regs
[BPF_REG_6
].type
!= PTR_TO_CTX
) {
2392 verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
2396 if (mode
== BPF_IND
) {
2397 /* check explicit source operand */
2398 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
2403 /* reset caller saved regs to unreadable */
2404 for (i
= 0; i
< CALLER_SAVED_REGS
; i
++) {
2405 reg
= regs
+ caller_saved
[i
];
2406 reg
->type
= NOT_INIT
;
2410 /* mark destination R0 register as readable, since it contains
2411 * the value fetched from the packet
2413 regs
[BPF_REG_0
].type
= UNKNOWN_VALUE
;
2417 /* non-recursive DFS pseudo code
2418 * 1 procedure DFS-iterative(G,v):
2419 * 2 label v as discovered
2420 * 3 let S be a stack
2422 * 5 while S is not empty
2424 * 7 if t is what we're looking for:
2426 * 9 for all edges e in G.adjacentEdges(t) do
2427 * 10 if edge e is already labelled
2428 * 11 continue with the next edge
2429 * 12 w <- G.adjacentVertex(t,e)
2430 * 13 if vertex w is not discovered and not explored
2431 * 14 label e as tree-edge
2432 * 15 label w as discovered
2435 * 18 else if vertex w is discovered
2436 * 19 label e as back-edge
2438 * 21 // vertex w is explored
2439 * 22 label e as forward- or cross-edge
2440 * 23 label t as explored
2445 * 0x11 - discovered and fall-through edge labelled
2446 * 0x12 - discovered and fall-through and branch edges labelled
2457 #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
2459 static int *insn_stack
; /* stack of insns to process */
2460 static int cur_stack
; /* current stack index */
2461 static int *insn_state
;
2463 /* t, w, e - match pseudo-code above:
2464 * t - index of current instruction
2465 * w - next instruction
2468 static int push_insn(int t
, int w
, int e
, struct bpf_verifier_env
*env
)
2470 if (e
== FALLTHROUGH
&& insn_state
[t
] >= (DISCOVERED
| FALLTHROUGH
))
2473 if (e
== BRANCH
&& insn_state
[t
] >= (DISCOVERED
| BRANCH
))
2476 if (w
< 0 || w
>= env
->prog
->len
) {
2477 verbose("jump out of range from insn %d to %d\n", t
, w
);
2482 /* mark branch target for state pruning */
2483 env
->explored_states
[w
] = STATE_LIST_MARK
;
2485 if (insn_state
[w
] == 0) {
2487 insn_state
[t
] = DISCOVERED
| e
;
2488 insn_state
[w
] = DISCOVERED
;
2489 if (cur_stack
>= env
->prog
->len
)
2491 insn_stack
[cur_stack
++] = w
;
2493 } else if ((insn_state
[w
] & 0xF0) == DISCOVERED
) {
2494 verbose("back-edge from insn %d to %d\n", t
, w
);
2496 } else if (insn_state
[w
] == EXPLORED
) {
2497 /* forward- or cross-edge */
2498 insn_state
[t
] = DISCOVERED
| e
;
2500 verbose("insn state internal bug\n");
2506 /* non-recursive depth-first-search to detect loops in BPF program
2507 * loop == back-edge in directed graph
2509 static int check_cfg(struct bpf_verifier_env
*env
)
2511 struct bpf_insn
*insns
= env
->prog
->insnsi
;
2512 int insn_cnt
= env
->prog
->len
;
2516 insn_state
= kcalloc(insn_cnt
, sizeof(int), GFP_KERNEL
);
2520 insn_stack
= kcalloc(insn_cnt
, sizeof(int), GFP_KERNEL
);
2526 insn_state
[0] = DISCOVERED
; /* mark 1st insn as discovered */
2527 insn_stack
[0] = 0; /* 0 is the first instruction */
2533 t
= insn_stack
[cur_stack
- 1];
2535 if (BPF_CLASS(insns
[t
].code
) == BPF_JMP
) {
2536 u8 opcode
= BPF_OP(insns
[t
].code
);
2538 if (opcode
== BPF_EXIT
) {
2540 } else if (opcode
== BPF_CALL
) {
2541 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
2546 if (t
+ 1 < insn_cnt
)
2547 env
->explored_states
[t
+ 1] = STATE_LIST_MARK
;
2548 } else if (opcode
== BPF_JA
) {
2549 if (BPF_SRC(insns
[t
].code
) != BPF_K
) {
2553 /* unconditional jump with single edge */
2554 ret
= push_insn(t
, t
+ insns
[t
].off
+ 1,
2560 /* tell verifier to check for equivalent states
2561 * after every call and jump
2563 if (t
+ 1 < insn_cnt
)
2564 env
->explored_states
[t
+ 1] = STATE_LIST_MARK
;
2566 /* conditional jump with two edges */
2567 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
2573 ret
= push_insn(t
, t
+ insns
[t
].off
+ 1, BRANCH
, env
);
2580 /* all other non-branch instructions with single
2583 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
2591 insn_state
[t
] = EXPLORED
;
2592 if (cur_stack
-- <= 0) {
2593 verbose("pop stack internal bug\n");
2600 for (i
= 0; i
< insn_cnt
; i
++) {
2601 if (insn_state
[i
] != EXPLORED
) {
2602 verbose("unreachable insn %d\n", i
);
2607 ret
= 0; /* cfg looks good */
2615 /* the following conditions reduce the number of explored insns
2616 * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet
2618 static bool compare_ptrs_to_packet(struct bpf_reg_state
*old
,
2619 struct bpf_reg_state
*cur
)
2621 if (old
->id
!= cur
->id
)
2624 /* old ptr_to_packet is more conservative, since it allows smaller
2626 * old(off=0,r=10) is equal to cur(off=0,r=20), because
2627 * old(off=0,r=10) means that with range=10 the verifier proceeded
2628 * further and found no issues with the program. Now we're in the same
2629 * spot with cur(off=0,r=20), so we're safe too, since anything further
2630 * will only be looking at most 10 bytes after this pointer.
2632 if (old
->off
== cur
->off
&& old
->range
< cur
->range
)
2635 /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0)
2636 * since both cannot be used for packet access and safe(old)
2637 * pointer has smaller off that could be used for further
2638 * 'if (ptr > data_end)' check
2640 * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean
2641 * that we cannot access the packet.
2642 * The safe range is:
2643 * [ptr, ptr + range - off)
2644 * so whenever off >=range, it means no safe bytes from this pointer.
2645 * When comparing old->off <= cur->off, it means that older code
2646 * went with smaller offset and that offset was later
2647 * used to figure out the safe range after 'if (ptr > data_end)' check
2648 * Say, 'old' state was explored like:
2649 * ... R3(off=0, r=0)
2651 * ... now R4(off=20,r=0) <-- here
2652 * if (R4 > data_end)
2653 * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access.
2654 * ... the code further went all the way to bpf_exit.
2655 * Now the 'cur' state at the mark 'here' has R4(off=30,r=0).
2656 * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier
2657 * goes further, such cur_R4 will give larger safe packet range after
2658 * 'if (R4 > data_end)' and all further insn were already good with r=20,
2659 * so they will be good with r=30 and we can prune the search.
2661 if (old
->off
<= cur
->off
&&
2662 old
->off
>= old
->range
&& cur
->off
>= cur
->range
)
2668 /* compare two verifier states
2670 * all states stored in state_list are known to be valid, since
2671 * verifier reached 'bpf_exit' instruction through them
2673 * this function is called when verifier exploring different branches of
2674 * execution popped from the state stack. If it sees an old state that has
2675 * more strict register state and more strict stack state then this execution
2676 * branch doesn't need to be explored further, since verifier already
2677 * concluded that more strict state leads to valid finish.
2679 * Therefore two states are equivalent if register state is more conservative
2680 * and explored stack state is more conservative than the current one.
2683 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
2684 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
2686 * In other words if current stack state (one being explored) has more
2687 * valid slots than old one that already passed validation, it means
2688 * the verifier can stop exploring and conclude that current state is valid too
2690 * Similarly with registers. If explored state has register type as invalid
2691 * whereas register type in current state is meaningful, it means that
2692 * the current state will reach 'bpf_exit' instruction safely
2694 static bool states_equal(struct bpf_verifier_env
*env
,
2695 struct bpf_verifier_state
*old
,
2696 struct bpf_verifier_state
*cur
)
2698 bool varlen_map_access
= env
->varlen_map_value_access
;
2699 struct bpf_reg_state
*rold
, *rcur
;
2702 for (i
= 0; i
< MAX_BPF_REG
; i
++) {
2703 rold
= &old
->regs
[i
];
2704 rcur
= &cur
->regs
[i
];
2706 if (memcmp(rold
, rcur
, sizeof(*rold
)) == 0)
2709 /* If the ranges were not the same, but everything else was and
2710 * we didn't do a variable access into a map then we are a-ok.
2712 if (!varlen_map_access
&&
2713 memcmp(rold
, rcur
, offsetofend(struct bpf_reg_state
, id
)) == 0)
2716 /* If we didn't map access then again we don't care about the
2717 * mismatched range values and it's ok if our old type was
2718 * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
2720 if (rold
->type
== NOT_INIT
||
2721 (!varlen_map_access
&& rold
->type
== UNKNOWN_VALUE
&&
2722 rcur
->type
!= NOT_INIT
))
2725 if (rold
->type
== PTR_TO_PACKET
&& rcur
->type
== PTR_TO_PACKET
&&
2726 compare_ptrs_to_packet(rold
, rcur
))
2732 for (i
= 0; i
< MAX_BPF_STACK
; i
++) {
2733 if (old
->stack_slot_type
[i
] == STACK_INVALID
)
2735 if (old
->stack_slot_type
[i
] != cur
->stack_slot_type
[i
])
2736 /* Ex: old explored (safe) state has STACK_SPILL in
2737 * this stack slot, but current has has STACK_MISC ->
2738 * this verifier states are not equivalent,
2739 * return false to continue verification of this path
2742 if (i
% BPF_REG_SIZE
)
2744 if (memcmp(&old
->spilled_regs
[i
/ BPF_REG_SIZE
],
2745 &cur
->spilled_regs
[i
/ BPF_REG_SIZE
],
2746 sizeof(old
->spilled_regs
[0])))
2747 /* when explored and current stack slot types are
2748 * the same, check that stored pointers types
2749 * are the same as well.
2750 * Ex: explored safe path could have stored
2751 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8}
2752 * but current path has stored:
2753 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16}
2754 * such verifier states are not equivalent.
2755 * return false to continue verification of this path
2764 static int is_state_visited(struct bpf_verifier_env
*env
, int insn_idx
)
2766 struct bpf_verifier_state_list
*new_sl
;
2767 struct bpf_verifier_state_list
*sl
;
2769 sl
= env
->explored_states
[insn_idx
];
2771 /* this 'insn_idx' instruction wasn't marked, so we will not
2772 * be doing state search here
2776 while (sl
!= STATE_LIST_MARK
) {
2777 if (states_equal(env
, &sl
->state
, &env
->cur_state
))
2778 /* reached equivalent register/stack state,
2785 /* there were no equivalent states, remember current one.
2786 * technically the current state is not proven to be safe yet,
2787 * but it will either reach bpf_exit (which means it's safe) or
2788 * it will be rejected. Since there are no loops, we won't be
2789 * seeing this 'insn_idx' instruction again on the way to bpf_exit
2791 new_sl
= kmalloc(sizeof(struct bpf_verifier_state_list
), GFP_USER
);
2795 /* add new state to the head of linked list */
2796 memcpy(&new_sl
->state
, &env
->cur_state
, sizeof(env
->cur_state
));
2797 new_sl
->next
= env
->explored_states
[insn_idx
];
2798 env
->explored_states
[insn_idx
] = new_sl
;
2802 static int ext_analyzer_insn_hook(struct bpf_verifier_env
*env
,
2803 int insn_idx
, int prev_insn_idx
)
2805 if (!env
->analyzer_ops
|| !env
->analyzer_ops
->insn_hook
)
2808 return env
->analyzer_ops
->insn_hook(env
, insn_idx
, prev_insn_idx
);
2811 static int do_check(struct bpf_verifier_env
*env
)
2813 struct bpf_verifier_state
*state
= &env
->cur_state
;
2814 struct bpf_insn
*insns
= env
->prog
->insnsi
;
2815 struct bpf_reg_state
*regs
= state
->regs
;
2816 int insn_cnt
= env
->prog
->len
;
2817 int insn_idx
, prev_insn_idx
= 0;
2818 int insn_processed
= 0;
2819 bool do_print_state
= false;
2821 init_reg_state(regs
);
2823 env
->varlen_map_value_access
= false;
2825 struct bpf_insn
*insn
;
2829 if (insn_idx
>= insn_cnt
) {
2830 verbose("invalid insn idx %d insn_cnt %d\n",
2831 insn_idx
, insn_cnt
);
2835 insn
= &insns
[insn_idx
];
2836 class = BPF_CLASS(insn
->code
);
2838 if (++insn_processed
> BPF_COMPLEXITY_LIMIT_INSNS
) {
2839 verbose("BPF program is too large. Processed %d insn\n",
2844 err
= is_state_visited(env
, insn_idx
);
2848 /* found equivalent state, can prune the search */
2851 verbose("\nfrom %d to %d: safe\n",
2852 prev_insn_idx
, insn_idx
);
2854 verbose("%d: safe\n", insn_idx
);
2856 goto process_bpf_exit
;
2859 if (log_level
&& do_print_state
) {
2860 verbose("\nfrom %d to %d:", prev_insn_idx
, insn_idx
);
2861 print_verifier_state(&env
->cur_state
);
2862 do_print_state
= false;
2866 verbose("%d: ", insn_idx
);
2867 print_bpf_insn(env
, insn
);
2870 err
= ext_analyzer_insn_hook(env
, insn_idx
, prev_insn_idx
);
2874 if (class == BPF_ALU
|| class == BPF_ALU64
) {
2875 err
= check_alu_op(env
, insn
);
2879 } else if (class == BPF_LDX
) {
2880 enum bpf_reg_type
*prev_src_type
, src_reg_type
;
2882 /* check for reserved fields is already done */
2884 /* check src operand */
2885 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
2889 err
= check_reg_arg(regs
, insn
->dst_reg
, DST_OP_NO_MARK
);
2893 src_reg_type
= regs
[insn
->src_reg
].type
;
2895 /* check that memory (src_reg + off) is readable,
2896 * the state of dst_reg will be updated by this func
2898 err
= check_mem_access(env
, insn
->src_reg
, insn
->off
,
2899 BPF_SIZE(insn
->code
), BPF_READ
,
2904 if (BPF_SIZE(insn
->code
) != BPF_W
&&
2905 BPF_SIZE(insn
->code
) != BPF_DW
) {
2910 prev_src_type
= &env
->insn_aux_data
[insn_idx
].ptr_type
;
2912 if (*prev_src_type
== NOT_INIT
) {
2914 * dst_reg = *(u32 *)(src_reg + off)
2915 * save type to validate intersecting paths
2917 *prev_src_type
= src_reg_type
;
2919 } else if (src_reg_type
!= *prev_src_type
&&
2920 (src_reg_type
== PTR_TO_CTX
||
2921 *prev_src_type
== PTR_TO_CTX
)) {
2922 /* ABuser program is trying to use the same insn
2923 * dst_reg = *(u32*) (src_reg + off)
2924 * with different pointer types:
2925 * src_reg == ctx in one branch and
2926 * src_reg == stack|map in some other branch.
2929 verbose("same insn cannot be used with different pointers\n");
2933 } else if (class == BPF_STX
) {
2934 enum bpf_reg_type
*prev_dst_type
, dst_reg_type
;
2936 if (BPF_MODE(insn
->code
) == BPF_XADD
) {
2937 err
= check_xadd(env
, insn
);
2944 /* check src1 operand */
2945 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
2948 /* check src2 operand */
2949 err
= check_reg_arg(regs
, insn
->dst_reg
, SRC_OP
);
2953 dst_reg_type
= regs
[insn
->dst_reg
].type
;
2955 /* check that memory (dst_reg + off) is writeable */
2956 err
= check_mem_access(env
, insn
->dst_reg
, insn
->off
,
2957 BPF_SIZE(insn
->code
), BPF_WRITE
,
2962 prev_dst_type
= &env
->insn_aux_data
[insn_idx
].ptr_type
;
2964 if (*prev_dst_type
== NOT_INIT
) {
2965 *prev_dst_type
= dst_reg_type
;
2966 } else if (dst_reg_type
!= *prev_dst_type
&&
2967 (dst_reg_type
== PTR_TO_CTX
||
2968 *prev_dst_type
== PTR_TO_CTX
)) {
2969 verbose("same insn cannot be used with different pointers\n");
2973 } else if (class == BPF_ST
) {
2974 if (BPF_MODE(insn
->code
) != BPF_MEM
||
2975 insn
->src_reg
!= BPF_REG_0
) {
2976 verbose("BPF_ST uses reserved fields\n");
2979 /* check src operand */
2980 err
= check_reg_arg(regs
, insn
->dst_reg
, SRC_OP
);
2984 /* check that memory (dst_reg + off) is writeable */
2985 err
= check_mem_access(env
, insn
->dst_reg
, insn
->off
,
2986 BPF_SIZE(insn
->code
), BPF_WRITE
,
2991 } else if (class == BPF_JMP
) {
2992 u8 opcode
= BPF_OP(insn
->code
);
2994 if (opcode
== BPF_CALL
) {
2995 if (BPF_SRC(insn
->code
) != BPF_K
||
2997 insn
->src_reg
!= BPF_REG_0
||
2998 insn
->dst_reg
!= BPF_REG_0
) {
2999 verbose("BPF_CALL uses reserved fields\n");
3003 err
= check_call(env
, insn
->imm
, insn_idx
);
3007 } else if (opcode
== BPF_JA
) {
3008 if (BPF_SRC(insn
->code
) != BPF_K
||
3010 insn
->src_reg
!= BPF_REG_0
||
3011 insn
->dst_reg
!= BPF_REG_0
) {
3012 verbose("BPF_JA uses reserved fields\n");
3016 insn_idx
+= insn
->off
+ 1;
3019 } else if (opcode
== BPF_EXIT
) {
3020 if (BPF_SRC(insn
->code
) != BPF_K
||
3022 insn
->src_reg
!= BPF_REG_0
||
3023 insn
->dst_reg
!= BPF_REG_0
) {
3024 verbose("BPF_EXIT uses reserved fields\n");
3028 /* eBPF calling convetion is such that R0 is used
3029 * to return the value from eBPF program.
3030 * Make sure that it's readable at this time
3031 * of bpf_exit, which means that program wrote
3032 * something into it earlier
3034 err
= check_reg_arg(regs
, BPF_REG_0
, SRC_OP
);
3038 if (is_pointer_value(env
, BPF_REG_0
)) {
3039 verbose("R0 leaks addr as return value\n");
3044 insn_idx
= pop_stack(env
, &prev_insn_idx
);
3048 do_print_state
= true;
3052 err
= check_cond_jmp_op(env
, insn
, &insn_idx
);
3056 } else if (class == BPF_LD
) {
3057 u8 mode
= BPF_MODE(insn
->code
);
3059 if (mode
== BPF_ABS
|| mode
== BPF_IND
) {
3060 err
= check_ld_abs(env
, insn
);
3064 } else if (mode
== BPF_IMM
) {
3065 err
= check_ld_imm(env
, insn
);
3071 verbose("invalid BPF_LD mode\n");
3074 reset_reg_range_values(regs
, insn
->dst_reg
);
3076 verbose("unknown insn class %d\n", class);
3083 verbose("processed %d insns\n", insn_processed
);
3087 static int check_map_prealloc(struct bpf_map
*map
)
3089 return (map
->map_type
!= BPF_MAP_TYPE_HASH
&&
3090 map
->map_type
!= BPF_MAP_TYPE_PERCPU_HASH
&&
3091 map
->map_type
!= BPF_MAP_TYPE_HASH_OF_MAPS
) ||
3092 !(map
->map_flags
& BPF_F_NO_PREALLOC
);
3095 static int check_map_prog_compatibility(struct bpf_map
*map
,
3096 struct bpf_prog
*prog
)
3099 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
3100 * preallocated hash maps, since doing memory allocation
3101 * in overflow_handler can crash depending on where nmi got
3104 if (prog
->type
== BPF_PROG_TYPE_PERF_EVENT
) {
3105 if (!check_map_prealloc(map
)) {
3106 verbose("perf_event programs can only use preallocated hash map\n");
3109 if (map
->inner_map_meta
&&
3110 !check_map_prealloc(map
->inner_map_meta
)) {
3111 verbose("perf_event programs can only use preallocated inner hash map\n");
3118 /* look for pseudo eBPF instructions that access map FDs and
3119 * replace them with actual map pointers
3121 static int replace_map_fd_with_map_ptr(struct bpf_verifier_env
*env
)
3123 struct bpf_insn
*insn
= env
->prog
->insnsi
;
3124 int insn_cnt
= env
->prog
->len
;
3127 err
= bpf_prog_calc_tag(env
->prog
);
3131 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
3132 if (BPF_CLASS(insn
->code
) == BPF_LDX
&&
3133 (BPF_MODE(insn
->code
) != BPF_MEM
|| insn
->imm
!= 0)) {
3134 verbose("BPF_LDX uses reserved fields\n");
3138 if (BPF_CLASS(insn
->code
) == BPF_STX
&&
3139 ((BPF_MODE(insn
->code
) != BPF_MEM
&&
3140 BPF_MODE(insn
->code
) != BPF_XADD
) || insn
->imm
!= 0)) {
3141 verbose("BPF_STX uses reserved fields\n");
3145 if (insn
[0].code
== (BPF_LD
| BPF_IMM
| BPF_DW
)) {
3146 struct bpf_map
*map
;
3149 if (i
== insn_cnt
- 1 || insn
[1].code
!= 0 ||
3150 insn
[1].dst_reg
!= 0 || insn
[1].src_reg
!= 0 ||
3152 verbose("invalid bpf_ld_imm64 insn\n");
3156 if (insn
->src_reg
== 0)
3157 /* valid generic load 64-bit imm */
3160 if (insn
->src_reg
!= BPF_PSEUDO_MAP_FD
) {
3161 verbose("unrecognized bpf_ld_imm64 insn\n");
3165 f
= fdget(insn
->imm
);
3166 map
= __bpf_map_get(f
);
3168 verbose("fd %d is not pointing to valid bpf_map\n",
3170 return PTR_ERR(map
);
3173 err
= check_map_prog_compatibility(map
, env
->prog
);
3179 /* store map pointer inside BPF_LD_IMM64 instruction */
3180 insn
[0].imm
= (u32
) (unsigned long) map
;
3181 insn
[1].imm
= ((u64
) (unsigned long) map
) >> 32;
3183 /* check whether we recorded this map already */
3184 for (j
= 0; j
< env
->used_map_cnt
; j
++)
3185 if (env
->used_maps
[j
] == map
) {
3190 if (env
->used_map_cnt
>= MAX_USED_MAPS
) {
3195 /* hold the map. If the program is rejected by verifier,
3196 * the map will be released by release_maps() or it
3197 * will be used by the valid program until it's unloaded
3198 * and all maps are released in free_bpf_prog_info()
3200 map
= bpf_map_inc(map
, false);
3203 return PTR_ERR(map
);
3205 env
->used_maps
[env
->used_map_cnt
++] = map
;
3214 /* now all pseudo BPF_LD_IMM64 instructions load valid
3215 * 'struct bpf_map *' into a register instead of user map_fd.
3216 * These pointers will be used later by verifier to validate map access.
3221 /* drop refcnt of maps used by the rejected program */
3222 static void release_maps(struct bpf_verifier_env
*env
)
3226 for (i
= 0; i
< env
->used_map_cnt
; i
++)
3227 bpf_map_put(env
->used_maps
[i
]);
3230 /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
3231 static void convert_pseudo_ld_imm64(struct bpf_verifier_env
*env
)
3233 struct bpf_insn
*insn
= env
->prog
->insnsi
;
3234 int insn_cnt
= env
->prog
->len
;
3237 for (i
= 0; i
< insn_cnt
; i
++, insn
++)
3238 if (insn
->code
== (BPF_LD
| BPF_IMM
| BPF_DW
))
3242 /* single env->prog->insni[off] instruction was replaced with the range
3243 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
3244 * [0, off) and [off, end) to new locations, so the patched range stays zero
3246 static int adjust_insn_aux_data(struct bpf_verifier_env
*env
, u32 prog_len
,
3249 struct bpf_insn_aux_data
*new_data
, *old_data
= env
->insn_aux_data
;
3253 new_data
= vzalloc(sizeof(struct bpf_insn_aux_data
) * prog_len
);
3256 memcpy(new_data
, old_data
, sizeof(struct bpf_insn_aux_data
) * off
);
3257 memcpy(new_data
+ off
+ cnt
- 1, old_data
+ off
,
3258 sizeof(struct bpf_insn_aux_data
) * (prog_len
- off
- cnt
+ 1));
3259 env
->insn_aux_data
= new_data
;
3264 static struct bpf_prog
*bpf_patch_insn_data(struct bpf_verifier_env
*env
, u32 off
,
3265 const struct bpf_insn
*patch
, u32 len
)
3267 struct bpf_prog
*new_prog
;
3269 new_prog
= bpf_patch_insn_single(env
->prog
, off
, patch
, len
);
3272 if (adjust_insn_aux_data(env
, new_prog
->len
, off
, len
))
3277 /* convert load instructions that access fields of 'struct __sk_buff'
3278 * into sequence of instructions that access fields of 'struct sk_buff'
3280 static int convert_ctx_accesses(struct bpf_verifier_env
*env
)
3282 const struct bpf_verifier_ops
*ops
= env
->prog
->aux
->ops
;
3283 const int insn_cnt
= env
->prog
->len
;
3284 struct bpf_insn insn_buf
[16], *insn
;
3285 struct bpf_prog
*new_prog
;
3286 enum bpf_access_type type
;
3287 int i
, cnt
, delta
= 0;
3289 if (ops
->gen_prologue
) {
3290 cnt
= ops
->gen_prologue(insn_buf
, env
->seen_direct_write
,
3292 if (cnt
>= ARRAY_SIZE(insn_buf
)) {
3293 verbose("bpf verifier is misconfigured\n");
3296 new_prog
= bpf_patch_insn_data(env
, 0, insn_buf
, cnt
);
3300 env
->prog
= new_prog
;
3305 if (!ops
->convert_ctx_access
)
3308 insn
= env
->prog
->insnsi
+ delta
;
3310 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
3311 if (insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_B
) ||
3312 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_H
) ||
3313 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_W
) ||
3314 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_DW
))
3316 else if (insn
->code
== (BPF_STX
| BPF_MEM
| BPF_B
) ||
3317 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_H
) ||
3318 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_W
) ||
3319 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_DW
))
3324 if (env
->insn_aux_data
[i
+ delta
].ptr_type
!= PTR_TO_CTX
)
3327 cnt
= ops
->convert_ctx_access(type
, insn
, insn_buf
, env
->prog
);
3328 if (cnt
== 0 || cnt
>= ARRAY_SIZE(insn_buf
)) {
3329 verbose("bpf verifier is misconfigured\n");
3333 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, insn_buf
, cnt
);
3339 /* keep walking new program and skip insns we just inserted */
3340 env
->prog
= new_prog
;
3341 insn
= new_prog
->insnsi
+ i
+ delta
;
3347 /* fixup insn->imm field of bpf_call instructions
3348 * and inline eligible helpers as explicit sequence of BPF instructions
3350 * this function is called after eBPF program passed verification
3352 static int fixup_bpf_calls(struct bpf_verifier_env
*env
)
3354 struct bpf_prog
*prog
= env
->prog
;
3355 struct bpf_insn
*insn
= prog
->insnsi
;
3356 const struct bpf_func_proto
*fn
;
3357 const int insn_cnt
= prog
->len
;
3358 struct bpf_insn insn_buf
[16];
3359 struct bpf_prog
*new_prog
;
3360 struct bpf_map
*map_ptr
;
3361 int i
, cnt
, delta
= 0;
3363 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
3364 if (insn
->code
!= (BPF_JMP
| BPF_CALL
))
3367 if (insn
->imm
== BPF_FUNC_get_route_realm
)
3368 prog
->dst_needed
= 1;
3369 if (insn
->imm
== BPF_FUNC_get_prandom_u32
)
3370 bpf_user_rnd_init_once();
3371 if (insn
->imm
== BPF_FUNC_tail_call
) {
3372 /* If we tail call into other programs, we
3373 * cannot make any assumptions since they can
3374 * be replaced dynamically during runtime in
3375 * the program array.
3377 prog
->cb_access
= 1;
3379 /* mark bpf_tail_call as different opcode to avoid
3380 * conditional branch in the interpeter for every normal
3381 * call and to prevent accidental JITing by JIT compiler
3382 * that doesn't support bpf_tail_call yet
3385 insn
->code
|= BPF_X
;
3389 if (ebpf_jit_enabled() && insn
->imm
== BPF_FUNC_map_lookup_elem
) {
3390 map_ptr
= env
->insn_aux_data
[i
+ delta
].map_ptr
;
3391 if (map_ptr
== BPF_MAP_PTR_POISON
||
3392 !map_ptr
->ops
->map_gen_lookup
)
3393 goto patch_call_imm
;
3395 cnt
= map_ptr
->ops
->map_gen_lookup(map_ptr
, insn_buf
);
3396 if (cnt
== 0 || cnt
>= ARRAY_SIZE(insn_buf
)) {
3397 verbose("bpf verifier is misconfigured\n");
3401 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, insn_buf
,
3408 /* keep walking new program and skip insns we just inserted */
3409 env
->prog
= prog
= new_prog
;
3410 insn
= new_prog
->insnsi
+ i
+ delta
;
3415 fn
= prog
->aux
->ops
->get_func_proto(insn
->imm
);
3416 /* all functions that have prototype and verifier allowed
3417 * programs to call them, must be real in-kernel functions
3420 verbose("kernel subsystem misconfigured func %s#%d\n",
3421 func_id_name(insn
->imm
), insn
->imm
);
3424 insn
->imm
= fn
->func
- __bpf_call_base
;
3430 static void free_states(struct bpf_verifier_env
*env
)
3432 struct bpf_verifier_state_list
*sl
, *sln
;
3435 if (!env
->explored_states
)
3438 for (i
= 0; i
< env
->prog
->len
; i
++) {
3439 sl
= env
->explored_states
[i
];
3442 while (sl
!= STATE_LIST_MARK
) {
3449 kfree(env
->explored_states
);
3452 int bpf_check(struct bpf_prog
**prog
, union bpf_attr
*attr
)
3454 char __user
*log_ubuf
= NULL
;
3455 struct bpf_verifier_env
*env
;
3458 /* 'struct bpf_verifier_env' can be global, but since it's not small,
3459 * allocate/free it every time bpf_check() is called
3461 env
= kzalloc(sizeof(struct bpf_verifier_env
), GFP_KERNEL
);
3465 env
->insn_aux_data
= vzalloc(sizeof(struct bpf_insn_aux_data
) *
3468 if (!env
->insn_aux_data
)
3472 /* grab the mutex to protect few globals used by verifier */
3473 mutex_lock(&bpf_verifier_lock
);
3475 if (attr
->log_level
|| attr
->log_buf
|| attr
->log_size
) {
3476 /* user requested verbose verifier output
3477 * and supplied buffer to store the verification trace
3479 log_level
= attr
->log_level
;
3480 log_ubuf
= (char __user
*) (unsigned long) attr
->log_buf
;
3481 log_size
= attr
->log_size
;
3485 /* log_* values have to be sane */
3486 if (log_size
< 128 || log_size
> UINT_MAX
>> 8 ||
3487 log_level
== 0 || log_ubuf
== NULL
)
3491 log_buf
= vmalloc(log_size
);
3498 ret
= replace_map_fd_with_map_ptr(env
);
3500 goto skip_full_check
;
3502 env
->explored_states
= kcalloc(env
->prog
->len
,
3503 sizeof(struct bpf_verifier_state_list
*),
3506 if (!env
->explored_states
)
3507 goto skip_full_check
;
3509 ret
= check_cfg(env
);
3511 goto skip_full_check
;
3513 env
->allow_ptr_leaks
= capable(CAP_SYS_ADMIN
);
3515 ret
= do_check(env
);
3518 while (pop_stack(env
, NULL
) >= 0);
3522 /* program is valid, convert *(u32*)(ctx + off) accesses */
3523 ret
= convert_ctx_accesses(env
);
3526 ret
= fixup_bpf_calls(env
);
3528 if (log_level
&& log_len
>= log_size
- 1) {
3529 BUG_ON(log_len
>= log_size
);
3530 /* verifier log exceeded user supplied buffer */
3532 /* fall through to return what was recorded */
3535 /* copy verifier log back to user space including trailing zero */
3536 if (log_level
&& copy_to_user(log_ubuf
, log_buf
, log_len
+ 1) != 0) {
3541 if (ret
== 0 && env
->used_map_cnt
) {
3542 /* if program passed verifier, update used_maps in bpf_prog_info */
3543 env
->prog
->aux
->used_maps
= kmalloc_array(env
->used_map_cnt
,
3544 sizeof(env
->used_maps
[0]),
3547 if (!env
->prog
->aux
->used_maps
) {
3552 memcpy(env
->prog
->aux
->used_maps
, env
->used_maps
,
3553 sizeof(env
->used_maps
[0]) * env
->used_map_cnt
);
3554 env
->prog
->aux
->used_map_cnt
= env
->used_map_cnt
;
3556 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
3557 * bpf_ld_imm64 instructions
3559 convert_pseudo_ld_imm64(env
);
3565 if (!env
->prog
->aux
->used_maps
)
3566 /* if we didn't copy map pointers into bpf_prog_info, release
3567 * them now. Otherwise free_bpf_prog_info() will release them.
3572 mutex_unlock(&bpf_verifier_lock
);
3573 vfree(env
->insn_aux_data
);
3579 int bpf_analyzer(struct bpf_prog
*prog
, const struct bpf_ext_analyzer_ops
*ops
,
3582 struct bpf_verifier_env
*env
;
3585 env
= kzalloc(sizeof(struct bpf_verifier_env
), GFP_KERNEL
);
3589 env
->insn_aux_data
= vzalloc(sizeof(struct bpf_insn_aux_data
) *
3592 if (!env
->insn_aux_data
)
3595 env
->analyzer_ops
= ops
;
3596 env
->analyzer_priv
= priv
;
3598 /* grab the mutex to protect few globals used by verifier */
3599 mutex_lock(&bpf_verifier_lock
);
3603 env
->explored_states
= kcalloc(env
->prog
->len
,
3604 sizeof(struct bpf_verifier_state_list
*),
3607 if (!env
->explored_states
)
3608 goto skip_full_check
;
3610 ret
= check_cfg(env
);
3612 goto skip_full_check
;
3614 env
->allow_ptr_leaks
= capable(CAP_SYS_ADMIN
);
3616 ret
= do_check(env
);
3619 while (pop_stack(env
, NULL
) >= 0);
3622 mutex_unlock(&bpf_verifier_lock
);
3623 vfree(env
->insn_aux_data
);
3628 EXPORT_SYMBOL_GPL(bpf_analyzer
);