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 98304
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 verbose(",min_align=%u", reg
->min_align
);
247 verbose(",aux_off=%u", reg
->aux_off
);
248 if (reg
->aux_off_align
)
249 verbose(",aux_off_align=%u", reg
->aux_off_align
);
251 for (i
= 0; i
< MAX_BPF_STACK
; i
+= BPF_REG_SIZE
) {
252 if (state
->stack_slot_type
[i
] == STACK_SPILL
)
253 verbose(" fp%d=%s", -MAX_BPF_STACK
+ i
,
254 reg_type_str
[state
->spilled_regs
[i
/ BPF_REG_SIZE
].type
]);
259 static const char *const bpf_class_string
[] = {
267 [BPF_ALU64
] = "alu64",
270 static const char *const bpf_alu_string
[16] = {
271 [BPF_ADD
>> 4] = "+=",
272 [BPF_SUB
>> 4] = "-=",
273 [BPF_MUL
>> 4] = "*=",
274 [BPF_DIV
>> 4] = "/=",
275 [BPF_OR
>> 4] = "|=",
276 [BPF_AND
>> 4] = "&=",
277 [BPF_LSH
>> 4] = "<<=",
278 [BPF_RSH
>> 4] = ">>=",
279 [BPF_NEG
>> 4] = "neg",
280 [BPF_MOD
>> 4] = "%=",
281 [BPF_XOR
>> 4] = "^=",
282 [BPF_MOV
>> 4] = "=",
283 [BPF_ARSH
>> 4] = "s>>=",
284 [BPF_END
>> 4] = "endian",
287 static const char *const bpf_ldst_string
[] = {
288 [BPF_W
>> 3] = "u32",
289 [BPF_H
>> 3] = "u16",
291 [BPF_DW
>> 3] = "u64",
294 static const char *const bpf_jmp_string
[16] = {
295 [BPF_JA
>> 4] = "jmp",
296 [BPF_JEQ
>> 4] = "==",
297 [BPF_JGT
>> 4] = ">",
298 [BPF_JGE
>> 4] = ">=",
299 [BPF_JSET
>> 4] = "&",
300 [BPF_JNE
>> 4] = "!=",
301 [BPF_JSGT
>> 4] = "s>",
302 [BPF_JSGE
>> 4] = "s>=",
303 [BPF_CALL
>> 4] = "call",
304 [BPF_EXIT
>> 4] = "exit",
307 static void print_bpf_insn(const struct bpf_verifier_env
*env
,
308 const struct bpf_insn
*insn
)
310 u8
class = BPF_CLASS(insn
->code
);
312 if (class == BPF_ALU
|| class == BPF_ALU64
) {
313 if (BPF_SRC(insn
->code
) == BPF_X
)
314 verbose("(%02x) %sr%d %s %sr%d\n",
315 insn
->code
, class == BPF_ALU
? "(u32) " : "",
317 bpf_alu_string
[BPF_OP(insn
->code
) >> 4],
318 class == BPF_ALU
? "(u32) " : "",
321 verbose("(%02x) %sr%d %s %s%d\n",
322 insn
->code
, class == BPF_ALU
? "(u32) " : "",
324 bpf_alu_string
[BPF_OP(insn
->code
) >> 4],
325 class == BPF_ALU
? "(u32) " : "",
327 } else if (class == BPF_STX
) {
328 if (BPF_MODE(insn
->code
) == BPF_MEM
)
329 verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
331 bpf_ldst_string
[BPF_SIZE(insn
->code
) >> 3],
333 insn
->off
, insn
->src_reg
);
334 else if (BPF_MODE(insn
->code
) == BPF_XADD
)
335 verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
337 bpf_ldst_string
[BPF_SIZE(insn
->code
) >> 3],
338 insn
->dst_reg
, insn
->off
,
341 verbose("BUG_%02x\n", insn
->code
);
342 } else if (class == BPF_ST
) {
343 if (BPF_MODE(insn
->code
) != BPF_MEM
) {
344 verbose("BUG_st_%02x\n", insn
->code
);
347 verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
349 bpf_ldst_string
[BPF_SIZE(insn
->code
) >> 3],
351 insn
->off
, insn
->imm
);
352 } else if (class == BPF_LDX
) {
353 if (BPF_MODE(insn
->code
) != BPF_MEM
) {
354 verbose("BUG_ldx_%02x\n", insn
->code
);
357 verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
358 insn
->code
, insn
->dst_reg
,
359 bpf_ldst_string
[BPF_SIZE(insn
->code
) >> 3],
360 insn
->src_reg
, insn
->off
);
361 } else if (class == BPF_LD
) {
362 if (BPF_MODE(insn
->code
) == BPF_ABS
) {
363 verbose("(%02x) r0 = *(%s *)skb[%d]\n",
365 bpf_ldst_string
[BPF_SIZE(insn
->code
) >> 3],
367 } else if (BPF_MODE(insn
->code
) == BPF_IND
) {
368 verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
370 bpf_ldst_string
[BPF_SIZE(insn
->code
) >> 3],
371 insn
->src_reg
, insn
->imm
);
372 } else if (BPF_MODE(insn
->code
) == BPF_IMM
&&
373 BPF_SIZE(insn
->code
) == BPF_DW
) {
374 /* At this point, we already made sure that the second
375 * part of the ldimm64 insn is accessible.
377 u64 imm
= ((u64
)(insn
+ 1)->imm
<< 32) | (u32
)insn
->imm
;
378 bool map_ptr
= insn
->src_reg
== BPF_PSEUDO_MAP_FD
;
380 if (map_ptr
&& !env
->allow_ptr_leaks
)
383 verbose("(%02x) r%d = 0x%llx\n", insn
->code
,
384 insn
->dst_reg
, (unsigned long long)imm
);
386 verbose("BUG_ld_%02x\n", insn
->code
);
389 } else if (class == BPF_JMP
) {
390 u8 opcode
= BPF_OP(insn
->code
);
392 if (opcode
== BPF_CALL
) {
393 verbose("(%02x) call %s#%d\n", insn
->code
,
394 func_id_name(insn
->imm
), insn
->imm
);
395 } else if (insn
->code
== (BPF_JMP
| BPF_JA
)) {
396 verbose("(%02x) goto pc%+d\n",
397 insn
->code
, insn
->off
);
398 } else if (insn
->code
== (BPF_JMP
| BPF_EXIT
)) {
399 verbose("(%02x) exit\n", insn
->code
);
400 } else if (BPF_SRC(insn
->code
) == BPF_X
) {
401 verbose("(%02x) if r%d %s r%d goto pc%+d\n",
402 insn
->code
, insn
->dst_reg
,
403 bpf_jmp_string
[BPF_OP(insn
->code
) >> 4],
404 insn
->src_reg
, insn
->off
);
406 verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
407 insn
->code
, insn
->dst_reg
,
408 bpf_jmp_string
[BPF_OP(insn
->code
) >> 4],
409 insn
->imm
, insn
->off
);
412 verbose("(%02x) %s\n", insn
->code
, bpf_class_string
[class]);
416 static int pop_stack(struct bpf_verifier_env
*env
, int *prev_insn_idx
)
418 struct bpf_verifier_stack_elem
*elem
;
421 if (env
->head
== NULL
)
424 memcpy(&env
->cur_state
, &env
->head
->st
, sizeof(env
->cur_state
));
425 insn_idx
= env
->head
->insn_idx
;
427 *prev_insn_idx
= env
->head
->prev_insn_idx
;
428 elem
= env
->head
->next
;
435 static struct bpf_verifier_state
*push_stack(struct bpf_verifier_env
*env
,
436 int insn_idx
, int prev_insn_idx
)
438 struct bpf_verifier_stack_elem
*elem
;
440 elem
= kmalloc(sizeof(struct bpf_verifier_stack_elem
), GFP_KERNEL
);
444 memcpy(&elem
->st
, &env
->cur_state
, sizeof(env
->cur_state
));
445 elem
->insn_idx
= insn_idx
;
446 elem
->prev_insn_idx
= prev_insn_idx
;
447 elem
->next
= env
->head
;
450 if (env
->stack_size
> BPF_COMPLEXITY_LIMIT_STACK
) {
451 verbose("BPF program is too complex\n");
456 /* pop all elements and return */
457 while (pop_stack(env
, NULL
) >= 0);
461 #define CALLER_SAVED_REGS 6
462 static const int caller_saved
[CALLER_SAVED_REGS
] = {
463 BPF_REG_0
, BPF_REG_1
, BPF_REG_2
, BPF_REG_3
, BPF_REG_4
, BPF_REG_5
466 static void mark_reg_not_init(struct bpf_reg_state
*regs
, u32 regno
)
468 BUG_ON(regno
>= MAX_BPF_REG
);
470 memset(®s
[regno
], 0, sizeof(regs
[regno
]));
471 regs
[regno
].type
= NOT_INIT
;
472 regs
[regno
].min_value
= BPF_REGISTER_MIN_RANGE
;
473 regs
[regno
].max_value
= BPF_REGISTER_MAX_RANGE
;
476 static void init_reg_state(struct bpf_reg_state
*regs
)
480 for (i
= 0; i
< MAX_BPF_REG
; i
++)
481 mark_reg_not_init(regs
, i
);
484 regs
[BPF_REG_FP
].type
= FRAME_PTR
;
486 /* 1st arg to a function */
487 regs
[BPF_REG_1
].type
= PTR_TO_CTX
;
490 static void __mark_reg_unknown_value(struct bpf_reg_state
*regs
, u32 regno
)
492 regs
[regno
].type
= UNKNOWN_VALUE
;
497 static void mark_reg_unknown_value(struct bpf_reg_state
*regs
, u32 regno
)
499 BUG_ON(regno
>= MAX_BPF_REG
);
500 __mark_reg_unknown_value(regs
, regno
);
503 static void reset_reg_range_values(struct bpf_reg_state
*regs
, u32 regno
)
505 regs
[regno
].min_value
= BPF_REGISTER_MIN_RANGE
;
506 regs
[regno
].max_value
= BPF_REGISTER_MAX_RANGE
;
507 regs
[regno
].min_align
= 0;
510 static void mark_reg_unknown_value_and_range(struct bpf_reg_state
*regs
,
513 mark_reg_unknown_value(regs
, regno
);
514 reset_reg_range_values(regs
, regno
);
518 SRC_OP
, /* register is used as source operand */
519 DST_OP
, /* register is used as destination operand */
520 DST_OP_NO_MARK
/* same as above, check only, don't mark */
523 static int check_reg_arg(struct bpf_reg_state
*regs
, u32 regno
,
526 if (regno
>= MAX_BPF_REG
) {
527 verbose("R%d is invalid\n", regno
);
532 /* check whether register used as source operand can be read */
533 if (regs
[regno
].type
== NOT_INIT
) {
534 verbose("R%d !read_ok\n", regno
);
538 /* check whether register used as dest operand can be written to */
539 if (regno
== BPF_REG_FP
) {
540 verbose("frame pointer is read only\n");
544 mark_reg_unknown_value(regs
, regno
);
549 static bool is_spillable_regtype(enum bpf_reg_type type
)
552 case PTR_TO_MAP_VALUE
:
553 case PTR_TO_MAP_VALUE_OR_NULL
:
554 case PTR_TO_MAP_VALUE_ADJ
:
558 case PTR_TO_PACKET_END
:
560 case CONST_PTR_TO_MAP
:
567 /* check_stack_read/write functions track spill/fill of registers,
568 * stack boundary and alignment are checked in check_mem_access()
570 static int check_stack_write(struct bpf_verifier_state
*state
, int off
,
571 int size
, int value_regno
)
574 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
575 * so it's aligned access and [off, off + size) are within stack limits
578 if (value_regno
>= 0 &&
579 is_spillable_regtype(state
->regs
[value_regno
].type
)) {
581 /* register containing pointer is being spilled into stack */
582 if (size
!= BPF_REG_SIZE
) {
583 verbose("invalid size of register spill\n");
587 /* save register state */
588 state
->spilled_regs
[(MAX_BPF_STACK
+ off
) / BPF_REG_SIZE
] =
589 state
->regs
[value_regno
];
591 for (i
= 0; i
< BPF_REG_SIZE
; i
++)
592 state
->stack_slot_type
[MAX_BPF_STACK
+ off
+ i
] = STACK_SPILL
;
594 /* regular write of data into stack */
595 state
->spilled_regs
[(MAX_BPF_STACK
+ off
) / BPF_REG_SIZE
] =
596 (struct bpf_reg_state
) {};
598 for (i
= 0; i
< size
; i
++)
599 state
->stack_slot_type
[MAX_BPF_STACK
+ off
+ i
] = STACK_MISC
;
604 static int check_stack_read(struct bpf_verifier_state
*state
, int off
, int size
,
610 slot_type
= &state
->stack_slot_type
[MAX_BPF_STACK
+ off
];
612 if (slot_type
[0] == STACK_SPILL
) {
613 if (size
!= BPF_REG_SIZE
) {
614 verbose("invalid size of register spill\n");
617 for (i
= 1; i
< BPF_REG_SIZE
; i
++) {
618 if (slot_type
[i
] != STACK_SPILL
) {
619 verbose("corrupted spill memory\n");
624 if (value_regno
>= 0)
625 /* restore register state from stack */
626 state
->regs
[value_regno
] =
627 state
->spilled_regs
[(MAX_BPF_STACK
+ off
) / BPF_REG_SIZE
];
630 for (i
= 0; i
< size
; i
++) {
631 if (slot_type
[i
] != STACK_MISC
) {
632 verbose("invalid read from stack off %d+%d size %d\n",
637 if (value_regno
>= 0)
638 /* have read misc data from the stack */
639 mark_reg_unknown_value_and_range(state
->regs
,
645 /* check read/write into map element returned by bpf_map_lookup_elem() */
646 static int check_map_access(struct bpf_verifier_env
*env
, u32 regno
, int off
,
649 struct bpf_map
*map
= env
->cur_state
.regs
[regno
].map_ptr
;
651 if (off
< 0 || size
<= 0 || off
+ size
> map
->value_size
) {
652 verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
653 map
->value_size
, off
, size
);
659 /* check read/write into an adjusted map element */
660 static int check_map_access_adj(struct bpf_verifier_env
*env
, u32 regno
,
663 struct bpf_verifier_state
*state
= &env
->cur_state
;
664 struct bpf_reg_state
*reg
= &state
->regs
[regno
];
667 /* We adjusted the register to this map value, so we
668 * need to change off and size to min_value and max_value
669 * respectively to make sure our theoretical access will be
673 print_verifier_state(state
);
674 env
->varlen_map_value_access
= true;
675 /* The minimum value is only important with signed
676 * comparisons where we can't assume the floor of a
677 * value is 0. If we are using signed variables for our
678 * index'es we need to make sure that whatever we use
679 * will have a set floor within our range.
681 if (reg
->min_value
< 0) {
682 verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
686 err
= check_map_access(env
, regno
, reg
->min_value
+ off
, size
);
688 verbose("R%d min value is outside of the array range\n",
693 /* If we haven't set a max value then we need to bail
694 * since we can't be sure we won't do bad things.
696 if (reg
->max_value
== BPF_REGISTER_MAX_RANGE
) {
697 verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
701 return check_map_access(env
, regno
, reg
->max_value
+ off
, size
);
704 #define MAX_PACKET_OFF 0xffff
706 static bool may_access_direct_pkt_data(struct bpf_verifier_env
*env
,
707 const struct bpf_call_arg_meta
*meta
,
708 enum bpf_access_type t
)
710 switch (env
->prog
->type
) {
711 case BPF_PROG_TYPE_LWT_IN
:
712 case BPF_PROG_TYPE_LWT_OUT
:
713 /* dst_input() and dst_output() can't write for now */
717 case BPF_PROG_TYPE_SCHED_CLS
:
718 case BPF_PROG_TYPE_SCHED_ACT
:
719 case BPF_PROG_TYPE_XDP
:
720 case BPF_PROG_TYPE_LWT_XMIT
:
722 return meta
->pkt_access
;
724 env
->seen_direct_write
= true;
731 static int check_packet_access(struct bpf_verifier_env
*env
, u32 regno
, int off
,
734 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
735 struct bpf_reg_state
*reg
= ®s
[regno
];
738 if (off
< 0 || size
<= 0 || off
+ size
> reg
->range
) {
739 verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
740 off
, size
, regno
, reg
->id
, reg
->off
, reg
->range
);
746 /* check access to 'struct bpf_context' fields */
747 static int check_ctx_access(struct bpf_verifier_env
*env
, int insn_idx
, int off
, int size
,
748 enum bpf_access_type t
, enum bpf_reg_type
*reg_type
)
750 struct bpf_insn_access_aux info
= {
751 .reg_type
= *reg_type
,
754 /* for analyzer ctx accesses are already validated and converted */
755 if (env
->analyzer_ops
)
758 if (env
->prog
->aux
->ops
->is_valid_access
&&
759 env
->prog
->aux
->ops
->is_valid_access(off
, size
, t
, &info
)) {
760 /* A non zero info.ctx_field_size indicates that this field is a
761 * candidate for later verifier transformation to load the whole
762 * field and then apply a mask when accessed with a narrower
763 * access than actual ctx access size. A zero info.ctx_field_size
764 * will only allow for whole field access and rejects any other
765 * type of narrower access.
767 env
->insn_aux_data
[insn_idx
].ctx_field_size
= info
.ctx_field_size
;
768 *reg_type
= info
.reg_type
;
770 /* remember the offset of last byte accessed in ctx */
771 if (env
->prog
->aux
->max_ctx_offset
< off
+ size
)
772 env
->prog
->aux
->max_ctx_offset
= off
+ size
;
776 verbose("invalid bpf_context access off=%d size=%d\n", off
, size
);
780 static bool is_pointer_value(struct bpf_verifier_env
*env
, int regno
)
782 if (env
->allow_ptr_leaks
)
785 switch (env
->cur_state
.regs
[regno
].type
) {
794 static int check_pkt_ptr_alignment(const struct bpf_reg_state
*reg
,
795 int off
, int size
, bool strict
)
800 /* Byte size accesses are always allowed. */
801 if (!strict
|| size
== 1)
806 if (reg
->aux_off_align
% size
) {
807 verbose("Packet access is only %u byte aligned, %d byte access not allowed\n",
808 reg
->aux_off_align
, size
);
811 reg_off
+= reg
->aux_off
;
814 /* For platforms that do not have a Kconfig enabling
815 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
816 * NET_IP_ALIGN is universally set to '2'. And on platforms
817 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
818 * to this code only in strict mode where we want to emulate
819 * the NET_IP_ALIGN==2 checking. Therefore use an
820 * unconditional IP align value of '2'.
823 if ((ip_align
+ reg_off
+ off
) % size
!= 0) {
824 verbose("misaligned packet access off %d+%d+%d size %d\n",
825 ip_align
, reg_off
, off
, size
);
832 static int check_val_ptr_alignment(const struct bpf_reg_state
*reg
,
833 int size
, bool strict
)
835 if (strict
&& size
!= 1) {
836 verbose("Unknown alignment. Only byte-sized access allowed in value access.\n");
843 static int check_ptr_alignment(struct bpf_verifier_env
*env
,
844 const struct bpf_reg_state
*reg
,
847 bool strict
= env
->strict_alignment
;
851 return check_pkt_ptr_alignment(reg
, off
, size
, strict
);
852 case PTR_TO_MAP_VALUE_ADJ
:
853 return check_val_ptr_alignment(reg
, size
, strict
);
855 if (off
% size
!= 0) {
856 verbose("misaligned access off %d size %d\n",
865 /* check whether memory at (regno + off) is accessible for t = (read | write)
866 * if t==write, value_regno is a register which value is stored into memory
867 * if t==read, value_regno is a register which will receive the value from memory
868 * if t==write && value_regno==-1, some unknown value is stored into memory
869 * if t==read && value_regno==-1, don't care what we read from memory
871 static int check_mem_access(struct bpf_verifier_env
*env
, int insn_idx
, u32 regno
, int off
,
872 int bpf_size
, enum bpf_access_type t
,
875 struct bpf_verifier_state
*state
= &env
->cur_state
;
876 struct bpf_reg_state
*reg
= &state
->regs
[regno
];
879 if (reg
->type
== PTR_TO_STACK
)
882 size
= bpf_size_to_bytes(bpf_size
);
886 err
= check_ptr_alignment(env
, reg
, off
, size
);
890 if (reg
->type
== PTR_TO_MAP_VALUE
||
891 reg
->type
== PTR_TO_MAP_VALUE_ADJ
) {
892 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
893 is_pointer_value(env
, value_regno
)) {
894 verbose("R%d leaks addr into map\n", value_regno
);
898 if (reg
->type
== PTR_TO_MAP_VALUE_ADJ
)
899 err
= check_map_access_adj(env
, regno
, off
, size
);
901 err
= check_map_access(env
, regno
, off
, size
);
902 if (!err
&& t
== BPF_READ
&& value_regno
>= 0)
903 mark_reg_unknown_value_and_range(state
->regs
,
906 } else if (reg
->type
== PTR_TO_CTX
) {
907 enum bpf_reg_type reg_type
= UNKNOWN_VALUE
;
909 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
910 is_pointer_value(env
, value_regno
)) {
911 verbose("R%d leaks addr into ctx\n", value_regno
);
914 err
= check_ctx_access(env
, insn_idx
, off
, size
, t
, ®_type
);
915 if (!err
&& t
== BPF_READ
&& value_regno
>= 0) {
916 mark_reg_unknown_value_and_range(state
->regs
,
918 /* note that reg.[id|off|range] == 0 */
919 state
->regs
[value_regno
].type
= reg_type
;
920 state
->regs
[value_regno
].aux_off
= 0;
921 state
->regs
[value_regno
].aux_off_align
= 0;
924 } else if (reg
->type
== FRAME_PTR
|| reg
->type
== PTR_TO_STACK
) {
925 if (off
>= 0 || off
< -MAX_BPF_STACK
) {
926 verbose("invalid stack off=%d size=%d\n", off
, size
);
930 if (env
->prog
->aux
->stack_depth
< -off
)
931 env
->prog
->aux
->stack_depth
= -off
;
933 if (t
== BPF_WRITE
) {
934 if (!env
->allow_ptr_leaks
&&
935 state
->stack_slot_type
[MAX_BPF_STACK
+ off
] == STACK_SPILL
&&
936 size
!= BPF_REG_SIZE
) {
937 verbose("attempt to corrupt spilled pointer on stack\n");
940 err
= check_stack_write(state
, off
, size
, value_regno
);
942 err
= check_stack_read(state
, off
, size
, value_regno
);
944 } else if (state
->regs
[regno
].type
== PTR_TO_PACKET
) {
945 if (t
== BPF_WRITE
&& !may_access_direct_pkt_data(env
, NULL
, t
)) {
946 verbose("cannot write into packet\n");
949 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
950 is_pointer_value(env
, value_regno
)) {
951 verbose("R%d leaks addr into packet\n", value_regno
);
954 err
= check_packet_access(env
, regno
, off
, size
);
955 if (!err
&& t
== BPF_READ
&& value_regno
>= 0)
956 mark_reg_unknown_value_and_range(state
->regs
,
959 verbose("R%d invalid mem access '%s'\n",
960 regno
, reg_type_str
[reg
->type
]);
964 if (!err
&& size
<= 2 && value_regno
>= 0 && env
->allow_ptr_leaks
&&
965 state
->regs
[value_regno
].type
== UNKNOWN_VALUE
) {
966 /* 1 or 2 byte load zero-extends, determine the number of
967 * zero upper bits. Not doing it fo 4 byte load, since
968 * such values cannot be added to ptr_to_packet anyway.
970 state
->regs
[value_regno
].imm
= 64 - size
* 8;
975 static int check_xadd(struct bpf_verifier_env
*env
, int insn_idx
, struct bpf_insn
*insn
)
977 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
980 if ((BPF_SIZE(insn
->code
) != BPF_W
&& BPF_SIZE(insn
->code
) != BPF_DW
) ||
982 verbose("BPF_XADD uses reserved fields\n");
986 /* check src1 operand */
987 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
991 /* check src2 operand */
992 err
= check_reg_arg(regs
, insn
->dst_reg
, SRC_OP
);
996 if (is_pointer_value(env
, insn
->src_reg
)) {
997 verbose("R%d leaks addr into mem\n", insn
->src_reg
);
1001 /* check whether atomic_add can read the memory */
1002 err
= check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
1003 BPF_SIZE(insn
->code
), BPF_READ
, -1);
1007 /* check whether atomic_add can write into the same memory */
1008 return check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
1009 BPF_SIZE(insn
->code
), BPF_WRITE
, -1);
1012 /* when register 'regno' is passed into function that will read 'access_size'
1013 * bytes from that pointer, make sure that it's within stack boundary
1014 * and all elements of stack are initialized
1016 static int check_stack_boundary(struct bpf_verifier_env
*env
, int regno
,
1017 int access_size
, bool zero_size_allowed
,
1018 struct bpf_call_arg_meta
*meta
)
1020 struct bpf_verifier_state
*state
= &env
->cur_state
;
1021 struct bpf_reg_state
*regs
= state
->regs
;
1024 if (regs
[regno
].type
!= PTR_TO_STACK
) {
1025 if (zero_size_allowed
&& access_size
== 0 &&
1026 regs
[regno
].type
== CONST_IMM
&&
1027 regs
[regno
].imm
== 0)
1030 verbose("R%d type=%s expected=%s\n", regno
,
1031 reg_type_str
[regs
[regno
].type
],
1032 reg_type_str
[PTR_TO_STACK
]);
1036 off
= regs
[regno
].imm
;
1037 if (off
>= 0 || off
< -MAX_BPF_STACK
|| off
+ access_size
> 0 ||
1039 verbose("invalid stack type R%d off=%d access_size=%d\n",
1040 regno
, off
, access_size
);
1044 if (env
->prog
->aux
->stack_depth
< -off
)
1045 env
->prog
->aux
->stack_depth
= -off
;
1047 if (meta
&& meta
->raw_mode
) {
1048 meta
->access_size
= access_size
;
1049 meta
->regno
= regno
;
1053 for (i
= 0; i
< access_size
; i
++) {
1054 if (state
->stack_slot_type
[MAX_BPF_STACK
+ off
+ i
] != STACK_MISC
) {
1055 verbose("invalid indirect read from stack off %d+%d size %d\n",
1056 off
, i
, access_size
);
1063 static int check_helper_mem_access(struct bpf_verifier_env
*env
, int regno
,
1064 int access_size
, bool zero_size_allowed
,
1065 struct bpf_call_arg_meta
*meta
)
1067 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
1069 switch (regs
[regno
].type
) {
1071 return check_packet_access(env
, regno
, 0, access_size
);
1072 case PTR_TO_MAP_VALUE
:
1073 return check_map_access(env
, regno
, 0, access_size
);
1074 case PTR_TO_MAP_VALUE_ADJ
:
1075 return check_map_access_adj(env
, regno
, 0, access_size
);
1076 default: /* const_imm|ptr_to_stack or invalid ptr */
1077 return check_stack_boundary(env
, regno
, access_size
,
1078 zero_size_allowed
, meta
);
1082 static int check_func_arg(struct bpf_verifier_env
*env
, u32 regno
,
1083 enum bpf_arg_type arg_type
,
1084 struct bpf_call_arg_meta
*meta
)
1086 struct bpf_reg_state
*regs
= env
->cur_state
.regs
, *reg
= ®s
[regno
];
1087 enum bpf_reg_type expected_type
, type
= reg
->type
;
1090 if (arg_type
== ARG_DONTCARE
)
1093 if (type
== NOT_INIT
) {
1094 verbose("R%d !read_ok\n", regno
);
1098 if (arg_type
== ARG_ANYTHING
) {
1099 if (is_pointer_value(env
, regno
)) {
1100 verbose("R%d leaks addr into helper function\n", regno
);
1106 if (type
== PTR_TO_PACKET
&&
1107 !may_access_direct_pkt_data(env
, meta
, BPF_READ
)) {
1108 verbose("helper access to the packet is not allowed\n");
1112 if (arg_type
== ARG_PTR_TO_MAP_KEY
||
1113 arg_type
== ARG_PTR_TO_MAP_VALUE
) {
1114 expected_type
= PTR_TO_STACK
;
1115 if (type
!= PTR_TO_PACKET
&& type
!= expected_type
)
1117 } else if (arg_type
== ARG_CONST_SIZE
||
1118 arg_type
== ARG_CONST_SIZE_OR_ZERO
) {
1119 expected_type
= CONST_IMM
;
1120 /* One exception. Allow UNKNOWN_VALUE registers when the
1121 * boundaries are known and don't cause unsafe memory accesses
1123 if (type
!= UNKNOWN_VALUE
&& type
!= expected_type
)
1125 } else if (arg_type
== ARG_CONST_MAP_PTR
) {
1126 expected_type
= CONST_PTR_TO_MAP
;
1127 if (type
!= expected_type
)
1129 } else if (arg_type
== ARG_PTR_TO_CTX
) {
1130 expected_type
= PTR_TO_CTX
;
1131 if (type
!= expected_type
)
1133 } else if (arg_type
== ARG_PTR_TO_MEM
||
1134 arg_type
== ARG_PTR_TO_UNINIT_MEM
) {
1135 expected_type
= PTR_TO_STACK
;
1136 /* One exception here. In case function allows for NULL to be
1137 * passed in as argument, it's a CONST_IMM type. Final test
1138 * happens during stack boundary checking.
1140 if (type
== CONST_IMM
&& reg
->imm
== 0)
1141 /* final test in check_stack_boundary() */;
1142 else if (type
!= PTR_TO_PACKET
&& type
!= PTR_TO_MAP_VALUE
&&
1143 type
!= PTR_TO_MAP_VALUE_ADJ
&& type
!= expected_type
)
1145 meta
->raw_mode
= arg_type
== ARG_PTR_TO_UNINIT_MEM
;
1147 verbose("unsupported arg_type %d\n", arg_type
);
1151 if (arg_type
== ARG_CONST_MAP_PTR
) {
1152 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
1153 meta
->map_ptr
= reg
->map_ptr
;
1154 } else if (arg_type
== ARG_PTR_TO_MAP_KEY
) {
1155 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1156 * check that [key, key + map->key_size) are within
1157 * stack limits and initialized
1159 if (!meta
->map_ptr
) {
1160 /* in function declaration map_ptr must come before
1161 * map_key, so that it's verified and known before
1162 * we have to check map_key here. Otherwise it means
1163 * that kernel subsystem misconfigured verifier
1165 verbose("invalid map_ptr to access map->key\n");
1168 if (type
== PTR_TO_PACKET
)
1169 err
= check_packet_access(env
, regno
, 0,
1170 meta
->map_ptr
->key_size
);
1172 err
= check_stack_boundary(env
, regno
,
1173 meta
->map_ptr
->key_size
,
1175 } else if (arg_type
== ARG_PTR_TO_MAP_VALUE
) {
1176 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1177 * check [value, value + map->value_size) validity
1179 if (!meta
->map_ptr
) {
1180 /* kernel subsystem misconfigured verifier */
1181 verbose("invalid map_ptr to access map->value\n");
1184 if (type
== PTR_TO_PACKET
)
1185 err
= check_packet_access(env
, regno
, 0,
1186 meta
->map_ptr
->value_size
);
1188 err
= check_stack_boundary(env
, regno
,
1189 meta
->map_ptr
->value_size
,
1191 } else if (arg_type
== ARG_CONST_SIZE
||
1192 arg_type
== ARG_CONST_SIZE_OR_ZERO
) {
1193 bool zero_size_allowed
= (arg_type
== ARG_CONST_SIZE_OR_ZERO
);
1195 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1196 * from stack pointer 'buf'. Check it
1197 * note: regno == len, regno - 1 == buf
1200 /* kernel subsystem misconfigured verifier */
1201 verbose("ARG_CONST_SIZE cannot be first argument\n");
1205 /* If the register is UNKNOWN_VALUE, the access check happens
1206 * using its boundaries. Otherwise, just use its imm
1208 if (type
== UNKNOWN_VALUE
) {
1209 /* For unprivileged variable accesses, disable raw
1210 * mode so that the program is required to
1211 * initialize all the memory that the helper could
1212 * just partially fill up.
1216 if (reg
->min_value
< 0) {
1217 verbose("R%d min value is negative, either use unsigned or 'var &= const'\n",
1222 if (reg
->min_value
== 0) {
1223 err
= check_helper_mem_access(env
, regno
- 1, 0,
1230 if (reg
->max_value
== BPF_REGISTER_MAX_RANGE
) {
1231 verbose("R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
1235 err
= check_helper_mem_access(env
, regno
- 1,
1237 zero_size_allowed
, meta
);
1241 /* register is CONST_IMM */
1242 err
= check_helper_mem_access(env
, regno
- 1, reg
->imm
,
1243 zero_size_allowed
, meta
);
1249 verbose("R%d type=%s expected=%s\n", regno
,
1250 reg_type_str
[type
], reg_type_str
[expected_type
]);
1254 static int check_map_func_compatibility(struct bpf_map
*map
, int func_id
)
1259 /* We need a two way check, first is from map perspective ... */
1260 switch (map
->map_type
) {
1261 case BPF_MAP_TYPE_PROG_ARRAY
:
1262 if (func_id
!= BPF_FUNC_tail_call
)
1265 case BPF_MAP_TYPE_PERF_EVENT_ARRAY
:
1266 if (func_id
!= BPF_FUNC_perf_event_read
&&
1267 func_id
!= BPF_FUNC_perf_event_output
)
1270 case BPF_MAP_TYPE_STACK_TRACE
:
1271 if (func_id
!= BPF_FUNC_get_stackid
)
1274 case BPF_MAP_TYPE_CGROUP_ARRAY
:
1275 if (func_id
!= BPF_FUNC_skb_under_cgroup
&&
1276 func_id
!= BPF_FUNC_current_task_under_cgroup
)
1279 case BPF_MAP_TYPE_ARRAY_OF_MAPS
:
1280 case BPF_MAP_TYPE_HASH_OF_MAPS
:
1281 if (func_id
!= BPF_FUNC_map_lookup_elem
)
1287 /* ... and second from the function itself. */
1289 case BPF_FUNC_tail_call
:
1290 if (map
->map_type
!= BPF_MAP_TYPE_PROG_ARRAY
)
1293 case BPF_FUNC_perf_event_read
:
1294 case BPF_FUNC_perf_event_output
:
1295 if (map
->map_type
!= BPF_MAP_TYPE_PERF_EVENT_ARRAY
)
1298 case BPF_FUNC_get_stackid
:
1299 if (map
->map_type
!= BPF_MAP_TYPE_STACK_TRACE
)
1302 case BPF_FUNC_current_task_under_cgroup
:
1303 case BPF_FUNC_skb_under_cgroup
:
1304 if (map
->map_type
!= BPF_MAP_TYPE_CGROUP_ARRAY
)
1313 verbose("cannot pass map_type %d into func %s#%d\n",
1314 map
->map_type
, func_id_name(func_id
), func_id
);
1318 static int check_raw_mode(const struct bpf_func_proto
*fn
)
1322 if (fn
->arg1_type
== ARG_PTR_TO_UNINIT_MEM
)
1324 if (fn
->arg2_type
== ARG_PTR_TO_UNINIT_MEM
)
1326 if (fn
->arg3_type
== ARG_PTR_TO_UNINIT_MEM
)
1328 if (fn
->arg4_type
== ARG_PTR_TO_UNINIT_MEM
)
1330 if (fn
->arg5_type
== ARG_PTR_TO_UNINIT_MEM
)
1333 return count
> 1 ? -EINVAL
: 0;
1336 static void clear_all_pkt_pointers(struct bpf_verifier_env
*env
)
1338 struct bpf_verifier_state
*state
= &env
->cur_state
;
1339 struct bpf_reg_state
*regs
= state
->regs
, *reg
;
1342 for (i
= 0; i
< MAX_BPF_REG
; i
++)
1343 if (regs
[i
].type
== PTR_TO_PACKET
||
1344 regs
[i
].type
== PTR_TO_PACKET_END
)
1345 mark_reg_unknown_value(regs
, i
);
1347 for (i
= 0; i
< MAX_BPF_STACK
; i
+= BPF_REG_SIZE
) {
1348 if (state
->stack_slot_type
[i
] != STACK_SPILL
)
1350 reg
= &state
->spilled_regs
[i
/ BPF_REG_SIZE
];
1351 if (reg
->type
!= PTR_TO_PACKET
&&
1352 reg
->type
!= PTR_TO_PACKET_END
)
1354 __mark_reg_unknown_value(state
->spilled_regs
,
1359 static int check_call(struct bpf_verifier_env
*env
, int func_id
, int insn_idx
)
1361 struct bpf_verifier_state
*state
= &env
->cur_state
;
1362 const struct bpf_func_proto
*fn
= NULL
;
1363 struct bpf_reg_state
*regs
= state
->regs
;
1364 struct bpf_call_arg_meta meta
;
1368 /* find function prototype */
1369 if (func_id
< 0 || func_id
>= __BPF_FUNC_MAX_ID
) {
1370 verbose("invalid func %s#%d\n", func_id_name(func_id
), func_id
);
1374 if (env
->prog
->aux
->ops
->get_func_proto
)
1375 fn
= env
->prog
->aux
->ops
->get_func_proto(func_id
);
1378 verbose("unknown func %s#%d\n", func_id_name(func_id
), func_id
);
1382 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1383 if (!env
->prog
->gpl_compatible
&& fn
->gpl_only
) {
1384 verbose("cannot call GPL only function from proprietary program\n");
1388 changes_data
= bpf_helper_changes_pkt_data(fn
->func
);
1390 memset(&meta
, 0, sizeof(meta
));
1391 meta
.pkt_access
= fn
->pkt_access
;
1393 /* We only support one arg being in raw mode at the moment, which
1394 * is sufficient for the helper functions we have right now.
1396 err
= check_raw_mode(fn
);
1398 verbose("kernel subsystem misconfigured func %s#%d\n",
1399 func_id_name(func_id
), func_id
);
1404 err
= check_func_arg(env
, BPF_REG_1
, fn
->arg1_type
, &meta
);
1407 err
= check_func_arg(env
, BPF_REG_2
, fn
->arg2_type
, &meta
);
1410 err
= check_func_arg(env
, BPF_REG_3
, fn
->arg3_type
, &meta
);
1413 err
= check_func_arg(env
, BPF_REG_4
, fn
->arg4_type
, &meta
);
1416 err
= check_func_arg(env
, BPF_REG_5
, fn
->arg5_type
, &meta
);
1420 /* Mark slots with STACK_MISC in case of raw mode, stack offset
1421 * is inferred from register state.
1423 for (i
= 0; i
< meta
.access_size
; i
++) {
1424 err
= check_mem_access(env
, insn_idx
, meta
.regno
, i
, BPF_B
, BPF_WRITE
, -1);
1429 /* reset caller saved regs */
1430 for (i
= 0; i
< CALLER_SAVED_REGS
; i
++)
1431 mark_reg_not_init(regs
, caller_saved
[i
]);
1433 /* update return register */
1434 if (fn
->ret_type
== RET_INTEGER
) {
1435 regs
[BPF_REG_0
].type
= UNKNOWN_VALUE
;
1436 } else if (fn
->ret_type
== RET_VOID
) {
1437 regs
[BPF_REG_0
].type
= NOT_INIT
;
1438 } else if (fn
->ret_type
== RET_PTR_TO_MAP_VALUE_OR_NULL
) {
1439 struct bpf_insn_aux_data
*insn_aux
;
1441 regs
[BPF_REG_0
].type
= PTR_TO_MAP_VALUE_OR_NULL
;
1442 regs
[BPF_REG_0
].max_value
= regs
[BPF_REG_0
].min_value
= 0;
1443 /* remember map_ptr, so that check_map_access()
1444 * can check 'value_size' boundary of memory access
1445 * to map element returned from bpf_map_lookup_elem()
1447 if (meta
.map_ptr
== NULL
) {
1448 verbose("kernel subsystem misconfigured verifier\n");
1451 regs
[BPF_REG_0
].map_ptr
= meta
.map_ptr
;
1452 regs
[BPF_REG_0
].id
= ++env
->id_gen
;
1453 insn_aux
= &env
->insn_aux_data
[insn_idx
];
1454 if (!insn_aux
->map_ptr
)
1455 insn_aux
->map_ptr
= meta
.map_ptr
;
1456 else if (insn_aux
->map_ptr
!= meta
.map_ptr
)
1457 insn_aux
->map_ptr
= BPF_MAP_PTR_POISON
;
1459 verbose("unknown return type %d of func %s#%d\n",
1460 fn
->ret_type
, func_id_name(func_id
), func_id
);
1464 err
= check_map_func_compatibility(meta
.map_ptr
, func_id
);
1469 clear_all_pkt_pointers(env
);
1473 static int check_packet_ptr_add(struct bpf_verifier_env
*env
,
1474 struct bpf_insn
*insn
)
1476 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
1477 struct bpf_reg_state
*dst_reg
= ®s
[insn
->dst_reg
];
1478 struct bpf_reg_state
*src_reg
= ®s
[insn
->src_reg
];
1479 struct bpf_reg_state tmp_reg
;
1482 if (BPF_SRC(insn
->code
) == BPF_K
) {
1483 /* pkt_ptr += imm */
1488 verbose("addition of negative constant to packet pointer is not allowed\n");
1491 if (imm
>= MAX_PACKET_OFF
||
1492 imm
+ dst_reg
->off
>= MAX_PACKET_OFF
) {
1493 verbose("constant %d is too large to add to packet pointer\n",
1497 /* a constant was added to pkt_ptr.
1498 * Remember it while keeping the same 'id'
1500 dst_reg
->off
+= imm
;
1504 if (src_reg
->type
== PTR_TO_PACKET
) {
1505 /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */
1506 tmp_reg
= *dst_reg
; /* save r7 state */
1507 *dst_reg
= *src_reg
; /* copy pkt_ptr state r6 into r7 */
1508 src_reg
= &tmp_reg
; /* pretend it's src_reg state */
1509 /* if the checks below reject it, the copy won't matter,
1510 * since we're rejecting the whole program. If all ok,
1511 * then imm22 state will be added to r7
1512 * and r7 will be pkt(id=0,off=22,r=62) while
1513 * r6 will stay as pkt(id=0,off=0,r=62)
1517 if (src_reg
->type
== CONST_IMM
) {
1518 /* pkt_ptr += reg where reg is known constant */
1522 /* disallow pkt_ptr += reg
1523 * if reg is not uknown_value with guaranteed zero upper bits
1524 * otherwise pkt_ptr may overflow and addition will become
1525 * subtraction which is not allowed
1527 if (src_reg
->type
!= UNKNOWN_VALUE
) {
1528 verbose("cannot add '%s' to ptr_to_packet\n",
1529 reg_type_str
[src_reg
->type
]);
1532 if (src_reg
->imm
< 48) {
1533 verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n",
1538 had_id
= (dst_reg
->id
!= 0);
1540 /* dst_reg stays as pkt_ptr type and since some positive
1541 * integer value was added to the pointer, increment its 'id'
1543 dst_reg
->id
= ++env
->id_gen
;
1545 /* something was added to pkt_ptr, set range to zero */
1546 dst_reg
->aux_off
+= dst_reg
->off
;
1550 dst_reg
->aux_off_align
= min(dst_reg
->aux_off_align
,
1551 src_reg
->min_align
);
1553 dst_reg
->aux_off_align
= src_reg
->min_align
;
1558 static int evaluate_reg_alu(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
1560 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
1561 struct bpf_reg_state
*dst_reg
= ®s
[insn
->dst_reg
];
1562 u8 opcode
= BPF_OP(insn
->code
);
1565 /* for type == UNKNOWN_VALUE:
1566 * imm > 0 -> number of zero upper bits
1567 * imm == 0 -> don't track which is the same as all bits can be non-zero
1570 if (BPF_SRC(insn
->code
) == BPF_X
) {
1571 struct bpf_reg_state
*src_reg
= ®s
[insn
->src_reg
];
1573 if (src_reg
->type
== UNKNOWN_VALUE
&& src_reg
->imm
> 0 &&
1574 dst_reg
->imm
&& opcode
== BPF_ADD
) {
1576 * where both have zero upper bits. Adding them
1577 * can only result making one more bit non-zero
1578 * in the larger value.
1579 * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
1580 * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
1582 dst_reg
->imm
= min(dst_reg
->imm
, src_reg
->imm
);
1586 if (src_reg
->type
== CONST_IMM
&& src_reg
->imm
> 0 &&
1587 dst_reg
->imm
&& opcode
== BPF_ADD
) {
1589 * where dreg has zero upper bits and sreg is const.
1590 * Adding them can only result making one more bit
1591 * non-zero in the larger value.
1593 imm_log2
= __ilog2_u64((long long)src_reg
->imm
);
1594 dst_reg
->imm
= min(dst_reg
->imm
, 63 - imm_log2
);
1598 /* all other cases non supported yet, just mark dst_reg */
1603 /* sign extend 32-bit imm into 64-bit to make sure that
1604 * negative values occupy bit 63. Note ilog2() would have
1605 * been incorrect, since sizeof(insn->imm) == 4
1607 imm_log2
= __ilog2_u64((long long)insn
->imm
);
1609 if (dst_reg
->imm
&& opcode
== BPF_LSH
) {
1611 * if reg was a result of 2 byte load, then its imm == 48
1612 * which means that upper 48 bits are zero and shifting this reg
1613 * left by 4 would mean that upper 44 bits are still zero
1615 dst_reg
->imm
-= insn
->imm
;
1616 } else if (dst_reg
->imm
&& opcode
== BPF_MUL
) {
1618 * if multiplying by 14 subtract 4
1619 * This is conservative calculation of upper zero bits.
1620 * It's not trying to special case insn->imm == 1 or 0 cases
1622 dst_reg
->imm
-= imm_log2
+ 1;
1623 } else if (opcode
== BPF_AND
) {
1625 dst_reg
->imm
= 63 - imm_log2
;
1626 } else if (dst_reg
->imm
&& opcode
== BPF_ADD
) {
1628 dst_reg
->imm
= min(dst_reg
->imm
, 63 - imm_log2
);
1630 } else if (opcode
== BPF_RSH
) {
1632 * which means that after right shift, upper bits will be zero
1633 * note that verifier already checked that
1634 * 0 <= imm < 64 for shift insn
1636 dst_reg
->imm
+= insn
->imm
;
1637 if (unlikely(dst_reg
->imm
> 64))
1638 /* some dumb code did:
1641 * and all bits are zero now */
1644 /* all other alu ops, means that we don't know what will
1645 * happen to the value, mark it with unknown number of zero bits
1650 if (dst_reg
->imm
< 0) {
1651 /* all 64 bits of the register can contain non-zero bits
1652 * and such value cannot be added to ptr_to_packet, since it
1653 * may overflow, mark it as unknown to avoid further eval
1660 static int evaluate_reg_imm_alu_unknown(struct bpf_verifier_env
*env
,
1661 struct bpf_insn
*insn
)
1663 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
1664 struct bpf_reg_state
*dst_reg
= ®s
[insn
->dst_reg
];
1665 struct bpf_reg_state
*src_reg
= ®s
[insn
->src_reg
];
1666 u8 opcode
= BPF_OP(insn
->code
);
1667 s64 imm_log2
= __ilog2_u64((long long)dst_reg
->imm
);
1669 /* BPF_X code with src_reg->type UNKNOWN_VALUE here. */
1670 if (src_reg
->imm
> 0 && dst_reg
->imm
) {
1674 * where both have zero upper bits. Adding them
1675 * can only result making one more bit non-zero
1676 * in the larger value.
1677 * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47)
1678 * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47)
1680 dst_reg
->imm
= min(src_reg
->imm
, 63 - imm_log2
);
1685 * AND can not extend zero bits only shrink
1686 * Ex. 0x00..00ffffff
1691 dst_reg
->imm
= max(src_reg
->imm
, 63 - imm_log2
);
1695 * OR can only extend zero bits
1696 * Ex. 0x00..00ffffff
1701 dst_reg
->imm
= min(src_reg
->imm
, 63 - imm_log2
);
1707 /* These may be flushed out later */
1709 mark_reg_unknown_value(regs
, insn
->dst_reg
);
1712 mark_reg_unknown_value(regs
, insn
->dst_reg
);
1715 dst_reg
->type
= UNKNOWN_VALUE
;
1719 static int evaluate_reg_imm_alu(struct bpf_verifier_env
*env
,
1720 struct bpf_insn
*insn
)
1722 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
1723 struct bpf_reg_state
*dst_reg
= ®s
[insn
->dst_reg
];
1724 struct bpf_reg_state
*src_reg
= ®s
[insn
->src_reg
];
1725 u8 opcode
= BPF_OP(insn
->code
);
1726 u64 dst_imm
= dst_reg
->imm
;
1728 if (BPF_SRC(insn
->code
) == BPF_X
&& src_reg
->type
== UNKNOWN_VALUE
)
1729 return evaluate_reg_imm_alu_unknown(env
, insn
);
1731 /* dst_reg->type == CONST_IMM here. Simulate execution of insns
1732 * containing ALU ops. Don't care about overflow or negative
1733 * values, just add/sub/... them; registers are in u64.
1735 if (opcode
== BPF_ADD
&& BPF_SRC(insn
->code
) == BPF_K
) {
1736 dst_imm
+= insn
->imm
;
1737 } else if (opcode
== BPF_ADD
&& BPF_SRC(insn
->code
) == BPF_X
&&
1738 src_reg
->type
== CONST_IMM
) {
1739 dst_imm
+= src_reg
->imm
;
1740 } else if (opcode
== BPF_SUB
&& BPF_SRC(insn
->code
) == BPF_K
) {
1741 dst_imm
-= insn
->imm
;
1742 } else if (opcode
== BPF_SUB
&& BPF_SRC(insn
->code
) == BPF_X
&&
1743 src_reg
->type
== CONST_IMM
) {
1744 dst_imm
-= src_reg
->imm
;
1745 } else if (opcode
== BPF_MUL
&& BPF_SRC(insn
->code
) == BPF_K
) {
1746 dst_imm
*= insn
->imm
;
1747 } else if (opcode
== BPF_MUL
&& BPF_SRC(insn
->code
) == BPF_X
&&
1748 src_reg
->type
== CONST_IMM
) {
1749 dst_imm
*= src_reg
->imm
;
1750 } else if (opcode
== BPF_OR
&& BPF_SRC(insn
->code
) == BPF_K
) {
1751 dst_imm
|= insn
->imm
;
1752 } else if (opcode
== BPF_OR
&& BPF_SRC(insn
->code
) == BPF_X
&&
1753 src_reg
->type
== CONST_IMM
) {
1754 dst_imm
|= src_reg
->imm
;
1755 } else if (opcode
== BPF_AND
&& BPF_SRC(insn
->code
) == BPF_K
) {
1756 dst_imm
&= insn
->imm
;
1757 } else if (opcode
== BPF_AND
&& BPF_SRC(insn
->code
) == BPF_X
&&
1758 src_reg
->type
== CONST_IMM
) {
1759 dst_imm
&= src_reg
->imm
;
1760 } else if (opcode
== BPF_RSH
&& BPF_SRC(insn
->code
) == BPF_K
) {
1761 dst_imm
>>= insn
->imm
;
1762 } else if (opcode
== BPF_RSH
&& BPF_SRC(insn
->code
) == BPF_X
&&
1763 src_reg
->type
== CONST_IMM
) {
1764 dst_imm
>>= src_reg
->imm
;
1765 } else if (opcode
== BPF_LSH
&& BPF_SRC(insn
->code
) == BPF_K
) {
1766 dst_imm
<<= insn
->imm
;
1767 } else if (opcode
== BPF_LSH
&& BPF_SRC(insn
->code
) == BPF_X
&&
1768 src_reg
->type
== CONST_IMM
) {
1769 dst_imm
<<= src_reg
->imm
;
1771 mark_reg_unknown_value(regs
, insn
->dst_reg
);
1775 dst_reg
->imm
= dst_imm
;
1780 static void check_reg_overflow(struct bpf_reg_state
*reg
)
1782 if (reg
->max_value
> BPF_REGISTER_MAX_RANGE
)
1783 reg
->max_value
= BPF_REGISTER_MAX_RANGE
;
1784 if (reg
->min_value
< BPF_REGISTER_MIN_RANGE
||
1785 reg
->min_value
> BPF_REGISTER_MAX_RANGE
)
1786 reg
->min_value
= BPF_REGISTER_MIN_RANGE
;
1789 static u32
calc_align(u32 imm
)
1793 return imm
- ((imm
- 1) & imm
);
1796 static void adjust_reg_min_max_vals(struct bpf_verifier_env
*env
,
1797 struct bpf_insn
*insn
)
1799 struct bpf_reg_state
*regs
= env
->cur_state
.regs
, *dst_reg
;
1800 s64 min_val
= BPF_REGISTER_MIN_RANGE
;
1801 u64 max_val
= BPF_REGISTER_MAX_RANGE
;
1802 u8 opcode
= BPF_OP(insn
->code
);
1803 u32 dst_align
, src_align
;
1805 dst_reg
= ®s
[insn
->dst_reg
];
1807 if (BPF_SRC(insn
->code
) == BPF_X
) {
1808 check_reg_overflow(®s
[insn
->src_reg
]);
1809 min_val
= regs
[insn
->src_reg
].min_value
;
1810 max_val
= regs
[insn
->src_reg
].max_value
;
1812 /* If the source register is a random pointer then the
1813 * min_value/max_value values represent the range of the known
1814 * accesses into that value, not the actual min/max value of the
1815 * register itself. In this case we have to reset the reg range
1816 * values so we know it is not safe to look at.
1818 if (regs
[insn
->src_reg
].type
!= CONST_IMM
&&
1819 regs
[insn
->src_reg
].type
!= UNKNOWN_VALUE
) {
1820 min_val
= BPF_REGISTER_MIN_RANGE
;
1821 max_val
= BPF_REGISTER_MAX_RANGE
;
1824 src_align
= regs
[insn
->src_reg
].min_align
;
1826 } else if (insn
->imm
< BPF_REGISTER_MAX_RANGE
&&
1827 (s64
)insn
->imm
> BPF_REGISTER_MIN_RANGE
) {
1828 min_val
= max_val
= insn
->imm
;
1829 src_align
= calc_align(insn
->imm
);
1832 dst_align
= dst_reg
->min_align
;
1834 /* We don't know anything about what was done to this register, mark it
1837 if (min_val
== BPF_REGISTER_MIN_RANGE
&&
1838 max_val
== BPF_REGISTER_MAX_RANGE
) {
1839 reset_reg_range_values(regs
, insn
->dst_reg
);
1843 /* If one of our values was at the end of our ranges then we can't just
1844 * do our normal operations to the register, we need to set the values
1845 * to the min/max since they are undefined.
1847 if (min_val
== BPF_REGISTER_MIN_RANGE
)
1848 dst_reg
->min_value
= BPF_REGISTER_MIN_RANGE
;
1849 if (max_val
== BPF_REGISTER_MAX_RANGE
)
1850 dst_reg
->max_value
= BPF_REGISTER_MAX_RANGE
;
1854 if (dst_reg
->min_value
!= BPF_REGISTER_MIN_RANGE
)
1855 dst_reg
->min_value
+= min_val
;
1856 if (dst_reg
->max_value
!= BPF_REGISTER_MAX_RANGE
)
1857 dst_reg
->max_value
+= max_val
;
1858 dst_reg
->min_align
= min(src_align
, dst_align
);
1861 if (dst_reg
->min_value
!= BPF_REGISTER_MIN_RANGE
)
1862 dst_reg
->min_value
-= min_val
;
1863 if (dst_reg
->max_value
!= BPF_REGISTER_MAX_RANGE
)
1864 dst_reg
->max_value
-= max_val
;
1865 dst_reg
->min_align
= min(src_align
, dst_align
);
1868 if (dst_reg
->min_value
!= BPF_REGISTER_MIN_RANGE
)
1869 dst_reg
->min_value
*= min_val
;
1870 if (dst_reg
->max_value
!= BPF_REGISTER_MAX_RANGE
)
1871 dst_reg
->max_value
*= max_val
;
1872 dst_reg
->min_align
= max(src_align
, dst_align
);
1875 /* Disallow AND'ing of negative numbers, ain't nobody got time
1876 * for that. Otherwise the minimum is 0 and the max is the max
1877 * value we could AND against.
1880 dst_reg
->min_value
= BPF_REGISTER_MIN_RANGE
;
1882 dst_reg
->min_value
= 0;
1883 dst_reg
->max_value
= max_val
;
1884 dst_reg
->min_align
= max(src_align
, dst_align
);
1887 /* Gotta have special overflow logic here, if we're shifting
1888 * more than MAX_RANGE then just assume we have an invalid
1891 if (min_val
> ilog2(BPF_REGISTER_MAX_RANGE
)) {
1892 dst_reg
->min_value
= BPF_REGISTER_MIN_RANGE
;
1893 dst_reg
->min_align
= 1;
1895 if (dst_reg
->min_value
!= BPF_REGISTER_MIN_RANGE
)
1896 dst_reg
->min_value
<<= min_val
;
1897 if (!dst_reg
->min_align
)
1898 dst_reg
->min_align
= 1;
1899 dst_reg
->min_align
<<= min_val
;
1901 if (max_val
> ilog2(BPF_REGISTER_MAX_RANGE
))
1902 dst_reg
->max_value
= BPF_REGISTER_MAX_RANGE
;
1903 else if (dst_reg
->max_value
!= BPF_REGISTER_MAX_RANGE
)
1904 dst_reg
->max_value
<<= max_val
;
1907 /* RSH by a negative number is undefined, and the BPF_RSH is an
1908 * unsigned shift, so make the appropriate casts.
1910 if (min_val
< 0 || dst_reg
->min_value
< 0) {
1911 dst_reg
->min_value
= BPF_REGISTER_MIN_RANGE
;
1913 dst_reg
->min_value
=
1914 (u64
)(dst_reg
->min_value
) >> min_val
;
1917 dst_reg
->min_align
= 1;
1919 dst_reg
->min_align
>>= (u64
) min_val
;
1920 if (!dst_reg
->min_align
)
1921 dst_reg
->min_align
= 1;
1923 if (dst_reg
->max_value
!= BPF_REGISTER_MAX_RANGE
)
1924 dst_reg
->max_value
>>= max_val
;
1927 reset_reg_range_values(regs
, insn
->dst_reg
);
1931 check_reg_overflow(dst_reg
);
1934 /* check validity of 32-bit and 64-bit arithmetic operations */
1935 static int check_alu_op(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
1937 struct bpf_reg_state
*regs
= env
->cur_state
.regs
, *dst_reg
;
1938 u8 opcode
= BPF_OP(insn
->code
);
1941 if (opcode
== BPF_END
|| opcode
== BPF_NEG
) {
1942 if (opcode
== BPF_NEG
) {
1943 if (BPF_SRC(insn
->code
) != 0 ||
1944 insn
->src_reg
!= BPF_REG_0
||
1945 insn
->off
!= 0 || insn
->imm
!= 0) {
1946 verbose("BPF_NEG uses reserved fields\n");
1950 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0 ||
1951 (insn
->imm
!= 16 && insn
->imm
!= 32 && insn
->imm
!= 64)) {
1952 verbose("BPF_END uses reserved fields\n");
1957 /* check src operand */
1958 err
= check_reg_arg(regs
, insn
->dst_reg
, SRC_OP
);
1962 if (is_pointer_value(env
, insn
->dst_reg
)) {
1963 verbose("R%d pointer arithmetic prohibited\n",
1968 /* check dest operand */
1969 err
= check_reg_arg(regs
, insn
->dst_reg
, DST_OP
);
1973 } else if (opcode
== BPF_MOV
) {
1975 if (BPF_SRC(insn
->code
) == BPF_X
) {
1976 if (insn
->imm
!= 0 || insn
->off
!= 0) {
1977 verbose("BPF_MOV uses reserved fields\n");
1981 /* check src operand */
1982 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
1986 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0) {
1987 verbose("BPF_MOV uses reserved fields\n");
1992 /* check dest operand */
1993 err
= check_reg_arg(regs
, insn
->dst_reg
, DST_OP
);
1997 /* we are setting our register to something new, we need to
1998 * reset its range values.
2000 reset_reg_range_values(regs
, insn
->dst_reg
);
2002 if (BPF_SRC(insn
->code
) == BPF_X
) {
2003 if (BPF_CLASS(insn
->code
) == BPF_ALU64
) {
2005 * copy register state to dest reg
2007 regs
[insn
->dst_reg
] = regs
[insn
->src_reg
];
2009 if (is_pointer_value(env
, insn
->src_reg
)) {
2010 verbose("R%d partial copy of pointer\n",
2014 mark_reg_unknown_value(regs
, insn
->dst_reg
);
2018 * remember the value we stored into this reg
2020 regs
[insn
->dst_reg
].type
= CONST_IMM
;
2021 regs
[insn
->dst_reg
].imm
= insn
->imm
;
2022 regs
[insn
->dst_reg
].id
= 0;
2023 regs
[insn
->dst_reg
].max_value
= insn
->imm
;
2024 regs
[insn
->dst_reg
].min_value
= insn
->imm
;
2025 regs
[insn
->dst_reg
].min_align
= calc_align(insn
->imm
);
2028 } else if (opcode
> BPF_END
) {
2029 verbose("invalid BPF_ALU opcode %x\n", opcode
);
2032 } else { /* all other ALU ops: and, sub, xor, add, ... */
2034 if (BPF_SRC(insn
->code
) == BPF_X
) {
2035 if (insn
->imm
!= 0 || insn
->off
!= 0) {
2036 verbose("BPF_ALU uses reserved fields\n");
2039 /* check src1 operand */
2040 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
2044 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0) {
2045 verbose("BPF_ALU uses reserved fields\n");
2050 /* check src2 operand */
2051 err
= check_reg_arg(regs
, insn
->dst_reg
, SRC_OP
);
2055 if ((opcode
== BPF_MOD
|| opcode
== BPF_DIV
) &&
2056 BPF_SRC(insn
->code
) == BPF_K
&& insn
->imm
== 0) {
2057 verbose("div by zero\n");
2061 if ((opcode
== BPF_LSH
|| opcode
== BPF_RSH
||
2062 opcode
== BPF_ARSH
) && BPF_SRC(insn
->code
) == BPF_K
) {
2063 int size
= BPF_CLASS(insn
->code
) == BPF_ALU64
? 64 : 32;
2065 if (insn
->imm
< 0 || insn
->imm
>= size
) {
2066 verbose("invalid shift %d\n", insn
->imm
);
2071 /* check dest operand */
2072 err
= check_reg_arg(regs
, insn
->dst_reg
, DST_OP_NO_MARK
);
2076 dst_reg
= ®s
[insn
->dst_reg
];
2078 /* first we want to adjust our ranges. */
2079 adjust_reg_min_max_vals(env
, insn
);
2081 /* pattern match 'bpf_add Rx, imm' instruction */
2082 if (opcode
== BPF_ADD
&& BPF_CLASS(insn
->code
) == BPF_ALU64
&&
2083 dst_reg
->type
== FRAME_PTR
&& BPF_SRC(insn
->code
) == BPF_K
) {
2084 dst_reg
->type
= PTR_TO_STACK
;
2085 dst_reg
->imm
= insn
->imm
;
2087 } else if (opcode
== BPF_ADD
&&
2088 BPF_CLASS(insn
->code
) == BPF_ALU64
&&
2089 dst_reg
->type
== PTR_TO_STACK
&&
2090 ((BPF_SRC(insn
->code
) == BPF_X
&&
2091 regs
[insn
->src_reg
].type
== CONST_IMM
) ||
2092 BPF_SRC(insn
->code
) == BPF_K
)) {
2093 if (BPF_SRC(insn
->code
) == BPF_X
)
2094 dst_reg
->imm
+= regs
[insn
->src_reg
].imm
;
2096 dst_reg
->imm
+= insn
->imm
;
2098 } else if (opcode
== BPF_ADD
&&
2099 BPF_CLASS(insn
->code
) == BPF_ALU64
&&
2100 (dst_reg
->type
== PTR_TO_PACKET
||
2101 (BPF_SRC(insn
->code
) == BPF_X
&&
2102 regs
[insn
->src_reg
].type
== PTR_TO_PACKET
))) {
2103 /* ptr_to_packet += K|X */
2104 return check_packet_ptr_add(env
, insn
);
2105 } else if (BPF_CLASS(insn
->code
) == BPF_ALU64
&&
2106 dst_reg
->type
== UNKNOWN_VALUE
&&
2107 env
->allow_ptr_leaks
) {
2108 /* unknown += K|X */
2109 return evaluate_reg_alu(env
, insn
);
2110 } else if (BPF_CLASS(insn
->code
) == BPF_ALU64
&&
2111 dst_reg
->type
== CONST_IMM
&&
2112 env
->allow_ptr_leaks
) {
2113 /* reg_imm += K|X */
2114 return evaluate_reg_imm_alu(env
, insn
);
2115 } else if (is_pointer_value(env
, insn
->dst_reg
)) {
2116 verbose("R%d pointer arithmetic prohibited\n",
2119 } else if (BPF_SRC(insn
->code
) == BPF_X
&&
2120 is_pointer_value(env
, insn
->src_reg
)) {
2121 verbose("R%d pointer arithmetic prohibited\n",
2126 /* If we did pointer math on a map value then just set it to our
2127 * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or
2128 * loads to this register appropriately, otherwise just mark the
2129 * register as unknown.
2131 if (env
->allow_ptr_leaks
&&
2132 BPF_CLASS(insn
->code
) == BPF_ALU64
&& opcode
== BPF_ADD
&&
2133 (dst_reg
->type
== PTR_TO_MAP_VALUE
||
2134 dst_reg
->type
== PTR_TO_MAP_VALUE_ADJ
))
2135 dst_reg
->type
= PTR_TO_MAP_VALUE_ADJ
;
2137 mark_reg_unknown_value(regs
, insn
->dst_reg
);
2143 static void find_good_pkt_pointers(struct bpf_verifier_state
*state
,
2144 struct bpf_reg_state
*dst_reg
)
2146 struct bpf_reg_state
*regs
= state
->regs
, *reg
;
2149 /* LLVM can generate two kind of checks:
2155 * if (r2 > pkt_end) goto <handle exception>
2159 * r2 == dst_reg, pkt_end == src_reg
2160 * r2=pkt(id=n,off=8,r=0)
2161 * r3=pkt(id=n,off=0,r=0)
2167 * if (pkt_end >= r2) goto <access okay>
2168 * <handle exception>
2171 * pkt_end == dst_reg, r2 == src_reg
2172 * r2=pkt(id=n,off=8,r=0)
2173 * r3=pkt(id=n,off=0,r=0)
2175 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
2176 * so that range of bytes [r3, r3 + 8) is safe to access.
2179 for (i
= 0; i
< MAX_BPF_REG
; i
++)
2180 if (regs
[i
].type
== PTR_TO_PACKET
&& regs
[i
].id
== dst_reg
->id
)
2181 /* keep the maximum range already checked */
2182 regs
[i
].range
= max(regs
[i
].range
, dst_reg
->off
);
2184 for (i
= 0; i
< MAX_BPF_STACK
; i
+= BPF_REG_SIZE
) {
2185 if (state
->stack_slot_type
[i
] != STACK_SPILL
)
2187 reg
= &state
->spilled_regs
[i
/ BPF_REG_SIZE
];
2188 if (reg
->type
== PTR_TO_PACKET
&& reg
->id
== dst_reg
->id
)
2189 reg
->range
= max(reg
->range
, dst_reg
->off
);
2193 /* Adjusts the register min/max values in the case that the dst_reg is the
2194 * variable register that we are working on, and src_reg is a constant or we're
2195 * simply doing a BPF_K check.
2197 static void reg_set_min_max(struct bpf_reg_state
*true_reg
,
2198 struct bpf_reg_state
*false_reg
, u64 val
,
2203 /* If this is false then we know nothing Jon Snow, but if it is
2204 * true then we know for sure.
2206 true_reg
->max_value
= true_reg
->min_value
= val
;
2209 /* If this is true we know nothing Jon Snow, but if it is false
2210 * we know the value for sure;
2212 false_reg
->max_value
= false_reg
->min_value
= val
;
2215 /* Unsigned comparison, the minimum value is 0. */
2216 false_reg
->min_value
= 0;
2219 /* If this is false then we know the maximum val is val,
2220 * otherwise we know the min val is val+1.
2222 false_reg
->max_value
= val
;
2223 true_reg
->min_value
= val
+ 1;
2226 /* Unsigned comparison, the minimum value is 0. */
2227 false_reg
->min_value
= 0;
2230 /* If this is false then we know the maximum value is val - 1,
2231 * otherwise we know the mimimum value is val.
2233 false_reg
->max_value
= val
- 1;
2234 true_reg
->min_value
= val
;
2240 check_reg_overflow(false_reg
);
2241 check_reg_overflow(true_reg
);
2244 /* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg
2245 * is the variable reg.
2247 static void reg_set_min_max_inv(struct bpf_reg_state
*true_reg
,
2248 struct bpf_reg_state
*false_reg
, u64 val
,
2253 /* If this is false then we know nothing Jon Snow, but if it is
2254 * true then we know for sure.
2256 true_reg
->max_value
= true_reg
->min_value
= val
;
2259 /* If this is true we know nothing Jon Snow, but if it is false
2260 * we know the value for sure;
2262 false_reg
->max_value
= false_reg
->min_value
= val
;
2265 /* Unsigned comparison, the minimum value is 0. */
2266 true_reg
->min_value
= 0;
2270 * If this is false, then the val is <= the register, if it is
2271 * true the register <= to the val.
2273 false_reg
->min_value
= val
;
2274 true_reg
->max_value
= val
- 1;
2277 /* Unsigned comparison, the minimum value is 0. */
2278 true_reg
->min_value
= 0;
2281 /* If this is false then constant < register, if it is true then
2282 * the register < constant.
2284 false_reg
->min_value
= val
+ 1;
2285 true_reg
->max_value
= val
;
2291 check_reg_overflow(false_reg
);
2292 check_reg_overflow(true_reg
);
2295 static void mark_map_reg(struct bpf_reg_state
*regs
, u32 regno
, u32 id
,
2296 enum bpf_reg_type type
)
2298 struct bpf_reg_state
*reg
= ®s
[regno
];
2300 if (reg
->type
== PTR_TO_MAP_VALUE_OR_NULL
&& reg
->id
== id
) {
2301 if (type
== UNKNOWN_VALUE
) {
2302 __mark_reg_unknown_value(regs
, regno
);
2303 } else if (reg
->map_ptr
->inner_map_meta
) {
2304 reg
->type
= CONST_PTR_TO_MAP
;
2305 reg
->map_ptr
= reg
->map_ptr
->inner_map_meta
;
2309 /* We don't need id from this point onwards anymore, thus we
2310 * should better reset it, so that state pruning has chances
2317 /* The logic is similar to find_good_pkt_pointers(), both could eventually
2318 * be folded together at some point.
2320 static void mark_map_regs(struct bpf_verifier_state
*state
, u32 regno
,
2321 enum bpf_reg_type type
)
2323 struct bpf_reg_state
*regs
= state
->regs
;
2324 u32 id
= regs
[regno
].id
;
2327 for (i
= 0; i
< MAX_BPF_REG
; i
++)
2328 mark_map_reg(regs
, i
, id
, type
);
2330 for (i
= 0; i
< MAX_BPF_STACK
; i
+= BPF_REG_SIZE
) {
2331 if (state
->stack_slot_type
[i
] != STACK_SPILL
)
2333 mark_map_reg(state
->spilled_regs
, i
/ BPF_REG_SIZE
, id
, type
);
2337 static int check_cond_jmp_op(struct bpf_verifier_env
*env
,
2338 struct bpf_insn
*insn
, int *insn_idx
)
2340 struct bpf_verifier_state
*other_branch
, *this_branch
= &env
->cur_state
;
2341 struct bpf_reg_state
*regs
= this_branch
->regs
, *dst_reg
;
2342 u8 opcode
= BPF_OP(insn
->code
);
2345 if (opcode
> BPF_EXIT
) {
2346 verbose("invalid BPF_JMP opcode %x\n", opcode
);
2350 if (BPF_SRC(insn
->code
) == BPF_X
) {
2351 if (insn
->imm
!= 0) {
2352 verbose("BPF_JMP uses reserved fields\n");
2356 /* check src1 operand */
2357 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
2361 if (is_pointer_value(env
, insn
->src_reg
)) {
2362 verbose("R%d pointer comparison prohibited\n",
2367 if (insn
->src_reg
!= BPF_REG_0
) {
2368 verbose("BPF_JMP uses reserved fields\n");
2373 /* check src2 operand */
2374 err
= check_reg_arg(regs
, insn
->dst_reg
, SRC_OP
);
2378 dst_reg
= ®s
[insn
->dst_reg
];
2380 /* detect if R == 0 where R was initialized to zero earlier */
2381 if (BPF_SRC(insn
->code
) == BPF_K
&&
2382 (opcode
== BPF_JEQ
|| opcode
== BPF_JNE
) &&
2383 dst_reg
->type
== CONST_IMM
&& dst_reg
->imm
== insn
->imm
) {
2384 if (opcode
== BPF_JEQ
) {
2385 /* if (imm == imm) goto pc+off;
2386 * only follow the goto, ignore fall-through
2388 *insn_idx
+= insn
->off
;
2391 /* if (imm != imm) goto pc+off;
2392 * only follow fall-through branch, since
2393 * that's where the program will go
2399 other_branch
= push_stack(env
, *insn_idx
+ insn
->off
+ 1, *insn_idx
);
2403 /* detect if we are comparing against a constant value so we can adjust
2404 * our min/max values for our dst register.
2406 if (BPF_SRC(insn
->code
) == BPF_X
) {
2407 if (regs
[insn
->src_reg
].type
== CONST_IMM
)
2408 reg_set_min_max(&other_branch
->regs
[insn
->dst_reg
],
2409 dst_reg
, regs
[insn
->src_reg
].imm
,
2411 else if (dst_reg
->type
== CONST_IMM
)
2412 reg_set_min_max_inv(&other_branch
->regs
[insn
->src_reg
],
2413 ®s
[insn
->src_reg
], dst_reg
->imm
,
2416 reg_set_min_max(&other_branch
->regs
[insn
->dst_reg
],
2417 dst_reg
, insn
->imm
, opcode
);
2420 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
2421 if (BPF_SRC(insn
->code
) == BPF_K
&&
2422 insn
->imm
== 0 && (opcode
== BPF_JEQ
|| opcode
== BPF_JNE
) &&
2423 dst_reg
->type
== PTR_TO_MAP_VALUE_OR_NULL
) {
2424 /* Mark all identical map registers in each branch as either
2425 * safe or unknown depending R == 0 or R != 0 conditional.
2427 mark_map_regs(this_branch
, insn
->dst_reg
,
2428 opcode
== BPF_JEQ
? PTR_TO_MAP_VALUE
: UNKNOWN_VALUE
);
2429 mark_map_regs(other_branch
, insn
->dst_reg
,
2430 opcode
== BPF_JEQ
? UNKNOWN_VALUE
: PTR_TO_MAP_VALUE
);
2431 } else if (BPF_SRC(insn
->code
) == BPF_X
&& opcode
== BPF_JGT
&&
2432 dst_reg
->type
== PTR_TO_PACKET
&&
2433 regs
[insn
->src_reg
].type
== PTR_TO_PACKET_END
) {
2434 find_good_pkt_pointers(this_branch
, dst_reg
);
2435 } else if (BPF_SRC(insn
->code
) == BPF_X
&& opcode
== BPF_JGE
&&
2436 dst_reg
->type
== PTR_TO_PACKET_END
&&
2437 regs
[insn
->src_reg
].type
== PTR_TO_PACKET
) {
2438 find_good_pkt_pointers(other_branch
, ®s
[insn
->src_reg
]);
2439 } else if (is_pointer_value(env
, insn
->dst_reg
)) {
2440 verbose("R%d pointer comparison prohibited\n", insn
->dst_reg
);
2444 print_verifier_state(this_branch
);
2448 /* return the map pointer stored inside BPF_LD_IMM64 instruction */
2449 static struct bpf_map
*ld_imm64_to_map_ptr(struct bpf_insn
*insn
)
2451 u64 imm64
= ((u64
) (u32
) insn
[0].imm
) | ((u64
) (u32
) insn
[1].imm
) << 32;
2453 return (struct bpf_map
*) (unsigned long) imm64
;
2456 /* verify BPF_LD_IMM64 instruction */
2457 static int check_ld_imm(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
2459 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
2462 if (BPF_SIZE(insn
->code
) != BPF_DW
) {
2463 verbose("invalid BPF_LD_IMM insn\n");
2466 if (insn
->off
!= 0) {
2467 verbose("BPF_LD_IMM64 uses reserved fields\n");
2471 err
= check_reg_arg(regs
, insn
->dst_reg
, DST_OP
);
2475 if (insn
->src_reg
== 0) {
2476 u64 imm
= ((u64
)(insn
+ 1)->imm
<< 32) | (u32
)insn
->imm
;
2478 regs
[insn
->dst_reg
].type
= CONST_IMM
;
2479 regs
[insn
->dst_reg
].imm
= imm
;
2480 regs
[insn
->dst_reg
].id
= 0;
2484 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
2485 BUG_ON(insn
->src_reg
!= BPF_PSEUDO_MAP_FD
);
2487 regs
[insn
->dst_reg
].type
= CONST_PTR_TO_MAP
;
2488 regs
[insn
->dst_reg
].map_ptr
= ld_imm64_to_map_ptr(insn
);
2492 static bool may_access_skb(enum bpf_prog_type type
)
2495 case BPF_PROG_TYPE_SOCKET_FILTER
:
2496 case BPF_PROG_TYPE_SCHED_CLS
:
2497 case BPF_PROG_TYPE_SCHED_ACT
:
2504 /* verify safety of LD_ABS|LD_IND instructions:
2505 * - they can only appear in the programs where ctx == skb
2506 * - since they are wrappers of function calls, they scratch R1-R5 registers,
2507 * preserve R6-R9, and store return value into R0
2510 * ctx == skb == R6 == CTX
2513 * SRC == any register
2514 * IMM == 32-bit immediate
2517 * R0 - 8/16/32-bit skb data converted to cpu endianness
2519 static int check_ld_abs(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
2521 struct bpf_reg_state
*regs
= env
->cur_state
.regs
;
2522 u8 mode
= BPF_MODE(insn
->code
);
2525 if (!may_access_skb(env
->prog
->type
)) {
2526 verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
2530 if (insn
->dst_reg
!= BPF_REG_0
|| insn
->off
!= 0 ||
2531 BPF_SIZE(insn
->code
) == BPF_DW
||
2532 (mode
== BPF_ABS
&& insn
->src_reg
!= BPF_REG_0
)) {
2533 verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
2537 /* check whether implicit source operand (register R6) is readable */
2538 err
= check_reg_arg(regs
, BPF_REG_6
, SRC_OP
);
2542 if (regs
[BPF_REG_6
].type
!= PTR_TO_CTX
) {
2543 verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
2547 if (mode
== BPF_IND
) {
2548 /* check explicit source operand */
2549 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
2554 /* reset caller saved regs to unreadable */
2555 for (i
= 0; i
< CALLER_SAVED_REGS
; i
++)
2556 mark_reg_not_init(regs
, caller_saved
[i
]);
2558 /* mark destination R0 register as readable, since it contains
2559 * the value fetched from the packet
2561 regs
[BPF_REG_0
].type
= UNKNOWN_VALUE
;
2565 /* non-recursive DFS pseudo code
2566 * 1 procedure DFS-iterative(G,v):
2567 * 2 label v as discovered
2568 * 3 let S be a stack
2570 * 5 while S is not empty
2572 * 7 if t is what we're looking for:
2574 * 9 for all edges e in G.adjacentEdges(t) do
2575 * 10 if edge e is already labelled
2576 * 11 continue with the next edge
2577 * 12 w <- G.adjacentVertex(t,e)
2578 * 13 if vertex w is not discovered and not explored
2579 * 14 label e as tree-edge
2580 * 15 label w as discovered
2583 * 18 else if vertex w is discovered
2584 * 19 label e as back-edge
2586 * 21 // vertex w is explored
2587 * 22 label e as forward- or cross-edge
2588 * 23 label t as explored
2593 * 0x11 - discovered and fall-through edge labelled
2594 * 0x12 - discovered and fall-through and branch edges labelled
2605 #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
2607 static int *insn_stack
; /* stack of insns to process */
2608 static int cur_stack
; /* current stack index */
2609 static int *insn_state
;
2611 /* t, w, e - match pseudo-code above:
2612 * t - index of current instruction
2613 * w - next instruction
2616 static int push_insn(int t
, int w
, int e
, struct bpf_verifier_env
*env
)
2618 if (e
== FALLTHROUGH
&& insn_state
[t
] >= (DISCOVERED
| FALLTHROUGH
))
2621 if (e
== BRANCH
&& insn_state
[t
] >= (DISCOVERED
| BRANCH
))
2624 if (w
< 0 || w
>= env
->prog
->len
) {
2625 verbose("jump out of range from insn %d to %d\n", t
, w
);
2630 /* mark branch target for state pruning */
2631 env
->explored_states
[w
] = STATE_LIST_MARK
;
2633 if (insn_state
[w
] == 0) {
2635 insn_state
[t
] = DISCOVERED
| e
;
2636 insn_state
[w
] = DISCOVERED
;
2637 if (cur_stack
>= env
->prog
->len
)
2639 insn_stack
[cur_stack
++] = w
;
2641 } else if ((insn_state
[w
] & 0xF0) == DISCOVERED
) {
2642 verbose("back-edge from insn %d to %d\n", t
, w
);
2644 } else if (insn_state
[w
] == EXPLORED
) {
2645 /* forward- or cross-edge */
2646 insn_state
[t
] = DISCOVERED
| e
;
2648 verbose("insn state internal bug\n");
2654 /* non-recursive depth-first-search to detect loops in BPF program
2655 * loop == back-edge in directed graph
2657 static int check_cfg(struct bpf_verifier_env
*env
)
2659 struct bpf_insn
*insns
= env
->prog
->insnsi
;
2660 int insn_cnt
= env
->prog
->len
;
2664 insn_state
= kcalloc(insn_cnt
, sizeof(int), GFP_KERNEL
);
2668 insn_stack
= kcalloc(insn_cnt
, sizeof(int), GFP_KERNEL
);
2674 insn_state
[0] = DISCOVERED
; /* mark 1st insn as discovered */
2675 insn_stack
[0] = 0; /* 0 is the first instruction */
2681 t
= insn_stack
[cur_stack
- 1];
2683 if (BPF_CLASS(insns
[t
].code
) == BPF_JMP
) {
2684 u8 opcode
= BPF_OP(insns
[t
].code
);
2686 if (opcode
== BPF_EXIT
) {
2688 } else if (opcode
== BPF_CALL
) {
2689 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
2694 if (t
+ 1 < insn_cnt
)
2695 env
->explored_states
[t
+ 1] = STATE_LIST_MARK
;
2696 } else if (opcode
== BPF_JA
) {
2697 if (BPF_SRC(insns
[t
].code
) != BPF_K
) {
2701 /* unconditional jump with single edge */
2702 ret
= push_insn(t
, t
+ insns
[t
].off
+ 1,
2708 /* tell verifier to check for equivalent states
2709 * after every call and jump
2711 if (t
+ 1 < insn_cnt
)
2712 env
->explored_states
[t
+ 1] = STATE_LIST_MARK
;
2714 /* conditional jump with two edges */
2715 env
->explored_states
[t
] = STATE_LIST_MARK
;
2716 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
2722 ret
= push_insn(t
, t
+ insns
[t
].off
+ 1, BRANCH
, env
);
2729 /* all other non-branch instructions with single
2732 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
2740 insn_state
[t
] = EXPLORED
;
2741 if (cur_stack
-- <= 0) {
2742 verbose("pop stack internal bug\n");
2749 for (i
= 0; i
< insn_cnt
; i
++) {
2750 if (insn_state
[i
] != EXPLORED
) {
2751 verbose("unreachable insn %d\n", i
);
2756 ret
= 0; /* cfg looks good */
2764 /* the following conditions reduce the number of explored insns
2765 * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet
2767 static bool compare_ptrs_to_packet(struct bpf_verifier_env
*env
,
2768 struct bpf_reg_state
*old
,
2769 struct bpf_reg_state
*cur
)
2771 if (old
->id
!= cur
->id
)
2774 /* old ptr_to_packet is more conservative, since it allows smaller
2776 * old(off=0,r=10) is equal to cur(off=0,r=20), because
2777 * old(off=0,r=10) means that with range=10 the verifier proceeded
2778 * further and found no issues with the program. Now we're in the same
2779 * spot with cur(off=0,r=20), so we're safe too, since anything further
2780 * will only be looking at most 10 bytes after this pointer.
2782 if (old
->off
== cur
->off
&& old
->range
< cur
->range
)
2785 /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0)
2786 * since both cannot be used for packet access and safe(old)
2787 * pointer has smaller off that could be used for further
2788 * 'if (ptr > data_end)' check
2790 * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean
2791 * that we cannot access the packet.
2792 * The safe range is:
2793 * [ptr, ptr + range - off)
2794 * so whenever off >=range, it means no safe bytes from this pointer.
2795 * When comparing old->off <= cur->off, it means that older code
2796 * went with smaller offset and that offset was later
2797 * used to figure out the safe range after 'if (ptr > data_end)' check
2798 * Say, 'old' state was explored like:
2799 * ... R3(off=0, r=0)
2801 * ... now R4(off=20,r=0) <-- here
2802 * if (R4 > data_end)
2803 * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access.
2804 * ... the code further went all the way to bpf_exit.
2805 * Now the 'cur' state at the mark 'here' has R4(off=30,r=0).
2806 * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier
2807 * goes further, such cur_R4 will give larger safe packet range after
2808 * 'if (R4 > data_end)' and all further insn were already good with r=20,
2809 * so they will be good with r=30 and we can prune the search.
2811 if (!env
->strict_alignment
&& old
->off
<= cur
->off
&&
2812 old
->off
>= old
->range
&& cur
->off
>= cur
->range
)
2818 /* compare two verifier states
2820 * all states stored in state_list are known to be valid, since
2821 * verifier reached 'bpf_exit' instruction through them
2823 * this function is called when verifier exploring different branches of
2824 * execution popped from the state stack. If it sees an old state that has
2825 * more strict register state and more strict stack state then this execution
2826 * branch doesn't need to be explored further, since verifier already
2827 * concluded that more strict state leads to valid finish.
2829 * Therefore two states are equivalent if register state is more conservative
2830 * and explored stack state is more conservative than the current one.
2833 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
2834 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
2836 * In other words if current stack state (one being explored) has more
2837 * valid slots than old one that already passed validation, it means
2838 * the verifier can stop exploring and conclude that current state is valid too
2840 * Similarly with registers. If explored state has register type as invalid
2841 * whereas register type in current state is meaningful, it means that
2842 * the current state will reach 'bpf_exit' instruction safely
2844 static bool states_equal(struct bpf_verifier_env
*env
,
2845 struct bpf_verifier_state
*old
,
2846 struct bpf_verifier_state
*cur
)
2848 bool varlen_map_access
= env
->varlen_map_value_access
;
2849 struct bpf_reg_state
*rold
, *rcur
;
2852 for (i
= 0; i
< MAX_BPF_REG
; i
++) {
2853 rold
= &old
->regs
[i
];
2854 rcur
= &cur
->regs
[i
];
2856 if (memcmp(rold
, rcur
, sizeof(*rold
)) == 0)
2859 /* If the ranges were not the same, but everything else was and
2860 * we didn't do a variable access into a map then we are a-ok.
2862 if (!varlen_map_access
&&
2863 memcmp(rold
, rcur
, offsetofend(struct bpf_reg_state
, id
)) == 0)
2866 /* If we didn't map access then again we don't care about the
2867 * mismatched range values and it's ok if our old type was
2868 * UNKNOWN and we didn't go to a NOT_INIT'ed reg.
2870 if (rold
->type
== NOT_INIT
||
2871 (!varlen_map_access
&& rold
->type
== UNKNOWN_VALUE
&&
2872 rcur
->type
!= NOT_INIT
))
2875 /* Don't care about the reg->id in this case. */
2876 if (rold
->type
== PTR_TO_MAP_VALUE_OR_NULL
&&
2877 rcur
->type
== PTR_TO_MAP_VALUE_OR_NULL
&&
2878 rold
->map_ptr
== rcur
->map_ptr
)
2881 if (rold
->type
== PTR_TO_PACKET
&& rcur
->type
== PTR_TO_PACKET
&&
2882 compare_ptrs_to_packet(env
, rold
, rcur
))
2888 for (i
= 0; i
< MAX_BPF_STACK
; i
++) {
2889 if (old
->stack_slot_type
[i
] == STACK_INVALID
)
2891 if (old
->stack_slot_type
[i
] != cur
->stack_slot_type
[i
])
2892 /* Ex: old explored (safe) state has STACK_SPILL in
2893 * this stack slot, but current has has STACK_MISC ->
2894 * this verifier states are not equivalent,
2895 * return false to continue verification of this path
2898 if (i
% BPF_REG_SIZE
)
2900 if (old
->stack_slot_type
[i
] != STACK_SPILL
)
2902 if (memcmp(&old
->spilled_regs
[i
/ BPF_REG_SIZE
],
2903 &cur
->spilled_regs
[i
/ BPF_REG_SIZE
],
2904 sizeof(old
->spilled_regs
[0])))
2905 /* when explored and current stack slot types are
2906 * the same, check that stored pointers types
2907 * are the same as well.
2908 * Ex: explored safe path could have stored
2909 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8}
2910 * but current path has stored:
2911 * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16}
2912 * such verifier states are not equivalent.
2913 * return false to continue verification of this path
2922 static int is_state_visited(struct bpf_verifier_env
*env
, int insn_idx
)
2924 struct bpf_verifier_state_list
*new_sl
;
2925 struct bpf_verifier_state_list
*sl
;
2927 sl
= env
->explored_states
[insn_idx
];
2929 /* this 'insn_idx' instruction wasn't marked, so we will not
2930 * be doing state search here
2934 while (sl
!= STATE_LIST_MARK
) {
2935 if (states_equal(env
, &sl
->state
, &env
->cur_state
))
2936 /* reached equivalent register/stack state,
2943 /* there were no equivalent states, remember current one.
2944 * technically the current state is not proven to be safe yet,
2945 * but it will either reach bpf_exit (which means it's safe) or
2946 * it will be rejected. Since there are no loops, we won't be
2947 * seeing this 'insn_idx' instruction again on the way to bpf_exit
2949 new_sl
= kmalloc(sizeof(struct bpf_verifier_state_list
), GFP_USER
);
2953 /* add new state to the head of linked list */
2954 memcpy(&new_sl
->state
, &env
->cur_state
, sizeof(env
->cur_state
));
2955 new_sl
->next
= env
->explored_states
[insn_idx
];
2956 env
->explored_states
[insn_idx
] = new_sl
;
2960 static int ext_analyzer_insn_hook(struct bpf_verifier_env
*env
,
2961 int insn_idx
, int prev_insn_idx
)
2963 if (!env
->analyzer_ops
|| !env
->analyzer_ops
->insn_hook
)
2966 return env
->analyzer_ops
->insn_hook(env
, insn_idx
, prev_insn_idx
);
2969 static int do_check(struct bpf_verifier_env
*env
)
2971 struct bpf_verifier_state
*state
= &env
->cur_state
;
2972 struct bpf_insn
*insns
= env
->prog
->insnsi
;
2973 struct bpf_reg_state
*regs
= state
->regs
;
2974 int insn_cnt
= env
->prog
->len
;
2975 int insn_idx
, prev_insn_idx
= 0;
2976 int insn_processed
= 0;
2977 bool do_print_state
= false;
2979 init_reg_state(regs
);
2981 env
->varlen_map_value_access
= false;
2983 struct bpf_insn
*insn
;
2987 if (insn_idx
>= insn_cnt
) {
2988 verbose("invalid insn idx %d insn_cnt %d\n",
2989 insn_idx
, insn_cnt
);
2993 insn
= &insns
[insn_idx
];
2994 class = BPF_CLASS(insn
->code
);
2996 if (++insn_processed
> BPF_COMPLEXITY_LIMIT_INSNS
) {
2997 verbose("BPF program is too large. Processed %d insn\n",
3002 err
= is_state_visited(env
, insn_idx
);
3006 /* found equivalent state, can prune the search */
3009 verbose("\nfrom %d to %d: safe\n",
3010 prev_insn_idx
, insn_idx
);
3012 verbose("%d: safe\n", insn_idx
);
3014 goto process_bpf_exit
;
3020 if (log_level
> 1 || (log_level
&& do_print_state
)) {
3022 verbose("%d:", insn_idx
);
3024 verbose("\nfrom %d to %d:",
3025 prev_insn_idx
, insn_idx
);
3026 print_verifier_state(&env
->cur_state
);
3027 do_print_state
= false;
3031 verbose("%d: ", insn_idx
);
3032 print_bpf_insn(env
, insn
);
3035 err
= ext_analyzer_insn_hook(env
, insn_idx
, prev_insn_idx
);
3039 if (class == BPF_ALU
|| class == BPF_ALU64
) {
3040 err
= check_alu_op(env
, insn
);
3044 } else if (class == BPF_LDX
) {
3045 enum bpf_reg_type
*prev_src_type
, src_reg_type
;
3047 /* check for reserved fields is already done */
3049 /* check src operand */
3050 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
3054 err
= check_reg_arg(regs
, insn
->dst_reg
, DST_OP_NO_MARK
);
3058 src_reg_type
= regs
[insn
->src_reg
].type
;
3060 /* check that memory (src_reg + off) is readable,
3061 * the state of dst_reg will be updated by this func
3063 err
= check_mem_access(env
, insn_idx
, insn
->src_reg
, insn
->off
,
3064 BPF_SIZE(insn
->code
), BPF_READ
,
3069 prev_src_type
= &env
->insn_aux_data
[insn_idx
].ptr_type
;
3071 if (*prev_src_type
== NOT_INIT
) {
3073 * dst_reg = *(u32 *)(src_reg + off)
3074 * save type to validate intersecting paths
3076 *prev_src_type
= src_reg_type
;
3078 } else if (src_reg_type
!= *prev_src_type
&&
3079 (src_reg_type
== PTR_TO_CTX
||
3080 *prev_src_type
== PTR_TO_CTX
)) {
3081 /* ABuser program is trying to use the same insn
3082 * dst_reg = *(u32*) (src_reg + off)
3083 * with different pointer types:
3084 * src_reg == ctx in one branch and
3085 * src_reg == stack|map in some other branch.
3088 verbose("same insn cannot be used with different pointers\n");
3092 } else if (class == BPF_STX
) {
3093 enum bpf_reg_type
*prev_dst_type
, dst_reg_type
;
3095 if (BPF_MODE(insn
->code
) == BPF_XADD
) {
3096 err
= check_xadd(env
, insn_idx
, insn
);
3103 /* check src1 operand */
3104 err
= check_reg_arg(regs
, insn
->src_reg
, SRC_OP
);
3107 /* check src2 operand */
3108 err
= check_reg_arg(regs
, insn
->dst_reg
, SRC_OP
);
3112 dst_reg_type
= regs
[insn
->dst_reg
].type
;
3114 /* check that memory (dst_reg + off) is writeable */
3115 err
= check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
3116 BPF_SIZE(insn
->code
), BPF_WRITE
,
3121 prev_dst_type
= &env
->insn_aux_data
[insn_idx
].ptr_type
;
3123 if (*prev_dst_type
== NOT_INIT
) {
3124 *prev_dst_type
= dst_reg_type
;
3125 } else if (dst_reg_type
!= *prev_dst_type
&&
3126 (dst_reg_type
== PTR_TO_CTX
||
3127 *prev_dst_type
== PTR_TO_CTX
)) {
3128 verbose("same insn cannot be used with different pointers\n");
3132 } else if (class == BPF_ST
) {
3133 if (BPF_MODE(insn
->code
) != BPF_MEM
||
3134 insn
->src_reg
!= BPF_REG_0
) {
3135 verbose("BPF_ST uses reserved fields\n");
3138 /* check src operand */
3139 err
= check_reg_arg(regs
, insn
->dst_reg
, SRC_OP
);
3143 /* check that memory (dst_reg + off) is writeable */
3144 err
= check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
3145 BPF_SIZE(insn
->code
), BPF_WRITE
,
3150 } else if (class == BPF_JMP
) {
3151 u8 opcode
= BPF_OP(insn
->code
);
3153 if (opcode
== BPF_CALL
) {
3154 if (BPF_SRC(insn
->code
) != BPF_K
||
3156 insn
->src_reg
!= BPF_REG_0
||
3157 insn
->dst_reg
!= BPF_REG_0
) {
3158 verbose("BPF_CALL uses reserved fields\n");
3162 err
= check_call(env
, insn
->imm
, insn_idx
);
3166 } else if (opcode
== BPF_JA
) {
3167 if (BPF_SRC(insn
->code
) != BPF_K
||
3169 insn
->src_reg
!= BPF_REG_0
||
3170 insn
->dst_reg
!= BPF_REG_0
) {
3171 verbose("BPF_JA uses reserved fields\n");
3175 insn_idx
+= insn
->off
+ 1;
3178 } else if (opcode
== BPF_EXIT
) {
3179 if (BPF_SRC(insn
->code
) != BPF_K
||
3181 insn
->src_reg
!= BPF_REG_0
||
3182 insn
->dst_reg
!= BPF_REG_0
) {
3183 verbose("BPF_EXIT uses reserved fields\n");
3187 /* eBPF calling convetion is such that R0 is used
3188 * to return the value from eBPF program.
3189 * Make sure that it's readable at this time
3190 * of bpf_exit, which means that program wrote
3191 * something into it earlier
3193 err
= check_reg_arg(regs
, BPF_REG_0
, SRC_OP
);
3197 if (is_pointer_value(env
, BPF_REG_0
)) {
3198 verbose("R0 leaks addr as return value\n");
3203 insn_idx
= pop_stack(env
, &prev_insn_idx
);
3207 do_print_state
= true;
3211 err
= check_cond_jmp_op(env
, insn
, &insn_idx
);
3215 } else if (class == BPF_LD
) {
3216 u8 mode
= BPF_MODE(insn
->code
);
3218 if (mode
== BPF_ABS
|| mode
== BPF_IND
) {
3219 err
= check_ld_abs(env
, insn
);
3223 } else if (mode
== BPF_IMM
) {
3224 err
= check_ld_imm(env
, insn
);
3230 verbose("invalid BPF_LD mode\n");
3233 reset_reg_range_values(regs
, insn
->dst_reg
);
3235 verbose("unknown insn class %d\n", class);
3242 verbose("processed %d insns, stack depth %d\n",
3243 insn_processed
, env
->prog
->aux
->stack_depth
);
3247 static int check_map_prealloc(struct bpf_map
*map
)
3249 return (map
->map_type
!= BPF_MAP_TYPE_HASH
&&
3250 map
->map_type
!= BPF_MAP_TYPE_PERCPU_HASH
&&
3251 map
->map_type
!= BPF_MAP_TYPE_HASH_OF_MAPS
) ||
3252 !(map
->map_flags
& BPF_F_NO_PREALLOC
);
3255 static int check_map_prog_compatibility(struct bpf_map
*map
,
3256 struct bpf_prog
*prog
)
3259 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
3260 * preallocated hash maps, since doing memory allocation
3261 * in overflow_handler can crash depending on where nmi got
3264 if (prog
->type
== BPF_PROG_TYPE_PERF_EVENT
) {
3265 if (!check_map_prealloc(map
)) {
3266 verbose("perf_event programs can only use preallocated hash map\n");
3269 if (map
->inner_map_meta
&&
3270 !check_map_prealloc(map
->inner_map_meta
)) {
3271 verbose("perf_event programs can only use preallocated inner hash map\n");
3278 /* look for pseudo eBPF instructions that access map FDs and
3279 * replace them with actual map pointers
3281 static int replace_map_fd_with_map_ptr(struct bpf_verifier_env
*env
)
3283 struct bpf_insn
*insn
= env
->prog
->insnsi
;
3284 int insn_cnt
= env
->prog
->len
;
3287 err
= bpf_prog_calc_tag(env
->prog
);
3291 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
3292 if (BPF_CLASS(insn
->code
) == BPF_LDX
&&
3293 (BPF_MODE(insn
->code
) != BPF_MEM
|| insn
->imm
!= 0)) {
3294 verbose("BPF_LDX uses reserved fields\n");
3298 if (BPF_CLASS(insn
->code
) == BPF_STX
&&
3299 ((BPF_MODE(insn
->code
) != BPF_MEM
&&
3300 BPF_MODE(insn
->code
) != BPF_XADD
) || insn
->imm
!= 0)) {
3301 verbose("BPF_STX uses reserved fields\n");
3305 if (insn
[0].code
== (BPF_LD
| BPF_IMM
| BPF_DW
)) {
3306 struct bpf_map
*map
;
3309 if (i
== insn_cnt
- 1 || insn
[1].code
!= 0 ||
3310 insn
[1].dst_reg
!= 0 || insn
[1].src_reg
!= 0 ||
3312 verbose("invalid bpf_ld_imm64 insn\n");
3316 if (insn
->src_reg
== 0)
3317 /* valid generic load 64-bit imm */
3320 if (insn
->src_reg
!= BPF_PSEUDO_MAP_FD
) {
3321 verbose("unrecognized bpf_ld_imm64 insn\n");
3325 f
= fdget(insn
->imm
);
3326 map
= __bpf_map_get(f
);
3328 verbose("fd %d is not pointing to valid bpf_map\n",
3330 return PTR_ERR(map
);
3333 err
= check_map_prog_compatibility(map
, env
->prog
);
3339 /* store map pointer inside BPF_LD_IMM64 instruction */
3340 insn
[0].imm
= (u32
) (unsigned long) map
;
3341 insn
[1].imm
= ((u64
) (unsigned long) map
) >> 32;
3343 /* check whether we recorded this map already */
3344 for (j
= 0; j
< env
->used_map_cnt
; j
++)
3345 if (env
->used_maps
[j
] == map
) {
3350 if (env
->used_map_cnt
>= MAX_USED_MAPS
) {
3355 /* hold the map. If the program is rejected by verifier,
3356 * the map will be released by release_maps() or it
3357 * will be used by the valid program until it's unloaded
3358 * and all maps are released in free_bpf_prog_info()
3360 map
= bpf_map_inc(map
, false);
3363 return PTR_ERR(map
);
3365 env
->used_maps
[env
->used_map_cnt
++] = map
;
3374 /* now all pseudo BPF_LD_IMM64 instructions load valid
3375 * 'struct bpf_map *' into a register instead of user map_fd.
3376 * These pointers will be used later by verifier to validate map access.
3381 /* drop refcnt of maps used by the rejected program */
3382 static void release_maps(struct bpf_verifier_env
*env
)
3386 for (i
= 0; i
< env
->used_map_cnt
; i
++)
3387 bpf_map_put(env
->used_maps
[i
]);
3390 /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
3391 static void convert_pseudo_ld_imm64(struct bpf_verifier_env
*env
)
3393 struct bpf_insn
*insn
= env
->prog
->insnsi
;
3394 int insn_cnt
= env
->prog
->len
;
3397 for (i
= 0; i
< insn_cnt
; i
++, insn
++)
3398 if (insn
->code
== (BPF_LD
| BPF_IMM
| BPF_DW
))
3402 /* single env->prog->insni[off] instruction was replaced with the range
3403 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
3404 * [0, off) and [off, end) to new locations, so the patched range stays zero
3406 static int adjust_insn_aux_data(struct bpf_verifier_env
*env
, u32 prog_len
,
3409 struct bpf_insn_aux_data
*new_data
, *old_data
= env
->insn_aux_data
;
3413 new_data
= vzalloc(sizeof(struct bpf_insn_aux_data
) * prog_len
);
3416 memcpy(new_data
, old_data
, sizeof(struct bpf_insn_aux_data
) * off
);
3417 memcpy(new_data
+ off
+ cnt
- 1, old_data
+ off
,
3418 sizeof(struct bpf_insn_aux_data
) * (prog_len
- off
- cnt
+ 1));
3419 env
->insn_aux_data
= new_data
;
3424 static struct bpf_prog
*bpf_patch_insn_data(struct bpf_verifier_env
*env
, u32 off
,
3425 const struct bpf_insn
*patch
, u32 len
)
3427 struct bpf_prog
*new_prog
;
3429 new_prog
= bpf_patch_insn_single(env
->prog
, off
, patch
, len
);
3432 if (adjust_insn_aux_data(env
, new_prog
->len
, off
, len
))
3437 /* convert load instructions that access fields of 'struct __sk_buff'
3438 * into sequence of instructions that access fields of 'struct sk_buff'
3440 static int convert_ctx_accesses(struct bpf_verifier_env
*env
)
3442 const struct bpf_verifier_ops
*ops
= env
->prog
->aux
->ops
;
3443 int i
, cnt
, size
, ctx_field_size
, delta
= 0;
3444 const int insn_cnt
= env
->prog
->len
;
3445 struct bpf_insn insn_buf
[16], *insn
;
3446 struct bpf_prog
*new_prog
;
3447 enum bpf_access_type type
;
3448 bool is_narrower_load
;
3451 if (ops
->gen_prologue
) {
3452 cnt
= ops
->gen_prologue(insn_buf
, env
->seen_direct_write
,
3454 if (cnt
>= ARRAY_SIZE(insn_buf
)) {
3455 verbose("bpf verifier is misconfigured\n");
3458 new_prog
= bpf_patch_insn_data(env
, 0, insn_buf
, cnt
);
3462 env
->prog
= new_prog
;
3467 if (!ops
->convert_ctx_access
)
3470 insn
= env
->prog
->insnsi
+ delta
;
3472 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
3473 if (insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_B
) ||
3474 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_H
) ||
3475 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_W
) ||
3476 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_DW
))
3478 else if (insn
->code
== (BPF_STX
| BPF_MEM
| BPF_B
) ||
3479 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_H
) ||
3480 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_W
) ||
3481 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_DW
))
3486 if (env
->insn_aux_data
[i
+ delta
].ptr_type
!= PTR_TO_CTX
)
3489 ctx_field_size
= env
->insn_aux_data
[i
+ delta
].ctx_field_size
;
3490 size
= BPF_LDST_BYTES(insn
);
3492 /* If the read access is a narrower load of the field,
3493 * convert to a 4/8-byte load, to minimum program type specific
3494 * convert_ctx_access changes. If conversion is successful,
3495 * we will apply proper mask to the result.
3497 is_narrower_load
= size
< ctx_field_size
;
3498 if (is_narrower_load
) {
3499 u32 off
= insn
->off
;
3502 if (type
== BPF_WRITE
) {
3503 verbose("bpf verifier narrow ctx access misconfigured\n");
3508 if (ctx_field_size
== 4)
3510 else if (ctx_field_size
== 8)
3513 insn
->off
= off
& ~(ctx_field_size
- 1);
3514 insn
->code
= BPF_LDX
| BPF_MEM
| size_code
;
3518 cnt
= ops
->convert_ctx_access(type
, insn
, insn_buf
, env
->prog
,
3520 if (cnt
== 0 || cnt
>= ARRAY_SIZE(insn_buf
) ||
3521 (ctx_field_size
&& !target_size
)) {
3522 verbose("bpf verifier is misconfigured\n");
3526 if (is_narrower_load
&& size
< target_size
) {
3527 if (ctx_field_size
<= 4)
3528 insn_buf
[cnt
++] = BPF_ALU32_IMM(BPF_AND
, insn
->dst_reg
,
3529 (1 << size
* 8) - 1);
3531 insn_buf
[cnt
++] = BPF_ALU64_IMM(BPF_AND
, insn
->dst_reg
,
3532 (1 << size
* 8) - 1);
3535 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, insn_buf
, cnt
);
3541 /* keep walking new program and skip insns we just inserted */
3542 env
->prog
= new_prog
;
3543 insn
= new_prog
->insnsi
+ i
+ delta
;
3549 /* fixup insn->imm field of bpf_call instructions
3550 * and inline eligible helpers as explicit sequence of BPF instructions
3552 * this function is called after eBPF program passed verification
3554 static int fixup_bpf_calls(struct bpf_verifier_env
*env
)
3556 struct bpf_prog
*prog
= env
->prog
;
3557 struct bpf_insn
*insn
= prog
->insnsi
;
3558 const struct bpf_func_proto
*fn
;
3559 const int insn_cnt
= prog
->len
;
3560 struct bpf_insn insn_buf
[16];
3561 struct bpf_prog
*new_prog
;
3562 struct bpf_map
*map_ptr
;
3563 int i
, cnt
, delta
= 0;
3565 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
3566 if (insn
->code
!= (BPF_JMP
| BPF_CALL
))
3569 if (insn
->imm
== BPF_FUNC_get_route_realm
)
3570 prog
->dst_needed
= 1;
3571 if (insn
->imm
== BPF_FUNC_get_prandom_u32
)
3572 bpf_user_rnd_init_once();
3573 if (insn
->imm
== BPF_FUNC_tail_call
) {
3574 /* If we tail call into other programs, we
3575 * cannot make any assumptions since they can
3576 * be replaced dynamically during runtime in
3577 * the program array.
3579 prog
->cb_access
= 1;
3580 env
->prog
->aux
->stack_depth
= MAX_BPF_STACK
;
3582 /* mark bpf_tail_call as different opcode to avoid
3583 * conditional branch in the interpeter for every normal
3584 * call and to prevent accidental JITing by JIT compiler
3585 * that doesn't support bpf_tail_call yet
3588 insn
->code
= BPF_JMP
| BPF_TAIL_CALL
;
3592 if (ebpf_jit_enabled() && insn
->imm
== BPF_FUNC_map_lookup_elem
) {
3593 map_ptr
= env
->insn_aux_data
[i
+ delta
].map_ptr
;
3594 if (map_ptr
== BPF_MAP_PTR_POISON
||
3595 !map_ptr
->ops
->map_gen_lookup
)
3596 goto patch_call_imm
;
3598 cnt
= map_ptr
->ops
->map_gen_lookup(map_ptr
, insn_buf
);
3599 if (cnt
== 0 || cnt
>= ARRAY_SIZE(insn_buf
)) {
3600 verbose("bpf verifier is misconfigured\n");
3604 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, insn_buf
,
3611 /* keep walking new program and skip insns we just inserted */
3612 env
->prog
= prog
= new_prog
;
3613 insn
= new_prog
->insnsi
+ i
+ delta
;
3618 fn
= prog
->aux
->ops
->get_func_proto(insn
->imm
);
3619 /* all functions that have prototype and verifier allowed
3620 * programs to call them, must be real in-kernel functions
3623 verbose("kernel subsystem misconfigured func %s#%d\n",
3624 func_id_name(insn
->imm
), insn
->imm
);
3627 insn
->imm
= fn
->func
- __bpf_call_base
;
3633 static void free_states(struct bpf_verifier_env
*env
)
3635 struct bpf_verifier_state_list
*sl
, *sln
;
3638 if (!env
->explored_states
)
3641 for (i
= 0; i
< env
->prog
->len
; i
++) {
3642 sl
= env
->explored_states
[i
];
3645 while (sl
!= STATE_LIST_MARK
) {
3652 kfree(env
->explored_states
);
3655 int bpf_check(struct bpf_prog
**prog
, union bpf_attr
*attr
)
3657 char __user
*log_ubuf
= NULL
;
3658 struct bpf_verifier_env
*env
;
3661 /* 'struct bpf_verifier_env' can be global, but since it's not small,
3662 * allocate/free it every time bpf_check() is called
3664 env
= kzalloc(sizeof(struct bpf_verifier_env
), GFP_KERNEL
);
3668 env
->insn_aux_data
= vzalloc(sizeof(struct bpf_insn_aux_data
) *
3671 if (!env
->insn_aux_data
)
3675 /* grab the mutex to protect few globals used by verifier */
3676 mutex_lock(&bpf_verifier_lock
);
3678 if (attr
->log_level
|| attr
->log_buf
|| attr
->log_size
) {
3679 /* user requested verbose verifier output
3680 * and supplied buffer to store the verification trace
3682 log_level
= attr
->log_level
;
3683 log_ubuf
= (char __user
*) (unsigned long) attr
->log_buf
;
3684 log_size
= attr
->log_size
;
3688 /* log_* values have to be sane */
3689 if (log_size
< 128 || log_size
> UINT_MAX
>> 8 ||
3690 log_level
== 0 || log_ubuf
== NULL
)
3694 log_buf
= vmalloc(log_size
);
3701 env
->strict_alignment
= !!(attr
->prog_flags
& BPF_F_STRICT_ALIGNMENT
);
3702 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
))
3703 env
->strict_alignment
= true;
3705 ret
= replace_map_fd_with_map_ptr(env
);
3707 goto skip_full_check
;
3709 env
->explored_states
= kcalloc(env
->prog
->len
,
3710 sizeof(struct bpf_verifier_state_list
*),
3713 if (!env
->explored_states
)
3714 goto skip_full_check
;
3716 ret
= check_cfg(env
);
3718 goto skip_full_check
;
3720 env
->allow_ptr_leaks
= capable(CAP_SYS_ADMIN
);
3722 ret
= do_check(env
);
3725 while (pop_stack(env
, NULL
) >= 0);
3729 /* program is valid, convert *(u32*)(ctx + off) accesses */
3730 ret
= convert_ctx_accesses(env
);
3733 ret
= fixup_bpf_calls(env
);
3735 if (log_level
&& log_len
>= log_size
- 1) {
3736 BUG_ON(log_len
>= log_size
);
3737 /* verifier log exceeded user supplied buffer */
3739 /* fall through to return what was recorded */
3742 /* copy verifier log back to user space including trailing zero */
3743 if (log_level
&& copy_to_user(log_ubuf
, log_buf
, log_len
+ 1) != 0) {
3748 if (ret
== 0 && env
->used_map_cnt
) {
3749 /* if program passed verifier, update used_maps in bpf_prog_info */
3750 env
->prog
->aux
->used_maps
= kmalloc_array(env
->used_map_cnt
,
3751 sizeof(env
->used_maps
[0]),
3754 if (!env
->prog
->aux
->used_maps
) {
3759 memcpy(env
->prog
->aux
->used_maps
, env
->used_maps
,
3760 sizeof(env
->used_maps
[0]) * env
->used_map_cnt
);
3761 env
->prog
->aux
->used_map_cnt
= env
->used_map_cnt
;
3763 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
3764 * bpf_ld_imm64 instructions
3766 convert_pseudo_ld_imm64(env
);
3772 if (!env
->prog
->aux
->used_maps
)
3773 /* if we didn't copy map pointers into bpf_prog_info, release
3774 * them now. Otherwise free_bpf_prog_info() will release them.
3779 mutex_unlock(&bpf_verifier_lock
);
3780 vfree(env
->insn_aux_data
);
3786 int bpf_analyzer(struct bpf_prog
*prog
, const struct bpf_ext_analyzer_ops
*ops
,
3789 struct bpf_verifier_env
*env
;
3792 env
= kzalloc(sizeof(struct bpf_verifier_env
), GFP_KERNEL
);
3796 env
->insn_aux_data
= vzalloc(sizeof(struct bpf_insn_aux_data
) *
3799 if (!env
->insn_aux_data
)
3802 env
->analyzer_ops
= ops
;
3803 env
->analyzer_priv
= priv
;
3805 /* grab the mutex to protect few globals used by verifier */
3806 mutex_lock(&bpf_verifier_lock
);
3810 env
->strict_alignment
= false;
3811 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
))
3812 env
->strict_alignment
= true;
3814 env
->explored_states
= kcalloc(env
->prog
->len
,
3815 sizeof(struct bpf_verifier_state_list
*),
3818 if (!env
->explored_states
)
3819 goto skip_full_check
;
3821 ret
= check_cfg(env
);
3823 goto skip_full_check
;
3825 env
->allow_ptr_leaks
= capable(CAP_SYS_ADMIN
);
3827 ret
= do_check(env
);
3830 while (pop_stack(env
, NULL
) >= 0);
3833 mutex_unlock(&bpf_verifier_lock
);
3834 vfree(env
->insn_aux_data
);
3839 EXPORT_SYMBOL_GPL(bpf_analyzer
);