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
, "function 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
++];
827 struct bpf_verifier_state
*skip_callee(struct bpf_verifier_env
*env
,
828 const struct bpf_verifier_state
*state
,
829 struct bpf_verifier_state
*parent
,
832 struct bpf_verifier_state
*tmp
= NULL
;
834 /* 'parent' could be a state of caller and
835 * 'state' could be a state of callee. In such case
836 * parent->curframe < state->curframe
837 * and it's ok for r1 - r5 registers
839 * 'parent' could be a callee's state after it bpf_exit-ed.
840 * In such case parent->curframe > state->curframe
841 * and it's ok for r0 only
843 if (parent
->curframe
== state
->curframe
||
844 (parent
->curframe
< state
->curframe
&&
845 regno
>= BPF_REG_1
&& regno
<= BPF_REG_5
) ||
846 (parent
->curframe
> state
->curframe
&&
850 if (parent
->curframe
> state
->curframe
&&
851 regno
>= BPF_REG_6
) {
852 /* for callee saved regs we have to skip the whole chain
853 * of states that belong to callee and mark as LIVE_READ
854 * the registers before the call
857 while (tmp
&& tmp
->curframe
!= state
->curframe
) {
868 verbose(env
, "verifier bug regno %d tmp %p\n", regno
, tmp
);
869 verbose(env
, "regno %d parent frame %d current frame %d\n",
870 regno
, parent
->curframe
, state
->curframe
);
874 static int mark_reg_read(struct bpf_verifier_env
*env
,
875 const struct bpf_verifier_state
*state
,
876 struct bpf_verifier_state
*parent
,
879 bool writes
= parent
== state
->parent
; /* Observe write marks */
881 if (regno
== BPF_REG_FP
)
882 /* We don't need to worry about FP liveness because it's read-only */
886 /* if read wasn't screened by an earlier write ... */
887 if (writes
&& state
->frame
[state
->curframe
]->regs
[regno
].live
& REG_LIVE_WRITTEN
)
889 parent
= skip_callee(env
, state
, parent
, regno
);
892 /* ... then we depend on parent's value */
893 parent
->frame
[parent
->curframe
]->regs
[regno
].live
|= REG_LIVE_READ
;
895 parent
= state
->parent
;
901 static int check_reg_arg(struct bpf_verifier_env
*env
, u32 regno
,
904 struct bpf_verifier_state
*vstate
= env
->cur_state
;
905 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
906 struct bpf_reg_state
*regs
= state
->regs
;
908 if (regno
>= MAX_BPF_REG
) {
909 verbose(env
, "R%d is invalid\n", regno
);
914 /* check whether register used as source operand can be read */
915 if (regs
[regno
].type
== NOT_INIT
) {
916 verbose(env
, "R%d !read_ok\n", regno
);
919 return mark_reg_read(env
, vstate
, vstate
->parent
, regno
);
921 /* check whether register used as dest operand can be written to */
922 if (regno
== BPF_REG_FP
) {
923 verbose(env
, "frame pointer is read only\n");
926 regs
[regno
].live
|= REG_LIVE_WRITTEN
;
928 mark_reg_unknown(env
, regs
, regno
);
933 static bool is_spillable_regtype(enum bpf_reg_type type
)
936 case PTR_TO_MAP_VALUE
:
937 case PTR_TO_MAP_VALUE_OR_NULL
:
941 case PTR_TO_PACKET_META
:
942 case PTR_TO_PACKET_END
:
943 case CONST_PTR_TO_MAP
:
950 /* Does this register contain a constant zero? */
951 static bool register_is_null(struct bpf_reg_state
*reg
)
953 return reg
->type
== SCALAR_VALUE
&& tnum_equals_const(reg
->var_off
, 0);
956 /* check_stack_read/write functions track spill/fill of registers,
957 * stack boundary and alignment are checked in check_mem_access()
959 static int check_stack_write(struct bpf_verifier_env
*env
,
960 struct bpf_func_state
*state
, /* func where register points to */
961 int off
, int size
, int value_regno
)
963 struct bpf_func_state
*cur
; /* state of the current function */
964 int i
, slot
= -off
- 1, spi
= slot
/ BPF_REG_SIZE
, err
;
965 enum bpf_reg_type type
;
967 err
= realloc_func_state(state
, round_up(slot
+ 1, BPF_REG_SIZE
),
971 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
972 * so it's aligned access and [off, off + size) are within stack limits
974 if (!env
->allow_ptr_leaks
&&
975 state
->stack
[spi
].slot_type
[0] == STACK_SPILL
&&
976 size
!= BPF_REG_SIZE
) {
977 verbose(env
, "attempt to corrupt spilled pointer on stack\n");
981 cur
= env
->cur_state
->frame
[env
->cur_state
->curframe
];
982 if (value_regno
>= 0 &&
983 is_spillable_regtype((type
= cur
->regs
[value_regno
].type
))) {
985 /* register containing pointer is being spilled into stack */
986 if (size
!= BPF_REG_SIZE
) {
987 verbose(env
, "invalid size of register spill\n");
991 if (state
!= cur
&& type
== PTR_TO_STACK
) {
992 verbose(env
, "cannot spill pointers to stack into stack frame of the caller\n");
996 /* save register state */
997 state
->stack
[spi
].spilled_ptr
= cur
->regs
[value_regno
];
998 state
->stack
[spi
].spilled_ptr
.live
|= REG_LIVE_WRITTEN
;
1000 for (i
= 0; i
< BPF_REG_SIZE
; i
++)
1001 state
->stack
[spi
].slot_type
[i
] = STACK_SPILL
;
1003 u8 type
= STACK_MISC
;
1005 /* regular write of data into stack */
1006 state
->stack
[spi
].spilled_ptr
= (struct bpf_reg_state
) {};
1008 /* only mark the slot as written if all 8 bytes were written
1009 * otherwise read propagation may incorrectly stop too soon
1010 * when stack slots are partially written.
1011 * This heuristic means that read propagation will be
1012 * conservative, since it will add reg_live_read marks
1013 * to stack slots all the way to first state when programs
1014 * writes+reads less than 8 bytes
1016 if (size
== BPF_REG_SIZE
)
1017 state
->stack
[spi
].spilled_ptr
.live
|= REG_LIVE_WRITTEN
;
1019 /* when we zero initialize stack slots mark them as such */
1020 if (value_regno
>= 0 &&
1021 register_is_null(&cur
->regs
[value_regno
]))
1024 for (i
= 0; i
< size
; i
++)
1025 state
->stack
[spi
].slot_type
[(slot
- i
) % BPF_REG_SIZE
] =
1031 /* registers of every function are unique and mark_reg_read() propagates
1032 * the liveness in the following cases:
1033 * - from callee into caller for R1 - R5 that were used as arguments
1034 * - from caller into callee for R0 that used as result of the call
1035 * - from caller to the same caller skipping states of the callee for R6 - R9,
1036 * since R6 - R9 are callee saved by implicit function prologue and
1037 * caller's R6 != callee's R6, so when we propagate liveness up to
1038 * parent states we need to skip callee states for R6 - R9.
1040 * stack slot marking is different, since stacks of caller and callee are
1041 * accessible in both (since caller can pass a pointer to caller's stack to
1042 * callee which can pass it to another function), hence mark_stack_slot_read()
1043 * has to propagate the stack liveness to all parent states at given frame number.
1053 * First *ptr is reading from f1's stack and mark_stack_slot_read() has
1054 * to mark liveness at the f1's frame and not f2's frame.
1055 * Second *ptr is also reading from f1's stack and mark_stack_slot_read() has
1056 * to propagate liveness to f2 states at f1's frame level and further into
1057 * f1 states at f1's frame level until write into that stack slot
1059 static void mark_stack_slot_read(struct bpf_verifier_env
*env
,
1060 const struct bpf_verifier_state
*state
,
1061 struct bpf_verifier_state
*parent
,
1062 int slot
, int frameno
)
1064 bool writes
= parent
== state
->parent
; /* Observe write marks */
1067 if (parent
->frame
[frameno
]->allocated_stack
<= slot
* BPF_REG_SIZE
)
1068 /* since LIVE_WRITTEN mark is only done for full 8-byte
1069 * write the read marks are conservative and parent
1070 * state may not even have the stack allocated. In such case
1071 * end the propagation, since the loop reached beginning
1075 /* if read wasn't screened by an earlier write ... */
1076 if (writes
&& state
->frame
[frameno
]->stack
[slot
].spilled_ptr
.live
& REG_LIVE_WRITTEN
)
1078 /* ... then we depend on parent's value */
1079 parent
->frame
[frameno
]->stack
[slot
].spilled_ptr
.live
|= REG_LIVE_READ
;
1081 parent
= state
->parent
;
1086 static int check_stack_read(struct bpf_verifier_env
*env
,
1087 struct bpf_func_state
*reg_state
/* func where register points to */,
1088 int off
, int size
, int value_regno
)
1090 struct bpf_verifier_state
*vstate
= env
->cur_state
;
1091 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
1092 int i
, slot
= -off
- 1, spi
= slot
/ BPF_REG_SIZE
;
1095 if (reg_state
->allocated_stack
<= slot
) {
1096 verbose(env
, "invalid read from stack off %d+0 size %d\n",
1100 stype
= reg_state
->stack
[spi
].slot_type
;
1102 if (stype
[0] == STACK_SPILL
) {
1103 if (size
!= BPF_REG_SIZE
) {
1104 verbose(env
, "invalid size of register spill\n");
1107 for (i
= 1; i
< BPF_REG_SIZE
; i
++) {
1108 if (stype
[(slot
- i
) % BPF_REG_SIZE
] != STACK_SPILL
) {
1109 verbose(env
, "corrupted spill memory\n");
1114 if (value_regno
>= 0) {
1115 /* restore register state from stack */
1116 state
->regs
[value_regno
] = reg_state
->stack
[spi
].spilled_ptr
;
1117 /* mark reg as written since spilled pointer state likely
1118 * has its liveness marks cleared by is_state_visited()
1119 * which resets stack/reg liveness for state transitions
1121 state
->regs
[value_regno
].live
|= REG_LIVE_WRITTEN
;
1123 mark_stack_slot_read(env
, vstate
, vstate
->parent
, spi
,
1124 reg_state
->frameno
);
1129 for (i
= 0; i
< size
; i
++) {
1130 if (stype
[(slot
- i
) % BPF_REG_SIZE
] == STACK_MISC
)
1132 if (stype
[(slot
- i
) % BPF_REG_SIZE
] == STACK_ZERO
) {
1136 verbose(env
, "invalid read from stack off %d+%d size %d\n",
1140 mark_stack_slot_read(env
, vstate
, vstate
->parent
, spi
,
1141 reg_state
->frameno
);
1142 if (value_regno
>= 0) {
1143 if (zeros
== size
) {
1144 /* any size read into register is zero extended,
1145 * so the whole register == const_zero
1147 __mark_reg_const_zero(&state
->regs
[value_regno
]);
1149 /* have read misc data from the stack */
1150 mark_reg_unknown(env
, state
->regs
, value_regno
);
1152 state
->regs
[value_regno
].live
|= REG_LIVE_WRITTEN
;
1158 /* check read/write into map element returned by bpf_map_lookup_elem() */
1159 static int __check_map_access(struct bpf_verifier_env
*env
, u32 regno
, int off
,
1160 int size
, bool zero_size_allowed
)
1162 struct bpf_reg_state
*regs
= cur_regs(env
);
1163 struct bpf_map
*map
= regs
[regno
].map_ptr
;
1165 if (off
< 0 || size
< 0 || (size
== 0 && !zero_size_allowed
) ||
1166 off
+ size
> map
->value_size
) {
1167 verbose(env
, "invalid access to map value, value_size=%d off=%d size=%d\n",
1168 map
->value_size
, off
, size
);
1174 /* check read/write into a map element with possible variable offset */
1175 static int check_map_access(struct bpf_verifier_env
*env
, u32 regno
,
1176 int off
, int size
, bool zero_size_allowed
)
1178 struct bpf_verifier_state
*vstate
= env
->cur_state
;
1179 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
1180 struct bpf_reg_state
*reg
= &state
->regs
[regno
];
1183 /* We may have adjusted the register to this map value, so we
1184 * need to try adding each of min_value and max_value to off
1185 * to make sure our theoretical access will be safe.
1188 print_verifier_state(env
, state
);
1189 /* The minimum value is only important with signed
1190 * comparisons where we can't assume the floor of a
1191 * value is 0. If we are using signed variables for our
1192 * index'es we need to make sure that whatever we use
1193 * will have a set floor within our range.
1195 if (reg
->smin_value
< 0) {
1196 verbose(env
, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1200 err
= __check_map_access(env
, regno
, reg
->smin_value
+ off
, size
,
1203 verbose(env
, "R%d min value is outside of the array range\n",
1208 /* If we haven't set a max value then we need to bail since we can't be
1209 * sure we won't do bad things.
1210 * If reg->umax_value + off could overflow, treat that as unbounded too.
1212 if (reg
->umax_value
>= BPF_MAX_VAR_OFF
) {
1213 verbose(env
, "R%d unbounded memory access, make sure to bounds check any array access into a map\n",
1217 err
= __check_map_access(env
, regno
, reg
->umax_value
+ off
, size
,
1220 verbose(env
, "R%d max value is outside of the array range\n",
1225 #define MAX_PACKET_OFF 0xffff
1227 static bool may_access_direct_pkt_data(struct bpf_verifier_env
*env
,
1228 const struct bpf_call_arg_meta
*meta
,
1229 enum bpf_access_type t
)
1231 switch (env
->prog
->type
) {
1232 case BPF_PROG_TYPE_LWT_IN
:
1233 case BPF_PROG_TYPE_LWT_OUT
:
1234 /* dst_input() and dst_output() can't write for now */
1238 case BPF_PROG_TYPE_SCHED_CLS
:
1239 case BPF_PROG_TYPE_SCHED_ACT
:
1240 case BPF_PROG_TYPE_XDP
:
1241 case BPF_PROG_TYPE_LWT_XMIT
:
1242 case BPF_PROG_TYPE_SK_SKB
:
1244 return meta
->pkt_access
;
1246 env
->seen_direct_write
= true;
1253 static int __check_packet_access(struct bpf_verifier_env
*env
, u32 regno
,
1254 int off
, int size
, bool zero_size_allowed
)
1256 struct bpf_reg_state
*regs
= cur_regs(env
);
1257 struct bpf_reg_state
*reg
= ®s
[regno
];
1259 if (off
< 0 || size
< 0 || (size
== 0 && !zero_size_allowed
) ||
1260 (u64
)off
+ size
> reg
->range
) {
1261 verbose(env
, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
1262 off
, size
, regno
, reg
->id
, reg
->off
, reg
->range
);
1268 static int check_packet_access(struct bpf_verifier_env
*env
, u32 regno
, int off
,
1269 int size
, bool zero_size_allowed
)
1271 struct bpf_reg_state
*regs
= cur_regs(env
);
1272 struct bpf_reg_state
*reg
= ®s
[regno
];
1275 /* We may have added a variable offset to the packet pointer; but any
1276 * reg->range we have comes after that. We are only checking the fixed
1280 /* We don't allow negative numbers, because we aren't tracking enough
1281 * detail to prove they're safe.
1283 if (reg
->smin_value
< 0) {
1284 verbose(env
, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1288 err
= __check_packet_access(env
, regno
, off
, size
, zero_size_allowed
);
1290 verbose(env
, "R%d offset is outside of the packet\n", regno
);
1296 /* check access to 'struct bpf_context' fields. Supports fixed offsets only */
1297 static int check_ctx_access(struct bpf_verifier_env
*env
, int insn_idx
, int off
, int size
,
1298 enum bpf_access_type t
, enum bpf_reg_type
*reg_type
)
1300 struct bpf_insn_access_aux info
= {
1301 .reg_type
= *reg_type
,
1304 if (env
->ops
->is_valid_access
&&
1305 env
->ops
->is_valid_access(off
, size
, t
, &info
)) {
1306 /* A non zero info.ctx_field_size indicates that this field is a
1307 * candidate for later verifier transformation to load the whole
1308 * field and then apply a mask when accessed with a narrower
1309 * access than actual ctx access size. A zero info.ctx_field_size
1310 * will only allow for whole field access and rejects any other
1311 * type of narrower access.
1313 *reg_type
= info
.reg_type
;
1315 env
->insn_aux_data
[insn_idx
].ctx_field_size
= info
.ctx_field_size
;
1316 /* remember the offset of last byte accessed in ctx */
1317 if (env
->prog
->aux
->max_ctx_offset
< off
+ size
)
1318 env
->prog
->aux
->max_ctx_offset
= off
+ size
;
1322 verbose(env
, "invalid bpf_context access off=%d size=%d\n", off
, size
);
1326 static bool __is_pointer_value(bool allow_ptr_leaks
,
1327 const struct bpf_reg_state
*reg
)
1329 if (allow_ptr_leaks
)
1332 return reg
->type
!= SCALAR_VALUE
;
1335 static bool is_pointer_value(struct bpf_verifier_env
*env
, int regno
)
1337 return __is_pointer_value(env
->allow_ptr_leaks
, cur_regs(env
) + regno
);
1340 static int check_pkt_ptr_alignment(struct bpf_verifier_env
*env
,
1341 const struct bpf_reg_state
*reg
,
1342 int off
, int size
, bool strict
)
1344 struct tnum reg_off
;
1347 /* Byte size accesses are always allowed. */
1348 if (!strict
|| size
== 1)
1351 /* For platforms that do not have a Kconfig enabling
1352 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1353 * NET_IP_ALIGN is universally set to '2'. And on platforms
1354 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1355 * to this code only in strict mode where we want to emulate
1356 * the NET_IP_ALIGN==2 checking. Therefore use an
1357 * unconditional IP align value of '2'.
1361 reg_off
= tnum_add(reg
->var_off
, tnum_const(ip_align
+ reg
->off
+ off
));
1362 if (!tnum_is_aligned(reg_off
, size
)) {
1365 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1367 "misaligned packet access off %d+%s+%d+%d size %d\n",
1368 ip_align
, tn_buf
, reg
->off
, off
, size
);
1375 static int check_generic_ptr_alignment(struct bpf_verifier_env
*env
,
1376 const struct bpf_reg_state
*reg
,
1377 const char *pointer_desc
,
1378 int off
, int size
, bool strict
)
1380 struct tnum reg_off
;
1382 /* Byte size accesses are always allowed. */
1383 if (!strict
|| size
== 1)
1386 reg_off
= tnum_add(reg
->var_off
, tnum_const(reg
->off
+ off
));
1387 if (!tnum_is_aligned(reg_off
, size
)) {
1390 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1391 verbose(env
, "misaligned %saccess off %s+%d+%d size %d\n",
1392 pointer_desc
, tn_buf
, reg
->off
, off
, size
);
1399 static int check_ptr_alignment(struct bpf_verifier_env
*env
,
1400 const struct bpf_reg_state
*reg
,
1403 bool strict
= env
->strict_alignment
;
1404 const char *pointer_desc
= "";
1406 switch (reg
->type
) {
1408 case PTR_TO_PACKET_META
:
1409 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1410 * right in front, treat it the very same way.
1412 return check_pkt_ptr_alignment(env
, reg
, off
, size
, strict
);
1413 case PTR_TO_MAP_VALUE
:
1414 pointer_desc
= "value ";
1417 pointer_desc
= "context ";
1420 pointer_desc
= "stack ";
1425 return check_generic_ptr_alignment(env
, reg
, pointer_desc
, off
, size
,
1429 static int update_stack_depth(struct bpf_verifier_env
*env
,
1430 const struct bpf_func_state
*func
,
1433 u16 stack
= env
->subprog_stack_depth
[func
->subprogno
], total
= 0;
1434 struct bpf_verifier_state
*cur
= env
->cur_state
;
1440 /* update known max for given subprogram */
1441 env
->subprog_stack_depth
[func
->subprogno
] = -off
;
1443 /* compute the total for current call chain */
1444 for (i
= 0; i
<= cur
->curframe
; i
++) {
1445 u32 depth
= env
->subprog_stack_depth
[cur
->frame
[i
]->subprogno
];
1447 /* round up to 32-bytes, since this is granularity
1448 * of interpreter stack sizes
1450 depth
= round_up(depth
, 32);
1454 if (total
> MAX_BPF_STACK
) {
1455 verbose(env
, "combined stack size of %d calls is %d. Too large\n",
1456 cur
->curframe
, total
);
1462 static int get_callee_stack_depth(struct bpf_verifier_env
*env
,
1463 const struct bpf_insn
*insn
, int idx
)
1465 int start
= idx
+ insn
->imm
+ 1, subprog
;
1467 subprog
= find_subprog(env
, start
);
1469 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1474 return env
->subprog_stack_depth
[subprog
];
1477 /* check whether memory at (regno + off) is accessible for t = (read | write)
1478 * if t==write, value_regno is a register which value is stored into memory
1479 * if t==read, value_regno is a register which will receive the value from memory
1480 * if t==write && value_regno==-1, some unknown value is stored into memory
1481 * if t==read && value_regno==-1, don't care what we read from memory
1483 static int check_mem_access(struct bpf_verifier_env
*env
, int insn_idx
, u32 regno
, int off
,
1484 int bpf_size
, enum bpf_access_type t
,
1487 struct bpf_reg_state
*regs
= cur_regs(env
);
1488 struct bpf_reg_state
*reg
= regs
+ regno
;
1489 struct bpf_func_state
*state
;
1492 size
= bpf_size_to_bytes(bpf_size
);
1496 /* alignment checks will add in reg->off themselves */
1497 err
= check_ptr_alignment(env
, reg
, off
, size
);
1501 /* for access checks, reg->off is just part of off */
1504 if (reg
->type
== PTR_TO_MAP_VALUE
) {
1505 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
1506 is_pointer_value(env
, value_regno
)) {
1507 verbose(env
, "R%d leaks addr into map\n", value_regno
);
1511 err
= check_map_access(env
, regno
, off
, size
, false);
1512 if (!err
&& t
== BPF_READ
&& value_regno
>= 0)
1513 mark_reg_unknown(env
, regs
, value_regno
);
1515 } else if (reg
->type
== PTR_TO_CTX
) {
1516 enum bpf_reg_type reg_type
= SCALAR_VALUE
;
1518 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
1519 is_pointer_value(env
, value_regno
)) {
1520 verbose(env
, "R%d leaks addr into ctx\n", value_regno
);
1523 /* ctx accesses must be at a fixed offset, so that we can
1524 * determine what type of data were returned.
1528 "dereference of modified ctx ptr R%d off=%d+%d, ctx+const is allowed, ctx+const+const is not\n",
1529 regno
, reg
->off
, off
- reg
->off
);
1532 if (!tnum_is_const(reg
->var_off
) || reg
->var_off
.value
) {
1535 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1537 "variable ctx access var_off=%s off=%d size=%d",
1541 err
= check_ctx_access(env
, insn_idx
, off
, size
, t
, ®_type
);
1542 if (!err
&& t
== BPF_READ
&& value_regno
>= 0) {
1543 /* ctx access returns either a scalar, or a
1544 * PTR_TO_PACKET[_META,_END]. In the latter
1545 * case, we know the offset is zero.
1547 if (reg_type
== SCALAR_VALUE
)
1548 mark_reg_unknown(env
, regs
, value_regno
);
1550 mark_reg_known_zero(env
, regs
,
1552 regs
[value_regno
].id
= 0;
1553 regs
[value_regno
].off
= 0;
1554 regs
[value_regno
].range
= 0;
1555 regs
[value_regno
].type
= reg_type
;
1558 } else if (reg
->type
== PTR_TO_STACK
) {
1559 /* stack accesses must be at a fixed offset, so that we can
1560 * determine what type of data were returned.
1561 * See check_stack_read().
1563 if (!tnum_is_const(reg
->var_off
)) {
1566 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1567 verbose(env
, "variable stack access var_off=%s off=%d size=%d",
1571 off
+= reg
->var_off
.value
;
1572 if (off
>= 0 || off
< -MAX_BPF_STACK
) {
1573 verbose(env
, "invalid stack off=%d size=%d\n", off
,
1578 state
= func(env
, reg
);
1579 err
= update_stack_depth(env
, state
, off
);
1584 err
= check_stack_write(env
, state
, off
, size
,
1587 err
= check_stack_read(env
, state
, off
, size
,
1589 } else if (reg_is_pkt_pointer(reg
)) {
1590 if (t
== BPF_WRITE
&& !may_access_direct_pkt_data(env
, NULL
, t
)) {
1591 verbose(env
, "cannot write into packet\n");
1594 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
1595 is_pointer_value(env
, value_regno
)) {
1596 verbose(env
, "R%d leaks addr into packet\n",
1600 err
= check_packet_access(env
, regno
, off
, size
, false);
1601 if (!err
&& t
== BPF_READ
&& value_regno
>= 0)
1602 mark_reg_unknown(env
, regs
, value_regno
);
1604 verbose(env
, "R%d invalid mem access '%s'\n", regno
,
1605 reg_type_str
[reg
->type
]);
1609 if (!err
&& size
< BPF_REG_SIZE
&& value_regno
>= 0 && t
== BPF_READ
&&
1610 regs
[value_regno
].type
== SCALAR_VALUE
) {
1611 /* b/h/w load zero-extends, mark upper bits as known 0 */
1612 regs
[value_regno
].var_off
=
1613 tnum_cast(regs
[value_regno
].var_off
, size
);
1614 __update_reg_bounds(®s
[value_regno
]);
1619 static int check_xadd(struct bpf_verifier_env
*env
, int insn_idx
, struct bpf_insn
*insn
)
1623 if ((BPF_SIZE(insn
->code
) != BPF_W
&& BPF_SIZE(insn
->code
) != BPF_DW
) ||
1625 verbose(env
, "BPF_XADD uses reserved fields\n");
1629 /* check src1 operand */
1630 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
1634 /* check src2 operand */
1635 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
1639 if (is_pointer_value(env
, insn
->src_reg
)) {
1640 verbose(env
, "R%d leaks addr into mem\n", insn
->src_reg
);
1644 /* check whether atomic_add can read the memory */
1645 err
= check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
1646 BPF_SIZE(insn
->code
), BPF_READ
, -1);
1650 /* check whether atomic_add can write into the same memory */
1651 return check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
1652 BPF_SIZE(insn
->code
), BPF_WRITE
, -1);
1655 /* when register 'regno' is passed into function that will read 'access_size'
1656 * bytes from that pointer, make sure that it's within stack boundary
1657 * and all elements of stack are initialized.
1658 * Unlike most pointer bounds-checking functions, this one doesn't take an
1659 * 'off' argument, so it has to add in reg->off itself.
1661 static int check_stack_boundary(struct bpf_verifier_env
*env
, int regno
,
1662 int access_size
, bool zero_size_allowed
,
1663 struct bpf_call_arg_meta
*meta
)
1665 struct bpf_reg_state
*reg
= cur_regs(env
) + regno
;
1666 struct bpf_func_state
*state
= func(env
, reg
);
1667 int off
, i
, slot
, spi
;
1669 if (reg
->type
!= PTR_TO_STACK
) {
1670 /* Allow zero-byte read from NULL, regardless of pointer type */
1671 if (zero_size_allowed
&& access_size
== 0 &&
1672 register_is_null(reg
))
1675 verbose(env
, "R%d type=%s expected=%s\n", regno
,
1676 reg_type_str
[reg
->type
],
1677 reg_type_str
[PTR_TO_STACK
]);
1681 /* Only allow fixed-offset stack reads */
1682 if (!tnum_is_const(reg
->var_off
)) {
1685 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1686 verbose(env
, "invalid variable stack read R%d var_off=%s\n",
1689 off
= reg
->off
+ reg
->var_off
.value
;
1690 if (off
>= 0 || off
< -MAX_BPF_STACK
|| off
+ access_size
> 0 ||
1691 access_size
< 0 || (access_size
== 0 && !zero_size_allowed
)) {
1692 verbose(env
, "invalid stack type R%d off=%d access_size=%d\n",
1693 regno
, off
, access_size
);
1697 if (meta
&& meta
->raw_mode
) {
1698 meta
->access_size
= access_size
;
1699 meta
->regno
= regno
;
1703 for (i
= 0; i
< access_size
; i
++) {
1706 slot
= -(off
+ i
) - 1;
1707 spi
= slot
/ BPF_REG_SIZE
;
1708 if (state
->allocated_stack
<= slot
)
1710 stype
= &state
->stack
[spi
].slot_type
[slot
% BPF_REG_SIZE
];
1711 if (*stype
== STACK_MISC
)
1713 if (*stype
== STACK_ZERO
) {
1714 /* helper can write anything into the stack */
1715 *stype
= STACK_MISC
;
1719 verbose(env
, "invalid indirect read from stack off %d+%d size %d\n",
1720 off
, i
, access_size
);
1723 /* reading any byte out of 8-byte 'spill_slot' will cause
1724 * the whole slot to be marked as 'read'
1726 mark_stack_slot_read(env
, env
->cur_state
, env
->cur_state
->parent
,
1727 spi
, state
->frameno
);
1729 return update_stack_depth(env
, state
, off
);
1732 static int check_helper_mem_access(struct bpf_verifier_env
*env
, int regno
,
1733 int access_size
, bool zero_size_allowed
,
1734 struct bpf_call_arg_meta
*meta
)
1736 struct bpf_reg_state
*regs
= cur_regs(env
), *reg
= ®s
[regno
];
1738 switch (reg
->type
) {
1740 case PTR_TO_PACKET_META
:
1741 return check_packet_access(env
, regno
, reg
->off
, access_size
,
1743 case PTR_TO_MAP_VALUE
:
1744 return check_map_access(env
, regno
, reg
->off
, access_size
,
1746 default: /* scalar_value|ptr_to_stack or invalid ptr */
1747 return check_stack_boundary(env
, regno
, access_size
,
1748 zero_size_allowed
, meta
);
1752 static int check_func_arg(struct bpf_verifier_env
*env
, u32 regno
,
1753 enum bpf_arg_type arg_type
,
1754 struct bpf_call_arg_meta
*meta
)
1756 struct bpf_reg_state
*regs
= cur_regs(env
), *reg
= ®s
[regno
];
1757 enum bpf_reg_type expected_type
, type
= reg
->type
;
1760 if (arg_type
== ARG_DONTCARE
)
1763 err
= check_reg_arg(env
, regno
, SRC_OP
);
1767 if (arg_type
== ARG_ANYTHING
) {
1768 if (is_pointer_value(env
, regno
)) {
1769 verbose(env
, "R%d leaks addr into helper function\n",
1776 if (type_is_pkt_pointer(type
) &&
1777 !may_access_direct_pkt_data(env
, meta
, BPF_READ
)) {
1778 verbose(env
, "helper access to the packet is not allowed\n");
1782 if (arg_type
== ARG_PTR_TO_MAP_KEY
||
1783 arg_type
== ARG_PTR_TO_MAP_VALUE
) {
1784 expected_type
= PTR_TO_STACK
;
1785 if (!type_is_pkt_pointer(type
) &&
1786 type
!= expected_type
)
1788 } else if (arg_type
== ARG_CONST_SIZE
||
1789 arg_type
== ARG_CONST_SIZE_OR_ZERO
) {
1790 expected_type
= SCALAR_VALUE
;
1791 if (type
!= expected_type
)
1793 } else if (arg_type
== ARG_CONST_MAP_PTR
) {
1794 expected_type
= CONST_PTR_TO_MAP
;
1795 if (type
!= expected_type
)
1797 } else if (arg_type
== ARG_PTR_TO_CTX
) {
1798 expected_type
= PTR_TO_CTX
;
1799 if (type
!= expected_type
)
1801 } else if (arg_type
== ARG_PTR_TO_MEM
||
1802 arg_type
== ARG_PTR_TO_MEM_OR_NULL
||
1803 arg_type
== ARG_PTR_TO_UNINIT_MEM
) {
1804 expected_type
= PTR_TO_STACK
;
1805 /* One exception here. In case function allows for NULL to be
1806 * passed in as argument, it's a SCALAR_VALUE type. Final test
1807 * happens during stack boundary checking.
1809 if (register_is_null(reg
) &&
1810 arg_type
== ARG_PTR_TO_MEM_OR_NULL
)
1811 /* final test in check_stack_boundary() */;
1812 else if (!type_is_pkt_pointer(type
) &&
1813 type
!= PTR_TO_MAP_VALUE
&&
1814 type
!= expected_type
)
1816 meta
->raw_mode
= arg_type
== ARG_PTR_TO_UNINIT_MEM
;
1818 verbose(env
, "unsupported arg_type %d\n", arg_type
);
1822 if (arg_type
== ARG_CONST_MAP_PTR
) {
1823 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
1824 meta
->map_ptr
= reg
->map_ptr
;
1825 } else if (arg_type
== ARG_PTR_TO_MAP_KEY
) {
1826 /* bpf_map_xxx(..., map_ptr, ..., key) call:
1827 * check that [key, key + map->key_size) are within
1828 * stack limits and initialized
1830 if (!meta
->map_ptr
) {
1831 /* in function declaration map_ptr must come before
1832 * map_key, so that it's verified and known before
1833 * we have to check map_key here. Otherwise it means
1834 * that kernel subsystem misconfigured verifier
1836 verbose(env
, "invalid map_ptr to access map->key\n");
1839 if (type_is_pkt_pointer(type
))
1840 err
= check_packet_access(env
, regno
, reg
->off
,
1841 meta
->map_ptr
->key_size
,
1844 err
= check_stack_boundary(env
, regno
,
1845 meta
->map_ptr
->key_size
,
1847 } else if (arg_type
== ARG_PTR_TO_MAP_VALUE
) {
1848 /* bpf_map_xxx(..., map_ptr, ..., value) call:
1849 * check [value, value + map->value_size) validity
1851 if (!meta
->map_ptr
) {
1852 /* kernel subsystem misconfigured verifier */
1853 verbose(env
, "invalid map_ptr to access map->value\n");
1856 if (type_is_pkt_pointer(type
))
1857 err
= check_packet_access(env
, regno
, reg
->off
,
1858 meta
->map_ptr
->value_size
,
1861 err
= check_stack_boundary(env
, regno
,
1862 meta
->map_ptr
->value_size
,
1864 } else if (arg_type
== ARG_CONST_SIZE
||
1865 arg_type
== ARG_CONST_SIZE_OR_ZERO
) {
1866 bool zero_size_allowed
= (arg_type
== ARG_CONST_SIZE_OR_ZERO
);
1868 /* bpf_xxx(..., buf, len) call will access 'len' bytes
1869 * from stack pointer 'buf'. Check it
1870 * note: regno == len, regno - 1 == buf
1873 /* kernel subsystem misconfigured verifier */
1875 "ARG_CONST_SIZE cannot be first argument\n");
1879 /* The register is SCALAR_VALUE; the access check
1880 * happens using its boundaries.
1883 if (!tnum_is_const(reg
->var_off
))
1884 /* For unprivileged variable accesses, disable raw
1885 * mode so that the program is required to
1886 * initialize all the memory that the helper could
1887 * just partially fill up.
1891 if (reg
->smin_value
< 0) {
1892 verbose(env
, "R%d min value is negative, either use unsigned or 'var &= const'\n",
1897 if (reg
->umin_value
== 0) {
1898 err
= check_helper_mem_access(env
, regno
- 1, 0,
1905 if (reg
->umax_value
>= BPF_MAX_VAR_SIZ
) {
1906 verbose(env
, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
1910 err
= check_helper_mem_access(env
, regno
- 1,
1912 zero_size_allowed
, meta
);
1917 verbose(env
, "R%d type=%s expected=%s\n", regno
,
1918 reg_type_str
[type
], reg_type_str
[expected_type
]);
1922 static int check_map_func_compatibility(struct bpf_verifier_env
*env
,
1923 struct bpf_map
*map
, int func_id
)
1928 /* We need a two way check, first is from map perspective ... */
1929 switch (map
->map_type
) {
1930 case BPF_MAP_TYPE_PROG_ARRAY
:
1931 if (func_id
!= BPF_FUNC_tail_call
)
1934 case BPF_MAP_TYPE_PERF_EVENT_ARRAY
:
1935 if (func_id
!= BPF_FUNC_perf_event_read
&&
1936 func_id
!= BPF_FUNC_perf_event_output
&&
1937 func_id
!= BPF_FUNC_perf_event_read_value
)
1940 case BPF_MAP_TYPE_STACK_TRACE
:
1941 if (func_id
!= BPF_FUNC_get_stackid
)
1944 case BPF_MAP_TYPE_CGROUP_ARRAY
:
1945 if (func_id
!= BPF_FUNC_skb_under_cgroup
&&
1946 func_id
!= BPF_FUNC_current_task_under_cgroup
)
1949 /* devmap returns a pointer to a live net_device ifindex that we cannot
1950 * allow to be modified from bpf side. So do not allow lookup elements
1953 case BPF_MAP_TYPE_DEVMAP
:
1954 if (func_id
!= BPF_FUNC_redirect_map
)
1957 /* Restrict bpf side of cpumap, open when use-cases appear */
1958 case BPF_MAP_TYPE_CPUMAP
:
1959 if (func_id
!= BPF_FUNC_redirect_map
)
1962 case BPF_MAP_TYPE_ARRAY_OF_MAPS
:
1963 case BPF_MAP_TYPE_HASH_OF_MAPS
:
1964 if (func_id
!= BPF_FUNC_map_lookup_elem
)
1967 case BPF_MAP_TYPE_SOCKMAP
:
1968 if (func_id
!= BPF_FUNC_sk_redirect_map
&&
1969 func_id
!= BPF_FUNC_sock_map_update
&&
1970 func_id
!= BPF_FUNC_map_delete_elem
)
1977 /* ... and second from the function itself. */
1979 case BPF_FUNC_tail_call
:
1980 if (map
->map_type
!= BPF_MAP_TYPE_PROG_ARRAY
)
1982 if (env
->subprog_cnt
) {
1983 verbose(env
, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
1987 case BPF_FUNC_perf_event_read
:
1988 case BPF_FUNC_perf_event_output
:
1989 case BPF_FUNC_perf_event_read_value
:
1990 if (map
->map_type
!= BPF_MAP_TYPE_PERF_EVENT_ARRAY
)
1993 case BPF_FUNC_get_stackid
:
1994 if (map
->map_type
!= BPF_MAP_TYPE_STACK_TRACE
)
1997 case BPF_FUNC_current_task_under_cgroup
:
1998 case BPF_FUNC_skb_under_cgroup
:
1999 if (map
->map_type
!= BPF_MAP_TYPE_CGROUP_ARRAY
)
2002 case BPF_FUNC_redirect_map
:
2003 if (map
->map_type
!= BPF_MAP_TYPE_DEVMAP
&&
2004 map
->map_type
!= BPF_MAP_TYPE_CPUMAP
)
2007 case BPF_FUNC_sk_redirect_map
:
2008 if (map
->map_type
!= BPF_MAP_TYPE_SOCKMAP
)
2011 case BPF_FUNC_sock_map_update
:
2012 if (map
->map_type
!= BPF_MAP_TYPE_SOCKMAP
)
2021 verbose(env
, "cannot pass map_type %d into func %s#%d\n",
2022 map
->map_type
, func_id_name(func_id
), func_id
);
2026 static int check_raw_mode(const struct bpf_func_proto
*fn
)
2030 if (fn
->arg1_type
== ARG_PTR_TO_UNINIT_MEM
)
2032 if (fn
->arg2_type
== ARG_PTR_TO_UNINIT_MEM
)
2034 if (fn
->arg3_type
== ARG_PTR_TO_UNINIT_MEM
)
2036 if (fn
->arg4_type
== ARG_PTR_TO_UNINIT_MEM
)
2038 if (fn
->arg5_type
== ARG_PTR_TO_UNINIT_MEM
)
2041 return count
> 1 ? -EINVAL
: 0;
2044 /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
2045 * are now invalid, so turn them into unknown SCALAR_VALUE.
2047 static void __clear_all_pkt_pointers(struct bpf_verifier_env
*env
,
2048 struct bpf_func_state
*state
)
2050 struct bpf_reg_state
*regs
= state
->regs
, *reg
;
2053 for (i
= 0; i
< MAX_BPF_REG
; i
++)
2054 if (reg_is_pkt_pointer_any(®s
[i
]))
2055 mark_reg_unknown(env
, regs
, i
);
2057 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
2058 if (state
->stack
[i
].slot_type
[0] != STACK_SPILL
)
2060 reg
= &state
->stack
[i
].spilled_ptr
;
2061 if (reg_is_pkt_pointer_any(reg
))
2062 __mark_reg_unknown(reg
);
2066 static void clear_all_pkt_pointers(struct bpf_verifier_env
*env
)
2068 struct bpf_verifier_state
*vstate
= env
->cur_state
;
2071 for (i
= 0; i
<= vstate
->curframe
; i
++)
2072 __clear_all_pkt_pointers(env
, vstate
->frame
[i
]);
2075 static int check_func_call(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
,
2078 struct bpf_verifier_state
*state
= env
->cur_state
;
2079 struct bpf_func_state
*caller
, *callee
;
2080 int i
, subprog
, target_insn
;
2082 if (state
->curframe
>= MAX_CALL_FRAMES
) {
2083 verbose(env
, "the call stack of %d frames is too deep\n",
2088 target_insn
= *insn_idx
+ insn
->imm
;
2089 subprog
= find_subprog(env
, target_insn
+ 1);
2091 verbose(env
, "verifier bug. No program starts at insn %d\n",
2096 caller
= state
->frame
[state
->curframe
];
2097 if (state
->frame
[state
->curframe
+ 1]) {
2098 verbose(env
, "verifier bug. Frame %d already allocated\n",
2099 state
->curframe
+ 1);
2103 callee
= kzalloc(sizeof(*callee
), GFP_KERNEL
);
2106 state
->frame
[state
->curframe
+ 1] = callee
;
2108 /* callee cannot access r0, r6 - r9 for reading and has to write
2109 * into its own stack before reading from it.
2110 * callee can read/write into caller's stack
2112 init_func_state(env
, callee
,
2113 /* remember the callsite, it will be used by bpf_exit */
2114 *insn_idx
/* callsite */,
2115 state
->curframe
+ 1 /* frameno within this callchain */,
2116 subprog
+ 1 /* subprog number within this prog */);
2118 /* copy r1 - r5 args that callee can access */
2119 for (i
= BPF_REG_1
; i
<= BPF_REG_5
; i
++)
2120 callee
->regs
[i
] = caller
->regs
[i
];
2122 /* after the call regsiters r0 - r5 were scratched */
2123 for (i
= 0; i
< CALLER_SAVED_REGS
; i
++) {
2124 mark_reg_not_init(env
, caller
->regs
, caller_saved
[i
]);
2125 check_reg_arg(env
, caller_saved
[i
], DST_OP_NO_MARK
);
2128 /* only increment it after check_reg_arg() finished */
2131 /* and go analyze first insn of the callee */
2132 *insn_idx
= target_insn
;
2134 if (env
->log
.level
) {
2135 verbose(env
, "caller:\n");
2136 print_verifier_state(env
, caller
);
2137 verbose(env
, "callee:\n");
2138 print_verifier_state(env
, callee
);
2143 static int prepare_func_exit(struct bpf_verifier_env
*env
, int *insn_idx
)
2145 struct bpf_verifier_state
*state
= env
->cur_state
;
2146 struct bpf_func_state
*caller
, *callee
;
2147 struct bpf_reg_state
*r0
;
2149 callee
= state
->frame
[state
->curframe
];
2150 r0
= &callee
->regs
[BPF_REG_0
];
2151 if (r0
->type
== PTR_TO_STACK
) {
2152 /* technically it's ok to return caller's stack pointer
2153 * (or caller's caller's pointer) back to the caller,
2154 * since these pointers are valid. Only current stack
2155 * pointer will be invalid as soon as function exits,
2156 * but let's be conservative
2158 verbose(env
, "cannot return stack pointer to the caller\n");
2163 caller
= state
->frame
[state
->curframe
];
2164 /* return to the caller whatever r0 had in the callee */
2165 caller
->regs
[BPF_REG_0
] = *r0
;
2167 *insn_idx
= callee
->callsite
+ 1;
2168 if (env
->log
.level
) {
2169 verbose(env
, "returning from callee:\n");
2170 print_verifier_state(env
, callee
);
2171 verbose(env
, "to caller at %d:\n", *insn_idx
);
2172 print_verifier_state(env
, caller
);
2174 /* clear everything in the callee */
2175 free_func_state(callee
);
2176 state
->frame
[state
->curframe
+ 1] = NULL
;
2180 static int check_helper_call(struct bpf_verifier_env
*env
, int func_id
, int insn_idx
)
2182 const struct bpf_func_proto
*fn
= NULL
;
2183 struct bpf_reg_state
*regs
;
2184 struct bpf_call_arg_meta meta
;
2188 /* find function prototype */
2189 if (func_id
< 0 || func_id
>= __BPF_FUNC_MAX_ID
) {
2190 verbose(env
, "invalid func %s#%d\n", func_id_name(func_id
),
2195 if (env
->ops
->get_func_proto
)
2196 fn
= env
->ops
->get_func_proto(func_id
);
2199 verbose(env
, "unknown func %s#%d\n", func_id_name(func_id
),
2204 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2205 if (!env
->prog
->gpl_compatible
&& fn
->gpl_only
) {
2206 verbose(env
, "cannot call GPL only function from proprietary program\n");
2210 changes_data
= bpf_helper_changes_pkt_data(fn
->func
);
2212 memset(&meta
, 0, sizeof(meta
));
2213 meta
.pkt_access
= fn
->pkt_access
;
2215 /* We only support one arg being in raw mode at the moment, which
2216 * is sufficient for the helper functions we have right now.
2218 err
= check_raw_mode(fn
);
2220 verbose(env
, "kernel subsystem misconfigured func %s#%d\n",
2221 func_id_name(func_id
), func_id
);
2226 err
= check_func_arg(env
, BPF_REG_1
, fn
->arg1_type
, &meta
);
2229 err
= check_func_arg(env
, BPF_REG_2
, fn
->arg2_type
, &meta
);
2232 err
= check_func_arg(env
, BPF_REG_3
, fn
->arg3_type
, &meta
);
2235 err
= check_func_arg(env
, BPF_REG_4
, fn
->arg4_type
, &meta
);
2238 err
= check_func_arg(env
, BPF_REG_5
, fn
->arg5_type
, &meta
);
2242 /* Mark slots with STACK_MISC in case of raw mode, stack offset
2243 * is inferred from register state.
2245 for (i
= 0; i
< meta
.access_size
; i
++) {
2246 err
= check_mem_access(env
, insn_idx
, meta
.regno
, i
, BPF_B
, BPF_WRITE
, -1);
2251 regs
= cur_regs(env
);
2252 /* reset caller saved regs */
2253 for (i
= 0; i
< CALLER_SAVED_REGS
; i
++) {
2254 mark_reg_not_init(env
, regs
, caller_saved
[i
]);
2255 check_reg_arg(env
, caller_saved
[i
], DST_OP_NO_MARK
);
2258 /* update return register (already marked as written above) */
2259 if (fn
->ret_type
== RET_INTEGER
) {
2260 /* sets type to SCALAR_VALUE */
2261 mark_reg_unknown(env
, regs
, BPF_REG_0
);
2262 } else if (fn
->ret_type
== RET_VOID
) {
2263 regs
[BPF_REG_0
].type
= NOT_INIT
;
2264 } else if (fn
->ret_type
== RET_PTR_TO_MAP_VALUE_OR_NULL
) {
2265 struct bpf_insn_aux_data
*insn_aux
;
2267 regs
[BPF_REG_0
].type
= PTR_TO_MAP_VALUE_OR_NULL
;
2268 /* There is no offset yet applied, variable or fixed */
2269 mark_reg_known_zero(env
, regs
, BPF_REG_0
);
2270 regs
[BPF_REG_0
].off
= 0;
2271 /* remember map_ptr, so that check_map_access()
2272 * can check 'value_size' boundary of memory access
2273 * to map element returned from bpf_map_lookup_elem()
2275 if (meta
.map_ptr
== NULL
) {
2277 "kernel subsystem misconfigured verifier\n");
2280 regs
[BPF_REG_0
].map_ptr
= meta
.map_ptr
;
2281 regs
[BPF_REG_0
].id
= ++env
->id_gen
;
2282 insn_aux
= &env
->insn_aux_data
[insn_idx
];
2283 if (!insn_aux
->map_ptr
)
2284 insn_aux
->map_ptr
= meta
.map_ptr
;
2285 else if (insn_aux
->map_ptr
!= meta
.map_ptr
)
2286 insn_aux
->map_ptr
= BPF_MAP_PTR_POISON
;
2288 verbose(env
, "unknown return type %d of func %s#%d\n",
2289 fn
->ret_type
, func_id_name(func_id
), func_id
);
2293 err
= check_map_func_compatibility(env
, meta
.map_ptr
, func_id
);
2298 clear_all_pkt_pointers(env
);
2302 static void coerce_reg_to_32(struct bpf_reg_state
*reg
)
2304 /* clear high 32 bits */
2305 reg
->var_off
= tnum_cast(reg
->var_off
, 4);
2307 __update_reg_bounds(reg
);
2310 static bool signed_add_overflows(s64 a
, s64 b
)
2312 /* Do the add in u64, where overflow is well-defined */
2313 s64 res
= (s64
)((u64
)a
+ (u64
)b
);
2320 static bool signed_sub_overflows(s64 a
, s64 b
)
2322 /* Do the sub in u64, where overflow is well-defined */
2323 s64 res
= (s64
)((u64
)a
- (u64
)b
);
2330 /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
2331 * Caller should also handle BPF_MOV case separately.
2332 * If we return -EACCES, caller may want to try again treating pointer as a
2333 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
2335 static int adjust_ptr_min_max_vals(struct bpf_verifier_env
*env
,
2336 struct bpf_insn
*insn
,
2337 const struct bpf_reg_state
*ptr_reg
,
2338 const struct bpf_reg_state
*off_reg
)
2340 struct bpf_verifier_state
*vstate
= env
->cur_state
;
2341 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
2342 struct bpf_reg_state
*regs
= state
->regs
, *dst_reg
;
2343 bool known
= tnum_is_const(off_reg
->var_off
);
2344 s64 smin_val
= off_reg
->smin_value
, smax_val
= off_reg
->smax_value
,
2345 smin_ptr
= ptr_reg
->smin_value
, smax_ptr
= ptr_reg
->smax_value
;
2346 u64 umin_val
= off_reg
->umin_value
, umax_val
= off_reg
->umax_value
,
2347 umin_ptr
= ptr_reg
->umin_value
, umax_ptr
= ptr_reg
->umax_value
;
2348 u8 opcode
= BPF_OP(insn
->code
);
2349 u32 dst
= insn
->dst_reg
;
2351 dst_reg
= ®s
[dst
];
2353 if (WARN_ON_ONCE(known
&& (smin_val
!= smax_val
))) {
2354 print_verifier_state(env
, state
);
2356 "verifier internal error: known but bad sbounds\n");
2359 if (WARN_ON_ONCE(known
&& (umin_val
!= umax_val
))) {
2360 print_verifier_state(env
, state
);
2362 "verifier internal error: known but bad ubounds\n");
2366 if (BPF_CLASS(insn
->code
) != BPF_ALU64
) {
2367 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
2368 if (!env
->allow_ptr_leaks
)
2370 "R%d 32-bit pointer arithmetic prohibited\n",
2375 if (ptr_reg
->type
== PTR_TO_MAP_VALUE_OR_NULL
) {
2376 if (!env
->allow_ptr_leaks
)
2377 verbose(env
, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n",
2381 if (ptr_reg
->type
== CONST_PTR_TO_MAP
) {
2382 if (!env
->allow_ptr_leaks
)
2383 verbose(env
, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n",
2387 if (ptr_reg
->type
== PTR_TO_PACKET_END
) {
2388 if (!env
->allow_ptr_leaks
)
2389 verbose(env
, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n",
2394 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
2395 * The id may be overwritten later if we create a new variable offset.
2397 dst_reg
->type
= ptr_reg
->type
;
2398 dst_reg
->id
= ptr_reg
->id
;
2402 /* We can take a fixed offset as long as it doesn't overflow
2403 * the s32 'off' field
2405 if (known
&& (ptr_reg
->off
+ smin_val
==
2406 (s64
)(s32
)(ptr_reg
->off
+ smin_val
))) {
2407 /* pointer += K. Accumulate it into fixed offset */
2408 dst_reg
->smin_value
= smin_ptr
;
2409 dst_reg
->smax_value
= smax_ptr
;
2410 dst_reg
->umin_value
= umin_ptr
;
2411 dst_reg
->umax_value
= umax_ptr
;
2412 dst_reg
->var_off
= ptr_reg
->var_off
;
2413 dst_reg
->off
= ptr_reg
->off
+ smin_val
;
2414 dst_reg
->range
= ptr_reg
->range
;
2417 /* A new variable offset is created. Note that off_reg->off
2418 * == 0, since it's a scalar.
2419 * dst_reg gets the pointer type and since some positive
2420 * integer value was added to the pointer, give it a new 'id'
2421 * if it's a PTR_TO_PACKET.
2422 * this creates a new 'base' pointer, off_reg (variable) gets
2423 * added into the variable offset, and we copy the fixed offset
2426 if (signed_add_overflows(smin_ptr
, smin_val
) ||
2427 signed_add_overflows(smax_ptr
, smax_val
)) {
2428 dst_reg
->smin_value
= S64_MIN
;
2429 dst_reg
->smax_value
= S64_MAX
;
2431 dst_reg
->smin_value
= smin_ptr
+ smin_val
;
2432 dst_reg
->smax_value
= smax_ptr
+ smax_val
;
2434 if (umin_ptr
+ umin_val
< umin_ptr
||
2435 umax_ptr
+ umax_val
< umax_ptr
) {
2436 dst_reg
->umin_value
= 0;
2437 dst_reg
->umax_value
= U64_MAX
;
2439 dst_reg
->umin_value
= umin_ptr
+ umin_val
;
2440 dst_reg
->umax_value
= umax_ptr
+ umax_val
;
2442 dst_reg
->var_off
= tnum_add(ptr_reg
->var_off
, off_reg
->var_off
);
2443 dst_reg
->off
= ptr_reg
->off
;
2444 if (reg_is_pkt_pointer(ptr_reg
)) {
2445 dst_reg
->id
= ++env
->id_gen
;
2446 /* something was added to pkt_ptr, set range to zero */
2451 if (dst_reg
== off_reg
) {
2452 /* scalar -= pointer. Creates an unknown scalar */
2453 if (!env
->allow_ptr_leaks
)
2454 verbose(env
, "R%d tried to subtract pointer from scalar\n",
2458 /* We don't allow subtraction from FP, because (according to
2459 * test_verifier.c test "invalid fp arithmetic", JITs might not
2460 * be able to deal with it.
2462 if (ptr_reg
->type
== PTR_TO_STACK
) {
2463 if (!env
->allow_ptr_leaks
)
2464 verbose(env
, "R%d subtraction from stack pointer prohibited\n",
2468 if (known
&& (ptr_reg
->off
- smin_val
==
2469 (s64
)(s32
)(ptr_reg
->off
- smin_val
))) {
2470 /* pointer -= K. Subtract it from fixed offset */
2471 dst_reg
->smin_value
= smin_ptr
;
2472 dst_reg
->smax_value
= smax_ptr
;
2473 dst_reg
->umin_value
= umin_ptr
;
2474 dst_reg
->umax_value
= umax_ptr
;
2475 dst_reg
->var_off
= ptr_reg
->var_off
;
2476 dst_reg
->id
= ptr_reg
->id
;
2477 dst_reg
->off
= ptr_reg
->off
- smin_val
;
2478 dst_reg
->range
= ptr_reg
->range
;
2481 /* A new variable offset is created. If the subtrahend is known
2482 * nonnegative, then any reg->range we had before is still good.
2484 if (signed_sub_overflows(smin_ptr
, smax_val
) ||
2485 signed_sub_overflows(smax_ptr
, smin_val
)) {
2486 /* Overflow possible, we know nothing */
2487 dst_reg
->smin_value
= S64_MIN
;
2488 dst_reg
->smax_value
= S64_MAX
;
2490 dst_reg
->smin_value
= smin_ptr
- smax_val
;
2491 dst_reg
->smax_value
= smax_ptr
- smin_val
;
2493 if (umin_ptr
< umax_val
) {
2494 /* Overflow possible, we know nothing */
2495 dst_reg
->umin_value
= 0;
2496 dst_reg
->umax_value
= U64_MAX
;
2498 /* Cannot overflow (as long as bounds are consistent) */
2499 dst_reg
->umin_value
= umin_ptr
- umax_val
;
2500 dst_reg
->umax_value
= umax_ptr
- umin_val
;
2502 dst_reg
->var_off
= tnum_sub(ptr_reg
->var_off
, off_reg
->var_off
);
2503 dst_reg
->off
= ptr_reg
->off
;
2504 if (reg_is_pkt_pointer(ptr_reg
)) {
2505 dst_reg
->id
= ++env
->id_gen
;
2506 /* something was added to pkt_ptr, set range to zero */
2514 /* bitwise ops on pointers are troublesome, prohibit for now.
2515 * (However, in principle we could allow some cases, e.g.
2516 * ptr &= ~3 which would reduce min_value by 3.)
2518 if (!env
->allow_ptr_leaks
)
2519 verbose(env
, "R%d bitwise operator %s on pointer prohibited\n",
2520 dst
, bpf_alu_string
[opcode
>> 4]);
2523 /* other operators (e.g. MUL,LSH) produce non-pointer results */
2524 if (!env
->allow_ptr_leaks
)
2525 verbose(env
, "R%d pointer arithmetic with %s operator prohibited\n",
2526 dst
, bpf_alu_string
[opcode
>> 4]);
2530 __update_reg_bounds(dst_reg
);
2531 __reg_deduce_bounds(dst_reg
);
2532 __reg_bound_offset(dst_reg
);
2536 static int adjust_scalar_min_max_vals(struct bpf_verifier_env
*env
,
2537 struct bpf_insn
*insn
,
2538 struct bpf_reg_state
*dst_reg
,
2539 struct bpf_reg_state src_reg
)
2541 struct bpf_reg_state
*regs
= cur_regs(env
);
2542 u8 opcode
= BPF_OP(insn
->code
);
2543 bool src_known
, dst_known
;
2544 s64 smin_val
, smax_val
;
2545 u64 umin_val
, umax_val
;
2547 if (BPF_CLASS(insn
->code
) != BPF_ALU64
) {
2548 /* 32-bit ALU ops are (32,32)->64 */
2549 coerce_reg_to_32(dst_reg
);
2550 coerce_reg_to_32(&src_reg
);
2552 smin_val
= src_reg
.smin_value
;
2553 smax_val
= src_reg
.smax_value
;
2554 umin_val
= src_reg
.umin_value
;
2555 umax_val
= src_reg
.umax_value
;
2556 src_known
= tnum_is_const(src_reg
.var_off
);
2557 dst_known
= tnum_is_const(dst_reg
->var_off
);
2561 if (signed_add_overflows(dst_reg
->smin_value
, smin_val
) ||
2562 signed_add_overflows(dst_reg
->smax_value
, smax_val
)) {
2563 dst_reg
->smin_value
= S64_MIN
;
2564 dst_reg
->smax_value
= S64_MAX
;
2566 dst_reg
->smin_value
+= smin_val
;
2567 dst_reg
->smax_value
+= smax_val
;
2569 if (dst_reg
->umin_value
+ umin_val
< umin_val
||
2570 dst_reg
->umax_value
+ umax_val
< umax_val
) {
2571 dst_reg
->umin_value
= 0;
2572 dst_reg
->umax_value
= U64_MAX
;
2574 dst_reg
->umin_value
+= umin_val
;
2575 dst_reg
->umax_value
+= umax_val
;
2577 dst_reg
->var_off
= tnum_add(dst_reg
->var_off
, src_reg
.var_off
);
2580 if (signed_sub_overflows(dst_reg
->smin_value
, smax_val
) ||
2581 signed_sub_overflows(dst_reg
->smax_value
, smin_val
)) {
2582 /* Overflow possible, we know nothing */
2583 dst_reg
->smin_value
= S64_MIN
;
2584 dst_reg
->smax_value
= S64_MAX
;
2586 dst_reg
->smin_value
-= smax_val
;
2587 dst_reg
->smax_value
-= smin_val
;
2589 if (dst_reg
->umin_value
< umax_val
) {
2590 /* Overflow possible, we know nothing */
2591 dst_reg
->umin_value
= 0;
2592 dst_reg
->umax_value
= U64_MAX
;
2594 /* Cannot overflow (as long as bounds are consistent) */
2595 dst_reg
->umin_value
-= umax_val
;
2596 dst_reg
->umax_value
-= umin_val
;
2598 dst_reg
->var_off
= tnum_sub(dst_reg
->var_off
, src_reg
.var_off
);
2601 dst_reg
->var_off
= tnum_mul(dst_reg
->var_off
, src_reg
.var_off
);
2602 if (smin_val
< 0 || dst_reg
->smin_value
< 0) {
2603 /* Ain't nobody got time to multiply that sign */
2604 __mark_reg_unbounded(dst_reg
);
2605 __update_reg_bounds(dst_reg
);
2608 /* Both values are positive, so we can work with unsigned and
2609 * copy the result to signed (unless it exceeds S64_MAX).
2611 if (umax_val
> U32_MAX
|| dst_reg
->umax_value
> U32_MAX
) {
2612 /* Potential overflow, we know nothing */
2613 __mark_reg_unbounded(dst_reg
);
2614 /* (except what we can learn from the var_off) */
2615 __update_reg_bounds(dst_reg
);
2618 dst_reg
->umin_value
*= umin_val
;
2619 dst_reg
->umax_value
*= umax_val
;
2620 if (dst_reg
->umax_value
> S64_MAX
) {
2621 /* Overflow possible, we know nothing */
2622 dst_reg
->smin_value
= S64_MIN
;
2623 dst_reg
->smax_value
= S64_MAX
;
2625 dst_reg
->smin_value
= dst_reg
->umin_value
;
2626 dst_reg
->smax_value
= dst_reg
->umax_value
;
2630 if (src_known
&& dst_known
) {
2631 __mark_reg_known(dst_reg
, dst_reg
->var_off
.value
&
2632 src_reg
.var_off
.value
);
2635 /* We get our minimum from the var_off, since that's inherently
2636 * bitwise. Our maximum is the minimum of the operands' maxima.
2638 dst_reg
->var_off
= tnum_and(dst_reg
->var_off
, src_reg
.var_off
);
2639 dst_reg
->umin_value
= dst_reg
->var_off
.value
;
2640 dst_reg
->umax_value
= min(dst_reg
->umax_value
, umax_val
);
2641 if (dst_reg
->smin_value
< 0 || smin_val
< 0) {
2642 /* Lose signed bounds when ANDing negative numbers,
2643 * ain't nobody got time for that.
2645 dst_reg
->smin_value
= S64_MIN
;
2646 dst_reg
->smax_value
= S64_MAX
;
2648 /* ANDing two positives gives a positive, so safe to
2649 * cast result into s64.
2651 dst_reg
->smin_value
= dst_reg
->umin_value
;
2652 dst_reg
->smax_value
= dst_reg
->umax_value
;
2654 /* We may learn something more from the var_off */
2655 __update_reg_bounds(dst_reg
);
2658 if (src_known
&& dst_known
) {
2659 __mark_reg_known(dst_reg
, dst_reg
->var_off
.value
|
2660 src_reg
.var_off
.value
);
2663 /* We get our maximum from the var_off, and our minimum is the
2664 * maximum of the operands' minima
2666 dst_reg
->var_off
= tnum_or(dst_reg
->var_off
, src_reg
.var_off
);
2667 dst_reg
->umin_value
= max(dst_reg
->umin_value
, umin_val
);
2668 dst_reg
->umax_value
= dst_reg
->var_off
.value
|
2669 dst_reg
->var_off
.mask
;
2670 if (dst_reg
->smin_value
< 0 || smin_val
< 0) {
2671 /* Lose signed bounds when ORing negative numbers,
2672 * ain't nobody got time for that.
2674 dst_reg
->smin_value
= S64_MIN
;
2675 dst_reg
->smax_value
= S64_MAX
;
2677 /* ORing two positives gives a positive, so safe to
2678 * cast result into s64.
2680 dst_reg
->smin_value
= dst_reg
->umin_value
;
2681 dst_reg
->smax_value
= dst_reg
->umax_value
;
2683 /* We may learn something more from the var_off */
2684 __update_reg_bounds(dst_reg
);
2687 if (umax_val
> 63) {
2688 /* Shifts greater than 63 are undefined. This includes
2689 * shifts by a negative number.
2691 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
2694 /* We lose all sign bit information (except what we can pick
2697 dst_reg
->smin_value
= S64_MIN
;
2698 dst_reg
->smax_value
= S64_MAX
;
2699 /* If we might shift our top bit out, then we know nothing */
2700 if (dst_reg
->umax_value
> 1ULL << (63 - umax_val
)) {
2701 dst_reg
->umin_value
= 0;
2702 dst_reg
->umax_value
= U64_MAX
;
2704 dst_reg
->umin_value
<<= umin_val
;
2705 dst_reg
->umax_value
<<= umax_val
;
2708 dst_reg
->var_off
= tnum_lshift(dst_reg
->var_off
, umin_val
);
2710 dst_reg
->var_off
= tnum_lshift(tnum_unknown
, umin_val
);
2711 /* We may learn something more from the var_off */
2712 __update_reg_bounds(dst_reg
);
2715 if (umax_val
> 63) {
2716 /* Shifts greater than 63 are undefined. This includes
2717 * shifts by a negative number.
2719 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
2722 /* BPF_RSH is an unsigned shift, so make the appropriate casts */
2723 if (dst_reg
->smin_value
< 0) {
2725 /* Sign bit will be cleared */
2726 dst_reg
->smin_value
= 0;
2728 /* Lost sign bit information */
2729 dst_reg
->smin_value
= S64_MIN
;
2730 dst_reg
->smax_value
= S64_MAX
;
2733 dst_reg
->smin_value
=
2734 (u64
)(dst_reg
->smin_value
) >> umax_val
;
2737 dst_reg
->var_off
= tnum_rshift(dst_reg
->var_off
,
2740 dst_reg
->var_off
= tnum_rshift(tnum_unknown
, umin_val
);
2741 dst_reg
->umin_value
>>= umax_val
;
2742 dst_reg
->umax_value
>>= umin_val
;
2743 /* We may learn something more from the var_off */
2744 __update_reg_bounds(dst_reg
);
2747 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
2751 __reg_deduce_bounds(dst_reg
);
2752 __reg_bound_offset(dst_reg
);
2756 /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
2759 static int adjust_reg_min_max_vals(struct bpf_verifier_env
*env
,
2760 struct bpf_insn
*insn
)
2762 struct bpf_verifier_state
*vstate
= env
->cur_state
;
2763 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
2764 struct bpf_reg_state
*regs
= state
->regs
, *dst_reg
, *src_reg
;
2765 struct bpf_reg_state
*ptr_reg
= NULL
, off_reg
= {0};
2766 u8 opcode
= BPF_OP(insn
->code
);
2769 dst_reg
= ®s
[insn
->dst_reg
];
2771 if (dst_reg
->type
!= SCALAR_VALUE
)
2773 if (BPF_SRC(insn
->code
) == BPF_X
) {
2774 src_reg
= ®s
[insn
->src_reg
];
2775 if (src_reg
->type
!= SCALAR_VALUE
) {
2776 if (dst_reg
->type
!= SCALAR_VALUE
) {
2777 /* Combining two pointers by any ALU op yields
2778 * an arbitrary scalar.
2780 if (!env
->allow_ptr_leaks
) {
2781 verbose(env
, "R%d pointer %s pointer prohibited\n",
2783 bpf_alu_string
[opcode
>> 4]);
2786 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
2789 /* scalar += pointer
2790 * This is legal, but we have to reverse our
2791 * src/dest handling in computing the range
2793 rc
= adjust_ptr_min_max_vals(env
, insn
,
2795 if (rc
== -EACCES
&& env
->allow_ptr_leaks
) {
2796 /* scalar += unknown scalar */
2797 __mark_reg_unknown(&off_reg
);
2798 return adjust_scalar_min_max_vals(
2804 } else if (ptr_reg
) {
2805 /* pointer += scalar */
2806 rc
= adjust_ptr_min_max_vals(env
, insn
,
2808 if (rc
== -EACCES
&& env
->allow_ptr_leaks
) {
2809 /* unknown scalar += scalar */
2810 __mark_reg_unknown(dst_reg
);
2811 return adjust_scalar_min_max_vals(
2812 env
, insn
, dst_reg
, *src_reg
);
2817 /* Pretend the src is a reg with a known value, since we only
2818 * need to be able to read from this state.
2820 off_reg
.type
= SCALAR_VALUE
;
2821 __mark_reg_known(&off_reg
, insn
->imm
);
2823 if (ptr_reg
) { /* pointer += K */
2824 rc
= adjust_ptr_min_max_vals(env
, insn
,
2826 if (rc
== -EACCES
&& env
->allow_ptr_leaks
) {
2827 /* unknown scalar += K */
2828 __mark_reg_unknown(dst_reg
);
2829 return adjust_scalar_min_max_vals(
2830 env
, insn
, dst_reg
, off_reg
);
2836 /* Got here implies adding two SCALAR_VALUEs */
2837 if (WARN_ON_ONCE(ptr_reg
)) {
2838 print_verifier_state(env
, state
);
2839 verbose(env
, "verifier internal error: unexpected ptr_reg\n");
2842 if (WARN_ON(!src_reg
)) {
2843 print_verifier_state(env
, state
);
2844 verbose(env
, "verifier internal error: no src_reg\n");
2847 return adjust_scalar_min_max_vals(env
, insn
, dst_reg
, *src_reg
);
2850 /* check validity of 32-bit and 64-bit arithmetic operations */
2851 static int check_alu_op(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
2853 struct bpf_reg_state
*regs
= cur_regs(env
);
2854 u8 opcode
= BPF_OP(insn
->code
);
2857 if (opcode
== BPF_END
|| opcode
== BPF_NEG
) {
2858 if (opcode
== BPF_NEG
) {
2859 if (BPF_SRC(insn
->code
) != 0 ||
2860 insn
->src_reg
!= BPF_REG_0
||
2861 insn
->off
!= 0 || insn
->imm
!= 0) {
2862 verbose(env
, "BPF_NEG uses reserved fields\n");
2866 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0 ||
2867 (insn
->imm
!= 16 && insn
->imm
!= 32 && insn
->imm
!= 64) ||
2868 BPF_CLASS(insn
->code
) == BPF_ALU64
) {
2869 verbose(env
, "BPF_END uses reserved fields\n");
2874 /* check src operand */
2875 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
2879 if (is_pointer_value(env
, insn
->dst_reg
)) {
2880 verbose(env
, "R%d pointer arithmetic prohibited\n",
2885 /* check dest operand */
2886 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP
);
2890 } else if (opcode
== BPF_MOV
) {
2892 if (BPF_SRC(insn
->code
) == BPF_X
) {
2893 if (insn
->imm
!= 0 || insn
->off
!= 0) {
2894 verbose(env
, "BPF_MOV uses reserved fields\n");
2898 /* check src operand */
2899 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
2903 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0) {
2904 verbose(env
, "BPF_MOV uses reserved fields\n");
2909 /* check dest operand */
2910 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP
);
2914 if (BPF_SRC(insn
->code
) == BPF_X
) {
2915 if (BPF_CLASS(insn
->code
) == BPF_ALU64
) {
2917 * copy register state to dest reg
2919 regs
[insn
->dst_reg
] = regs
[insn
->src_reg
];
2920 regs
[insn
->dst_reg
].live
|= REG_LIVE_WRITTEN
;
2923 if (is_pointer_value(env
, insn
->src_reg
)) {
2925 "R%d partial copy of pointer\n",
2929 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
2930 /* high 32 bits are known zero. */
2931 regs
[insn
->dst_reg
].var_off
= tnum_cast(
2932 regs
[insn
->dst_reg
].var_off
, 4);
2933 __update_reg_bounds(®s
[insn
->dst_reg
]);
2937 * remember the value we stored into this reg
2939 regs
[insn
->dst_reg
].type
= SCALAR_VALUE
;
2940 __mark_reg_known(regs
+ insn
->dst_reg
, insn
->imm
);
2943 } else if (opcode
> BPF_END
) {
2944 verbose(env
, "invalid BPF_ALU opcode %x\n", opcode
);
2947 } else { /* all other ALU ops: and, sub, xor, add, ... */
2949 if (BPF_SRC(insn
->code
) == BPF_X
) {
2950 if (insn
->imm
!= 0 || insn
->off
!= 0) {
2951 verbose(env
, "BPF_ALU uses reserved fields\n");
2954 /* check src1 operand */
2955 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
2959 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0) {
2960 verbose(env
, "BPF_ALU uses reserved fields\n");
2965 /* check src2 operand */
2966 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
2970 if ((opcode
== BPF_MOD
|| opcode
== BPF_DIV
) &&
2971 BPF_SRC(insn
->code
) == BPF_K
&& insn
->imm
== 0) {
2972 verbose(env
, "div by zero\n");
2976 if ((opcode
== BPF_LSH
|| opcode
== BPF_RSH
||
2977 opcode
== BPF_ARSH
) && BPF_SRC(insn
->code
) == BPF_K
) {
2978 int size
= BPF_CLASS(insn
->code
) == BPF_ALU64
? 64 : 32;
2980 if (insn
->imm
< 0 || insn
->imm
>= size
) {
2981 verbose(env
, "invalid shift %d\n", insn
->imm
);
2986 /* check dest operand */
2987 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP_NO_MARK
);
2991 return adjust_reg_min_max_vals(env
, insn
);
2997 static void find_good_pkt_pointers(struct bpf_verifier_state
*vstate
,
2998 struct bpf_reg_state
*dst_reg
,
2999 enum bpf_reg_type type
,
3000 bool range_right_open
)
3002 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
3003 struct bpf_reg_state
*regs
= state
->regs
, *reg
;
3007 if (dst_reg
->off
< 0 ||
3008 (dst_reg
->off
== 0 && range_right_open
))
3009 /* This doesn't give us any range */
3012 if (dst_reg
->umax_value
> MAX_PACKET_OFF
||
3013 dst_reg
->umax_value
+ dst_reg
->off
> MAX_PACKET_OFF
)
3014 /* Risk of overflow. For instance, ptr + (1<<63) may be less
3015 * than pkt_end, but that's because it's also less than pkt.
3019 new_range
= dst_reg
->off
;
3020 if (range_right_open
)
3023 /* Examples for register markings:
3025 * pkt_data in dst register:
3029 * if (r2 > pkt_end) goto <handle exception>
3034 * if (r2 < pkt_end) goto <access okay>
3035 * <handle exception>
3038 * r2 == dst_reg, pkt_end == src_reg
3039 * r2=pkt(id=n,off=8,r=0)
3040 * r3=pkt(id=n,off=0,r=0)
3042 * pkt_data in src register:
3046 * if (pkt_end >= r2) goto <access okay>
3047 * <handle exception>
3051 * if (pkt_end <= r2) goto <handle exception>
3055 * pkt_end == dst_reg, r2 == src_reg
3056 * r2=pkt(id=n,off=8,r=0)
3057 * r3=pkt(id=n,off=0,r=0)
3059 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
3060 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
3061 * and [r3, r3 + 8-1) respectively is safe to access depending on
3065 /* If our ids match, then we must have the same max_value. And we
3066 * don't care about the other reg's fixed offset, since if it's too big
3067 * the range won't allow anything.
3068 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
3070 for (i
= 0; i
< MAX_BPF_REG
; i
++)
3071 if (regs
[i
].type
== type
&& regs
[i
].id
== dst_reg
->id
)
3072 /* keep the maximum range already checked */
3073 regs
[i
].range
= max(regs
[i
].range
, new_range
);
3075 for (j
= 0; j
<= vstate
->curframe
; j
++) {
3076 state
= vstate
->frame
[j
];
3077 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
3078 if (state
->stack
[i
].slot_type
[0] != STACK_SPILL
)
3080 reg
= &state
->stack
[i
].spilled_ptr
;
3081 if (reg
->type
== type
&& reg
->id
== dst_reg
->id
)
3082 reg
->range
= max(reg
->range
, new_range
);
3087 /* Adjusts the register min/max values in the case that the dst_reg is the
3088 * variable register that we are working on, and src_reg is a constant or we're
3089 * simply doing a BPF_K check.
3090 * In JEQ/JNE cases we also adjust the var_off values.
3092 static void reg_set_min_max(struct bpf_reg_state
*true_reg
,
3093 struct bpf_reg_state
*false_reg
, u64 val
,
3096 /* If the dst_reg is a pointer, we can't learn anything about its
3097 * variable offset from the compare (unless src_reg were a pointer into
3098 * the same object, but we don't bother with that.
3099 * Since false_reg and true_reg have the same type by construction, we
3100 * only need to check one of them for pointerness.
3102 if (__is_pointer_value(false, false_reg
))
3107 /* If this is false then we know nothing Jon Snow, but if it is
3108 * true then we know for sure.
3110 __mark_reg_known(true_reg
, val
);
3113 /* If this is true we know nothing Jon Snow, but if it is false
3114 * we know the value for sure;
3116 __mark_reg_known(false_reg
, val
);
3119 false_reg
->umax_value
= min(false_reg
->umax_value
, val
);
3120 true_reg
->umin_value
= max(true_reg
->umin_value
, val
+ 1);
3123 false_reg
->smax_value
= min_t(s64
, false_reg
->smax_value
, val
);
3124 true_reg
->smin_value
= max_t(s64
, true_reg
->smin_value
, val
+ 1);
3127 false_reg
->umin_value
= max(false_reg
->umin_value
, val
);
3128 true_reg
->umax_value
= min(true_reg
->umax_value
, val
- 1);
3131 false_reg
->smin_value
= max_t(s64
, false_reg
->smin_value
, val
);
3132 true_reg
->smax_value
= min_t(s64
, true_reg
->smax_value
, val
- 1);
3135 false_reg
->umax_value
= min(false_reg
->umax_value
, val
- 1);
3136 true_reg
->umin_value
= max(true_reg
->umin_value
, val
);
3139 false_reg
->smax_value
= min_t(s64
, false_reg
->smax_value
, val
- 1);
3140 true_reg
->smin_value
= max_t(s64
, true_reg
->smin_value
, val
);
3143 false_reg
->umin_value
= max(false_reg
->umin_value
, val
+ 1);
3144 true_reg
->umax_value
= min(true_reg
->umax_value
, val
);
3147 false_reg
->smin_value
= max_t(s64
, false_reg
->smin_value
, val
+ 1);
3148 true_reg
->smax_value
= min_t(s64
, true_reg
->smax_value
, val
);
3154 __reg_deduce_bounds(false_reg
);
3155 __reg_deduce_bounds(true_reg
);
3156 /* We might have learned some bits from the bounds. */
3157 __reg_bound_offset(false_reg
);
3158 __reg_bound_offset(true_reg
);
3159 /* Intersecting with the old var_off might have improved our bounds
3160 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3161 * then new var_off is (0; 0x7f...fc) which improves our umax.
3163 __update_reg_bounds(false_reg
);
3164 __update_reg_bounds(true_reg
);
3167 /* Same as above, but for the case that dst_reg holds a constant and src_reg is
3170 static void reg_set_min_max_inv(struct bpf_reg_state
*true_reg
,
3171 struct bpf_reg_state
*false_reg
, u64 val
,
3174 if (__is_pointer_value(false, false_reg
))
3179 /* If this is false then we know nothing Jon Snow, but if it is
3180 * true then we know for sure.
3182 __mark_reg_known(true_reg
, val
);
3185 /* If this is true we know nothing Jon Snow, but if it is false
3186 * we know the value for sure;
3188 __mark_reg_known(false_reg
, val
);
3191 true_reg
->umax_value
= min(true_reg
->umax_value
, val
- 1);
3192 false_reg
->umin_value
= max(false_reg
->umin_value
, val
);
3195 true_reg
->smax_value
= min_t(s64
, true_reg
->smax_value
, val
- 1);
3196 false_reg
->smin_value
= max_t(s64
, false_reg
->smin_value
, val
);
3199 true_reg
->umin_value
= max(true_reg
->umin_value
, val
+ 1);
3200 false_reg
->umax_value
= min(false_reg
->umax_value
, val
);
3203 true_reg
->smin_value
= max_t(s64
, true_reg
->smin_value
, val
+ 1);
3204 false_reg
->smax_value
= min_t(s64
, false_reg
->smax_value
, val
);
3207 true_reg
->umax_value
= min(true_reg
->umax_value
, val
);
3208 false_reg
->umin_value
= max(false_reg
->umin_value
, val
+ 1);
3211 true_reg
->smax_value
= min_t(s64
, true_reg
->smax_value
, val
);
3212 false_reg
->smin_value
= max_t(s64
, false_reg
->smin_value
, val
+ 1);
3215 true_reg
->umin_value
= max(true_reg
->umin_value
, val
);
3216 false_reg
->umax_value
= min(false_reg
->umax_value
, val
- 1);
3219 true_reg
->smin_value
= max_t(s64
, true_reg
->smin_value
, val
);
3220 false_reg
->smax_value
= min_t(s64
, false_reg
->smax_value
, val
- 1);
3226 __reg_deduce_bounds(false_reg
);
3227 __reg_deduce_bounds(true_reg
);
3228 /* We might have learned some bits from the bounds. */
3229 __reg_bound_offset(false_reg
);
3230 __reg_bound_offset(true_reg
);
3231 /* Intersecting with the old var_off might have improved our bounds
3232 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3233 * then new var_off is (0; 0x7f...fc) which improves our umax.
3235 __update_reg_bounds(false_reg
);
3236 __update_reg_bounds(true_reg
);
3239 /* Regs are known to be equal, so intersect their min/max/var_off */
3240 static void __reg_combine_min_max(struct bpf_reg_state
*src_reg
,
3241 struct bpf_reg_state
*dst_reg
)
3243 src_reg
->umin_value
= dst_reg
->umin_value
= max(src_reg
->umin_value
,
3244 dst_reg
->umin_value
);
3245 src_reg
->umax_value
= dst_reg
->umax_value
= min(src_reg
->umax_value
,
3246 dst_reg
->umax_value
);
3247 src_reg
->smin_value
= dst_reg
->smin_value
= max(src_reg
->smin_value
,
3248 dst_reg
->smin_value
);
3249 src_reg
->smax_value
= dst_reg
->smax_value
= min(src_reg
->smax_value
,
3250 dst_reg
->smax_value
);
3251 src_reg
->var_off
= dst_reg
->var_off
= tnum_intersect(src_reg
->var_off
,
3253 /* We might have learned new bounds from the var_off. */
3254 __update_reg_bounds(src_reg
);
3255 __update_reg_bounds(dst_reg
);
3256 /* We might have learned something about the sign bit. */
3257 __reg_deduce_bounds(src_reg
);
3258 __reg_deduce_bounds(dst_reg
);
3259 /* We might have learned some bits from the bounds. */
3260 __reg_bound_offset(src_reg
);
3261 __reg_bound_offset(dst_reg
);
3262 /* Intersecting with the old var_off might have improved our bounds
3263 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3264 * then new var_off is (0; 0x7f...fc) which improves our umax.
3266 __update_reg_bounds(src_reg
);
3267 __update_reg_bounds(dst_reg
);
3270 static void reg_combine_min_max(struct bpf_reg_state
*true_src
,
3271 struct bpf_reg_state
*true_dst
,
3272 struct bpf_reg_state
*false_src
,
3273 struct bpf_reg_state
*false_dst
,
3278 __reg_combine_min_max(true_src
, true_dst
);
3281 __reg_combine_min_max(false_src
, false_dst
);
3286 static void mark_map_reg(struct bpf_reg_state
*regs
, u32 regno
, u32 id
,
3289 struct bpf_reg_state
*reg
= ®s
[regno
];
3291 if (reg
->type
== PTR_TO_MAP_VALUE_OR_NULL
&& reg
->id
== id
) {
3292 /* Old offset (both fixed and variable parts) should
3293 * have been known-zero, because we don't allow pointer
3294 * arithmetic on pointers that might be NULL.
3296 if (WARN_ON_ONCE(reg
->smin_value
|| reg
->smax_value
||
3297 !tnum_equals_const(reg
->var_off
, 0) ||
3299 __mark_reg_known_zero(reg
);
3303 reg
->type
= SCALAR_VALUE
;
3304 } else if (reg
->map_ptr
->inner_map_meta
) {
3305 reg
->type
= CONST_PTR_TO_MAP
;
3306 reg
->map_ptr
= reg
->map_ptr
->inner_map_meta
;
3308 reg
->type
= PTR_TO_MAP_VALUE
;
3310 /* We don't need id from this point onwards anymore, thus we
3311 * should better reset it, so that state pruning has chances
3318 /* The logic is similar to find_good_pkt_pointers(), both could eventually
3319 * be folded together at some point.
3321 static void mark_map_regs(struct bpf_verifier_state
*vstate
, u32 regno
,
3324 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
3325 struct bpf_reg_state
*regs
= state
->regs
;
3326 u32 id
= regs
[regno
].id
;
3329 for (i
= 0; i
< MAX_BPF_REG
; i
++)
3330 mark_map_reg(regs
, i
, id
, is_null
);
3332 for (j
= 0; j
<= vstate
->curframe
; j
++) {
3333 state
= vstate
->frame
[j
];
3334 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
3335 if (state
->stack
[i
].slot_type
[0] != STACK_SPILL
)
3337 mark_map_reg(&state
->stack
[i
].spilled_ptr
, 0, id
, is_null
);
3342 static bool try_match_pkt_pointers(const struct bpf_insn
*insn
,
3343 struct bpf_reg_state
*dst_reg
,
3344 struct bpf_reg_state
*src_reg
,
3345 struct bpf_verifier_state
*this_branch
,
3346 struct bpf_verifier_state
*other_branch
)
3348 if (BPF_SRC(insn
->code
) != BPF_X
)
3351 switch (BPF_OP(insn
->code
)) {
3353 if ((dst_reg
->type
== PTR_TO_PACKET
&&
3354 src_reg
->type
== PTR_TO_PACKET_END
) ||
3355 (dst_reg
->type
== PTR_TO_PACKET_META
&&
3356 reg_is_init_pkt_pointer(src_reg
, PTR_TO_PACKET
))) {
3357 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
3358 find_good_pkt_pointers(this_branch
, dst_reg
,
3359 dst_reg
->type
, false);
3360 } else if ((dst_reg
->type
== PTR_TO_PACKET_END
&&
3361 src_reg
->type
== PTR_TO_PACKET
) ||
3362 (reg_is_init_pkt_pointer(dst_reg
, PTR_TO_PACKET
) &&
3363 src_reg
->type
== PTR_TO_PACKET_META
)) {
3364 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
3365 find_good_pkt_pointers(other_branch
, src_reg
,
3366 src_reg
->type
, true);
3372 if ((dst_reg
->type
== PTR_TO_PACKET
&&
3373 src_reg
->type
== PTR_TO_PACKET_END
) ||
3374 (dst_reg
->type
== PTR_TO_PACKET_META
&&
3375 reg_is_init_pkt_pointer(src_reg
, PTR_TO_PACKET
))) {
3376 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
3377 find_good_pkt_pointers(other_branch
, dst_reg
,
3378 dst_reg
->type
, true);
3379 } else if ((dst_reg
->type
== PTR_TO_PACKET_END
&&
3380 src_reg
->type
== PTR_TO_PACKET
) ||
3381 (reg_is_init_pkt_pointer(dst_reg
, PTR_TO_PACKET
) &&
3382 src_reg
->type
== PTR_TO_PACKET_META
)) {
3383 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
3384 find_good_pkt_pointers(this_branch
, src_reg
,
3385 src_reg
->type
, false);
3391 if ((dst_reg
->type
== PTR_TO_PACKET
&&
3392 src_reg
->type
== PTR_TO_PACKET_END
) ||
3393 (dst_reg
->type
== PTR_TO_PACKET_META
&&
3394 reg_is_init_pkt_pointer(src_reg
, PTR_TO_PACKET
))) {
3395 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
3396 find_good_pkt_pointers(this_branch
, dst_reg
,
3397 dst_reg
->type
, true);
3398 } else if ((dst_reg
->type
== PTR_TO_PACKET_END
&&
3399 src_reg
->type
== PTR_TO_PACKET
) ||
3400 (reg_is_init_pkt_pointer(dst_reg
, PTR_TO_PACKET
) &&
3401 src_reg
->type
== PTR_TO_PACKET_META
)) {
3402 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
3403 find_good_pkt_pointers(other_branch
, src_reg
,
3404 src_reg
->type
, false);
3410 if ((dst_reg
->type
== PTR_TO_PACKET
&&
3411 src_reg
->type
== PTR_TO_PACKET_END
) ||
3412 (dst_reg
->type
== PTR_TO_PACKET_META
&&
3413 reg_is_init_pkt_pointer(src_reg
, PTR_TO_PACKET
))) {
3414 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
3415 find_good_pkt_pointers(other_branch
, dst_reg
,
3416 dst_reg
->type
, false);
3417 } else if ((dst_reg
->type
== PTR_TO_PACKET_END
&&
3418 src_reg
->type
== PTR_TO_PACKET
) ||
3419 (reg_is_init_pkt_pointer(dst_reg
, PTR_TO_PACKET
) &&
3420 src_reg
->type
== PTR_TO_PACKET_META
)) {
3421 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
3422 find_good_pkt_pointers(this_branch
, src_reg
,
3423 src_reg
->type
, true);
3435 static int check_cond_jmp_op(struct bpf_verifier_env
*env
,
3436 struct bpf_insn
*insn
, int *insn_idx
)
3438 struct bpf_verifier_state
*this_branch
= env
->cur_state
;
3439 struct bpf_verifier_state
*other_branch
;
3440 struct bpf_reg_state
*regs
= this_branch
->frame
[this_branch
->curframe
]->regs
;
3441 struct bpf_reg_state
*dst_reg
, *other_branch_regs
;
3442 u8 opcode
= BPF_OP(insn
->code
);
3445 if (opcode
> BPF_JSLE
) {
3446 verbose(env
, "invalid BPF_JMP opcode %x\n", opcode
);
3450 if (BPF_SRC(insn
->code
) == BPF_X
) {
3451 if (insn
->imm
!= 0) {
3452 verbose(env
, "BPF_JMP uses reserved fields\n");
3456 /* check src1 operand */
3457 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
3461 if (is_pointer_value(env
, insn
->src_reg
)) {
3462 verbose(env
, "R%d pointer comparison prohibited\n",
3467 if (insn
->src_reg
!= BPF_REG_0
) {
3468 verbose(env
, "BPF_JMP uses reserved fields\n");
3473 /* check src2 operand */
3474 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
3478 dst_reg
= ®s
[insn
->dst_reg
];
3480 /* detect if R == 0 where R was initialized to zero earlier */
3481 if (BPF_SRC(insn
->code
) == BPF_K
&&
3482 (opcode
== BPF_JEQ
|| opcode
== BPF_JNE
) &&
3483 dst_reg
->type
== SCALAR_VALUE
&&
3484 tnum_is_const(dst_reg
->var_off
)) {
3485 if ((opcode
== BPF_JEQ
&& dst_reg
->var_off
.value
== insn
->imm
) ||
3486 (opcode
== BPF_JNE
&& dst_reg
->var_off
.value
!= insn
->imm
)) {
3487 /* if (imm == imm) goto pc+off;
3488 * only follow the goto, ignore fall-through
3490 *insn_idx
+= insn
->off
;
3493 /* if (imm != imm) goto pc+off;
3494 * only follow fall-through branch, since
3495 * that's where the program will go
3501 other_branch
= push_stack(env
, *insn_idx
+ insn
->off
+ 1, *insn_idx
);
3504 other_branch_regs
= other_branch
->frame
[other_branch
->curframe
]->regs
;
3506 /* detect if we are comparing against a constant value so we can adjust
3507 * our min/max values for our dst register.
3508 * this is only legit if both are scalars (or pointers to the same
3509 * object, I suppose, but we don't support that right now), because
3510 * otherwise the different base pointers mean the offsets aren't
3513 if (BPF_SRC(insn
->code
) == BPF_X
) {
3514 if (dst_reg
->type
== SCALAR_VALUE
&&
3515 regs
[insn
->src_reg
].type
== SCALAR_VALUE
) {
3516 if (tnum_is_const(regs
[insn
->src_reg
].var_off
))
3517 reg_set_min_max(&other_branch_regs
[insn
->dst_reg
],
3518 dst_reg
, regs
[insn
->src_reg
].var_off
.value
,
3520 else if (tnum_is_const(dst_reg
->var_off
))
3521 reg_set_min_max_inv(&other_branch_regs
[insn
->src_reg
],
3522 ®s
[insn
->src_reg
],
3523 dst_reg
->var_off
.value
, opcode
);
3524 else if (opcode
== BPF_JEQ
|| opcode
== BPF_JNE
)
3525 /* Comparing for equality, we can combine knowledge */
3526 reg_combine_min_max(&other_branch_regs
[insn
->src_reg
],
3527 &other_branch_regs
[insn
->dst_reg
],
3528 ®s
[insn
->src_reg
],
3529 ®s
[insn
->dst_reg
], opcode
);
3531 } else if (dst_reg
->type
== SCALAR_VALUE
) {
3532 reg_set_min_max(&other_branch_regs
[insn
->dst_reg
],
3533 dst_reg
, insn
->imm
, opcode
);
3536 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
3537 if (BPF_SRC(insn
->code
) == BPF_K
&&
3538 insn
->imm
== 0 && (opcode
== BPF_JEQ
|| opcode
== BPF_JNE
) &&
3539 dst_reg
->type
== PTR_TO_MAP_VALUE_OR_NULL
) {
3540 /* Mark all identical map registers in each branch as either
3541 * safe or unknown depending R == 0 or R != 0 conditional.
3543 mark_map_regs(this_branch
, insn
->dst_reg
, opcode
== BPF_JNE
);
3544 mark_map_regs(other_branch
, insn
->dst_reg
, opcode
== BPF_JEQ
);
3545 } else if (!try_match_pkt_pointers(insn
, dst_reg
, ®s
[insn
->src_reg
],
3546 this_branch
, other_branch
) &&
3547 is_pointer_value(env
, insn
->dst_reg
)) {
3548 verbose(env
, "R%d pointer comparison prohibited\n",
3553 print_verifier_state(env
, this_branch
->frame
[this_branch
->curframe
]);
3557 /* return the map pointer stored inside BPF_LD_IMM64 instruction */
3558 static struct bpf_map
*ld_imm64_to_map_ptr(struct bpf_insn
*insn
)
3560 u64 imm64
= ((u64
) (u32
) insn
[0].imm
) | ((u64
) (u32
) insn
[1].imm
) << 32;
3562 return (struct bpf_map
*) (unsigned long) imm64
;
3565 /* verify BPF_LD_IMM64 instruction */
3566 static int check_ld_imm(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
3568 struct bpf_reg_state
*regs
= cur_regs(env
);
3571 if (BPF_SIZE(insn
->code
) != BPF_DW
) {
3572 verbose(env
, "invalid BPF_LD_IMM insn\n");
3575 if (insn
->off
!= 0) {
3576 verbose(env
, "BPF_LD_IMM64 uses reserved fields\n");
3580 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP
);
3584 if (insn
->src_reg
== 0) {
3585 u64 imm
= ((u64
)(insn
+ 1)->imm
<< 32) | (u32
)insn
->imm
;
3587 regs
[insn
->dst_reg
].type
= SCALAR_VALUE
;
3588 __mark_reg_known(®s
[insn
->dst_reg
], imm
);
3592 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
3593 BUG_ON(insn
->src_reg
!= BPF_PSEUDO_MAP_FD
);
3595 regs
[insn
->dst_reg
].type
= CONST_PTR_TO_MAP
;
3596 regs
[insn
->dst_reg
].map_ptr
= ld_imm64_to_map_ptr(insn
);
3600 static bool may_access_skb(enum bpf_prog_type type
)
3603 case BPF_PROG_TYPE_SOCKET_FILTER
:
3604 case BPF_PROG_TYPE_SCHED_CLS
:
3605 case BPF_PROG_TYPE_SCHED_ACT
:
3612 /* verify safety of LD_ABS|LD_IND instructions:
3613 * - they can only appear in the programs where ctx == skb
3614 * - since they are wrappers of function calls, they scratch R1-R5 registers,
3615 * preserve R6-R9, and store return value into R0
3618 * ctx == skb == R6 == CTX
3621 * SRC == any register
3622 * IMM == 32-bit immediate
3625 * R0 - 8/16/32-bit skb data converted to cpu endianness
3627 static int check_ld_abs(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
3629 struct bpf_reg_state
*regs
= cur_regs(env
);
3630 u8 mode
= BPF_MODE(insn
->code
);
3633 if (!may_access_skb(env
->prog
->type
)) {
3634 verbose(env
, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
3638 if (env
->subprog_cnt
) {
3639 /* when program has LD_ABS insn JITs and interpreter assume
3640 * that r1 == ctx == skb which is not the case for callees
3641 * that can have arbitrary arguments. It's problematic
3642 * for main prog as well since JITs would need to analyze
3643 * all functions in order to make proper register save/restore
3644 * decisions in the main prog. Hence disallow LD_ABS with calls
3646 verbose(env
, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
3650 if (insn
->dst_reg
!= BPF_REG_0
|| insn
->off
!= 0 ||
3651 BPF_SIZE(insn
->code
) == BPF_DW
||
3652 (mode
== BPF_ABS
&& insn
->src_reg
!= BPF_REG_0
)) {
3653 verbose(env
, "BPF_LD_[ABS|IND] uses reserved fields\n");
3657 /* check whether implicit source operand (register R6) is readable */
3658 err
= check_reg_arg(env
, BPF_REG_6
, SRC_OP
);
3662 if (regs
[BPF_REG_6
].type
!= PTR_TO_CTX
) {
3664 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
3668 if (mode
== BPF_IND
) {
3669 /* check explicit source operand */
3670 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
3675 /* reset caller saved regs to unreadable */
3676 for (i
= 0; i
< CALLER_SAVED_REGS
; i
++) {
3677 mark_reg_not_init(env
, regs
, caller_saved
[i
]);
3678 check_reg_arg(env
, caller_saved
[i
], DST_OP_NO_MARK
);
3681 /* mark destination R0 register as readable, since it contains
3682 * the value fetched from the packet.
3683 * Already marked as written above.
3685 mark_reg_unknown(env
, regs
, BPF_REG_0
);
3689 static int check_return_code(struct bpf_verifier_env
*env
)
3691 struct bpf_reg_state
*reg
;
3692 struct tnum range
= tnum_range(0, 1);
3694 switch (env
->prog
->type
) {
3695 case BPF_PROG_TYPE_CGROUP_SKB
:
3696 case BPF_PROG_TYPE_CGROUP_SOCK
:
3697 case BPF_PROG_TYPE_SOCK_OPS
:
3698 case BPF_PROG_TYPE_CGROUP_DEVICE
:
3704 reg
= cur_regs(env
) + BPF_REG_0
;
3705 if (reg
->type
!= SCALAR_VALUE
) {
3706 verbose(env
, "At program exit the register R0 is not a known value (%s)\n",
3707 reg_type_str
[reg
->type
]);
3711 if (!tnum_in(range
, reg
->var_off
)) {
3712 verbose(env
, "At program exit the register R0 ");
3713 if (!tnum_is_unknown(reg
->var_off
)) {
3716 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
3717 verbose(env
, "has value %s", tn_buf
);
3719 verbose(env
, "has unknown scalar value");
3721 verbose(env
, " should have been 0 or 1\n");
3727 /* non-recursive DFS pseudo code
3728 * 1 procedure DFS-iterative(G,v):
3729 * 2 label v as discovered
3730 * 3 let S be a stack
3732 * 5 while S is not empty
3734 * 7 if t is what we're looking for:
3736 * 9 for all edges e in G.adjacentEdges(t) do
3737 * 10 if edge e is already labelled
3738 * 11 continue with the next edge
3739 * 12 w <- G.adjacentVertex(t,e)
3740 * 13 if vertex w is not discovered and not explored
3741 * 14 label e as tree-edge
3742 * 15 label w as discovered
3745 * 18 else if vertex w is discovered
3746 * 19 label e as back-edge
3748 * 21 // vertex w is explored
3749 * 22 label e as forward- or cross-edge
3750 * 23 label t as explored
3755 * 0x11 - discovered and fall-through edge labelled
3756 * 0x12 - discovered and fall-through and branch edges labelled
3767 #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
3769 static int *insn_stack
; /* stack of insns to process */
3770 static int cur_stack
; /* current stack index */
3771 static int *insn_state
;
3773 /* t, w, e - match pseudo-code above:
3774 * t - index of current instruction
3775 * w - next instruction
3778 static int push_insn(int t
, int w
, int e
, struct bpf_verifier_env
*env
)
3780 if (e
== FALLTHROUGH
&& insn_state
[t
] >= (DISCOVERED
| FALLTHROUGH
))
3783 if (e
== BRANCH
&& insn_state
[t
] >= (DISCOVERED
| BRANCH
))
3786 if (w
< 0 || w
>= env
->prog
->len
) {
3787 verbose(env
, "jump out of range from insn %d to %d\n", t
, w
);
3792 /* mark branch target for state pruning */
3793 env
->explored_states
[w
] = STATE_LIST_MARK
;
3795 if (insn_state
[w
] == 0) {
3797 insn_state
[t
] = DISCOVERED
| e
;
3798 insn_state
[w
] = DISCOVERED
;
3799 if (cur_stack
>= env
->prog
->len
)
3801 insn_stack
[cur_stack
++] = w
;
3803 } else if ((insn_state
[w
] & 0xF0) == DISCOVERED
) {
3804 verbose(env
, "back-edge from insn %d to %d\n", t
, w
);
3806 } else if (insn_state
[w
] == EXPLORED
) {
3807 /* forward- or cross-edge */
3808 insn_state
[t
] = DISCOVERED
| e
;
3810 verbose(env
, "insn state internal bug\n");
3816 /* non-recursive depth-first-search to detect loops in BPF program
3817 * loop == back-edge in directed graph
3819 static int check_cfg(struct bpf_verifier_env
*env
)
3821 struct bpf_insn
*insns
= env
->prog
->insnsi
;
3822 int insn_cnt
= env
->prog
->len
;
3826 ret
= check_subprogs(env
);
3830 insn_state
= kcalloc(insn_cnt
, sizeof(int), GFP_KERNEL
);
3834 insn_stack
= kcalloc(insn_cnt
, sizeof(int), GFP_KERNEL
);
3840 insn_state
[0] = DISCOVERED
; /* mark 1st insn as discovered */
3841 insn_stack
[0] = 0; /* 0 is the first instruction */
3847 t
= insn_stack
[cur_stack
- 1];
3849 if (BPF_CLASS(insns
[t
].code
) == BPF_JMP
) {
3850 u8 opcode
= BPF_OP(insns
[t
].code
);
3852 if (opcode
== BPF_EXIT
) {
3854 } else if (opcode
== BPF_CALL
) {
3855 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
3860 if (t
+ 1 < insn_cnt
)
3861 env
->explored_states
[t
+ 1] = STATE_LIST_MARK
;
3862 if (insns
[t
].src_reg
== BPF_PSEUDO_CALL
) {
3863 env
->explored_states
[t
] = STATE_LIST_MARK
;
3864 ret
= push_insn(t
, t
+ insns
[t
].imm
+ 1, BRANCH
, env
);
3870 } else if (opcode
== BPF_JA
) {
3871 if (BPF_SRC(insns
[t
].code
) != BPF_K
) {
3875 /* unconditional jump with single edge */
3876 ret
= push_insn(t
, t
+ insns
[t
].off
+ 1,
3882 /* tell verifier to check for equivalent states
3883 * after every call and jump
3885 if (t
+ 1 < insn_cnt
)
3886 env
->explored_states
[t
+ 1] = STATE_LIST_MARK
;
3888 /* conditional jump with two edges */
3889 env
->explored_states
[t
] = STATE_LIST_MARK
;
3890 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
3896 ret
= push_insn(t
, t
+ insns
[t
].off
+ 1, BRANCH
, env
);
3903 /* all other non-branch instructions with single
3906 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
3914 insn_state
[t
] = EXPLORED
;
3915 if (cur_stack
-- <= 0) {
3916 verbose(env
, "pop stack internal bug\n");
3923 for (i
= 0; i
< insn_cnt
; i
++) {
3924 if (insn_state
[i
] != EXPLORED
) {
3925 verbose(env
, "unreachable insn %d\n", i
);
3930 ret
= 0; /* cfg looks good */
3938 /* check %cur's range satisfies %old's */
3939 static bool range_within(struct bpf_reg_state
*old
,
3940 struct bpf_reg_state
*cur
)
3942 return old
->umin_value
<= cur
->umin_value
&&
3943 old
->umax_value
>= cur
->umax_value
&&
3944 old
->smin_value
<= cur
->smin_value
&&
3945 old
->smax_value
>= cur
->smax_value
;
3948 /* Maximum number of register states that can exist at once */
3949 #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
3955 /* If in the old state two registers had the same id, then they need to have
3956 * the same id in the new state as well. But that id could be different from
3957 * the old state, so we need to track the mapping from old to new ids.
3958 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
3959 * regs with old id 5 must also have new id 9 for the new state to be safe. But
3960 * regs with a different old id could still have new id 9, we don't care about
3962 * So we look through our idmap to see if this old id has been seen before. If
3963 * so, we require the new id to match; otherwise, we add the id pair to the map.
3965 static bool check_ids(u32 old_id
, u32 cur_id
, struct idpair
*idmap
)
3969 for (i
= 0; i
< ID_MAP_SIZE
; i
++) {
3970 if (!idmap
[i
].old
) {
3971 /* Reached an empty slot; haven't seen this id before */
3972 idmap
[i
].old
= old_id
;
3973 idmap
[i
].cur
= cur_id
;
3976 if (idmap
[i
].old
== old_id
)
3977 return idmap
[i
].cur
== cur_id
;
3979 /* We ran out of idmap slots, which should be impossible */
3984 /* Returns true if (rold safe implies rcur safe) */
3985 static bool regsafe(struct bpf_reg_state
*rold
, struct bpf_reg_state
*rcur
,
3986 struct idpair
*idmap
)
3990 if (!(rold
->live
& REG_LIVE_READ
))
3991 /* explored state didn't use this */
3994 equal
= memcmp(rold
, rcur
, offsetof(struct bpf_reg_state
, frameno
)) == 0;
3996 if (rold
->type
== PTR_TO_STACK
)
3997 /* two stack pointers are equal only if they're pointing to
3998 * the same stack frame, since fp-8 in foo != fp-8 in bar
4000 return equal
&& rold
->frameno
== rcur
->frameno
;
4005 if (rold
->type
== NOT_INIT
)
4006 /* explored state can't have used this */
4008 if (rcur
->type
== NOT_INIT
)
4010 switch (rold
->type
) {
4012 if (rcur
->type
== SCALAR_VALUE
) {
4013 /* new val must satisfy old val knowledge */
4014 return range_within(rold
, rcur
) &&
4015 tnum_in(rold
->var_off
, rcur
->var_off
);
4017 /* if we knew anything about the old value, we're not
4018 * equal, because we can't know anything about the
4019 * scalar value of the pointer in the new value.
4021 return rold
->umin_value
== 0 &&
4022 rold
->umax_value
== U64_MAX
&&
4023 rold
->smin_value
== S64_MIN
&&
4024 rold
->smax_value
== S64_MAX
&&
4025 tnum_is_unknown(rold
->var_off
);
4027 case PTR_TO_MAP_VALUE
:
4028 /* If the new min/max/var_off satisfy the old ones and
4029 * everything else matches, we are OK.
4030 * We don't care about the 'id' value, because nothing
4031 * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL)
4033 return memcmp(rold
, rcur
, offsetof(struct bpf_reg_state
, id
)) == 0 &&
4034 range_within(rold
, rcur
) &&
4035 tnum_in(rold
->var_off
, rcur
->var_off
);
4036 case PTR_TO_MAP_VALUE_OR_NULL
:
4037 /* a PTR_TO_MAP_VALUE could be safe to use as a
4038 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
4039 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
4040 * checked, doing so could have affected others with the same
4041 * id, and we can't check for that because we lost the id when
4042 * we converted to a PTR_TO_MAP_VALUE.
4044 if (rcur
->type
!= PTR_TO_MAP_VALUE_OR_NULL
)
4046 if (memcmp(rold
, rcur
, offsetof(struct bpf_reg_state
, id
)))
4048 /* Check our ids match any regs they're supposed to */
4049 return check_ids(rold
->id
, rcur
->id
, idmap
);
4050 case PTR_TO_PACKET_META
:
4052 if (rcur
->type
!= rold
->type
)
4054 /* We must have at least as much range as the old ptr
4055 * did, so that any accesses which were safe before are
4056 * still safe. This is true even if old range < old off,
4057 * since someone could have accessed through (ptr - k), or
4058 * even done ptr -= k in a register, to get a safe access.
4060 if (rold
->range
> rcur
->range
)
4062 /* If the offsets don't match, we can't trust our alignment;
4063 * nor can we be sure that we won't fall out of range.
4065 if (rold
->off
!= rcur
->off
)
4067 /* id relations must be preserved */
4068 if (rold
->id
&& !check_ids(rold
->id
, rcur
->id
, idmap
))
4070 /* new val must satisfy old val knowledge */
4071 return range_within(rold
, rcur
) &&
4072 tnum_in(rold
->var_off
, rcur
->var_off
);
4074 case CONST_PTR_TO_MAP
:
4075 case PTR_TO_PACKET_END
:
4076 /* Only valid matches are exact, which memcmp() above
4077 * would have accepted
4080 /* Don't know what's going on, just say it's not safe */
4084 /* Shouldn't get here; if we do, say it's not safe */
4089 static bool stacksafe(struct bpf_func_state
*old
,
4090 struct bpf_func_state
*cur
,
4091 struct idpair
*idmap
)
4095 /* if explored stack has more populated slots than current stack
4096 * such stacks are not equivalent
4098 if (old
->allocated_stack
> cur
->allocated_stack
)
4101 /* walk slots of the explored stack and ignore any additional
4102 * slots in the current stack, since explored(safe) state
4105 for (i
= 0; i
< old
->allocated_stack
; i
++) {
4106 spi
= i
/ BPF_REG_SIZE
;
4108 if (!(old
->stack
[spi
].spilled_ptr
.live
& REG_LIVE_READ
))
4109 /* explored state didn't use this */
4112 if (old
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
] == STACK_INVALID
)
4114 /* if old state was safe with misc data in the stack
4115 * it will be safe with zero-initialized stack.
4116 * The opposite is not true
4118 if (old
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
] == STACK_MISC
&&
4119 cur
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
] == STACK_ZERO
)
4121 if (old
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
] !=
4122 cur
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
])
4123 /* Ex: old explored (safe) state has STACK_SPILL in
4124 * this stack slot, but current has has STACK_MISC ->
4125 * this verifier states are not equivalent,
4126 * return false to continue verification of this path
4129 if (i
% BPF_REG_SIZE
)
4131 if (old
->stack
[spi
].slot_type
[0] != STACK_SPILL
)
4133 if (!regsafe(&old
->stack
[spi
].spilled_ptr
,
4134 &cur
->stack
[spi
].spilled_ptr
,
4136 /* when explored and current stack slot are both storing
4137 * spilled registers, check that stored pointers types
4138 * are the same as well.
4139 * Ex: explored safe path could have stored
4140 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
4141 * but current path has stored:
4142 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
4143 * such verifier states are not equivalent.
4144 * return false to continue verification of this path
4151 /* compare two verifier states
4153 * all states stored in state_list are known to be valid, since
4154 * verifier reached 'bpf_exit' instruction through them
4156 * this function is called when verifier exploring different branches of
4157 * execution popped from the state stack. If it sees an old state that has
4158 * more strict register state and more strict stack state then this execution
4159 * branch doesn't need to be explored further, since verifier already
4160 * concluded that more strict state leads to valid finish.
4162 * Therefore two states are equivalent if register state is more conservative
4163 * and explored stack state is more conservative than the current one.
4166 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
4167 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
4169 * In other words if current stack state (one being explored) has more
4170 * valid slots than old one that already passed validation, it means
4171 * the verifier can stop exploring and conclude that current state is valid too
4173 * Similarly with registers. If explored state has register type as invalid
4174 * whereas register type in current state is meaningful, it means that
4175 * the current state will reach 'bpf_exit' instruction safely
4177 static bool func_states_equal(struct bpf_func_state
*old
,
4178 struct bpf_func_state
*cur
)
4180 struct idpair
*idmap
;
4184 idmap
= kcalloc(ID_MAP_SIZE
, sizeof(struct idpair
), GFP_KERNEL
);
4185 /* If we failed to allocate the idmap, just say it's not safe */
4189 for (i
= 0; i
< MAX_BPF_REG
; i
++) {
4190 if (!regsafe(&old
->regs
[i
], &cur
->regs
[i
], idmap
))
4194 if (!stacksafe(old
, cur
, idmap
))
4202 static bool states_equal(struct bpf_verifier_env
*env
,
4203 struct bpf_verifier_state
*old
,
4204 struct bpf_verifier_state
*cur
)
4208 if (old
->curframe
!= cur
->curframe
)
4211 /* for states to be equal callsites have to be the same
4212 * and all frame states need to be equivalent
4214 for (i
= 0; i
<= old
->curframe
; i
++) {
4215 if (old
->frame
[i
]->callsite
!= cur
->frame
[i
]->callsite
)
4217 if (!func_states_equal(old
->frame
[i
], cur
->frame
[i
]))
4223 /* A write screens off any subsequent reads; but write marks come from the
4224 * straight-line code between a state and its parent. When we arrive at an
4225 * equivalent state (jump target or such) we didn't arrive by the straight-line
4226 * code, so read marks in the state must propagate to the parent regardless
4227 * of the state's write marks. That's what 'parent == state->parent' comparison
4228 * in mark_reg_read() and mark_stack_slot_read() is for.
4230 static int propagate_liveness(struct bpf_verifier_env
*env
,
4231 const struct bpf_verifier_state
*vstate
,
4232 struct bpf_verifier_state
*vparent
)
4234 int i
, frame
, err
= 0;
4235 struct bpf_func_state
*state
, *parent
;
4237 if (vparent
->curframe
!= vstate
->curframe
) {
4238 WARN(1, "propagate_live: parent frame %d current frame %d\n",
4239 vparent
->curframe
, vstate
->curframe
);
4242 /* Propagate read liveness of registers... */
4243 BUILD_BUG_ON(BPF_REG_FP
+ 1 != MAX_BPF_REG
);
4244 /* We don't need to worry about FP liveness because it's read-only */
4245 for (i
= 0; i
< BPF_REG_FP
; i
++) {
4246 if (vparent
->frame
[vparent
->curframe
]->regs
[i
].live
& REG_LIVE_READ
)
4248 if (vstate
->frame
[vstate
->curframe
]->regs
[i
].live
& REG_LIVE_READ
) {
4249 err
= mark_reg_read(env
, vstate
, vparent
, i
);
4255 /* ... and stack slots */
4256 for (frame
= 0; frame
<= vstate
->curframe
; frame
++) {
4257 state
= vstate
->frame
[frame
];
4258 parent
= vparent
->frame
[frame
];
4259 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
&&
4260 i
< parent
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
4261 if (parent
->stack
[i
].spilled_ptr
.live
& REG_LIVE_READ
)
4263 if (state
->stack
[i
].spilled_ptr
.live
& REG_LIVE_READ
)
4264 mark_stack_slot_read(env
, vstate
, vparent
, i
, frame
);
4270 static int is_state_visited(struct bpf_verifier_env
*env
, int insn_idx
)
4272 struct bpf_verifier_state_list
*new_sl
;
4273 struct bpf_verifier_state_list
*sl
;
4274 struct bpf_verifier_state
*cur
= env
->cur_state
;
4277 sl
= env
->explored_states
[insn_idx
];
4279 /* this 'insn_idx' instruction wasn't marked, so we will not
4280 * be doing state search here
4284 while (sl
!= STATE_LIST_MARK
) {
4285 if (states_equal(env
, &sl
->state
, cur
)) {
4286 /* reached equivalent register/stack state,
4288 * Registers read by the continuation are read by us.
4289 * If we have any write marks in env->cur_state, they
4290 * will prevent corresponding reads in the continuation
4291 * from reaching our parent (an explored_state). Our
4292 * own state will get the read marks recorded, but
4293 * they'll be immediately forgotten as we're pruning
4294 * this state and will pop a new one.
4296 err
= propagate_liveness(env
, &sl
->state
, cur
);
4304 /* there were no equivalent states, remember current one.
4305 * technically the current state is not proven to be safe yet,
4306 * but it will either reach outer most bpf_exit (which means it's safe)
4307 * or it will be rejected. Since there are no loops, we won't be
4308 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
4309 * again on the way to bpf_exit
4311 new_sl
= kzalloc(sizeof(struct bpf_verifier_state_list
), GFP_KERNEL
);
4315 /* add new state to the head of linked list */
4316 err
= copy_verifier_state(&new_sl
->state
, cur
);
4318 free_verifier_state(&new_sl
->state
, false);
4322 new_sl
->next
= env
->explored_states
[insn_idx
];
4323 env
->explored_states
[insn_idx
] = new_sl
;
4324 /* connect new state to parentage chain */
4325 cur
->parent
= &new_sl
->state
;
4326 /* clear write marks in current state: the writes we did are not writes
4327 * our child did, so they don't screen off its reads from us.
4328 * (There are no read marks in current state, because reads always mark
4329 * their parent and current state never has children yet. Only
4330 * explored_states can get read marks.)
4332 for (i
= 0; i
< BPF_REG_FP
; i
++)
4333 cur
->frame
[cur
->curframe
]->regs
[i
].live
= REG_LIVE_NONE
;
4335 /* all stack frames are accessible from callee, clear them all */
4336 for (j
= 0; j
<= cur
->curframe
; j
++) {
4337 struct bpf_func_state
*frame
= cur
->frame
[j
];
4339 for (i
= 0; i
< frame
->allocated_stack
/ BPF_REG_SIZE
; i
++)
4340 frame
->stack
[i
].spilled_ptr
.live
= REG_LIVE_NONE
;
4345 static int ext_analyzer_insn_hook(struct bpf_verifier_env
*env
,
4346 int insn_idx
, int prev_insn_idx
)
4348 if (env
->dev_ops
&& env
->dev_ops
->insn_hook
)
4349 return env
->dev_ops
->insn_hook(env
, insn_idx
, prev_insn_idx
);
4354 static int do_check(struct bpf_verifier_env
*env
)
4356 struct bpf_verifier_state
*state
;
4357 struct bpf_insn
*insns
= env
->prog
->insnsi
;
4358 struct bpf_reg_state
*regs
;
4359 int insn_cnt
= env
->prog
->len
, i
;
4360 int insn_idx
, prev_insn_idx
= 0;
4361 int insn_processed
= 0;
4362 bool do_print_state
= false;
4364 state
= kzalloc(sizeof(struct bpf_verifier_state
), GFP_KERNEL
);
4367 state
->curframe
= 0;
4368 state
->parent
= NULL
;
4369 state
->frame
[0] = kzalloc(sizeof(struct bpf_func_state
), GFP_KERNEL
);
4370 if (!state
->frame
[0]) {
4374 env
->cur_state
= state
;
4375 init_func_state(env
, state
->frame
[0],
4376 BPF_MAIN_FUNC
/* callsite */,
4378 0 /* subprogno, zero == main subprog */);
4381 struct bpf_insn
*insn
;
4385 if (insn_idx
>= insn_cnt
) {
4386 verbose(env
, "invalid insn idx %d insn_cnt %d\n",
4387 insn_idx
, insn_cnt
);
4391 insn
= &insns
[insn_idx
];
4392 class = BPF_CLASS(insn
->code
);
4394 if (++insn_processed
> BPF_COMPLEXITY_LIMIT_INSNS
) {
4396 "BPF program is too large. Processed %d insn\n",
4401 err
= is_state_visited(env
, insn_idx
);
4405 /* found equivalent state, can prune the search */
4406 if (env
->log
.level
) {
4408 verbose(env
, "\nfrom %d to %d: safe\n",
4409 prev_insn_idx
, insn_idx
);
4411 verbose(env
, "%d: safe\n", insn_idx
);
4413 goto process_bpf_exit
;
4419 if (env
->log
.level
> 1 || (env
->log
.level
&& do_print_state
)) {
4420 if (env
->log
.level
> 1)
4421 verbose(env
, "%d:", insn_idx
);
4423 verbose(env
, "\nfrom %d to %d:",
4424 prev_insn_idx
, insn_idx
);
4425 print_verifier_state(env
, state
->frame
[state
->curframe
]);
4426 do_print_state
= false;
4429 if (env
->log
.level
) {
4430 verbose(env
, "%d: ", insn_idx
);
4431 print_bpf_insn(verbose
, env
, insn
,
4432 env
->allow_ptr_leaks
);
4435 err
= ext_analyzer_insn_hook(env
, insn_idx
, prev_insn_idx
);
4439 regs
= cur_regs(env
);
4440 env
->insn_aux_data
[insn_idx
].seen
= true;
4441 if (class == BPF_ALU
|| class == BPF_ALU64
) {
4442 err
= check_alu_op(env
, insn
);
4446 } else if (class == BPF_LDX
) {
4447 enum bpf_reg_type
*prev_src_type
, src_reg_type
;
4449 /* check for reserved fields is already done */
4451 /* check src operand */
4452 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
4456 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP_NO_MARK
);
4460 src_reg_type
= regs
[insn
->src_reg
].type
;
4462 /* check that memory (src_reg + off) is readable,
4463 * the state of dst_reg will be updated by this func
4465 err
= check_mem_access(env
, insn_idx
, insn
->src_reg
, insn
->off
,
4466 BPF_SIZE(insn
->code
), BPF_READ
,
4471 prev_src_type
= &env
->insn_aux_data
[insn_idx
].ptr_type
;
4473 if (*prev_src_type
== NOT_INIT
) {
4475 * dst_reg = *(u32 *)(src_reg + off)
4476 * save type to validate intersecting paths
4478 *prev_src_type
= src_reg_type
;
4480 } else if (src_reg_type
!= *prev_src_type
&&
4481 (src_reg_type
== PTR_TO_CTX
||
4482 *prev_src_type
== PTR_TO_CTX
)) {
4483 /* ABuser program is trying to use the same insn
4484 * dst_reg = *(u32*) (src_reg + off)
4485 * with different pointer types:
4486 * src_reg == ctx in one branch and
4487 * src_reg == stack|map in some other branch.
4490 verbose(env
, "same insn cannot be used with different pointers\n");
4494 } else if (class == BPF_STX
) {
4495 enum bpf_reg_type
*prev_dst_type
, dst_reg_type
;
4497 if (BPF_MODE(insn
->code
) == BPF_XADD
) {
4498 err
= check_xadd(env
, insn_idx
, insn
);
4505 /* check src1 operand */
4506 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
4509 /* check src2 operand */
4510 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
4514 dst_reg_type
= regs
[insn
->dst_reg
].type
;
4516 /* check that memory (dst_reg + off) is writeable */
4517 err
= check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
4518 BPF_SIZE(insn
->code
), BPF_WRITE
,
4523 prev_dst_type
= &env
->insn_aux_data
[insn_idx
].ptr_type
;
4525 if (*prev_dst_type
== NOT_INIT
) {
4526 *prev_dst_type
= dst_reg_type
;
4527 } else if (dst_reg_type
!= *prev_dst_type
&&
4528 (dst_reg_type
== PTR_TO_CTX
||
4529 *prev_dst_type
== PTR_TO_CTX
)) {
4530 verbose(env
, "same insn cannot be used with different pointers\n");
4534 } else if (class == BPF_ST
) {
4535 if (BPF_MODE(insn
->code
) != BPF_MEM
||
4536 insn
->src_reg
!= BPF_REG_0
) {
4537 verbose(env
, "BPF_ST uses reserved fields\n");
4540 /* check src operand */
4541 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
4545 /* check that memory (dst_reg + off) is writeable */
4546 err
= check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
4547 BPF_SIZE(insn
->code
), BPF_WRITE
,
4552 } else if (class == BPF_JMP
) {
4553 u8 opcode
= BPF_OP(insn
->code
);
4555 if (opcode
== BPF_CALL
) {
4556 if (BPF_SRC(insn
->code
) != BPF_K
||
4558 (insn
->src_reg
!= BPF_REG_0
&&
4559 insn
->src_reg
!= BPF_PSEUDO_CALL
) ||
4560 insn
->dst_reg
!= BPF_REG_0
) {
4561 verbose(env
, "BPF_CALL uses reserved fields\n");
4565 if (insn
->src_reg
== BPF_PSEUDO_CALL
)
4566 err
= check_func_call(env
, insn
, &insn_idx
);
4568 err
= check_helper_call(env
, insn
->imm
, insn_idx
);
4572 } else if (opcode
== BPF_JA
) {
4573 if (BPF_SRC(insn
->code
) != BPF_K
||
4575 insn
->src_reg
!= BPF_REG_0
||
4576 insn
->dst_reg
!= BPF_REG_0
) {
4577 verbose(env
, "BPF_JA uses reserved fields\n");
4581 insn_idx
+= insn
->off
+ 1;
4584 } else if (opcode
== BPF_EXIT
) {
4585 if (BPF_SRC(insn
->code
) != BPF_K
||
4587 insn
->src_reg
!= BPF_REG_0
||
4588 insn
->dst_reg
!= BPF_REG_0
) {
4589 verbose(env
, "BPF_EXIT uses reserved fields\n");
4593 if (state
->curframe
) {
4594 /* exit from nested function */
4595 prev_insn_idx
= insn_idx
;
4596 err
= prepare_func_exit(env
, &insn_idx
);
4599 do_print_state
= true;
4603 /* eBPF calling convetion is such that R0 is used
4604 * to return the value from eBPF program.
4605 * Make sure that it's readable at this time
4606 * of bpf_exit, which means that program wrote
4607 * something into it earlier
4609 err
= check_reg_arg(env
, BPF_REG_0
, SRC_OP
);
4613 if (is_pointer_value(env
, BPF_REG_0
)) {
4614 verbose(env
, "R0 leaks addr as return value\n");
4618 err
= check_return_code(env
);
4622 err
= pop_stack(env
, &prev_insn_idx
, &insn_idx
);
4628 do_print_state
= true;
4632 err
= check_cond_jmp_op(env
, insn
, &insn_idx
);
4636 } else if (class == BPF_LD
) {
4637 u8 mode
= BPF_MODE(insn
->code
);
4639 if (mode
== BPF_ABS
|| mode
== BPF_IND
) {
4640 err
= check_ld_abs(env
, insn
);
4644 } else if (mode
== BPF_IMM
) {
4645 err
= check_ld_imm(env
, insn
);
4650 env
->insn_aux_data
[insn_idx
].seen
= true;
4652 verbose(env
, "invalid BPF_LD mode\n");
4656 verbose(env
, "unknown insn class %d\n", class);
4663 verbose(env
, "processed %d insns, stack depth ", insn_processed
);
4664 for (i
= 0; i
< env
->subprog_cnt
+ 1; i
++) {
4665 u32 depth
= env
->subprog_stack_depth
[i
];
4667 verbose(env
, "%d", depth
);
4668 if (i
+ 1 < env
->subprog_cnt
+ 1)
4672 env
->prog
->aux
->stack_depth
= env
->subprog_stack_depth
[0];
4676 static int check_map_prealloc(struct bpf_map
*map
)
4678 return (map
->map_type
!= BPF_MAP_TYPE_HASH
&&
4679 map
->map_type
!= BPF_MAP_TYPE_PERCPU_HASH
&&
4680 map
->map_type
!= BPF_MAP_TYPE_HASH_OF_MAPS
) ||
4681 !(map
->map_flags
& BPF_F_NO_PREALLOC
);
4684 static int check_map_prog_compatibility(struct bpf_verifier_env
*env
,
4685 struct bpf_map
*map
,
4686 struct bpf_prog
*prog
)
4689 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
4690 * preallocated hash maps, since doing memory allocation
4691 * in overflow_handler can crash depending on where nmi got
4694 if (prog
->type
== BPF_PROG_TYPE_PERF_EVENT
) {
4695 if (!check_map_prealloc(map
)) {
4696 verbose(env
, "perf_event programs can only use preallocated hash map\n");
4699 if (map
->inner_map_meta
&&
4700 !check_map_prealloc(map
->inner_map_meta
)) {
4701 verbose(env
, "perf_event programs can only use preallocated inner hash map\n");
4708 /* look for pseudo eBPF instructions that access map FDs and
4709 * replace them with actual map pointers
4711 static int replace_map_fd_with_map_ptr(struct bpf_verifier_env
*env
)
4713 struct bpf_insn
*insn
= env
->prog
->insnsi
;
4714 int insn_cnt
= env
->prog
->len
;
4717 err
= bpf_prog_calc_tag(env
->prog
);
4721 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
4722 if (BPF_CLASS(insn
->code
) == BPF_LDX
&&
4723 (BPF_MODE(insn
->code
) != BPF_MEM
|| insn
->imm
!= 0)) {
4724 verbose(env
, "BPF_LDX uses reserved fields\n");
4728 if (BPF_CLASS(insn
->code
) == BPF_STX
&&
4729 ((BPF_MODE(insn
->code
) != BPF_MEM
&&
4730 BPF_MODE(insn
->code
) != BPF_XADD
) || insn
->imm
!= 0)) {
4731 verbose(env
, "BPF_STX uses reserved fields\n");
4735 if (insn
[0].code
== (BPF_LD
| BPF_IMM
| BPF_DW
)) {
4736 struct bpf_map
*map
;
4739 if (i
== insn_cnt
- 1 || insn
[1].code
!= 0 ||
4740 insn
[1].dst_reg
!= 0 || insn
[1].src_reg
!= 0 ||
4742 verbose(env
, "invalid bpf_ld_imm64 insn\n");
4746 if (insn
->src_reg
== 0)
4747 /* valid generic load 64-bit imm */
4750 if (insn
->src_reg
!= BPF_PSEUDO_MAP_FD
) {
4752 "unrecognized bpf_ld_imm64 insn\n");
4756 f
= fdget(insn
->imm
);
4757 map
= __bpf_map_get(f
);
4759 verbose(env
, "fd %d is not pointing to valid bpf_map\n",
4761 return PTR_ERR(map
);
4764 err
= check_map_prog_compatibility(env
, map
, env
->prog
);
4770 /* store map pointer inside BPF_LD_IMM64 instruction */
4771 insn
[0].imm
= (u32
) (unsigned long) map
;
4772 insn
[1].imm
= ((u64
) (unsigned long) map
) >> 32;
4774 /* check whether we recorded this map already */
4775 for (j
= 0; j
< env
->used_map_cnt
; j
++)
4776 if (env
->used_maps
[j
] == map
) {
4781 if (env
->used_map_cnt
>= MAX_USED_MAPS
) {
4786 /* hold the map. If the program is rejected by verifier,
4787 * the map will be released by release_maps() or it
4788 * will be used by the valid program until it's unloaded
4789 * and all maps are released in free_bpf_prog_info()
4791 map
= bpf_map_inc(map
, false);
4794 return PTR_ERR(map
);
4796 env
->used_maps
[env
->used_map_cnt
++] = map
;
4805 /* now all pseudo BPF_LD_IMM64 instructions load valid
4806 * 'struct bpf_map *' into a register instead of user map_fd.
4807 * These pointers will be used later by verifier to validate map access.
4812 /* drop refcnt of maps used by the rejected program */
4813 static void release_maps(struct bpf_verifier_env
*env
)
4817 for (i
= 0; i
< env
->used_map_cnt
; i
++)
4818 bpf_map_put(env
->used_maps
[i
]);
4821 /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
4822 static void convert_pseudo_ld_imm64(struct bpf_verifier_env
*env
)
4824 struct bpf_insn
*insn
= env
->prog
->insnsi
;
4825 int insn_cnt
= env
->prog
->len
;
4828 for (i
= 0; i
< insn_cnt
; i
++, insn
++)
4829 if (insn
->code
== (BPF_LD
| BPF_IMM
| BPF_DW
))
4833 /* single env->prog->insni[off] instruction was replaced with the range
4834 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
4835 * [0, off) and [off, end) to new locations, so the patched range stays zero
4837 static int adjust_insn_aux_data(struct bpf_verifier_env
*env
, u32 prog_len
,
4840 struct bpf_insn_aux_data
*new_data
, *old_data
= env
->insn_aux_data
;
4845 new_data
= vzalloc(sizeof(struct bpf_insn_aux_data
) * prog_len
);
4848 memcpy(new_data
, old_data
, sizeof(struct bpf_insn_aux_data
) * off
);
4849 memcpy(new_data
+ off
+ cnt
- 1, old_data
+ off
,
4850 sizeof(struct bpf_insn_aux_data
) * (prog_len
- off
- cnt
+ 1));
4851 for (i
= off
; i
< off
+ cnt
- 1; i
++)
4852 new_data
[i
].seen
= true;
4853 env
->insn_aux_data
= new_data
;
4858 static void adjust_subprog_starts(struct bpf_verifier_env
*env
, u32 off
, u32 len
)
4864 for (i
= 0; i
< env
->subprog_cnt
; i
++) {
4865 if (env
->subprog_starts
[i
] < off
)
4867 env
->subprog_starts
[i
] += len
- 1;
4871 static struct bpf_prog
*bpf_patch_insn_data(struct bpf_verifier_env
*env
, u32 off
,
4872 const struct bpf_insn
*patch
, u32 len
)
4874 struct bpf_prog
*new_prog
;
4876 new_prog
= bpf_patch_insn_single(env
->prog
, off
, patch
, len
);
4879 if (adjust_insn_aux_data(env
, new_prog
->len
, off
, len
))
4881 adjust_subprog_starts(env
, off
, len
);
4885 /* The verifier does more data flow analysis than llvm and will not explore
4886 * branches that are dead at run time. Malicious programs can have dead code
4887 * too. Therefore replace all dead at-run-time code with nops.
4889 static void sanitize_dead_code(struct bpf_verifier_env
*env
)
4891 struct bpf_insn_aux_data
*aux_data
= env
->insn_aux_data
;
4892 struct bpf_insn nop
= BPF_MOV64_REG(BPF_REG_0
, BPF_REG_0
);
4893 struct bpf_insn
*insn
= env
->prog
->insnsi
;
4894 const int insn_cnt
= env
->prog
->len
;
4897 for (i
= 0; i
< insn_cnt
; i
++) {
4898 if (aux_data
[i
].seen
)
4900 memcpy(insn
+ i
, &nop
, sizeof(nop
));
4904 /* convert load instructions that access fields of 'struct __sk_buff'
4905 * into sequence of instructions that access fields of 'struct sk_buff'
4907 static int convert_ctx_accesses(struct bpf_verifier_env
*env
)
4909 const struct bpf_verifier_ops
*ops
= env
->ops
;
4910 int i
, cnt
, size
, ctx_field_size
, delta
= 0;
4911 const int insn_cnt
= env
->prog
->len
;
4912 struct bpf_insn insn_buf
[16], *insn
;
4913 struct bpf_prog
*new_prog
;
4914 enum bpf_access_type type
;
4915 bool is_narrower_load
;
4918 if (ops
->gen_prologue
) {
4919 cnt
= ops
->gen_prologue(insn_buf
, env
->seen_direct_write
,
4921 if (cnt
>= ARRAY_SIZE(insn_buf
)) {
4922 verbose(env
, "bpf verifier is misconfigured\n");
4925 new_prog
= bpf_patch_insn_data(env
, 0, insn_buf
, cnt
);
4929 env
->prog
= new_prog
;
4934 if (!ops
->convert_ctx_access
)
4937 insn
= env
->prog
->insnsi
+ delta
;
4939 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
4940 if (insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_B
) ||
4941 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_H
) ||
4942 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_W
) ||
4943 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_DW
))
4945 else if (insn
->code
== (BPF_STX
| BPF_MEM
| BPF_B
) ||
4946 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_H
) ||
4947 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_W
) ||
4948 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_DW
))
4953 if (env
->insn_aux_data
[i
+ delta
].ptr_type
!= PTR_TO_CTX
)
4956 ctx_field_size
= env
->insn_aux_data
[i
+ delta
].ctx_field_size
;
4957 size
= BPF_LDST_BYTES(insn
);
4959 /* If the read access is a narrower load of the field,
4960 * convert to a 4/8-byte load, to minimum program type specific
4961 * convert_ctx_access changes. If conversion is successful,
4962 * we will apply proper mask to the result.
4964 is_narrower_load
= size
< ctx_field_size
;
4965 if (is_narrower_load
) {
4966 u32 off
= insn
->off
;
4969 if (type
== BPF_WRITE
) {
4970 verbose(env
, "bpf verifier narrow ctx access misconfigured\n");
4975 if (ctx_field_size
== 4)
4977 else if (ctx_field_size
== 8)
4980 insn
->off
= off
& ~(ctx_field_size
- 1);
4981 insn
->code
= BPF_LDX
| BPF_MEM
| size_code
;
4985 cnt
= ops
->convert_ctx_access(type
, insn
, insn_buf
, env
->prog
,
4987 if (cnt
== 0 || cnt
>= ARRAY_SIZE(insn_buf
) ||
4988 (ctx_field_size
&& !target_size
)) {
4989 verbose(env
, "bpf verifier is misconfigured\n");
4993 if (is_narrower_load
&& size
< target_size
) {
4994 if (ctx_field_size
<= 4)
4995 insn_buf
[cnt
++] = BPF_ALU32_IMM(BPF_AND
, insn
->dst_reg
,
4996 (1 << size
* 8) - 1);
4998 insn_buf
[cnt
++] = BPF_ALU64_IMM(BPF_AND
, insn
->dst_reg
,
4999 (1 << size
* 8) - 1);
5002 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, insn_buf
, cnt
);
5008 /* keep walking new program and skip insns we just inserted */
5009 env
->prog
= new_prog
;
5010 insn
= new_prog
->insnsi
+ i
+ delta
;
5016 static int jit_subprogs(struct bpf_verifier_env
*env
)
5018 struct bpf_prog
*prog
= env
->prog
, **func
, *tmp
;
5019 int i
, j
, subprog_start
, subprog_end
= 0, len
, subprog
;
5020 struct bpf_insn
*insn
= prog
->insnsi
;
5024 if (env
->subprog_cnt
== 0)
5027 for (i
= 0; i
< prog
->len
; i
++, insn
++) {
5028 if (insn
->code
!= (BPF_JMP
| BPF_CALL
) ||
5029 insn
->src_reg
!= BPF_PSEUDO_CALL
)
5031 subprog
= find_subprog(env
, i
+ insn
->imm
+ 1);
5033 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
5037 /* temporarily remember subprog id inside insn instead of
5038 * aux_data, since next loop will split up all insns into funcs
5040 insn
->off
= subprog
+ 1;
5041 /* remember original imm in case JIT fails and fallback
5042 * to interpreter will be needed
5044 env
->insn_aux_data
[i
].call_imm
= insn
->imm
;
5045 /* point imm to __bpf_call_base+1 from JITs point of view */
5049 func
= kzalloc(sizeof(prog
) * (env
->subprog_cnt
+ 1), GFP_KERNEL
);
5053 for (i
= 0; i
<= env
->subprog_cnt
; i
++) {
5054 subprog_start
= subprog_end
;
5055 if (env
->subprog_cnt
== i
)
5056 subprog_end
= prog
->len
;
5058 subprog_end
= env
->subprog_starts
[i
];
5060 len
= subprog_end
- subprog_start
;
5061 func
[i
] = bpf_prog_alloc(bpf_prog_size(len
), GFP_USER
);
5064 memcpy(func
[i
]->insnsi
, &prog
->insnsi
[subprog_start
],
5065 len
* sizeof(struct bpf_insn
));
5067 func
[i
]->is_func
= 1;
5068 /* Use bpf_prog_F_tag to indicate functions in stack traces.
5069 * Long term would need debug info to populate names
5071 func
[i
]->aux
->name
[0] = 'F';
5072 func
[i
]->aux
->stack_depth
= env
->subprog_stack_depth
[i
];
5073 func
[i
]->jit_requested
= 1;
5074 func
[i
] = bpf_int_jit_compile(func
[i
]);
5075 if (!func
[i
]->jited
) {
5081 /* at this point all bpf functions were successfully JITed
5082 * now populate all bpf_calls with correct addresses and
5083 * run last pass of JIT
5085 for (i
= 0; i
<= env
->subprog_cnt
; i
++) {
5086 insn
= func
[i
]->insnsi
;
5087 for (j
= 0; j
< func
[i
]->len
; j
++, insn
++) {
5088 if (insn
->code
!= (BPF_JMP
| BPF_CALL
) ||
5089 insn
->src_reg
!= BPF_PSEUDO_CALL
)
5091 subprog
= insn
->off
;
5093 insn
->imm
= (u64 (*)(u64
, u64
, u64
, u64
, u64
))
5094 func
[subprog
]->bpf_func
-
5098 for (i
= 0; i
<= env
->subprog_cnt
; i
++) {
5099 old_bpf_func
= func
[i
]->bpf_func
;
5100 tmp
= bpf_int_jit_compile(func
[i
]);
5101 if (tmp
!= func
[i
] || func
[i
]->bpf_func
!= old_bpf_func
) {
5102 verbose(env
, "JIT doesn't support bpf-to-bpf calls\n");
5109 /* finally lock prog and jit images for all functions and
5112 for (i
= 0; i
<= env
->subprog_cnt
; i
++) {
5113 bpf_prog_lock_ro(func
[i
]);
5114 bpf_prog_kallsyms_add(func
[i
]);
5117 prog
->bpf_func
= func
[0]->bpf_func
;
5118 prog
->aux
->func
= func
;
5119 prog
->aux
->func_cnt
= env
->subprog_cnt
+ 1;
5122 for (i
= 0; i
<= env
->subprog_cnt
; i
++)
5124 bpf_jit_free(func
[i
]);
5126 /* cleanup main prog to be interpreted */
5127 prog
->jit_requested
= 0;
5128 for (i
= 0, insn
= prog
->insnsi
; i
< prog
->len
; i
++, insn
++) {
5129 if (insn
->code
!= (BPF_JMP
| BPF_CALL
) ||
5130 insn
->src_reg
!= BPF_PSEUDO_CALL
)
5133 insn
->imm
= env
->insn_aux_data
[i
].call_imm
;
5138 static int fixup_call_args(struct bpf_verifier_env
*env
)
5140 struct bpf_prog
*prog
= env
->prog
;
5141 struct bpf_insn
*insn
= prog
->insnsi
;
5144 if (env
->prog
->jit_requested
)
5145 if (jit_subprogs(env
) == 0)
5148 for (i
= 0; i
< prog
->len
; i
++, insn
++) {
5149 if (insn
->code
!= (BPF_JMP
| BPF_CALL
) ||
5150 insn
->src_reg
!= BPF_PSEUDO_CALL
)
5152 depth
= get_callee_stack_depth(env
, insn
, i
);
5155 bpf_patch_call_args(insn
, depth
);
5160 /* fixup insn->imm field of bpf_call instructions
5161 * and inline eligible helpers as explicit sequence of BPF instructions
5163 * this function is called after eBPF program passed verification
5165 static int fixup_bpf_calls(struct bpf_verifier_env
*env
)
5167 struct bpf_prog
*prog
= env
->prog
;
5168 struct bpf_insn
*insn
= prog
->insnsi
;
5169 const struct bpf_func_proto
*fn
;
5170 const int insn_cnt
= prog
->len
;
5171 struct bpf_insn insn_buf
[16];
5172 struct bpf_prog
*new_prog
;
5173 struct bpf_map
*map_ptr
;
5174 int i
, cnt
, delta
= 0;
5176 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
5177 if (insn
->code
!= (BPF_JMP
| BPF_CALL
))
5179 if (insn
->src_reg
== BPF_PSEUDO_CALL
)
5182 if (insn
->imm
== BPF_FUNC_get_route_realm
)
5183 prog
->dst_needed
= 1;
5184 if (insn
->imm
== BPF_FUNC_get_prandom_u32
)
5185 bpf_user_rnd_init_once();
5186 if (insn
->imm
== BPF_FUNC_override_return
)
5187 prog
->kprobe_override
= 1;
5188 if (insn
->imm
== BPF_FUNC_tail_call
) {
5189 /* If we tail call into other programs, we
5190 * cannot make any assumptions since they can
5191 * be replaced dynamically during runtime in
5192 * the program array.
5194 prog
->cb_access
= 1;
5195 env
->prog
->aux
->stack_depth
= MAX_BPF_STACK
;
5197 /* mark bpf_tail_call as different opcode to avoid
5198 * conditional branch in the interpeter for every normal
5199 * call and to prevent accidental JITing by JIT compiler
5200 * that doesn't support bpf_tail_call yet
5203 insn
->code
= BPF_JMP
| BPF_TAIL_CALL
;
5207 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
5208 * handlers are currently limited to 64 bit only.
5210 if (prog
->jit_requested
&& BITS_PER_LONG
== 64 &&
5211 insn
->imm
== BPF_FUNC_map_lookup_elem
) {
5212 map_ptr
= env
->insn_aux_data
[i
+ delta
].map_ptr
;
5213 if (map_ptr
== BPF_MAP_PTR_POISON
||
5214 !map_ptr
->ops
->map_gen_lookup
)
5215 goto patch_call_imm
;
5217 cnt
= map_ptr
->ops
->map_gen_lookup(map_ptr
, insn_buf
);
5218 if (cnt
== 0 || cnt
>= ARRAY_SIZE(insn_buf
)) {
5219 verbose(env
, "bpf verifier is misconfigured\n");
5223 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, insn_buf
,
5230 /* keep walking new program and skip insns we just inserted */
5231 env
->prog
= prog
= new_prog
;
5232 insn
= new_prog
->insnsi
+ i
+ delta
;
5236 if (insn
->imm
== BPF_FUNC_redirect_map
) {
5237 /* Note, we cannot use prog directly as imm as subsequent
5238 * rewrites would still change the prog pointer. The only
5239 * stable address we can use is aux, which also works with
5240 * prog clones during blinding.
5242 u64 addr
= (unsigned long)prog
->aux
;
5243 struct bpf_insn r4_ld
[] = {
5244 BPF_LD_IMM64(BPF_REG_4
, addr
),
5247 cnt
= ARRAY_SIZE(r4_ld
);
5249 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, r4_ld
, cnt
);
5254 env
->prog
= prog
= new_prog
;
5255 insn
= new_prog
->insnsi
+ i
+ delta
;
5258 fn
= env
->ops
->get_func_proto(insn
->imm
);
5259 /* all functions that have prototype and verifier allowed
5260 * programs to call them, must be real in-kernel functions
5264 "kernel subsystem misconfigured func %s#%d\n",
5265 func_id_name(insn
->imm
), insn
->imm
);
5268 insn
->imm
= fn
->func
- __bpf_call_base
;
5274 static void free_states(struct bpf_verifier_env
*env
)
5276 struct bpf_verifier_state_list
*sl
, *sln
;
5279 if (!env
->explored_states
)
5282 for (i
= 0; i
< env
->prog
->len
; i
++) {
5283 sl
= env
->explored_states
[i
];
5286 while (sl
!= STATE_LIST_MARK
) {
5288 free_verifier_state(&sl
->state
, false);
5294 kfree(env
->explored_states
);
5297 int bpf_check(struct bpf_prog
**prog
, union bpf_attr
*attr
)
5299 struct bpf_verifier_env
*env
;
5300 struct bpf_verifer_log
*log
;
5303 /* no program is valid */
5304 if (ARRAY_SIZE(bpf_verifier_ops
) == 0)
5307 /* 'struct bpf_verifier_env' can be global, but since it's not small,
5308 * allocate/free it every time bpf_check() is called
5310 env
= kzalloc(sizeof(struct bpf_verifier_env
), GFP_KERNEL
);
5315 env
->insn_aux_data
= vzalloc(sizeof(struct bpf_insn_aux_data
) *
5318 if (!env
->insn_aux_data
)
5321 env
->ops
= bpf_verifier_ops
[env
->prog
->type
];
5323 /* grab the mutex to protect few globals used by verifier */
5324 mutex_lock(&bpf_verifier_lock
);
5326 if (attr
->log_level
|| attr
->log_buf
|| attr
->log_size
) {
5327 /* user requested verbose verifier output
5328 * and supplied buffer to store the verification trace
5330 log
->level
= attr
->log_level
;
5331 log
->ubuf
= (char __user
*) (unsigned long) attr
->log_buf
;
5332 log
->len_total
= attr
->log_size
;
5335 /* log attributes have to be sane */
5336 if (log
->len_total
< 128 || log
->len_total
> UINT_MAX
>> 8 ||
5337 !log
->level
|| !log
->ubuf
)
5341 env
->strict_alignment
= !!(attr
->prog_flags
& BPF_F_STRICT_ALIGNMENT
);
5342 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
))
5343 env
->strict_alignment
= true;
5345 if (env
->prog
->aux
->offload
) {
5346 ret
= bpf_prog_offload_verifier_prep(env
);
5351 ret
= replace_map_fd_with_map_ptr(env
);
5353 goto skip_full_check
;
5355 env
->explored_states
= kcalloc(env
->prog
->len
,
5356 sizeof(struct bpf_verifier_state_list
*),
5359 if (!env
->explored_states
)
5360 goto skip_full_check
;
5362 env
->allow_ptr_leaks
= capable(CAP_SYS_ADMIN
);
5364 ret
= check_cfg(env
);
5366 goto skip_full_check
;
5368 ret
= do_check(env
);
5369 if (env
->cur_state
) {
5370 free_verifier_state(env
->cur_state
, true);
5371 env
->cur_state
= NULL
;
5375 while (!pop_stack(env
, NULL
, NULL
));
5379 sanitize_dead_code(env
);
5382 /* program is valid, convert *(u32*)(ctx + off) accesses */
5383 ret
= convert_ctx_accesses(env
);
5386 ret
= fixup_bpf_calls(env
);
5389 ret
= fixup_call_args(env
);
5391 if (log
->level
&& bpf_verifier_log_full(log
))
5393 if (log
->level
&& !log
->ubuf
) {
5395 goto err_release_maps
;
5398 if (ret
== 0 && env
->used_map_cnt
) {
5399 /* if program passed verifier, update used_maps in bpf_prog_info */
5400 env
->prog
->aux
->used_maps
= kmalloc_array(env
->used_map_cnt
,
5401 sizeof(env
->used_maps
[0]),
5404 if (!env
->prog
->aux
->used_maps
) {
5406 goto err_release_maps
;
5409 memcpy(env
->prog
->aux
->used_maps
, env
->used_maps
,
5410 sizeof(env
->used_maps
[0]) * env
->used_map_cnt
);
5411 env
->prog
->aux
->used_map_cnt
= env
->used_map_cnt
;
5413 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
5414 * bpf_ld_imm64 instructions
5416 convert_pseudo_ld_imm64(env
);
5420 if (!env
->prog
->aux
->used_maps
)
5421 /* if we didn't copy map pointers into bpf_prog_info, release
5422 * them now. Otherwise free_bpf_prog_info() will release them.
5427 mutex_unlock(&bpf_verifier_lock
);
5428 vfree(env
->insn_aux_data
);