1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 * Copyright (c) 2016 Facebook
4 * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io
6 #include <uapi/linux/btf.h>
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/slab.h>
10 #include <linux/bpf.h>
11 #include <linux/btf.h>
12 #include <linux/bpf_verifier.h>
13 #include <linux/filter.h>
14 #include <net/netlink.h>
15 #include <linux/file.h>
16 #include <linux/vmalloc.h>
17 #include <linux/stringify.h>
18 #include <linux/bsearch.h>
19 #include <linux/sort.h>
20 #include <linux/perf_event.h>
21 #include <linux/ctype.h>
22 #include <linux/error-injection.h>
23 #include <linux/bpf_lsm.h>
27 static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
28 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
29 [_id] = & _name ## _verifier_ops,
30 #define BPF_MAP_TYPE(_id, _ops)
31 #define BPF_LINK_TYPE(_id, _name)
32 #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, PTR_TO_SOCKET. These are
84 * four pointer 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.
144 * The following reference types represent a potential reference to a kernel
145 * resource which, after first being allocated, must be checked and freed by
147 * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET
149 * When the verifier sees a helper call return a reference type, it allocates a
150 * pointer id for the reference and stores it in the current function state.
151 * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into
152 * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type
153 * passes through a NULL-check conditional. For the branch wherein the state is
154 * changed to CONST_IMM, the verifier releases the reference.
156 * For each helper function that allocates a reference, such as
157 * bpf_sk_lookup_tcp(), there is a corresponding release function, such as
158 * bpf_sk_release(). When a reference type passes into the release function,
159 * the verifier also releases the reference. If any unchecked or unreleased
160 * reference remains at the end of the program, the verifier rejects it.
163 /* verifier_state + insn_idx are pushed to stack when branch is encountered */
164 struct bpf_verifier_stack_elem {
165 /* verifer state is 'st'
166 * before processing instruction 'insn_idx'
167 * and after processing instruction 'prev_insn_idx'
169 struct bpf_verifier_state st;
172 struct bpf_verifier_stack_elem *next;
173 /* length of verifier log at the time this state was pushed on stack */
177 #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192
178 #define BPF_COMPLEXITY_LIMIT_STATES 64
180 #define BPF_MAP_KEY_POISON (1ULL << 63)
181 #define BPF_MAP_KEY_SEEN (1ULL << 62)
183 #define BPF_MAP_PTR_UNPRIV 1UL
184 #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \
185 POISON_POINTER_DELTA))
186 #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV))
188 static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux)
190 return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON;
193 static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux)
195 return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV;
198 static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux,
199 const struct bpf_map *map, bool unpriv)
201 BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV);
202 unpriv |= bpf_map_ptr_unpriv(aux);
203 aux->map_ptr_state = (unsigned long)map |
204 (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL);
207 static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux)
209 return aux->map_key_state & BPF_MAP_KEY_POISON;
212 static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux)
214 return !(aux->map_key_state & BPF_MAP_KEY_SEEN);
217 static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux)
219 return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON);
222 static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state)
224 bool poisoned = bpf_map_key_poisoned(aux);
226 aux->map_key_state = state | BPF_MAP_KEY_SEEN |
227 (poisoned ? BPF_MAP_KEY_POISON : 0ULL);
230 struct bpf_call_arg_meta {
231 struct bpf_map *map_ptr;
243 struct btf *btf_vmlinux;
245 static DEFINE_MUTEX(bpf_verifier_lock);
247 static const struct bpf_line_info *
248 find_linfo(const struct bpf_verifier_env *env, u32 insn_off)
250 const struct bpf_line_info *linfo;
251 const struct bpf_prog *prog;
255 nr_linfo = prog->aux->nr_linfo;
257 if (!nr_linfo || insn_off >= prog->len)
260 linfo = prog->aux->linfo;
261 for (i = 1; i < nr_linfo; i++)
262 if (insn_off < linfo[i].insn_off)
265 return &linfo[i - 1];
268 void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,
273 n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);
275 WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
276 "verifier log line truncated - local buffer too short\n");
278 n = min(log->len_total - log->len_used - 1, n);
281 if (log->level == BPF_LOG_KERNEL) {
282 pr_err("BPF:%s\n", log->kbuf);
285 if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))
291 static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos)
295 if (!bpf_verifier_log_needed(log))
298 log->len_used = new_pos;
299 if (put_user(zero, log->ubuf + new_pos))
303 /* log_level controls verbosity level of eBPF verifier.
304 * bpf_verifier_log_write() is used to dump the verification trace to the log,
305 * so the user can figure out what's wrong with the program
307 __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
308 const char *fmt, ...)
312 if (!bpf_verifier_log_needed(&env->log))
316 bpf_verifier_vlog(&env->log, fmt, args);
319 EXPORT_SYMBOL_GPL(bpf_verifier_log_write);
321 __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...)
323 struct bpf_verifier_env *env = private_data;
326 if (!bpf_verifier_log_needed(&env->log))
330 bpf_verifier_vlog(&env->log, fmt, args);
334 __printf(2, 3) void bpf_log(struct bpf_verifier_log *log,
335 const char *fmt, ...)
339 if (!bpf_verifier_log_needed(log))
343 bpf_verifier_vlog(log, fmt, args);
347 static const char *ltrim(const char *s)
355 __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env,
357 const char *prefix_fmt, ...)
359 const struct bpf_line_info *linfo;
361 if (!bpf_verifier_log_needed(&env->log))
364 linfo = find_linfo(env, insn_off);
365 if (!linfo || linfo == env->prev_linfo)
371 va_start(args, prefix_fmt);
372 bpf_verifier_vlog(&env->log, prefix_fmt, args);
377 ltrim(btf_name_by_offset(env->prog->aux->btf,
380 env->prev_linfo = linfo;
383 static bool type_is_pkt_pointer(enum bpf_reg_type type)
385 return type == PTR_TO_PACKET ||
386 type == PTR_TO_PACKET_META;
389 static bool type_is_sk_pointer(enum bpf_reg_type type)
391 return type == PTR_TO_SOCKET ||
392 type == PTR_TO_SOCK_COMMON ||
393 type == PTR_TO_TCP_SOCK ||
394 type == PTR_TO_XDP_SOCK;
397 static bool reg_type_not_null(enum bpf_reg_type type)
399 return type == PTR_TO_SOCKET ||
400 type == PTR_TO_TCP_SOCK ||
401 type == PTR_TO_MAP_VALUE ||
402 type == PTR_TO_SOCK_COMMON ||
403 type == PTR_TO_BTF_ID;
406 static bool reg_type_may_be_null(enum bpf_reg_type type)
408 return type == PTR_TO_MAP_VALUE_OR_NULL ||
409 type == PTR_TO_SOCKET_OR_NULL ||
410 type == PTR_TO_SOCK_COMMON_OR_NULL ||
411 type == PTR_TO_TCP_SOCK_OR_NULL ||
412 type == PTR_TO_BTF_ID_OR_NULL ||
413 type == PTR_TO_MEM_OR_NULL;
416 static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg)
418 return reg->type == PTR_TO_MAP_VALUE &&
419 map_value_has_spin_lock(reg->map_ptr);
422 static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type)
424 return type == PTR_TO_SOCKET ||
425 type == PTR_TO_SOCKET_OR_NULL ||
426 type == PTR_TO_TCP_SOCK ||
427 type == PTR_TO_TCP_SOCK_OR_NULL ||
428 type == PTR_TO_MEM ||
429 type == PTR_TO_MEM_OR_NULL;
432 static bool arg_type_may_be_refcounted(enum bpf_arg_type type)
434 return type == ARG_PTR_TO_SOCK_COMMON;
437 /* Determine whether the function releases some resources allocated by another
438 * function call. The first reference type argument will be assumed to be
439 * released by release_reference().
441 static bool is_release_function(enum bpf_func_id func_id)
443 return func_id == BPF_FUNC_sk_release ||
444 func_id == BPF_FUNC_ringbuf_submit ||
445 func_id == BPF_FUNC_ringbuf_discard;
448 static bool may_be_acquire_function(enum bpf_func_id func_id)
450 return func_id == BPF_FUNC_sk_lookup_tcp ||
451 func_id == BPF_FUNC_sk_lookup_udp ||
452 func_id == BPF_FUNC_skc_lookup_tcp ||
453 func_id == BPF_FUNC_map_lookup_elem ||
454 func_id == BPF_FUNC_ringbuf_reserve;
457 static bool is_acquire_function(enum bpf_func_id func_id,
458 const struct bpf_map *map)
460 enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC;
462 if (func_id == BPF_FUNC_sk_lookup_tcp ||
463 func_id == BPF_FUNC_sk_lookup_udp ||
464 func_id == BPF_FUNC_skc_lookup_tcp ||
465 func_id == BPF_FUNC_ringbuf_reserve)
468 if (func_id == BPF_FUNC_map_lookup_elem &&
469 (map_type == BPF_MAP_TYPE_SOCKMAP ||
470 map_type == BPF_MAP_TYPE_SOCKHASH))
476 static bool is_ptr_cast_function(enum bpf_func_id func_id)
478 return func_id == BPF_FUNC_tcp_sock ||
479 func_id == BPF_FUNC_sk_fullsock;
482 /* string representation of 'enum bpf_reg_type' */
483 static const char * const reg_type_str[] = {
485 [SCALAR_VALUE] = "inv",
486 [PTR_TO_CTX] = "ctx",
487 [CONST_PTR_TO_MAP] = "map_ptr",
488 [PTR_TO_MAP_VALUE] = "map_value",
489 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
490 [PTR_TO_STACK] = "fp",
491 [PTR_TO_PACKET] = "pkt",
492 [PTR_TO_PACKET_META] = "pkt_meta",
493 [PTR_TO_PACKET_END] = "pkt_end",
494 [PTR_TO_FLOW_KEYS] = "flow_keys",
495 [PTR_TO_SOCKET] = "sock",
496 [PTR_TO_SOCKET_OR_NULL] = "sock_or_null",
497 [PTR_TO_SOCK_COMMON] = "sock_common",
498 [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null",
499 [PTR_TO_TCP_SOCK] = "tcp_sock",
500 [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null",
501 [PTR_TO_TP_BUFFER] = "tp_buffer",
502 [PTR_TO_XDP_SOCK] = "xdp_sock",
503 [PTR_TO_BTF_ID] = "ptr_",
504 [PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_",
505 [PTR_TO_MEM] = "mem",
506 [PTR_TO_MEM_OR_NULL] = "mem_or_null",
509 static char slot_type_char[] = {
510 [STACK_INVALID] = '?',
516 static void print_liveness(struct bpf_verifier_env *env,
517 enum bpf_reg_liveness live)
519 if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE))
521 if (live & REG_LIVE_READ)
523 if (live & REG_LIVE_WRITTEN)
525 if (live & REG_LIVE_DONE)
529 static struct bpf_func_state *func(struct bpf_verifier_env *env,
530 const struct bpf_reg_state *reg)
532 struct bpf_verifier_state *cur = env->cur_state;
534 return cur->frame[reg->frameno];
537 const char *kernel_type_name(u32 id)
539 return btf_name_by_offset(btf_vmlinux,
540 btf_type_by_id(btf_vmlinux, id)->name_off);
543 static void print_verifier_state(struct bpf_verifier_env *env,
544 const struct bpf_func_state *state)
546 const struct bpf_reg_state *reg;
551 verbose(env, " frame%d:", state->frameno);
552 for (i = 0; i < MAX_BPF_REG; i++) {
553 reg = &state->regs[i];
557 verbose(env, " R%d", i);
558 print_liveness(env, reg->live);
559 verbose(env, "=%s", reg_type_str[t]);
560 if (t == SCALAR_VALUE && reg->precise)
562 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
563 tnum_is_const(reg->var_off)) {
564 /* reg->off should be 0 for SCALAR_VALUE */
565 verbose(env, "%lld", reg->var_off.value + reg->off);
567 if (t == PTR_TO_BTF_ID || t == PTR_TO_BTF_ID_OR_NULL)
568 verbose(env, "%s", kernel_type_name(reg->btf_id));
569 verbose(env, "(id=%d", reg->id);
570 if (reg_type_may_be_refcounted_or_null(t))
571 verbose(env, ",ref_obj_id=%d", reg->ref_obj_id);
572 if (t != SCALAR_VALUE)
573 verbose(env, ",off=%d", reg->off);
574 if (type_is_pkt_pointer(t))
575 verbose(env, ",r=%d", reg->range);
576 else if (t == CONST_PTR_TO_MAP ||
577 t == PTR_TO_MAP_VALUE ||
578 t == PTR_TO_MAP_VALUE_OR_NULL)
579 verbose(env, ",ks=%d,vs=%d",
580 reg->map_ptr->key_size,
581 reg->map_ptr->value_size);
582 if (tnum_is_const(reg->var_off)) {
583 /* Typically an immediate SCALAR_VALUE, but
584 * could be a pointer whose offset is too big
587 verbose(env, ",imm=%llx", reg->var_off.value);
589 if (reg->smin_value != reg->umin_value &&
590 reg->smin_value != S64_MIN)
591 verbose(env, ",smin_value=%lld",
592 (long long)reg->smin_value);
593 if (reg->smax_value != reg->umax_value &&
594 reg->smax_value != S64_MAX)
595 verbose(env, ",smax_value=%lld",
596 (long long)reg->smax_value);
597 if (reg->umin_value != 0)
598 verbose(env, ",umin_value=%llu",
599 (unsigned long long)reg->umin_value);
600 if (reg->umax_value != U64_MAX)
601 verbose(env, ",umax_value=%llu",
602 (unsigned long long)reg->umax_value);
603 if (!tnum_is_unknown(reg->var_off)) {
606 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
607 verbose(env, ",var_off=%s", tn_buf);
609 if (reg->s32_min_value != reg->smin_value &&
610 reg->s32_min_value != S32_MIN)
611 verbose(env, ",s32_min_value=%d",
612 (int)(reg->s32_min_value));
613 if (reg->s32_max_value != reg->smax_value &&
614 reg->s32_max_value != S32_MAX)
615 verbose(env, ",s32_max_value=%d",
616 (int)(reg->s32_max_value));
617 if (reg->u32_min_value != reg->umin_value &&
618 reg->u32_min_value != U32_MIN)
619 verbose(env, ",u32_min_value=%d",
620 (int)(reg->u32_min_value));
621 if (reg->u32_max_value != reg->umax_value &&
622 reg->u32_max_value != U32_MAX)
623 verbose(env, ",u32_max_value=%d",
624 (int)(reg->u32_max_value));
629 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
630 char types_buf[BPF_REG_SIZE + 1];
634 for (j = 0; j < BPF_REG_SIZE; j++) {
635 if (state->stack[i].slot_type[j] != STACK_INVALID)
637 types_buf[j] = slot_type_char[
638 state->stack[i].slot_type[j]];
640 types_buf[BPF_REG_SIZE] = 0;
643 verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE);
644 print_liveness(env, state->stack[i].spilled_ptr.live);
645 if (state->stack[i].slot_type[0] == STACK_SPILL) {
646 reg = &state->stack[i].spilled_ptr;
648 verbose(env, "=%s", reg_type_str[t]);
649 if (t == SCALAR_VALUE && reg->precise)
651 if (t == SCALAR_VALUE && tnum_is_const(reg->var_off))
652 verbose(env, "%lld", reg->var_off.value + reg->off);
654 verbose(env, "=%s", types_buf);
657 if (state->acquired_refs && state->refs[0].id) {
658 verbose(env, " refs=%d", state->refs[0].id);
659 for (i = 1; i < state->acquired_refs; i++)
660 if (state->refs[i].id)
661 verbose(env, ",%d", state->refs[i].id);
666 #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \
667 static int copy_##NAME##_state(struct bpf_func_state *dst, \
668 const struct bpf_func_state *src) \
672 if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \
673 /* internal bug, make state invalid to reject the program */ \
674 memset(dst, 0, sizeof(*dst)); \
677 memcpy(dst->FIELD, src->FIELD, \
678 sizeof(*src->FIELD) * (src->COUNT / SIZE)); \
681 /* copy_reference_state() */
682 COPY_STATE_FN(reference, acquired_refs, refs, 1)
683 /* copy_stack_state() */
684 COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
687 #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \
688 static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \
691 u32 old_size = state->COUNT; \
692 struct bpf_##NAME##_state *new_##FIELD; \
693 int slot = size / SIZE; \
695 if (size <= old_size || !size) { \
698 state->COUNT = slot * SIZE; \
699 if (!size && old_size) { \
700 kfree(state->FIELD); \
701 state->FIELD = NULL; \
705 new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \
711 memcpy(new_##FIELD, state->FIELD, \
712 sizeof(*new_##FIELD) * (old_size / SIZE)); \
713 memset(new_##FIELD + old_size / SIZE, 0, \
714 sizeof(*new_##FIELD) * (size - old_size) / SIZE); \
716 state->COUNT = slot * SIZE; \
717 kfree(state->FIELD); \
718 state->FIELD = new_##FIELD; \
721 /* realloc_reference_state() */
722 REALLOC_STATE_FN(reference, acquired_refs, refs, 1)
723 /* realloc_stack_state() */
724 REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
725 #undef REALLOC_STATE_FN
727 /* do_check() starts with zero-sized stack in struct bpf_verifier_state to
728 * make it consume minimal amount of memory. check_stack_write() access from
729 * the program calls into realloc_func_state() to grow the stack size.
730 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
731 * which realloc_stack_state() copies over. It points to previous
732 * bpf_verifier_state which is never reallocated.
734 static int realloc_func_state(struct bpf_func_state *state, int stack_size,
735 int refs_size, bool copy_old)
737 int err = realloc_reference_state(state, refs_size, copy_old);
740 return realloc_stack_state(state, stack_size, copy_old);
743 /* Acquire a pointer id from the env and update the state->refs to include
744 * this new pointer reference.
745 * On success, returns a valid pointer id to associate with the register
746 * On failure, returns a negative errno.
748 static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx)
750 struct bpf_func_state *state = cur_func(env);
751 int new_ofs = state->acquired_refs;
754 err = realloc_reference_state(state, state->acquired_refs + 1, true);
758 state->refs[new_ofs].id = id;
759 state->refs[new_ofs].insn_idx = insn_idx;
764 /* release function corresponding to acquire_reference_state(). Idempotent. */
765 static int release_reference_state(struct bpf_func_state *state, int ptr_id)
769 last_idx = state->acquired_refs - 1;
770 for (i = 0; i < state->acquired_refs; i++) {
771 if (state->refs[i].id == ptr_id) {
772 if (last_idx && i != last_idx)
773 memcpy(&state->refs[i], &state->refs[last_idx],
774 sizeof(*state->refs));
775 memset(&state->refs[last_idx], 0, sizeof(*state->refs));
776 state->acquired_refs--;
783 static int transfer_reference_state(struct bpf_func_state *dst,
784 struct bpf_func_state *src)
786 int err = realloc_reference_state(dst, src->acquired_refs, false);
789 err = copy_reference_state(dst, src);
795 static void free_func_state(struct bpf_func_state *state)
804 static void clear_jmp_history(struct bpf_verifier_state *state)
806 kfree(state->jmp_history);
807 state->jmp_history = NULL;
808 state->jmp_history_cnt = 0;
811 static void free_verifier_state(struct bpf_verifier_state *state,
816 for (i = 0; i <= state->curframe; i++) {
817 free_func_state(state->frame[i]);
818 state->frame[i] = NULL;
820 clear_jmp_history(state);
825 /* copy verifier state from src to dst growing dst stack space
826 * when necessary to accommodate larger src stack
828 static int copy_func_state(struct bpf_func_state *dst,
829 const struct bpf_func_state *src)
833 err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs,
837 memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs));
838 err = copy_reference_state(dst, src);
841 return copy_stack_state(dst, src);
844 static int copy_verifier_state(struct bpf_verifier_state *dst_state,
845 const struct bpf_verifier_state *src)
847 struct bpf_func_state *dst;
848 u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt;
851 if (dst_state->jmp_history_cnt < src->jmp_history_cnt) {
852 kfree(dst_state->jmp_history);
853 dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER);
854 if (!dst_state->jmp_history)
857 memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz);
858 dst_state->jmp_history_cnt = src->jmp_history_cnt;
860 /* if dst has more stack frames then src frame, free them */
861 for (i = src->curframe + 1; i <= dst_state->curframe; i++) {
862 free_func_state(dst_state->frame[i]);
863 dst_state->frame[i] = NULL;
865 dst_state->speculative = src->speculative;
866 dst_state->curframe = src->curframe;
867 dst_state->active_spin_lock = src->active_spin_lock;
868 dst_state->branches = src->branches;
869 dst_state->parent = src->parent;
870 dst_state->first_insn_idx = src->first_insn_idx;
871 dst_state->last_insn_idx = src->last_insn_idx;
872 for (i = 0; i <= src->curframe; i++) {
873 dst = dst_state->frame[i];
875 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
878 dst_state->frame[i] = dst;
880 err = copy_func_state(dst, src->frame[i]);
887 static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st)
890 u32 br = --st->branches;
892 /* WARN_ON(br > 1) technically makes sense here,
893 * but see comment in push_stack(), hence:
895 WARN_ONCE((int)br < 0,
896 "BUG update_branch_counts:branches_to_explore=%d\n",
904 static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
905 int *insn_idx, bool pop_log)
907 struct bpf_verifier_state *cur = env->cur_state;
908 struct bpf_verifier_stack_elem *elem, *head = env->head;
911 if (env->head == NULL)
915 err = copy_verifier_state(cur, &head->st);
920 bpf_vlog_reset(&env->log, head->log_pos);
922 *insn_idx = head->insn_idx;
924 *prev_insn_idx = head->prev_insn_idx;
926 free_verifier_state(&head->st, false);
933 static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
934 int insn_idx, int prev_insn_idx,
937 struct bpf_verifier_state *cur = env->cur_state;
938 struct bpf_verifier_stack_elem *elem;
941 elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
945 elem->insn_idx = insn_idx;
946 elem->prev_insn_idx = prev_insn_idx;
947 elem->next = env->head;
948 elem->log_pos = env->log.len_used;
951 err = copy_verifier_state(&elem->st, cur);
954 elem->st.speculative |= speculative;
955 if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) {
956 verbose(env, "The sequence of %d jumps is too complex.\n",
960 if (elem->st.parent) {
961 ++elem->st.parent->branches;
962 /* WARN_ON(branches > 2) technically makes sense here,
964 * 1. speculative states will bump 'branches' for non-branch
966 * 2. is_state_visited() heuristics may decide not to create
967 * a new state for a sequence of branches and all such current
968 * and cloned states will be pointing to a single parent state
969 * which might have large 'branches' count.
974 free_verifier_state(env->cur_state, true);
975 env->cur_state = NULL;
976 /* pop all elements and return */
977 while (!pop_stack(env, NULL, NULL, false));
981 #define CALLER_SAVED_REGS 6
982 static const int caller_saved[CALLER_SAVED_REGS] = {
983 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
986 static void __mark_reg_not_init(const struct bpf_verifier_env *env,
987 struct bpf_reg_state *reg);
989 /* Mark the unknown part of a register (variable offset or scalar value) as
990 * known to have the value @imm.
992 static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
994 /* Clear id, off, and union(map_ptr, range) */
995 memset(((u8 *)reg) + sizeof(reg->type), 0,
996 offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
997 reg->var_off = tnum_const(imm);
998 reg->smin_value = (s64)imm;
999 reg->smax_value = (s64)imm;
1000 reg->umin_value = imm;
1001 reg->umax_value = imm;
1003 reg->s32_min_value = (s32)imm;
1004 reg->s32_max_value = (s32)imm;
1005 reg->u32_min_value = (u32)imm;
1006 reg->u32_max_value = (u32)imm;
1009 static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm)
1011 reg->var_off = tnum_const_subreg(reg->var_off, imm);
1012 reg->s32_min_value = (s32)imm;
1013 reg->s32_max_value = (s32)imm;
1014 reg->u32_min_value = (u32)imm;
1015 reg->u32_max_value = (u32)imm;
1018 /* Mark the 'variable offset' part of a register as zero. This should be
1019 * used only on registers holding a pointer type.
1021 static void __mark_reg_known_zero(struct bpf_reg_state *reg)
1023 __mark_reg_known(reg, 0);
1026 static void __mark_reg_const_zero(struct bpf_reg_state *reg)
1028 __mark_reg_known(reg, 0);
1029 reg->type = SCALAR_VALUE;
1032 static void mark_reg_known_zero(struct bpf_verifier_env *env,
1033 struct bpf_reg_state *regs, u32 regno)
1035 if (WARN_ON(regno >= MAX_BPF_REG)) {
1036 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
1037 /* Something bad happened, let's kill all regs */
1038 for (regno = 0; regno < MAX_BPF_REG; regno++)
1039 __mark_reg_not_init(env, regs + regno);
1042 __mark_reg_known_zero(regs + regno);
1045 static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
1047 return type_is_pkt_pointer(reg->type);
1050 static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
1052 return reg_is_pkt_pointer(reg) ||
1053 reg->type == PTR_TO_PACKET_END;
1056 /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
1057 static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
1058 enum bpf_reg_type which)
1060 /* The register can already have a range from prior markings.
1061 * This is fine as long as it hasn't been advanced from its
1064 return reg->type == which &&
1067 tnum_equals_const(reg->var_off, 0);
1070 /* Reset the min/max bounds of a register */
1071 static void __mark_reg_unbounded(struct bpf_reg_state *reg)
1073 reg->smin_value = S64_MIN;
1074 reg->smax_value = S64_MAX;
1075 reg->umin_value = 0;
1076 reg->umax_value = U64_MAX;
1078 reg->s32_min_value = S32_MIN;
1079 reg->s32_max_value = S32_MAX;
1080 reg->u32_min_value = 0;
1081 reg->u32_max_value = U32_MAX;
1084 static void __mark_reg64_unbounded(struct bpf_reg_state *reg)
1086 reg->smin_value = S64_MIN;
1087 reg->smax_value = S64_MAX;
1088 reg->umin_value = 0;
1089 reg->umax_value = U64_MAX;
1092 static void __mark_reg32_unbounded(struct bpf_reg_state *reg)
1094 reg->s32_min_value = S32_MIN;
1095 reg->s32_max_value = S32_MAX;
1096 reg->u32_min_value = 0;
1097 reg->u32_max_value = U32_MAX;
1100 static void __update_reg32_bounds(struct bpf_reg_state *reg)
1102 struct tnum var32_off = tnum_subreg(reg->var_off);
1104 /* min signed is max(sign bit) | min(other bits) */
1105 reg->s32_min_value = max_t(s32, reg->s32_min_value,
1106 var32_off.value | (var32_off.mask & S32_MIN));
1107 /* max signed is min(sign bit) | max(other bits) */
1108 reg->s32_max_value = min_t(s32, reg->s32_max_value,
1109 var32_off.value | (var32_off.mask & S32_MAX));
1110 reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value);
1111 reg->u32_max_value = min(reg->u32_max_value,
1112 (u32)(var32_off.value | var32_off.mask));
1115 static void __update_reg64_bounds(struct bpf_reg_state *reg)
1117 /* min signed is max(sign bit) | min(other bits) */
1118 reg->smin_value = max_t(s64, reg->smin_value,
1119 reg->var_off.value | (reg->var_off.mask & S64_MIN));
1120 /* max signed is min(sign bit) | max(other bits) */
1121 reg->smax_value = min_t(s64, reg->smax_value,
1122 reg->var_off.value | (reg->var_off.mask & S64_MAX));
1123 reg->umin_value = max(reg->umin_value, reg->var_off.value);
1124 reg->umax_value = min(reg->umax_value,
1125 reg->var_off.value | reg->var_off.mask);
1128 static void __update_reg_bounds(struct bpf_reg_state *reg)
1130 __update_reg32_bounds(reg);
1131 __update_reg64_bounds(reg);
1134 /* Uses signed min/max values to inform unsigned, and vice-versa */
1135 static void __reg32_deduce_bounds(struct bpf_reg_state *reg)
1137 /* Learn sign from signed bounds.
1138 * If we cannot cross the sign boundary, then signed and unsigned bounds
1139 * are the same, so combine. This works even in the negative case, e.g.
1140 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
1142 if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) {
1143 reg->s32_min_value = reg->u32_min_value =
1144 max_t(u32, reg->s32_min_value, reg->u32_min_value);
1145 reg->s32_max_value = reg->u32_max_value =
1146 min_t(u32, reg->s32_max_value, reg->u32_max_value);
1149 /* Learn sign from unsigned bounds. Signed bounds cross the sign
1150 * boundary, so we must be careful.
1152 if ((s32)reg->u32_max_value >= 0) {
1153 /* Positive. We can't learn anything from the smin, but smax
1154 * is positive, hence safe.
1156 reg->s32_min_value = reg->u32_min_value;
1157 reg->s32_max_value = reg->u32_max_value =
1158 min_t(u32, reg->s32_max_value, reg->u32_max_value);
1159 } else if ((s32)reg->u32_min_value < 0) {
1160 /* Negative. We can't learn anything from the smax, but smin
1161 * is negative, hence safe.
1163 reg->s32_min_value = reg->u32_min_value =
1164 max_t(u32, reg->s32_min_value, reg->u32_min_value);
1165 reg->s32_max_value = reg->u32_max_value;
1169 static void __reg64_deduce_bounds(struct bpf_reg_state *reg)
1171 /* Learn sign from signed bounds.
1172 * If we cannot cross the sign boundary, then signed and unsigned bounds
1173 * are the same, so combine. This works even in the negative case, e.g.
1174 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
1176 if (reg->smin_value >= 0 || reg->smax_value < 0) {
1177 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
1179 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
1183 /* Learn sign from unsigned bounds. Signed bounds cross the sign
1184 * boundary, so we must be careful.
1186 if ((s64)reg->umax_value >= 0) {
1187 /* Positive. We can't learn anything from the smin, but smax
1188 * is positive, hence safe.
1190 reg->smin_value = reg->umin_value;
1191 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
1193 } else if ((s64)reg->umin_value < 0) {
1194 /* Negative. We can't learn anything from the smax, but smin
1195 * is negative, hence safe.
1197 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
1199 reg->smax_value = reg->umax_value;
1203 static void __reg_deduce_bounds(struct bpf_reg_state *reg)
1205 __reg32_deduce_bounds(reg);
1206 __reg64_deduce_bounds(reg);
1209 /* Attempts to improve var_off based on unsigned min/max information */
1210 static void __reg_bound_offset(struct bpf_reg_state *reg)
1212 struct tnum var64_off = tnum_intersect(reg->var_off,
1213 tnum_range(reg->umin_value,
1215 struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off),
1216 tnum_range(reg->u32_min_value,
1217 reg->u32_max_value));
1219 reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off);
1222 static void __reg_assign_32_into_64(struct bpf_reg_state *reg)
1224 reg->umin_value = reg->u32_min_value;
1225 reg->umax_value = reg->u32_max_value;
1226 /* Attempt to pull 32-bit signed bounds into 64-bit bounds
1227 * but must be positive otherwise set to worse case bounds
1228 * and refine later from tnum.
1230 if (reg->s32_min_value >= 0 && reg->s32_max_value >= 0)
1231 reg->smax_value = reg->s32_max_value;
1233 reg->smax_value = U32_MAX;
1234 if (reg->s32_min_value >= 0)
1235 reg->smin_value = reg->s32_min_value;
1237 reg->smin_value = 0;
1240 static void __reg_combine_32_into_64(struct bpf_reg_state *reg)
1242 /* special case when 64-bit register has upper 32-bit register
1243 * zeroed. Typically happens after zext or <<32, >>32 sequence
1244 * allowing us to use 32-bit bounds directly,
1246 if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) {
1247 __reg_assign_32_into_64(reg);
1249 /* Otherwise the best we can do is push lower 32bit known and
1250 * unknown bits into register (var_off set from jmp logic)
1251 * then learn as much as possible from the 64-bit tnum
1252 * known and unknown bits. The previous smin/smax bounds are
1253 * invalid here because of jmp32 compare so mark them unknown
1254 * so they do not impact tnum bounds calculation.
1256 __mark_reg64_unbounded(reg);
1257 __update_reg_bounds(reg);
1260 /* Intersecting with the old var_off might have improved our bounds
1261 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
1262 * then new var_off is (0; 0x7f...fc) which improves our umax.
1264 __reg_deduce_bounds(reg);
1265 __reg_bound_offset(reg);
1266 __update_reg_bounds(reg);
1269 static bool __reg64_bound_s32(s64 a)
1271 if (a > S32_MIN && a < S32_MAX)
1276 static bool __reg64_bound_u32(u64 a)
1278 if (a > U32_MIN && a < U32_MAX)
1283 static void __reg_combine_64_into_32(struct bpf_reg_state *reg)
1285 __mark_reg32_unbounded(reg);
1287 if (__reg64_bound_s32(reg->smin_value))
1288 reg->s32_min_value = (s32)reg->smin_value;
1289 if (__reg64_bound_s32(reg->smax_value))
1290 reg->s32_max_value = (s32)reg->smax_value;
1291 if (__reg64_bound_u32(reg->umin_value))
1292 reg->u32_min_value = (u32)reg->umin_value;
1293 if (__reg64_bound_u32(reg->umax_value))
1294 reg->u32_max_value = (u32)reg->umax_value;
1296 /* Intersecting with the old var_off might have improved our bounds
1297 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
1298 * then new var_off is (0; 0x7f...fc) which improves our umax.
1300 __reg_deduce_bounds(reg);
1301 __reg_bound_offset(reg);
1302 __update_reg_bounds(reg);
1305 /* Mark a register as having a completely unknown (scalar) value. */
1306 static void __mark_reg_unknown(const struct bpf_verifier_env *env,
1307 struct bpf_reg_state *reg)
1310 * Clear type, id, off, and union(map_ptr, range) and
1311 * padding between 'type' and union
1313 memset(reg, 0, offsetof(struct bpf_reg_state, var_off));
1314 reg->type = SCALAR_VALUE;
1315 reg->var_off = tnum_unknown;
1317 reg->precise = env->subprog_cnt > 1 || !env->bpf_capable;
1318 __mark_reg_unbounded(reg);
1321 static void mark_reg_unknown(struct bpf_verifier_env *env,
1322 struct bpf_reg_state *regs, u32 regno)
1324 if (WARN_ON(regno >= MAX_BPF_REG)) {
1325 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
1326 /* Something bad happened, let's kill all regs except FP */
1327 for (regno = 0; regno < BPF_REG_FP; regno++)
1328 __mark_reg_not_init(env, regs + regno);
1331 __mark_reg_unknown(env, regs + regno);
1334 static void __mark_reg_not_init(const struct bpf_verifier_env *env,
1335 struct bpf_reg_state *reg)
1337 __mark_reg_unknown(env, reg);
1338 reg->type = NOT_INIT;
1341 static void mark_reg_not_init(struct bpf_verifier_env *env,
1342 struct bpf_reg_state *regs, u32 regno)
1344 if (WARN_ON(regno >= MAX_BPF_REG)) {
1345 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
1346 /* Something bad happened, let's kill all regs except FP */
1347 for (regno = 0; regno < BPF_REG_FP; regno++)
1348 __mark_reg_not_init(env, regs + regno);
1351 __mark_reg_not_init(env, regs + regno);
1354 #define DEF_NOT_SUBREG (0)
1355 static void init_reg_state(struct bpf_verifier_env *env,
1356 struct bpf_func_state *state)
1358 struct bpf_reg_state *regs = state->regs;
1361 for (i = 0; i < MAX_BPF_REG; i++) {
1362 mark_reg_not_init(env, regs, i);
1363 regs[i].live = REG_LIVE_NONE;
1364 regs[i].parent = NULL;
1365 regs[i].subreg_def = DEF_NOT_SUBREG;
1369 regs[BPF_REG_FP].type = PTR_TO_STACK;
1370 mark_reg_known_zero(env, regs, BPF_REG_FP);
1371 regs[BPF_REG_FP].frameno = state->frameno;
1374 #define BPF_MAIN_FUNC (-1)
1375 static void init_func_state(struct bpf_verifier_env *env,
1376 struct bpf_func_state *state,
1377 int callsite, int frameno, int subprogno)
1379 state->callsite = callsite;
1380 state->frameno = frameno;
1381 state->subprogno = subprogno;
1382 init_reg_state(env, state);
1386 SRC_OP, /* register is used as source operand */
1387 DST_OP, /* register is used as destination operand */
1388 DST_OP_NO_MARK /* same as above, check only, don't mark */
1391 static int cmp_subprogs(const void *a, const void *b)
1393 return ((struct bpf_subprog_info *)a)->start -
1394 ((struct bpf_subprog_info *)b)->start;
1397 static int find_subprog(struct bpf_verifier_env *env, int off)
1399 struct bpf_subprog_info *p;
1401 p = bsearch(&off, env->subprog_info, env->subprog_cnt,
1402 sizeof(env->subprog_info[0]), cmp_subprogs);
1405 return p - env->subprog_info;
1409 static int add_subprog(struct bpf_verifier_env *env, int off)
1411 int insn_cnt = env->prog->len;
1414 if (off >= insn_cnt || off < 0) {
1415 verbose(env, "call to invalid destination\n");
1418 ret = find_subprog(env, off);
1421 if (env->subprog_cnt >= BPF_MAX_SUBPROGS) {
1422 verbose(env, "too many subprograms\n");
1425 env->subprog_info[env->subprog_cnt++].start = off;
1426 sort(env->subprog_info, env->subprog_cnt,
1427 sizeof(env->subprog_info[0]), cmp_subprogs, NULL);
1431 static int check_subprogs(struct bpf_verifier_env *env)
1433 int i, ret, subprog_start, subprog_end, off, cur_subprog = 0;
1434 struct bpf_subprog_info *subprog = env->subprog_info;
1435 struct bpf_insn *insn = env->prog->insnsi;
1436 int insn_cnt = env->prog->len;
1438 /* Add entry function. */
1439 ret = add_subprog(env, 0);
1443 /* determine subprog starts. The end is one before the next starts */
1444 for (i = 0; i < insn_cnt; i++) {
1445 if (insn[i].code != (BPF_JMP | BPF_CALL))
1447 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1449 if (!env->bpf_capable) {
1451 "function calls to other bpf functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n");
1454 ret = add_subprog(env, i + insn[i].imm + 1);
1459 /* Add a fake 'exit' subprog which could simplify subprog iteration
1460 * logic. 'subprog_cnt' should not be increased.
1462 subprog[env->subprog_cnt].start = insn_cnt;
1464 if (env->log.level & BPF_LOG_LEVEL2)
1465 for (i = 0; i < env->subprog_cnt; i++)
1466 verbose(env, "func#%d @%d\n", i, subprog[i].start);
1468 /* now check that all jumps are within the same subprog */
1469 subprog_start = subprog[cur_subprog].start;
1470 subprog_end = subprog[cur_subprog + 1].start;
1471 for (i = 0; i < insn_cnt; i++) {
1472 u8 code = insn[i].code;
1474 if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32)
1476 if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
1478 off = i + insn[i].off + 1;
1479 if (off < subprog_start || off >= subprog_end) {
1480 verbose(env, "jump out of range from insn %d to %d\n", i, off);
1484 if (i == subprog_end - 1) {
1485 /* to avoid fall-through from one subprog into another
1486 * the last insn of the subprog should be either exit
1487 * or unconditional jump back
1489 if (code != (BPF_JMP | BPF_EXIT) &&
1490 code != (BPF_JMP | BPF_JA)) {
1491 verbose(env, "last insn is not an exit or jmp\n");
1494 subprog_start = subprog_end;
1496 if (cur_subprog < env->subprog_cnt)
1497 subprog_end = subprog[cur_subprog + 1].start;
1503 /* Parentage chain of this register (or stack slot) should take care of all
1504 * issues like callee-saved registers, stack slot allocation time, etc.
1506 static int mark_reg_read(struct bpf_verifier_env *env,
1507 const struct bpf_reg_state *state,
1508 struct bpf_reg_state *parent, u8 flag)
1510 bool writes = parent == state->parent; /* Observe write marks */
1514 /* if read wasn't screened by an earlier write ... */
1515 if (writes && state->live & REG_LIVE_WRITTEN)
1517 if (parent->live & REG_LIVE_DONE) {
1518 verbose(env, "verifier BUG type %s var_off %lld off %d\n",
1519 reg_type_str[parent->type],
1520 parent->var_off.value, parent->off);
1523 /* The first condition is more likely to be true than the
1524 * second, checked it first.
1526 if ((parent->live & REG_LIVE_READ) == flag ||
1527 parent->live & REG_LIVE_READ64)
1528 /* The parentage chain never changes and
1529 * this parent was already marked as LIVE_READ.
1530 * There is no need to keep walking the chain again and
1531 * keep re-marking all parents as LIVE_READ.
1532 * This case happens when the same register is read
1533 * multiple times without writes into it in-between.
1534 * Also, if parent has the stronger REG_LIVE_READ64 set,
1535 * then no need to set the weak REG_LIVE_READ32.
1538 /* ... then we depend on parent's value */
1539 parent->live |= flag;
1540 /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */
1541 if (flag == REG_LIVE_READ64)
1542 parent->live &= ~REG_LIVE_READ32;
1544 parent = state->parent;
1549 if (env->longest_mark_read_walk < cnt)
1550 env->longest_mark_read_walk = cnt;
1554 /* This function is supposed to be used by the following 32-bit optimization
1555 * code only. It returns TRUE if the source or destination register operates
1556 * on 64-bit, otherwise return FALSE.
1558 static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn,
1559 u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t)
1564 class = BPF_CLASS(code);
1566 if (class == BPF_JMP) {
1567 /* BPF_EXIT for "main" will reach here. Return TRUE
1572 if (op == BPF_CALL) {
1573 /* BPF to BPF call will reach here because of marking
1574 * caller saved clobber with DST_OP_NO_MARK for which we
1575 * don't care the register def because they are anyway
1576 * marked as NOT_INIT already.
1578 if (insn->src_reg == BPF_PSEUDO_CALL)
1580 /* Helper call will reach here because of arg type
1581 * check, conservatively return TRUE.
1590 if (class == BPF_ALU64 || class == BPF_JMP ||
1591 /* BPF_END always use BPF_ALU class. */
1592 (class == BPF_ALU && op == BPF_END && insn->imm == 64))
1595 if (class == BPF_ALU || class == BPF_JMP32)
1598 if (class == BPF_LDX) {
1600 return BPF_SIZE(code) == BPF_DW;
1601 /* LDX source must be ptr. */
1605 if (class == BPF_STX) {
1606 if (reg->type != SCALAR_VALUE)
1608 return BPF_SIZE(code) == BPF_DW;
1611 if (class == BPF_LD) {
1612 u8 mode = BPF_MODE(code);
1615 if (mode == BPF_IMM)
1618 /* Both LD_IND and LD_ABS return 32-bit data. */
1622 /* Implicit ctx ptr. */
1623 if (regno == BPF_REG_6)
1626 /* Explicit source could be any width. */
1630 if (class == BPF_ST)
1631 /* The only source register for BPF_ST is a ptr. */
1634 /* Conservatively return true at default. */
1638 /* Return TRUE if INSN doesn't have explicit value define. */
1639 static bool insn_no_def(struct bpf_insn *insn)
1641 u8 class = BPF_CLASS(insn->code);
1643 return (class == BPF_JMP || class == BPF_JMP32 ||
1644 class == BPF_STX || class == BPF_ST);
1647 /* Return TRUE if INSN has defined any 32-bit value explicitly. */
1648 static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn)
1650 if (insn_no_def(insn))
1653 return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP);
1656 static void mark_insn_zext(struct bpf_verifier_env *env,
1657 struct bpf_reg_state *reg)
1659 s32 def_idx = reg->subreg_def;
1661 if (def_idx == DEF_NOT_SUBREG)
1664 env->insn_aux_data[def_idx - 1].zext_dst = true;
1665 /* The dst will be zero extended, so won't be sub-register anymore. */
1666 reg->subreg_def = DEF_NOT_SUBREG;
1669 static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
1670 enum reg_arg_type t)
1672 struct bpf_verifier_state *vstate = env->cur_state;
1673 struct bpf_func_state *state = vstate->frame[vstate->curframe];
1674 struct bpf_insn *insn = env->prog->insnsi + env->insn_idx;
1675 struct bpf_reg_state *reg, *regs = state->regs;
1678 if (regno >= MAX_BPF_REG) {
1679 verbose(env, "R%d is invalid\n", regno);
1684 rw64 = is_reg64(env, insn, regno, reg, t);
1686 /* check whether register used as source operand can be read */
1687 if (reg->type == NOT_INIT) {
1688 verbose(env, "R%d !read_ok\n", regno);
1691 /* We don't need to worry about FP liveness because it's read-only */
1692 if (regno == BPF_REG_FP)
1696 mark_insn_zext(env, reg);
1698 return mark_reg_read(env, reg, reg->parent,
1699 rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32);
1701 /* check whether register used as dest operand can be written to */
1702 if (regno == BPF_REG_FP) {
1703 verbose(env, "frame pointer is read only\n");
1706 reg->live |= REG_LIVE_WRITTEN;
1707 reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1;
1709 mark_reg_unknown(env, regs, regno);
1714 /* for any branch, call, exit record the history of jmps in the given state */
1715 static int push_jmp_history(struct bpf_verifier_env *env,
1716 struct bpf_verifier_state *cur)
1718 u32 cnt = cur->jmp_history_cnt;
1719 struct bpf_idx_pair *p;
1722 p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER);
1725 p[cnt - 1].idx = env->insn_idx;
1726 p[cnt - 1].prev_idx = env->prev_insn_idx;
1727 cur->jmp_history = p;
1728 cur->jmp_history_cnt = cnt;
1732 /* Backtrack one insn at a time. If idx is not at the top of recorded
1733 * history then previous instruction came from straight line execution.
1735 static int get_prev_insn_idx(struct bpf_verifier_state *st, int i,
1740 if (cnt && st->jmp_history[cnt - 1].idx == i) {
1741 i = st->jmp_history[cnt - 1].prev_idx;
1749 /* For given verifier state backtrack_insn() is called from the last insn to
1750 * the first insn. Its purpose is to compute a bitmask of registers and
1751 * stack slots that needs precision in the parent verifier state.
1753 static int backtrack_insn(struct bpf_verifier_env *env, int idx,
1754 u32 *reg_mask, u64 *stack_mask)
1756 const struct bpf_insn_cbs cbs = {
1757 .cb_print = verbose,
1758 .private_data = env,
1760 struct bpf_insn *insn = env->prog->insnsi + idx;
1761 u8 class = BPF_CLASS(insn->code);
1762 u8 opcode = BPF_OP(insn->code);
1763 u8 mode = BPF_MODE(insn->code);
1764 u32 dreg = 1u << insn->dst_reg;
1765 u32 sreg = 1u << insn->src_reg;
1768 if (insn->code == 0)
1770 if (env->log.level & BPF_LOG_LEVEL) {
1771 verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask);
1772 verbose(env, "%d: ", idx);
1773 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
1776 if (class == BPF_ALU || class == BPF_ALU64) {
1777 if (!(*reg_mask & dreg))
1779 if (opcode == BPF_MOV) {
1780 if (BPF_SRC(insn->code) == BPF_X) {
1782 * dreg needs precision after this insn
1783 * sreg needs precision before this insn
1789 * dreg needs precision after this insn.
1790 * Corresponding register is already marked
1791 * as precise=true in this verifier state.
1792 * No further markings in parent are necessary
1797 if (BPF_SRC(insn->code) == BPF_X) {
1799 * both dreg and sreg need precision
1804 * dreg still needs precision before this insn
1807 } else if (class == BPF_LDX) {
1808 if (!(*reg_mask & dreg))
1812 /* scalars can only be spilled into stack w/o losing precision.
1813 * Load from any other memory can be zero extended.
1814 * The desire to keep that precision is already indicated
1815 * by 'precise' mark in corresponding register of this state.
1816 * No further tracking necessary.
1818 if (insn->src_reg != BPF_REG_FP)
1820 if (BPF_SIZE(insn->code) != BPF_DW)
1823 /* dreg = *(u64 *)[fp - off] was a fill from the stack.
1824 * that [fp - off] slot contains scalar that needs to be
1825 * tracked with precision
1827 spi = (-insn->off - 1) / BPF_REG_SIZE;
1829 verbose(env, "BUG spi %d\n", spi);
1830 WARN_ONCE(1, "verifier backtracking bug");
1833 *stack_mask |= 1ull << spi;
1834 } else if (class == BPF_STX || class == BPF_ST) {
1835 if (*reg_mask & dreg)
1836 /* stx & st shouldn't be using _scalar_ dst_reg
1837 * to access memory. It means backtracking
1838 * encountered a case of pointer subtraction.
1841 /* scalars can only be spilled into stack */
1842 if (insn->dst_reg != BPF_REG_FP)
1844 if (BPF_SIZE(insn->code) != BPF_DW)
1846 spi = (-insn->off - 1) / BPF_REG_SIZE;
1848 verbose(env, "BUG spi %d\n", spi);
1849 WARN_ONCE(1, "verifier backtracking bug");
1852 if (!(*stack_mask & (1ull << spi)))
1854 *stack_mask &= ~(1ull << spi);
1855 if (class == BPF_STX)
1857 } else if (class == BPF_JMP || class == BPF_JMP32) {
1858 if (opcode == BPF_CALL) {
1859 if (insn->src_reg == BPF_PSEUDO_CALL)
1861 /* regular helper call sets R0 */
1863 if (*reg_mask & 0x3f) {
1864 /* if backtracing was looking for registers R1-R5
1865 * they should have been found already.
1867 verbose(env, "BUG regs %x\n", *reg_mask);
1868 WARN_ONCE(1, "verifier backtracking bug");
1871 } else if (opcode == BPF_EXIT) {
1874 } else if (class == BPF_LD) {
1875 if (!(*reg_mask & dreg))
1878 /* It's ld_imm64 or ld_abs or ld_ind.
1879 * For ld_imm64 no further tracking of precision
1880 * into parent is necessary
1882 if (mode == BPF_IND || mode == BPF_ABS)
1883 /* to be analyzed */
1889 /* the scalar precision tracking algorithm:
1890 * . at the start all registers have precise=false.
1891 * . scalar ranges are tracked as normal through alu and jmp insns.
1892 * . once precise value of the scalar register is used in:
1893 * . ptr + scalar alu
1894 * . if (scalar cond K|scalar)
1895 * . helper_call(.., scalar, ...) where ARG_CONST is expected
1896 * backtrack through the verifier states and mark all registers and
1897 * stack slots with spilled constants that these scalar regisers
1898 * should be precise.
1899 * . during state pruning two registers (or spilled stack slots)
1900 * are equivalent if both are not precise.
1902 * Note the verifier cannot simply walk register parentage chain,
1903 * since many different registers and stack slots could have been
1904 * used to compute single precise scalar.
1906 * The approach of starting with precise=true for all registers and then
1907 * backtrack to mark a register as not precise when the verifier detects
1908 * that program doesn't care about specific value (e.g., when helper
1909 * takes register as ARG_ANYTHING parameter) is not safe.
1911 * It's ok to walk single parentage chain of the verifier states.
1912 * It's possible that this backtracking will go all the way till 1st insn.
1913 * All other branches will be explored for needing precision later.
1915 * The backtracking needs to deal with cases like:
1916 * R8=map_value(id=0,off=0,ks=4,vs=1952,imm=0) R9_w=map_value(id=0,off=40,ks=4,vs=1952,imm=0)
1919 * if r5 > 0x79f goto pc+7
1920 * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff))
1923 * call bpf_perf_event_output#25
1924 * where .arg5_type = ARG_CONST_SIZE_OR_ZERO
1928 * call foo // uses callee's r6 inside to compute r0
1932 * to track above reg_mask/stack_mask needs to be independent for each frame.
1934 * Also if parent's curframe > frame where backtracking started,
1935 * the verifier need to mark registers in both frames, otherwise callees
1936 * may incorrectly prune callers. This is similar to
1937 * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences")
1939 * For now backtracking falls back into conservative marking.
1941 static void mark_all_scalars_precise(struct bpf_verifier_env *env,
1942 struct bpf_verifier_state *st)
1944 struct bpf_func_state *func;
1945 struct bpf_reg_state *reg;
1948 /* big hammer: mark all scalars precise in this path.
1949 * pop_stack may still get !precise scalars.
1951 for (; st; st = st->parent)
1952 for (i = 0; i <= st->curframe; i++) {
1953 func = st->frame[i];
1954 for (j = 0; j < BPF_REG_FP; j++) {
1955 reg = &func->regs[j];
1956 if (reg->type != SCALAR_VALUE)
1958 reg->precise = true;
1960 for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) {
1961 if (func->stack[j].slot_type[0] != STACK_SPILL)
1963 reg = &func->stack[j].spilled_ptr;
1964 if (reg->type != SCALAR_VALUE)
1966 reg->precise = true;
1971 static int __mark_chain_precision(struct bpf_verifier_env *env, int regno,
1974 struct bpf_verifier_state *st = env->cur_state;
1975 int first_idx = st->first_insn_idx;
1976 int last_idx = env->insn_idx;
1977 struct bpf_func_state *func;
1978 struct bpf_reg_state *reg;
1979 u32 reg_mask = regno >= 0 ? 1u << regno : 0;
1980 u64 stack_mask = spi >= 0 ? 1ull << spi : 0;
1981 bool skip_first = true;
1982 bool new_marks = false;
1985 if (!env->bpf_capable)
1988 func = st->frame[st->curframe];
1990 reg = &func->regs[regno];
1991 if (reg->type != SCALAR_VALUE) {
1992 WARN_ONCE(1, "backtracing misuse");
1999 reg->precise = true;
2003 if (func->stack[spi].slot_type[0] != STACK_SPILL) {
2007 reg = &func->stack[spi].spilled_ptr;
2008 if (reg->type != SCALAR_VALUE) {
2016 reg->precise = true;
2022 if (!reg_mask && !stack_mask)
2025 DECLARE_BITMAP(mask, 64);
2026 u32 history = st->jmp_history_cnt;
2028 if (env->log.level & BPF_LOG_LEVEL)
2029 verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx);
2030 for (i = last_idx;;) {
2035 err = backtrack_insn(env, i, ®_mask, &stack_mask);
2037 if (err == -ENOTSUPP) {
2038 mark_all_scalars_precise(env, st);
2043 if (!reg_mask && !stack_mask)
2044 /* Found assignment(s) into tracked register in this state.
2045 * Since this state is already marked, just return.
2046 * Nothing to be tracked further in the parent state.
2051 i = get_prev_insn_idx(st, i, &history);
2052 if (i >= env->prog->len) {
2053 /* This can happen if backtracking reached insn 0
2054 * and there are still reg_mask or stack_mask
2056 * It means the backtracking missed the spot where
2057 * particular register was initialized with a constant.
2059 verbose(env, "BUG backtracking idx %d\n", i);
2060 WARN_ONCE(1, "verifier backtracking bug");
2069 func = st->frame[st->curframe];
2070 bitmap_from_u64(mask, reg_mask);
2071 for_each_set_bit(i, mask, 32) {
2072 reg = &func->regs[i];
2073 if (reg->type != SCALAR_VALUE) {
2074 reg_mask &= ~(1u << i);
2079 reg->precise = true;
2082 bitmap_from_u64(mask, stack_mask);
2083 for_each_set_bit(i, mask, 64) {
2084 if (i >= func->allocated_stack / BPF_REG_SIZE) {
2085 /* the sequence of instructions:
2087 * 3: (7b) *(u64 *)(r3 -8) = r0
2088 * 4: (79) r4 = *(u64 *)(r10 -8)
2089 * doesn't contain jmps. It's backtracked
2090 * as a single block.
2091 * During backtracking insn 3 is not recognized as
2092 * stack access, so at the end of backtracking
2093 * stack slot fp-8 is still marked in stack_mask.
2094 * However the parent state may not have accessed
2095 * fp-8 and it's "unallocated" stack space.
2096 * In such case fallback to conservative.
2098 mark_all_scalars_precise(env, st);
2102 if (func->stack[i].slot_type[0] != STACK_SPILL) {
2103 stack_mask &= ~(1ull << i);
2106 reg = &func->stack[i].spilled_ptr;
2107 if (reg->type != SCALAR_VALUE) {
2108 stack_mask &= ~(1ull << i);
2113 reg->precise = true;
2115 if (env->log.level & BPF_LOG_LEVEL) {
2116 print_verifier_state(env, func);
2117 verbose(env, "parent %s regs=%x stack=%llx marks\n",
2118 new_marks ? "didn't have" : "already had",
2119 reg_mask, stack_mask);
2122 if (!reg_mask && !stack_mask)
2127 last_idx = st->last_insn_idx;
2128 first_idx = st->first_insn_idx;
2133 static int mark_chain_precision(struct bpf_verifier_env *env, int regno)
2135 return __mark_chain_precision(env, regno, -1);
2138 static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi)
2140 return __mark_chain_precision(env, -1, spi);
2143 static bool is_spillable_regtype(enum bpf_reg_type type)
2146 case PTR_TO_MAP_VALUE:
2147 case PTR_TO_MAP_VALUE_OR_NULL:
2151 case PTR_TO_PACKET_META:
2152 case PTR_TO_PACKET_END:
2153 case PTR_TO_FLOW_KEYS:
2154 case CONST_PTR_TO_MAP:
2156 case PTR_TO_SOCKET_OR_NULL:
2157 case PTR_TO_SOCK_COMMON:
2158 case PTR_TO_SOCK_COMMON_OR_NULL:
2159 case PTR_TO_TCP_SOCK:
2160 case PTR_TO_TCP_SOCK_OR_NULL:
2161 case PTR_TO_XDP_SOCK:
2163 case PTR_TO_BTF_ID_OR_NULL:
2170 /* Does this register contain a constant zero? */
2171 static bool register_is_null(struct bpf_reg_state *reg)
2173 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
2176 static bool register_is_const(struct bpf_reg_state *reg)
2178 return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off);
2181 static bool __is_pointer_value(bool allow_ptr_leaks,
2182 const struct bpf_reg_state *reg)
2184 if (allow_ptr_leaks)
2187 return reg->type != SCALAR_VALUE;
2190 static void save_register_state(struct bpf_func_state *state,
2191 int spi, struct bpf_reg_state *reg)
2195 state->stack[spi].spilled_ptr = *reg;
2196 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
2198 for (i = 0; i < BPF_REG_SIZE; i++)
2199 state->stack[spi].slot_type[i] = STACK_SPILL;
2202 /* check_stack_read/write functions track spill/fill of registers,
2203 * stack boundary and alignment are checked in check_mem_access()
2205 static int check_stack_write(struct bpf_verifier_env *env,
2206 struct bpf_func_state *state, /* func where register points to */
2207 int off, int size, int value_regno, int insn_idx)
2209 struct bpf_func_state *cur; /* state of the current function */
2210 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
2211 u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg;
2212 struct bpf_reg_state *reg = NULL;
2214 err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE),
2215 state->acquired_refs, true);
2218 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
2219 * so it's aligned access and [off, off + size) are within stack limits
2221 if (!env->allow_ptr_leaks &&
2222 state->stack[spi].slot_type[0] == STACK_SPILL &&
2223 size != BPF_REG_SIZE) {
2224 verbose(env, "attempt to corrupt spilled pointer on stack\n");
2228 cur = env->cur_state->frame[env->cur_state->curframe];
2229 if (value_regno >= 0)
2230 reg = &cur->regs[value_regno];
2232 if (reg && size == BPF_REG_SIZE && register_is_const(reg) &&
2233 !register_is_null(reg) && env->bpf_capable) {
2234 if (dst_reg != BPF_REG_FP) {
2235 /* The backtracking logic can only recognize explicit
2236 * stack slot address like [fp - 8]. Other spill of
2237 * scalar via different register has to be conervative.
2238 * Backtrack from here and mark all registers as precise
2239 * that contributed into 'reg' being a constant.
2241 err = mark_chain_precision(env, value_regno);
2245 save_register_state(state, spi, reg);
2246 } else if (reg && is_spillable_regtype(reg->type)) {
2247 /* register containing pointer is being spilled into stack */
2248 if (size != BPF_REG_SIZE) {
2249 verbose_linfo(env, insn_idx, "; ");
2250 verbose(env, "invalid size of register spill\n");
2254 if (state != cur && reg->type == PTR_TO_STACK) {
2255 verbose(env, "cannot spill pointers to stack into stack frame of the caller\n");
2259 if (!env->bypass_spec_v4) {
2260 bool sanitize = false;
2262 if (state->stack[spi].slot_type[0] == STACK_SPILL &&
2263 register_is_const(&state->stack[spi].spilled_ptr))
2265 for (i = 0; i < BPF_REG_SIZE; i++)
2266 if (state->stack[spi].slot_type[i] == STACK_MISC) {
2271 int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off;
2272 int soff = (-spi - 1) * BPF_REG_SIZE;
2274 /* detected reuse of integer stack slot with a pointer
2275 * which means either llvm is reusing stack slot or
2276 * an attacker is trying to exploit CVE-2018-3639
2277 * (speculative store bypass)
2278 * Have to sanitize that slot with preemptive
2281 if (*poff && *poff != soff) {
2282 /* disallow programs where single insn stores
2283 * into two different stack slots, since verifier
2284 * cannot sanitize them
2287 "insn %d cannot access two stack slots fp%d and fp%d",
2288 insn_idx, *poff, soff);
2294 save_register_state(state, spi, reg);
2296 u8 type = STACK_MISC;
2298 /* regular write of data into stack destroys any spilled ptr */
2299 state->stack[spi].spilled_ptr.type = NOT_INIT;
2300 /* Mark slots as STACK_MISC if they belonged to spilled ptr. */
2301 if (state->stack[spi].slot_type[0] == STACK_SPILL)
2302 for (i = 0; i < BPF_REG_SIZE; i++)
2303 state->stack[spi].slot_type[i] = STACK_MISC;
2305 /* only mark the slot as written if all 8 bytes were written
2306 * otherwise read propagation may incorrectly stop too soon
2307 * when stack slots are partially written.
2308 * This heuristic means that read propagation will be
2309 * conservative, since it will add reg_live_read marks
2310 * to stack slots all the way to first state when programs
2311 * writes+reads less than 8 bytes
2313 if (size == BPF_REG_SIZE)
2314 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
2316 /* when we zero initialize stack slots mark them as such */
2317 if (reg && register_is_null(reg)) {
2318 /* backtracking doesn't work for STACK_ZERO yet. */
2319 err = mark_chain_precision(env, value_regno);
2325 /* Mark slots affected by this stack write. */
2326 for (i = 0; i < size; i++)
2327 state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
2333 static int check_stack_read(struct bpf_verifier_env *env,
2334 struct bpf_func_state *reg_state /* func where register points to */,
2335 int off, int size, int value_regno)
2337 struct bpf_verifier_state *vstate = env->cur_state;
2338 struct bpf_func_state *state = vstate->frame[vstate->curframe];
2339 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
2340 struct bpf_reg_state *reg;
2343 if (reg_state->allocated_stack <= slot) {
2344 verbose(env, "invalid read from stack off %d+0 size %d\n",
2348 stype = reg_state->stack[spi].slot_type;
2349 reg = ®_state->stack[spi].spilled_ptr;
2351 if (stype[0] == STACK_SPILL) {
2352 if (size != BPF_REG_SIZE) {
2353 if (reg->type != SCALAR_VALUE) {
2354 verbose_linfo(env, env->insn_idx, "; ");
2355 verbose(env, "invalid size of register fill\n");
2358 if (value_regno >= 0) {
2359 mark_reg_unknown(env, state->regs, value_regno);
2360 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
2362 mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64);
2365 for (i = 1; i < BPF_REG_SIZE; i++) {
2366 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
2367 verbose(env, "corrupted spill memory\n");
2372 if (value_regno >= 0) {
2373 /* restore register state from stack */
2374 state->regs[value_regno] = *reg;
2375 /* mark reg as written since spilled pointer state likely
2376 * has its liveness marks cleared by is_state_visited()
2377 * which resets stack/reg liveness for state transitions
2379 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
2380 } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) {
2381 /* If value_regno==-1, the caller is asking us whether
2382 * it is acceptable to use this value as a SCALAR_VALUE
2384 * We must not allow unprivileged callers to do that
2385 * with spilled pointers.
2387 verbose(env, "leaking pointer from stack off %d\n",
2391 mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64);
2395 for (i = 0; i < size; i++) {
2396 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC)
2398 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) {
2402 verbose(env, "invalid read from stack off %d+%d size %d\n",
2406 mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64);
2407 if (value_regno >= 0) {
2408 if (zeros == size) {
2409 /* any size read into register is zero extended,
2410 * so the whole register == const_zero
2412 __mark_reg_const_zero(&state->regs[value_regno]);
2413 /* backtracking doesn't support STACK_ZERO yet,
2414 * so mark it precise here, so that later
2415 * backtracking can stop here.
2416 * Backtracking may not need this if this register
2417 * doesn't participate in pointer adjustment.
2418 * Forward propagation of precise flag is not
2419 * necessary either. This mark is only to stop
2420 * backtracking. Any register that contributed
2421 * to const 0 was marked precise before spill.
2423 state->regs[value_regno].precise = true;
2425 /* have read misc data from the stack */
2426 mark_reg_unknown(env, state->regs, value_regno);
2428 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
2434 static int check_stack_access(struct bpf_verifier_env *env,
2435 const struct bpf_reg_state *reg,
2438 /* Stack accesses must be at a fixed offset, so that we
2439 * can determine what type of data were returned. See
2440 * check_stack_read().
2442 if (!tnum_is_const(reg->var_off)) {
2445 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2446 verbose(env, "variable stack access var_off=%s off=%d size=%d\n",
2451 if (off >= 0 || off < -MAX_BPF_STACK) {
2452 verbose(env, "invalid stack off=%d size=%d\n", off, size);
2459 static int check_map_access_type(struct bpf_verifier_env *env, u32 regno,
2460 int off, int size, enum bpf_access_type type)
2462 struct bpf_reg_state *regs = cur_regs(env);
2463 struct bpf_map *map = regs[regno].map_ptr;
2464 u32 cap = bpf_map_flags_to_cap(map);
2466 if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) {
2467 verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n",
2468 map->value_size, off, size);
2472 if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) {
2473 verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n",
2474 map->value_size, off, size);
2481 /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */
2482 static int __check_mem_access(struct bpf_verifier_env *env, int regno,
2483 int off, int size, u32 mem_size,
2484 bool zero_size_allowed)
2486 bool size_ok = size > 0 || (size == 0 && zero_size_allowed);
2487 struct bpf_reg_state *reg;
2489 if (off >= 0 && size_ok && (u64)off + size <= mem_size)
2492 reg = &cur_regs(env)[regno];
2493 switch (reg->type) {
2494 case PTR_TO_MAP_VALUE:
2495 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
2496 mem_size, off, size);
2499 case PTR_TO_PACKET_META:
2500 case PTR_TO_PACKET_END:
2501 verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
2502 off, size, regno, reg->id, off, mem_size);
2506 verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n",
2507 mem_size, off, size);
2513 /* check read/write into a memory region with possible variable offset */
2514 static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno,
2515 int off, int size, u32 mem_size,
2516 bool zero_size_allowed)
2518 struct bpf_verifier_state *vstate = env->cur_state;
2519 struct bpf_func_state *state = vstate->frame[vstate->curframe];
2520 struct bpf_reg_state *reg = &state->regs[regno];
2523 /* We may have adjusted the register pointing to memory region, so we
2524 * need to try adding each of min_value and max_value to off
2525 * to make sure our theoretical access will be safe.
2527 if (env->log.level & BPF_LOG_LEVEL)
2528 print_verifier_state(env, state);
2530 /* The minimum value is only important with signed
2531 * comparisons where we can't assume the floor of a
2532 * value is 0. If we are using signed variables for our
2533 * index'es we need to make sure that whatever we use
2534 * will have a set floor within our range.
2536 if (reg->smin_value < 0 &&
2537 (reg->smin_value == S64_MIN ||
2538 (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) ||
2539 reg->smin_value + off < 0)) {
2540 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
2544 err = __check_mem_access(env, regno, reg->smin_value + off, size,
2545 mem_size, zero_size_allowed);
2547 verbose(env, "R%d min value is outside of the allowed memory range\n",
2552 /* If we haven't set a max value then we need to bail since we can't be
2553 * sure we won't do bad things.
2554 * If reg->umax_value + off could overflow, treat that as unbounded too.
2556 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
2557 verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n",
2561 err = __check_mem_access(env, regno, reg->umax_value + off, size,
2562 mem_size, zero_size_allowed);
2564 verbose(env, "R%d max value is outside of the allowed memory range\n",
2572 /* check read/write into a map element with possible variable offset */
2573 static int check_map_access(struct bpf_verifier_env *env, u32 regno,
2574 int off, int size, bool zero_size_allowed)
2576 struct bpf_verifier_state *vstate = env->cur_state;
2577 struct bpf_func_state *state = vstate->frame[vstate->curframe];
2578 struct bpf_reg_state *reg = &state->regs[regno];
2579 struct bpf_map *map = reg->map_ptr;
2582 err = check_mem_region_access(env, regno, off, size, map->value_size,
2587 if (map_value_has_spin_lock(map)) {
2588 u32 lock = map->spin_lock_off;
2590 /* if any part of struct bpf_spin_lock can be touched by
2591 * load/store reject this program.
2592 * To check that [x1, x2) overlaps with [y1, y2)
2593 * it is sufficient to check x1 < y2 && y1 < x2.
2595 if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) &&
2596 lock < reg->umax_value + off + size) {
2597 verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n");
2604 #define MAX_PACKET_OFF 0xffff
2606 static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
2607 const struct bpf_call_arg_meta *meta,
2608 enum bpf_access_type t)
2610 switch (env->prog->type) {
2611 /* Program types only with direct read access go here! */
2612 case BPF_PROG_TYPE_LWT_IN:
2613 case BPF_PROG_TYPE_LWT_OUT:
2614 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2615 case BPF_PROG_TYPE_SK_REUSEPORT:
2616 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2617 case BPF_PROG_TYPE_CGROUP_SKB:
2622 /* Program types with direct read + write access go here! */
2623 case BPF_PROG_TYPE_SCHED_CLS:
2624 case BPF_PROG_TYPE_SCHED_ACT:
2625 case BPF_PROG_TYPE_XDP:
2626 case BPF_PROG_TYPE_LWT_XMIT:
2627 case BPF_PROG_TYPE_SK_SKB:
2628 case BPF_PROG_TYPE_SK_MSG:
2630 return meta->pkt_access;
2632 env->seen_direct_write = true;
2635 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2637 env->seen_direct_write = true;
2646 static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
2647 int size, bool zero_size_allowed)
2649 struct bpf_reg_state *regs = cur_regs(env);
2650 struct bpf_reg_state *reg = ®s[regno];
2653 /* We may have added a variable offset to the packet pointer; but any
2654 * reg->range we have comes after that. We are only checking the fixed
2658 /* We don't allow negative numbers, because we aren't tracking enough
2659 * detail to prove they're safe.
2661 if (reg->smin_value < 0) {
2662 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
2666 err = __check_mem_access(env, regno, off, size, reg->range,
2669 verbose(env, "R%d offset is outside of the packet\n", regno);
2673 /* __check_mem_access has made sure "off + size - 1" is within u16.
2674 * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff,
2675 * otherwise find_good_pkt_pointers would have refused to set range info
2676 * that __check_mem_access would have rejected this pkt access.
2677 * Therefore, "off + reg->umax_value + size - 1" won't overflow u32.
2679 env->prog->aux->max_pkt_offset =
2680 max_t(u32, env->prog->aux->max_pkt_offset,
2681 off + reg->umax_value + size - 1);
2686 /* check access to 'struct bpf_context' fields. Supports fixed offsets only */
2687 static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
2688 enum bpf_access_type t, enum bpf_reg_type *reg_type,
2691 struct bpf_insn_access_aux info = {
2692 .reg_type = *reg_type,
2696 if (env->ops->is_valid_access &&
2697 env->ops->is_valid_access(off, size, t, env->prog, &info)) {
2698 /* A non zero info.ctx_field_size indicates that this field is a
2699 * candidate for later verifier transformation to load the whole
2700 * field and then apply a mask when accessed with a narrower
2701 * access than actual ctx access size. A zero info.ctx_field_size
2702 * will only allow for whole field access and rejects any other
2703 * type of narrower access.
2705 *reg_type = info.reg_type;
2707 if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL)
2708 *btf_id = info.btf_id;
2710 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
2711 /* remember the offset of last byte accessed in ctx */
2712 if (env->prog->aux->max_ctx_offset < off + size)
2713 env->prog->aux->max_ctx_offset = off + size;
2717 verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
2721 static int check_flow_keys_access(struct bpf_verifier_env *env, int off,
2724 if (size < 0 || off < 0 ||
2725 (u64)off + size > sizeof(struct bpf_flow_keys)) {
2726 verbose(env, "invalid access to flow keys off=%d size=%d\n",
2733 static int check_sock_access(struct bpf_verifier_env *env, int insn_idx,
2734 u32 regno, int off, int size,
2735 enum bpf_access_type t)
2737 struct bpf_reg_state *regs = cur_regs(env);
2738 struct bpf_reg_state *reg = ®s[regno];
2739 struct bpf_insn_access_aux info = {};
2742 if (reg->smin_value < 0) {
2743 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
2748 switch (reg->type) {
2749 case PTR_TO_SOCK_COMMON:
2750 valid = bpf_sock_common_is_valid_access(off, size, t, &info);
2753 valid = bpf_sock_is_valid_access(off, size, t, &info);
2755 case PTR_TO_TCP_SOCK:
2756 valid = bpf_tcp_sock_is_valid_access(off, size, t, &info);
2758 case PTR_TO_XDP_SOCK:
2759 valid = bpf_xdp_sock_is_valid_access(off, size, t, &info);
2767 env->insn_aux_data[insn_idx].ctx_field_size =
2768 info.ctx_field_size;
2772 verbose(env, "R%d invalid %s access off=%d size=%d\n",
2773 regno, reg_type_str[reg->type], off, size);
2778 static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)
2780 return cur_regs(env) + regno;
2783 static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
2785 return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno));
2788 static bool is_ctx_reg(struct bpf_verifier_env *env, int regno)
2790 const struct bpf_reg_state *reg = reg_state(env, regno);
2792 return reg->type == PTR_TO_CTX;
2795 static bool is_sk_reg(struct bpf_verifier_env *env, int regno)
2797 const struct bpf_reg_state *reg = reg_state(env, regno);
2799 return type_is_sk_pointer(reg->type);
2802 static bool is_pkt_reg(struct bpf_verifier_env *env, int regno)
2804 const struct bpf_reg_state *reg = reg_state(env, regno);
2806 return type_is_pkt_pointer(reg->type);
2809 static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno)
2811 const struct bpf_reg_state *reg = reg_state(env, regno);
2813 /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */
2814 return reg->type == PTR_TO_FLOW_KEYS;
2817 static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
2818 const struct bpf_reg_state *reg,
2819 int off, int size, bool strict)
2821 struct tnum reg_off;
2824 /* Byte size accesses are always allowed. */
2825 if (!strict || size == 1)
2828 /* For platforms that do not have a Kconfig enabling
2829 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
2830 * NET_IP_ALIGN is universally set to '2'. And on platforms
2831 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
2832 * to this code only in strict mode where we want to emulate
2833 * the NET_IP_ALIGN==2 checking. Therefore use an
2834 * unconditional IP align value of '2'.
2838 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
2839 if (!tnum_is_aligned(reg_off, size)) {
2842 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2844 "misaligned packet access off %d+%s+%d+%d size %d\n",
2845 ip_align, tn_buf, reg->off, off, size);
2852 static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
2853 const struct bpf_reg_state *reg,
2854 const char *pointer_desc,
2855 int off, int size, bool strict)
2857 struct tnum reg_off;
2859 /* Byte size accesses are always allowed. */
2860 if (!strict || size == 1)
2863 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
2864 if (!tnum_is_aligned(reg_off, size)) {
2867 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2868 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
2869 pointer_desc, tn_buf, reg->off, off, size);
2876 static int check_ptr_alignment(struct bpf_verifier_env *env,
2877 const struct bpf_reg_state *reg, int off,
2878 int size, bool strict_alignment_once)
2880 bool strict = env->strict_alignment || strict_alignment_once;
2881 const char *pointer_desc = "";
2883 switch (reg->type) {
2885 case PTR_TO_PACKET_META:
2886 /* Special case, because of NET_IP_ALIGN. Given metadata sits
2887 * right in front, treat it the very same way.
2889 return check_pkt_ptr_alignment(env, reg, off, size, strict);
2890 case PTR_TO_FLOW_KEYS:
2891 pointer_desc = "flow keys ";
2893 case PTR_TO_MAP_VALUE:
2894 pointer_desc = "value ";
2897 pointer_desc = "context ";
2900 pointer_desc = "stack ";
2901 /* The stack spill tracking logic in check_stack_write()
2902 * and check_stack_read() relies on stack accesses being
2908 pointer_desc = "sock ";
2910 case PTR_TO_SOCK_COMMON:
2911 pointer_desc = "sock_common ";
2913 case PTR_TO_TCP_SOCK:
2914 pointer_desc = "tcp_sock ";
2916 case PTR_TO_XDP_SOCK:
2917 pointer_desc = "xdp_sock ";
2922 return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
2926 static int update_stack_depth(struct bpf_verifier_env *env,
2927 const struct bpf_func_state *func,
2930 u16 stack = env->subprog_info[func->subprogno].stack_depth;
2935 /* update known max for given subprogram */
2936 env->subprog_info[func->subprogno].stack_depth = -off;
2940 /* starting from main bpf function walk all instructions of the function
2941 * and recursively walk all callees that given function can call.
2942 * Ignore jump and exit insns.
2943 * Since recursion is prevented by check_cfg() this algorithm
2944 * only needs a local stack of MAX_CALL_FRAMES to remember callsites
2946 static int check_max_stack_depth(struct bpf_verifier_env *env)
2948 int depth = 0, frame = 0, idx = 0, i = 0, subprog_end;
2949 struct bpf_subprog_info *subprog = env->subprog_info;
2950 struct bpf_insn *insn = env->prog->insnsi;
2951 int ret_insn[MAX_CALL_FRAMES];
2952 int ret_prog[MAX_CALL_FRAMES];
2955 /* round up to 32-bytes, since this is granularity
2956 * of interpreter stack size
2958 depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
2959 if (depth > MAX_BPF_STACK) {
2960 verbose(env, "combined stack size of %d calls is %d. Too large\n",
2965 subprog_end = subprog[idx + 1].start;
2966 for (; i < subprog_end; i++) {
2967 if (insn[i].code != (BPF_JMP | BPF_CALL))
2969 if (insn[i].src_reg != BPF_PSEUDO_CALL)
2971 /* remember insn and function to return to */
2972 ret_insn[frame] = i + 1;
2973 ret_prog[frame] = idx;
2975 /* find the callee */
2976 i = i + insn[i].imm + 1;
2977 idx = find_subprog(env, i);
2979 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
2984 if (frame >= MAX_CALL_FRAMES) {
2985 verbose(env, "the call stack of %d frames is too deep !\n",
2991 /* end of for() loop means the last insn of the 'subprog'
2992 * was reached. Doesn't matter whether it was JA or EXIT
2996 depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
2998 i = ret_insn[frame];
2999 idx = ret_prog[frame];
3003 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
3004 static int get_callee_stack_depth(struct bpf_verifier_env *env,
3005 const struct bpf_insn *insn, int idx)
3007 int start = idx + insn->imm + 1, subprog;
3009 subprog = find_subprog(env, start);
3011 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
3015 return env->subprog_info[subprog].stack_depth;
3019 int check_ctx_reg(struct bpf_verifier_env *env,
3020 const struct bpf_reg_state *reg, int regno)
3022 /* Access to ctx or passing it to a helper is only allowed in
3023 * its original, unmodified form.
3027 verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
3032 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
3035 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
3036 verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
3043 static int check_tp_buffer_access(struct bpf_verifier_env *env,
3044 const struct bpf_reg_state *reg,
3045 int regno, int off, int size)
3049 "R%d invalid tracepoint buffer access: off=%d, size=%d",
3053 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
3056 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
3058 "R%d invalid variable buffer offset: off=%d, var_off=%s",
3059 regno, off, tn_buf);
3062 if (off + size > env->prog->aux->max_tp_access)
3063 env->prog->aux->max_tp_access = off + size;
3068 /* BPF architecture zero extends alu32 ops into 64-bit registesr */
3069 static void zext_32_to_64(struct bpf_reg_state *reg)
3071 reg->var_off = tnum_subreg(reg->var_off);
3072 __reg_assign_32_into_64(reg);
3075 /* truncate register to smaller size (in bytes)
3076 * must be called with size < BPF_REG_SIZE
3078 static void coerce_reg_to_size(struct bpf_reg_state *reg, int size)
3082 /* clear high bits in bit representation */
3083 reg->var_off = tnum_cast(reg->var_off, size);
3085 /* fix arithmetic bounds */
3086 mask = ((u64)1 << (size * 8)) - 1;
3087 if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) {
3088 reg->umin_value &= mask;
3089 reg->umax_value &= mask;
3091 reg->umin_value = 0;
3092 reg->umax_value = mask;
3094 reg->smin_value = reg->umin_value;
3095 reg->smax_value = reg->umax_value;
3097 /* If size is smaller than 32bit register the 32bit register
3098 * values are also truncated so we push 64-bit bounds into
3099 * 32-bit bounds. Above were truncated < 32-bits already.
3103 __reg_combine_64_into_32(reg);
3106 static bool bpf_map_is_rdonly(const struct bpf_map *map)
3108 return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen;
3111 static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val)
3117 err = map->ops->map_direct_value_addr(map, &addr, off);
3120 ptr = (void *)(long)addr + off;
3124 *val = (u64)*(u8 *)ptr;
3127 *val = (u64)*(u16 *)ptr;
3130 *val = (u64)*(u32 *)ptr;
3141 static int check_ptr_to_btf_access(struct bpf_verifier_env *env,
3142 struct bpf_reg_state *regs,
3143 int regno, int off, int size,
3144 enum bpf_access_type atype,
3147 struct bpf_reg_state *reg = regs + regno;
3148 const struct btf_type *t = btf_type_by_id(btf_vmlinux, reg->btf_id);
3149 const char *tname = btf_name_by_offset(btf_vmlinux, t->name_off);
3155 "R%d is ptr_%s invalid negative access: off=%d\n",
3159 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
3162 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
3164 "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n",
3165 regno, tname, off, tn_buf);
3169 if (env->ops->btf_struct_access) {
3170 ret = env->ops->btf_struct_access(&env->log, t, off, size,
3173 if (atype != BPF_READ) {
3174 verbose(env, "only read is supported\n");
3178 ret = btf_struct_access(&env->log, t, off, size, atype,
3185 if (atype == BPF_READ && value_regno >= 0) {
3186 if (ret == SCALAR_VALUE) {
3187 mark_reg_unknown(env, regs, value_regno);
3190 mark_reg_known_zero(env, regs, value_regno);
3191 regs[value_regno].type = PTR_TO_BTF_ID;
3192 regs[value_regno].btf_id = btf_id;
3198 /* check whether memory at (regno + off) is accessible for t = (read | write)
3199 * if t==write, value_regno is a register which value is stored into memory
3200 * if t==read, value_regno is a register which will receive the value from memory
3201 * if t==write && value_regno==-1, some unknown value is stored into memory
3202 * if t==read && value_regno==-1, don't care what we read from memory
3204 static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
3205 int off, int bpf_size, enum bpf_access_type t,
3206 int value_regno, bool strict_alignment_once)
3208 struct bpf_reg_state *regs = cur_regs(env);
3209 struct bpf_reg_state *reg = regs + regno;
3210 struct bpf_func_state *state;
3213 size = bpf_size_to_bytes(bpf_size);
3217 /* alignment checks will add in reg->off themselves */
3218 err = check_ptr_alignment(env, reg, off, size, strict_alignment_once);
3222 /* for access checks, reg->off is just part of off */
3225 if (reg->type == PTR_TO_MAP_VALUE) {
3226 if (t == BPF_WRITE && value_regno >= 0 &&
3227 is_pointer_value(env, value_regno)) {
3228 verbose(env, "R%d leaks addr into map\n", value_regno);
3231 err = check_map_access_type(env, regno, off, size, t);
3234 err = check_map_access(env, regno, off, size, false);
3235 if (!err && t == BPF_READ && value_regno >= 0) {
3236 struct bpf_map *map = reg->map_ptr;
3238 /* if map is read-only, track its contents as scalars */
3239 if (tnum_is_const(reg->var_off) &&
3240 bpf_map_is_rdonly(map) &&
3241 map->ops->map_direct_value_addr) {
3242 int map_off = off + reg->var_off.value;
3245 err = bpf_map_direct_read(map, map_off, size,
3250 regs[value_regno].type = SCALAR_VALUE;
3251 __mark_reg_known(®s[value_regno], val);
3253 mark_reg_unknown(env, regs, value_regno);
3256 } else if (reg->type == PTR_TO_MEM) {
3257 if (t == BPF_WRITE && value_regno >= 0 &&
3258 is_pointer_value(env, value_regno)) {
3259 verbose(env, "R%d leaks addr into mem\n", value_regno);
3262 err = check_mem_region_access(env, regno, off, size,
3263 reg->mem_size, false);
3264 if (!err && t == BPF_READ && value_regno >= 0)
3265 mark_reg_unknown(env, regs, value_regno);
3266 } else if (reg->type == PTR_TO_CTX) {
3267 enum bpf_reg_type reg_type = SCALAR_VALUE;
3270 if (t == BPF_WRITE && value_regno >= 0 &&
3271 is_pointer_value(env, value_regno)) {
3272 verbose(env, "R%d leaks addr into ctx\n", value_regno);
3276 err = check_ctx_reg(env, reg, regno);
3280 err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf_id);
3282 verbose_linfo(env, insn_idx, "; ");
3283 if (!err && t == BPF_READ && value_regno >= 0) {
3284 /* ctx access returns either a scalar, or a
3285 * PTR_TO_PACKET[_META,_END]. In the latter
3286 * case, we know the offset is zero.
3288 if (reg_type == SCALAR_VALUE) {
3289 mark_reg_unknown(env, regs, value_regno);
3291 mark_reg_known_zero(env, regs,
3293 if (reg_type_may_be_null(reg_type))
3294 regs[value_regno].id = ++env->id_gen;
3295 /* A load of ctx field could have different
3296 * actual load size with the one encoded in the
3297 * insn. When the dst is PTR, it is for sure not
3300 regs[value_regno].subreg_def = DEF_NOT_SUBREG;
3301 if (reg_type == PTR_TO_BTF_ID ||
3302 reg_type == PTR_TO_BTF_ID_OR_NULL)
3303 regs[value_regno].btf_id = btf_id;
3305 regs[value_regno].type = reg_type;
3308 } else if (reg->type == PTR_TO_STACK) {
3309 off += reg->var_off.value;
3310 err = check_stack_access(env, reg, off, size);
3314 state = func(env, reg);
3315 err = update_stack_depth(env, state, off);
3320 err = check_stack_write(env, state, off, size,
3321 value_regno, insn_idx);
3323 err = check_stack_read(env, state, off, size,
3325 } else if (reg_is_pkt_pointer(reg)) {
3326 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
3327 verbose(env, "cannot write into packet\n");
3330 if (t == BPF_WRITE && value_regno >= 0 &&
3331 is_pointer_value(env, value_regno)) {
3332 verbose(env, "R%d leaks addr into packet\n",
3336 err = check_packet_access(env, regno, off, size, false);
3337 if (!err && t == BPF_READ && value_regno >= 0)
3338 mark_reg_unknown(env, regs, value_regno);
3339 } else if (reg->type == PTR_TO_FLOW_KEYS) {
3340 if (t == BPF_WRITE && value_regno >= 0 &&
3341 is_pointer_value(env, value_regno)) {
3342 verbose(env, "R%d leaks addr into flow keys\n",
3347 err = check_flow_keys_access(env, off, size);
3348 if (!err && t == BPF_READ && value_regno >= 0)
3349 mark_reg_unknown(env, regs, value_regno);
3350 } else if (type_is_sk_pointer(reg->type)) {
3351 if (t == BPF_WRITE) {
3352 verbose(env, "R%d cannot write into %s\n",
3353 regno, reg_type_str[reg->type]);
3356 err = check_sock_access(env, insn_idx, regno, off, size, t);
3357 if (!err && value_regno >= 0)
3358 mark_reg_unknown(env, regs, value_regno);
3359 } else if (reg->type == PTR_TO_TP_BUFFER) {
3360 err = check_tp_buffer_access(env, reg, regno, off, size);
3361 if (!err && t == BPF_READ && value_regno >= 0)
3362 mark_reg_unknown(env, regs, value_regno);
3363 } else if (reg->type == PTR_TO_BTF_ID) {
3364 err = check_ptr_to_btf_access(env, regs, regno, off, size, t,
3367 verbose(env, "R%d invalid mem access '%s'\n", regno,
3368 reg_type_str[reg->type]);
3372 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
3373 regs[value_regno].type == SCALAR_VALUE) {
3374 /* b/h/w load zero-extends, mark upper bits as known 0 */
3375 coerce_reg_to_size(®s[value_regno], size);
3380 static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
3384 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
3386 verbose(env, "BPF_XADD uses reserved fields\n");
3390 /* check src1 operand */
3391 err = check_reg_arg(env, insn->src_reg, SRC_OP);
3395 /* check src2 operand */
3396 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
3400 if (is_pointer_value(env, insn->src_reg)) {
3401 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
3405 if (is_ctx_reg(env, insn->dst_reg) ||
3406 is_pkt_reg(env, insn->dst_reg) ||
3407 is_flow_key_reg(env, insn->dst_reg) ||
3408 is_sk_reg(env, insn->dst_reg)) {
3409 verbose(env, "BPF_XADD stores into R%d %s is not allowed\n",
3411 reg_type_str[reg_state(env, insn->dst_reg)->type]);
3415 /* check whether atomic_add can read the memory */
3416 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
3417 BPF_SIZE(insn->code), BPF_READ, -1, true);
3421 /* check whether atomic_add can write into the same memory */
3422 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
3423 BPF_SIZE(insn->code), BPF_WRITE, -1, true);
3426 static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno,
3427 int off, int access_size,
3428 bool zero_size_allowed)
3430 struct bpf_reg_state *reg = reg_state(env, regno);
3432 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
3433 access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
3434 if (tnum_is_const(reg->var_off)) {
3435 verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
3436 regno, off, access_size);
3440 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
3441 verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n",
3442 regno, tn_buf, access_size);
3449 /* when register 'regno' is passed into function that will read 'access_size'
3450 * bytes from that pointer, make sure that it's within stack boundary
3451 * and all elements of stack are initialized.
3452 * Unlike most pointer bounds-checking functions, this one doesn't take an
3453 * 'off' argument, so it has to add in reg->off itself.
3455 static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
3456 int access_size, bool zero_size_allowed,
3457 struct bpf_call_arg_meta *meta)
3459 struct bpf_reg_state *reg = reg_state(env, regno);
3460 struct bpf_func_state *state = func(env, reg);
3461 int err, min_off, max_off, i, j, slot, spi;
3463 if (reg->type != PTR_TO_STACK) {
3464 /* Allow zero-byte read from NULL, regardless of pointer type */
3465 if (zero_size_allowed && access_size == 0 &&
3466 register_is_null(reg))
3469 verbose(env, "R%d type=%s expected=%s\n", regno,
3470 reg_type_str[reg->type],
3471 reg_type_str[PTR_TO_STACK]);
3475 if (tnum_is_const(reg->var_off)) {
3476 min_off = max_off = reg->var_off.value + reg->off;
3477 err = __check_stack_boundary(env, regno, min_off, access_size,
3482 /* Variable offset is prohibited for unprivileged mode for
3483 * simplicity since it requires corresponding support in
3484 * Spectre masking for stack ALU.
3485 * See also retrieve_ptr_limit().
3487 if (!env->bypass_spec_v1) {
3490 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
3491 verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n",
3495 /* Only initialized buffer on stack is allowed to be accessed
3496 * with variable offset. With uninitialized buffer it's hard to
3497 * guarantee that whole memory is marked as initialized on
3498 * helper return since specific bounds are unknown what may
3499 * cause uninitialized stack leaking.
3501 if (meta && meta->raw_mode)
3504 if (reg->smax_value >= BPF_MAX_VAR_OFF ||
3505 reg->smax_value <= -BPF_MAX_VAR_OFF) {
3506 verbose(env, "R%d unbounded indirect variable offset stack access\n",
3510 min_off = reg->smin_value + reg->off;
3511 max_off = reg->smax_value + reg->off;
3512 err = __check_stack_boundary(env, regno, min_off, access_size,
3515 verbose(env, "R%d min value is outside of stack bound\n",
3519 err = __check_stack_boundary(env, regno, max_off, access_size,
3522 verbose(env, "R%d max value is outside of stack bound\n",
3528 if (meta && meta->raw_mode) {
3529 meta->access_size = access_size;
3530 meta->regno = regno;
3534 for (i = min_off; i < max_off + access_size; i++) {
3538 spi = slot / BPF_REG_SIZE;
3539 if (state->allocated_stack <= slot)
3541 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
3542 if (*stype == STACK_MISC)
3544 if (*stype == STACK_ZERO) {
3545 /* helper can write anything into the stack */
3546 *stype = STACK_MISC;
3550 if (state->stack[spi].slot_type[0] == STACK_SPILL &&
3551 state->stack[spi].spilled_ptr.type == PTR_TO_BTF_ID)
3554 if (state->stack[spi].slot_type[0] == STACK_SPILL &&
3555 state->stack[spi].spilled_ptr.type == SCALAR_VALUE) {
3556 __mark_reg_unknown(env, &state->stack[spi].spilled_ptr);
3557 for (j = 0; j < BPF_REG_SIZE; j++)
3558 state->stack[spi].slot_type[j] = STACK_MISC;
3563 if (tnum_is_const(reg->var_off)) {
3564 verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
3565 min_off, i - min_off, access_size);
3569 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
3570 verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n",
3571 tn_buf, i - min_off, access_size);
3575 /* reading any byte out of 8-byte 'spill_slot' will cause
3576 * the whole slot to be marked as 'read'
3578 mark_reg_read(env, &state->stack[spi].spilled_ptr,
3579 state->stack[spi].spilled_ptr.parent,
3582 return update_stack_depth(env, state, min_off);
3585 static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
3586 int access_size, bool zero_size_allowed,
3587 struct bpf_call_arg_meta *meta)
3589 struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno];
3591 switch (reg->type) {
3593 case PTR_TO_PACKET_META:
3594 return check_packet_access(env, regno, reg->off, access_size,
3596 case PTR_TO_MAP_VALUE:
3597 if (check_map_access_type(env, regno, reg->off, access_size,
3598 meta && meta->raw_mode ? BPF_WRITE :
3601 return check_map_access(env, regno, reg->off, access_size,
3604 return check_mem_region_access(env, regno, reg->off,
3605 access_size, reg->mem_size,
3607 default: /* scalar_value|ptr_to_stack or invalid ptr */
3608 return check_stack_boundary(env, regno, access_size,
3609 zero_size_allowed, meta);
3613 /* Implementation details:
3614 * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL
3615 * Two bpf_map_lookups (even with the same key) will have different reg->id.
3616 * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after
3617 * value_or_null->value transition, since the verifier only cares about
3618 * the range of access to valid map value pointer and doesn't care about actual
3619 * address of the map element.
3620 * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps
3621 * reg->id > 0 after value_or_null->value transition. By doing so
3622 * two bpf_map_lookups will be considered two different pointers that
3623 * point to different bpf_spin_locks.
3624 * The verifier allows taking only one bpf_spin_lock at a time to avoid
3626 * Since only one bpf_spin_lock is allowed the checks are simpler than
3627 * reg_is_refcounted() logic. The verifier needs to remember only
3628 * one spin_lock instead of array of acquired_refs.
3629 * cur_state->active_spin_lock remembers which map value element got locked
3630 * and clears it after bpf_spin_unlock.
3632 static int process_spin_lock(struct bpf_verifier_env *env, int regno,
3635 struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno];
3636 struct bpf_verifier_state *cur = env->cur_state;
3637 bool is_const = tnum_is_const(reg->var_off);
3638 struct bpf_map *map = reg->map_ptr;
3639 u64 val = reg->var_off.value;
3641 if (reg->type != PTR_TO_MAP_VALUE) {
3642 verbose(env, "R%d is not a pointer to map_value\n", regno);
3647 "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n",
3653 "map '%s' has to have BTF in order to use bpf_spin_lock\n",
3657 if (!map_value_has_spin_lock(map)) {
3658 if (map->spin_lock_off == -E2BIG)
3660 "map '%s' has more than one 'struct bpf_spin_lock'\n",
3662 else if (map->spin_lock_off == -ENOENT)
3664 "map '%s' doesn't have 'struct bpf_spin_lock'\n",
3668 "map '%s' is not a struct type or bpf_spin_lock is mangled\n",
3672 if (map->spin_lock_off != val + reg->off) {
3673 verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n",
3678 if (cur->active_spin_lock) {
3680 "Locking two bpf_spin_locks are not allowed\n");
3683 cur->active_spin_lock = reg->id;
3685 if (!cur->active_spin_lock) {
3686 verbose(env, "bpf_spin_unlock without taking a lock\n");
3689 if (cur->active_spin_lock != reg->id) {
3690 verbose(env, "bpf_spin_unlock of different lock\n");
3693 cur->active_spin_lock = 0;
3698 static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
3700 return type == ARG_PTR_TO_MEM ||
3701 type == ARG_PTR_TO_MEM_OR_NULL ||
3702 type == ARG_PTR_TO_UNINIT_MEM;
3705 static bool arg_type_is_mem_size(enum bpf_arg_type type)
3707 return type == ARG_CONST_SIZE ||
3708 type == ARG_CONST_SIZE_OR_ZERO;
3711 static bool arg_type_is_alloc_mem_ptr(enum bpf_arg_type type)
3713 return type == ARG_PTR_TO_ALLOC_MEM ||
3714 type == ARG_PTR_TO_ALLOC_MEM_OR_NULL;
3717 static bool arg_type_is_alloc_size(enum bpf_arg_type type)
3719 return type == ARG_CONST_ALLOC_SIZE_OR_ZERO;
3722 static bool arg_type_is_int_ptr(enum bpf_arg_type type)
3724 return type == ARG_PTR_TO_INT ||
3725 type == ARG_PTR_TO_LONG;
3728 static int int_ptr_type_to_size(enum bpf_arg_type type)
3730 if (type == ARG_PTR_TO_INT)
3732 else if (type == ARG_PTR_TO_LONG)
3738 static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
3739 enum bpf_arg_type arg_type,
3740 struct bpf_call_arg_meta *meta)
3742 struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno];
3743 enum bpf_reg_type expected_type, type = reg->type;
3746 if (arg_type == ARG_DONTCARE)
3749 err = check_reg_arg(env, regno, SRC_OP);
3753 if (arg_type == ARG_ANYTHING) {
3754 if (is_pointer_value(env, regno)) {
3755 verbose(env, "R%d leaks addr into helper function\n",
3762 if (type_is_pkt_pointer(type) &&
3763 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
3764 verbose(env, "helper access to the packet is not allowed\n");
3768 if (arg_type == ARG_PTR_TO_MAP_KEY ||
3769 arg_type == ARG_PTR_TO_MAP_VALUE ||
3770 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE ||
3771 arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) {
3772 expected_type = PTR_TO_STACK;
3773 if (register_is_null(reg) &&
3774 arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL)
3775 /* final test in check_stack_boundary() */;
3776 else if (!type_is_pkt_pointer(type) &&
3777 type != PTR_TO_MAP_VALUE &&
3778 type != expected_type)
3780 } else if (arg_type == ARG_CONST_SIZE ||
3781 arg_type == ARG_CONST_SIZE_OR_ZERO ||
3782 arg_type == ARG_CONST_ALLOC_SIZE_OR_ZERO) {
3783 expected_type = SCALAR_VALUE;
3784 if (type != expected_type)
3786 } else if (arg_type == ARG_CONST_MAP_PTR) {
3787 expected_type = CONST_PTR_TO_MAP;
3788 if (type != expected_type)
3790 } else if (arg_type == ARG_PTR_TO_CTX ||
3791 arg_type == ARG_PTR_TO_CTX_OR_NULL) {
3792 expected_type = PTR_TO_CTX;
3793 if (!(register_is_null(reg) &&
3794 arg_type == ARG_PTR_TO_CTX_OR_NULL)) {
3795 if (type != expected_type)
3797 err = check_ctx_reg(env, reg, regno);
3801 } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) {
3802 expected_type = PTR_TO_SOCK_COMMON;
3803 /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */
3804 if (!type_is_sk_pointer(type))
3806 if (reg->ref_obj_id) {
3807 if (meta->ref_obj_id) {
3808 verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",
3809 regno, reg->ref_obj_id,
3813 meta->ref_obj_id = reg->ref_obj_id;
3815 } else if (arg_type == ARG_PTR_TO_SOCKET) {
3816 expected_type = PTR_TO_SOCKET;
3817 if (type != expected_type)
3819 } else if (arg_type == ARG_PTR_TO_BTF_ID) {
3820 expected_type = PTR_TO_BTF_ID;
3821 if (type != expected_type)
3823 if (reg->btf_id != meta->btf_id) {
3824 verbose(env, "Helper has type %s got %s in R%d\n",
3825 kernel_type_name(meta->btf_id),
3826 kernel_type_name(reg->btf_id), regno);
3830 if (!tnum_is_const(reg->var_off) || reg->var_off.value || reg->off) {
3831 verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n",
3835 } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) {
3836 if (meta->func_id == BPF_FUNC_spin_lock) {
3837 if (process_spin_lock(env, regno, true))
3839 } else if (meta->func_id == BPF_FUNC_spin_unlock) {
3840 if (process_spin_lock(env, regno, false))
3843 verbose(env, "verifier internal error\n");
3846 } else if (arg_type_is_mem_ptr(arg_type)) {
3847 expected_type = PTR_TO_STACK;
3848 /* One exception here. In case function allows for NULL to be
3849 * passed in as argument, it's a SCALAR_VALUE type. Final test
3850 * happens during stack boundary checking.
3852 if (register_is_null(reg) &&
3853 (arg_type == ARG_PTR_TO_MEM_OR_NULL ||
3854 arg_type == ARG_PTR_TO_ALLOC_MEM_OR_NULL))
3855 /* final test in check_stack_boundary() */;
3856 else if (!type_is_pkt_pointer(type) &&
3857 type != PTR_TO_MAP_VALUE &&
3858 type != PTR_TO_MEM &&
3859 type != expected_type)
3861 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
3862 } else if (arg_type_is_alloc_mem_ptr(arg_type)) {
3863 expected_type = PTR_TO_MEM;
3864 if (register_is_null(reg) &&
3865 arg_type == ARG_PTR_TO_ALLOC_MEM_OR_NULL)
3866 /* final test in check_stack_boundary() */;
3867 else if (type != expected_type)
3869 if (meta->ref_obj_id) {
3870 verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",
3871 regno, reg->ref_obj_id,
3875 meta->ref_obj_id = reg->ref_obj_id;
3876 } else if (arg_type_is_int_ptr(arg_type)) {
3877 expected_type = PTR_TO_STACK;
3878 if (!type_is_pkt_pointer(type) &&
3879 type != PTR_TO_MAP_VALUE &&
3880 type != expected_type)
3883 verbose(env, "unsupported arg_type %d\n", arg_type);
3887 if (arg_type == ARG_CONST_MAP_PTR) {
3888 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
3889 meta->map_ptr = reg->map_ptr;
3890 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
3891 /* bpf_map_xxx(..., map_ptr, ..., key) call:
3892 * check that [key, key + map->key_size) are within
3893 * stack limits and initialized
3895 if (!meta->map_ptr) {
3896 /* in function declaration map_ptr must come before
3897 * map_key, so that it's verified and known before
3898 * we have to check map_key here. Otherwise it means
3899 * that kernel subsystem misconfigured verifier
3901 verbose(env, "invalid map_ptr to access map->key\n");
3904 err = check_helper_mem_access(env, regno,
3905 meta->map_ptr->key_size, false,
3907 } else if (arg_type == ARG_PTR_TO_MAP_VALUE ||
3908 (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL &&
3909 !register_is_null(reg)) ||
3910 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
3911 /* bpf_map_xxx(..., map_ptr, ..., value) call:
3912 * check [value, value + map->value_size) validity
3914 if (!meta->map_ptr) {
3915 /* kernel subsystem misconfigured verifier */
3916 verbose(env, "invalid map_ptr to access map->value\n");
3919 meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE);
3920 err = check_helper_mem_access(env, regno,
3921 meta->map_ptr->value_size, false,
3923 } else if (arg_type_is_mem_size(arg_type)) {
3924 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
3926 /* This is used to refine r0 return value bounds for helpers
3927 * that enforce this value as an upper bound on return values.
3928 * See do_refine_retval_range() for helpers that can refine
3929 * the return value. C type of helper is u32 so we pull register
3930 * bound from umax_value however, if negative verifier errors
3931 * out. Only upper bounds can be learned because retval is an
3932 * int type and negative retvals are allowed.
3934 meta->msize_max_value = reg->umax_value;
3936 /* The register is SCALAR_VALUE; the access check
3937 * happens using its boundaries.
3939 if (!tnum_is_const(reg->var_off))
3940 /* For unprivileged variable accesses, disable raw
3941 * mode so that the program is required to
3942 * initialize all the memory that the helper could
3943 * just partially fill up.
3947 if (reg->smin_value < 0) {
3948 verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
3953 if (reg->umin_value == 0) {
3954 err = check_helper_mem_access(env, regno - 1, 0,
3961 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
3962 verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
3966 err = check_helper_mem_access(env, regno - 1,
3968 zero_size_allowed, meta);
3970 err = mark_chain_precision(env, regno);
3971 } else if (arg_type_is_alloc_size(arg_type)) {
3972 if (!tnum_is_const(reg->var_off)) {
3973 verbose(env, "R%d unbounded size, use 'var &= const' or 'if (var < const)'\n",
3977 meta->mem_size = reg->var_off.value;
3978 } else if (arg_type_is_int_ptr(arg_type)) {
3979 int size = int_ptr_type_to_size(arg_type);
3981 err = check_helper_mem_access(env, regno, size, false, meta);
3984 err = check_ptr_alignment(env, reg, 0, size, true);
3989 verbose(env, "R%d type=%s expected=%s\n", regno,
3990 reg_type_str[type], reg_type_str[expected_type]);
3994 static int check_map_func_compatibility(struct bpf_verifier_env *env,
3995 struct bpf_map *map, int func_id)
4000 /* We need a two way check, first is from map perspective ... */
4001 switch (map->map_type) {
4002 case BPF_MAP_TYPE_PROG_ARRAY:
4003 if (func_id != BPF_FUNC_tail_call)
4006 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
4007 if (func_id != BPF_FUNC_perf_event_read &&
4008 func_id != BPF_FUNC_perf_event_output &&
4009 func_id != BPF_FUNC_skb_output &&
4010 func_id != BPF_FUNC_perf_event_read_value &&
4011 func_id != BPF_FUNC_xdp_output)
4014 case BPF_MAP_TYPE_RINGBUF:
4015 if (func_id != BPF_FUNC_ringbuf_output &&
4016 func_id != BPF_FUNC_ringbuf_reserve &&
4017 func_id != BPF_FUNC_ringbuf_submit &&
4018 func_id != BPF_FUNC_ringbuf_discard &&
4019 func_id != BPF_FUNC_ringbuf_query)
4022 case BPF_MAP_TYPE_STACK_TRACE:
4023 if (func_id != BPF_FUNC_get_stackid)
4026 case BPF_MAP_TYPE_CGROUP_ARRAY:
4027 if (func_id != BPF_FUNC_skb_under_cgroup &&
4028 func_id != BPF_FUNC_current_task_under_cgroup)
4031 case BPF_MAP_TYPE_CGROUP_STORAGE:
4032 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
4033 if (func_id != BPF_FUNC_get_local_storage)
4036 case BPF_MAP_TYPE_DEVMAP:
4037 case BPF_MAP_TYPE_DEVMAP_HASH:
4038 if (func_id != BPF_FUNC_redirect_map &&
4039 func_id != BPF_FUNC_map_lookup_elem)
4042 /* Restrict bpf side of cpumap and xskmap, open when use-cases
4045 case BPF_MAP_TYPE_CPUMAP:
4046 if (func_id != BPF_FUNC_redirect_map)
4049 case BPF_MAP_TYPE_XSKMAP:
4050 if (func_id != BPF_FUNC_redirect_map &&
4051 func_id != BPF_FUNC_map_lookup_elem)
4054 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
4055 case BPF_MAP_TYPE_HASH_OF_MAPS:
4056 if (func_id != BPF_FUNC_map_lookup_elem)
4059 case BPF_MAP_TYPE_SOCKMAP:
4060 if (func_id != BPF_FUNC_sk_redirect_map &&
4061 func_id != BPF_FUNC_sock_map_update &&
4062 func_id != BPF_FUNC_map_delete_elem &&
4063 func_id != BPF_FUNC_msg_redirect_map &&
4064 func_id != BPF_FUNC_sk_select_reuseport &&
4065 func_id != BPF_FUNC_map_lookup_elem)
4068 case BPF_MAP_TYPE_SOCKHASH:
4069 if (func_id != BPF_FUNC_sk_redirect_hash &&
4070 func_id != BPF_FUNC_sock_hash_update &&
4071 func_id != BPF_FUNC_map_delete_elem &&
4072 func_id != BPF_FUNC_msg_redirect_hash &&
4073 func_id != BPF_FUNC_sk_select_reuseport &&
4074 func_id != BPF_FUNC_map_lookup_elem)
4077 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
4078 if (func_id != BPF_FUNC_sk_select_reuseport)
4081 case BPF_MAP_TYPE_QUEUE:
4082 case BPF_MAP_TYPE_STACK:
4083 if (func_id != BPF_FUNC_map_peek_elem &&
4084 func_id != BPF_FUNC_map_pop_elem &&
4085 func_id != BPF_FUNC_map_push_elem)
4088 case BPF_MAP_TYPE_SK_STORAGE:
4089 if (func_id != BPF_FUNC_sk_storage_get &&
4090 func_id != BPF_FUNC_sk_storage_delete)
4097 /* ... and second from the function itself. */
4099 case BPF_FUNC_tail_call:
4100 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
4102 if (env->subprog_cnt > 1) {
4103 verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
4107 case BPF_FUNC_perf_event_read:
4108 case BPF_FUNC_perf_event_output:
4109 case BPF_FUNC_perf_event_read_value:
4110 case BPF_FUNC_skb_output:
4111 case BPF_FUNC_xdp_output:
4112 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
4115 case BPF_FUNC_get_stackid:
4116 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
4119 case BPF_FUNC_current_task_under_cgroup:
4120 case BPF_FUNC_skb_under_cgroup:
4121 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
4124 case BPF_FUNC_redirect_map:
4125 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
4126 map->map_type != BPF_MAP_TYPE_DEVMAP_HASH &&
4127 map->map_type != BPF_MAP_TYPE_CPUMAP &&
4128 map->map_type != BPF_MAP_TYPE_XSKMAP)
4131 case BPF_FUNC_sk_redirect_map:
4132 case BPF_FUNC_msg_redirect_map:
4133 case BPF_FUNC_sock_map_update:
4134 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
4137 case BPF_FUNC_sk_redirect_hash:
4138 case BPF_FUNC_msg_redirect_hash:
4139 case BPF_FUNC_sock_hash_update:
4140 if (map->map_type != BPF_MAP_TYPE_SOCKHASH)
4143 case BPF_FUNC_get_local_storage:
4144 if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
4145 map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
4148 case BPF_FUNC_sk_select_reuseport:
4149 if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY &&
4150 map->map_type != BPF_MAP_TYPE_SOCKMAP &&
4151 map->map_type != BPF_MAP_TYPE_SOCKHASH)
4154 case BPF_FUNC_map_peek_elem:
4155 case BPF_FUNC_map_pop_elem:
4156 case BPF_FUNC_map_push_elem:
4157 if (map->map_type != BPF_MAP_TYPE_QUEUE &&
4158 map->map_type != BPF_MAP_TYPE_STACK)
4161 case BPF_FUNC_sk_storage_get:
4162 case BPF_FUNC_sk_storage_delete:
4163 if (map->map_type != BPF_MAP_TYPE_SK_STORAGE)
4172 verbose(env, "cannot pass map_type %d into func %s#%d\n",
4173 map->map_type, func_id_name(func_id), func_id);
4177 static bool check_raw_mode_ok(const struct bpf_func_proto *fn)
4181 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
4183 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
4185 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
4187 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
4189 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
4192 /* We only support one arg being in raw mode at the moment,
4193 * which is sufficient for the helper functions we have
4199 static bool check_args_pair_invalid(enum bpf_arg_type arg_curr,
4200 enum bpf_arg_type arg_next)
4202 return (arg_type_is_mem_ptr(arg_curr) &&
4203 !arg_type_is_mem_size(arg_next)) ||
4204 (!arg_type_is_mem_ptr(arg_curr) &&
4205 arg_type_is_mem_size(arg_next));
4208 static bool check_arg_pair_ok(const struct bpf_func_proto *fn)
4210 /* bpf_xxx(..., buf, len) call will access 'len'
4211 * bytes from memory 'buf'. Both arg types need
4212 * to be paired, so make sure there's no buggy
4213 * helper function specification.
4215 if (arg_type_is_mem_size(fn->arg1_type) ||
4216 arg_type_is_mem_ptr(fn->arg5_type) ||
4217 check_args_pair_invalid(fn->arg1_type, fn->arg2_type) ||
4218 check_args_pair_invalid(fn->arg2_type, fn->arg3_type) ||
4219 check_args_pair_invalid(fn->arg3_type, fn->arg4_type) ||
4220 check_args_pair_invalid(fn->arg4_type, fn->arg5_type))
4226 static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id)
4230 if (arg_type_may_be_refcounted(fn->arg1_type))
4232 if (arg_type_may_be_refcounted(fn->arg2_type))
4234 if (arg_type_may_be_refcounted(fn->arg3_type))
4236 if (arg_type_may_be_refcounted(fn->arg4_type))
4238 if (arg_type_may_be_refcounted(fn->arg5_type))
4241 /* A reference acquiring function cannot acquire
4242 * another refcounted ptr.
4244 if (may_be_acquire_function(func_id) && count)
4247 /* We only support one arg being unreferenced at the moment,
4248 * which is sufficient for the helper functions we have right now.
4253 static int check_func_proto(const struct bpf_func_proto *fn, int func_id)
4255 return check_raw_mode_ok(fn) &&
4256 check_arg_pair_ok(fn) &&
4257 check_refcount_ok(fn, func_id) ? 0 : -EINVAL;
4260 /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
4261 * are now invalid, so turn them into unknown SCALAR_VALUE.
4263 static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
4264 struct bpf_func_state *state)
4266 struct bpf_reg_state *regs = state->regs, *reg;
4269 for (i = 0; i < MAX_BPF_REG; i++)
4270 if (reg_is_pkt_pointer_any(®s[i]))
4271 mark_reg_unknown(env, regs, i);
4273 bpf_for_each_spilled_reg(i, state, reg) {
4276 if (reg_is_pkt_pointer_any(reg))
4277 __mark_reg_unknown(env, reg);
4281 static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
4283 struct bpf_verifier_state *vstate = env->cur_state;
4286 for (i = 0; i <= vstate->curframe; i++)
4287 __clear_all_pkt_pointers(env, vstate->frame[i]);
4290 static void release_reg_references(struct bpf_verifier_env *env,
4291 struct bpf_func_state *state,
4294 struct bpf_reg_state *regs = state->regs, *reg;
4297 for (i = 0; i < MAX_BPF_REG; i++)
4298 if (regs[i].ref_obj_id == ref_obj_id)
4299 mark_reg_unknown(env, regs, i);
4301 bpf_for_each_spilled_reg(i, state, reg) {
4304 if (reg->ref_obj_id == ref_obj_id)
4305 __mark_reg_unknown(env, reg);
4309 /* The pointer with the specified id has released its reference to kernel
4310 * resources. Identify all copies of the same pointer and clear the reference.
4312 static int release_reference(struct bpf_verifier_env *env,
4315 struct bpf_verifier_state *vstate = env->cur_state;
4319 err = release_reference_state(cur_func(env), ref_obj_id);
4323 for (i = 0; i <= vstate->curframe; i++)
4324 release_reg_references(env, vstate->frame[i], ref_obj_id);
4329 static void clear_caller_saved_regs(struct bpf_verifier_env *env,
4330 struct bpf_reg_state *regs)
4334 /* after the call registers r0 - r5 were scratched */
4335 for (i = 0; i < CALLER_SAVED_REGS; i++) {
4336 mark_reg_not_init(env, regs, caller_saved[i]);
4337 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
4341 static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
4344 struct bpf_verifier_state *state = env->cur_state;
4345 struct bpf_func_info_aux *func_info_aux;
4346 struct bpf_func_state *caller, *callee;
4347 int i, err, subprog, target_insn;
4348 bool is_global = false;
4350 if (state->curframe + 1 >= MAX_CALL_FRAMES) {
4351 verbose(env, "the call stack of %d frames is too deep\n",
4352 state->curframe + 2);
4356 target_insn = *insn_idx + insn->imm;
4357 subprog = find_subprog(env, target_insn + 1);
4359 verbose(env, "verifier bug. No program starts at insn %d\n",
4364 caller = state->frame[state->curframe];
4365 if (state->frame[state->curframe + 1]) {
4366 verbose(env, "verifier bug. Frame %d already allocated\n",
4367 state->curframe + 1);
4371 func_info_aux = env->prog->aux->func_info_aux;
4373 is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL;
4374 err = btf_check_func_arg_match(env, subprog, caller->regs);
4379 verbose(env, "Caller passes invalid args into func#%d\n",
4383 if (env->log.level & BPF_LOG_LEVEL)
4385 "Func#%d is global and valid. Skipping.\n",
4387 clear_caller_saved_regs(env, caller->regs);
4389 /* All global functions return SCALAR_VALUE */
4390 mark_reg_unknown(env, caller->regs, BPF_REG_0);
4392 /* continue with next insn after call */
4397 callee = kzalloc(sizeof(*callee), GFP_KERNEL);
4400 state->frame[state->curframe + 1] = callee;
4402 /* callee cannot access r0, r6 - r9 for reading and has to write
4403 * into its own stack before reading from it.
4404 * callee can read/write into caller's stack
4406 init_func_state(env, callee,
4407 /* remember the callsite, it will be used by bpf_exit */
4408 *insn_idx /* callsite */,
4409 state->curframe + 1 /* frameno within this callchain */,
4410 subprog /* subprog number within this prog */);
4412 /* Transfer references to the callee */
4413 err = transfer_reference_state(callee, caller);
4417 /* copy r1 - r5 args that callee can access. The copy includes parent
4418 * pointers, which connects us up to the liveness chain
4420 for (i = BPF_REG_1; i <= BPF_REG_5; i++)
4421 callee->regs[i] = caller->regs[i];
4423 clear_caller_saved_regs(env, caller->regs);
4425 /* only increment it after check_reg_arg() finished */
4428 /* and go analyze first insn of the callee */
4429 *insn_idx = target_insn;
4431 if (env->log.level & BPF_LOG_LEVEL) {
4432 verbose(env, "caller:\n");
4433 print_verifier_state(env, caller);
4434 verbose(env, "callee:\n");
4435 print_verifier_state(env, callee);
4440 static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
4442 struct bpf_verifier_state *state = env->cur_state;
4443 struct bpf_func_state *caller, *callee;
4444 struct bpf_reg_state *r0;
4447 callee = state->frame[state->curframe];
4448 r0 = &callee->regs[BPF_REG_0];
4449 if (r0->type == PTR_TO_STACK) {
4450 /* technically it's ok to return caller's stack pointer
4451 * (or caller's caller's pointer) back to the caller,
4452 * since these pointers are valid. Only current stack
4453 * pointer will be invalid as soon as function exits,
4454 * but let's be conservative
4456 verbose(env, "cannot return stack pointer to the caller\n");
4461 caller = state->frame[state->curframe];
4462 /* return to the caller whatever r0 had in the callee */
4463 caller->regs[BPF_REG_0] = *r0;
4465 /* Transfer references to the caller */
4466 err = transfer_reference_state(caller, callee);
4470 *insn_idx = callee->callsite + 1;
4471 if (env->log.level & BPF_LOG_LEVEL) {
4472 verbose(env, "returning from callee:\n");
4473 print_verifier_state(env, callee);
4474 verbose(env, "to caller at %d:\n", *insn_idx);
4475 print_verifier_state(env, caller);
4477 /* clear everything in the callee */
4478 free_func_state(callee);
4479 state->frame[state->curframe + 1] = NULL;
4483 static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type,
4485 struct bpf_call_arg_meta *meta)
4487 struct bpf_reg_state *ret_reg = ®s[BPF_REG_0];
4489 if (ret_type != RET_INTEGER ||
4490 (func_id != BPF_FUNC_get_stack &&
4491 func_id != BPF_FUNC_probe_read_str &&
4492 func_id != BPF_FUNC_probe_read_kernel_str &&
4493 func_id != BPF_FUNC_probe_read_user_str))
4496 ret_reg->smax_value = meta->msize_max_value;
4497 ret_reg->s32_max_value = meta->msize_max_value;
4498 __reg_deduce_bounds(ret_reg);
4499 __reg_bound_offset(ret_reg);
4500 __update_reg_bounds(ret_reg);
4504 record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
4505 int func_id, int insn_idx)
4507 struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx];
4508 struct bpf_map *map = meta->map_ptr;
4510 if (func_id != BPF_FUNC_tail_call &&
4511 func_id != BPF_FUNC_map_lookup_elem &&
4512 func_id != BPF_FUNC_map_update_elem &&
4513 func_id != BPF_FUNC_map_delete_elem &&
4514 func_id != BPF_FUNC_map_push_elem &&
4515 func_id != BPF_FUNC_map_pop_elem &&
4516 func_id != BPF_FUNC_map_peek_elem)
4520 verbose(env, "kernel subsystem misconfigured verifier\n");
4524 /* In case of read-only, some additional restrictions
4525 * need to be applied in order to prevent altering the
4526 * state of the map from program side.
4528 if ((map->map_flags & BPF_F_RDONLY_PROG) &&
4529 (func_id == BPF_FUNC_map_delete_elem ||
4530 func_id == BPF_FUNC_map_update_elem ||
4531 func_id == BPF_FUNC_map_push_elem ||
4532 func_id == BPF_FUNC_map_pop_elem)) {
4533 verbose(env, "write into map forbidden\n");
4537 if (!BPF_MAP_PTR(aux->map_ptr_state))
4538 bpf_map_ptr_store(aux, meta->map_ptr,
4539 !meta->map_ptr->bypass_spec_v1);
4540 else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr)
4541 bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON,
4542 !meta->map_ptr->bypass_spec_v1);
4547 record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
4548 int func_id, int insn_idx)
4550 struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx];
4551 struct bpf_reg_state *regs = cur_regs(env), *reg;
4552 struct bpf_map *map = meta->map_ptr;
4557 if (func_id != BPF_FUNC_tail_call)
4559 if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) {
4560 verbose(env, "kernel subsystem misconfigured verifier\n");
4564 range = tnum_range(0, map->max_entries - 1);
4565 reg = ®s[BPF_REG_3];
4567 if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) {
4568 bpf_map_key_store(aux, BPF_MAP_KEY_POISON);
4572 err = mark_chain_precision(env, BPF_REG_3);
4576 val = reg->var_off.value;
4577 if (bpf_map_key_unseen(aux))
4578 bpf_map_key_store(aux, val);
4579 else if (!bpf_map_key_poisoned(aux) &&
4580 bpf_map_key_immediate(aux) != val)
4581 bpf_map_key_store(aux, BPF_MAP_KEY_POISON);
4585 static int check_reference_leak(struct bpf_verifier_env *env)
4587 struct bpf_func_state *state = cur_func(env);
4590 for (i = 0; i < state->acquired_refs; i++) {
4591 verbose(env, "Unreleased reference id=%d alloc_insn=%d\n",
4592 state->refs[i].id, state->refs[i].insn_idx);
4594 return state->acquired_refs ? -EINVAL : 0;
4597 static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
4599 const struct bpf_func_proto *fn = NULL;
4600 struct bpf_reg_state *regs;
4601 struct bpf_call_arg_meta meta;
4605 /* find function prototype */
4606 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
4607 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
4612 if (env->ops->get_func_proto)
4613 fn = env->ops->get_func_proto(func_id, env->prog);
4615 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
4620 /* eBPF programs must be GPL compatible to use GPL-ed functions */
4621 if (!env->prog->gpl_compatible && fn->gpl_only) {
4622 verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n");
4626 /* With LD_ABS/IND some JITs save/restore skb from r1. */
4627 changes_data = bpf_helper_changes_pkt_data(fn->func);
4628 if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
4629 verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
4630 func_id_name(func_id), func_id);
4634 memset(&meta, 0, sizeof(meta));
4635 meta.pkt_access = fn->pkt_access;
4637 err = check_func_proto(fn, func_id);
4639 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
4640 func_id_name(func_id), func_id);
4644 meta.func_id = func_id;
4646 for (i = 0; i < 5; i++) {
4647 err = btf_resolve_helper_id(&env->log, fn, i);
4650 err = check_func_arg(env, BPF_REG_1 + i, fn->arg_type[i], &meta);
4655 err = record_func_map(env, &meta, func_id, insn_idx);
4659 err = record_func_key(env, &meta, func_id, insn_idx);
4663 /* Mark slots with STACK_MISC in case of raw mode, stack offset
4664 * is inferred from register state.
4666 for (i = 0; i < meta.access_size; i++) {
4667 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
4668 BPF_WRITE, -1, false);
4673 if (func_id == BPF_FUNC_tail_call) {
4674 err = check_reference_leak(env);
4676 verbose(env, "tail_call would lead to reference leak\n");
4679 } else if (is_release_function(func_id)) {
4680 err = release_reference(env, meta.ref_obj_id);
4682 verbose(env, "func %s#%d reference has not been acquired before\n",
4683 func_id_name(func_id), func_id);
4688 regs = cur_regs(env);
4690 /* check that flags argument in get_local_storage(map, flags) is 0,
4691 * this is required because get_local_storage() can't return an error.
4693 if (func_id == BPF_FUNC_get_local_storage &&
4694 !register_is_null(®s[BPF_REG_2])) {
4695 verbose(env, "get_local_storage() doesn't support non-zero flags\n");
4699 /* reset caller saved regs */
4700 for (i = 0; i < CALLER_SAVED_REGS; i++) {
4701 mark_reg_not_init(env, regs, caller_saved[i]);
4702 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
4705 /* helper call returns 64-bit value. */
4706 regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG;
4708 /* update return register (already marked as written above) */
4709 if (fn->ret_type == RET_INTEGER) {
4710 /* sets type to SCALAR_VALUE */
4711 mark_reg_unknown(env, regs, BPF_REG_0);
4712 } else if (fn->ret_type == RET_VOID) {
4713 regs[BPF_REG_0].type = NOT_INIT;
4714 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL ||
4715 fn->ret_type == RET_PTR_TO_MAP_VALUE) {
4716 /* There is no offset yet applied, variable or fixed */
4717 mark_reg_known_zero(env, regs, BPF_REG_0);
4718 /* remember map_ptr, so that check_map_access()
4719 * can check 'value_size' boundary of memory access
4720 * to map element returned from bpf_map_lookup_elem()
4722 if (meta.map_ptr == NULL) {
4724 "kernel subsystem misconfigured verifier\n");
4727 regs[BPF_REG_0].map_ptr = meta.map_ptr;
4728 if (fn->ret_type == RET_PTR_TO_MAP_VALUE) {
4729 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
4730 if (map_value_has_spin_lock(meta.map_ptr))
4731 regs[BPF_REG_0].id = ++env->id_gen;
4733 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
4734 regs[BPF_REG_0].id = ++env->id_gen;
4736 } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) {
4737 mark_reg_known_zero(env, regs, BPF_REG_0);
4738 regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
4739 regs[BPF_REG_0].id = ++env->id_gen;
4740 } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) {
4741 mark_reg_known_zero(env, regs, BPF_REG_0);
4742 regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL;
4743 regs[BPF_REG_0].id = ++env->id_gen;
4744 } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) {
4745 mark_reg_known_zero(env, regs, BPF_REG_0);
4746 regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL;
4747 regs[BPF_REG_0].id = ++env->id_gen;
4748 } else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) {
4749 mark_reg_known_zero(env, regs, BPF_REG_0);
4750 regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL;
4751 regs[BPF_REG_0].id = ++env->id_gen;
4752 regs[BPF_REG_0].mem_size = meta.mem_size;
4754 verbose(env, "unknown return type %d of func %s#%d\n",
4755 fn->ret_type, func_id_name(func_id), func_id);
4759 if (is_ptr_cast_function(func_id)) {
4760 /* For release_reference() */
4761 regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
4762 } else if (is_acquire_function(func_id, meta.map_ptr)) {
4763 int id = acquire_reference_state(env, insn_idx);
4767 /* For mark_ptr_or_null_reg() */
4768 regs[BPF_REG_0].id = id;
4769 /* For release_reference() */
4770 regs[BPF_REG_0].ref_obj_id = id;
4773 do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
4775 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
4779 if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) {
4780 const char *err_str;
4782 #ifdef CONFIG_PERF_EVENTS
4783 err = get_callchain_buffers(sysctl_perf_event_max_stack);
4784 err_str = "cannot get callchain buffer for func %s#%d\n";
4787 err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n";
4790 verbose(env, err_str, func_id_name(func_id), func_id);
4794 env->prog->has_callchain_buf = true;
4798 clear_all_pkt_pointers(env);
4802 static bool signed_add_overflows(s64 a, s64 b)
4804 /* Do the add in u64, where overflow is well-defined */
4805 s64 res = (s64)((u64)a + (u64)b);
4812 static bool signed_add32_overflows(s64 a, s64 b)
4814 /* Do the add in u32, where overflow is well-defined */
4815 s32 res = (s32)((u32)a + (u32)b);
4822 static bool signed_sub_overflows(s32 a, s32 b)
4824 /* Do the sub in u64, where overflow is well-defined */
4825 s64 res = (s64)((u64)a - (u64)b);
4832 static bool signed_sub32_overflows(s32 a, s32 b)
4834 /* Do the sub in u64, where overflow is well-defined */
4835 s32 res = (s32)((u32)a - (u32)b);
4842 static bool check_reg_sane_offset(struct bpf_verifier_env *env,
4843 const struct bpf_reg_state *reg,
4844 enum bpf_reg_type type)
4846 bool known = tnum_is_const(reg->var_off);
4847 s64 val = reg->var_off.value;
4848 s64 smin = reg->smin_value;
4850 if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) {
4851 verbose(env, "math between %s pointer and %lld is not allowed\n",
4852 reg_type_str[type], val);
4856 if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) {
4857 verbose(env, "%s pointer offset %d is not allowed\n",
4858 reg_type_str[type], reg->off);
4862 if (smin == S64_MIN) {
4863 verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n",
4864 reg_type_str[type]);
4868 if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) {
4869 verbose(env, "value %lld makes %s pointer be out of bounds\n",
4870 smin, reg_type_str[type]);
4877 static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
4879 return &env->insn_aux_data[env->insn_idx];
4882 static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
4883 u32 *ptr_limit, u8 opcode, bool off_is_neg)
4885 bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
4886 (opcode == BPF_SUB && !off_is_neg);
4889 switch (ptr_reg->type) {
4891 /* Indirect variable offset stack access is prohibited in
4892 * unprivileged mode so it's not handled here.
4894 off = ptr_reg->off + ptr_reg->var_off.value;
4896 *ptr_limit = MAX_BPF_STACK + off;
4900 case PTR_TO_MAP_VALUE:
4902 *ptr_limit = ptr_reg->umax_value + ptr_reg->off;
4904 off = ptr_reg->smin_value + ptr_reg->off;
4905 *ptr_limit = ptr_reg->map_ptr->value_size - off;
4913 static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
4914 const struct bpf_insn *insn)
4916 return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K;
4919 static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,
4920 u32 alu_state, u32 alu_limit)
4922 /* If we arrived here from different branches with different
4923 * state or limits to sanitize, then this won't work.
4925 if (aux->alu_state &&
4926 (aux->alu_state != alu_state ||
4927 aux->alu_limit != alu_limit))
4930 /* Corresponding fixup done in fixup_bpf_calls(). */
4931 aux->alu_state = alu_state;
4932 aux->alu_limit = alu_limit;
4936 static int sanitize_val_alu(struct bpf_verifier_env *env,
4937 struct bpf_insn *insn)
4939 struct bpf_insn_aux_data *aux = cur_aux(env);
4941 if (can_skip_alu_sanitation(env, insn))
4944 return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0);
4947 static int sanitize_ptr_alu(struct bpf_verifier_env *env,
4948 struct bpf_insn *insn,
4949 const struct bpf_reg_state *ptr_reg,
4950 struct bpf_reg_state *dst_reg,
4953 struct bpf_verifier_state *vstate = env->cur_state;
4954 struct bpf_insn_aux_data *aux = cur_aux(env);
4955 bool ptr_is_dst_reg = ptr_reg == dst_reg;
4956 u8 opcode = BPF_OP(insn->code);
4957 u32 alu_state, alu_limit;
4958 struct bpf_reg_state tmp;
4961 if (can_skip_alu_sanitation(env, insn))
4964 /* We already marked aux for masking from non-speculative
4965 * paths, thus we got here in the first place. We only care
4966 * to explore bad access from here.
4968 if (vstate->speculative)
4971 alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
4972 alu_state |= ptr_is_dst_reg ?
4973 BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
4975 if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg))
4977 if (update_alu_sanitation_state(aux, alu_state, alu_limit))
4980 /* Simulate and find potential out-of-bounds access under
4981 * speculative execution from truncation as a result of
4982 * masking when off was not within expected range. If off
4983 * sits in dst, then we temporarily need to move ptr there
4984 * to simulate dst (== 0) +/-= ptr. Needed, for example,
4985 * for cases where we use K-based arithmetic in one direction
4986 * and truncated reg-based in the other in order to explore
4989 if (!ptr_is_dst_reg) {
4991 *dst_reg = *ptr_reg;
4993 ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true);
4994 if (!ptr_is_dst_reg && ret)
4996 return !ret ? -EFAULT : 0;
4999 /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
5000 * Caller should also handle BPF_MOV case separately.
5001 * If we return -EACCES, caller may want to try again treating pointer as a
5002 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
5004 static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
5005 struct bpf_insn *insn,
5006 const struct bpf_reg_state *ptr_reg,
5007 const struct bpf_reg_state *off_reg)
5009 struct bpf_verifier_state *vstate = env->cur_state;
5010 struct bpf_func_state *state = vstate->frame[vstate->curframe];
5011 struct bpf_reg_state *regs = state->regs, *dst_reg;
5012 bool known = tnum_is_const(off_reg->var_off);
5013 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
5014 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
5015 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
5016 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
5017 u32 dst = insn->dst_reg, src = insn->src_reg;
5018 u8 opcode = BPF_OP(insn->code);
5021 dst_reg = ®s[dst];
5023 if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
5024 smin_val > smax_val || umin_val > umax_val) {
5025 /* Taint dst register if offset had invalid bounds derived from
5026 * e.g. dead branches.
5028 __mark_reg_unknown(env, dst_reg);
5032 if (BPF_CLASS(insn->code) != BPF_ALU64) {
5033 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
5035 "R%d 32-bit pointer arithmetic prohibited\n",
5040 switch (ptr_reg->type) {
5041 case PTR_TO_MAP_VALUE_OR_NULL:
5042 verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
5043 dst, reg_type_str[ptr_reg->type]);
5045 case CONST_PTR_TO_MAP:
5046 case PTR_TO_PACKET_END:
5048 case PTR_TO_SOCKET_OR_NULL:
5049 case PTR_TO_SOCK_COMMON:
5050 case PTR_TO_SOCK_COMMON_OR_NULL:
5051 case PTR_TO_TCP_SOCK:
5052 case PTR_TO_TCP_SOCK_OR_NULL:
5053 case PTR_TO_XDP_SOCK:
5054 verbose(env, "R%d pointer arithmetic on %s prohibited\n",
5055 dst, reg_type_str[ptr_reg->type]);
5057 case PTR_TO_MAP_VALUE:
5058 if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) {
5059 verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",
5060 off_reg == dst_reg ? dst : src);
5068 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
5069 * The id may be overwritten later if we create a new variable offset.
5071 dst_reg->type = ptr_reg->type;
5072 dst_reg->id = ptr_reg->id;
5074 if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) ||
5075 !check_reg_sane_offset(env, ptr_reg, ptr_reg->type))
5078 /* pointer types do not carry 32-bit bounds at the moment. */
5079 __mark_reg32_unbounded(dst_reg);
5083 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
5085 verbose(env, "R%d tried to add from different maps or paths\n", dst);
5088 /* We can take a fixed offset as long as it doesn't overflow
5089 * the s32 'off' field
5091 if (known && (ptr_reg->off + smin_val ==
5092 (s64)(s32)(ptr_reg->off + smin_val))) {
5093 /* pointer += K. Accumulate it into fixed offset */
5094 dst_reg->smin_value = smin_ptr;
5095 dst_reg->smax_value = smax_ptr;
5096 dst_reg->umin_value = umin_ptr;
5097 dst_reg->umax_value = umax_ptr;
5098 dst_reg->var_off = ptr_reg->var_off;
5099 dst_reg->off = ptr_reg->off + smin_val;
5100 dst_reg->raw = ptr_reg->raw;
5103 /* A new variable offset is created. Note that off_reg->off
5104 * == 0, since it's a scalar.
5105 * dst_reg gets the pointer type and since some positive
5106 * integer value was added to the pointer, give it a new 'id'
5107 * if it's a PTR_TO_PACKET.
5108 * this creates a new 'base' pointer, off_reg (variable) gets
5109 * added into the variable offset, and we copy the fixed offset
5112 if (signed_add_overflows(smin_ptr, smin_val) ||
5113 signed_add_overflows(smax_ptr, smax_val)) {
5114 dst_reg->smin_value = S64_MIN;
5115 dst_reg->smax_value = S64_MAX;
5117 dst_reg->smin_value = smin_ptr + smin_val;
5118 dst_reg->smax_value = smax_ptr + smax_val;
5120 if (umin_ptr + umin_val < umin_ptr ||
5121 umax_ptr + umax_val < umax_ptr) {
5122 dst_reg->umin_value = 0;
5123 dst_reg->umax_value = U64_MAX;
5125 dst_reg->umin_value = umin_ptr + umin_val;
5126 dst_reg->umax_value = umax_ptr + umax_val;
5128 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
5129 dst_reg->off = ptr_reg->off;
5130 dst_reg->raw = ptr_reg->raw;
5131 if (reg_is_pkt_pointer(ptr_reg)) {
5132 dst_reg->id = ++env->id_gen;
5133 /* something was added to pkt_ptr, set range to zero */
5138 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
5140 verbose(env, "R%d tried to sub from different maps or paths\n", dst);
5143 if (dst_reg == off_reg) {
5144 /* scalar -= pointer. Creates an unknown scalar */
5145 verbose(env, "R%d tried to subtract pointer from scalar\n",
5149 /* We don't allow subtraction from FP, because (according to
5150 * test_verifier.c test "invalid fp arithmetic", JITs might not
5151 * be able to deal with it.
5153 if (ptr_reg->type == PTR_TO_STACK) {
5154 verbose(env, "R%d subtraction from stack pointer prohibited\n",
5158 if (known && (ptr_reg->off - smin_val ==
5159 (s64)(s32)(ptr_reg->off - smin_val))) {
5160 /* pointer -= K. Subtract it from fixed offset */
5161 dst_reg->smin_value = smin_ptr;
5162 dst_reg->smax_value = smax_ptr;
5163 dst_reg->umin_value = umin_ptr;
5164 dst_reg->umax_value = umax_ptr;
5165 dst_reg->var_off = ptr_reg->var_off;
5166 dst_reg->id = ptr_reg->id;
5167 dst_reg->off = ptr_reg->off - smin_val;
5168 dst_reg->raw = ptr_reg->raw;
5171 /* A new variable offset is created. If the subtrahend is known
5172 * nonnegative, then any reg->range we had before is still good.
5174 if (signed_sub_overflows(smin_ptr, smax_val) ||
5175 signed_sub_overflows(smax_ptr, smin_val)) {
5176 /* Overflow possible, we know nothing */
5177 dst_reg->smin_value = S64_MIN;
5178 dst_reg->smax_value = S64_MAX;
5180 dst_reg->smin_value = smin_ptr - smax_val;
5181 dst_reg->smax_value = smax_ptr - smin_val;
5183 if (umin_ptr < umax_val) {
5184 /* Overflow possible, we know nothing */
5185 dst_reg->umin_value = 0;
5186 dst_reg->umax_value = U64_MAX;
5188 /* Cannot overflow (as long as bounds are consistent) */
5189 dst_reg->umin_value = umin_ptr - umax_val;
5190 dst_reg->umax_value = umax_ptr - umin_val;
5192 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
5193 dst_reg->off = ptr_reg->off;
5194 dst_reg->raw = ptr_reg->raw;
5195 if (reg_is_pkt_pointer(ptr_reg)) {
5196 dst_reg->id = ++env->id_gen;
5197 /* something was added to pkt_ptr, set range to zero */
5205 /* bitwise ops on pointers are troublesome, prohibit. */
5206 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
5207 dst, bpf_alu_string[opcode >> 4]);
5210 /* other operators (e.g. MUL,LSH) produce non-pointer results */
5211 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
5212 dst, bpf_alu_string[opcode >> 4]);
5216 if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type))
5219 __update_reg_bounds(dst_reg);
5220 __reg_deduce_bounds(dst_reg);
5221 __reg_bound_offset(dst_reg);
5223 /* For unprivileged we require that resulting offset must be in bounds
5224 * in order to be able to sanitize access later on.
5226 if (!env->bypass_spec_v1) {
5227 if (dst_reg->type == PTR_TO_MAP_VALUE &&
5228 check_map_access(env, dst, dst_reg->off, 1, false)) {
5229 verbose(env, "R%d pointer arithmetic of map value goes out of range, "
5230 "prohibited for !root\n", dst);
5232 } else if (dst_reg->type == PTR_TO_STACK &&
5233 check_stack_access(env, dst_reg, dst_reg->off +
5234 dst_reg->var_off.value, 1)) {
5235 verbose(env, "R%d stack pointer arithmetic goes out of range, "
5236 "prohibited for !root\n", dst);
5244 static void scalar32_min_max_add(struct bpf_reg_state *dst_reg,
5245 struct bpf_reg_state *src_reg)
5247 s32 smin_val = src_reg->s32_min_value;
5248 s32 smax_val = src_reg->s32_max_value;
5249 u32 umin_val = src_reg->u32_min_value;
5250 u32 umax_val = src_reg->u32_max_value;
5252 if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) ||
5253 signed_add32_overflows(dst_reg->s32_max_value, smax_val)) {
5254 dst_reg->s32_min_value = S32_MIN;
5255 dst_reg->s32_max_value = S32_MAX;
5257 dst_reg->s32_min_value += smin_val;
5258 dst_reg->s32_max_value += smax_val;
5260 if (dst_reg->u32_min_value + umin_val < umin_val ||
5261 dst_reg->u32_max_value + umax_val < umax_val) {
5262 dst_reg->u32_min_value = 0;
5263 dst_reg->u32_max_value = U32_MAX;
5265 dst_reg->u32_min_value += umin_val;
5266 dst_reg->u32_max_value += umax_val;
5270 static void scalar_min_max_add(struct bpf_reg_state *dst_reg,
5271 struct bpf_reg_state *src_reg)
5273 s64 smin_val = src_reg->smin_value;
5274 s64 smax_val = src_reg->smax_value;
5275 u64 umin_val = src_reg->umin_value;
5276 u64 umax_val = src_reg->umax_value;
5278 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
5279 signed_add_overflows(dst_reg->smax_value, smax_val)) {
5280 dst_reg->smin_value = S64_MIN;
5281 dst_reg->smax_value = S64_MAX;
5283 dst_reg->smin_value += smin_val;
5284 dst_reg->smax_value += smax_val;
5286 if (dst_reg->umin_value + umin_val < umin_val ||
5287 dst_reg->umax_value + umax_val < umax_val) {
5288 dst_reg->umin_value = 0;
5289 dst_reg->umax_value = U64_MAX;
5291 dst_reg->umin_value += umin_val;
5292 dst_reg->umax_value += umax_val;
5296 static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg,
5297 struct bpf_reg_state *src_reg)
5299 s32 smin_val = src_reg->s32_min_value;
5300 s32 smax_val = src_reg->s32_max_value;
5301 u32 umin_val = src_reg->u32_min_value;
5302 u32 umax_val = src_reg->u32_max_value;
5304 if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) ||
5305 signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) {
5306 /* Overflow possible, we know nothing */
5307 dst_reg->s32_min_value = S32_MIN;
5308 dst_reg->s32_max_value = S32_MAX;
5310 dst_reg->s32_min_value -= smax_val;
5311 dst_reg->s32_max_value -= smin_val;
5313 if (dst_reg->u32_min_value < umax_val) {
5314 /* Overflow possible, we know nothing */
5315 dst_reg->u32_min_value = 0;
5316 dst_reg->u32_max_value = U32_MAX;
5318 /* Cannot overflow (as long as bounds are consistent) */
5319 dst_reg->u32_min_value -= umax_val;
5320 dst_reg->u32_max_value -= umin_val;
5324 static void scalar_min_max_sub(struct bpf_reg_state *dst_reg,
5325 struct bpf_reg_state *src_reg)
5327 s64 smin_val = src_reg->smin_value;
5328 s64 smax_val = src_reg->smax_value;
5329 u64 umin_val = src_reg->umin_value;
5330 u64 umax_val = src_reg->umax_value;
5332 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
5333 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
5334 /* Overflow possible, we know nothing */
5335 dst_reg->smin_value = S64_MIN;
5336 dst_reg->smax_value = S64_MAX;
5338 dst_reg->smin_value -= smax_val;
5339 dst_reg->smax_value -= smin_val;
5341 if (dst_reg->umin_value < umax_val) {
5342 /* Overflow possible, we know nothing */
5343 dst_reg->umin_value = 0;
5344 dst_reg->umax_value = U64_MAX;
5346 /* Cannot overflow (as long as bounds are consistent) */
5347 dst_reg->umin_value -= umax_val;
5348 dst_reg->umax_value -= umin_val;
5352 static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg,
5353 struct bpf_reg_state *src_reg)
5355 s32 smin_val = src_reg->s32_min_value;
5356 u32 umin_val = src_reg->u32_min_value;
5357 u32 umax_val = src_reg->u32_max_value;
5359 if (smin_val < 0 || dst_reg->s32_min_value < 0) {
5360 /* Ain't nobody got time to multiply that sign */
5361 __mark_reg32_unbounded(dst_reg);
5364 /* Both values are positive, so we can work with unsigned and
5365 * copy the result to signed (unless it exceeds S32_MAX).
5367 if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) {
5368 /* Potential overflow, we know nothing */
5369 __mark_reg32_unbounded(dst_reg);
5372 dst_reg->u32_min_value *= umin_val;
5373 dst_reg->u32_max_value *= umax_val;
5374 if (dst_reg->u32_max_value > S32_MAX) {
5375 /* Overflow possible, we know nothing */
5376 dst_reg->s32_min_value = S32_MIN;
5377 dst_reg->s32_max_value = S32_MAX;
5379 dst_reg->s32_min_value = dst_reg->u32_min_value;
5380 dst_reg->s32_max_value = dst_reg->u32_max_value;
5384 static void scalar_min_max_mul(struct bpf_reg_state *dst_reg,
5385 struct bpf_reg_state *src_reg)
5387 s64 smin_val = src_reg->smin_value;
5388 u64 umin_val = src_reg->umin_value;
5389 u64 umax_val = src_reg->umax_value;
5391 if (smin_val < 0 || dst_reg->smin_value < 0) {
5392 /* Ain't nobody got time to multiply that sign */
5393 __mark_reg64_unbounded(dst_reg);
5396 /* Both values are positive, so we can work with unsigned and
5397 * copy the result to signed (unless it exceeds S64_MAX).
5399 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
5400 /* Potential overflow, we know nothing */
5401 __mark_reg64_unbounded(dst_reg);
5404 dst_reg->umin_value *= umin_val;
5405 dst_reg->umax_value *= umax_val;
5406 if (dst_reg->umax_value > S64_MAX) {
5407 /* Overflow possible, we know nothing */
5408 dst_reg->smin_value = S64_MIN;
5409 dst_reg->smax_value = S64_MAX;
5411 dst_reg->smin_value = dst_reg->umin_value;
5412 dst_reg->smax_value = dst_reg->umax_value;
5416 static void scalar32_min_max_and(struct bpf_reg_state *dst_reg,
5417 struct bpf_reg_state *src_reg)
5419 bool src_known = tnum_subreg_is_const(src_reg->var_off);
5420 bool dst_known = tnum_subreg_is_const(dst_reg->var_off);
5421 struct tnum var32_off = tnum_subreg(dst_reg->var_off);
5422 s32 smin_val = src_reg->s32_min_value;
5423 u32 umax_val = src_reg->u32_max_value;
5425 /* Assuming scalar64_min_max_and will be called so its safe
5426 * to skip updating register for known 32-bit case.
5428 if (src_known && dst_known)
5431 /* We get our minimum from the var_off, since that's inherently
5432 * bitwise. Our maximum is the minimum of the operands' maxima.
5434 dst_reg->u32_min_value = var32_off.value;
5435 dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val);
5436 if (dst_reg->s32_min_value < 0 || smin_val < 0) {
5437 /* Lose signed bounds when ANDing negative numbers,
5438 * ain't nobody got time for that.
5440 dst_reg->s32_min_value = S32_MIN;
5441 dst_reg->s32_max_value = S32_MAX;
5443 /* ANDing two positives gives a positive, so safe to
5444 * cast result into s64.
5446 dst_reg->s32_min_value = dst_reg->u32_min_value;
5447 dst_reg->s32_max_value = dst_reg->u32_max_value;
5452 static void scalar_min_max_and(struct bpf_reg_state *dst_reg,
5453 struct bpf_reg_state *src_reg)
5455 bool src_known = tnum_is_const(src_reg->var_off);
5456 bool dst_known = tnum_is_const(dst_reg->var_off);
5457 s64 smin_val = src_reg->smin_value;
5458 u64 umax_val = src_reg->umax_value;
5460 if (src_known && dst_known) {
5461 __mark_reg_known(dst_reg, dst_reg->var_off.value &
5462 src_reg->var_off.value);
5466 /* We get our minimum from the var_off, since that's inherently
5467 * bitwise. Our maximum is the minimum of the operands' maxima.
5469 dst_reg->umin_value = dst_reg->var_off.value;
5470 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
5471 if (dst_reg->smin_value < 0 || smin_val < 0) {
5472 /* Lose signed bounds when ANDing negative numbers,
5473 * ain't nobody got time for that.
5475 dst_reg->smin_value = S64_MIN;
5476 dst_reg->smax_value = S64_MAX;
5478 /* ANDing two positives gives a positive, so safe to
5479 * cast result into s64.
5481 dst_reg->smin_value = dst_reg->umin_value;
5482 dst_reg->smax_value = dst_reg->umax_value;
5484 /* We may learn something more from the var_off */
5485 __update_reg_bounds(dst_reg);
5488 static void scalar32_min_max_or(struct bpf_reg_state *dst_reg,
5489 struct bpf_reg_state *src_reg)
5491 bool src_known = tnum_subreg_is_const(src_reg->var_off);
5492 bool dst_known = tnum_subreg_is_const(dst_reg->var_off);
5493 struct tnum var32_off = tnum_subreg(dst_reg->var_off);
5494 s32 smin_val = src_reg->smin_value;
5495 u32 umin_val = src_reg->umin_value;
5497 /* Assuming scalar64_min_max_or will be called so it is safe
5498 * to skip updating register for known case.
5500 if (src_known && dst_known)
5503 /* We get our maximum from the var_off, and our minimum is the
5504 * maximum of the operands' minima
5506 dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val);
5507 dst_reg->u32_max_value = var32_off.value | var32_off.mask;
5508 if (dst_reg->s32_min_value < 0 || smin_val < 0) {
5509 /* Lose signed bounds when ORing negative numbers,
5510 * ain't nobody got time for that.
5512 dst_reg->s32_min_value = S32_MIN;
5513 dst_reg->s32_max_value = S32_MAX;
5515 /* ORing two positives gives a positive, so safe to
5516 * cast result into s64.
5518 dst_reg->s32_min_value = dst_reg->umin_value;
5519 dst_reg->s32_max_value = dst_reg->umax_value;
5523 static void scalar_min_max_or(struct bpf_reg_state *dst_reg,
5524 struct bpf_reg_state *src_reg)
5526 bool src_known = tnum_is_const(src_reg->var_off);
5527 bool dst_known = tnum_is_const(dst_reg->var_off);
5528 s64 smin_val = src_reg->smin_value;
5529 u64 umin_val = src_reg->umin_value;
5531 if (src_known && dst_known) {
5532 __mark_reg_known(dst_reg, dst_reg->var_off.value |
5533 src_reg->var_off.value);
5537 /* We get our maximum from the var_off, and our minimum is the
5538 * maximum of the operands' minima
5540 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
5541 dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask;
5542 if (dst_reg->smin_value < 0 || smin_val < 0) {
5543 /* Lose signed bounds when ORing negative numbers,
5544 * ain't nobody got time for that.
5546 dst_reg->smin_value = S64_MIN;
5547 dst_reg->smax_value = S64_MAX;
5549 /* ORing two positives gives a positive, so safe to
5550 * cast result into s64.
5552 dst_reg->smin_value = dst_reg->umin_value;
5553 dst_reg->smax_value = dst_reg->umax_value;
5555 /* We may learn something more from the var_off */
5556 __update_reg_bounds(dst_reg);
5559 static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg,
5560 u64 umin_val, u64 umax_val)
5562 /* We lose all sign bit information (except what we can pick
5565 dst_reg->s32_min_value = S32_MIN;
5566 dst_reg->s32_max_value = S32_MAX;
5567 /* If we might shift our top bit out, then we know nothing */
5568 if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) {
5569 dst_reg->u32_min_value = 0;
5570 dst_reg->u32_max_value = U32_MAX;
5572 dst_reg->u32_min_value <<= umin_val;
5573 dst_reg->u32_max_value <<= umax_val;
5577 static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg,
5578 struct bpf_reg_state *src_reg)
5580 u32 umax_val = src_reg->u32_max_value;
5581 u32 umin_val = src_reg->u32_min_value;
5582 /* u32 alu operation will zext upper bits */
5583 struct tnum subreg = tnum_subreg(dst_reg->var_off);
5585 __scalar32_min_max_lsh(dst_reg, umin_val, umax_val);
5586 dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val));
5587 /* Not required but being careful mark reg64 bounds as unknown so
5588 * that we are forced to pick them up from tnum and zext later and
5589 * if some path skips this step we are still safe.
5591 __mark_reg64_unbounded(dst_reg);
5592 __update_reg32_bounds(dst_reg);
5595 static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg,
5596 u64 umin_val, u64 umax_val)
5598 /* Special case <<32 because it is a common compiler pattern to sign
5599 * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are
5600 * positive we know this shift will also be positive so we can track
5601 * bounds correctly. Otherwise we lose all sign bit information except
5602 * what we can pick up from var_off. Perhaps we can generalize this
5603 * later to shifts of any length.
5605 if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0)
5606 dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32;
5608 dst_reg->smax_value = S64_MAX;
5610 if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0)
5611 dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32;
5613 dst_reg->smin_value = S64_MIN;
5615 /* If we might shift our top bit out, then we know nothing */
5616 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
5617 dst_reg->umin_value = 0;
5618 dst_reg->umax_value = U64_MAX;
5620 dst_reg->umin_value <<= umin_val;
5621 dst_reg->umax_value <<= umax_val;
5625 static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg,
5626 struct bpf_reg_state *src_reg)
5628 u64 umax_val = src_reg->umax_value;
5629 u64 umin_val = src_reg->umin_value;
5631 /* scalar64 calc uses 32bit unshifted bounds so must be called first */
5632 __scalar64_min_max_lsh(dst_reg, umin_val, umax_val);
5633 __scalar32_min_max_lsh(dst_reg, umin_val, umax_val);
5635 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
5636 /* We may learn something more from the var_off */
5637 __update_reg_bounds(dst_reg);
5640 static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg,
5641 struct bpf_reg_state *src_reg)
5643 struct tnum subreg = tnum_subreg(dst_reg->var_off);
5644 u32 umax_val = src_reg->u32_max_value;
5645 u32 umin_val = src_reg->u32_min_value;
5647 /* BPF_RSH is an unsigned shift. If the value in dst_reg might
5648 * be negative, then either:
5649 * 1) src_reg might be zero, so the sign bit of the result is
5650 * unknown, so we lose our signed bounds
5651 * 2) it's known negative, thus the unsigned bounds capture the
5653 * 3) the signed bounds cross zero, so they tell us nothing
5655 * If the value in dst_reg is known nonnegative, then again the
5656 * unsigned bounts capture the signed bounds.
5657 * Thus, in all cases it suffices to blow away our signed bounds
5658 * and rely on inferring new ones from the unsigned bounds and
5659 * var_off of the result.
5661 dst_reg->s32_min_value = S32_MIN;
5662 dst_reg->s32_max_value = S32_MAX;
5664 dst_reg->var_off = tnum_rshift(subreg, umin_val);
5665 dst_reg->u32_min_value >>= umax_val;
5666 dst_reg->u32_max_value >>= umin_val;
5668 __mark_reg64_unbounded(dst_reg);
5669 __update_reg32_bounds(dst_reg);
5672 static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg,
5673 struct bpf_reg_state *src_reg)
5675 u64 umax_val = src_reg->umax_value;
5676 u64 umin_val = src_reg->umin_value;
5678 /* BPF_RSH is an unsigned shift. If the value in dst_reg might
5679 * be negative, then either:
5680 * 1) src_reg might be zero, so the sign bit of the result is
5681 * unknown, so we lose our signed bounds
5682 * 2) it's known negative, thus the unsigned bounds capture the
5684 * 3) the signed bounds cross zero, so they tell us nothing
5686 * If the value in dst_reg is known nonnegative, then again the
5687 * unsigned bounts capture the signed bounds.
5688 * Thus, in all cases it suffices to blow away our signed bounds
5689 * and rely on inferring new ones from the unsigned bounds and
5690 * var_off of the result.
5692 dst_reg->smin_value = S64_MIN;
5693 dst_reg->smax_value = S64_MAX;
5694 dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val);
5695 dst_reg->umin_value >>= umax_val;
5696 dst_reg->umax_value >>= umin_val;
5698 /* Its not easy to operate on alu32 bounds here because it depends
5699 * on bits being shifted in. Take easy way out and mark unbounded
5700 * so we can recalculate later from tnum.
5702 __mark_reg32_unbounded(dst_reg);
5703 __update_reg_bounds(dst_reg);
5706 static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg,
5707 struct bpf_reg_state *src_reg)
5709 u64 umin_val = src_reg->u32_min_value;
5711 /* Upon reaching here, src_known is true and
5712 * umax_val is equal to umin_val.
5714 dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val);
5715 dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val);
5717 dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32);
5719 /* blow away the dst_reg umin_value/umax_value and rely on
5720 * dst_reg var_off to refine the result.
5722 dst_reg->u32_min_value = 0;
5723 dst_reg->u32_max_value = U32_MAX;
5725 __mark_reg64_unbounded(dst_reg);
5726 __update_reg32_bounds(dst_reg);
5729 static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg,
5730 struct bpf_reg_state *src_reg)
5732 u64 umin_val = src_reg->umin_value;
5734 /* Upon reaching here, src_known is true and umax_val is equal
5737 dst_reg->smin_value >>= umin_val;
5738 dst_reg->smax_value >>= umin_val;
5740 dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64);
5742 /* blow away the dst_reg umin_value/umax_value and rely on
5743 * dst_reg var_off to refine the result.
5745 dst_reg->umin_value = 0;
5746 dst_reg->umax_value = U64_MAX;
5748 /* Its not easy to operate on alu32 bounds here because it depends
5749 * on bits being shifted in from upper 32-bits. Take easy way out
5750 * and mark unbounded so we can recalculate later from tnum.
5752 __mark_reg32_unbounded(dst_reg);
5753 __update_reg_bounds(dst_reg);
5756 /* WARNING: This function does calculations on 64-bit values, but the actual
5757 * execution may occur on 32-bit values. Therefore, things like bitshifts
5758 * need extra checks in the 32-bit case.
5760 static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
5761 struct bpf_insn *insn,
5762 struct bpf_reg_state *dst_reg,
5763 struct bpf_reg_state src_reg)
5765 struct bpf_reg_state *regs = cur_regs(env);
5766 u8 opcode = BPF_OP(insn->code);
5768 s64 smin_val, smax_val;
5769 u64 umin_val, umax_val;
5770 s32 s32_min_val, s32_max_val;
5771 u32 u32_min_val, u32_max_val;
5772 u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
5773 u32 dst = insn->dst_reg;
5775 bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64);
5777 smin_val = src_reg.smin_value;
5778 smax_val = src_reg.smax_value;
5779 umin_val = src_reg.umin_value;
5780 umax_val = src_reg.umax_value;
5782 s32_min_val = src_reg.s32_min_value;
5783 s32_max_val = src_reg.s32_max_value;
5784 u32_min_val = src_reg.u32_min_value;
5785 u32_max_val = src_reg.u32_max_value;
5788 src_known = tnum_subreg_is_const(src_reg.var_off);
5790 (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) ||
5791 s32_min_val > s32_max_val || u32_min_val > u32_max_val) {
5792 /* Taint dst register if offset had invalid bounds
5793 * derived from e.g. dead branches.
5795 __mark_reg_unknown(env, dst_reg);
5799 src_known = tnum_is_const(src_reg.var_off);
5801 (smin_val != smax_val || umin_val != umax_val)) ||
5802 smin_val > smax_val || umin_val > umax_val) {
5803 /* Taint dst register if offset had invalid bounds
5804 * derived from e.g. dead branches.
5806 __mark_reg_unknown(env, dst_reg);
5812 opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) {
5813 __mark_reg_unknown(env, dst_reg);
5817 /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops.
5818 * There are two classes of instructions: The first class we track both
5819 * alu32 and alu64 sign/unsigned bounds independently this provides the
5820 * greatest amount of precision when alu operations are mixed with jmp32
5821 * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD,
5822 * and BPF_OR. This is possible because these ops have fairly easy to
5823 * understand and calculate behavior in both 32-bit and 64-bit alu ops.
5824 * See alu32 verifier tests for examples. The second class of
5825 * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy
5826 * with regards to tracking sign/unsigned bounds because the bits may
5827 * cross subreg boundaries in the alu64 case. When this happens we mark
5828 * the reg unbounded in the subreg bound space and use the resulting
5829 * tnum to calculate an approximation of the sign/unsigned bounds.
5833 ret = sanitize_val_alu(env, insn);
5835 verbose(env, "R%d tried to add from different pointers or scalars\n", dst);
5838 scalar32_min_max_add(dst_reg, &src_reg);
5839 scalar_min_max_add(dst_reg, &src_reg);
5840 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
5843 ret = sanitize_val_alu(env, insn);
5845 verbose(env, "R%d tried to sub from different pointers or scalars\n", dst);
5848 scalar32_min_max_sub(dst_reg, &src_reg);
5849 scalar_min_max_sub(dst_reg, &src_reg);
5850 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
5853 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
5854 scalar32_min_max_mul(dst_reg, &src_reg);
5855 scalar_min_max_mul(dst_reg, &src_reg);
5858 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
5859 scalar32_min_max_and(dst_reg, &src_reg);
5860 scalar_min_max_and(dst_reg, &src_reg);
5863 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
5864 scalar32_min_max_or(dst_reg, &src_reg);
5865 scalar_min_max_or(dst_reg, &src_reg);
5868 if (umax_val >= insn_bitness) {
5869 /* Shifts greater than 31 or 63 are undefined.
5870 * This includes shifts by a negative number.
5872 mark_reg_unknown(env, regs, insn->dst_reg);
5876 scalar32_min_max_lsh(dst_reg, &src_reg);
5878 scalar_min_max_lsh(dst_reg, &src_reg);
5881 if (umax_val >= insn_bitness) {
5882 /* Shifts greater than 31 or 63 are undefined.
5883 * This includes shifts by a negative number.
5885 mark_reg_unknown(env, regs, insn->dst_reg);
5889 scalar32_min_max_rsh(dst_reg, &src_reg);
5891 scalar_min_max_rsh(dst_reg, &src_reg);
5894 if (umax_val >= insn_bitness) {
5895 /* Shifts greater than 31 or 63 are undefined.
5896 * This includes shifts by a negative number.
5898 mark_reg_unknown(env, regs, insn->dst_reg);
5902 scalar32_min_max_arsh(dst_reg, &src_reg);
5904 scalar_min_max_arsh(dst_reg, &src_reg);
5907 mark_reg_unknown(env, regs, insn->dst_reg);
5911 /* ALU32 ops are zero extended into 64bit register */
5913 zext_32_to_64(dst_reg);
5915 __update_reg_bounds(dst_reg);
5916 __reg_deduce_bounds(dst_reg);
5917 __reg_bound_offset(dst_reg);
5921 /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
5924 static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
5925 struct bpf_insn *insn)
5927 struct bpf_verifier_state *vstate = env->cur_state;
5928 struct bpf_func_state *state = vstate->frame[vstate->curframe];
5929 struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg;
5930 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
5931 u8 opcode = BPF_OP(insn->code);
5934 dst_reg = ®s[insn->dst_reg];
5936 if (dst_reg->type != SCALAR_VALUE)
5938 if (BPF_SRC(insn->code) == BPF_X) {
5939 src_reg = ®s[insn->src_reg];
5940 if (src_reg->type != SCALAR_VALUE) {
5941 if (dst_reg->type != SCALAR_VALUE) {
5942 /* Combining two pointers by any ALU op yields
5943 * an arbitrary scalar. Disallow all math except
5944 * pointer subtraction
5946 if (opcode == BPF_SUB && env->allow_ptr_leaks) {
5947 mark_reg_unknown(env, regs, insn->dst_reg);
5950 verbose(env, "R%d pointer %s pointer prohibited\n",
5952 bpf_alu_string[opcode >> 4]);
5955 /* scalar += pointer
5956 * This is legal, but we have to reverse our
5957 * src/dest handling in computing the range
5959 err = mark_chain_precision(env, insn->dst_reg);
5962 return adjust_ptr_min_max_vals(env, insn,
5965 } else if (ptr_reg) {
5966 /* pointer += scalar */
5967 err = mark_chain_precision(env, insn->src_reg);
5970 return adjust_ptr_min_max_vals(env, insn,
5974 /* Pretend the src is a reg with a known value, since we only
5975 * need to be able to read from this state.
5977 off_reg.type = SCALAR_VALUE;
5978 __mark_reg_known(&off_reg, insn->imm);
5980 if (ptr_reg) /* pointer += K */
5981 return adjust_ptr_min_max_vals(env, insn,
5985 /* Got here implies adding two SCALAR_VALUEs */
5986 if (WARN_ON_ONCE(ptr_reg)) {
5987 print_verifier_state(env, state);
5988 verbose(env, "verifier internal error: unexpected ptr_reg\n");
5991 if (WARN_ON(!src_reg)) {
5992 print_verifier_state(env, state);
5993 verbose(env, "verifier internal error: no src_reg\n");
5996 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
5999 /* check validity of 32-bit and 64-bit arithmetic operations */
6000 static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
6002 struct bpf_reg_state *regs = cur_regs(env);
6003 u8 opcode = BPF_OP(insn->code);
6006 if (opcode == BPF_END || opcode == BPF_NEG) {
6007 if (opcode == BPF_NEG) {
6008 if (BPF_SRC(insn->code) != 0 ||
6009 insn->src_reg != BPF_REG_0 ||
6010 insn->off != 0 || insn->imm != 0) {
6011 verbose(env, "BPF_NEG uses reserved fields\n");
6015 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
6016 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
6017 BPF_CLASS(insn->code) == BPF_ALU64) {
6018 verbose(env, "BPF_END uses reserved fields\n");
6023 /* check src operand */
6024 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
6028 if (is_pointer_value(env, insn->dst_reg)) {
6029 verbose(env, "R%d pointer arithmetic prohibited\n",
6034 /* check dest operand */
6035 err = check_reg_arg(env, insn->dst_reg, DST_OP);
6039 } else if (opcode == BPF_MOV) {
6041 if (BPF_SRC(insn->code) == BPF_X) {
6042 if (insn->imm != 0 || insn->off != 0) {
6043 verbose(env, "BPF_MOV uses reserved fields\n");
6047 /* check src operand */
6048 err = check_reg_arg(env, insn->src_reg, SRC_OP);
6052 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
6053 verbose(env, "BPF_MOV uses reserved fields\n");
6058 /* check dest operand, mark as required later */
6059 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
6063 if (BPF_SRC(insn->code) == BPF_X) {
6064 struct bpf_reg_state *src_reg = regs + insn->src_reg;
6065 struct bpf_reg_state *dst_reg = regs + insn->dst_reg;
6067 if (BPF_CLASS(insn->code) == BPF_ALU64) {
6069 * copy register state to dest reg
6071 *dst_reg = *src_reg;
6072 dst_reg->live |= REG_LIVE_WRITTEN;
6073 dst_reg->subreg_def = DEF_NOT_SUBREG;
6076 if (is_pointer_value(env, insn->src_reg)) {
6078 "R%d partial copy of pointer\n",
6081 } else if (src_reg->type == SCALAR_VALUE) {
6082 *dst_reg = *src_reg;
6083 dst_reg->live |= REG_LIVE_WRITTEN;
6084 dst_reg->subreg_def = env->insn_idx + 1;
6086 mark_reg_unknown(env, regs,
6089 zext_32_to_64(dst_reg);
6093 * remember the value we stored into this reg
6095 /* clear any state __mark_reg_known doesn't set */
6096 mark_reg_unknown(env, regs, insn->dst_reg);
6097 regs[insn->dst_reg].type = SCALAR_VALUE;
6098 if (BPF_CLASS(insn->code) == BPF_ALU64) {
6099 __mark_reg_known(regs + insn->dst_reg,
6102 __mark_reg_known(regs + insn->dst_reg,
6107 } else if (opcode > BPF_END) {
6108 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
6111 } else { /* all other ALU ops: and, sub, xor, add, ... */
6113 if (BPF_SRC(insn->code) == BPF_X) {
6114 if (insn->imm != 0 || insn->off != 0) {
6115 verbose(env, "BPF_ALU uses reserved fields\n");
6118 /* check src1 operand */
6119 err = check_reg_arg(env, insn->src_reg, SRC_OP);
6123 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
6124 verbose(env, "BPF_ALU uses reserved fields\n");
6129 /* check src2 operand */
6130 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
6134 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
6135 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
6136 verbose(env, "div by zero\n");
6140 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
6141 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
6142 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
6144 if (insn->imm < 0 || insn->imm >= size) {
6145 verbose(env, "invalid shift %d\n", insn->imm);
6150 /* check dest operand */
6151 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
6155 return adjust_reg_min_max_vals(env, insn);
6161 static void __find_good_pkt_pointers(struct bpf_func_state *state,
6162 struct bpf_reg_state *dst_reg,
6163 enum bpf_reg_type type, u16 new_range)
6165 struct bpf_reg_state *reg;
6168 for (i = 0; i < MAX_BPF_REG; i++) {
6169 reg = &state->regs[i];
6170 if (reg->type == type && reg->id == dst_reg->id)
6171 /* keep the maximum range already checked */
6172 reg->range = max(reg->range, new_range);
6175 bpf_for_each_spilled_reg(i, state, reg) {
6178 if (reg->type == type && reg->id == dst_reg->id)
6179 reg->range = max(reg->range, new_range);
6183 static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
6184 struct bpf_reg_state *dst_reg,
6185 enum bpf_reg_type type,
6186 bool range_right_open)
6191 if (dst_reg->off < 0 ||
6192 (dst_reg->off == 0 && range_right_open))
6193 /* This doesn't give us any range */
6196 if (dst_reg->umax_value > MAX_PACKET_OFF ||
6197 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
6198 /* Risk of overflow. For instance, ptr + (1<<63) may be less
6199 * than pkt_end, but that's because it's also less than pkt.
6203 new_range = dst_reg->off;
6204 if (range_right_open)
6207 /* Examples for register markings:
6209 * pkt_data in dst register:
6213 * if (r2 > pkt_end) goto <handle exception>
6218 * if (r2 < pkt_end) goto <access okay>
6219 * <handle exception>
6222 * r2 == dst_reg, pkt_end == src_reg
6223 * r2=pkt(id=n,off=8,r=0)
6224 * r3=pkt(id=n,off=0,r=0)
6226 * pkt_data in src register:
6230 * if (pkt_end >= r2) goto <access okay>
6231 * <handle exception>
6235 * if (pkt_end <= r2) goto <handle exception>
6239 * pkt_end == dst_reg, r2 == src_reg
6240 * r2=pkt(id=n,off=8,r=0)
6241 * r3=pkt(id=n,off=0,r=0)
6243 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
6244 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
6245 * and [r3, r3 + 8-1) respectively is safe to access depending on
6249 /* If our ids match, then we must have the same max_value. And we
6250 * don't care about the other reg's fixed offset, since if it's too big
6251 * the range won't allow anything.
6252 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
6254 for (i = 0; i <= vstate->curframe; i++)
6255 __find_good_pkt_pointers(vstate->frame[i], dst_reg, type,
6259 static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode)
6261 struct tnum subreg = tnum_subreg(reg->var_off);
6262 s32 sval = (s32)val;
6266 if (tnum_is_const(subreg))
6267 return !!tnum_equals_const(subreg, val);
6270 if (tnum_is_const(subreg))
6271 return !tnum_equals_const(subreg, val);
6274 if ((~subreg.mask & subreg.value) & val)
6276 if (!((subreg.mask | subreg.value) & val))
6280 if (reg->u32_min_value > val)
6282 else if (reg->u32_max_value <= val)
6286 if (reg->s32_min_value > sval)
6288 else if (reg->s32_max_value < sval)
6292 if (reg->u32_max_value < val)
6294 else if (reg->u32_min_value >= val)
6298 if (reg->s32_max_value < sval)
6300 else if (reg->s32_min_value >= sval)
6304 if (reg->u32_min_value >= val)
6306 else if (reg->u32_max_value < val)
6310 if (reg->s32_min_value >= sval)
6312 else if (reg->s32_max_value < sval)
6316 if (reg->u32_max_value <= val)
6318 else if (reg->u32_min_value > val)
6322 if (reg->s32_max_value <= sval)
6324 else if (reg->s32_min_value > sval)
6333 static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode)
6335 s64 sval = (s64)val;
6339 if (tnum_is_const(reg->var_off))
6340 return !!tnum_equals_const(reg->var_off, val);
6343 if (tnum_is_const(reg->var_off))
6344 return !tnum_equals_const(reg->var_off, val);
6347 if ((~reg->var_off.mask & reg->var_off.value) & val)
6349 if (!((reg->var_off.mask | reg->var_off.value) & val))
6353 if (reg->umin_value > val)
6355 else if (reg->umax_value <= val)
6359 if (reg->smin_value > sval)
6361 else if (reg->smax_value < sval)
6365 if (reg->umax_value < val)
6367 else if (reg->umin_value >= val)
6371 if (reg->smax_value < sval)
6373 else if (reg->smin_value >= sval)
6377 if (reg->umin_value >= val)
6379 else if (reg->umax_value < val)
6383 if (reg->smin_value >= sval)
6385 else if (reg->smax_value < sval)
6389 if (reg->umax_value <= val)
6391 else if (reg->umin_value > val)
6395 if (reg->smax_value <= sval)
6397 else if (reg->smin_value > sval)
6405 /* compute branch direction of the expression "if (reg opcode val) goto target;"
6407 * 1 - branch will be taken and "goto target" will be executed
6408 * 0 - branch will not be taken and fall-through to next insn
6409 * -1 - unknown. Example: "if (reg < 5)" is unknown when register value
6412 static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode,
6415 if (__is_pointer_value(false, reg)) {
6416 if (!reg_type_not_null(reg->type))
6419 /* If pointer is valid tests against zero will fail so we can
6420 * use this to direct branch taken.
6436 return is_branch32_taken(reg, val, opcode);
6437 return is_branch64_taken(reg, val, opcode);
6440 /* Adjusts the register min/max values in the case that the dst_reg is the
6441 * variable register that we are working on, and src_reg is a constant or we're
6442 * simply doing a BPF_K check.
6443 * In JEQ/JNE cases we also adjust the var_off values.
6445 static void reg_set_min_max(struct bpf_reg_state *true_reg,
6446 struct bpf_reg_state *false_reg,
6448 u8 opcode, bool is_jmp32)
6450 struct tnum false_32off = tnum_subreg(false_reg->var_off);
6451 struct tnum false_64off = false_reg->var_off;
6452 struct tnum true_32off = tnum_subreg(true_reg->var_off);
6453 struct tnum true_64off = true_reg->var_off;
6454 s64 sval = (s64)val;
6455 s32 sval32 = (s32)val32;
6457 /* If the dst_reg is a pointer, we can't learn anything about its
6458 * variable offset from the compare (unless src_reg were a pointer into
6459 * the same object, but we don't bother with that.
6460 * Since false_reg and true_reg have the same type by construction, we
6461 * only need to check one of them for pointerness.
6463 if (__is_pointer_value(false, false_reg))
6470 struct bpf_reg_state *reg =
6471 opcode == BPF_JEQ ? true_reg : false_reg;
6473 /* For BPF_JEQ, if this is false we know nothing Jon Snow, but
6474 * if it is true we know the value for sure. Likewise for
6478 __mark_reg32_known(reg, val32);
6480 __mark_reg_known(reg, val);
6485 false_32off = tnum_and(false_32off, tnum_const(~val32));
6486 if (is_power_of_2(val32))
6487 true_32off = tnum_or(true_32off,
6490 false_64off = tnum_and(false_64off, tnum_const(~val));
6491 if (is_power_of_2(val))
6492 true_64off = tnum_or(true_64off,
6500 u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1;
6501 u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32;
6503 false_reg->u32_max_value = min(false_reg->u32_max_value,
6505 true_reg->u32_min_value = max(true_reg->u32_min_value,
6508 u64 false_umax = opcode == BPF_JGT ? val : val - 1;
6509 u64 true_umin = opcode == BPF_JGT ? val + 1 : val;
6511 false_reg->umax_value = min(false_reg->umax_value, false_umax);
6512 true_reg->umin_value = max(true_reg->umin_value, true_umin);
6520 s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1;
6521 s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32;
6523 false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax);
6524 true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin);
6526 s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1;
6527 s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval;
6529 false_reg->smax_value = min(false_reg->smax_value, false_smax);
6530 true_reg->smin_value = max(true_reg->smin_value, true_smin);
6538 u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1;
6539 u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32;
6541 false_reg->u32_min_value = max(false_reg->u32_min_value,
6543 true_reg->u32_max_value = min(true_reg->u32_max_value,
6546 u64 false_umin = opcode == BPF_JLT ? val : val + 1;
6547 u64 true_umax = opcode == BPF_JLT ? val - 1 : val;
6549 false_reg->umin_value = max(false_reg->umin_value, false_umin);
6550 true_reg->umax_value = min(true_reg->umax_value, true_umax);
6558 s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1;
6559 s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32;
6561 false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin);
6562 true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax);
6564 s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1;
6565 s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval;
6567 false_reg->smin_value = max(false_reg->smin_value, false_smin);
6568 true_reg->smax_value = min(true_reg->smax_value, true_smax);
6577 false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off),
6578 tnum_subreg(false_32off));
6579 true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off),
6580 tnum_subreg(true_32off));
6581 __reg_combine_32_into_64(false_reg);
6582 __reg_combine_32_into_64(true_reg);
6584 false_reg->var_off = false_64off;
6585 true_reg->var_off = true_64off;
6586 __reg_combine_64_into_32(false_reg);
6587 __reg_combine_64_into_32(true_reg);
6591 /* Same as above, but for the case that dst_reg holds a constant and src_reg is
6594 static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
6595 struct bpf_reg_state *false_reg,
6597 u8 opcode, bool is_jmp32)
6599 /* How can we transform "a <op> b" into "b <op> a"? */
6600 static const u8 opcode_flip[16] = {
6601 /* these stay the same */
6602 [BPF_JEQ >> 4] = BPF_JEQ,
6603 [BPF_JNE >> 4] = BPF_JNE,
6604 [BPF_JSET >> 4] = BPF_JSET,
6605 /* these swap "lesser" and "greater" (L and G in the opcodes) */
6606 [BPF_JGE >> 4] = BPF_JLE,
6607 [BPF_JGT >> 4] = BPF_JLT,
6608 [BPF_JLE >> 4] = BPF_JGE,
6609 [BPF_JLT >> 4] = BPF_JGT,
6610 [BPF_JSGE >> 4] = BPF_JSLE,
6611 [BPF_JSGT >> 4] = BPF_JSLT,
6612 [BPF_JSLE >> 4] = BPF_JSGE,
6613 [BPF_JSLT >> 4] = BPF_JSGT
6615 opcode = opcode_flip[opcode >> 4];
6616 /* This uses zero as "not present in table"; luckily the zero opcode,
6617 * BPF_JA, can't get here.
6620 reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32);
6623 /* Regs are known to be equal, so intersect their min/max/var_off */
6624 static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
6625 struct bpf_reg_state *dst_reg)
6627 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
6628 dst_reg->umin_value);
6629 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
6630 dst_reg->umax_value);
6631 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
6632 dst_reg->smin_value);
6633 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
6634 dst_reg->smax_value);
6635 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
6637 /* We might have learned new bounds from the var_off. */
6638 __update_reg_bounds(src_reg);
6639 __update_reg_bounds(dst_reg);
6640 /* We might have learned something about the sign bit. */
6641 __reg_deduce_bounds(src_reg);
6642 __reg_deduce_bounds(dst_reg);
6643 /* We might have learned some bits from the bounds. */
6644 __reg_bound_offset(src_reg);
6645 __reg_bound_offset(dst_reg);
6646 /* Intersecting with the old var_off might have improved our bounds
6647 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
6648 * then new var_off is (0; 0x7f...fc) which improves our umax.
6650 __update_reg_bounds(src_reg);
6651 __update_reg_bounds(dst_reg);
6654 static void reg_combine_min_max(struct bpf_reg_state *true_src,
6655 struct bpf_reg_state *true_dst,
6656 struct bpf_reg_state *false_src,
6657 struct bpf_reg_state *false_dst,
6662 __reg_combine_min_max(true_src, true_dst);
6665 __reg_combine_min_max(false_src, false_dst);
6670 static void mark_ptr_or_null_reg(struct bpf_func_state *state,
6671 struct bpf_reg_state *reg, u32 id,
6674 if (reg_type_may_be_null(reg->type) && reg->id == id) {
6675 /* Old offset (both fixed and variable parts) should
6676 * have been known-zero, because we don't allow pointer
6677 * arithmetic on pointers that might be NULL.
6679 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
6680 !tnum_equals_const(reg->var_off, 0) ||
6682 __mark_reg_known_zero(reg);
6686 reg->type = SCALAR_VALUE;
6687 } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
6688 const struct bpf_map *map = reg->map_ptr;
6690 if (map->inner_map_meta) {
6691 reg->type = CONST_PTR_TO_MAP;
6692 reg->map_ptr = map->inner_map_meta;
6693 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
6694 reg->type = PTR_TO_XDP_SOCK;
6695 } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP ||
6696 map->map_type == BPF_MAP_TYPE_SOCKHASH) {
6697 reg->type = PTR_TO_SOCKET;
6699 reg->type = PTR_TO_MAP_VALUE;
6701 } else if (reg->type == PTR_TO_SOCKET_OR_NULL) {
6702 reg->type = PTR_TO_SOCKET;
6703 } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) {
6704 reg->type = PTR_TO_SOCK_COMMON;
6705 } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) {
6706 reg->type = PTR_TO_TCP_SOCK;
6707 } else if (reg->type == PTR_TO_BTF_ID_OR_NULL) {
6708 reg->type = PTR_TO_BTF_ID;
6709 } else if (reg->type == PTR_TO_MEM_OR_NULL) {
6710 reg->type = PTR_TO_MEM;
6713 /* We don't need id and ref_obj_id from this point
6714 * onwards anymore, thus we should better reset it,
6715 * so that state pruning has chances to take effect.
6718 reg->ref_obj_id = 0;
6719 } else if (!reg_may_point_to_spin_lock(reg)) {
6720 /* For not-NULL ptr, reg->ref_obj_id will be reset
6721 * in release_reg_references().
6723 * reg->id is still used by spin_lock ptr. Other
6724 * than spin_lock ptr type, reg->id can be reset.
6731 static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id,
6734 struct bpf_reg_state *reg;
6737 for (i = 0; i < MAX_BPF_REG; i++)
6738 mark_ptr_or_null_reg(state, &state->regs[i], id, is_null);
6740 bpf_for_each_spilled_reg(i, state, reg) {
6743 mark_ptr_or_null_reg(state, reg, id, is_null);
6747 /* The logic is similar to find_good_pkt_pointers(), both could eventually
6748 * be folded together at some point.
6750 static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno,
6753 struct bpf_func_state *state = vstate->frame[vstate->curframe];
6754 struct bpf_reg_state *regs = state->regs;
6755 u32 ref_obj_id = regs[regno].ref_obj_id;
6756 u32 id = regs[regno].id;
6759 if (ref_obj_id && ref_obj_id == id && is_null)
6760 /* regs[regno] is in the " == NULL" branch.
6761 * No one could have freed the reference state before
6762 * doing the NULL check.
6764 WARN_ON_ONCE(release_reference_state(state, id));
6766 for (i = 0; i <= vstate->curframe; i++)
6767 __mark_ptr_or_null_regs(vstate->frame[i], id, is_null);
6770 static bool try_match_pkt_pointers(const struct bpf_insn *insn,
6771 struct bpf_reg_state *dst_reg,
6772 struct bpf_reg_state *src_reg,
6773 struct bpf_verifier_state *this_branch,
6774 struct bpf_verifier_state *other_branch)
6776 if (BPF_SRC(insn->code) != BPF_X)
6779 /* Pointers are always 64-bit. */
6780 if (BPF_CLASS(insn->code) == BPF_JMP32)
6783 switch (BPF_OP(insn->code)) {
6785 if ((dst_reg->type == PTR_TO_PACKET &&
6786 src_reg->type == PTR_TO_PACKET_END) ||
6787 (dst_reg->type == PTR_TO_PACKET_META &&
6788 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
6789 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
6790 find_good_pkt_pointers(this_branch, dst_reg,
6791 dst_reg->type, false);
6792 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
6793 src_reg->type == PTR_TO_PACKET) ||
6794 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
6795 src_reg->type == PTR_TO_PACKET_META)) {
6796 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
6797 find_good_pkt_pointers(other_branch, src_reg,
6798 src_reg->type, true);
6804 if ((dst_reg->type == PTR_TO_PACKET &&
6805 src_reg->type == PTR_TO_PACKET_END) ||
6806 (dst_reg->type == PTR_TO_PACKET_META &&
6807 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
6808 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
6809 find_good_pkt_pointers(other_branch, dst_reg,
6810 dst_reg->type, true);
6811 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
6812 src_reg->type == PTR_TO_PACKET) ||
6813 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
6814 src_reg->type == PTR_TO_PACKET_META)) {
6815 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
6816 find_good_pkt_pointers(this_branch, src_reg,
6817 src_reg->type, false);
6823 if ((dst_reg->type == PTR_TO_PACKET &&
6824 src_reg->type == PTR_TO_PACKET_END) ||
6825 (dst_reg->type == PTR_TO_PACKET_META &&
6826 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
6827 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
6828 find_good_pkt_pointers(this_branch, dst_reg,
6829 dst_reg->type, true);
6830 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
6831 src_reg->type == PTR_TO_PACKET) ||
6832 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
6833 src_reg->type == PTR_TO_PACKET_META)) {
6834 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
6835 find_good_pkt_pointers(other_branch, src_reg,
6836 src_reg->type, false);
6842 if ((dst_reg->type == PTR_TO_PACKET &&
6843 src_reg->type == PTR_TO_PACKET_END) ||
6844 (dst_reg->type == PTR_TO_PACKET_META &&
6845 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
6846 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
6847 find_good_pkt_pointers(other_branch, dst_reg,
6848 dst_reg->type, false);
6849 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
6850 src_reg->type == PTR_TO_PACKET) ||
6851 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
6852 src_reg->type == PTR_TO_PACKET_META)) {
6853 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
6854 find_good_pkt_pointers(this_branch, src_reg,
6855 src_reg->type, true);
6867 static int check_cond_jmp_op(struct bpf_verifier_env *env,
6868 struct bpf_insn *insn, int *insn_idx)
6870 struct bpf_verifier_state *this_branch = env->cur_state;
6871 struct bpf_verifier_state *other_branch;
6872 struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
6873 struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL;
6874 u8 opcode = BPF_OP(insn->code);
6879 /* Only conditional jumps are expected to reach here. */
6880 if (opcode == BPF_JA || opcode > BPF_JSLE) {
6881 verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode);
6885 if (BPF_SRC(insn->code) == BPF_X) {
6886 if (insn->imm != 0) {
6887 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
6891 /* check src1 operand */
6892 err = check_reg_arg(env, insn->src_reg, SRC_OP);
6896 if (is_pointer_value(env, insn->src_reg)) {
6897 verbose(env, "R%d pointer comparison prohibited\n",
6901 src_reg = ®s[insn->src_reg];
6903 if (insn->src_reg != BPF_REG_0) {
6904 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
6909 /* check src2 operand */
6910 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
6914 dst_reg = ®s[insn->dst_reg];
6915 is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
6917 if (BPF_SRC(insn->code) == BPF_K) {
6918 pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32);
6919 } else if (src_reg->type == SCALAR_VALUE &&
6920 is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) {
6921 pred = is_branch_taken(dst_reg,
6922 tnum_subreg(src_reg->var_off).value,
6925 } else if (src_reg->type == SCALAR_VALUE &&
6926 !is_jmp32 && tnum_is_const(src_reg->var_off)) {
6927 pred = is_branch_taken(dst_reg,
6928 src_reg->var_off.value,
6934 /* If we get here with a dst_reg pointer type it is because
6935 * above is_branch_taken() special cased the 0 comparison.
6937 if (!__is_pointer_value(false, dst_reg))
6938 err = mark_chain_precision(env, insn->dst_reg);
6939 if (BPF_SRC(insn->code) == BPF_X && !err)
6940 err = mark_chain_precision(env, insn->src_reg);
6945 /* only follow the goto, ignore fall-through */
6946 *insn_idx += insn->off;
6948 } else if (pred == 0) {
6949 /* only follow fall-through branch, since
6950 * that's where the program will go
6955 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx,
6959 other_branch_regs = other_branch->frame[other_branch->curframe]->regs;
6961 /* detect if we are comparing against a constant value so we can adjust
6962 * our min/max values for our dst register.
6963 * this is only legit if both are scalars (or pointers to the same
6964 * object, I suppose, but we don't support that right now), because
6965 * otherwise the different base pointers mean the offsets aren't
6968 if (BPF_SRC(insn->code) == BPF_X) {
6969 struct bpf_reg_state *src_reg = ®s[insn->src_reg];
6971 if (dst_reg->type == SCALAR_VALUE &&
6972 src_reg->type == SCALAR_VALUE) {
6973 if (tnum_is_const(src_reg->var_off) ||
6975 tnum_is_const(tnum_subreg(src_reg->var_off))))
6976 reg_set_min_max(&other_branch_regs[insn->dst_reg],
6978 src_reg->var_off.value,
6979 tnum_subreg(src_reg->var_off).value,
6981 else if (tnum_is_const(dst_reg->var_off) ||
6983 tnum_is_const(tnum_subreg(dst_reg->var_off))))
6984 reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
6986 dst_reg->var_off.value,
6987 tnum_subreg(dst_reg->var_off).value,
6989 else if (!is_jmp32 &&
6990 (opcode == BPF_JEQ || opcode == BPF_JNE))
6991 /* Comparing for equality, we can combine knowledge */
6992 reg_combine_min_max(&other_branch_regs[insn->src_reg],
6993 &other_branch_regs[insn->dst_reg],
6994 src_reg, dst_reg, opcode);
6996 } else if (dst_reg->type == SCALAR_VALUE) {
6997 reg_set_min_max(&other_branch_regs[insn->dst_reg],
6998 dst_reg, insn->imm, (u32)insn->imm,
7002 /* detect if R == 0 where R is returned from bpf_map_lookup_elem().
7003 * NOTE: these optimizations below are related with pointer comparison
7004 * which will never be JMP32.
7006 if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K &&
7007 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
7008 reg_type_may_be_null(dst_reg->type)) {
7009 /* Mark all identical registers in each branch as either
7010 * safe or unknown depending R == 0 or R != 0 conditional.
7012 mark_ptr_or_null_regs(this_branch, insn->dst_reg,
7014 mark_ptr_or_null_regs(other_branch, insn->dst_reg,
7016 } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg],
7017 this_branch, other_branch) &&
7018 is_pointer_value(env, insn->dst_reg)) {
7019 verbose(env, "R%d pointer comparison prohibited\n",
7023 if (env->log.level & BPF_LOG_LEVEL)
7024 print_verifier_state(env, this_branch->frame[this_branch->curframe]);
7028 /* verify BPF_LD_IMM64 instruction */
7029 static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
7031 struct bpf_insn_aux_data *aux = cur_aux(env);
7032 struct bpf_reg_state *regs = cur_regs(env);
7033 struct bpf_map *map;
7036 if (BPF_SIZE(insn->code) != BPF_DW) {
7037 verbose(env, "invalid BPF_LD_IMM insn\n");
7040 if (insn->off != 0) {
7041 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
7045 err = check_reg_arg(env, insn->dst_reg, DST_OP);
7049 if (insn->src_reg == 0) {
7050 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
7052 regs[insn->dst_reg].type = SCALAR_VALUE;
7053 __mark_reg_known(®s[insn->dst_reg], imm);
7057 map = env->used_maps[aux->map_index];
7058 mark_reg_known_zero(env, regs, insn->dst_reg);
7059 regs[insn->dst_reg].map_ptr = map;
7061 if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) {
7062 regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
7063 regs[insn->dst_reg].off = aux->map_off;
7064 if (map_value_has_spin_lock(map))
7065 regs[insn->dst_reg].id = ++env->id_gen;
7066 } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
7067 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
7069 verbose(env, "bpf verifier is misconfigured\n");
7076 static bool may_access_skb(enum bpf_prog_type type)
7079 case BPF_PROG_TYPE_SOCKET_FILTER:
7080 case BPF_PROG_TYPE_SCHED_CLS:
7081 case BPF_PROG_TYPE_SCHED_ACT:
7088 /* verify safety of LD_ABS|LD_IND instructions:
7089 * - they can only appear in the programs where ctx == skb
7090 * - since they are wrappers of function calls, they scratch R1-R5 registers,
7091 * preserve R6-R9, and store return value into R0
7094 * ctx == skb == R6 == CTX
7097 * SRC == any register
7098 * IMM == 32-bit immediate
7101 * R0 - 8/16/32-bit skb data converted to cpu endianness
7103 static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
7105 struct bpf_reg_state *regs = cur_regs(env);
7106 static const int ctx_reg = BPF_REG_6;
7107 u8 mode = BPF_MODE(insn->code);
7110 if (!may_access_skb(env->prog->type)) {
7111 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
7115 if (!env->ops->gen_ld_abs) {
7116 verbose(env, "bpf verifier is misconfigured\n");
7120 if (env->subprog_cnt > 1) {
7121 /* when program has LD_ABS insn JITs and interpreter assume
7122 * that r1 == ctx == skb which is not the case for callees
7123 * that can have arbitrary arguments. It's problematic
7124 * for main prog as well since JITs would need to analyze
7125 * all functions in order to make proper register save/restore
7126 * decisions in the main prog. Hence disallow LD_ABS with calls
7128 verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
7132 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
7133 BPF_SIZE(insn->code) == BPF_DW ||
7134 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
7135 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
7139 /* check whether implicit source operand (register R6) is readable */
7140 err = check_reg_arg(env, ctx_reg, SRC_OP);
7144 /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as
7145 * gen_ld_abs() may terminate the program at runtime, leading to
7148 err = check_reference_leak(env);
7150 verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n");
7154 if (env->cur_state->active_spin_lock) {
7155 verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n");
7159 if (regs[ctx_reg].type != PTR_TO_CTX) {
7161 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
7165 if (mode == BPF_IND) {
7166 /* check explicit source operand */
7167 err = check_reg_arg(env, insn->src_reg, SRC_OP);
7172 err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg);
7176 /* reset caller saved regs to unreadable */
7177 for (i = 0; i < CALLER_SAVED_REGS; i++) {
7178 mark_reg_not_init(env, regs, caller_saved[i]);
7179 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
7182 /* mark destination R0 register as readable, since it contains
7183 * the value fetched from the packet.
7184 * Already marked as written above.
7186 mark_reg_unknown(env, regs, BPF_REG_0);
7187 /* ld_abs load up to 32-bit skb data. */
7188 regs[BPF_REG_0].subreg_def = env->insn_idx + 1;
7192 static int check_return_code(struct bpf_verifier_env *env)
7194 struct tnum enforce_attach_type_range = tnum_unknown;
7195 const struct bpf_prog *prog = env->prog;
7196 struct bpf_reg_state *reg;
7197 struct tnum range = tnum_range(0, 1);
7200 /* LSM and struct_ops func-ptr's return type could be "void" */
7201 if ((env->prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
7202 env->prog->type == BPF_PROG_TYPE_LSM) &&
7203 !prog->aux->attach_func_proto->type)
7206 /* eBPF calling convetion is such that R0 is used
7207 * to return the value from eBPF program.
7208 * Make sure that it's readable at this time
7209 * of bpf_exit, which means that program wrote
7210 * something into it earlier
7212 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
7216 if (is_pointer_value(env, BPF_REG_0)) {
7217 verbose(env, "R0 leaks addr as return value\n");
7221 switch (env->prog->type) {
7222 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
7223 if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG ||
7224 env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG ||
7225 env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME ||
7226 env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME ||
7227 env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME ||
7228 env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME)
7229 range = tnum_range(1, 1);
7231 case BPF_PROG_TYPE_CGROUP_SKB:
7232 if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) {
7233 range = tnum_range(0, 3);
7234 enforce_attach_type_range = tnum_range(2, 3);
7237 case BPF_PROG_TYPE_CGROUP_SOCK:
7238 case BPF_PROG_TYPE_SOCK_OPS:
7239 case BPF_PROG_TYPE_CGROUP_DEVICE:
7240 case BPF_PROG_TYPE_CGROUP_SYSCTL:
7241 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
7243 case BPF_PROG_TYPE_RAW_TRACEPOINT:
7244 if (!env->prog->aux->attach_btf_id)
7246 range = tnum_const(0);
7248 case BPF_PROG_TYPE_TRACING:
7249 switch (env->prog->expected_attach_type) {
7250 case BPF_TRACE_FENTRY:
7251 case BPF_TRACE_FEXIT:
7252 range = tnum_const(0);
7254 case BPF_TRACE_RAW_TP:
7255 case BPF_MODIFY_RETURN:
7257 case BPF_TRACE_ITER:
7263 case BPF_PROG_TYPE_EXT:
7264 /* freplace program can return anything as its return value
7265 * depends on the to-be-replaced kernel func or bpf program.
7271 reg = cur_regs(env) + BPF_REG_0;
7272 if (reg->type != SCALAR_VALUE) {
7273 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
7274 reg_type_str[reg->type]);
7278 if (!tnum_in(range, reg->var_off)) {
7281 verbose(env, "At program exit the register R0 ");
7282 if (!tnum_is_unknown(reg->var_off)) {
7283 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
7284 verbose(env, "has value %s", tn_buf);
7286 verbose(env, "has unknown scalar value");
7288 tnum_strn(tn_buf, sizeof(tn_buf), range);
7289 verbose(env, " should have been in %s\n", tn_buf);
7293 if (!tnum_is_unknown(enforce_attach_type_range) &&
7294 tnum_in(enforce_attach_type_range, reg->var_off))
7295 env->prog->enforce_expected_attach_type = 1;
7299 /* non-recursive DFS pseudo code
7300 * 1 procedure DFS-iterative(G,v):
7301 * 2 label v as discovered
7302 * 3 let S be a stack
7304 * 5 while S is not empty
7306 * 7 if t is what we're looking for:
7308 * 9 for all edges e in G.adjacentEdges(t) do
7309 * 10 if edge e is already labelled
7310 * 11 continue with the next edge
7311 * 12 w <- G.adjacentVertex(t,e)
7312 * 13 if vertex w is not discovered and not explored
7313 * 14 label e as tree-edge
7314 * 15 label w as discovered
7317 * 18 else if vertex w is discovered
7318 * 19 label e as back-edge
7320 * 21 // vertex w is explored
7321 * 22 label e as forward- or cross-edge
7322 * 23 label t as explored
7327 * 0x11 - discovered and fall-through edge labelled
7328 * 0x12 - discovered and fall-through and branch edges labelled
7339 static u32 state_htab_size(struct bpf_verifier_env *env)
7341 return env->prog->len;
7344 static struct bpf_verifier_state_list **explored_state(
7345 struct bpf_verifier_env *env,
7348 struct bpf_verifier_state *cur = env->cur_state;
7349 struct bpf_func_state *state = cur->frame[cur->curframe];
7351 return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)];
7354 static void init_explored_state(struct bpf_verifier_env *env, int idx)
7356 env->insn_aux_data[idx].prune_point = true;
7359 /* t, w, e - match pseudo-code above:
7360 * t - index of current instruction
7361 * w - next instruction
7364 static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
7367 int *insn_stack = env->cfg.insn_stack;
7368 int *insn_state = env->cfg.insn_state;
7370 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
7373 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
7376 if (w < 0 || w >= env->prog->len) {
7377 verbose_linfo(env, t, "%d: ", t);
7378 verbose(env, "jump out of range from insn %d to %d\n", t, w);
7383 /* mark branch target for state pruning */
7384 init_explored_state(env, w);
7386 if (insn_state[w] == 0) {
7388 insn_state[t] = DISCOVERED | e;
7389 insn_state[w] = DISCOVERED;
7390 if (env->cfg.cur_stack >= env->prog->len)
7392 insn_stack[env->cfg.cur_stack++] = w;
7394 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
7395 if (loop_ok && env->bpf_capable)
7397 verbose_linfo(env, t, "%d: ", t);
7398 verbose_linfo(env, w, "%d: ", w);
7399 verbose(env, "back-edge from insn %d to %d\n", t, w);
7401 } else if (insn_state[w] == EXPLORED) {
7402 /* forward- or cross-edge */
7403 insn_state[t] = DISCOVERED | e;
7405 verbose(env, "insn state internal bug\n");
7411 /* non-recursive depth-first-search to detect loops in BPF program
7412 * loop == back-edge in directed graph
7414 static int check_cfg(struct bpf_verifier_env *env)
7416 struct bpf_insn *insns = env->prog->insnsi;
7417 int insn_cnt = env->prog->len;
7418 int *insn_stack, *insn_state;
7422 insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
7426 insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
7432 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
7433 insn_stack[0] = 0; /* 0 is the first instruction */
7434 env->cfg.cur_stack = 1;
7437 if (env->cfg.cur_stack == 0)
7439 t = insn_stack[env->cfg.cur_stack - 1];
7441 if (BPF_CLASS(insns[t].code) == BPF_JMP ||
7442 BPF_CLASS(insns[t].code) == BPF_JMP32) {
7443 u8 opcode = BPF_OP(insns[t].code);
7445 if (opcode == BPF_EXIT) {
7447 } else if (opcode == BPF_CALL) {
7448 ret = push_insn(t, t + 1, FALLTHROUGH, env, false);
7453 if (t + 1 < insn_cnt)
7454 init_explored_state(env, t + 1);
7455 if (insns[t].src_reg == BPF_PSEUDO_CALL) {
7456 init_explored_state(env, t);
7457 ret = push_insn(t, t + insns[t].imm + 1, BRANCH,
7464 } else if (opcode == BPF_JA) {
7465 if (BPF_SRC(insns[t].code) != BPF_K) {
7469 /* unconditional jump with single edge */
7470 ret = push_insn(t, t + insns[t].off + 1,
7471 FALLTHROUGH, env, true);
7476 /* unconditional jmp is not a good pruning point,
7477 * but it's marked, since backtracking needs
7478 * to record jmp history in is_state_visited().
7480 init_explored_state(env, t + insns[t].off + 1);
7481 /* tell verifier to check for equivalent states
7482 * after every call and jump
7484 if (t + 1 < insn_cnt)
7485 init_explored_state(env, t + 1);
7487 /* conditional jump with two edges */
7488 init_explored_state(env, t);
7489 ret = push_insn(t, t + 1, FALLTHROUGH, env, true);
7495 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true);
7502 /* all other non-branch instructions with single
7505 ret = push_insn(t, t + 1, FALLTHROUGH, env, false);
7513 insn_state[t] = EXPLORED;
7514 if (env->cfg.cur_stack-- <= 0) {
7515 verbose(env, "pop stack internal bug\n");
7522 for (i = 0; i < insn_cnt; i++) {
7523 if (insn_state[i] != EXPLORED) {
7524 verbose(env, "unreachable insn %d\n", i);
7529 ret = 0; /* cfg looks good */
7534 env->cfg.insn_state = env->cfg.insn_stack = NULL;
7538 /* The minimum supported BTF func info size */
7539 #define MIN_BPF_FUNCINFO_SIZE 8
7540 #define MAX_FUNCINFO_REC_SIZE 252
7542 static int check_btf_func(struct bpf_verifier_env *env,
7543 const union bpf_attr *attr,
7544 union bpf_attr __user *uattr)
7546 u32 i, nfuncs, urec_size, min_size;
7547 u32 krec_size = sizeof(struct bpf_func_info);
7548 struct bpf_func_info *krecord;
7549 struct bpf_func_info_aux *info_aux = NULL;
7550 const struct btf_type *type;
7551 struct bpf_prog *prog;
7552 const struct btf *btf;
7553 void __user *urecord;
7554 u32 prev_offset = 0;
7557 nfuncs = attr->func_info_cnt;
7561 if (nfuncs != env->subprog_cnt) {
7562 verbose(env, "number of funcs in func_info doesn't match number of subprogs\n");
7566 urec_size = attr->func_info_rec_size;
7567 if (urec_size < MIN_BPF_FUNCINFO_SIZE ||
7568 urec_size > MAX_FUNCINFO_REC_SIZE ||
7569 urec_size % sizeof(u32)) {
7570 verbose(env, "invalid func info rec size %u\n", urec_size);
7575 btf = prog->aux->btf;
7577 urecord = u64_to_user_ptr(attr->func_info);
7578 min_size = min_t(u32, krec_size, urec_size);
7580 krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN);
7583 info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN);
7587 for (i = 0; i < nfuncs; i++) {
7588 ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);
7590 if (ret == -E2BIG) {
7591 verbose(env, "nonzero tailing record in func info");
7592 /* set the size kernel expects so loader can zero
7593 * out the rest of the record.
7595 if (put_user(min_size, &uattr->func_info_rec_size))
7601 if (copy_from_user(&krecord[i], urecord, min_size)) {
7606 /* check insn_off */
7608 if (krecord[i].insn_off) {
7610 "nonzero insn_off %u for the first func info record",
7611 krecord[i].insn_off);
7615 } else if (krecord[i].insn_off <= prev_offset) {
7617 "same or smaller insn offset (%u) than previous func info record (%u)",
7618 krecord[i].insn_off, prev_offset);
7623 if (env->subprog_info[i].start != krecord[i].insn_off) {
7624 verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n");
7630 type = btf_type_by_id(btf, krecord[i].type_id);
7631 if (!type || !btf_type_is_func(type)) {
7632 verbose(env, "invalid type id %d in func info",
7633 krecord[i].type_id);
7637 info_aux[i].linkage = BTF_INFO_VLEN(type->info);
7638 prev_offset = krecord[i].insn_off;
7639 urecord += urec_size;
7642 prog->aux->func_info = krecord;
7643 prog->aux->func_info_cnt = nfuncs;
7644 prog->aux->func_info_aux = info_aux;
7653 static void adjust_btf_func(struct bpf_verifier_env *env)
7655 struct bpf_prog_aux *aux = env->prog->aux;
7658 if (!aux->func_info)
7661 for (i = 0; i < env->subprog_cnt; i++)
7662 aux->func_info[i].insn_off = env->subprog_info[i].start;
7665 #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \
7666 sizeof(((struct bpf_line_info *)(0))->line_col))
7667 #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE
7669 static int check_btf_line(struct bpf_verifier_env *env,
7670 const union bpf_attr *attr,
7671 union bpf_attr __user *uattr)
7673 u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0;
7674 struct bpf_subprog_info *sub;
7675 struct bpf_line_info *linfo;
7676 struct bpf_prog *prog;
7677 const struct btf *btf;
7678 void __user *ulinfo;
7681 nr_linfo = attr->line_info_cnt;
7685 rec_size = attr->line_info_rec_size;
7686 if (rec_size < MIN_BPF_LINEINFO_SIZE ||
7687 rec_size > MAX_LINEINFO_REC_SIZE ||
7688 rec_size & (sizeof(u32) - 1))
7691 /* Need to zero it in case the userspace may
7692 * pass in a smaller bpf_line_info object.
7694 linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info),
7695 GFP_KERNEL | __GFP_NOWARN);
7700 btf = prog->aux->btf;
7703 sub = env->subprog_info;
7704 ulinfo = u64_to_user_ptr(attr->line_info);
7705 expected_size = sizeof(struct bpf_line_info);
7706 ncopy = min_t(u32, expected_size, rec_size);
7707 for (i = 0; i < nr_linfo; i++) {
7708 err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size);
7710 if (err == -E2BIG) {
7711 verbose(env, "nonzero tailing record in line_info");
7712 if (put_user(expected_size,
7713 &uattr->line_info_rec_size))
7719 if (copy_from_user(&linfo[i], ulinfo, ncopy)) {
7725 * Check insn_off to ensure
7726 * 1) strictly increasing AND
7727 * 2) bounded by prog->len
7729 * The linfo[0].insn_off == 0 check logically falls into
7730 * the later "missing bpf_line_info for func..." case
7731 * because the first linfo[0].insn_off must be the
7732 * first sub also and the first sub must have
7733 * subprog_info[0].start == 0.
7735 if ((i && linfo[i].insn_off <= prev_offset) ||
7736 linfo[i].insn_off >= prog->len) {
7737 verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n",
7738 i, linfo[i].insn_off, prev_offset,
7744 if (!prog->insnsi[linfo[i].insn_off].code) {
7746 "Invalid insn code at line_info[%u].insn_off\n",
7752 if (!btf_name_by_offset(btf, linfo[i].line_off) ||
7753 !btf_name_by_offset(btf, linfo[i].file_name_off)) {
7754 verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i);
7759 if (s != env->subprog_cnt) {
7760 if (linfo[i].insn_off == sub[s].start) {
7761 sub[s].linfo_idx = i;
7763 } else if (sub[s].start < linfo[i].insn_off) {
7764 verbose(env, "missing bpf_line_info for func#%u\n", s);
7770 prev_offset = linfo[i].insn_off;
7774 if (s != env->subprog_cnt) {
7775 verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n",
7776 env->subprog_cnt - s, s);
7781 prog->aux->linfo = linfo;
7782 prog->aux->nr_linfo = nr_linfo;
7791 static int check_btf_info(struct bpf_verifier_env *env,
7792 const union bpf_attr *attr,
7793 union bpf_attr __user *uattr)
7798 if (!attr->func_info_cnt && !attr->line_info_cnt)
7801 btf = btf_get_by_fd(attr->prog_btf_fd);
7803 return PTR_ERR(btf);
7804 env->prog->aux->btf = btf;
7806 err = check_btf_func(env, attr, uattr);
7810 err = check_btf_line(env, attr, uattr);
7817 /* check %cur's range satisfies %old's */
7818 static bool range_within(struct bpf_reg_state *old,
7819 struct bpf_reg_state *cur)
7821 return old->umin_value <= cur->umin_value &&
7822 old->umax_value >= cur->umax_value &&
7823 old->smin_value <= cur->smin_value &&
7824 old->smax_value >= cur->smax_value;
7827 /* Maximum number of register states that can exist at once */
7828 #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
7834 /* If in the old state two registers had the same id, then they need to have
7835 * the same id in the new state as well. But that id could be different from
7836 * the old state, so we need to track the mapping from old to new ids.
7837 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
7838 * regs with old id 5 must also have new id 9 for the new state to be safe. But
7839 * regs with a different old id could still have new id 9, we don't care about
7841 * So we look through our idmap to see if this old id has been seen before. If
7842 * so, we require the new id to match; otherwise, we add the id pair to the map.
7844 static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
7848 for (i = 0; i < ID_MAP_SIZE; i++) {
7849 if (!idmap[i].old) {
7850 /* Reached an empty slot; haven't seen this id before */
7851 idmap[i].old = old_id;
7852 idmap[i].cur = cur_id;
7855 if (idmap[i].old == old_id)
7856 return idmap[i].cur == cur_id;
7858 /* We ran out of idmap slots, which should be impossible */
7863 static void clean_func_state(struct bpf_verifier_env *env,
7864 struct bpf_func_state *st)
7866 enum bpf_reg_liveness live;
7869 for (i = 0; i < BPF_REG_FP; i++) {
7870 live = st->regs[i].live;
7871 /* liveness must not touch this register anymore */
7872 st->regs[i].live |= REG_LIVE_DONE;
7873 if (!(live & REG_LIVE_READ))
7874 /* since the register is unused, clear its state
7875 * to make further comparison simpler
7877 __mark_reg_not_init(env, &st->regs[i]);
7880 for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) {
7881 live = st->stack[i].spilled_ptr.live;
7882 /* liveness must not touch this stack slot anymore */
7883 st->stack[i].spilled_ptr.live |= REG_LIVE_DONE;
7884 if (!(live & REG_LIVE_READ)) {
7885 __mark_reg_not_init(env, &st->stack[i].spilled_ptr);
7886 for (j = 0; j < BPF_REG_SIZE; j++)
7887 st->stack[i].slot_type[j] = STACK_INVALID;
7892 static void clean_verifier_state(struct bpf_verifier_env *env,
7893 struct bpf_verifier_state *st)
7897 if (st->frame[0]->regs[0].live & REG_LIVE_DONE)
7898 /* all regs in this state in all frames were already marked */
7901 for (i = 0; i <= st->curframe; i++)
7902 clean_func_state(env, st->frame[i]);
7905 /* the parentage chains form a tree.
7906 * the verifier states are added to state lists at given insn and
7907 * pushed into state stack for future exploration.
7908 * when the verifier reaches bpf_exit insn some of the verifer states
7909 * stored in the state lists have their final liveness state already,
7910 * but a lot of states will get revised from liveness point of view when
7911 * the verifier explores other branches.
7914 * 2: if r1 == 100 goto pc+1
7917 * when the verifier reaches exit insn the register r0 in the state list of
7918 * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch
7919 * of insn 2 and goes exploring further. At the insn 4 it will walk the
7920 * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ.
7922 * Since the verifier pushes the branch states as it sees them while exploring
7923 * the program the condition of walking the branch instruction for the second
7924 * time means that all states below this branch were already explored and
7925 * their final liveness markes are already propagated.
7926 * Hence when the verifier completes the search of state list in is_state_visited()
7927 * we can call this clean_live_states() function to mark all liveness states
7928 * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state'
7930 * This function also clears the registers and stack for states that !READ
7931 * to simplify state merging.
7933 * Important note here that walking the same branch instruction in the callee
7934 * doesn't meant that the states are DONE. The verifier has to compare
7937 static void clean_live_states(struct bpf_verifier_env *env, int insn,
7938 struct bpf_verifier_state *cur)
7940 struct bpf_verifier_state_list *sl;
7943 sl = *explored_state(env, insn);
7945 if (sl->state.branches)
7947 if (sl->state.insn_idx != insn ||
7948 sl->state.curframe != cur->curframe)
7950 for (i = 0; i <= cur->curframe; i++)
7951 if (sl->state.frame[i]->callsite != cur->frame[i]->callsite)
7953 clean_verifier_state(env, &sl->state);
7959 /* Returns true if (rold safe implies rcur safe) */
7960 static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
7961 struct idpair *idmap)
7965 if (!(rold->live & REG_LIVE_READ))
7966 /* explored state didn't use this */
7969 equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0;
7971 if (rold->type == PTR_TO_STACK)
7972 /* two stack pointers are equal only if they're pointing to
7973 * the same stack frame, since fp-8 in foo != fp-8 in bar
7975 return equal && rold->frameno == rcur->frameno;
7980 if (rold->type == NOT_INIT)
7981 /* explored state can't have used this */
7983 if (rcur->type == NOT_INIT)
7985 switch (rold->type) {
7987 if (rcur->type == SCALAR_VALUE) {
7988 if (!rold->precise && !rcur->precise)
7990 /* new val must satisfy old val knowledge */
7991 return range_within(rold, rcur) &&
7992 tnum_in(rold->var_off, rcur->var_off);
7994 /* We're trying to use a pointer in place of a scalar.
7995 * Even if the scalar was unbounded, this could lead to
7996 * pointer leaks because scalars are allowed to leak
7997 * while pointers are not. We could make this safe in
7998 * special cases if root is calling us, but it's
7999 * probably not worth the hassle.
8003 case PTR_TO_MAP_VALUE:
8004 /* If the new min/max/var_off satisfy the old ones and
8005 * everything else matches, we are OK.
8006 * 'id' is not compared, since it's only used for maps with
8007 * bpf_spin_lock inside map element and in such cases if
8008 * the rest of the prog is valid for one map element then
8009 * it's valid for all map elements regardless of the key
8010 * used in bpf_map_lookup()
8012 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
8013 range_within(rold, rcur) &&
8014 tnum_in(rold->var_off, rcur->var_off);
8015 case PTR_TO_MAP_VALUE_OR_NULL:
8016 /* a PTR_TO_MAP_VALUE could be safe to use as a
8017 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
8018 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
8019 * checked, doing so could have affected others with the same
8020 * id, and we can't check for that because we lost the id when
8021 * we converted to a PTR_TO_MAP_VALUE.
8023 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
8025 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
8027 /* Check our ids match any regs they're supposed to */
8028 return check_ids(rold->id, rcur->id, idmap);
8029 case PTR_TO_PACKET_META:
8031 if (rcur->type != rold->type)
8033 /* We must have at least as much range as the old ptr
8034 * did, so that any accesses which were safe before are
8035 * still safe. This is true even if old range < old off,
8036 * since someone could have accessed through (ptr - k), or
8037 * even done ptr -= k in a register, to get a safe access.
8039 if (rold->range > rcur->range)
8041 /* If the offsets don't match, we can't trust our alignment;
8042 * nor can we be sure that we won't fall out of range.
8044 if (rold->off != rcur->off)
8046 /* id relations must be preserved */
8047 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
8049 /* new val must satisfy old val knowledge */
8050 return range_within(rold, rcur) &&
8051 tnum_in(rold->var_off, rcur->var_off);
8053 case CONST_PTR_TO_MAP:
8054 case PTR_TO_PACKET_END:
8055 case PTR_TO_FLOW_KEYS:
8057 case PTR_TO_SOCKET_OR_NULL:
8058 case PTR_TO_SOCK_COMMON:
8059 case PTR_TO_SOCK_COMMON_OR_NULL:
8060 case PTR_TO_TCP_SOCK:
8061 case PTR_TO_TCP_SOCK_OR_NULL:
8062 case PTR_TO_XDP_SOCK:
8063 /* Only valid matches are exact, which memcmp() above
8064 * would have accepted
8067 /* Don't know what's going on, just say it's not safe */
8071 /* Shouldn't get here; if we do, say it's not safe */
8076 static bool stacksafe(struct bpf_func_state *old,
8077 struct bpf_func_state *cur,
8078 struct idpair *idmap)
8082 /* walk slots of the explored stack and ignore any additional
8083 * slots in the current stack, since explored(safe) state
8086 for (i = 0; i < old->allocated_stack; i++) {
8087 spi = i / BPF_REG_SIZE;
8089 if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) {
8090 i += BPF_REG_SIZE - 1;
8091 /* explored state didn't use this */
8095 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
8098 /* explored stack has more populated slots than current stack
8099 * and these slots were used
8101 if (i >= cur->allocated_stack)
8104 /* if old state was safe with misc data in the stack
8105 * it will be safe with zero-initialized stack.
8106 * The opposite is not true
8108 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
8109 cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
8111 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
8112 cur->stack[spi].slot_type[i % BPF_REG_SIZE])
8113 /* Ex: old explored (safe) state has STACK_SPILL in
8114 * this stack slot, but current has has STACK_MISC ->
8115 * this verifier states are not equivalent,
8116 * return false to continue verification of this path
8119 if (i % BPF_REG_SIZE)
8121 if (old->stack[spi].slot_type[0] != STACK_SPILL)
8123 if (!regsafe(&old->stack[spi].spilled_ptr,
8124 &cur->stack[spi].spilled_ptr,
8126 /* when explored and current stack slot are both storing
8127 * spilled registers, check that stored pointers types
8128 * are the same as well.
8129 * Ex: explored safe path could have stored
8130 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
8131 * but current path has stored:
8132 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
8133 * such verifier states are not equivalent.
8134 * return false to continue verification of this path
8141 static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur)
8143 if (old->acquired_refs != cur->acquired_refs)
8145 return !memcmp(old->refs, cur->refs,
8146 sizeof(*old->refs) * old->acquired_refs);
8149 /* compare two verifier states
8151 * all states stored in state_list are known to be valid, since
8152 * verifier reached 'bpf_exit' instruction through them
8154 * this function is called when verifier exploring different branches of
8155 * execution popped from the state stack. If it sees an old state that has
8156 * more strict register state and more strict stack state then this execution
8157 * branch doesn't need to be explored further, since verifier already
8158 * concluded that more strict state leads to valid finish.
8160 * Therefore two states are equivalent if register state is more conservative
8161 * and explored stack state is more conservative than the current one.
8164 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
8165 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
8167 * In other words if current stack state (one being explored) has more
8168 * valid slots than old one that already passed validation, it means
8169 * the verifier can stop exploring and conclude that current state is valid too
8171 * Similarly with registers. If explored state has register type as invalid
8172 * whereas register type in current state is meaningful, it means that
8173 * the current state will reach 'bpf_exit' instruction safely
8175 static bool func_states_equal(struct bpf_func_state *old,
8176 struct bpf_func_state *cur)
8178 struct idpair *idmap;
8182 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
8183 /* If we failed to allocate the idmap, just say it's not safe */
8187 for (i = 0; i < MAX_BPF_REG; i++) {
8188 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
8192 if (!stacksafe(old, cur, idmap))
8195 if (!refsafe(old, cur))
8203 static bool states_equal(struct bpf_verifier_env *env,
8204 struct bpf_verifier_state *old,
8205 struct bpf_verifier_state *cur)
8209 if (old->curframe != cur->curframe)
8212 /* Verification state from speculative execution simulation
8213 * must never prune a non-speculative execution one.
8215 if (old->speculative && !cur->speculative)
8218 if (old->active_spin_lock != cur->active_spin_lock)
8221 /* for states to be equal callsites have to be the same
8222 * and all frame states need to be equivalent
8224 for (i = 0; i <= old->curframe; i++) {
8225 if (old->frame[i]->callsite != cur->frame[i]->callsite)
8227 if (!func_states_equal(old->frame[i], cur->frame[i]))
8233 /* Return 0 if no propagation happened. Return negative error code if error
8234 * happened. Otherwise, return the propagated bit.
8236 static int propagate_liveness_reg(struct bpf_verifier_env *env,
8237 struct bpf_reg_state *reg,
8238 struct bpf_reg_state *parent_reg)
8240 u8 parent_flag = parent_reg->live & REG_LIVE_READ;
8241 u8 flag = reg->live & REG_LIVE_READ;
8244 /* When comes here, read flags of PARENT_REG or REG could be any of
8245 * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need
8246 * of propagation if PARENT_REG has strongest REG_LIVE_READ64.
8248 if (parent_flag == REG_LIVE_READ64 ||
8249 /* Or if there is no read flag from REG. */
8251 /* Or if the read flag from REG is the same as PARENT_REG. */
8252 parent_flag == flag)
8255 err = mark_reg_read(env, reg, parent_reg, flag);
8262 /* A write screens off any subsequent reads; but write marks come from the
8263 * straight-line code between a state and its parent. When we arrive at an
8264 * equivalent state (jump target or such) we didn't arrive by the straight-line
8265 * code, so read marks in the state must propagate to the parent regardless
8266 * of the state's write marks. That's what 'parent == state->parent' comparison
8267 * in mark_reg_read() is for.
8269 static int propagate_liveness(struct bpf_verifier_env *env,
8270 const struct bpf_verifier_state *vstate,
8271 struct bpf_verifier_state *vparent)
8273 struct bpf_reg_state *state_reg, *parent_reg;
8274 struct bpf_func_state *state, *parent;
8275 int i, frame, err = 0;
8277 if (vparent->curframe != vstate->curframe) {
8278 WARN(1, "propagate_live: parent frame %d current frame %d\n",
8279 vparent->curframe, vstate->curframe);
8282 /* Propagate read liveness of registers... */
8283 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
8284 for (frame = 0; frame <= vstate->curframe; frame++) {
8285 parent = vparent->frame[frame];
8286 state = vstate->frame[frame];
8287 parent_reg = parent->regs;
8288 state_reg = state->regs;
8289 /* We don't need to worry about FP liveness, it's read-only */
8290 for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) {
8291 err = propagate_liveness_reg(env, &state_reg[i],
8295 if (err == REG_LIVE_READ64)
8296 mark_insn_zext(env, &parent_reg[i]);
8299 /* Propagate stack slots. */
8300 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
8301 i < parent->allocated_stack / BPF_REG_SIZE; i++) {
8302 parent_reg = &parent->stack[i].spilled_ptr;
8303 state_reg = &state->stack[i].spilled_ptr;
8304 err = propagate_liveness_reg(env, state_reg,
8313 /* find precise scalars in the previous equivalent state and
8314 * propagate them into the current state
8316 static int propagate_precision(struct bpf_verifier_env *env,
8317 const struct bpf_verifier_state *old)
8319 struct bpf_reg_state *state_reg;
8320 struct bpf_func_state *state;
8323 state = old->frame[old->curframe];
8324 state_reg = state->regs;
8325 for (i = 0; i < BPF_REG_FP; i++, state_reg++) {
8326 if (state_reg->type != SCALAR_VALUE ||
8327 !state_reg->precise)
8329 if (env->log.level & BPF_LOG_LEVEL2)
8330 verbose(env, "propagating r%d\n", i);
8331 err = mark_chain_precision(env, i);
8336 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
8337 if (state->stack[i].slot_type[0] != STACK_SPILL)
8339 state_reg = &state->stack[i].spilled_ptr;
8340 if (state_reg->type != SCALAR_VALUE ||
8341 !state_reg->precise)
8343 if (env->log.level & BPF_LOG_LEVEL2)
8344 verbose(env, "propagating fp%d\n",
8345 (-i - 1) * BPF_REG_SIZE);
8346 err = mark_chain_precision_stack(env, i);
8353 static bool states_maybe_looping(struct bpf_verifier_state *old,
8354 struct bpf_verifier_state *cur)
8356 struct bpf_func_state *fold, *fcur;
8357 int i, fr = cur->curframe;
8359 if (old->curframe != fr)
8362 fold = old->frame[fr];
8363 fcur = cur->frame[fr];
8364 for (i = 0; i < MAX_BPF_REG; i++)
8365 if (memcmp(&fold->regs[i], &fcur->regs[i],
8366 offsetof(struct bpf_reg_state, parent)))
8372 static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
8374 struct bpf_verifier_state_list *new_sl;
8375 struct bpf_verifier_state_list *sl, **pprev;
8376 struct bpf_verifier_state *cur = env->cur_state, *new;
8377 int i, j, err, states_cnt = 0;
8378 bool add_new_state = env->test_state_freq ? true : false;
8380 cur->last_insn_idx = env->prev_insn_idx;
8381 if (!env->insn_aux_data[insn_idx].prune_point)
8382 /* this 'insn_idx' instruction wasn't marked, so we will not
8383 * be doing state search here
8387 /* bpf progs typically have pruning point every 4 instructions
8388 * http://vger.kernel.org/bpfconf2019.html#session-1
8389 * Do not add new state for future pruning if the verifier hasn't seen
8390 * at least 2 jumps and at least 8 instructions.
8391 * This heuristics helps decrease 'total_states' and 'peak_states' metric.
8392 * In tests that amounts to up to 50% reduction into total verifier
8393 * memory consumption and 20% verifier time speedup.
8395 if (env->jmps_processed - env->prev_jmps_processed >= 2 &&
8396 env->insn_processed - env->prev_insn_processed >= 8)
8397 add_new_state = true;
8399 pprev = explored_state(env, insn_idx);
8402 clean_live_states(env, insn_idx, cur);
8406 if (sl->state.insn_idx != insn_idx)
8408 if (sl->state.branches) {
8409 if (states_maybe_looping(&sl->state, cur) &&
8410 states_equal(env, &sl->state, cur)) {
8411 verbose_linfo(env, insn_idx, "; ");
8412 verbose(env, "infinite loop detected at insn %d\n", insn_idx);
8415 /* if the verifier is processing a loop, avoid adding new state
8416 * too often, since different loop iterations have distinct
8417 * states and may not help future pruning.
8418 * This threshold shouldn't be too low to make sure that
8419 * a loop with large bound will be rejected quickly.
8420 * The most abusive loop will be:
8422 * if r1 < 1000000 goto pc-2
8423 * 1M insn_procssed limit / 100 == 10k peak states.
8424 * This threshold shouldn't be too high either, since states
8425 * at the end of the loop are likely to be useful in pruning.
8427 if (env->jmps_processed - env->prev_jmps_processed < 20 &&
8428 env->insn_processed - env->prev_insn_processed < 100)
8429 add_new_state = false;
8432 if (states_equal(env, &sl->state, cur)) {
8434 /* reached equivalent register/stack state,
8436 * Registers read by the continuation are read by us.
8437 * If we have any write marks in env->cur_state, they
8438 * will prevent corresponding reads in the continuation
8439 * from reaching our parent (an explored_state). Our
8440 * own state will get the read marks recorded, but
8441 * they'll be immediately forgotten as we're pruning
8442 * this state and will pop a new one.
8444 err = propagate_liveness(env, &sl->state, cur);
8446 /* if previous state reached the exit with precision and
8447 * current state is equivalent to it (except precsion marks)
8448 * the precision needs to be propagated back in
8449 * the current state.
8451 err = err ? : push_jmp_history(env, cur);
8452 err = err ? : propagate_precision(env, &sl->state);
8458 /* when new state is not going to be added do not increase miss count.
8459 * Otherwise several loop iterations will remove the state
8460 * recorded earlier. The goal of these heuristics is to have
8461 * states from some iterations of the loop (some in the beginning
8462 * and some at the end) to help pruning.
8466 /* heuristic to determine whether this state is beneficial
8467 * to keep checking from state equivalence point of view.
8468 * Higher numbers increase max_states_per_insn and verification time,
8469 * but do not meaningfully decrease insn_processed.
8471 if (sl->miss_cnt > sl->hit_cnt * 3 + 3) {
8472 /* the state is unlikely to be useful. Remove it to
8473 * speed up verification
8476 if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) {
8477 u32 br = sl->state.branches;
8480 "BUG live_done but branches_to_explore %d\n",
8482 free_verifier_state(&sl->state, false);
8486 /* cannot free this state, since parentage chain may
8487 * walk it later. Add it for free_list instead to
8488 * be freed at the end of verification
8490 sl->next = env->free_list;
8491 env->free_list = sl;
8501 if (env->max_states_per_insn < states_cnt)
8502 env->max_states_per_insn = states_cnt;
8504 if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)
8505 return push_jmp_history(env, cur);
8508 return push_jmp_history(env, cur);
8510 /* There were no equivalent states, remember the current one.
8511 * Technically the current state is not proven to be safe yet,
8512 * but it will either reach outer most bpf_exit (which means it's safe)
8513 * or it will be rejected. When there are no loops the verifier won't be
8514 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
8515 * again on the way to bpf_exit.
8516 * When looping the sl->state.branches will be > 0 and this state
8517 * will not be considered for equivalence until branches == 0.
8519 new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
8522 env->total_states++;
8524 env->prev_jmps_processed = env->jmps_processed;
8525 env->prev_insn_processed = env->insn_processed;
8527 /* add new state to the head of linked list */
8528 new = &new_sl->state;
8529 err = copy_verifier_state(new, cur);
8531 free_verifier_state(new, false);
8535 new->insn_idx = insn_idx;
8536 WARN_ONCE(new->branches != 1,
8537 "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx);
8540 cur->first_insn_idx = insn_idx;
8541 clear_jmp_history(cur);
8542 new_sl->next = *explored_state(env, insn_idx);
8543 *explored_state(env, insn_idx) = new_sl;
8544 /* connect new state to parentage chain. Current frame needs all
8545 * registers connected. Only r6 - r9 of the callers are alive (pushed
8546 * to the stack implicitly by JITs) so in callers' frames connect just
8547 * r6 - r9 as an optimization. Callers will have r1 - r5 connected to
8548 * the state of the call instruction (with WRITTEN set), and r0 comes
8549 * from callee with its full parentage chain, anyway.
8551 /* clear write marks in current state: the writes we did are not writes
8552 * our child did, so they don't screen off its reads from us.
8553 * (There are no read marks in current state, because reads always mark
8554 * their parent and current state never has children yet. Only
8555 * explored_states can get read marks.)
8557 for (j = 0; j <= cur->curframe; j++) {
8558 for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++)
8559 cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i];
8560 for (i = 0; i < BPF_REG_FP; i++)
8561 cur->frame[j]->regs[i].live = REG_LIVE_NONE;
8564 /* all stack frames are accessible from callee, clear them all */
8565 for (j = 0; j <= cur->curframe; j++) {
8566 struct bpf_func_state *frame = cur->frame[j];
8567 struct bpf_func_state *newframe = new->frame[j];
8569 for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) {
8570 frame->stack[i].spilled_ptr.live = REG_LIVE_NONE;
8571 frame->stack[i].spilled_ptr.parent =
8572 &newframe->stack[i].spilled_ptr;
8578 /* Return true if it's OK to have the same insn return a different type. */
8579 static bool reg_type_mismatch_ok(enum bpf_reg_type type)
8584 case PTR_TO_SOCKET_OR_NULL:
8585 case PTR_TO_SOCK_COMMON:
8586 case PTR_TO_SOCK_COMMON_OR_NULL:
8587 case PTR_TO_TCP_SOCK:
8588 case PTR_TO_TCP_SOCK_OR_NULL:
8589 case PTR_TO_XDP_SOCK:
8591 case PTR_TO_BTF_ID_OR_NULL:
8598 /* If an instruction was previously used with particular pointer types, then we
8599 * need to be careful to avoid cases such as the below, where it may be ok
8600 * for one branch accessing the pointer, but not ok for the other branch:
8605 * R1 = some_other_valid_ptr;
8608 * R2 = *(u32 *)(R1 + 0);
8610 static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev)
8612 return src != prev && (!reg_type_mismatch_ok(src) ||
8613 !reg_type_mismatch_ok(prev));
8616 static int do_check(struct bpf_verifier_env *env)
8618 bool pop_log = !(env->log.level & BPF_LOG_LEVEL2);
8619 struct bpf_verifier_state *state = env->cur_state;
8620 struct bpf_insn *insns = env->prog->insnsi;
8621 struct bpf_reg_state *regs;
8622 int insn_cnt = env->prog->len;
8623 bool do_print_state = false;
8624 int prev_insn_idx = -1;
8627 struct bpf_insn *insn;
8631 env->prev_insn_idx = prev_insn_idx;
8632 if (env->insn_idx >= insn_cnt) {
8633 verbose(env, "invalid insn idx %d insn_cnt %d\n",
8634 env->insn_idx, insn_cnt);
8638 insn = &insns[env->insn_idx];
8639 class = BPF_CLASS(insn->code);
8641 if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
8643 "BPF program is too large. Processed %d insn\n",
8644 env->insn_processed);
8648 err = is_state_visited(env, env->insn_idx);
8652 /* found equivalent state, can prune the search */
8653 if (env->log.level & BPF_LOG_LEVEL) {
8655 verbose(env, "\nfrom %d to %d%s: safe\n",
8656 env->prev_insn_idx, env->insn_idx,
8657 env->cur_state->speculative ?
8658 " (speculative execution)" : "");
8660 verbose(env, "%d: safe\n", env->insn_idx);
8662 goto process_bpf_exit;
8665 if (signal_pending(current))
8671 if (env->log.level & BPF_LOG_LEVEL2 ||
8672 (env->log.level & BPF_LOG_LEVEL && do_print_state)) {
8673 if (env->log.level & BPF_LOG_LEVEL2)
8674 verbose(env, "%d:", env->insn_idx);
8676 verbose(env, "\nfrom %d to %d%s:",
8677 env->prev_insn_idx, env->insn_idx,
8678 env->cur_state->speculative ?
8679 " (speculative execution)" : "");
8680 print_verifier_state(env, state->frame[state->curframe]);
8681 do_print_state = false;
8684 if (env->log.level & BPF_LOG_LEVEL) {
8685 const struct bpf_insn_cbs cbs = {
8686 .cb_print = verbose,
8687 .private_data = env,
8690 verbose_linfo(env, env->insn_idx, "; ");
8691 verbose(env, "%d: ", env->insn_idx);
8692 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
8695 if (bpf_prog_is_dev_bound(env->prog->aux)) {
8696 err = bpf_prog_offload_verify_insn(env, env->insn_idx,
8697 env->prev_insn_idx);
8702 regs = cur_regs(env);
8703 env->insn_aux_data[env->insn_idx].seen = env->pass_cnt;
8704 prev_insn_idx = env->insn_idx;
8706 if (class == BPF_ALU || class == BPF_ALU64) {
8707 err = check_alu_op(env, insn);
8711 } else if (class == BPF_LDX) {
8712 enum bpf_reg_type *prev_src_type, src_reg_type;
8714 /* check for reserved fields is already done */
8716 /* check src operand */
8717 err = check_reg_arg(env, insn->src_reg, SRC_OP);
8721 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
8725 src_reg_type = regs[insn->src_reg].type;
8727 /* check that memory (src_reg + off) is readable,
8728 * the state of dst_reg will be updated by this func
8730 err = check_mem_access(env, env->insn_idx, insn->src_reg,
8731 insn->off, BPF_SIZE(insn->code),
8732 BPF_READ, insn->dst_reg, false);
8736 prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type;
8738 if (*prev_src_type == NOT_INIT) {
8740 * dst_reg = *(u32 *)(src_reg + off)
8741 * save type to validate intersecting paths
8743 *prev_src_type = src_reg_type;
8745 } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) {
8746 /* ABuser program is trying to use the same insn
8747 * dst_reg = *(u32*) (src_reg + off)
8748 * with different pointer types:
8749 * src_reg == ctx in one branch and
8750 * src_reg == stack|map in some other branch.
8753 verbose(env, "same insn cannot be used with different pointers\n");
8757 } else if (class == BPF_STX) {
8758 enum bpf_reg_type *prev_dst_type, dst_reg_type;
8760 if (BPF_MODE(insn->code) == BPF_XADD) {
8761 err = check_xadd(env, env->insn_idx, insn);
8768 /* check src1 operand */
8769 err = check_reg_arg(env, insn->src_reg, SRC_OP);
8772 /* check src2 operand */
8773 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
8777 dst_reg_type = regs[insn->dst_reg].type;
8779 /* check that memory (dst_reg + off) is writeable */
8780 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
8781 insn->off, BPF_SIZE(insn->code),
8782 BPF_WRITE, insn->src_reg, false);
8786 prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type;
8788 if (*prev_dst_type == NOT_INIT) {
8789 *prev_dst_type = dst_reg_type;
8790 } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) {
8791 verbose(env, "same insn cannot be used with different pointers\n");
8795 } else if (class == BPF_ST) {
8796 if (BPF_MODE(insn->code) != BPF_MEM ||
8797 insn->src_reg != BPF_REG_0) {
8798 verbose(env, "BPF_ST uses reserved fields\n");
8801 /* check src operand */
8802 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
8806 if (is_ctx_reg(env, insn->dst_reg)) {
8807 verbose(env, "BPF_ST stores into R%d %s is not allowed\n",
8809 reg_type_str[reg_state(env, insn->dst_reg)->type]);
8813 /* check that memory (dst_reg + off) is writeable */
8814 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
8815 insn->off, BPF_SIZE(insn->code),
8816 BPF_WRITE, -1, false);
8820 } else if (class == BPF_JMP || class == BPF_JMP32) {
8821 u8 opcode = BPF_OP(insn->code);
8823 env->jmps_processed++;
8824 if (opcode == BPF_CALL) {
8825 if (BPF_SRC(insn->code) != BPF_K ||
8827 (insn->src_reg != BPF_REG_0 &&
8828 insn->src_reg != BPF_PSEUDO_CALL) ||
8829 insn->dst_reg != BPF_REG_0 ||
8830 class == BPF_JMP32) {
8831 verbose(env, "BPF_CALL uses reserved fields\n");
8835 if (env->cur_state->active_spin_lock &&
8836 (insn->src_reg == BPF_PSEUDO_CALL ||
8837 insn->imm != BPF_FUNC_spin_unlock)) {
8838 verbose(env, "function calls are not allowed while holding a lock\n");
8841 if (insn->src_reg == BPF_PSEUDO_CALL)
8842 err = check_func_call(env, insn, &env->insn_idx);
8844 err = check_helper_call(env, insn->imm, env->insn_idx);
8848 } else if (opcode == BPF_JA) {
8849 if (BPF_SRC(insn->code) != BPF_K ||
8851 insn->src_reg != BPF_REG_0 ||
8852 insn->dst_reg != BPF_REG_0 ||
8853 class == BPF_JMP32) {
8854 verbose(env, "BPF_JA uses reserved fields\n");
8858 env->insn_idx += insn->off + 1;
8861 } else if (opcode == BPF_EXIT) {
8862 if (BPF_SRC(insn->code) != BPF_K ||
8864 insn->src_reg != BPF_REG_0 ||
8865 insn->dst_reg != BPF_REG_0 ||
8866 class == BPF_JMP32) {
8867 verbose(env, "BPF_EXIT uses reserved fields\n");
8871 if (env->cur_state->active_spin_lock) {
8872 verbose(env, "bpf_spin_unlock is missing\n");
8876 if (state->curframe) {
8877 /* exit from nested function */
8878 err = prepare_func_exit(env, &env->insn_idx);
8881 do_print_state = true;
8885 err = check_reference_leak(env);
8889 err = check_return_code(env);
8893 update_branch_counts(env, env->cur_state);
8894 err = pop_stack(env, &prev_insn_idx,
8895 &env->insn_idx, pop_log);
8901 do_print_state = true;
8905 err = check_cond_jmp_op(env, insn, &env->insn_idx);
8909 } else if (class == BPF_LD) {
8910 u8 mode = BPF_MODE(insn->code);
8912 if (mode == BPF_ABS || mode == BPF_IND) {
8913 err = check_ld_abs(env, insn);
8917 } else if (mode == BPF_IMM) {
8918 err = check_ld_imm(env, insn);
8923 env->insn_aux_data[env->insn_idx].seen = env->pass_cnt;
8925 verbose(env, "invalid BPF_LD mode\n");
8929 verbose(env, "unknown insn class %d\n", class);
8939 static int check_map_prealloc(struct bpf_map *map)
8941 return (map->map_type != BPF_MAP_TYPE_HASH &&
8942 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
8943 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
8944 !(map->map_flags & BPF_F_NO_PREALLOC);
8947 static bool is_tracing_prog_type(enum bpf_prog_type type)
8950 case BPF_PROG_TYPE_KPROBE:
8951 case BPF_PROG_TYPE_TRACEPOINT:
8952 case BPF_PROG_TYPE_PERF_EVENT:
8953 case BPF_PROG_TYPE_RAW_TRACEPOINT:
8960 static bool is_preallocated_map(struct bpf_map *map)
8962 if (!check_map_prealloc(map))
8964 if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta))
8969 static int check_map_prog_compatibility(struct bpf_verifier_env *env,
8970 struct bpf_map *map,
8971 struct bpf_prog *prog)
8975 * Validate that trace type programs use preallocated hash maps.
8977 * For programs attached to PERF events this is mandatory as the
8978 * perf NMI can hit any arbitrary code sequence.
8980 * All other trace types using preallocated hash maps are unsafe as
8981 * well because tracepoint or kprobes can be inside locked regions
8982 * of the memory allocator or at a place where a recursion into the
8983 * memory allocator would see inconsistent state.
8985 * On RT enabled kernels run-time allocation of all trace type
8986 * programs is strictly prohibited due to lock type constraints. On
8987 * !RT kernels it is allowed for backwards compatibility reasons for
8988 * now, but warnings are emitted so developers are made aware of
8989 * the unsafety and can fix their programs before this is enforced.
8991 if (is_tracing_prog_type(prog->type) && !is_preallocated_map(map)) {
8992 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
8993 verbose(env, "perf_event programs can only use preallocated hash map\n");
8996 if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
8997 verbose(env, "trace type programs can only use preallocated hash map\n");
9000 WARN_ONCE(1, "trace type BPF program uses run-time allocation\n");
9001 verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n");
9004 if ((is_tracing_prog_type(prog->type) ||
9005 prog->type == BPF_PROG_TYPE_SOCKET_FILTER) &&
9006 map_value_has_spin_lock(map)) {
9007 verbose(env, "tracing progs cannot use bpf_spin_lock yet\n");
9011 if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) &&
9012 !bpf_offload_prog_map_match(prog, map)) {
9013 verbose(env, "offload device mismatch between prog and map\n");
9017 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
9018 verbose(env, "bpf_struct_ops map cannot be used in prog\n");
9025 static bool bpf_map_is_cgroup_storage(struct bpf_map *map)
9027 return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE ||
9028 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
9031 /* look for pseudo eBPF instructions that access map FDs and
9032 * replace them with actual map pointers
9034 static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
9036 struct bpf_insn *insn = env->prog->insnsi;
9037 int insn_cnt = env->prog->len;
9040 err = bpf_prog_calc_tag(env->prog);
9044 for (i = 0; i < insn_cnt; i++, insn++) {
9045 if (BPF_CLASS(insn->code) == BPF_LDX &&
9046 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
9047 verbose(env, "BPF_LDX uses reserved fields\n");
9051 if (BPF_CLASS(insn->code) == BPF_STX &&
9052 ((BPF_MODE(insn->code) != BPF_MEM &&
9053 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
9054 verbose(env, "BPF_STX uses reserved fields\n");
9058 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
9059 struct bpf_insn_aux_data *aux;
9060 struct bpf_map *map;
9064 if (i == insn_cnt - 1 || insn[1].code != 0 ||
9065 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
9067 verbose(env, "invalid bpf_ld_imm64 insn\n");
9071 if (insn[0].src_reg == 0)
9072 /* valid generic load 64-bit imm */
9075 /* In final convert_pseudo_ld_imm64() step, this is
9076 * converted into regular 64-bit imm load insn.
9078 if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD &&
9079 insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) ||
9080 (insn[0].src_reg == BPF_PSEUDO_MAP_FD &&
9081 insn[1].imm != 0)) {
9083 "unrecognized bpf_ld_imm64 insn\n");
9087 f = fdget(insn[0].imm);
9088 map = __bpf_map_get(f);
9090 verbose(env, "fd %d is not pointing to valid bpf_map\n",
9092 return PTR_ERR(map);
9095 err = check_map_prog_compatibility(env, map, env->prog);
9101 aux = &env->insn_aux_data[i];
9102 if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
9103 addr = (unsigned long)map;
9105 u32 off = insn[1].imm;
9107 if (off >= BPF_MAX_VAR_OFF) {
9108 verbose(env, "direct value offset of %u is not allowed\n", off);
9113 if (!map->ops->map_direct_value_addr) {
9114 verbose(env, "no direct value access support for this map type\n");
9119 err = map->ops->map_direct_value_addr(map, &addr, off);
9121 verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n",
9122 map->value_size, off);
9131 insn[0].imm = (u32)addr;
9132 insn[1].imm = addr >> 32;
9134 /* check whether we recorded this map already */
9135 for (j = 0; j < env->used_map_cnt; j++) {
9136 if (env->used_maps[j] == map) {
9143 if (env->used_map_cnt >= MAX_USED_MAPS) {
9148 /* hold the map. If the program is rejected by verifier,
9149 * the map will be released by release_maps() or it
9150 * will be used by the valid program until it's unloaded
9151 * and all maps are released in free_used_maps()
9155 aux->map_index = env->used_map_cnt;
9156 env->used_maps[env->used_map_cnt++] = map;
9158 if (bpf_map_is_cgroup_storage(map) &&
9159 bpf_cgroup_storage_assign(env->prog->aux, map)) {
9160 verbose(env, "only one cgroup storage of each type is allowed\n");
9172 /* Basic sanity check before we invest more work here. */
9173 if (!bpf_opcode_in_insntable(insn->code)) {
9174 verbose(env, "unknown opcode %02x\n", insn->code);
9179 /* now all pseudo BPF_LD_IMM64 instructions load valid
9180 * 'struct bpf_map *' into a register instead of user map_fd.
9181 * These pointers will be used later by verifier to validate map access.
9186 /* drop refcnt of maps used by the rejected program */
9187 static void release_maps(struct bpf_verifier_env *env)
9189 __bpf_free_used_maps(env->prog->aux, env->used_maps,
9193 /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
9194 static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
9196 struct bpf_insn *insn = env->prog->insnsi;
9197 int insn_cnt = env->prog->len;
9200 for (i = 0; i < insn_cnt; i++, insn++)
9201 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
9205 /* single env->prog->insni[off] instruction was replaced with the range
9206 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
9207 * [0, off) and [off, end) to new locations, so the patched range stays zero
9209 static int adjust_insn_aux_data(struct bpf_verifier_env *env,
9210 struct bpf_prog *new_prog, u32 off, u32 cnt)
9212 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
9213 struct bpf_insn *insn = new_prog->insnsi;
9217 /* aux info at OFF always needs adjustment, no matter fast path
9218 * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the
9219 * original insn at old prog.
9221 old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1);
9225 prog_len = new_prog->len;
9226 new_data = vzalloc(array_size(prog_len,
9227 sizeof(struct bpf_insn_aux_data)));
9230 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
9231 memcpy(new_data + off + cnt - 1, old_data + off,
9232 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
9233 for (i = off; i < off + cnt - 1; i++) {
9234 new_data[i].seen = env->pass_cnt;
9235 new_data[i].zext_dst = insn_has_def32(env, insn + i);
9237 env->insn_aux_data = new_data;
9242 static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
9248 /* NOTE: fake 'exit' subprog should be updated as well. */
9249 for (i = 0; i <= env->subprog_cnt; i++) {
9250 if (env->subprog_info[i].start <= off)
9252 env->subprog_info[i].start += len - 1;
9256 static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
9257 const struct bpf_insn *patch, u32 len)
9259 struct bpf_prog *new_prog;
9261 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
9262 if (IS_ERR(new_prog)) {
9263 if (PTR_ERR(new_prog) == -ERANGE)
9265 "insn %d cannot be patched due to 16-bit range\n",
9266 env->insn_aux_data[off].orig_idx);
9269 if (adjust_insn_aux_data(env, new_prog, off, len))
9271 adjust_subprog_starts(env, off, len);
9275 static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
9280 /* find first prog starting at or after off (first to remove) */
9281 for (i = 0; i < env->subprog_cnt; i++)
9282 if (env->subprog_info[i].start >= off)
9284 /* find first prog starting at or after off + cnt (first to stay) */
9285 for (j = i; j < env->subprog_cnt; j++)
9286 if (env->subprog_info[j].start >= off + cnt)
9288 /* if j doesn't start exactly at off + cnt, we are just removing
9289 * the front of previous prog
9291 if (env->subprog_info[j].start != off + cnt)
9295 struct bpf_prog_aux *aux = env->prog->aux;
9298 /* move fake 'exit' subprog as well */
9299 move = env->subprog_cnt + 1 - j;
9301 memmove(env->subprog_info + i,
9302 env->subprog_info + j,
9303 sizeof(*env->subprog_info) * move);
9304 env->subprog_cnt -= j - i;
9306 /* remove func_info */
9307 if (aux->func_info) {
9308 move = aux->func_info_cnt - j;
9310 memmove(aux->func_info + i,
9312 sizeof(*aux->func_info) * move);
9313 aux->func_info_cnt -= j - i;
9314 /* func_info->insn_off is set after all code rewrites,
9315 * in adjust_btf_func() - no need to adjust
9319 /* convert i from "first prog to remove" to "first to adjust" */
9320 if (env->subprog_info[i].start == off)
9324 /* update fake 'exit' subprog as well */
9325 for (; i <= env->subprog_cnt; i++)
9326 env->subprog_info[i].start -= cnt;
9331 static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off,
9334 struct bpf_prog *prog = env->prog;
9335 u32 i, l_off, l_cnt, nr_linfo;
9336 struct bpf_line_info *linfo;
9338 nr_linfo = prog->aux->nr_linfo;
9342 linfo = prog->aux->linfo;
9344 /* find first line info to remove, count lines to be removed */
9345 for (i = 0; i < nr_linfo; i++)
9346 if (linfo[i].insn_off >= off)
9351 for (; i < nr_linfo; i++)
9352 if (linfo[i].insn_off < off + cnt)
9357 /* First live insn doesn't match first live linfo, it needs to "inherit"
9358 * last removed linfo. prog is already modified, so prog->len == off
9359 * means no live instructions after (tail of the program was removed).
9361 if (prog->len != off && l_cnt &&
9362 (i == nr_linfo || linfo[i].insn_off != off + cnt)) {
9364 linfo[--i].insn_off = off + cnt;
9367 /* remove the line info which refer to the removed instructions */
9369 memmove(linfo + l_off, linfo + i,
9370 sizeof(*linfo) * (nr_linfo - i));
9372 prog->aux->nr_linfo -= l_cnt;
9373 nr_linfo = prog->aux->nr_linfo;
9376 /* pull all linfo[i].insn_off >= off + cnt in by cnt */
9377 for (i = l_off; i < nr_linfo; i++)
9378 linfo[i].insn_off -= cnt;
9380 /* fix up all subprogs (incl. 'exit') which start >= off */
9381 for (i = 0; i <= env->subprog_cnt; i++)
9382 if (env->subprog_info[i].linfo_idx > l_off) {
9383 /* program may have started in the removed region but
9384 * may not be fully removed
9386 if (env->subprog_info[i].linfo_idx >= l_off + l_cnt)
9387 env->subprog_info[i].linfo_idx -= l_cnt;
9389 env->subprog_info[i].linfo_idx = l_off;
9395 static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
9397 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
9398 unsigned int orig_prog_len = env->prog->len;
9401 if (bpf_prog_is_dev_bound(env->prog->aux))
9402 bpf_prog_offload_remove_insns(env, off, cnt);
9404 err = bpf_remove_insns(env->prog, off, cnt);
9408 err = adjust_subprog_starts_after_remove(env, off, cnt);
9412 err = bpf_adj_linfo_after_remove(env, off, cnt);
9416 memmove(aux_data + off, aux_data + off + cnt,
9417 sizeof(*aux_data) * (orig_prog_len - off - cnt));
9422 /* The verifier does more data flow analysis than llvm and will not
9423 * explore branches that are dead at run time. Malicious programs can
9424 * have dead code too. Therefore replace all dead at-run-time code
9427 * Just nops are not optimal, e.g. if they would sit at the end of the
9428 * program and through another bug we would manage to jump there, then
9429 * we'd execute beyond program memory otherwise. Returning exception
9430 * code also wouldn't work since we can have subprogs where the dead
9431 * code could be located.
9433 static void sanitize_dead_code(struct bpf_verifier_env *env)
9435 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
9436 struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1);
9437 struct bpf_insn *insn = env->prog->insnsi;
9438 const int insn_cnt = env->prog->len;
9441 for (i = 0; i < insn_cnt; i++) {
9442 if (aux_data[i].seen)
9444 memcpy(insn + i, &trap, sizeof(trap));
9448 static bool insn_is_cond_jump(u8 code)
9452 if (BPF_CLASS(code) == BPF_JMP32)
9455 if (BPF_CLASS(code) != BPF_JMP)
9459 return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL;
9462 static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env)
9464 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
9465 struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
9466 struct bpf_insn *insn = env->prog->insnsi;
9467 const int insn_cnt = env->prog->len;
9470 for (i = 0; i < insn_cnt; i++, insn++) {
9471 if (!insn_is_cond_jump(insn->code))
9474 if (!aux_data[i + 1].seen)
9476 else if (!aux_data[i + 1 + insn->off].seen)
9481 if (bpf_prog_is_dev_bound(env->prog->aux))
9482 bpf_prog_offload_replace_insn(env, i, &ja);
9484 memcpy(insn, &ja, sizeof(ja));
9488 static int opt_remove_dead_code(struct bpf_verifier_env *env)
9490 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
9491 int insn_cnt = env->prog->len;
9494 for (i = 0; i < insn_cnt; i++) {
9498 while (i + j < insn_cnt && !aux_data[i + j].seen)
9503 err = verifier_remove_insns(env, i, j);
9506 insn_cnt = env->prog->len;
9512 static int opt_remove_nops(struct bpf_verifier_env *env)
9514 const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
9515 struct bpf_insn *insn = env->prog->insnsi;
9516 int insn_cnt = env->prog->len;
9519 for (i = 0; i < insn_cnt; i++) {
9520 if (memcmp(&insn[i], &ja, sizeof(ja)))
9523 err = verifier_remove_insns(env, i, 1);
9533 static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,
9534 const union bpf_attr *attr)
9536 struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4];
9537 struct bpf_insn_aux_data *aux = env->insn_aux_data;
9538 int i, patch_len, delta = 0, len = env->prog->len;
9539 struct bpf_insn *insns = env->prog->insnsi;
9540 struct bpf_prog *new_prog;
9543 rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32;
9544 zext_patch[1] = BPF_ZEXT_REG(0);
9545 rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0);
9546 rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
9547 rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX);
9548 for (i = 0; i < len; i++) {
9549 int adj_idx = i + delta;
9550 struct bpf_insn insn;
9552 insn = insns[adj_idx];
9553 if (!aux[adj_idx].zext_dst) {
9561 class = BPF_CLASS(code);
9562 if (insn_no_def(&insn))
9565 /* NOTE: arg "reg" (the fourth one) is only used for
9566 * BPF_STX which has been ruled out in above
9567 * check, it is safe to pass NULL here.
9569 if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) {
9570 if (class == BPF_LD &&
9571 BPF_MODE(code) == BPF_IMM)
9576 /* ctx load could be transformed into wider load. */
9577 if (class == BPF_LDX &&
9578 aux[adj_idx].ptr_type == PTR_TO_CTX)
9581 imm_rnd = get_random_int();
9582 rnd_hi32_patch[0] = insn;
9583 rnd_hi32_patch[1].imm = imm_rnd;
9584 rnd_hi32_patch[3].dst_reg = insn.dst_reg;
9585 patch = rnd_hi32_patch;
9587 goto apply_patch_buffer;
9590 if (!bpf_jit_needs_zext())
9593 zext_patch[0] = insn;
9594 zext_patch[1].dst_reg = insn.dst_reg;
9595 zext_patch[1].src_reg = insn.dst_reg;
9599 new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len);
9602 env->prog = new_prog;
9603 insns = new_prog->insnsi;
9604 aux = env->insn_aux_data;
9605 delta += patch_len - 1;
9611 /* convert load instructions that access fields of a context type into a
9612 * sequence of instructions that access fields of the underlying structure:
9613 * struct __sk_buff -> struct sk_buff
9614 * struct bpf_sock_ops -> struct sock
9616 static int convert_ctx_accesses(struct bpf_verifier_env *env)
9618 const struct bpf_verifier_ops *ops = env->ops;
9619 int i, cnt, size, ctx_field_size, delta = 0;
9620 const int insn_cnt = env->prog->len;
9621 struct bpf_insn insn_buf[16], *insn;
9622 u32 target_size, size_default, off;
9623 struct bpf_prog *new_prog;
9624 enum bpf_access_type type;
9625 bool is_narrower_load;
9627 if (ops->gen_prologue || env->seen_direct_write) {
9628 if (!ops->gen_prologue) {
9629 verbose(env, "bpf verifier is misconfigured\n");
9632 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
9634 if (cnt >= ARRAY_SIZE(insn_buf)) {
9635 verbose(env, "bpf verifier is misconfigured\n");
9638 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
9642 env->prog = new_prog;
9647 if (bpf_prog_is_dev_bound(env->prog->aux))
9650 insn = env->prog->insnsi + delta;
9652 for (i = 0; i < insn_cnt; i++, insn++) {
9653 bpf_convert_ctx_access_t convert_ctx_access;
9655 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
9656 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
9657 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
9658 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
9660 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
9661 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
9662 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
9663 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
9668 if (type == BPF_WRITE &&
9669 env->insn_aux_data[i + delta].sanitize_stack_off) {
9670 struct bpf_insn patch[] = {
9671 /* Sanitize suspicious stack slot with zero.
9672 * There are no memory dependencies for this store,
9673 * since it's only using frame pointer and immediate
9676 BPF_ST_MEM(BPF_DW, BPF_REG_FP,
9677 env->insn_aux_data[i + delta].sanitize_stack_off,
9679 /* the original STX instruction will immediately
9680 * overwrite the same stack slot with appropriate value
9685 cnt = ARRAY_SIZE(patch);
9686 new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt);
9691 env->prog = new_prog;
9692 insn = new_prog->insnsi + i + delta;
9696 switch (env->insn_aux_data[i + delta].ptr_type) {
9698 if (!ops->convert_ctx_access)
9700 convert_ctx_access = ops->convert_ctx_access;
9703 case PTR_TO_SOCK_COMMON:
9704 convert_ctx_access = bpf_sock_convert_ctx_access;
9706 case PTR_TO_TCP_SOCK:
9707 convert_ctx_access = bpf_tcp_sock_convert_ctx_access;
9709 case PTR_TO_XDP_SOCK:
9710 convert_ctx_access = bpf_xdp_sock_convert_ctx_access;
9713 if (type == BPF_READ) {
9714 insn->code = BPF_LDX | BPF_PROBE_MEM |
9715 BPF_SIZE((insn)->code);
9716 env->prog->aux->num_exentries++;
9717 } else if (env->prog->type != BPF_PROG_TYPE_STRUCT_OPS) {
9718 verbose(env, "Writes through BTF pointers are not allowed\n");
9726 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
9727 size = BPF_LDST_BYTES(insn);
9729 /* If the read access is a narrower load of the field,
9730 * convert to a 4/8-byte load, to minimum program type specific
9731 * convert_ctx_access changes. If conversion is successful,
9732 * we will apply proper mask to the result.
9734 is_narrower_load = size < ctx_field_size;
9735 size_default = bpf_ctx_off_adjust_machine(ctx_field_size);
9737 if (is_narrower_load) {
9740 if (type == BPF_WRITE) {
9741 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
9746 if (ctx_field_size == 4)
9748 else if (ctx_field_size == 8)
9751 insn->off = off & ~(size_default - 1);
9752 insn->code = BPF_LDX | BPF_MEM | size_code;
9756 cnt = convert_ctx_access(type, insn, insn_buf, env->prog,
9758 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
9759 (ctx_field_size && !target_size)) {
9760 verbose(env, "bpf verifier is misconfigured\n");
9764 if (is_narrower_load && size < target_size) {
9765 u8 shift = bpf_ctx_narrow_access_offset(
9766 off, size, size_default) * 8;
9767 if (ctx_field_size <= 4) {
9769 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH,
9772 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
9773 (1 << size * 8) - 1);
9776 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH,
9779 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
9780 (1ULL << size * 8) - 1);
9784 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
9790 /* keep walking new program and skip insns we just inserted */
9791 env->prog = new_prog;
9792 insn = new_prog->insnsi + i + delta;
9798 static int jit_subprogs(struct bpf_verifier_env *env)
9800 struct bpf_prog *prog = env->prog, **func, *tmp;
9801 int i, j, subprog_start, subprog_end = 0, len, subprog;
9802 struct bpf_insn *insn;
9806 if (env->subprog_cnt <= 1)
9809 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
9810 if (insn->code != (BPF_JMP | BPF_CALL) ||
9811 insn->src_reg != BPF_PSEUDO_CALL)
9813 /* Upon error here we cannot fall back to interpreter but
9814 * need a hard reject of the program. Thus -EFAULT is
9815 * propagated in any case.
9817 subprog = find_subprog(env, i + insn->imm + 1);
9819 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
9823 /* temporarily remember subprog id inside insn instead of
9824 * aux_data, since next loop will split up all insns into funcs
9826 insn->off = subprog;
9827 /* remember original imm in case JIT fails and fallback
9828 * to interpreter will be needed
9830 env->insn_aux_data[i].call_imm = insn->imm;
9831 /* point imm to __bpf_call_base+1 from JITs point of view */
9835 err = bpf_prog_alloc_jited_linfo(prog);
9840 func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL);
9844 for (i = 0; i < env->subprog_cnt; i++) {
9845 subprog_start = subprog_end;
9846 subprog_end = env->subprog_info[i + 1].start;
9848 len = subprog_end - subprog_start;
9849 /* BPF_PROG_RUN doesn't call subprogs directly,
9850 * hence main prog stats include the runtime of subprogs.
9851 * subprogs don't have IDs and not reachable via prog_get_next_id
9852 * func[i]->aux->stats will never be accessed and stays NULL
9854 func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER);
9857 memcpy(func[i]->insnsi, &prog->insnsi[subprog_start],
9858 len * sizeof(struct bpf_insn));
9859 func[i]->type = prog->type;
9861 if (bpf_prog_calc_tag(func[i]))
9863 func[i]->is_func = 1;
9864 func[i]->aux->func_idx = i;
9865 /* the btf and func_info will be freed only at prog->aux */
9866 func[i]->aux->btf = prog->aux->btf;
9867 func[i]->aux->func_info = prog->aux->func_info;
9869 /* Use bpf_prog_F_tag to indicate functions in stack traces.
9870 * Long term would need debug info to populate names
9872 func[i]->aux->name[0] = 'F';
9873 func[i]->aux->stack_depth = env->subprog_info[i].stack_depth;
9874 func[i]->jit_requested = 1;
9875 func[i]->aux->linfo = prog->aux->linfo;
9876 func[i]->aux->nr_linfo = prog->aux->nr_linfo;
9877 func[i]->aux->jited_linfo = prog->aux->jited_linfo;
9878 func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx;
9879 func[i] = bpf_int_jit_compile(func[i]);
9880 if (!func[i]->jited) {
9886 /* at this point all bpf functions were successfully JITed
9887 * now populate all bpf_calls with correct addresses and
9888 * run last pass of JIT
9890 for (i = 0; i < env->subprog_cnt; i++) {
9891 insn = func[i]->insnsi;
9892 for (j = 0; j < func[i]->len; j++, insn++) {
9893 if (insn->code != (BPF_JMP | BPF_CALL) ||
9894 insn->src_reg != BPF_PSEUDO_CALL)
9896 subprog = insn->off;
9897 insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) -
9901 /* we use the aux data to keep a list of the start addresses
9902 * of the JITed images for each function in the program
9904 * for some architectures, such as powerpc64, the imm field
9905 * might not be large enough to hold the offset of the start
9906 * address of the callee's JITed image from __bpf_call_base
9908 * in such cases, we can lookup the start address of a callee
9909 * by using its subprog id, available from the off field of
9910 * the call instruction, as an index for this list
9912 func[i]->aux->func = func;
9913 func[i]->aux->func_cnt = env->subprog_cnt;
9915 for (i = 0; i < env->subprog_cnt; i++) {
9916 old_bpf_func = func[i]->bpf_func;
9917 tmp = bpf_int_jit_compile(func[i]);
9918 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
9919 verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
9926 /* finally lock prog and jit images for all functions and
9929 for (i = 0; i < env->subprog_cnt; i++) {
9930 bpf_prog_lock_ro(func[i]);
9931 bpf_prog_kallsyms_add(func[i]);
9934 /* Last step: make now unused interpreter insns from main
9935 * prog consistent for later dump requests, so they can
9936 * later look the same as if they were interpreted only.
9938 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
9939 if (insn->code != (BPF_JMP | BPF_CALL) ||
9940 insn->src_reg != BPF_PSEUDO_CALL)
9942 insn->off = env->insn_aux_data[i].call_imm;
9943 subprog = find_subprog(env, i + insn->off + 1);
9944 insn->imm = subprog;
9948 prog->bpf_func = func[0]->bpf_func;
9949 prog->aux->func = func;
9950 prog->aux->func_cnt = env->subprog_cnt;
9951 bpf_prog_free_unused_jited_linfo(prog);
9954 for (i = 0; i < env->subprog_cnt; i++)
9956 bpf_jit_free(func[i]);
9959 /* cleanup main prog to be interpreted */
9960 prog->jit_requested = 0;
9961 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
9962 if (insn->code != (BPF_JMP | BPF_CALL) ||
9963 insn->src_reg != BPF_PSEUDO_CALL)
9966 insn->imm = env->insn_aux_data[i].call_imm;
9968 bpf_prog_free_jited_linfo(prog);
9972 static int fixup_call_args(struct bpf_verifier_env *env)
9974 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
9975 struct bpf_prog *prog = env->prog;
9976 struct bpf_insn *insn = prog->insnsi;
9981 if (env->prog->jit_requested &&
9982 !bpf_prog_is_dev_bound(env->prog->aux)) {
9983 err = jit_subprogs(env);
9989 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
9990 for (i = 0; i < prog->len; i++, insn++) {
9991 if (insn->code != (BPF_JMP | BPF_CALL) ||
9992 insn->src_reg != BPF_PSEUDO_CALL)
9994 depth = get_callee_stack_depth(env, insn, i);
9997 bpf_patch_call_args(insn, depth);
10004 /* fixup insn->imm field of bpf_call instructions
10005 * and inline eligible helpers as explicit sequence of BPF instructions
10007 * this function is called after eBPF program passed verification
10009 static int fixup_bpf_calls(struct bpf_verifier_env *env)
10011 struct bpf_prog *prog = env->prog;
10012 bool expect_blinding = bpf_jit_blinding_enabled(prog);
10013 struct bpf_insn *insn = prog->insnsi;
10014 const struct bpf_func_proto *fn;
10015 const int insn_cnt = prog->len;
10016 const struct bpf_map_ops *ops;
10017 struct bpf_insn_aux_data *aux;
10018 struct bpf_insn insn_buf[16];
10019 struct bpf_prog *new_prog;
10020 struct bpf_map *map_ptr;
10021 int i, ret, cnt, delta = 0;
10023 for (i = 0; i < insn_cnt; i++, insn++) {
10024 if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) ||
10025 insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
10026 insn->code == (BPF_ALU | BPF_MOD | BPF_X) ||
10027 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
10028 bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
10029 struct bpf_insn mask_and_div[] = {
10030 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
10031 /* Rx div 0 -> 0 */
10032 BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2),
10033 BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg),
10034 BPF_JMP_IMM(BPF_JA, 0, 0, 1),
10037 struct bpf_insn mask_and_mod[] = {
10038 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
10039 /* Rx mod 0 -> Rx */
10040 BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1),
10043 struct bpf_insn *patchlet;
10045 if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
10046 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
10047 patchlet = mask_and_div + (is64 ? 1 : 0);
10048 cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0);
10050 patchlet = mask_and_mod + (is64 ? 1 : 0);
10051 cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0);
10054 new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt);
10059 env->prog = prog = new_prog;
10060 insn = new_prog->insnsi + i + delta;
10064 if (BPF_CLASS(insn->code) == BPF_LD &&
10065 (BPF_MODE(insn->code) == BPF_ABS ||
10066 BPF_MODE(insn->code) == BPF_IND)) {
10067 cnt = env->ops->gen_ld_abs(insn, insn_buf);
10068 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
10069 verbose(env, "bpf verifier is misconfigured\n");
10073 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
10078 env->prog = prog = new_prog;
10079 insn = new_prog->insnsi + i + delta;
10083 if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) ||
10084 insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) {
10085 const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X;
10086 const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X;
10087 struct bpf_insn insn_buf[16];
10088 struct bpf_insn *patch = &insn_buf[0];
10092 aux = &env->insn_aux_data[i + delta];
10093 if (!aux->alu_state ||
10094 aux->alu_state == BPF_ALU_NON_POINTER)
10097 isneg = aux->alu_state & BPF_ALU_NEG_VALUE;
10098 issrc = (aux->alu_state & BPF_ALU_SANITIZE) ==
10099 BPF_ALU_SANITIZE_SRC;
10101 off_reg = issrc ? insn->src_reg : insn->dst_reg;
10103 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
10104 *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1);
10105 *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);
10106 *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);
10107 *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0);
10108 *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63);
10110 *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX,
10112 insn->src_reg = BPF_REG_AX;
10114 *patch++ = BPF_ALU64_REG(BPF_AND, off_reg,
10118 insn->code = insn->code == code_add ?
10119 code_sub : code_add;
10121 if (issrc && isneg)
10122 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
10123 cnt = patch - insn_buf;
10125 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
10130 env->prog = prog = new_prog;
10131 insn = new_prog->insnsi + i + delta;
10135 if (insn->code != (BPF_JMP | BPF_CALL))
10137 if (insn->src_reg == BPF_PSEUDO_CALL)
10140 if (insn->imm == BPF_FUNC_get_route_realm)
10141 prog->dst_needed = 1;
10142 if (insn->imm == BPF_FUNC_get_prandom_u32)
10143 bpf_user_rnd_init_once();
10144 if (insn->imm == BPF_FUNC_override_return)
10145 prog->kprobe_override = 1;
10146 if (insn->imm == BPF_FUNC_tail_call) {
10147 /* If we tail call into other programs, we
10148 * cannot make any assumptions since they can
10149 * be replaced dynamically during runtime in
10150 * the program array.
10152 prog->cb_access = 1;
10153 env->prog->aux->stack_depth = MAX_BPF_STACK;
10154 env->prog->aux->max_pkt_offset = MAX_PACKET_OFF;
10156 /* mark bpf_tail_call as different opcode to avoid
10157 * conditional branch in the interpeter for every normal
10158 * call and to prevent accidental JITing by JIT compiler
10159 * that doesn't support bpf_tail_call yet
10162 insn->code = BPF_JMP | BPF_TAIL_CALL;
10164 aux = &env->insn_aux_data[i + delta];
10165 if (env->bpf_capable && !expect_blinding &&
10166 prog->jit_requested &&
10167 !bpf_map_key_poisoned(aux) &&
10168 !bpf_map_ptr_poisoned(aux) &&
10169 !bpf_map_ptr_unpriv(aux)) {
10170 struct bpf_jit_poke_descriptor desc = {
10171 .reason = BPF_POKE_REASON_TAIL_CALL,
10172 .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state),
10173 .tail_call.key = bpf_map_key_immediate(aux),
10176 ret = bpf_jit_add_poke_descriptor(prog, &desc);
10178 verbose(env, "adding tail call poke descriptor failed\n");
10182 insn->imm = ret + 1;
10186 if (!bpf_map_ptr_unpriv(aux))
10189 /* instead of changing every JIT dealing with tail_call
10190 * emit two extra insns:
10191 * if (index >= max_entries) goto out;
10192 * index &= array->index_mask;
10193 * to avoid out-of-bounds cpu speculation
10195 if (bpf_map_ptr_poisoned(aux)) {
10196 verbose(env, "tail_call abusing map_ptr\n");
10200 map_ptr = BPF_MAP_PTR(aux->map_ptr_state);
10201 insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3,
10202 map_ptr->max_entries, 2);
10203 insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3,
10204 container_of(map_ptr,
10207 insn_buf[2] = *insn;
10209 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
10214 env->prog = prog = new_prog;
10215 insn = new_prog->insnsi + i + delta;
10219 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
10220 * and other inlining handlers are currently limited to 64 bit
10223 if (prog->jit_requested && BITS_PER_LONG == 64 &&
10224 (insn->imm == BPF_FUNC_map_lookup_elem ||
10225 insn->imm == BPF_FUNC_map_update_elem ||
10226 insn->imm == BPF_FUNC_map_delete_elem ||
10227 insn->imm == BPF_FUNC_map_push_elem ||
10228 insn->imm == BPF_FUNC_map_pop_elem ||
10229 insn->imm == BPF_FUNC_map_peek_elem)) {
10230 aux = &env->insn_aux_data[i + delta];
10231 if (bpf_map_ptr_poisoned(aux))
10232 goto patch_call_imm;
10234 map_ptr = BPF_MAP_PTR(aux->map_ptr_state);
10235 ops = map_ptr->ops;
10236 if (insn->imm == BPF_FUNC_map_lookup_elem &&
10237 ops->map_gen_lookup) {
10238 cnt = ops->map_gen_lookup(map_ptr, insn_buf);
10239 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
10240 verbose(env, "bpf verifier is misconfigured\n");
10244 new_prog = bpf_patch_insn_data(env, i + delta,
10250 env->prog = prog = new_prog;
10251 insn = new_prog->insnsi + i + delta;
10255 BUILD_BUG_ON(!__same_type(ops->map_lookup_elem,
10256 (void *(*)(struct bpf_map *map, void *key))NULL));
10257 BUILD_BUG_ON(!__same_type(ops->map_delete_elem,
10258 (int (*)(struct bpf_map *map, void *key))NULL));
10259 BUILD_BUG_ON(!__same_type(ops->map_update_elem,
10260 (int (*)(struct bpf_map *map, void *key, void *value,
10262 BUILD_BUG_ON(!__same_type(ops->map_push_elem,
10263 (int (*)(struct bpf_map *map, void *value,
10265 BUILD_BUG_ON(!__same_type(ops->map_pop_elem,
10266 (int (*)(struct bpf_map *map, void *value))NULL));
10267 BUILD_BUG_ON(!__same_type(ops->map_peek_elem,
10268 (int (*)(struct bpf_map *map, void *value))NULL));
10270 switch (insn->imm) {
10271 case BPF_FUNC_map_lookup_elem:
10272 insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) -
10275 case BPF_FUNC_map_update_elem:
10276 insn->imm = BPF_CAST_CALL(ops->map_update_elem) -
10279 case BPF_FUNC_map_delete_elem:
10280 insn->imm = BPF_CAST_CALL(ops->map_delete_elem) -
10283 case BPF_FUNC_map_push_elem:
10284 insn->imm = BPF_CAST_CALL(ops->map_push_elem) -
10287 case BPF_FUNC_map_pop_elem:
10288 insn->imm = BPF_CAST_CALL(ops->map_pop_elem) -
10291 case BPF_FUNC_map_peek_elem:
10292 insn->imm = BPF_CAST_CALL(ops->map_peek_elem) -
10297 goto patch_call_imm;
10300 if (prog->jit_requested && BITS_PER_LONG == 64 &&
10301 insn->imm == BPF_FUNC_jiffies64) {
10302 struct bpf_insn ld_jiffies_addr[2] = {
10303 BPF_LD_IMM64(BPF_REG_0,
10304 (unsigned long)&jiffies),
10307 insn_buf[0] = ld_jiffies_addr[0];
10308 insn_buf[1] = ld_jiffies_addr[1];
10309 insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0,
10313 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
10319 env->prog = prog = new_prog;
10320 insn = new_prog->insnsi + i + delta;
10325 fn = env->ops->get_func_proto(insn->imm, env->prog);
10326 /* all functions that have prototype and verifier allowed
10327 * programs to call them, must be real in-kernel functions
10331 "kernel subsystem misconfigured func %s#%d\n",
10332 func_id_name(insn->imm), insn->imm);
10335 insn->imm = fn->func - __bpf_call_base;
10338 /* Since poke tab is now finalized, publish aux to tracker. */
10339 for (i = 0; i < prog->aux->size_poke_tab; i++) {
10340 map_ptr = prog->aux->poke_tab[i].tail_call.map;
10341 if (!map_ptr->ops->map_poke_track ||
10342 !map_ptr->ops->map_poke_untrack ||
10343 !map_ptr->ops->map_poke_run) {
10344 verbose(env, "bpf verifier is misconfigured\n");
10348 ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux);
10350 verbose(env, "tracking tail call prog failed\n");
10358 static void free_states(struct bpf_verifier_env *env)
10360 struct bpf_verifier_state_list *sl, *sln;
10363 sl = env->free_list;
10366 free_verifier_state(&sl->state, false);
10370 env->free_list = NULL;
10372 if (!env->explored_states)
10375 for (i = 0; i < state_htab_size(env); i++) {
10376 sl = env->explored_states[i];
10380 free_verifier_state(&sl->state, false);
10384 env->explored_states[i] = NULL;
10388 /* The verifier is using insn_aux_data[] to store temporary data during
10389 * verification and to store information for passes that run after the
10390 * verification like dead code sanitization. do_check_common() for subprogram N
10391 * may analyze many other subprograms. sanitize_insn_aux_data() clears all
10392 * temporary data after do_check_common() finds that subprogram N cannot be
10393 * verified independently. pass_cnt counts the number of times
10394 * do_check_common() was run and insn->aux->seen tells the pass number
10395 * insn_aux_data was touched. These variables are compared to clear temporary
10396 * data from failed pass. For testing and experiments do_check_common() can be
10397 * run multiple times even when prior attempt to verify is unsuccessful.
10399 static void sanitize_insn_aux_data(struct bpf_verifier_env *env)
10401 struct bpf_insn *insn = env->prog->insnsi;
10402 struct bpf_insn_aux_data *aux;
10405 for (i = 0; i < env->prog->len; i++) {
10406 class = BPF_CLASS(insn[i].code);
10407 if (class != BPF_LDX && class != BPF_STX)
10409 aux = &env->insn_aux_data[i];
10410 if (aux->seen != env->pass_cnt)
10412 memset(aux, 0, offsetof(typeof(*aux), orig_idx));
10416 static int do_check_common(struct bpf_verifier_env *env, int subprog)
10418 bool pop_log = !(env->log.level & BPF_LOG_LEVEL2);
10419 struct bpf_verifier_state *state;
10420 struct bpf_reg_state *regs;
10423 env->prev_linfo = NULL;
10426 state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
10429 state->curframe = 0;
10430 state->speculative = false;
10431 state->branches = 1;
10432 state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL);
10433 if (!state->frame[0]) {
10437 env->cur_state = state;
10438 init_func_state(env, state->frame[0],
10439 BPF_MAIN_FUNC /* callsite */,
10443 regs = state->frame[state->curframe]->regs;
10444 if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) {
10445 ret = btf_prepare_func_args(env, subprog, regs);
10448 for (i = BPF_REG_1; i <= BPF_REG_5; i++) {
10449 if (regs[i].type == PTR_TO_CTX)
10450 mark_reg_known_zero(env, regs, i);
10451 else if (regs[i].type == SCALAR_VALUE)
10452 mark_reg_unknown(env, regs, i);
10455 /* 1st arg to a function */
10456 regs[BPF_REG_1].type = PTR_TO_CTX;
10457 mark_reg_known_zero(env, regs, BPF_REG_1);
10458 ret = btf_check_func_arg_match(env, subprog, regs);
10459 if (ret == -EFAULT)
10460 /* unlikely verifier bug. abort.
10461 * ret == 0 and ret < 0 are sadly acceptable for
10462 * main() function due to backward compatibility.
10463 * Like socket filter program may be written as:
10464 * int bpf_prog(struct pt_regs *ctx)
10465 * and never dereference that ctx in the program.
10466 * 'struct pt_regs' is a type mismatch for socket
10467 * filter that should be using 'struct __sk_buff'.
10472 ret = do_check(env);
10474 /* check for NULL is necessary, since cur_state can be freed inside
10475 * do_check() under memory pressure.
10477 if (env->cur_state) {
10478 free_verifier_state(env->cur_state, true);
10479 env->cur_state = NULL;
10481 while (!pop_stack(env, NULL, NULL, false));
10482 if (!ret && pop_log)
10483 bpf_vlog_reset(&env->log, 0);
10486 /* clean aux data in case subprog was rejected */
10487 sanitize_insn_aux_data(env);
10491 /* Verify all global functions in a BPF program one by one based on their BTF.
10492 * All global functions must pass verification. Otherwise the whole program is rejected.
10503 * foo() will be verified first for R1=any_scalar_value. During verification it
10504 * will be assumed that bar() already verified successfully and call to bar()
10505 * from foo() will be checked for type match only. Later bar() will be verified
10506 * independently to check that it's safe for R1=any_scalar_value.
10508 static int do_check_subprogs(struct bpf_verifier_env *env)
10510 struct bpf_prog_aux *aux = env->prog->aux;
10513 if (!aux->func_info)
10516 for (i = 1; i < env->subprog_cnt; i++) {
10517 if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL)
10519 env->insn_idx = env->subprog_info[i].start;
10520 WARN_ON_ONCE(env->insn_idx == 0);
10521 ret = do_check_common(env, i);
10524 } else if (env->log.level & BPF_LOG_LEVEL) {
10526 "Func#%d is safe for any args that match its prototype\n",
10533 static int do_check_main(struct bpf_verifier_env *env)
10538 ret = do_check_common(env, 0);
10540 env->prog->aux->stack_depth = env->subprog_info[0].stack_depth;
10545 static void print_verification_stats(struct bpf_verifier_env *env)
10549 if (env->log.level & BPF_LOG_STATS) {
10550 verbose(env, "verification time %lld usec\n",
10551 div_u64(env->verification_time, 1000));
10552 verbose(env, "stack depth ");
10553 for (i = 0; i < env->subprog_cnt; i++) {
10554 u32 depth = env->subprog_info[i].stack_depth;
10556 verbose(env, "%d", depth);
10557 if (i + 1 < env->subprog_cnt)
10560 verbose(env, "\n");
10562 verbose(env, "processed %d insns (limit %d) max_states_per_insn %d "
10563 "total_states %d peak_states %d mark_read %d\n",
10564 env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS,
10565 env->max_states_per_insn, env->total_states,
10566 env->peak_states, env->longest_mark_read_walk);
10569 static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
10571 const struct btf_type *t, *func_proto;
10572 const struct bpf_struct_ops *st_ops;
10573 const struct btf_member *member;
10574 struct bpf_prog *prog = env->prog;
10575 u32 btf_id, member_idx;
10578 btf_id = prog->aux->attach_btf_id;
10579 st_ops = bpf_struct_ops_find(btf_id);
10581 verbose(env, "attach_btf_id %u is not a supported struct\n",
10587 member_idx = prog->expected_attach_type;
10588 if (member_idx >= btf_type_vlen(t)) {
10589 verbose(env, "attach to invalid member idx %u of struct %s\n",
10590 member_idx, st_ops->name);
10594 member = &btf_type_member(t)[member_idx];
10595 mname = btf_name_by_offset(btf_vmlinux, member->name_off);
10596 func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type,
10599 verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n",
10600 mname, member_idx, st_ops->name);
10604 if (st_ops->check_member) {
10605 int err = st_ops->check_member(t, member);
10608 verbose(env, "attach to unsupported member %s of struct %s\n",
10609 mname, st_ops->name);
10614 prog->aux->attach_func_proto = func_proto;
10615 prog->aux->attach_func_name = mname;
10616 env->ops = st_ops->verifier_ops;
10620 #define SECURITY_PREFIX "security_"
10622 static int check_attach_modify_return(struct bpf_prog *prog, unsigned long addr)
10624 if (within_error_injection_list(addr) ||
10625 !strncmp(SECURITY_PREFIX, prog->aux->attach_func_name,
10626 sizeof(SECURITY_PREFIX) - 1))
10632 static int check_attach_btf_id(struct bpf_verifier_env *env)
10634 struct bpf_prog *prog = env->prog;
10635 bool prog_extension = prog->type == BPF_PROG_TYPE_EXT;
10636 struct bpf_prog *tgt_prog = prog->aux->linked_prog;
10637 u32 btf_id = prog->aux->attach_btf_id;
10638 const char prefix[] = "btf_trace_";
10639 struct btf_func_model fmodel;
10640 int ret = 0, subprog = -1, i;
10641 struct bpf_trampoline *tr;
10642 const struct btf_type *t;
10643 bool conservative = true;
10649 if (prog->type == BPF_PROG_TYPE_STRUCT_OPS)
10650 return check_struct_ops_btf_id(env);
10652 if (prog->type != BPF_PROG_TYPE_TRACING &&
10653 prog->type != BPF_PROG_TYPE_LSM &&
10658 verbose(env, "Tracing programs must provide btf_id\n");
10661 btf = bpf_prog_get_target_btf(prog);
10664 "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n");
10667 t = btf_type_by_id(btf, btf_id);
10669 verbose(env, "attach_btf_id %u is invalid\n", btf_id);
10672 tname = btf_name_by_offset(btf, t->name_off);
10674 verbose(env, "attach_btf_id %u doesn't have a name\n", btf_id);
10678 struct bpf_prog_aux *aux = tgt_prog->aux;
10680 for (i = 0; i < aux->func_info_cnt; i++)
10681 if (aux->func_info[i].type_id == btf_id) {
10685 if (subprog == -1) {
10686 verbose(env, "Subprog %s doesn't exist\n", tname);
10689 conservative = aux->func_info_aux[subprog].unreliable;
10690 if (prog_extension) {
10691 if (conservative) {
10693 "Cannot replace static functions\n");
10696 if (!prog->jit_requested) {
10698 "Extension programs should be JITed\n");
10701 env->ops = bpf_verifier_ops[tgt_prog->type];
10702 prog->expected_attach_type = tgt_prog->expected_attach_type;
10704 if (!tgt_prog->jited) {
10705 verbose(env, "Can attach to only JITed progs\n");
10708 if (tgt_prog->type == prog->type) {
10709 /* Cannot fentry/fexit another fentry/fexit program.
10710 * Cannot attach program extension to another extension.
10711 * It's ok to attach fentry/fexit to extension program.
10713 verbose(env, "Cannot recursively attach\n");
10716 if (tgt_prog->type == BPF_PROG_TYPE_TRACING &&
10718 (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY ||
10719 tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) {
10720 /* Program extensions can extend all program types
10721 * except fentry/fexit. The reason is the following.
10722 * The fentry/fexit programs are used for performance
10723 * analysis, stats and can be attached to any program
10724 * type except themselves. When extension program is
10725 * replacing XDP function it is necessary to allow
10726 * performance analysis of all functions. Both original
10727 * XDP program and its program extension. Hence
10728 * attaching fentry/fexit to BPF_PROG_TYPE_EXT is
10729 * allowed. If extending of fentry/fexit was allowed it
10730 * would be possible to create long call chain
10731 * fentry->extension->fentry->extension beyond
10732 * reasonable stack size. Hence extending fentry is not
10735 verbose(env, "Cannot extend fentry/fexit\n");
10738 key = ((u64)aux->id) << 32 | btf_id;
10740 if (prog_extension) {
10741 verbose(env, "Cannot replace kernel functions\n");
10747 switch (prog->expected_attach_type) {
10748 case BPF_TRACE_RAW_TP:
10751 "Only FENTRY/FEXIT progs are attachable to another BPF prog\n");
10754 if (!btf_type_is_typedef(t)) {
10755 verbose(env, "attach_btf_id %u is not a typedef\n",
10759 if (strncmp(prefix, tname, sizeof(prefix) - 1)) {
10760 verbose(env, "attach_btf_id %u points to wrong type name %s\n",
10764 tname += sizeof(prefix) - 1;
10765 t = btf_type_by_id(btf, t->type);
10766 if (!btf_type_is_ptr(t))
10767 /* should never happen in valid vmlinux build */
10769 t = btf_type_by_id(btf, t->type);
10770 if (!btf_type_is_func_proto(t))
10771 /* should never happen in valid vmlinux build */
10774 /* remember two read only pointers that are valid for
10775 * the life time of the kernel
10777 prog->aux->attach_func_name = tname;
10778 prog->aux->attach_func_proto = t;
10779 prog->aux->attach_btf_trace = true;
10781 case BPF_TRACE_ITER:
10782 if (!btf_type_is_func(t)) {
10783 verbose(env, "attach_btf_id %u is not a function\n",
10787 t = btf_type_by_id(btf, t->type);
10788 if (!btf_type_is_func_proto(t))
10790 prog->aux->attach_func_name = tname;
10791 prog->aux->attach_func_proto = t;
10792 if (!bpf_iter_prog_supported(prog))
10794 ret = btf_distill_func_proto(&env->log, btf, t,
10798 if (!prog_extension)
10801 case BPF_MODIFY_RETURN:
10803 case BPF_TRACE_FENTRY:
10804 case BPF_TRACE_FEXIT:
10805 prog->aux->attach_func_name = tname;
10806 if (prog->type == BPF_PROG_TYPE_LSM) {
10807 ret = bpf_lsm_verify_prog(&env->log, prog);
10812 if (!btf_type_is_func(t)) {
10813 verbose(env, "attach_btf_id %u is not a function\n",
10817 if (prog_extension &&
10818 btf_check_type_match(env, prog, btf, t))
10820 t = btf_type_by_id(btf, t->type);
10821 if (!btf_type_is_func_proto(t))
10823 tr = bpf_trampoline_lookup(key);
10826 /* t is either vmlinux type or another program's type */
10827 prog->aux->attach_func_proto = t;
10828 mutex_lock(&tr->mutex);
10829 if (tr->func.addr) {
10830 prog->aux->trampoline = tr;
10833 if (tgt_prog && conservative) {
10834 prog->aux->attach_func_proto = NULL;
10837 ret = btf_distill_func_proto(&env->log, btf, t,
10838 tname, &tr->func.model);
10843 addr = (long) tgt_prog->bpf_func;
10845 addr = (long) tgt_prog->aux->func[subprog]->bpf_func;
10847 addr = kallsyms_lookup_name(tname);
10850 "The address of function %s cannot be found\n",
10857 if (prog->expected_attach_type == BPF_MODIFY_RETURN) {
10858 ret = check_attach_modify_return(prog, addr);
10860 verbose(env, "%s() is not modifiable\n",
10861 prog->aux->attach_func_name);
10866 tr->func.addr = (void *)addr;
10867 prog->aux->trampoline = tr;
10869 mutex_unlock(&tr->mutex);
10871 bpf_trampoline_put(tr);
10876 int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
10877 union bpf_attr __user *uattr)
10879 u64 start_time = ktime_get_ns();
10880 struct bpf_verifier_env *env;
10881 struct bpf_verifier_log *log;
10882 int i, len, ret = -EINVAL;
10885 /* no program is valid */
10886 if (ARRAY_SIZE(bpf_verifier_ops) == 0)
10889 /* 'struct bpf_verifier_env' can be global, but since it's not small,
10890 * allocate/free it every time bpf_check() is called
10892 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
10897 len = (*prog)->len;
10898 env->insn_aux_data =
10899 vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len));
10901 if (!env->insn_aux_data)
10903 for (i = 0; i < len; i++)
10904 env->insn_aux_data[i].orig_idx = i;
10906 env->ops = bpf_verifier_ops[env->prog->type];
10907 is_priv = bpf_capable();
10909 if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) {
10910 mutex_lock(&bpf_verifier_lock);
10912 btf_vmlinux = btf_parse_vmlinux();
10913 mutex_unlock(&bpf_verifier_lock);
10916 /* grab the mutex to protect few globals used by verifier */
10918 mutex_lock(&bpf_verifier_lock);
10920 if (attr->log_level || attr->log_buf || attr->log_size) {
10921 /* user requested verbose verifier output
10922 * and supplied buffer to store the verification trace
10924 log->level = attr->log_level;
10925 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
10926 log->len_total = attr->log_size;
10929 /* log attributes have to be sane */
10930 if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 ||
10931 !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK)
10935 if (IS_ERR(btf_vmlinux)) {
10936 /* Either gcc or pahole or kernel are broken. */
10937 verbose(env, "in-kernel BTF is malformed\n");
10938 ret = PTR_ERR(btf_vmlinux);
10939 goto skip_full_check;
10942 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
10943 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
10944 env->strict_alignment = true;
10945 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
10946 env->strict_alignment = false;
10948 env->allow_ptr_leaks = bpf_allow_ptr_leaks();
10949 env->bypass_spec_v1 = bpf_bypass_spec_v1();
10950 env->bypass_spec_v4 = bpf_bypass_spec_v4();
10951 env->bpf_capable = bpf_capable();
10954 env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ;
10956 ret = replace_map_fd_with_map_ptr(env);
10958 goto skip_full_check;
10960 if (bpf_prog_is_dev_bound(env->prog->aux)) {
10961 ret = bpf_prog_offload_verifier_prep(env->prog);
10963 goto skip_full_check;
10966 env->explored_states = kvcalloc(state_htab_size(env),
10967 sizeof(struct bpf_verifier_state_list *),
10970 if (!env->explored_states)
10971 goto skip_full_check;
10973 ret = check_subprogs(env);
10975 goto skip_full_check;
10977 ret = check_btf_info(env, attr, uattr);
10979 goto skip_full_check;
10981 ret = check_attach_btf_id(env);
10983 goto skip_full_check;
10985 ret = check_cfg(env);
10987 goto skip_full_check;
10989 ret = do_check_subprogs(env);
10990 ret = ret ?: do_check_main(env);
10992 if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux))
10993 ret = bpf_prog_offload_finalize(env);
10996 kvfree(env->explored_states);
10999 ret = check_max_stack_depth(env);
11001 /* instruction rewrites happen after this point */
11004 opt_hard_wire_dead_code_branches(env);
11006 ret = opt_remove_dead_code(env);
11008 ret = opt_remove_nops(env);
11011 sanitize_dead_code(env);
11015 /* program is valid, convert *(u32*)(ctx + off) accesses */
11016 ret = convert_ctx_accesses(env);
11019 ret = fixup_bpf_calls(env);
11021 /* do 32-bit optimization after insn patching has done so those patched
11022 * insns could be handled correctly.
11024 if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) {
11025 ret = opt_subreg_zext_lo32_rnd_hi32(env, attr);
11026 env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret
11031 ret = fixup_call_args(env);
11033 env->verification_time = ktime_get_ns() - start_time;
11034 print_verification_stats(env);
11036 if (log->level && bpf_verifier_log_full(log))
11038 if (log->level && !log->ubuf) {
11040 goto err_release_maps;
11043 if (ret == 0 && env->used_map_cnt) {
11044 /* if program passed verifier, update used_maps in bpf_prog_info */
11045 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
11046 sizeof(env->used_maps[0]),
11049 if (!env->prog->aux->used_maps) {
11051 goto err_release_maps;
11054 memcpy(env->prog->aux->used_maps, env->used_maps,
11055 sizeof(env->used_maps[0]) * env->used_map_cnt);
11056 env->prog->aux->used_map_cnt = env->used_map_cnt;
11058 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
11059 * bpf_ld_imm64 instructions
11061 convert_pseudo_ld_imm64(env);
11065 adjust_btf_func(env);
11068 if (!env->prog->aux->used_maps)
11069 /* if we didn't copy map pointers into bpf_prog_info, release
11070 * them now. Otherwise free_used_maps() will release them.
11074 /* extension progs temporarily inherit the attach_type of their targets
11075 for verification purposes, so set it back to zero before returning
11077 if (env->prog->type == BPF_PROG_TYPE_EXT)
11078 env->prog->expected_attach_type = 0;
11083 mutex_unlock(&bpf_verifier_lock);
11084 vfree(env->insn_aux_data);