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>
23 #include <linux/bsearch.h>
24 #include <linux/sort.h>
28 static const struct bpf_verifier_ops
* const bpf_verifier_ops
[] = {
29 #define BPF_PROG_TYPE(_id, _name) \
30 [_id] = & _name ## _verifier_ops,
31 #define BPF_MAP_TYPE(_id, _ops)
32 #include <linux/bpf_types.h>
37 /* bpf_check() is a static code analyzer that walks eBPF program
38 * instruction by instruction and updates register/stack state.
39 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
41 * The first pass is depth-first-search to check that the program is a DAG.
42 * It rejects the following programs:
43 * - larger than BPF_MAXINSNS insns
44 * - if loop is present (detected via back-edge)
45 * - unreachable insns exist (shouldn't be a forest. program = one function)
46 * - out of bounds or malformed jumps
47 * The second pass is all possible path descent from the 1st insn.
48 * Since it's analyzing all pathes through the program, the length of the
49 * analysis is limited to 64k insn, which may be hit even if total number of
50 * insn is less then 4K, but there are too many branches that change stack/regs.
51 * Number of 'branches to be analyzed' is limited to 1k
53 * On entry to each instruction, each register has a type, and the instruction
54 * changes the types of the registers depending on instruction semantics.
55 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
58 * All registers are 64-bit.
59 * R0 - return register
60 * R1-R5 argument passing registers
61 * R6-R9 callee saved registers
62 * R10 - frame pointer read-only
64 * At the start of BPF program the register R1 contains a pointer to bpf_context
65 * and has type PTR_TO_CTX.
67 * Verifier tracks arithmetic operations on pointers in case:
68 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
69 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
70 * 1st insn copies R10 (which has FRAME_PTR) type into R1
71 * and 2nd arithmetic instruction is pattern matched to recognize
72 * that it wants to construct a pointer to some element within stack.
73 * So after 2nd insn, the register R1 has type PTR_TO_STACK
74 * (and -20 constant is saved for further stack bounds checking).
75 * Meaning that this reg is a pointer to stack plus known immediate constant.
77 * Most of the time the registers have SCALAR_VALUE type, which
78 * means the register has some value, but it's not a valid pointer.
79 * (like pointer plus pointer becomes SCALAR_VALUE type)
81 * When verifier sees load or store instructions the type of base register
82 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK. These are three pointer
83 * types recognized by check_mem_access() function.
85 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
86 * and the range of [ptr, ptr + map's value_size) is accessible.
88 * registers used to pass values to function calls are checked against
89 * function argument constraints.
91 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
92 * It means that the register type passed to this function must be
93 * PTR_TO_STACK and it will be used inside the function as
94 * 'pointer to map element key'
96 * For example the argument constraints for bpf_map_lookup_elem():
97 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
98 * .arg1_type = ARG_CONST_MAP_PTR,
99 * .arg2_type = ARG_PTR_TO_MAP_KEY,
101 * ret_type says that this function returns 'pointer to map elem value or null'
102 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
103 * 2nd argument should be a pointer to stack, which will be used inside
104 * the helper function as a pointer to map element key.
106 * On the kernel side the helper function looks like:
107 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
109 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
110 * void *key = (void *) (unsigned long) r2;
113 * here kernel can access 'key' and 'map' pointers safely, knowing that
114 * [key, key + map->key_size) bytes are valid and were initialized on
115 * the stack of eBPF program.
118 * Corresponding eBPF program may look like:
119 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
120 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
121 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
122 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
123 * here verifier looks at prototype of map_lookup_elem() and sees:
124 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
125 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
127 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
128 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
129 * and were initialized prior to this call.
130 * If it's ok, then verifier allows this BPF_CALL insn and looks at
131 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
132 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
133 * returns ether pointer to map value or NULL.
135 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
136 * insn, the register holding that pointer in the true branch changes state to
137 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
138 * branch. See check_cond_jmp_op().
140 * After the call R0 is set to return type of the function and registers R1-R5
141 * are set to NOT_INIT to indicate that they are no longer readable.
144 /* verifier_state + insn_idx are pushed to stack when branch is encountered */
145 struct bpf_verifier_stack_elem
{
146 /* verifer state is 'st'
147 * before processing instruction 'insn_idx'
148 * and after processing instruction 'prev_insn_idx'
150 struct bpf_verifier_state st
;
153 struct bpf_verifier_stack_elem
*next
;
156 #define BPF_COMPLEXITY_LIMIT_INSNS 131072
157 #define BPF_COMPLEXITY_LIMIT_STACK 1024
159 #define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA)
161 struct bpf_call_arg_meta
{
162 struct bpf_map
*map_ptr
;
169 static DEFINE_MUTEX(bpf_verifier_lock
);
171 /* log_level controls verbosity level of eBPF verifier.
172 * verbose() is used to dump the verification trace to the log, so the user
173 * can figure out what's wrong with the program
175 static __printf(2, 3) void verbose(struct bpf_verifier_env
*env
,
176 const char *fmt
, ...)
178 struct bpf_verifer_log
*log
= &env
->log
;
182 if (!log
->level
|| !log
->ubuf
|| bpf_verifier_log_full(log
))
186 n
= vscnprintf(log
->kbuf
, BPF_VERIFIER_TMP_LOG_SIZE
, fmt
, args
);
189 WARN_ONCE(n
>= BPF_VERIFIER_TMP_LOG_SIZE
- 1,
190 "verifier log line truncated - local buffer too short\n");
192 n
= min(log
->len_total
- log
->len_used
- 1, n
);
195 if (!copy_to_user(log
->ubuf
+ log
->len_used
, log
->kbuf
, n
+ 1))
201 static bool type_is_pkt_pointer(enum bpf_reg_type type
)
203 return type
== PTR_TO_PACKET
||
204 type
== PTR_TO_PACKET_META
;
207 /* string representation of 'enum bpf_reg_type' */
208 static const char * const reg_type_str
[] = {
210 [SCALAR_VALUE
] = "inv",
211 [PTR_TO_CTX
] = "ctx",
212 [CONST_PTR_TO_MAP
] = "map_ptr",
213 [PTR_TO_MAP_VALUE
] = "map_value",
214 [PTR_TO_MAP_VALUE_OR_NULL
] = "map_value_or_null",
215 [PTR_TO_STACK
] = "fp",
216 [PTR_TO_PACKET
] = "pkt",
217 [PTR_TO_PACKET_META
] = "pkt_meta",
218 [PTR_TO_PACKET_END
] = "pkt_end",
221 static void print_liveness(struct bpf_verifier_env
*env
,
222 enum bpf_reg_liveness live
)
224 if (live
& (REG_LIVE_READ
| REG_LIVE_WRITTEN
))
226 if (live
& REG_LIVE_READ
)
228 if (live
& REG_LIVE_WRITTEN
)
232 static struct bpf_func_state
*func(struct bpf_verifier_env
*env
,
233 const struct bpf_reg_state
*reg
)
235 struct bpf_verifier_state
*cur
= env
->cur_state
;
237 return cur
->frame
[reg
->frameno
];
240 static void print_verifier_state(struct bpf_verifier_env
*env
,
241 const struct bpf_func_state
*state
)
243 const struct bpf_reg_state
*reg
;
248 verbose(env
, " frame%d:", state
->frameno
);
249 for (i
= 0; i
< MAX_BPF_REG
; i
++) {
250 reg
= &state
->regs
[i
];
254 verbose(env
, " R%d", i
);
255 print_liveness(env
, reg
->live
);
256 verbose(env
, "=%s", reg_type_str
[t
]);
257 if ((t
== SCALAR_VALUE
|| t
== PTR_TO_STACK
) &&
258 tnum_is_const(reg
->var_off
)) {
259 /* reg->off should be 0 for SCALAR_VALUE */
260 verbose(env
, "%lld", reg
->var_off
.value
+ reg
->off
);
261 if (t
== PTR_TO_STACK
)
262 verbose(env
, ",call_%d", func(env
, reg
)->callsite
);
264 verbose(env
, "(id=%d", reg
->id
);
265 if (t
!= SCALAR_VALUE
)
266 verbose(env
, ",off=%d", reg
->off
);
267 if (type_is_pkt_pointer(t
))
268 verbose(env
, ",r=%d", reg
->range
);
269 else if (t
== CONST_PTR_TO_MAP
||
270 t
== PTR_TO_MAP_VALUE
||
271 t
== PTR_TO_MAP_VALUE_OR_NULL
)
272 verbose(env
, ",ks=%d,vs=%d",
273 reg
->map_ptr
->key_size
,
274 reg
->map_ptr
->value_size
);
275 if (tnum_is_const(reg
->var_off
)) {
276 /* Typically an immediate SCALAR_VALUE, but
277 * could be a pointer whose offset is too big
280 verbose(env
, ",imm=%llx", reg
->var_off
.value
);
282 if (reg
->smin_value
!= reg
->umin_value
&&
283 reg
->smin_value
!= S64_MIN
)
284 verbose(env
, ",smin_value=%lld",
285 (long long)reg
->smin_value
);
286 if (reg
->smax_value
!= reg
->umax_value
&&
287 reg
->smax_value
!= S64_MAX
)
288 verbose(env
, ",smax_value=%lld",
289 (long long)reg
->smax_value
);
290 if (reg
->umin_value
!= 0)
291 verbose(env
, ",umin_value=%llu",
292 (unsigned long long)reg
->umin_value
);
293 if (reg
->umax_value
!= U64_MAX
)
294 verbose(env
, ",umax_value=%llu",
295 (unsigned long long)reg
->umax_value
);
296 if (!tnum_is_unknown(reg
->var_off
)) {
299 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
300 verbose(env
, ",var_off=%s", tn_buf
);
306 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
307 if (state
->stack
[i
].slot_type
[0] == STACK_SPILL
) {
308 verbose(env
, " fp%d",
309 (-i
- 1) * BPF_REG_SIZE
);
310 print_liveness(env
, state
->stack
[i
].spilled_ptr
.live
);
312 reg_type_str
[state
->stack
[i
].spilled_ptr
.type
]);
314 if (state
->stack
[i
].slot_type
[0] == STACK_ZERO
)
315 verbose(env
, " fp%d=0", (-i
- 1) * BPF_REG_SIZE
);
320 static int copy_stack_state(struct bpf_func_state
*dst
,
321 const struct bpf_func_state
*src
)
325 if (WARN_ON_ONCE(dst
->allocated_stack
< src
->allocated_stack
)) {
326 /* internal bug, make state invalid to reject the program */
327 memset(dst
, 0, sizeof(*dst
));
330 memcpy(dst
->stack
, src
->stack
,
331 sizeof(*src
->stack
) * (src
->allocated_stack
/ BPF_REG_SIZE
));
335 /* do_check() starts with zero-sized stack in struct bpf_verifier_state to
336 * make it consume minimal amount of memory. check_stack_write() access from
337 * the program calls into realloc_func_state() to grow the stack size.
338 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
339 * which this function copies over. It points to previous bpf_verifier_state
340 * which is never reallocated
342 static int realloc_func_state(struct bpf_func_state
*state
, int size
,
345 u32 old_size
= state
->allocated_stack
;
346 struct bpf_stack_state
*new_stack
;
347 int slot
= size
/ BPF_REG_SIZE
;
349 if (size
<= old_size
|| !size
) {
352 state
->allocated_stack
= slot
* BPF_REG_SIZE
;
353 if (!size
&& old_size
) {
359 new_stack
= kmalloc_array(slot
, sizeof(struct bpf_stack_state
),
365 memcpy(new_stack
, state
->stack
,
366 sizeof(*new_stack
) * (old_size
/ BPF_REG_SIZE
));
367 memset(new_stack
+ old_size
/ BPF_REG_SIZE
, 0,
368 sizeof(*new_stack
) * (size
- old_size
) / BPF_REG_SIZE
);
370 state
->allocated_stack
= slot
* BPF_REG_SIZE
;
372 state
->stack
= new_stack
;
376 static void free_func_state(struct bpf_func_state
*state
)
382 static void free_verifier_state(struct bpf_verifier_state
*state
,
387 for (i
= 0; i
<= state
->curframe
; i
++) {
388 free_func_state(state
->frame
[i
]);
389 state
->frame
[i
] = NULL
;
395 /* copy verifier state from src to dst growing dst stack space
396 * when necessary to accommodate larger src stack
398 static int copy_func_state(struct bpf_func_state
*dst
,
399 const struct bpf_func_state
*src
)
403 err
= realloc_func_state(dst
, src
->allocated_stack
, false);
406 memcpy(dst
, src
, offsetof(struct bpf_func_state
, allocated_stack
));
407 return copy_stack_state(dst
, src
);
410 static int copy_verifier_state(struct bpf_verifier_state
*dst_state
,
411 const struct bpf_verifier_state
*src
)
413 struct bpf_func_state
*dst
;
416 /* if dst has more stack frames then src frame, free them */
417 for (i
= src
->curframe
+ 1; i
<= dst_state
->curframe
; i
++) {
418 free_func_state(dst_state
->frame
[i
]);
419 dst_state
->frame
[i
] = NULL
;
421 dst_state
->curframe
= src
->curframe
;
422 dst_state
->parent
= src
->parent
;
423 for (i
= 0; i
<= src
->curframe
; i
++) {
424 dst
= dst_state
->frame
[i
];
426 dst
= kzalloc(sizeof(*dst
), GFP_KERNEL
);
429 dst_state
->frame
[i
] = dst
;
431 err
= copy_func_state(dst
, src
->frame
[i
]);
438 static int pop_stack(struct bpf_verifier_env
*env
, int *prev_insn_idx
,
441 struct bpf_verifier_state
*cur
= env
->cur_state
;
442 struct bpf_verifier_stack_elem
*elem
, *head
= env
->head
;
445 if (env
->head
== NULL
)
449 err
= copy_verifier_state(cur
, &head
->st
);
454 *insn_idx
= head
->insn_idx
;
456 *prev_insn_idx
= head
->prev_insn_idx
;
458 free_verifier_state(&head
->st
, false);
465 static struct bpf_verifier_state
*push_stack(struct bpf_verifier_env
*env
,
466 int insn_idx
, int prev_insn_idx
)
468 struct bpf_verifier_state
*cur
= env
->cur_state
;
469 struct bpf_verifier_stack_elem
*elem
;
472 elem
= kzalloc(sizeof(struct bpf_verifier_stack_elem
), GFP_KERNEL
);
476 elem
->insn_idx
= insn_idx
;
477 elem
->prev_insn_idx
= prev_insn_idx
;
478 elem
->next
= env
->head
;
481 err
= copy_verifier_state(&elem
->st
, cur
);
484 if (env
->stack_size
> BPF_COMPLEXITY_LIMIT_STACK
) {
485 verbose(env
, "BPF program is too complex\n");
490 /* pop all elements and return */
491 while (!pop_stack(env
, NULL
, NULL
));
495 #define CALLER_SAVED_REGS 6
496 static const int caller_saved
[CALLER_SAVED_REGS
] = {
497 BPF_REG_0
, BPF_REG_1
, BPF_REG_2
, BPF_REG_3
, BPF_REG_4
, BPF_REG_5
499 #define CALLEE_SAVED_REGS 5
500 static const int callee_saved
[CALLEE_SAVED_REGS
] = {
501 BPF_REG_6
, BPF_REG_7
, BPF_REG_8
, BPF_REG_9
504 static void __mark_reg_not_init(struct bpf_reg_state
*reg
);
506 /* Mark the unknown part of a register (variable offset or scalar value) as
507 * known to have the value @imm.
509 static void __mark_reg_known(struct bpf_reg_state
*reg
, u64 imm
)
512 reg
->var_off
= tnum_const(imm
);
513 reg
->smin_value
= (s64
)imm
;
514 reg
->smax_value
= (s64
)imm
;
515 reg
->umin_value
= imm
;
516 reg
->umax_value
= imm
;
519 /* Mark the 'variable offset' part of a register as zero. This should be
520 * used only on registers holding a pointer type.
522 static void __mark_reg_known_zero(struct bpf_reg_state
*reg
)
524 __mark_reg_known(reg
, 0);
527 static void __mark_reg_const_zero(struct bpf_reg_state
*reg
)
529 __mark_reg_known(reg
, 0);
531 reg
->type
= SCALAR_VALUE
;
534 static void mark_reg_known_zero(struct bpf_verifier_env
*env
,
535 struct bpf_reg_state
*regs
, u32 regno
)
537 if (WARN_ON(regno
>= MAX_BPF_REG
)) {
538 verbose(env
, "mark_reg_known_zero(regs, %u)\n", regno
);
539 /* Something bad happened, let's kill all regs */
540 for (regno
= 0; regno
< MAX_BPF_REG
; regno
++)
541 __mark_reg_not_init(regs
+ regno
);
544 __mark_reg_known_zero(regs
+ regno
);
547 static bool reg_is_pkt_pointer(const struct bpf_reg_state
*reg
)
549 return type_is_pkt_pointer(reg
->type
);
552 static bool reg_is_pkt_pointer_any(const struct bpf_reg_state
*reg
)
554 return reg_is_pkt_pointer(reg
) ||
555 reg
->type
== PTR_TO_PACKET_END
;
558 /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
559 static bool reg_is_init_pkt_pointer(const struct bpf_reg_state
*reg
,
560 enum bpf_reg_type which
)
562 /* The register can already have a range from prior markings.
563 * This is fine as long as it hasn't been advanced from its
566 return reg
->type
== which
&&
569 tnum_equals_const(reg
->var_off
, 0);
572 /* Attempts to improve min/max values based on var_off information */
573 static void __update_reg_bounds(struct bpf_reg_state
*reg
)
575 /* min signed is max(sign bit) | min(other bits) */
576 reg
->smin_value
= max_t(s64
, reg
->smin_value
,
577 reg
->var_off
.value
| (reg
->var_off
.mask
& S64_MIN
));
578 /* max signed is min(sign bit) | max(other bits) */
579 reg
->smax_value
= min_t(s64
, reg
->smax_value
,
580 reg
->var_off
.value
| (reg
->var_off
.mask
& S64_MAX
));
581 reg
->umin_value
= max(reg
->umin_value
, reg
->var_off
.value
);
582 reg
->umax_value
= min(reg
->umax_value
,
583 reg
->var_off
.value
| reg
->var_off
.mask
);
586 /* Uses signed min/max values to inform unsigned, and vice-versa */
587 static void __reg_deduce_bounds(struct bpf_reg_state
*reg
)
589 /* Learn sign from signed bounds.
590 * If we cannot cross the sign boundary, then signed and unsigned bounds
591 * are the same, so combine. This works even in the negative case, e.g.
592 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
594 if (reg
->smin_value
>= 0 || reg
->smax_value
< 0) {
595 reg
->smin_value
= reg
->umin_value
= max_t(u64
, reg
->smin_value
,
597 reg
->smax_value
= reg
->umax_value
= min_t(u64
, reg
->smax_value
,
601 /* Learn sign from unsigned bounds. Signed bounds cross the sign
602 * boundary, so we must be careful.
604 if ((s64
)reg
->umax_value
>= 0) {
605 /* Positive. We can't learn anything from the smin, but smax
606 * is positive, hence safe.
608 reg
->smin_value
= reg
->umin_value
;
609 reg
->smax_value
= reg
->umax_value
= min_t(u64
, reg
->smax_value
,
611 } else if ((s64
)reg
->umin_value
< 0) {
612 /* Negative. We can't learn anything from the smax, but smin
613 * is negative, hence safe.
615 reg
->smin_value
= reg
->umin_value
= max_t(u64
, reg
->smin_value
,
617 reg
->smax_value
= reg
->umax_value
;
621 /* Attempts to improve var_off based on unsigned min/max information */
622 static void __reg_bound_offset(struct bpf_reg_state
*reg
)
624 reg
->var_off
= tnum_intersect(reg
->var_off
,
625 tnum_range(reg
->umin_value
,
629 /* Reset the min/max bounds of a register */
630 static void __mark_reg_unbounded(struct bpf_reg_state
*reg
)
632 reg
->smin_value
= S64_MIN
;
633 reg
->smax_value
= S64_MAX
;
635 reg
->umax_value
= U64_MAX
;
638 /* Mark a register as having a completely unknown (scalar) value. */
639 static void __mark_reg_unknown(struct bpf_reg_state
*reg
)
641 reg
->type
= SCALAR_VALUE
;
644 reg
->var_off
= tnum_unknown
;
646 __mark_reg_unbounded(reg
);
649 static void mark_reg_unknown(struct bpf_verifier_env
*env
,
650 struct bpf_reg_state
*regs
, u32 regno
)
652 if (WARN_ON(regno
>= MAX_BPF_REG
)) {
653 verbose(env
, "mark_reg_unknown(regs, %u)\n", regno
);
654 /* Something bad happened, let's kill all regs except FP */
655 for (regno
= 0; regno
< BPF_REG_FP
; regno
++)
656 __mark_reg_not_init(regs
+ regno
);
659 __mark_reg_unknown(regs
+ regno
);
662 static void __mark_reg_not_init(struct bpf_reg_state
*reg
)
664 __mark_reg_unknown(reg
);
665 reg
->type
= NOT_INIT
;
668 static void mark_reg_not_init(struct bpf_verifier_env
*env
,
669 struct bpf_reg_state
*regs
, u32 regno
)
671 if (WARN_ON(regno
>= MAX_BPF_REG
)) {
672 verbose(env
, "mark_reg_not_init(regs, %u)\n", regno
);
673 /* Something bad happened, let's kill all regs except FP */
674 for (regno
= 0; regno
< BPF_REG_FP
; regno
++)
675 __mark_reg_not_init(regs
+ regno
);
678 __mark_reg_not_init(regs
+ regno
);
681 static void init_reg_state(struct bpf_verifier_env
*env
,
682 struct bpf_func_state
*state
)
684 struct bpf_reg_state
*regs
= state
->regs
;
687 for (i
= 0; i
< MAX_BPF_REG
; i
++) {
688 mark_reg_not_init(env
, regs
, i
);
689 regs
[i
].live
= REG_LIVE_NONE
;
693 regs
[BPF_REG_FP
].type
= PTR_TO_STACK
;
694 mark_reg_known_zero(env
, regs
, BPF_REG_FP
);
695 regs
[BPF_REG_FP
].frameno
= state
->frameno
;
697 /* 1st arg to a function */
698 regs
[BPF_REG_1
].type
= PTR_TO_CTX
;
699 mark_reg_known_zero(env
, regs
, BPF_REG_1
);
702 #define BPF_MAIN_FUNC (-1)
703 static void init_func_state(struct bpf_verifier_env
*env
,
704 struct bpf_func_state
*state
,
705 int callsite
, int frameno
, int subprogno
)
707 state
->callsite
= callsite
;
708 state
->frameno
= frameno
;
709 state
->subprogno
= subprogno
;
710 init_reg_state(env
, state
);
714 SRC_OP
, /* register is used as source operand */
715 DST_OP
, /* register is used as destination operand */
716 DST_OP_NO_MARK
/* same as above, check only, don't mark */
719 static int cmp_subprogs(const void *a
, const void *b
)
721 return *(int *)a
- *(int *)b
;
724 static int find_subprog(struct bpf_verifier_env
*env
, int off
)
728 p
= bsearch(&off
, env
->subprog_starts
, env
->subprog_cnt
,
729 sizeof(env
->subprog_starts
[0]), cmp_subprogs
);
732 return p
- env
->subprog_starts
;
736 static int add_subprog(struct bpf_verifier_env
*env
, int off
)
738 int insn_cnt
= env
->prog
->len
;
741 if (off
>= insn_cnt
|| off
< 0) {
742 verbose(env
, "call to invalid destination\n");
745 ret
= find_subprog(env
, off
);
748 if (env
->subprog_cnt
>= BPF_MAX_SUBPROGS
) {
749 verbose(env
, "too many subprograms\n");
752 env
->subprog_starts
[env
->subprog_cnt
++] = off
;
753 sort(env
->subprog_starts
, env
->subprog_cnt
,
754 sizeof(env
->subprog_starts
[0]), cmp_subprogs
, NULL
);
758 static int check_subprogs(struct bpf_verifier_env
*env
)
760 int i
, ret
, subprog_start
, subprog_end
, off
, cur_subprog
= 0;
761 struct bpf_insn
*insn
= env
->prog
->insnsi
;
762 int insn_cnt
= env
->prog
->len
;
764 /* determine subprog starts. The end is one before the next starts */
765 for (i
= 0; i
< insn_cnt
; i
++) {
766 if (insn
[i
].code
!= (BPF_JMP
| BPF_CALL
))
768 if (insn
[i
].src_reg
!= BPF_PSEUDO_CALL
)
770 if (!env
->allow_ptr_leaks
) {
771 verbose(env
, "function calls to other bpf functions are allowed for root only\n");
774 if (bpf_prog_is_dev_bound(env
->prog
->aux
)) {
775 verbose(env
, "funcation calls in offloaded programs are not supported yet\n");
778 ret
= add_subprog(env
, i
+ insn
[i
].imm
+ 1);
783 if (env
->log
.level
> 1)
784 for (i
= 0; i
< env
->subprog_cnt
; i
++)
785 verbose(env
, "func#%d @%d\n", i
, env
->subprog_starts
[i
]);
787 /* now check that all jumps are within the same subprog */
789 if (env
->subprog_cnt
== cur_subprog
)
790 subprog_end
= insn_cnt
;
792 subprog_end
= env
->subprog_starts
[cur_subprog
++];
793 for (i
= 0; i
< insn_cnt
; i
++) {
794 u8 code
= insn
[i
].code
;
796 if (BPF_CLASS(code
) != BPF_JMP
)
798 if (BPF_OP(code
) == BPF_EXIT
|| BPF_OP(code
) == BPF_CALL
)
800 off
= i
+ insn
[i
].off
+ 1;
801 if (off
< subprog_start
|| off
>= subprog_end
) {
802 verbose(env
, "jump out of range from insn %d to %d\n", i
, off
);
806 if (i
== subprog_end
- 1) {
807 /* to avoid fall-through from one subprog into another
808 * the last insn of the subprog should be either exit
809 * or unconditional jump back
811 if (code
!= (BPF_JMP
| BPF_EXIT
) &&
812 code
!= (BPF_JMP
| BPF_JA
)) {
813 verbose(env
, "last insn is not an exit or jmp\n");
816 subprog_start
= subprog_end
;
817 if (env
->subprog_cnt
== cur_subprog
)
818 subprog_end
= insn_cnt
;
820 subprog_end
= env
->subprog_starts
[cur_subprog
++];
826 struct bpf_verifier_state
*skip_callee(struct bpf_verifier_env
*env
,
827 const struct bpf_verifier_state
*state
,
828 struct bpf_verifier_state
*parent
,
831 struct bpf_verifier_state
*tmp
= NULL
;
833 /* 'parent' could be a state of caller and
834 * 'state' could be a state of callee. In such case
835 * parent->curframe < state->curframe
836 * and it's ok for r1 - r5 registers
838 * 'parent' could be a callee's state after it bpf_exit-ed.
839 * In such case parent->curframe > state->curframe
840 * and it's ok for r0 only
842 if (parent
->curframe
== state
->curframe
||
843 (parent
->curframe
< state
->curframe
&&
844 regno
>= BPF_REG_1
&& regno
<= BPF_REG_5
) ||
845 (parent
->curframe
> state
->curframe
&&
849 if (parent
->curframe
> state
->curframe
&&
850 regno
>= BPF_REG_6
) {
851 /* for callee saved regs we have to skip the whole chain
852 * of states that belong to callee and mark as LIVE_READ
853 * the registers before the call
856 while (tmp
&& tmp
->curframe
!= state
->curframe
) {
867 verbose(env
, "verifier bug regno %d tmp %p\n", regno
, tmp
);
868 verbose(env
, "regno %d parent frame %d current frame %d\n",
869 regno
, parent
->curframe
, state
->curframe
);
873 static int mark_reg_read(struct bpf_verifier_env
*env
,
874 const struct bpf_verifier_state
*state
,
875 struct bpf_verifier_state
*parent
,
878 bool writes
= parent
== state
->parent
; /* Observe write marks */
880 if (regno
== BPF_REG_FP
)
881 /* We don't need to worry about FP liveness because it's read-only */
885 /* if read wasn't screened by an earlier write ... */
886 if (writes
&& state
->frame
[state
->curframe
]->regs
[regno
].live
& REG_LIVE_WRITTEN
)
888 parent
= skip_callee(env
, state
, parent
, regno
);
891 /* ... then we depend on parent's value */
892 parent
->frame
[parent
->curframe
]->regs
[regno
].live
|= REG_LIVE_READ
;
894 parent
= state
->parent
;
900 static int check_reg_arg(struct bpf_verifier_env
*env
, u32 regno
,
903 struct bpf_verifier_state
*vstate
= env
->cur_state
;
904 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
905 struct bpf_reg_state
*regs
= state
->regs
;
907 if (regno
>= MAX_BPF_REG
) {
908 verbose(env
, "R%d is invalid\n", regno
);
913 /* check whether register used as source operand can be read */
914 if (regs
[regno
].type
== NOT_INIT
) {
915 verbose(env
, "R%d !read_ok\n", regno
);
918 return mark_reg_read(env
, vstate
, vstate
->parent
, regno
);
920 /* check whether register used as dest operand can be written to */
921 if (regno
== BPF_REG_FP
) {
922 verbose(env
, "frame pointer is read only\n");
925 regs
[regno
].live
|= REG_LIVE_WRITTEN
;
927 mark_reg_unknown(env
, regs
, regno
);
932 static bool is_spillable_regtype(enum bpf_reg_type type
)
935 case PTR_TO_MAP_VALUE
:
936 case PTR_TO_MAP_VALUE_OR_NULL
:
940 case PTR_TO_PACKET_META
:
941 case PTR_TO_PACKET_END
:
942 case CONST_PTR_TO_MAP
:
949 /* Does this register contain a constant zero? */
950 static bool register_is_null(struct bpf_reg_state
*reg
)
952 return reg
->type
== SCALAR_VALUE
&& tnum_equals_const(reg
->var_off
, 0);
955 /* check_stack_read/write functions track spill/fill of registers,
956 * stack boundary and alignment are checked in check_mem_access()
958 static int check_stack_write(struct bpf_verifier_env
*env
,
959 struct bpf_func_state
*state
, /* func where register points to */
960 int off
, int size
, int value_regno
)
962 struct bpf_func_state
*cur
; /* state of the current function */
963 int i
, slot
= -off
- 1, spi
= slot
/ BPF_REG_SIZE
, err
;
964 enum bpf_reg_type type
;
966 err
= realloc_func_state(state
, round_up(slot
+ 1, BPF_REG_SIZE
),
970 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
971 * so it's aligned access and [off, off + size) are within stack limits
973 if (!env
->allow_ptr_leaks
&&
974 state
->stack
[spi
].slot_type
[0] == STACK_SPILL
&&
975 size
!= BPF_REG_SIZE
) {
976 verbose(env
, "attempt to corrupt spilled pointer on stack\n");
980 cur
= env
->cur_state
->frame
[env
->cur_state
->curframe
];
981 if (value_regno
>= 0 &&
982 is_spillable_regtype((type
= cur
->regs
[value_regno
].type
))) {
984 /* register containing pointer is being spilled into stack */
985 if (size
!= BPF_REG_SIZE
) {
986 verbose(env
, "invalid size of register spill\n");
990 if (state
!= cur
&& type
== PTR_TO_STACK
) {
991 verbose(env
, "cannot spill pointers to stack into stack frame of the caller\n");
995 /* save register state */
996 state
->stack
[spi
].spilled_ptr
= cur
->regs
[value_regno
];
997 state
->stack
[spi
].spilled_ptr
.live
|= REG_LIVE_WRITTEN
;
999 for (i
= 0; i
< BPF_REG_SIZE
; i
++)
1000 state
->stack
[spi
].slot_type
[i
] = STACK_SPILL
;
1002 u8 type
= STACK_MISC
;
1004 /* regular write of data into stack */
1005 state
->stack
[spi
].spilled_ptr
= (struct bpf_reg_state
) {};
1007 /* only mark the slot as written if all 8 bytes were written
1008 * otherwise read propagation may incorrectly stop too soon
1009 * when stack slots are partially written.
1010 * This heuristic means that read propagation will be
1011 * conservative, since it will add reg_live_read marks
1012 * to stack slots all the way to first state when programs
1013 * writes+reads less than 8 bytes
1015 if (size
== BPF_REG_SIZE
)
1016 state
->stack
[spi
].spilled_ptr
.live
|= REG_LIVE_WRITTEN
;
1018 /* when we zero initialize stack slots mark them as such */
1019 if (value_regno
>= 0 &&
1020 register_is_null(&cur
->regs
[value_regno
]))
1023 for (i
= 0; i
< size
; i
++)
1024 state
->stack
[spi
].slot_type
[(slot
- i
) % BPF_REG_SIZE
] =
1030 /* registers of every function are unique and mark_reg_read() propagates
1031 * the liveness in the following cases:
1032 * - from callee into caller for R1 - R5 that were used as arguments
1033 * - from caller into callee for R0 that used as result of the call
1034 * - from caller to the same caller skipping states of the callee for R6 - R9,
1035 * since R6 - R9 are callee saved by implicit function prologue and
1036 * caller's R6 != callee's R6, so when we propagate liveness up to
1037 * parent states we need to skip callee states for R6 - R9.
1039 * stack slot marking is different, since stacks of caller and callee are
1040 * accessible in both (since caller can pass a pointer to caller's stack to
1041 * callee which can pass it to another function), hence mark_stack_slot_read()
1042 * has to propagate the stack liveness to all parent states at given frame number.
1052 * First *ptr is reading from f1's stack and mark_stack_slot_read() has
1053 * to mark liveness at the f1's frame and not f2's frame.
1054 * Second *ptr is also reading from f1's stack and mark_stack_slot_read() has
1055 * to propagate liveness to f2 states at f1's frame level and further into
1056 * f1 states at f1's frame level until write into that stack slot
1058 static void mark_stack_slot_read(struct bpf_verifier_env
*env
,
1059 const struct bpf_verifier_state
*state
,
1060 struct bpf_verifier_state
*parent
,
1061 int slot
, int frameno
)
1063 bool writes
= parent
== state
->parent
; /* Observe write marks */
1066 if (parent
->frame
[frameno
]->allocated_stack
<= slot
* BPF_REG_SIZE
)
1067 /* since LIVE_WRITTEN mark is only done for full 8-byte
1068 * write the read marks are conservative and parent
1069 * state may not even have the stack allocated. In such case
1070 * end the propagation, since the loop reached beginning
1074 /* if read wasn't screened by an earlier write ... */
1075 if (writes
&& state
->frame
[frameno
]->stack
[slot
].spilled_ptr
.live
& REG_LIVE_WRITTEN
)
1077 /* ... then we depend on parent's value */
1078 parent
->frame
[frameno
]->stack
[slot
].spilled_ptr
.live
|= REG_LIVE_READ
;
1080 parent
= state
->parent
;
1085 static int check_stack_read(struct bpf_verifier_env
*env
,
1086 struct bpf_func_state
*reg_state
/* func where register points to */,
1087 int off
, int size
, int value_regno
)
1089 struct bpf_verifier_state
*vstate
= env
->cur_state
;
1090 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
1091 int i
, slot
= -off
- 1, spi
= slot
/ BPF_REG_SIZE
;
1094 if (reg_state
->allocated_stack
<= slot
) {
1095 verbose(env
, "invalid read from stack off %d+0 size %d\n",
1099 stype
= reg_state
->stack
[spi
].slot_type
;
1101 if (stype
[0] == STACK_SPILL
) {
1102 if (size
!= BPF_REG_SIZE
) {
1103 verbose(env
, "invalid size of register spill\n");
1106 for (i
= 1; i
< BPF_REG_SIZE
; i
++) {
1107 if (stype
[(slot
- i
) % BPF_REG_SIZE
] != STACK_SPILL
) {
1108 verbose(env
, "corrupted spill memory\n");
1113 if (value_regno
>= 0) {
1114 /* restore register state from stack */
1115 state
->regs
[value_regno
] = reg_state
->stack
[spi
].spilled_ptr
;
1116 /* mark reg as written since spilled pointer state likely
1117 * has its liveness marks cleared by is_state_visited()
1118 * which resets stack/reg liveness for state transitions
1120 state
->regs
[value_regno
].live
|= REG_LIVE_WRITTEN
;
1122 mark_stack_slot_read(env
, vstate
, vstate
->parent
, spi
,
1123 reg_state
->frameno
);
1128 for (i
= 0; i
< size
; i
++) {
1129 if (stype
[(slot
- i
) % BPF_REG_SIZE
] == STACK_MISC
)
1131 if (stype
[(slot
- i
) % BPF_REG_SIZE
] == STACK_ZERO
) {
1135 verbose(env
, "invalid read from stack off %d+%d size %d\n",
1139 mark_stack_slot_read(env
, vstate
, vstate
->parent
, spi
,
1140 reg_state
->frameno
);
1141 if (value_regno
>= 0) {
1142 if (zeros
== size
) {
1143 /* any size read into register is zero extended,
1144 * so the whole register == const_zero
1146 __mark_reg_const_zero(&state
->regs
[value_regno
]);
1148 /* have read misc data from the stack */
1149 mark_reg_unknown(env
, state
->regs
, value_regno
);
1151 state
->regs
[value_regno
].live
|= REG_LIVE_WRITTEN
;
1157 /* check read/write into map element returned by bpf_map_lookup_elem() */
1158 static int __check_map_access(struct bpf_verifier_env
*env
, u32 regno
, int off
,
1159 int size
, bool zero_size_allowed
)
1161 struct bpf_reg_state
*regs
= cur_regs(env
);
1162 struct bpf_map
*map
= regs
[regno
].map_ptr
;
1164 if (off
< 0 || size
< 0 || (size
== 0 && !zero_size_allowed
) ||
1165 off
+ size
> map
->value_size
) {
1166 verbose(env
, "invalid access to map value, value_size=%d off=%d size=%d\n",
1167 map
->value_size
, off
, size
);
1173 /* check read/write into a map element with possible variable offset */
1174 static int check_map_access(struct bpf_verifier_env
*env
, u32 regno
,
1175 int off
, int size
, bool zero_size_allowed
)
1177 struct bpf_verifier_state
*vstate
= env
->cur_state
;
1178 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
1179 struct bpf_reg_state
*reg
= &state
->regs
[regno
];
1182 /* We may have adjusted the register to this map value, so we
1183 * need to try adding each of min_value and max_value to off
1184 * to make sure our theoretical access will be safe.
1187 print_verifier_state(env
, state
);
1188 /* The minimum value is only important with signed
1189 * comparisons where we can't assume the floor of a
1190 * value is 0. If we are using signed variables for our
1191 * index'es we need to make sure that whatever we use
1192 * will have a set floor within our range.
1194 if (reg
->smin_value
< 0) {
1195 verbose(env
, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1199 err
= __check_map_access(env
, regno
, reg
->smin_value
+ off
, size
,
1202 verbose(env
, "R%d min value is outside of the array range\n",
1207 /* If we haven't set a max value then we need to bail since we can't be
1208 * sure we won't do bad things.
1209 * If reg->umax_value + off could overflow, treat that as unbounded too.
1211 if (reg
->umax_value
>= BPF_MAX_VAR_OFF
) {
1212 verbose(env
, "R%d unbounded memory access, make sure to bounds check any array access into a map\n",
1216 err
= __check_map_access(env
, regno
, reg
->umax_value
+ off
, size
,
1219 verbose(env
, "R%d max value is outside of the array range\n",
1224 #define MAX_PACKET_OFF 0xffff
1226 static bool may_access_direct_pkt_data(struct bpf_verifier_env
*env
,
1227 const struct bpf_call_arg_meta
*meta
,
1228 enum bpf_access_type t
)
1230 switch (env
->prog
->type
) {
1231 case BPF_PROG_TYPE_LWT_IN
:
1232 case BPF_PROG_TYPE_LWT_OUT
:
1233 /* dst_input() and dst_output() can't write for now */
1237 case BPF_PROG_TYPE_SCHED_CLS
:
1238 case BPF_PROG_TYPE_SCHED_ACT
:
1239 case BPF_PROG_TYPE_XDP
:
1240 case BPF_PROG_TYPE_LWT_XMIT
:
1241 case BPF_PROG_TYPE_SK_SKB
:
1243 return meta
->pkt_access
;
1245 env
->seen_direct_write
= true;
1252 static int __check_packet_access(struct bpf_verifier_env
*env
, u32 regno
,
1253 int off
, int size
, bool zero_size_allowed
)
1255 struct bpf_reg_state
*regs
= cur_regs(env
);
1256 struct bpf_reg_state
*reg
= ®s
[regno
];
1258 if (off
< 0 || size
< 0 || (size
== 0 && !zero_size_allowed
) ||
1259 (u64
)off
+ size
> reg
->range
) {
1260 verbose(env
, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
1261 off
, size
, regno
, reg
->id
, reg
->off
, reg
->range
);
1267 static int check_packet_access(struct bpf_verifier_env
*env
, u32 regno
, int off
,
1268 int size
, bool zero_size_allowed
)
1270 struct bpf_reg_state
*regs
= cur_regs(env
);
1271 struct bpf_reg_state
*reg
= ®s
[regno
];
1274 /* We may have added a variable offset to the packet pointer; but any
1275 * reg->range we have comes after that. We are only checking the fixed
1279 /* We don't allow negative numbers, because we aren't tracking enough
1280 * detail to prove they're safe.
1282 if (reg
->smin_value
< 0) {
1283 verbose(env
, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1287 err
= __check_packet_access(env
, regno
, off
, size
, zero_size_allowed
);
1289 verbose(env
, "R%d offset is outside of the packet\n", regno
);
1295 /* check access to 'struct bpf_context' fields. Supports fixed offsets only */
1296 static int check_ctx_access(struct bpf_verifier_env
*env
, int insn_idx
, int off
, int size
,
1297 enum bpf_access_type t
, enum bpf_reg_type
*reg_type
)
1299 struct bpf_insn_access_aux info
= {
1300 .reg_type
= *reg_type
,
1303 if (env
->ops
->is_valid_access
&&
1304 env
->ops
->is_valid_access(off
, size
, t
, &info
)) {
1305 /* A non zero info.ctx_field_size indicates that this field is a
1306 * candidate for later verifier transformation to load the whole
1307 * field and then apply a mask when accessed with a narrower
1308 * access than actual ctx access size. A zero info.ctx_field_size
1309 * will only allow for whole field access and rejects any other
1310 * type of narrower access.
1312 *reg_type
= info
.reg_type
;
1314 env
->insn_aux_data
[insn_idx
].ctx_field_size
= info
.ctx_field_size
;
1315 /* remember the offset of last byte accessed in ctx */
1316 if (env
->prog
->aux
->max_ctx_offset
< off
+ size
)
1317 env
->prog
->aux
->max_ctx_offset
= off
+ size
;
1321 verbose(env
, "invalid bpf_context access off=%d size=%d\n", off
, size
);
1325 static bool __is_pointer_value(bool allow_ptr_leaks
,
1326 const struct bpf_reg_state
*reg
)
1328 if (allow_ptr_leaks
)
1331 return reg
->type
!= SCALAR_VALUE
;
1334 static bool is_pointer_value(struct bpf_verifier_env
*env
, int regno
)
1336 return __is_pointer_value(env
->allow_ptr_leaks
, cur_regs(env
) + regno
);
1339 static int check_pkt_ptr_alignment(struct bpf_verifier_env
*env
,
1340 const struct bpf_reg_state
*reg
,
1341 int off
, int size
, bool strict
)
1343 struct tnum reg_off
;
1346 /* Byte size accesses are always allowed. */
1347 if (!strict
|| size
== 1)
1350 /* For platforms that do not have a Kconfig enabling
1351 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1352 * NET_IP_ALIGN is universally set to '2'. And on platforms
1353 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1354 * to this code only in strict mode where we want to emulate
1355 * the NET_IP_ALIGN==2 checking. Therefore use an
1356 * unconditional IP align value of '2'.
1360 reg_off
= tnum_add(reg
->var_off
, tnum_const(ip_align
+ reg
->off
+ off
));
1361 if (!tnum_is_aligned(reg_off
, size
)) {
1364 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1366 "misaligned packet access off %d+%s+%d+%d size %d\n",
1367 ip_align
, tn_buf
, reg
->off
, off
, size
);
1374 static int check_generic_ptr_alignment(struct bpf_verifier_env
*env
,
1375 const struct bpf_reg_state
*reg
,
1376 const char *pointer_desc
,
1377 int off
, int size
, bool strict
)
1379 struct tnum reg_off
;
1381 /* Byte size accesses are always allowed. */
1382 if (!strict
|| size
== 1)
1385 reg_off
= tnum_add(reg
->var_off
, tnum_const(reg
->off
+ off
));
1386 if (!tnum_is_aligned(reg_off
, size
)) {
1389 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1390 verbose(env
, "misaligned %saccess off %s+%d+%d size %d\n",
1391 pointer_desc
, tn_buf
, reg
->off
, off
, size
);
1398 static int check_ptr_alignment(struct bpf_verifier_env
*env
,
1399 const struct bpf_reg_state
*reg
,
1402 bool strict
= env
->strict_alignment
;
1403 const char *pointer_desc
= "";
1405 switch (reg
->type
) {
1407 case PTR_TO_PACKET_META
:
1408 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1409 * right in front, treat it the very same way.
1411 return check_pkt_ptr_alignment(env
, reg
, off
, size
, strict
);
1412 case PTR_TO_MAP_VALUE
:
1413 pointer_desc
= "value ";
1416 pointer_desc
= "context ";
1419 pointer_desc
= "stack ";
1424 return check_generic_ptr_alignment(env
, reg
, pointer_desc
, off
, size
,
1428 static int update_stack_depth(struct bpf_verifier_env
*env
,
1429 const struct bpf_func_state
*func
,
1432 u16 stack
= env
->subprog_stack_depth
[func
->subprogno
], total
= 0;
1433 struct bpf_verifier_state
*cur
= env
->cur_state
;
1439 /* update known max for given subprogram */
1440 env
->subprog_stack_depth
[func
->subprogno
] = -off
;
1442 /* compute the total for current call chain */
1443 for (i
= 0; i
<= cur
->curframe
; i
++) {
1444 u32 depth
= env
->subprog_stack_depth
[cur
->frame
[i
]->subprogno
];
1446 /* round up to 32-bytes, since this is granularity
1447 * of interpreter stack sizes
1449 depth
= round_up(depth
, 32);
1453 if (total
> MAX_BPF_STACK
) {
1454 verbose(env
, "combined stack size of %d calls is %d. Too large\n",
1455 cur
->curframe
, total
);
1461 static int get_callee_stack_depth(struct bpf_verifier_env
*env
,
1462 const struct bpf_insn
*insn
, int idx
)
1464 int start
= idx
+ insn
->imm
+ 1, subprog
;
1466 subprog
= find_subprog(env
, start
);
1468 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1473 return env
->subprog_stack_depth
[subprog
];
1476 /* check whether memory at (regno + off) is accessible for t = (read | write)
1477 * if t==write, value_regno is a register which value is stored into memory
1478 * if t==read, value_regno is a register which will receive the value from memory
1479 * if t==write && value_regno==-1, some unknown value is stored into memory
1480 * if t==read && value_regno==-1, don't care what we read from memory
1482 static int check_mem_access(struct bpf_verifier_env
*env
, int insn_idx
, u32 regno
, int off
,
1483 int bpf_size
, enum bpf_access_type t
,
1486 struct bpf_reg_state
*regs
= cur_regs(env
);
1487 struct bpf_reg_state
*reg
= regs
+ regno
;
1488 struct bpf_func_state
*state
;
1491 size
= bpf_size_to_bytes(bpf_size
);
1495 /* alignment checks will add in reg->off themselves */
1496 err
= check_ptr_alignment(env
, reg
, off
, size
);
1500 /* for access checks, reg->off is just part of off */
1503 if (reg
->type
== PTR_TO_MAP_VALUE
) {
1504 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
1505 is_pointer_value(env
, value_regno
)) {
1506 verbose(env
, "R%d leaks addr into map\n", value_regno
);
1510 err
= check_map_access(env
, regno
, off
, size
, false);
1511 if (!err
&& t
== BPF_READ
&& value_regno
>= 0)
1512 mark_reg_unknown(env
, regs
, value_regno
);
1514 } else if (reg
->type
== PTR_TO_CTX
) {
1515 enum bpf_reg_type reg_type
= SCALAR_VALUE
;
1517 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
1518 is_pointer_value(env
, value_regno
)) {
1519 verbose(env
, "R%d leaks addr into ctx\n", value_regno
);
1522 /* ctx accesses must be at a fixed offset, so that we can
1523 * determine what type of data were returned.
1527 "dereference of modified ctx ptr R%d off=%d+%d, ctx+const is allowed, ctx+const+const is not\n",
1528 regno
, reg
->off
, off
- reg
->off
);
1531 if (!tnum_is_const(reg
->var_off
) || reg
->var_off
.value
) {
1534 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1536 "variable ctx access var_off=%s off=%d size=%d",
1540 err
= check_ctx_access(env
, insn_idx
, off
, size
, t
, ®_type
);
1541 if (!err
&& t
== BPF_READ
&& value_regno
>= 0) {
1542 /* ctx access returns either a scalar, or a
1543 * PTR_TO_PACKET[_META,_END]. In the latter
1544 * case, we know the offset is zero.
1546 if (reg_type
== SCALAR_VALUE
)
1547 mark_reg_unknown(env
, regs
, value_regno
);
1549 mark_reg_known_zero(env
, regs
,
1551 regs
[value_regno
].id
= 0;
1552 regs
[value_regno
].off
= 0;
1553 regs
[value_regno
].range
= 0;
1554 regs
[value_regno
].type
= reg_type
;
1557 } else if (reg
->type
== PTR_TO_STACK
) {
1558 /* stack accesses must be at a fixed offset, so that we can
1559 * determine what type of data were returned.
1560 * See check_stack_read().
1562 if (!tnum_is_const(reg
->var_off
)) {
1565 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1566 verbose(env
, "variable stack access var_off=%s off=%d size=%d",
1570 off
+= reg
->var_off
.value
;
1571 if (off
>= 0 || off
< -MAX_BPF_STACK
) {
1572 verbose(env
, "invalid stack off=%d size=%d\n", off
,
1577 state
= func(env
, reg
);
1578 err
= update_stack_depth(env
, state
, off
);
1583 err
= check_stack_write(env
, state
, off
, size
,
1586 err
= check_stack_read(env
, state
, off
, size
,
1588 } else if (reg_is_pkt_pointer(reg
)) {
1589 if (t
== BPF_WRITE
&& !may_access_direct_pkt_data(env
, NULL
, t
)) {
1590 verbose(env
, "cannot write into packet\n");
1593 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
1594 is_pointer_value(env
, value_regno
)) {
1595 verbose(env
, "R%d leaks addr into packet\n",
1599 err
= check_packet_access(env
, regno
, off
, size
, false);
1600 if (!err
&& t
== BPF_READ
&& value_regno
>= 0)
1601 mark_reg_unknown(env
, regs
, value_regno
);
1603 verbose(env
, "R%d invalid mem access '%s'\n", regno
,
1604 reg_type_str
[reg
->type
]);
1608 if (!err
&& size
< BPF_REG_SIZE
&& value_regno
>= 0 && t
== BPF_READ
&&
1609 regs
[value_regno
].type
== SCALAR_VALUE
) {
1610 /* b/h/w load zero-extends, mark upper bits as known 0 */
1611 regs
[value_regno
].var_off
=
1612 tnum_cast(regs
[value_regno
].var_off
, size
);
1613 __update_reg_bounds(®s
[value_regno
]);
1618 static int check_xadd(struct bpf_verifier_env
*env
, int insn_idx
, struct bpf_insn
*insn
)
1622 if ((BPF_SIZE(insn
->code
) != BPF_W
&& BPF_SIZE(insn
->code
) != BPF_DW
) ||
1624 verbose(env
, "BPF_XADD uses reserved fields\n");
1628 /* check src1 operand */
1629 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
1633 /* check src2 operand */
1634 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
1638 if (is_pointer_value(env
, insn
->src_reg
)) {
1639 verbose(env
, "R%d leaks addr into mem\n", insn
->src_reg
);
1643 /* check whether atomic_add can read the memory */
1644 err
= check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
1645 BPF_SIZE(insn
->code
), BPF_READ
, -1);
1649 /* check whether atomic_add can write into the same memory */
1650 return check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
1651 BPF_SIZE(insn
->code
), BPF_WRITE
, -1);
1654 /* when register 'regno' is passed into function that will read 'access_size'
1655 * bytes from that pointer, make sure that it's within stack boundary
1656 * and all elements of stack are initialized.
1657 * Unlike most pointer bounds-checking functions, this one doesn't take an
1658 * 'off' argument, so it has to add in reg->off itself.
1660 static int check_stack_boundary(struct bpf_verifier_env
*env
, int regno
,
1661 int access_size
, bool zero_size_allowed
,
1662 struct bpf_call_arg_meta
*meta
)
1664 struct bpf_reg_state
*reg
= cur_regs(env
) + regno
;
1665 struct bpf_func_state
*state
= func(env
, reg
);
1666 int off
, i
, slot
, spi
;
1668 if (reg
->type
!= PTR_TO_STACK
) {
1669 /* Allow zero-byte read from NULL, regardless of pointer type */
1670 if (zero_size_allowed
&& access_size
== 0 &&
1671 register_is_null(reg
))
1674 verbose(env
, "R%d type=%s expected=%s\n", regno
,
1675 reg_type_str
[reg
->type
],
1676 reg_type_str
[PTR_TO_STACK
]);
1680 /* Only allow fixed-offset stack reads */
1681 if (!tnum_is_const(reg
->var_off
)) {
1684 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1685 verbose(env
, "invalid variable stack read R%d var_off=%s\n",
1688 off
= reg
->off
+ reg
->var_off
.value
;
1689 if (off
>= 0 || off
< -MAX_BPF_STACK
|| off
+ access_size
> 0 ||
1690 access_size
< 0 || (access_size
== 0 && !zero_size_allowed
)) {
1691 verbose(env
, "invalid stack type R%d off=%d access_size=%d\n",
1692 regno
, off
, access_size
);
1696 if (meta
&& meta
->raw_mode
) {
1697 meta
->access_size
= access_size
;
1698 meta
->regno
= regno
;
1702 for (i
= 0; i
< access_size
; i
++) {
1705 slot
= -(off
+ i
) - 1;
1706 spi
= slot
/ BPF_REG_SIZE
;
1707 if (state
->allocated_stack
<= slot
)
1709 stype
= &state
->stack
[spi
].slot_type
[slot
% BPF_REG_SIZE
];
1710 if (*stype
== STACK_MISC
)
1712 if (*stype
== STACK_ZERO
) {
1713 /* helper can write anything into the stack */
1714 *stype
= STACK_MISC
;
1718 verbose(env
, "invalid indirect read from stack off %d+%d size %d\n",
1719 off
, i
, access_size
);
1722 /* reading any byte out of 8-byte 'spill_slot' will cause
1723 * the whole slot to be marked as 'read'
1725 mark_stack_slot_read(env
, env
->cur_state
, env
->cur_state
->parent
,
1726 spi
, state
->frameno
);
1728 return update_stack_depth(env
, state
, off
);
1731 static int check_helper_mem_access(struct bpf_verifier_env
*env
, int regno
,
1732 int access_size
, bool zero_size_allowed
,
1733 struct bpf_call_arg_meta
*meta
)
1735 struct bpf_reg_state
*regs
= cur_regs(env
), *reg
= ®s
[regno
];
1737 switch (reg
->type
) {
1739 case PTR_TO_PACKET_META
:
1740 return check_packet_access(env
, regno
, reg
->off
, access_size
,
1742 case PTR_TO_MAP_VALUE
:
1743 return check_map_access(env
, regno
, reg
->off
, access_size
,
1745 default: /* scalar_value|ptr_to_stack or invalid ptr */
1746 return check_stack_boundary(env
, regno
, access_size
,
1747 zero_size_allowed
, meta
);
1751 static int check_func_arg(struct bpf_verifier_env
*env
, u32 regno
,
1752 enum bpf_arg_type arg_type
,
1753 struct bpf_call_arg_meta
*meta
)
1755 struct bpf_reg_state
*regs
= cur_regs(env
), *reg
= ®s
[regno
];
1756 enum bpf_reg_type expected_type
, type
= reg
->type
;
1759 if (arg_type
== ARG_DONTCARE
)
1762 err
= check_reg_arg(env
, regno
, SRC_OP
);
1766 if (arg_type
== ARG_ANYTHING
) {
1767 if (is_pointer_value(env
, regno
)) {
1768 verbose(env
, "R%d leaks addr into helper function\n",
1775 if (type_is_pkt_pointer(type
) &&
1776 !may_access_direct_pkt_data(env
, meta
, BPF_READ
)) {
1777 verbose(env
, "helper access to the packet is not allowed\n");
1781 if (arg_type
== ARG_PTR_TO_MAP_KEY
||
1782 arg_type
== ARG_PTR_TO_MAP_VALUE
) {
1783 expected_type
= PTR_TO_STACK
;
1784 if (!type_is_pkt_pointer(type
) &&
1785 type
!= expected_type
)
1787 } else if (arg_type
== ARG_CONST_SIZE
||
1788 arg_type
== ARG_CONST_SIZE_OR_ZERO
) {
1789 expected_type
= SCALAR_VALUE
;
1790 if (type
!= expected_type
)
1792 } else if (arg_type
== ARG_CONST_MAP_PTR
) {
1793 expected_type
= CONST_PTR_TO_MAP
;
1794 if (type
!= expected_type
)
1796 } else if (arg_type
== ARG_PTR_TO_CTX
) {
1797 expected_type
= PTR_TO_CTX
;
1798 if (type
!= expected_type
)
1800 } else if (arg_type
== ARG_PTR_TO_MEM
||
1801 arg_type
== ARG_PTR_TO_MEM_OR_NULL
||
1802 arg_type
== ARG_PTR_TO_UNINIT_MEM
) {
1803 expected_type
= PTR_TO_STACK
;
1804 /* One exception here. In case function allows for NULL to be
1805 * passed in as argument, it's a SCALAR_VALUE type. Final test
1806 * happens during stack boundary checking.
1808 if (register_is_null(reg
) &&
1809 arg_type
== ARG_PTR_TO_MEM_OR_NULL
)
1810 /* final test in check_stack_boundary() */;
1811 else if (!type_is_pkt_pointer(type
) &&
1812 type
!= PTR_TO_MAP_VALUE
&&
1813 type
!= expected_type
)
1815 meta
->raw_mode
= arg_type
== ARG_PTR_TO_UNINIT_MEM
;
1817 verbose(env
, "unsupported arg_type %d\n", arg_type
);
1821 if (arg_type
== ARG_CONST_MAP_PTR
) {
1822 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
1823 meta
->map_ptr
= reg
->map_ptr
;
1824 } else if (arg_type
== ARG_PTR_TO_MAP_KEY
) {
1825 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1826 * check that [key, key + map->key_size) are within
1827 * stack limits and initialized
1829 if (!meta
->map_ptr
) {
1830 /* in function declaration map_ptr must come before
1831 * map_key, so that it's verified and known before
1832 * we have to check map_key here. Otherwise it means
1833 * that kernel subsystem misconfigured verifier
1835 verbose(env
, "invalid map_ptr to access map->key\n");
1838 if (type_is_pkt_pointer(type
))
1839 err
= check_packet_access(env
, regno
, reg
->off
,
1840 meta
->map_ptr
->key_size
,
1843 err
= check_stack_boundary(env
, regno
,
1844 meta
->map_ptr
->key_size
,
1846 } else if (arg_type
== ARG_PTR_TO_MAP_VALUE
) {
1847 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1848 * check [value, value + map->value_size) validity
1850 if (!meta
->map_ptr
) {
1851 /* kernel subsystem misconfigured verifier */
1852 verbose(env
, "invalid map_ptr to access map->value\n");
1855 if (type_is_pkt_pointer(type
))
1856 err
= check_packet_access(env
, regno
, reg
->off
,
1857 meta
->map_ptr
->value_size
,
1860 err
= check_stack_boundary(env
, regno
,
1861 meta
->map_ptr
->value_size
,
1863 } else if (arg_type
== ARG_CONST_SIZE
||
1864 arg_type
== ARG_CONST_SIZE_OR_ZERO
) {
1865 bool zero_size_allowed
= (arg_type
== ARG_CONST_SIZE_OR_ZERO
);
1867 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1868 * from stack pointer 'buf'. Check it
1869 * note: regno == len, regno - 1 == buf
1872 /* kernel subsystem misconfigured verifier */
1874 "ARG_CONST_SIZE cannot be first argument\n");
1878 /* The register is SCALAR_VALUE; the access check
1879 * happens using its boundaries.
1882 if (!tnum_is_const(reg
->var_off
))
1883 /* For unprivileged variable accesses, disable raw
1884 * mode so that the program is required to
1885 * initialize all the memory that the helper could
1886 * just partially fill up.
1890 if (reg
->smin_value
< 0) {
1891 verbose(env
, "R%d min value is negative, either use unsigned or 'var &= const'\n",
1896 if (reg
->umin_value
== 0) {
1897 err
= check_helper_mem_access(env
, regno
- 1, 0,
1904 if (reg
->umax_value
>= BPF_MAX_VAR_SIZ
) {
1905 verbose(env
, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
1909 err
= check_helper_mem_access(env
, regno
- 1,
1911 zero_size_allowed
, meta
);
1916 verbose(env
, "R%d type=%s expected=%s\n", regno
,
1917 reg_type_str
[type
], reg_type_str
[expected_type
]);
1921 static int check_map_func_compatibility(struct bpf_verifier_env
*env
,
1922 struct bpf_map
*map
, int func_id
)
1927 /* We need a two way check, first is from map perspective ... */
1928 switch (map
->map_type
) {
1929 case BPF_MAP_TYPE_PROG_ARRAY
:
1930 if (func_id
!= BPF_FUNC_tail_call
)
1933 case BPF_MAP_TYPE_PERF_EVENT_ARRAY
:
1934 if (func_id
!= BPF_FUNC_perf_event_read
&&
1935 func_id
!= BPF_FUNC_perf_event_output
&&
1936 func_id
!= BPF_FUNC_perf_event_read_value
)
1939 case BPF_MAP_TYPE_STACK_TRACE
:
1940 if (func_id
!= BPF_FUNC_get_stackid
)
1943 case BPF_MAP_TYPE_CGROUP_ARRAY
:
1944 if (func_id
!= BPF_FUNC_skb_under_cgroup
&&
1945 func_id
!= BPF_FUNC_current_task_under_cgroup
)
1948 /* devmap returns a pointer to a live net_device ifindex that we cannot
1949 * allow to be modified from bpf side. So do not allow lookup elements
1952 case BPF_MAP_TYPE_DEVMAP
:
1953 if (func_id
!= BPF_FUNC_redirect_map
)
1956 /* Restrict bpf side of cpumap, open when use-cases appear */
1957 case BPF_MAP_TYPE_CPUMAP
:
1958 if (func_id
!= BPF_FUNC_redirect_map
)
1961 case BPF_MAP_TYPE_ARRAY_OF_MAPS
:
1962 case BPF_MAP_TYPE_HASH_OF_MAPS
:
1963 if (func_id
!= BPF_FUNC_map_lookup_elem
)
1966 case BPF_MAP_TYPE_SOCKMAP
:
1967 if (func_id
!= BPF_FUNC_sk_redirect_map
&&
1968 func_id
!= BPF_FUNC_sock_map_update
&&
1969 func_id
!= BPF_FUNC_map_delete_elem
)
1976 /* ... and second from the function itself. */
1978 case BPF_FUNC_tail_call
:
1979 if (map
->map_type
!= BPF_MAP_TYPE_PROG_ARRAY
)
1981 if (env
->subprog_cnt
) {
1982 verbose(env
, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
1986 case BPF_FUNC_perf_event_read
:
1987 case BPF_FUNC_perf_event_output
:
1988 case BPF_FUNC_perf_event_read_value
:
1989 if (map
->map_type
!= BPF_MAP_TYPE_PERF_EVENT_ARRAY
)
1992 case BPF_FUNC_get_stackid
:
1993 if (map
->map_type
!= BPF_MAP_TYPE_STACK_TRACE
)
1996 case BPF_FUNC_current_task_under_cgroup
:
1997 case BPF_FUNC_skb_under_cgroup
:
1998 if (map
->map_type
!= BPF_MAP_TYPE_CGROUP_ARRAY
)
2001 case BPF_FUNC_redirect_map
:
2002 if (map
->map_type
!= BPF_MAP_TYPE_DEVMAP
&&
2003 map
->map_type
!= BPF_MAP_TYPE_CPUMAP
)
2006 case BPF_FUNC_sk_redirect_map
:
2007 if (map
->map_type
!= BPF_MAP_TYPE_SOCKMAP
)
2010 case BPF_FUNC_sock_map_update
:
2011 if (map
->map_type
!= BPF_MAP_TYPE_SOCKMAP
)
2020 verbose(env
, "cannot pass map_type %d into func %s#%d\n",
2021 map
->map_type
, func_id_name(func_id
), func_id
);
2025 static int check_raw_mode(const struct bpf_func_proto
*fn
)
2029 if (fn
->arg1_type
== ARG_PTR_TO_UNINIT_MEM
)
2031 if (fn
->arg2_type
== ARG_PTR_TO_UNINIT_MEM
)
2033 if (fn
->arg3_type
== ARG_PTR_TO_UNINIT_MEM
)
2035 if (fn
->arg4_type
== ARG_PTR_TO_UNINIT_MEM
)
2037 if (fn
->arg5_type
== ARG_PTR_TO_UNINIT_MEM
)
2040 return count
> 1 ? -EINVAL
: 0;
2043 /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
2044 * are now invalid, so turn them into unknown SCALAR_VALUE.
2046 static void __clear_all_pkt_pointers(struct bpf_verifier_env
*env
,
2047 struct bpf_func_state
*state
)
2049 struct bpf_reg_state
*regs
= state
->regs
, *reg
;
2052 for (i
= 0; i
< MAX_BPF_REG
; i
++)
2053 if (reg_is_pkt_pointer_any(®s
[i
]))
2054 mark_reg_unknown(env
, regs
, i
);
2056 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
2057 if (state
->stack
[i
].slot_type
[0] != STACK_SPILL
)
2059 reg
= &state
->stack
[i
].spilled_ptr
;
2060 if (reg_is_pkt_pointer_any(reg
))
2061 __mark_reg_unknown(reg
);
2065 static void clear_all_pkt_pointers(struct bpf_verifier_env
*env
)
2067 struct bpf_verifier_state
*vstate
= env
->cur_state
;
2070 for (i
= 0; i
<= vstate
->curframe
; i
++)
2071 __clear_all_pkt_pointers(env
, vstate
->frame
[i
]);
2074 static int check_func_call(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
,
2077 struct bpf_verifier_state
*state
= env
->cur_state
;
2078 struct bpf_func_state
*caller
, *callee
;
2079 int i
, subprog
, target_insn
;
2081 if (state
->curframe
>= MAX_CALL_FRAMES
) {
2082 verbose(env
, "the call stack of %d frames is too deep\n",
2087 target_insn
= *insn_idx
+ insn
->imm
;
2088 subprog
= find_subprog(env
, target_insn
+ 1);
2090 verbose(env
, "verifier bug. No program starts at insn %d\n",
2095 caller
= state
->frame
[state
->curframe
];
2096 if (state
->frame
[state
->curframe
+ 1]) {
2097 verbose(env
, "verifier bug. Frame %d already allocated\n",
2098 state
->curframe
+ 1);
2102 callee
= kzalloc(sizeof(*callee
), GFP_KERNEL
);
2105 state
->frame
[state
->curframe
+ 1] = callee
;
2107 /* callee cannot access r0, r6 - r9 for reading and has to write
2108 * into its own stack before reading from it.
2109 * callee can read/write into caller's stack
2111 init_func_state(env
, callee
,
2112 /* remember the callsite, it will be used by bpf_exit */
2113 *insn_idx
/* callsite */,
2114 state
->curframe
+ 1 /* frameno within this callchain */,
2115 subprog
+ 1 /* subprog number within this prog */);
2117 /* copy r1 - r5 args that callee can access */
2118 for (i
= BPF_REG_1
; i
<= BPF_REG_5
; i
++)
2119 callee
->regs
[i
] = caller
->regs
[i
];
2121 /* after the call regsiters r0 - r5 were scratched */
2122 for (i
= 0; i
< CALLER_SAVED_REGS
; i
++) {
2123 mark_reg_not_init(env
, caller
->regs
, caller_saved
[i
]);
2124 check_reg_arg(env
, caller_saved
[i
], DST_OP_NO_MARK
);
2127 /* only increment it after check_reg_arg() finished */
2130 /* and go analyze first insn of the callee */
2131 *insn_idx
= target_insn
;
2133 if (env
->log
.level
) {
2134 verbose(env
, "caller:\n");
2135 print_verifier_state(env
, caller
);
2136 verbose(env
, "callee:\n");
2137 print_verifier_state(env
, callee
);
2142 static int prepare_func_exit(struct bpf_verifier_env
*env
, int *insn_idx
)
2144 struct bpf_verifier_state
*state
= env
->cur_state
;
2145 struct bpf_func_state
*caller
, *callee
;
2146 struct bpf_reg_state
*r0
;
2148 callee
= state
->frame
[state
->curframe
];
2149 r0
= &callee
->regs
[BPF_REG_0
];
2150 if (r0
->type
== PTR_TO_STACK
) {
2151 /* technically it's ok to return caller's stack pointer
2152 * (or caller's caller's pointer) back to the caller,
2153 * since these pointers are valid. Only current stack
2154 * pointer will be invalid as soon as function exits,
2155 * but let's be conservative
2157 verbose(env
, "cannot return stack pointer to the caller\n");
2162 caller
= state
->frame
[state
->curframe
];
2163 /* return to the caller whatever r0 had in the callee */
2164 caller
->regs
[BPF_REG_0
] = *r0
;
2166 *insn_idx
= callee
->callsite
+ 1;
2167 if (env
->log
.level
) {
2168 verbose(env
, "returning from callee:\n");
2169 print_verifier_state(env
, callee
);
2170 verbose(env
, "to caller at %d:\n", *insn_idx
);
2171 print_verifier_state(env
, caller
);
2173 /* clear everything in the callee */
2174 free_func_state(callee
);
2175 state
->frame
[state
->curframe
+ 1] = NULL
;
2179 static int check_helper_call(struct bpf_verifier_env
*env
, int func_id
, int insn_idx
)
2181 const struct bpf_func_proto
*fn
= NULL
;
2182 struct bpf_reg_state
*regs
;
2183 struct bpf_call_arg_meta meta
;
2187 /* find function prototype */
2188 if (func_id
< 0 || func_id
>= __BPF_FUNC_MAX_ID
) {
2189 verbose(env
, "invalid func %s#%d\n", func_id_name(func_id
),
2194 if (env
->ops
->get_func_proto
)
2195 fn
= env
->ops
->get_func_proto(func_id
);
2198 verbose(env
, "unknown func %s#%d\n", func_id_name(func_id
),
2203 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2204 if (!env
->prog
->gpl_compatible
&& fn
->gpl_only
) {
2205 verbose(env
, "cannot call GPL only function from proprietary program\n");
2209 changes_data
= bpf_helper_changes_pkt_data(fn
->func
);
2211 memset(&meta
, 0, sizeof(meta
));
2212 meta
.pkt_access
= fn
->pkt_access
;
2214 /* We only support one arg being in raw mode at the moment, which
2215 * is sufficient for the helper functions we have right now.
2217 err
= check_raw_mode(fn
);
2219 verbose(env
, "kernel subsystem misconfigured func %s#%d\n",
2220 func_id_name(func_id
), func_id
);
2225 err
= check_func_arg(env
, BPF_REG_1
, fn
->arg1_type
, &meta
);
2228 err
= check_func_arg(env
, BPF_REG_2
, fn
->arg2_type
, &meta
);
2231 err
= check_func_arg(env
, BPF_REG_3
, fn
->arg3_type
, &meta
);
2234 err
= check_func_arg(env
, BPF_REG_4
, fn
->arg4_type
, &meta
);
2237 err
= check_func_arg(env
, BPF_REG_5
, fn
->arg5_type
, &meta
);
2241 /* Mark slots with STACK_MISC in case of raw mode, stack offset
2242 * is inferred from register state.
2244 for (i
= 0; i
< meta
.access_size
; i
++) {
2245 err
= check_mem_access(env
, insn_idx
, meta
.regno
, i
, BPF_B
, BPF_WRITE
, -1);
2250 regs
= cur_regs(env
);
2251 /* reset caller saved regs */
2252 for (i
= 0; i
< CALLER_SAVED_REGS
; i
++) {
2253 mark_reg_not_init(env
, regs
, caller_saved
[i
]);
2254 check_reg_arg(env
, caller_saved
[i
], DST_OP_NO_MARK
);
2257 /* update return register (already marked as written above) */
2258 if (fn
->ret_type
== RET_INTEGER
) {
2259 /* sets type to SCALAR_VALUE */
2260 mark_reg_unknown(env
, regs
, BPF_REG_0
);
2261 } else if (fn
->ret_type
== RET_VOID
) {
2262 regs
[BPF_REG_0
].type
= NOT_INIT
;
2263 } else if (fn
->ret_type
== RET_PTR_TO_MAP_VALUE_OR_NULL
) {
2264 struct bpf_insn_aux_data
*insn_aux
;
2266 regs
[BPF_REG_0
].type
= PTR_TO_MAP_VALUE_OR_NULL
;
2267 /* There is no offset yet applied, variable or fixed */
2268 mark_reg_known_zero(env
, regs
, BPF_REG_0
);
2269 regs
[BPF_REG_0
].off
= 0;
2270 /* remember map_ptr, so that check_map_access()
2271 * can check 'value_size' boundary of memory access
2272 * to map element returned from bpf_map_lookup_elem()
2274 if (meta
.map_ptr
== NULL
) {
2276 "kernel subsystem misconfigured verifier\n");
2279 regs
[BPF_REG_0
].map_ptr
= meta
.map_ptr
;
2280 regs
[BPF_REG_0
].id
= ++env
->id_gen
;
2281 insn_aux
= &env
->insn_aux_data
[insn_idx
];
2282 if (!insn_aux
->map_ptr
)
2283 insn_aux
->map_ptr
= meta
.map_ptr
;
2284 else if (insn_aux
->map_ptr
!= meta
.map_ptr
)
2285 insn_aux
->map_ptr
= BPF_MAP_PTR_POISON
;
2287 verbose(env
, "unknown return type %d of func %s#%d\n",
2288 fn
->ret_type
, func_id_name(func_id
), func_id
);
2292 err
= check_map_func_compatibility(env
, meta
.map_ptr
, func_id
);
2297 clear_all_pkt_pointers(env
);
2301 static void coerce_reg_to_32(struct bpf_reg_state
*reg
)
2303 /* clear high 32 bits */
2304 reg
->var_off
= tnum_cast(reg
->var_off
, 4);
2306 __update_reg_bounds(reg
);
2309 static bool signed_add_overflows(s64 a
, s64 b
)
2311 /* Do the add in u64, where overflow is well-defined */
2312 s64 res
= (s64
)((u64
)a
+ (u64
)b
);
2319 static bool signed_sub_overflows(s64 a
, s64 b
)
2321 /* Do the sub in u64, where overflow is well-defined */
2322 s64 res
= (s64
)((u64
)a
- (u64
)b
);
2329 /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
2330 * Caller should also handle BPF_MOV case separately.
2331 * If we return -EACCES, caller may want to try again treating pointer as a
2332 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
2334 static int adjust_ptr_min_max_vals(struct bpf_verifier_env
*env
,
2335 struct bpf_insn
*insn
,
2336 const struct bpf_reg_state
*ptr_reg
,
2337 const struct bpf_reg_state
*off_reg
)
2339 struct bpf_verifier_state
*vstate
= env
->cur_state
;
2340 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
2341 struct bpf_reg_state
*regs
= state
->regs
, *dst_reg
;
2342 bool known
= tnum_is_const(off_reg
->var_off
);
2343 s64 smin_val
= off_reg
->smin_value
, smax_val
= off_reg
->smax_value
,
2344 smin_ptr
= ptr_reg
->smin_value
, smax_ptr
= ptr_reg
->smax_value
;
2345 u64 umin_val
= off_reg
->umin_value
, umax_val
= off_reg
->umax_value
,
2346 umin_ptr
= ptr_reg
->umin_value
, umax_ptr
= ptr_reg
->umax_value
;
2347 u8 opcode
= BPF_OP(insn
->code
);
2348 u32 dst
= insn
->dst_reg
;
2350 dst_reg
= ®s
[dst
];
2352 if (WARN_ON_ONCE(known
&& (smin_val
!= smax_val
))) {
2353 print_verifier_state(env
, state
);
2355 "verifier internal error: known but bad sbounds\n");
2358 if (WARN_ON_ONCE(known
&& (umin_val
!= umax_val
))) {
2359 print_verifier_state(env
, state
);
2361 "verifier internal error: known but bad ubounds\n");
2365 if (BPF_CLASS(insn
->code
) != BPF_ALU64
) {
2366 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
2367 if (!env
->allow_ptr_leaks
)
2369 "R%d 32-bit pointer arithmetic prohibited\n",
2374 if (ptr_reg
->type
== PTR_TO_MAP_VALUE_OR_NULL
) {
2375 if (!env
->allow_ptr_leaks
)
2376 verbose(env
, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n",
2380 if (ptr_reg
->type
== CONST_PTR_TO_MAP
) {
2381 if (!env
->allow_ptr_leaks
)
2382 verbose(env
, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n",
2386 if (ptr_reg
->type
== PTR_TO_PACKET_END
) {
2387 if (!env
->allow_ptr_leaks
)
2388 verbose(env
, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n",
2393 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
2394 * The id may be overwritten later if we create a new variable offset.
2396 dst_reg
->type
= ptr_reg
->type
;
2397 dst_reg
->id
= ptr_reg
->id
;
2401 /* We can take a fixed offset as long as it doesn't overflow
2402 * the s32 'off' field
2404 if (known
&& (ptr_reg
->off
+ smin_val
==
2405 (s64
)(s32
)(ptr_reg
->off
+ smin_val
))) {
2406 /* pointer += K. Accumulate it into fixed offset */
2407 dst_reg
->smin_value
= smin_ptr
;
2408 dst_reg
->smax_value
= smax_ptr
;
2409 dst_reg
->umin_value
= umin_ptr
;
2410 dst_reg
->umax_value
= umax_ptr
;
2411 dst_reg
->var_off
= ptr_reg
->var_off
;
2412 dst_reg
->off
= ptr_reg
->off
+ smin_val
;
2413 dst_reg
->range
= ptr_reg
->range
;
2416 /* A new variable offset is created. Note that off_reg->off
2417 * == 0, since it's a scalar.
2418 * dst_reg gets the pointer type and since some positive
2419 * integer value was added to the pointer, give it a new 'id'
2420 * if it's a PTR_TO_PACKET.
2421 * this creates a new 'base' pointer, off_reg (variable) gets
2422 * added into the variable offset, and we copy the fixed offset
2425 if (signed_add_overflows(smin_ptr
, smin_val
) ||
2426 signed_add_overflows(smax_ptr
, smax_val
)) {
2427 dst_reg
->smin_value
= S64_MIN
;
2428 dst_reg
->smax_value
= S64_MAX
;
2430 dst_reg
->smin_value
= smin_ptr
+ smin_val
;
2431 dst_reg
->smax_value
= smax_ptr
+ smax_val
;
2433 if (umin_ptr
+ umin_val
< umin_ptr
||
2434 umax_ptr
+ umax_val
< umax_ptr
) {
2435 dst_reg
->umin_value
= 0;
2436 dst_reg
->umax_value
= U64_MAX
;
2438 dst_reg
->umin_value
= umin_ptr
+ umin_val
;
2439 dst_reg
->umax_value
= umax_ptr
+ umax_val
;
2441 dst_reg
->var_off
= tnum_add(ptr_reg
->var_off
, off_reg
->var_off
);
2442 dst_reg
->off
= ptr_reg
->off
;
2443 if (reg_is_pkt_pointer(ptr_reg
)) {
2444 dst_reg
->id
= ++env
->id_gen
;
2445 /* something was added to pkt_ptr, set range to zero */
2450 if (dst_reg
== off_reg
) {
2451 /* scalar -= pointer. Creates an unknown scalar */
2452 if (!env
->allow_ptr_leaks
)
2453 verbose(env
, "R%d tried to subtract pointer from scalar\n",
2457 /* We don't allow subtraction from FP, because (according to
2458 * test_verifier.c test "invalid fp arithmetic", JITs might not
2459 * be able to deal with it.
2461 if (ptr_reg
->type
== PTR_TO_STACK
) {
2462 if (!env
->allow_ptr_leaks
)
2463 verbose(env
, "R%d subtraction from stack pointer prohibited\n",
2467 if (known
&& (ptr_reg
->off
- smin_val
==
2468 (s64
)(s32
)(ptr_reg
->off
- smin_val
))) {
2469 /* pointer -= K. Subtract it from fixed offset */
2470 dst_reg
->smin_value
= smin_ptr
;
2471 dst_reg
->smax_value
= smax_ptr
;
2472 dst_reg
->umin_value
= umin_ptr
;
2473 dst_reg
->umax_value
= umax_ptr
;
2474 dst_reg
->var_off
= ptr_reg
->var_off
;
2475 dst_reg
->id
= ptr_reg
->id
;
2476 dst_reg
->off
= ptr_reg
->off
- smin_val
;
2477 dst_reg
->range
= ptr_reg
->range
;
2480 /* A new variable offset is created. If the subtrahend is known
2481 * nonnegative, then any reg->range we had before is still good.
2483 if (signed_sub_overflows(smin_ptr
, smax_val
) ||
2484 signed_sub_overflows(smax_ptr
, smin_val
)) {
2485 /* Overflow possible, we know nothing */
2486 dst_reg
->smin_value
= S64_MIN
;
2487 dst_reg
->smax_value
= S64_MAX
;
2489 dst_reg
->smin_value
= smin_ptr
- smax_val
;
2490 dst_reg
->smax_value
= smax_ptr
- smin_val
;
2492 if (umin_ptr
< umax_val
) {
2493 /* Overflow possible, we know nothing */
2494 dst_reg
->umin_value
= 0;
2495 dst_reg
->umax_value
= U64_MAX
;
2497 /* Cannot overflow (as long as bounds are consistent) */
2498 dst_reg
->umin_value
= umin_ptr
- umax_val
;
2499 dst_reg
->umax_value
= umax_ptr
- umin_val
;
2501 dst_reg
->var_off
= tnum_sub(ptr_reg
->var_off
, off_reg
->var_off
);
2502 dst_reg
->off
= ptr_reg
->off
;
2503 if (reg_is_pkt_pointer(ptr_reg
)) {
2504 dst_reg
->id
= ++env
->id_gen
;
2505 /* something was added to pkt_ptr, set range to zero */
2513 /* bitwise ops on pointers are troublesome, prohibit for now.
2514 * (However, in principle we could allow some cases, e.g.
2515 * ptr &= ~3 which would reduce min_value by 3.)
2517 if (!env
->allow_ptr_leaks
)
2518 verbose(env
, "R%d bitwise operator %s on pointer prohibited\n",
2519 dst
, bpf_alu_string
[opcode
>> 4]);
2522 /* other operators (e.g. MUL,LSH) produce non-pointer results */
2523 if (!env
->allow_ptr_leaks
)
2524 verbose(env
, "R%d pointer arithmetic with %s operator prohibited\n",
2525 dst
, bpf_alu_string
[opcode
>> 4]);
2529 __update_reg_bounds(dst_reg
);
2530 __reg_deduce_bounds(dst_reg
);
2531 __reg_bound_offset(dst_reg
);
2535 static int adjust_scalar_min_max_vals(struct bpf_verifier_env
*env
,
2536 struct bpf_insn
*insn
,
2537 struct bpf_reg_state
*dst_reg
,
2538 struct bpf_reg_state src_reg
)
2540 struct bpf_reg_state
*regs
= cur_regs(env
);
2541 u8 opcode
= BPF_OP(insn
->code
);
2542 bool src_known
, dst_known
;
2543 s64 smin_val
, smax_val
;
2544 u64 umin_val
, umax_val
;
2546 if (BPF_CLASS(insn
->code
) != BPF_ALU64
) {
2547 /* 32-bit ALU ops are (32,32)->64 */
2548 coerce_reg_to_32(dst_reg
);
2549 coerce_reg_to_32(&src_reg
);
2551 smin_val
= src_reg
.smin_value
;
2552 smax_val
= src_reg
.smax_value
;
2553 umin_val
= src_reg
.umin_value
;
2554 umax_val
= src_reg
.umax_value
;
2555 src_known
= tnum_is_const(src_reg
.var_off
);
2556 dst_known
= tnum_is_const(dst_reg
->var_off
);
2560 if (signed_add_overflows(dst_reg
->smin_value
, smin_val
) ||
2561 signed_add_overflows(dst_reg
->smax_value
, smax_val
)) {
2562 dst_reg
->smin_value
= S64_MIN
;
2563 dst_reg
->smax_value
= S64_MAX
;
2565 dst_reg
->smin_value
+= smin_val
;
2566 dst_reg
->smax_value
+= smax_val
;
2568 if (dst_reg
->umin_value
+ umin_val
< umin_val
||
2569 dst_reg
->umax_value
+ umax_val
< umax_val
) {
2570 dst_reg
->umin_value
= 0;
2571 dst_reg
->umax_value
= U64_MAX
;
2573 dst_reg
->umin_value
+= umin_val
;
2574 dst_reg
->umax_value
+= umax_val
;
2576 dst_reg
->var_off
= tnum_add(dst_reg
->var_off
, src_reg
.var_off
);
2579 if (signed_sub_overflows(dst_reg
->smin_value
, smax_val
) ||
2580 signed_sub_overflows(dst_reg
->smax_value
, smin_val
)) {
2581 /* Overflow possible, we know nothing */
2582 dst_reg
->smin_value
= S64_MIN
;
2583 dst_reg
->smax_value
= S64_MAX
;
2585 dst_reg
->smin_value
-= smax_val
;
2586 dst_reg
->smax_value
-= smin_val
;
2588 if (dst_reg
->umin_value
< umax_val
) {
2589 /* Overflow possible, we know nothing */
2590 dst_reg
->umin_value
= 0;
2591 dst_reg
->umax_value
= U64_MAX
;
2593 /* Cannot overflow (as long as bounds are consistent) */
2594 dst_reg
->umin_value
-= umax_val
;
2595 dst_reg
->umax_value
-= umin_val
;
2597 dst_reg
->var_off
= tnum_sub(dst_reg
->var_off
, src_reg
.var_off
);
2600 dst_reg
->var_off
= tnum_mul(dst_reg
->var_off
, src_reg
.var_off
);
2601 if (smin_val
< 0 || dst_reg
->smin_value
< 0) {
2602 /* Ain't nobody got time to multiply that sign */
2603 __mark_reg_unbounded(dst_reg
);
2604 __update_reg_bounds(dst_reg
);
2607 /* Both values are positive, so we can work with unsigned and
2608 * copy the result to signed (unless it exceeds S64_MAX).
2610 if (umax_val
> U32_MAX
|| dst_reg
->umax_value
> U32_MAX
) {
2611 /* Potential overflow, we know nothing */
2612 __mark_reg_unbounded(dst_reg
);
2613 /* (except what we can learn from the var_off) */
2614 __update_reg_bounds(dst_reg
);
2617 dst_reg
->umin_value
*= umin_val
;
2618 dst_reg
->umax_value
*= umax_val
;
2619 if (dst_reg
->umax_value
> S64_MAX
) {
2620 /* Overflow possible, we know nothing */
2621 dst_reg
->smin_value
= S64_MIN
;
2622 dst_reg
->smax_value
= S64_MAX
;
2624 dst_reg
->smin_value
= dst_reg
->umin_value
;
2625 dst_reg
->smax_value
= dst_reg
->umax_value
;
2629 if (src_known
&& dst_known
) {
2630 __mark_reg_known(dst_reg
, dst_reg
->var_off
.value
&
2631 src_reg
.var_off
.value
);
2634 /* We get our minimum from the var_off, since that's inherently
2635 * bitwise. Our maximum is the minimum of the operands' maxima.
2637 dst_reg
->var_off
= tnum_and(dst_reg
->var_off
, src_reg
.var_off
);
2638 dst_reg
->umin_value
= dst_reg
->var_off
.value
;
2639 dst_reg
->umax_value
= min(dst_reg
->umax_value
, umax_val
);
2640 if (dst_reg
->smin_value
< 0 || smin_val
< 0) {
2641 /* Lose signed bounds when ANDing negative numbers,
2642 * ain't nobody got time for that.
2644 dst_reg
->smin_value
= S64_MIN
;
2645 dst_reg
->smax_value
= S64_MAX
;
2647 /* ANDing two positives gives a positive, so safe to
2648 * cast result into s64.
2650 dst_reg
->smin_value
= dst_reg
->umin_value
;
2651 dst_reg
->smax_value
= dst_reg
->umax_value
;
2653 /* We may learn something more from the var_off */
2654 __update_reg_bounds(dst_reg
);
2657 if (src_known
&& dst_known
) {
2658 __mark_reg_known(dst_reg
, dst_reg
->var_off
.value
|
2659 src_reg
.var_off
.value
);
2662 /* We get our maximum from the var_off, and our minimum is the
2663 * maximum of the operands' minima
2665 dst_reg
->var_off
= tnum_or(dst_reg
->var_off
, src_reg
.var_off
);
2666 dst_reg
->umin_value
= max(dst_reg
->umin_value
, umin_val
);
2667 dst_reg
->umax_value
= dst_reg
->var_off
.value
|
2668 dst_reg
->var_off
.mask
;
2669 if (dst_reg
->smin_value
< 0 || smin_val
< 0) {
2670 /* Lose signed bounds when ORing negative numbers,
2671 * ain't nobody got time for that.
2673 dst_reg
->smin_value
= S64_MIN
;
2674 dst_reg
->smax_value
= S64_MAX
;
2676 /* ORing two positives gives a positive, so safe to
2677 * cast result into s64.
2679 dst_reg
->smin_value
= dst_reg
->umin_value
;
2680 dst_reg
->smax_value
= dst_reg
->umax_value
;
2682 /* We may learn something more from the var_off */
2683 __update_reg_bounds(dst_reg
);
2686 if (umax_val
> 63) {
2687 /* Shifts greater than 63 are undefined. This includes
2688 * shifts by a negative number.
2690 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
2693 /* We lose all sign bit information (except what we can pick
2696 dst_reg
->smin_value
= S64_MIN
;
2697 dst_reg
->smax_value
= S64_MAX
;
2698 /* If we might shift our top bit out, then we know nothing */
2699 if (dst_reg
->umax_value
> 1ULL << (63 - umax_val
)) {
2700 dst_reg
->umin_value
= 0;
2701 dst_reg
->umax_value
= U64_MAX
;
2703 dst_reg
->umin_value
<<= umin_val
;
2704 dst_reg
->umax_value
<<= umax_val
;
2707 dst_reg
->var_off
= tnum_lshift(dst_reg
->var_off
, umin_val
);
2709 dst_reg
->var_off
= tnum_lshift(tnum_unknown
, umin_val
);
2710 /* We may learn something more from the var_off */
2711 __update_reg_bounds(dst_reg
);
2714 if (umax_val
> 63) {
2715 /* Shifts greater than 63 are undefined. This includes
2716 * shifts by a negative number.
2718 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
2721 /* BPF_RSH is an unsigned shift, so make the appropriate casts */
2722 if (dst_reg
->smin_value
< 0) {
2724 /* Sign bit will be cleared */
2725 dst_reg
->smin_value
= 0;
2727 /* Lost sign bit information */
2728 dst_reg
->smin_value
= S64_MIN
;
2729 dst_reg
->smax_value
= S64_MAX
;
2732 dst_reg
->smin_value
=
2733 (u64
)(dst_reg
->smin_value
) >> umax_val
;
2736 dst_reg
->var_off
= tnum_rshift(dst_reg
->var_off
,
2739 dst_reg
->var_off
= tnum_rshift(tnum_unknown
, umin_val
);
2740 dst_reg
->umin_value
>>= umax_val
;
2741 dst_reg
->umax_value
>>= umin_val
;
2742 /* We may learn something more from the var_off */
2743 __update_reg_bounds(dst_reg
);
2746 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
2750 __reg_deduce_bounds(dst_reg
);
2751 __reg_bound_offset(dst_reg
);
2755 /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
2758 static int adjust_reg_min_max_vals(struct bpf_verifier_env
*env
,
2759 struct bpf_insn
*insn
)
2761 struct bpf_verifier_state
*vstate
= env
->cur_state
;
2762 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
2763 struct bpf_reg_state
*regs
= state
->regs
, *dst_reg
, *src_reg
;
2764 struct bpf_reg_state
*ptr_reg
= NULL
, off_reg
= {0};
2765 u8 opcode
= BPF_OP(insn
->code
);
2768 dst_reg
= ®s
[insn
->dst_reg
];
2770 if (dst_reg
->type
!= SCALAR_VALUE
)
2772 if (BPF_SRC(insn
->code
) == BPF_X
) {
2773 src_reg
= ®s
[insn
->src_reg
];
2774 if (src_reg
->type
!= SCALAR_VALUE
) {
2775 if (dst_reg
->type
!= SCALAR_VALUE
) {
2776 /* Combining two pointers by any ALU op yields
2777 * an arbitrary scalar.
2779 if (!env
->allow_ptr_leaks
) {
2780 verbose(env
, "R%d pointer %s pointer prohibited\n",
2782 bpf_alu_string
[opcode
>> 4]);
2785 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
2788 /* scalar += pointer
2789 * This is legal, but we have to reverse our
2790 * src/dest handling in computing the range
2792 rc
= adjust_ptr_min_max_vals(env
, insn
,
2794 if (rc
== -EACCES
&& env
->allow_ptr_leaks
) {
2795 /* scalar += unknown scalar */
2796 __mark_reg_unknown(&off_reg
);
2797 return adjust_scalar_min_max_vals(
2803 } else if (ptr_reg
) {
2804 /* pointer += scalar */
2805 rc
= adjust_ptr_min_max_vals(env
, insn
,
2807 if (rc
== -EACCES
&& env
->allow_ptr_leaks
) {
2808 /* unknown scalar += scalar */
2809 __mark_reg_unknown(dst_reg
);
2810 return adjust_scalar_min_max_vals(
2811 env
, insn
, dst_reg
, *src_reg
);
2816 /* Pretend the src is a reg with a known value, since we only
2817 * need to be able to read from this state.
2819 off_reg
.type
= SCALAR_VALUE
;
2820 __mark_reg_known(&off_reg
, insn
->imm
);
2822 if (ptr_reg
) { /* pointer += K */
2823 rc
= adjust_ptr_min_max_vals(env
, insn
,
2825 if (rc
== -EACCES
&& env
->allow_ptr_leaks
) {
2826 /* unknown scalar += K */
2827 __mark_reg_unknown(dst_reg
);
2828 return adjust_scalar_min_max_vals(
2829 env
, insn
, dst_reg
, off_reg
);
2835 /* Got here implies adding two SCALAR_VALUEs */
2836 if (WARN_ON_ONCE(ptr_reg
)) {
2837 print_verifier_state(env
, state
);
2838 verbose(env
, "verifier internal error: unexpected ptr_reg\n");
2841 if (WARN_ON(!src_reg
)) {
2842 print_verifier_state(env
, state
);
2843 verbose(env
, "verifier internal error: no src_reg\n");
2846 return adjust_scalar_min_max_vals(env
, insn
, dst_reg
, *src_reg
);
2849 /* check validity of 32-bit and 64-bit arithmetic operations */
2850 static int check_alu_op(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
2852 struct bpf_reg_state
*regs
= cur_regs(env
);
2853 u8 opcode
= BPF_OP(insn
->code
);
2856 if (opcode
== BPF_END
|| opcode
== BPF_NEG
) {
2857 if (opcode
== BPF_NEG
) {
2858 if (BPF_SRC(insn
->code
) != 0 ||
2859 insn
->src_reg
!= BPF_REG_0
||
2860 insn
->off
!= 0 || insn
->imm
!= 0) {
2861 verbose(env
, "BPF_NEG uses reserved fields\n");
2865 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0 ||
2866 (insn
->imm
!= 16 && insn
->imm
!= 32 && insn
->imm
!= 64) ||
2867 BPF_CLASS(insn
->code
) == BPF_ALU64
) {
2868 verbose(env
, "BPF_END uses reserved fields\n");
2873 /* check src operand */
2874 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
2878 if (is_pointer_value(env
, insn
->dst_reg
)) {
2879 verbose(env
, "R%d pointer arithmetic prohibited\n",
2884 /* check dest operand */
2885 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP
);
2889 } else if (opcode
== BPF_MOV
) {
2891 if (BPF_SRC(insn
->code
) == BPF_X
) {
2892 if (insn
->imm
!= 0 || insn
->off
!= 0) {
2893 verbose(env
, "BPF_MOV uses reserved fields\n");
2897 /* check src operand */
2898 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
2902 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0) {
2903 verbose(env
, "BPF_MOV uses reserved fields\n");
2908 /* check dest operand */
2909 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP
);
2913 if (BPF_SRC(insn
->code
) == BPF_X
) {
2914 if (BPF_CLASS(insn
->code
) == BPF_ALU64
) {
2916 * copy register state to dest reg
2918 regs
[insn
->dst_reg
] = regs
[insn
->src_reg
];
2919 regs
[insn
->dst_reg
].live
|= REG_LIVE_WRITTEN
;
2922 if (is_pointer_value(env
, insn
->src_reg
)) {
2924 "R%d partial copy of pointer\n",
2928 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
2929 /* high 32 bits are known zero. */
2930 regs
[insn
->dst_reg
].var_off
= tnum_cast(
2931 regs
[insn
->dst_reg
].var_off
, 4);
2932 __update_reg_bounds(®s
[insn
->dst_reg
]);
2936 * remember the value we stored into this reg
2938 regs
[insn
->dst_reg
].type
= SCALAR_VALUE
;
2939 __mark_reg_known(regs
+ insn
->dst_reg
, insn
->imm
);
2942 } else if (opcode
> BPF_END
) {
2943 verbose(env
, "invalid BPF_ALU opcode %x\n", opcode
);
2946 } else { /* all other ALU ops: and, sub, xor, add, ... */
2948 if (BPF_SRC(insn
->code
) == BPF_X
) {
2949 if (insn
->imm
!= 0 || insn
->off
!= 0) {
2950 verbose(env
, "BPF_ALU uses reserved fields\n");
2953 /* check src1 operand */
2954 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
2958 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0) {
2959 verbose(env
, "BPF_ALU uses reserved fields\n");
2964 /* check src2 operand */
2965 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
2969 if ((opcode
== BPF_MOD
|| opcode
== BPF_DIV
) &&
2970 BPF_SRC(insn
->code
) == BPF_K
&& insn
->imm
== 0) {
2971 verbose(env
, "div by zero\n");
2975 if ((opcode
== BPF_LSH
|| opcode
== BPF_RSH
||
2976 opcode
== BPF_ARSH
) && BPF_SRC(insn
->code
) == BPF_K
) {
2977 int size
= BPF_CLASS(insn
->code
) == BPF_ALU64
? 64 : 32;
2979 if (insn
->imm
< 0 || insn
->imm
>= size
) {
2980 verbose(env
, "invalid shift %d\n", insn
->imm
);
2985 /* check dest operand */
2986 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP_NO_MARK
);
2990 return adjust_reg_min_max_vals(env
, insn
);
2996 static void find_good_pkt_pointers(struct bpf_verifier_state
*vstate
,
2997 struct bpf_reg_state
*dst_reg
,
2998 enum bpf_reg_type type
,
2999 bool range_right_open
)
3001 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
3002 struct bpf_reg_state
*regs
= state
->regs
, *reg
;
3006 if (dst_reg
->off
< 0 ||
3007 (dst_reg
->off
== 0 && range_right_open
))
3008 /* This doesn't give us any range */
3011 if (dst_reg
->umax_value
> MAX_PACKET_OFF
||
3012 dst_reg
->umax_value
+ dst_reg
->off
> MAX_PACKET_OFF
)
3013 /* Risk of overflow. For instance, ptr + (1<<63) may be less
3014 * than pkt_end, but that's because it's also less than pkt.
3018 new_range
= dst_reg
->off
;
3019 if (range_right_open
)
3022 /* Examples for register markings:
3024 * pkt_data in dst register:
3028 * if (r2 > pkt_end) goto <handle exception>
3033 * if (r2 < pkt_end) goto <access okay>
3034 * <handle exception>
3037 * r2 == dst_reg, pkt_end == src_reg
3038 * r2=pkt(id=n,off=8,r=0)
3039 * r3=pkt(id=n,off=0,r=0)
3041 * pkt_data in src register:
3045 * if (pkt_end >= r2) goto <access okay>
3046 * <handle exception>
3050 * if (pkt_end <= r2) goto <handle exception>
3054 * pkt_end == dst_reg, r2 == src_reg
3055 * r2=pkt(id=n,off=8,r=0)
3056 * r3=pkt(id=n,off=0,r=0)
3058 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
3059 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
3060 * and [r3, r3 + 8-1) respectively is safe to access depending on
3064 /* If our ids match, then we must have the same max_value. And we
3065 * don't care about the other reg's fixed offset, since if it's too big
3066 * the range won't allow anything.
3067 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
3069 for (i
= 0; i
< MAX_BPF_REG
; i
++)
3070 if (regs
[i
].type
== type
&& regs
[i
].id
== dst_reg
->id
)
3071 /* keep the maximum range already checked */
3072 regs
[i
].range
= max(regs
[i
].range
, new_range
);
3074 for (j
= 0; j
<= vstate
->curframe
; j
++) {
3075 state
= vstate
->frame
[j
];
3076 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
3077 if (state
->stack
[i
].slot_type
[0] != STACK_SPILL
)
3079 reg
= &state
->stack
[i
].spilled_ptr
;
3080 if (reg
->type
== type
&& reg
->id
== dst_reg
->id
)
3081 reg
->range
= max(reg
->range
, new_range
);
3086 /* Adjusts the register min/max values in the case that the dst_reg is the
3087 * variable register that we are working on, and src_reg is a constant or we're
3088 * simply doing a BPF_K check.
3089 * In JEQ/JNE cases we also adjust the var_off values.
3091 static void reg_set_min_max(struct bpf_reg_state
*true_reg
,
3092 struct bpf_reg_state
*false_reg
, u64 val
,
3095 /* If the dst_reg is a pointer, we can't learn anything about its
3096 * variable offset from the compare (unless src_reg were a pointer into
3097 * the same object, but we don't bother with that.
3098 * Since false_reg and true_reg have the same type by construction, we
3099 * only need to check one of them for pointerness.
3101 if (__is_pointer_value(false, false_reg
))
3106 /* If this is false then we know nothing Jon Snow, but if it is
3107 * true then we know for sure.
3109 __mark_reg_known(true_reg
, val
);
3112 /* If this is true we know nothing Jon Snow, but if it is false
3113 * we know the value for sure;
3115 __mark_reg_known(false_reg
, val
);
3118 false_reg
->umax_value
= min(false_reg
->umax_value
, val
);
3119 true_reg
->umin_value
= max(true_reg
->umin_value
, val
+ 1);
3122 false_reg
->smax_value
= min_t(s64
, false_reg
->smax_value
, val
);
3123 true_reg
->smin_value
= max_t(s64
, true_reg
->smin_value
, val
+ 1);
3126 false_reg
->umin_value
= max(false_reg
->umin_value
, val
);
3127 true_reg
->umax_value
= min(true_reg
->umax_value
, val
- 1);
3130 false_reg
->smin_value
= max_t(s64
, false_reg
->smin_value
, val
);
3131 true_reg
->smax_value
= min_t(s64
, true_reg
->smax_value
, val
- 1);
3134 false_reg
->umax_value
= min(false_reg
->umax_value
, val
- 1);
3135 true_reg
->umin_value
= max(true_reg
->umin_value
, val
);
3138 false_reg
->smax_value
= min_t(s64
, false_reg
->smax_value
, val
- 1);
3139 true_reg
->smin_value
= max_t(s64
, true_reg
->smin_value
, val
);
3142 false_reg
->umin_value
= max(false_reg
->umin_value
, val
+ 1);
3143 true_reg
->umax_value
= min(true_reg
->umax_value
, val
);
3146 false_reg
->smin_value
= max_t(s64
, false_reg
->smin_value
, val
+ 1);
3147 true_reg
->smax_value
= min_t(s64
, true_reg
->smax_value
, val
);
3153 __reg_deduce_bounds(false_reg
);
3154 __reg_deduce_bounds(true_reg
);
3155 /* We might have learned some bits from the bounds. */
3156 __reg_bound_offset(false_reg
);
3157 __reg_bound_offset(true_reg
);
3158 /* Intersecting with the old var_off might have improved our bounds
3159 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3160 * then new var_off is (0; 0x7f...fc) which improves our umax.
3162 __update_reg_bounds(false_reg
);
3163 __update_reg_bounds(true_reg
);
3166 /* Same as above, but for the case that dst_reg holds a constant and src_reg is
3169 static void reg_set_min_max_inv(struct bpf_reg_state
*true_reg
,
3170 struct bpf_reg_state
*false_reg
, u64 val
,
3173 if (__is_pointer_value(false, false_reg
))
3178 /* If this is false then we know nothing Jon Snow, but if it is
3179 * true then we know for sure.
3181 __mark_reg_known(true_reg
, val
);
3184 /* If this is true we know nothing Jon Snow, but if it is false
3185 * we know the value for sure;
3187 __mark_reg_known(false_reg
, val
);
3190 true_reg
->umax_value
= min(true_reg
->umax_value
, val
- 1);
3191 false_reg
->umin_value
= max(false_reg
->umin_value
, val
);
3194 true_reg
->smax_value
= min_t(s64
, true_reg
->smax_value
, val
- 1);
3195 false_reg
->smin_value
= max_t(s64
, false_reg
->smin_value
, val
);
3198 true_reg
->umin_value
= max(true_reg
->umin_value
, val
+ 1);
3199 false_reg
->umax_value
= min(false_reg
->umax_value
, val
);
3202 true_reg
->smin_value
= max_t(s64
, true_reg
->smin_value
, val
+ 1);
3203 false_reg
->smax_value
= min_t(s64
, false_reg
->smax_value
, val
);
3206 true_reg
->umax_value
= min(true_reg
->umax_value
, val
);
3207 false_reg
->umin_value
= max(false_reg
->umin_value
, val
+ 1);
3210 true_reg
->smax_value
= min_t(s64
, true_reg
->smax_value
, val
);
3211 false_reg
->smin_value
= max_t(s64
, false_reg
->smin_value
, val
+ 1);
3214 true_reg
->umin_value
= max(true_reg
->umin_value
, val
);
3215 false_reg
->umax_value
= min(false_reg
->umax_value
, val
- 1);
3218 true_reg
->smin_value
= max_t(s64
, true_reg
->smin_value
, val
);
3219 false_reg
->smax_value
= min_t(s64
, false_reg
->smax_value
, val
- 1);
3225 __reg_deduce_bounds(false_reg
);
3226 __reg_deduce_bounds(true_reg
);
3227 /* We might have learned some bits from the bounds. */
3228 __reg_bound_offset(false_reg
);
3229 __reg_bound_offset(true_reg
);
3230 /* Intersecting with the old var_off might have improved our bounds
3231 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3232 * then new var_off is (0; 0x7f...fc) which improves our umax.
3234 __update_reg_bounds(false_reg
);
3235 __update_reg_bounds(true_reg
);
3238 /* Regs are known to be equal, so intersect their min/max/var_off */
3239 static void __reg_combine_min_max(struct bpf_reg_state
*src_reg
,
3240 struct bpf_reg_state
*dst_reg
)
3242 src_reg
->umin_value
= dst_reg
->umin_value
= max(src_reg
->umin_value
,
3243 dst_reg
->umin_value
);
3244 src_reg
->umax_value
= dst_reg
->umax_value
= min(src_reg
->umax_value
,
3245 dst_reg
->umax_value
);
3246 src_reg
->smin_value
= dst_reg
->smin_value
= max(src_reg
->smin_value
,
3247 dst_reg
->smin_value
);
3248 src_reg
->smax_value
= dst_reg
->smax_value
= min(src_reg
->smax_value
,
3249 dst_reg
->smax_value
);
3250 src_reg
->var_off
= dst_reg
->var_off
= tnum_intersect(src_reg
->var_off
,
3252 /* We might have learned new bounds from the var_off. */
3253 __update_reg_bounds(src_reg
);
3254 __update_reg_bounds(dst_reg
);
3255 /* We might have learned something about the sign bit. */
3256 __reg_deduce_bounds(src_reg
);
3257 __reg_deduce_bounds(dst_reg
);
3258 /* We might have learned some bits from the bounds. */
3259 __reg_bound_offset(src_reg
);
3260 __reg_bound_offset(dst_reg
);
3261 /* Intersecting with the old var_off might have improved our bounds
3262 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3263 * then new var_off is (0; 0x7f...fc) which improves our umax.
3265 __update_reg_bounds(src_reg
);
3266 __update_reg_bounds(dst_reg
);
3269 static void reg_combine_min_max(struct bpf_reg_state
*true_src
,
3270 struct bpf_reg_state
*true_dst
,
3271 struct bpf_reg_state
*false_src
,
3272 struct bpf_reg_state
*false_dst
,
3277 __reg_combine_min_max(true_src
, true_dst
);
3280 __reg_combine_min_max(false_src
, false_dst
);
3285 static void mark_map_reg(struct bpf_reg_state
*regs
, u32 regno
, u32 id
,
3288 struct bpf_reg_state
*reg
= ®s
[regno
];
3290 if (reg
->type
== PTR_TO_MAP_VALUE_OR_NULL
&& reg
->id
== id
) {
3291 /* Old offset (both fixed and variable parts) should
3292 * have been known-zero, because we don't allow pointer
3293 * arithmetic on pointers that might be NULL.
3295 if (WARN_ON_ONCE(reg
->smin_value
|| reg
->smax_value
||
3296 !tnum_equals_const(reg
->var_off
, 0) ||
3298 __mark_reg_known_zero(reg
);
3302 reg
->type
= SCALAR_VALUE
;
3303 } else if (reg
->map_ptr
->inner_map_meta
) {
3304 reg
->type
= CONST_PTR_TO_MAP
;
3305 reg
->map_ptr
= reg
->map_ptr
->inner_map_meta
;
3307 reg
->type
= PTR_TO_MAP_VALUE
;
3309 /* We don't need id from this point onwards anymore, thus we
3310 * should better reset it, so that state pruning has chances
3317 /* The logic is similar to find_good_pkt_pointers(), both could eventually
3318 * be folded together at some point.
3320 static void mark_map_regs(struct bpf_verifier_state
*vstate
, u32 regno
,
3323 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
3324 struct bpf_reg_state
*regs
= state
->regs
;
3325 u32 id
= regs
[regno
].id
;
3328 for (i
= 0; i
< MAX_BPF_REG
; i
++)
3329 mark_map_reg(regs
, i
, id
, is_null
);
3331 for (j
= 0; j
<= vstate
->curframe
; j
++) {
3332 state
= vstate
->frame
[j
];
3333 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
3334 if (state
->stack
[i
].slot_type
[0] != STACK_SPILL
)
3336 mark_map_reg(&state
->stack
[i
].spilled_ptr
, 0, id
, is_null
);
3341 static bool try_match_pkt_pointers(const struct bpf_insn
*insn
,
3342 struct bpf_reg_state
*dst_reg
,
3343 struct bpf_reg_state
*src_reg
,
3344 struct bpf_verifier_state
*this_branch
,
3345 struct bpf_verifier_state
*other_branch
)
3347 if (BPF_SRC(insn
->code
) != BPF_X
)
3350 switch (BPF_OP(insn
->code
)) {
3352 if ((dst_reg
->type
== PTR_TO_PACKET
&&
3353 src_reg
->type
== PTR_TO_PACKET_END
) ||
3354 (dst_reg
->type
== PTR_TO_PACKET_META
&&
3355 reg_is_init_pkt_pointer(src_reg
, PTR_TO_PACKET
))) {
3356 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
3357 find_good_pkt_pointers(this_branch
, dst_reg
,
3358 dst_reg
->type
, false);
3359 } else if ((dst_reg
->type
== PTR_TO_PACKET_END
&&
3360 src_reg
->type
== PTR_TO_PACKET
) ||
3361 (reg_is_init_pkt_pointer(dst_reg
, PTR_TO_PACKET
) &&
3362 src_reg
->type
== PTR_TO_PACKET_META
)) {
3363 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
3364 find_good_pkt_pointers(other_branch
, src_reg
,
3365 src_reg
->type
, true);
3371 if ((dst_reg
->type
== PTR_TO_PACKET
&&
3372 src_reg
->type
== PTR_TO_PACKET_END
) ||
3373 (dst_reg
->type
== PTR_TO_PACKET_META
&&
3374 reg_is_init_pkt_pointer(src_reg
, PTR_TO_PACKET
))) {
3375 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
3376 find_good_pkt_pointers(other_branch
, dst_reg
,
3377 dst_reg
->type
, true);
3378 } else if ((dst_reg
->type
== PTR_TO_PACKET_END
&&
3379 src_reg
->type
== PTR_TO_PACKET
) ||
3380 (reg_is_init_pkt_pointer(dst_reg
, PTR_TO_PACKET
) &&
3381 src_reg
->type
== PTR_TO_PACKET_META
)) {
3382 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
3383 find_good_pkt_pointers(this_branch
, src_reg
,
3384 src_reg
->type
, false);
3390 if ((dst_reg
->type
== PTR_TO_PACKET
&&
3391 src_reg
->type
== PTR_TO_PACKET_END
) ||
3392 (dst_reg
->type
== PTR_TO_PACKET_META
&&
3393 reg_is_init_pkt_pointer(src_reg
, PTR_TO_PACKET
))) {
3394 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
3395 find_good_pkt_pointers(this_branch
, dst_reg
,
3396 dst_reg
->type
, true);
3397 } else if ((dst_reg
->type
== PTR_TO_PACKET_END
&&
3398 src_reg
->type
== PTR_TO_PACKET
) ||
3399 (reg_is_init_pkt_pointer(dst_reg
, PTR_TO_PACKET
) &&
3400 src_reg
->type
== PTR_TO_PACKET_META
)) {
3401 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
3402 find_good_pkt_pointers(other_branch
, src_reg
,
3403 src_reg
->type
, false);
3409 if ((dst_reg
->type
== PTR_TO_PACKET
&&
3410 src_reg
->type
== PTR_TO_PACKET_END
) ||
3411 (dst_reg
->type
== PTR_TO_PACKET_META
&&
3412 reg_is_init_pkt_pointer(src_reg
, PTR_TO_PACKET
))) {
3413 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
3414 find_good_pkt_pointers(other_branch
, dst_reg
,
3415 dst_reg
->type
, false);
3416 } else if ((dst_reg
->type
== PTR_TO_PACKET_END
&&
3417 src_reg
->type
== PTR_TO_PACKET
) ||
3418 (reg_is_init_pkt_pointer(dst_reg
, PTR_TO_PACKET
) &&
3419 src_reg
->type
== PTR_TO_PACKET_META
)) {
3420 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
3421 find_good_pkt_pointers(this_branch
, src_reg
,
3422 src_reg
->type
, true);
3434 static int check_cond_jmp_op(struct bpf_verifier_env
*env
,
3435 struct bpf_insn
*insn
, int *insn_idx
)
3437 struct bpf_verifier_state
*this_branch
= env
->cur_state
;
3438 struct bpf_verifier_state
*other_branch
;
3439 struct bpf_reg_state
*regs
= this_branch
->frame
[this_branch
->curframe
]->regs
;
3440 struct bpf_reg_state
*dst_reg
, *other_branch_regs
;
3441 u8 opcode
= BPF_OP(insn
->code
);
3444 if (opcode
> BPF_JSLE
) {
3445 verbose(env
, "invalid BPF_JMP opcode %x\n", opcode
);
3449 if (BPF_SRC(insn
->code
) == BPF_X
) {
3450 if (insn
->imm
!= 0) {
3451 verbose(env
, "BPF_JMP uses reserved fields\n");
3455 /* check src1 operand */
3456 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
3460 if (is_pointer_value(env
, insn
->src_reg
)) {
3461 verbose(env
, "R%d pointer comparison prohibited\n",
3466 if (insn
->src_reg
!= BPF_REG_0
) {
3467 verbose(env
, "BPF_JMP uses reserved fields\n");
3472 /* check src2 operand */
3473 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
3477 dst_reg
= ®s
[insn
->dst_reg
];
3479 /* detect if R == 0 where R was initialized to zero earlier */
3480 if (BPF_SRC(insn
->code
) == BPF_K
&&
3481 (opcode
== BPF_JEQ
|| opcode
== BPF_JNE
) &&
3482 dst_reg
->type
== SCALAR_VALUE
&&
3483 tnum_is_const(dst_reg
->var_off
)) {
3484 if ((opcode
== BPF_JEQ
&& dst_reg
->var_off
.value
== insn
->imm
) ||
3485 (opcode
== BPF_JNE
&& dst_reg
->var_off
.value
!= insn
->imm
)) {
3486 /* if (imm == imm) goto pc+off;
3487 * only follow the goto, ignore fall-through
3489 *insn_idx
+= insn
->off
;
3492 /* if (imm != imm) goto pc+off;
3493 * only follow fall-through branch, since
3494 * that's where the program will go
3500 other_branch
= push_stack(env
, *insn_idx
+ insn
->off
+ 1, *insn_idx
);
3503 other_branch_regs
= other_branch
->frame
[other_branch
->curframe
]->regs
;
3505 /* detect if we are comparing against a constant value so we can adjust
3506 * our min/max values for our dst register.
3507 * this is only legit if both are scalars (or pointers to the same
3508 * object, I suppose, but we don't support that right now), because
3509 * otherwise the different base pointers mean the offsets aren't
3512 if (BPF_SRC(insn
->code
) == BPF_X
) {
3513 if (dst_reg
->type
== SCALAR_VALUE
&&
3514 regs
[insn
->src_reg
].type
== SCALAR_VALUE
) {
3515 if (tnum_is_const(regs
[insn
->src_reg
].var_off
))
3516 reg_set_min_max(&other_branch_regs
[insn
->dst_reg
],
3517 dst_reg
, regs
[insn
->src_reg
].var_off
.value
,
3519 else if (tnum_is_const(dst_reg
->var_off
))
3520 reg_set_min_max_inv(&other_branch_regs
[insn
->src_reg
],
3521 ®s
[insn
->src_reg
],
3522 dst_reg
->var_off
.value
, opcode
);
3523 else if (opcode
== BPF_JEQ
|| opcode
== BPF_JNE
)
3524 /* Comparing for equality, we can combine knowledge */
3525 reg_combine_min_max(&other_branch_regs
[insn
->src_reg
],
3526 &other_branch_regs
[insn
->dst_reg
],
3527 ®s
[insn
->src_reg
],
3528 ®s
[insn
->dst_reg
], opcode
);
3530 } else if (dst_reg
->type
== SCALAR_VALUE
) {
3531 reg_set_min_max(&other_branch_regs
[insn
->dst_reg
],
3532 dst_reg
, insn
->imm
, opcode
);
3535 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
3536 if (BPF_SRC(insn
->code
) == BPF_K
&&
3537 insn
->imm
== 0 && (opcode
== BPF_JEQ
|| opcode
== BPF_JNE
) &&
3538 dst_reg
->type
== PTR_TO_MAP_VALUE_OR_NULL
) {
3539 /* Mark all identical map registers in each branch as either
3540 * safe or unknown depending R == 0 or R != 0 conditional.
3542 mark_map_regs(this_branch
, insn
->dst_reg
, opcode
== BPF_JNE
);
3543 mark_map_regs(other_branch
, insn
->dst_reg
, opcode
== BPF_JEQ
);
3544 } else if (!try_match_pkt_pointers(insn
, dst_reg
, ®s
[insn
->src_reg
],
3545 this_branch
, other_branch
) &&
3546 is_pointer_value(env
, insn
->dst_reg
)) {
3547 verbose(env
, "R%d pointer comparison prohibited\n",
3552 print_verifier_state(env
, this_branch
->frame
[this_branch
->curframe
]);
3556 /* return the map pointer stored inside BPF_LD_IMM64 instruction */
3557 static struct bpf_map
*ld_imm64_to_map_ptr(struct bpf_insn
*insn
)
3559 u64 imm64
= ((u64
) (u32
) insn
[0].imm
) | ((u64
) (u32
) insn
[1].imm
) << 32;
3561 return (struct bpf_map
*) (unsigned long) imm64
;
3564 /* verify BPF_LD_IMM64 instruction */
3565 static int check_ld_imm(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
3567 struct bpf_reg_state
*regs
= cur_regs(env
);
3570 if (BPF_SIZE(insn
->code
) != BPF_DW
) {
3571 verbose(env
, "invalid BPF_LD_IMM insn\n");
3574 if (insn
->off
!= 0) {
3575 verbose(env
, "BPF_LD_IMM64 uses reserved fields\n");
3579 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP
);
3583 if (insn
->src_reg
== 0) {
3584 u64 imm
= ((u64
)(insn
+ 1)->imm
<< 32) | (u32
)insn
->imm
;
3586 regs
[insn
->dst_reg
].type
= SCALAR_VALUE
;
3587 __mark_reg_known(®s
[insn
->dst_reg
], imm
);
3591 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
3592 BUG_ON(insn
->src_reg
!= BPF_PSEUDO_MAP_FD
);
3594 regs
[insn
->dst_reg
].type
= CONST_PTR_TO_MAP
;
3595 regs
[insn
->dst_reg
].map_ptr
= ld_imm64_to_map_ptr(insn
);
3599 static bool may_access_skb(enum bpf_prog_type type
)
3602 case BPF_PROG_TYPE_SOCKET_FILTER
:
3603 case BPF_PROG_TYPE_SCHED_CLS
:
3604 case BPF_PROG_TYPE_SCHED_ACT
:
3611 /* verify safety of LD_ABS|LD_IND instructions:
3612 * - they can only appear in the programs where ctx == skb
3613 * - since they are wrappers of function calls, they scratch R1-R5 registers,
3614 * preserve R6-R9, and store return value into R0
3617 * ctx == skb == R6 == CTX
3620 * SRC == any register
3621 * IMM == 32-bit immediate
3624 * R0 - 8/16/32-bit skb data converted to cpu endianness
3626 static int check_ld_abs(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
3628 struct bpf_reg_state
*regs
= cur_regs(env
);
3629 u8 mode
= BPF_MODE(insn
->code
);
3632 if (!may_access_skb(env
->prog
->type
)) {
3633 verbose(env
, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
3637 if (env
->subprog_cnt
) {
3638 /* when program has LD_ABS insn JITs and interpreter assume
3639 * that r1 == ctx == skb which is not the case for callees
3640 * that can have arbitrary arguments. It's problematic
3641 * for main prog as well since JITs would need to analyze
3642 * all functions in order to make proper register save/restore
3643 * decisions in the main prog. Hence disallow LD_ABS with calls
3645 verbose(env
, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
3649 if (insn
->dst_reg
!= BPF_REG_0
|| insn
->off
!= 0 ||
3650 BPF_SIZE(insn
->code
) == BPF_DW
||
3651 (mode
== BPF_ABS
&& insn
->src_reg
!= BPF_REG_0
)) {
3652 verbose(env
, "BPF_LD_[ABS|IND] uses reserved fields\n");
3656 /* check whether implicit source operand (register R6) is readable */
3657 err
= check_reg_arg(env
, BPF_REG_6
, SRC_OP
);
3661 if (regs
[BPF_REG_6
].type
!= PTR_TO_CTX
) {
3663 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
3667 if (mode
== BPF_IND
) {
3668 /* check explicit source operand */
3669 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
3674 /* reset caller saved regs to unreadable */
3675 for (i
= 0; i
< CALLER_SAVED_REGS
; i
++) {
3676 mark_reg_not_init(env
, regs
, caller_saved
[i
]);
3677 check_reg_arg(env
, caller_saved
[i
], DST_OP_NO_MARK
);
3680 /* mark destination R0 register as readable, since it contains
3681 * the value fetched from the packet.
3682 * Already marked as written above.
3684 mark_reg_unknown(env
, regs
, BPF_REG_0
);
3688 static int check_return_code(struct bpf_verifier_env
*env
)
3690 struct bpf_reg_state
*reg
;
3691 struct tnum range
= tnum_range(0, 1);
3693 switch (env
->prog
->type
) {
3694 case BPF_PROG_TYPE_CGROUP_SKB
:
3695 case BPF_PROG_TYPE_CGROUP_SOCK
:
3696 case BPF_PROG_TYPE_SOCK_OPS
:
3697 case BPF_PROG_TYPE_CGROUP_DEVICE
:
3703 reg
= cur_regs(env
) + BPF_REG_0
;
3704 if (reg
->type
!= SCALAR_VALUE
) {
3705 verbose(env
, "At program exit the register R0 is not a known value (%s)\n",
3706 reg_type_str
[reg
->type
]);
3710 if (!tnum_in(range
, reg
->var_off
)) {
3711 verbose(env
, "At program exit the register R0 ");
3712 if (!tnum_is_unknown(reg
->var_off
)) {
3715 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
3716 verbose(env
, "has value %s", tn_buf
);
3718 verbose(env
, "has unknown scalar value");
3720 verbose(env
, " should have been 0 or 1\n");
3726 /* non-recursive DFS pseudo code
3727 * 1 procedure DFS-iterative(G,v):
3728 * 2 label v as discovered
3729 * 3 let S be a stack
3731 * 5 while S is not empty
3733 * 7 if t is what we're looking for:
3735 * 9 for all edges e in G.adjacentEdges(t) do
3736 * 10 if edge e is already labelled
3737 * 11 continue with the next edge
3738 * 12 w <- G.adjacentVertex(t,e)
3739 * 13 if vertex w is not discovered and not explored
3740 * 14 label e as tree-edge
3741 * 15 label w as discovered
3744 * 18 else if vertex w is discovered
3745 * 19 label e as back-edge
3747 * 21 // vertex w is explored
3748 * 22 label e as forward- or cross-edge
3749 * 23 label t as explored
3754 * 0x11 - discovered and fall-through edge labelled
3755 * 0x12 - discovered and fall-through and branch edges labelled
3766 #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
3768 static int *insn_stack
; /* stack of insns to process */
3769 static int cur_stack
; /* current stack index */
3770 static int *insn_state
;
3772 /* t, w, e - match pseudo-code above:
3773 * t - index of current instruction
3774 * w - next instruction
3777 static int push_insn(int t
, int w
, int e
, struct bpf_verifier_env
*env
)
3779 if (e
== FALLTHROUGH
&& insn_state
[t
] >= (DISCOVERED
| FALLTHROUGH
))
3782 if (e
== BRANCH
&& insn_state
[t
] >= (DISCOVERED
| BRANCH
))
3785 if (w
< 0 || w
>= env
->prog
->len
) {
3786 verbose(env
, "jump out of range from insn %d to %d\n", t
, w
);
3791 /* mark branch target for state pruning */
3792 env
->explored_states
[w
] = STATE_LIST_MARK
;
3794 if (insn_state
[w
] == 0) {
3796 insn_state
[t
] = DISCOVERED
| e
;
3797 insn_state
[w
] = DISCOVERED
;
3798 if (cur_stack
>= env
->prog
->len
)
3800 insn_stack
[cur_stack
++] = w
;
3802 } else if ((insn_state
[w
] & 0xF0) == DISCOVERED
) {
3803 verbose(env
, "back-edge from insn %d to %d\n", t
, w
);
3805 } else if (insn_state
[w
] == EXPLORED
) {
3806 /* forward- or cross-edge */
3807 insn_state
[t
] = DISCOVERED
| e
;
3809 verbose(env
, "insn state internal bug\n");
3815 /* non-recursive depth-first-search to detect loops in BPF program
3816 * loop == back-edge in directed graph
3818 static int check_cfg(struct bpf_verifier_env
*env
)
3820 struct bpf_insn
*insns
= env
->prog
->insnsi
;
3821 int insn_cnt
= env
->prog
->len
;
3825 ret
= check_subprogs(env
);
3829 insn_state
= kcalloc(insn_cnt
, sizeof(int), GFP_KERNEL
);
3833 insn_stack
= kcalloc(insn_cnt
, sizeof(int), GFP_KERNEL
);
3839 insn_state
[0] = DISCOVERED
; /* mark 1st insn as discovered */
3840 insn_stack
[0] = 0; /* 0 is the first instruction */
3846 t
= insn_stack
[cur_stack
- 1];
3848 if (BPF_CLASS(insns
[t
].code
) == BPF_JMP
) {
3849 u8 opcode
= BPF_OP(insns
[t
].code
);
3851 if (opcode
== BPF_EXIT
) {
3853 } else if (opcode
== BPF_CALL
) {
3854 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
3859 if (t
+ 1 < insn_cnt
)
3860 env
->explored_states
[t
+ 1] = STATE_LIST_MARK
;
3861 if (insns
[t
].src_reg
== BPF_PSEUDO_CALL
) {
3862 env
->explored_states
[t
] = STATE_LIST_MARK
;
3863 ret
= push_insn(t
, t
+ insns
[t
].imm
+ 1, BRANCH
, env
);
3869 } else if (opcode
== BPF_JA
) {
3870 if (BPF_SRC(insns
[t
].code
) != BPF_K
) {
3874 /* unconditional jump with single edge */
3875 ret
= push_insn(t
, t
+ insns
[t
].off
+ 1,
3881 /* tell verifier to check for equivalent states
3882 * after every call and jump
3884 if (t
+ 1 < insn_cnt
)
3885 env
->explored_states
[t
+ 1] = STATE_LIST_MARK
;
3887 /* conditional jump with two edges */
3888 env
->explored_states
[t
] = STATE_LIST_MARK
;
3889 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
3895 ret
= push_insn(t
, t
+ insns
[t
].off
+ 1, BRANCH
, env
);
3902 /* all other non-branch instructions with single
3905 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
3913 insn_state
[t
] = EXPLORED
;
3914 if (cur_stack
-- <= 0) {
3915 verbose(env
, "pop stack internal bug\n");
3922 for (i
= 0; i
< insn_cnt
; i
++) {
3923 if (insn_state
[i
] != EXPLORED
) {
3924 verbose(env
, "unreachable insn %d\n", i
);
3929 ret
= 0; /* cfg looks good */
3937 /* check %cur's range satisfies %old's */
3938 static bool range_within(struct bpf_reg_state
*old
,
3939 struct bpf_reg_state
*cur
)
3941 return old
->umin_value
<= cur
->umin_value
&&
3942 old
->umax_value
>= cur
->umax_value
&&
3943 old
->smin_value
<= cur
->smin_value
&&
3944 old
->smax_value
>= cur
->smax_value
;
3947 /* Maximum number of register states that can exist at once */
3948 #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
3954 /* If in the old state two registers had the same id, then they need to have
3955 * the same id in the new state as well. But that id could be different from
3956 * the old state, so we need to track the mapping from old to new ids.
3957 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
3958 * regs with old id 5 must also have new id 9 for the new state to be safe. But
3959 * regs with a different old id could still have new id 9, we don't care about
3961 * So we look through our idmap to see if this old id has been seen before. If
3962 * so, we require the new id to match; otherwise, we add the id pair to the map.
3964 static bool check_ids(u32 old_id
, u32 cur_id
, struct idpair
*idmap
)
3968 for (i
= 0; i
< ID_MAP_SIZE
; i
++) {
3969 if (!idmap
[i
].old
) {
3970 /* Reached an empty slot; haven't seen this id before */
3971 idmap
[i
].old
= old_id
;
3972 idmap
[i
].cur
= cur_id
;
3975 if (idmap
[i
].old
== old_id
)
3976 return idmap
[i
].cur
== cur_id
;
3978 /* We ran out of idmap slots, which should be impossible */
3983 /* Returns true if (rold safe implies rcur safe) */
3984 static bool regsafe(struct bpf_reg_state
*rold
, struct bpf_reg_state
*rcur
,
3985 struct idpair
*idmap
)
3989 if (!(rold
->live
& REG_LIVE_READ
))
3990 /* explored state didn't use this */
3993 equal
= memcmp(rold
, rcur
, offsetof(struct bpf_reg_state
, frameno
)) == 0;
3995 if (rold
->type
== PTR_TO_STACK
)
3996 /* two stack pointers are equal only if they're pointing to
3997 * the same stack frame, since fp-8 in foo != fp-8 in bar
3999 return equal
&& rold
->frameno
== rcur
->frameno
;
4004 if (rold
->type
== NOT_INIT
)
4005 /* explored state can't have used this */
4007 if (rcur
->type
== NOT_INIT
)
4009 switch (rold
->type
) {
4011 if (rcur
->type
== SCALAR_VALUE
) {
4012 /* new val must satisfy old val knowledge */
4013 return range_within(rold
, rcur
) &&
4014 tnum_in(rold
->var_off
, rcur
->var_off
);
4016 /* if we knew anything about the old value, we're not
4017 * equal, because we can't know anything about the
4018 * scalar value of the pointer in the new value.
4020 return rold
->umin_value
== 0 &&
4021 rold
->umax_value
== U64_MAX
&&
4022 rold
->smin_value
== S64_MIN
&&
4023 rold
->smax_value
== S64_MAX
&&
4024 tnum_is_unknown(rold
->var_off
);
4026 case PTR_TO_MAP_VALUE
:
4027 /* If the new min/max/var_off satisfy the old ones and
4028 * everything else matches, we are OK.
4029 * We don't care about the 'id' value, because nothing
4030 * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL)
4032 return memcmp(rold
, rcur
, offsetof(struct bpf_reg_state
, id
)) == 0 &&
4033 range_within(rold
, rcur
) &&
4034 tnum_in(rold
->var_off
, rcur
->var_off
);
4035 case PTR_TO_MAP_VALUE_OR_NULL
:
4036 /* a PTR_TO_MAP_VALUE could be safe to use as a
4037 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
4038 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
4039 * checked, doing so could have affected others with the same
4040 * id, and we can't check for that because we lost the id when
4041 * we converted to a PTR_TO_MAP_VALUE.
4043 if (rcur
->type
!= PTR_TO_MAP_VALUE_OR_NULL
)
4045 if (memcmp(rold
, rcur
, offsetof(struct bpf_reg_state
, id
)))
4047 /* Check our ids match any regs they're supposed to */
4048 return check_ids(rold
->id
, rcur
->id
, idmap
);
4049 case PTR_TO_PACKET_META
:
4051 if (rcur
->type
!= rold
->type
)
4053 /* We must have at least as much range as the old ptr
4054 * did, so that any accesses which were safe before are
4055 * still safe. This is true even if old range < old off,
4056 * since someone could have accessed through (ptr - k), or
4057 * even done ptr -= k in a register, to get a safe access.
4059 if (rold
->range
> rcur
->range
)
4061 /* If the offsets don't match, we can't trust our alignment;
4062 * nor can we be sure that we won't fall out of range.
4064 if (rold
->off
!= rcur
->off
)
4066 /* id relations must be preserved */
4067 if (rold
->id
&& !check_ids(rold
->id
, rcur
->id
, idmap
))
4069 /* new val must satisfy old val knowledge */
4070 return range_within(rold
, rcur
) &&
4071 tnum_in(rold
->var_off
, rcur
->var_off
);
4073 case CONST_PTR_TO_MAP
:
4074 case PTR_TO_PACKET_END
:
4075 /* Only valid matches are exact, which memcmp() above
4076 * would have accepted
4079 /* Don't know what's going on, just say it's not safe */
4083 /* Shouldn't get here; if we do, say it's not safe */
4088 static bool stacksafe(struct bpf_func_state
*old
,
4089 struct bpf_func_state
*cur
,
4090 struct idpair
*idmap
)
4094 /* if explored stack has more populated slots than current stack
4095 * such stacks are not equivalent
4097 if (old
->allocated_stack
> cur
->allocated_stack
)
4100 /* walk slots of the explored stack and ignore any additional
4101 * slots in the current stack, since explored(safe) state
4104 for (i
= 0; i
< old
->allocated_stack
; i
++) {
4105 spi
= i
/ BPF_REG_SIZE
;
4107 if (!(old
->stack
[spi
].spilled_ptr
.live
& REG_LIVE_READ
))
4108 /* explored state didn't use this */
4111 if (old
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
] == STACK_INVALID
)
4113 /* if old state was safe with misc data in the stack
4114 * it will be safe with zero-initialized stack.
4115 * The opposite is not true
4117 if (old
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
] == STACK_MISC
&&
4118 cur
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
] == STACK_ZERO
)
4120 if (old
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
] !=
4121 cur
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
])
4122 /* Ex: old explored (safe) state has STACK_SPILL in
4123 * this stack slot, but current has has STACK_MISC ->
4124 * this verifier states are not equivalent,
4125 * return false to continue verification of this path
4128 if (i
% BPF_REG_SIZE
)
4130 if (old
->stack
[spi
].slot_type
[0] != STACK_SPILL
)
4132 if (!regsafe(&old
->stack
[spi
].spilled_ptr
,
4133 &cur
->stack
[spi
].spilled_ptr
,
4135 /* when explored and current stack slot are both storing
4136 * spilled registers, check that stored pointers types
4137 * are the same as well.
4138 * Ex: explored safe path could have stored
4139 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
4140 * but current path has stored:
4141 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
4142 * such verifier states are not equivalent.
4143 * return false to continue verification of this path
4150 /* compare two verifier states
4152 * all states stored in state_list are known to be valid, since
4153 * verifier reached 'bpf_exit' instruction through them
4155 * this function is called when verifier exploring different branches of
4156 * execution popped from the state stack. If it sees an old state that has
4157 * more strict register state and more strict stack state then this execution
4158 * branch doesn't need to be explored further, since verifier already
4159 * concluded that more strict state leads to valid finish.
4161 * Therefore two states are equivalent if register state is more conservative
4162 * and explored stack state is more conservative than the current one.
4165 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
4166 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
4168 * In other words if current stack state (one being explored) has more
4169 * valid slots than old one that already passed validation, it means
4170 * the verifier can stop exploring and conclude that current state is valid too
4172 * Similarly with registers. If explored state has register type as invalid
4173 * whereas register type in current state is meaningful, it means that
4174 * the current state will reach 'bpf_exit' instruction safely
4176 static bool func_states_equal(struct bpf_func_state
*old
,
4177 struct bpf_func_state
*cur
)
4179 struct idpair
*idmap
;
4183 idmap
= kcalloc(ID_MAP_SIZE
, sizeof(struct idpair
), GFP_KERNEL
);
4184 /* If we failed to allocate the idmap, just say it's not safe */
4188 for (i
= 0; i
< MAX_BPF_REG
; i
++) {
4189 if (!regsafe(&old
->regs
[i
], &cur
->regs
[i
], idmap
))
4193 if (!stacksafe(old
, cur
, idmap
))
4201 static bool states_equal(struct bpf_verifier_env
*env
,
4202 struct bpf_verifier_state
*old
,
4203 struct bpf_verifier_state
*cur
)
4207 if (old
->curframe
!= cur
->curframe
)
4210 /* for states to be equal callsites have to be the same
4211 * and all frame states need to be equivalent
4213 for (i
= 0; i
<= old
->curframe
; i
++) {
4214 if (old
->frame
[i
]->callsite
!= cur
->frame
[i
]->callsite
)
4216 if (!func_states_equal(old
->frame
[i
], cur
->frame
[i
]))
4222 /* A write screens off any subsequent reads; but write marks come from the
4223 * straight-line code between a state and its parent. When we arrive at an
4224 * equivalent state (jump target or such) we didn't arrive by the straight-line
4225 * code, so read marks in the state must propagate to the parent regardless
4226 * of the state's write marks. That's what 'parent == state->parent' comparison
4227 * in mark_reg_read() and mark_stack_slot_read() is for.
4229 static int propagate_liveness(struct bpf_verifier_env
*env
,
4230 const struct bpf_verifier_state
*vstate
,
4231 struct bpf_verifier_state
*vparent
)
4233 int i
, frame
, err
= 0;
4234 struct bpf_func_state
*state
, *parent
;
4236 if (vparent
->curframe
!= vstate
->curframe
) {
4237 WARN(1, "propagate_live: parent frame %d current frame %d\n",
4238 vparent
->curframe
, vstate
->curframe
);
4241 /* Propagate read liveness of registers... */
4242 BUILD_BUG_ON(BPF_REG_FP
+ 1 != MAX_BPF_REG
);
4243 /* We don't need to worry about FP liveness because it's read-only */
4244 for (i
= 0; i
< BPF_REG_FP
; i
++) {
4245 if (vparent
->frame
[vparent
->curframe
]->regs
[i
].live
& REG_LIVE_READ
)
4247 if (vstate
->frame
[vstate
->curframe
]->regs
[i
].live
& REG_LIVE_READ
) {
4248 err
= mark_reg_read(env
, vstate
, vparent
, i
);
4254 /* ... and stack slots */
4255 for (frame
= 0; frame
<= vstate
->curframe
; frame
++) {
4256 state
= vstate
->frame
[frame
];
4257 parent
= vparent
->frame
[frame
];
4258 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
&&
4259 i
< parent
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
4260 if (parent
->stack
[i
].spilled_ptr
.live
& REG_LIVE_READ
)
4262 if (state
->stack
[i
].spilled_ptr
.live
& REG_LIVE_READ
)
4263 mark_stack_slot_read(env
, vstate
, vparent
, i
, frame
);
4269 static int is_state_visited(struct bpf_verifier_env
*env
, int insn_idx
)
4271 struct bpf_verifier_state_list
*new_sl
;
4272 struct bpf_verifier_state_list
*sl
;
4273 struct bpf_verifier_state
*cur
= env
->cur_state
;
4276 sl
= env
->explored_states
[insn_idx
];
4278 /* this 'insn_idx' instruction wasn't marked, so we will not
4279 * be doing state search here
4283 while (sl
!= STATE_LIST_MARK
) {
4284 if (states_equal(env
, &sl
->state
, cur
)) {
4285 /* reached equivalent register/stack state,
4287 * Registers read by the continuation are read by us.
4288 * If we have any write marks in env->cur_state, they
4289 * will prevent corresponding reads in the continuation
4290 * from reaching our parent (an explored_state). Our
4291 * own state will get the read marks recorded, but
4292 * they'll be immediately forgotten as we're pruning
4293 * this state and will pop a new one.
4295 err
= propagate_liveness(env
, &sl
->state
, cur
);
4303 /* there were no equivalent states, remember current one.
4304 * technically the current state is not proven to be safe yet,
4305 * but it will either reach outer most bpf_exit (which means it's safe)
4306 * or it will be rejected. Since there are no loops, we won't be
4307 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
4308 * again on the way to bpf_exit
4310 new_sl
= kzalloc(sizeof(struct bpf_verifier_state_list
), GFP_KERNEL
);
4314 /* add new state to the head of linked list */
4315 err
= copy_verifier_state(&new_sl
->state
, cur
);
4317 free_verifier_state(&new_sl
->state
, false);
4321 new_sl
->next
= env
->explored_states
[insn_idx
];
4322 env
->explored_states
[insn_idx
] = new_sl
;
4323 /* connect new state to parentage chain */
4324 cur
->parent
= &new_sl
->state
;
4325 /* clear write marks in current state: the writes we did are not writes
4326 * our child did, so they don't screen off its reads from us.
4327 * (There are no read marks in current state, because reads always mark
4328 * their parent and current state never has children yet. Only
4329 * explored_states can get read marks.)
4331 for (i
= 0; i
< BPF_REG_FP
; i
++)
4332 cur
->frame
[cur
->curframe
]->regs
[i
].live
= REG_LIVE_NONE
;
4334 /* all stack frames are accessible from callee, clear them all */
4335 for (j
= 0; j
<= cur
->curframe
; j
++) {
4336 struct bpf_func_state
*frame
= cur
->frame
[j
];
4338 for (i
= 0; i
< frame
->allocated_stack
/ BPF_REG_SIZE
; i
++)
4339 frame
->stack
[i
].spilled_ptr
.live
= REG_LIVE_NONE
;
4344 static int ext_analyzer_insn_hook(struct bpf_verifier_env
*env
,
4345 int insn_idx
, int prev_insn_idx
)
4347 if (env
->dev_ops
&& env
->dev_ops
->insn_hook
)
4348 return env
->dev_ops
->insn_hook(env
, insn_idx
, prev_insn_idx
);
4353 static int do_check(struct bpf_verifier_env
*env
)
4355 struct bpf_verifier_state
*state
;
4356 struct bpf_insn
*insns
= env
->prog
->insnsi
;
4357 struct bpf_reg_state
*regs
;
4358 int insn_cnt
= env
->prog
->len
, i
;
4359 int insn_idx
, prev_insn_idx
= 0;
4360 int insn_processed
= 0;
4361 bool do_print_state
= false;
4363 state
= kzalloc(sizeof(struct bpf_verifier_state
), GFP_KERNEL
);
4366 state
->curframe
= 0;
4367 state
->parent
= NULL
;
4368 state
->frame
[0] = kzalloc(sizeof(struct bpf_func_state
), GFP_KERNEL
);
4369 if (!state
->frame
[0]) {
4373 env
->cur_state
= state
;
4374 init_func_state(env
, state
->frame
[0],
4375 BPF_MAIN_FUNC
/* callsite */,
4377 0 /* subprogno, zero == main subprog */);
4380 struct bpf_insn
*insn
;
4384 if (insn_idx
>= insn_cnt
) {
4385 verbose(env
, "invalid insn idx %d insn_cnt %d\n",
4386 insn_idx
, insn_cnt
);
4390 insn
= &insns
[insn_idx
];
4391 class = BPF_CLASS(insn
->code
);
4393 if (++insn_processed
> BPF_COMPLEXITY_LIMIT_INSNS
) {
4395 "BPF program is too large. Processed %d insn\n",
4400 err
= is_state_visited(env
, insn_idx
);
4404 /* found equivalent state, can prune the search */
4405 if (env
->log
.level
) {
4407 verbose(env
, "\nfrom %d to %d: safe\n",
4408 prev_insn_idx
, insn_idx
);
4410 verbose(env
, "%d: safe\n", insn_idx
);
4412 goto process_bpf_exit
;
4418 if (env
->log
.level
> 1 || (env
->log
.level
&& do_print_state
)) {
4419 if (env
->log
.level
> 1)
4420 verbose(env
, "%d:", insn_idx
);
4422 verbose(env
, "\nfrom %d to %d:",
4423 prev_insn_idx
, insn_idx
);
4424 print_verifier_state(env
, state
->frame
[state
->curframe
]);
4425 do_print_state
= false;
4428 if (env
->log
.level
) {
4429 verbose(env
, "%d: ", insn_idx
);
4430 print_bpf_insn(verbose
, env
, insn
,
4431 env
->allow_ptr_leaks
);
4434 err
= ext_analyzer_insn_hook(env
, insn_idx
, prev_insn_idx
);
4438 regs
= cur_regs(env
);
4439 env
->insn_aux_data
[insn_idx
].seen
= true;
4440 if (class == BPF_ALU
|| class == BPF_ALU64
) {
4441 err
= check_alu_op(env
, insn
);
4445 } else if (class == BPF_LDX
) {
4446 enum bpf_reg_type
*prev_src_type
, src_reg_type
;
4448 /* check for reserved fields is already done */
4450 /* check src operand */
4451 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
4455 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP_NO_MARK
);
4459 src_reg_type
= regs
[insn
->src_reg
].type
;
4461 /* check that memory (src_reg + off) is readable,
4462 * the state of dst_reg will be updated by this func
4464 err
= check_mem_access(env
, insn_idx
, insn
->src_reg
, insn
->off
,
4465 BPF_SIZE(insn
->code
), BPF_READ
,
4470 prev_src_type
= &env
->insn_aux_data
[insn_idx
].ptr_type
;
4472 if (*prev_src_type
== NOT_INIT
) {
4474 * dst_reg = *(u32 *)(src_reg + off)
4475 * save type to validate intersecting paths
4477 *prev_src_type
= src_reg_type
;
4479 } else if (src_reg_type
!= *prev_src_type
&&
4480 (src_reg_type
== PTR_TO_CTX
||
4481 *prev_src_type
== PTR_TO_CTX
)) {
4482 /* ABuser program is trying to use the same insn
4483 * dst_reg = *(u32*) (src_reg + off)
4484 * with different pointer types:
4485 * src_reg == ctx in one branch and
4486 * src_reg == stack|map in some other branch.
4489 verbose(env
, "same insn cannot be used with different pointers\n");
4493 } else if (class == BPF_STX
) {
4494 enum bpf_reg_type
*prev_dst_type
, dst_reg_type
;
4496 if (BPF_MODE(insn
->code
) == BPF_XADD
) {
4497 err
= check_xadd(env
, insn_idx
, insn
);
4504 /* check src1 operand */
4505 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
4508 /* check src2 operand */
4509 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
4513 dst_reg_type
= regs
[insn
->dst_reg
].type
;
4515 /* check that memory (dst_reg + off) is writeable */
4516 err
= check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
4517 BPF_SIZE(insn
->code
), BPF_WRITE
,
4522 prev_dst_type
= &env
->insn_aux_data
[insn_idx
].ptr_type
;
4524 if (*prev_dst_type
== NOT_INIT
) {
4525 *prev_dst_type
= dst_reg_type
;
4526 } else if (dst_reg_type
!= *prev_dst_type
&&
4527 (dst_reg_type
== PTR_TO_CTX
||
4528 *prev_dst_type
== PTR_TO_CTX
)) {
4529 verbose(env
, "same insn cannot be used with different pointers\n");
4533 } else if (class == BPF_ST
) {
4534 if (BPF_MODE(insn
->code
) != BPF_MEM
||
4535 insn
->src_reg
!= BPF_REG_0
) {
4536 verbose(env
, "BPF_ST uses reserved fields\n");
4539 /* check src operand */
4540 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
4544 /* check that memory (dst_reg + off) is writeable */
4545 err
= check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
4546 BPF_SIZE(insn
->code
), BPF_WRITE
,
4551 } else if (class == BPF_JMP
) {
4552 u8 opcode
= BPF_OP(insn
->code
);
4554 if (opcode
== BPF_CALL
) {
4555 if (BPF_SRC(insn
->code
) != BPF_K
||
4557 (insn
->src_reg
!= BPF_REG_0
&&
4558 insn
->src_reg
!= BPF_PSEUDO_CALL
) ||
4559 insn
->dst_reg
!= BPF_REG_0
) {
4560 verbose(env
, "BPF_CALL uses reserved fields\n");
4564 if (insn
->src_reg
== BPF_PSEUDO_CALL
)
4565 err
= check_func_call(env
, insn
, &insn_idx
);
4567 err
= check_helper_call(env
, insn
->imm
, insn_idx
);
4571 } else if (opcode
== BPF_JA
) {
4572 if (BPF_SRC(insn
->code
) != BPF_K
||
4574 insn
->src_reg
!= BPF_REG_0
||
4575 insn
->dst_reg
!= BPF_REG_0
) {
4576 verbose(env
, "BPF_JA uses reserved fields\n");
4580 insn_idx
+= insn
->off
+ 1;
4583 } else if (opcode
== BPF_EXIT
) {
4584 if (BPF_SRC(insn
->code
) != BPF_K
||
4586 insn
->src_reg
!= BPF_REG_0
||
4587 insn
->dst_reg
!= BPF_REG_0
) {
4588 verbose(env
, "BPF_EXIT uses reserved fields\n");
4592 if (state
->curframe
) {
4593 /* exit from nested function */
4594 prev_insn_idx
= insn_idx
;
4595 err
= prepare_func_exit(env
, &insn_idx
);
4598 do_print_state
= true;
4602 /* eBPF calling convetion is such that R0 is used
4603 * to return the value from eBPF program.
4604 * Make sure that it's readable at this time
4605 * of bpf_exit, which means that program wrote
4606 * something into it earlier
4608 err
= check_reg_arg(env
, BPF_REG_0
, SRC_OP
);
4612 if (is_pointer_value(env
, BPF_REG_0
)) {
4613 verbose(env
, "R0 leaks addr as return value\n");
4617 err
= check_return_code(env
);
4621 err
= pop_stack(env
, &prev_insn_idx
, &insn_idx
);
4627 do_print_state
= true;
4631 err
= check_cond_jmp_op(env
, insn
, &insn_idx
);
4635 } else if (class == BPF_LD
) {
4636 u8 mode
= BPF_MODE(insn
->code
);
4638 if (mode
== BPF_ABS
|| mode
== BPF_IND
) {
4639 err
= check_ld_abs(env
, insn
);
4643 } else if (mode
== BPF_IMM
) {
4644 err
= check_ld_imm(env
, insn
);
4649 env
->insn_aux_data
[insn_idx
].seen
= true;
4651 verbose(env
, "invalid BPF_LD mode\n");
4655 verbose(env
, "unknown insn class %d\n", class);
4662 verbose(env
, "processed %d insns, stack depth ", insn_processed
);
4663 for (i
= 0; i
< env
->subprog_cnt
+ 1; i
++) {
4664 u32 depth
= env
->subprog_stack_depth
[i
];
4666 verbose(env
, "%d", depth
);
4667 if (i
+ 1 < env
->subprog_cnt
+ 1)
4671 env
->prog
->aux
->stack_depth
= env
->subprog_stack_depth
[0];
4675 static int check_map_prealloc(struct bpf_map
*map
)
4677 return (map
->map_type
!= BPF_MAP_TYPE_HASH
&&
4678 map
->map_type
!= BPF_MAP_TYPE_PERCPU_HASH
&&
4679 map
->map_type
!= BPF_MAP_TYPE_HASH_OF_MAPS
) ||
4680 !(map
->map_flags
& BPF_F_NO_PREALLOC
);
4683 static int check_map_prog_compatibility(struct bpf_verifier_env
*env
,
4684 struct bpf_map
*map
,
4685 struct bpf_prog
*prog
)
4688 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
4689 * preallocated hash maps, since doing memory allocation
4690 * in overflow_handler can crash depending on where nmi got
4693 if (prog
->type
== BPF_PROG_TYPE_PERF_EVENT
) {
4694 if (!check_map_prealloc(map
)) {
4695 verbose(env
, "perf_event programs can only use preallocated hash map\n");
4698 if (map
->inner_map_meta
&&
4699 !check_map_prealloc(map
->inner_map_meta
)) {
4700 verbose(env
, "perf_event programs can only use preallocated inner hash map\n");
4707 /* look for pseudo eBPF instructions that access map FDs and
4708 * replace them with actual map pointers
4710 static int replace_map_fd_with_map_ptr(struct bpf_verifier_env
*env
)
4712 struct bpf_insn
*insn
= env
->prog
->insnsi
;
4713 int insn_cnt
= env
->prog
->len
;
4716 err
= bpf_prog_calc_tag(env
->prog
);
4720 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
4721 if (BPF_CLASS(insn
->code
) == BPF_LDX
&&
4722 (BPF_MODE(insn
->code
) != BPF_MEM
|| insn
->imm
!= 0)) {
4723 verbose(env
, "BPF_LDX uses reserved fields\n");
4727 if (BPF_CLASS(insn
->code
) == BPF_STX
&&
4728 ((BPF_MODE(insn
->code
) != BPF_MEM
&&
4729 BPF_MODE(insn
->code
) != BPF_XADD
) || insn
->imm
!= 0)) {
4730 verbose(env
, "BPF_STX uses reserved fields\n");
4734 if (insn
[0].code
== (BPF_LD
| BPF_IMM
| BPF_DW
)) {
4735 struct bpf_map
*map
;
4738 if (i
== insn_cnt
- 1 || insn
[1].code
!= 0 ||
4739 insn
[1].dst_reg
!= 0 || insn
[1].src_reg
!= 0 ||
4741 verbose(env
, "invalid bpf_ld_imm64 insn\n");
4745 if (insn
->src_reg
== 0)
4746 /* valid generic load 64-bit imm */
4749 if (insn
->src_reg
!= BPF_PSEUDO_MAP_FD
) {
4751 "unrecognized bpf_ld_imm64 insn\n");
4755 f
= fdget(insn
->imm
);
4756 map
= __bpf_map_get(f
);
4758 verbose(env
, "fd %d is not pointing to valid bpf_map\n",
4760 return PTR_ERR(map
);
4763 err
= check_map_prog_compatibility(env
, map
, env
->prog
);
4769 /* store map pointer inside BPF_LD_IMM64 instruction */
4770 insn
[0].imm
= (u32
) (unsigned long) map
;
4771 insn
[1].imm
= ((u64
) (unsigned long) map
) >> 32;
4773 /* check whether we recorded this map already */
4774 for (j
= 0; j
< env
->used_map_cnt
; j
++)
4775 if (env
->used_maps
[j
] == map
) {
4780 if (env
->used_map_cnt
>= MAX_USED_MAPS
) {
4785 /* hold the map. If the program is rejected by verifier,
4786 * the map will be released by release_maps() or it
4787 * will be used by the valid program until it's unloaded
4788 * and all maps are released in free_bpf_prog_info()
4790 map
= bpf_map_inc(map
, false);
4793 return PTR_ERR(map
);
4795 env
->used_maps
[env
->used_map_cnt
++] = map
;
4804 /* now all pseudo BPF_LD_IMM64 instructions load valid
4805 * 'struct bpf_map *' into a register instead of user map_fd.
4806 * These pointers will be used later by verifier to validate map access.
4811 /* drop refcnt of maps used by the rejected program */
4812 static void release_maps(struct bpf_verifier_env
*env
)
4816 for (i
= 0; i
< env
->used_map_cnt
; i
++)
4817 bpf_map_put(env
->used_maps
[i
]);
4820 /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
4821 static void convert_pseudo_ld_imm64(struct bpf_verifier_env
*env
)
4823 struct bpf_insn
*insn
= env
->prog
->insnsi
;
4824 int insn_cnt
= env
->prog
->len
;
4827 for (i
= 0; i
< insn_cnt
; i
++, insn
++)
4828 if (insn
->code
== (BPF_LD
| BPF_IMM
| BPF_DW
))
4832 /* single env->prog->insni[off] instruction was replaced with the range
4833 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
4834 * [0, off) and [off, end) to new locations, so the patched range stays zero
4836 static int adjust_insn_aux_data(struct bpf_verifier_env
*env
, u32 prog_len
,
4839 struct bpf_insn_aux_data
*new_data
, *old_data
= env
->insn_aux_data
;
4844 new_data
= vzalloc(sizeof(struct bpf_insn_aux_data
) * prog_len
);
4847 memcpy(new_data
, old_data
, sizeof(struct bpf_insn_aux_data
) * off
);
4848 memcpy(new_data
+ off
+ cnt
- 1, old_data
+ off
,
4849 sizeof(struct bpf_insn_aux_data
) * (prog_len
- off
- cnt
+ 1));
4850 for (i
= off
; i
< off
+ cnt
- 1; i
++)
4851 new_data
[i
].seen
= true;
4852 env
->insn_aux_data
= new_data
;
4857 static void adjust_subprog_starts(struct bpf_verifier_env
*env
, u32 off
, u32 len
)
4863 for (i
= 0; i
< env
->subprog_cnt
; i
++) {
4864 if (env
->subprog_starts
[i
] < off
)
4866 env
->subprog_starts
[i
] += len
- 1;
4870 static struct bpf_prog
*bpf_patch_insn_data(struct bpf_verifier_env
*env
, u32 off
,
4871 const struct bpf_insn
*patch
, u32 len
)
4873 struct bpf_prog
*new_prog
;
4875 new_prog
= bpf_patch_insn_single(env
->prog
, off
, patch
, len
);
4878 if (adjust_insn_aux_data(env
, new_prog
->len
, off
, len
))
4880 adjust_subprog_starts(env
, off
, len
);
4884 /* The verifier does more data flow analysis than llvm and will not explore
4885 * branches that are dead at run time. Malicious programs can have dead code
4886 * too. Therefore replace all dead at-run-time code with nops.
4888 static void sanitize_dead_code(struct bpf_verifier_env
*env
)
4890 struct bpf_insn_aux_data
*aux_data
= env
->insn_aux_data
;
4891 struct bpf_insn nop
= BPF_MOV64_REG(BPF_REG_0
, BPF_REG_0
);
4892 struct bpf_insn
*insn
= env
->prog
->insnsi
;
4893 const int insn_cnt
= env
->prog
->len
;
4896 for (i
= 0; i
< insn_cnt
; i
++) {
4897 if (aux_data
[i
].seen
)
4899 memcpy(insn
+ i
, &nop
, sizeof(nop
));
4903 /* convert load instructions that access fields of 'struct __sk_buff'
4904 * into sequence of instructions that access fields of 'struct sk_buff'
4906 static int convert_ctx_accesses(struct bpf_verifier_env
*env
)
4908 const struct bpf_verifier_ops
*ops
= env
->ops
;
4909 int i
, cnt
, size
, ctx_field_size
, delta
= 0;
4910 const int insn_cnt
= env
->prog
->len
;
4911 struct bpf_insn insn_buf
[16], *insn
;
4912 struct bpf_prog
*new_prog
;
4913 enum bpf_access_type type
;
4914 bool is_narrower_load
;
4917 if (ops
->gen_prologue
) {
4918 cnt
= ops
->gen_prologue(insn_buf
, env
->seen_direct_write
,
4920 if (cnt
>= ARRAY_SIZE(insn_buf
)) {
4921 verbose(env
, "bpf verifier is misconfigured\n");
4924 new_prog
= bpf_patch_insn_data(env
, 0, insn_buf
, cnt
);
4928 env
->prog
= new_prog
;
4933 if (!ops
->convert_ctx_access
)
4936 insn
= env
->prog
->insnsi
+ delta
;
4938 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
4939 if (insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_B
) ||
4940 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_H
) ||
4941 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_W
) ||
4942 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_DW
))
4944 else if (insn
->code
== (BPF_STX
| BPF_MEM
| BPF_B
) ||
4945 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_H
) ||
4946 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_W
) ||
4947 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_DW
))
4952 if (env
->insn_aux_data
[i
+ delta
].ptr_type
!= PTR_TO_CTX
)
4955 ctx_field_size
= env
->insn_aux_data
[i
+ delta
].ctx_field_size
;
4956 size
= BPF_LDST_BYTES(insn
);
4958 /* If the read access is a narrower load of the field,
4959 * convert to a 4/8-byte load, to minimum program type specific
4960 * convert_ctx_access changes. If conversion is successful,
4961 * we will apply proper mask to the result.
4963 is_narrower_load
= size
< ctx_field_size
;
4964 if (is_narrower_load
) {
4965 u32 off
= insn
->off
;
4968 if (type
== BPF_WRITE
) {
4969 verbose(env
, "bpf verifier narrow ctx access misconfigured\n");
4974 if (ctx_field_size
== 4)
4976 else if (ctx_field_size
== 8)
4979 insn
->off
= off
& ~(ctx_field_size
- 1);
4980 insn
->code
= BPF_LDX
| BPF_MEM
| size_code
;
4984 cnt
= ops
->convert_ctx_access(type
, insn
, insn_buf
, env
->prog
,
4986 if (cnt
== 0 || cnt
>= ARRAY_SIZE(insn_buf
) ||
4987 (ctx_field_size
&& !target_size
)) {
4988 verbose(env
, "bpf verifier is misconfigured\n");
4992 if (is_narrower_load
&& size
< target_size
) {
4993 if (ctx_field_size
<= 4)
4994 insn_buf
[cnt
++] = BPF_ALU32_IMM(BPF_AND
, insn
->dst_reg
,
4995 (1 << size
* 8) - 1);
4997 insn_buf
[cnt
++] = BPF_ALU64_IMM(BPF_AND
, insn
->dst_reg
,
4998 (1 << size
* 8) - 1);
5001 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, insn_buf
, cnt
);
5007 /* keep walking new program and skip insns we just inserted */
5008 env
->prog
= new_prog
;
5009 insn
= new_prog
->insnsi
+ i
+ delta
;
5015 static int jit_subprogs(struct bpf_verifier_env
*env
)
5017 struct bpf_prog
*prog
= env
->prog
, **func
, *tmp
;
5018 int i
, j
, subprog_start
, subprog_end
= 0, len
, subprog
;
5019 struct bpf_insn
*insn
= prog
->insnsi
;
5023 if (env
->subprog_cnt
== 0)
5026 for (i
= 0; i
< prog
->len
; i
++, insn
++) {
5027 if (insn
->code
!= (BPF_JMP
| BPF_CALL
) ||
5028 insn
->src_reg
!= BPF_PSEUDO_CALL
)
5030 subprog
= find_subprog(env
, i
+ insn
->imm
+ 1);
5032 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
5036 /* temporarily remember subprog id inside insn instead of
5037 * aux_data, since next loop will split up all insns into funcs
5039 insn
->off
= subprog
+ 1;
5040 /* remember original imm in case JIT fails and fallback
5041 * to interpreter will be needed
5043 env
->insn_aux_data
[i
].call_imm
= insn
->imm
;
5044 /* point imm to __bpf_call_base+1 from JITs point of view */
5048 func
= kzalloc(sizeof(prog
) * (env
->subprog_cnt
+ 1), GFP_KERNEL
);
5052 for (i
= 0; i
<= env
->subprog_cnt
; i
++) {
5053 subprog_start
= subprog_end
;
5054 if (env
->subprog_cnt
== i
)
5055 subprog_end
= prog
->len
;
5057 subprog_end
= env
->subprog_starts
[i
];
5059 len
= subprog_end
- subprog_start
;
5060 func
[i
] = bpf_prog_alloc(bpf_prog_size(len
), GFP_USER
);
5063 memcpy(func
[i
]->insnsi
, &prog
->insnsi
[subprog_start
],
5064 len
* sizeof(struct bpf_insn
));
5066 func
[i
]->is_func
= 1;
5067 /* Use bpf_prog_F_tag to indicate functions in stack traces.
5068 * Long term would need debug info to populate names
5070 func
[i
]->aux
->name
[0] = 'F';
5071 func
[i
]->aux
->stack_depth
= env
->subprog_stack_depth
[i
];
5072 func
[i
]->jit_requested
= 1;
5073 func
[i
] = bpf_int_jit_compile(func
[i
]);
5074 if (!func
[i
]->jited
) {
5080 /* at this point all bpf functions were successfully JITed
5081 * now populate all bpf_calls with correct addresses and
5082 * run last pass of JIT
5084 for (i
= 0; i
<= env
->subprog_cnt
; i
++) {
5085 insn
= func
[i
]->insnsi
;
5086 for (j
= 0; j
< func
[i
]->len
; j
++, insn
++) {
5087 if (insn
->code
!= (BPF_JMP
| BPF_CALL
) ||
5088 insn
->src_reg
!= BPF_PSEUDO_CALL
)
5090 subprog
= insn
->off
;
5092 insn
->imm
= (u64 (*)(u64
, u64
, u64
, u64
, u64
))
5093 func
[subprog
]->bpf_func
-
5097 for (i
= 0; i
<= env
->subprog_cnt
; i
++) {
5098 old_bpf_func
= func
[i
]->bpf_func
;
5099 tmp
= bpf_int_jit_compile(func
[i
]);
5100 if (tmp
!= func
[i
] || func
[i
]->bpf_func
!= old_bpf_func
) {
5101 verbose(env
, "JIT doesn't support bpf-to-bpf calls\n");
5108 /* finally lock prog and jit images for all functions and
5111 for (i
= 0; i
<= env
->subprog_cnt
; i
++) {
5112 bpf_prog_lock_ro(func
[i
]);
5113 bpf_prog_kallsyms_add(func
[i
]);
5116 prog
->bpf_func
= func
[0]->bpf_func
;
5117 prog
->aux
->func
= func
;
5118 prog
->aux
->func_cnt
= env
->subprog_cnt
+ 1;
5121 for (i
= 0; i
<= env
->subprog_cnt
; i
++)
5123 bpf_jit_free(func
[i
]);
5125 /* cleanup main prog to be interpreted */
5126 prog
->jit_requested
= 0;
5127 for (i
= 0, insn
= prog
->insnsi
; i
< prog
->len
; i
++, insn
++) {
5128 if (insn
->code
!= (BPF_JMP
| BPF_CALL
) ||
5129 insn
->src_reg
!= BPF_PSEUDO_CALL
)
5132 insn
->imm
= env
->insn_aux_data
[i
].call_imm
;
5137 static int fixup_call_args(struct bpf_verifier_env
*env
)
5139 struct bpf_prog
*prog
= env
->prog
;
5140 struct bpf_insn
*insn
= prog
->insnsi
;
5143 if (env
->prog
->jit_requested
)
5144 if (jit_subprogs(env
) == 0)
5147 for (i
= 0; i
< prog
->len
; i
++, insn
++) {
5148 if (insn
->code
!= (BPF_JMP
| BPF_CALL
) ||
5149 insn
->src_reg
!= BPF_PSEUDO_CALL
)
5151 depth
= get_callee_stack_depth(env
, insn
, i
);
5154 bpf_patch_call_args(insn
, depth
);
5159 /* fixup insn->imm field of bpf_call instructions
5160 * and inline eligible helpers as explicit sequence of BPF instructions
5162 * this function is called after eBPF program passed verification
5164 static int fixup_bpf_calls(struct bpf_verifier_env
*env
)
5166 struct bpf_prog
*prog
= env
->prog
;
5167 struct bpf_insn
*insn
= prog
->insnsi
;
5168 const struct bpf_func_proto
*fn
;
5169 const int insn_cnt
= prog
->len
;
5170 struct bpf_insn insn_buf
[16];
5171 struct bpf_prog
*new_prog
;
5172 struct bpf_map
*map_ptr
;
5173 int i
, cnt
, delta
= 0;
5175 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
5176 if (insn
->code
!= (BPF_JMP
| BPF_CALL
))
5178 if (insn
->src_reg
== BPF_PSEUDO_CALL
)
5181 if (insn
->imm
== BPF_FUNC_get_route_realm
)
5182 prog
->dst_needed
= 1;
5183 if (insn
->imm
== BPF_FUNC_get_prandom_u32
)
5184 bpf_user_rnd_init_once();
5185 if (insn
->imm
== BPF_FUNC_override_return
)
5186 prog
->kprobe_override
= 1;
5187 if (insn
->imm
== BPF_FUNC_tail_call
) {
5188 /* If we tail call into other programs, we
5189 * cannot make any assumptions since they can
5190 * be replaced dynamically during runtime in
5191 * the program array.
5193 prog
->cb_access
= 1;
5194 env
->prog
->aux
->stack_depth
= MAX_BPF_STACK
;
5196 /* mark bpf_tail_call as different opcode to avoid
5197 * conditional branch in the interpeter for every normal
5198 * call and to prevent accidental JITing by JIT compiler
5199 * that doesn't support bpf_tail_call yet
5202 insn
->code
= BPF_JMP
| BPF_TAIL_CALL
;
5206 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
5207 * handlers are currently limited to 64 bit only.
5209 if (prog
->jit_requested
&& BITS_PER_LONG
== 64 &&
5210 insn
->imm
== BPF_FUNC_map_lookup_elem
) {
5211 map_ptr
= env
->insn_aux_data
[i
+ delta
].map_ptr
;
5212 if (map_ptr
== BPF_MAP_PTR_POISON
||
5213 !map_ptr
->ops
->map_gen_lookup
)
5214 goto patch_call_imm
;
5216 cnt
= map_ptr
->ops
->map_gen_lookup(map_ptr
, insn_buf
);
5217 if (cnt
== 0 || cnt
>= ARRAY_SIZE(insn_buf
)) {
5218 verbose(env
, "bpf verifier is misconfigured\n");
5222 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, insn_buf
,
5229 /* keep walking new program and skip insns we just inserted */
5230 env
->prog
= prog
= new_prog
;
5231 insn
= new_prog
->insnsi
+ i
+ delta
;
5235 if (insn
->imm
== BPF_FUNC_redirect_map
) {
5236 /* Note, we cannot use prog directly as imm as subsequent
5237 * rewrites would still change the prog pointer. The only
5238 * stable address we can use is aux, which also works with
5239 * prog clones during blinding.
5241 u64 addr
= (unsigned long)prog
->aux
;
5242 struct bpf_insn r4_ld
[] = {
5243 BPF_LD_IMM64(BPF_REG_4
, addr
),
5246 cnt
= ARRAY_SIZE(r4_ld
);
5248 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, r4_ld
, cnt
);
5253 env
->prog
= prog
= new_prog
;
5254 insn
= new_prog
->insnsi
+ i
+ delta
;
5257 fn
= env
->ops
->get_func_proto(insn
->imm
);
5258 /* all functions that have prototype and verifier allowed
5259 * programs to call them, must be real in-kernel functions
5263 "kernel subsystem misconfigured func %s#%d\n",
5264 func_id_name(insn
->imm
), insn
->imm
);
5267 insn
->imm
= fn
->func
- __bpf_call_base
;
5273 static void free_states(struct bpf_verifier_env
*env
)
5275 struct bpf_verifier_state_list
*sl
, *sln
;
5278 if (!env
->explored_states
)
5281 for (i
= 0; i
< env
->prog
->len
; i
++) {
5282 sl
= env
->explored_states
[i
];
5285 while (sl
!= STATE_LIST_MARK
) {
5287 free_verifier_state(&sl
->state
, false);
5293 kfree(env
->explored_states
);
5296 int bpf_check(struct bpf_prog
**prog
, union bpf_attr
*attr
)
5298 struct bpf_verifier_env
*env
;
5299 struct bpf_verifer_log
*log
;
5302 /* no program is valid */
5303 if (ARRAY_SIZE(bpf_verifier_ops
) == 0)
5306 /* 'struct bpf_verifier_env' can be global, but since it's not small,
5307 * allocate/free it every time bpf_check() is called
5309 env
= kzalloc(sizeof(struct bpf_verifier_env
), GFP_KERNEL
);
5314 env
->insn_aux_data
= vzalloc(sizeof(struct bpf_insn_aux_data
) *
5317 if (!env
->insn_aux_data
)
5320 env
->ops
= bpf_verifier_ops
[env
->prog
->type
];
5322 /* grab the mutex to protect few globals used by verifier */
5323 mutex_lock(&bpf_verifier_lock
);
5325 if (attr
->log_level
|| attr
->log_buf
|| attr
->log_size
) {
5326 /* user requested verbose verifier output
5327 * and supplied buffer to store the verification trace
5329 log
->level
= attr
->log_level
;
5330 log
->ubuf
= (char __user
*) (unsigned long) attr
->log_buf
;
5331 log
->len_total
= attr
->log_size
;
5334 /* log attributes have to be sane */
5335 if (log
->len_total
< 128 || log
->len_total
> UINT_MAX
>> 8 ||
5336 !log
->level
|| !log
->ubuf
)
5340 env
->strict_alignment
= !!(attr
->prog_flags
& BPF_F_STRICT_ALIGNMENT
);
5341 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
))
5342 env
->strict_alignment
= true;
5344 if (env
->prog
->aux
->offload
) {
5345 ret
= bpf_prog_offload_verifier_prep(env
);
5350 ret
= replace_map_fd_with_map_ptr(env
);
5352 goto skip_full_check
;
5354 env
->explored_states
= kcalloc(env
->prog
->len
,
5355 sizeof(struct bpf_verifier_state_list
*),
5358 if (!env
->explored_states
)
5359 goto skip_full_check
;
5361 env
->allow_ptr_leaks
= capable(CAP_SYS_ADMIN
);
5363 ret
= check_cfg(env
);
5365 goto skip_full_check
;
5367 ret
= do_check(env
);
5368 if (env
->cur_state
) {
5369 free_verifier_state(env
->cur_state
, true);
5370 env
->cur_state
= NULL
;
5374 while (!pop_stack(env
, NULL
, NULL
));
5378 sanitize_dead_code(env
);
5381 /* program is valid, convert *(u32*)(ctx + off) accesses */
5382 ret
= convert_ctx_accesses(env
);
5385 ret
= fixup_bpf_calls(env
);
5388 ret
= fixup_call_args(env
);
5390 if (log
->level
&& bpf_verifier_log_full(log
))
5392 if (log
->level
&& !log
->ubuf
) {
5394 goto err_release_maps
;
5397 if (ret
== 0 && env
->used_map_cnt
) {
5398 /* if program passed verifier, update used_maps in bpf_prog_info */
5399 env
->prog
->aux
->used_maps
= kmalloc_array(env
->used_map_cnt
,
5400 sizeof(env
->used_maps
[0]),
5403 if (!env
->prog
->aux
->used_maps
) {
5405 goto err_release_maps
;
5408 memcpy(env
->prog
->aux
->used_maps
, env
->used_maps
,
5409 sizeof(env
->used_maps
[0]) * env
->used_map_cnt
);
5410 env
->prog
->aux
->used_map_cnt
= env
->used_map_cnt
;
5412 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
5413 * bpf_ld_imm64 instructions
5415 convert_pseudo_ld_imm64(env
);
5419 if (!env
->prog
->aux
->used_maps
)
5420 /* if we didn't copy map pointers into bpf_prog_info, release
5421 * them now. Otherwise free_bpf_prog_info() will release them.
5426 mutex_unlock(&bpf_verifier_lock
);
5427 vfree(env
->insn_aux_data
);