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>
25 #include <linux/perf_event.h>
29 static const struct bpf_verifier_ops
* const bpf_verifier_ops
[] = {
30 #define BPF_PROG_TYPE(_id, _name) \
31 [_id] = & _name ## _verifier_ops,
32 #define BPF_MAP_TYPE(_id, _ops)
33 #include <linux/bpf_types.h>
38 /* bpf_check() is a static code analyzer that walks eBPF program
39 * instruction by instruction and updates register/stack state.
40 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
42 * The first pass is depth-first-search to check that the program is a DAG.
43 * It rejects the following programs:
44 * - larger than BPF_MAXINSNS insns
45 * - if loop is present (detected via back-edge)
46 * - unreachable insns exist (shouldn't be a forest. program = one function)
47 * - out of bounds or malformed jumps
48 * The second pass is all possible path descent from the 1st insn.
49 * Since it's analyzing all pathes through the program, the length of the
50 * analysis is limited to 64k insn, which may be hit even if total number of
51 * insn is less then 4K, but there are too many branches that change stack/regs.
52 * Number of 'branches to be analyzed' is limited to 1k
54 * On entry to each instruction, each register has a type, and the instruction
55 * changes the types of the registers depending on instruction semantics.
56 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
59 * All registers are 64-bit.
60 * R0 - return register
61 * R1-R5 argument passing registers
62 * R6-R9 callee saved registers
63 * R10 - frame pointer read-only
65 * At the start of BPF program the register R1 contains a pointer to bpf_context
66 * and has type PTR_TO_CTX.
68 * Verifier tracks arithmetic operations on pointers in case:
69 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
70 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
71 * 1st insn copies R10 (which has FRAME_PTR) type into R1
72 * and 2nd arithmetic instruction is pattern matched to recognize
73 * that it wants to construct a pointer to some element within stack.
74 * So after 2nd insn, the register R1 has type PTR_TO_STACK
75 * (and -20 constant is saved for further stack bounds checking).
76 * Meaning that this reg is a pointer to stack plus known immediate constant.
78 * Most of the time the registers have SCALAR_VALUE type, which
79 * means the register has some value, but it's not a valid pointer.
80 * (like pointer plus pointer becomes SCALAR_VALUE type)
82 * When verifier sees load or store instructions the type of base register
83 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK. These are three pointer
84 * types recognized by check_mem_access() function.
86 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
87 * and the range of [ptr, ptr + map's value_size) is accessible.
89 * registers used to pass values to function calls are checked against
90 * function argument constraints.
92 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
93 * It means that the register type passed to this function must be
94 * PTR_TO_STACK and it will be used inside the function as
95 * 'pointer to map element key'
97 * For example the argument constraints for bpf_map_lookup_elem():
98 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
99 * .arg1_type = ARG_CONST_MAP_PTR,
100 * .arg2_type = ARG_PTR_TO_MAP_KEY,
102 * ret_type says that this function returns 'pointer to map elem value or null'
103 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
104 * 2nd argument should be a pointer to stack, which will be used inside
105 * the helper function as a pointer to map element key.
107 * On the kernel side the helper function looks like:
108 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
110 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
111 * void *key = (void *) (unsigned long) r2;
114 * here kernel can access 'key' and 'map' pointers safely, knowing that
115 * [key, key + map->key_size) bytes are valid and were initialized on
116 * the stack of eBPF program.
119 * Corresponding eBPF program may look like:
120 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
121 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
122 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
123 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
124 * here verifier looks at prototype of map_lookup_elem() and sees:
125 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
126 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
128 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
129 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
130 * and were initialized prior to this call.
131 * If it's ok, then verifier allows this BPF_CALL insn and looks at
132 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
133 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
134 * returns ether pointer to map value or NULL.
136 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
137 * insn, the register holding that pointer in the true branch changes state to
138 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
139 * branch. See check_cond_jmp_op().
141 * After the call R0 is set to return type of the function and registers R1-R5
142 * are set to NOT_INIT to indicate that they are no longer readable.
145 /* verifier_state + insn_idx are pushed to stack when branch is encountered */
146 struct bpf_verifier_stack_elem
{
147 /* verifer state is 'st'
148 * before processing instruction 'insn_idx'
149 * and after processing instruction 'prev_insn_idx'
151 struct bpf_verifier_state st
;
154 struct bpf_verifier_stack_elem
*next
;
157 #define BPF_COMPLEXITY_LIMIT_INSNS 131072
158 #define BPF_COMPLEXITY_LIMIT_STACK 1024
160 #define BPF_MAP_PTR_UNPRIV 1UL
161 #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \
162 POISON_POINTER_DELTA))
163 #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV))
165 static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data
*aux
)
167 return BPF_MAP_PTR(aux
->map_state
) == BPF_MAP_PTR_POISON
;
170 static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data
*aux
)
172 return aux
->map_state
& BPF_MAP_PTR_UNPRIV
;
175 static void bpf_map_ptr_store(struct bpf_insn_aux_data
*aux
,
176 const struct bpf_map
*map
, bool unpriv
)
178 BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON
& BPF_MAP_PTR_UNPRIV
);
179 unpriv
|= bpf_map_ptr_unpriv(aux
);
180 aux
->map_state
= (unsigned long)map
|
181 (unpriv
? BPF_MAP_PTR_UNPRIV
: 0UL);
184 struct bpf_call_arg_meta
{
185 struct bpf_map
*map_ptr
;
190 s64 msize_smax_value
;
191 u64 msize_umax_value
;
194 static DEFINE_MUTEX(bpf_verifier_lock
);
196 void bpf_verifier_vlog(struct bpf_verifier_log
*log
, const char *fmt
,
201 n
= vscnprintf(log
->kbuf
, BPF_VERIFIER_TMP_LOG_SIZE
, fmt
, args
);
203 WARN_ONCE(n
>= BPF_VERIFIER_TMP_LOG_SIZE
- 1,
204 "verifier log line truncated - local buffer too short\n");
206 n
= min(log
->len_total
- log
->len_used
- 1, n
);
209 if (!copy_to_user(log
->ubuf
+ log
->len_used
, log
->kbuf
, n
+ 1))
215 /* log_level controls verbosity level of eBPF verifier.
216 * bpf_verifier_log_write() is used to dump the verification trace to the log,
217 * so the user can figure out what's wrong with the program
219 __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env
*env
,
220 const char *fmt
, ...)
224 if (!bpf_verifier_log_needed(&env
->log
))
228 bpf_verifier_vlog(&env
->log
, fmt
, args
);
231 EXPORT_SYMBOL_GPL(bpf_verifier_log_write
);
233 __printf(2, 3) static void verbose(void *private_data
, const char *fmt
, ...)
235 struct bpf_verifier_env
*env
= private_data
;
238 if (!bpf_verifier_log_needed(&env
->log
))
242 bpf_verifier_vlog(&env
->log
, fmt
, args
);
246 static bool type_is_pkt_pointer(enum bpf_reg_type type
)
248 return type
== PTR_TO_PACKET
||
249 type
== PTR_TO_PACKET_META
;
252 /* string representation of 'enum bpf_reg_type' */
253 static const char * const reg_type_str
[] = {
255 [SCALAR_VALUE
] = "inv",
256 [PTR_TO_CTX
] = "ctx",
257 [CONST_PTR_TO_MAP
] = "map_ptr",
258 [PTR_TO_MAP_VALUE
] = "map_value",
259 [PTR_TO_MAP_VALUE_OR_NULL
] = "map_value_or_null",
260 [PTR_TO_STACK
] = "fp",
261 [PTR_TO_PACKET
] = "pkt",
262 [PTR_TO_PACKET_META
] = "pkt_meta",
263 [PTR_TO_PACKET_END
] = "pkt_end",
266 static void print_liveness(struct bpf_verifier_env
*env
,
267 enum bpf_reg_liveness live
)
269 if (live
& (REG_LIVE_READ
| REG_LIVE_WRITTEN
))
271 if (live
& REG_LIVE_READ
)
273 if (live
& REG_LIVE_WRITTEN
)
277 static struct bpf_func_state
*func(struct bpf_verifier_env
*env
,
278 const struct bpf_reg_state
*reg
)
280 struct bpf_verifier_state
*cur
= env
->cur_state
;
282 return cur
->frame
[reg
->frameno
];
285 static void print_verifier_state(struct bpf_verifier_env
*env
,
286 const struct bpf_func_state
*state
)
288 const struct bpf_reg_state
*reg
;
293 verbose(env
, " frame%d:", state
->frameno
);
294 for (i
= 0; i
< MAX_BPF_REG
; i
++) {
295 reg
= &state
->regs
[i
];
299 verbose(env
, " R%d", i
);
300 print_liveness(env
, reg
->live
);
301 verbose(env
, "=%s", reg_type_str
[t
]);
302 if ((t
== SCALAR_VALUE
|| t
== PTR_TO_STACK
) &&
303 tnum_is_const(reg
->var_off
)) {
304 /* reg->off should be 0 for SCALAR_VALUE */
305 verbose(env
, "%lld", reg
->var_off
.value
+ reg
->off
);
306 if (t
== PTR_TO_STACK
)
307 verbose(env
, ",call_%d", func(env
, reg
)->callsite
);
309 verbose(env
, "(id=%d", reg
->id
);
310 if (t
!= SCALAR_VALUE
)
311 verbose(env
, ",off=%d", reg
->off
);
312 if (type_is_pkt_pointer(t
))
313 verbose(env
, ",r=%d", reg
->range
);
314 else if (t
== CONST_PTR_TO_MAP
||
315 t
== PTR_TO_MAP_VALUE
||
316 t
== PTR_TO_MAP_VALUE_OR_NULL
)
317 verbose(env
, ",ks=%d,vs=%d",
318 reg
->map_ptr
->key_size
,
319 reg
->map_ptr
->value_size
);
320 if (tnum_is_const(reg
->var_off
)) {
321 /* Typically an immediate SCALAR_VALUE, but
322 * could be a pointer whose offset is too big
325 verbose(env
, ",imm=%llx", reg
->var_off
.value
);
327 if (reg
->smin_value
!= reg
->umin_value
&&
328 reg
->smin_value
!= S64_MIN
)
329 verbose(env
, ",smin_value=%lld",
330 (long long)reg
->smin_value
);
331 if (reg
->smax_value
!= reg
->umax_value
&&
332 reg
->smax_value
!= S64_MAX
)
333 verbose(env
, ",smax_value=%lld",
334 (long long)reg
->smax_value
);
335 if (reg
->umin_value
!= 0)
336 verbose(env
, ",umin_value=%llu",
337 (unsigned long long)reg
->umin_value
);
338 if (reg
->umax_value
!= U64_MAX
)
339 verbose(env
, ",umax_value=%llu",
340 (unsigned long long)reg
->umax_value
);
341 if (!tnum_is_unknown(reg
->var_off
)) {
344 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
345 verbose(env
, ",var_off=%s", tn_buf
);
351 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
352 if (state
->stack
[i
].slot_type
[0] == STACK_SPILL
) {
353 verbose(env
, " fp%d",
354 (-i
- 1) * BPF_REG_SIZE
);
355 print_liveness(env
, state
->stack
[i
].spilled_ptr
.live
);
357 reg_type_str
[state
->stack
[i
].spilled_ptr
.type
]);
359 if (state
->stack
[i
].slot_type
[0] == STACK_ZERO
)
360 verbose(env
, " fp%d=0", (-i
- 1) * BPF_REG_SIZE
);
365 static int copy_stack_state(struct bpf_func_state
*dst
,
366 const struct bpf_func_state
*src
)
370 if (WARN_ON_ONCE(dst
->allocated_stack
< src
->allocated_stack
)) {
371 /* internal bug, make state invalid to reject the program */
372 memset(dst
, 0, sizeof(*dst
));
375 memcpy(dst
->stack
, src
->stack
,
376 sizeof(*src
->stack
) * (src
->allocated_stack
/ BPF_REG_SIZE
));
380 /* do_check() starts with zero-sized stack in struct bpf_verifier_state to
381 * make it consume minimal amount of memory. check_stack_write() access from
382 * the program calls into realloc_func_state() to grow the stack size.
383 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
384 * which this function copies over. It points to previous bpf_verifier_state
385 * which is never reallocated
387 static int realloc_func_state(struct bpf_func_state
*state
, int size
,
390 u32 old_size
= state
->allocated_stack
;
391 struct bpf_stack_state
*new_stack
;
392 int slot
= size
/ BPF_REG_SIZE
;
394 if (size
<= old_size
|| !size
) {
397 state
->allocated_stack
= slot
* BPF_REG_SIZE
;
398 if (!size
&& old_size
) {
404 new_stack
= kmalloc_array(slot
, sizeof(struct bpf_stack_state
),
410 memcpy(new_stack
, state
->stack
,
411 sizeof(*new_stack
) * (old_size
/ BPF_REG_SIZE
));
412 memset(new_stack
+ old_size
/ BPF_REG_SIZE
, 0,
413 sizeof(*new_stack
) * (size
- old_size
) / BPF_REG_SIZE
);
415 state
->allocated_stack
= slot
* BPF_REG_SIZE
;
417 state
->stack
= new_stack
;
421 static void free_func_state(struct bpf_func_state
*state
)
429 static void free_verifier_state(struct bpf_verifier_state
*state
,
434 for (i
= 0; i
<= state
->curframe
; i
++) {
435 free_func_state(state
->frame
[i
]);
436 state
->frame
[i
] = NULL
;
442 /* copy verifier state from src to dst growing dst stack space
443 * when necessary to accommodate larger src stack
445 static int copy_func_state(struct bpf_func_state
*dst
,
446 const struct bpf_func_state
*src
)
450 err
= realloc_func_state(dst
, src
->allocated_stack
, false);
453 memcpy(dst
, src
, offsetof(struct bpf_func_state
, allocated_stack
));
454 return copy_stack_state(dst
, src
);
457 static int copy_verifier_state(struct bpf_verifier_state
*dst_state
,
458 const struct bpf_verifier_state
*src
)
460 struct bpf_func_state
*dst
;
463 /* if dst has more stack frames then src frame, free them */
464 for (i
= src
->curframe
+ 1; i
<= dst_state
->curframe
; i
++) {
465 free_func_state(dst_state
->frame
[i
]);
466 dst_state
->frame
[i
] = NULL
;
468 dst_state
->curframe
= src
->curframe
;
469 dst_state
->parent
= src
->parent
;
470 for (i
= 0; i
<= src
->curframe
; i
++) {
471 dst
= dst_state
->frame
[i
];
473 dst
= kzalloc(sizeof(*dst
), GFP_KERNEL
);
476 dst_state
->frame
[i
] = dst
;
478 err
= copy_func_state(dst
, src
->frame
[i
]);
485 static int pop_stack(struct bpf_verifier_env
*env
, int *prev_insn_idx
,
488 struct bpf_verifier_state
*cur
= env
->cur_state
;
489 struct bpf_verifier_stack_elem
*elem
, *head
= env
->head
;
492 if (env
->head
== NULL
)
496 err
= copy_verifier_state(cur
, &head
->st
);
501 *insn_idx
= head
->insn_idx
;
503 *prev_insn_idx
= head
->prev_insn_idx
;
505 free_verifier_state(&head
->st
, false);
512 static struct bpf_verifier_state
*push_stack(struct bpf_verifier_env
*env
,
513 int insn_idx
, int prev_insn_idx
)
515 struct bpf_verifier_state
*cur
= env
->cur_state
;
516 struct bpf_verifier_stack_elem
*elem
;
519 elem
= kzalloc(sizeof(struct bpf_verifier_stack_elem
), GFP_KERNEL
);
523 elem
->insn_idx
= insn_idx
;
524 elem
->prev_insn_idx
= prev_insn_idx
;
525 elem
->next
= env
->head
;
528 err
= copy_verifier_state(&elem
->st
, cur
);
531 if (env
->stack_size
> BPF_COMPLEXITY_LIMIT_STACK
) {
532 verbose(env
, "BPF program is too complex\n");
537 free_verifier_state(env
->cur_state
, true);
538 env
->cur_state
= NULL
;
539 /* pop all elements and return */
540 while (!pop_stack(env
, NULL
, NULL
));
544 #define CALLER_SAVED_REGS 6
545 static const int caller_saved
[CALLER_SAVED_REGS
] = {
546 BPF_REG_0
, BPF_REG_1
, BPF_REG_2
, BPF_REG_3
, BPF_REG_4
, BPF_REG_5
549 static void __mark_reg_not_init(struct bpf_reg_state
*reg
);
551 /* Mark the unknown part of a register (variable offset or scalar value) as
552 * known to have the value @imm.
554 static void __mark_reg_known(struct bpf_reg_state
*reg
, u64 imm
)
557 reg
->var_off
= tnum_const(imm
);
558 reg
->smin_value
= (s64
)imm
;
559 reg
->smax_value
= (s64
)imm
;
560 reg
->umin_value
= imm
;
561 reg
->umax_value
= imm
;
564 /* Mark the 'variable offset' part of a register as zero. This should be
565 * used only on registers holding a pointer type.
567 static void __mark_reg_known_zero(struct bpf_reg_state
*reg
)
569 __mark_reg_known(reg
, 0);
572 static void __mark_reg_const_zero(struct bpf_reg_state
*reg
)
574 __mark_reg_known(reg
, 0);
576 reg
->type
= SCALAR_VALUE
;
579 static void mark_reg_known_zero(struct bpf_verifier_env
*env
,
580 struct bpf_reg_state
*regs
, u32 regno
)
582 if (WARN_ON(regno
>= MAX_BPF_REG
)) {
583 verbose(env
, "mark_reg_known_zero(regs, %u)\n", regno
);
584 /* Something bad happened, let's kill all regs */
585 for (regno
= 0; regno
< MAX_BPF_REG
; regno
++)
586 __mark_reg_not_init(regs
+ regno
);
589 __mark_reg_known_zero(regs
+ regno
);
592 static bool reg_is_pkt_pointer(const struct bpf_reg_state
*reg
)
594 return type_is_pkt_pointer(reg
->type
);
597 static bool reg_is_pkt_pointer_any(const struct bpf_reg_state
*reg
)
599 return reg_is_pkt_pointer(reg
) ||
600 reg
->type
== PTR_TO_PACKET_END
;
603 /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
604 static bool reg_is_init_pkt_pointer(const struct bpf_reg_state
*reg
,
605 enum bpf_reg_type which
)
607 /* The register can already have a range from prior markings.
608 * This is fine as long as it hasn't been advanced from its
611 return reg
->type
== which
&&
614 tnum_equals_const(reg
->var_off
, 0);
617 /* Attempts to improve min/max values based on var_off information */
618 static void __update_reg_bounds(struct bpf_reg_state
*reg
)
620 /* min signed is max(sign bit) | min(other bits) */
621 reg
->smin_value
= max_t(s64
, reg
->smin_value
,
622 reg
->var_off
.value
| (reg
->var_off
.mask
& S64_MIN
));
623 /* max signed is min(sign bit) | max(other bits) */
624 reg
->smax_value
= min_t(s64
, reg
->smax_value
,
625 reg
->var_off
.value
| (reg
->var_off
.mask
& S64_MAX
));
626 reg
->umin_value
= max(reg
->umin_value
, reg
->var_off
.value
);
627 reg
->umax_value
= min(reg
->umax_value
,
628 reg
->var_off
.value
| reg
->var_off
.mask
);
631 /* Uses signed min/max values to inform unsigned, and vice-versa */
632 static void __reg_deduce_bounds(struct bpf_reg_state
*reg
)
634 /* Learn sign from signed bounds.
635 * If we cannot cross the sign boundary, then signed and unsigned bounds
636 * are the same, so combine. This works even in the negative case, e.g.
637 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
639 if (reg
->smin_value
>= 0 || reg
->smax_value
< 0) {
640 reg
->smin_value
= reg
->umin_value
= max_t(u64
, reg
->smin_value
,
642 reg
->smax_value
= reg
->umax_value
= min_t(u64
, reg
->smax_value
,
646 /* Learn sign from unsigned bounds. Signed bounds cross the sign
647 * boundary, so we must be careful.
649 if ((s64
)reg
->umax_value
>= 0) {
650 /* Positive. We can't learn anything from the smin, but smax
651 * is positive, hence safe.
653 reg
->smin_value
= reg
->umin_value
;
654 reg
->smax_value
= reg
->umax_value
= min_t(u64
, reg
->smax_value
,
656 } else if ((s64
)reg
->umin_value
< 0) {
657 /* Negative. We can't learn anything from the smax, but smin
658 * is negative, hence safe.
660 reg
->smin_value
= reg
->umin_value
= max_t(u64
, reg
->smin_value
,
662 reg
->smax_value
= reg
->umax_value
;
666 /* Attempts to improve var_off based on unsigned min/max information */
667 static void __reg_bound_offset(struct bpf_reg_state
*reg
)
669 reg
->var_off
= tnum_intersect(reg
->var_off
,
670 tnum_range(reg
->umin_value
,
674 /* Reset the min/max bounds of a register */
675 static void __mark_reg_unbounded(struct bpf_reg_state
*reg
)
677 reg
->smin_value
= S64_MIN
;
678 reg
->smax_value
= S64_MAX
;
680 reg
->umax_value
= U64_MAX
;
683 /* Mark a register as having a completely unknown (scalar) value. */
684 static void __mark_reg_unknown(struct bpf_reg_state
*reg
)
686 reg
->type
= SCALAR_VALUE
;
689 reg
->var_off
= tnum_unknown
;
691 __mark_reg_unbounded(reg
);
694 static void mark_reg_unknown(struct bpf_verifier_env
*env
,
695 struct bpf_reg_state
*regs
, u32 regno
)
697 if (WARN_ON(regno
>= MAX_BPF_REG
)) {
698 verbose(env
, "mark_reg_unknown(regs, %u)\n", regno
);
699 /* Something bad happened, let's kill all regs except FP */
700 for (regno
= 0; regno
< BPF_REG_FP
; regno
++)
701 __mark_reg_not_init(regs
+ regno
);
704 __mark_reg_unknown(regs
+ regno
);
707 static void __mark_reg_not_init(struct bpf_reg_state
*reg
)
709 __mark_reg_unknown(reg
);
710 reg
->type
= NOT_INIT
;
713 static void mark_reg_not_init(struct bpf_verifier_env
*env
,
714 struct bpf_reg_state
*regs
, u32 regno
)
716 if (WARN_ON(regno
>= MAX_BPF_REG
)) {
717 verbose(env
, "mark_reg_not_init(regs, %u)\n", regno
);
718 /* Something bad happened, let's kill all regs except FP */
719 for (regno
= 0; regno
< BPF_REG_FP
; regno
++)
720 __mark_reg_not_init(regs
+ regno
);
723 __mark_reg_not_init(regs
+ regno
);
726 static void init_reg_state(struct bpf_verifier_env
*env
,
727 struct bpf_func_state
*state
)
729 struct bpf_reg_state
*regs
= state
->regs
;
732 for (i
= 0; i
< MAX_BPF_REG
; i
++) {
733 mark_reg_not_init(env
, regs
, i
);
734 regs
[i
].live
= REG_LIVE_NONE
;
738 regs
[BPF_REG_FP
].type
= PTR_TO_STACK
;
739 mark_reg_known_zero(env
, regs
, BPF_REG_FP
);
740 regs
[BPF_REG_FP
].frameno
= state
->frameno
;
742 /* 1st arg to a function */
743 regs
[BPF_REG_1
].type
= PTR_TO_CTX
;
744 mark_reg_known_zero(env
, regs
, BPF_REG_1
);
747 #define BPF_MAIN_FUNC (-1)
748 static void init_func_state(struct bpf_verifier_env
*env
,
749 struct bpf_func_state
*state
,
750 int callsite
, int frameno
, int subprogno
)
752 state
->callsite
= callsite
;
753 state
->frameno
= frameno
;
754 state
->subprogno
= subprogno
;
755 init_reg_state(env
, state
);
759 SRC_OP
, /* register is used as source operand */
760 DST_OP
, /* register is used as destination operand */
761 DST_OP_NO_MARK
/* same as above, check only, don't mark */
764 static int cmp_subprogs(const void *a
, const void *b
)
766 return ((struct bpf_subprog_info
*)a
)->start
-
767 ((struct bpf_subprog_info
*)b
)->start
;
770 static int find_subprog(struct bpf_verifier_env
*env
, int off
)
772 struct bpf_subprog_info
*p
;
774 p
= bsearch(&off
, env
->subprog_info
, env
->subprog_cnt
,
775 sizeof(env
->subprog_info
[0]), cmp_subprogs
);
778 return p
- env
->subprog_info
;
782 static int add_subprog(struct bpf_verifier_env
*env
, int off
)
784 int insn_cnt
= env
->prog
->len
;
787 if (off
>= insn_cnt
|| off
< 0) {
788 verbose(env
, "call to invalid destination\n");
791 ret
= find_subprog(env
, off
);
794 if (env
->subprog_cnt
>= BPF_MAX_SUBPROGS
) {
795 verbose(env
, "too many subprograms\n");
798 env
->subprog_info
[env
->subprog_cnt
++].start
= off
;
799 sort(env
->subprog_info
, env
->subprog_cnt
,
800 sizeof(env
->subprog_info
[0]), cmp_subprogs
, NULL
);
804 static int check_subprogs(struct bpf_verifier_env
*env
)
806 int i
, ret
, subprog_start
, subprog_end
, off
, cur_subprog
= 0;
807 struct bpf_subprog_info
*subprog
= env
->subprog_info
;
808 struct bpf_insn
*insn
= env
->prog
->insnsi
;
809 int insn_cnt
= env
->prog
->len
;
811 /* Add entry function. */
812 ret
= add_subprog(env
, 0);
816 /* determine subprog starts. The end is one before the next starts */
817 for (i
= 0; i
< insn_cnt
; i
++) {
818 if (insn
[i
].code
!= (BPF_JMP
| BPF_CALL
))
820 if (insn
[i
].src_reg
!= BPF_PSEUDO_CALL
)
822 if (!env
->allow_ptr_leaks
) {
823 verbose(env
, "function calls to other bpf functions are allowed for root only\n");
826 if (bpf_prog_is_dev_bound(env
->prog
->aux
)) {
827 verbose(env
, "function calls in offloaded programs are not supported yet\n");
830 ret
= add_subprog(env
, i
+ insn
[i
].imm
+ 1);
835 /* Add a fake 'exit' subprog which could simplify subprog iteration
836 * logic. 'subprog_cnt' should not be increased.
838 subprog
[env
->subprog_cnt
].start
= insn_cnt
;
840 if (env
->log
.level
> 1)
841 for (i
= 0; i
< env
->subprog_cnt
; i
++)
842 verbose(env
, "func#%d @%d\n", i
, subprog
[i
].start
);
844 /* now check that all jumps are within the same subprog */
845 subprog_start
= subprog
[cur_subprog
].start
;
846 subprog_end
= subprog
[cur_subprog
+ 1].start
;
847 for (i
= 0; i
< insn_cnt
; i
++) {
848 u8 code
= insn
[i
].code
;
850 if (BPF_CLASS(code
) != BPF_JMP
)
852 if (BPF_OP(code
) == BPF_EXIT
|| BPF_OP(code
) == BPF_CALL
)
854 off
= i
+ insn
[i
].off
+ 1;
855 if (off
< subprog_start
|| off
>= subprog_end
) {
856 verbose(env
, "jump out of range from insn %d to %d\n", i
, off
);
860 if (i
== subprog_end
- 1) {
861 /* to avoid fall-through from one subprog into another
862 * the last insn of the subprog should be either exit
863 * or unconditional jump back
865 if (code
!= (BPF_JMP
| BPF_EXIT
) &&
866 code
!= (BPF_JMP
| BPF_JA
)) {
867 verbose(env
, "last insn is not an exit or jmp\n");
870 subprog_start
= subprog_end
;
872 if (cur_subprog
< env
->subprog_cnt
)
873 subprog_end
= subprog
[cur_subprog
+ 1].start
;
880 struct bpf_verifier_state
*skip_callee(struct bpf_verifier_env
*env
,
881 const struct bpf_verifier_state
*state
,
882 struct bpf_verifier_state
*parent
,
885 struct bpf_verifier_state
*tmp
= NULL
;
887 /* 'parent' could be a state of caller and
888 * 'state' could be a state of callee. In such case
889 * parent->curframe < state->curframe
890 * and it's ok for r1 - r5 registers
892 * 'parent' could be a callee's state after it bpf_exit-ed.
893 * In such case parent->curframe > state->curframe
894 * and it's ok for r0 only
896 if (parent
->curframe
== state
->curframe
||
897 (parent
->curframe
< state
->curframe
&&
898 regno
>= BPF_REG_1
&& regno
<= BPF_REG_5
) ||
899 (parent
->curframe
> state
->curframe
&&
903 if (parent
->curframe
> state
->curframe
&&
904 regno
>= BPF_REG_6
) {
905 /* for callee saved regs we have to skip the whole chain
906 * of states that belong to callee and mark as LIVE_READ
907 * the registers before the call
910 while (tmp
&& tmp
->curframe
!= state
->curframe
) {
921 verbose(env
, "verifier bug regno %d tmp %p\n", regno
, tmp
);
922 verbose(env
, "regno %d parent frame %d current frame %d\n",
923 regno
, parent
->curframe
, state
->curframe
);
927 static int mark_reg_read(struct bpf_verifier_env
*env
,
928 const struct bpf_verifier_state
*state
,
929 struct bpf_verifier_state
*parent
,
932 bool writes
= parent
== state
->parent
; /* Observe write marks */
934 if (regno
== BPF_REG_FP
)
935 /* We don't need to worry about FP liveness because it's read-only */
939 /* if read wasn't screened by an earlier write ... */
940 if (writes
&& state
->frame
[state
->curframe
]->regs
[regno
].live
& REG_LIVE_WRITTEN
)
942 parent
= skip_callee(env
, state
, parent
, regno
);
945 /* ... then we depend on parent's value */
946 parent
->frame
[parent
->curframe
]->regs
[regno
].live
|= REG_LIVE_READ
;
948 parent
= state
->parent
;
954 static int check_reg_arg(struct bpf_verifier_env
*env
, u32 regno
,
957 struct bpf_verifier_state
*vstate
= env
->cur_state
;
958 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
959 struct bpf_reg_state
*regs
= state
->regs
;
961 if (regno
>= MAX_BPF_REG
) {
962 verbose(env
, "R%d is invalid\n", regno
);
967 /* check whether register used as source operand can be read */
968 if (regs
[regno
].type
== NOT_INIT
) {
969 verbose(env
, "R%d !read_ok\n", regno
);
972 return mark_reg_read(env
, vstate
, vstate
->parent
, regno
);
974 /* check whether register used as dest operand can be written to */
975 if (regno
== BPF_REG_FP
) {
976 verbose(env
, "frame pointer is read only\n");
979 regs
[regno
].live
|= REG_LIVE_WRITTEN
;
981 mark_reg_unknown(env
, regs
, regno
);
986 static bool is_spillable_regtype(enum bpf_reg_type type
)
989 case PTR_TO_MAP_VALUE
:
990 case PTR_TO_MAP_VALUE_OR_NULL
:
994 case PTR_TO_PACKET_META
:
995 case PTR_TO_PACKET_END
:
996 case CONST_PTR_TO_MAP
:
1003 /* Does this register contain a constant zero? */
1004 static bool register_is_null(struct bpf_reg_state
*reg
)
1006 return reg
->type
== SCALAR_VALUE
&& tnum_equals_const(reg
->var_off
, 0);
1009 /* check_stack_read/write functions track spill/fill of registers,
1010 * stack boundary and alignment are checked in check_mem_access()
1012 static int check_stack_write(struct bpf_verifier_env
*env
,
1013 struct bpf_func_state
*state
, /* func where register points to */
1014 int off
, int size
, int value_regno
, int insn_idx
)
1016 struct bpf_func_state
*cur
; /* state of the current function */
1017 int i
, slot
= -off
- 1, spi
= slot
/ BPF_REG_SIZE
, err
;
1018 enum bpf_reg_type type
;
1020 err
= realloc_func_state(state
, round_up(slot
+ 1, BPF_REG_SIZE
),
1024 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
1025 * so it's aligned access and [off, off + size) are within stack limits
1027 if (!env
->allow_ptr_leaks
&&
1028 state
->stack
[spi
].slot_type
[0] == STACK_SPILL
&&
1029 size
!= BPF_REG_SIZE
) {
1030 verbose(env
, "attempt to corrupt spilled pointer on stack\n");
1034 cur
= env
->cur_state
->frame
[env
->cur_state
->curframe
];
1035 if (value_regno
>= 0 &&
1036 is_spillable_regtype((type
= cur
->regs
[value_regno
].type
))) {
1038 /* register containing pointer is being spilled into stack */
1039 if (size
!= BPF_REG_SIZE
) {
1040 verbose(env
, "invalid size of register spill\n");
1044 if (state
!= cur
&& type
== PTR_TO_STACK
) {
1045 verbose(env
, "cannot spill pointers to stack into stack frame of the caller\n");
1049 /* save register state */
1050 state
->stack
[spi
].spilled_ptr
= cur
->regs
[value_regno
];
1051 state
->stack
[spi
].spilled_ptr
.live
|= REG_LIVE_WRITTEN
;
1053 for (i
= 0; i
< BPF_REG_SIZE
; i
++) {
1054 if (state
->stack
[spi
].slot_type
[i
] == STACK_MISC
&&
1055 !env
->allow_ptr_leaks
) {
1056 int *poff
= &env
->insn_aux_data
[insn_idx
].sanitize_stack_off
;
1057 int soff
= (-spi
- 1) * BPF_REG_SIZE
;
1059 /* detected reuse of integer stack slot with a pointer
1060 * which means either llvm is reusing stack slot or
1061 * an attacker is trying to exploit CVE-2018-3639
1062 * (speculative store bypass)
1063 * Have to sanitize that slot with preemptive
1066 if (*poff
&& *poff
!= soff
) {
1067 /* disallow programs where single insn stores
1068 * into two different stack slots, since verifier
1069 * cannot sanitize them
1072 "insn %d cannot access two stack slots fp%d and fp%d",
1073 insn_idx
, *poff
, soff
);
1078 state
->stack
[spi
].slot_type
[i
] = STACK_SPILL
;
1081 u8 type
= STACK_MISC
;
1083 /* regular write of data into stack */
1084 state
->stack
[spi
].spilled_ptr
= (struct bpf_reg_state
) {};
1086 /* only mark the slot as written if all 8 bytes were written
1087 * otherwise read propagation may incorrectly stop too soon
1088 * when stack slots are partially written.
1089 * This heuristic means that read propagation will be
1090 * conservative, since it will add reg_live_read marks
1091 * to stack slots all the way to first state when programs
1092 * writes+reads less than 8 bytes
1094 if (size
== BPF_REG_SIZE
)
1095 state
->stack
[spi
].spilled_ptr
.live
|= REG_LIVE_WRITTEN
;
1097 /* when we zero initialize stack slots mark them as such */
1098 if (value_regno
>= 0 &&
1099 register_is_null(&cur
->regs
[value_regno
]))
1102 for (i
= 0; i
< size
; i
++)
1103 state
->stack
[spi
].slot_type
[(slot
- i
) % BPF_REG_SIZE
] =
1109 /* registers of every function are unique and mark_reg_read() propagates
1110 * the liveness in the following cases:
1111 * - from callee into caller for R1 - R5 that were used as arguments
1112 * - from caller into callee for R0 that used as result of the call
1113 * - from caller to the same caller skipping states of the callee for R6 - R9,
1114 * since R6 - R9 are callee saved by implicit function prologue and
1115 * caller's R6 != callee's R6, so when we propagate liveness up to
1116 * parent states we need to skip callee states for R6 - R9.
1118 * stack slot marking is different, since stacks of caller and callee are
1119 * accessible in both (since caller can pass a pointer to caller's stack to
1120 * callee which can pass it to another function), hence mark_stack_slot_read()
1121 * has to propagate the stack liveness to all parent states at given frame number.
1131 * First *ptr is reading from f1's stack and mark_stack_slot_read() has
1132 * to mark liveness at the f1's frame and not f2's frame.
1133 * Second *ptr is also reading from f1's stack and mark_stack_slot_read() has
1134 * to propagate liveness to f2 states at f1's frame level and further into
1135 * f1 states at f1's frame level until write into that stack slot
1137 static void mark_stack_slot_read(struct bpf_verifier_env
*env
,
1138 const struct bpf_verifier_state
*state
,
1139 struct bpf_verifier_state
*parent
,
1140 int slot
, int frameno
)
1142 bool writes
= parent
== state
->parent
; /* Observe write marks */
1145 if (parent
->frame
[frameno
]->allocated_stack
<= slot
* BPF_REG_SIZE
)
1146 /* since LIVE_WRITTEN mark is only done for full 8-byte
1147 * write the read marks are conservative and parent
1148 * state may not even have the stack allocated. In such case
1149 * end the propagation, since the loop reached beginning
1153 /* if read wasn't screened by an earlier write ... */
1154 if (writes
&& state
->frame
[frameno
]->stack
[slot
].spilled_ptr
.live
& REG_LIVE_WRITTEN
)
1156 /* ... then we depend on parent's value */
1157 parent
->frame
[frameno
]->stack
[slot
].spilled_ptr
.live
|= REG_LIVE_READ
;
1159 parent
= state
->parent
;
1164 static int check_stack_read(struct bpf_verifier_env
*env
,
1165 struct bpf_func_state
*reg_state
/* func where register points to */,
1166 int off
, int size
, int value_regno
)
1168 struct bpf_verifier_state
*vstate
= env
->cur_state
;
1169 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
1170 int i
, slot
= -off
- 1, spi
= slot
/ BPF_REG_SIZE
;
1173 if (reg_state
->allocated_stack
<= slot
) {
1174 verbose(env
, "invalid read from stack off %d+0 size %d\n",
1178 stype
= reg_state
->stack
[spi
].slot_type
;
1180 if (stype
[0] == STACK_SPILL
) {
1181 if (size
!= BPF_REG_SIZE
) {
1182 verbose(env
, "invalid size of register spill\n");
1185 for (i
= 1; i
< BPF_REG_SIZE
; i
++) {
1186 if (stype
[(slot
- i
) % BPF_REG_SIZE
] != STACK_SPILL
) {
1187 verbose(env
, "corrupted spill memory\n");
1192 if (value_regno
>= 0) {
1193 /* restore register state from stack */
1194 state
->regs
[value_regno
] = reg_state
->stack
[spi
].spilled_ptr
;
1195 /* mark reg as written since spilled pointer state likely
1196 * has its liveness marks cleared by is_state_visited()
1197 * which resets stack/reg liveness for state transitions
1199 state
->regs
[value_regno
].live
|= REG_LIVE_WRITTEN
;
1201 mark_stack_slot_read(env
, vstate
, vstate
->parent
, spi
,
1202 reg_state
->frameno
);
1207 for (i
= 0; i
< size
; i
++) {
1208 if (stype
[(slot
- i
) % BPF_REG_SIZE
] == STACK_MISC
)
1210 if (stype
[(slot
- i
) % BPF_REG_SIZE
] == STACK_ZERO
) {
1214 verbose(env
, "invalid read from stack off %d+%d size %d\n",
1218 mark_stack_slot_read(env
, vstate
, vstate
->parent
, spi
,
1219 reg_state
->frameno
);
1220 if (value_regno
>= 0) {
1221 if (zeros
== size
) {
1222 /* any size read into register is zero extended,
1223 * so the whole register == const_zero
1225 __mark_reg_const_zero(&state
->regs
[value_regno
]);
1227 /* have read misc data from the stack */
1228 mark_reg_unknown(env
, state
->regs
, value_regno
);
1230 state
->regs
[value_regno
].live
|= REG_LIVE_WRITTEN
;
1236 /* check read/write into map element returned by bpf_map_lookup_elem() */
1237 static int __check_map_access(struct bpf_verifier_env
*env
, u32 regno
, int off
,
1238 int size
, bool zero_size_allowed
)
1240 struct bpf_reg_state
*regs
= cur_regs(env
);
1241 struct bpf_map
*map
= regs
[regno
].map_ptr
;
1243 if (off
< 0 || size
< 0 || (size
== 0 && !zero_size_allowed
) ||
1244 off
+ size
> map
->value_size
) {
1245 verbose(env
, "invalid access to map value, value_size=%d off=%d size=%d\n",
1246 map
->value_size
, off
, size
);
1252 /* check read/write into a map element with possible variable offset */
1253 static int check_map_access(struct bpf_verifier_env
*env
, u32 regno
,
1254 int off
, int size
, bool zero_size_allowed
)
1256 struct bpf_verifier_state
*vstate
= env
->cur_state
;
1257 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
1258 struct bpf_reg_state
*reg
= &state
->regs
[regno
];
1261 /* We may have adjusted the register to this map value, so we
1262 * need to try adding each of min_value and max_value to off
1263 * to make sure our theoretical access will be safe.
1266 print_verifier_state(env
, state
);
1267 /* The minimum value is only important with signed
1268 * comparisons where we can't assume the floor of a
1269 * value is 0. If we are using signed variables for our
1270 * index'es we need to make sure that whatever we use
1271 * will have a set floor within our range.
1273 if (reg
->smin_value
< 0) {
1274 verbose(env
, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1278 err
= __check_map_access(env
, regno
, reg
->smin_value
+ off
, size
,
1281 verbose(env
, "R%d min value is outside of the array range\n",
1286 /* If we haven't set a max value then we need to bail since we can't be
1287 * sure we won't do bad things.
1288 * If reg->umax_value + off could overflow, treat that as unbounded too.
1290 if (reg
->umax_value
>= BPF_MAX_VAR_OFF
) {
1291 verbose(env
, "R%d unbounded memory access, make sure to bounds check any array access into a map\n",
1295 err
= __check_map_access(env
, regno
, reg
->umax_value
+ off
, size
,
1298 verbose(env
, "R%d max value is outside of the array range\n",
1303 #define MAX_PACKET_OFF 0xffff
1305 static bool may_access_direct_pkt_data(struct bpf_verifier_env
*env
,
1306 const struct bpf_call_arg_meta
*meta
,
1307 enum bpf_access_type t
)
1309 switch (env
->prog
->type
) {
1310 case BPF_PROG_TYPE_LWT_IN
:
1311 case BPF_PROG_TYPE_LWT_OUT
:
1312 case BPF_PROG_TYPE_LWT_SEG6LOCAL
:
1313 /* dst_input() and dst_output() can't write for now */
1317 case BPF_PROG_TYPE_SCHED_CLS
:
1318 case BPF_PROG_TYPE_SCHED_ACT
:
1319 case BPF_PROG_TYPE_XDP
:
1320 case BPF_PROG_TYPE_LWT_XMIT
:
1321 case BPF_PROG_TYPE_SK_SKB
:
1322 case BPF_PROG_TYPE_SK_MSG
:
1324 return meta
->pkt_access
;
1326 env
->seen_direct_write
= true;
1333 static int __check_packet_access(struct bpf_verifier_env
*env
, u32 regno
,
1334 int off
, int size
, bool zero_size_allowed
)
1336 struct bpf_reg_state
*regs
= cur_regs(env
);
1337 struct bpf_reg_state
*reg
= ®s
[regno
];
1339 if (off
< 0 || size
< 0 || (size
== 0 && !zero_size_allowed
) ||
1340 (u64
)off
+ size
> reg
->range
) {
1341 verbose(env
, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
1342 off
, size
, regno
, reg
->id
, reg
->off
, reg
->range
);
1348 static int check_packet_access(struct bpf_verifier_env
*env
, u32 regno
, int off
,
1349 int size
, bool zero_size_allowed
)
1351 struct bpf_reg_state
*regs
= cur_regs(env
);
1352 struct bpf_reg_state
*reg
= ®s
[regno
];
1355 /* We may have added a variable offset to the packet pointer; but any
1356 * reg->range we have comes after that. We are only checking the fixed
1360 /* We don't allow negative numbers, because we aren't tracking enough
1361 * detail to prove they're safe.
1363 if (reg
->smin_value
< 0) {
1364 verbose(env
, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1368 err
= __check_packet_access(env
, regno
, off
, size
, zero_size_allowed
);
1370 verbose(env
, "R%d offset is outside of the packet\n", regno
);
1376 /* check access to 'struct bpf_context' fields. Supports fixed offsets only */
1377 static int check_ctx_access(struct bpf_verifier_env
*env
, int insn_idx
, int off
, int size
,
1378 enum bpf_access_type t
, enum bpf_reg_type
*reg_type
)
1380 struct bpf_insn_access_aux info
= {
1381 .reg_type
= *reg_type
,
1384 if (env
->ops
->is_valid_access
&&
1385 env
->ops
->is_valid_access(off
, size
, t
, env
->prog
, &info
)) {
1386 /* A non zero info.ctx_field_size indicates that this field is a
1387 * candidate for later verifier transformation to load the whole
1388 * field and then apply a mask when accessed with a narrower
1389 * access than actual ctx access size. A zero info.ctx_field_size
1390 * will only allow for whole field access and rejects any other
1391 * type of narrower access.
1393 *reg_type
= info
.reg_type
;
1395 env
->insn_aux_data
[insn_idx
].ctx_field_size
= info
.ctx_field_size
;
1396 /* remember the offset of last byte accessed in ctx */
1397 if (env
->prog
->aux
->max_ctx_offset
< off
+ size
)
1398 env
->prog
->aux
->max_ctx_offset
= off
+ size
;
1402 verbose(env
, "invalid bpf_context access off=%d size=%d\n", off
, size
);
1406 static bool __is_pointer_value(bool allow_ptr_leaks
,
1407 const struct bpf_reg_state
*reg
)
1409 if (allow_ptr_leaks
)
1412 return reg
->type
!= SCALAR_VALUE
;
1415 static bool is_pointer_value(struct bpf_verifier_env
*env
, int regno
)
1417 return __is_pointer_value(env
->allow_ptr_leaks
, cur_regs(env
) + regno
);
1420 static bool is_ctx_reg(struct bpf_verifier_env
*env
, int regno
)
1422 const struct bpf_reg_state
*reg
= cur_regs(env
) + regno
;
1424 return reg
->type
== PTR_TO_CTX
;
1427 static bool is_pkt_reg(struct bpf_verifier_env
*env
, int regno
)
1429 const struct bpf_reg_state
*reg
= cur_regs(env
) + regno
;
1431 return type_is_pkt_pointer(reg
->type
);
1434 static int check_pkt_ptr_alignment(struct bpf_verifier_env
*env
,
1435 const struct bpf_reg_state
*reg
,
1436 int off
, int size
, bool strict
)
1438 struct tnum reg_off
;
1441 /* Byte size accesses are always allowed. */
1442 if (!strict
|| size
== 1)
1445 /* For platforms that do not have a Kconfig enabling
1446 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1447 * NET_IP_ALIGN is universally set to '2'. And on platforms
1448 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1449 * to this code only in strict mode where we want to emulate
1450 * the NET_IP_ALIGN==2 checking. Therefore use an
1451 * unconditional IP align value of '2'.
1455 reg_off
= tnum_add(reg
->var_off
, tnum_const(ip_align
+ reg
->off
+ off
));
1456 if (!tnum_is_aligned(reg_off
, size
)) {
1459 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1461 "misaligned packet access off %d+%s+%d+%d size %d\n",
1462 ip_align
, tn_buf
, reg
->off
, off
, size
);
1469 static int check_generic_ptr_alignment(struct bpf_verifier_env
*env
,
1470 const struct bpf_reg_state
*reg
,
1471 const char *pointer_desc
,
1472 int off
, int size
, bool strict
)
1474 struct tnum reg_off
;
1476 /* Byte size accesses are always allowed. */
1477 if (!strict
|| size
== 1)
1480 reg_off
= tnum_add(reg
->var_off
, tnum_const(reg
->off
+ off
));
1481 if (!tnum_is_aligned(reg_off
, size
)) {
1484 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1485 verbose(env
, "misaligned %saccess off %s+%d+%d size %d\n",
1486 pointer_desc
, tn_buf
, reg
->off
, off
, size
);
1493 static int check_ptr_alignment(struct bpf_verifier_env
*env
,
1494 const struct bpf_reg_state
*reg
, int off
,
1495 int size
, bool strict_alignment_once
)
1497 bool strict
= env
->strict_alignment
|| strict_alignment_once
;
1498 const char *pointer_desc
= "";
1500 switch (reg
->type
) {
1502 case PTR_TO_PACKET_META
:
1503 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1504 * right in front, treat it the very same way.
1506 return check_pkt_ptr_alignment(env
, reg
, off
, size
, strict
);
1507 case PTR_TO_MAP_VALUE
:
1508 pointer_desc
= "value ";
1511 pointer_desc
= "context ";
1514 pointer_desc
= "stack ";
1515 /* The stack spill tracking logic in check_stack_write()
1516 * and check_stack_read() relies on stack accesses being
1524 return check_generic_ptr_alignment(env
, reg
, pointer_desc
, off
, size
,
1528 static int update_stack_depth(struct bpf_verifier_env
*env
,
1529 const struct bpf_func_state
*func
,
1532 u16 stack
= env
->subprog_info
[func
->subprogno
].stack_depth
;
1537 /* update known max for given subprogram */
1538 env
->subprog_info
[func
->subprogno
].stack_depth
= -off
;
1542 /* starting from main bpf function walk all instructions of the function
1543 * and recursively walk all callees that given function can call.
1544 * Ignore jump and exit insns.
1545 * Since recursion is prevented by check_cfg() this algorithm
1546 * only needs a local stack of MAX_CALL_FRAMES to remember callsites
1548 static int check_max_stack_depth(struct bpf_verifier_env
*env
)
1550 int depth
= 0, frame
= 0, idx
= 0, i
= 0, subprog_end
;
1551 struct bpf_subprog_info
*subprog
= env
->subprog_info
;
1552 struct bpf_insn
*insn
= env
->prog
->insnsi
;
1553 int ret_insn
[MAX_CALL_FRAMES
];
1554 int ret_prog
[MAX_CALL_FRAMES
];
1557 /* round up to 32-bytes, since this is granularity
1558 * of interpreter stack size
1560 depth
+= round_up(max_t(u32
, subprog
[idx
].stack_depth
, 1), 32);
1561 if (depth
> MAX_BPF_STACK
) {
1562 verbose(env
, "combined stack size of %d calls is %d. Too large\n",
1567 subprog_end
= subprog
[idx
+ 1].start
;
1568 for (; i
< subprog_end
; i
++) {
1569 if (insn
[i
].code
!= (BPF_JMP
| BPF_CALL
))
1571 if (insn
[i
].src_reg
!= BPF_PSEUDO_CALL
)
1573 /* remember insn and function to return to */
1574 ret_insn
[frame
] = i
+ 1;
1575 ret_prog
[frame
] = idx
;
1577 /* find the callee */
1578 i
= i
+ insn
[i
].imm
+ 1;
1579 idx
= find_subprog(env
, i
);
1581 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1586 if (frame
>= MAX_CALL_FRAMES
) {
1587 WARN_ONCE(1, "verifier bug. Call stack is too deep\n");
1592 /* end of for() loop means the last insn of the 'subprog'
1593 * was reached. Doesn't matter whether it was JA or EXIT
1597 depth
-= round_up(max_t(u32
, subprog
[idx
].stack_depth
, 1), 32);
1599 i
= ret_insn
[frame
];
1600 idx
= ret_prog
[frame
];
1604 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1605 static int get_callee_stack_depth(struct bpf_verifier_env
*env
,
1606 const struct bpf_insn
*insn
, int idx
)
1608 int start
= idx
+ insn
->imm
+ 1, subprog
;
1610 subprog
= find_subprog(env
, start
);
1612 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1616 return env
->subprog_info
[subprog
].stack_depth
;
1620 /* truncate register to smaller size (in bytes)
1621 * must be called with size < BPF_REG_SIZE
1623 static void coerce_reg_to_size(struct bpf_reg_state
*reg
, int size
)
1627 /* clear high bits in bit representation */
1628 reg
->var_off
= tnum_cast(reg
->var_off
, size
);
1630 /* fix arithmetic bounds */
1631 mask
= ((u64
)1 << (size
* 8)) - 1;
1632 if ((reg
->umin_value
& ~mask
) == (reg
->umax_value
& ~mask
)) {
1633 reg
->umin_value
&= mask
;
1634 reg
->umax_value
&= mask
;
1636 reg
->umin_value
= 0;
1637 reg
->umax_value
= mask
;
1639 reg
->smin_value
= reg
->umin_value
;
1640 reg
->smax_value
= reg
->umax_value
;
1643 /* check whether memory at (regno + off) is accessible for t = (read | write)
1644 * if t==write, value_regno is a register which value is stored into memory
1645 * if t==read, value_regno is a register which will receive the value from memory
1646 * if t==write && value_regno==-1, some unknown value is stored into memory
1647 * if t==read && value_regno==-1, don't care what we read from memory
1649 static int check_mem_access(struct bpf_verifier_env
*env
, int insn_idx
, u32 regno
,
1650 int off
, int bpf_size
, enum bpf_access_type t
,
1651 int value_regno
, bool strict_alignment_once
)
1653 struct bpf_reg_state
*regs
= cur_regs(env
);
1654 struct bpf_reg_state
*reg
= regs
+ regno
;
1655 struct bpf_func_state
*state
;
1658 size
= bpf_size_to_bytes(bpf_size
);
1662 /* alignment checks will add in reg->off themselves */
1663 err
= check_ptr_alignment(env
, reg
, off
, size
, strict_alignment_once
);
1667 /* for access checks, reg->off is just part of off */
1670 if (reg
->type
== PTR_TO_MAP_VALUE
) {
1671 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
1672 is_pointer_value(env
, value_regno
)) {
1673 verbose(env
, "R%d leaks addr into map\n", value_regno
);
1677 err
= check_map_access(env
, regno
, off
, size
, false);
1678 if (!err
&& t
== BPF_READ
&& value_regno
>= 0)
1679 mark_reg_unknown(env
, regs
, value_regno
);
1681 } else if (reg
->type
== PTR_TO_CTX
) {
1682 enum bpf_reg_type reg_type
= SCALAR_VALUE
;
1684 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
1685 is_pointer_value(env
, value_regno
)) {
1686 verbose(env
, "R%d leaks addr into ctx\n", value_regno
);
1689 /* ctx accesses must be at a fixed offset, so that we can
1690 * determine what type of data were returned.
1694 "dereference of modified ctx ptr R%d off=%d+%d, ctx+const is allowed, ctx+const+const is not\n",
1695 regno
, reg
->off
, off
- reg
->off
);
1698 if (!tnum_is_const(reg
->var_off
) || reg
->var_off
.value
) {
1701 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1703 "variable ctx access var_off=%s off=%d size=%d",
1707 err
= check_ctx_access(env
, insn_idx
, off
, size
, t
, ®_type
);
1708 if (!err
&& t
== BPF_READ
&& value_regno
>= 0) {
1709 /* ctx access returns either a scalar, or a
1710 * PTR_TO_PACKET[_META,_END]. In the latter
1711 * case, we know the offset is zero.
1713 if (reg_type
== SCALAR_VALUE
)
1714 mark_reg_unknown(env
, regs
, value_regno
);
1716 mark_reg_known_zero(env
, regs
,
1718 regs
[value_regno
].id
= 0;
1719 regs
[value_regno
].off
= 0;
1720 regs
[value_regno
].range
= 0;
1721 regs
[value_regno
].type
= reg_type
;
1724 } else if (reg
->type
== PTR_TO_STACK
) {
1725 /* stack accesses must be at a fixed offset, so that we can
1726 * determine what type of data were returned.
1727 * See check_stack_read().
1729 if (!tnum_is_const(reg
->var_off
)) {
1732 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1733 verbose(env
, "variable stack access var_off=%s off=%d size=%d",
1737 off
+= reg
->var_off
.value
;
1738 if (off
>= 0 || off
< -MAX_BPF_STACK
) {
1739 verbose(env
, "invalid stack off=%d size=%d\n", off
,
1744 state
= func(env
, reg
);
1745 err
= update_stack_depth(env
, state
, off
);
1750 err
= check_stack_write(env
, state
, off
, size
,
1751 value_regno
, insn_idx
);
1753 err
= check_stack_read(env
, state
, off
, size
,
1755 } else if (reg_is_pkt_pointer(reg
)) {
1756 if (t
== BPF_WRITE
&& !may_access_direct_pkt_data(env
, NULL
, t
)) {
1757 verbose(env
, "cannot write into packet\n");
1760 if (t
== BPF_WRITE
&& value_regno
>= 0 &&
1761 is_pointer_value(env
, value_regno
)) {
1762 verbose(env
, "R%d leaks addr into packet\n",
1766 err
= check_packet_access(env
, regno
, off
, size
, false);
1767 if (!err
&& t
== BPF_READ
&& value_regno
>= 0)
1768 mark_reg_unknown(env
, regs
, value_regno
);
1770 verbose(env
, "R%d invalid mem access '%s'\n", regno
,
1771 reg_type_str
[reg
->type
]);
1775 if (!err
&& size
< BPF_REG_SIZE
&& value_regno
>= 0 && t
== BPF_READ
&&
1776 regs
[value_regno
].type
== SCALAR_VALUE
) {
1777 /* b/h/w load zero-extends, mark upper bits as known 0 */
1778 coerce_reg_to_size(®s
[value_regno
], size
);
1783 static int check_xadd(struct bpf_verifier_env
*env
, int insn_idx
, struct bpf_insn
*insn
)
1787 if ((BPF_SIZE(insn
->code
) != BPF_W
&& BPF_SIZE(insn
->code
) != BPF_DW
) ||
1789 verbose(env
, "BPF_XADD uses reserved fields\n");
1793 /* check src1 operand */
1794 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
1798 /* check src2 operand */
1799 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
1803 if (is_pointer_value(env
, insn
->src_reg
)) {
1804 verbose(env
, "R%d leaks addr into mem\n", insn
->src_reg
);
1808 if (is_ctx_reg(env
, insn
->dst_reg
) ||
1809 is_pkt_reg(env
, insn
->dst_reg
)) {
1810 verbose(env
, "BPF_XADD stores into R%d %s is not allowed\n",
1811 insn
->dst_reg
, is_ctx_reg(env
, insn
->dst_reg
) ?
1812 "context" : "packet");
1816 /* check whether atomic_add can read the memory */
1817 err
= check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
1818 BPF_SIZE(insn
->code
), BPF_READ
, -1, true);
1822 /* check whether atomic_add can write into the same memory */
1823 return check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
1824 BPF_SIZE(insn
->code
), BPF_WRITE
, -1, true);
1827 /* when register 'regno' is passed into function that will read 'access_size'
1828 * bytes from that pointer, make sure that it's within stack boundary
1829 * and all elements of stack are initialized.
1830 * Unlike most pointer bounds-checking functions, this one doesn't take an
1831 * 'off' argument, so it has to add in reg->off itself.
1833 static int check_stack_boundary(struct bpf_verifier_env
*env
, int regno
,
1834 int access_size
, bool zero_size_allowed
,
1835 struct bpf_call_arg_meta
*meta
)
1837 struct bpf_reg_state
*reg
= cur_regs(env
) + regno
;
1838 struct bpf_func_state
*state
= func(env
, reg
);
1839 int off
, i
, slot
, spi
;
1841 if (reg
->type
!= PTR_TO_STACK
) {
1842 /* Allow zero-byte read from NULL, regardless of pointer type */
1843 if (zero_size_allowed
&& access_size
== 0 &&
1844 register_is_null(reg
))
1847 verbose(env
, "R%d type=%s expected=%s\n", regno
,
1848 reg_type_str
[reg
->type
],
1849 reg_type_str
[PTR_TO_STACK
]);
1853 /* Only allow fixed-offset stack reads */
1854 if (!tnum_is_const(reg
->var_off
)) {
1857 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
1858 verbose(env
, "invalid variable stack read R%d var_off=%s\n",
1862 off
= reg
->off
+ reg
->var_off
.value
;
1863 if (off
>= 0 || off
< -MAX_BPF_STACK
|| off
+ access_size
> 0 ||
1864 access_size
< 0 || (access_size
== 0 && !zero_size_allowed
)) {
1865 verbose(env
, "invalid stack type R%d off=%d access_size=%d\n",
1866 regno
, off
, access_size
);
1870 if (meta
&& meta
->raw_mode
) {
1871 meta
->access_size
= access_size
;
1872 meta
->regno
= regno
;
1876 for (i
= 0; i
< access_size
; i
++) {
1879 slot
= -(off
+ i
) - 1;
1880 spi
= slot
/ BPF_REG_SIZE
;
1881 if (state
->allocated_stack
<= slot
)
1883 stype
= &state
->stack
[spi
].slot_type
[slot
% BPF_REG_SIZE
];
1884 if (*stype
== STACK_MISC
)
1886 if (*stype
== STACK_ZERO
) {
1887 /* helper can write anything into the stack */
1888 *stype
= STACK_MISC
;
1892 verbose(env
, "invalid indirect read from stack off %d+%d size %d\n",
1893 off
, i
, access_size
);
1896 /* reading any byte out of 8-byte 'spill_slot' will cause
1897 * the whole slot to be marked as 'read'
1899 mark_stack_slot_read(env
, env
->cur_state
, env
->cur_state
->parent
,
1900 spi
, state
->frameno
);
1902 return update_stack_depth(env
, state
, off
);
1905 static int check_helper_mem_access(struct bpf_verifier_env
*env
, int regno
,
1906 int access_size
, bool zero_size_allowed
,
1907 struct bpf_call_arg_meta
*meta
)
1909 struct bpf_reg_state
*regs
= cur_regs(env
), *reg
= ®s
[regno
];
1911 switch (reg
->type
) {
1913 case PTR_TO_PACKET_META
:
1914 return check_packet_access(env
, regno
, reg
->off
, access_size
,
1916 case PTR_TO_MAP_VALUE
:
1917 return check_map_access(env
, regno
, reg
->off
, access_size
,
1919 default: /* scalar_value|ptr_to_stack or invalid ptr */
1920 return check_stack_boundary(env
, regno
, access_size
,
1921 zero_size_allowed
, meta
);
1925 static bool arg_type_is_mem_ptr(enum bpf_arg_type type
)
1927 return type
== ARG_PTR_TO_MEM
||
1928 type
== ARG_PTR_TO_MEM_OR_NULL
||
1929 type
== ARG_PTR_TO_UNINIT_MEM
;
1932 static bool arg_type_is_mem_size(enum bpf_arg_type type
)
1934 return type
== ARG_CONST_SIZE
||
1935 type
== ARG_CONST_SIZE_OR_ZERO
;
1938 static int check_func_arg(struct bpf_verifier_env
*env
, u32 regno
,
1939 enum bpf_arg_type arg_type
,
1940 struct bpf_call_arg_meta
*meta
)
1942 struct bpf_reg_state
*regs
= cur_regs(env
), *reg
= ®s
[regno
];
1943 enum bpf_reg_type expected_type
, type
= reg
->type
;
1946 if (arg_type
== ARG_DONTCARE
)
1949 err
= check_reg_arg(env
, regno
, SRC_OP
);
1953 if (arg_type
== ARG_ANYTHING
) {
1954 if (is_pointer_value(env
, regno
)) {
1955 verbose(env
, "R%d leaks addr into helper function\n",
1962 if (type_is_pkt_pointer(type
) &&
1963 !may_access_direct_pkt_data(env
, meta
, BPF_READ
)) {
1964 verbose(env
, "helper access to the packet is not allowed\n");
1968 if (arg_type
== ARG_PTR_TO_MAP_KEY
||
1969 arg_type
== ARG_PTR_TO_MAP_VALUE
) {
1970 expected_type
= PTR_TO_STACK
;
1971 if (!type_is_pkt_pointer(type
) && type
!= PTR_TO_MAP_VALUE
&&
1972 type
!= expected_type
)
1974 } else if (arg_type
== ARG_CONST_SIZE
||
1975 arg_type
== ARG_CONST_SIZE_OR_ZERO
) {
1976 expected_type
= SCALAR_VALUE
;
1977 if (type
!= expected_type
)
1979 } else if (arg_type
== ARG_CONST_MAP_PTR
) {
1980 expected_type
= CONST_PTR_TO_MAP
;
1981 if (type
!= expected_type
)
1983 } else if (arg_type
== ARG_PTR_TO_CTX
) {
1984 expected_type
= PTR_TO_CTX
;
1985 if (type
!= expected_type
)
1987 } else if (arg_type_is_mem_ptr(arg_type
)) {
1988 expected_type
= PTR_TO_STACK
;
1989 /* One exception here. In case function allows for NULL to be
1990 * passed in as argument, it's a SCALAR_VALUE type. Final test
1991 * happens during stack boundary checking.
1993 if (register_is_null(reg
) &&
1994 arg_type
== ARG_PTR_TO_MEM_OR_NULL
)
1995 /* final test in check_stack_boundary() */;
1996 else if (!type_is_pkt_pointer(type
) &&
1997 type
!= PTR_TO_MAP_VALUE
&&
1998 type
!= expected_type
)
2000 meta
->raw_mode
= arg_type
== ARG_PTR_TO_UNINIT_MEM
;
2002 verbose(env
, "unsupported arg_type %d\n", arg_type
);
2006 if (arg_type
== ARG_CONST_MAP_PTR
) {
2007 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
2008 meta
->map_ptr
= reg
->map_ptr
;
2009 } else if (arg_type
== ARG_PTR_TO_MAP_KEY
) {
2010 /* bpf_map_xxx(..., map_ptr, ..., key) call:
2011 * check that [key, key + map->key_size) are within
2012 * stack limits and initialized
2014 if (!meta
->map_ptr
) {
2015 /* in function declaration map_ptr must come before
2016 * map_key, so that it's verified and known before
2017 * we have to check map_key here. Otherwise it means
2018 * that kernel subsystem misconfigured verifier
2020 verbose(env
, "invalid map_ptr to access map->key\n");
2023 err
= check_helper_mem_access(env
, regno
,
2024 meta
->map_ptr
->key_size
, false,
2026 } else if (arg_type
== ARG_PTR_TO_MAP_VALUE
) {
2027 /* bpf_map_xxx(..., map_ptr, ..., value) call:
2028 * check [value, value + map->value_size) validity
2030 if (!meta
->map_ptr
) {
2031 /* kernel subsystem misconfigured verifier */
2032 verbose(env
, "invalid map_ptr to access map->value\n");
2035 err
= check_helper_mem_access(env
, regno
,
2036 meta
->map_ptr
->value_size
, false,
2038 } else if (arg_type_is_mem_size(arg_type
)) {
2039 bool zero_size_allowed
= (arg_type
== ARG_CONST_SIZE_OR_ZERO
);
2041 /* remember the mem_size which may be used later
2042 * to refine return values.
2044 meta
->msize_smax_value
= reg
->smax_value
;
2045 meta
->msize_umax_value
= reg
->umax_value
;
2047 /* The register is SCALAR_VALUE; the access check
2048 * happens using its boundaries.
2050 if (!tnum_is_const(reg
->var_off
))
2051 /* For unprivileged variable accesses, disable raw
2052 * mode so that the program is required to
2053 * initialize all the memory that the helper could
2054 * just partially fill up.
2058 if (reg
->smin_value
< 0) {
2059 verbose(env
, "R%d min value is negative, either use unsigned or 'var &= const'\n",
2064 if (reg
->umin_value
== 0) {
2065 err
= check_helper_mem_access(env
, regno
- 1, 0,
2072 if (reg
->umax_value
>= BPF_MAX_VAR_SIZ
) {
2073 verbose(env
, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
2077 err
= check_helper_mem_access(env
, regno
- 1,
2079 zero_size_allowed
, meta
);
2084 verbose(env
, "R%d type=%s expected=%s\n", regno
,
2085 reg_type_str
[type
], reg_type_str
[expected_type
]);
2089 static int check_map_func_compatibility(struct bpf_verifier_env
*env
,
2090 struct bpf_map
*map
, int func_id
)
2095 /* We need a two way check, first is from map perspective ... */
2096 switch (map
->map_type
) {
2097 case BPF_MAP_TYPE_PROG_ARRAY
:
2098 if (func_id
!= BPF_FUNC_tail_call
)
2101 case BPF_MAP_TYPE_PERF_EVENT_ARRAY
:
2102 if (func_id
!= BPF_FUNC_perf_event_read
&&
2103 func_id
!= BPF_FUNC_perf_event_output
&&
2104 func_id
!= BPF_FUNC_perf_event_read_value
)
2107 case BPF_MAP_TYPE_STACK_TRACE
:
2108 if (func_id
!= BPF_FUNC_get_stackid
)
2111 case BPF_MAP_TYPE_CGROUP_ARRAY
:
2112 if (func_id
!= BPF_FUNC_skb_under_cgroup
&&
2113 func_id
!= BPF_FUNC_current_task_under_cgroup
)
2116 /* devmap returns a pointer to a live net_device ifindex that we cannot
2117 * allow to be modified from bpf side. So do not allow lookup elements
2120 case BPF_MAP_TYPE_DEVMAP
:
2121 if (func_id
!= BPF_FUNC_redirect_map
)
2124 /* Restrict bpf side of cpumap and xskmap, open when use-cases
2127 case BPF_MAP_TYPE_CPUMAP
:
2128 case BPF_MAP_TYPE_XSKMAP
:
2129 if (func_id
!= BPF_FUNC_redirect_map
)
2132 case BPF_MAP_TYPE_ARRAY_OF_MAPS
:
2133 case BPF_MAP_TYPE_HASH_OF_MAPS
:
2134 if (func_id
!= BPF_FUNC_map_lookup_elem
)
2137 case BPF_MAP_TYPE_SOCKMAP
:
2138 if (func_id
!= BPF_FUNC_sk_redirect_map
&&
2139 func_id
!= BPF_FUNC_sock_map_update
&&
2140 func_id
!= BPF_FUNC_map_delete_elem
&&
2141 func_id
!= BPF_FUNC_msg_redirect_map
)
2144 case BPF_MAP_TYPE_SOCKHASH
:
2145 if (func_id
!= BPF_FUNC_sk_redirect_hash
&&
2146 func_id
!= BPF_FUNC_sock_hash_update
&&
2147 func_id
!= BPF_FUNC_map_delete_elem
&&
2148 func_id
!= BPF_FUNC_msg_redirect_hash
)
2155 /* ... and second from the function itself. */
2157 case BPF_FUNC_tail_call
:
2158 if (map
->map_type
!= BPF_MAP_TYPE_PROG_ARRAY
)
2160 if (env
->subprog_cnt
> 1) {
2161 verbose(env
, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
2165 case BPF_FUNC_perf_event_read
:
2166 case BPF_FUNC_perf_event_output
:
2167 case BPF_FUNC_perf_event_read_value
:
2168 if (map
->map_type
!= BPF_MAP_TYPE_PERF_EVENT_ARRAY
)
2171 case BPF_FUNC_get_stackid
:
2172 if (map
->map_type
!= BPF_MAP_TYPE_STACK_TRACE
)
2175 case BPF_FUNC_current_task_under_cgroup
:
2176 case BPF_FUNC_skb_under_cgroup
:
2177 if (map
->map_type
!= BPF_MAP_TYPE_CGROUP_ARRAY
)
2180 case BPF_FUNC_redirect_map
:
2181 if (map
->map_type
!= BPF_MAP_TYPE_DEVMAP
&&
2182 map
->map_type
!= BPF_MAP_TYPE_CPUMAP
&&
2183 map
->map_type
!= BPF_MAP_TYPE_XSKMAP
)
2186 case BPF_FUNC_sk_redirect_map
:
2187 case BPF_FUNC_msg_redirect_map
:
2188 case BPF_FUNC_sock_map_update
:
2189 if (map
->map_type
!= BPF_MAP_TYPE_SOCKMAP
)
2192 case BPF_FUNC_sk_redirect_hash
:
2193 case BPF_FUNC_msg_redirect_hash
:
2194 case BPF_FUNC_sock_hash_update
:
2195 if (map
->map_type
!= BPF_MAP_TYPE_SOCKHASH
)
2204 verbose(env
, "cannot pass map_type %d into func %s#%d\n",
2205 map
->map_type
, func_id_name(func_id
), func_id
);
2209 static bool check_raw_mode_ok(const struct bpf_func_proto
*fn
)
2213 if (fn
->arg1_type
== ARG_PTR_TO_UNINIT_MEM
)
2215 if (fn
->arg2_type
== ARG_PTR_TO_UNINIT_MEM
)
2217 if (fn
->arg3_type
== ARG_PTR_TO_UNINIT_MEM
)
2219 if (fn
->arg4_type
== ARG_PTR_TO_UNINIT_MEM
)
2221 if (fn
->arg5_type
== ARG_PTR_TO_UNINIT_MEM
)
2224 /* We only support one arg being in raw mode at the moment,
2225 * which is sufficient for the helper functions we have
2231 static bool check_args_pair_invalid(enum bpf_arg_type arg_curr
,
2232 enum bpf_arg_type arg_next
)
2234 return (arg_type_is_mem_ptr(arg_curr
) &&
2235 !arg_type_is_mem_size(arg_next
)) ||
2236 (!arg_type_is_mem_ptr(arg_curr
) &&
2237 arg_type_is_mem_size(arg_next
));
2240 static bool check_arg_pair_ok(const struct bpf_func_proto
*fn
)
2242 /* bpf_xxx(..., buf, len) call will access 'len'
2243 * bytes from memory 'buf'. Both arg types need
2244 * to be paired, so make sure there's no buggy
2245 * helper function specification.
2247 if (arg_type_is_mem_size(fn
->arg1_type
) ||
2248 arg_type_is_mem_ptr(fn
->arg5_type
) ||
2249 check_args_pair_invalid(fn
->arg1_type
, fn
->arg2_type
) ||
2250 check_args_pair_invalid(fn
->arg2_type
, fn
->arg3_type
) ||
2251 check_args_pair_invalid(fn
->arg3_type
, fn
->arg4_type
) ||
2252 check_args_pair_invalid(fn
->arg4_type
, fn
->arg5_type
))
2258 static int check_func_proto(const struct bpf_func_proto
*fn
)
2260 return check_raw_mode_ok(fn
) &&
2261 check_arg_pair_ok(fn
) ? 0 : -EINVAL
;
2264 /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
2265 * are now invalid, so turn them into unknown SCALAR_VALUE.
2267 static void __clear_all_pkt_pointers(struct bpf_verifier_env
*env
,
2268 struct bpf_func_state
*state
)
2270 struct bpf_reg_state
*regs
= state
->regs
, *reg
;
2273 for (i
= 0; i
< MAX_BPF_REG
; i
++)
2274 if (reg_is_pkt_pointer_any(®s
[i
]))
2275 mark_reg_unknown(env
, regs
, i
);
2277 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
2278 if (state
->stack
[i
].slot_type
[0] != STACK_SPILL
)
2280 reg
= &state
->stack
[i
].spilled_ptr
;
2281 if (reg_is_pkt_pointer_any(reg
))
2282 __mark_reg_unknown(reg
);
2286 static void clear_all_pkt_pointers(struct bpf_verifier_env
*env
)
2288 struct bpf_verifier_state
*vstate
= env
->cur_state
;
2291 for (i
= 0; i
<= vstate
->curframe
; i
++)
2292 __clear_all_pkt_pointers(env
, vstate
->frame
[i
]);
2295 static int check_func_call(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
,
2298 struct bpf_verifier_state
*state
= env
->cur_state
;
2299 struct bpf_func_state
*caller
, *callee
;
2300 int i
, subprog
, target_insn
;
2302 if (state
->curframe
+ 1 >= MAX_CALL_FRAMES
) {
2303 verbose(env
, "the call stack of %d frames is too deep\n",
2304 state
->curframe
+ 2);
2308 target_insn
= *insn_idx
+ insn
->imm
;
2309 subprog
= find_subprog(env
, target_insn
+ 1);
2311 verbose(env
, "verifier bug. No program starts at insn %d\n",
2316 caller
= state
->frame
[state
->curframe
];
2317 if (state
->frame
[state
->curframe
+ 1]) {
2318 verbose(env
, "verifier bug. Frame %d already allocated\n",
2319 state
->curframe
+ 1);
2323 callee
= kzalloc(sizeof(*callee
), GFP_KERNEL
);
2326 state
->frame
[state
->curframe
+ 1] = callee
;
2328 /* callee cannot access r0, r6 - r9 for reading and has to write
2329 * into its own stack before reading from it.
2330 * callee can read/write into caller's stack
2332 init_func_state(env
, callee
,
2333 /* remember the callsite, it will be used by bpf_exit */
2334 *insn_idx
/* callsite */,
2335 state
->curframe
+ 1 /* frameno within this callchain */,
2336 subprog
/* subprog number within this prog */);
2338 /* copy r1 - r5 args that callee can access */
2339 for (i
= BPF_REG_1
; i
<= BPF_REG_5
; i
++)
2340 callee
->regs
[i
] = caller
->regs
[i
];
2342 /* after the call regsiters r0 - r5 were scratched */
2343 for (i
= 0; i
< CALLER_SAVED_REGS
; i
++) {
2344 mark_reg_not_init(env
, caller
->regs
, caller_saved
[i
]);
2345 check_reg_arg(env
, caller_saved
[i
], DST_OP_NO_MARK
);
2348 /* only increment it after check_reg_arg() finished */
2351 /* and go analyze first insn of the callee */
2352 *insn_idx
= target_insn
;
2354 if (env
->log
.level
) {
2355 verbose(env
, "caller:\n");
2356 print_verifier_state(env
, caller
);
2357 verbose(env
, "callee:\n");
2358 print_verifier_state(env
, callee
);
2363 static int prepare_func_exit(struct bpf_verifier_env
*env
, int *insn_idx
)
2365 struct bpf_verifier_state
*state
= env
->cur_state
;
2366 struct bpf_func_state
*caller
, *callee
;
2367 struct bpf_reg_state
*r0
;
2369 callee
= state
->frame
[state
->curframe
];
2370 r0
= &callee
->regs
[BPF_REG_0
];
2371 if (r0
->type
== PTR_TO_STACK
) {
2372 /* technically it's ok to return caller's stack pointer
2373 * (or caller's caller's pointer) back to the caller,
2374 * since these pointers are valid. Only current stack
2375 * pointer will be invalid as soon as function exits,
2376 * but let's be conservative
2378 verbose(env
, "cannot return stack pointer to the caller\n");
2383 caller
= state
->frame
[state
->curframe
];
2384 /* return to the caller whatever r0 had in the callee */
2385 caller
->regs
[BPF_REG_0
] = *r0
;
2387 *insn_idx
= callee
->callsite
+ 1;
2388 if (env
->log
.level
) {
2389 verbose(env
, "returning from callee:\n");
2390 print_verifier_state(env
, callee
);
2391 verbose(env
, "to caller at %d:\n", *insn_idx
);
2392 print_verifier_state(env
, caller
);
2394 /* clear everything in the callee */
2395 free_func_state(callee
);
2396 state
->frame
[state
->curframe
+ 1] = NULL
;
2400 static void do_refine_retval_range(struct bpf_reg_state
*regs
, int ret_type
,
2402 struct bpf_call_arg_meta
*meta
)
2404 struct bpf_reg_state
*ret_reg
= ®s
[BPF_REG_0
];
2406 if (ret_type
!= RET_INTEGER
||
2407 (func_id
!= BPF_FUNC_get_stack
&&
2408 func_id
!= BPF_FUNC_probe_read_str
))
2411 ret_reg
->smax_value
= meta
->msize_smax_value
;
2412 ret_reg
->umax_value
= meta
->msize_umax_value
;
2413 __reg_deduce_bounds(ret_reg
);
2414 __reg_bound_offset(ret_reg
);
2418 record_func_map(struct bpf_verifier_env
*env
, struct bpf_call_arg_meta
*meta
,
2419 int func_id
, int insn_idx
)
2421 struct bpf_insn_aux_data
*aux
= &env
->insn_aux_data
[insn_idx
];
2423 if (func_id
!= BPF_FUNC_tail_call
&&
2424 func_id
!= BPF_FUNC_map_lookup_elem
&&
2425 func_id
!= BPF_FUNC_map_update_elem
&&
2426 func_id
!= BPF_FUNC_map_delete_elem
)
2429 if (meta
->map_ptr
== NULL
) {
2430 verbose(env
, "kernel subsystem misconfigured verifier\n");
2434 if (!BPF_MAP_PTR(aux
->map_state
))
2435 bpf_map_ptr_store(aux
, meta
->map_ptr
,
2436 meta
->map_ptr
->unpriv_array
);
2437 else if (BPF_MAP_PTR(aux
->map_state
) != meta
->map_ptr
)
2438 bpf_map_ptr_store(aux
, BPF_MAP_PTR_POISON
,
2439 meta
->map_ptr
->unpriv_array
);
2443 static int check_helper_call(struct bpf_verifier_env
*env
, int func_id
, int insn_idx
)
2445 const struct bpf_func_proto
*fn
= NULL
;
2446 struct bpf_reg_state
*regs
;
2447 struct bpf_call_arg_meta meta
;
2451 /* find function prototype */
2452 if (func_id
< 0 || func_id
>= __BPF_FUNC_MAX_ID
) {
2453 verbose(env
, "invalid func %s#%d\n", func_id_name(func_id
),
2458 if (env
->ops
->get_func_proto
)
2459 fn
= env
->ops
->get_func_proto(func_id
, env
->prog
);
2461 verbose(env
, "unknown func %s#%d\n", func_id_name(func_id
),
2466 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2467 if (!env
->prog
->gpl_compatible
&& fn
->gpl_only
) {
2468 verbose(env
, "cannot call GPL-restricted function from non-GPL compatible program\n");
2472 /* With LD_ABS/IND some JITs save/restore skb from r1. */
2473 changes_data
= bpf_helper_changes_pkt_data(fn
->func
);
2474 if (changes_data
&& fn
->arg1_type
!= ARG_PTR_TO_CTX
) {
2475 verbose(env
, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
2476 func_id_name(func_id
), func_id
);
2480 memset(&meta
, 0, sizeof(meta
));
2481 meta
.pkt_access
= fn
->pkt_access
;
2483 err
= check_func_proto(fn
);
2485 verbose(env
, "kernel subsystem misconfigured func %s#%d\n",
2486 func_id_name(func_id
), func_id
);
2491 err
= check_func_arg(env
, BPF_REG_1
, fn
->arg1_type
, &meta
);
2494 err
= check_func_arg(env
, BPF_REG_2
, fn
->arg2_type
, &meta
);
2497 err
= check_func_arg(env
, BPF_REG_3
, fn
->arg3_type
, &meta
);
2500 err
= check_func_arg(env
, BPF_REG_4
, fn
->arg4_type
, &meta
);
2503 err
= check_func_arg(env
, BPF_REG_5
, fn
->arg5_type
, &meta
);
2507 err
= record_func_map(env
, &meta
, func_id
, insn_idx
);
2511 /* Mark slots with STACK_MISC in case of raw mode, stack offset
2512 * is inferred from register state.
2514 for (i
= 0; i
< meta
.access_size
; i
++) {
2515 err
= check_mem_access(env
, insn_idx
, meta
.regno
, i
, BPF_B
,
2516 BPF_WRITE
, -1, false);
2521 regs
= cur_regs(env
);
2522 /* reset caller saved regs */
2523 for (i
= 0; i
< CALLER_SAVED_REGS
; i
++) {
2524 mark_reg_not_init(env
, regs
, caller_saved
[i
]);
2525 check_reg_arg(env
, caller_saved
[i
], DST_OP_NO_MARK
);
2528 /* update return register (already marked as written above) */
2529 if (fn
->ret_type
== RET_INTEGER
) {
2530 /* sets type to SCALAR_VALUE */
2531 mark_reg_unknown(env
, regs
, BPF_REG_0
);
2532 } else if (fn
->ret_type
== RET_VOID
) {
2533 regs
[BPF_REG_0
].type
= NOT_INIT
;
2534 } else if (fn
->ret_type
== RET_PTR_TO_MAP_VALUE_OR_NULL
) {
2535 regs
[BPF_REG_0
].type
= PTR_TO_MAP_VALUE_OR_NULL
;
2536 /* There is no offset yet applied, variable or fixed */
2537 mark_reg_known_zero(env
, regs
, BPF_REG_0
);
2538 regs
[BPF_REG_0
].off
= 0;
2539 /* remember map_ptr, so that check_map_access()
2540 * can check 'value_size' boundary of memory access
2541 * to map element returned from bpf_map_lookup_elem()
2543 if (meta
.map_ptr
== NULL
) {
2545 "kernel subsystem misconfigured verifier\n");
2548 regs
[BPF_REG_0
].map_ptr
= meta
.map_ptr
;
2549 regs
[BPF_REG_0
].id
= ++env
->id_gen
;
2551 verbose(env
, "unknown return type %d of func %s#%d\n",
2552 fn
->ret_type
, func_id_name(func_id
), func_id
);
2556 do_refine_retval_range(regs
, fn
->ret_type
, func_id
, &meta
);
2558 err
= check_map_func_compatibility(env
, meta
.map_ptr
, func_id
);
2562 if (func_id
== BPF_FUNC_get_stack
&& !env
->prog
->has_callchain_buf
) {
2563 const char *err_str
;
2565 #ifdef CONFIG_PERF_EVENTS
2566 err
= get_callchain_buffers(sysctl_perf_event_max_stack
);
2567 err_str
= "cannot get callchain buffer for func %s#%d\n";
2570 err_str
= "func %s#%d not supported without CONFIG_PERF_EVENTS\n";
2573 verbose(env
, err_str
, func_id_name(func_id
), func_id
);
2577 env
->prog
->has_callchain_buf
= true;
2581 clear_all_pkt_pointers(env
);
2585 static bool signed_add_overflows(s64 a
, s64 b
)
2587 /* Do the add in u64, where overflow is well-defined */
2588 s64 res
= (s64
)((u64
)a
+ (u64
)b
);
2595 static bool signed_sub_overflows(s64 a
, s64 b
)
2597 /* Do the sub in u64, where overflow is well-defined */
2598 s64 res
= (s64
)((u64
)a
- (u64
)b
);
2605 static bool check_reg_sane_offset(struct bpf_verifier_env
*env
,
2606 const struct bpf_reg_state
*reg
,
2607 enum bpf_reg_type type
)
2609 bool known
= tnum_is_const(reg
->var_off
);
2610 s64 val
= reg
->var_off
.value
;
2611 s64 smin
= reg
->smin_value
;
2613 if (known
&& (val
>= BPF_MAX_VAR_OFF
|| val
<= -BPF_MAX_VAR_OFF
)) {
2614 verbose(env
, "math between %s pointer and %lld is not allowed\n",
2615 reg_type_str
[type
], val
);
2619 if (reg
->off
>= BPF_MAX_VAR_OFF
|| reg
->off
<= -BPF_MAX_VAR_OFF
) {
2620 verbose(env
, "%s pointer offset %d is not allowed\n",
2621 reg_type_str
[type
], reg
->off
);
2625 if (smin
== S64_MIN
) {
2626 verbose(env
, "math between %s pointer and register with unbounded min value is not allowed\n",
2627 reg_type_str
[type
]);
2631 if (smin
>= BPF_MAX_VAR_OFF
|| smin
<= -BPF_MAX_VAR_OFF
) {
2632 verbose(env
, "value %lld makes %s pointer be out of bounds\n",
2633 smin
, reg_type_str
[type
]);
2640 /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
2641 * Caller should also handle BPF_MOV case separately.
2642 * If we return -EACCES, caller may want to try again treating pointer as a
2643 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
2645 static int adjust_ptr_min_max_vals(struct bpf_verifier_env
*env
,
2646 struct bpf_insn
*insn
,
2647 const struct bpf_reg_state
*ptr_reg
,
2648 const struct bpf_reg_state
*off_reg
)
2650 struct bpf_verifier_state
*vstate
= env
->cur_state
;
2651 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
2652 struct bpf_reg_state
*regs
= state
->regs
, *dst_reg
;
2653 bool known
= tnum_is_const(off_reg
->var_off
);
2654 s64 smin_val
= off_reg
->smin_value
, smax_val
= off_reg
->smax_value
,
2655 smin_ptr
= ptr_reg
->smin_value
, smax_ptr
= ptr_reg
->smax_value
;
2656 u64 umin_val
= off_reg
->umin_value
, umax_val
= off_reg
->umax_value
,
2657 umin_ptr
= ptr_reg
->umin_value
, umax_ptr
= ptr_reg
->umax_value
;
2658 u8 opcode
= BPF_OP(insn
->code
);
2659 u32 dst
= insn
->dst_reg
;
2661 dst_reg
= ®s
[dst
];
2663 if ((known
&& (smin_val
!= smax_val
|| umin_val
!= umax_val
)) ||
2664 smin_val
> smax_val
|| umin_val
> umax_val
) {
2665 /* Taint dst register if offset had invalid bounds derived from
2666 * e.g. dead branches.
2668 __mark_reg_unknown(dst_reg
);
2672 if (BPF_CLASS(insn
->code
) != BPF_ALU64
) {
2673 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
2675 "R%d 32-bit pointer arithmetic prohibited\n",
2680 if (ptr_reg
->type
== PTR_TO_MAP_VALUE_OR_NULL
) {
2681 verbose(env
, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n",
2685 if (ptr_reg
->type
== CONST_PTR_TO_MAP
) {
2686 verbose(env
, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n",
2690 if (ptr_reg
->type
== PTR_TO_PACKET_END
) {
2691 verbose(env
, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n",
2696 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
2697 * The id may be overwritten later if we create a new variable offset.
2699 dst_reg
->type
= ptr_reg
->type
;
2700 dst_reg
->id
= ptr_reg
->id
;
2702 if (!check_reg_sane_offset(env
, off_reg
, ptr_reg
->type
) ||
2703 !check_reg_sane_offset(env
, ptr_reg
, ptr_reg
->type
))
2708 /* We can take a fixed offset as long as it doesn't overflow
2709 * the s32 'off' field
2711 if (known
&& (ptr_reg
->off
+ smin_val
==
2712 (s64
)(s32
)(ptr_reg
->off
+ smin_val
))) {
2713 /* pointer += K. Accumulate it into fixed offset */
2714 dst_reg
->smin_value
= smin_ptr
;
2715 dst_reg
->smax_value
= smax_ptr
;
2716 dst_reg
->umin_value
= umin_ptr
;
2717 dst_reg
->umax_value
= umax_ptr
;
2718 dst_reg
->var_off
= ptr_reg
->var_off
;
2719 dst_reg
->off
= ptr_reg
->off
+ smin_val
;
2720 dst_reg
->range
= ptr_reg
->range
;
2723 /* A new variable offset is created. Note that off_reg->off
2724 * == 0, since it's a scalar.
2725 * dst_reg gets the pointer type and since some positive
2726 * integer value was added to the pointer, give it a new 'id'
2727 * if it's a PTR_TO_PACKET.
2728 * this creates a new 'base' pointer, off_reg (variable) gets
2729 * added into the variable offset, and we copy the fixed offset
2732 if (signed_add_overflows(smin_ptr
, smin_val
) ||
2733 signed_add_overflows(smax_ptr
, smax_val
)) {
2734 dst_reg
->smin_value
= S64_MIN
;
2735 dst_reg
->smax_value
= S64_MAX
;
2737 dst_reg
->smin_value
= smin_ptr
+ smin_val
;
2738 dst_reg
->smax_value
= smax_ptr
+ smax_val
;
2740 if (umin_ptr
+ umin_val
< umin_ptr
||
2741 umax_ptr
+ umax_val
< umax_ptr
) {
2742 dst_reg
->umin_value
= 0;
2743 dst_reg
->umax_value
= U64_MAX
;
2745 dst_reg
->umin_value
= umin_ptr
+ umin_val
;
2746 dst_reg
->umax_value
= umax_ptr
+ umax_val
;
2748 dst_reg
->var_off
= tnum_add(ptr_reg
->var_off
, off_reg
->var_off
);
2749 dst_reg
->off
= ptr_reg
->off
;
2750 if (reg_is_pkt_pointer(ptr_reg
)) {
2751 dst_reg
->id
= ++env
->id_gen
;
2752 /* something was added to pkt_ptr, set range to zero */
2757 if (dst_reg
== off_reg
) {
2758 /* scalar -= pointer. Creates an unknown scalar */
2759 verbose(env
, "R%d tried to subtract pointer from scalar\n",
2763 /* We don't allow subtraction from FP, because (according to
2764 * test_verifier.c test "invalid fp arithmetic", JITs might not
2765 * be able to deal with it.
2767 if (ptr_reg
->type
== PTR_TO_STACK
) {
2768 verbose(env
, "R%d subtraction from stack pointer prohibited\n",
2772 if (known
&& (ptr_reg
->off
- smin_val
==
2773 (s64
)(s32
)(ptr_reg
->off
- smin_val
))) {
2774 /* pointer -= K. Subtract it from fixed offset */
2775 dst_reg
->smin_value
= smin_ptr
;
2776 dst_reg
->smax_value
= smax_ptr
;
2777 dst_reg
->umin_value
= umin_ptr
;
2778 dst_reg
->umax_value
= umax_ptr
;
2779 dst_reg
->var_off
= ptr_reg
->var_off
;
2780 dst_reg
->id
= ptr_reg
->id
;
2781 dst_reg
->off
= ptr_reg
->off
- smin_val
;
2782 dst_reg
->range
= ptr_reg
->range
;
2785 /* A new variable offset is created. If the subtrahend is known
2786 * nonnegative, then any reg->range we had before is still good.
2788 if (signed_sub_overflows(smin_ptr
, smax_val
) ||
2789 signed_sub_overflows(smax_ptr
, smin_val
)) {
2790 /* Overflow possible, we know nothing */
2791 dst_reg
->smin_value
= S64_MIN
;
2792 dst_reg
->smax_value
= S64_MAX
;
2794 dst_reg
->smin_value
= smin_ptr
- smax_val
;
2795 dst_reg
->smax_value
= smax_ptr
- smin_val
;
2797 if (umin_ptr
< umax_val
) {
2798 /* Overflow possible, we know nothing */
2799 dst_reg
->umin_value
= 0;
2800 dst_reg
->umax_value
= U64_MAX
;
2802 /* Cannot overflow (as long as bounds are consistent) */
2803 dst_reg
->umin_value
= umin_ptr
- umax_val
;
2804 dst_reg
->umax_value
= umax_ptr
- umin_val
;
2806 dst_reg
->var_off
= tnum_sub(ptr_reg
->var_off
, off_reg
->var_off
);
2807 dst_reg
->off
= ptr_reg
->off
;
2808 if (reg_is_pkt_pointer(ptr_reg
)) {
2809 dst_reg
->id
= ++env
->id_gen
;
2810 /* something was added to pkt_ptr, set range to zero */
2818 /* bitwise ops on pointers are troublesome, prohibit. */
2819 verbose(env
, "R%d bitwise operator %s on pointer prohibited\n",
2820 dst
, bpf_alu_string
[opcode
>> 4]);
2823 /* other operators (e.g. MUL,LSH) produce non-pointer results */
2824 verbose(env
, "R%d pointer arithmetic with %s operator prohibited\n",
2825 dst
, bpf_alu_string
[opcode
>> 4]);
2829 if (!check_reg_sane_offset(env
, dst_reg
, ptr_reg
->type
))
2832 __update_reg_bounds(dst_reg
);
2833 __reg_deduce_bounds(dst_reg
);
2834 __reg_bound_offset(dst_reg
);
2838 /* WARNING: This function does calculations on 64-bit values, but the actual
2839 * execution may occur on 32-bit values. Therefore, things like bitshifts
2840 * need extra checks in the 32-bit case.
2842 static int adjust_scalar_min_max_vals(struct bpf_verifier_env
*env
,
2843 struct bpf_insn
*insn
,
2844 struct bpf_reg_state
*dst_reg
,
2845 struct bpf_reg_state src_reg
)
2847 struct bpf_reg_state
*regs
= cur_regs(env
);
2848 u8 opcode
= BPF_OP(insn
->code
);
2849 bool src_known
, dst_known
;
2850 s64 smin_val
, smax_val
;
2851 u64 umin_val
, umax_val
;
2852 u64 insn_bitness
= (BPF_CLASS(insn
->code
) == BPF_ALU64
) ? 64 : 32;
2854 smin_val
= src_reg
.smin_value
;
2855 smax_val
= src_reg
.smax_value
;
2856 umin_val
= src_reg
.umin_value
;
2857 umax_val
= src_reg
.umax_value
;
2858 src_known
= tnum_is_const(src_reg
.var_off
);
2859 dst_known
= tnum_is_const(dst_reg
->var_off
);
2861 if ((src_known
&& (smin_val
!= smax_val
|| umin_val
!= umax_val
)) ||
2862 smin_val
> smax_val
|| umin_val
> umax_val
) {
2863 /* Taint dst register if offset had invalid bounds derived from
2864 * e.g. dead branches.
2866 __mark_reg_unknown(dst_reg
);
2871 opcode
!= BPF_ADD
&& opcode
!= BPF_SUB
&& opcode
!= BPF_AND
) {
2872 __mark_reg_unknown(dst_reg
);
2878 if (signed_add_overflows(dst_reg
->smin_value
, smin_val
) ||
2879 signed_add_overflows(dst_reg
->smax_value
, smax_val
)) {
2880 dst_reg
->smin_value
= S64_MIN
;
2881 dst_reg
->smax_value
= S64_MAX
;
2883 dst_reg
->smin_value
+= smin_val
;
2884 dst_reg
->smax_value
+= smax_val
;
2886 if (dst_reg
->umin_value
+ umin_val
< umin_val
||
2887 dst_reg
->umax_value
+ umax_val
< umax_val
) {
2888 dst_reg
->umin_value
= 0;
2889 dst_reg
->umax_value
= U64_MAX
;
2891 dst_reg
->umin_value
+= umin_val
;
2892 dst_reg
->umax_value
+= umax_val
;
2894 dst_reg
->var_off
= tnum_add(dst_reg
->var_off
, src_reg
.var_off
);
2897 if (signed_sub_overflows(dst_reg
->smin_value
, smax_val
) ||
2898 signed_sub_overflows(dst_reg
->smax_value
, smin_val
)) {
2899 /* Overflow possible, we know nothing */
2900 dst_reg
->smin_value
= S64_MIN
;
2901 dst_reg
->smax_value
= S64_MAX
;
2903 dst_reg
->smin_value
-= smax_val
;
2904 dst_reg
->smax_value
-= smin_val
;
2906 if (dst_reg
->umin_value
< umax_val
) {
2907 /* Overflow possible, we know nothing */
2908 dst_reg
->umin_value
= 0;
2909 dst_reg
->umax_value
= U64_MAX
;
2911 /* Cannot overflow (as long as bounds are consistent) */
2912 dst_reg
->umin_value
-= umax_val
;
2913 dst_reg
->umax_value
-= umin_val
;
2915 dst_reg
->var_off
= tnum_sub(dst_reg
->var_off
, src_reg
.var_off
);
2918 dst_reg
->var_off
= tnum_mul(dst_reg
->var_off
, src_reg
.var_off
);
2919 if (smin_val
< 0 || dst_reg
->smin_value
< 0) {
2920 /* Ain't nobody got time to multiply that sign */
2921 __mark_reg_unbounded(dst_reg
);
2922 __update_reg_bounds(dst_reg
);
2925 /* Both values are positive, so we can work with unsigned and
2926 * copy the result to signed (unless it exceeds S64_MAX).
2928 if (umax_val
> U32_MAX
|| dst_reg
->umax_value
> U32_MAX
) {
2929 /* Potential overflow, we know nothing */
2930 __mark_reg_unbounded(dst_reg
);
2931 /* (except what we can learn from the var_off) */
2932 __update_reg_bounds(dst_reg
);
2935 dst_reg
->umin_value
*= umin_val
;
2936 dst_reg
->umax_value
*= umax_val
;
2937 if (dst_reg
->umax_value
> S64_MAX
) {
2938 /* Overflow possible, we know nothing */
2939 dst_reg
->smin_value
= S64_MIN
;
2940 dst_reg
->smax_value
= S64_MAX
;
2942 dst_reg
->smin_value
= dst_reg
->umin_value
;
2943 dst_reg
->smax_value
= dst_reg
->umax_value
;
2947 if (src_known
&& dst_known
) {
2948 __mark_reg_known(dst_reg
, dst_reg
->var_off
.value
&
2949 src_reg
.var_off
.value
);
2952 /* We get our minimum from the var_off, since that's inherently
2953 * bitwise. Our maximum is the minimum of the operands' maxima.
2955 dst_reg
->var_off
= tnum_and(dst_reg
->var_off
, src_reg
.var_off
);
2956 dst_reg
->umin_value
= dst_reg
->var_off
.value
;
2957 dst_reg
->umax_value
= min(dst_reg
->umax_value
, umax_val
);
2958 if (dst_reg
->smin_value
< 0 || smin_val
< 0) {
2959 /* Lose signed bounds when ANDing negative numbers,
2960 * ain't nobody got time for that.
2962 dst_reg
->smin_value
= S64_MIN
;
2963 dst_reg
->smax_value
= S64_MAX
;
2965 /* ANDing two positives gives a positive, so safe to
2966 * cast result into s64.
2968 dst_reg
->smin_value
= dst_reg
->umin_value
;
2969 dst_reg
->smax_value
= dst_reg
->umax_value
;
2971 /* We may learn something more from the var_off */
2972 __update_reg_bounds(dst_reg
);
2975 if (src_known
&& dst_known
) {
2976 __mark_reg_known(dst_reg
, dst_reg
->var_off
.value
|
2977 src_reg
.var_off
.value
);
2980 /* We get our maximum from the var_off, and our minimum is the
2981 * maximum of the operands' minima
2983 dst_reg
->var_off
= tnum_or(dst_reg
->var_off
, src_reg
.var_off
);
2984 dst_reg
->umin_value
= max(dst_reg
->umin_value
, umin_val
);
2985 dst_reg
->umax_value
= dst_reg
->var_off
.value
|
2986 dst_reg
->var_off
.mask
;
2987 if (dst_reg
->smin_value
< 0 || smin_val
< 0) {
2988 /* Lose signed bounds when ORing negative numbers,
2989 * ain't nobody got time for that.
2991 dst_reg
->smin_value
= S64_MIN
;
2992 dst_reg
->smax_value
= S64_MAX
;
2994 /* ORing two positives gives a positive, so safe to
2995 * cast result into s64.
2997 dst_reg
->smin_value
= dst_reg
->umin_value
;
2998 dst_reg
->smax_value
= dst_reg
->umax_value
;
3000 /* We may learn something more from the var_off */
3001 __update_reg_bounds(dst_reg
);
3004 if (umax_val
>= insn_bitness
) {
3005 /* Shifts greater than 31 or 63 are undefined.
3006 * This includes shifts by a negative number.
3008 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
3011 /* We lose all sign bit information (except what we can pick
3014 dst_reg
->smin_value
= S64_MIN
;
3015 dst_reg
->smax_value
= S64_MAX
;
3016 /* If we might shift our top bit out, then we know nothing */
3017 if (dst_reg
->umax_value
> 1ULL << (63 - umax_val
)) {
3018 dst_reg
->umin_value
= 0;
3019 dst_reg
->umax_value
= U64_MAX
;
3021 dst_reg
->umin_value
<<= umin_val
;
3022 dst_reg
->umax_value
<<= umax_val
;
3024 dst_reg
->var_off
= tnum_lshift(dst_reg
->var_off
, umin_val
);
3025 /* We may learn something more from the var_off */
3026 __update_reg_bounds(dst_reg
);
3029 if (umax_val
>= insn_bitness
) {
3030 /* Shifts greater than 31 or 63 are undefined.
3031 * This includes shifts by a negative number.
3033 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
3036 /* BPF_RSH is an unsigned shift. If the value in dst_reg might
3037 * be negative, then either:
3038 * 1) src_reg might be zero, so the sign bit of the result is
3039 * unknown, so we lose our signed bounds
3040 * 2) it's known negative, thus the unsigned bounds capture the
3042 * 3) the signed bounds cross zero, so they tell us nothing
3044 * If the value in dst_reg is known nonnegative, then again the
3045 * unsigned bounts capture the signed bounds.
3046 * Thus, in all cases it suffices to blow away our signed bounds
3047 * and rely on inferring new ones from the unsigned bounds and
3048 * var_off of the result.
3050 dst_reg
->smin_value
= S64_MIN
;
3051 dst_reg
->smax_value
= S64_MAX
;
3052 dst_reg
->var_off
= tnum_rshift(dst_reg
->var_off
, umin_val
);
3053 dst_reg
->umin_value
>>= umax_val
;
3054 dst_reg
->umax_value
>>= umin_val
;
3055 /* We may learn something more from the var_off */
3056 __update_reg_bounds(dst_reg
);
3059 if (umax_val
>= insn_bitness
) {
3060 /* Shifts greater than 31 or 63 are undefined.
3061 * This includes shifts by a negative number.
3063 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
3067 /* Upon reaching here, src_known is true and
3068 * umax_val is equal to umin_val.
3070 dst_reg
->smin_value
>>= umin_val
;
3071 dst_reg
->smax_value
>>= umin_val
;
3072 dst_reg
->var_off
= tnum_arshift(dst_reg
->var_off
, umin_val
);
3074 /* blow away the dst_reg umin_value/umax_value and rely on
3075 * dst_reg var_off to refine the result.
3077 dst_reg
->umin_value
= 0;
3078 dst_reg
->umax_value
= U64_MAX
;
3079 __update_reg_bounds(dst_reg
);
3082 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
3086 if (BPF_CLASS(insn
->code
) != BPF_ALU64
) {
3087 /* 32-bit ALU ops are (32,32)->32 */
3088 coerce_reg_to_size(dst_reg
, 4);
3089 coerce_reg_to_size(&src_reg
, 4);
3092 __reg_deduce_bounds(dst_reg
);
3093 __reg_bound_offset(dst_reg
);
3097 /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
3100 static int adjust_reg_min_max_vals(struct bpf_verifier_env
*env
,
3101 struct bpf_insn
*insn
)
3103 struct bpf_verifier_state
*vstate
= env
->cur_state
;
3104 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
3105 struct bpf_reg_state
*regs
= state
->regs
, *dst_reg
, *src_reg
;
3106 struct bpf_reg_state
*ptr_reg
= NULL
, off_reg
= {0};
3107 u8 opcode
= BPF_OP(insn
->code
);
3109 dst_reg
= ®s
[insn
->dst_reg
];
3111 if (dst_reg
->type
!= SCALAR_VALUE
)
3113 if (BPF_SRC(insn
->code
) == BPF_X
) {
3114 src_reg
= ®s
[insn
->src_reg
];
3115 if (src_reg
->type
!= SCALAR_VALUE
) {
3116 if (dst_reg
->type
!= SCALAR_VALUE
) {
3117 /* Combining two pointers by any ALU op yields
3118 * an arbitrary scalar. Disallow all math except
3119 * pointer subtraction
3121 if (opcode
== BPF_SUB
){
3122 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
3125 verbose(env
, "R%d pointer %s pointer prohibited\n",
3127 bpf_alu_string
[opcode
>> 4]);
3130 /* scalar += pointer
3131 * This is legal, but we have to reverse our
3132 * src/dest handling in computing the range
3134 return adjust_ptr_min_max_vals(env
, insn
,
3137 } else if (ptr_reg
) {
3138 /* pointer += scalar */
3139 return adjust_ptr_min_max_vals(env
, insn
,
3143 /* Pretend the src is a reg with a known value, since we only
3144 * need to be able to read from this state.
3146 off_reg
.type
= SCALAR_VALUE
;
3147 __mark_reg_known(&off_reg
, insn
->imm
);
3149 if (ptr_reg
) /* pointer += K */
3150 return adjust_ptr_min_max_vals(env
, insn
,
3154 /* Got here implies adding two SCALAR_VALUEs */
3155 if (WARN_ON_ONCE(ptr_reg
)) {
3156 print_verifier_state(env
, state
);
3157 verbose(env
, "verifier internal error: unexpected ptr_reg\n");
3160 if (WARN_ON(!src_reg
)) {
3161 print_verifier_state(env
, state
);
3162 verbose(env
, "verifier internal error: no src_reg\n");
3165 return adjust_scalar_min_max_vals(env
, insn
, dst_reg
, *src_reg
);
3168 /* check validity of 32-bit and 64-bit arithmetic operations */
3169 static int check_alu_op(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
3171 struct bpf_reg_state
*regs
= cur_regs(env
);
3172 u8 opcode
= BPF_OP(insn
->code
);
3175 if (opcode
== BPF_END
|| opcode
== BPF_NEG
) {
3176 if (opcode
== BPF_NEG
) {
3177 if (BPF_SRC(insn
->code
) != 0 ||
3178 insn
->src_reg
!= BPF_REG_0
||
3179 insn
->off
!= 0 || insn
->imm
!= 0) {
3180 verbose(env
, "BPF_NEG uses reserved fields\n");
3184 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0 ||
3185 (insn
->imm
!= 16 && insn
->imm
!= 32 && insn
->imm
!= 64) ||
3186 BPF_CLASS(insn
->code
) == BPF_ALU64
) {
3187 verbose(env
, "BPF_END uses reserved fields\n");
3192 /* check src operand */
3193 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
3197 if (is_pointer_value(env
, insn
->dst_reg
)) {
3198 verbose(env
, "R%d pointer arithmetic prohibited\n",
3203 /* check dest operand */
3204 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP
);
3208 } else if (opcode
== BPF_MOV
) {
3210 if (BPF_SRC(insn
->code
) == BPF_X
) {
3211 if (insn
->imm
!= 0 || insn
->off
!= 0) {
3212 verbose(env
, "BPF_MOV uses reserved fields\n");
3216 /* check src operand */
3217 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
3221 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0) {
3222 verbose(env
, "BPF_MOV uses reserved fields\n");
3227 /* check dest operand */
3228 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP
);
3232 if (BPF_SRC(insn
->code
) == BPF_X
) {
3233 if (BPF_CLASS(insn
->code
) == BPF_ALU64
) {
3235 * copy register state to dest reg
3237 regs
[insn
->dst_reg
] = regs
[insn
->src_reg
];
3238 regs
[insn
->dst_reg
].live
|= REG_LIVE_WRITTEN
;
3241 if (is_pointer_value(env
, insn
->src_reg
)) {
3243 "R%d partial copy of pointer\n",
3247 mark_reg_unknown(env
, regs
, insn
->dst_reg
);
3248 coerce_reg_to_size(®s
[insn
->dst_reg
], 4);
3252 * remember the value we stored into this reg
3254 regs
[insn
->dst_reg
].type
= SCALAR_VALUE
;
3255 if (BPF_CLASS(insn
->code
) == BPF_ALU64
) {
3256 __mark_reg_known(regs
+ insn
->dst_reg
,
3259 __mark_reg_known(regs
+ insn
->dst_reg
,
3264 } else if (opcode
> BPF_END
) {
3265 verbose(env
, "invalid BPF_ALU opcode %x\n", opcode
);
3268 } else { /* all other ALU ops: and, sub, xor, add, ... */
3270 if (BPF_SRC(insn
->code
) == BPF_X
) {
3271 if (insn
->imm
!= 0 || insn
->off
!= 0) {
3272 verbose(env
, "BPF_ALU uses reserved fields\n");
3275 /* check src1 operand */
3276 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
3280 if (insn
->src_reg
!= BPF_REG_0
|| insn
->off
!= 0) {
3281 verbose(env
, "BPF_ALU uses reserved fields\n");
3286 /* check src2 operand */
3287 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
3291 if ((opcode
== BPF_MOD
|| opcode
== BPF_DIV
) &&
3292 BPF_SRC(insn
->code
) == BPF_K
&& insn
->imm
== 0) {
3293 verbose(env
, "div by zero\n");
3297 if (opcode
== BPF_ARSH
&& BPF_CLASS(insn
->code
) != BPF_ALU64
) {
3298 verbose(env
, "BPF_ARSH not supported for 32 bit ALU\n");
3302 if ((opcode
== BPF_LSH
|| opcode
== BPF_RSH
||
3303 opcode
== BPF_ARSH
) && BPF_SRC(insn
->code
) == BPF_K
) {
3304 int size
= BPF_CLASS(insn
->code
) == BPF_ALU64
? 64 : 32;
3306 if (insn
->imm
< 0 || insn
->imm
>= size
) {
3307 verbose(env
, "invalid shift %d\n", insn
->imm
);
3312 /* check dest operand */
3313 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP_NO_MARK
);
3317 return adjust_reg_min_max_vals(env
, insn
);
3323 static void find_good_pkt_pointers(struct bpf_verifier_state
*vstate
,
3324 struct bpf_reg_state
*dst_reg
,
3325 enum bpf_reg_type type
,
3326 bool range_right_open
)
3328 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
3329 struct bpf_reg_state
*regs
= state
->regs
, *reg
;
3333 if (dst_reg
->off
< 0 ||
3334 (dst_reg
->off
== 0 && range_right_open
))
3335 /* This doesn't give us any range */
3338 if (dst_reg
->umax_value
> MAX_PACKET_OFF
||
3339 dst_reg
->umax_value
+ dst_reg
->off
> MAX_PACKET_OFF
)
3340 /* Risk of overflow. For instance, ptr + (1<<63) may be less
3341 * than pkt_end, but that's because it's also less than pkt.
3345 new_range
= dst_reg
->off
;
3346 if (range_right_open
)
3349 /* Examples for register markings:
3351 * pkt_data in dst register:
3355 * if (r2 > pkt_end) goto <handle exception>
3360 * if (r2 < pkt_end) goto <access okay>
3361 * <handle exception>
3364 * r2 == dst_reg, pkt_end == src_reg
3365 * r2=pkt(id=n,off=8,r=0)
3366 * r3=pkt(id=n,off=0,r=0)
3368 * pkt_data in src register:
3372 * if (pkt_end >= r2) goto <access okay>
3373 * <handle exception>
3377 * if (pkt_end <= r2) goto <handle exception>
3381 * pkt_end == dst_reg, r2 == src_reg
3382 * r2=pkt(id=n,off=8,r=0)
3383 * r3=pkt(id=n,off=0,r=0)
3385 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
3386 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
3387 * and [r3, r3 + 8-1) respectively is safe to access depending on
3391 /* If our ids match, then we must have the same max_value. And we
3392 * don't care about the other reg's fixed offset, since if it's too big
3393 * the range won't allow anything.
3394 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
3396 for (i
= 0; i
< MAX_BPF_REG
; i
++)
3397 if (regs
[i
].type
== type
&& regs
[i
].id
== dst_reg
->id
)
3398 /* keep the maximum range already checked */
3399 regs
[i
].range
= max(regs
[i
].range
, new_range
);
3401 for (j
= 0; j
<= vstate
->curframe
; j
++) {
3402 state
= vstate
->frame
[j
];
3403 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
3404 if (state
->stack
[i
].slot_type
[0] != STACK_SPILL
)
3406 reg
= &state
->stack
[i
].spilled_ptr
;
3407 if (reg
->type
== type
&& reg
->id
== dst_reg
->id
)
3408 reg
->range
= max(reg
->range
, new_range
);
3413 /* Adjusts the register min/max values in the case that the dst_reg is the
3414 * variable register that we are working on, and src_reg is a constant or we're
3415 * simply doing a BPF_K check.
3416 * In JEQ/JNE cases we also adjust the var_off values.
3418 static void reg_set_min_max(struct bpf_reg_state
*true_reg
,
3419 struct bpf_reg_state
*false_reg
, u64 val
,
3422 /* If the dst_reg is a pointer, we can't learn anything about its
3423 * variable offset from the compare (unless src_reg were a pointer into
3424 * the same object, but we don't bother with that.
3425 * Since false_reg and true_reg have the same type by construction, we
3426 * only need to check one of them for pointerness.
3428 if (__is_pointer_value(false, false_reg
))
3433 /* If this is false then we know nothing Jon Snow, but if it is
3434 * true then we know for sure.
3436 __mark_reg_known(true_reg
, val
);
3439 /* If this is true we know nothing Jon Snow, but if it is false
3440 * we know the value for sure;
3442 __mark_reg_known(false_reg
, val
);
3445 false_reg
->umax_value
= min(false_reg
->umax_value
, val
);
3446 true_reg
->umin_value
= max(true_reg
->umin_value
, val
+ 1);
3449 false_reg
->smax_value
= min_t(s64
, false_reg
->smax_value
, val
);
3450 true_reg
->smin_value
= max_t(s64
, true_reg
->smin_value
, val
+ 1);
3453 false_reg
->umin_value
= max(false_reg
->umin_value
, val
);
3454 true_reg
->umax_value
= min(true_reg
->umax_value
, val
- 1);
3457 false_reg
->smin_value
= max_t(s64
, false_reg
->smin_value
, val
);
3458 true_reg
->smax_value
= min_t(s64
, true_reg
->smax_value
, val
- 1);
3461 false_reg
->umax_value
= min(false_reg
->umax_value
, val
- 1);
3462 true_reg
->umin_value
= max(true_reg
->umin_value
, val
);
3465 false_reg
->smax_value
= min_t(s64
, false_reg
->smax_value
, val
- 1);
3466 true_reg
->smin_value
= max_t(s64
, true_reg
->smin_value
, val
);
3469 false_reg
->umin_value
= max(false_reg
->umin_value
, val
+ 1);
3470 true_reg
->umax_value
= min(true_reg
->umax_value
, val
);
3473 false_reg
->smin_value
= max_t(s64
, false_reg
->smin_value
, val
+ 1);
3474 true_reg
->smax_value
= min_t(s64
, true_reg
->smax_value
, val
);
3480 __reg_deduce_bounds(false_reg
);
3481 __reg_deduce_bounds(true_reg
);
3482 /* We might have learned some bits from the bounds. */
3483 __reg_bound_offset(false_reg
);
3484 __reg_bound_offset(true_reg
);
3485 /* Intersecting with the old var_off might have improved our bounds
3486 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3487 * then new var_off is (0; 0x7f...fc) which improves our umax.
3489 __update_reg_bounds(false_reg
);
3490 __update_reg_bounds(true_reg
);
3493 /* Same as above, but for the case that dst_reg holds a constant and src_reg is
3496 static void reg_set_min_max_inv(struct bpf_reg_state
*true_reg
,
3497 struct bpf_reg_state
*false_reg
, u64 val
,
3500 if (__is_pointer_value(false, false_reg
))
3505 /* If this is false then we know nothing Jon Snow, but if it is
3506 * true then we know for sure.
3508 __mark_reg_known(true_reg
, val
);
3511 /* If this is true we know nothing Jon Snow, but if it is false
3512 * we know the value for sure;
3514 __mark_reg_known(false_reg
, val
);
3517 true_reg
->umax_value
= min(true_reg
->umax_value
, val
- 1);
3518 false_reg
->umin_value
= max(false_reg
->umin_value
, val
);
3521 true_reg
->smax_value
= min_t(s64
, true_reg
->smax_value
, val
- 1);
3522 false_reg
->smin_value
= max_t(s64
, false_reg
->smin_value
, val
);
3525 true_reg
->umin_value
= max(true_reg
->umin_value
, val
+ 1);
3526 false_reg
->umax_value
= min(false_reg
->umax_value
, val
);
3529 true_reg
->smin_value
= max_t(s64
, true_reg
->smin_value
, val
+ 1);
3530 false_reg
->smax_value
= min_t(s64
, false_reg
->smax_value
, val
);
3533 true_reg
->umax_value
= min(true_reg
->umax_value
, val
);
3534 false_reg
->umin_value
= max(false_reg
->umin_value
, val
+ 1);
3537 true_reg
->smax_value
= min_t(s64
, true_reg
->smax_value
, val
);
3538 false_reg
->smin_value
= max_t(s64
, false_reg
->smin_value
, val
+ 1);
3541 true_reg
->umin_value
= max(true_reg
->umin_value
, val
);
3542 false_reg
->umax_value
= min(false_reg
->umax_value
, val
- 1);
3545 true_reg
->smin_value
= max_t(s64
, true_reg
->smin_value
, val
);
3546 false_reg
->smax_value
= min_t(s64
, false_reg
->smax_value
, val
- 1);
3552 __reg_deduce_bounds(false_reg
);
3553 __reg_deduce_bounds(true_reg
);
3554 /* We might have learned some bits from the bounds. */
3555 __reg_bound_offset(false_reg
);
3556 __reg_bound_offset(true_reg
);
3557 /* Intersecting with the old var_off might have improved our bounds
3558 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3559 * then new var_off is (0; 0x7f...fc) which improves our umax.
3561 __update_reg_bounds(false_reg
);
3562 __update_reg_bounds(true_reg
);
3565 /* Regs are known to be equal, so intersect their min/max/var_off */
3566 static void __reg_combine_min_max(struct bpf_reg_state
*src_reg
,
3567 struct bpf_reg_state
*dst_reg
)
3569 src_reg
->umin_value
= dst_reg
->umin_value
= max(src_reg
->umin_value
,
3570 dst_reg
->umin_value
);
3571 src_reg
->umax_value
= dst_reg
->umax_value
= min(src_reg
->umax_value
,
3572 dst_reg
->umax_value
);
3573 src_reg
->smin_value
= dst_reg
->smin_value
= max(src_reg
->smin_value
,
3574 dst_reg
->smin_value
);
3575 src_reg
->smax_value
= dst_reg
->smax_value
= min(src_reg
->smax_value
,
3576 dst_reg
->smax_value
);
3577 src_reg
->var_off
= dst_reg
->var_off
= tnum_intersect(src_reg
->var_off
,
3579 /* We might have learned new bounds from the var_off. */
3580 __update_reg_bounds(src_reg
);
3581 __update_reg_bounds(dst_reg
);
3582 /* We might have learned something about the sign bit. */
3583 __reg_deduce_bounds(src_reg
);
3584 __reg_deduce_bounds(dst_reg
);
3585 /* We might have learned some bits from the bounds. */
3586 __reg_bound_offset(src_reg
);
3587 __reg_bound_offset(dst_reg
);
3588 /* Intersecting with the old var_off might have improved our bounds
3589 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
3590 * then new var_off is (0; 0x7f...fc) which improves our umax.
3592 __update_reg_bounds(src_reg
);
3593 __update_reg_bounds(dst_reg
);
3596 static void reg_combine_min_max(struct bpf_reg_state
*true_src
,
3597 struct bpf_reg_state
*true_dst
,
3598 struct bpf_reg_state
*false_src
,
3599 struct bpf_reg_state
*false_dst
,
3604 __reg_combine_min_max(true_src
, true_dst
);
3607 __reg_combine_min_max(false_src
, false_dst
);
3612 static void mark_map_reg(struct bpf_reg_state
*regs
, u32 regno
, u32 id
,
3615 struct bpf_reg_state
*reg
= ®s
[regno
];
3617 if (reg
->type
== PTR_TO_MAP_VALUE_OR_NULL
&& reg
->id
== id
) {
3618 /* Old offset (both fixed and variable parts) should
3619 * have been known-zero, because we don't allow pointer
3620 * arithmetic on pointers that might be NULL.
3622 if (WARN_ON_ONCE(reg
->smin_value
|| reg
->smax_value
||
3623 !tnum_equals_const(reg
->var_off
, 0) ||
3625 __mark_reg_known_zero(reg
);
3629 reg
->type
= SCALAR_VALUE
;
3630 } else if (reg
->map_ptr
->inner_map_meta
) {
3631 reg
->type
= CONST_PTR_TO_MAP
;
3632 reg
->map_ptr
= reg
->map_ptr
->inner_map_meta
;
3634 reg
->type
= PTR_TO_MAP_VALUE
;
3636 /* We don't need id from this point onwards anymore, thus we
3637 * should better reset it, so that state pruning has chances
3644 /* The logic is similar to find_good_pkt_pointers(), both could eventually
3645 * be folded together at some point.
3647 static void mark_map_regs(struct bpf_verifier_state
*vstate
, u32 regno
,
3650 struct bpf_func_state
*state
= vstate
->frame
[vstate
->curframe
];
3651 struct bpf_reg_state
*regs
= state
->regs
;
3652 u32 id
= regs
[regno
].id
;
3655 for (i
= 0; i
< MAX_BPF_REG
; i
++)
3656 mark_map_reg(regs
, i
, id
, is_null
);
3658 for (j
= 0; j
<= vstate
->curframe
; j
++) {
3659 state
= vstate
->frame
[j
];
3660 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
3661 if (state
->stack
[i
].slot_type
[0] != STACK_SPILL
)
3663 mark_map_reg(&state
->stack
[i
].spilled_ptr
, 0, id
, is_null
);
3668 static bool try_match_pkt_pointers(const struct bpf_insn
*insn
,
3669 struct bpf_reg_state
*dst_reg
,
3670 struct bpf_reg_state
*src_reg
,
3671 struct bpf_verifier_state
*this_branch
,
3672 struct bpf_verifier_state
*other_branch
)
3674 if (BPF_SRC(insn
->code
) != BPF_X
)
3677 switch (BPF_OP(insn
->code
)) {
3679 if ((dst_reg
->type
== PTR_TO_PACKET
&&
3680 src_reg
->type
== PTR_TO_PACKET_END
) ||
3681 (dst_reg
->type
== PTR_TO_PACKET_META
&&
3682 reg_is_init_pkt_pointer(src_reg
, PTR_TO_PACKET
))) {
3683 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
3684 find_good_pkt_pointers(this_branch
, dst_reg
,
3685 dst_reg
->type
, false);
3686 } else if ((dst_reg
->type
== PTR_TO_PACKET_END
&&
3687 src_reg
->type
== PTR_TO_PACKET
) ||
3688 (reg_is_init_pkt_pointer(dst_reg
, PTR_TO_PACKET
) &&
3689 src_reg
->type
== PTR_TO_PACKET_META
)) {
3690 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
3691 find_good_pkt_pointers(other_branch
, src_reg
,
3692 src_reg
->type
, true);
3698 if ((dst_reg
->type
== PTR_TO_PACKET
&&
3699 src_reg
->type
== PTR_TO_PACKET_END
) ||
3700 (dst_reg
->type
== PTR_TO_PACKET_META
&&
3701 reg_is_init_pkt_pointer(src_reg
, PTR_TO_PACKET
))) {
3702 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
3703 find_good_pkt_pointers(other_branch
, dst_reg
,
3704 dst_reg
->type
, true);
3705 } else if ((dst_reg
->type
== PTR_TO_PACKET_END
&&
3706 src_reg
->type
== PTR_TO_PACKET
) ||
3707 (reg_is_init_pkt_pointer(dst_reg
, PTR_TO_PACKET
) &&
3708 src_reg
->type
== PTR_TO_PACKET_META
)) {
3709 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
3710 find_good_pkt_pointers(this_branch
, src_reg
,
3711 src_reg
->type
, false);
3717 if ((dst_reg
->type
== PTR_TO_PACKET
&&
3718 src_reg
->type
== PTR_TO_PACKET_END
) ||
3719 (dst_reg
->type
== PTR_TO_PACKET_META
&&
3720 reg_is_init_pkt_pointer(src_reg
, PTR_TO_PACKET
))) {
3721 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
3722 find_good_pkt_pointers(this_branch
, dst_reg
,
3723 dst_reg
->type
, true);
3724 } else if ((dst_reg
->type
== PTR_TO_PACKET_END
&&
3725 src_reg
->type
== PTR_TO_PACKET
) ||
3726 (reg_is_init_pkt_pointer(dst_reg
, PTR_TO_PACKET
) &&
3727 src_reg
->type
== PTR_TO_PACKET_META
)) {
3728 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
3729 find_good_pkt_pointers(other_branch
, src_reg
,
3730 src_reg
->type
, false);
3736 if ((dst_reg
->type
== PTR_TO_PACKET
&&
3737 src_reg
->type
== PTR_TO_PACKET_END
) ||
3738 (dst_reg
->type
== PTR_TO_PACKET_META
&&
3739 reg_is_init_pkt_pointer(src_reg
, PTR_TO_PACKET
))) {
3740 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
3741 find_good_pkt_pointers(other_branch
, dst_reg
,
3742 dst_reg
->type
, false);
3743 } else if ((dst_reg
->type
== PTR_TO_PACKET_END
&&
3744 src_reg
->type
== PTR_TO_PACKET
) ||
3745 (reg_is_init_pkt_pointer(dst_reg
, PTR_TO_PACKET
) &&
3746 src_reg
->type
== PTR_TO_PACKET_META
)) {
3747 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
3748 find_good_pkt_pointers(this_branch
, src_reg
,
3749 src_reg
->type
, true);
3761 static int check_cond_jmp_op(struct bpf_verifier_env
*env
,
3762 struct bpf_insn
*insn
, int *insn_idx
)
3764 struct bpf_verifier_state
*this_branch
= env
->cur_state
;
3765 struct bpf_verifier_state
*other_branch
;
3766 struct bpf_reg_state
*regs
= this_branch
->frame
[this_branch
->curframe
]->regs
;
3767 struct bpf_reg_state
*dst_reg
, *other_branch_regs
;
3768 u8 opcode
= BPF_OP(insn
->code
);
3771 if (opcode
> BPF_JSLE
) {
3772 verbose(env
, "invalid BPF_JMP opcode %x\n", opcode
);
3776 if (BPF_SRC(insn
->code
) == BPF_X
) {
3777 if (insn
->imm
!= 0) {
3778 verbose(env
, "BPF_JMP uses reserved fields\n");
3782 /* check src1 operand */
3783 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
3787 if (is_pointer_value(env
, insn
->src_reg
)) {
3788 verbose(env
, "R%d pointer comparison prohibited\n",
3793 if (insn
->src_reg
!= BPF_REG_0
) {
3794 verbose(env
, "BPF_JMP uses reserved fields\n");
3799 /* check src2 operand */
3800 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
3804 dst_reg
= ®s
[insn
->dst_reg
];
3806 /* detect if R == 0 where R was initialized to zero earlier */
3807 if (BPF_SRC(insn
->code
) == BPF_K
&&
3808 (opcode
== BPF_JEQ
|| opcode
== BPF_JNE
) &&
3809 dst_reg
->type
== SCALAR_VALUE
&&
3810 tnum_is_const(dst_reg
->var_off
)) {
3811 if ((opcode
== BPF_JEQ
&& dst_reg
->var_off
.value
== insn
->imm
) ||
3812 (opcode
== BPF_JNE
&& dst_reg
->var_off
.value
!= insn
->imm
)) {
3813 /* if (imm == imm) goto pc+off;
3814 * only follow the goto, ignore fall-through
3816 *insn_idx
+= insn
->off
;
3819 /* if (imm != imm) goto pc+off;
3820 * only follow fall-through branch, since
3821 * that's where the program will go
3827 other_branch
= push_stack(env
, *insn_idx
+ insn
->off
+ 1, *insn_idx
);
3830 other_branch_regs
= other_branch
->frame
[other_branch
->curframe
]->regs
;
3832 /* detect if we are comparing against a constant value so we can adjust
3833 * our min/max values for our dst register.
3834 * this is only legit if both are scalars (or pointers to the same
3835 * object, I suppose, but we don't support that right now), because
3836 * otherwise the different base pointers mean the offsets aren't
3839 if (BPF_SRC(insn
->code
) == BPF_X
) {
3840 if (dst_reg
->type
== SCALAR_VALUE
&&
3841 regs
[insn
->src_reg
].type
== SCALAR_VALUE
) {
3842 if (tnum_is_const(regs
[insn
->src_reg
].var_off
))
3843 reg_set_min_max(&other_branch_regs
[insn
->dst_reg
],
3844 dst_reg
, regs
[insn
->src_reg
].var_off
.value
,
3846 else if (tnum_is_const(dst_reg
->var_off
))
3847 reg_set_min_max_inv(&other_branch_regs
[insn
->src_reg
],
3848 ®s
[insn
->src_reg
],
3849 dst_reg
->var_off
.value
, opcode
);
3850 else if (opcode
== BPF_JEQ
|| opcode
== BPF_JNE
)
3851 /* Comparing for equality, we can combine knowledge */
3852 reg_combine_min_max(&other_branch_regs
[insn
->src_reg
],
3853 &other_branch_regs
[insn
->dst_reg
],
3854 ®s
[insn
->src_reg
],
3855 ®s
[insn
->dst_reg
], opcode
);
3857 } else if (dst_reg
->type
== SCALAR_VALUE
) {
3858 reg_set_min_max(&other_branch_regs
[insn
->dst_reg
],
3859 dst_reg
, insn
->imm
, opcode
);
3862 /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */
3863 if (BPF_SRC(insn
->code
) == BPF_K
&&
3864 insn
->imm
== 0 && (opcode
== BPF_JEQ
|| opcode
== BPF_JNE
) &&
3865 dst_reg
->type
== PTR_TO_MAP_VALUE_OR_NULL
) {
3866 /* Mark all identical map registers in each branch as either
3867 * safe or unknown depending R == 0 or R != 0 conditional.
3869 mark_map_regs(this_branch
, insn
->dst_reg
, opcode
== BPF_JNE
);
3870 mark_map_regs(other_branch
, insn
->dst_reg
, opcode
== BPF_JEQ
);
3871 } else if (!try_match_pkt_pointers(insn
, dst_reg
, ®s
[insn
->src_reg
],
3872 this_branch
, other_branch
) &&
3873 is_pointer_value(env
, insn
->dst_reg
)) {
3874 verbose(env
, "R%d pointer comparison prohibited\n",
3879 print_verifier_state(env
, this_branch
->frame
[this_branch
->curframe
]);
3883 /* return the map pointer stored inside BPF_LD_IMM64 instruction */
3884 static struct bpf_map
*ld_imm64_to_map_ptr(struct bpf_insn
*insn
)
3886 u64 imm64
= ((u64
) (u32
) insn
[0].imm
) | ((u64
) (u32
) insn
[1].imm
) << 32;
3888 return (struct bpf_map
*) (unsigned long) imm64
;
3891 /* verify BPF_LD_IMM64 instruction */
3892 static int check_ld_imm(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
3894 struct bpf_reg_state
*regs
= cur_regs(env
);
3897 if (BPF_SIZE(insn
->code
) != BPF_DW
) {
3898 verbose(env
, "invalid BPF_LD_IMM insn\n");
3901 if (insn
->off
!= 0) {
3902 verbose(env
, "BPF_LD_IMM64 uses reserved fields\n");
3906 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP
);
3910 if (insn
->src_reg
== 0) {
3911 u64 imm
= ((u64
)(insn
+ 1)->imm
<< 32) | (u32
)insn
->imm
;
3913 regs
[insn
->dst_reg
].type
= SCALAR_VALUE
;
3914 __mark_reg_known(®s
[insn
->dst_reg
], imm
);
3918 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
3919 BUG_ON(insn
->src_reg
!= BPF_PSEUDO_MAP_FD
);
3921 regs
[insn
->dst_reg
].type
= CONST_PTR_TO_MAP
;
3922 regs
[insn
->dst_reg
].map_ptr
= ld_imm64_to_map_ptr(insn
);
3926 static bool may_access_skb(enum bpf_prog_type type
)
3929 case BPF_PROG_TYPE_SOCKET_FILTER
:
3930 case BPF_PROG_TYPE_SCHED_CLS
:
3931 case BPF_PROG_TYPE_SCHED_ACT
:
3938 /* verify safety of LD_ABS|LD_IND instructions:
3939 * - they can only appear in the programs where ctx == skb
3940 * - since they are wrappers of function calls, they scratch R1-R5 registers,
3941 * preserve R6-R9, and store return value into R0
3944 * ctx == skb == R6 == CTX
3947 * SRC == any register
3948 * IMM == 32-bit immediate
3951 * R0 - 8/16/32-bit skb data converted to cpu endianness
3953 static int check_ld_abs(struct bpf_verifier_env
*env
, struct bpf_insn
*insn
)
3955 struct bpf_reg_state
*regs
= cur_regs(env
);
3956 u8 mode
= BPF_MODE(insn
->code
);
3959 if (!may_access_skb(env
->prog
->type
)) {
3960 verbose(env
, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
3964 if (!env
->ops
->gen_ld_abs
) {
3965 verbose(env
, "bpf verifier is misconfigured\n");
3969 if (env
->subprog_cnt
> 1) {
3970 /* when program has LD_ABS insn JITs and interpreter assume
3971 * that r1 == ctx == skb which is not the case for callees
3972 * that can have arbitrary arguments. It's problematic
3973 * for main prog as well since JITs would need to analyze
3974 * all functions in order to make proper register save/restore
3975 * decisions in the main prog. Hence disallow LD_ABS with calls
3977 verbose(env
, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
3981 if (insn
->dst_reg
!= BPF_REG_0
|| insn
->off
!= 0 ||
3982 BPF_SIZE(insn
->code
) == BPF_DW
||
3983 (mode
== BPF_ABS
&& insn
->src_reg
!= BPF_REG_0
)) {
3984 verbose(env
, "BPF_LD_[ABS|IND] uses reserved fields\n");
3988 /* check whether implicit source operand (register R6) is readable */
3989 err
= check_reg_arg(env
, BPF_REG_6
, SRC_OP
);
3993 if (regs
[BPF_REG_6
].type
!= PTR_TO_CTX
) {
3995 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
3999 if (mode
== BPF_IND
) {
4000 /* check explicit source operand */
4001 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
4006 /* reset caller saved regs to unreadable */
4007 for (i
= 0; i
< CALLER_SAVED_REGS
; i
++) {
4008 mark_reg_not_init(env
, regs
, caller_saved
[i
]);
4009 check_reg_arg(env
, caller_saved
[i
], DST_OP_NO_MARK
);
4012 /* mark destination R0 register as readable, since it contains
4013 * the value fetched from the packet.
4014 * Already marked as written above.
4016 mark_reg_unknown(env
, regs
, BPF_REG_0
);
4020 static int check_return_code(struct bpf_verifier_env
*env
)
4022 struct bpf_reg_state
*reg
;
4023 struct tnum range
= tnum_range(0, 1);
4025 switch (env
->prog
->type
) {
4026 case BPF_PROG_TYPE_CGROUP_SKB
:
4027 case BPF_PROG_TYPE_CGROUP_SOCK
:
4028 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR
:
4029 case BPF_PROG_TYPE_SOCK_OPS
:
4030 case BPF_PROG_TYPE_CGROUP_DEVICE
:
4036 reg
= cur_regs(env
) + BPF_REG_0
;
4037 if (reg
->type
!= SCALAR_VALUE
) {
4038 verbose(env
, "At program exit the register R0 is not a known value (%s)\n",
4039 reg_type_str
[reg
->type
]);
4043 if (!tnum_in(range
, reg
->var_off
)) {
4044 verbose(env
, "At program exit the register R0 ");
4045 if (!tnum_is_unknown(reg
->var_off
)) {
4048 tnum_strn(tn_buf
, sizeof(tn_buf
), reg
->var_off
);
4049 verbose(env
, "has value %s", tn_buf
);
4051 verbose(env
, "has unknown scalar value");
4053 verbose(env
, " should have been 0 or 1\n");
4059 /* non-recursive DFS pseudo code
4060 * 1 procedure DFS-iterative(G,v):
4061 * 2 label v as discovered
4062 * 3 let S be a stack
4064 * 5 while S is not empty
4066 * 7 if t is what we're looking for:
4068 * 9 for all edges e in G.adjacentEdges(t) do
4069 * 10 if edge e is already labelled
4070 * 11 continue with the next edge
4071 * 12 w <- G.adjacentVertex(t,e)
4072 * 13 if vertex w is not discovered and not explored
4073 * 14 label e as tree-edge
4074 * 15 label w as discovered
4077 * 18 else if vertex w is discovered
4078 * 19 label e as back-edge
4080 * 21 // vertex w is explored
4081 * 22 label e as forward- or cross-edge
4082 * 23 label t as explored
4087 * 0x11 - discovered and fall-through edge labelled
4088 * 0x12 - discovered and fall-through and branch edges labelled
4099 #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
4101 static int *insn_stack
; /* stack of insns to process */
4102 static int cur_stack
; /* current stack index */
4103 static int *insn_state
;
4105 /* t, w, e - match pseudo-code above:
4106 * t - index of current instruction
4107 * w - next instruction
4110 static int push_insn(int t
, int w
, int e
, struct bpf_verifier_env
*env
)
4112 if (e
== FALLTHROUGH
&& insn_state
[t
] >= (DISCOVERED
| FALLTHROUGH
))
4115 if (e
== BRANCH
&& insn_state
[t
] >= (DISCOVERED
| BRANCH
))
4118 if (w
< 0 || w
>= env
->prog
->len
) {
4119 verbose(env
, "jump out of range from insn %d to %d\n", t
, w
);
4124 /* mark branch target for state pruning */
4125 env
->explored_states
[w
] = STATE_LIST_MARK
;
4127 if (insn_state
[w
] == 0) {
4129 insn_state
[t
] = DISCOVERED
| e
;
4130 insn_state
[w
] = DISCOVERED
;
4131 if (cur_stack
>= env
->prog
->len
)
4133 insn_stack
[cur_stack
++] = w
;
4135 } else if ((insn_state
[w
] & 0xF0) == DISCOVERED
) {
4136 verbose(env
, "back-edge from insn %d to %d\n", t
, w
);
4138 } else if (insn_state
[w
] == EXPLORED
) {
4139 /* forward- or cross-edge */
4140 insn_state
[t
] = DISCOVERED
| e
;
4142 verbose(env
, "insn state internal bug\n");
4148 /* non-recursive depth-first-search to detect loops in BPF program
4149 * loop == back-edge in directed graph
4151 static int check_cfg(struct bpf_verifier_env
*env
)
4153 struct bpf_insn
*insns
= env
->prog
->insnsi
;
4154 int insn_cnt
= env
->prog
->len
;
4158 ret
= check_subprogs(env
);
4162 insn_state
= kcalloc(insn_cnt
, sizeof(int), GFP_KERNEL
);
4166 insn_stack
= kcalloc(insn_cnt
, sizeof(int), GFP_KERNEL
);
4172 insn_state
[0] = DISCOVERED
; /* mark 1st insn as discovered */
4173 insn_stack
[0] = 0; /* 0 is the first instruction */
4179 t
= insn_stack
[cur_stack
- 1];
4181 if (BPF_CLASS(insns
[t
].code
) == BPF_JMP
) {
4182 u8 opcode
= BPF_OP(insns
[t
].code
);
4184 if (opcode
== BPF_EXIT
) {
4186 } else if (opcode
== BPF_CALL
) {
4187 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
4192 if (t
+ 1 < insn_cnt
)
4193 env
->explored_states
[t
+ 1] = STATE_LIST_MARK
;
4194 if (insns
[t
].src_reg
== BPF_PSEUDO_CALL
) {
4195 env
->explored_states
[t
] = STATE_LIST_MARK
;
4196 ret
= push_insn(t
, t
+ insns
[t
].imm
+ 1, BRANCH
, env
);
4202 } else if (opcode
== BPF_JA
) {
4203 if (BPF_SRC(insns
[t
].code
) != BPF_K
) {
4207 /* unconditional jump with single edge */
4208 ret
= push_insn(t
, t
+ insns
[t
].off
+ 1,
4214 /* tell verifier to check for equivalent states
4215 * after every call and jump
4217 if (t
+ 1 < insn_cnt
)
4218 env
->explored_states
[t
+ 1] = STATE_LIST_MARK
;
4220 /* conditional jump with two edges */
4221 env
->explored_states
[t
] = STATE_LIST_MARK
;
4222 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
4228 ret
= push_insn(t
, t
+ insns
[t
].off
+ 1, BRANCH
, env
);
4235 /* all other non-branch instructions with single
4238 ret
= push_insn(t
, t
+ 1, FALLTHROUGH
, env
);
4246 insn_state
[t
] = EXPLORED
;
4247 if (cur_stack
-- <= 0) {
4248 verbose(env
, "pop stack internal bug\n");
4255 for (i
= 0; i
< insn_cnt
; i
++) {
4256 if (insn_state
[i
] != EXPLORED
) {
4257 verbose(env
, "unreachable insn %d\n", i
);
4262 ret
= 0; /* cfg looks good */
4270 /* check %cur's range satisfies %old's */
4271 static bool range_within(struct bpf_reg_state
*old
,
4272 struct bpf_reg_state
*cur
)
4274 return old
->umin_value
<= cur
->umin_value
&&
4275 old
->umax_value
>= cur
->umax_value
&&
4276 old
->smin_value
<= cur
->smin_value
&&
4277 old
->smax_value
>= cur
->smax_value
;
4280 /* Maximum number of register states that can exist at once */
4281 #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
4287 /* If in the old state two registers had the same id, then they need to have
4288 * the same id in the new state as well. But that id could be different from
4289 * the old state, so we need to track the mapping from old to new ids.
4290 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
4291 * regs with old id 5 must also have new id 9 for the new state to be safe. But
4292 * regs with a different old id could still have new id 9, we don't care about
4294 * So we look through our idmap to see if this old id has been seen before. If
4295 * so, we require the new id to match; otherwise, we add the id pair to the map.
4297 static bool check_ids(u32 old_id
, u32 cur_id
, struct idpair
*idmap
)
4301 for (i
= 0; i
< ID_MAP_SIZE
; i
++) {
4302 if (!idmap
[i
].old
) {
4303 /* Reached an empty slot; haven't seen this id before */
4304 idmap
[i
].old
= old_id
;
4305 idmap
[i
].cur
= cur_id
;
4308 if (idmap
[i
].old
== old_id
)
4309 return idmap
[i
].cur
== cur_id
;
4311 /* We ran out of idmap slots, which should be impossible */
4316 /* Returns true if (rold safe implies rcur safe) */
4317 static bool regsafe(struct bpf_reg_state
*rold
, struct bpf_reg_state
*rcur
,
4318 struct idpair
*idmap
)
4322 if (!(rold
->live
& REG_LIVE_READ
))
4323 /* explored state didn't use this */
4326 equal
= memcmp(rold
, rcur
, offsetof(struct bpf_reg_state
, frameno
)) == 0;
4328 if (rold
->type
== PTR_TO_STACK
)
4329 /* two stack pointers are equal only if they're pointing to
4330 * the same stack frame, since fp-8 in foo != fp-8 in bar
4332 return equal
&& rold
->frameno
== rcur
->frameno
;
4337 if (rold
->type
== NOT_INIT
)
4338 /* explored state can't have used this */
4340 if (rcur
->type
== NOT_INIT
)
4342 switch (rold
->type
) {
4344 if (rcur
->type
== SCALAR_VALUE
) {
4345 /* new val must satisfy old val knowledge */
4346 return range_within(rold
, rcur
) &&
4347 tnum_in(rold
->var_off
, rcur
->var_off
);
4349 /* We're trying to use a pointer in place of a scalar.
4350 * Even if the scalar was unbounded, this could lead to
4351 * pointer leaks because scalars are allowed to leak
4352 * while pointers are not. We could make this safe in
4353 * special cases if root is calling us, but it's
4354 * probably not worth the hassle.
4358 case PTR_TO_MAP_VALUE
:
4359 /* If the new min/max/var_off satisfy the old ones and
4360 * everything else matches, we are OK.
4361 * We don't care about the 'id' value, because nothing
4362 * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL)
4364 return memcmp(rold
, rcur
, offsetof(struct bpf_reg_state
, id
)) == 0 &&
4365 range_within(rold
, rcur
) &&
4366 tnum_in(rold
->var_off
, rcur
->var_off
);
4367 case PTR_TO_MAP_VALUE_OR_NULL
:
4368 /* a PTR_TO_MAP_VALUE could be safe to use as a
4369 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
4370 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
4371 * checked, doing so could have affected others with the same
4372 * id, and we can't check for that because we lost the id when
4373 * we converted to a PTR_TO_MAP_VALUE.
4375 if (rcur
->type
!= PTR_TO_MAP_VALUE_OR_NULL
)
4377 if (memcmp(rold
, rcur
, offsetof(struct bpf_reg_state
, id
)))
4379 /* Check our ids match any regs they're supposed to */
4380 return check_ids(rold
->id
, rcur
->id
, idmap
);
4381 case PTR_TO_PACKET_META
:
4383 if (rcur
->type
!= rold
->type
)
4385 /* We must have at least as much range as the old ptr
4386 * did, so that any accesses which were safe before are
4387 * still safe. This is true even if old range < old off,
4388 * since someone could have accessed through (ptr - k), or
4389 * even done ptr -= k in a register, to get a safe access.
4391 if (rold
->range
> rcur
->range
)
4393 /* If the offsets don't match, we can't trust our alignment;
4394 * nor can we be sure that we won't fall out of range.
4396 if (rold
->off
!= rcur
->off
)
4398 /* id relations must be preserved */
4399 if (rold
->id
&& !check_ids(rold
->id
, rcur
->id
, idmap
))
4401 /* new val must satisfy old val knowledge */
4402 return range_within(rold
, rcur
) &&
4403 tnum_in(rold
->var_off
, rcur
->var_off
);
4405 case CONST_PTR_TO_MAP
:
4406 case PTR_TO_PACKET_END
:
4407 /* Only valid matches are exact, which memcmp() above
4408 * would have accepted
4411 /* Don't know what's going on, just say it's not safe */
4415 /* Shouldn't get here; if we do, say it's not safe */
4420 static bool stacksafe(struct bpf_func_state
*old
,
4421 struct bpf_func_state
*cur
,
4422 struct idpair
*idmap
)
4426 /* if explored stack has more populated slots than current stack
4427 * such stacks are not equivalent
4429 if (old
->allocated_stack
> cur
->allocated_stack
)
4432 /* walk slots of the explored stack and ignore any additional
4433 * slots in the current stack, since explored(safe) state
4436 for (i
= 0; i
< old
->allocated_stack
; i
++) {
4437 spi
= i
/ BPF_REG_SIZE
;
4439 if (!(old
->stack
[spi
].spilled_ptr
.live
& REG_LIVE_READ
))
4440 /* explored state didn't use this */
4443 if (old
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
] == STACK_INVALID
)
4445 /* if old state was safe with misc data in the stack
4446 * it will be safe with zero-initialized stack.
4447 * The opposite is not true
4449 if (old
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
] == STACK_MISC
&&
4450 cur
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
] == STACK_ZERO
)
4452 if (old
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
] !=
4453 cur
->stack
[spi
].slot_type
[i
% BPF_REG_SIZE
])
4454 /* Ex: old explored (safe) state has STACK_SPILL in
4455 * this stack slot, but current has has STACK_MISC ->
4456 * this verifier states are not equivalent,
4457 * return false to continue verification of this path
4460 if (i
% BPF_REG_SIZE
)
4462 if (old
->stack
[spi
].slot_type
[0] != STACK_SPILL
)
4464 if (!regsafe(&old
->stack
[spi
].spilled_ptr
,
4465 &cur
->stack
[spi
].spilled_ptr
,
4467 /* when explored and current stack slot are both storing
4468 * spilled registers, check that stored pointers types
4469 * are the same as well.
4470 * Ex: explored safe path could have stored
4471 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
4472 * but current path has stored:
4473 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
4474 * such verifier states are not equivalent.
4475 * return false to continue verification of this path
4482 /* compare two verifier states
4484 * all states stored in state_list are known to be valid, since
4485 * verifier reached 'bpf_exit' instruction through them
4487 * this function is called when verifier exploring different branches of
4488 * execution popped from the state stack. If it sees an old state that has
4489 * more strict register state and more strict stack state then this execution
4490 * branch doesn't need to be explored further, since verifier already
4491 * concluded that more strict state leads to valid finish.
4493 * Therefore two states are equivalent if register state is more conservative
4494 * and explored stack state is more conservative than the current one.
4497 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
4498 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
4500 * In other words if current stack state (one being explored) has more
4501 * valid slots than old one that already passed validation, it means
4502 * the verifier can stop exploring and conclude that current state is valid too
4504 * Similarly with registers. If explored state has register type as invalid
4505 * whereas register type in current state is meaningful, it means that
4506 * the current state will reach 'bpf_exit' instruction safely
4508 static bool func_states_equal(struct bpf_func_state
*old
,
4509 struct bpf_func_state
*cur
)
4511 struct idpair
*idmap
;
4515 idmap
= kcalloc(ID_MAP_SIZE
, sizeof(struct idpair
), GFP_KERNEL
);
4516 /* If we failed to allocate the idmap, just say it's not safe */
4520 for (i
= 0; i
< MAX_BPF_REG
; i
++) {
4521 if (!regsafe(&old
->regs
[i
], &cur
->regs
[i
], idmap
))
4525 if (!stacksafe(old
, cur
, idmap
))
4533 static bool states_equal(struct bpf_verifier_env
*env
,
4534 struct bpf_verifier_state
*old
,
4535 struct bpf_verifier_state
*cur
)
4539 if (old
->curframe
!= cur
->curframe
)
4542 /* for states to be equal callsites have to be the same
4543 * and all frame states need to be equivalent
4545 for (i
= 0; i
<= old
->curframe
; i
++) {
4546 if (old
->frame
[i
]->callsite
!= cur
->frame
[i
]->callsite
)
4548 if (!func_states_equal(old
->frame
[i
], cur
->frame
[i
]))
4554 /* A write screens off any subsequent reads; but write marks come from the
4555 * straight-line code between a state and its parent. When we arrive at an
4556 * equivalent state (jump target or such) we didn't arrive by the straight-line
4557 * code, so read marks in the state must propagate to the parent regardless
4558 * of the state's write marks. That's what 'parent == state->parent' comparison
4559 * in mark_reg_read() and mark_stack_slot_read() is for.
4561 static int propagate_liveness(struct bpf_verifier_env
*env
,
4562 const struct bpf_verifier_state
*vstate
,
4563 struct bpf_verifier_state
*vparent
)
4565 int i
, frame
, err
= 0;
4566 struct bpf_func_state
*state
, *parent
;
4568 if (vparent
->curframe
!= vstate
->curframe
) {
4569 WARN(1, "propagate_live: parent frame %d current frame %d\n",
4570 vparent
->curframe
, vstate
->curframe
);
4573 /* Propagate read liveness of registers... */
4574 BUILD_BUG_ON(BPF_REG_FP
+ 1 != MAX_BPF_REG
);
4575 /* We don't need to worry about FP liveness because it's read-only */
4576 for (i
= 0; i
< BPF_REG_FP
; i
++) {
4577 if (vparent
->frame
[vparent
->curframe
]->regs
[i
].live
& REG_LIVE_READ
)
4579 if (vstate
->frame
[vstate
->curframe
]->regs
[i
].live
& REG_LIVE_READ
) {
4580 err
= mark_reg_read(env
, vstate
, vparent
, i
);
4586 /* ... and stack slots */
4587 for (frame
= 0; frame
<= vstate
->curframe
; frame
++) {
4588 state
= vstate
->frame
[frame
];
4589 parent
= vparent
->frame
[frame
];
4590 for (i
= 0; i
< state
->allocated_stack
/ BPF_REG_SIZE
&&
4591 i
< parent
->allocated_stack
/ BPF_REG_SIZE
; i
++) {
4592 if (parent
->stack
[i
].spilled_ptr
.live
& REG_LIVE_READ
)
4594 if (state
->stack
[i
].spilled_ptr
.live
& REG_LIVE_READ
)
4595 mark_stack_slot_read(env
, vstate
, vparent
, i
, frame
);
4601 static int is_state_visited(struct bpf_verifier_env
*env
, int insn_idx
)
4603 struct bpf_verifier_state_list
*new_sl
;
4604 struct bpf_verifier_state_list
*sl
;
4605 struct bpf_verifier_state
*cur
= env
->cur_state
;
4608 sl
= env
->explored_states
[insn_idx
];
4610 /* this 'insn_idx' instruction wasn't marked, so we will not
4611 * be doing state search here
4615 while (sl
!= STATE_LIST_MARK
) {
4616 if (states_equal(env
, &sl
->state
, cur
)) {
4617 /* reached equivalent register/stack state,
4619 * Registers read by the continuation are read by us.
4620 * If we have any write marks in env->cur_state, they
4621 * will prevent corresponding reads in the continuation
4622 * from reaching our parent (an explored_state). Our
4623 * own state will get the read marks recorded, but
4624 * they'll be immediately forgotten as we're pruning
4625 * this state and will pop a new one.
4627 err
= propagate_liveness(env
, &sl
->state
, cur
);
4635 /* there were no equivalent states, remember current one.
4636 * technically the current state is not proven to be safe yet,
4637 * but it will either reach outer most bpf_exit (which means it's safe)
4638 * or it will be rejected. Since there are no loops, we won't be
4639 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
4640 * again on the way to bpf_exit
4642 new_sl
= kzalloc(sizeof(struct bpf_verifier_state_list
), GFP_KERNEL
);
4646 /* add new state to the head of linked list */
4647 err
= copy_verifier_state(&new_sl
->state
, cur
);
4649 free_verifier_state(&new_sl
->state
, false);
4653 new_sl
->next
= env
->explored_states
[insn_idx
];
4654 env
->explored_states
[insn_idx
] = new_sl
;
4655 /* connect new state to parentage chain */
4656 cur
->parent
= &new_sl
->state
;
4657 /* clear write marks in current state: the writes we did are not writes
4658 * our child did, so they don't screen off its reads from us.
4659 * (There are no read marks in current state, because reads always mark
4660 * their parent and current state never has children yet. Only
4661 * explored_states can get read marks.)
4663 for (i
= 0; i
< BPF_REG_FP
; i
++)
4664 cur
->frame
[cur
->curframe
]->regs
[i
].live
= REG_LIVE_NONE
;
4666 /* all stack frames are accessible from callee, clear them all */
4667 for (j
= 0; j
<= cur
->curframe
; j
++) {
4668 struct bpf_func_state
*frame
= cur
->frame
[j
];
4670 for (i
= 0; i
< frame
->allocated_stack
/ BPF_REG_SIZE
; i
++)
4671 frame
->stack
[i
].spilled_ptr
.live
= REG_LIVE_NONE
;
4676 static int do_check(struct bpf_verifier_env
*env
)
4678 struct bpf_verifier_state
*state
;
4679 struct bpf_insn
*insns
= env
->prog
->insnsi
;
4680 struct bpf_reg_state
*regs
;
4681 int insn_cnt
= env
->prog
->len
, i
;
4682 int insn_idx
, prev_insn_idx
= 0;
4683 int insn_processed
= 0;
4684 bool do_print_state
= false;
4686 state
= kzalloc(sizeof(struct bpf_verifier_state
), GFP_KERNEL
);
4689 state
->curframe
= 0;
4690 state
->parent
= NULL
;
4691 state
->frame
[0] = kzalloc(sizeof(struct bpf_func_state
), GFP_KERNEL
);
4692 if (!state
->frame
[0]) {
4696 env
->cur_state
= state
;
4697 init_func_state(env
, state
->frame
[0],
4698 BPF_MAIN_FUNC
/* callsite */,
4700 0 /* subprogno, zero == main subprog */);
4703 struct bpf_insn
*insn
;
4707 if (insn_idx
>= insn_cnt
) {
4708 verbose(env
, "invalid insn idx %d insn_cnt %d\n",
4709 insn_idx
, insn_cnt
);
4713 insn
= &insns
[insn_idx
];
4714 class = BPF_CLASS(insn
->code
);
4716 if (++insn_processed
> BPF_COMPLEXITY_LIMIT_INSNS
) {
4718 "BPF program is too large. Processed %d insn\n",
4723 err
= is_state_visited(env
, insn_idx
);
4727 /* found equivalent state, can prune the search */
4728 if (env
->log
.level
) {
4730 verbose(env
, "\nfrom %d to %d: safe\n",
4731 prev_insn_idx
, insn_idx
);
4733 verbose(env
, "%d: safe\n", insn_idx
);
4735 goto process_bpf_exit
;
4741 if (env
->log
.level
> 1 || (env
->log
.level
&& do_print_state
)) {
4742 if (env
->log
.level
> 1)
4743 verbose(env
, "%d:", insn_idx
);
4745 verbose(env
, "\nfrom %d to %d:",
4746 prev_insn_idx
, insn_idx
);
4747 print_verifier_state(env
, state
->frame
[state
->curframe
]);
4748 do_print_state
= false;
4751 if (env
->log
.level
) {
4752 const struct bpf_insn_cbs cbs
= {
4753 .cb_print
= verbose
,
4754 .private_data
= env
,
4757 verbose(env
, "%d: ", insn_idx
);
4758 print_bpf_insn(&cbs
, insn
, env
->allow_ptr_leaks
);
4761 if (bpf_prog_is_dev_bound(env
->prog
->aux
)) {
4762 err
= bpf_prog_offload_verify_insn(env
, insn_idx
,
4768 regs
= cur_regs(env
);
4769 env
->insn_aux_data
[insn_idx
].seen
= true;
4770 if (class == BPF_ALU
|| class == BPF_ALU64
) {
4771 err
= check_alu_op(env
, insn
);
4775 } else if (class == BPF_LDX
) {
4776 enum bpf_reg_type
*prev_src_type
, src_reg_type
;
4778 /* check for reserved fields is already done */
4780 /* check src operand */
4781 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
4785 err
= check_reg_arg(env
, insn
->dst_reg
, DST_OP_NO_MARK
);
4789 src_reg_type
= regs
[insn
->src_reg
].type
;
4791 /* check that memory (src_reg + off) is readable,
4792 * the state of dst_reg will be updated by this func
4794 err
= check_mem_access(env
, insn_idx
, insn
->src_reg
, insn
->off
,
4795 BPF_SIZE(insn
->code
), BPF_READ
,
4796 insn
->dst_reg
, false);
4800 prev_src_type
= &env
->insn_aux_data
[insn_idx
].ptr_type
;
4802 if (*prev_src_type
== NOT_INIT
) {
4804 * dst_reg = *(u32 *)(src_reg + off)
4805 * save type to validate intersecting paths
4807 *prev_src_type
= src_reg_type
;
4809 } else if (src_reg_type
!= *prev_src_type
&&
4810 (src_reg_type
== PTR_TO_CTX
||
4811 *prev_src_type
== PTR_TO_CTX
)) {
4812 /* ABuser program is trying to use the same insn
4813 * dst_reg = *(u32*) (src_reg + off)
4814 * with different pointer types:
4815 * src_reg == ctx in one branch and
4816 * src_reg == stack|map in some other branch.
4819 verbose(env
, "same insn cannot be used with different pointers\n");
4823 } else if (class == BPF_STX
) {
4824 enum bpf_reg_type
*prev_dst_type
, dst_reg_type
;
4826 if (BPF_MODE(insn
->code
) == BPF_XADD
) {
4827 err
= check_xadd(env
, insn_idx
, insn
);
4834 /* check src1 operand */
4835 err
= check_reg_arg(env
, insn
->src_reg
, SRC_OP
);
4838 /* check src2 operand */
4839 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
4843 dst_reg_type
= regs
[insn
->dst_reg
].type
;
4845 /* check that memory (dst_reg + off) is writeable */
4846 err
= check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
4847 BPF_SIZE(insn
->code
), BPF_WRITE
,
4848 insn
->src_reg
, false);
4852 prev_dst_type
= &env
->insn_aux_data
[insn_idx
].ptr_type
;
4854 if (*prev_dst_type
== NOT_INIT
) {
4855 *prev_dst_type
= dst_reg_type
;
4856 } else if (dst_reg_type
!= *prev_dst_type
&&
4857 (dst_reg_type
== PTR_TO_CTX
||
4858 *prev_dst_type
== PTR_TO_CTX
)) {
4859 verbose(env
, "same insn cannot be used with different pointers\n");
4863 } else if (class == BPF_ST
) {
4864 if (BPF_MODE(insn
->code
) != BPF_MEM
||
4865 insn
->src_reg
!= BPF_REG_0
) {
4866 verbose(env
, "BPF_ST uses reserved fields\n");
4869 /* check src operand */
4870 err
= check_reg_arg(env
, insn
->dst_reg
, SRC_OP
);
4874 if (is_ctx_reg(env
, insn
->dst_reg
)) {
4875 verbose(env
, "BPF_ST stores into R%d context is not allowed\n",
4880 /* check that memory (dst_reg + off) is writeable */
4881 err
= check_mem_access(env
, insn_idx
, insn
->dst_reg
, insn
->off
,
4882 BPF_SIZE(insn
->code
), BPF_WRITE
,
4887 } else if (class == BPF_JMP
) {
4888 u8 opcode
= BPF_OP(insn
->code
);
4890 if (opcode
== BPF_CALL
) {
4891 if (BPF_SRC(insn
->code
) != BPF_K
||
4893 (insn
->src_reg
!= BPF_REG_0
&&
4894 insn
->src_reg
!= BPF_PSEUDO_CALL
) ||
4895 insn
->dst_reg
!= BPF_REG_0
) {
4896 verbose(env
, "BPF_CALL uses reserved fields\n");
4900 if (insn
->src_reg
== BPF_PSEUDO_CALL
)
4901 err
= check_func_call(env
, insn
, &insn_idx
);
4903 err
= check_helper_call(env
, insn
->imm
, insn_idx
);
4907 } else if (opcode
== BPF_JA
) {
4908 if (BPF_SRC(insn
->code
) != BPF_K
||
4910 insn
->src_reg
!= BPF_REG_0
||
4911 insn
->dst_reg
!= BPF_REG_0
) {
4912 verbose(env
, "BPF_JA uses reserved fields\n");
4916 insn_idx
+= insn
->off
+ 1;
4919 } else if (opcode
== BPF_EXIT
) {
4920 if (BPF_SRC(insn
->code
) != BPF_K
||
4922 insn
->src_reg
!= BPF_REG_0
||
4923 insn
->dst_reg
!= BPF_REG_0
) {
4924 verbose(env
, "BPF_EXIT uses reserved fields\n");
4928 if (state
->curframe
) {
4929 /* exit from nested function */
4930 prev_insn_idx
= insn_idx
;
4931 err
= prepare_func_exit(env
, &insn_idx
);
4934 do_print_state
= true;
4938 /* eBPF calling convetion is such that R0 is used
4939 * to return the value from eBPF program.
4940 * Make sure that it's readable at this time
4941 * of bpf_exit, which means that program wrote
4942 * something into it earlier
4944 err
= check_reg_arg(env
, BPF_REG_0
, SRC_OP
);
4948 if (is_pointer_value(env
, BPF_REG_0
)) {
4949 verbose(env
, "R0 leaks addr as return value\n");
4953 err
= check_return_code(env
);
4957 err
= pop_stack(env
, &prev_insn_idx
, &insn_idx
);
4963 do_print_state
= true;
4967 err
= check_cond_jmp_op(env
, insn
, &insn_idx
);
4971 } else if (class == BPF_LD
) {
4972 u8 mode
= BPF_MODE(insn
->code
);
4974 if (mode
== BPF_ABS
|| mode
== BPF_IND
) {
4975 err
= check_ld_abs(env
, insn
);
4979 } else if (mode
== BPF_IMM
) {
4980 err
= check_ld_imm(env
, insn
);
4985 env
->insn_aux_data
[insn_idx
].seen
= true;
4987 verbose(env
, "invalid BPF_LD mode\n");
4991 verbose(env
, "unknown insn class %d\n", class);
4998 verbose(env
, "processed %d insns (limit %d), stack depth ",
4999 insn_processed
, BPF_COMPLEXITY_LIMIT_INSNS
);
5000 for (i
= 0; i
< env
->subprog_cnt
; i
++) {
5001 u32 depth
= env
->subprog_info
[i
].stack_depth
;
5003 verbose(env
, "%d", depth
);
5004 if (i
+ 1 < env
->subprog_cnt
)
5008 env
->prog
->aux
->stack_depth
= env
->subprog_info
[0].stack_depth
;
5012 static int check_map_prealloc(struct bpf_map
*map
)
5014 return (map
->map_type
!= BPF_MAP_TYPE_HASH
&&
5015 map
->map_type
!= BPF_MAP_TYPE_PERCPU_HASH
&&
5016 map
->map_type
!= BPF_MAP_TYPE_HASH_OF_MAPS
) ||
5017 !(map
->map_flags
& BPF_F_NO_PREALLOC
);
5020 static int check_map_prog_compatibility(struct bpf_verifier_env
*env
,
5021 struct bpf_map
*map
,
5022 struct bpf_prog
*prog
)
5025 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
5026 * preallocated hash maps, since doing memory allocation
5027 * in overflow_handler can crash depending on where nmi got
5030 if (prog
->type
== BPF_PROG_TYPE_PERF_EVENT
) {
5031 if (!check_map_prealloc(map
)) {
5032 verbose(env
, "perf_event programs can only use preallocated hash map\n");
5035 if (map
->inner_map_meta
&&
5036 !check_map_prealloc(map
->inner_map_meta
)) {
5037 verbose(env
, "perf_event programs can only use preallocated inner hash map\n");
5042 if ((bpf_prog_is_dev_bound(prog
->aux
) || bpf_map_is_dev_bound(map
)) &&
5043 !bpf_offload_dev_match(prog
, map
)) {
5044 verbose(env
, "offload device mismatch between prog and map\n");
5051 /* look for pseudo eBPF instructions that access map FDs and
5052 * replace them with actual map pointers
5054 static int replace_map_fd_with_map_ptr(struct bpf_verifier_env
*env
)
5056 struct bpf_insn
*insn
= env
->prog
->insnsi
;
5057 int insn_cnt
= env
->prog
->len
;
5060 err
= bpf_prog_calc_tag(env
->prog
);
5064 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
5065 if (BPF_CLASS(insn
->code
) == BPF_LDX
&&
5066 (BPF_MODE(insn
->code
) != BPF_MEM
|| insn
->imm
!= 0)) {
5067 verbose(env
, "BPF_LDX uses reserved fields\n");
5071 if (BPF_CLASS(insn
->code
) == BPF_STX
&&
5072 ((BPF_MODE(insn
->code
) != BPF_MEM
&&
5073 BPF_MODE(insn
->code
) != BPF_XADD
) || insn
->imm
!= 0)) {
5074 verbose(env
, "BPF_STX uses reserved fields\n");
5078 if (insn
[0].code
== (BPF_LD
| BPF_IMM
| BPF_DW
)) {
5079 struct bpf_map
*map
;
5082 if (i
== insn_cnt
- 1 || insn
[1].code
!= 0 ||
5083 insn
[1].dst_reg
!= 0 || insn
[1].src_reg
!= 0 ||
5085 verbose(env
, "invalid bpf_ld_imm64 insn\n");
5089 if (insn
->src_reg
== 0)
5090 /* valid generic load 64-bit imm */
5093 if (insn
->src_reg
!= BPF_PSEUDO_MAP_FD
) {
5095 "unrecognized bpf_ld_imm64 insn\n");
5099 f
= fdget(insn
->imm
);
5100 map
= __bpf_map_get(f
);
5102 verbose(env
, "fd %d is not pointing to valid bpf_map\n",
5104 return PTR_ERR(map
);
5107 err
= check_map_prog_compatibility(env
, map
, env
->prog
);
5113 /* store map pointer inside BPF_LD_IMM64 instruction */
5114 insn
[0].imm
= (u32
) (unsigned long) map
;
5115 insn
[1].imm
= ((u64
) (unsigned long) map
) >> 32;
5117 /* check whether we recorded this map already */
5118 for (j
= 0; j
< env
->used_map_cnt
; j
++)
5119 if (env
->used_maps
[j
] == map
) {
5124 if (env
->used_map_cnt
>= MAX_USED_MAPS
) {
5129 /* hold the map. If the program is rejected by verifier,
5130 * the map will be released by release_maps() or it
5131 * will be used by the valid program until it's unloaded
5132 * and all maps are released in free_used_maps()
5134 map
= bpf_map_inc(map
, false);
5137 return PTR_ERR(map
);
5139 env
->used_maps
[env
->used_map_cnt
++] = map
;
5148 /* Basic sanity check before we invest more work here. */
5149 if (!bpf_opcode_in_insntable(insn
->code
)) {
5150 verbose(env
, "unknown opcode %02x\n", insn
->code
);
5155 /* now all pseudo BPF_LD_IMM64 instructions load valid
5156 * 'struct bpf_map *' into a register instead of user map_fd.
5157 * These pointers will be used later by verifier to validate map access.
5162 /* drop refcnt of maps used by the rejected program */
5163 static void release_maps(struct bpf_verifier_env
*env
)
5167 for (i
= 0; i
< env
->used_map_cnt
; i
++)
5168 bpf_map_put(env
->used_maps
[i
]);
5171 /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
5172 static void convert_pseudo_ld_imm64(struct bpf_verifier_env
*env
)
5174 struct bpf_insn
*insn
= env
->prog
->insnsi
;
5175 int insn_cnt
= env
->prog
->len
;
5178 for (i
= 0; i
< insn_cnt
; i
++, insn
++)
5179 if (insn
->code
== (BPF_LD
| BPF_IMM
| BPF_DW
))
5183 /* single env->prog->insni[off] instruction was replaced with the range
5184 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
5185 * [0, off) and [off, end) to new locations, so the patched range stays zero
5187 static int adjust_insn_aux_data(struct bpf_verifier_env
*env
, u32 prog_len
,
5190 struct bpf_insn_aux_data
*new_data
, *old_data
= env
->insn_aux_data
;
5195 new_data
= vzalloc(sizeof(struct bpf_insn_aux_data
) * prog_len
);
5198 memcpy(new_data
, old_data
, sizeof(struct bpf_insn_aux_data
) * off
);
5199 memcpy(new_data
+ off
+ cnt
- 1, old_data
+ off
,
5200 sizeof(struct bpf_insn_aux_data
) * (prog_len
- off
- cnt
+ 1));
5201 for (i
= off
; i
< off
+ cnt
- 1; i
++)
5202 new_data
[i
].seen
= true;
5203 env
->insn_aux_data
= new_data
;
5208 static void adjust_subprog_starts(struct bpf_verifier_env
*env
, u32 off
, u32 len
)
5214 /* NOTE: fake 'exit' subprog should be updated as well. */
5215 for (i
= 0; i
<= env
->subprog_cnt
; i
++) {
5216 if (env
->subprog_info
[i
].start
< off
)
5218 env
->subprog_info
[i
].start
+= len
- 1;
5222 static struct bpf_prog
*bpf_patch_insn_data(struct bpf_verifier_env
*env
, u32 off
,
5223 const struct bpf_insn
*patch
, u32 len
)
5225 struct bpf_prog
*new_prog
;
5227 new_prog
= bpf_patch_insn_single(env
->prog
, off
, patch
, len
);
5230 if (adjust_insn_aux_data(env
, new_prog
->len
, off
, len
))
5232 adjust_subprog_starts(env
, off
, len
);
5236 /* The verifier does more data flow analysis than llvm and will not
5237 * explore branches that are dead at run time. Malicious programs can
5238 * have dead code too. Therefore replace all dead at-run-time code
5241 * Just nops are not optimal, e.g. if they would sit at the end of the
5242 * program and through another bug we would manage to jump there, then
5243 * we'd execute beyond program memory otherwise. Returning exception
5244 * code also wouldn't work since we can have subprogs where the dead
5245 * code could be located.
5247 static void sanitize_dead_code(struct bpf_verifier_env
*env
)
5249 struct bpf_insn_aux_data
*aux_data
= env
->insn_aux_data
;
5250 struct bpf_insn trap
= BPF_JMP_IMM(BPF_JA
, 0, 0, -1);
5251 struct bpf_insn
*insn
= env
->prog
->insnsi
;
5252 const int insn_cnt
= env
->prog
->len
;
5255 for (i
= 0; i
< insn_cnt
; i
++) {
5256 if (aux_data
[i
].seen
)
5258 memcpy(insn
+ i
, &trap
, sizeof(trap
));
5262 /* convert load instructions that access fields of 'struct __sk_buff'
5263 * into sequence of instructions that access fields of 'struct sk_buff'
5265 static int convert_ctx_accesses(struct bpf_verifier_env
*env
)
5267 const struct bpf_verifier_ops
*ops
= env
->ops
;
5268 int i
, cnt
, size
, ctx_field_size
, delta
= 0;
5269 const int insn_cnt
= env
->prog
->len
;
5270 struct bpf_insn insn_buf
[16], *insn
;
5271 struct bpf_prog
*new_prog
;
5272 enum bpf_access_type type
;
5273 bool is_narrower_load
;
5276 if (ops
->gen_prologue
) {
5277 cnt
= ops
->gen_prologue(insn_buf
, env
->seen_direct_write
,
5279 if (cnt
>= ARRAY_SIZE(insn_buf
)) {
5280 verbose(env
, "bpf verifier is misconfigured\n");
5283 new_prog
= bpf_patch_insn_data(env
, 0, insn_buf
, cnt
);
5287 env
->prog
= new_prog
;
5292 if (!ops
->convert_ctx_access
|| bpf_prog_is_dev_bound(env
->prog
->aux
))
5295 insn
= env
->prog
->insnsi
+ delta
;
5297 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
5298 if (insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_B
) ||
5299 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_H
) ||
5300 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_W
) ||
5301 insn
->code
== (BPF_LDX
| BPF_MEM
| BPF_DW
))
5303 else if (insn
->code
== (BPF_STX
| BPF_MEM
| BPF_B
) ||
5304 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_H
) ||
5305 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_W
) ||
5306 insn
->code
== (BPF_STX
| BPF_MEM
| BPF_DW
))
5311 if (type
== BPF_WRITE
&&
5312 env
->insn_aux_data
[i
+ delta
].sanitize_stack_off
) {
5313 struct bpf_insn patch
[] = {
5314 /* Sanitize suspicious stack slot with zero.
5315 * There are no memory dependencies for this store,
5316 * since it's only using frame pointer and immediate
5319 BPF_ST_MEM(BPF_DW
, BPF_REG_FP
,
5320 env
->insn_aux_data
[i
+ delta
].sanitize_stack_off
,
5322 /* the original STX instruction will immediately
5323 * overwrite the same stack slot with appropriate value
5328 cnt
= ARRAY_SIZE(patch
);
5329 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, patch
, cnt
);
5334 env
->prog
= new_prog
;
5335 insn
= new_prog
->insnsi
+ i
+ delta
;
5339 if (env
->insn_aux_data
[i
+ delta
].ptr_type
!= PTR_TO_CTX
)
5342 ctx_field_size
= env
->insn_aux_data
[i
+ delta
].ctx_field_size
;
5343 size
= BPF_LDST_BYTES(insn
);
5345 /* If the read access is a narrower load of the field,
5346 * convert to a 4/8-byte load, to minimum program type specific
5347 * convert_ctx_access changes. If conversion is successful,
5348 * we will apply proper mask to the result.
5350 is_narrower_load
= size
< ctx_field_size
;
5351 if (is_narrower_load
) {
5352 u32 size_default
= bpf_ctx_off_adjust_machine(ctx_field_size
);
5353 u32 off
= insn
->off
;
5356 if (type
== BPF_WRITE
) {
5357 verbose(env
, "bpf verifier narrow ctx access misconfigured\n");
5362 if (ctx_field_size
== 4)
5364 else if (ctx_field_size
== 8)
5367 insn
->off
= off
& ~(size_default
- 1);
5368 insn
->code
= BPF_LDX
| BPF_MEM
| size_code
;
5372 cnt
= ops
->convert_ctx_access(type
, insn
, insn_buf
, env
->prog
,
5374 if (cnt
== 0 || cnt
>= ARRAY_SIZE(insn_buf
) ||
5375 (ctx_field_size
&& !target_size
)) {
5376 verbose(env
, "bpf verifier is misconfigured\n");
5380 if (is_narrower_load
&& size
< target_size
) {
5381 if (ctx_field_size
<= 4)
5382 insn_buf
[cnt
++] = BPF_ALU32_IMM(BPF_AND
, insn
->dst_reg
,
5383 (1 << size
* 8) - 1);
5385 insn_buf
[cnt
++] = BPF_ALU64_IMM(BPF_AND
, insn
->dst_reg
,
5386 (1 << size
* 8) - 1);
5389 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, insn_buf
, cnt
);
5395 /* keep walking new program and skip insns we just inserted */
5396 env
->prog
= new_prog
;
5397 insn
= new_prog
->insnsi
+ i
+ delta
;
5403 static int jit_subprogs(struct bpf_verifier_env
*env
)
5405 struct bpf_prog
*prog
= env
->prog
, **func
, *tmp
;
5406 int i
, j
, subprog_start
, subprog_end
= 0, len
, subprog
;
5407 struct bpf_insn
*insn
;
5411 if (env
->subprog_cnt
<= 1)
5414 for (i
= 0, insn
= prog
->insnsi
; i
< prog
->len
; i
++, insn
++) {
5415 if (insn
->code
!= (BPF_JMP
| BPF_CALL
) ||
5416 insn
->src_reg
!= BPF_PSEUDO_CALL
)
5418 subprog
= find_subprog(env
, i
+ insn
->imm
+ 1);
5420 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
5424 /* temporarily remember subprog id inside insn instead of
5425 * aux_data, since next loop will split up all insns into funcs
5427 insn
->off
= subprog
;
5428 /* remember original imm in case JIT fails and fallback
5429 * to interpreter will be needed
5431 env
->insn_aux_data
[i
].call_imm
= insn
->imm
;
5432 /* point imm to __bpf_call_base+1 from JITs point of view */
5436 func
= kzalloc(sizeof(prog
) * env
->subprog_cnt
, GFP_KERNEL
);
5440 for (i
= 0; i
< env
->subprog_cnt
; i
++) {
5441 subprog_start
= subprog_end
;
5442 subprog_end
= env
->subprog_info
[i
+ 1].start
;
5444 len
= subprog_end
- subprog_start
;
5445 func
[i
] = bpf_prog_alloc(bpf_prog_size(len
), GFP_USER
);
5448 memcpy(func
[i
]->insnsi
, &prog
->insnsi
[subprog_start
],
5449 len
* sizeof(struct bpf_insn
));
5450 func
[i
]->type
= prog
->type
;
5452 if (bpf_prog_calc_tag(func
[i
]))
5454 func
[i
]->is_func
= 1;
5455 /* Use bpf_prog_F_tag to indicate functions in stack traces.
5456 * Long term would need debug info to populate names
5458 func
[i
]->aux
->name
[0] = 'F';
5459 func
[i
]->aux
->stack_depth
= env
->subprog_info
[i
].stack_depth
;
5460 func
[i
]->jit_requested
= 1;
5461 func
[i
] = bpf_int_jit_compile(func
[i
]);
5462 if (!func
[i
]->jited
) {
5468 /* at this point all bpf functions were successfully JITed
5469 * now populate all bpf_calls with correct addresses and
5470 * run last pass of JIT
5472 for (i
= 0; i
< env
->subprog_cnt
; i
++) {
5473 insn
= func
[i
]->insnsi
;
5474 for (j
= 0; j
< func
[i
]->len
; j
++, insn
++) {
5475 if (insn
->code
!= (BPF_JMP
| BPF_CALL
) ||
5476 insn
->src_reg
!= BPF_PSEUDO_CALL
)
5478 subprog
= insn
->off
;
5479 insn
->imm
= (u64 (*)(u64
, u64
, u64
, u64
, u64
))
5480 func
[subprog
]->bpf_func
-
5484 /* we use the aux data to keep a list of the start addresses
5485 * of the JITed images for each function in the program
5487 * for some architectures, such as powerpc64, the imm field
5488 * might not be large enough to hold the offset of the start
5489 * address of the callee's JITed image from __bpf_call_base
5491 * in such cases, we can lookup the start address of a callee
5492 * by using its subprog id, available from the off field of
5493 * the call instruction, as an index for this list
5495 func
[i
]->aux
->func
= func
;
5496 func
[i
]->aux
->func_cnt
= env
->subprog_cnt
;
5498 for (i
= 0; i
< env
->subprog_cnt
; i
++) {
5499 old_bpf_func
= func
[i
]->bpf_func
;
5500 tmp
= bpf_int_jit_compile(func
[i
]);
5501 if (tmp
!= func
[i
] || func
[i
]->bpf_func
!= old_bpf_func
) {
5502 verbose(env
, "JIT doesn't support bpf-to-bpf calls\n");
5509 /* finally lock prog and jit images for all functions and
5512 for (i
= 0; i
< env
->subprog_cnt
; i
++) {
5513 bpf_prog_lock_ro(func
[i
]);
5514 bpf_prog_kallsyms_add(func
[i
]);
5517 /* Last step: make now unused interpreter insns from main
5518 * prog consistent for later dump requests, so they can
5519 * later look the same as if they were interpreted only.
5521 for (i
= 0, insn
= prog
->insnsi
; i
< prog
->len
; i
++, insn
++) {
5522 if (insn
->code
!= (BPF_JMP
| BPF_CALL
) ||
5523 insn
->src_reg
!= BPF_PSEUDO_CALL
)
5525 insn
->off
= env
->insn_aux_data
[i
].call_imm
;
5526 subprog
= find_subprog(env
, i
+ insn
->off
+ 1);
5527 insn
->imm
= subprog
;
5531 prog
->bpf_func
= func
[0]->bpf_func
;
5532 prog
->aux
->func
= func
;
5533 prog
->aux
->func_cnt
= env
->subprog_cnt
;
5536 for (i
= 0; i
< env
->subprog_cnt
; i
++)
5538 bpf_jit_free(func
[i
]);
5540 /* cleanup main prog to be interpreted */
5541 prog
->jit_requested
= 0;
5542 for (i
= 0, insn
= prog
->insnsi
; i
< prog
->len
; i
++, insn
++) {
5543 if (insn
->code
!= (BPF_JMP
| BPF_CALL
) ||
5544 insn
->src_reg
!= BPF_PSEUDO_CALL
)
5547 insn
->imm
= env
->insn_aux_data
[i
].call_imm
;
5552 static int fixup_call_args(struct bpf_verifier_env
*env
)
5554 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
5555 struct bpf_prog
*prog
= env
->prog
;
5556 struct bpf_insn
*insn
= prog
->insnsi
;
5562 if (env
->prog
->jit_requested
) {
5563 err
= jit_subprogs(env
);
5567 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
5568 for (i
= 0; i
< prog
->len
; i
++, insn
++) {
5569 if (insn
->code
!= (BPF_JMP
| BPF_CALL
) ||
5570 insn
->src_reg
!= BPF_PSEUDO_CALL
)
5572 depth
= get_callee_stack_depth(env
, insn
, i
);
5575 bpf_patch_call_args(insn
, depth
);
5582 /* fixup insn->imm field of bpf_call instructions
5583 * and inline eligible helpers as explicit sequence of BPF instructions
5585 * this function is called after eBPF program passed verification
5587 static int fixup_bpf_calls(struct bpf_verifier_env
*env
)
5589 struct bpf_prog
*prog
= env
->prog
;
5590 struct bpf_insn
*insn
= prog
->insnsi
;
5591 const struct bpf_func_proto
*fn
;
5592 const int insn_cnt
= prog
->len
;
5593 const struct bpf_map_ops
*ops
;
5594 struct bpf_insn_aux_data
*aux
;
5595 struct bpf_insn insn_buf
[16];
5596 struct bpf_prog
*new_prog
;
5597 struct bpf_map
*map_ptr
;
5598 int i
, cnt
, delta
= 0;
5600 for (i
= 0; i
< insn_cnt
; i
++, insn
++) {
5601 if (insn
->code
== (BPF_ALU64
| BPF_MOD
| BPF_X
) ||
5602 insn
->code
== (BPF_ALU64
| BPF_DIV
| BPF_X
) ||
5603 insn
->code
== (BPF_ALU
| BPF_MOD
| BPF_X
) ||
5604 insn
->code
== (BPF_ALU
| BPF_DIV
| BPF_X
)) {
5605 bool is64
= BPF_CLASS(insn
->code
) == BPF_ALU64
;
5606 struct bpf_insn mask_and_div
[] = {
5607 BPF_MOV32_REG(insn
->src_reg
, insn
->src_reg
),
5609 BPF_JMP_IMM(BPF_JNE
, insn
->src_reg
, 0, 2),
5610 BPF_ALU32_REG(BPF_XOR
, insn
->dst_reg
, insn
->dst_reg
),
5611 BPF_JMP_IMM(BPF_JA
, 0, 0, 1),
5614 struct bpf_insn mask_and_mod
[] = {
5615 BPF_MOV32_REG(insn
->src_reg
, insn
->src_reg
),
5616 /* Rx mod 0 -> Rx */
5617 BPF_JMP_IMM(BPF_JEQ
, insn
->src_reg
, 0, 1),
5620 struct bpf_insn
*patchlet
;
5622 if (insn
->code
== (BPF_ALU64
| BPF_DIV
| BPF_X
) ||
5623 insn
->code
== (BPF_ALU
| BPF_DIV
| BPF_X
)) {
5624 patchlet
= mask_and_div
+ (is64
? 1 : 0);
5625 cnt
= ARRAY_SIZE(mask_and_div
) - (is64
? 1 : 0);
5627 patchlet
= mask_and_mod
+ (is64
? 1 : 0);
5628 cnt
= ARRAY_SIZE(mask_and_mod
) - (is64
? 1 : 0);
5631 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, patchlet
, cnt
);
5636 env
->prog
= prog
= new_prog
;
5637 insn
= new_prog
->insnsi
+ i
+ delta
;
5641 if (BPF_CLASS(insn
->code
) == BPF_LD
&&
5642 (BPF_MODE(insn
->code
) == BPF_ABS
||
5643 BPF_MODE(insn
->code
) == BPF_IND
)) {
5644 cnt
= env
->ops
->gen_ld_abs(insn
, insn_buf
);
5645 if (cnt
== 0 || cnt
>= ARRAY_SIZE(insn_buf
)) {
5646 verbose(env
, "bpf verifier is misconfigured\n");
5650 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, insn_buf
, cnt
);
5655 env
->prog
= prog
= new_prog
;
5656 insn
= new_prog
->insnsi
+ i
+ delta
;
5660 if (insn
->code
!= (BPF_JMP
| BPF_CALL
))
5662 if (insn
->src_reg
== BPF_PSEUDO_CALL
)
5665 if (insn
->imm
== BPF_FUNC_get_route_realm
)
5666 prog
->dst_needed
= 1;
5667 if (insn
->imm
== BPF_FUNC_get_prandom_u32
)
5668 bpf_user_rnd_init_once();
5669 if (insn
->imm
== BPF_FUNC_override_return
)
5670 prog
->kprobe_override
= 1;
5671 if (insn
->imm
== BPF_FUNC_tail_call
) {
5672 /* If we tail call into other programs, we
5673 * cannot make any assumptions since they can
5674 * be replaced dynamically during runtime in
5675 * the program array.
5677 prog
->cb_access
= 1;
5678 env
->prog
->aux
->stack_depth
= MAX_BPF_STACK
;
5680 /* mark bpf_tail_call as different opcode to avoid
5681 * conditional branch in the interpeter for every normal
5682 * call and to prevent accidental JITing by JIT compiler
5683 * that doesn't support bpf_tail_call yet
5686 insn
->code
= BPF_JMP
| BPF_TAIL_CALL
;
5688 aux
= &env
->insn_aux_data
[i
+ delta
];
5689 if (!bpf_map_ptr_unpriv(aux
))
5692 /* instead of changing every JIT dealing with tail_call
5693 * emit two extra insns:
5694 * if (index >= max_entries) goto out;
5695 * index &= array->index_mask;
5696 * to avoid out-of-bounds cpu speculation
5698 if (bpf_map_ptr_poisoned(aux
)) {
5699 verbose(env
, "tail_call abusing map_ptr\n");
5703 map_ptr
= BPF_MAP_PTR(aux
->map_state
);
5704 insn_buf
[0] = BPF_JMP_IMM(BPF_JGE
, BPF_REG_3
,
5705 map_ptr
->max_entries
, 2);
5706 insn_buf
[1] = BPF_ALU32_IMM(BPF_AND
, BPF_REG_3
,
5707 container_of(map_ptr
,
5710 insn_buf
[2] = *insn
;
5712 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, insn_buf
, cnt
);
5717 env
->prog
= prog
= new_prog
;
5718 insn
= new_prog
->insnsi
+ i
+ delta
;
5722 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
5723 * and other inlining handlers are currently limited to 64 bit
5726 if (prog
->jit_requested
&& BITS_PER_LONG
== 64 &&
5727 (insn
->imm
== BPF_FUNC_map_lookup_elem
||
5728 insn
->imm
== BPF_FUNC_map_update_elem
||
5729 insn
->imm
== BPF_FUNC_map_delete_elem
)) {
5730 aux
= &env
->insn_aux_data
[i
+ delta
];
5731 if (bpf_map_ptr_poisoned(aux
))
5732 goto patch_call_imm
;
5734 map_ptr
= BPF_MAP_PTR(aux
->map_state
);
5736 if (insn
->imm
== BPF_FUNC_map_lookup_elem
&&
5737 ops
->map_gen_lookup
) {
5738 cnt
= ops
->map_gen_lookup(map_ptr
, insn_buf
);
5739 if (cnt
== 0 || cnt
>= ARRAY_SIZE(insn_buf
)) {
5740 verbose(env
, "bpf verifier is misconfigured\n");
5744 new_prog
= bpf_patch_insn_data(env
, i
+ delta
,
5750 env
->prog
= prog
= new_prog
;
5751 insn
= new_prog
->insnsi
+ i
+ delta
;
5755 BUILD_BUG_ON(!__same_type(ops
->map_lookup_elem
,
5756 (void *(*)(struct bpf_map
*map
, void *key
))NULL
));
5757 BUILD_BUG_ON(!__same_type(ops
->map_delete_elem
,
5758 (int (*)(struct bpf_map
*map
, void *key
))NULL
));
5759 BUILD_BUG_ON(!__same_type(ops
->map_update_elem
,
5760 (int (*)(struct bpf_map
*map
, void *key
, void *value
,
5762 switch (insn
->imm
) {
5763 case BPF_FUNC_map_lookup_elem
:
5764 insn
->imm
= BPF_CAST_CALL(ops
->map_lookup_elem
) -
5767 case BPF_FUNC_map_update_elem
:
5768 insn
->imm
= BPF_CAST_CALL(ops
->map_update_elem
) -
5771 case BPF_FUNC_map_delete_elem
:
5772 insn
->imm
= BPF_CAST_CALL(ops
->map_delete_elem
) -
5777 goto patch_call_imm
;
5780 if (insn
->imm
== BPF_FUNC_redirect_map
) {
5781 /* Note, we cannot use prog directly as imm as subsequent
5782 * rewrites would still change the prog pointer. The only
5783 * stable address we can use is aux, which also works with
5784 * prog clones during blinding.
5786 u64 addr
= (unsigned long)prog
->aux
;
5787 struct bpf_insn r4_ld
[] = {
5788 BPF_LD_IMM64(BPF_REG_4
, addr
),
5791 cnt
= ARRAY_SIZE(r4_ld
);
5793 new_prog
= bpf_patch_insn_data(env
, i
+ delta
, r4_ld
, cnt
);
5798 env
->prog
= prog
= new_prog
;
5799 insn
= new_prog
->insnsi
+ i
+ delta
;
5802 fn
= env
->ops
->get_func_proto(insn
->imm
, env
->prog
);
5803 /* all functions that have prototype and verifier allowed
5804 * programs to call them, must be real in-kernel functions
5808 "kernel subsystem misconfigured func %s#%d\n",
5809 func_id_name(insn
->imm
), insn
->imm
);
5812 insn
->imm
= fn
->func
- __bpf_call_base
;
5818 static void free_states(struct bpf_verifier_env
*env
)
5820 struct bpf_verifier_state_list
*sl
, *sln
;
5823 if (!env
->explored_states
)
5826 for (i
= 0; i
< env
->prog
->len
; i
++) {
5827 sl
= env
->explored_states
[i
];
5830 while (sl
!= STATE_LIST_MARK
) {
5832 free_verifier_state(&sl
->state
, false);
5838 kfree(env
->explored_states
);
5841 int bpf_check(struct bpf_prog
**prog
, union bpf_attr
*attr
)
5843 struct bpf_verifier_env
*env
;
5844 struct bpf_verifier_log
*log
;
5847 /* no program is valid */
5848 if (ARRAY_SIZE(bpf_verifier_ops
) == 0)
5851 /* 'struct bpf_verifier_env' can be global, but since it's not small,
5852 * allocate/free it every time bpf_check() is called
5854 env
= kzalloc(sizeof(struct bpf_verifier_env
), GFP_KERNEL
);
5859 env
->insn_aux_data
= vzalloc(sizeof(struct bpf_insn_aux_data
) *
5862 if (!env
->insn_aux_data
)
5865 env
->ops
= bpf_verifier_ops
[env
->prog
->type
];
5867 /* grab the mutex to protect few globals used by verifier */
5868 mutex_lock(&bpf_verifier_lock
);
5870 if (attr
->log_level
|| attr
->log_buf
|| attr
->log_size
) {
5871 /* user requested verbose verifier output
5872 * and supplied buffer to store the verification trace
5874 log
->level
= attr
->log_level
;
5875 log
->ubuf
= (char __user
*) (unsigned long) attr
->log_buf
;
5876 log
->len_total
= attr
->log_size
;
5879 /* log attributes have to be sane */
5880 if (log
->len_total
< 128 || log
->len_total
> UINT_MAX
>> 8 ||
5881 !log
->level
|| !log
->ubuf
)
5885 env
->strict_alignment
= !!(attr
->prog_flags
& BPF_F_STRICT_ALIGNMENT
);
5886 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
))
5887 env
->strict_alignment
= true;
5889 ret
= replace_map_fd_with_map_ptr(env
);
5891 goto skip_full_check
;
5893 if (bpf_prog_is_dev_bound(env
->prog
->aux
)) {
5894 ret
= bpf_prog_offload_verifier_prep(env
);
5896 goto skip_full_check
;
5899 env
->explored_states
= kcalloc(env
->prog
->len
,
5900 sizeof(struct bpf_verifier_state_list
*),
5903 if (!env
->explored_states
)
5904 goto skip_full_check
;
5906 env
->allow_ptr_leaks
= capable(CAP_SYS_ADMIN
);
5908 ret
= check_cfg(env
);
5910 goto skip_full_check
;
5912 ret
= do_check(env
);
5913 if (env
->cur_state
) {
5914 free_verifier_state(env
->cur_state
, true);
5915 env
->cur_state
= NULL
;
5919 while (!pop_stack(env
, NULL
, NULL
));
5923 sanitize_dead_code(env
);
5926 ret
= check_max_stack_depth(env
);
5929 /* program is valid, convert *(u32*)(ctx + off) accesses */
5930 ret
= convert_ctx_accesses(env
);
5933 ret
= fixup_bpf_calls(env
);
5936 ret
= fixup_call_args(env
);
5938 if (log
->level
&& bpf_verifier_log_full(log
))
5940 if (log
->level
&& !log
->ubuf
) {
5942 goto err_release_maps
;
5945 if (ret
== 0 && env
->used_map_cnt
) {
5946 /* if program passed verifier, update used_maps in bpf_prog_info */
5947 env
->prog
->aux
->used_maps
= kmalloc_array(env
->used_map_cnt
,
5948 sizeof(env
->used_maps
[0]),
5951 if (!env
->prog
->aux
->used_maps
) {
5953 goto err_release_maps
;
5956 memcpy(env
->prog
->aux
->used_maps
, env
->used_maps
,
5957 sizeof(env
->used_maps
[0]) * env
->used_map_cnt
);
5958 env
->prog
->aux
->used_map_cnt
= env
->used_map_cnt
;
5960 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
5961 * bpf_ld_imm64 instructions
5963 convert_pseudo_ld_imm64(env
);
5967 if (!env
->prog
->aux
->used_maps
)
5968 /* if we didn't copy map pointers into bpf_prog_info, release
5969 * them now. Otherwise free_used_maps() will release them.
5974 mutex_unlock(&bpf_verifier_lock
);
5975 vfree(env
->insn_aux_data
);