Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016 Facebook |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4 | * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5 | */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6 | #include <uapi/linux/btf.h> |
Jakub Kicinski | aef2fed | 2021-12-15 18:55:37 -0800 | [diff] [blame] | 7 | #include <linux/bpf-cgroup.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 8 | #include <linux/kernel.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/bpf.h> |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 12 | #include <linux/btf.h> |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 13 | #include <linux/bpf_verifier.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 14 | #include <linux/filter.h> |
| 15 | #include <net/netlink.h> |
| 16 | #include <linux/file.h> |
| 17 | #include <linux/vmalloc.h> |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 18 | #include <linux/stringify.h> |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 19 | #include <linux/bsearch.h> |
| 20 | #include <linux/sort.h> |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 21 | #include <linux/perf_event.h> |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 22 | #include <linux/ctype.h> |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 23 | #include <linux/error-injection.h> |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 24 | #include <linux/bpf_lsm.h> |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 25 | #include <linux/btf_ids.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 26 | |
Jakub Kicinski | f4ac7e0 | 2017-10-09 10:30:12 -0700 | [diff] [blame] | 27 | #include "disasm.h" |
| 28 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 29 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
Alexei Starovoitov | 91cc1a9 | 2019-11-14 10:57:15 -0800 | [diff] [blame] | 30 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 31 | [_id] = & _name ## _verifier_ops, |
| 32 | #define BPF_MAP_TYPE(_id, _ops) |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 33 | #define BPF_LINK_TYPE(_id, _name) |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 34 | #include <linux/bpf_types.h> |
| 35 | #undef BPF_PROG_TYPE |
| 36 | #undef BPF_MAP_TYPE |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 37 | #undef BPF_LINK_TYPE |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 40 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 41 | * instruction by instruction and updates register/stack state. |
| 42 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 43 | * |
| 44 | * The first pass is depth-first-search to check that the program is a DAG. |
| 45 | * It rejects the following programs: |
| 46 | * - larger than BPF_MAXINSNS insns |
| 47 | * - if loop is present (detected via back-edge) |
| 48 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 49 | * - out of bounds or malformed jumps |
| 50 | * The second pass is all possible path descent from the 1st insn. |
Zhen Lei | 8fb33b6 | 2021-05-25 10:56:59 +0800 | [diff] [blame] | 51 | * Since it's analyzing all paths through the program, the length of the |
Gary Lin | eba38a9 | 2017-03-01 16:25:51 +0800 | [diff] [blame] | 52 | * analysis is limited to 64k insn, which may be hit even if total number of |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 53 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 54 | * Number of 'branches to be analyzed' is limited to 1k |
| 55 | * |
| 56 | * On entry to each instruction, each register has a type, and the instruction |
| 57 | * changes the types of the registers depending on instruction semantics. |
| 58 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 59 | * copied to R1. |
| 60 | * |
| 61 | * All registers are 64-bit. |
| 62 | * R0 - return register |
| 63 | * R1-R5 argument passing registers |
| 64 | * R6-R9 callee saved registers |
| 65 | * R10 - frame pointer read-only |
| 66 | * |
| 67 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 68 | * and has type PTR_TO_CTX. |
| 69 | * |
| 70 | * Verifier tracks arithmetic operations on pointers in case: |
| 71 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 72 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 73 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 74 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 75 | * that it wants to construct a pointer to some element within stack. |
| 76 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 77 | * (and -20 constant is saved for further stack bounds checking). |
| 78 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 79 | * |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 80 | * Most of the time the registers have SCALAR_VALUE type, which |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 81 | * means the register has some value, but it's not a valid pointer. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 82 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 83 | * |
| 84 | * When verifier sees load or store instructions the type of base register |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 85 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
| 86 | * four pointer types recognized by check_mem_access() function. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 87 | * |
| 88 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 89 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 90 | * |
| 91 | * registers used to pass values to function calls are checked against |
| 92 | * function argument constraints. |
| 93 | * |
| 94 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 95 | * It means that the register type passed to this function must be |
| 96 | * PTR_TO_STACK and it will be used inside the function as |
| 97 | * 'pointer to map element key' |
| 98 | * |
| 99 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 100 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 101 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 102 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 103 | * |
| 104 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 105 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 106 | * 2nd argument should be a pointer to stack, which will be used inside |
| 107 | * the helper function as a pointer to map element key. |
| 108 | * |
| 109 | * On the kernel side the helper function looks like: |
| 110 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 111 | * { |
| 112 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 113 | * void *key = (void *) (unsigned long) r2; |
| 114 | * void *value; |
| 115 | * |
| 116 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 117 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 118 | * the stack of eBPF program. |
| 119 | * } |
| 120 | * |
| 121 | * Corresponding eBPF program may look like: |
| 122 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 123 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 124 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 125 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 126 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 127 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 128 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 129 | * |
| 130 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 131 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 132 | * and were initialized prior to this call. |
| 133 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 134 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 135 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
Zhen Lei | 8fb33b6 | 2021-05-25 10:56:59 +0800 | [diff] [blame] | 136 | * returns either pointer to map value or NULL. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 137 | * |
| 138 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 139 | * insn, the register holding that pointer in the true branch changes state to |
| 140 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 141 | * branch. See check_cond_jmp_op(). |
| 142 | * |
| 143 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 144 | * are set to NOT_INIT to indicate that they are no longer readable. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 145 | * |
| 146 | * The following reference types represent a potential reference to a kernel |
| 147 | * resource which, after first being allocated, must be checked and freed by |
| 148 | * the BPF program: |
| 149 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET |
| 150 | * |
| 151 | * When the verifier sees a helper call return a reference type, it allocates a |
| 152 | * pointer id for the reference and stores it in the current function state. |
| 153 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into |
| 154 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type |
| 155 | * passes through a NULL-check conditional. For the branch wherein the state is |
| 156 | * changed to CONST_IMM, the verifier releases the reference. |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 157 | * |
| 158 | * For each helper function that allocates a reference, such as |
| 159 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as |
| 160 | * bpf_sk_release(). When a reference type passes into the release function, |
| 161 | * the verifier also releases the reference. If any unchecked or unreleased |
| 162 | * reference remains at the end of the program, the verifier rejects it. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 163 | */ |
| 164 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 165 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 166 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 167 | /* verifer state is 'st' |
| 168 | * before processing instruction 'insn_idx' |
| 169 | * and after processing instruction 'prev_insn_idx' |
| 170 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 171 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 172 | int insn_idx; |
| 173 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 174 | struct bpf_verifier_stack_elem *next; |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 175 | /* length of verifier log at the time this state was pushed on stack */ |
| 176 | u32 log_pos; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 179 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 180 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 181 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 182 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
| 183 | #define BPF_MAP_KEY_SEEN (1ULL << 62) |
| 184 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 185 | #define BPF_MAP_PTR_UNPRIV 1UL |
| 186 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ |
| 187 | POISON_POINTER_DELTA)) |
| 188 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) |
| 189 | |
| 190 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
| 191 | { |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 192 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) |
| 196 | { |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 197 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, |
| 201 | const struct bpf_map *map, bool unpriv) |
| 202 | { |
| 203 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); |
| 204 | unpriv |= bpf_map_ptr_unpriv(aux); |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 205 | aux->map_ptr_state = (unsigned long)map | |
| 206 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); |
| 207 | } |
| 208 | |
| 209 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) |
| 210 | { |
| 211 | return aux->map_key_state & BPF_MAP_KEY_POISON; |
| 212 | } |
| 213 | |
| 214 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) |
| 215 | { |
| 216 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); |
| 217 | } |
| 218 | |
| 219 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) |
| 220 | { |
| 221 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); |
| 222 | } |
| 223 | |
| 224 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) |
| 225 | { |
| 226 | bool poisoned = bpf_map_key_poisoned(aux); |
| 227 | |
| 228 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | |
| 229 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 230 | } |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 231 | |
Yonghong Song | 23a2d70 | 2021-02-04 15:48:27 -0800 | [diff] [blame] | 232 | static bool bpf_pseudo_call(const struct bpf_insn *insn) |
| 233 | { |
| 234 | return insn->code == (BPF_JMP | BPF_CALL) && |
| 235 | insn->src_reg == BPF_PSEUDO_CALL; |
| 236 | } |
| 237 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 238 | static bool bpf_pseudo_kfunc_call(const struct bpf_insn *insn) |
| 239 | { |
| 240 | return insn->code == (BPF_JMP | BPF_CALL) && |
| 241 | insn->src_reg == BPF_PSEUDO_KFUNC_CALL; |
| 242 | } |
| 243 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 244 | struct bpf_call_arg_meta { |
| 245 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 246 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 247 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 248 | int regno; |
| 249 | int access_size; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 250 | int mem_size; |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 251 | u64 msize_max_value; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 252 | int ref_obj_id; |
Alexei Starovoitov | 3e8ce29 | 2021-07-14 17:54:11 -0700 | [diff] [blame] | 253 | int map_uid; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 254 | int func_id; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 255 | struct btf *btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 256 | u32 btf_id; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 257 | struct btf *ret_btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 258 | u32 ret_btf_id; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 259 | u32 subprogno; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 260 | }; |
| 261 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 262 | struct btf *btf_vmlinux; |
| 263 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 264 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 265 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 266 | static const struct bpf_line_info * |
| 267 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) |
| 268 | { |
| 269 | const struct bpf_line_info *linfo; |
| 270 | const struct bpf_prog *prog; |
| 271 | u32 i, nr_linfo; |
| 272 | |
| 273 | prog = env->prog; |
| 274 | nr_linfo = prog->aux->nr_linfo; |
| 275 | |
| 276 | if (!nr_linfo || insn_off >= prog->len) |
| 277 | return NULL; |
| 278 | |
| 279 | linfo = prog->aux->linfo; |
| 280 | for (i = 1; i < nr_linfo; i++) |
| 281 | if (insn_off < linfo[i].insn_off) |
| 282 | break; |
| 283 | |
| 284 | return &linfo[i - 1]; |
| 285 | } |
| 286 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 287 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
| 288 | va_list args) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 289 | { |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 290 | unsigned int n; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 291 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 292 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 293 | |
| 294 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 295 | "verifier log line truncated - local buffer too short\n"); |
| 296 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 297 | if (log->level == BPF_LOG_KERNEL) { |
Hou Tao | 436d404 | 2021-12-01 15:34:57 +0800 | [diff] [blame] | 298 | bool newline = n > 0 && log->kbuf[n - 1] == '\n'; |
| 299 | |
| 300 | pr_err("BPF: %s%s", log->kbuf, newline ? "" : "\n"); |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 301 | return; |
| 302 | } |
Hou Tao | 436d404 | 2021-12-01 15:34:57 +0800 | [diff] [blame] | 303 | |
| 304 | n = min(log->len_total - log->len_used - 1, n); |
| 305 | log->kbuf[n] = '\0'; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 306 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 307 | log->len_used += n; |
| 308 | else |
| 309 | log->ubuf = NULL; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 310 | } |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 311 | |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 312 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
| 313 | { |
| 314 | char zero = 0; |
| 315 | |
| 316 | if (!bpf_verifier_log_needed(log)) |
| 317 | return; |
| 318 | |
| 319 | log->len_used = new_pos; |
| 320 | if (put_user(zero, log->ubuf + new_pos)) |
| 321 | log->ubuf = NULL; |
| 322 | } |
| 323 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 324 | /* log_level controls verbosity level of eBPF verifier. |
| 325 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 326 | * so the user can figure out what's wrong with the program |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 327 | */ |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 328 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 329 | const char *fmt, ...) |
| 330 | { |
| 331 | va_list args; |
| 332 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 333 | if (!bpf_verifier_log_needed(&env->log)) |
| 334 | return; |
| 335 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 336 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 337 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 338 | va_end(args); |
| 339 | } |
| 340 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 341 | |
| 342 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
| 343 | { |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 344 | struct bpf_verifier_env *env = private_data; |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 345 | va_list args; |
| 346 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 347 | if (!bpf_verifier_log_needed(&env->log)) |
| 348 | return; |
| 349 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 350 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 351 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 352 | va_end(args); |
| 353 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 354 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 355 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
| 356 | const char *fmt, ...) |
| 357 | { |
| 358 | va_list args; |
| 359 | |
| 360 | if (!bpf_verifier_log_needed(log)) |
| 361 | return; |
| 362 | |
| 363 | va_start(args, fmt); |
| 364 | bpf_verifier_vlog(log, fmt, args); |
| 365 | va_end(args); |
| 366 | } |
| 367 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 368 | static const char *ltrim(const char *s) |
| 369 | { |
| 370 | while (isspace(*s)) |
| 371 | s++; |
| 372 | |
| 373 | return s; |
| 374 | } |
| 375 | |
| 376 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, |
| 377 | u32 insn_off, |
| 378 | const char *prefix_fmt, ...) |
| 379 | { |
| 380 | const struct bpf_line_info *linfo; |
| 381 | |
| 382 | if (!bpf_verifier_log_needed(&env->log)) |
| 383 | return; |
| 384 | |
| 385 | linfo = find_linfo(env, insn_off); |
| 386 | if (!linfo || linfo == env->prev_linfo) |
| 387 | return; |
| 388 | |
| 389 | if (prefix_fmt) { |
| 390 | va_list args; |
| 391 | |
| 392 | va_start(args, prefix_fmt); |
| 393 | bpf_verifier_vlog(&env->log, prefix_fmt, args); |
| 394 | va_end(args); |
| 395 | } |
| 396 | |
| 397 | verbose(env, "%s\n", |
| 398 | ltrim(btf_name_by_offset(env->prog->aux->btf, |
| 399 | linfo->line_off))); |
| 400 | |
| 401 | env->prev_linfo = linfo; |
| 402 | } |
| 403 | |
Yonghong Song | bc2591d | 2021-02-26 12:49:22 -0800 | [diff] [blame] | 404 | static void verbose_invalid_scalar(struct bpf_verifier_env *env, |
| 405 | struct bpf_reg_state *reg, |
| 406 | struct tnum *range, const char *ctx, |
| 407 | const char *reg_name) |
| 408 | { |
| 409 | char tn_buf[48]; |
| 410 | |
| 411 | verbose(env, "At %s the register %s ", ctx, reg_name); |
| 412 | if (!tnum_is_unknown(reg->var_off)) { |
| 413 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 414 | verbose(env, "has value %s", tn_buf); |
| 415 | } else { |
| 416 | verbose(env, "has unknown scalar value"); |
| 417 | } |
| 418 | tnum_strn(tn_buf, sizeof(tn_buf), *range); |
| 419 | verbose(env, " should have been in %s\n", tn_buf); |
| 420 | } |
| 421 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 422 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 423 | { |
| 424 | return type == PTR_TO_PACKET || |
| 425 | type == PTR_TO_PACKET_META; |
| 426 | } |
| 427 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 428 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
| 429 | { |
| 430 | return type == PTR_TO_SOCKET || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 431 | type == PTR_TO_SOCK_COMMON || |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 432 | type == PTR_TO_TCP_SOCK || |
| 433 | type == PTR_TO_XDP_SOCK; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 434 | } |
| 435 | |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 436 | static bool reg_type_not_null(enum bpf_reg_type type) |
| 437 | { |
| 438 | return type == PTR_TO_SOCKET || |
| 439 | type == PTR_TO_TCP_SOCK || |
| 440 | type == PTR_TO_MAP_VALUE || |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 441 | type == PTR_TO_MAP_KEY || |
Yonghong Song | 01c66c4 | 2020-06-30 10:12:40 -0700 | [diff] [blame] | 442 | type == PTR_TO_SOCK_COMMON; |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 445 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
| 446 | { |
| 447 | return reg->type == PTR_TO_MAP_VALUE && |
| 448 | map_value_has_spin_lock(reg->map_ptr); |
| 449 | } |
| 450 | |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 451 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
| 452 | { |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 453 | return base_type(type) == PTR_TO_SOCKET || |
| 454 | base_type(type) == PTR_TO_TCP_SOCK || |
| 455 | base_type(type) == PTR_TO_MEM; |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Hao Luo | 20b2aff | 2021-12-16 16:31:48 -0800 | [diff] [blame] | 458 | static bool type_is_rdonly_mem(u32 type) |
| 459 | { |
| 460 | return type & MEM_RDONLY; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 463 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 464 | { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 465 | return type == ARG_PTR_TO_SOCK_COMMON; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Hao Luo | 48946bd | 2021-12-16 16:31:45 -0800 | [diff] [blame] | 468 | static bool type_may_be_null(u32 type) |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 469 | { |
Hao Luo | 48946bd | 2021-12-16 16:31:45 -0800 | [diff] [blame] | 470 | return type & PTR_MAYBE_NULL; |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 471 | } |
| 472 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 473 | /* Determine whether the function releases some resources allocated by another |
| 474 | * function call. The first reference type argument will be assumed to be |
| 475 | * released by release_reference(). |
| 476 | */ |
| 477 | static bool is_release_function(enum bpf_func_id func_id) |
| 478 | { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 479 | return func_id == BPF_FUNC_sk_release || |
| 480 | func_id == BPF_FUNC_ringbuf_submit || |
| 481 | func_id == BPF_FUNC_ringbuf_discard; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 484 | static bool may_be_acquire_function(enum bpf_func_id func_id) |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 485 | { |
| 486 | return func_id == BPF_FUNC_sk_lookup_tcp || |
Lorenz Bauer | edbf8c0 | 2019-03-22 09:54:01 +0800 | [diff] [blame] | 487 | func_id == BPF_FUNC_sk_lookup_udp || |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 488 | func_id == BPF_FUNC_skc_lookup_tcp || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 489 | func_id == BPF_FUNC_map_lookup_elem || |
| 490 | func_id == BPF_FUNC_ringbuf_reserve; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static bool is_acquire_function(enum bpf_func_id func_id, |
| 494 | const struct bpf_map *map) |
| 495 | { |
| 496 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; |
| 497 | |
| 498 | if (func_id == BPF_FUNC_sk_lookup_tcp || |
| 499 | func_id == BPF_FUNC_sk_lookup_udp || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 500 | func_id == BPF_FUNC_skc_lookup_tcp || |
| 501 | func_id == BPF_FUNC_ringbuf_reserve) |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 502 | return true; |
| 503 | |
| 504 | if (func_id == BPF_FUNC_map_lookup_elem && |
| 505 | (map_type == BPF_MAP_TYPE_SOCKMAP || |
| 506 | map_type == BPF_MAP_TYPE_SOCKHASH)) |
| 507 | return true; |
| 508 | |
| 509 | return false; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 512 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
| 513 | { |
| 514 | return func_id == BPF_FUNC_tcp_sock || |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 515 | func_id == BPF_FUNC_sk_fullsock || |
| 516 | func_id == BPF_FUNC_skc_to_tcp_sock || |
| 517 | func_id == BPF_FUNC_skc_to_tcp6_sock || |
| 518 | func_id == BPF_FUNC_skc_to_udp6_sock || |
| 519 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || |
| 520 | func_id == BPF_FUNC_skc_to_tcp_request_sock; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Brendan Jackman | 3949186 | 2021-03-04 18:56:46 -0800 | [diff] [blame] | 523 | static bool is_cmpxchg_insn(const struct bpf_insn *insn) |
| 524 | { |
| 525 | return BPF_CLASS(insn->code) == BPF_STX && |
| 526 | BPF_MODE(insn->code) == BPF_ATOMIC && |
| 527 | insn->imm == BPF_CMPXCHG; |
| 528 | } |
| 529 | |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 530 | /* string representation of 'enum bpf_reg_type' |
| 531 | * |
| 532 | * Note that reg_type_str() can not appear more than once in a single verbose() |
| 533 | * statement. |
| 534 | */ |
| 535 | static const char *reg_type_str(struct bpf_verifier_env *env, |
| 536 | enum bpf_reg_type type) |
| 537 | { |
Hao Luo | 20b2aff | 2021-12-16 16:31:48 -0800 | [diff] [blame] | 538 | char postfix[16] = {0}, prefix[16] = {0}; |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 539 | static const char * const str[] = { |
| 540 | [NOT_INIT] = "?", |
| 541 | [SCALAR_VALUE] = "inv", |
| 542 | [PTR_TO_CTX] = "ctx", |
| 543 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 544 | [PTR_TO_MAP_VALUE] = "map_value", |
| 545 | [PTR_TO_STACK] = "fp", |
| 546 | [PTR_TO_PACKET] = "pkt", |
| 547 | [PTR_TO_PACKET_META] = "pkt_meta", |
| 548 | [PTR_TO_PACKET_END] = "pkt_end", |
| 549 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
| 550 | [PTR_TO_SOCKET] = "sock", |
| 551 | [PTR_TO_SOCK_COMMON] = "sock_common", |
| 552 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
| 553 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
| 554 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
| 555 | [PTR_TO_BTF_ID] = "ptr_", |
| 556 | [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", |
| 557 | [PTR_TO_MEM] = "mem", |
Hao Luo | 20b2aff | 2021-12-16 16:31:48 -0800 | [diff] [blame] | 558 | [PTR_TO_BUF] = "buf", |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 559 | [PTR_TO_FUNC] = "func", |
| 560 | [PTR_TO_MAP_KEY] = "map_key", |
| 561 | }; |
| 562 | |
| 563 | if (type & PTR_MAYBE_NULL) { |
| 564 | if (base_type(type) == PTR_TO_BTF_ID || |
| 565 | base_type(type) == PTR_TO_PERCPU_BTF_ID) |
| 566 | strncpy(postfix, "or_null_", 16); |
| 567 | else |
| 568 | strncpy(postfix, "_or_null", 16); |
| 569 | } |
| 570 | |
Hao Luo | 20b2aff | 2021-12-16 16:31:48 -0800 | [diff] [blame] | 571 | if (type & MEM_RDONLY) |
| 572 | strncpy(prefix, "rdonly_", 16); |
| 573 | |
| 574 | snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s", |
| 575 | prefix, str[base_type(type)], postfix); |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 576 | return env->type_str_buf; |
| 577 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 578 | |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 579 | static char slot_type_char[] = { |
| 580 | [STACK_INVALID] = '?', |
| 581 | [STACK_SPILL] = 'r', |
| 582 | [STACK_MISC] = 'm', |
| 583 | [STACK_ZERO] = '0', |
| 584 | }; |
| 585 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 586 | static void print_liveness(struct bpf_verifier_env *env, |
| 587 | enum bpf_reg_liveness live) |
| 588 | { |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 589 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 590 | verbose(env, "_"); |
| 591 | if (live & REG_LIVE_READ) |
| 592 | verbose(env, "r"); |
| 593 | if (live & REG_LIVE_WRITTEN) |
| 594 | verbose(env, "w"); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 595 | if (live & REG_LIVE_DONE) |
| 596 | verbose(env, "D"); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 599 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 600 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 601 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 602 | struct bpf_verifier_state *cur = env->cur_state; |
| 603 | |
| 604 | return cur->frame[reg->frameno]; |
| 605 | } |
| 606 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 607 | static const char *kernel_type_name(const struct btf* btf, u32 id) |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 608 | { |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 609 | return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 612 | static void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno) |
| 613 | { |
| 614 | env->scratched_regs |= 1U << regno; |
| 615 | } |
| 616 | |
| 617 | static void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi) |
| 618 | { |
| 619 | env->scratched_stack_slots |= 1UL << spi; |
| 620 | } |
| 621 | |
| 622 | static bool reg_scratched(const struct bpf_verifier_env *env, u32 regno) |
| 623 | { |
| 624 | return (env->scratched_regs >> regno) & 1; |
| 625 | } |
| 626 | |
| 627 | static bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno) |
| 628 | { |
| 629 | return (env->scratched_stack_slots >> regno) & 1; |
| 630 | } |
| 631 | |
| 632 | static bool verifier_state_scratched(const struct bpf_verifier_env *env) |
| 633 | { |
| 634 | return env->scratched_regs || env->scratched_stack_slots; |
| 635 | } |
| 636 | |
| 637 | static void mark_verifier_state_clean(struct bpf_verifier_env *env) |
| 638 | { |
| 639 | env->scratched_regs = 0U; |
| 640 | env->scratched_stack_slots = 0UL; |
| 641 | } |
| 642 | |
| 643 | /* Used for printing the entire verifier state. */ |
| 644 | static void mark_verifier_state_scratched(struct bpf_verifier_env *env) |
| 645 | { |
| 646 | env->scratched_regs = ~0U; |
| 647 | env->scratched_stack_slots = ~0UL; |
| 648 | } |
| 649 | |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 650 | /* The reg state of a pointer or a bounded scalar was saved when |
| 651 | * it was spilled to the stack. |
| 652 | */ |
| 653 | static bool is_spilled_reg(const struct bpf_stack_state *stack) |
| 654 | { |
| 655 | return stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL; |
| 656 | } |
| 657 | |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 658 | static void scrub_spilled_slot(u8 *stype) |
| 659 | { |
| 660 | if (*stype != STACK_INVALID) |
| 661 | *stype = STACK_MISC; |
| 662 | } |
| 663 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 664 | static void print_verifier_state(struct bpf_verifier_env *env, |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 665 | const struct bpf_func_state *state, |
| 666 | bool print_all) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 667 | { |
| 668 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 669 | enum bpf_reg_type t; |
| 670 | int i; |
| 671 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 672 | if (state->frameno) |
| 673 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 674 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 675 | reg = &state->regs[i]; |
| 676 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 677 | if (t == NOT_INIT) |
| 678 | continue; |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 679 | if (!print_all && !reg_scratched(env, i)) |
| 680 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 681 | verbose(env, " R%d", i); |
| 682 | print_liveness(env, reg->live); |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 683 | verbose(env, "=%s", reg_type_str(env, t)); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 684 | if (t == SCALAR_VALUE && reg->precise) |
| 685 | verbose(env, "P"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 686 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 687 | tnum_is_const(reg->var_off)) { |
| 688 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 689 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 690 | } else { |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 691 | if (base_type(t) == PTR_TO_BTF_ID || |
| 692 | base_type(t) == PTR_TO_PERCPU_BTF_ID) |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 693 | verbose(env, "%s", kernel_type_name(reg->btf, reg->btf_id)); |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 694 | verbose(env, "(id=%d", reg->id); |
| 695 | if (reg_type_may_be_refcounted_or_null(t)) |
| 696 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 697 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 698 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 699 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 700 | verbose(env, ",r=%d", reg->range); |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 701 | else if (base_type(t) == CONST_PTR_TO_MAP || |
| 702 | base_type(t) == PTR_TO_MAP_KEY || |
| 703 | base_type(t) == PTR_TO_MAP_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 704 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 705 | reg->map_ptr->key_size, |
| 706 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 707 | if (tnum_is_const(reg->var_off)) { |
| 708 | /* Typically an immediate SCALAR_VALUE, but |
| 709 | * could be a pointer whose offset is too big |
| 710 | * for reg->off |
| 711 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 712 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 713 | } else { |
| 714 | if (reg->smin_value != reg->umin_value && |
| 715 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 716 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 717 | (long long)reg->smin_value); |
| 718 | if (reg->smax_value != reg->umax_value && |
| 719 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 720 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 721 | (long long)reg->smax_value); |
| 722 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 723 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 724 | (unsigned long long)reg->umin_value); |
| 725 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 726 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 727 | (unsigned long long)reg->umax_value); |
| 728 | if (!tnum_is_unknown(reg->var_off)) { |
| 729 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 730 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 731 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 732 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 733 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 734 | if (reg->s32_min_value != reg->smin_value && |
| 735 | reg->s32_min_value != S32_MIN) |
| 736 | verbose(env, ",s32_min_value=%d", |
| 737 | (int)(reg->s32_min_value)); |
| 738 | if (reg->s32_max_value != reg->smax_value && |
| 739 | reg->s32_max_value != S32_MAX) |
| 740 | verbose(env, ",s32_max_value=%d", |
| 741 | (int)(reg->s32_max_value)); |
| 742 | if (reg->u32_min_value != reg->umin_value && |
| 743 | reg->u32_min_value != U32_MIN) |
| 744 | verbose(env, ",u32_min_value=%d", |
| 745 | (int)(reg->u32_min_value)); |
| 746 | if (reg->u32_max_value != reg->umax_value && |
| 747 | reg->u32_max_value != U32_MAX) |
| 748 | verbose(env, ",u32_max_value=%d", |
| 749 | (int)(reg->u32_max_value)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 750 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 751 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 752 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 753 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 754 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 755 | char types_buf[BPF_REG_SIZE + 1]; |
| 756 | bool valid = false; |
| 757 | int j; |
| 758 | |
| 759 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 760 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 761 | valid = true; |
| 762 | types_buf[j] = slot_type_char[ |
| 763 | state->stack[i].slot_type[j]]; |
| 764 | } |
| 765 | types_buf[BPF_REG_SIZE] = 0; |
| 766 | if (!valid) |
| 767 | continue; |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 768 | if (!print_all && !stack_slot_scratched(env, i)) |
| 769 | continue; |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 770 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 771 | print_liveness(env, state->stack[i].spilled_ptr.live); |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 772 | if (is_spilled_reg(&state->stack[i])) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 773 | reg = &state->stack[i].spilled_ptr; |
| 774 | t = reg->type; |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 775 | verbose(env, "=%s", reg_type_str(env, t)); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 776 | if (t == SCALAR_VALUE && reg->precise) |
| 777 | verbose(env, "P"); |
| 778 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) |
| 779 | verbose(env, "%lld", reg->var_off.value + reg->off); |
| 780 | } else { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 781 | verbose(env, "=%s", types_buf); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 782 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 783 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 784 | if (state->acquired_refs && state->refs[0].id) { |
| 785 | verbose(env, " refs=%d", state->refs[0].id); |
| 786 | for (i = 1; i < state->acquired_refs; i++) |
| 787 | if (state->refs[i].id) |
| 788 | verbose(env, ",%d", state->refs[i].id); |
| 789 | } |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 790 | if (state->in_callback_fn) |
| 791 | verbose(env, " cb"); |
| 792 | if (state->in_async_callback_fn) |
| 793 | verbose(env, " async_cb"); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 794 | verbose(env, "\n"); |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 795 | mark_verifier_state_clean(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame] | 798 | static inline u32 vlog_alignment(u32 pos) |
| 799 | { |
| 800 | return round_up(max(pos + BPF_LOG_MIN_ALIGNMENT / 2, BPF_LOG_ALIGNMENT), |
| 801 | BPF_LOG_MIN_ALIGNMENT) - pos - 1; |
| 802 | } |
| 803 | |
| 804 | static void print_insn_state(struct bpf_verifier_env *env, |
| 805 | const struct bpf_func_state *state) |
| 806 | { |
| 807 | if (env->prev_log_len && env->prev_log_len == env->log.len_used) { |
| 808 | /* remove new line character */ |
| 809 | bpf_vlog_reset(&env->log, env->prev_log_len - 1); |
| 810 | verbose(env, "%*c;", vlog_alignment(env->prev_insn_print_len), ' '); |
| 811 | } else { |
| 812 | verbose(env, "%d:", env->insn_idx); |
| 813 | } |
| 814 | print_verifier_state(env, state, false); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 817 | /* copy array src of length n * size bytes to dst. dst is reallocated if it's too |
| 818 | * small to hold src. This is different from krealloc since we don't want to preserve |
| 819 | * the contents of dst. |
| 820 | * |
| 821 | * Leaves dst untouched if src is NULL or length is zero. Returns NULL if memory could |
| 822 | * not be allocated. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 823 | */ |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 824 | static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t flags) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 825 | { |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 826 | size_t bytes; |
| 827 | |
| 828 | if (ZERO_OR_NULL_PTR(src)) |
| 829 | goto out; |
| 830 | |
| 831 | if (unlikely(check_mul_overflow(n, size, &bytes))) |
| 832 | return NULL; |
| 833 | |
| 834 | if (ksize(dst) < bytes) { |
| 835 | kfree(dst); |
| 836 | dst = kmalloc_track_caller(bytes, flags); |
| 837 | if (!dst) |
| 838 | return NULL; |
| 839 | } |
| 840 | |
| 841 | memcpy(dst, src, bytes); |
| 842 | out: |
| 843 | return dst ? dst : ZERO_SIZE_PTR; |
| 844 | } |
| 845 | |
| 846 | /* resize an array from old_n items to new_n items. the array is reallocated if it's too |
| 847 | * small to hold new_n items. new items are zeroed out if the array grows. |
| 848 | * |
| 849 | * Contrary to krealloc_array, does not free arr if new_n is zero. |
| 850 | */ |
| 851 | static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size) |
| 852 | { |
| 853 | if (!new_n || old_n == new_n) |
| 854 | goto out; |
| 855 | |
| 856 | arr = krealloc_array(arr, new_n, size, GFP_KERNEL); |
| 857 | if (!arr) |
| 858 | return NULL; |
| 859 | |
| 860 | if (new_n > old_n) |
| 861 | memset(arr + old_n * size, 0, (new_n - old_n) * size); |
| 862 | |
| 863 | out: |
| 864 | return arr ? arr : ZERO_SIZE_PTR; |
| 865 | } |
| 866 | |
| 867 | static int copy_reference_state(struct bpf_func_state *dst, const struct bpf_func_state *src) |
| 868 | { |
| 869 | dst->refs = copy_array(dst->refs, src->refs, src->acquired_refs, |
| 870 | sizeof(struct bpf_reference_state), GFP_KERNEL); |
| 871 | if (!dst->refs) |
| 872 | return -ENOMEM; |
| 873 | |
| 874 | dst->acquired_refs = src->acquired_refs; |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src) |
| 879 | { |
| 880 | size_t n = src->allocated_stack / BPF_REG_SIZE; |
| 881 | |
| 882 | dst->stack = copy_array(dst->stack, src->stack, n, sizeof(struct bpf_stack_state), |
| 883 | GFP_KERNEL); |
| 884 | if (!dst->stack) |
| 885 | return -ENOMEM; |
| 886 | |
| 887 | dst->allocated_stack = src->allocated_stack; |
| 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | static int resize_reference_state(struct bpf_func_state *state, size_t n) |
| 892 | { |
| 893 | state->refs = realloc_array(state->refs, state->acquired_refs, n, |
| 894 | sizeof(struct bpf_reference_state)); |
| 895 | if (!state->refs) |
| 896 | return -ENOMEM; |
| 897 | |
| 898 | state->acquired_refs = n; |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | static int grow_stack_state(struct bpf_func_state *state, int size) |
| 903 | { |
| 904 | size_t old_n = state->allocated_stack / BPF_REG_SIZE, n = size / BPF_REG_SIZE; |
| 905 | |
| 906 | if (old_n >= n) |
| 907 | return 0; |
| 908 | |
| 909 | state->stack = realloc_array(state->stack, old_n, n, sizeof(struct bpf_stack_state)); |
| 910 | if (!state->stack) |
| 911 | return -ENOMEM; |
| 912 | |
| 913 | state->allocated_stack = size; |
| 914 | return 0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 915 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 916 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 917 | /* Acquire a pointer id from the env and update the state->refs to include |
| 918 | * this new pointer reference. |
| 919 | * On success, returns a valid pointer id to associate with the register |
| 920 | * On failure, returns a negative errno. |
| 921 | */ |
| 922 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 923 | { |
| 924 | struct bpf_func_state *state = cur_func(env); |
| 925 | int new_ofs = state->acquired_refs; |
| 926 | int id, err; |
| 927 | |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 928 | err = resize_reference_state(state, state->acquired_refs + 1); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 929 | if (err) |
| 930 | return err; |
| 931 | id = ++env->id_gen; |
| 932 | state->refs[new_ofs].id = id; |
| 933 | state->refs[new_ofs].insn_idx = insn_idx; |
| 934 | |
| 935 | return id; |
| 936 | } |
| 937 | |
| 938 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 939 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 940 | { |
| 941 | int i, last_idx; |
| 942 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 943 | last_idx = state->acquired_refs - 1; |
| 944 | for (i = 0; i < state->acquired_refs; i++) { |
| 945 | if (state->refs[i].id == ptr_id) { |
| 946 | if (last_idx && i != last_idx) |
| 947 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 948 | sizeof(*state->refs)); |
| 949 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 950 | state->acquired_refs--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 951 | return 0; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 952 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 953 | } |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 954 | return -EINVAL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 957 | static void free_func_state(struct bpf_func_state *state) |
| 958 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 959 | if (!state) |
| 960 | return; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 961 | kfree(state->refs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 962 | kfree(state->stack); |
| 963 | kfree(state); |
| 964 | } |
| 965 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 966 | static void clear_jmp_history(struct bpf_verifier_state *state) |
| 967 | { |
| 968 | kfree(state->jmp_history); |
| 969 | state->jmp_history = NULL; |
| 970 | state->jmp_history_cnt = 0; |
| 971 | } |
| 972 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 973 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 974 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 975 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 976 | int i; |
| 977 | |
| 978 | for (i = 0; i <= state->curframe; i++) { |
| 979 | free_func_state(state->frame[i]); |
| 980 | state->frame[i] = NULL; |
| 981 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 982 | clear_jmp_history(state); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 983 | if (free_self) |
| 984 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | /* copy verifier state from src to dst growing dst stack space |
| 988 | * when necessary to accommodate larger src stack |
| 989 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 990 | static int copy_func_state(struct bpf_func_state *dst, |
| 991 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 992 | { |
| 993 | int err; |
| 994 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 995 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 996 | err = copy_reference_state(dst, src); |
| 997 | if (err) |
| 998 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 999 | return copy_stack_state(dst, src); |
| 1000 | } |
| 1001 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1002 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 1003 | const struct bpf_verifier_state *src) |
| 1004 | { |
| 1005 | struct bpf_func_state *dst; |
| 1006 | int i, err; |
| 1007 | |
Lorenz Bauer | 06ab6a5 | 2021-04-29 14:46:55 +0100 | [diff] [blame] | 1008 | dst_state->jmp_history = copy_array(dst_state->jmp_history, src->jmp_history, |
| 1009 | src->jmp_history_cnt, sizeof(struct bpf_idx_pair), |
| 1010 | GFP_USER); |
| 1011 | if (!dst_state->jmp_history) |
| 1012 | return -ENOMEM; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1013 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
| 1014 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1015 | /* if dst has more stack frames then src frame, free them */ |
| 1016 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 1017 | free_func_state(dst_state->frame[i]); |
| 1018 | dst_state->frame[i] = NULL; |
| 1019 | } |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 1020 | dst_state->speculative = src->speculative; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1021 | dst_state->curframe = src->curframe; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 1022 | dst_state->active_spin_lock = src->active_spin_lock; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 1023 | dst_state->branches = src->branches; |
| 1024 | dst_state->parent = src->parent; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1025 | dst_state->first_insn_idx = src->first_insn_idx; |
| 1026 | dst_state->last_insn_idx = src->last_insn_idx; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1027 | for (i = 0; i <= src->curframe; i++) { |
| 1028 | dst = dst_state->frame[i]; |
| 1029 | if (!dst) { |
| 1030 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 1031 | if (!dst) |
| 1032 | return -ENOMEM; |
| 1033 | dst_state->frame[i] = dst; |
| 1034 | } |
| 1035 | err = copy_func_state(dst, src->frame[i]); |
| 1036 | if (err) |
| 1037 | return err; |
| 1038 | } |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 1042 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
| 1043 | { |
| 1044 | while (st) { |
| 1045 | u32 br = --st->branches; |
| 1046 | |
| 1047 | /* WARN_ON(br > 1) technically makes sense here, |
| 1048 | * but see comment in push_stack(), hence: |
| 1049 | */ |
| 1050 | WARN_ONCE((int)br < 0, |
| 1051 | "BUG update_branch_counts:branches_to_explore=%d\n", |
| 1052 | br); |
| 1053 | if (br) |
| 1054 | break; |
| 1055 | st = st->parent; |
| 1056 | } |
| 1057 | } |
| 1058 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1059 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 1060 | int *insn_idx, bool pop_log) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1061 | { |
| 1062 | struct bpf_verifier_state *cur = env->cur_state; |
| 1063 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 1064 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1065 | |
| 1066 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1067 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1068 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1069 | if (cur) { |
| 1070 | err = copy_verifier_state(cur, &head->st); |
| 1071 | if (err) |
| 1072 | return err; |
| 1073 | } |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 1074 | if (pop_log) |
| 1075 | bpf_vlog_reset(&env->log, head->log_pos); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1076 | if (insn_idx) |
| 1077 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1078 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1079 | *prev_insn_idx = head->prev_insn_idx; |
| 1080 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 1081 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1082 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1083 | env->head = elem; |
| 1084 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1085 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1088 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 1089 | int insn_idx, int prev_insn_idx, |
| 1090 | bool speculative) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1091 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1092 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1093 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1094 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1095 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1096 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1097 | if (!elem) |
| 1098 | goto err; |
| 1099 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1100 | elem->insn_idx = insn_idx; |
| 1101 | elem->prev_insn_idx = prev_insn_idx; |
| 1102 | elem->next = env->head; |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 1103 | elem->log_pos = env->log.len_used; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1104 | env->head = elem; |
| 1105 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 1106 | err = copy_verifier_state(&elem->st, cur); |
| 1107 | if (err) |
| 1108 | goto err; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 1109 | elem->st.speculative |= speculative; |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 1110 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
| 1111 | verbose(env, "The sequence of %d jumps is too complex.\n", |
| 1112 | env->stack_size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1113 | goto err; |
| 1114 | } |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 1115 | if (elem->st.parent) { |
| 1116 | ++elem->st.parent->branches; |
| 1117 | /* WARN_ON(branches > 2) technically makes sense here, |
| 1118 | * but |
| 1119 | * 1. speculative states will bump 'branches' for non-branch |
| 1120 | * instructions |
| 1121 | * 2. is_state_visited() heuristics may decide not to create |
| 1122 | * a new state for a sequence of branches and all such current |
| 1123 | * and cloned states will be pointing to a single parent state |
| 1124 | * which might have large 'branches' count. |
| 1125 | */ |
| 1126 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1127 | return &elem->st; |
| 1128 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 1129 | free_verifier_state(env->cur_state, true); |
| 1130 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1131 | /* pop all elements and return */ |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 1132 | while (!pop_stack(env, NULL, NULL, false)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1133 | return NULL; |
| 1134 | } |
| 1135 | |
| 1136 | #define CALLER_SAVED_REGS 6 |
| 1137 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 1138 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 1139 | }; |
| 1140 | |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1141 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 1142 | struct bpf_reg_state *reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1143 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 1144 | /* This helper doesn't clear reg->id */ |
| 1145 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1146 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1147 | reg->var_off = tnum_const(imm); |
| 1148 | reg->smin_value = (s64)imm; |
| 1149 | reg->smax_value = (s64)imm; |
| 1150 | reg->umin_value = imm; |
| 1151 | reg->umax_value = imm; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1152 | |
| 1153 | reg->s32_min_value = (s32)imm; |
| 1154 | reg->s32_max_value = (s32)imm; |
| 1155 | reg->u32_min_value = (u32)imm; |
| 1156 | reg->u32_max_value = (u32)imm; |
| 1157 | } |
| 1158 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 1159 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 1160 | * known to have the value @imm. |
| 1161 | */ |
| 1162 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 1163 | { |
| 1164 | /* Clear id, off, and union(map_ptr, range) */ |
| 1165 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 1166 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
| 1167 | ___mark_reg_known(reg, imm); |
| 1168 | } |
| 1169 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1170 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
| 1171 | { |
| 1172 | reg->var_off = tnum_const_subreg(reg->var_off, imm); |
| 1173 | reg->s32_min_value = (s32)imm; |
| 1174 | reg->s32_max_value = (s32)imm; |
| 1175 | reg->u32_min_value = (u32)imm; |
| 1176 | reg->u32_max_value = (u32)imm; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1177 | } |
| 1178 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1179 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 1180 | * used only on registers holding a pointer type. |
| 1181 | */ |
| 1182 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 1183 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1184 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1185 | } |
| 1186 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1187 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 1188 | { |
| 1189 | __mark_reg_known(reg, 0); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1190 | reg->type = SCALAR_VALUE; |
| 1191 | } |
| 1192 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1193 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 1194 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1195 | { |
| 1196 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1197 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1198 | /* Something bad happened, let's kill all regs */ |
| 1199 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1200 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1201 | return; |
| 1202 | } |
| 1203 | __mark_reg_known_zero(regs + regno); |
| 1204 | } |
| 1205 | |
Dmitrii Banshchikov | 4ddb741 | 2021-02-13 00:56:40 +0400 | [diff] [blame] | 1206 | static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) |
| 1207 | { |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 1208 | if (base_type(reg->type) == PTR_TO_MAP_VALUE) { |
Dmitrii Banshchikov | 4ddb741 | 2021-02-13 00:56:40 +0400 | [diff] [blame] | 1209 | const struct bpf_map *map = reg->map_ptr; |
| 1210 | |
| 1211 | if (map->inner_map_meta) { |
| 1212 | reg->type = CONST_PTR_TO_MAP; |
| 1213 | reg->map_ptr = map->inner_map_meta; |
Alexei Starovoitov | 3e8ce29 | 2021-07-14 17:54:11 -0700 | [diff] [blame] | 1214 | /* transfer reg's id which is unique for every map_lookup_elem |
| 1215 | * as UID of the inner map. |
| 1216 | */ |
Alexei Starovoitov | 34d11a4 | 2021-11-10 09:25:56 -0800 | [diff] [blame] | 1217 | if (map_value_has_timer(map->inner_map_meta)) |
| 1218 | reg->map_uid = reg->id; |
Dmitrii Banshchikov | 4ddb741 | 2021-02-13 00:56:40 +0400 | [diff] [blame] | 1219 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
| 1220 | reg->type = PTR_TO_XDP_SOCK; |
| 1221 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || |
| 1222 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { |
| 1223 | reg->type = PTR_TO_SOCKET; |
| 1224 | } else { |
| 1225 | reg->type = PTR_TO_MAP_VALUE; |
| 1226 | } |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 1227 | return; |
Dmitrii Banshchikov | 4ddb741 | 2021-02-13 00:56:40 +0400 | [diff] [blame] | 1228 | } |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 1229 | |
| 1230 | reg->type &= ~PTR_MAYBE_NULL; |
Dmitrii Banshchikov | 4ddb741 | 2021-02-13 00:56:40 +0400 | [diff] [blame] | 1231 | } |
| 1232 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1233 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 1234 | { |
| 1235 | return type_is_pkt_pointer(reg->type); |
| 1236 | } |
| 1237 | |
| 1238 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 1239 | { |
| 1240 | return reg_is_pkt_pointer(reg) || |
| 1241 | reg->type == PTR_TO_PACKET_END; |
| 1242 | } |
| 1243 | |
| 1244 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 1245 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 1246 | enum bpf_reg_type which) |
| 1247 | { |
| 1248 | /* The register can already have a range from prior markings. |
| 1249 | * This is fine as long as it hasn't been advanced from its |
| 1250 | * origin. |
| 1251 | */ |
| 1252 | return reg->type == which && |
| 1253 | reg->id == 0 && |
| 1254 | reg->off == 0 && |
| 1255 | tnum_equals_const(reg->var_off, 0); |
| 1256 | } |
| 1257 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1258 | /* Reset the min/max bounds of a register */ |
| 1259 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 1260 | { |
| 1261 | reg->smin_value = S64_MIN; |
| 1262 | reg->smax_value = S64_MAX; |
| 1263 | reg->umin_value = 0; |
| 1264 | reg->umax_value = U64_MAX; |
| 1265 | |
| 1266 | reg->s32_min_value = S32_MIN; |
| 1267 | reg->s32_max_value = S32_MAX; |
| 1268 | reg->u32_min_value = 0; |
| 1269 | reg->u32_max_value = U32_MAX; |
| 1270 | } |
| 1271 | |
| 1272 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) |
| 1273 | { |
| 1274 | reg->smin_value = S64_MIN; |
| 1275 | reg->smax_value = S64_MAX; |
| 1276 | reg->umin_value = 0; |
| 1277 | reg->umax_value = U64_MAX; |
| 1278 | } |
| 1279 | |
| 1280 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) |
| 1281 | { |
| 1282 | reg->s32_min_value = S32_MIN; |
| 1283 | reg->s32_max_value = S32_MAX; |
| 1284 | reg->u32_min_value = 0; |
| 1285 | reg->u32_max_value = U32_MAX; |
| 1286 | } |
| 1287 | |
| 1288 | static void __update_reg32_bounds(struct bpf_reg_state *reg) |
| 1289 | { |
| 1290 | struct tnum var32_off = tnum_subreg(reg->var_off); |
| 1291 | |
| 1292 | /* min signed is max(sign bit) | min(other bits) */ |
| 1293 | reg->s32_min_value = max_t(s32, reg->s32_min_value, |
| 1294 | var32_off.value | (var32_off.mask & S32_MIN)); |
| 1295 | /* max signed is min(sign bit) | max(other bits) */ |
| 1296 | reg->s32_max_value = min_t(s32, reg->s32_max_value, |
| 1297 | var32_off.value | (var32_off.mask & S32_MAX)); |
| 1298 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); |
| 1299 | reg->u32_max_value = min(reg->u32_max_value, |
| 1300 | (u32)(var32_off.value | var32_off.mask)); |
| 1301 | } |
| 1302 | |
| 1303 | static void __update_reg64_bounds(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1304 | { |
| 1305 | /* min signed is max(sign bit) | min(other bits) */ |
| 1306 | reg->smin_value = max_t(s64, reg->smin_value, |
| 1307 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 1308 | /* max signed is min(sign bit) | max(other bits) */ |
| 1309 | reg->smax_value = min_t(s64, reg->smax_value, |
| 1310 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 1311 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 1312 | reg->umax_value = min(reg->umax_value, |
| 1313 | reg->var_off.value | reg->var_off.mask); |
| 1314 | } |
| 1315 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1316 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 1317 | { |
| 1318 | __update_reg32_bounds(reg); |
| 1319 | __update_reg64_bounds(reg); |
| 1320 | } |
| 1321 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1322 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1323 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
| 1324 | { |
| 1325 | /* Learn sign from signed bounds. |
| 1326 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 1327 | * are the same, so combine. This works even in the negative case, e.g. |
| 1328 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 1329 | */ |
| 1330 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { |
| 1331 | reg->s32_min_value = reg->u32_min_value = |
| 1332 | max_t(u32, reg->s32_min_value, reg->u32_min_value); |
| 1333 | reg->s32_max_value = reg->u32_max_value = |
| 1334 | min_t(u32, reg->s32_max_value, reg->u32_max_value); |
| 1335 | return; |
| 1336 | } |
| 1337 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 1338 | * boundary, so we must be careful. |
| 1339 | */ |
| 1340 | if ((s32)reg->u32_max_value >= 0) { |
| 1341 | /* Positive. We can't learn anything from the smin, but smax |
| 1342 | * is positive, hence safe. |
| 1343 | */ |
| 1344 | reg->s32_min_value = reg->u32_min_value; |
| 1345 | reg->s32_max_value = reg->u32_max_value = |
| 1346 | min_t(u32, reg->s32_max_value, reg->u32_max_value); |
| 1347 | } else if ((s32)reg->u32_min_value < 0) { |
| 1348 | /* Negative. We can't learn anything from the smax, but smin |
| 1349 | * is negative, hence safe. |
| 1350 | */ |
| 1351 | reg->s32_min_value = reg->u32_min_value = |
| 1352 | max_t(u32, reg->s32_min_value, reg->u32_min_value); |
| 1353 | reg->s32_max_value = reg->u32_max_value; |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1358 | { |
| 1359 | /* Learn sign from signed bounds. |
| 1360 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 1361 | * are the same, so combine. This works even in the negative case, e.g. |
| 1362 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 1363 | */ |
| 1364 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 1365 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1366 | reg->umin_value); |
| 1367 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1368 | reg->umax_value); |
| 1369 | return; |
| 1370 | } |
| 1371 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 1372 | * boundary, so we must be careful. |
| 1373 | */ |
| 1374 | if ((s64)reg->umax_value >= 0) { |
| 1375 | /* Positive. We can't learn anything from the smin, but smax |
| 1376 | * is positive, hence safe. |
| 1377 | */ |
| 1378 | reg->smin_value = reg->umin_value; |
| 1379 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1380 | reg->umax_value); |
| 1381 | } else if ((s64)reg->umin_value < 0) { |
| 1382 | /* Negative. We can't learn anything from the smax, but smin |
| 1383 | * is negative, hence safe. |
| 1384 | */ |
| 1385 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1386 | reg->umin_value); |
| 1387 | reg->smax_value = reg->umax_value; |
| 1388 | } |
| 1389 | } |
| 1390 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1391 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 1392 | { |
| 1393 | __reg32_deduce_bounds(reg); |
| 1394 | __reg64_deduce_bounds(reg); |
| 1395 | } |
| 1396 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1397 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 1398 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 1399 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1400 | struct tnum var64_off = tnum_intersect(reg->var_off, |
| 1401 | tnum_range(reg->umin_value, |
| 1402 | reg->umax_value)); |
| 1403 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), |
| 1404 | tnum_range(reg->u32_min_value, |
| 1405 | reg->u32_max_value)); |
| 1406 | |
| 1407 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1408 | } |
| 1409 | |
Daniel Borkmann | e572ff8 | 2021-12-15 22:28:48 +0000 | [diff] [blame] | 1410 | static bool __reg32_bound_s64(s32 a) |
| 1411 | { |
| 1412 | return a >= 0 && a <= S32_MAX; |
| 1413 | } |
| 1414 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1415 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1416 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1417 | reg->umin_value = reg->u32_min_value; |
| 1418 | reg->umax_value = reg->u32_max_value; |
Daniel Borkmann | e572ff8 | 2021-12-15 22:28:48 +0000 | [diff] [blame] | 1419 | |
| 1420 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds but must |
| 1421 | * be positive otherwise set to worse case bounds and refine later |
| 1422 | * from tnum. |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1423 | */ |
Daniel Borkmann | e572ff8 | 2021-12-15 22:28:48 +0000 | [diff] [blame] | 1424 | if (__reg32_bound_s64(reg->s32_min_value) && |
| 1425 | __reg32_bound_s64(reg->s32_max_value)) { |
John Fastabend | 3a71dc3 | 2020-05-29 10:28:40 -0700 | [diff] [blame] | 1426 | reg->smin_value = reg->s32_min_value; |
Daniel Borkmann | e572ff8 | 2021-12-15 22:28:48 +0000 | [diff] [blame] | 1427 | reg->smax_value = reg->s32_max_value; |
| 1428 | } else { |
John Fastabend | 3a71dc3 | 2020-05-29 10:28:40 -0700 | [diff] [blame] | 1429 | reg->smin_value = 0; |
Daniel Borkmann | e572ff8 | 2021-12-15 22:28:48 +0000 | [diff] [blame] | 1430 | reg->smax_value = U32_MAX; |
| 1431 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) |
| 1435 | { |
| 1436 | /* special case when 64-bit register has upper 32-bit register |
| 1437 | * zeroed. Typically happens after zext or <<32, >>32 sequence |
| 1438 | * allowing us to use 32-bit bounds directly, |
| 1439 | */ |
| 1440 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { |
| 1441 | __reg_assign_32_into_64(reg); |
| 1442 | } else { |
| 1443 | /* Otherwise the best we can do is push lower 32bit known and |
| 1444 | * unknown bits into register (var_off set from jmp logic) |
| 1445 | * then learn as much as possible from the 64-bit tnum |
| 1446 | * known and unknown bits. The previous smin/smax bounds are |
| 1447 | * invalid here because of jmp32 compare so mark them unknown |
| 1448 | * so they do not impact tnum bounds calculation. |
| 1449 | */ |
| 1450 | __mark_reg64_unbounded(reg); |
| 1451 | __update_reg_bounds(reg); |
| 1452 | } |
| 1453 | |
| 1454 | /* Intersecting with the old var_off might have improved our bounds |
| 1455 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 1456 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 1457 | */ |
| 1458 | __reg_deduce_bounds(reg); |
| 1459 | __reg_bound_offset(reg); |
| 1460 | __update_reg_bounds(reg); |
| 1461 | } |
| 1462 | |
| 1463 | static bool __reg64_bound_s32(s64 a) |
| 1464 | { |
Alexei Starovoitov | 388e2c0 | 2021-11-01 15:21:52 -0700 | [diff] [blame] | 1465 | return a >= S32_MIN && a <= S32_MAX; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | static bool __reg64_bound_u32(u64 a) |
| 1469 | { |
Alexei Starovoitov | b9979db | 2021-11-01 15:21:51 -0700 | [diff] [blame] | 1470 | return a >= U32_MIN && a <= U32_MAX; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) |
| 1474 | { |
| 1475 | __mark_reg32_unbounded(reg); |
| 1476 | |
Alexei Starovoitov | b0270958 | 2020-12-08 19:01:51 +0100 | [diff] [blame] | 1477 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1478 | reg->s32_min_value = (s32)reg->smin_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1479 | reg->s32_max_value = (s32)reg->smax_value; |
Alexei Starovoitov | b0270958 | 2020-12-08 19:01:51 +0100 | [diff] [blame] | 1480 | } |
Daniel Borkmann | 10bf4e8 | 2021-04-23 13:59:55 +0000 | [diff] [blame] | 1481 | if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value)) { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1482 | reg->u32_min_value = (u32)reg->umin_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1483 | reg->u32_max_value = (u32)reg->umax_value; |
Daniel Borkmann | 10bf4e8 | 2021-04-23 13:59:55 +0000 | [diff] [blame] | 1484 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1485 | |
| 1486 | /* Intersecting with the old var_off might have improved our bounds |
| 1487 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 1488 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 1489 | */ |
| 1490 | __reg_deduce_bounds(reg); |
| 1491 | __reg_bound_offset(reg); |
| 1492 | __update_reg_bounds(reg); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1493 | } |
| 1494 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1495 | /* Mark a register as having a completely unknown (scalar) value. */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1496 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
| 1497 | struct bpf_reg_state *reg) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1498 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 1499 | /* |
| 1500 | * Clear type, id, off, and union(map_ptr, range) and |
| 1501 | * padding between 'type' and union |
| 1502 | */ |
| 1503 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1504 | reg->type = SCALAR_VALUE; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1505 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1506 | reg->frameno = 0; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 1507 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1508 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1509 | } |
| 1510 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1511 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 1512 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1513 | { |
| 1514 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1515 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 1516 | /* Something bad happened, let's kill all regs except FP */ |
| 1517 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1518 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1519 | return; |
| 1520 | } |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1521 | __mark_reg_unknown(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1522 | } |
| 1523 | |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1524 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 1525 | struct bpf_reg_state *reg) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1526 | { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1527 | __mark_reg_unknown(env, reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1528 | reg->type = NOT_INIT; |
| 1529 | } |
| 1530 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1531 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 1532 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 1533 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1534 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1535 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 1536 | /* Something bad happened, let's kill all regs except FP */ |
| 1537 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1538 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1539 | return; |
| 1540 | } |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1541 | __mark_reg_not_init(env, regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 1542 | } |
| 1543 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 1544 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
| 1545 | struct bpf_reg_state *regs, u32 regno, |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 1546 | enum bpf_reg_type reg_type, |
| 1547 | struct btf *btf, u32 btf_id) |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 1548 | { |
| 1549 | if (reg_type == SCALAR_VALUE) { |
| 1550 | mark_reg_unknown(env, regs, regno); |
| 1551 | return; |
| 1552 | } |
| 1553 | mark_reg_known_zero(env, regs, regno); |
| 1554 | regs[regno].type = PTR_TO_BTF_ID; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 1555 | regs[regno].btf = btf; |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 1556 | regs[regno].btf_id = btf_id; |
| 1557 | } |
| 1558 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1559 | #define DEF_NOT_SUBREG (0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1560 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1561 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1562 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1563 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1564 | int i; |
| 1565 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1566 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1567 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1568 | regs[i].live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1569 | regs[i].parent = NULL; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1570 | regs[i].subreg_def = DEF_NOT_SUBREG; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1571 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1572 | |
| 1573 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1574 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1575 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1576 | regs[BPF_REG_FP].frameno = state->frameno; |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 1577 | } |
| 1578 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1579 | #define BPF_MAIN_FUNC (-1) |
| 1580 | static void init_func_state(struct bpf_verifier_env *env, |
| 1581 | struct bpf_func_state *state, |
| 1582 | int callsite, int frameno, int subprogno) |
| 1583 | { |
| 1584 | state->callsite = callsite; |
| 1585 | state->frameno = frameno; |
| 1586 | state->subprogno = subprogno; |
| 1587 | init_reg_state(env, state); |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 1588 | mark_verifier_state_scratched(env); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1589 | } |
| 1590 | |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 1591 | /* Similar to push_stack(), but for async callbacks */ |
| 1592 | static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, |
| 1593 | int insn_idx, int prev_insn_idx, |
| 1594 | int subprog) |
| 1595 | { |
| 1596 | struct bpf_verifier_stack_elem *elem; |
| 1597 | struct bpf_func_state *frame; |
| 1598 | |
| 1599 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
| 1600 | if (!elem) |
| 1601 | goto err; |
| 1602 | |
| 1603 | elem->insn_idx = insn_idx; |
| 1604 | elem->prev_insn_idx = prev_insn_idx; |
| 1605 | elem->next = env->head; |
| 1606 | elem->log_pos = env->log.len_used; |
| 1607 | env->head = elem; |
| 1608 | env->stack_size++; |
| 1609 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
| 1610 | verbose(env, |
| 1611 | "The sequence of %d jumps is too complex for async cb.\n", |
| 1612 | env->stack_size); |
| 1613 | goto err; |
| 1614 | } |
| 1615 | /* Unlike push_stack() do not copy_verifier_state(). |
| 1616 | * The caller state doesn't matter. |
| 1617 | * This is async callback. It starts in a fresh stack. |
| 1618 | * Initialize it similar to do_check_common(). |
| 1619 | */ |
| 1620 | elem->st.branches = 1; |
| 1621 | frame = kzalloc(sizeof(*frame), GFP_KERNEL); |
| 1622 | if (!frame) |
| 1623 | goto err; |
| 1624 | init_func_state(env, frame, |
| 1625 | BPF_MAIN_FUNC /* callsite */, |
| 1626 | 0 /* frameno within this callchain */, |
| 1627 | subprog /* subprog number within this prog */); |
| 1628 | elem->st.frame[0] = frame; |
| 1629 | return &elem->st; |
| 1630 | err: |
| 1631 | free_verifier_state(env->cur_state, true); |
| 1632 | env->cur_state = NULL; |
| 1633 | /* pop all elements and return */ |
| 1634 | while (!pop_stack(env, NULL, NULL, false)); |
| 1635 | return NULL; |
| 1636 | } |
| 1637 | |
| 1638 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1639 | enum reg_arg_type { |
| 1640 | SRC_OP, /* register is used as source operand */ |
| 1641 | DST_OP, /* register is used as destination operand */ |
| 1642 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 1643 | }; |
| 1644 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1645 | static int cmp_subprogs(const void *a, const void *b) |
| 1646 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1647 | return ((struct bpf_subprog_info *)a)->start - |
| 1648 | ((struct bpf_subprog_info *)b)->start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 1652 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1653 | struct bpf_subprog_info *p; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1654 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1655 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 1656 | sizeof(env->subprog_info[0]), cmp_subprogs); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1657 | if (!p) |
| 1658 | return -ENOENT; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1659 | return p - env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1660 | |
| 1661 | } |
| 1662 | |
| 1663 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 1664 | { |
| 1665 | int insn_cnt = env->prog->len; |
| 1666 | int ret; |
| 1667 | |
| 1668 | if (off >= insn_cnt || off < 0) { |
| 1669 | verbose(env, "call to invalid destination\n"); |
| 1670 | return -EINVAL; |
| 1671 | } |
| 1672 | ret = find_subprog(env, off); |
| 1673 | if (ret >= 0) |
Yonghong Song | 282a0f4 | 2021-02-26 12:49:24 -0800 | [diff] [blame] | 1674 | return ret; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1675 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1676 | verbose(env, "too many subprograms\n"); |
| 1677 | return -E2BIG; |
| 1678 | } |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1679 | /* determine subprog starts. The end is one before the next starts */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1680 | env->subprog_info[env->subprog_cnt++].start = off; |
| 1681 | sort(env->subprog_info, env->subprog_cnt, |
| 1682 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
Yonghong Song | 282a0f4 | 2021-02-26 12:49:24 -0800 | [diff] [blame] | 1683 | return env->subprog_cnt - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1684 | } |
| 1685 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1686 | #define MAX_KFUNC_DESCS 256 |
| 1687 | #define MAX_KFUNC_BTFS 256 |
| 1688 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1689 | struct bpf_kfunc_desc { |
| 1690 | struct btf_func_model func_model; |
| 1691 | u32 func_id; |
| 1692 | s32 imm; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1693 | u16 offset; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1694 | }; |
| 1695 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1696 | struct bpf_kfunc_btf { |
| 1697 | struct btf *btf; |
| 1698 | struct module *module; |
| 1699 | u16 offset; |
| 1700 | }; |
| 1701 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1702 | struct bpf_kfunc_desc_tab { |
| 1703 | struct bpf_kfunc_desc descs[MAX_KFUNC_DESCS]; |
| 1704 | u32 nr_descs; |
| 1705 | }; |
| 1706 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1707 | struct bpf_kfunc_btf_tab { |
| 1708 | struct bpf_kfunc_btf descs[MAX_KFUNC_BTFS]; |
| 1709 | u32 nr_descs; |
| 1710 | }; |
| 1711 | |
| 1712 | static int kfunc_desc_cmp_by_id_off(const void *a, const void *b) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1713 | { |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1714 | const struct bpf_kfunc_desc *d0 = a; |
| 1715 | const struct bpf_kfunc_desc *d1 = b; |
| 1716 | |
| 1717 | /* func_id is not greater than BTF_MAX_TYPE */ |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1718 | return d0->func_id - d1->func_id ?: d0->offset - d1->offset; |
| 1719 | } |
| 1720 | |
| 1721 | static int kfunc_btf_cmp_by_off(const void *a, const void *b) |
| 1722 | { |
| 1723 | const struct bpf_kfunc_btf *d0 = a; |
| 1724 | const struct bpf_kfunc_btf *d1 = b; |
| 1725 | |
| 1726 | return d0->offset - d1->offset; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | static const struct bpf_kfunc_desc * |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1730 | find_kfunc_desc(const struct bpf_prog *prog, u32 func_id, u16 offset) |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1731 | { |
| 1732 | struct bpf_kfunc_desc desc = { |
| 1733 | .func_id = func_id, |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1734 | .offset = offset, |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1735 | }; |
| 1736 | struct bpf_kfunc_desc_tab *tab; |
| 1737 | |
| 1738 | tab = prog->aux->kfunc_tab; |
| 1739 | return bsearch(&desc, tab->descs, tab->nr_descs, |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1740 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_id_off); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1741 | } |
| 1742 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1743 | static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env, |
| 1744 | s16 offset, struct module **btf_modp) |
| 1745 | { |
| 1746 | struct bpf_kfunc_btf kf_btf = { .offset = offset }; |
| 1747 | struct bpf_kfunc_btf_tab *tab; |
| 1748 | struct bpf_kfunc_btf *b; |
| 1749 | struct module *mod; |
| 1750 | struct btf *btf; |
| 1751 | int btf_fd; |
| 1752 | |
| 1753 | tab = env->prog->aux->kfunc_btf_tab; |
| 1754 | b = bsearch(&kf_btf, tab->descs, tab->nr_descs, |
| 1755 | sizeof(tab->descs[0]), kfunc_btf_cmp_by_off); |
| 1756 | if (!b) { |
| 1757 | if (tab->nr_descs == MAX_KFUNC_BTFS) { |
| 1758 | verbose(env, "too many different module BTFs\n"); |
| 1759 | return ERR_PTR(-E2BIG); |
| 1760 | } |
| 1761 | |
| 1762 | if (bpfptr_is_null(env->fd_array)) { |
| 1763 | verbose(env, "kfunc offset > 0 without fd_array is invalid\n"); |
| 1764 | return ERR_PTR(-EPROTO); |
| 1765 | } |
| 1766 | |
| 1767 | if (copy_from_bpfptr_offset(&btf_fd, env->fd_array, |
| 1768 | offset * sizeof(btf_fd), |
| 1769 | sizeof(btf_fd))) |
| 1770 | return ERR_PTR(-EFAULT); |
| 1771 | |
| 1772 | btf = btf_get_by_fd(btf_fd); |
Kumar Kartikeya Dwivedi | 588cd7ef | 2021-10-09 09:39:00 +0530 | [diff] [blame] | 1773 | if (IS_ERR(btf)) { |
| 1774 | verbose(env, "invalid module BTF fd specified\n"); |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1775 | return btf; |
Kumar Kartikeya Dwivedi | 588cd7ef | 2021-10-09 09:39:00 +0530 | [diff] [blame] | 1776 | } |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1777 | |
| 1778 | if (!btf_is_module(btf)) { |
| 1779 | verbose(env, "BTF fd for kfunc is not a module BTF\n"); |
| 1780 | btf_put(btf); |
| 1781 | return ERR_PTR(-EINVAL); |
| 1782 | } |
| 1783 | |
| 1784 | mod = btf_try_get_module(btf); |
| 1785 | if (!mod) { |
| 1786 | btf_put(btf); |
| 1787 | return ERR_PTR(-ENXIO); |
| 1788 | } |
| 1789 | |
| 1790 | b = &tab->descs[tab->nr_descs++]; |
| 1791 | b->btf = btf; |
| 1792 | b->module = mod; |
| 1793 | b->offset = offset; |
| 1794 | |
| 1795 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), |
| 1796 | kfunc_btf_cmp_by_off, NULL); |
| 1797 | } |
| 1798 | if (btf_modp) |
| 1799 | *btf_modp = b->module; |
| 1800 | return b->btf; |
| 1801 | } |
| 1802 | |
| 1803 | void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab) |
| 1804 | { |
| 1805 | if (!tab) |
| 1806 | return; |
| 1807 | |
| 1808 | while (tab->nr_descs--) { |
| 1809 | module_put(tab->descs[tab->nr_descs].module); |
| 1810 | btf_put(tab->descs[tab->nr_descs].btf); |
| 1811 | } |
| 1812 | kfree(tab); |
| 1813 | } |
| 1814 | |
| 1815 | static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, |
| 1816 | u32 func_id, s16 offset, |
| 1817 | struct module **btf_modp) |
| 1818 | { |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1819 | if (offset) { |
| 1820 | if (offset < 0) { |
| 1821 | /* In the future, this can be allowed to increase limit |
| 1822 | * of fd index into fd_array, interpreted as u16. |
| 1823 | */ |
| 1824 | verbose(env, "negative offset disallowed for kernel module function call\n"); |
| 1825 | return ERR_PTR(-EINVAL); |
| 1826 | } |
| 1827 | |
Kumar Kartikeya Dwivedi | 588cd7ef | 2021-10-09 09:39:00 +0530 | [diff] [blame] | 1828 | return __find_kfunc_desc_btf(env, offset, btf_modp); |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1829 | } |
| 1830 | return btf_vmlinux ?: ERR_PTR(-ENOENT); |
| 1831 | } |
| 1832 | |
| 1833 | static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1834 | { |
| 1835 | const struct btf_type *func, *func_proto; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1836 | struct bpf_kfunc_btf_tab *btf_tab; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1837 | struct bpf_kfunc_desc_tab *tab; |
| 1838 | struct bpf_prog_aux *prog_aux; |
| 1839 | struct bpf_kfunc_desc *desc; |
| 1840 | const char *func_name; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1841 | struct btf *desc_btf; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1842 | unsigned long addr; |
| 1843 | int err; |
| 1844 | |
| 1845 | prog_aux = env->prog->aux; |
| 1846 | tab = prog_aux->kfunc_tab; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1847 | btf_tab = prog_aux->kfunc_btf_tab; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1848 | if (!tab) { |
| 1849 | if (!btf_vmlinux) { |
| 1850 | verbose(env, "calling kernel function is not supported without CONFIG_DEBUG_INFO_BTF\n"); |
| 1851 | return -ENOTSUPP; |
| 1852 | } |
| 1853 | |
| 1854 | if (!env->prog->jit_requested) { |
| 1855 | verbose(env, "JIT is required for calling kernel function\n"); |
| 1856 | return -ENOTSUPP; |
| 1857 | } |
| 1858 | |
| 1859 | if (!bpf_jit_supports_kfunc_call()) { |
| 1860 | verbose(env, "JIT does not support calling kernel function\n"); |
| 1861 | return -ENOTSUPP; |
| 1862 | } |
| 1863 | |
| 1864 | if (!env->prog->gpl_compatible) { |
| 1865 | verbose(env, "cannot call kernel function from non-GPL compatible program\n"); |
| 1866 | return -EINVAL; |
| 1867 | } |
| 1868 | |
| 1869 | tab = kzalloc(sizeof(*tab), GFP_KERNEL); |
| 1870 | if (!tab) |
| 1871 | return -ENOMEM; |
| 1872 | prog_aux->kfunc_tab = tab; |
| 1873 | } |
| 1874 | |
Kumar Kartikeya Dwivedi | a5d8272 | 2021-10-02 06:47:50 +0530 | [diff] [blame] | 1875 | /* func_id == 0 is always invalid, but instead of returning an error, be |
| 1876 | * conservative and wait until the code elimination pass before returning |
| 1877 | * error, so that invalid calls that get pruned out can be in BPF programs |
| 1878 | * loaded from userspace. It is also required that offset be untouched |
| 1879 | * for such calls. |
| 1880 | */ |
| 1881 | if (!func_id && !offset) |
| 1882 | return 0; |
| 1883 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1884 | if (!btf_tab && offset) { |
| 1885 | btf_tab = kzalloc(sizeof(*btf_tab), GFP_KERNEL); |
| 1886 | if (!btf_tab) |
| 1887 | return -ENOMEM; |
| 1888 | prog_aux->kfunc_btf_tab = btf_tab; |
| 1889 | } |
| 1890 | |
| 1891 | desc_btf = find_kfunc_desc_btf(env, func_id, offset, NULL); |
| 1892 | if (IS_ERR(desc_btf)) { |
| 1893 | verbose(env, "failed to find BTF for kernel function\n"); |
| 1894 | return PTR_ERR(desc_btf); |
| 1895 | } |
| 1896 | |
| 1897 | if (find_kfunc_desc(env->prog, func_id, offset)) |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1898 | return 0; |
| 1899 | |
| 1900 | if (tab->nr_descs == MAX_KFUNC_DESCS) { |
| 1901 | verbose(env, "too many different kernel function calls\n"); |
| 1902 | return -E2BIG; |
| 1903 | } |
| 1904 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1905 | func = btf_type_by_id(desc_btf, func_id); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1906 | if (!func || !btf_type_is_func(func)) { |
| 1907 | verbose(env, "kernel btf_id %u is not a function\n", |
| 1908 | func_id); |
| 1909 | return -EINVAL; |
| 1910 | } |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1911 | func_proto = btf_type_by_id(desc_btf, func->type); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1912 | if (!func_proto || !btf_type_is_func_proto(func_proto)) { |
| 1913 | verbose(env, "kernel function btf_id %u does not have a valid func_proto\n", |
| 1914 | func_id); |
| 1915 | return -EINVAL; |
| 1916 | } |
| 1917 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1918 | func_name = btf_name_by_offset(desc_btf, func->name_off); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1919 | addr = kallsyms_lookup_name(func_name); |
| 1920 | if (!addr) { |
| 1921 | verbose(env, "cannot find address for kernel function %s\n", |
| 1922 | func_name); |
| 1923 | return -EINVAL; |
| 1924 | } |
| 1925 | |
| 1926 | desc = &tab->descs[tab->nr_descs++]; |
| 1927 | desc->func_id = func_id; |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 1928 | desc->imm = BPF_CALL_IMM(addr); |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1929 | desc->offset = offset; |
| 1930 | err = btf_distill_func_proto(&env->log, desc_btf, |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1931 | func_proto, func_name, |
| 1932 | &desc->func_model); |
| 1933 | if (!err) |
| 1934 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1935 | kfunc_desc_cmp_by_id_off, NULL); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1936 | return err; |
| 1937 | } |
| 1938 | |
| 1939 | static int kfunc_desc_cmp_by_imm(const void *a, const void *b) |
| 1940 | { |
| 1941 | const struct bpf_kfunc_desc *d0 = a; |
| 1942 | const struct bpf_kfunc_desc *d1 = b; |
| 1943 | |
| 1944 | if (d0->imm > d1->imm) |
| 1945 | return 1; |
| 1946 | else if (d0->imm < d1->imm) |
| 1947 | return -1; |
| 1948 | return 0; |
| 1949 | } |
| 1950 | |
| 1951 | static void sort_kfunc_descs_by_imm(struct bpf_prog *prog) |
| 1952 | { |
| 1953 | struct bpf_kfunc_desc_tab *tab; |
| 1954 | |
| 1955 | tab = prog->aux->kfunc_tab; |
| 1956 | if (!tab) |
| 1957 | return; |
| 1958 | |
| 1959 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), |
| 1960 | kfunc_desc_cmp_by_imm, NULL); |
| 1961 | } |
| 1962 | |
| 1963 | bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog) |
| 1964 | { |
| 1965 | return !!prog->aux->kfunc_tab; |
| 1966 | } |
| 1967 | |
| 1968 | const struct btf_func_model * |
| 1969 | bpf_jit_find_kfunc_model(const struct bpf_prog *prog, |
| 1970 | const struct bpf_insn *insn) |
| 1971 | { |
| 1972 | const struct bpf_kfunc_desc desc = { |
| 1973 | .imm = insn->imm, |
| 1974 | }; |
| 1975 | const struct bpf_kfunc_desc *res; |
| 1976 | struct bpf_kfunc_desc_tab *tab; |
| 1977 | |
| 1978 | tab = prog->aux->kfunc_tab; |
| 1979 | res = bsearch(&desc, tab->descs, tab->nr_descs, |
| 1980 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_imm); |
| 1981 | |
| 1982 | return res ? &res->func_model : NULL; |
| 1983 | } |
| 1984 | |
| 1985 | static int add_subprog_and_kfunc(struct bpf_verifier_env *env) |
| 1986 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1987 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1988 | struct bpf_insn *insn = env->prog->insnsi; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1989 | int i, ret, insn_cnt = env->prog->len; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1990 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 1991 | /* Add entry function. */ |
| 1992 | ret = add_subprog(env, 0); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1993 | if (ret) |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 1994 | return ret; |
| 1995 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1996 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 1997 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn) && |
| 1998 | !bpf_pseudo_kfunc_call(insn)) |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 1999 | continue; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2000 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2001 | if (!env->bpf_capable) { |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2002 | verbose(env, "loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2003 | return -EPERM; |
| 2004 | } |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2005 | |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 2006 | if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn)) |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2007 | ret = add_subprog(env, i + insn->imm + 1); |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 2008 | else |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 2009 | ret = add_kfunc_call(env, insn->imm, insn->off); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2010 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2011 | if (ret < 0) |
| 2012 | return ret; |
| 2013 | } |
| 2014 | |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 2015 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 2016 | * logic. 'subprog_cnt' should not be increased. |
| 2017 | */ |
| 2018 | subprog[env->subprog_cnt].start = insn_cnt; |
| 2019 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 2020 | if (env->log.level & BPF_LOG_LEVEL2) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2021 | for (i = 0; i < env->subprog_cnt; i++) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2022 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2023 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2024 | return 0; |
| 2025 | } |
| 2026 | |
| 2027 | static int check_subprogs(struct bpf_verifier_env *env) |
| 2028 | { |
| 2029 | int i, subprog_start, subprog_end, off, cur_subprog = 0; |
| 2030 | struct bpf_subprog_info *subprog = env->subprog_info; |
| 2031 | struct bpf_insn *insn = env->prog->insnsi; |
| 2032 | int insn_cnt = env->prog->len; |
| 2033 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2034 | /* now check that all jumps are within the same subprog */ |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 2035 | subprog_start = subprog[cur_subprog].start; |
| 2036 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2037 | for (i = 0; i < insn_cnt; i++) { |
| 2038 | u8 code = insn[i].code; |
| 2039 | |
Maciej Fijalkowski | 7f6e431 | 2020-09-16 23:10:07 +0200 | [diff] [blame] | 2040 | if (code == (BPF_JMP | BPF_CALL) && |
| 2041 | insn[i].imm == BPF_FUNC_tail_call && |
| 2042 | insn[i].src_reg != BPF_PSEUDO_CALL) |
| 2043 | subprog[cur_subprog].has_tail_call = true; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 2044 | if (BPF_CLASS(code) == BPF_LD && |
| 2045 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) |
| 2046 | subprog[cur_subprog].has_ld_abs = true; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 2047 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2048 | goto next; |
| 2049 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 2050 | goto next; |
| 2051 | off = i + insn[i].off + 1; |
| 2052 | if (off < subprog_start || off >= subprog_end) { |
| 2053 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 2054 | return -EINVAL; |
| 2055 | } |
| 2056 | next: |
| 2057 | if (i == subprog_end - 1) { |
| 2058 | /* to avoid fall-through from one subprog into another |
| 2059 | * the last insn of the subprog should be either exit |
| 2060 | * or unconditional jump back |
| 2061 | */ |
| 2062 | if (code != (BPF_JMP | BPF_EXIT) && |
| 2063 | code != (BPF_JMP | BPF_JA)) { |
| 2064 | verbose(env, "last insn is not an exit or jmp\n"); |
| 2065 | return -EINVAL; |
| 2066 | } |
| 2067 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 2068 | cur_subprog++; |
| 2069 | if (cur_subprog < env->subprog_cnt) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2070 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2071 | } |
| 2072 | } |
| 2073 | return 0; |
| 2074 | } |
| 2075 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2076 | /* Parentage chain of this register (or stack slot) should take care of all |
| 2077 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 2078 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2079 | static int mark_reg_read(struct bpf_verifier_env *env, |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2080 | const struct bpf_reg_state *state, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2081 | struct bpf_reg_state *parent, u8 flag) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2082 | { |
| 2083 | bool writes = parent == state->parent; /* Observe write marks */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 2084 | int cnt = 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2085 | |
| 2086 | while (parent) { |
| 2087 | /* if read wasn't screened by an earlier write ... */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2088 | if (writes && state->live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2089 | break; |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 2090 | if (parent->live & REG_LIVE_DONE) { |
| 2091 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 2092 | reg_type_str(env, parent->type), |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 2093 | parent->var_off.value, parent->off); |
| 2094 | return -EFAULT; |
| 2095 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2096 | /* The first condition is more likely to be true than the |
| 2097 | * second, checked it first. |
| 2098 | */ |
| 2099 | if ((parent->live & REG_LIVE_READ) == flag || |
| 2100 | parent->live & REG_LIVE_READ64) |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 2101 | /* The parentage chain never changes and |
| 2102 | * this parent was already marked as LIVE_READ. |
| 2103 | * There is no need to keep walking the chain again and |
| 2104 | * keep re-marking all parents as LIVE_READ. |
| 2105 | * This case happens when the same register is read |
| 2106 | * multiple times without writes into it in-between. |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2107 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
| 2108 | * then no need to set the weak REG_LIVE_READ32. |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 2109 | */ |
| 2110 | break; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2111 | /* ... then we depend on parent's value */ |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2112 | parent->live |= flag; |
| 2113 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ |
| 2114 | if (flag == REG_LIVE_READ64) |
| 2115 | parent->live &= ~REG_LIVE_READ32; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2116 | state = parent; |
| 2117 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2118 | writes = true; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 2119 | cnt++; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2120 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 2121 | |
| 2122 | if (env->longest_mark_read_walk < cnt) |
| 2123 | env->longest_mark_read_walk = cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2124 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2125 | } |
| 2126 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2127 | /* This function is supposed to be used by the following 32-bit optimization |
| 2128 | * code only. It returns TRUE if the source or destination register operates |
| 2129 | * on 64-bit, otherwise return FALSE. |
| 2130 | */ |
| 2131 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 2132 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) |
| 2133 | { |
| 2134 | u8 code, class, op; |
| 2135 | |
| 2136 | code = insn->code; |
| 2137 | class = BPF_CLASS(code); |
| 2138 | op = BPF_OP(code); |
| 2139 | if (class == BPF_JMP) { |
| 2140 | /* BPF_EXIT for "main" will reach here. Return TRUE |
| 2141 | * conservatively. |
| 2142 | */ |
| 2143 | if (op == BPF_EXIT) |
| 2144 | return true; |
| 2145 | if (op == BPF_CALL) { |
| 2146 | /* BPF to BPF call will reach here because of marking |
| 2147 | * caller saved clobber with DST_OP_NO_MARK for which we |
| 2148 | * don't care the register def because they are anyway |
| 2149 | * marked as NOT_INIT already. |
| 2150 | */ |
| 2151 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 2152 | return false; |
| 2153 | /* Helper call will reach here because of arg type |
| 2154 | * check, conservatively return TRUE. |
| 2155 | */ |
| 2156 | if (t == SRC_OP) |
| 2157 | return true; |
| 2158 | |
| 2159 | return false; |
| 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | if (class == BPF_ALU64 || class == BPF_JMP || |
| 2164 | /* BPF_END always use BPF_ALU class. */ |
| 2165 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) |
| 2166 | return true; |
| 2167 | |
| 2168 | if (class == BPF_ALU || class == BPF_JMP32) |
| 2169 | return false; |
| 2170 | |
| 2171 | if (class == BPF_LDX) { |
| 2172 | if (t != SRC_OP) |
| 2173 | return BPF_SIZE(code) == BPF_DW; |
| 2174 | /* LDX source must be ptr. */ |
| 2175 | return true; |
| 2176 | } |
| 2177 | |
| 2178 | if (class == BPF_STX) { |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 2179 | /* BPF_STX (including atomic variants) has multiple source |
| 2180 | * operands, one of which is a ptr. Check whether the caller is |
| 2181 | * asking about it. |
| 2182 | */ |
| 2183 | if (t == SRC_OP && reg->type != SCALAR_VALUE) |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2184 | return true; |
| 2185 | return BPF_SIZE(code) == BPF_DW; |
| 2186 | } |
| 2187 | |
| 2188 | if (class == BPF_LD) { |
| 2189 | u8 mode = BPF_MODE(code); |
| 2190 | |
| 2191 | /* LD_IMM64 */ |
| 2192 | if (mode == BPF_IMM) |
| 2193 | return true; |
| 2194 | |
| 2195 | /* Both LD_IND and LD_ABS return 32-bit data. */ |
| 2196 | if (t != SRC_OP) |
| 2197 | return false; |
| 2198 | |
| 2199 | /* Implicit ctx ptr. */ |
| 2200 | if (regno == BPF_REG_6) |
| 2201 | return true; |
| 2202 | |
| 2203 | /* Explicit source could be any width. */ |
| 2204 | return true; |
| 2205 | } |
| 2206 | |
| 2207 | if (class == BPF_ST) |
| 2208 | /* The only source register for BPF_ST is a ptr. */ |
| 2209 | return true; |
| 2210 | |
| 2211 | /* Conservatively return true at default. */ |
| 2212 | return true; |
| 2213 | } |
| 2214 | |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 2215 | /* Return the regno defined by the insn, or -1. */ |
| 2216 | static int insn_def_regno(const struct bpf_insn *insn) |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 2217 | { |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 2218 | switch (BPF_CLASS(insn->code)) { |
| 2219 | case BPF_JMP: |
| 2220 | case BPF_JMP32: |
| 2221 | case BPF_ST: |
| 2222 | return -1; |
| 2223 | case BPF_STX: |
| 2224 | if (BPF_MODE(insn->code) == BPF_ATOMIC && |
| 2225 | (insn->imm & BPF_FETCH)) { |
| 2226 | if (insn->imm == BPF_CMPXCHG) |
| 2227 | return BPF_REG_0; |
| 2228 | else |
| 2229 | return insn->src_reg; |
| 2230 | } else { |
| 2231 | return -1; |
| 2232 | } |
| 2233 | default: |
| 2234 | return insn->dst_reg; |
| 2235 | } |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ |
| 2239 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 2240 | { |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 2241 | int dst_reg = insn_def_regno(insn); |
| 2242 | |
| 2243 | if (dst_reg == -1) |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 2244 | return false; |
| 2245 | |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 2246 | return !is_reg64(env, insn, dst_reg, NULL, DST_OP); |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 2247 | } |
| 2248 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2249 | static void mark_insn_zext(struct bpf_verifier_env *env, |
| 2250 | struct bpf_reg_state *reg) |
| 2251 | { |
| 2252 | s32 def_idx = reg->subreg_def; |
| 2253 | |
| 2254 | if (def_idx == DEF_NOT_SUBREG) |
| 2255 | return; |
| 2256 | |
| 2257 | env->insn_aux_data[def_idx - 1].zext_dst = true; |
| 2258 | /* The dst will be zero extended, so won't be sub-register anymore. */ |
| 2259 | reg->subreg_def = DEF_NOT_SUBREG; |
| 2260 | } |
| 2261 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2262 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2263 | enum reg_arg_type t) |
| 2264 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2265 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2266 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2267 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 2268 | struct bpf_reg_state *reg, *regs = state->regs; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2269 | bool rw64; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2270 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2271 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2272 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2273 | return -EINVAL; |
| 2274 | } |
| 2275 | |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 2276 | mark_reg_scratched(env, regno); |
| 2277 | |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 2278 | reg = ®s[regno]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2279 | rw64 = is_reg64(env, insn, regno, reg, t); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2280 | if (t == SRC_OP) { |
| 2281 | /* check whether register used as source operand can be read */ |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 2282 | if (reg->type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2283 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2284 | return -EACCES; |
| 2285 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2286 | /* We don't need to worry about FP liveness because it's read-only */ |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 2287 | if (regno == BPF_REG_FP) |
| 2288 | return 0; |
| 2289 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2290 | if (rw64) |
| 2291 | mark_insn_zext(env, reg); |
| 2292 | |
| 2293 | return mark_reg_read(env, reg, reg->parent, |
| 2294 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2295 | } else { |
| 2296 | /* check whether register used as dest operand can be written to */ |
| 2297 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2298 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2299 | return -EACCES; |
| 2300 | } |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 2301 | reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2302 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2303 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2304 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2305 | } |
| 2306 | return 0; |
| 2307 | } |
| 2308 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2309 | /* for any branch, call, exit record the history of jmps in the given state */ |
| 2310 | static int push_jmp_history(struct bpf_verifier_env *env, |
| 2311 | struct bpf_verifier_state *cur) |
| 2312 | { |
| 2313 | u32 cnt = cur->jmp_history_cnt; |
| 2314 | struct bpf_idx_pair *p; |
| 2315 | |
| 2316 | cnt++; |
| 2317 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); |
| 2318 | if (!p) |
| 2319 | return -ENOMEM; |
| 2320 | p[cnt - 1].idx = env->insn_idx; |
| 2321 | p[cnt - 1].prev_idx = env->prev_insn_idx; |
| 2322 | cur->jmp_history = p; |
| 2323 | cur->jmp_history_cnt = cnt; |
| 2324 | return 0; |
| 2325 | } |
| 2326 | |
| 2327 | /* Backtrack one insn at a time. If idx is not at the top of recorded |
| 2328 | * history then previous instruction came from straight line execution. |
| 2329 | */ |
| 2330 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, |
| 2331 | u32 *history) |
| 2332 | { |
| 2333 | u32 cnt = *history; |
| 2334 | |
| 2335 | if (cnt && st->jmp_history[cnt - 1].idx == i) { |
| 2336 | i = st->jmp_history[cnt - 1].prev_idx; |
| 2337 | (*history)--; |
| 2338 | } else { |
| 2339 | i--; |
| 2340 | } |
| 2341 | return i; |
| 2342 | } |
| 2343 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2344 | static const char *disasm_kfunc_name(void *data, const struct bpf_insn *insn) |
| 2345 | { |
| 2346 | const struct btf_type *func; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 2347 | struct btf *desc_btf; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2348 | |
| 2349 | if (insn->src_reg != BPF_PSEUDO_KFUNC_CALL) |
| 2350 | return NULL; |
| 2351 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 2352 | desc_btf = find_kfunc_desc_btf(data, insn->imm, insn->off, NULL); |
| 2353 | if (IS_ERR(desc_btf)) |
| 2354 | return "<error>"; |
| 2355 | |
| 2356 | func = btf_type_by_id(desc_btf, insn->imm); |
| 2357 | return btf_name_by_offset(desc_btf, func->name_off); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2358 | } |
| 2359 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2360 | /* For given verifier state backtrack_insn() is called from the last insn to |
| 2361 | * the first insn. Its purpose is to compute a bitmask of registers and |
| 2362 | * stack slots that needs precision in the parent verifier state. |
| 2363 | */ |
| 2364 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, |
| 2365 | u32 *reg_mask, u64 *stack_mask) |
| 2366 | { |
| 2367 | const struct bpf_insn_cbs cbs = { |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2368 | .cb_call = disasm_kfunc_name, |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2369 | .cb_print = verbose, |
| 2370 | .private_data = env, |
| 2371 | }; |
| 2372 | struct bpf_insn *insn = env->prog->insnsi + idx; |
| 2373 | u8 class = BPF_CLASS(insn->code); |
| 2374 | u8 opcode = BPF_OP(insn->code); |
| 2375 | u8 mode = BPF_MODE(insn->code); |
| 2376 | u32 dreg = 1u << insn->dst_reg; |
| 2377 | u32 sreg = 1u << insn->src_reg; |
| 2378 | u32 spi; |
| 2379 | |
| 2380 | if (insn->code == 0) |
| 2381 | return 0; |
Christy Lee | 496f332 | 2021-12-16 13:33:58 -0800 | [diff] [blame] | 2382 | if (env->log.level & BPF_LOG_LEVEL2) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2383 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
| 2384 | verbose(env, "%d: ", idx); |
| 2385 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
| 2386 | } |
| 2387 | |
| 2388 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 2389 | if (!(*reg_mask & dreg)) |
| 2390 | return 0; |
| 2391 | if (opcode == BPF_MOV) { |
| 2392 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2393 | /* dreg = sreg |
| 2394 | * dreg needs precision after this insn |
| 2395 | * sreg needs precision before this insn |
| 2396 | */ |
| 2397 | *reg_mask &= ~dreg; |
| 2398 | *reg_mask |= sreg; |
| 2399 | } else { |
| 2400 | /* dreg = K |
| 2401 | * dreg needs precision after this insn. |
| 2402 | * Corresponding register is already marked |
| 2403 | * as precise=true in this verifier state. |
| 2404 | * No further markings in parent are necessary |
| 2405 | */ |
| 2406 | *reg_mask &= ~dreg; |
| 2407 | } |
| 2408 | } else { |
| 2409 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2410 | /* dreg += sreg |
| 2411 | * both dreg and sreg need precision |
| 2412 | * before this insn |
| 2413 | */ |
| 2414 | *reg_mask |= sreg; |
| 2415 | } /* else dreg += K |
| 2416 | * dreg still needs precision before this insn |
| 2417 | */ |
| 2418 | } |
| 2419 | } else if (class == BPF_LDX) { |
| 2420 | if (!(*reg_mask & dreg)) |
| 2421 | return 0; |
| 2422 | *reg_mask &= ~dreg; |
| 2423 | |
| 2424 | /* scalars can only be spilled into stack w/o losing precision. |
| 2425 | * Load from any other memory can be zero extended. |
| 2426 | * The desire to keep that precision is already indicated |
| 2427 | * by 'precise' mark in corresponding register of this state. |
| 2428 | * No further tracking necessary. |
| 2429 | */ |
| 2430 | if (insn->src_reg != BPF_REG_FP) |
| 2431 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2432 | |
| 2433 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. |
| 2434 | * that [fp - off] slot contains scalar that needs to be |
| 2435 | * tracked with precision |
| 2436 | */ |
| 2437 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 2438 | if (spi >= 64) { |
| 2439 | verbose(env, "BUG spi %d\n", spi); |
| 2440 | WARN_ONCE(1, "verifier backtracking bug"); |
| 2441 | return -EFAULT; |
| 2442 | } |
| 2443 | *stack_mask |= 1ull << spi; |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 2444 | } else if (class == BPF_STX || class == BPF_ST) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2445 | if (*reg_mask & dreg) |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 2446 | /* stx & st shouldn't be using _scalar_ dst_reg |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2447 | * to access memory. It means backtracking |
| 2448 | * encountered a case of pointer subtraction. |
| 2449 | */ |
| 2450 | return -ENOTSUPP; |
| 2451 | /* scalars can only be spilled into stack */ |
| 2452 | if (insn->dst_reg != BPF_REG_FP) |
| 2453 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2454 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 2455 | if (spi >= 64) { |
| 2456 | verbose(env, "BUG spi %d\n", spi); |
| 2457 | WARN_ONCE(1, "verifier backtracking bug"); |
| 2458 | return -EFAULT; |
| 2459 | } |
| 2460 | if (!(*stack_mask & (1ull << spi))) |
| 2461 | return 0; |
| 2462 | *stack_mask &= ~(1ull << spi); |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 2463 | if (class == BPF_STX) |
| 2464 | *reg_mask |= sreg; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2465 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
| 2466 | if (opcode == BPF_CALL) { |
| 2467 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 2468 | return -ENOTSUPP; |
| 2469 | /* regular helper call sets R0 */ |
| 2470 | *reg_mask &= ~1; |
| 2471 | if (*reg_mask & 0x3f) { |
| 2472 | /* if backtracing was looking for registers R1-R5 |
| 2473 | * they should have been found already. |
| 2474 | */ |
| 2475 | verbose(env, "BUG regs %x\n", *reg_mask); |
| 2476 | WARN_ONCE(1, "verifier backtracking bug"); |
| 2477 | return -EFAULT; |
| 2478 | } |
| 2479 | } else if (opcode == BPF_EXIT) { |
| 2480 | return -ENOTSUPP; |
| 2481 | } |
| 2482 | } else if (class == BPF_LD) { |
| 2483 | if (!(*reg_mask & dreg)) |
| 2484 | return 0; |
| 2485 | *reg_mask &= ~dreg; |
| 2486 | /* It's ld_imm64 or ld_abs or ld_ind. |
| 2487 | * For ld_imm64 no further tracking of precision |
| 2488 | * into parent is necessary |
| 2489 | */ |
| 2490 | if (mode == BPF_IND || mode == BPF_ABS) |
| 2491 | /* to be analyzed */ |
| 2492 | return -ENOTSUPP; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2493 | } |
| 2494 | return 0; |
| 2495 | } |
| 2496 | |
| 2497 | /* the scalar precision tracking algorithm: |
| 2498 | * . at the start all registers have precise=false. |
| 2499 | * . scalar ranges are tracked as normal through alu and jmp insns. |
| 2500 | * . once precise value of the scalar register is used in: |
| 2501 | * . ptr + scalar alu |
| 2502 | * . if (scalar cond K|scalar) |
| 2503 | * . helper_call(.., scalar, ...) where ARG_CONST is expected |
| 2504 | * backtrack through the verifier states and mark all registers and |
| 2505 | * stack slots with spilled constants that these scalar regisers |
| 2506 | * should be precise. |
| 2507 | * . during state pruning two registers (or spilled stack slots) |
| 2508 | * are equivalent if both are not precise. |
| 2509 | * |
| 2510 | * Note the verifier cannot simply walk register parentage chain, |
| 2511 | * since many different registers and stack slots could have been |
| 2512 | * used to compute single precise scalar. |
| 2513 | * |
| 2514 | * The approach of starting with precise=true for all registers and then |
| 2515 | * backtrack to mark a register as not precise when the verifier detects |
| 2516 | * that program doesn't care about specific value (e.g., when helper |
| 2517 | * takes register as ARG_ANYTHING parameter) is not safe. |
| 2518 | * |
| 2519 | * It's ok to walk single parentage chain of the verifier states. |
| 2520 | * It's possible that this backtracking will go all the way till 1st insn. |
| 2521 | * All other branches will be explored for needing precision later. |
| 2522 | * |
| 2523 | * The backtracking needs to deal with cases like: |
| 2524 | * 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) |
| 2525 | * r9 -= r8 |
| 2526 | * r5 = r9 |
| 2527 | * if r5 > 0x79f goto pc+7 |
| 2528 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) |
| 2529 | * r5 += 1 |
| 2530 | * ... |
| 2531 | * call bpf_perf_event_output#25 |
| 2532 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO |
| 2533 | * |
| 2534 | * and this case: |
| 2535 | * r6 = 1 |
| 2536 | * call foo // uses callee's r6 inside to compute r0 |
| 2537 | * r0 += r6 |
| 2538 | * if r0 == 0 goto |
| 2539 | * |
| 2540 | * to track above reg_mask/stack_mask needs to be independent for each frame. |
| 2541 | * |
| 2542 | * Also if parent's curframe > frame where backtracking started, |
| 2543 | * the verifier need to mark registers in both frames, otherwise callees |
| 2544 | * may incorrectly prune callers. This is similar to |
| 2545 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") |
| 2546 | * |
| 2547 | * For now backtracking falls back into conservative marking. |
| 2548 | */ |
| 2549 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, |
| 2550 | struct bpf_verifier_state *st) |
| 2551 | { |
| 2552 | struct bpf_func_state *func; |
| 2553 | struct bpf_reg_state *reg; |
| 2554 | int i, j; |
| 2555 | |
| 2556 | /* big hammer: mark all scalars precise in this path. |
| 2557 | * pop_stack may still get !precise scalars. |
| 2558 | */ |
| 2559 | for (; st; st = st->parent) |
| 2560 | for (i = 0; i <= st->curframe; i++) { |
| 2561 | func = st->frame[i]; |
| 2562 | for (j = 0; j < BPF_REG_FP; j++) { |
| 2563 | reg = &func->regs[j]; |
| 2564 | if (reg->type != SCALAR_VALUE) |
| 2565 | continue; |
| 2566 | reg->precise = true; |
| 2567 | } |
| 2568 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 2569 | if (!is_spilled_reg(&func->stack[j])) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2570 | continue; |
| 2571 | reg = &func->stack[j].spilled_ptr; |
| 2572 | if (reg->type != SCALAR_VALUE) |
| 2573 | continue; |
| 2574 | reg->precise = true; |
| 2575 | } |
| 2576 | } |
| 2577 | } |
| 2578 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2579 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
| 2580 | int spi) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2581 | { |
| 2582 | struct bpf_verifier_state *st = env->cur_state; |
| 2583 | int first_idx = st->first_insn_idx; |
| 2584 | int last_idx = env->insn_idx; |
| 2585 | struct bpf_func_state *func; |
| 2586 | struct bpf_reg_state *reg; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2587 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
| 2588 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2589 | bool skip_first = true; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2590 | bool new_marks = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2591 | int i, err; |
| 2592 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2593 | if (!env->bpf_capable) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2594 | return 0; |
| 2595 | |
| 2596 | func = st->frame[st->curframe]; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2597 | if (regno >= 0) { |
| 2598 | reg = &func->regs[regno]; |
| 2599 | if (reg->type != SCALAR_VALUE) { |
| 2600 | WARN_ONCE(1, "backtracing misuse"); |
| 2601 | return -EFAULT; |
| 2602 | } |
| 2603 | if (!reg->precise) |
| 2604 | new_marks = true; |
| 2605 | else |
| 2606 | reg_mask = 0; |
| 2607 | reg->precise = true; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2608 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2609 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2610 | while (spi >= 0) { |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 2611 | if (!is_spilled_reg(&func->stack[spi])) { |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2612 | stack_mask = 0; |
| 2613 | break; |
| 2614 | } |
| 2615 | reg = &func->stack[spi].spilled_ptr; |
| 2616 | if (reg->type != SCALAR_VALUE) { |
| 2617 | stack_mask = 0; |
| 2618 | break; |
| 2619 | } |
| 2620 | if (!reg->precise) |
| 2621 | new_marks = true; |
| 2622 | else |
| 2623 | stack_mask = 0; |
| 2624 | reg->precise = true; |
| 2625 | break; |
| 2626 | } |
| 2627 | |
| 2628 | if (!new_marks) |
| 2629 | return 0; |
| 2630 | if (!reg_mask && !stack_mask) |
| 2631 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2632 | for (;;) { |
| 2633 | DECLARE_BITMAP(mask, 64); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2634 | u32 history = st->jmp_history_cnt; |
| 2635 | |
Christy Lee | 496f332 | 2021-12-16 13:33:58 -0800 | [diff] [blame] | 2636 | if (env->log.level & BPF_LOG_LEVEL2) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2637 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
| 2638 | for (i = last_idx;;) { |
| 2639 | if (skip_first) { |
| 2640 | err = 0; |
| 2641 | skip_first = false; |
| 2642 | } else { |
| 2643 | err = backtrack_insn(env, i, ®_mask, &stack_mask); |
| 2644 | } |
| 2645 | if (err == -ENOTSUPP) { |
| 2646 | mark_all_scalars_precise(env, st); |
| 2647 | return 0; |
| 2648 | } else if (err) { |
| 2649 | return err; |
| 2650 | } |
| 2651 | if (!reg_mask && !stack_mask) |
| 2652 | /* Found assignment(s) into tracked register in this state. |
| 2653 | * Since this state is already marked, just return. |
| 2654 | * Nothing to be tracked further in the parent state. |
| 2655 | */ |
| 2656 | return 0; |
| 2657 | if (i == first_idx) |
| 2658 | break; |
| 2659 | i = get_prev_insn_idx(st, i, &history); |
| 2660 | if (i >= env->prog->len) { |
| 2661 | /* This can happen if backtracking reached insn 0 |
| 2662 | * and there are still reg_mask or stack_mask |
| 2663 | * to backtrack. |
| 2664 | * It means the backtracking missed the spot where |
| 2665 | * particular register was initialized with a constant. |
| 2666 | */ |
| 2667 | verbose(env, "BUG backtracking idx %d\n", i); |
| 2668 | WARN_ONCE(1, "verifier backtracking bug"); |
| 2669 | return -EFAULT; |
| 2670 | } |
| 2671 | } |
| 2672 | st = st->parent; |
| 2673 | if (!st) |
| 2674 | break; |
| 2675 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2676 | new_marks = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2677 | func = st->frame[st->curframe]; |
| 2678 | bitmap_from_u64(mask, reg_mask); |
| 2679 | for_each_set_bit(i, mask, 32) { |
| 2680 | reg = &func->regs[i]; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2681 | if (reg->type != SCALAR_VALUE) { |
| 2682 | reg_mask &= ~(1u << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2683 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2684 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2685 | if (!reg->precise) |
| 2686 | new_marks = true; |
| 2687 | reg->precise = true; |
| 2688 | } |
| 2689 | |
| 2690 | bitmap_from_u64(mask, stack_mask); |
| 2691 | for_each_set_bit(i, mask, 64) { |
| 2692 | if (i >= func->allocated_stack / BPF_REG_SIZE) { |
Alexei Starovoitov | 2339cd6 | 2019-09-03 15:16:17 -0700 | [diff] [blame] | 2693 | /* the sequence of instructions: |
| 2694 | * 2: (bf) r3 = r10 |
| 2695 | * 3: (7b) *(u64 *)(r3 -8) = r0 |
| 2696 | * 4: (79) r4 = *(u64 *)(r10 -8) |
| 2697 | * doesn't contain jmps. It's backtracked |
| 2698 | * as a single block. |
| 2699 | * During backtracking insn 3 is not recognized as |
| 2700 | * stack access, so at the end of backtracking |
| 2701 | * stack slot fp-8 is still marked in stack_mask. |
| 2702 | * However the parent state may not have accessed |
| 2703 | * fp-8 and it's "unallocated" stack space. |
| 2704 | * In such case fallback to conservative. |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2705 | */ |
Alexei Starovoitov | 2339cd6 | 2019-09-03 15:16:17 -0700 | [diff] [blame] | 2706 | mark_all_scalars_precise(env, st); |
| 2707 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2708 | } |
| 2709 | |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 2710 | if (!is_spilled_reg(&func->stack[i])) { |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2711 | stack_mask &= ~(1ull << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2712 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2713 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2714 | reg = &func->stack[i].spilled_ptr; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2715 | if (reg->type != SCALAR_VALUE) { |
| 2716 | stack_mask &= ~(1ull << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2717 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2718 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2719 | if (!reg->precise) |
| 2720 | new_marks = true; |
| 2721 | reg->precise = true; |
| 2722 | } |
Christy Lee | 496f332 | 2021-12-16 13:33:58 -0800 | [diff] [blame] | 2723 | if (env->log.level & BPF_LOG_LEVEL2) { |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame] | 2724 | verbose(env, "parent %s regs=%x stack=%llx marks:", |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2725 | new_marks ? "didn't have" : "already had", |
| 2726 | reg_mask, stack_mask); |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame] | 2727 | print_verifier_state(env, func, true); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2728 | } |
| 2729 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2730 | if (!reg_mask && !stack_mask) |
| 2731 | break; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2732 | if (!new_marks) |
| 2733 | break; |
| 2734 | |
| 2735 | last_idx = st->last_insn_idx; |
| 2736 | first_idx = st->first_insn_idx; |
| 2737 | } |
| 2738 | return 0; |
| 2739 | } |
| 2740 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2741 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
| 2742 | { |
| 2743 | return __mark_chain_precision(env, regno, -1); |
| 2744 | } |
| 2745 | |
| 2746 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) |
| 2747 | { |
| 2748 | return __mark_chain_precision(env, -1, spi); |
| 2749 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2750 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2751 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 2752 | { |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 2753 | switch (base_type(type)) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2754 | case PTR_TO_MAP_VALUE: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2755 | case PTR_TO_STACK: |
| 2756 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2757 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2758 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2759 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2760 | case PTR_TO_FLOW_KEYS: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2761 | case CONST_PTR_TO_MAP: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2762 | case PTR_TO_SOCKET: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2763 | case PTR_TO_SOCK_COMMON: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2764 | case PTR_TO_TCP_SOCK: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2765 | case PTR_TO_XDP_SOCK: |
Martin KaFai Lau | 65726b5 | 2020-01-08 16:34:54 -0800 | [diff] [blame] | 2766 | case PTR_TO_BTF_ID: |
Hao Luo | 20b2aff | 2021-12-16 16:31:48 -0800 | [diff] [blame] | 2767 | case PTR_TO_BUF: |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 2768 | case PTR_TO_PERCPU_BTF_ID: |
Gilad Reti | 744ea4e | 2021-01-13 07:38:07 +0200 | [diff] [blame] | 2769 | case PTR_TO_MEM: |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 2770 | case PTR_TO_FUNC: |
| 2771 | case PTR_TO_MAP_KEY: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2772 | return true; |
| 2773 | default: |
| 2774 | return false; |
| 2775 | } |
| 2776 | } |
| 2777 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2778 | /* Does this register contain a constant zero? */ |
| 2779 | static bool register_is_null(struct bpf_reg_state *reg) |
| 2780 | { |
| 2781 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 2782 | } |
| 2783 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2784 | static bool register_is_const(struct bpf_reg_state *reg) |
| 2785 | { |
| 2786 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); |
| 2787 | } |
| 2788 | |
Yonghong Song | 5689d49 | 2020-10-08 18:12:38 -0700 | [diff] [blame] | 2789 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
| 2790 | { |
| 2791 | return tnum_is_unknown(reg->var_off) && |
| 2792 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && |
| 2793 | reg->umin_value == 0 && reg->umax_value == U64_MAX && |
| 2794 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && |
| 2795 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; |
| 2796 | } |
| 2797 | |
| 2798 | static bool register_is_bounded(struct bpf_reg_state *reg) |
| 2799 | { |
| 2800 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); |
| 2801 | } |
| 2802 | |
Jann Horn | 6e7e63c | 2020-04-17 02:00:06 +0200 | [diff] [blame] | 2803 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 2804 | const struct bpf_reg_state *reg) |
| 2805 | { |
| 2806 | if (allow_ptr_leaks) |
| 2807 | return false; |
| 2808 | |
| 2809 | return reg->type != SCALAR_VALUE; |
| 2810 | } |
| 2811 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2812 | static void save_register_state(struct bpf_func_state *state, |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2813 | int spi, struct bpf_reg_state *reg, |
| 2814 | int size) |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2815 | { |
| 2816 | int i; |
| 2817 | |
| 2818 | state->stack[spi].spilled_ptr = *reg; |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2819 | if (size == BPF_REG_SIZE) |
| 2820 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2821 | |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2822 | for (i = BPF_REG_SIZE; i > BPF_REG_SIZE - size; i--) |
| 2823 | state->stack[spi].slot_type[i - 1] = STACK_SPILL; |
| 2824 | |
| 2825 | /* size < 8 bytes spill */ |
| 2826 | for (; i; i--) |
| 2827 | scrub_spilled_slot(&state->stack[spi].slot_type[i - 1]); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2828 | } |
| 2829 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 2830 | /* check_stack_{read,write}_fixed_off functions track spill/fill of registers, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2831 | * stack boundary and alignment are checked in check_mem_access() |
| 2832 | */ |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 2833 | static int check_stack_write_fixed_off(struct bpf_verifier_env *env, |
| 2834 | /* stack frame we're writing to */ |
| 2835 | struct bpf_func_state *state, |
| 2836 | int off, int size, int value_regno, |
| 2837 | int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2838 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2839 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2840 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2841 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2842 | struct bpf_reg_state *reg = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2843 | |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 2844 | err = grow_stack_state(state, round_up(slot + 1, BPF_REG_SIZE)); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2845 | if (err) |
| 2846 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2847 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 2848 | * so it's aligned access and [off, off + size) are within stack limits |
| 2849 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2850 | if (!env->allow_ptr_leaks && |
| 2851 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 2852 | size != BPF_REG_SIZE) { |
| 2853 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 2854 | return -EACCES; |
| 2855 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2856 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2857 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2858 | if (value_regno >= 0) |
| 2859 | reg = &cur->regs[value_regno]; |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 2860 | if (!env->bypass_spec_v4) { |
| 2861 | bool sanitize = reg && is_spillable_regtype(reg->type); |
| 2862 | |
| 2863 | for (i = 0; i < size; i++) { |
| 2864 | if (state->stack[spi].slot_type[i] == STACK_INVALID) { |
| 2865 | sanitize = true; |
| 2866 | break; |
| 2867 | } |
| 2868 | } |
| 2869 | |
| 2870 | if (sanitize) |
| 2871 | env->insn_aux_data[insn_idx].sanitize_stack_spill = true; |
| 2872 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2873 | |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 2874 | mark_stack_slot_scratched(env, spi); |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2875 | if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) && |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2876 | !register_is_null(reg) && env->bpf_capable) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2877 | if (dst_reg != BPF_REG_FP) { |
| 2878 | /* The backtracking logic can only recognize explicit |
| 2879 | * stack slot address like [fp - 8]. Other spill of |
Zhen Lei | 8fb33b6 | 2021-05-25 10:56:59 +0800 | [diff] [blame] | 2880 | * scalar via different register has to be conservative. |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2881 | * Backtrack from here and mark all registers as precise |
| 2882 | * that contributed into 'reg' being a constant. |
| 2883 | */ |
| 2884 | err = mark_chain_precision(env, value_regno); |
| 2885 | if (err) |
| 2886 | return err; |
| 2887 | } |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2888 | save_register_state(state, spi, reg, size); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2889 | } else if (reg && is_spillable_regtype(reg->type)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2890 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2891 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2892 | verbose_linfo(env, insn_idx, "; "); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2893 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2894 | return -EACCES; |
| 2895 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2896 | if (state != cur && reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2897 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 2898 | return -EINVAL; |
| 2899 | } |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2900 | save_register_state(state, spi, reg, size); |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2901 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2902 | u8 type = STACK_MISC; |
| 2903 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2904 | /* regular write of data into stack destroys any spilled ptr */ |
| 2905 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2906 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 2907 | if (is_spilled_reg(&state->stack[spi])) |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2908 | for (i = 0; i < BPF_REG_SIZE; i++) |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2909 | scrub_spilled_slot(&state->stack[spi].slot_type[i]); |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2910 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2911 | /* only mark the slot as written if all 8 bytes were written |
| 2912 | * otherwise read propagation may incorrectly stop too soon |
| 2913 | * when stack slots are partially written. |
| 2914 | * This heuristic means that read propagation will be |
| 2915 | * conservative, since it will add reg_live_read marks |
| 2916 | * to stack slots all the way to first state when programs |
| 2917 | * writes+reads less than 8 bytes |
| 2918 | */ |
| 2919 | if (size == BPF_REG_SIZE) |
| 2920 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 2921 | |
| 2922 | /* when we zero initialize stack slots mark them as such */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2923 | if (reg && register_is_null(reg)) { |
| 2924 | /* backtracking doesn't work for STACK_ZERO yet. */ |
| 2925 | err = mark_chain_precision(env, value_regno); |
| 2926 | if (err) |
| 2927 | return err; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2928 | type = STACK_ZERO; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2929 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2930 | |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2931 | /* Mark slots affected by this stack write. */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2932 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2933 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2934 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2935 | } |
| 2936 | return 0; |
| 2937 | } |
| 2938 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 2939 | /* Write the stack: 'stack[ptr_regno + off] = value_regno'. 'ptr_regno' is |
| 2940 | * known to contain a variable offset. |
| 2941 | * This function checks whether the write is permitted and conservatively |
| 2942 | * tracks the effects of the write, considering that each stack slot in the |
| 2943 | * dynamic range is potentially written to. |
| 2944 | * |
| 2945 | * 'off' includes 'regno->off'. |
| 2946 | * 'value_regno' can be -1, meaning that an unknown value is being written to |
| 2947 | * the stack. |
| 2948 | * |
| 2949 | * Spilled pointers in range are not marked as written because we don't know |
| 2950 | * what's going to be actually written. This means that read propagation for |
| 2951 | * future reads cannot be terminated by this write. |
| 2952 | * |
| 2953 | * For privileged programs, uninitialized stack slots are considered |
| 2954 | * initialized by this write (even though we don't know exactly what offsets |
| 2955 | * are going to be written to). The idea is that we don't want the verifier to |
| 2956 | * reject future reads that access slots written to through variable offsets. |
| 2957 | */ |
| 2958 | static int check_stack_write_var_off(struct bpf_verifier_env *env, |
| 2959 | /* func where register points to */ |
| 2960 | struct bpf_func_state *state, |
| 2961 | int ptr_regno, int off, int size, |
| 2962 | int value_regno, int insn_idx) |
| 2963 | { |
| 2964 | struct bpf_func_state *cur; /* state of the current function */ |
| 2965 | int min_off, max_off; |
| 2966 | int i, err; |
| 2967 | struct bpf_reg_state *ptr_reg = NULL, *value_reg = NULL; |
| 2968 | bool writing_zero = false; |
| 2969 | /* set if the fact that we're writing a zero is used to let any |
| 2970 | * stack slots remain STACK_ZERO |
| 2971 | */ |
| 2972 | bool zero_used = false; |
| 2973 | |
| 2974 | cur = env->cur_state->frame[env->cur_state->curframe]; |
| 2975 | ptr_reg = &cur->regs[ptr_regno]; |
| 2976 | min_off = ptr_reg->smin_value + off; |
| 2977 | max_off = ptr_reg->smax_value + off + size; |
| 2978 | if (value_regno >= 0) |
| 2979 | value_reg = &cur->regs[value_regno]; |
| 2980 | if (value_reg && register_is_null(value_reg)) |
| 2981 | writing_zero = true; |
| 2982 | |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 2983 | err = grow_stack_state(state, round_up(-min_off, BPF_REG_SIZE)); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 2984 | if (err) |
| 2985 | return err; |
| 2986 | |
| 2987 | |
| 2988 | /* Variable offset writes destroy any spilled pointers in range. */ |
| 2989 | for (i = min_off; i < max_off; i++) { |
| 2990 | u8 new_type, *stype; |
| 2991 | int slot, spi; |
| 2992 | |
| 2993 | slot = -i - 1; |
| 2994 | spi = slot / BPF_REG_SIZE; |
| 2995 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 2996 | mark_stack_slot_scratched(env, spi); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 2997 | |
| 2998 | if (!env->allow_ptr_leaks |
| 2999 | && *stype != NOT_INIT |
| 3000 | && *stype != SCALAR_VALUE) { |
| 3001 | /* Reject the write if there's are spilled pointers in |
| 3002 | * range. If we didn't reject here, the ptr status |
| 3003 | * would be erased below (even though not all slots are |
| 3004 | * actually overwritten), possibly opening the door to |
| 3005 | * leaks. |
| 3006 | */ |
| 3007 | verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d", |
| 3008 | insn_idx, i); |
| 3009 | return -EINVAL; |
| 3010 | } |
| 3011 | |
| 3012 | /* Erase all spilled pointers. */ |
| 3013 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
| 3014 | |
| 3015 | /* Update the slot type. */ |
| 3016 | new_type = STACK_MISC; |
| 3017 | if (writing_zero && *stype == STACK_ZERO) { |
| 3018 | new_type = STACK_ZERO; |
| 3019 | zero_used = true; |
| 3020 | } |
| 3021 | /* If the slot is STACK_INVALID, we check whether it's OK to |
| 3022 | * pretend that it will be initialized by this write. The slot |
| 3023 | * might not actually be written to, and so if we mark it as |
| 3024 | * initialized future reads might leak uninitialized memory. |
| 3025 | * For privileged programs, we will accept such reads to slots |
| 3026 | * that may or may not be written because, if we're reject |
| 3027 | * them, the error would be too confusing. |
| 3028 | */ |
| 3029 | if (*stype == STACK_INVALID && !env->allow_uninit_stack) { |
| 3030 | verbose(env, "uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d", |
| 3031 | insn_idx, i); |
| 3032 | return -EINVAL; |
| 3033 | } |
| 3034 | *stype = new_type; |
| 3035 | } |
| 3036 | if (zero_used) { |
| 3037 | /* backtracking doesn't work for STACK_ZERO yet. */ |
| 3038 | err = mark_chain_precision(env, value_regno); |
| 3039 | if (err) |
| 3040 | return err; |
| 3041 | } |
| 3042 | return 0; |
| 3043 | } |
| 3044 | |
| 3045 | /* When register 'dst_regno' is assigned some values from stack[min_off, |
| 3046 | * max_off), we set the register's type according to the types of the |
| 3047 | * respective stack slots. If all the stack values are known to be zeros, then |
| 3048 | * so is the destination reg. Otherwise, the register is considered to be |
| 3049 | * SCALAR. This function does not deal with register filling; the caller must |
| 3050 | * ensure that all spilled registers in the stack range have been marked as |
| 3051 | * read. |
| 3052 | */ |
| 3053 | static void mark_reg_stack_read(struct bpf_verifier_env *env, |
| 3054 | /* func where src register points to */ |
| 3055 | struct bpf_func_state *ptr_state, |
| 3056 | int min_off, int max_off, int dst_regno) |
| 3057 | { |
| 3058 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3059 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 3060 | int i, slot, spi; |
| 3061 | u8 *stype; |
| 3062 | int zeros = 0; |
| 3063 | |
| 3064 | for (i = min_off; i < max_off; i++) { |
| 3065 | slot = -i - 1; |
| 3066 | spi = slot / BPF_REG_SIZE; |
| 3067 | stype = ptr_state->stack[spi].slot_type; |
| 3068 | if (stype[slot % BPF_REG_SIZE] != STACK_ZERO) |
| 3069 | break; |
| 3070 | zeros++; |
| 3071 | } |
| 3072 | if (zeros == max_off - min_off) { |
| 3073 | /* any access_size read into register is zero extended, |
| 3074 | * so the whole register == const_zero |
| 3075 | */ |
| 3076 | __mark_reg_const_zero(&state->regs[dst_regno]); |
| 3077 | /* backtracking doesn't support STACK_ZERO yet, |
| 3078 | * so mark it precise here, so that later |
| 3079 | * backtracking can stop here. |
| 3080 | * Backtracking may not need this if this register |
| 3081 | * doesn't participate in pointer adjustment. |
| 3082 | * Forward propagation of precise flag is not |
| 3083 | * necessary either. This mark is only to stop |
| 3084 | * backtracking. Any register that contributed |
| 3085 | * to const 0 was marked precise before spill. |
| 3086 | */ |
| 3087 | state->regs[dst_regno].precise = true; |
| 3088 | } else { |
| 3089 | /* have read misc data from the stack */ |
| 3090 | mark_reg_unknown(env, state->regs, dst_regno); |
| 3091 | } |
| 3092 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
| 3093 | } |
| 3094 | |
| 3095 | /* Read the stack at 'off' and put the results into the register indicated by |
| 3096 | * 'dst_regno'. It handles reg filling if the addressed stack slot is a |
| 3097 | * spilled reg. |
| 3098 | * |
| 3099 | * 'dst_regno' can be -1, meaning that the read value is not going to a |
| 3100 | * register. |
| 3101 | * |
| 3102 | * The access is assumed to be within the current stack bounds. |
| 3103 | */ |
| 3104 | static int check_stack_read_fixed_off(struct bpf_verifier_env *env, |
| 3105 | /* func where src register points to */ |
| 3106 | struct bpf_func_state *reg_state, |
| 3107 | int off, int size, int dst_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3108 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3109 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3110 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3111 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3112 | struct bpf_reg_state *reg; |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 3113 | u8 *stype, type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3114 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3115 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3116 | reg = ®_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3117 | |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 3118 | if (is_spilled_reg(®_state->stack[spi])) { |
Martin KaFai Lau | f30d4968 | 2021-11-01 23:45:35 -0700 | [diff] [blame] | 3119 | u8 spill_size = 1; |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 3120 | |
Martin KaFai Lau | f30d4968 | 2021-11-01 23:45:35 -0700 | [diff] [blame] | 3121 | for (i = BPF_REG_SIZE - 1; i > 0 && stype[i - 1] == STACK_SPILL; i--) |
| 3122 | spill_size++; |
| 3123 | |
| 3124 | if (size != BPF_REG_SIZE || spill_size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3125 | if (reg->type != SCALAR_VALUE) { |
| 3126 | verbose_linfo(env, env->insn_idx, "; "); |
| 3127 | verbose(env, "invalid size of register fill\n"); |
| 3128 | return -EACCES; |
| 3129 | } |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 3130 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3131 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 3132 | if (dst_regno < 0) |
| 3133 | return 0; |
| 3134 | |
Martin KaFai Lau | f30d4968 | 2021-11-01 23:45:35 -0700 | [diff] [blame] | 3135 | if (!(off % BPF_REG_SIZE) && size == spill_size) { |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 3136 | /* The earlier check_reg_arg() has decided the |
| 3137 | * subreg_def for this insn. Save it first. |
| 3138 | */ |
| 3139 | s32 subreg_def = state->regs[dst_regno].subreg_def; |
| 3140 | |
| 3141 | state->regs[dst_regno] = *reg; |
| 3142 | state->regs[dst_regno].subreg_def = subreg_def; |
| 3143 | } else { |
| 3144 | for (i = 0; i < size; i++) { |
| 3145 | type = stype[(slot - i) % BPF_REG_SIZE]; |
| 3146 | if (type == STACK_SPILL) |
| 3147 | continue; |
| 3148 | if (type == STACK_MISC) |
| 3149 | continue; |
| 3150 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 3151 | off, i, size); |
| 3152 | return -EACCES; |
| 3153 | } |
| 3154 | mark_reg_unknown(env, state->regs, dst_regno); |
| 3155 | } |
| 3156 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3157 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3158 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3159 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3160 | if (dst_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3161 | /* restore register state from stack */ |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3162 | state->regs[dst_regno] = *reg; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 3163 | /* mark reg as written since spilled pointer state likely |
| 3164 | * has its liveness marks cleared by is_state_visited() |
| 3165 | * which resets stack/reg liveness for state transitions |
| 3166 | */ |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3167 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
Jann Horn | 6e7e63c | 2020-04-17 02:00:06 +0200 | [diff] [blame] | 3168 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3169 | /* If dst_regno==-1, the caller is asking us whether |
Jann Horn | 6e7e63c | 2020-04-17 02:00:06 +0200 | [diff] [blame] | 3170 | * it is acceptable to use this value as a SCALAR_VALUE |
| 3171 | * (e.g. for XADD). |
| 3172 | * We must not allow unprivileged callers to do that |
| 3173 | * with spilled pointers. |
| 3174 | */ |
| 3175 | verbose(env, "leaking pointer from stack off %d\n", |
| 3176 | off); |
| 3177 | return -EACCES; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3178 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3179 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3180 | } else { |
| 3181 | for (i = 0; i < size; i++) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3182 | type = stype[(slot - i) % BPF_REG_SIZE]; |
| 3183 | if (type == STACK_MISC) |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3184 | continue; |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3185 | if (type == STACK_ZERO) |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3186 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3187 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 3188 | off, i, size); |
| 3189 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3190 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3191 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3192 | if (dst_regno >= 0) |
| 3193 | mark_reg_stack_read(env, reg_state, off, off + size, dst_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3194 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3195 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3196 | } |
| 3197 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3198 | enum stack_access_src { |
| 3199 | ACCESS_DIRECT = 1, /* the access is performed by an instruction */ |
| 3200 | ACCESS_HELPER = 2, /* the access is performed by a helper */ |
| 3201 | }; |
| 3202 | |
| 3203 | static int check_stack_range_initialized(struct bpf_verifier_env *env, |
| 3204 | int regno, int off, int access_size, |
| 3205 | bool zero_size_allowed, |
| 3206 | enum stack_access_src type, |
| 3207 | struct bpf_call_arg_meta *meta); |
| 3208 | |
| 3209 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3210 | { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3211 | return cur_regs(env) + regno; |
| 3212 | } |
| 3213 | |
| 3214 | /* Read the stack at 'ptr_regno + off' and put the result into the register |
| 3215 | * 'dst_regno'. |
| 3216 | * 'off' includes the pointer register's fixed offset(i.e. 'ptr_regno.off'), |
| 3217 | * but not its variable offset. |
| 3218 | * 'size' is assumed to be <= reg size and the access is assumed to be aligned. |
| 3219 | * |
| 3220 | * As opposed to check_stack_read_fixed_off, this function doesn't deal with |
| 3221 | * filling registers (i.e. reads of spilled register cannot be detected when |
| 3222 | * the offset is not fixed). We conservatively mark 'dst_regno' as containing |
| 3223 | * SCALAR_VALUE. That's why we assert that the 'ptr_regno' has a variable |
| 3224 | * offset; for a fixed offset check_stack_read_fixed_off should be used |
| 3225 | * instead. |
| 3226 | */ |
| 3227 | static int check_stack_read_var_off(struct bpf_verifier_env *env, |
| 3228 | int ptr_regno, int off, int size, int dst_regno) |
| 3229 | { |
| 3230 | /* The state of the source register. */ |
| 3231 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); |
| 3232 | struct bpf_func_state *ptr_state = func(env, reg); |
| 3233 | int err; |
| 3234 | int min_off, max_off; |
| 3235 | |
| 3236 | /* Note that we pass a NULL meta, so raw access will not be permitted. |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3237 | */ |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3238 | err = check_stack_range_initialized(env, ptr_regno, off, size, |
| 3239 | false, ACCESS_DIRECT, NULL); |
| 3240 | if (err) |
| 3241 | return err; |
| 3242 | |
| 3243 | min_off = reg->smin_value + off; |
| 3244 | max_off = reg->smax_value + off; |
| 3245 | mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); |
| 3246 | return 0; |
| 3247 | } |
| 3248 | |
| 3249 | /* check_stack_read dispatches to check_stack_read_fixed_off or |
| 3250 | * check_stack_read_var_off. |
| 3251 | * |
| 3252 | * The caller must ensure that the offset falls within the allocated stack |
| 3253 | * bounds. |
| 3254 | * |
| 3255 | * 'dst_regno' is a register which will receive the value from the stack. It |
| 3256 | * can be -1, meaning that the read value is not going to a register. |
| 3257 | */ |
| 3258 | static int check_stack_read(struct bpf_verifier_env *env, |
| 3259 | int ptr_regno, int off, int size, |
| 3260 | int dst_regno) |
| 3261 | { |
| 3262 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); |
| 3263 | struct bpf_func_state *state = func(env, reg); |
| 3264 | int err; |
| 3265 | /* Some accesses are only permitted with a static offset. */ |
| 3266 | bool var_off = !tnum_is_const(reg->var_off); |
| 3267 | |
| 3268 | /* The offset is required to be static when reads don't go to a |
| 3269 | * register, in order to not leak pointers (see |
| 3270 | * check_stack_read_fixed_off). |
| 3271 | */ |
| 3272 | if (dst_regno < 0 && var_off) { |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3273 | char tn_buf[48]; |
| 3274 | |
| 3275 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3276 | verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n", |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3277 | tn_buf, off, size); |
| 3278 | return -EACCES; |
| 3279 | } |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3280 | /* Variable offset is prohibited for unprivileged mode for simplicity |
| 3281 | * since it requires corresponding support in Spectre masking for stack |
| 3282 | * ALU. See also retrieve_ptr_limit(). |
| 3283 | */ |
| 3284 | if (!env->bypass_spec_v1 && var_off) { |
| 3285 | char tn_buf[48]; |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3286 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3287 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3288 | verbose(env, "R%d variable offset stack access prohibited for !root, var_off=%s\n", |
| 3289 | ptr_regno, tn_buf); |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3290 | return -EACCES; |
| 3291 | } |
| 3292 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3293 | if (!var_off) { |
| 3294 | off += reg->var_off.value; |
| 3295 | err = check_stack_read_fixed_off(env, state, off, size, |
| 3296 | dst_regno); |
| 3297 | } else { |
| 3298 | /* Variable offset stack reads need more conservative handling |
| 3299 | * than fixed offset ones. Note that dst_regno >= 0 on this |
| 3300 | * branch. |
| 3301 | */ |
| 3302 | err = check_stack_read_var_off(env, ptr_regno, off, size, |
| 3303 | dst_regno); |
| 3304 | } |
| 3305 | return err; |
| 3306 | } |
| 3307 | |
| 3308 | |
| 3309 | /* check_stack_write dispatches to check_stack_write_fixed_off or |
| 3310 | * check_stack_write_var_off. |
| 3311 | * |
| 3312 | * 'ptr_regno' is the register used as a pointer into the stack. |
| 3313 | * 'off' includes 'ptr_regno->off', but not its variable offset (if any). |
| 3314 | * 'value_regno' is the register whose value we're writing to the stack. It can |
| 3315 | * be -1, meaning that we're not writing from a register. |
| 3316 | * |
| 3317 | * The caller must ensure that the offset falls within the maximum stack size. |
| 3318 | */ |
| 3319 | static int check_stack_write(struct bpf_verifier_env *env, |
| 3320 | int ptr_regno, int off, int size, |
| 3321 | int value_regno, int insn_idx) |
| 3322 | { |
| 3323 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); |
| 3324 | struct bpf_func_state *state = func(env, reg); |
| 3325 | int err; |
| 3326 | |
| 3327 | if (tnum_is_const(reg->var_off)) { |
| 3328 | off += reg->var_off.value; |
| 3329 | err = check_stack_write_fixed_off(env, state, off, size, |
| 3330 | value_regno, insn_idx); |
| 3331 | } else { |
| 3332 | /* Variable offset stack reads need more conservative handling |
| 3333 | * than fixed offset ones. |
| 3334 | */ |
| 3335 | err = check_stack_write_var_off(env, state, |
| 3336 | ptr_regno, off, size, |
| 3337 | value_regno, insn_idx); |
| 3338 | } |
| 3339 | return err; |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3340 | } |
| 3341 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3342 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
| 3343 | int off, int size, enum bpf_access_type type) |
| 3344 | { |
| 3345 | struct bpf_reg_state *regs = cur_regs(env); |
| 3346 | struct bpf_map *map = regs[regno].map_ptr; |
| 3347 | u32 cap = bpf_map_flags_to_cap(map); |
| 3348 | |
| 3349 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { |
| 3350 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", |
| 3351 | map->value_size, off, size); |
| 3352 | return -EACCES; |
| 3353 | } |
| 3354 | |
| 3355 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { |
| 3356 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", |
| 3357 | map->value_size, off, size); |
| 3358 | return -EACCES; |
| 3359 | } |
| 3360 | |
| 3361 | return 0; |
| 3362 | } |
| 3363 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3364 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
| 3365 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, |
| 3366 | int off, int size, u32 mem_size, |
| 3367 | bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3368 | { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3369 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
| 3370 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3371 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3372 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) |
| 3373 | return 0; |
| 3374 | |
| 3375 | reg = &cur_regs(env)[regno]; |
| 3376 | switch (reg->type) { |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 3377 | case PTR_TO_MAP_KEY: |
| 3378 | verbose(env, "invalid access to map key, key_size=%d off=%d size=%d\n", |
| 3379 | mem_size, off, size); |
| 3380 | break; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3381 | case PTR_TO_MAP_VALUE: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3382 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3383 | mem_size, off, size); |
| 3384 | break; |
| 3385 | case PTR_TO_PACKET: |
| 3386 | case PTR_TO_PACKET_META: |
| 3387 | case PTR_TO_PACKET_END: |
| 3388 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
| 3389 | off, size, regno, reg->id, off, mem_size); |
| 3390 | break; |
| 3391 | case PTR_TO_MEM: |
| 3392 | default: |
| 3393 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", |
| 3394 | mem_size, off, size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3395 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3396 | |
| 3397 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3398 | } |
| 3399 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3400 | /* check read/write into a memory region with possible variable offset */ |
| 3401 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, |
| 3402 | int off, int size, u32 mem_size, |
| 3403 | bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3404 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3405 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3406 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3407 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 3408 | int err; |
| 3409 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3410 | /* We may have adjusted the register pointing to memory region, so we |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3411 | * need to try adding each of min_value and max_value to off |
| 3412 | * to make sure our theoretical access will be safe. |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame] | 3413 | * |
| 3414 | * The minimum value is only important with signed |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3415 | * comparisons where we can't assume the floor of a |
| 3416 | * value is 0. If we are using signed variables for our |
| 3417 | * index'es we need to make sure that whatever we use |
| 3418 | * will have a set floor within our range. |
| 3419 | */ |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 3420 | if (reg->smin_value < 0 && |
| 3421 | (reg->smin_value == S64_MIN || |
| 3422 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || |
| 3423 | reg->smin_value + off < 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3424 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3425 | regno); |
| 3426 | return -EACCES; |
| 3427 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3428 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
| 3429 | mem_size, zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3430 | if (err) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3431 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3432 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3433 | return err; |
| 3434 | } |
| 3435 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3436 | /* If we haven't set a max value then we need to bail since we can't be |
| 3437 | * sure we won't do bad things. |
| 3438 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3439 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3440 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3441 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3442 | regno); |
| 3443 | return -EACCES; |
| 3444 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3445 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
| 3446 | mem_size, zero_size_allowed); |
| 3447 | if (err) { |
| 3448 | verbose(env, "R%d max value is outside of the allowed memory range\n", |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3449 | regno); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3450 | return err; |
| 3451 | } |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3452 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3453 | return 0; |
| 3454 | } |
| 3455 | |
| 3456 | /* check read/write into a map element with possible variable offset */ |
| 3457 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
| 3458 | int off, int size, bool zero_size_allowed) |
| 3459 | { |
| 3460 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3461 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 3462 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 3463 | struct bpf_map *map = reg->map_ptr; |
| 3464 | int err; |
| 3465 | |
| 3466 | err = check_mem_region_access(env, regno, off, size, map->value_size, |
| 3467 | zero_size_allowed); |
| 3468 | if (err) |
| 3469 | return err; |
| 3470 | |
| 3471 | if (map_value_has_spin_lock(map)) { |
| 3472 | u32 lock = map->spin_lock_off; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3473 | |
| 3474 | /* if any part of struct bpf_spin_lock can be touched by |
| 3475 | * load/store reject this program. |
| 3476 | * To check that [x1, x2) overlaps with [y1, y2) |
| 3477 | * it is sufficient to check x1 < y2 && y1 < x2. |
| 3478 | */ |
| 3479 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && |
| 3480 | lock < reg->umax_value + off + size) { |
| 3481 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); |
| 3482 | return -EACCES; |
| 3483 | } |
| 3484 | } |
Alexei Starovoitov | 6813466 | 2021-07-14 17:54:10 -0700 | [diff] [blame] | 3485 | if (map_value_has_timer(map)) { |
| 3486 | u32 t = map->timer_off; |
| 3487 | |
| 3488 | if (reg->smin_value + off < t + sizeof(struct bpf_timer) && |
| 3489 | t < reg->umax_value + off + size) { |
| 3490 | verbose(env, "bpf_timer cannot be accessed directly by load/store\n"); |
| 3491 | return -EACCES; |
| 3492 | } |
| 3493 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3494 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3495 | } |
| 3496 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3497 | #define MAX_PACKET_OFF 0xffff |
| 3498 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 3499 | static enum bpf_prog_type resolve_prog_type(struct bpf_prog *prog) |
| 3500 | { |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 3501 | return prog->aux->dst_prog ? prog->aux->dst_prog->type : prog->type; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 3502 | } |
| 3503 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3504 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 3505 | const struct bpf_call_arg_meta *meta, |
| 3506 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3507 | { |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 3508 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
| 3509 | |
| 3510 | switch (prog_type) { |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 3511 | /* Program types only with direct read access go here! */ |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 3512 | case BPF_PROG_TYPE_LWT_IN: |
| 3513 | case BPF_PROG_TYPE_LWT_OUT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 3514 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 3515 | case BPF_PROG_TYPE_SK_REUSEPORT: |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 3516 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Daniel Borkmann | d5563d3 | 2018-10-24 22:05:46 +0200 | [diff] [blame] | 3517 | case BPF_PROG_TYPE_CGROUP_SKB: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 3518 | if (t == BPF_WRITE) |
| 3519 | return false; |
Gustavo A. R. Silva | 8731745 | 2020-10-02 18:42:17 -0500 | [diff] [blame] | 3520 | fallthrough; |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 3521 | |
| 3522 | /* Program types with direct read + write access go here! */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3523 | case BPF_PROG_TYPE_SCHED_CLS: |
| 3524 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3525 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 3526 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 3527 | case BPF_PROG_TYPE_SK_SKB: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 3528 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3529 | if (meta) |
| 3530 | return meta->pkt_access; |
| 3531 | |
| 3532 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3533 | return true; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 3534 | |
| 3535 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 3536 | if (t == BPF_WRITE) |
| 3537 | env->seen_direct_write = true; |
| 3538 | |
| 3539 | return true; |
| 3540 | |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3541 | default: |
| 3542 | return false; |
| 3543 | } |
| 3544 | } |
| 3545 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3546 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3547 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3548 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3549 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3550 | struct bpf_reg_state *reg = ®s[regno]; |
| 3551 | int err; |
| 3552 | |
| 3553 | /* We may have added a variable offset to the packet pointer; but any |
| 3554 | * reg->range we have comes after that. We are only checking the fixed |
| 3555 | * offset. |
| 3556 | */ |
| 3557 | |
| 3558 | /* We don't allow negative numbers, because we aren't tracking enough |
| 3559 | * detail to prove they're safe. |
| 3560 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3561 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3562 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3563 | regno); |
| 3564 | return -EACCES; |
| 3565 | } |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 3566 | |
| 3567 | err = reg->range < 0 ? -EINVAL : |
| 3568 | __check_mem_access(env, regno, off, size, reg->range, |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3569 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3570 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3571 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3572 | return err; |
| 3573 | } |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 3574 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3575 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 3576 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 3577 | * otherwise find_good_pkt_pointers would have refused to set range info |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3578 | * that __check_mem_access would have rejected this pkt access. |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 3579 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 3580 | */ |
| 3581 | env->prog->aux->max_pkt_offset = |
| 3582 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 3583 | off + reg->umax_value + size - 1); |
| 3584 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3585 | return err; |
| 3586 | } |
| 3587 | |
| 3588 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3589 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3590 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3591 | struct btf **btf, u32 *btf_id) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3592 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 3593 | struct bpf_insn_access_aux info = { |
| 3594 | .reg_type = *reg_type, |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3595 | .log = &env->log, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 3596 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3597 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 3598 | if (env->ops->is_valid_access && |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 3599 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 3600 | /* A non zero info.ctx_field_size indicates that this field is a |
| 3601 | * candidate for later verifier transformation to load the whole |
| 3602 | * field and then apply a mask when accessed with a narrower |
| 3603 | * access than actual ctx access size. A zero info.ctx_field_size |
| 3604 | * will only allow for whole field access and rejects any other |
| 3605 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3606 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 3607 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3608 | |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 3609 | if (base_type(*reg_type) == PTR_TO_BTF_ID) { |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3610 | *btf = info.btf; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3611 | *btf_id = info.btf_id; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3612 | } else { |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3613 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3614 | } |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 3615 | /* remember the offset of last byte accessed in ctx */ |
| 3616 | if (env->prog->aux->max_ctx_offset < off + size) |
| 3617 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3618 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 3619 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3620 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3621 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3622 | return -EACCES; |
| 3623 | } |
| 3624 | |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 3625 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 3626 | int size) |
| 3627 | { |
| 3628 | if (size < 0 || off < 0 || |
| 3629 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 3630 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 3631 | off, size); |
| 3632 | return -EACCES; |
| 3633 | } |
| 3634 | return 0; |
| 3635 | } |
| 3636 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 3637 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
| 3638 | u32 regno, int off, int size, |
| 3639 | enum bpf_access_type t) |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3640 | { |
| 3641 | struct bpf_reg_state *regs = cur_regs(env); |
| 3642 | struct bpf_reg_state *reg = ®s[regno]; |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 3643 | struct bpf_insn_access_aux info = {}; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3644 | bool valid; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3645 | |
| 3646 | if (reg->smin_value < 0) { |
| 3647 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 3648 | regno); |
| 3649 | return -EACCES; |
| 3650 | } |
| 3651 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3652 | switch (reg->type) { |
| 3653 | case PTR_TO_SOCK_COMMON: |
| 3654 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); |
| 3655 | break; |
| 3656 | case PTR_TO_SOCKET: |
| 3657 | valid = bpf_sock_is_valid_access(off, size, t, &info); |
| 3658 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 3659 | case PTR_TO_TCP_SOCK: |
| 3660 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); |
| 3661 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 3662 | case PTR_TO_XDP_SOCK: |
| 3663 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); |
| 3664 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3665 | default: |
| 3666 | valid = false; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3667 | } |
| 3668 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 3669 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3670 | if (valid) { |
| 3671 | env->insn_aux_data[insn_idx].ctx_field_size = |
| 3672 | info.ctx_field_size; |
| 3673 | return 0; |
| 3674 | } |
| 3675 | |
| 3676 | verbose(env, "R%d invalid %s access off=%d size=%d\n", |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 3677 | regno, reg_type_str(env, reg->type), off, size); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3678 | |
| 3679 | return -EACCES; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3680 | } |
| 3681 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3682 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 3683 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 3684 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3685 | } |
| 3686 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 3687 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 3688 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 3689 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 3690 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3691 | return reg->type == PTR_TO_CTX; |
| 3692 | } |
| 3693 | |
| 3694 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) |
| 3695 | { |
| 3696 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 3697 | |
| 3698 | return type_is_sk_pointer(reg->type); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 3699 | } |
| 3700 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3701 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 3702 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 3703 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3704 | |
| 3705 | return type_is_pkt_pointer(reg->type); |
| 3706 | } |
| 3707 | |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 3708 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 3709 | { |
| 3710 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 3711 | |
| 3712 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 3713 | return reg->type == PTR_TO_FLOW_KEYS; |
| 3714 | } |
| 3715 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3716 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 3717 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3718 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3719 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3720 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 3721 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3722 | |
| 3723 | /* Byte size accesses are always allowed. */ |
| 3724 | if (!strict || size == 1) |
| 3725 | return 0; |
| 3726 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 3727 | /* For platforms that do not have a Kconfig enabling |
| 3728 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 3729 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 3730 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 3731 | * to this code only in strict mode where we want to emulate |
| 3732 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 3733 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 3734 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 3735 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3736 | |
| 3737 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 3738 | if (!tnum_is_aligned(reg_off, size)) { |
| 3739 | char tn_buf[48]; |
| 3740 | |
| 3741 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3742 | verbose(env, |
| 3743 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3744 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3745 | return -EACCES; |
| 3746 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3747 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3748 | return 0; |
| 3749 | } |
| 3750 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3751 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 3752 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3753 | const char *pointer_desc, |
| 3754 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3755 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3756 | struct tnum reg_off; |
| 3757 | |
| 3758 | /* Byte size accesses are always allowed. */ |
| 3759 | if (!strict || size == 1) |
| 3760 | return 0; |
| 3761 | |
| 3762 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 3763 | if (!tnum_is_aligned(reg_off, size)) { |
| 3764 | char tn_buf[48]; |
| 3765 | |
| 3766 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3767 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3768 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3769 | return -EACCES; |
| 3770 | } |
| 3771 | |
| 3772 | return 0; |
| 3773 | } |
| 3774 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 3775 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3776 | const struct bpf_reg_state *reg, int off, |
| 3777 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3778 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3779 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3780 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3781 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3782 | switch (reg->type) { |
| 3783 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3784 | case PTR_TO_PACKET_META: |
| 3785 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 3786 | * right in front, treat it the very same way. |
| 3787 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3788 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 3789 | case PTR_TO_FLOW_KEYS: |
| 3790 | pointer_desc = "flow keys "; |
| 3791 | break; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 3792 | case PTR_TO_MAP_KEY: |
| 3793 | pointer_desc = "key "; |
| 3794 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3795 | case PTR_TO_MAP_VALUE: |
| 3796 | pointer_desc = "value "; |
| 3797 | break; |
| 3798 | case PTR_TO_CTX: |
| 3799 | pointer_desc = "context "; |
| 3800 | break; |
| 3801 | case PTR_TO_STACK: |
| 3802 | pointer_desc = "stack "; |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3803 | /* The stack spill tracking logic in check_stack_write_fixed_off() |
| 3804 | * and check_stack_read_fixed_off() relies on stack accesses being |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 3805 | * aligned. |
| 3806 | */ |
| 3807 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3808 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3809 | case PTR_TO_SOCKET: |
| 3810 | pointer_desc = "sock "; |
| 3811 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3812 | case PTR_TO_SOCK_COMMON: |
| 3813 | pointer_desc = "sock_common "; |
| 3814 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 3815 | case PTR_TO_TCP_SOCK: |
| 3816 | pointer_desc = "tcp_sock "; |
| 3817 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 3818 | case PTR_TO_XDP_SOCK: |
| 3819 | pointer_desc = "xdp_sock "; |
| 3820 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3821 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3822 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3823 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3824 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 3825 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3826 | } |
| 3827 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3828 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 3829 | const struct bpf_func_state *func, |
| 3830 | int off) |
| 3831 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3832 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3833 | |
| 3834 | if (stack >= -off) |
| 3835 | return 0; |
| 3836 | |
| 3837 | /* update known max for given subprogram */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3838 | env->subprog_info[func->subprogno].stack_depth = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3839 | return 0; |
| 3840 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3841 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3842 | /* starting from main bpf function walk all instructions of the function |
| 3843 | * and recursively walk all callees that given function can call. |
| 3844 | * Ignore jump and exit insns. |
| 3845 | * Since recursion is prevented by check_cfg() this algorithm |
| 3846 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 3847 | */ |
| 3848 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 3849 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3850 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 3851 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3852 | struct bpf_insn *insn = env->prog->insnsi; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3853 | bool tail_call_reachable = false; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3854 | int ret_insn[MAX_CALL_FRAMES]; |
| 3855 | int ret_prog[MAX_CALL_FRAMES]; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3856 | int j; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3857 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3858 | process_func: |
Maciej Fijalkowski | 7f6e431 | 2020-09-16 23:10:07 +0200 | [diff] [blame] | 3859 | /* protect against potential stack overflow that might happen when |
| 3860 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack |
| 3861 | * depth for such case down to 256 so that the worst case scenario |
| 3862 | * would result in 8k stack size (32 which is tailcall limit * 256 = |
| 3863 | * 8k). |
| 3864 | * |
| 3865 | * To get the idea what might happen, see an example: |
| 3866 | * func1 -> sub rsp, 128 |
| 3867 | * subfunc1 -> sub rsp, 256 |
| 3868 | * tailcall1 -> add rsp, 256 |
| 3869 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) |
| 3870 | * subfunc2 -> sub rsp, 64 |
| 3871 | * subfunc22 -> sub rsp, 128 |
| 3872 | * tailcall2 -> add rsp, 128 |
| 3873 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) |
| 3874 | * |
| 3875 | * tailcall will unwind the current stack frame but it will not get rid |
| 3876 | * of caller's stack as shown on the example above. |
| 3877 | */ |
| 3878 | if (idx && subprog[idx].has_tail_call && depth >= 256) { |
| 3879 | verbose(env, |
| 3880 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", |
| 3881 | depth); |
| 3882 | return -EACCES; |
| 3883 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3884 | /* round up to 32-bytes, since this is granularity |
| 3885 | * of interpreter stack size |
| 3886 | */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3887 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3888 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3889 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3890 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3891 | return -EACCES; |
| 3892 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3893 | continue_func: |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 3894 | subprog_end = subprog[idx + 1].start; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3895 | for (; i < subprog_end; i++) { |
Alexei Starovoitov | 7ddc80a | 2021-07-14 17:54:15 -0700 | [diff] [blame] | 3896 | int next_insn; |
| 3897 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 3898 | if (!bpf_pseudo_call(insn + i) && !bpf_pseudo_func(insn + i)) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3899 | continue; |
| 3900 | /* remember insn and function to return to */ |
| 3901 | ret_insn[frame] = i + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3902 | ret_prog[frame] = idx; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3903 | |
| 3904 | /* find the callee */ |
Alexei Starovoitov | 7ddc80a | 2021-07-14 17:54:15 -0700 | [diff] [blame] | 3905 | next_insn = i + insn[i].imm + 1; |
| 3906 | idx = find_subprog(env, next_insn); |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3907 | if (idx < 0) { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3908 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
Alexei Starovoitov | 7ddc80a | 2021-07-14 17:54:15 -0700 | [diff] [blame] | 3909 | next_insn); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3910 | return -EFAULT; |
| 3911 | } |
Alexei Starovoitov | 7ddc80a | 2021-07-14 17:54:15 -0700 | [diff] [blame] | 3912 | if (subprog[idx].is_async_cb) { |
| 3913 | if (subprog[idx].has_tail_call) { |
| 3914 | verbose(env, "verifier bug. subprog has tail_call and async cb\n"); |
| 3915 | return -EFAULT; |
| 3916 | } |
| 3917 | /* async callbacks don't increase bpf prog stack size */ |
| 3918 | continue; |
| 3919 | } |
| 3920 | i = next_insn; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3921 | |
| 3922 | if (subprog[idx].has_tail_call) |
| 3923 | tail_call_reachable = true; |
| 3924 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3925 | frame++; |
| 3926 | if (frame >= MAX_CALL_FRAMES) { |
Paul Chaignon | 927cb78 | 2019-03-20 13:58:27 +0100 | [diff] [blame] | 3927 | verbose(env, "the call stack of %d frames is too deep !\n", |
| 3928 | frame); |
| 3929 | return -E2BIG; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3930 | } |
| 3931 | goto process_func; |
| 3932 | } |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3933 | /* if tail call got detected across bpf2bpf calls then mark each of the |
| 3934 | * currently present subprog frames as tail call reachable subprogs; |
| 3935 | * this info will be utilized by JIT so that we will be preserving the |
| 3936 | * tail call counter throughout bpf2bpf calls combined with tailcalls |
| 3937 | */ |
| 3938 | if (tail_call_reachable) |
| 3939 | for (j = 0; j < frame; j++) |
| 3940 | subprog[ret_prog[j]].tail_call_reachable = true; |
Daniel Borkmann | 5dd0a6b | 2021-07-12 22:57:35 +0200 | [diff] [blame] | 3941 | if (subprog[0].tail_call_reachable) |
| 3942 | env->prog->aux->tail_call_reachable = true; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3943 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3944 | /* end of for() loop means the last insn of the 'subprog' |
| 3945 | * was reached. Doesn't matter whether it was JA or EXIT |
| 3946 | */ |
| 3947 | if (frame == 0) |
| 3948 | return 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3949 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3950 | frame--; |
| 3951 | i = ret_insn[frame]; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3952 | idx = ret_prog[frame]; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3953 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3954 | } |
| 3955 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 3956 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 3957 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 3958 | const struct bpf_insn *insn, int idx) |
| 3959 | { |
| 3960 | int start = idx + insn->imm + 1, subprog; |
| 3961 | |
| 3962 | subprog = find_subprog(env, start); |
| 3963 | if (subprog < 0) { |
| 3964 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 3965 | start); |
| 3966 | return -EFAULT; |
| 3967 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3968 | return env->subprog_info[subprog].stack_depth; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 3969 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 3970 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 3971 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 3972 | int check_ctx_reg(struct bpf_verifier_env *env, |
| 3973 | const struct bpf_reg_state *reg, int regno) |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 3974 | { |
| 3975 | /* Access to ctx or passing it to a helper is only allowed in |
| 3976 | * its original, unmodified form. |
| 3977 | */ |
| 3978 | |
| 3979 | if (reg->off) { |
| 3980 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 3981 | regno, reg->off); |
| 3982 | return -EACCES; |
| 3983 | } |
| 3984 | |
| 3985 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 3986 | char tn_buf[48]; |
| 3987 | |
| 3988 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3989 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 3990 | return -EACCES; |
| 3991 | } |
| 3992 | |
| 3993 | return 0; |
| 3994 | } |
| 3995 | |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3996 | static int __check_buffer_access(struct bpf_verifier_env *env, |
| 3997 | const char *buf_info, |
| 3998 | const struct bpf_reg_state *reg, |
| 3999 | int regno, int off, int size) |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 4000 | { |
| 4001 | if (off < 0) { |
| 4002 | verbose(env, |
Yonghong Song | 4fc00b7 | 2020-07-28 15:18:01 -0700 | [diff] [blame] | 4003 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4004 | regno, buf_info, off, size); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 4005 | return -EACCES; |
| 4006 | } |
| 4007 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 4008 | char tn_buf[48]; |
| 4009 | |
| 4010 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 4011 | verbose(env, |
Yonghong Song | 4fc00b7 | 2020-07-28 15:18:01 -0700 | [diff] [blame] | 4012 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 4013 | regno, off, tn_buf); |
| 4014 | return -EACCES; |
| 4015 | } |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4016 | |
| 4017 | return 0; |
| 4018 | } |
| 4019 | |
| 4020 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
| 4021 | const struct bpf_reg_state *reg, |
| 4022 | int regno, int off, int size) |
| 4023 | { |
| 4024 | int err; |
| 4025 | |
| 4026 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); |
| 4027 | if (err) |
| 4028 | return err; |
| 4029 | |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 4030 | if (off + size > env->prog->aux->max_tp_access) |
| 4031 | env->prog->aux->max_tp_access = off + size; |
| 4032 | |
| 4033 | return 0; |
| 4034 | } |
| 4035 | |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4036 | static int check_buffer_access(struct bpf_verifier_env *env, |
| 4037 | const struct bpf_reg_state *reg, |
| 4038 | int regno, int off, int size, |
| 4039 | bool zero_size_allowed, |
| 4040 | const char *buf_info, |
| 4041 | u32 *max_access) |
| 4042 | { |
| 4043 | int err; |
| 4044 | |
| 4045 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); |
| 4046 | if (err) |
| 4047 | return err; |
| 4048 | |
| 4049 | if (off + size > *max_access) |
| 4050 | *max_access = off + size; |
| 4051 | |
| 4052 | return 0; |
| 4053 | } |
| 4054 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 4055 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
| 4056 | static void zext_32_to_64(struct bpf_reg_state *reg) |
| 4057 | { |
| 4058 | reg->var_off = tnum_subreg(reg->var_off); |
| 4059 | __reg_assign_32_into_64(reg); |
| 4060 | } |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 4061 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 4062 | /* truncate register to smaller size (in bytes) |
| 4063 | * must be called with size < BPF_REG_SIZE |
| 4064 | */ |
| 4065 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 4066 | { |
| 4067 | u64 mask; |
| 4068 | |
| 4069 | /* clear high bits in bit representation */ |
| 4070 | reg->var_off = tnum_cast(reg->var_off, size); |
| 4071 | |
| 4072 | /* fix arithmetic bounds */ |
| 4073 | mask = ((u64)1 << (size * 8)) - 1; |
| 4074 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 4075 | reg->umin_value &= mask; |
| 4076 | reg->umax_value &= mask; |
| 4077 | } else { |
| 4078 | reg->umin_value = 0; |
| 4079 | reg->umax_value = mask; |
| 4080 | } |
| 4081 | reg->smin_value = reg->umin_value; |
| 4082 | reg->smax_value = reg->umax_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 4083 | |
| 4084 | /* If size is smaller than 32bit register the 32bit register |
| 4085 | * values are also truncated so we push 64-bit bounds into |
| 4086 | * 32-bit bounds. Above were truncated < 32-bits already. |
| 4087 | */ |
| 4088 | if (size >= 4) |
| 4089 | return; |
| 4090 | __reg_combine_64_into_32(reg); |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 4091 | } |
| 4092 | |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 4093 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
| 4094 | { |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 4095 | /* A map is considered read-only if the following condition are true: |
| 4096 | * |
| 4097 | * 1) BPF program side cannot change any of the map content. The |
| 4098 | * BPF_F_RDONLY_PROG flag is throughout the lifetime of a map |
| 4099 | * and was set at map creation time. |
| 4100 | * 2) The map value(s) have been initialized from user space by a |
| 4101 | * loader and then "frozen", such that no new map update/delete |
| 4102 | * operations from syscall side are possible for the rest of |
| 4103 | * the map's lifetime from that point onwards. |
| 4104 | * 3) Any parallel/pending map update/delete operations from syscall |
| 4105 | * side have been completed. Only after that point, it's safe to |
| 4106 | * assume that map value(s) are immutable. |
| 4107 | */ |
| 4108 | return (map->map_flags & BPF_F_RDONLY_PROG) && |
| 4109 | READ_ONCE(map->frozen) && |
| 4110 | !bpf_map_write_active(map); |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) |
| 4114 | { |
| 4115 | void *ptr; |
| 4116 | u64 addr; |
| 4117 | int err; |
| 4118 | |
| 4119 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 4120 | if (err) |
| 4121 | return err; |
Andrii Nakryiko | 2dedd7d | 2019-10-11 10:20:53 -0700 | [diff] [blame] | 4122 | ptr = (void *)(long)addr + off; |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 4123 | |
| 4124 | switch (size) { |
| 4125 | case sizeof(u8): |
| 4126 | *val = (u64)*(u8 *)ptr; |
| 4127 | break; |
| 4128 | case sizeof(u16): |
| 4129 | *val = (u64)*(u16 *)ptr; |
| 4130 | break; |
| 4131 | case sizeof(u32): |
| 4132 | *val = (u64)*(u32 *)ptr; |
| 4133 | break; |
| 4134 | case sizeof(u64): |
| 4135 | *val = *(u64 *)ptr; |
| 4136 | break; |
| 4137 | default: |
| 4138 | return -EINVAL; |
| 4139 | } |
| 4140 | return 0; |
| 4141 | } |
| 4142 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4143 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
| 4144 | struct bpf_reg_state *regs, |
| 4145 | int regno, int off, int size, |
| 4146 | enum bpf_access_type atype, |
| 4147 | int value_regno) |
| 4148 | { |
| 4149 | struct bpf_reg_state *reg = regs + regno; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4150 | const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); |
| 4151 | const char *tname = btf_name_by_offset(reg->btf, t->name_off); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4152 | u32 btf_id; |
| 4153 | int ret; |
| 4154 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4155 | if (off < 0) { |
| 4156 | verbose(env, |
| 4157 | "R%d is ptr_%s invalid negative access: off=%d\n", |
| 4158 | regno, tname, off); |
| 4159 | return -EACCES; |
| 4160 | } |
| 4161 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 4162 | char tn_buf[48]; |
| 4163 | |
| 4164 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 4165 | verbose(env, |
| 4166 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", |
| 4167 | regno, tname, off, tn_buf); |
| 4168 | return -EACCES; |
| 4169 | } |
| 4170 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 4171 | if (env->ops->btf_struct_access) { |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4172 | ret = env->ops->btf_struct_access(&env->log, reg->btf, t, |
| 4173 | off, size, atype, &btf_id); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 4174 | } else { |
| 4175 | if (atype != BPF_READ) { |
| 4176 | verbose(env, "only read is supported\n"); |
| 4177 | return -EACCES; |
| 4178 | } |
| 4179 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4180 | ret = btf_struct_access(&env->log, reg->btf, t, off, size, |
| 4181 | atype, &btf_id); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 4182 | } |
| 4183 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4184 | if (ret < 0) |
| 4185 | return ret; |
| 4186 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 4187 | if (atype == BPF_READ && value_regno >= 0) |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4188 | mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 4189 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4190 | return 0; |
| 4191 | } |
| 4192 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 4193 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, |
| 4194 | struct bpf_reg_state *regs, |
| 4195 | int regno, int off, int size, |
| 4196 | enum bpf_access_type atype, |
| 4197 | int value_regno) |
| 4198 | { |
| 4199 | struct bpf_reg_state *reg = regs + regno; |
| 4200 | struct bpf_map *map = reg->map_ptr; |
| 4201 | const struct btf_type *t; |
| 4202 | const char *tname; |
| 4203 | u32 btf_id; |
| 4204 | int ret; |
| 4205 | |
| 4206 | if (!btf_vmlinux) { |
| 4207 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); |
| 4208 | return -ENOTSUPP; |
| 4209 | } |
| 4210 | |
| 4211 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { |
| 4212 | verbose(env, "map_ptr access not supported for map type %d\n", |
| 4213 | map->map_type); |
| 4214 | return -ENOTSUPP; |
| 4215 | } |
| 4216 | |
| 4217 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); |
| 4218 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 4219 | |
| 4220 | if (!env->allow_ptr_to_map_access) { |
| 4221 | verbose(env, |
| 4222 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", |
| 4223 | tname); |
| 4224 | return -EPERM; |
| 4225 | } |
| 4226 | |
| 4227 | if (off < 0) { |
| 4228 | verbose(env, "R%d is %s invalid negative access: off=%d\n", |
| 4229 | regno, tname, off); |
| 4230 | return -EACCES; |
| 4231 | } |
| 4232 | |
| 4233 | if (atype != BPF_READ) { |
| 4234 | verbose(env, "only read from %s is supported\n", tname); |
| 4235 | return -EACCES; |
| 4236 | } |
| 4237 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4238 | ret = btf_struct_access(&env->log, btf_vmlinux, t, off, size, atype, &btf_id); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 4239 | if (ret < 0) |
| 4240 | return ret; |
| 4241 | |
| 4242 | if (value_regno >= 0) |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4243 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_vmlinux, btf_id); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 4244 | |
| 4245 | return 0; |
| 4246 | } |
| 4247 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4248 | /* Check that the stack access at the given offset is within bounds. The |
| 4249 | * maximum valid offset is -1. |
| 4250 | * |
| 4251 | * The minimum valid offset is -MAX_BPF_STACK for writes, and |
| 4252 | * -state->allocated_stack for reads. |
| 4253 | */ |
| 4254 | static int check_stack_slot_within_bounds(int off, |
| 4255 | struct bpf_func_state *state, |
| 4256 | enum bpf_access_type t) |
| 4257 | { |
| 4258 | int min_valid_off; |
| 4259 | |
| 4260 | if (t == BPF_WRITE) |
| 4261 | min_valid_off = -MAX_BPF_STACK; |
| 4262 | else |
| 4263 | min_valid_off = -state->allocated_stack; |
| 4264 | |
| 4265 | if (off < min_valid_off || off > -1) |
| 4266 | return -EACCES; |
| 4267 | return 0; |
| 4268 | } |
| 4269 | |
| 4270 | /* Check that the stack access at 'regno + off' falls within the maximum stack |
| 4271 | * bounds. |
| 4272 | * |
| 4273 | * 'off' includes `regno->offset`, but not its dynamic part (if any). |
| 4274 | */ |
| 4275 | static int check_stack_access_within_bounds( |
| 4276 | struct bpf_verifier_env *env, |
| 4277 | int regno, int off, int access_size, |
| 4278 | enum stack_access_src src, enum bpf_access_type type) |
| 4279 | { |
| 4280 | struct bpf_reg_state *regs = cur_regs(env); |
| 4281 | struct bpf_reg_state *reg = regs + regno; |
| 4282 | struct bpf_func_state *state = func(env, reg); |
| 4283 | int min_off, max_off; |
| 4284 | int err; |
| 4285 | char *err_extra; |
| 4286 | |
| 4287 | if (src == ACCESS_HELPER) |
| 4288 | /* We don't know if helpers are reading or writing (or both). */ |
| 4289 | err_extra = " indirect access to"; |
| 4290 | else if (type == BPF_READ) |
| 4291 | err_extra = " read from"; |
| 4292 | else |
| 4293 | err_extra = " write to"; |
| 4294 | |
| 4295 | if (tnum_is_const(reg->var_off)) { |
| 4296 | min_off = reg->var_off.value + off; |
| 4297 | if (access_size > 0) |
| 4298 | max_off = min_off + access_size - 1; |
| 4299 | else |
| 4300 | max_off = min_off; |
| 4301 | } else { |
| 4302 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
| 4303 | reg->smin_value <= -BPF_MAX_VAR_OFF) { |
| 4304 | verbose(env, "invalid unbounded variable-offset%s stack R%d\n", |
| 4305 | err_extra, regno); |
| 4306 | return -EACCES; |
| 4307 | } |
| 4308 | min_off = reg->smin_value + off; |
| 4309 | if (access_size > 0) |
| 4310 | max_off = reg->smax_value + off + access_size - 1; |
| 4311 | else |
| 4312 | max_off = min_off; |
| 4313 | } |
| 4314 | |
| 4315 | err = check_stack_slot_within_bounds(min_off, state, type); |
| 4316 | if (!err) |
| 4317 | err = check_stack_slot_within_bounds(max_off, state, type); |
| 4318 | |
| 4319 | if (err) { |
| 4320 | if (tnum_is_const(reg->var_off)) { |
| 4321 | verbose(env, "invalid%s stack R%d off=%d size=%d\n", |
| 4322 | err_extra, regno, off, access_size); |
| 4323 | } else { |
| 4324 | char tn_buf[48]; |
| 4325 | |
| 4326 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 4327 | verbose(env, "invalid variable-offset%s stack R%d var_off=%s size=%d\n", |
| 4328 | err_extra, regno, tn_buf, access_size); |
| 4329 | } |
| 4330 | } |
| 4331 | return err; |
| 4332 | } |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 4333 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4334 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 4335 | * if t==write, value_regno is a register which value is stored into memory |
| 4336 | * if t==read, value_regno is a register which will receive the value from memory |
| 4337 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 4338 | * if t==read && value_regno==-1, don't care what we read from memory |
| 4339 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 4340 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 4341 | int off, int bpf_size, enum bpf_access_type t, |
| 4342 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4343 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4344 | struct bpf_reg_state *regs = cur_regs(env); |
| 4345 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4346 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4347 | int size, err = 0; |
| 4348 | |
| 4349 | size = bpf_size_to_bytes(bpf_size); |
| 4350 | if (size < 0) |
| 4351 | return size; |
| 4352 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4353 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 4354 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4355 | if (err) |
| 4356 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4357 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4358 | /* for access checks, reg->off is just part of off */ |
| 4359 | off += reg->off; |
| 4360 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 4361 | if (reg->type == PTR_TO_MAP_KEY) { |
| 4362 | if (t == BPF_WRITE) { |
| 4363 | verbose(env, "write to change key R%d not allowed\n", regno); |
| 4364 | return -EACCES; |
| 4365 | } |
| 4366 | |
| 4367 | err = check_mem_region_access(env, regno, off, size, |
| 4368 | reg->map_ptr->key_size, false); |
| 4369 | if (err) |
| 4370 | return err; |
| 4371 | if (value_regno >= 0) |
| 4372 | mark_reg_unknown(env, regs, value_regno); |
| 4373 | } else if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4374 | if (t == BPF_WRITE && value_regno >= 0 && |
| 4375 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4376 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4377 | return -EACCES; |
| 4378 | } |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4379 | err = check_map_access_type(env, regno, off, size, t); |
| 4380 | if (err) |
| 4381 | return err; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 4382 | err = check_map_access(env, regno, off, size, false); |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 4383 | if (!err && t == BPF_READ && value_regno >= 0) { |
| 4384 | struct bpf_map *map = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4385 | |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 4386 | /* if map is read-only, track its contents as scalars */ |
| 4387 | if (tnum_is_const(reg->var_off) && |
| 4388 | bpf_map_is_rdonly(map) && |
| 4389 | map->ops->map_direct_value_addr) { |
| 4390 | int map_off = off + reg->var_off.value; |
| 4391 | u64 val = 0; |
| 4392 | |
| 4393 | err = bpf_map_direct_read(map, map_off, size, |
| 4394 | &val); |
| 4395 | if (err) |
| 4396 | return err; |
| 4397 | |
| 4398 | regs[value_regno].type = SCALAR_VALUE; |
| 4399 | __mark_reg_known(®s[value_regno], val); |
| 4400 | } else { |
| 4401 | mark_reg_unknown(env, regs, value_regno); |
| 4402 | } |
| 4403 | } |
Hao Luo | 34d3a78 | 2021-12-16 16:31:50 -0800 | [diff] [blame] | 4404 | } else if (base_type(reg->type) == PTR_TO_MEM) { |
| 4405 | bool rdonly_mem = type_is_rdonly_mem(reg->type); |
| 4406 | |
| 4407 | if (type_may_be_null(reg->type)) { |
| 4408 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 4409 | reg_type_str(env, reg->type)); |
| 4410 | return -EACCES; |
| 4411 | } |
| 4412 | |
| 4413 | if (t == BPF_WRITE && rdonly_mem) { |
| 4414 | verbose(env, "R%d cannot write into %s\n", |
| 4415 | regno, reg_type_str(env, reg->type)); |
| 4416 | return -EACCES; |
| 4417 | } |
| 4418 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4419 | if (t == BPF_WRITE && value_regno >= 0 && |
| 4420 | is_pointer_value(env, value_regno)) { |
| 4421 | verbose(env, "R%d leaks addr into mem\n", value_regno); |
| 4422 | return -EACCES; |
| 4423 | } |
Hao Luo | 34d3a78 | 2021-12-16 16:31:50 -0800 | [diff] [blame] | 4424 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4425 | err = check_mem_region_access(env, regno, off, size, |
| 4426 | reg->mem_size, false); |
Hao Luo | 34d3a78 | 2021-12-16 16:31:50 -0800 | [diff] [blame] | 4427 | if (!err && value_regno >= 0 && (t == BPF_READ || rdonly_mem)) |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4428 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4429 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4430 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4431 | struct btf *btf = NULL; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4432 | u32 btf_id = 0; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 4433 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4434 | if (t == BPF_WRITE && value_regno >= 0 && |
| 4435 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4436 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4437 | return -EACCES; |
| 4438 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4439 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 4440 | err = check_ctx_reg(env, reg, regno); |
| 4441 | if (err < 0) |
| 4442 | return err; |
| 4443 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4444 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, &btf_id); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4445 | if (err) |
| 4446 | verbose_linfo(env, insn_idx, "; "); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4447 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4448 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4449 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 4450 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4451 | */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4452 | if (reg_type == SCALAR_VALUE) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4453 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4454 | } else { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4455 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4456 | value_regno); |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 4457 | if (type_may_be_null(reg_type)) |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4458 | regs[value_regno].id = ++env->id_gen; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 4459 | /* A load of ctx field could have different |
| 4460 | * actual load size with the one encoded in the |
| 4461 | * insn. When the dst is PTR, it is for sure not |
| 4462 | * a sub-register. |
| 4463 | */ |
| 4464 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 4465 | if (base_type(reg_type) == PTR_TO_BTF_ID) { |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4466 | regs[value_regno].btf = btf; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4467 | regs[value_regno].btf_id = btf_id; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4468 | } |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4469 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4470 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4471 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4472 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4473 | } else if (reg->type == PTR_TO_STACK) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4474 | /* Basic bounds checks. */ |
| 4475 | err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t); |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 4476 | if (err) |
| 4477 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 4478 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4479 | state = func(env, reg); |
| 4480 | err = update_stack_depth(env, state, off); |
| 4481 | if (err) |
| 4482 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 4483 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4484 | if (t == BPF_READ) |
| 4485 | err = check_stack_read(env, regno, off, size, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4486 | value_regno); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4487 | else |
| 4488 | err = check_stack_write(env, regno, off, size, |
| 4489 | value_regno, insn_idx); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4490 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 4491 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4492 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4493 | return -EACCES; |
| 4494 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 4495 | if (t == BPF_WRITE && value_regno >= 0 && |
| 4496 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4497 | verbose(env, "R%d leaks addr into packet\n", |
| 4498 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 4499 | return -EACCES; |
| 4500 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 4501 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4502 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4503 | mark_reg_unknown(env, regs, value_regno); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 4504 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 4505 | if (t == BPF_WRITE && value_regno >= 0 && |
| 4506 | is_pointer_value(env, value_regno)) { |
| 4507 | verbose(env, "R%d leaks addr into flow keys\n", |
| 4508 | value_regno); |
| 4509 | return -EACCES; |
| 4510 | } |
| 4511 | |
| 4512 | err = check_flow_keys_access(env, off, size); |
| 4513 | if (!err && t == BPF_READ && value_regno >= 0) |
| 4514 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4515 | } else if (type_is_sk_pointer(reg->type)) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 4516 | if (t == BPF_WRITE) { |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4517 | verbose(env, "R%d cannot write into %s\n", |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 4518 | regno, reg_type_str(env, reg->type)); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 4519 | return -EACCES; |
| 4520 | } |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 4521 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 4522 | if (!err && value_regno >= 0) |
| 4523 | mark_reg_unknown(env, regs, value_regno); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 4524 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
| 4525 | err = check_tp_buffer_access(env, reg, regno, off, size); |
| 4526 | if (!err && t == BPF_READ && value_regno >= 0) |
| 4527 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4528 | } else if (reg->type == PTR_TO_BTF_ID) { |
| 4529 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
| 4530 | value_regno); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 4531 | } else if (reg->type == CONST_PTR_TO_MAP) { |
| 4532 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, |
| 4533 | value_regno); |
Hao Luo | 20b2aff | 2021-12-16 16:31:48 -0800 | [diff] [blame] | 4534 | } else if (base_type(reg->type) == PTR_TO_BUF) { |
| 4535 | bool rdonly_mem = type_is_rdonly_mem(reg->type); |
| 4536 | const char *buf_info; |
| 4537 | u32 *max_access; |
| 4538 | |
| 4539 | if (rdonly_mem) { |
| 4540 | if (t == BPF_WRITE) { |
| 4541 | verbose(env, "R%d cannot write into %s\n", |
| 4542 | regno, reg_type_str(env, reg->type)); |
| 4543 | return -EACCES; |
| 4544 | } |
| 4545 | buf_info = "rdonly"; |
| 4546 | max_access = &env->prog->aux->max_rdonly_access; |
| 4547 | } else { |
| 4548 | buf_info = "rdwr"; |
| 4549 | max_access = &env->prog->aux->max_rdwr_access; |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4550 | } |
Hao Luo | 20b2aff | 2021-12-16 16:31:48 -0800 | [diff] [blame] | 4551 | |
Colin Ian King | f6dfbe31 | 2020-07-27 18:54:11 +0100 | [diff] [blame] | 4552 | err = check_buffer_access(env, reg, regno, off, size, false, |
Hao Luo | 20b2aff | 2021-12-16 16:31:48 -0800 | [diff] [blame] | 4553 | buf_info, max_access); |
| 4554 | |
| 4555 | if (!err && value_regno >= 0 && (rdonly_mem || t == BPF_READ)) |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4556 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4557 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4558 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 4559 | reg_type_str(env, reg->type)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4560 | return -EACCES; |
| 4561 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4562 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4563 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4564 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4565 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 4566 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4567 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4568 | return err; |
| 4569 | } |
| 4570 | |
Brendan Jackman | 91c960b | 2021-01-14 18:17:44 +0000 | [diff] [blame] | 4571 | static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4572 | { |
Brendan Jackman | 5ffa255 | 2021-01-14 18:17:47 +0000 | [diff] [blame] | 4573 | int load_reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4574 | int err; |
| 4575 | |
Brendan Jackman | 5ca419f | 2021-01-14 18:17:46 +0000 | [diff] [blame] | 4576 | switch (insn->imm) { |
| 4577 | case BPF_ADD: |
| 4578 | case BPF_ADD | BPF_FETCH: |
Brendan Jackman | 981f94c | 2021-01-14 18:17:49 +0000 | [diff] [blame] | 4579 | case BPF_AND: |
| 4580 | case BPF_AND | BPF_FETCH: |
| 4581 | case BPF_OR: |
| 4582 | case BPF_OR | BPF_FETCH: |
| 4583 | case BPF_XOR: |
| 4584 | case BPF_XOR | BPF_FETCH: |
Brendan Jackman | 5ffa255 | 2021-01-14 18:17:47 +0000 | [diff] [blame] | 4585 | case BPF_XCHG: |
| 4586 | case BPF_CMPXCHG: |
Brendan Jackman | 5ca419f | 2021-01-14 18:17:46 +0000 | [diff] [blame] | 4587 | break; |
| 4588 | default: |
Brendan Jackman | 91c960b | 2021-01-14 18:17:44 +0000 | [diff] [blame] | 4589 | verbose(env, "BPF_ATOMIC uses invalid atomic opcode %02x\n", insn->imm); |
| 4590 | return -EINVAL; |
| 4591 | } |
| 4592 | |
| 4593 | if (BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) { |
| 4594 | verbose(env, "invalid atomic operand size\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4595 | return -EINVAL; |
| 4596 | } |
| 4597 | |
| 4598 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4599 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4600 | if (err) |
| 4601 | return err; |
| 4602 | |
| 4603 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4604 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4605 | if (err) |
| 4606 | return err; |
| 4607 | |
Brendan Jackman | 5ffa255 | 2021-01-14 18:17:47 +0000 | [diff] [blame] | 4608 | if (insn->imm == BPF_CMPXCHG) { |
| 4609 | /* Check comparison of R0 with memory location */ |
Daniel Borkmann | a82fe085 | 2021-12-07 11:02:02 +0000 | [diff] [blame] | 4610 | const u32 aux_reg = BPF_REG_0; |
| 4611 | |
| 4612 | err = check_reg_arg(env, aux_reg, SRC_OP); |
Brendan Jackman | 5ffa255 | 2021-01-14 18:17:47 +0000 | [diff] [blame] | 4613 | if (err) |
| 4614 | return err; |
Daniel Borkmann | a82fe085 | 2021-12-07 11:02:02 +0000 | [diff] [blame] | 4615 | |
| 4616 | if (is_pointer_value(env, aux_reg)) { |
| 4617 | verbose(env, "R%d leaks addr into mem\n", aux_reg); |
| 4618 | return -EACCES; |
| 4619 | } |
Brendan Jackman | 5ffa255 | 2021-01-14 18:17:47 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 4622 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4623 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 4624 | return -EACCES; |
| 4625 | } |
| 4626 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 4627 | if (is_ctx_reg(env, insn->dst_reg) || |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 4628 | is_pkt_reg(env, insn->dst_reg) || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4629 | is_flow_key_reg(env, insn->dst_reg) || |
| 4630 | is_sk_reg(env, insn->dst_reg)) { |
Brendan Jackman | 91c960b | 2021-01-14 18:17:44 +0000 | [diff] [blame] | 4631 | verbose(env, "BPF_ATOMIC stores into R%d %s is not allowed\n", |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 4632 | insn->dst_reg, |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 4633 | reg_type_str(env, reg_state(env, insn->dst_reg)->type)); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 4634 | return -EACCES; |
| 4635 | } |
| 4636 | |
Brendan Jackman | 37086bf | 2021-02-02 13:50:02 +0000 | [diff] [blame] | 4637 | if (insn->imm & BPF_FETCH) { |
| 4638 | if (insn->imm == BPF_CMPXCHG) |
| 4639 | load_reg = BPF_REG_0; |
| 4640 | else |
| 4641 | load_reg = insn->src_reg; |
| 4642 | |
| 4643 | /* check and record load of old value */ |
| 4644 | err = check_reg_arg(env, load_reg, DST_OP); |
| 4645 | if (err) |
| 4646 | return err; |
| 4647 | } else { |
| 4648 | /* This instruction accesses a memory location but doesn't |
| 4649 | * actually load it into a register. |
| 4650 | */ |
| 4651 | load_reg = -1; |
| 4652 | } |
| 4653 | |
Daniel Borkmann | 7d3baf0 | 2021-12-07 12:51:56 +0000 | [diff] [blame] | 4654 | /* Check whether we can read the memory, with second call for fetch |
| 4655 | * case to simulate the register fill. |
| 4656 | */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4657 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | 7d3baf0 | 2021-12-07 12:51:56 +0000 | [diff] [blame] | 4658 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
| 4659 | if (!err && load_reg >= 0) |
| 4660 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
| 4661 | BPF_SIZE(insn->code), BPF_READ, load_reg, |
| 4662 | true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4663 | if (err) |
| 4664 | return err; |
| 4665 | |
Daniel Borkmann | 7d3baf0 | 2021-12-07 12:51:56 +0000 | [diff] [blame] | 4666 | /* Check whether we can write into the same memory. */ |
Brendan Jackman | 5ca419f | 2021-01-14 18:17:46 +0000 | [diff] [blame] | 4667 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
| 4668 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
| 4669 | if (err) |
| 4670 | return err; |
| 4671 | |
Brendan Jackman | 5ca419f | 2021-01-14 18:17:46 +0000 | [diff] [blame] | 4672 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4673 | } |
| 4674 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4675 | /* When register 'regno' is used to read the stack (either directly or through |
| 4676 | * a helper function) make sure that it's within stack boundary and, depending |
| 4677 | * on the access type, that all elements of the stack are initialized. |
| 4678 | * |
| 4679 | * 'off' includes 'regno->off', but not its dynamic part (if any). |
| 4680 | * |
| 4681 | * All registers that have been spilled on the stack in the slots within the |
| 4682 | * read offsets are marked as read. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4683 | */ |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4684 | static int check_stack_range_initialized( |
| 4685 | struct bpf_verifier_env *env, int regno, int off, |
| 4686 | int access_size, bool zero_size_allowed, |
| 4687 | enum stack_access_src type, struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4688 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 4689 | struct bpf_reg_state *reg = reg_state(env, regno); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4690 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 4691 | int err, min_off, max_off, i, j, slot, spi; |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4692 | char *err_extra = type == ACCESS_HELPER ? " indirect" : ""; |
| 4693 | enum bpf_access_type bounds_check_type; |
| 4694 | /* Some accesses can write anything into the stack, others are |
| 4695 | * read-only. |
| 4696 | */ |
| 4697 | bool clobber = false; |
| 4698 | |
| 4699 | if (access_size == 0 && !zero_size_allowed) { |
| 4700 | verbose(env, "invalid zero-sized read\n"); |
| 4701 | return -EACCES; |
| 4702 | } |
| 4703 | |
| 4704 | if (type == ACCESS_HELPER) { |
| 4705 | /* The bounds checks for writes are more permissive than for |
| 4706 | * reads. However, if raw_mode is not set, we'll do extra |
| 4707 | * checks below. |
| 4708 | */ |
| 4709 | bounds_check_type = BPF_WRITE; |
| 4710 | clobber = true; |
| 4711 | } else { |
| 4712 | bounds_check_type = BPF_READ; |
| 4713 | } |
| 4714 | err = check_stack_access_within_bounds(env, regno, off, access_size, |
| 4715 | type, bounds_check_type); |
| 4716 | if (err) |
| 4717 | return err; |
| 4718 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4719 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4720 | if (tnum_is_const(reg->var_off)) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4721 | min_off = max_off = reg->var_off.value + off; |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4722 | } else { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 4723 | /* Variable offset is prohibited for unprivileged mode for |
| 4724 | * simplicity since it requires corresponding support in |
| 4725 | * Spectre masking for stack ALU. |
| 4726 | * See also retrieve_ptr_limit(). |
| 4727 | */ |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 4728 | if (!env->bypass_spec_v1) { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 4729 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4730 | |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 4731 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4732 | verbose(env, "R%d%s variable offset stack access prohibited for !root, var_off=%s\n", |
| 4733 | regno, err_extra, tn_buf); |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 4734 | return -EACCES; |
| 4735 | } |
Andrey Ignatov | f2bcd05 | 2019-04-03 23:22:37 -0700 | [diff] [blame] | 4736 | /* Only initialized buffer on stack is allowed to be accessed |
| 4737 | * with variable offset. With uninitialized buffer it's hard to |
| 4738 | * guarantee that whole memory is marked as initialized on |
| 4739 | * helper return since specific bounds are unknown what may |
| 4740 | * cause uninitialized stack leaking. |
| 4741 | */ |
| 4742 | if (meta && meta->raw_mode) |
| 4743 | meta = NULL; |
| 4744 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4745 | min_off = reg->smin_value + off; |
| 4746 | max_off = reg->smax_value + off; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4747 | } |
| 4748 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4749 | if (meta && meta->raw_mode) { |
| 4750 | meta->access_size = access_size; |
| 4751 | meta->regno = regno; |
| 4752 | return 0; |
| 4753 | } |
| 4754 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4755 | for (i = min_off; i < max_off + access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4756 | u8 *stype; |
| 4757 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4758 | slot = -i - 1; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4759 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4760 | if (state->allocated_stack <= slot) |
| 4761 | goto err; |
| 4762 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 4763 | if (*stype == STACK_MISC) |
| 4764 | goto mark; |
| 4765 | if (*stype == STACK_ZERO) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4766 | if (clobber) { |
| 4767 | /* helper can write anything into the stack */ |
| 4768 | *stype = STACK_MISC; |
| 4769 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4770 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4771 | } |
Yonghong Song | 1d68f22 | 2020-05-09 10:59:15 -0700 | [diff] [blame] | 4772 | |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 4773 | if (is_spilled_reg(&state->stack[spi]) && |
Yonghong Song | 1d68f22 | 2020-05-09 10:59:15 -0700 | [diff] [blame] | 4774 | state->stack[spi].spilled_ptr.type == PTR_TO_BTF_ID) |
| 4775 | goto mark; |
| 4776 | |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 4777 | if (is_spilled_reg(&state->stack[spi]) && |
Yonghong Song | cd17d38 | 2020-12-09 17:33:49 -0800 | [diff] [blame] | 4778 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
| 4779 | env->allow_ptr_leaks)) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4780 | if (clobber) { |
| 4781 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
| 4782 | for (j = 0; j < BPF_REG_SIZE; j++) |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 4783 | scrub_spilled_slot(&state->stack[spi].slot_type[j]); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4784 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 4785 | goto mark; |
| 4786 | } |
| 4787 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4788 | err: |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4789 | if (tnum_is_const(reg->var_off)) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4790 | verbose(env, "invalid%s read from stack R%d off %d+%d size %d\n", |
| 4791 | err_extra, regno, min_off, i - min_off, access_size); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4792 | } else { |
| 4793 | char tn_buf[48]; |
| 4794 | |
| 4795 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4796 | verbose(env, "invalid%s read from stack R%d var_off %s+%d size %d\n", |
| 4797 | err_extra, regno, tn_buf, i - min_off, access_size); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4798 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4799 | return -EACCES; |
| 4800 | mark: |
| 4801 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 4802 | * the whole slot to be marked as 'read' |
| 4803 | */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 4804 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 4805 | state->stack[spi].spilled_ptr.parent, |
| 4806 | REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4807 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4808 | return update_stack_depth(env, state, min_off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4809 | } |
| 4810 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4811 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 4812 | int access_size, bool zero_size_allowed, |
| 4813 | struct bpf_call_arg_meta *meta) |
| 4814 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4815 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Hao Luo | 20b2aff | 2021-12-16 16:31:48 -0800 | [diff] [blame] | 4816 | const char *buf_info; |
| 4817 | u32 *max_access; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4818 | |
Hao Luo | 20b2aff | 2021-12-16 16:31:48 -0800 | [diff] [blame] | 4819 | switch (base_type(reg->type)) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4820 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4821 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 4822 | return check_packet_access(env, regno, reg->off, access_size, |
| 4823 | zero_size_allowed); |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 4824 | case PTR_TO_MAP_KEY: |
| 4825 | return check_mem_region_access(env, regno, reg->off, access_size, |
| 4826 | reg->map_ptr->key_size, false); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4827 | case PTR_TO_MAP_VALUE: |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4828 | if (check_map_access_type(env, regno, reg->off, access_size, |
| 4829 | meta && meta->raw_mode ? BPF_WRITE : |
| 4830 | BPF_READ)) |
| 4831 | return -EACCES; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 4832 | return check_map_access(env, regno, reg->off, access_size, |
| 4833 | zero_size_allowed); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4834 | case PTR_TO_MEM: |
| 4835 | return check_mem_region_access(env, regno, reg->off, |
| 4836 | access_size, reg->mem_size, |
| 4837 | zero_size_allowed); |
Hao Luo | 20b2aff | 2021-12-16 16:31:48 -0800 | [diff] [blame] | 4838 | case PTR_TO_BUF: |
| 4839 | if (type_is_rdonly_mem(reg->type)) { |
| 4840 | if (meta && meta->raw_mode) |
| 4841 | return -EACCES; |
| 4842 | |
| 4843 | buf_info = "rdonly"; |
| 4844 | max_access = &env->prog->aux->max_rdonly_access; |
| 4845 | } else { |
| 4846 | buf_info = "rdwr"; |
| 4847 | max_access = &env->prog->aux->max_rdwr_access; |
| 4848 | } |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4849 | return check_buffer_access(env, reg, regno, reg->off, |
| 4850 | access_size, zero_size_allowed, |
Hao Luo | 20b2aff | 2021-12-16 16:31:48 -0800 | [diff] [blame] | 4851 | buf_info, max_access); |
Lorenz Bauer | 0d004c02 | 2020-09-21 13:12:18 +0100 | [diff] [blame] | 4852 | case PTR_TO_STACK: |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4853 | return check_stack_range_initialized( |
| 4854 | env, |
| 4855 | regno, reg->off, access_size, |
| 4856 | zero_size_allowed, ACCESS_HELPER, meta); |
Lorenz Bauer | 0d004c02 | 2020-09-21 13:12:18 +0100 | [diff] [blame] | 4857 | default: /* scalar_value or invalid ptr */ |
| 4858 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
| 4859 | if (zero_size_allowed && access_size == 0 && |
| 4860 | register_is_null(reg)) |
| 4861 | return 0; |
| 4862 | |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 4863 | verbose(env, "R%d type=%s ", regno, |
| 4864 | reg_type_str(env, reg->type)); |
| 4865 | verbose(env, "expected=%s\n", reg_type_str(env, PTR_TO_STACK)); |
Lorenz Bauer | 0d004c02 | 2020-09-21 13:12:18 +0100 | [diff] [blame] | 4866 | return -EACCES; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4867 | } |
| 4868 | } |
| 4869 | |
Dmitrii Banshchikov | e5069b9c | 2021-02-13 00:56:41 +0400 | [diff] [blame] | 4870 | int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
| 4871 | u32 regno, u32 mem_size) |
| 4872 | { |
| 4873 | if (register_is_null(reg)) |
| 4874 | return 0; |
| 4875 | |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 4876 | if (type_may_be_null(reg->type)) { |
Dmitrii Banshchikov | e5069b9c | 2021-02-13 00:56:41 +0400 | [diff] [blame] | 4877 | /* Assuming that the register contains a value check if the memory |
| 4878 | * access is safe. Temporarily save and restore the register's state as |
| 4879 | * the conversion shouldn't be visible to a caller. |
| 4880 | */ |
| 4881 | const struct bpf_reg_state saved_reg = *reg; |
| 4882 | int rv; |
| 4883 | |
| 4884 | mark_ptr_not_null_reg(reg); |
| 4885 | rv = check_helper_mem_access(env, regno, mem_size, true, NULL); |
| 4886 | *reg = saved_reg; |
| 4887 | return rv; |
| 4888 | } |
| 4889 | |
| 4890 | return check_helper_mem_access(env, regno, mem_size, true, NULL); |
| 4891 | } |
| 4892 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 4893 | /* Implementation details: |
| 4894 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL |
| 4895 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
| 4896 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after |
| 4897 | * value_or_null->value transition, since the verifier only cares about |
| 4898 | * the range of access to valid map value pointer and doesn't care about actual |
| 4899 | * address of the map element. |
| 4900 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
| 4901 | * reg->id > 0 after value_or_null->value transition. By doing so |
| 4902 | * two bpf_map_lookups will be considered two different pointers that |
| 4903 | * point to different bpf_spin_locks. |
| 4904 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
| 4905 | * dead-locks. |
| 4906 | * Since only one bpf_spin_lock is allowed the checks are simpler than |
| 4907 | * reg_is_refcounted() logic. The verifier needs to remember only |
| 4908 | * one spin_lock instead of array of acquired_refs. |
| 4909 | * cur_state->active_spin_lock remembers which map value element got locked |
| 4910 | * and clears it after bpf_spin_unlock. |
| 4911 | */ |
| 4912 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, |
| 4913 | bool is_lock) |
| 4914 | { |
| 4915 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 4916 | struct bpf_verifier_state *cur = env->cur_state; |
| 4917 | bool is_const = tnum_is_const(reg->var_off); |
| 4918 | struct bpf_map *map = reg->map_ptr; |
| 4919 | u64 val = reg->var_off.value; |
| 4920 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 4921 | if (!is_const) { |
| 4922 | verbose(env, |
| 4923 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", |
| 4924 | regno); |
| 4925 | return -EINVAL; |
| 4926 | } |
| 4927 | if (!map->btf) { |
| 4928 | verbose(env, |
| 4929 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", |
| 4930 | map->name); |
| 4931 | return -EINVAL; |
| 4932 | } |
| 4933 | if (!map_value_has_spin_lock(map)) { |
| 4934 | if (map->spin_lock_off == -E2BIG) |
| 4935 | verbose(env, |
| 4936 | "map '%s' has more than one 'struct bpf_spin_lock'\n", |
| 4937 | map->name); |
| 4938 | else if (map->spin_lock_off == -ENOENT) |
| 4939 | verbose(env, |
| 4940 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", |
| 4941 | map->name); |
| 4942 | else |
| 4943 | verbose(env, |
| 4944 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", |
| 4945 | map->name); |
| 4946 | return -EINVAL; |
| 4947 | } |
| 4948 | if (map->spin_lock_off != val + reg->off) { |
| 4949 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", |
| 4950 | val + reg->off); |
| 4951 | return -EINVAL; |
| 4952 | } |
| 4953 | if (is_lock) { |
| 4954 | if (cur->active_spin_lock) { |
| 4955 | verbose(env, |
| 4956 | "Locking two bpf_spin_locks are not allowed\n"); |
| 4957 | return -EINVAL; |
| 4958 | } |
| 4959 | cur->active_spin_lock = reg->id; |
| 4960 | } else { |
| 4961 | if (!cur->active_spin_lock) { |
| 4962 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
| 4963 | return -EINVAL; |
| 4964 | } |
| 4965 | if (cur->active_spin_lock != reg->id) { |
| 4966 | verbose(env, "bpf_spin_unlock of different lock\n"); |
| 4967 | return -EINVAL; |
| 4968 | } |
| 4969 | cur->active_spin_lock = 0; |
| 4970 | } |
| 4971 | return 0; |
| 4972 | } |
| 4973 | |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 4974 | static int process_timer_func(struct bpf_verifier_env *env, int regno, |
| 4975 | struct bpf_call_arg_meta *meta) |
| 4976 | { |
| 4977 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 4978 | bool is_const = tnum_is_const(reg->var_off); |
| 4979 | struct bpf_map *map = reg->map_ptr; |
| 4980 | u64 val = reg->var_off.value; |
| 4981 | |
| 4982 | if (!is_const) { |
| 4983 | verbose(env, |
| 4984 | "R%d doesn't have constant offset. bpf_timer has to be at the constant offset\n", |
| 4985 | regno); |
| 4986 | return -EINVAL; |
| 4987 | } |
| 4988 | if (!map->btf) { |
| 4989 | verbose(env, "map '%s' has to have BTF in order to use bpf_timer\n", |
| 4990 | map->name); |
| 4991 | return -EINVAL; |
| 4992 | } |
Alexei Starovoitov | 6813466 | 2021-07-14 17:54:10 -0700 | [diff] [blame] | 4993 | if (!map_value_has_timer(map)) { |
| 4994 | if (map->timer_off == -E2BIG) |
| 4995 | verbose(env, |
| 4996 | "map '%s' has more than one 'struct bpf_timer'\n", |
| 4997 | map->name); |
| 4998 | else if (map->timer_off == -ENOENT) |
| 4999 | verbose(env, |
| 5000 | "map '%s' doesn't have 'struct bpf_timer'\n", |
| 5001 | map->name); |
| 5002 | else |
| 5003 | verbose(env, |
| 5004 | "map '%s' is not a struct type or bpf_timer is mangled\n", |
| 5005 | map->name); |
| 5006 | return -EINVAL; |
| 5007 | } |
| 5008 | if (map->timer_off != val + reg->off) { |
| 5009 | verbose(env, "off %lld doesn't point to 'struct bpf_timer' that is at %d\n", |
| 5010 | val + reg->off, map->timer_off); |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 5011 | return -EINVAL; |
| 5012 | } |
| 5013 | if (meta->map_ptr) { |
| 5014 | verbose(env, "verifier bug. Two map pointers in a timer helper\n"); |
| 5015 | return -EFAULT; |
| 5016 | } |
Alexei Starovoitov | 3e8ce29 | 2021-07-14 17:54:11 -0700 | [diff] [blame] | 5017 | meta->map_uid = reg->map_uid; |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 5018 | meta->map_ptr = map; |
| 5019 | return 0; |
| 5020 | } |
| 5021 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 5022 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 5023 | { |
Hao Luo | 48946bd | 2021-12-16 16:31:45 -0800 | [diff] [blame] | 5024 | return base_type(type) == ARG_PTR_TO_MEM || |
| 5025 | base_type(type) == ARG_PTR_TO_UNINIT_MEM; |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 5026 | } |
| 5027 | |
| 5028 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 5029 | { |
| 5030 | return type == ARG_CONST_SIZE || |
| 5031 | type == ARG_CONST_SIZE_OR_ZERO; |
| 5032 | } |
| 5033 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5034 | static bool arg_type_is_alloc_size(enum bpf_arg_type type) |
| 5035 | { |
| 5036 | return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; |
| 5037 | } |
| 5038 | |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 5039 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
| 5040 | { |
| 5041 | return type == ARG_PTR_TO_INT || |
| 5042 | type == ARG_PTR_TO_LONG; |
| 5043 | } |
| 5044 | |
| 5045 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
| 5046 | { |
| 5047 | if (type == ARG_PTR_TO_INT) |
| 5048 | return sizeof(u32); |
| 5049 | else if (type == ARG_PTR_TO_LONG) |
| 5050 | return sizeof(u64); |
| 5051 | |
| 5052 | return -EINVAL; |
| 5053 | } |
| 5054 | |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 5055 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
| 5056 | const struct bpf_call_arg_meta *meta, |
| 5057 | enum bpf_arg_type *arg_type) |
| 5058 | { |
| 5059 | if (!meta->map_ptr) { |
| 5060 | /* kernel subsystem misconfigured verifier */ |
| 5061 | verbose(env, "invalid map_ptr to access map->type\n"); |
| 5062 | return -EACCES; |
| 5063 | } |
| 5064 | |
| 5065 | switch (meta->map_ptr->map_type) { |
| 5066 | case BPF_MAP_TYPE_SOCKMAP: |
| 5067 | case BPF_MAP_TYPE_SOCKHASH: |
| 5068 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { |
Lorenz Bauer | 6550f2d | 2020-09-28 10:08:02 +0100 | [diff] [blame] | 5069 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 5070 | } else { |
| 5071 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); |
| 5072 | return -EINVAL; |
| 5073 | } |
| 5074 | break; |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 5075 | case BPF_MAP_TYPE_BLOOM_FILTER: |
| 5076 | if (meta->func_id == BPF_FUNC_map_peek_elem) |
| 5077 | *arg_type = ARG_PTR_TO_MAP_VALUE; |
| 5078 | break; |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 5079 | default: |
| 5080 | break; |
| 5081 | } |
| 5082 | return 0; |
| 5083 | } |
| 5084 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5085 | struct bpf_reg_types { |
| 5086 | const enum bpf_reg_type types[10]; |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5087 | u32 *btf_id; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5088 | }; |
| 5089 | |
| 5090 | static const struct bpf_reg_types map_key_value_types = { |
| 5091 | .types = { |
| 5092 | PTR_TO_STACK, |
| 5093 | PTR_TO_PACKET, |
| 5094 | PTR_TO_PACKET_META, |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 5095 | PTR_TO_MAP_KEY, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5096 | PTR_TO_MAP_VALUE, |
| 5097 | }, |
| 5098 | }; |
| 5099 | |
| 5100 | static const struct bpf_reg_types sock_types = { |
| 5101 | .types = { |
| 5102 | PTR_TO_SOCK_COMMON, |
| 5103 | PTR_TO_SOCKET, |
| 5104 | PTR_TO_TCP_SOCK, |
| 5105 | PTR_TO_XDP_SOCK, |
| 5106 | }, |
| 5107 | }; |
| 5108 | |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 5109 | #ifdef CONFIG_NET |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5110 | static const struct bpf_reg_types btf_id_sock_common_types = { |
| 5111 | .types = { |
| 5112 | PTR_TO_SOCK_COMMON, |
| 5113 | PTR_TO_SOCKET, |
| 5114 | PTR_TO_TCP_SOCK, |
| 5115 | PTR_TO_XDP_SOCK, |
| 5116 | PTR_TO_BTF_ID, |
| 5117 | }, |
| 5118 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], |
| 5119 | }; |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 5120 | #endif |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5121 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5122 | static const struct bpf_reg_types mem_types = { |
| 5123 | .types = { |
| 5124 | PTR_TO_STACK, |
| 5125 | PTR_TO_PACKET, |
| 5126 | PTR_TO_PACKET_META, |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 5127 | PTR_TO_MAP_KEY, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5128 | PTR_TO_MAP_VALUE, |
| 5129 | PTR_TO_MEM, |
Hao Luo | 20b2aff | 2021-12-16 16:31:48 -0800 | [diff] [blame] | 5130 | PTR_TO_BUF, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5131 | }, |
| 5132 | }; |
| 5133 | |
| 5134 | static const struct bpf_reg_types int_ptr_types = { |
| 5135 | .types = { |
| 5136 | PTR_TO_STACK, |
| 5137 | PTR_TO_PACKET, |
| 5138 | PTR_TO_PACKET_META, |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 5139 | PTR_TO_MAP_KEY, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5140 | PTR_TO_MAP_VALUE, |
| 5141 | }, |
| 5142 | }; |
| 5143 | |
| 5144 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; |
| 5145 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; |
| 5146 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; |
| 5147 | static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } }; |
| 5148 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; |
| 5149 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; |
| 5150 | static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5151 | static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_PERCPU_BTF_ID } }; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 5152 | static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } }; |
| 5153 | static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } }; |
Florent Revest | fff13c4 | 2021-04-19 17:52:39 +0200 | [diff] [blame] | 5154 | static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 5155 | static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE } }; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5156 | |
Lorenz Bauer | 0789e13b | 2020-09-23 17:01:55 +0100 | [diff] [blame] | 5157 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5158 | [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, |
| 5159 | [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, |
| 5160 | [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5161 | [ARG_CONST_SIZE] = &scalar_types, |
| 5162 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, |
| 5163 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, |
| 5164 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, |
| 5165 | [ARG_PTR_TO_CTX] = &context_types, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5166 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 5167 | #ifdef CONFIG_NET |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5168 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 5169 | #endif |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5170 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5171 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
| 5172 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, |
| 5173 | [ARG_PTR_TO_MEM] = &mem_types, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5174 | [ARG_PTR_TO_UNINIT_MEM] = &mem_types, |
| 5175 | [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5176 | [ARG_PTR_TO_INT] = &int_ptr_types, |
| 5177 | [ARG_PTR_TO_LONG] = &int_ptr_types, |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5178 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 5179 | [ARG_PTR_TO_FUNC] = &func_ptr_types, |
Hao Luo | 48946bd | 2021-12-16 16:31:45 -0800 | [diff] [blame] | 5180 | [ARG_PTR_TO_STACK] = &stack_ptr_types, |
Florent Revest | fff13c4 | 2021-04-19 17:52:39 +0200 | [diff] [blame] | 5181 | [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 5182 | [ARG_PTR_TO_TIMER] = &timer_types, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5183 | }; |
| 5184 | |
| 5185 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5186 | enum bpf_arg_type arg_type, |
| 5187 | const u32 *arg_btf_id) |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5188 | { |
| 5189 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 5190 | enum bpf_reg_type expected, type = reg->type; |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5191 | const struct bpf_reg_types *compatible; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5192 | int i, j; |
| 5193 | |
Hao Luo | 48946bd | 2021-12-16 16:31:45 -0800 | [diff] [blame] | 5194 | compatible = compatible_reg_types[base_type(arg_type)]; |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5195 | if (!compatible) { |
| 5196 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); |
| 5197 | return -EFAULT; |
| 5198 | } |
| 5199 | |
Hao Luo | 216e3cd | 2021-12-16 16:31:51 -0800 | [diff] [blame] | 5200 | /* ARG_PTR_TO_MEM + RDONLY is compatible with PTR_TO_MEM and PTR_TO_MEM + RDONLY, |
| 5201 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM and NOT with PTR_TO_MEM + RDONLY |
| 5202 | * |
| 5203 | * Same for MAYBE_NULL: |
| 5204 | * |
| 5205 | * ARG_PTR_TO_MEM + MAYBE_NULL is compatible with PTR_TO_MEM and PTR_TO_MEM + MAYBE_NULL, |
| 5206 | * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM but NOT with PTR_TO_MEM + MAYBE_NULL |
| 5207 | * |
| 5208 | * Therefore we fold these flags depending on the arg_type before comparison. |
| 5209 | */ |
| 5210 | if (arg_type & MEM_RDONLY) |
| 5211 | type &= ~MEM_RDONLY; |
| 5212 | if (arg_type & PTR_MAYBE_NULL) |
| 5213 | type &= ~PTR_MAYBE_NULL; |
| 5214 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5215 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
| 5216 | expected = compatible->types[i]; |
| 5217 | if (expected == NOT_INIT) |
| 5218 | break; |
| 5219 | |
| 5220 | if (type == expected) |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5221 | goto found; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5222 | } |
| 5223 | |
Hao Luo | 216e3cd | 2021-12-16 16:31:51 -0800 | [diff] [blame] | 5224 | verbose(env, "R%d type=%s expected=", regno, reg_type_str(env, reg->type)); |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5225 | for (j = 0; j + 1 < i; j++) |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 5226 | verbose(env, "%s, ", reg_type_str(env, compatible->types[j])); |
| 5227 | verbose(env, "%s\n", reg_type_str(env, compatible->types[j])); |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5228 | return -EACCES; |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5229 | |
| 5230 | found: |
Hao Luo | 216e3cd | 2021-12-16 16:31:51 -0800 | [diff] [blame] | 5231 | if (reg->type == PTR_TO_BTF_ID) { |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5232 | if (!arg_btf_id) { |
| 5233 | if (!compatible->btf_id) { |
| 5234 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); |
| 5235 | return -EFAULT; |
| 5236 | } |
| 5237 | arg_btf_id = compatible->btf_id; |
| 5238 | } |
| 5239 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 5240 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, |
| 5241 | btf_vmlinux, *arg_btf_id)) { |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5242 | verbose(env, "R%d is of type %s but %s is expected\n", |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 5243 | regno, kernel_type_name(reg->btf, reg->btf_id), |
| 5244 | kernel_type_name(btf_vmlinux, *arg_btf_id)); |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5245 | return -EACCES; |
| 5246 | } |
| 5247 | |
| 5248 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 5249 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", |
| 5250 | regno); |
| 5251 | return -EACCES; |
| 5252 | } |
| 5253 | } |
| 5254 | |
| 5255 | return 0; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5256 | } |
| 5257 | |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5258 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
| 5259 | struct bpf_call_arg_meta *meta, |
| 5260 | const struct bpf_func_proto *fn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5261 | { |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5262 | u32 regno = BPF_REG_1 + arg; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5263 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5264 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5265 | enum bpf_reg_type type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5266 | int err = 0; |
| 5267 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 5268 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5269 | return 0; |
| 5270 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5271 | err = check_reg_arg(env, regno, SRC_OP); |
| 5272 | if (err) |
| 5273 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5274 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5275 | if (arg_type == ARG_ANYTHING) { |
| 5276 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5277 | verbose(env, "R%d leaks addr into helper function\n", |
| 5278 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5279 | return -EACCES; |
| 5280 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 5281 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5282 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 5283 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5284 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 5285 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5286 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 5287 | return -EACCES; |
| 5288 | } |
| 5289 | |
Hao Luo | 48946bd | 2021-12-16 16:31:45 -0800 | [diff] [blame] | 5290 | if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE || |
| 5291 | base_type(arg_type) == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 5292 | err = resolve_map_arg_type(env, meta, &arg_type); |
| 5293 | if (err) |
| 5294 | return err; |
| 5295 | } |
| 5296 | |
Hao Luo | 48946bd | 2021-12-16 16:31:45 -0800 | [diff] [blame] | 5297 | if (register_is_null(reg) && type_may_be_null(arg_type)) |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 5298 | /* A NULL register has a SCALAR_VALUE type, so skip |
| 5299 | * type checking. |
| 5300 | */ |
| 5301 | goto skip_type_check; |
Jiri Olsa | faaf4a7 | 2020-08-25 21:21:18 +0200 | [diff] [blame] | 5302 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5303 | err = check_reg_type(env, regno, arg_type, fn->arg_btf_id[arg]); |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5304 | if (err) |
| 5305 | return err; |
| 5306 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5307 | if (type == PTR_TO_CTX) { |
Lorenz Bauer | feec704 | 2020-09-21 13:12:23 +0100 | [diff] [blame] | 5308 | err = check_ctx_reg(env, reg, regno); |
| 5309 | if (err < 0) |
| 5310 | return err; |
Lorenz Bauer | d7b9454 | 2020-09-21 13:12:21 +0100 | [diff] [blame] | 5311 | } |
| 5312 | |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 5313 | skip_type_check: |
Lorenz Bauer | 02f7c95 | 2020-09-21 13:12:22 +0100 | [diff] [blame] | 5314 | if (reg->ref_obj_id) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5315 | if (meta->ref_obj_id) { |
| 5316 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", |
| 5317 | regno, reg->ref_obj_id, |
| 5318 | meta->ref_obj_id); |
| 5319 | return -EFAULT; |
| 5320 | } |
| 5321 | meta->ref_obj_id = reg->ref_obj_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5322 | } |
| 5323 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5324 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 5325 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Alexei Starovoitov | 3e8ce29 | 2021-07-14 17:54:11 -0700 | [diff] [blame] | 5326 | if (meta->map_ptr) { |
| 5327 | /* Use map_uid (which is unique id of inner map) to reject: |
| 5328 | * inner_map1 = bpf_map_lookup_elem(outer_map, key1) |
| 5329 | * inner_map2 = bpf_map_lookup_elem(outer_map, key2) |
| 5330 | * if (inner_map1 && inner_map2) { |
| 5331 | * timer = bpf_map_lookup_elem(inner_map1); |
| 5332 | * if (timer) |
| 5333 | * // mismatch would have been allowed |
| 5334 | * bpf_timer_init(timer, inner_map2); |
| 5335 | * } |
| 5336 | * |
| 5337 | * Comparing map_ptr is enough to distinguish normal and outer maps. |
| 5338 | */ |
| 5339 | if (meta->map_ptr != reg->map_ptr || |
| 5340 | meta->map_uid != reg->map_uid) { |
| 5341 | verbose(env, |
| 5342 | "timer pointer in R1 map_uid=%d doesn't match map pointer in R2 map_uid=%d\n", |
| 5343 | meta->map_uid, reg->map_uid); |
| 5344 | return -EINVAL; |
| 5345 | } |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 5346 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5347 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 3e8ce29 | 2021-07-14 17:54:11 -0700 | [diff] [blame] | 5348 | meta->map_uid = reg->map_uid; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5349 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 5350 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 5351 | * check that [key, key + map->key_size) are within |
| 5352 | * stack limits and initialized |
| 5353 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5354 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5355 | /* in function declaration map_ptr must come before |
| 5356 | * map_key, so that it's verified and known before |
| 5357 | * we have to check map_key here. Otherwise it means |
| 5358 | * that kernel subsystem misconfigured verifier |
| 5359 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5360 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5361 | return -EACCES; |
| 5362 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 5363 | err = check_helper_mem_access(env, regno, |
| 5364 | meta->map_ptr->key_size, false, |
| 5365 | NULL); |
Hao Luo | 48946bd | 2021-12-16 16:31:45 -0800 | [diff] [blame] | 5366 | } else if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE || |
| 5367 | base_type(arg_type) == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
| 5368 | if (type_may_be_null(arg_type) && register_is_null(reg)) |
| 5369 | return 0; |
| 5370 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5371 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 5372 | * check [value, value + map->value_size) validity |
| 5373 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5374 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5375 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5376 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5377 | return -EACCES; |
| 5378 | } |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 5379 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 5380 | err = check_helper_mem_access(env, regno, |
| 5381 | meta->map_ptr->value_size, false, |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 5382 | meta); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5383 | } else if (arg_type == ARG_PTR_TO_PERCPU_BTF_ID) { |
| 5384 | if (!reg->btf_id) { |
| 5385 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); |
| 5386 | return -EACCES; |
| 5387 | } |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 5388 | meta->ret_btf = reg->btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5389 | meta->ret_btf_id = reg->btf_id; |
Lorenz Bauer | c18f0b6 | 2020-09-21 13:12:25 +0100 | [diff] [blame] | 5390 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
| 5391 | if (meta->func_id == BPF_FUNC_spin_lock) { |
| 5392 | if (process_spin_lock(env, regno, true)) |
| 5393 | return -EACCES; |
| 5394 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
| 5395 | if (process_spin_lock(env, regno, false)) |
| 5396 | return -EACCES; |
| 5397 | } else { |
| 5398 | verbose(env, "verifier internal error\n"); |
| 5399 | return -EFAULT; |
| 5400 | } |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 5401 | } else if (arg_type == ARG_PTR_TO_TIMER) { |
| 5402 | if (process_timer_func(env, regno, meta)) |
| 5403 | return -EACCES; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 5404 | } else if (arg_type == ARG_PTR_TO_FUNC) { |
| 5405 | meta->subprogno = reg->subprogno; |
Lorenz Bauer | a2bbe7c | 2020-09-21 13:12:24 +0100 | [diff] [blame] | 5406 | } else if (arg_type_is_mem_ptr(arg_type)) { |
| 5407 | /* The access to this pointer is only checked when we hit the |
| 5408 | * next is_mem_size argument below. |
| 5409 | */ |
| 5410 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 5411 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 5412 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5413 | |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 5414 | /* This is used to refine r0 return value bounds for helpers |
| 5415 | * that enforce this value as an upper bound on return values. |
| 5416 | * See do_refine_retval_range() for helpers that can refine |
| 5417 | * the return value. C type of helper is u32 so we pull register |
| 5418 | * bound from umax_value however, if negative verifier errors |
| 5419 | * out. Only upper bounds can be learned because retval is an |
| 5420 | * int type and negative retvals are allowed. |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 5421 | */ |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 5422 | meta->msize_max_value = reg->umax_value; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 5423 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5424 | /* The register is SCALAR_VALUE; the access check |
| 5425 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 5426 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5427 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 5428 | /* For unprivileged variable accesses, disable raw |
| 5429 | * mode so that the program is required to |
| 5430 | * initialize all the memory that the helper could |
| 5431 | * just partially fill up. |
| 5432 | */ |
| 5433 | meta = NULL; |
| 5434 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5435 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5436 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5437 | regno); |
| 5438 | return -EACCES; |
| 5439 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 5440 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5441 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5442 | err = check_helper_mem_access(env, regno - 1, 0, |
| 5443 | zero_size_allowed, |
| 5444 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 5445 | if (err) |
| 5446 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 5447 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5448 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5449 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5450 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5451 | regno); |
| 5452 | return -EACCES; |
| 5453 | } |
| 5454 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5455 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5456 | zero_size_allowed, meta); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 5457 | if (!err) |
| 5458 | err = mark_chain_precision(env, regno); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5459 | } else if (arg_type_is_alloc_size(arg_type)) { |
| 5460 | if (!tnum_is_const(reg->var_off)) { |
Brendan Jackman | 28a8add | 2021-01-12 12:39:13 +0000 | [diff] [blame] | 5461 | verbose(env, "R%d is not a known constant'\n", |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5462 | regno); |
| 5463 | return -EACCES; |
| 5464 | } |
| 5465 | meta->mem_size = reg->var_off.value; |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 5466 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 5467 | int size = int_ptr_type_to_size(arg_type); |
| 5468 | |
| 5469 | err = check_helper_mem_access(env, regno, size, false, meta); |
| 5470 | if (err) |
| 5471 | return err; |
| 5472 | err = check_ptr_alignment(env, reg, 0, size, true); |
Florent Revest | fff13c4 | 2021-04-19 17:52:39 +0200 | [diff] [blame] | 5473 | } else if (arg_type == ARG_PTR_TO_CONST_STR) { |
| 5474 | struct bpf_map *map = reg->map_ptr; |
| 5475 | int map_off; |
| 5476 | u64 map_addr; |
| 5477 | char *str_ptr; |
| 5478 | |
Florent Revest | a8fad73 | 2021-04-23 01:55:43 +0200 | [diff] [blame] | 5479 | if (!bpf_map_is_rdonly(map)) { |
Florent Revest | fff13c4 | 2021-04-19 17:52:39 +0200 | [diff] [blame] | 5480 | verbose(env, "R%d does not point to a readonly map'\n", regno); |
| 5481 | return -EACCES; |
| 5482 | } |
| 5483 | |
| 5484 | if (!tnum_is_const(reg->var_off)) { |
| 5485 | verbose(env, "R%d is not a constant address'\n", regno); |
| 5486 | return -EACCES; |
| 5487 | } |
| 5488 | |
| 5489 | if (!map->ops->map_direct_value_addr) { |
| 5490 | verbose(env, "no direct value access support for this map type\n"); |
| 5491 | return -EACCES; |
| 5492 | } |
| 5493 | |
| 5494 | err = check_map_access(env, regno, reg->off, |
| 5495 | map->value_size - reg->off, false); |
| 5496 | if (err) |
| 5497 | return err; |
| 5498 | |
| 5499 | map_off = reg->off + reg->var_off.value; |
| 5500 | err = map->ops->map_direct_value_addr(map, &map_addr, map_off); |
| 5501 | if (err) { |
| 5502 | verbose(env, "direct value access on string failed\n"); |
| 5503 | return err; |
| 5504 | } |
| 5505 | |
| 5506 | str_ptr = (char *)(long)(map_addr); |
| 5507 | if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) { |
| 5508 | verbose(env, "string is not zero-terminated\n"); |
| 5509 | return -EINVAL; |
| 5510 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | return err; |
| 5514 | } |
| 5515 | |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 5516 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
| 5517 | { |
| 5518 | enum bpf_attach_type eatype = env->prog->expected_attach_type; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 5519 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 5520 | |
| 5521 | if (func_id != BPF_FUNC_map_update_elem) |
| 5522 | return false; |
| 5523 | |
| 5524 | /* It's not possible to get access to a locked struct sock in these |
| 5525 | * contexts, so updating is safe. |
| 5526 | */ |
| 5527 | switch (type) { |
| 5528 | case BPF_PROG_TYPE_TRACING: |
| 5529 | if (eatype == BPF_TRACE_ITER) |
| 5530 | return true; |
| 5531 | break; |
| 5532 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 5533 | case BPF_PROG_TYPE_SCHED_CLS: |
| 5534 | case BPF_PROG_TYPE_SCHED_ACT: |
| 5535 | case BPF_PROG_TYPE_XDP: |
| 5536 | case BPF_PROG_TYPE_SK_REUSEPORT: |
| 5537 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
| 5538 | case BPF_PROG_TYPE_SK_LOOKUP: |
| 5539 | return true; |
| 5540 | default: |
| 5541 | break; |
| 5542 | } |
| 5543 | |
| 5544 | verbose(env, "cannot update sockmap in this context\n"); |
| 5545 | return false; |
| 5546 | } |
| 5547 | |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 5548 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
| 5549 | { |
| 5550 | return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); |
| 5551 | } |
| 5552 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5553 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 5554 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 5555 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 5556 | if (!map) |
| 5557 | return 0; |
| 5558 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5559 | /* We need a two way check, first is from map perspective ... */ |
| 5560 | switch (map->map_type) { |
| 5561 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 5562 | if (func_id != BPF_FUNC_tail_call) |
| 5563 | goto error; |
| 5564 | break; |
| 5565 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 5566 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 5567 | func_id != BPF_FUNC_perf_event_output && |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 5568 | func_id != BPF_FUNC_skb_output && |
Eelco Chaudron | d831ee8 | 2020-03-06 08:59:23 +0000 | [diff] [blame] | 5569 | func_id != BPF_FUNC_perf_event_read_value && |
| 5570 | func_id != BPF_FUNC_xdp_output) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5571 | goto error; |
| 5572 | break; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5573 | case BPF_MAP_TYPE_RINGBUF: |
| 5574 | if (func_id != BPF_FUNC_ringbuf_output && |
| 5575 | func_id != BPF_FUNC_ringbuf_reserve && |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5576 | func_id != BPF_FUNC_ringbuf_query) |
| 5577 | goto error; |
| 5578 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5579 | case BPF_MAP_TYPE_STACK_TRACE: |
| 5580 | if (func_id != BPF_FUNC_get_stackid) |
| 5581 | goto error; |
| 5582 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 5583 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 5584 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 5585 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 5586 | goto error; |
| 5587 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 5588 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 5589 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 5590 | if (func_id != BPF_FUNC_get_local_storage) |
| 5591 | goto error; |
| 5592 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 5593 | case BPF_MAP_TYPE_DEVMAP: |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 5594 | case BPF_MAP_TYPE_DEVMAP_HASH: |
Toke Høiland-Jørgensen | 0cdbb4b | 2019-06-28 11:12:35 +0200 | [diff] [blame] | 5595 | if (func_id != BPF_FUNC_redirect_map && |
| 5596 | func_id != BPF_FUNC_map_lookup_elem) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 5597 | goto error; |
| 5598 | break; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 5599 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 5600 | * appear. |
| 5601 | */ |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 5602 | case BPF_MAP_TYPE_CPUMAP: |
| 5603 | if (func_id != BPF_FUNC_redirect_map) |
| 5604 | goto error; |
| 5605 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 5606 | case BPF_MAP_TYPE_XSKMAP: |
| 5607 | if (func_id != BPF_FUNC_redirect_map && |
| 5608 | func_id != BPF_FUNC_map_lookup_elem) |
| 5609 | goto error; |
| 5610 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5611 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 5612 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5613 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 5614 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 5615 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 5616 | case BPF_MAP_TYPE_SOCKMAP: |
| 5617 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 5618 | func_id != BPF_FUNC_sock_map_update && |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 5619 | func_id != BPF_FUNC_map_delete_elem && |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 5620 | func_id != BPF_FUNC_msg_redirect_map && |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 5621 | func_id != BPF_FUNC_sk_select_reuseport && |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 5622 | func_id != BPF_FUNC_map_lookup_elem && |
| 5623 | !may_update_sockmap(env, func_id)) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 5624 | goto error; |
| 5625 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 5626 | case BPF_MAP_TYPE_SOCKHASH: |
| 5627 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 5628 | func_id != BPF_FUNC_sock_hash_update && |
| 5629 | func_id != BPF_FUNC_map_delete_elem && |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 5630 | func_id != BPF_FUNC_msg_redirect_hash && |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 5631 | func_id != BPF_FUNC_sk_select_reuseport && |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 5632 | func_id != BPF_FUNC_map_lookup_elem && |
| 5633 | !may_update_sockmap(env, func_id)) |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 5634 | goto error; |
| 5635 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 5636 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 5637 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 5638 | goto error; |
| 5639 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 5640 | case BPF_MAP_TYPE_QUEUE: |
| 5641 | case BPF_MAP_TYPE_STACK: |
| 5642 | if (func_id != BPF_FUNC_map_peek_elem && |
| 5643 | func_id != BPF_FUNC_map_pop_elem && |
| 5644 | func_id != BPF_FUNC_map_push_elem) |
| 5645 | goto error; |
| 5646 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 5647 | case BPF_MAP_TYPE_SK_STORAGE: |
| 5648 | if (func_id != BPF_FUNC_sk_storage_get && |
| 5649 | func_id != BPF_FUNC_sk_storage_delete) |
| 5650 | goto error; |
| 5651 | break; |
KP Singh | 8ea6368 | 2020-08-25 20:29:17 +0200 | [diff] [blame] | 5652 | case BPF_MAP_TYPE_INODE_STORAGE: |
| 5653 | if (func_id != BPF_FUNC_inode_storage_get && |
| 5654 | func_id != BPF_FUNC_inode_storage_delete) |
| 5655 | goto error; |
| 5656 | break; |
KP Singh | 4cf1bc1 | 2020-11-06 10:37:40 +0000 | [diff] [blame] | 5657 | case BPF_MAP_TYPE_TASK_STORAGE: |
| 5658 | if (func_id != BPF_FUNC_task_storage_get && |
| 5659 | func_id != BPF_FUNC_task_storage_delete) |
| 5660 | goto error; |
| 5661 | break; |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 5662 | case BPF_MAP_TYPE_BLOOM_FILTER: |
| 5663 | if (func_id != BPF_FUNC_map_peek_elem && |
| 5664 | func_id != BPF_FUNC_map_push_elem) |
| 5665 | goto error; |
| 5666 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5667 | default: |
| 5668 | break; |
| 5669 | } |
| 5670 | |
| 5671 | /* ... and second from the function itself. */ |
| 5672 | switch (func_id) { |
| 5673 | case BPF_FUNC_tail_call: |
| 5674 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 5675 | goto error; |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 5676 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
| 5677 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5678 | return -EINVAL; |
| 5679 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5680 | break; |
| 5681 | case BPF_FUNC_perf_event_read: |
| 5682 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 5683 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 5684 | case BPF_FUNC_skb_output: |
Eelco Chaudron | d831ee8 | 2020-03-06 08:59:23 +0000 | [diff] [blame] | 5685 | case BPF_FUNC_xdp_output: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5686 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 5687 | goto error; |
| 5688 | break; |
Daniel Borkmann | 5b029a3 | 2021-08-23 21:02:09 +0200 | [diff] [blame] | 5689 | case BPF_FUNC_ringbuf_output: |
| 5690 | case BPF_FUNC_ringbuf_reserve: |
| 5691 | case BPF_FUNC_ringbuf_query: |
| 5692 | if (map->map_type != BPF_MAP_TYPE_RINGBUF) |
| 5693 | goto error; |
| 5694 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5695 | case BPF_FUNC_get_stackid: |
| 5696 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 5697 | goto error; |
| 5698 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 5699 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 5700 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 5701 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 5702 | goto error; |
| 5703 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 5704 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 5705 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 5706 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 5707 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 5708 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 5709 | goto error; |
| 5710 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 5711 | case BPF_FUNC_sk_redirect_map: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 5712 | case BPF_FUNC_msg_redirect_map: |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 5713 | case BPF_FUNC_sock_map_update: |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 5714 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 5715 | goto error; |
| 5716 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 5717 | case BPF_FUNC_sk_redirect_hash: |
| 5718 | case BPF_FUNC_msg_redirect_hash: |
| 5719 | case BPF_FUNC_sock_hash_update: |
| 5720 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 5721 | goto error; |
| 5722 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 5723 | case BPF_FUNC_get_local_storage: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 5724 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 5725 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 5726 | goto error; |
| 5727 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 5728 | case BPF_FUNC_sk_select_reuseport: |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 5729 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
| 5730 | map->map_type != BPF_MAP_TYPE_SOCKMAP && |
| 5731 | map->map_type != BPF_MAP_TYPE_SOCKHASH) |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 5732 | goto error; |
| 5733 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 5734 | case BPF_FUNC_map_pop_elem: |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 5735 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 5736 | map->map_type != BPF_MAP_TYPE_STACK) |
| 5737 | goto error; |
| 5738 | break; |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 5739 | case BPF_FUNC_map_peek_elem: |
| 5740 | case BPF_FUNC_map_push_elem: |
| 5741 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 5742 | map->map_type != BPF_MAP_TYPE_STACK && |
| 5743 | map->map_type != BPF_MAP_TYPE_BLOOM_FILTER) |
| 5744 | goto error; |
| 5745 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 5746 | case BPF_FUNC_sk_storage_get: |
| 5747 | case BPF_FUNC_sk_storage_delete: |
| 5748 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
| 5749 | goto error; |
| 5750 | break; |
KP Singh | 8ea6368 | 2020-08-25 20:29:17 +0200 | [diff] [blame] | 5751 | case BPF_FUNC_inode_storage_get: |
| 5752 | case BPF_FUNC_inode_storage_delete: |
| 5753 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) |
| 5754 | goto error; |
| 5755 | break; |
KP Singh | 4cf1bc1 | 2020-11-06 10:37:40 +0000 | [diff] [blame] | 5756 | case BPF_FUNC_task_storage_get: |
| 5757 | case BPF_FUNC_task_storage_delete: |
| 5758 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) |
| 5759 | goto error; |
| 5760 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5761 | default: |
| 5762 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
| 5765 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5766 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5767 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 5768 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5769 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 5770 | } |
| 5771 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 5772 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5773 | { |
| 5774 | int count = 0; |
| 5775 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 5776 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5777 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 5778 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5779 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 5780 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5781 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 5782 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5783 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 5784 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5785 | count++; |
| 5786 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 5787 | /* We only support one arg being in raw mode at the moment, |
| 5788 | * which is sufficient for the helper functions we have |
| 5789 | * right now. |
| 5790 | */ |
| 5791 | return count <= 1; |
| 5792 | } |
| 5793 | |
| 5794 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 5795 | enum bpf_arg_type arg_next) |
| 5796 | { |
| 5797 | return (arg_type_is_mem_ptr(arg_curr) && |
| 5798 | !arg_type_is_mem_size(arg_next)) || |
| 5799 | (!arg_type_is_mem_ptr(arg_curr) && |
| 5800 | arg_type_is_mem_size(arg_next)); |
| 5801 | } |
| 5802 | |
| 5803 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 5804 | { |
| 5805 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 5806 | * bytes from memory 'buf'. Both arg types need |
| 5807 | * to be paired, so make sure there's no buggy |
| 5808 | * helper function specification. |
| 5809 | */ |
| 5810 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 5811 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 5812 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 5813 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 5814 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 5815 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 5816 | return false; |
| 5817 | |
| 5818 | return true; |
| 5819 | } |
| 5820 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5821 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5822 | { |
| 5823 | int count = 0; |
| 5824 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5825 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5826 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5827 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5828 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5829 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5830 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5831 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5832 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5833 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5834 | count++; |
| 5835 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5836 | /* A reference acquiring function cannot acquire |
| 5837 | * another refcounted ptr. |
| 5838 | */ |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 5839 | if (may_be_acquire_function(func_id) && count) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5840 | return false; |
| 5841 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5842 | /* We only support one arg being unreferenced at the moment, |
| 5843 | * which is sufficient for the helper functions we have right now. |
| 5844 | */ |
| 5845 | return count <= 1; |
| 5846 | } |
| 5847 | |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 5848 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
| 5849 | { |
| 5850 | int i; |
| 5851 | |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5852 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 5853 | if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) |
| 5854 | return false; |
| 5855 | |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5856 | if (fn->arg_type[i] != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i]) |
| 5857 | return false; |
| 5858 | } |
| 5859 | |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 5860 | return true; |
| 5861 | } |
| 5862 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5863 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 5864 | { |
| 5865 | return check_raw_mode_ok(fn) && |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5866 | check_arg_pair_ok(fn) && |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 5867 | check_btf_id_ok(fn) && |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5868 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5869 | } |
| 5870 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5871 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 5872 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5873 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5874 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 5875 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5876 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5877 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5878 | int i; |
| 5879 | |
| 5880 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5881 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5882 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5883 | |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 5884 | bpf_for_each_spilled_reg(i, state, reg) { |
| 5885 | if (!reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5886 | continue; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5887 | if (reg_is_pkt_pointer_any(reg)) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 5888 | __mark_reg_unknown(env, reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5889 | } |
| 5890 | } |
| 5891 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5892 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 5893 | { |
| 5894 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5895 | int i; |
| 5896 | |
| 5897 | for (i = 0; i <= vstate->curframe; i++) |
| 5898 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 5899 | } |
| 5900 | |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 5901 | enum { |
| 5902 | AT_PKT_END = -1, |
| 5903 | BEYOND_PKT_END = -2, |
| 5904 | }; |
| 5905 | |
| 5906 | static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range_open) |
| 5907 | { |
| 5908 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 5909 | struct bpf_reg_state *reg = &state->regs[regn]; |
| 5910 | |
| 5911 | if (reg->type != PTR_TO_PACKET) |
| 5912 | /* PTR_TO_PACKET_META is not supported yet */ |
| 5913 | return; |
| 5914 | |
| 5915 | /* The 'reg' is pkt > pkt_end or pkt >= pkt_end. |
| 5916 | * How far beyond pkt_end it goes is unknown. |
| 5917 | * if (!range_open) it's the case of pkt >= pkt_end |
| 5918 | * if (range_open) it's the case of pkt > pkt_end |
| 5919 | * hence this pointer is at least 1 byte bigger than pkt_end |
| 5920 | */ |
| 5921 | if (range_open) |
| 5922 | reg->range = BEYOND_PKT_END; |
| 5923 | else |
| 5924 | reg->range = AT_PKT_END; |
| 5925 | } |
| 5926 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5927 | static void release_reg_references(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5928 | struct bpf_func_state *state, |
| 5929 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5930 | { |
| 5931 | struct bpf_reg_state *regs = state->regs, *reg; |
| 5932 | int i; |
| 5933 | |
| 5934 | for (i = 0; i < MAX_BPF_REG; i++) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5935 | if (regs[i].ref_obj_id == ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5936 | mark_reg_unknown(env, regs, i); |
| 5937 | |
| 5938 | bpf_for_each_spilled_reg(i, state, reg) { |
| 5939 | if (!reg) |
| 5940 | continue; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5941 | if (reg->ref_obj_id == ref_obj_id) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 5942 | __mark_reg_unknown(env, reg); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5943 | } |
| 5944 | } |
| 5945 | |
| 5946 | /* The pointer with the specified id has released its reference to kernel |
| 5947 | * resources. Identify all copies of the same pointer and clear the reference. |
| 5948 | */ |
| 5949 | static int release_reference(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5950 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5951 | { |
| 5952 | struct bpf_verifier_state *vstate = env->cur_state; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5953 | int err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5954 | int i; |
| 5955 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5956 | err = release_reference_state(cur_func(env), ref_obj_id); |
| 5957 | if (err) |
| 5958 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5959 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5960 | for (i = 0; i <= vstate->curframe; i++) |
| 5961 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
| 5962 | |
| 5963 | return 0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5964 | } |
| 5965 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 5966 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
| 5967 | struct bpf_reg_state *regs) |
| 5968 | { |
| 5969 | int i; |
| 5970 | |
| 5971 | /* after the call registers r0 - r5 were scratched */ |
| 5972 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 5973 | mark_reg_not_init(env, regs, caller_saved[i]); |
| 5974 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 5975 | } |
| 5976 | } |
| 5977 | |
Yonghong Song | 1435137 | 2021-02-26 12:49:23 -0800 | [diff] [blame] | 5978 | typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env, |
| 5979 | struct bpf_func_state *caller, |
| 5980 | struct bpf_func_state *callee, |
| 5981 | int insn_idx); |
| 5982 | |
| 5983 | static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 5984 | int *insn_idx, int subprog, |
| 5985 | set_callee_state_fn set_callee_state_cb) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5986 | { |
| 5987 | struct bpf_verifier_state *state = env->cur_state; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 5988 | struct bpf_func_info_aux *func_info_aux; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5989 | struct bpf_func_state *caller, *callee; |
Yonghong Song | 1435137 | 2021-02-26 12:49:23 -0800 | [diff] [blame] | 5990 | int err; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 5991 | bool is_global = false; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5992 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 5993 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5994 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 5995 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5996 | return -E2BIG; |
| 5997 | } |
| 5998 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5999 | caller = state->frame[state->curframe]; |
| 6000 | if (state->frame[state->curframe + 1]) { |
| 6001 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 6002 | state->curframe + 1); |
| 6003 | return -EFAULT; |
| 6004 | } |
| 6005 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 6006 | func_info_aux = env->prog->aux->func_info_aux; |
| 6007 | if (func_info_aux) |
| 6008 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; |
Martin KaFai Lau | 34747c4 | 2021-03-24 18:51:36 -0700 | [diff] [blame] | 6009 | err = btf_check_subprog_arg_match(env, subprog, caller->regs); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 6010 | if (err == -EFAULT) |
| 6011 | return err; |
| 6012 | if (is_global) { |
| 6013 | if (err) { |
| 6014 | verbose(env, "Caller passes invalid args into func#%d\n", |
| 6015 | subprog); |
| 6016 | return err; |
| 6017 | } else { |
| 6018 | if (env->log.level & BPF_LOG_LEVEL) |
| 6019 | verbose(env, |
| 6020 | "Func#%d is global and valid. Skipping.\n", |
| 6021 | subprog); |
| 6022 | clear_caller_saved_regs(env, caller->regs); |
| 6023 | |
Ilya Leoshkevich | 45159b2 | 2021-02-12 05:04:08 +0100 | [diff] [blame] | 6024 | /* All global functions return a 64-bit SCALAR_VALUE */ |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 6025 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
Ilya Leoshkevich | 45159b2 | 2021-02-12 05:04:08 +0100 | [diff] [blame] | 6026 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 6027 | |
| 6028 | /* continue with next insn after call */ |
| 6029 | return 0; |
| 6030 | } |
| 6031 | } |
| 6032 | |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 6033 | if (insn->code == (BPF_JMP | BPF_CALL) && |
Kris Van Hees | a5bebc4 | 2022-01-05 16:01:50 -0500 | [diff] [blame] | 6034 | insn->src_reg == 0 && |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 6035 | insn->imm == BPF_FUNC_timer_set_callback) { |
| 6036 | struct bpf_verifier_state *async_cb; |
| 6037 | |
| 6038 | /* there is no real recursion here. timer callbacks are async */ |
Alexei Starovoitov | 7ddc80a | 2021-07-14 17:54:15 -0700 | [diff] [blame] | 6039 | env->subprog_info[subprog].is_async_cb = true; |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 6040 | async_cb = push_async_cb(env, env->subprog_info[subprog].start, |
| 6041 | *insn_idx, subprog); |
| 6042 | if (!async_cb) |
| 6043 | return -EFAULT; |
| 6044 | callee = async_cb->frame[0]; |
| 6045 | callee->async_entry_cnt = caller->async_entry_cnt + 1; |
| 6046 | |
| 6047 | /* Convert bpf_timer_set_callback() args into timer callback args */ |
| 6048 | err = set_callee_state_cb(env, caller, callee, *insn_idx); |
| 6049 | if (err) |
| 6050 | return err; |
| 6051 | |
| 6052 | clear_caller_saved_regs(env, caller->regs); |
| 6053 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
| 6054 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 6055 | /* continue with next insn after call */ |
| 6056 | return 0; |
| 6057 | } |
| 6058 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6059 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 6060 | if (!callee) |
| 6061 | return -ENOMEM; |
| 6062 | state->frame[state->curframe + 1] = callee; |
| 6063 | |
| 6064 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 6065 | * into its own stack before reading from it. |
| 6066 | * callee can read/write into caller's stack |
| 6067 | */ |
| 6068 | init_func_state(env, callee, |
| 6069 | /* remember the callsite, it will be used by bpf_exit */ |
| 6070 | *insn_idx /* callsite */, |
| 6071 | state->curframe + 1 /* frameno within this callchain */, |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6072 | subprog /* subprog number within this prog */); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6073 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6074 | /* Transfer references to the callee */ |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 6075 | err = copy_reference_state(callee, caller); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6076 | if (err) |
| 6077 | return err; |
| 6078 | |
Yonghong Song | 1435137 | 2021-02-26 12:49:23 -0800 | [diff] [blame] | 6079 | err = set_callee_state_cb(env, caller, callee, *insn_idx); |
| 6080 | if (err) |
| 6081 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6082 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 6083 | clear_caller_saved_regs(env, caller->regs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6084 | |
| 6085 | /* only increment it after check_reg_arg() finished */ |
| 6086 | state->curframe++; |
| 6087 | |
| 6088 | /* and go analyze first insn of the callee */ |
Yonghong Song | 1435137 | 2021-02-26 12:49:23 -0800 | [diff] [blame] | 6089 | *insn_idx = env->subprog_info[subprog].start - 1; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6090 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6091 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6092 | verbose(env, "caller:\n"); |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 6093 | print_verifier_state(env, caller, true); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6094 | verbose(env, "callee:\n"); |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 6095 | print_verifier_state(env, callee, true); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6096 | } |
| 6097 | return 0; |
| 6098 | } |
| 6099 | |
Yonghong Song | 314ee05 | 2021-02-26 12:49:27 -0800 | [diff] [blame] | 6100 | int map_set_for_each_callback_args(struct bpf_verifier_env *env, |
| 6101 | struct bpf_func_state *caller, |
| 6102 | struct bpf_func_state *callee) |
| 6103 | { |
| 6104 | /* bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, |
| 6105 | * void *callback_ctx, u64 flags); |
| 6106 | * callback_fn(struct bpf_map *map, void *key, void *value, |
| 6107 | * void *callback_ctx); |
| 6108 | */ |
| 6109 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; |
| 6110 | |
| 6111 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; |
| 6112 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); |
| 6113 | callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr; |
| 6114 | |
| 6115 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; |
| 6116 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); |
| 6117 | callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr; |
| 6118 | |
| 6119 | /* pointer to stack or null */ |
| 6120 | callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3]; |
| 6121 | |
| 6122 | /* unused */ |
| 6123 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); |
| 6124 | return 0; |
| 6125 | } |
| 6126 | |
Yonghong Song | 1435137 | 2021-02-26 12:49:23 -0800 | [diff] [blame] | 6127 | static int set_callee_state(struct bpf_verifier_env *env, |
| 6128 | struct bpf_func_state *caller, |
| 6129 | struct bpf_func_state *callee, int insn_idx) |
| 6130 | { |
| 6131 | int i; |
| 6132 | |
| 6133 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 6134 | * pointers, which connects us up to the liveness chain |
| 6135 | */ |
| 6136 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 6137 | callee->regs[i] = caller->regs[i]; |
| 6138 | return 0; |
| 6139 | } |
| 6140 | |
| 6141 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 6142 | int *insn_idx) |
| 6143 | { |
| 6144 | int subprog, target_insn; |
| 6145 | |
| 6146 | target_insn = *insn_idx + insn->imm + 1; |
| 6147 | subprog = find_subprog(env, target_insn); |
| 6148 | if (subprog < 0) { |
| 6149 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 6150 | target_insn); |
| 6151 | return -EFAULT; |
| 6152 | } |
| 6153 | |
| 6154 | return __check_func_call(env, insn, insn_idx, subprog, set_callee_state); |
| 6155 | } |
| 6156 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6157 | static int set_map_elem_callback_state(struct bpf_verifier_env *env, |
| 6158 | struct bpf_func_state *caller, |
| 6159 | struct bpf_func_state *callee, |
| 6160 | int insn_idx) |
| 6161 | { |
| 6162 | struct bpf_insn_aux_data *insn_aux = &env->insn_aux_data[insn_idx]; |
| 6163 | struct bpf_map *map; |
| 6164 | int err; |
| 6165 | |
| 6166 | if (bpf_map_ptr_poisoned(insn_aux)) { |
| 6167 | verbose(env, "tail_call abusing map_ptr\n"); |
| 6168 | return -EINVAL; |
| 6169 | } |
| 6170 | |
| 6171 | map = BPF_MAP_PTR(insn_aux->map_ptr_state); |
| 6172 | if (!map->ops->map_set_for_each_callback_args || |
| 6173 | !map->ops->map_for_each_callback) { |
| 6174 | verbose(env, "callback function not allowed for map\n"); |
| 6175 | return -ENOTSUPP; |
| 6176 | } |
| 6177 | |
| 6178 | err = map->ops->map_set_for_each_callback_args(env, caller, callee); |
| 6179 | if (err) |
| 6180 | return err; |
| 6181 | |
| 6182 | callee->in_callback_fn = true; |
| 6183 | return 0; |
| 6184 | } |
| 6185 | |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6186 | static int set_loop_callback_state(struct bpf_verifier_env *env, |
| 6187 | struct bpf_func_state *caller, |
| 6188 | struct bpf_func_state *callee, |
| 6189 | int insn_idx) |
| 6190 | { |
| 6191 | /* bpf_loop(u32 nr_loops, void *callback_fn, void *callback_ctx, |
| 6192 | * u64 flags); |
| 6193 | * callback_fn(u32 index, void *callback_ctx); |
| 6194 | */ |
| 6195 | callee->regs[BPF_REG_1].type = SCALAR_VALUE; |
| 6196 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; |
| 6197 | |
| 6198 | /* unused */ |
| 6199 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); |
| 6200 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); |
| 6201 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); |
| 6202 | |
| 6203 | callee->in_callback_fn = true; |
| 6204 | return 0; |
| 6205 | } |
| 6206 | |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 6207 | static int set_timer_callback_state(struct bpf_verifier_env *env, |
| 6208 | struct bpf_func_state *caller, |
| 6209 | struct bpf_func_state *callee, |
| 6210 | int insn_idx) |
| 6211 | { |
| 6212 | struct bpf_map *map_ptr = caller->regs[BPF_REG_1].map_ptr; |
| 6213 | |
| 6214 | /* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn); |
| 6215 | * callback_fn(struct bpf_map *map, void *key, void *value); |
| 6216 | */ |
| 6217 | callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP; |
| 6218 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); |
| 6219 | callee->regs[BPF_REG_1].map_ptr = map_ptr; |
| 6220 | |
| 6221 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; |
| 6222 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); |
| 6223 | callee->regs[BPF_REG_2].map_ptr = map_ptr; |
| 6224 | |
| 6225 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; |
| 6226 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); |
| 6227 | callee->regs[BPF_REG_3].map_ptr = map_ptr; |
| 6228 | |
| 6229 | /* unused */ |
| 6230 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); |
| 6231 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 6232 | callee->in_async_callback_fn = true; |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 6233 | return 0; |
| 6234 | } |
| 6235 | |
Song Liu | 7c7e3d3 | 2021-11-05 16:23:29 -0700 | [diff] [blame] | 6236 | static int set_find_vma_callback_state(struct bpf_verifier_env *env, |
| 6237 | struct bpf_func_state *caller, |
| 6238 | struct bpf_func_state *callee, |
| 6239 | int insn_idx) |
| 6240 | { |
| 6241 | /* bpf_find_vma(struct task_struct *task, u64 addr, |
| 6242 | * void *callback_fn, void *callback_ctx, u64 flags) |
| 6243 | * (callback_fn)(struct task_struct *task, |
| 6244 | * struct vm_area_struct *vma, void *callback_ctx); |
| 6245 | */ |
| 6246 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; |
| 6247 | |
| 6248 | callee->regs[BPF_REG_2].type = PTR_TO_BTF_ID; |
| 6249 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); |
| 6250 | callee->regs[BPF_REG_2].btf = btf_vmlinux; |
Song Liu | d19ddb4 | 2021-11-12 07:02:43 -0800 | [diff] [blame] | 6251 | callee->regs[BPF_REG_2].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA], |
Song Liu | 7c7e3d3 | 2021-11-05 16:23:29 -0700 | [diff] [blame] | 6252 | |
| 6253 | /* pointer to stack or null */ |
| 6254 | callee->regs[BPF_REG_3] = caller->regs[BPF_REG_4]; |
| 6255 | |
| 6256 | /* unused */ |
| 6257 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); |
| 6258 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); |
| 6259 | callee->in_callback_fn = true; |
| 6260 | return 0; |
| 6261 | } |
| 6262 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6263 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 6264 | { |
| 6265 | struct bpf_verifier_state *state = env->cur_state; |
| 6266 | struct bpf_func_state *caller, *callee; |
| 6267 | struct bpf_reg_state *r0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6268 | int err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6269 | |
| 6270 | callee = state->frame[state->curframe]; |
| 6271 | r0 = &callee->regs[BPF_REG_0]; |
| 6272 | if (r0->type == PTR_TO_STACK) { |
| 6273 | /* technically it's ok to return caller's stack pointer |
| 6274 | * (or caller's caller's pointer) back to the caller, |
| 6275 | * since these pointers are valid. Only current stack |
| 6276 | * pointer will be invalid as soon as function exits, |
| 6277 | * but let's be conservative |
| 6278 | */ |
| 6279 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 6280 | return -EINVAL; |
| 6281 | } |
| 6282 | |
| 6283 | state->curframe--; |
| 6284 | caller = state->frame[state->curframe]; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6285 | if (callee->in_callback_fn) { |
| 6286 | /* enforce R0 return value range [0, 1]. */ |
| 6287 | struct tnum range = tnum_range(0, 1); |
| 6288 | |
| 6289 | if (r0->type != SCALAR_VALUE) { |
| 6290 | verbose(env, "R0 not a scalar value\n"); |
| 6291 | return -EACCES; |
| 6292 | } |
| 6293 | if (!tnum_in(range, r0->var_off)) { |
| 6294 | verbose_invalid_scalar(env, r0, &range, "callback return", "R0"); |
| 6295 | return -EINVAL; |
| 6296 | } |
| 6297 | } else { |
| 6298 | /* return to the caller whatever r0 had in the callee */ |
| 6299 | caller->regs[BPF_REG_0] = *r0; |
| 6300 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6301 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6302 | /* Transfer references to the caller */ |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 6303 | err = copy_reference_state(caller, callee); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6304 | if (err) |
| 6305 | return err; |
| 6306 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6307 | *insn_idx = callee->callsite + 1; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6308 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6309 | verbose(env, "returning from callee:\n"); |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 6310 | print_verifier_state(env, callee, true); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6311 | verbose(env, "to caller at %d:\n", *insn_idx); |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 6312 | print_verifier_state(env, caller, true); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6313 | } |
| 6314 | /* clear everything in the callee */ |
| 6315 | free_func_state(callee); |
| 6316 | state->frame[state->curframe + 1] = NULL; |
| 6317 | return 0; |
| 6318 | } |
| 6319 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 6320 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 6321 | int func_id, |
| 6322 | struct bpf_call_arg_meta *meta) |
| 6323 | { |
| 6324 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 6325 | |
| 6326 | if (ret_type != RET_INTEGER || |
| 6327 | (func_id != BPF_FUNC_get_stack && |
Dave Marchevsky | fd0b88f | 2021-04-16 13:47:02 -0700 | [diff] [blame] | 6328 | func_id != BPF_FUNC_get_task_stack && |
Daniel Borkmann | 47cc0ed | 2020-05-15 12:11:17 +0200 | [diff] [blame] | 6329 | func_id != BPF_FUNC_probe_read_str && |
| 6330 | func_id != BPF_FUNC_probe_read_kernel_str && |
| 6331 | func_id != BPF_FUNC_probe_read_user_str)) |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 6332 | return; |
| 6333 | |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 6334 | ret_reg->smax_value = meta->msize_max_value; |
John Fastabend | fa123ac | 2020-03-30 14:36:59 -0700 | [diff] [blame] | 6335 | ret_reg->s32_max_value = meta->msize_max_value; |
Alexei Starovoitov | b0270958 | 2020-12-08 19:01:51 +0100 | [diff] [blame] | 6336 | ret_reg->smin_value = -MAX_ERRNO; |
| 6337 | ret_reg->s32_min_value = -MAX_ERRNO; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 6338 | __reg_deduce_bounds(ret_reg); |
| 6339 | __reg_bound_offset(ret_reg); |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 6340 | __update_reg_bounds(ret_reg); |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 6341 | } |
| 6342 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6343 | static int |
| 6344 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 6345 | int func_id, int insn_idx) |
| 6346 | { |
| 6347 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 6348 | struct bpf_map *map = meta->map_ptr; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6349 | |
| 6350 | if (func_id != BPF_FUNC_tail_call && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6351 | func_id != BPF_FUNC_map_lookup_elem && |
| 6352 | func_id != BPF_FUNC_map_update_elem && |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 6353 | func_id != BPF_FUNC_map_delete_elem && |
| 6354 | func_id != BPF_FUNC_map_push_elem && |
| 6355 | func_id != BPF_FUNC_map_pop_elem && |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6356 | func_id != BPF_FUNC_map_peek_elem && |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 6357 | func_id != BPF_FUNC_for_each_map_elem && |
| 6358 | func_id != BPF_FUNC_redirect_map) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6359 | return 0; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6360 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 6361 | if (map == NULL) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6362 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 6363 | return -EINVAL; |
| 6364 | } |
| 6365 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 6366 | /* In case of read-only, some additional restrictions |
| 6367 | * need to be applied in order to prevent altering the |
| 6368 | * state of the map from program side. |
| 6369 | */ |
| 6370 | if ((map->map_flags & BPF_F_RDONLY_PROG) && |
| 6371 | (func_id == BPF_FUNC_map_delete_elem || |
| 6372 | func_id == BPF_FUNC_map_update_elem || |
| 6373 | func_id == BPF_FUNC_map_push_elem || |
| 6374 | func_id == BPF_FUNC_map_pop_elem)) { |
| 6375 | verbose(env, "write into map forbidden\n"); |
| 6376 | return -EACCES; |
| 6377 | } |
| 6378 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 6379 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6380 | bpf_map_ptr_store(aux, meta->map_ptr, |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 6381 | !meta->map_ptr->bypass_spec_v1); |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 6382 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6383 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 6384 | !meta->map_ptr->bypass_spec_v1); |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6385 | return 0; |
| 6386 | } |
| 6387 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 6388 | static int |
| 6389 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 6390 | int func_id, int insn_idx) |
| 6391 | { |
| 6392 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
| 6393 | struct bpf_reg_state *regs = cur_regs(env), *reg; |
| 6394 | struct bpf_map *map = meta->map_ptr; |
| 6395 | struct tnum range; |
| 6396 | u64 val; |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 6397 | int err; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 6398 | |
| 6399 | if (func_id != BPF_FUNC_tail_call) |
| 6400 | return 0; |
| 6401 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { |
| 6402 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 6403 | return -EINVAL; |
| 6404 | } |
| 6405 | |
| 6406 | range = tnum_range(0, map->max_entries - 1); |
| 6407 | reg = ®s[BPF_REG_3]; |
| 6408 | |
| 6409 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { |
| 6410 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
| 6411 | return 0; |
| 6412 | } |
| 6413 | |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 6414 | err = mark_chain_precision(env, BPF_REG_3); |
| 6415 | if (err) |
| 6416 | return err; |
| 6417 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 6418 | val = reg->var_off.value; |
| 6419 | if (bpf_map_key_unseen(aux)) |
| 6420 | bpf_map_key_store(aux, val); |
| 6421 | else if (!bpf_map_key_poisoned(aux) && |
| 6422 | bpf_map_key_immediate(aux) != val) |
| 6423 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
| 6424 | return 0; |
| 6425 | } |
| 6426 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6427 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 6428 | { |
| 6429 | struct bpf_func_state *state = cur_func(env); |
| 6430 | int i; |
| 6431 | |
| 6432 | for (i = 0; i < state->acquired_refs; i++) { |
| 6433 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 6434 | state->refs[i].id, state->refs[i].insn_idx); |
| 6435 | } |
| 6436 | return state->acquired_refs ? -EINVAL : 0; |
| 6437 | } |
| 6438 | |
Florent Revest | 7b15523 | 2021-04-19 17:52:40 +0200 | [diff] [blame] | 6439 | static int check_bpf_snprintf_call(struct bpf_verifier_env *env, |
| 6440 | struct bpf_reg_state *regs) |
| 6441 | { |
| 6442 | struct bpf_reg_state *fmt_reg = ®s[BPF_REG_3]; |
| 6443 | struct bpf_reg_state *data_len_reg = ®s[BPF_REG_5]; |
| 6444 | struct bpf_map *fmt_map = fmt_reg->map_ptr; |
| 6445 | int err, fmt_map_off, num_args; |
| 6446 | u64 fmt_addr; |
| 6447 | char *fmt; |
| 6448 | |
| 6449 | /* data must be an array of u64 */ |
| 6450 | if (data_len_reg->var_off.value % 8) |
| 6451 | return -EINVAL; |
| 6452 | num_args = data_len_reg->var_off.value / 8; |
| 6453 | |
| 6454 | /* fmt being ARG_PTR_TO_CONST_STR guarantees that var_off is const |
| 6455 | * and map_direct_value_addr is set. |
| 6456 | */ |
| 6457 | fmt_map_off = fmt_reg->off + fmt_reg->var_off.value; |
| 6458 | err = fmt_map->ops->map_direct_value_addr(fmt_map, &fmt_addr, |
| 6459 | fmt_map_off); |
Florent Revest | 8e8ee10 | 2021-04-23 01:55:42 +0200 | [diff] [blame] | 6460 | if (err) { |
| 6461 | verbose(env, "verifier bug\n"); |
| 6462 | return -EFAULT; |
| 6463 | } |
Florent Revest | 7b15523 | 2021-04-19 17:52:40 +0200 | [diff] [blame] | 6464 | fmt = (char *)(long)fmt_addr + fmt_map_off; |
| 6465 | |
| 6466 | /* We are also guaranteed that fmt+fmt_map_off is NULL terminated, we |
| 6467 | * can focus on validating the format specifiers. |
| 6468 | */ |
Florent Revest | 48cac3f | 2021-04-27 19:43:13 +0200 | [diff] [blame] | 6469 | err = bpf_bprintf_prepare(fmt, UINT_MAX, NULL, NULL, num_args); |
Florent Revest | 7b15523 | 2021-04-19 17:52:40 +0200 | [diff] [blame] | 6470 | if (err < 0) |
| 6471 | verbose(env, "Invalid format string\n"); |
| 6472 | |
| 6473 | return err; |
| 6474 | } |
| 6475 | |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 6476 | static int check_get_func_ip(struct bpf_verifier_env *env) |
| 6477 | { |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 6478 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
| 6479 | int func_id = BPF_FUNC_get_func_ip; |
| 6480 | |
| 6481 | if (type == BPF_PROG_TYPE_TRACING) { |
Jiri Olsa | f92c1e1 | 2021-12-08 20:32:44 +0100 | [diff] [blame] | 6482 | if (!bpf_prog_has_trampoline(env->prog)) { |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 6483 | verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", |
| 6484 | func_id_name(func_id), func_id); |
| 6485 | return -ENOTSUPP; |
| 6486 | } |
| 6487 | return 0; |
Jiri Olsa | 9ffd9f3 | 2021-07-14 11:43:56 +0200 | [diff] [blame] | 6488 | } else if (type == BPF_PROG_TYPE_KPROBE) { |
| 6489 | return 0; |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 6490 | } |
| 6491 | |
| 6492 | verbose(env, "func %s#%d not supported for program type %d\n", |
| 6493 | func_id_name(func_id), func_id, type); |
| 6494 | return -ENOTSUPP; |
| 6495 | } |
| 6496 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6497 | static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 6498 | int *insn_idx_p) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6499 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6500 | const struct bpf_func_proto *fn = NULL; |
Hao Luo | 3c48073 | 2021-12-16 16:31:46 -0800 | [diff] [blame] | 6501 | enum bpf_return_type ret_type; |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6502 | enum bpf_type_flag ret_flag; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6503 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 6504 | struct bpf_call_arg_meta meta; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6505 | int insn_idx = *insn_idx_p; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6506 | bool changes_data; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6507 | int i, err, func_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6508 | |
| 6509 | /* find function prototype */ |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6510 | func_id = insn->imm; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6511 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6512 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 6513 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6514 | return -EINVAL; |
| 6515 | } |
| 6516 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 6517 | if (env->ops->get_func_proto) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 6518 | fn = env->ops->get_func_proto(func_id, env->prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6519 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6520 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 6521 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6522 | return -EINVAL; |
| 6523 | } |
| 6524 | |
| 6525 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 6526 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Daniel Borkmann | 3fe2867 | 2018-06-02 23:06:33 +0200 | [diff] [blame] | 6527 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6528 | return -EINVAL; |
| 6529 | } |
| 6530 | |
Jiri Olsa | eae2e83 | 2020-08-25 21:21:19 +0200 | [diff] [blame] | 6531 | if (fn->allowed && !fn->allowed(env->prog)) { |
| 6532 | verbose(env, "helper call is not allowed in probe\n"); |
| 6533 | return -EINVAL; |
| 6534 | } |
| 6535 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 6536 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 6537 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 6538 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 6539 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 6540 | func_id_name(func_id), func_id); |
| 6541 | return -EINVAL; |
| 6542 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6543 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 6544 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6545 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 6546 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 6547 | err = check_func_proto(fn, func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 6548 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6549 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 6550 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 6551 | return err; |
| 6552 | } |
| 6553 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6554 | meta.func_id = func_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6555 | /* check args */ |
Dmitrii Banshchikov | 523a4cf | 2021-02-26 00:26:29 +0400 | [diff] [blame] | 6556 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 6557 | err = check_func_arg(env, i, &meta, fn); |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 6558 | if (err) |
| 6559 | return err; |
| 6560 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6561 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6562 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 6563 | if (err) |
| 6564 | return err; |
| 6565 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 6566 | err = record_func_key(env, &meta, func_id, insn_idx); |
| 6567 | if (err) |
| 6568 | return err; |
| 6569 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 6570 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 6571 | * is inferred from register state. |
| 6572 | */ |
| 6573 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 6574 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 6575 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 6576 | if (err) |
| 6577 | return err; |
| 6578 | } |
| 6579 | |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6580 | if (is_release_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 6581 | err = release_reference(env, meta.ref_obj_id); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 6582 | if (err) { |
| 6583 | verbose(env, "func %s#%d reference has not been acquired before\n", |
| 6584 | func_id_name(func_id), func_id); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6585 | return err; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 6586 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6587 | } |
| 6588 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6589 | regs = cur_regs(env); |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 6590 | |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6591 | switch (func_id) { |
| 6592 | case BPF_FUNC_tail_call: |
| 6593 | err = check_reference_leak(env); |
| 6594 | if (err) { |
| 6595 | verbose(env, "tail_call would lead to reference leak\n"); |
| 6596 | return err; |
| 6597 | } |
| 6598 | break; |
| 6599 | case BPF_FUNC_get_local_storage: |
| 6600 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 6601 | * this is required because get_local_storage() can't return an error. |
| 6602 | */ |
| 6603 | if (!register_is_null(®s[BPF_REG_2])) { |
| 6604 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 6605 | return -EINVAL; |
| 6606 | } |
| 6607 | break; |
| 6608 | case BPF_FUNC_for_each_map_elem: |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6609 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
| 6610 | set_map_elem_callback_state); |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6611 | break; |
| 6612 | case BPF_FUNC_timer_set_callback: |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 6613 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
| 6614 | set_timer_callback_state); |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6615 | break; |
| 6616 | case BPF_FUNC_find_vma: |
Song Liu | 7c7e3d3 | 2021-11-05 16:23:29 -0700 | [diff] [blame] | 6617 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
| 6618 | set_find_vma_callback_state); |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6619 | break; |
| 6620 | case BPF_FUNC_snprintf: |
| 6621 | err = check_bpf_snprintf_call(env, regs); |
| 6622 | break; |
| 6623 | case BPF_FUNC_loop: |
| 6624 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
| 6625 | set_loop_callback_state); |
| 6626 | break; |
Song Liu | 7c7e3d3 | 2021-11-05 16:23:29 -0700 | [diff] [blame] | 6627 | } |
| 6628 | |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6629 | if (err) |
| 6630 | return err; |
Florent Revest | 7b15523 | 2021-04-19 17:52:40 +0200 | [diff] [blame] | 6631 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6632 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6633 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6634 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6635 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 6636 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6637 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6638 | /* helper call returns 64-bit value. */ |
| 6639 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 6640 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6641 | /* update return register (already marked as written above) */ |
Hao Luo | 3c48073 | 2021-12-16 16:31:46 -0800 | [diff] [blame] | 6642 | ret_type = fn->ret_type; |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6643 | ret_flag = type_flag(fn->ret_type); |
Hao Luo | 3c48073 | 2021-12-16 16:31:46 -0800 | [diff] [blame] | 6644 | if (ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6645 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6646 | mark_reg_unknown(env, regs, BPF_REG_0); |
Hao Luo | 3c48073 | 2021-12-16 16:31:46 -0800 | [diff] [blame] | 6647 | } else if (ret_type == RET_VOID) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6648 | regs[BPF_REG_0].type = NOT_INIT; |
Hao Luo | 3c48073 | 2021-12-16 16:31:46 -0800 | [diff] [blame] | 6649 | } else if (base_type(ret_type) == RET_PTR_TO_MAP_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6650 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6651 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6652 | /* remember map_ptr, so that check_map_access() |
| 6653 | * can check 'value_size' boundary of memory access |
| 6654 | * to map element returned from bpf_map_lookup_elem() |
| 6655 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 6656 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6657 | verbose(env, |
| 6658 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6659 | return -EINVAL; |
| 6660 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 6661 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Alexei Starovoitov | 3e8ce29 | 2021-07-14 17:54:11 -0700 | [diff] [blame] | 6662 | regs[BPF_REG_0].map_uid = meta.map_uid; |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6663 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE | ret_flag; |
| 6664 | if (!type_may_be_null(ret_type) && |
| 6665 | map_value_has_spin_lock(meta.map_ptr)) { |
| 6666 | regs[BPF_REG_0].id = ++env->id_gen; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 6667 | } |
Hao Luo | 3c48073 | 2021-12-16 16:31:46 -0800 | [diff] [blame] | 6668 | } else if (base_type(ret_type) == RET_PTR_TO_SOCKET) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6669 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6670 | regs[BPF_REG_0].type = PTR_TO_SOCKET | ret_flag; |
Hao Luo | 3c48073 | 2021-12-16 16:31:46 -0800 | [diff] [blame] | 6671 | } else if (base_type(ret_type) == RET_PTR_TO_SOCK_COMMON) { |
Lorenz Bauer | 85a51f8 | 2019-03-22 09:54:00 +0800 | [diff] [blame] | 6672 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6673 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON | ret_flag; |
Hao Luo | 3c48073 | 2021-12-16 16:31:46 -0800 | [diff] [blame] | 6674 | } else if (base_type(ret_type) == RET_PTR_TO_TCP_SOCK) { |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 6675 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6676 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK | ret_flag; |
Hao Luo | 3c48073 | 2021-12-16 16:31:46 -0800 | [diff] [blame] | 6677 | } else if (base_type(ret_type) == RET_PTR_TO_ALLOC_MEM) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 6678 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6679 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 6680 | regs[BPF_REG_0].mem_size = meta.mem_size; |
Hao Luo | 3c48073 | 2021-12-16 16:31:46 -0800 | [diff] [blame] | 6681 | } else if (base_type(ret_type) == RET_PTR_TO_MEM_OR_BTF_ID) { |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 6682 | const struct btf_type *t; |
| 6683 | |
| 6684 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 6685 | t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 6686 | if (!btf_type_is_struct(t)) { |
| 6687 | u32 tsize; |
| 6688 | const struct btf_type *ret; |
| 6689 | const char *tname; |
| 6690 | |
| 6691 | /* resolve the type size of ksym. */ |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 6692 | ret = btf_resolve_size(meta.ret_btf, t, &tsize); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 6693 | if (IS_ERR(ret)) { |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 6694 | tname = btf_name_by_offset(meta.ret_btf, t->name_off); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 6695 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
| 6696 | tname, PTR_ERR(ret)); |
| 6697 | return -EINVAL; |
| 6698 | } |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6699 | regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 6700 | regs[BPF_REG_0].mem_size = tsize; |
| 6701 | } else { |
Hao Luo | 34d3a78 | 2021-12-16 16:31:50 -0800 | [diff] [blame] | 6702 | /* MEM_RDONLY may be carried from ret_flag, but it |
| 6703 | * doesn't apply on PTR_TO_BTF_ID. Fold it, otherwise |
| 6704 | * it will confuse the check of PTR_TO_BTF_ID in |
| 6705 | * check_mem_access(). |
| 6706 | */ |
| 6707 | ret_flag &= ~MEM_RDONLY; |
| 6708 | |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6709 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 6710 | regs[BPF_REG_0].btf = meta.ret_btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 6711 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
| 6712 | } |
Hao Luo | 3c48073 | 2021-12-16 16:31:46 -0800 | [diff] [blame] | 6713 | } else if (base_type(ret_type) == RET_PTR_TO_BTF_ID) { |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 6714 | int ret_btf_id; |
| 6715 | |
| 6716 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6717 | regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag; |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 6718 | ret_btf_id = *fn->ret_btf_id; |
| 6719 | if (ret_btf_id == 0) { |
Hao Luo | 3c48073 | 2021-12-16 16:31:46 -0800 | [diff] [blame] | 6720 | verbose(env, "invalid return type %u of func %s#%d\n", |
| 6721 | base_type(ret_type), func_id_name(func_id), |
| 6722 | func_id); |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 6723 | return -EINVAL; |
| 6724 | } |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 6725 | /* current BPF helper definitions are only coming from |
| 6726 | * built-in code with type IDs from vmlinux BTF |
| 6727 | */ |
| 6728 | regs[BPF_REG_0].btf = btf_vmlinux; |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 6729 | regs[BPF_REG_0].btf_id = ret_btf_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6730 | } else { |
Hao Luo | 3c48073 | 2021-12-16 16:31:46 -0800 | [diff] [blame] | 6731 | verbose(env, "unknown return type %u of func %s#%d\n", |
| 6732 | base_type(ret_type), func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6733 | return -EINVAL; |
| 6734 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 6735 | |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6736 | if (type_may_be_null(regs[BPF_REG_0].type)) |
Martin KaFai Lau | 93c230e | 2020-10-19 12:42:12 -0700 | [diff] [blame] | 6737 | regs[BPF_REG_0].id = ++env->id_gen; |
| 6738 | |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 6739 | if (is_ptr_cast_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 6740 | /* For release_reference() */ |
| 6741 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 6742 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 6743 | int id = acquire_reference_state(env, insn_idx); |
| 6744 | |
| 6745 | if (id < 0) |
| 6746 | return id; |
| 6747 | /* For mark_ptr_or_null_reg() */ |
| 6748 | regs[BPF_REG_0].id = id; |
| 6749 | /* For release_reference() */ |
| 6750 | regs[BPF_REG_0].ref_obj_id = id; |
| 6751 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 6752 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 6753 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
| 6754 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6755 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 6756 | if (err) |
| 6757 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 6758 | |
Song Liu | fa28dcb | 2020-06-29 23:28:44 -0700 | [diff] [blame] | 6759 | if ((func_id == BPF_FUNC_get_stack || |
| 6760 | func_id == BPF_FUNC_get_task_stack) && |
| 6761 | !env->prog->has_callchain_buf) { |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 6762 | const char *err_str; |
| 6763 | |
| 6764 | #ifdef CONFIG_PERF_EVENTS |
| 6765 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 6766 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 6767 | #else |
| 6768 | err = -ENOTSUPP; |
| 6769 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 6770 | #endif |
| 6771 | if (err) { |
| 6772 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 6773 | return err; |
| 6774 | } |
| 6775 | |
| 6776 | env->prog->has_callchain_buf = true; |
| 6777 | } |
| 6778 | |
Song Liu | 5d99cb2c | 2020-07-23 11:06:45 -0700 | [diff] [blame] | 6779 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
| 6780 | env->prog->call_get_stack = true; |
| 6781 | |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 6782 | if (func_id == BPF_FUNC_get_func_ip) { |
| 6783 | if (check_get_func_ip(env)) |
| 6784 | return -ENOTSUPP; |
| 6785 | env->prog->call_get_func_ip = true; |
| 6786 | } |
| 6787 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6788 | if (changes_data) |
| 6789 | clear_all_pkt_pointers(env); |
| 6790 | return 0; |
| 6791 | } |
| 6792 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6793 | /* mark_btf_func_reg_size() is used when the reg size is determined by |
| 6794 | * the BTF func_proto's return value size and argument. |
| 6795 | */ |
| 6796 | static void mark_btf_func_reg_size(struct bpf_verifier_env *env, u32 regno, |
| 6797 | size_t reg_size) |
| 6798 | { |
| 6799 | struct bpf_reg_state *reg = &cur_regs(env)[regno]; |
| 6800 | |
| 6801 | if (regno == BPF_REG_0) { |
| 6802 | /* Function return value */ |
| 6803 | reg->live |= REG_LIVE_WRITTEN; |
| 6804 | reg->subreg_def = reg_size == sizeof(u64) ? |
| 6805 | DEF_NOT_SUBREG : env->insn_idx + 1; |
| 6806 | } else { |
| 6807 | /* Function argument */ |
| 6808 | if (reg_size == sizeof(u64)) { |
| 6809 | mark_insn_zext(env, reg); |
| 6810 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
| 6811 | } else { |
| 6812 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ32); |
| 6813 | } |
| 6814 | } |
| 6815 | } |
| 6816 | |
| 6817 | static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 6818 | { |
| 6819 | const struct btf_type *t, *func, *func_proto, *ptr_type; |
| 6820 | struct bpf_reg_state *regs = cur_regs(env); |
| 6821 | const char *func_name, *ptr_type_name; |
| 6822 | u32 i, nargs, func_id, ptr_type_id; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6823 | struct module *btf_mod = NULL; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6824 | const struct btf_param *args; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6825 | struct btf *desc_btf; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6826 | int err; |
| 6827 | |
Kumar Kartikeya Dwivedi | a5d8272 | 2021-10-02 06:47:50 +0530 | [diff] [blame] | 6828 | /* skip for now, but return error when we find this in fixup_kfunc_call */ |
| 6829 | if (!insn->imm) |
| 6830 | return 0; |
| 6831 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6832 | desc_btf = find_kfunc_desc_btf(env, insn->imm, insn->off, &btf_mod); |
| 6833 | if (IS_ERR(desc_btf)) |
| 6834 | return PTR_ERR(desc_btf); |
| 6835 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6836 | func_id = insn->imm; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6837 | func = btf_type_by_id(desc_btf, func_id); |
| 6838 | func_name = btf_name_by_offset(desc_btf, func->name_off); |
| 6839 | func_proto = btf_type_by_id(desc_btf, func->type); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6840 | |
| 6841 | if (!env->ops->check_kfunc_call || |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6842 | !env->ops->check_kfunc_call(func_id, btf_mod)) { |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6843 | verbose(env, "calling kernel function %s is not allowed\n", |
| 6844 | func_name); |
| 6845 | return -EACCES; |
| 6846 | } |
| 6847 | |
| 6848 | /* Check the arguments */ |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6849 | err = btf_check_kfunc_arg_match(env, desc_btf, func_id, regs); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6850 | if (err) |
| 6851 | return err; |
| 6852 | |
| 6853 | for (i = 0; i < CALLER_SAVED_REGS; i++) |
| 6854 | mark_reg_not_init(env, regs, caller_saved[i]); |
| 6855 | |
| 6856 | /* Check return type */ |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6857 | t = btf_type_skip_modifiers(desc_btf, func_proto->type, NULL); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6858 | if (btf_type_is_scalar(t)) { |
| 6859 | mark_reg_unknown(env, regs, BPF_REG_0); |
| 6860 | mark_btf_func_reg_size(env, BPF_REG_0, t->size); |
| 6861 | } else if (btf_type_is_ptr(t)) { |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6862 | ptr_type = btf_type_skip_modifiers(desc_btf, t->type, |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6863 | &ptr_type_id); |
| 6864 | if (!btf_type_is_struct(ptr_type)) { |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6865 | ptr_type_name = btf_name_by_offset(desc_btf, |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6866 | ptr_type->name_off); |
| 6867 | verbose(env, "kernel function %s returns pointer type %s %s is not supported\n", |
| 6868 | func_name, btf_type_str(ptr_type), |
| 6869 | ptr_type_name); |
| 6870 | return -EINVAL; |
| 6871 | } |
| 6872 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6873 | regs[BPF_REG_0].btf = desc_btf; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6874 | regs[BPF_REG_0].type = PTR_TO_BTF_ID; |
| 6875 | regs[BPF_REG_0].btf_id = ptr_type_id; |
| 6876 | mark_btf_func_reg_size(env, BPF_REG_0, sizeof(void *)); |
| 6877 | } /* else { add_kfunc_call() ensures it is btf_type_is_void(t) } */ |
| 6878 | |
| 6879 | nargs = btf_type_vlen(func_proto); |
| 6880 | args = (const struct btf_param *)(func_proto + 1); |
| 6881 | for (i = 0; i < nargs; i++) { |
| 6882 | u32 regno = i + 1; |
| 6883 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6884 | t = btf_type_skip_modifiers(desc_btf, args[i].type, NULL); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6885 | if (btf_type_is_ptr(t)) |
| 6886 | mark_btf_func_reg_size(env, regno, sizeof(void *)); |
| 6887 | else |
| 6888 | /* scalar. ensured by btf_check_kfunc_arg_match() */ |
| 6889 | mark_btf_func_reg_size(env, regno, t->size); |
| 6890 | } |
| 6891 | |
| 6892 | return 0; |
| 6893 | } |
| 6894 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6895 | static bool signed_add_overflows(s64 a, s64 b) |
| 6896 | { |
| 6897 | /* Do the add in u64, where overflow is well-defined */ |
| 6898 | s64 res = (s64)((u64)a + (u64)b); |
| 6899 | |
| 6900 | if (b < 0) |
| 6901 | return res > a; |
| 6902 | return res < a; |
| 6903 | } |
| 6904 | |
Daniel Borkmann | bc895e8 | 2021-01-20 00:24:24 +0100 | [diff] [blame] | 6905 | static bool signed_add32_overflows(s32 a, s32 b) |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6906 | { |
| 6907 | /* Do the add in u32, where overflow is well-defined */ |
| 6908 | s32 res = (s32)((u32)a + (u32)b); |
| 6909 | |
| 6910 | if (b < 0) |
| 6911 | return res > a; |
| 6912 | return res < a; |
| 6913 | } |
| 6914 | |
Daniel Borkmann | bc895e8 | 2021-01-20 00:24:24 +0100 | [diff] [blame] | 6915 | static bool signed_sub_overflows(s64 a, s64 b) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6916 | { |
| 6917 | /* Do the sub in u64, where overflow is well-defined */ |
| 6918 | s64 res = (s64)((u64)a - (u64)b); |
| 6919 | |
| 6920 | if (b < 0) |
| 6921 | return res < a; |
| 6922 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 6923 | } |
| 6924 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6925 | static bool signed_sub32_overflows(s32 a, s32 b) |
| 6926 | { |
Daniel Borkmann | bc895e8 | 2021-01-20 00:24:24 +0100 | [diff] [blame] | 6927 | /* Do the sub in u32, where overflow is well-defined */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6928 | s32 res = (s32)((u32)a - (u32)b); |
| 6929 | |
| 6930 | if (b < 0) |
| 6931 | return res < a; |
| 6932 | return res > a; |
| 6933 | } |
| 6934 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 6935 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 6936 | const struct bpf_reg_state *reg, |
| 6937 | enum bpf_reg_type type) |
| 6938 | { |
| 6939 | bool known = tnum_is_const(reg->var_off); |
| 6940 | s64 val = reg->var_off.value; |
| 6941 | s64 smin = reg->smin_value; |
| 6942 | |
| 6943 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 6944 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6945 | reg_type_str(env, type), val); |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 6946 | return false; |
| 6947 | } |
| 6948 | |
| 6949 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 6950 | verbose(env, "%s pointer offset %d is not allowed\n", |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6951 | reg_type_str(env, type), reg->off); |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 6952 | return false; |
| 6953 | } |
| 6954 | |
| 6955 | if (smin == S64_MIN) { |
| 6956 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6957 | reg_type_str(env, type)); |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 6958 | return false; |
| 6959 | } |
| 6960 | |
| 6961 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 6962 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 6963 | smin, reg_type_str(env, type)); |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 6964 | return false; |
| 6965 | } |
| 6966 | |
| 6967 | return true; |
| 6968 | } |
| 6969 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6970 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
| 6971 | { |
| 6972 | return &env->insn_aux_data[env->insn_idx]; |
| 6973 | } |
| 6974 | |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 6975 | enum { |
| 6976 | REASON_BOUNDS = -1, |
| 6977 | REASON_TYPE = -2, |
| 6978 | REASON_PATHS = -3, |
| 6979 | REASON_LIMIT = -4, |
| 6980 | REASON_STACK = -5, |
| 6981 | }; |
| 6982 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6983 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
Daniel Borkmann | bb01a1b | 2021-05-21 10:19:22 +0000 | [diff] [blame] | 6984 | u32 *alu_limit, bool mask_to_left) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6985 | { |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 6986 | u32 max = 0, ptr_limit = 0; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6987 | |
| 6988 | switch (ptr_reg->type) { |
| 6989 | case PTR_TO_STACK: |
Piotr Krysiuk | 1b1597e | 2021-03-16 09:47:02 +0100 | [diff] [blame] | 6990 | /* Offset 0 is out-of-bounds, but acceptable start for the |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 6991 | * left direction, see BPF_REG_FP. Also, unknown scalar |
| 6992 | * offset where we would need to deal with min/max bounds is |
| 6993 | * currently prohibited for unprivileged. |
Piotr Krysiuk | 1b1597e | 2021-03-16 09:47:02 +0100 | [diff] [blame] | 6994 | */ |
| 6995 | max = MAX_BPF_STACK + mask_to_left; |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 6996 | ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off); |
Daniel Borkmann | b658bbb | 2021-03-23 09:04:10 +0100 | [diff] [blame] | 6997 | break; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6998 | case PTR_TO_MAP_VALUE: |
Piotr Krysiuk | 1b1597e | 2021-03-16 09:47:02 +0100 | [diff] [blame] | 6999 | max = ptr_reg->map_ptr->value_size; |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7000 | ptr_limit = (mask_to_left ? |
| 7001 | ptr_reg->smin_value : |
| 7002 | ptr_reg->umax_value) + ptr_reg->off; |
Daniel Borkmann | b658bbb | 2021-03-23 09:04:10 +0100 | [diff] [blame] | 7003 | break; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7004 | default: |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 7005 | return REASON_TYPE; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7006 | } |
Daniel Borkmann | b658bbb | 2021-03-23 09:04:10 +0100 | [diff] [blame] | 7007 | |
| 7008 | if (ptr_limit >= max) |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 7009 | return REASON_LIMIT; |
Daniel Borkmann | b658bbb | 2021-03-23 09:04:10 +0100 | [diff] [blame] | 7010 | *alu_limit = ptr_limit; |
| 7011 | return 0; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7012 | } |
| 7013 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 7014 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
| 7015 | const struct bpf_insn *insn) |
| 7016 | { |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 7017 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 7018 | } |
| 7019 | |
| 7020 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, |
| 7021 | u32 alu_state, u32 alu_limit) |
| 7022 | { |
| 7023 | /* If we arrived here from different branches with different |
| 7024 | * state or limits to sanitize, then this won't work. |
| 7025 | */ |
| 7026 | if (aux->alu_state && |
| 7027 | (aux->alu_state != alu_state || |
| 7028 | aux->alu_limit != alu_limit)) |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 7029 | return REASON_PATHS; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 7030 | |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 7031 | /* Corresponding fixup done in do_misc_fixups(). */ |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 7032 | aux->alu_state = alu_state; |
| 7033 | aux->alu_limit = alu_limit; |
| 7034 | return 0; |
| 7035 | } |
| 7036 | |
| 7037 | static int sanitize_val_alu(struct bpf_verifier_env *env, |
| 7038 | struct bpf_insn *insn) |
| 7039 | { |
| 7040 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 7041 | |
| 7042 | if (can_skip_alu_sanitation(env, insn)) |
| 7043 | return 0; |
| 7044 | |
| 7045 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); |
| 7046 | } |
| 7047 | |
Daniel Borkmann | f528819 | 2021-03-24 11:25:39 +0100 | [diff] [blame] | 7048 | static bool sanitize_needed(u8 opcode) |
| 7049 | { |
| 7050 | return opcode == BPF_ADD || opcode == BPF_SUB; |
| 7051 | } |
| 7052 | |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7053 | struct bpf_sanitize_info { |
| 7054 | struct bpf_insn_aux_data aux; |
Daniel Borkmann | bb01a1b | 2021-05-21 10:19:22 +0000 | [diff] [blame] | 7055 | bool mask_to_left; |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7056 | }; |
| 7057 | |
Daniel Borkmann | 9183671a | 2021-05-28 15:47:32 +0000 | [diff] [blame] | 7058 | static struct bpf_verifier_state * |
| 7059 | sanitize_speculative_path(struct bpf_verifier_env *env, |
| 7060 | const struct bpf_insn *insn, |
| 7061 | u32 next_idx, u32 curr_idx) |
| 7062 | { |
| 7063 | struct bpf_verifier_state *branch; |
| 7064 | struct bpf_reg_state *regs; |
| 7065 | |
| 7066 | branch = push_stack(env, next_idx, curr_idx, true); |
| 7067 | if (branch && insn) { |
| 7068 | regs = branch->frame[branch->curframe]->regs; |
| 7069 | if (BPF_SRC(insn->code) == BPF_K) { |
| 7070 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 7071 | } else if (BPF_SRC(insn->code) == BPF_X) { |
| 7072 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 7073 | mark_reg_unknown(env, regs, insn->src_reg); |
| 7074 | } |
| 7075 | } |
| 7076 | return branch; |
| 7077 | } |
| 7078 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7079 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
| 7080 | struct bpf_insn *insn, |
| 7081 | const struct bpf_reg_state *ptr_reg, |
Daniel Borkmann | 6f55b2f | 2021-03-22 15:45:52 +0100 | [diff] [blame] | 7082 | const struct bpf_reg_state *off_reg, |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7083 | struct bpf_reg_state *dst_reg, |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7084 | struct bpf_sanitize_info *info, |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7085 | const bool commit_window) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7086 | { |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7087 | struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7088 | struct bpf_verifier_state *vstate = env->cur_state; |
Daniel Borkmann | 801c605 | 2021-04-29 15:19:37 +0000 | [diff] [blame] | 7089 | bool off_is_imm = tnum_is_const(off_reg->var_off); |
Daniel Borkmann | 6f55b2f | 2021-03-22 15:45:52 +0100 | [diff] [blame] | 7090 | bool off_is_neg = off_reg->smin_value < 0; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7091 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
| 7092 | u8 opcode = BPF_OP(insn->code); |
| 7093 | u32 alu_state, alu_limit; |
| 7094 | struct bpf_reg_state tmp; |
| 7095 | bool ret; |
Piotr Krysiuk | f232326 | 2021-03-16 09:47:02 +0100 | [diff] [blame] | 7096 | int err; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7097 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 7098 | if (can_skip_alu_sanitation(env, insn)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7099 | return 0; |
| 7100 | |
| 7101 | /* We already marked aux for masking from non-speculative |
| 7102 | * paths, thus we got here in the first place. We only care |
| 7103 | * to explore bad access from here. |
| 7104 | */ |
| 7105 | if (vstate->speculative) |
| 7106 | goto do_sim; |
| 7107 | |
Daniel Borkmann | bb01a1b | 2021-05-21 10:19:22 +0000 | [diff] [blame] | 7108 | if (!commit_window) { |
| 7109 | if (!tnum_is_const(off_reg->var_off) && |
| 7110 | (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) |
| 7111 | return REASON_BOUNDS; |
| 7112 | |
| 7113 | info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || |
| 7114 | (opcode == BPF_SUB && !off_is_neg); |
| 7115 | } |
| 7116 | |
| 7117 | err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); |
Piotr Krysiuk | f232326 | 2021-03-16 09:47:02 +0100 | [diff] [blame] | 7118 | if (err < 0) |
| 7119 | return err; |
| 7120 | |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7121 | if (commit_window) { |
| 7122 | /* In commit phase we narrow the masking window based on |
| 7123 | * the observed pointer move after the simulated operation. |
| 7124 | */ |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7125 | alu_state = info->aux.alu_state; |
| 7126 | alu_limit = abs(info->aux.alu_limit - alu_limit); |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7127 | } else { |
| 7128 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; |
Daniel Borkmann | 801c605 | 2021-04-29 15:19:37 +0000 | [diff] [blame] | 7129 | alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0; |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7130 | alu_state |= ptr_is_dst_reg ? |
| 7131 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 7132 | |
| 7133 | /* Limit pruning on unknown scalars to enable deep search for |
| 7134 | * potential masking differences from other program paths. |
| 7135 | */ |
| 7136 | if (!off_is_imm) |
| 7137 | env->explore_alu_limits = true; |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7138 | } |
| 7139 | |
Piotr Krysiuk | f232326 | 2021-03-16 09:47:02 +0100 | [diff] [blame] | 7140 | err = update_alu_sanitation_state(aux, alu_state, alu_limit); |
| 7141 | if (err < 0) |
| 7142 | return err; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7143 | do_sim: |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7144 | /* If we're in commit phase, we're done here given we already |
| 7145 | * pushed the truncated dst_reg into the speculative verification |
| 7146 | * stack. |
Daniel Borkmann | a703619 | 2021-05-04 08:58:25 +0000 | [diff] [blame] | 7147 | * |
| 7148 | * Also, when register is a known constant, we rewrite register-based |
| 7149 | * operation to immediate-based, and thus do not need masking (and as |
| 7150 | * a consequence, do not need to simulate the zero-truncation either). |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7151 | */ |
Daniel Borkmann | a703619 | 2021-05-04 08:58:25 +0000 | [diff] [blame] | 7152 | if (commit_window || off_is_imm) |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7153 | return 0; |
| 7154 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7155 | /* Simulate and find potential out-of-bounds access under |
| 7156 | * speculative execution from truncation as a result of |
| 7157 | * masking when off was not within expected range. If off |
| 7158 | * sits in dst, then we temporarily need to move ptr there |
| 7159 | * to simulate dst (== 0) +/-= ptr. Needed, for example, |
| 7160 | * for cases where we use K-based arithmetic in one direction |
| 7161 | * and truncated reg-based in the other in order to explore |
| 7162 | * bad access. |
| 7163 | */ |
| 7164 | if (!ptr_is_dst_reg) { |
| 7165 | tmp = *dst_reg; |
| 7166 | *dst_reg = *ptr_reg; |
| 7167 | } |
Daniel Borkmann | 9183671a | 2021-05-28 15:47:32 +0000 | [diff] [blame] | 7168 | ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, |
| 7169 | env->insn_idx); |
Xu Yu | 0803278 | 2019-03-21 18:00:35 +0800 | [diff] [blame] | 7170 | if (!ptr_is_dst_reg && ret) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7171 | *dst_reg = tmp; |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 7172 | return !ret ? REASON_STACK : 0; |
| 7173 | } |
| 7174 | |
Daniel Borkmann | fe9a5ca | 2021-05-28 13:47:27 +0000 | [diff] [blame] | 7175 | static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) |
| 7176 | { |
| 7177 | struct bpf_verifier_state *vstate = env->cur_state; |
| 7178 | |
| 7179 | /* If we simulate paths under speculation, we don't update the |
| 7180 | * insn as 'seen' such that when we verify unreachable paths in |
| 7181 | * the non-speculative domain, sanitize_dead_code() can still |
| 7182 | * rewrite/sanitize them. |
| 7183 | */ |
| 7184 | if (!vstate->speculative) |
| 7185 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
| 7186 | } |
| 7187 | |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 7188 | static int sanitize_err(struct bpf_verifier_env *env, |
| 7189 | const struct bpf_insn *insn, int reason, |
| 7190 | const struct bpf_reg_state *off_reg, |
| 7191 | const struct bpf_reg_state *dst_reg) |
| 7192 | { |
| 7193 | static const char *err = "pointer arithmetic with it prohibited for !root"; |
| 7194 | const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; |
| 7195 | u32 dst = insn->dst_reg, src = insn->src_reg; |
| 7196 | |
| 7197 | switch (reason) { |
| 7198 | case REASON_BOUNDS: |
| 7199 | verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", |
| 7200 | off_reg == dst_reg ? dst : src, err); |
| 7201 | break; |
| 7202 | case REASON_TYPE: |
| 7203 | verbose(env, "R%d has pointer with unsupported alu operation, %s\n", |
| 7204 | off_reg == dst_reg ? src : dst, err); |
| 7205 | break; |
| 7206 | case REASON_PATHS: |
| 7207 | verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", |
| 7208 | dst, op, err); |
| 7209 | break; |
| 7210 | case REASON_LIMIT: |
| 7211 | verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", |
| 7212 | dst, op, err); |
| 7213 | break; |
| 7214 | case REASON_STACK: |
| 7215 | verbose(env, "R%d could not be pushed for speculative verification, %s\n", |
| 7216 | dst, err); |
| 7217 | break; |
| 7218 | default: |
| 7219 | verbose(env, "verifier internal error: unknown reason (%d)\n", |
| 7220 | reason); |
| 7221 | break; |
| 7222 | } |
| 7223 | |
| 7224 | return -EACCES; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7225 | } |
| 7226 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 7227 | /* check that stack access falls within stack limits and that 'reg' doesn't |
| 7228 | * have a variable offset. |
| 7229 | * |
| 7230 | * Variable offset is prohibited for unprivileged mode for simplicity since it |
| 7231 | * requires corresponding support in Spectre masking for stack ALU. See also |
| 7232 | * retrieve_ptr_limit(). |
| 7233 | * |
| 7234 | * |
| 7235 | * 'off' includes 'reg->off'. |
| 7236 | */ |
| 7237 | static int check_stack_access_for_ptr_arithmetic( |
| 7238 | struct bpf_verifier_env *env, |
| 7239 | int regno, |
| 7240 | const struct bpf_reg_state *reg, |
| 7241 | int off) |
| 7242 | { |
| 7243 | if (!tnum_is_const(reg->var_off)) { |
| 7244 | char tn_buf[48]; |
| 7245 | |
| 7246 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 7247 | verbose(env, "R%d variable stack access prohibited for !root, var_off=%s off=%d\n", |
| 7248 | regno, tn_buf, off); |
| 7249 | return -EACCES; |
| 7250 | } |
| 7251 | |
| 7252 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 7253 | verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 7254 | "prohibited for !root; off=%d\n", regno, off); |
| 7255 | return -EACCES; |
| 7256 | } |
| 7257 | |
| 7258 | return 0; |
| 7259 | } |
| 7260 | |
Daniel Borkmann | 073815b | 2021-03-23 15:05:48 +0100 | [diff] [blame] | 7261 | static int sanitize_check_bounds(struct bpf_verifier_env *env, |
| 7262 | const struct bpf_insn *insn, |
| 7263 | const struct bpf_reg_state *dst_reg) |
| 7264 | { |
| 7265 | u32 dst = insn->dst_reg; |
| 7266 | |
| 7267 | /* For unprivileged we require that resulting offset must be in bounds |
| 7268 | * in order to be able to sanitize access later on. |
| 7269 | */ |
| 7270 | if (env->bypass_spec_v1) |
| 7271 | return 0; |
| 7272 | |
| 7273 | switch (dst_reg->type) { |
| 7274 | case PTR_TO_STACK: |
| 7275 | if (check_stack_access_for_ptr_arithmetic(env, dst, dst_reg, |
| 7276 | dst_reg->off + dst_reg->var_off.value)) |
| 7277 | return -EACCES; |
| 7278 | break; |
| 7279 | case PTR_TO_MAP_VALUE: |
| 7280 | if (check_map_access(env, dst, dst_reg->off, 1, false)) { |
| 7281 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
| 7282 | "prohibited for !root\n", dst); |
| 7283 | return -EACCES; |
| 7284 | } |
| 7285 | break; |
| 7286 | default: |
| 7287 | break; |
| 7288 | } |
| 7289 | |
| 7290 | return 0; |
| 7291 | } |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 7292 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7293 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7294 | * Caller should also handle BPF_MOV case separately. |
| 7295 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 7296 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 7297 | */ |
| 7298 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 7299 | struct bpf_insn *insn, |
| 7300 | const struct bpf_reg_state *ptr_reg, |
| 7301 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7302 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7303 | struct bpf_verifier_state *vstate = env->cur_state; |
| 7304 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 7305 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7306 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7307 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 7308 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 7309 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 7310 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7311 | struct bpf_sanitize_info info = {}; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7312 | u8 opcode = BPF_OP(insn->code); |
Daniel Borkmann | 24c109b | 2021-03-23 08:51:02 +0100 | [diff] [blame] | 7313 | u32 dst = insn->dst_reg; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7314 | int ret; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7315 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7316 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7317 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 7318 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 7319 | smin_val > smax_val || umin_val > umax_val) { |
| 7320 | /* Taint dst register if offset had invalid bounds derived from |
| 7321 | * e.g. dead branches. |
| 7322 | */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 7323 | __mark_reg_unknown(env, dst_reg); |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 7324 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7325 | } |
| 7326 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7327 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 7328 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Yonghong Song | 6c69354 | 2020-06-18 16:46:31 -0700 | [diff] [blame] | 7329 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
| 7330 | __mark_reg_unknown(env, dst_reg); |
| 7331 | return 0; |
| 7332 | } |
| 7333 | |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 7334 | verbose(env, |
| 7335 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 7336 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7337 | return -EACCES; |
| 7338 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 7339 | |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 7340 | if (ptr_reg->type & PTR_MAYBE_NULL) { |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 7341 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 7342 | dst, reg_type_str(env, ptr_reg->type)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7343 | return -EACCES; |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 7344 | } |
| 7345 | |
| 7346 | switch (base_type(ptr_reg->type)) { |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 7347 | case CONST_PTR_TO_MAP: |
Yonghong Song | 7c69673 | 2020-09-08 10:57:02 -0700 | [diff] [blame] | 7348 | /* smin_val represents the known value */ |
| 7349 | if (known && smin_val == 0 && opcode == BPF_ADD) |
| 7350 | break; |
Gustavo A. R. Silva | 8731745 | 2020-10-02 18:42:17 -0500 | [diff] [blame] | 7351 | fallthrough; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 7352 | case PTR_TO_PACKET_END: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7353 | case PTR_TO_SOCKET: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 7354 | case PTR_TO_SOCK_COMMON: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 7355 | case PTR_TO_TCP_SOCK: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 7356 | case PTR_TO_XDP_SOCK: |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 7357 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 7358 | dst, reg_type_str(env, ptr_reg->type)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7359 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 7360 | default: |
| 7361 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7362 | } |
| 7363 | |
| 7364 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 7365 | * The id may be overwritten later if we create a new variable offset. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7366 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7367 | dst_reg->type = ptr_reg->type; |
| 7368 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 7369 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 7370 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 7371 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 7372 | return -EINVAL; |
| 7373 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7374 | /* pointer types do not carry 32-bit bounds at the moment. */ |
| 7375 | __mark_reg32_unbounded(dst_reg); |
| 7376 | |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7377 | if (sanitize_needed(opcode)) { |
| 7378 | ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg, |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7379 | &info, false); |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 7380 | if (ret < 0) |
| 7381 | return sanitize_err(env, insn, ret, off_reg, dst_reg); |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7382 | } |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 7383 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7384 | switch (opcode) { |
| 7385 | case BPF_ADD: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7386 | /* We can take a fixed offset as long as it doesn't overflow |
| 7387 | * the s32 'off' field |
| 7388 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7389 | if (known && (ptr_reg->off + smin_val == |
| 7390 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7391 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7392 | dst_reg->smin_value = smin_ptr; |
| 7393 | dst_reg->smax_value = smax_ptr; |
| 7394 | dst_reg->umin_value = umin_ptr; |
| 7395 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7396 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7397 | dst_reg->off = ptr_reg->off + smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 7398 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7399 | break; |
| 7400 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7401 | /* A new variable offset is created. Note that off_reg->off |
| 7402 | * == 0, since it's a scalar. |
| 7403 | * dst_reg gets the pointer type and since some positive |
| 7404 | * integer value was added to the pointer, give it a new 'id' |
| 7405 | * if it's a PTR_TO_PACKET. |
| 7406 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 7407 | * added into the variable offset, and we copy the fixed offset |
| 7408 | * from ptr_reg. |
| 7409 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7410 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 7411 | signed_add_overflows(smax_ptr, smax_val)) { |
| 7412 | dst_reg->smin_value = S64_MIN; |
| 7413 | dst_reg->smax_value = S64_MAX; |
| 7414 | } else { |
| 7415 | dst_reg->smin_value = smin_ptr + smin_val; |
| 7416 | dst_reg->smax_value = smax_ptr + smax_val; |
| 7417 | } |
| 7418 | if (umin_ptr + umin_val < umin_ptr || |
| 7419 | umax_ptr + umax_val < umax_ptr) { |
| 7420 | dst_reg->umin_value = 0; |
| 7421 | dst_reg->umax_value = U64_MAX; |
| 7422 | } else { |
| 7423 | dst_reg->umin_value = umin_ptr + umin_val; |
| 7424 | dst_reg->umax_value = umax_ptr + umax_val; |
| 7425 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7426 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 7427 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 7428 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 7429 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7430 | dst_reg->id = ++env->id_gen; |
| 7431 | /* something was added to pkt_ptr, set range to zero */ |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 7432 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7433 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7434 | break; |
| 7435 | case BPF_SUB: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7436 | if (dst_reg == off_reg) { |
| 7437 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 7438 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 7439 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7440 | return -EACCES; |
| 7441 | } |
| 7442 | /* We don't allow subtraction from FP, because (according to |
| 7443 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 7444 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 7445 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7446 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 7447 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 7448 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7449 | return -EACCES; |
| 7450 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7451 | if (known && (ptr_reg->off - smin_val == |
| 7452 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7453 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7454 | dst_reg->smin_value = smin_ptr; |
| 7455 | dst_reg->smax_value = smax_ptr; |
| 7456 | dst_reg->umin_value = umin_ptr; |
| 7457 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7458 | dst_reg->var_off = ptr_reg->var_off; |
| 7459 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7460 | dst_reg->off = ptr_reg->off - smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 7461 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7462 | break; |
| 7463 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7464 | /* A new variable offset is created. If the subtrahend is known |
| 7465 | * nonnegative, then any reg->range we had before is still good. |
| 7466 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7467 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 7468 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 7469 | /* Overflow possible, we know nothing */ |
| 7470 | dst_reg->smin_value = S64_MIN; |
| 7471 | dst_reg->smax_value = S64_MAX; |
| 7472 | } else { |
| 7473 | dst_reg->smin_value = smin_ptr - smax_val; |
| 7474 | dst_reg->smax_value = smax_ptr - smin_val; |
| 7475 | } |
| 7476 | if (umin_ptr < umax_val) { |
| 7477 | /* Overflow possible, we know nothing */ |
| 7478 | dst_reg->umin_value = 0; |
| 7479 | dst_reg->umax_value = U64_MAX; |
| 7480 | } else { |
| 7481 | /* Cannot overflow (as long as bounds are consistent) */ |
| 7482 | dst_reg->umin_value = umin_ptr - umax_val; |
| 7483 | dst_reg->umax_value = umax_ptr - umin_val; |
| 7484 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7485 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 7486 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 7487 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 7488 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7489 | dst_reg->id = ++env->id_gen; |
| 7490 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7491 | if (smin_val < 0) |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 7492 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7493 | } |
| 7494 | break; |
| 7495 | case BPF_AND: |
| 7496 | case BPF_OR: |
| 7497 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 7498 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 7499 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 7500 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7501 | return -EACCES; |
| 7502 | default: |
| 7503 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 7504 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 7505 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7506 | return -EACCES; |
| 7507 | } |
| 7508 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 7509 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 7510 | return -EINVAL; |
| 7511 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7512 | __update_reg_bounds(dst_reg); |
| 7513 | __reg_deduce_bounds(dst_reg); |
| 7514 | __reg_bound_offset(dst_reg); |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 7515 | |
Daniel Borkmann | 073815b | 2021-03-23 15:05:48 +0100 | [diff] [blame] | 7516 | if (sanitize_check_bounds(env, insn, dst_reg) < 0) |
| 7517 | return -EACCES; |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7518 | if (sanitize_needed(opcode)) { |
| 7519 | ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg, |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7520 | &info, true); |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7521 | if (ret < 0) |
| 7522 | return sanitize_err(env, insn, ret, off_reg, dst_reg); |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 7523 | } |
| 7524 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7525 | return 0; |
| 7526 | } |
| 7527 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7528 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
| 7529 | struct bpf_reg_state *src_reg) |
| 7530 | { |
| 7531 | s32 smin_val = src_reg->s32_min_value; |
| 7532 | s32 smax_val = src_reg->s32_max_value; |
| 7533 | u32 umin_val = src_reg->u32_min_value; |
| 7534 | u32 umax_val = src_reg->u32_max_value; |
| 7535 | |
| 7536 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || |
| 7537 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { |
| 7538 | dst_reg->s32_min_value = S32_MIN; |
| 7539 | dst_reg->s32_max_value = S32_MAX; |
| 7540 | } else { |
| 7541 | dst_reg->s32_min_value += smin_val; |
| 7542 | dst_reg->s32_max_value += smax_val; |
| 7543 | } |
| 7544 | if (dst_reg->u32_min_value + umin_val < umin_val || |
| 7545 | dst_reg->u32_max_value + umax_val < umax_val) { |
| 7546 | dst_reg->u32_min_value = 0; |
| 7547 | dst_reg->u32_max_value = U32_MAX; |
| 7548 | } else { |
| 7549 | dst_reg->u32_min_value += umin_val; |
| 7550 | dst_reg->u32_max_value += umax_val; |
| 7551 | } |
| 7552 | } |
| 7553 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7554 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
| 7555 | struct bpf_reg_state *src_reg) |
| 7556 | { |
| 7557 | s64 smin_val = src_reg->smin_value; |
| 7558 | s64 smax_val = src_reg->smax_value; |
| 7559 | u64 umin_val = src_reg->umin_value; |
| 7560 | u64 umax_val = src_reg->umax_value; |
| 7561 | |
| 7562 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 7563 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 7564 | dst_reg->smin_value = S64_MIN; |
| 7565 | dst_reg->smax_value = S64_MAX; |
| 7566 | } else { |
| 7567 | dst_reg->smin_value += smin_val; |
| 7568 | dst_reg->smax_value += smax_val; |
| 7569 | } |
| 7570 | if (dst_reg->umin_value + umin_val < umin_val || |
| 7571 | dst_reg->umax_value + umax_val < umax_val) { |
| 7572 | dst_reg->umin_value = 0; |
| 7573 | dst_reg->umax_value = U64_MAX; |
| 7574 | } else { |
| 7575 | dst_reg->umin_value += umin_val; |
| 7576 | dst_reg->umax_value += umax_val; |
| 7577 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7578 | } |
| 7579 | |
| 7580 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, |
| 7581 | struct bpf_reg_state *src_reg) |
| 7582 | { |
| 7583 | s32 smin_val = src_reg->s32_min_value; |
| 7584 | s32 smax_val = src_reg->s32_max_value; |
| 7585 | u32 umin_val = src_reg->u32_min_value; |
| 7586 | u32 umax_val = src_reg->u32_max_value; |
| 7587 | |
| 7588 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || |
| 7589 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { |
| 7590 | /* Overflow possible, we know nothing */ |
| 7591 | dst_reg->s32_min_value = S32_MIN; |
| 7592 | dst_reg->s32_max_value = S32_MAX; |
| 7593 | } else { |
| 7594 | dst_reg->s32_min_value -= smax_val; |
| 7595 | dst_reg->s32_max_value -= smin_val; |
| 7596 | } |
| 7597 | if (dst_reg->u32_min_value < umax_val) { |
| 7598 | /* Overflow possible, we know nothing */ |
| 7599 | dst_reg->u32_min_value = 0; |
| 7600 | dst_reg->u32_max_value = U32_MAX; |
| 7601 | } else { |
| 7602 | /* Cannot overflow (as long as bounds are consistent) */ |
| 7603 | dst_reg->u32_min_value -= umax_val; |
| 7604 | dst_reg->u32_max_value -= umin_val; |
| 7605 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7606 | } |
| 7607 | |
| 7608 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, |
| 7609 | struct bpf_reg_state *src_reg) |
| 7610 | { |
| 7611 | s64 smin_val = src_reg->smin_value; |
| 7612 | s64 smax_val = src_reg->smax_value; |
| 7613 | u64 umin_val = src_reg->umin_value; |
| 7614 | u64 umax_val = src_reg->umax_value; |
| 7615 | |
| 7616 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 7617 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 7618 | /* Overflow possible, we know nothing */ |
| 7619 | dst_reg->smin_value = S64_MIN; |
| 7620 | dst_reg->smax_value = S64_MAX; |
| 7621 | } else { |
| 7622 | dst_reg->smin_value -= smax_val; |
| 7623 | dst_reg->smax_value -= smin_val; |
| 7624 | } |
| 7625 | if (dst_reg->umin_value < umax_val) { |
| 7626 | /* Overflow possible, we know nothing */ |
| 7627 | dst_reg->umin_value = 0; |
| 7628 | dst_reg->umax_value = U64_MAX; |
| 7629 | } else { |
| 7630 | /* Cannot overflow (as long as bounds are consistent) */ |
| 7631 | dst_reg->umin_value -= umax_val; |
| 7632 | dst_reg->umax_value -= umin_val; |
| 7633 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7634 | } |
| 7635 | |
| 7636 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, |
| 7637 | struct bpf_reg_state *src_reg) |
| 7638 | { |
| 7639 | s32 smin_val = src_reg->s32_min_value; |
| 7640 | u32 umin_val = src_reg->u32_min_value; |
| 7641 | u32 umax_val = src_reg->u32_max_value; |
| 7642 | |
| 7643 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { |
| 7644 | /* Ain't nobody got time to multiply that sign */ |
| 7645 | __mark_reg32_unbounded(dst_reg); |
| 7646 | return; |
| 7647 | } |
| 7648 | /* Both values are positive, so we can work with unsigned and |
| 7649 | * copy the result to signed (unless it exceeds S32_MAX). |
| 7650 | */ |
| 7651 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { |
| 7652 | /* Potential overflow, we know nothing */ |
| 7653 | __mark_reg32_unbounded(dst_reg); |
| 7654 | return; |
| 7655 | } |
| 7656 | dst_reg->u32_min_value *= umin_val; |
| 7657 | dst_reg->u32_max_value *= umax_val; |
| 7658 | if (dst_reg->u32_max_value > S32_MAX) { |
| 7659 | /* Overflow possible, we know nothing */ |
| 7660 | dst_reg->s32_min_value = S32_MIN; |
| 7661 | dst_reg->s32_max_value = S32_MAX; |
| 7662 | } else { |
| 7663 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 7664 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 7665 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7666 | } |
| 7667 | |
| 7668 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, |
| 7669 | struct bpf_reg_state *src_reg) |
| 7670 | { |
| 7671 | s64 smin_val = src_reg->smin_value; |
| 7672 | u64 umin_val = src_reg->umin_value; |
| 7673 | u64 umax_val = src_reg->umax_value; |
| 7674 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7675 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
| 7676 | /* Ain't nobody got time to multiply that sign */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7677 | __mark_reg64_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7678 | return; |
| 7679 | } |
| 7680 | /* Both values are positive, so we can work with unsigned and |
| 7681 | * copy the result to signed (unless it exceeds S64_MAX). |
| 7682 | */ |
| 7683 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 7684 | /* Potential overflow, we know nothing */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7685 | __mark_reg64_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7686 | return; |
| 7687 | } |
| 7688 | dst_reg->umin_value *= umin_val; |
| 7689 | dst_reg->umax_value *= umax_val; |
| 7690 | if (dst_reg->umax_value > S64_MAX) { |
| 7691 | /* Overflow possible, we know nothing */ |
| 7692 | dst_reg->smin_value = S64_MIN; |
| 7693 | dst_reg->smax_value = S64_MAX; |
| 7694 | } else { |
| 7695 | dst_reg->smin_value = dst_reg->umin_value; |
| 7696 | dst_reg->smax_value = dst_reg->umax_value; |
| 7697 | } |
| 7698 | } |
| 7699 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7700 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
| 7701 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7702 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7703 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 7704 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 7705 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 7706 | s32 smin_val = src_reg->s32_min_value; |
| 7707 | u32 umax_val = src_reg->u32_max_value; |
| 7708 | |
Daniel Borkmann | 049c4e1 | 2021-05-10 13:10:44 +0000 | [diff] [blame] | 7709 | if (src_known && dst_known) { |
| 7710 | __mark_reg32_known(dst_reg, var32_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7711 | return; |
Daniel Borkmann | 049c4e1 | 2021-05-10 13:10:44 +0000 | [diff] [blame] | 7712 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7713 | |
| 7714 | /* We get our minimum from the var_off, since that's inherently |
| 7715 | * bitwise. Our maximum is the minimum of the operands' maxima. |
| 7716 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7717 | dst_reg->u32_min_value = var32_off.value; |
| 7718 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); |
| 7719 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { |
| 7720 | /* Lose signed bounds when ANDing negative numbers, |
| 7721 | * ain't nobody got time for that. |
| 7722 | */ |
| 7723 | dst_reg->s32_min_value = S32_MIN; |
| 7724 | dst_reg->s32_max_value = S32_MAX; |
| 7725 | } else { |
| 7726 | /* ANDing two positives gives a positive, so safe to |
| 7727 | * cast result into s64. |
| 7728 | */ |
| 7729 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 7730 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 7731 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7732 | } |
| 7733 | |
| 7734 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
| 7735 | struct bpf_reg_state *src_reg) |
| 7736 | { |
| 7737 | bool src_known = tnum_is_const(src_reg->var_off); |
| 7738 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 7739 | s64 smin_val = src_reg->smin_value; |
| 7740 | u64 umax_val = src_reg->umax_value; |
| 7741 | |
| 7742 | if (src_known && dst_known) { |
John Fastabend | 4fbb38a | 2020-09-24 11:45:06 -0700 | [diff] [blame] | 7743 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7744 | return; |
| 7745 | } |
| 7746 | |
| 7747 | /* We get our minimum from the var_off, since that's inherently |
| 7748 | * bitwise. Our maximum is the minimum of the operands' maxima. |
| 7749 | */ |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7750 | dst_reg->umin_value = dst_reg->var_off.value; |
| 7751 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 7752 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 7753 | /* Lose signed bounds when ANDing negative numbers, |
| 7754 | * ain't nobody got time for that. |
| 7755 | */ |
| 7756 | dst_reg->smin_value = S64_MIN; |
| 7757 | dst_reg->smax_value = S64_MAX; |
| 7758 | } else { |
| 7759 | /* ANDing two positives gives a positive, so safe to |
| 7760 | * cast result into s64. |
| 7761 | */ |
| 7762 | dst_reg->smin_value = dst_reg->umin_value; |
| 7763 | dst_reg->smax_value = dst_reg->umax_value; |
| 7764 | } |
| 7765 | /* We may learn something more from the var_off */ |
| 7766 | __update_reg_bounds(dst_reg); |
| 7767 | } |
| 7768 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7769 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
| 7770 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7771 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7772 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 7773 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 7774 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
Daniel Borkmann | 5b9fbeb | 2020-10-07 15:48:58 +0200 | [diff] [blame] | 7775 | s32 smin_val = src_reg->s32_min_value; |
| 7776 | u32 umin_val = src_reg->u32_min_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7777 | |
Daniel Borkmann | 049c4e1 | 2021-05-10 13:10:44 +0000 | [diff] [blame] | 7778 | if (src_known && dst_known) { |
| 7779 | __mark_reg32_known(dst_reg, var32_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7780 | return; |
Daniel Borkmann | 049c4e1 | 2021-05-10 13:10:44 +0000 | [diff] [blame] | 7781 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7782 | |
| 7783 | /* We get our maximum from the var_off, and our minimum is the |
| 7784 | * maximum of the operands' minima |
| 7785 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7786 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); |
| 7787 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; |
| 7788 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { |
| 7789 | /* Lose signed bounds when ORing negative numbers, |
| 7790 | * ain't nobody got time for that. |
| 7791 | */ |
| 7792 | dst_reg->s32_min_value = S32_MIN; |
| 7793 | dst_reg->s32_max_value = S32_MAX; |
| 7794 | } else { |
| 7795 | /* ORing two positives gives a positive, so safe to |
| 7796 | * cast result into s64. |
| 7797 | */ |
Daniel Borkmann | 5b9fbeb | 2020-10-07 15:48:58 +0200 | [diff] [blame] | 7798 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 7799 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7800 | } |
| 7801 | } |
| 7802 | |
| 7803 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
| 7804 | struct bpf_reg_state *src_reg) |
| 7805 | { |
| 7806 | bool src_known = tnum_is_const(src_reg->var_off); |
| 7807 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 7808 | s64 smin_val = src_reg->smin_value; |
| 7809 | u64 umin_val = src_reg->umin_value; |
| 7810 | |
| 7811 | if (src_known && dst_known) { |
John Fastabend | 4fbb38a | 2020-09-24 11:45:06 -0700 | [diff] [blame] | 7812 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7813 | return; |
| 7814 | } |
| 7815 | |
| 7816 | /* We get our maximum from the var_off, and our minimum is the |
| 7817 | * maximum of the operands' minima |
| 7818 | */ |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7819 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 7820 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; |
| 7821 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 7822 | /* Lose signed bounds when ORing negative numbers, |
| 7823 | * ain't nobody got time for that. |
| 7824 | */ |
| 7825 | dst_reg->smin_value = S64_MIN; |
| 7826 | dst_reg->smax_value = S64_MAX; |
| 7827 | } else { |
| 7828 | /* ORing two positives gives a positive, so safe to |
| 7829 | * cast result into s64. |
| 7830 | */ |
| 7831 | dst_reg->smin_value = dst_reg->umin_value; |
| 7832 | dst_reg->smax_value = dst_reg->umax_value; |
| 7833 | } |
| 7834 | /* We may learn something more from the var_off */ |
| 7835 | __update_reg_bounds(dst_reg); |
| 7836 | } |
| 7837 | |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 7838 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
| 7839 | struct bpf_reg_state *src_reg) |
| 7840 | { |
| 7841 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 7842 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 7843 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 7844 | s32 smin_val = src_reg->s32_min_value; |
| 7845 | |
Daniel Borkmann | 049c4e1 | 2021-05-10 13:10:44 +0000 | [diff] [blame] | 7846 | if (src_known && dst_known) { |
| 7847 | __mark_reg32_known(dst_reg, var32_off.value); |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 7848 | return; |
Daniel Borkmann | 049c4e1 | 2021-05-10 13:10:44 +0000 | [diff] [blame] | 7849 | } |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 7850 | |
| 7851 | /* We get both minimum and maximum from the var32_off. */ |
| 7852 | dst_reg->u32_min_value = var32_off.value; |
| 7853 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; |
| 7854 | |
| 7855 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { |
| 7856 | /* XORing two positive sign numbers gives a positive, |
| 7857 | * so safe to cast u32 result into s32. |
| 7858 | */ |
| 7859 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 7860 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 7861 | } else { |
| 7862 | dst_reg->s32_min_value = S32_MIN; |
| 7863 | dst_reg->s32_max_value = S32_MAX; |
| 7864 | } |
| 7865 | } |
| 7866 | |
| 7867 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, |
| 7868 | struct bpf_reg_state *src_reg) |
| 7869 | { |
| 7870 | bool src_known = tnum_is_const(src_reg->var_off); |
| 7871 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 7872 | s64 smin_val = src_reg->smin_value; |
| 7873 | |
| 7874 | if (src_known && dst_known) { |
| 7875 | /* dst_reg->var_off.value has been updated earlier */ |
| 7876 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
| 7877 | return; |
| 7878 | } |
| 7879 | |
| 7880 | /* We get both minimum and maximum from the var_off. */ |
| 7881 | dst_reg->umin_value = dst_reg->var_off.value; |
| 7882 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; |
| 7883 | |
| 7884 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { |
| 7885 | /* XORing two positive sign numbers gives a positive, |
| 7886 | * so safe to cast u64 result into s64. |
| 7887 | */ |
| 7888 | dst_reg->smin_value = dst_reg->umin_value; |
| 7889 | dst_reg->smax_value = dst_reg->umax_value; |
| 7890 | } else { |
| 7891 | dst_reg->smin_value = S64_MIN; |
| 7892 | dst_reg->smax_value = S64_MAX; |
| 7893 | } |
| 7894 | |
| 7895 | __update_reg_bounds(dst_reg); |
| 7896 | } |
| 7897 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7898 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 7899 | u64 umin_val, u64 umax_val) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7900 | { |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7901 | /* We lose all sign bit information (except what we can pick |
| 7902 | * up from var_off) |
| 7903 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7904 | dst_reg->s32_min_value = S32_MIN; |
| 7905 | dst_reg->s32_max_value = S32_MAX; |
| 7906 | /* If we might shift our top bit out, then we know nothing */ |
| 7907 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { |
| 7908 | dst_reg->u32_min_value = 0; |
| 7909 | dst_reg->u32_max_value = U32_MAX; |
| 7910 | } else { |
| 7911 | dst_reg->u32_min_value <<= umin_val; |
| 7912 | dst_reg->u32_max_value <<= umax_val; |
| 7913 | } |
| 7914 | } |
| 7915 | |
| 7916 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 7917 | struct bpf_reg_state *src_reg) |
| 7918 | { |
| 7919 | u32 umax_val = src_reg->u32_max_value; |
| 7920 | u32 umin_val = src_reg->u32_min_value; |
| 7921 | /* u32 alu operation will zext upper bits */ |
| 7922 | struct tnum subreg = tnum_subreg(dst_reg->var_off); |
| 7923 | |
| 7924 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); |
| 7925 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); |
| 7926 | /* Not required but being careful mark reg64 bounds as unknown so |
| 7927 | * that we are forced to pick them up from tnum and zext later and |
| 7928 | * if some path skips this step we are still safe. |
| 7929 | */ |
| 7930 | __mark_reg64_unbounded(dst_reg); |
| 7931 | __update_reg32_bounds(dst_reg); |
| 7932 | } |
| 7933 | |
| 7934 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 7935 | u64 umin_val, u64 umax_val) |
| 7936 | { |
| 7937 | /* Special case <<32 because it is a common compiler pattern to sign |
| 7938 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are |
| 7939 | * positive we know this shift will also be positive so we can track |
| 7940 | * bounds correctly. Otherwise we lose all sign bit information except |
| 7941 | * what we can pick up from var_off. Perhaps we can generalize this |
| 7942 | * later to shifts of any length. |
| 7943 | */ |
| 7944 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) |
| 7945 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; |
| 7946 | else |
| 7947 | dst_reg->smax_value = S64_MAX; |
| 7948 | |
| 7949 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) |
| 7950 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; |
| 7951 | else |
| 7952 | dst_reg->smin_value = S64_MIN; |
| 7953 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7954 | /* If we might shift our top bit out, then we know nothing */ |
| 7955 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 7956 | dst_reg->umin_value = 0; |
| 7957 | dst_reg->umax_value = U64_MAX; |
| 7958 | } else { |
| 7959 | dst_reg->umin_value <<= umin_val; |
| 7960 | dst_reg->umax_value <<= umax_val; |
| 7961 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7962 | } |
| 7963 | |
| 7964 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 7965 | struct bpf_reg_state *src_reg) |
| 7966 | { |
| 7967 | u64 umax_val = src_reg->umax_value; |
| 7968 | u64 umin_val = src_reg->umin_value; |
| 7969 | |
| 7970 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ |
| 7971 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); |
| 7972 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); |
| 7973 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7974 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
| 7975 | /* We may learn something more from the var_off */ |
| 7976 | __update_reg_bounds(dst_reg); |
| 7977 | } |
| 7978 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7979 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
| 7980 | struct bpf_reg_state *src_reg) |
| 7981 | { |
| 7982 | struct tnum subreg = tnum_subreg(dst_reg->var_off); |
| 7983 | u32 umax_val = src_reg->u32_max_value; |
| 7984 | u32 umin_val = src_reg->u32_min_value; |
| 7985 | |
| 7986 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 7987 | * be negative, then either: |
| 7988 | * 1) src_reg might be zero, so the sign bit of the result is |
| 7989 | * unknown, so we lose our signed bounds |
| 7990 | * 2) it's known negative, thus the unsigned bounds capture the |
| 7991 | * signed bounds |
| 7992 | * 3) the signed bounds cross zero, so they tell us nothing |
| 7993 | * about the result |
| 7994 | * If the value in dst_reg is known nonnegative, then again the |
Tobias Klauser | 18b24d7 | 2021-01-21 18:43:24 +0100 | [diff] [blame] | 7995 | * unsigned bounds capture the signed bounds. |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7996 | * Thus, in all cases it suffices to blow away our signed bounds |
| 7997 | * and rely on inferring new ones from the unsigned bounds and |
| 7998 | * var_off of the result. |
| 7999 | */ |
| 8000 | dst_reg->s32_min_value = S32_MIN; |
| 8001 | dst_reg->s32_max_value = S32_MAX; |
| 8002 | |
| 8003 | dst_reg->var_off = tnum_rshift(subreg, umin_val); |
| 8004 | dst_reg->u32_min_value >>= umax_val; |
| 8005 | dst_reg->u32_max_value >>= umin_val; |
| 8006 | |
| 8007 | __mark_reg64_unbounded(dst_reg); |
| 8008 | __update_reg32_bounds(dst_reg); |
| 8009 | } |
| 8010 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8011 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
| 8012 | struct bpf_reg_state *src_reg) |
| 8013 | { |
| 8014 | u64 umax_val = src_reg->umax_value; |
| 8015 | u64 umin_val = src_reg->umin_value; |
| 8016 | |
| 8017 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 8018 | * be negative, then either: |
| 8019 | * 1) src_reg might be zero, so the sign bit of the result is |
| 8020 | * unknown, so we lose our signed bounds |
| 8021 | * 2) it's known negative, thus the unsigned bounds capture the |
| 8022 | * signed bounds |
| 8023 | * 3) the signed bounds cross zero, so they tell us nothing |
| 8024 | * about the result |
| 8025 | * If the value in dst_reg is known nonnegative, then again the |
Tobias Klauser | 18b24d7 | 2021-01-21 18:43:24 +0100 | [diff] [blame] | 8026 | * unsigned bounds capture the signed bounds. |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8027 | * Thus, in all cases it suffices to blow away our signed bounds |
| 8028 | * and rely on inferring new ones from the unsigned bounds and |
| 8029 | * var_off of the result. |
| 8030 | */ |
| 8031 | dst_reg->smin_value = S64_MIN; |
| 8032 | dst_reg->smax_value = S64_MAX; |
| 8033 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
| 8034 | dst_reg->umin_value >>= umax_val; |
| 8035 | dst_reg->umax_value >>= umin_val; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8036 | |
| 8037 | /* Its not easy to operate on alu32 bounds here because it depends |
| 8038 | * on bits being shifted in. Take easy way out and mark unbounded |
| 8039 | * so we can recalculate later from tnum. |
| 8040 | */ |
| 8041 | __mark_reg32_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8042 | __update_reg_bounds(dst_reg); |
| 8043 | } |
| 8044 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8045 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
| 8046 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8047 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8048 | u64 umin_val = src_reg->u32_min_value; |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8049 | |
| 8050 | /* Upon reaching here, src_known is true and |
| 8051 | * umax_val is equal to umin_val. |
| 8052 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8053 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
| 8054 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8055 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8056 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
| 8057 | |
| 8058 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 8059 | * dst_reg var_off to refine the result. |
| 8060 | */ |
| 8061 | dst_reg->u32_min_value = 0; |
| 8062 | dst_reg->u32_max_value = U32_MAX; |
| 8063 | |
| 8064 | __mark_reg64_unbounded(dst_reg); |
| 8065 | __update_reg32_bounds(dst_reg); |
| 8066 | } |
| 8067 | |
| 8068 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, |
| 8069 | struct bpf_reg_state *src_reg) |
| 8070 | { |
| 8071 | u64 umin_val = src_reg->umin_value; |
| 8072 | |
| 8073 | /* Upon reaching here, src_known is true and umax_val is equal |
| 8074 | * to umin_val. |
| 8075 | */ |
| 8076 | dst_reg->smin_value >>= umin_val; |
| 8077 | dst_reg->smax_value >>= umin_val; |
| 8078 | |
| 8079 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8080 | |
| 8081 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 8082 | * dst_reg var_off to refine the result. |
| 8083 | */ |
| 8084 | dst_reg->umin_value = 0; |
| 8085 | dst_reg->umax_value = U64_MAX; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8086 | |
| 8087 | /* Its not easy to operate on alu32 bounds here because it depends |
| 8088 | * on bits being shifted in from upper 32-bits. Take easy way out |
| 8089 | * and mark unbounded so we can recalculate later from tnum. |
| 8090 | */ |
| 8091 | __mark_reg32_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8092 | __update_reg_bounds(dst_reg); |
| 8093 | } |
| 8094 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 8095 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 8096 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 8097 | * need extra checks in the 32-bit case. |
| 8098 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8099 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 8100 | struct bpf_insn *insn, |
| 8101 | struct bpf_reg_state *dst_reg, |
| 8102 | struct bpf_reg_state src_reg) |
| 8103 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8104 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8105 | u8 opcode = BPF_OP(insn->code); |
Mao Wenan | b0b3fb6 | 2020-04-18 09:37:35 +0800 | [diff] [blame] | 8106 | bool src_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8107 | s64 smin_val, smax_val; |
| 8108 | u64 umin_val, umax_val; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8109 | s32 s32_min_val, s32_max_val; |
| 8110 | u32 u32_min_val, u32_max_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 8111 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8112 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 8113 | int ret; |
Jann Horn | b799207 | 2018-10-05 18:17:59 +0200 | [diff] [blame] | 8114 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8115 | smin_val = src_reg.smin_value; |
| 8116 | smax_val = src_reg.smax_value; |
| 8117 | umin_val = src_reg.umin_value; |
| 8118 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8119 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8120 | s32_min_val = src_reg.s32_min_value; |
| 8121 | s32_max_val = src_reg.s32_max_value; |
| 8122 | u32_min_val = src_reg.u32_min_value; |
| 8123 | u32_max_val = src_reg.u32_max_value; |
| 8124 | |
| 8125 | if (alu32) { |
| 8126 | src_known = tnum_subreg_is_const(src_reg.var_off); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8127 | if ((src_known && |
| 8128 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || |
| 8129 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { |
| 8130 | /* Taint dst register if offset had invalid bounds |
| 8131 | * derived from e.g. dead branches. |
| 8132 | */ |
| 8133 | __mark_reg_unknown(env, dst_reg); |
| 8134 | return 0; |
| 8135 | } |
| 8136 | } else { |
| 8137 | src_known = tnum_is_const(src_reg.var_off); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8138 | if ((src_known && |
| 8139 | (smin_val != smax_val || umin_val != umax_val)) || |
| 8140 | smin_val > smax_val || umin_val > umax_val) { |
| 8141 | /* Taint dst register if offset had invalid bounds |
| 8142 | * derived from e.g. dead branches. |
| 8143 | */ |
| 8144 | __mark_reg_unknown(env, dst_reg); |
| 8145 | return 0; |
| 8146 | } |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 8147 | } |
| 8148 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 8149 | if (!src_known && |
| 8150 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 8151 | __mark_reg_unknown(env, dst_reg); |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 8152 | return 0; |
| 8153 | } |
| 8154 | |
Daniel Borkmann | f528819 | 2021-03-24 11:25:39 +0100 | [diff] [blame] | 8155 | if (sanitize_needed(opcode)) { |
| 8156 | ret = sanitize_val_alu(env, insn); |
| 8157 | if (ret < 0) |
| 8158 | return sanitize_err(env, insn, ret, NULL, NULL); |
| 8159 | } |
| 8160 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8161 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
| 8162 | * There are two classes of instructions: The first class we track both |
| 8163 | * alu32 and alu64 sign/unsigned bounds independently this provides the |
| 8164 | * greatest amount of precision when alu operations are mixed with jmp32 |
| 8165 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, |
| 8166 | * and BPF_OR. This is possible because these ops have fairly easy to |
| 8167 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. |
| 8168 | * See alu32 verifier tests for examples. The second class of |
| 8169 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy |
| 8170 | * with regards to tracking sign/unsigned bounds because the bits may |
| 8171 | * cross subreg boundaries in the alu64 case. When this happens we mark |
| 8172 | * the reg unbounded in the subreg bound space and use the resulting |
| 8173 | * tnum to calculate an approximation of the sign/unsigned bounds. |
| 8174 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8175 | switch (opcode) { |
| 8176 | case BPF_ADD: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8177 | scalar32_min_max_add(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8178 | scalar_min_max_add(dst_reg, &src_reg); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8179 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8180 | break; |
| 8181 | case BPF_SUB: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8182 | scalar32_min_max_sub(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8183 | scalar_min_max_sub(dst_reg, &src_reg); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8184 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8185 | break; |
| 8186 | case BPF_MUL: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8187 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 8188 | scalar32_min_max_mul(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8189 | scalar_min_max_mul(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8190 | break; |
| 8191 | case BPF_AND: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8192 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
| 8193 | scalar32_min_max_and(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8194 | scalar_min_max_and(dst_reg, &src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8195 | break; |
| 8196 | case BPF_OR: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8197 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
| 8198 | scalar32_min_max_or(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8199 | scalar_min_max_or(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8200 | break; |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 8201 | case BPF_XOR: |
| 8202 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); |
| 8203 | scalar32_min_max_xor(dst_reg, &src_reg); |
| 8204 | scalar_min_max_xor(dst_reg, &src_reg); |
| 8205 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8206 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 8207 | if (umax_val >= insn_bitness) { |
| 8208 | /* Shifts greater than 31 or 63 are undefined. |
| 8209 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8210 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8211 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8212 | break; |
| 8213 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8214 | if (alu32) |
| 8215 | scalar32_min_max_lsh(dst_reg, &src_reg); |
| 8216 | else |
| 8217 | scalar_min_max_lsh(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8218 | break; |
| 8219 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 8220 | if (umax_val >= insn_bitness) { |
| 8221 | /* Shifts greater than 31 or 63 are undefined. |
| 8222 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8223 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8224 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8225 | break; |
| 8226 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8227 | if (alu32) |
| 8228 | scalar32_min_max_rsh(dst_reg, &src_reg); |
| 8229 | else |
| 8230 | scalar_min_max_rsh(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8231 | break; |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 8232 | case BPF_ARSH: |
| 8233 | if (umax_val >= insn_bitness) { |
| 8234 | /* Shifts greater than 31 or 63 are undefined. |
| 8235 | * This includes shifts by a negative number. |
| 8236 | */ |
| 8237 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 8238 | break; |
| 8239 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8240 | if (alu32) |
| 8241 | scalar32_min_max_arsh(dst_reg, &src_reg); |
| 8242 | else |
| 8243 | scalar_min_max_arsh(dst_reg, &src_reg); |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 8244 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8245 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8246 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8247 | break; |
| 8248 | } |
| 8249 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8250 | /* ALU32 ops are zero extended into 64bit register */ |
| 8251 | if (alu32) |
| 8252 | zext_32_to_64(dst_reg); |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 8253 | |
John Fastabend | 294f2fc | 2020-03-24 10:38:37 -0700 | [diff] [blame] | 8254 | __update_reg_bounds(dst_reg); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8255 | __reg_deduce_bounds(dst_reg); |
| 8256 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8257 | return 0; |
| 8258 | } |
| 8259 | |
| 8260 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 8261 | * and var_off. |
| 8262 | */ |
| 8263 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 8264 | struct bpf_insn *insn) |
| 8265 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8266 | struct bpf_verifier_state *vstate = env->cur_state; |
| 8267 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 8268 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8269 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 8270 | u8 opcode = BPF_OP(insn->code); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8271 | int err; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8272 | |
| 8273 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8274 | src_reg = NULL; |
| 8275 | if (dst_reg->type != SCALAR_VALUE) |
| 8276 | ptr_reg = dst_reg; |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 8277 | else |
| 8278 | /* Make sure ID is cleared otherwise dst_reg min/max could be |
| 8279 | * incorrectly propagated into other registers by find_equal_scalars() |
| 8280 | */ |
| 8281 | dst_reg->id = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8282 | if (BPF_SRC(insn->code) == BPF_X) { |
| 8283 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8284 | if (src_reg->type != SCALAR_VALUE) { |
| 8285 | if (dst_reg->type != SCALAR_VALUE) { |
| 8286 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 8287 | * an arbitrary scalar. Disallow all math except |
| 8288 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8289 | */ |
Alexei Starovoitov | dd06682 | 2018-09-12 14:06:10 -0700 | [diff] [blame] | 8290 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 8291 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 8292 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8293 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 8294 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 8295 | insn->dst_reg, |
| 8296 | bpf_alu_string[opcode >> 4]); |
| 8297 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8298 | } else { |
| 8299 | /* scalar += pointer |
| 8300 | * This is legal, but we have to reverse our |
| 8301 | * src/dest handling in computing the range |
| 8302 | */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8303 | err = mark_chain_precision(env, insn->dst_reg); |
| 8304 | if (err) |
| 8305 | return err; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 8306 | return adjust_ptr_min_max_vals(env, insn, |
| 8307 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8308 | } |
| 8309 | } else if (ptr_reg) { |
| 8310 | /* pointer += scalar */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8311 | err = mark_chain_precision(env, insn->src_reg); |
| 8312 | if (err) |
| 8313 | return err; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 8314 | return adjust_ptr_min_max_vals(env, insn, |
| 8315 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8316 | } |
| 8317 | } else { |
| 8318 | /* Pretend the src is a reg with a known value, since we only |
| 8319 | * need to be able to read from this state. |
| 8320 | */ |
| 8321 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8322 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8323 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 8324 | if (ptr_reg) /* pointer += K */ |
| 8325 | return adjust_ptr_min_max_vals(env, insn, |
| 8326 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8327 | } |
| 8328 | |
| 8329 | /* Got here implies adding two SCALAR_VALUEs */ |
| 8330 | if (WARN_ON_ONCE(ptr_reg)) { |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 8331 | print_verifier_state(env, state, true); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8332 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8333 | return -EINVAL; |
| 8334 | } |
| 8335 | if (WARN_ON(!src_reg)) { |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 8336 | print_verifier_state(env, state, true); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8337 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8338 | return -EINVAL; |
| 8339 | } |
| 8340 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8341 | } |
| 8342 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8343 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8344 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8345 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8346 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8347 | u8 opcode = BPF_OP(insn->code); |
| 8348 | int err; |
| 8349 | |
| 8350 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 8351 | if (opcode == BPF_NEG) { |
| 8352 | if (BPF_SRC(insn->code) != 0 || |
| 8353 | insn->src_reg != BPF_REG_0 || |
| 8354 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8355 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8356 | return -EINVAL; |
| 8357 | } |
| 8358 | } else { |
| 8359 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 8360 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 8361 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8362 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8363 | return -EINVAL; |
| 8364 | } |
| 8365 | } |
| 8366 | |
| 8367 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8368 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8369 | if (err) |
| 8370 | return err; |
| 8371 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 8372 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8373 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 8374 | insn->dst_reg); |
| 8375 | return -EACCES; |
| 8376 | } |
| 8377 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8378 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8379 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8380 | if (err) |
| 8381 | return err; |
| 8382 | |
| 8383 | } else if (opcode == BPF_MOV) { |
| 8384 | |
| 8385 | if (BPF_SRC(insn->code) == BPF_X) { |
| 8386 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8387 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8388 | return -EINVAL; |
| 8389 | } |
| 8390 | |
| 8391 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8392 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8393 | if (err) |
| 8394 | return err; |
| 8395 | } else { |
| 8396 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8397 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8398 | return -EINVAL; |
| 8399 | } |
| 8400 | } |
| 8401 | |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 8402 | /* check dest operand, mark as required later */ |
| 8403 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8404 | if (err) |
| 8405 | return err; |
| 8406 | |
| 8407 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 8408 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
| 8409 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; |
| 8410 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8411 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 8412 | /* case: R1 = R2 |
| 8413 | * copy register state to dest reg |
| 8414 | */ |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 8415 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
| 8416 | /* Assign src and dst registers the same ID |
| 8417 | * that will be used by find_equal_scalars() |
| 8418 | * to propagate min/max range. |
| 8419 | */ |
| 8420 | src_reg->id = ++env->id_gen; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 8421 | *dst_reg = *src_reg; |
| 8422 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8423 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8424 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8425 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 8426 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8427 | verbose(env, |
| 8428 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 8429 | insn->src_reg); |
| 8430 | return -EACCES; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 8431 | } else if (src_reg->type == SCALAR_VALUE) { |
| 8432 | *dst_reg = *src_reg; |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 8433 | /* Make sure ID is cleared otherwise |
| 8434 | * dst_reg min/max could be incorrectly |
| 8435 | * propagated into src_reg by find_equal_scalars() |
| 8436 | */ |
| 8437 | dst_reg->id = 0; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 8438 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8439 | dst_reg->subreg_def = env->insn_idx + 1; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 8440 | } else { |
| 8441 | mark_reg_unknown(env, regs, |
| 8442 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 8443 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8444 | zext_32_to_64(dst_reg); |
Daniel Borkmann | 3cf2b61 | 2021-12-15 22:02:19 +0000 | [diff] [blame] | 8445 | |
| 8446 | __update_reg_bounds(dst_reg); |
| 8447 | __reg_deduce_bounds(dst_reg); |
| 8448 | __reg_bound_offset(dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8449 | } |
| 8450 | } else { |
| 8451 | /* case: R = imm |
| 8452 | * remember the value we stored into this reg |
| 8453 | */ |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 8454 | /* clear any state __mark_reg_known doesn't set */ |
| 8455 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8456 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 8457 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 8458 | __mark_reg_known(regs + insn->dst_reg, |
| 8459 | insn->imm); |
| 8460 | } else { |
| 8461 | __mark_reg_known(regs + insn->dst_reg, |
| 8462 | (u32)insn->imm); |
| 8463 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8464 | } |
| 8465 | |
| 8466 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8467 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8468 | return -EINVAL; |
| 8469 | |
| 8470 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 8471 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8472 | if (BPF_SRC(insn->code) == BPF_X) { |
| 8473 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8474 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8475 | return -EINVAL; |
| 8476 | } |
| 8477 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8478 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8479 | if (err) |
| 8480 | return err; |
| 8481 | } else { |
| 8482 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8483 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8484 | return -EINVAL; |
| 8485 | } |
| 8486 | } |
| 8487 | |
| 8488 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8489 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8490 | if (err) |
| 8491 | return err; |
| 8492 | |
| 8493 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 8494 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8495 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8496 | return -EINVAL; |
| 8497 | } |
| 8498 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 8499 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 8500 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 8501 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 8502 | |
| 8503 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8504 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 8505 | return -EINVAL; |
| 8506 | } |
| 8507 | } |
| 8508 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 8509 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8510 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 8511 | if (err) |
| 8512 | return err; |
| 8513 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8514 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8515 | } |
| 8516 | |
| 8517 | return 0; |
| 8518 | } |
| 8519 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 8520 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
| 8521 | struct bpf_reg_state *dst_reg, |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 8522 | enum bpf_reg_type type, int new_range) |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 8523 | { |
| 8524 | struct bpf_reg_state *reg; |
| 8525 | int i; |
| 8526 | |
| 8527 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 8528 | reg = &state->regs[i]; |
| 8529 | if (reg->type == type && reg->id == dst_reg->id) |
| 8530 | /* keep the maximum range already checked */ |
| 8531 | reg->range = max(reg->range, new_range); |
| 8532 | } |
| 8533 | |
| 8534 | bpf_for_each_spilled_reg(i, state, reg) { |
| 8535 | if (!reg) |
| 8536 | continue; |
| 8537 | if (reg->type == type && reg->id == dst_reg->id) |
| 8538 | reg->range = max(reg->range, new_range); |
| 8539 | } |
| 8540 | } |
| 8541 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8542 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 8543 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 8544 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8545 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8546 | { |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 8547 | int new_range, i; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8548 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8549 | if (dst_reg->off < 0 || |
| 8550 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8551 | /* This doesn't give us any range */ |
| 8552 | return; |
| 8553 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8554 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 8555 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8556 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 8557 | * than pkt_end, but that's because it's also less than pkt. |
| 8558 | */ |
| 8559 | return; |
| 8560 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8561 | new_range = dst_reg->off; |
| 8562 | if (range_right_open) |
Maxim Mikityanskiy | 2fa7d94 | 2021-11-30 20:16:07 +0200 | [diff] [blame] | 8563 | new_range++; |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8564 | |
| 8565 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8566 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8567 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8568 | * |
| 8569 | * r2 = r3; |
| 8570 | * r2 += 8; |
| 8571 | * if (r2 > pkt_end) goto <handle exception> |
| 8572 | * <access okay> |
| 8573 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 8574 | * r2 = r3; |
| 8575 | * r2 += 8; |
| 8576 | * if (r2 < pkt_end) goto <access okay> |
| 8577 | * <handle exception> |
| 8578 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8579 | * Where: |
| 8580 | * r2 == dst_reg, pkt_end == src_reg |
| 8581 | * r2=pkt(id=n,off=8,r=0) |
| 8582 | * r3=pkt(id=n,off=0,r=0) |
| 8583 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8584 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8585 | * |
| 8586 | * r2 = r3; |
| 8587 | * r2 += 8; |
| 8588 | * if (pkt_end >= r2) goto <access okay> |
| 8589 | * <handle exception> |
| 8590 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 8591 | * r2 = r3; |
| 8592 | * r2 += 8; |
| 8593 | * if (pkt_end <= r2) goto <handle exception> |
| 8594 | * <access okay> |
| 8595 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8596 | * Where: |
| 8597 | * pkt_end == dst_reg, r2 == src_reg |
| 8598 | * r2=pkt(id=n,off=8,r=0) |
| 8599 | * r3=pkt(id=n,off=0,r=0) |
| 8600 | * |
| 8601 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8602 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 8603 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 8604 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8605 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8606 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8607 | /* If our ids match, then we must have the same max_value. And we |
| 8608 | * don't care about the other reg's fixed offset, since if it's too big |
| 8609 | * the range won't allow anything. |
| 8610 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 8611 | */ |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 8612 | for (i = 0; i <= vstate->curframe; i++) |
| 8613 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, |
| 8614 | new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8615 | } |
| 8616 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8617 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8618 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8619 | struct tnum subreg = tnum_subreg(reg->var_off); |
| 8620 | s32 sval = (s32)val; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8621 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8622 | switch (opcode) { |
| 8623 | case BPF_JEQ: |
| 8624 | if (tnum_is_const(subreg)) |
| 8625 | return !!tnum_equals_const(subreg, val); |
| 8626 | break; |
| 8627 | case BPF_JNE: |
| 8628 | if (tnum_is_const(subreg)) |
| 8629 | return !tnum_equals_const(subreg, val); |
| 8630 | break; |
| 8631 | case BPF_JSET: |
| 8632 | if ((~subreg.mask & subreg.value) & val) |
| 8633 | return 1; |
| 8634 | if (!((subreg.mask | subreg.value) & val)) |
| 8635 | return 0; |
| 8636 | break; |
| 8637 | case BPF_JGT: |
| 8638 | if (reg->u32_min_value > val) |
| 8639 | return 1; |
| 8640 | else if (reg->u32_max_value <= val) |
| 8641 | return 0; |
| 8642 | break; |
| 8643 | case BPF_JSGT: |
| 8644 | if (reg->s32_min_value > sval) |
| 8645 | return 1; |
Daniel Borkmann | ee114dd | 2021-02-05 17:20:14 +0100 | [diff] [blame] | 8646 | else if (reg->s32_max_value <= sval) |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8647 | return 0; |
| 8648 | break; |
| 8649 | case BPF_JLT: |
| 8650 | if (reg->u32_max_value < val) |
| 8651 | return 1; |
| 8652 | else if (reg->u32_min_value >= val) |
| 8653 | return 0; |
| 8654 | break; |
| 8655 | case BPF_JSLT: |
| 8656 | if (reg->s32_max_value < sval) |
| 8657 | return 1; |
| 8658 | else if (reg->s32_min_value >= sval) |
| 8659 | return 0; |
| 8660 | break; |
| 8661 | case BPF_JGE: |
| 8662 | if (reg->u32_min_value >= val) |
| 8663 | return 1; |
| 8664 | else if (reg->u32_max_value < val) |
| 8665 | return 0; |
| 8666 | break; |
| 8667 | case BPF_JSGE: |
| 8668 | if (reg->s32_min_value >= sval) |
| 8669 | return 1; |
| 8670 | else if (reg->s32_max_value < sval) |
| 8671 | return 0; |
| 8672 | break; |
| 8673 | case BPF_JLE: |
| 8674 | if (reg->u32_max_value <= val) |
| 8675 | return 1; |
| 8676 | else if (reg->u32_min_value > val) |
| 8677 | return 0; |
| 8678 | break; |
| 8679 | case BPF_JSLE: |
| 8680 | if (reg->s32_max_value <= sval) |
| 8681 | return 1; |
| 8682 | else if (reg->s32_min_value > sval) |
| 8683 | return 0; |
| 8684 | break; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 8685 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8686 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8687 | return -1; |
| 8688 | } |
| 8689 | |
| 8690 | |
| 8691 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) |
| 8692 | { |
| 8693 | s64 sval = (s64)val; |
| 8694 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8695 | switch (opcode) { |
| 8696 | case BPF_JEQ: |
| 8697 | if (tnum_is_const(reg->var_off)) |
| 8698 | return !!tnum_equals_const(reg->var_off, val); |
| 8699 | break; |
| 8700 | case BPF_JNE: |
| 8701 | if (tnum_is_const(reg->var_off)) |
| 8702 | return !tnum_equals_const(reg->var_off, val); |
| 8703 | break; |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 8704 | case BPF_JSET: |
| 8705 | if ((~reg->var_off.mask & reg->var_off.value) & val) |
| 8706 | return 1; |
| 8707 | if (!((reg->var_off.mask | reg->var_off.value) & val)) |
| 8708 | return 0; |
| 8709 | break; |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8710 | case BPF_JGT: |
| 8711 | if (reg->umin_value > val) |
| 8712 | return 1; |
| 8713 | else if (reg->umax_value <= val) |
| 8714 | return 0; |
| 8715 | break; |
| 8716 | case BPF_JSGT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8717 | if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8718 | return 1; |
Daniel Borkmann | ee114dd | 2021-02-05 17:20:14 +0100 | [diff] [blame] | 8719 | else if (reg->smax_value <= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8720 | return 0; |
| 8721 | break; |
| 8722 | case BPF_JLT: |
| 8723 | if (reg->umax_value < val) |
| 8724 | return 1; |
| 8725 | else if (reg->umin_value >= val) |
| 8726 | return 0; |
| 8727 | break; |
| 8728 | case BPF_JSLT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8729 | if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8730 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8731 | else if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8732 | return 0; |
| 8733 | break; |
| 8734 | case BPF_JGE: |
| 8735 | if (reg->umin_value >= val) |
| 8736 | return 1; |
| 8737 | else if (reg->umax_value < val) |
| 8738 | return 0; |
| 8739 | break; |
| 8740 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8741 | if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8742 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8743 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8744 | return 0; |
| 8745 | break; |
| 8746 | case BPF_JLE: |
| 8747 | if (reg->umax_value <= val) |
| 8748 | return 1; |
| 8749 | else if (reg->umin_value > val) |
| 8750 | return 0; |
| 8751 | break; |
| 8752 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8753 | if (reg->smax_value <= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8754 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8755 | else if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8756 | return 0; |
| 8757 | break; |
| 8758 | } |
| 8759 | |
| 8760 | return -1; |
| 8761 | } |
| 8762 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8763 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
| 8764 | * and return: |
| 8765 | * 1 - branch will be taken and "goto target" will be executed |
| 8766 | * 0 - branch will not be taken and fall-through to next insn |
| 8767 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value |
| 8768 | * range [0,10] |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 8769 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8770 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
| 8771 | bool is_jmp32) |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 8772 | { |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 8773 | if (__is_pointer_value(false, reg)) { |
| 8774 | if (!reg_type_not_null(reg->type)) |
| 8775 | return -1; |
| 8776 | |
| 8777 | /* If pointer is valid tests against zero will fail so we can |
| 8778 | * use this to direct branch taken. |
| 8779 | */ |
| 8780 | if (val != 0) |
| 8781 | return -1; |
| 8782 | |
| 8783 | switch (opcode) { |
| 8784 | case BPF_JEQ: |
| 8785 | return 0; |
| 8786 | case BPF_JNE: |
| 8787 | return 1; |
| 8788 | default: |
| 8789 | return -1; |
| 8790 | } |
| 8791 | } |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 8792 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8793 | if (is_jmp32) |
| 8794 | return is_branch32_taken(reg, val, opcode); |
| 8795 | return is_branch64_taken(reg, val, opcode); |
Jann Horn | 604dca5 | 2020-03-30 18:03:23 +0200 | [diff] [blame] | 8796 | } |
| 8797 | |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 8798 | static int flip_opcode(u32 opcode) |
| 8799 | { |
| 8800 | /* How can we transform "a <op> b" into "b <op> a"? */ |
| 8801 | static const u8 opcode_flip[16] = { |
| 8802 | /* these stay the same */ |
| 8803 | [BPF_JEQ >> 4] = BPF_JEQ, |
| 8804 | [BPF_JNE >> 4] = BPF_JNE, |
| 8805 | [BPF_JSET >> 4] = BPF_JSET, |
| 8806 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ |
| 8807 | [BPF_JGE >> 4] = BPF_JLE, |
| 8808 | [BPF_JGT >> 4] = BPF_JLT, |
| 8809 | [BPF_JLE >> 4] = BPF_JGE, |
| 8810 | [BPF_JLT >> 4] = BPF_JGT, |
| 8811 | [BPF_JSGE >> 4] = BPF_JSLE, |
| 8812 | [BPF_JSGT >> 4] = BPF_JSLT, |
| 8813 | [BPF_JSLE >> 4] = BPF_JSGE, |
| 8814 | [BPF_JSLT >> 4] = BPF_JSGT |
| 8815 | }; |
| 8816 | return opcode_flip[opcode >> 4]; |
| 8817 | } |
| 8818 | |
| 8819 | static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg, |
| 8820 | struct bpf_reg_state *src_reg, |
| 8821 | u8 opcode) |
| 8822 | { |
| 8823 | struct bpf_reg_state *pkt; |
| 8824 | |
| 8825 | if (src_reg->type == PTR_TO_PACKET_END) { |
| 8826 | pkt = dst_reg; |
| 8827 | } else if (dst_reg->type == PTR_TO_PACKET_END) { |
| 8828 | pkt = src_reg; |
| 8829 | opcode = flip_opcode(opcode); |
| 8830 | } else { |
| 8831 | return -1; |
| 8832 | } |
| 8833 | |
| 8834 | if (pkt->range >= 0) |
| 8835 | return -1; |
| 8836 | |
| 8837 | switch (opcode) { |
| 8838 | case BPF_JLE: |
| 8839 | /* pkt <= pkt_end */ |
| 8840 | fallthrough; |
| 8841 | case BPF_JGT: |
| 8842 | /* pkt > pkt_end */ |
| 8843 | if (pkt->range == BEYOND_PKT_END) |
| 8844 | /* pkt has at last one extra byte beyond pkt_end */ |
| 8845 | return opcode == BPF_JGT; |
| 8846 | break; |
| 8847 | case BPF_JLT: |
| 8848 | /* pkt < pkt_end */ |
| 8849 | fallthrough; |
| 8850 | case BPF_JGE: |
| 8851 | /* pkt >= pkt_end */ |
| 8852 | if (pkt->range == BEYOND_PKT_END || pkt->range == AT_PKT_END) |
| 8853 | return opcode == BPF_JGE; |
| 8854 | break; |
| 8855 | } |
| 8856 | return -1; |
| 8857 | } |
| 8858 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8859 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 8860 | * variable register that we are working on, and src_reg is a constant or we're |
| 8861 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8862 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8863 | */ |
| 8864 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8865 | struct bpf_reg_state *false_reg, |
| 8866 | u64 val, u32 val32, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 8867 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8868 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8869 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
| 8870 | struct tnum false_64off = false_reg->var_off; |
| 8871 | struct tnum true_32off = tnum_subreg(true_reg->var_off); |
| 8872 | struct tnum true_64off = true_reg->var_off; |
| 8873 | s64 sval = (s64)val; |
| 8874 | s32 sval32 = (s32)val32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8875 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8876 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 8877 | * variable offset from the compare (unless src_reg were a pointer into |
| 8878 | * the same object, but we don't bother with that. |
| 8879 | * Since false_reg and true_reg have the same type by construction, we |
| 8880 | * only need to check one of them for pointerness. |
| 8881 | */ |
| 8882 | if (__is_pointer_value(false, false_reg)) |
| 8883 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 8884 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8885 | switch (opcode) { |
| 8886 | case BPF_JEQ: |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8887 | case BPF_JNE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8888 | { |
| 8889 | struct bpf_reg_state *reg = |
| 8890 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 8891 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 8892 | /* JEQ/JNE comparison doesn't change the register equivalence. |
| 8893 | * r1 = r2; |
| 8894 | * if (r1 == 42) goto label; |
| 8895 | * ... |
| 8896 | * label: // here both r1 and r2 are known to be 42. |
| 8897 | * |
| 8898 | * Hence when marking register as known preserve it's ID. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8899 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8900 | if (is_jmp32) |
| 8901 | __mark_reg32_known(reg, val32); |
| 8902 | else |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 8903 | ___mark_reg_known(reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8904 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8905 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 8906 | case BPF_JSET: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8907 | if (is_jmp32) { |
| 8908 | false_32off = tnum_and(false_32off, tnum_const(~val32)); |
| 8909 | if (is_power_of_2(val32)) |
| 8910 | true_32off = tnum_or(true_32off, |
| 8911 | tnum_const(val32)); |
| 8912 | } else { |
| 8913 | false_64off = tnum_and(false_64off, tnum_const(~val)); |
| 8914 | if (is_power_of_2(val)) |
| 8915 | true_64off = tnum_or(true_64off, |
| 8916 | tnum_const(val)); |
| 8917 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 8918 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8919 | case BPF_JGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8920 | case BPF_JGT: |
| 8921 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8922 | if (is_jmp32) { |
| 8923 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; |
| 8924 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; |
| 8925 | |
| 8926 | false_reg->u32_max_value = min(false_reg->u32_max_value, |
| 8927 | false_umax); |
| 8928 | true_reg->u32_min_value = max(true_reg->u32_min_value, |
| 8929 | true_umin); |
| 8930 | } else { |
| 8931 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; |
| 8932 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; |
| 8933 | |
| 8934 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 8935 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
| 8936 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8937 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8938 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8939 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8940 | case BPF_JSGT: |
| 8941 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8942 | if (is_jmp32) { |
| 8943 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; |
| 8944 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8945 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8946 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
| 8947 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); |
| 8948 | } else { |
| 8949 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; |
| 8950 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; |
| 8951 | |
| 8952 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 8953 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
| 8954 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8955 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8956 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 8957 | case BPF_JLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8958 | case BPF_JLT: |
| 8959 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8960 | if (is_jmp32) { |
| 8961 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; |
| 8962 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; |
| 8963 | |
| 8964 | false_reg->u32_min_value = max(false_reg->u32_min_value, |
| 8965 | false_umin); |
| 8966 | true_reg->u32_max_value = min(true_reg->u32_max_value, |
| 8967 | true_umax); |
| 8968 | } else { |
| 8969 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; |
| 8970 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; |
| 8971 | |
| 8972 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 8973 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
| 8974 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 8975 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8976 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 8977 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8978 | case BPF_JSLT: |
| 8979 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8980 | if (is_jmp32) { |
| 8981 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; |
| 8982 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8983 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8984 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
| 8985 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); |
| 8986 | } else { |
| 8987 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; |
| 8988 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; |
| 8989 | |
| 8990 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 8991 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
| 8992 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 8993 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8994 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8995 | default: |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 8996 | return; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8997 | } |
| 8998 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8999 | if (is_jmp32) { |
| 9000 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), |
| 9001 | tnum_subreg(false_32off)); |
| 9002 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), |
| 9003 | tnum_subreg(true_32off)); |
| 9004 | __reg_combine_32_into_64(false_reg); |
| 9005 | __reg_combine_32_into_64(true_reg); |
| 9006 | } else { |
| 9007 | false_reg->var_off = false_64off; |
| 9008 | true_reg->var_off = true_64off; |
| 9009 | __reg_combine_64_into_32(false_reg); |
| 9010 | __reg_combine_64_into_32(true_reg); |
| 9011 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 9012 | } |
| 9013 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9014 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 9015 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 9016 | */ |
| 9017 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9018 | struct bpf_reg_state *false_reg, |
| 9019 | u64 val, u32 val32, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9020 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 9021 | { |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9022 | opcode = flip_opcode(opcode); |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 9023 | /* This uses zero as "not present in table"; luckily the zero opcode, |
| 9024 | * BPF_JA, can't get here. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 9025 | */ |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 9026 | if (opcode) |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9027 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9028 | } |
| 9029 | |
| 9030 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 9031 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 9032 | struct bpf_reg_state *dst_reg) |
| 9033 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 9034 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 9035 | dst_reg->umin_value); |
| 9036 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 9037 | dst_reg->umax_value); |
| 9038 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 9039 | dst_reg->smin_value); |
| 9040 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 9041 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9042 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 9043 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 9044 | /* We might have learned new bounds from the var_off. */ |
| 9045 | __update_reg_bounds(src_reg); |
| 9046 | __update_reg_bounds(dst_reg); |
| 9047 | /* We might have learned something about the sign bit. */ |
| 9048 | __reg_deduce_bounds(src_reg); |
| 9049 | __reg_deduce_bounds(dst_reg); |
| 9050 | /* We might have learned some bits from the bounds. */ |
| 9051 | __reg_bound_offset(src_reg); |
| 9052 | __reg_bound_offset(dst_reg); |
| 9053 | /* Intersecting with the old var_off might have improved our bounds |
| 9054 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 9055 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 9056 | */ |
| 9057 | __update_reg_bounds(src_reg); |
| 9058 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9059 | } |
| 9060 | |
| 9061 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 9062 | struct bpf_reg_state *true_dst, |
| 9063 | struct bpf_reg_state *false_src, |
| 9064 | struct bpf_reg_state *false_dst, |
| 9065 | u8 opcode) |
| 9066 | { |
| 9067 | switch (opcode) { |
| 9068 | case BPF_JEQ: |
| 9069 | __reg_combine_min_max(true_src, true_dst); |
| 9070 | break; |
| 9071 | case BPF_JNE: |
| 9072 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 9073 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 9074 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 9075 | } |
| 9076 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9077 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 9078 | struct bpf_reg_state *reg, u32 id, |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 9079 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9080 | { |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 9081 | if (type_may_be_null(reg->type) && reg->id == id && |
Martin KaFai Lau | 93c230e | 2020-10-19 12:42:12 -0700 | [diff] [blame] | 9082 | !WARN_ON_ONCE(!reg->id)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 9083 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 9084 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9085 | reg->off)) { |
Daniel Borkmann | e60b0d1 | 2022-01-05 11:35:13 -0800 | [diff] [blame] | 9086 | /* Old offset (both fixed and variable parts) should |
| 9087 | * have been known-zero, because we don't allow pointer |
| 9088 | * arithmetic on pointers that might be NULL. If we |
| 9089 | * see this happening, don't convert the register. |
| 9090 | */ |
| 9091 | return; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9092 | } |
| 9093 | if (is_null) { |
| 9094 | reg->type = SCALAR_VALUE; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 9095 | /* We don't need id and ref_obj_id from this point |
| 9096 | * onwards anymore, thus we should better reset it, |
| 9097 | * so that state pruning has chances to take effect. |
| 9098 | */ |
| 9099 | reg->id = 0; |
| 9100 | reg->ref_obj_id = 0; |
Dmitrii Banshchikov | 4ddb741 | 2021-02-13 00:56:40 +0400 | [diff] [blame] | 9101 | |
| 9102 | return; |
| 9103 | } |
| 9104 | |
| 9105 | mark_ptr_not_null_reg(reg); |
| 9106 | |
| 9107 | if (!reg_may_point_to_spin_lock(reg)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 9108 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
| 9109 | * in release_reg_references(). |
| 9110 | * |
| 9111 | * reg->id is still used by spin_lock ptr. Other |
| 9112 | * than spin_lock ptr type, reg->id can be reset. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9113 | */ |
| 9114 | reg->id = 0; |
| 9115 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9116 | } |
| 9117 | } |
| 9118 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 9119 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
| 9120 | bool is_null) |
| 9121 | { |
| 9122 | struct bpf_reg_state *reg; |
| 9123 | int i; |
| 9124 | |
| 9125 | for (i = 0; i < MAX_BPF_REG; i++) |
| 9126 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); |
| 9127 | |
| 9128 | bpf_for_each_spilled_reg(i, state, reg) { |
| 9129 | if (!reg) |
| 9130 | continue; |
| 9131 | mark_ptr_or_null_reg(state, reg, id, is_null); |
| 9132 | } |
| 9133 | } |
| 9134 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9135 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 9136 | * be folded together at some point. |
| 9137 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 9138 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 9139 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9140 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9141 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 9142 | struct bpf_reg_state *regs = state->regs; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 9143 | u32 ref_obj_id = regs[regno].ref_obj_id; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 9144 | u32 id = regs[regno].id; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 9145 | int i; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9146 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 9147 | if (ref_obj_id && ref_obj_id == id && is_null) |
| 9148 | /* regs[regno] is in the " == NULL" branch. |
| 9149 | * No one could have freed the reference state before |
| 9150 | * doing the NULL check. |
| 9151 | */ |
| 9152 | WARN_ON_ONCE(release_reference_state(state, id)); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9153 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 9154 | for (i = 0; i <= vstate->curframe; i++) |
| 9155 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9156 | } |
| 9157 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9158 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 9159 | struct bpf_reg_state *dst_reg, |
| 9160 | struct bpf_reg_state *src_reg, |
| 9161 | struct bpf_verifier_state *this_branch, |
| 9162 | struct bpf_verifier_state *other_branch) |
| 9163 | { |
| 9164 | if (BPF_SRC(insn->code) != BPF_X) |
| 9165 | return false; |
| 9166 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9167 | /* Pointers are always 64-bit. */ |
| 9168 | if (BPF_CLASS(insn->code) == BPF_JMP32) |
| 9169 | return false; |
| 9170 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9171 | switch (BPF_OP(insn->code)) { |
| 9172 | case BPF_JGT: |
| 9173 | if ((dst_reg->type == PTR_TO_PACKET && |
| 9174 | src_reg->type == PTR_TO_PACKET_END) || |
| 9175 | (dst_reg->type == PTR_TO_PACKET_META && |
| 9176 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 9177 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 9178 | find_good_pkt_pointers(this_branch, dst_reg, |
| 9179 | dst_reg->type, false); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9180 | mark_pkt_end(other_branch, insn->dst_reg, true); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9181 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 9182 | src_reg->type == PTR_TO_PACKET) || |
| 9183 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 9184 | src_reg->type == PTR_TO_PACKET_META)) { |
| 9185 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 9186 | find_good_pkt_pointers(other_branch, src_reg, |
| 9187 | src_reg->type, true); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9188 | mark_pkt_end(this_branch, insn->src_reg, false); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9189 | } else { |
| 9190 | return false; |
| 9191 | } |
| 9192 | break; |
| 9193 | case BPF_JLT: |
| 9194 | if ((dst_reg->type == PTR_TO_PACKET && |
| 9195 | src_reg->type == PTR_TO_PACKET_END) || |
| 9196 | (dst_reg->type == PTR_TO_PACKET_META && |
| 9197 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 9198 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 9199 | find_good_pkt_pointers(other_branch, dst_reg, |
| 9200 | dst_reg->type, true); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9201 | mark_pkt_end(this_branch, insn->dst_reg, false); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9202 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 9203 | src_reg->type == PTR_TO_PACKET) || |
| 9204 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 9205 | src_reg->type == PTR_TO_PACKET_META)) { |
| 9206 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 9207 | find_good_pkt_pointers(this_branch, src_reg, |
| 9208 | src_reg->type, false); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9209 | mark_pkt_end(other_branch, insn->src_reg, true); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9210 | } else { |
| 9211 | return false; |
| 9212 | } |
| 9213 | break; |
| 9214 | case BPF_JGE: |
| 9215 | if ((dst_reg->type == PTR_TO_PACKET && |
| 9216 | src_reg->type == PTR_TO_PACKET_END) || |
| 9217 | (dst_reg->type == PTR_TO_PACKET_META && |
| 9218 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 9219 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 9220 | find_good_pkt_pointers(this_branch, dst_reg, |
| 9221 | dst_reg->type, true); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9222 | mark_pkt_end(other_branch, insn->dst_reg, false); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9223 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 9224 | src_reg->type == PTR_TO_PACKET) || |
| 9225 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 9226 | src_reg->type == PTR_TO_PACKET_META)) { |
| 9227 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 9228 | find_good_pkt_pointers(other_branch, src_reg, |
| 9229 | src_reg->type, false); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9230 | mark_pkt_end(this_branch, insn->src_reg, true); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9231 | } else { |
| 9232 | return false; |
| 9233 | } |
| 9234 | break; |
| 9235 | case BPF_JLE: |
| 9236 | if ((dst_reg->type == PTR_TO_PACKET && |
| 9237 | src_reg->type == PTR_TO_PACKET_END) || |
| 9238 | (dst_reg->type == PTR_TO_PACKET_META && |
| 9239 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 9240 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 9241 | find_good_pkt_pointers(other_branch, dst_reg, |
| 9242 | dst_reg->type, false); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9243 | mark_pkt_end(this_branch, insn->dst_reg, true); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9244 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 9245 | src_reg->type == PTR_TO_PACKET) || |
| 9246 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 9247 | src_reg->type == PTR_TO_PACKET_META)) { |
| 9248 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 9249 | find_good_pkt_pointers(this_branch, src_reg, |
| 9250 | src_reg->type, true); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9251 | mark_pkt_end(other_branch, insn->src_reg, false); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9252 | } else { |
| 9253 | return false; |
| 9254 | } |
| 9255 | break; |
| 9256 | default: |
| 9257 | return false; |
| 9258 | } |
| 9259 | |
| 9260 | return true; |
| 9261 | } |
| 9262 | |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 9263 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
| 9264 | struct bpf_reg_state *known_reg) |
| 9265 | { |
| 9266 | struct bpf_func_state *state; |
| 9267 | struct bpf_reg_state *reg; |
| 9268 | int i, j; |
| 9269 | |
| 9270 | for (i = 0; i <= vstate->curframe; i++) { |
| 9271 | state = vstate->frame[i]; |
| 9272 | for (j = 0; j < MAX_BPF_REG; j++) { |
| 9273 | reg = &state->regs[j]; |
| 9274 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) |
| 9275 | *reg = *known_reg; |
| 9276 | } |
| 9277 | |
| 9278 | bpf_for_each_spilled_reg(j, state, reg) { |
| 9279 | if (!reg) |
| 9280 | continue; |
| 9281 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) |
| 9282 | *reg = *known_reg; |
| 9283 | } |
| 9284 | } |
| 9285 | } |
| 9286 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9287 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9288 | struct bpf_insn *insn, int *insn_idx) |
| 9289 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9290 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 9291 | struct bpf_verifier_state *other_branch; |
| 9292 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9293 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9294 | u8 opcode = BPF_OP(insn->code); |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9295 | bool is_jmp32; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9296 | int pred = -1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9297 | int err; |
| 9298 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9299 | /* Only conditional jumps are expected to reach here. */ |
| 9300 | if (opcode == BPF_JA || opcode > BPF_JSLE) { |
| 9301 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9302 | return -EINVAL; |
| 9303 | } |
| 9304 | |
| 9305 | if (BPF_SRC(insn->code) == BPF_X) { |
| 9306 | if (insn->imm != 0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9307 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9308 | return -EINVAL; |
| 9309 | } |
| 9310 | |
| 9311 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9312 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9313 | if (err) |
| 9314 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 9315 | |
| 9316 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9317 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 9318 | insn->src_reg); |
| 9319 | return -EACCES; |
| 9320 | } |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9321 | src_reg = ®s[insn->src_reg]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9322 | } else { |
| 9323 | if (insn->src_reg != BPF_REG_0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9324 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9325 | return -EINVAL; |
| 9326 | } |
| 9327 | } |
| 9328 | |
| 9329 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9330 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9331 | if (err) |
| 9332 | return err; |
| 9333 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 9334 | dst_reg = ®s[insn->dst_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9335 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 9336 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9337 | if (BPF_SRC(insn->code) == BPF_K) { |
| 9338 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); |
| 9339 | } else if (src_reg->type == SCALAR_VALUE && |
| 9340 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { |
| 9341 | pred = is_branch_taken(dst_reg, |
| 9342 | tnum_subreg(src_reg->var_off).value, |
| 9343 | opcode, |
| 9344 | is_jmp32); |
| 9345 | } else if (src_reg->type == SCALAR_VALUE && |
| 9346 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { |
| 9347 | pred = is_branch_taken(dst_reg, |
| 9348 | src_reg->var_off.value, |
| 9349 | opcode, |
| 9350 | is_jmp32); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9351 | } else if (reg_is_pkt_pointer_any(dst_reg) && |
| 9352 | reg_is_pkt_pointer_any(src_reg) && |
| 9353 | !is_jmp32) { |
| 9354 | pred = is_pkt_ptr_branch_taken(dst_reg, src_reg, opcode); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9355 | } |
| 9356 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9357 | if (pred >= 0) { |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 9358 | /* If we get here with a dst_reg pointer type it is because |
| 9359 | * above is_branch_taken() special cased the 0 comparison. |
| 9360 | */ |
| 9361 | if (!__is_pointer_value(false, dst_reg)) |
| 9362 | err = mark_chain_precision(env, insn->dst_reg); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9363 | if (BPF_SRC(insn->code) == BPF_X && !err && |
| 9364 | !__is_pointer_value(false, src_reg)) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9365 | err = mark_chain_precision(env, insn->src_reg); |
| 9366 | if (err) |
| 9367 | return err; |
| 9368 | } |
Daniel Borkmann | 9183671a | 2021-05-28 15:47:32 +0000 | [diff] [blame] | 9369 | |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9370 | if (pred == 1) { |
Daniel Borkmann | 9183671a | 2021-05-28 15:47:32 +0000 | [diff] [blame] | 9371 | /* Only follow the goto, ignore fall-through. If needed, push |
| 9372 | * the fall-through branch for simulation under speculative |
| 9373 | * execution. |
| 9374 | */ |
| 9375 | if (!env->bypass_spec_v1 && |
| 9376 | !sanitize_speculative_path(env, insn, *insn_idx + 1, |
| 9377 | *insn_idx)) |
| 9378 | return -EFAULT; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9379 | *insn_idx += insn->off; |
| 9380 | return 0; |
| 9381 | } else if (pred == 0) { |
Daniel Borkmann | 9183671a | 2021-05-28 15:47:32 +0000 | [diff] [blame] | 9382 | /* Only follow the fall-through branch, since that's where the |
| 9383 | * program will go. If needed, push the goto branch for |
| 9384 | * simulation under speculative execution. |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9385 | */ |
Daniel Borkmann | 9183671a | 2021-05-28 15:47:32 +0000 | [diff] [blame] | 9386 | if (!env->bypass_spec_v1 && |
| 9387 | !sanitize_speculative_path(env, insn, |
| 9388 | *insn_idx + insn->off + 1, |
| 9389 | *insn_idx)) |
| 9390 | return -EFAULT; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9391 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9392 | } |
| 9393 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 9394 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
| 9395 | false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9396 | if (!other_branch) |
| 9397 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9398 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9399 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 9400 | /* detect if we are comparing against a constant value so we can adjust |
| 9401 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9402 | * this is only legit if both are scalars (or pointers to the same |
| 9403 | * object, I suppose, but we don't support that right now), because |
| 9404 | * otherwise the different base pointers mean the offsets aren't |
| 9405 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 9406 | */ |
| 9407 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9408 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9409 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9410 | if (dst_reg->type == SCALAR_VALUE && |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9411 | src_reg->type == SCALAR_VALUE) { |
| 9412 | if (tnum_is_const(src_reg->var_off) || |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9413 | (is_jmp32 && |
| 9414 | tnum_is_const(tnum_subreg(src_reg->var_off)))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9415 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9416 | dst_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9417 | src_reg->var_off.value, |
| 9418 | tnum_subreg(src_reg->var_off).value, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9419 | opcode, is_jmp32); |
| 9420 | else if (tnum_is_const(dst_reg->var_off) || |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9421 | (is_jmp32 && |
| 9422 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9423 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9424 | src_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9425 | dst_reg->var_off.value, |
| 9426 | tnum_subreg(dst_reg->var_off).value, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9427 | opcode, is_jmp32); |
| 9428 | else if (!is_jmp32 && |
| 9429 | (opcode == BPF_JEQ || opcode == BPF_JNE)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9430 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9431 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 9432 | &other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9433 | src_reg, dst_reg, opcode); |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 9434 | if (src_reg->id && |
| 9435 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 9436 | find_equal_scalars(this_branch, src_reg); |
| 9437 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); |
| 9438 | } |
| 9439 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9440 | } |
| 9441 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9442 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9443 | dst_reg, insn->imm, (u32)insn->imm, |
| 9444 | opcode, is_jmp32); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 9445 | } |
| 9446 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 9447 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
| 9448 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 9449 | find_equal_scalars(this_branch, dst_reg); |
| 9450 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); |
| 9451 | } |
| 9452 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9453 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
| 9454 | * NOTE: these optimizations below are related with pointer comparison |
| 9455 | * which will never be JMP32. |
| 9456 | */ |
| 9457 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 9458 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 9459 | type_may_be_null(dst_reg->type)) { |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 9460 | /* Mark all identical registers in each branch as either |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9461 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 9462 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 9463 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 9464 | opcode == BPF_JNE); |
| 9465 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 9466 | opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9467 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 9468 | this_branch, other_branch) && |
| 9469 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9470 | verbose(env, "R%d pointer comparison prohibited\n", |
| 9471 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 9472 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9473 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9474 | if (env->log.level & BPF_LOG_LEVEL) |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame] | 9475 | print_insn_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9476 | return 0; |
| 9477 | } |
| 9478 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9479 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9480 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9481 | { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9482 | struct bpf_insn_aux_data *aux = cur_aux(env); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9483 | struct bpf_reg_state *regs = cur_regs(env); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9484 | struct bpf_reg_state *dst_reg; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9485 | struct bpf_map *map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9486 | int err; |
| 9487 | |
| 9488 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9489 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9490 | return -EINVAL; |
| 9491 | } |
| 9492 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9493 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9494 | return -EINVAL; |
| 9495 | } |
| 9496 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9497 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9498 | if (err) |
| 9499 | return err; |
| 9500 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9501 | dst_reg = ®s[insn->dst_reg]; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 9502 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 9503 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 9504 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9505 | dst_reg->type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 9506 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9507 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 9508 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9509 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9510 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
| 9511 | mark_reg_known_zero(env, regs, insn->dst_reg); |
| 9512 | |
| 9513 | dst_reg->type = aux->btf_var.reg_type; |
Hao Luo | 34d3a78 | 2021-12-16 16:31:50 -0800 | [diff] [blame] | 9514 | switch (base_type(dst_reg->type)) { |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9515 | case PTR_TO_MEM: |
| 9516 | dst_reg->mem_size = aux->btf_var.mem_size; |
| 9517 | break; |
| 9518 | case PTR_TO_BTF_ID: |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 9519 | case PTR_TO_PERCPU_BTF_ID: |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 9520 | dst_reg->btf = aux->btf_var.btf; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9521 | dst_reg->btf_id = aux->btf_var.btf_id; |
| 9522 | break; |
| 9523 | default: |
| 9524 | verbose(env, "bpf verifier is misconfigured\n"); |
| 9525 | return -EFAULT; |
| 9526 | } |
| 9527 | return 0; |
| 9528 | } |
| 9529 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 9530 | if (insn->src_reg == BPF_PSEUDO_FUNC) { |
| 9531 | struct bpf_prog_aux *aux = env->prog->aux; |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 9532 | u32 subprogno = find_subprog(env, |
| 9533 | env->insn_idx + insn->imm + 1); |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 9534 | |
| 9535 | if (!aux->func_info) { |
| 9536 | verbose(env, "missing btf func_info\n"); |
| 9537 | return -EINVAL; |
| 9538 | } |
| 9539 | if (aux->func_info_aux[subprogno].linkage != BTF_FUNC_STATIC) { |
| 9540 | verbose(env, "callback function not static\n"); |
| 9541 | return -EINVAL; |
| 9542 | } |
| 9543 | |
| 9544 | dst_reg->type = PTR_TO_FUNC; |
| 9545 | dst_reg->subprogno = subprogno; |
| 9546 | return 0; |
| 9547 | } |
| 9548 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9549 | map = env->used_maps[aux->map_index]; |
| 9550 | mark_reg_known_zero(env, regs, insn->dst_reg); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9551 | dst_reg->map_ptr = map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9552 | |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 9553 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE || |
| 9554 | insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE) { |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9555 | dst_reg->type = PTR_TO_MAP_VALUE; |
| 9556 | dst_reg->off = aux->map_off; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9557 | if (map_value_has_spin_lock(map)) |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9558 | dst_reg->id = ++env->id_gen; |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 9559 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD || |
| 9560 | insn->src_reg == BPF_PSEUDO_MAP_IDX) { |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9561 | dst_reg->type = CONST_PTR_TO_MAP; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9562 | } else { |
| 9563 | verbose(env, "bpf verifier is misconfigured\n"); |
| 9564 | return -EINVAL; |
| 9565 | } |
| 9566 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9567 | return 0; |
| 9568 | } |
| 9569 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 9570 | static bool may_access_skb(enum bpf_prog_type type) |
| 9571 | { |
| 9572 | switch (type) { |
| 9573 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 9574 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 9575 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 9576 | return true; |
| 9577 | default: |
| 9578 | return false; |
| 9579 | } |
| 9580 | } |
| 9581 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9582 | /* verify safety of LD_ABS|LD_IND instructions: |
| 9583 | * - they can only appear in the programs where ctx == skb |
| 9584 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 9585 | * preserve R6-R9, and store return value into R0 |
| 9586 | * |
| 9587 | * Implicit input: |
| 9588 | * ctx == skb == R6 == CTX |
| 9589 | * |
| 9590 | * Explicit input: |
| 9591 | * SRC == any register |
| 9592 | * IMM == 32-bit immediate |
| 9593 | * |
| 9594 | * Output: |
| 9595 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 9596 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9597 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9598 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9599 | struct bpf_reg_state *regs = cur_regs(env); |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 9600 | static const int ctx_reg = BPF_REG_6; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9601 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9602 | int i, err; |
| 9603 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9604 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9605 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9606 | return -EINVAL; |
| 9607 | } |
| 9608 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 9609 | if (!env->ops->gen_ld_abs) { |
| 9610 | verbose(env, "bpf verifier is misconfigured\n"); |
| 9611 | return -EINVAL; |
| 9612 | } |
| 9613 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9614 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 9615 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9616 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9617 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9618 | return -EINVAL; |
| 9619 | } |
| 9620 | |
| 9621 | /* check whether implicit source operand (register R6) is readable */ |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 9622 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9623 | if (err) |
| 9624 | return err; |
| 9625 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9626 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 9627 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 9628 | * reference leak. |
| 9629 | */ |
| 9630 | err = check_reference_leak(env); |
| 9631 | if (err) { |
| 9632 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 9633 | return err; |
| 9634 | } |
| 9635 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9636 | if (env->cur_state->active_spin_lock) { |
| 9637 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
| 9638 | return -EINVAL; |
| 9639 | } |
| 9640 | |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 9641 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9642 | verbose(env, |
| 9643 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9644 | return -EINVAL; |
| 9645 | } |
| 9646 | |
| 9647 | if (mode == BPF_IND) { |
| 9648 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9649 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9650 | if (err) |
| 9651 | return err; |
| 9652 | } |
| 9653 | |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 9654 | err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg); |
| 9655 | if (err < 0) |
| 9656 | return err; |
| 9657 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9658 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9659 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9660 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9661 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 9662 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9663 | |
| 9664 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9665 | * the value fetched from the packet. |
| 9666 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9667 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9668 | mark_reg_unknown(env, regs, BPF_REG_0); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 9669 | /* ld_abs load up to 32-bit skb data. */ |
| 9670 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9671 | return 0; |
| 9672 | } |
| 9673 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9674 | static int check_return_code(struct bpf_verifier_env *env) |
| 9675 | { |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 9676 | struct tnum enforce_attach_type_range = tnum_unknown; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 9677 | const struct bpf_prog *prog = env->prog; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9678 | struct bpf_reg_state *reg; |
| 9679 | struct tnum range = tnum_range(0, 1); |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9680 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 9681 | int err; |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 9682 | struct bpf_func_state *frame = env->cur_state->frame[0]; |
| 9683 | const bool is_subprog = frame->subprogno; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 9684 | |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 9685 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
Dmitrii Banshchikov | f782e2c | 2020-11-13 17:17:56 +0000 | [diff] [blame] | 9686 | if (!is_subprog && |
| 9687 | (prog_type == BPF_PROG_TYPE_STRUCT_OPS || |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9688 | prog_type == BPF_PROG_TYPE_LSM) && |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 9689 | !prog->aux->attach_func_proto->type) |
| 9690 | return 0; |
| 9691 | |
Zhen Lei | 8fb33b6 | 2021-05-25 10:56:59 +0800 | [diff] [blame] | 9692 | /* eBPF calling convention is such that R0 is used |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 9693 | * to return the value from eBPF program. |
| 9694 | * Make sure that it's readable at this time |
| 9695 | * of bpf_exit, which means that program wrote |
| 9696 | * something into it earlier |
| 9697 | */ |
| 9698 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
| 9699 | if (err) |
| 9700 | return err; |
| 9701 | |
| 9702 | if (is_pointer_value(env, BPF_REG_0)) { |
| 9703 | verbose(env, "R0 leaks addr as return value\n"); |
| 9704 | return -EACCES; |
| 9705 | } |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9706 | |
Dmitrii Banshchikov | f782e2c | 2020-11-13 17:17:56 +0000 | [diff] [blame] | 9707 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 9708 | |
| 9709 | if (frame->in_async_callback_fn) { |
| 9710 | /* enforce return zero from async callbacks like timer */ |
| 9711 | if (reg->type != SCALAR_VALUE) { |
| 9712 | verbose(env, "In async callback the register R0 is not a known value (%s)\n", |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 9713 | reg_type_str(env, reg->type)); |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 9714 | return -EINVAL; |
| 9715 | } |
| 9716 | |
| 9717 | if (!tnum_in(tnum_const(0), reg->var_off)) { |
| 9718 | verbose_invalid_scalar(env, reg, &range, "async callback", "R0"); |
| 9719 | return -EINVAL; |
| 9720 | } |
| 9721 | return 0; |
| 9722 | } |
| 9723 | |
Dmitrii Banshchikov | f782e2c | 2020-11-13 17:17:56 +0000 | [diff] [blame] | 9724 | if (is_subprog) { |
| 9725 | if (reg->type != SCALAR_VALUE) { |
| 9726 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 9727 | reg_type_str(env, reg->type)); |
Dmitrii Banshchikov | f782e2c | 2020-11-13 17:17:56 +0000 | [diff] [blame] | 9728 | return -EINVAL; |
| 9729 | } |
| 9730 | return 0; |
| 9731 | } |
| 9732 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9733 | switch (prog_type) { |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 9734 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 9735 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || |
Daniel Borkmann | 1b66d25 | 2020-05-19 00:45:45 +0200 | [diff] [blame] | 9736 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
| 9737 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || |
| 9738 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || |
| 9739 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || |
| 9740 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 9741 | range = tnum_range(1, 1); |
Stanislav Fomichev | 7724121 | 2021-01-27 11:31:39 -0800 | [diff] [blame] | 9742 | if (env->prog->expected_attach_type == BPF_CGROUP_INET4_BIND || |
| 9743 | env->prog->expected_attach_type == BPF_CGROUP_INET6_BIND) |
| 9744 | range = tnum_range(0, 3); |
Gustavo A. R. Silva | ed4ed40 | 2019-07-11 11:22:33 -0500 | [diff] [blame] | 9745 | break; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9746 | case BPF_PROG_TYPE_CGROUP_SKB: |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 9747 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
| 9748 | range = tnum_range(0, 3); |
| 9749 | enforce_attach_type_range = tnum_range(2, 3); |
| 9750 | } |
Gustavo A. R. Silva | ed4ed40 | 2019-07-11 11:22:33 -0500 | [diff] [blame] | 9751 | break; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9752 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 9753 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 9754 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 9755 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 9756 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9757 | break; |
Alexei Starovoitov | 15ab09b | 2019-10-28 20:24:26 -0700 | [diff] [blame] | 9758 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 9759 | if (!env->prog->aux->attach_btf_id) |
| 9760 | return 0; |
| 9761 | range = tnum_const(0); |
| 9762 | break; |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 9763 | case BPF_PROG_TYPE_TRACING: |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 9764 | switch (env->prog->expected_attach_type) { |
| 9765 | case BPF_TRACE_FENTRY: |
| 9766 | case BPF_TRACE_FEXIT: |
| 9767 | range = tnum_const(0); |
| 9768 | break; |
| 9769 | case BPF_TRACE_RAW_TP: |
| 9770 | case BPF_MODIFY_RETURN: |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 9771 | return 0; |
Daniel Borkmann | 2ec0616 | 2020-05-16 00:39:18 +0200 | [diff] [blame] | 9772 | case BPF_TRACE_ITER: |
| 9773 | break; |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 9774 | default: |
| 9775 | return -ENOTSUPP; |
| 9776 | } |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 9777 | break; |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 9778 | case BPF_PROG_TYPE_SK_LOOKUP: |
| 9779 | range = tnum_range(SK_DROP, SK_PASS); |
| 9780 | break; |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 9781 | case BPF_PROG_TYPE_EXT: |
| 9782 | /* freplace program can return anything as its return value |
| 9783 | * depends on the to-be-replaced kernel func or bpf program. |
| 9784 | */ |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9785 | default: |
| 9786 | return 0; |
| 9787 | } |
| 9788 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9789 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9790 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 9791 | reg_type_str(env, reg->type)); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9792 | return -EINVAL; |
| 9793 | } |
| 9794 | |
| 9795 | if (!tnum_in(range, reg->var_off)) { |
Yonghong Song | bc2591d | 2021-02-26 12:49:22 -0800 | [diff] [blame] | 9796 | verbose_invalid_scalar(env, reg, &range, "program exit", "R0"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9797 | return -EINVAL; |
| 9798 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 9799 | |
| 9800 | if (!tnum_is_unknown(enforce_attach_type_range) && |
| 9801 | tnum_in(enforce_attach_type_range, reg->var_off)) |
| 9802 | env->prog->enforce_expected_attach_type = 1; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9803 | return 0; |
| 9804 | } |
| 9805 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9806 | /* non-recursive DFS pseudo code |
| 9807 | * 1 procedure DFS-iterative(G,v): |
| 9808 | * 2 label v as discovered |
| 9809 | * 3 let S be a stack |
| 9810 | * 4 S.push(v) |
| 9811 | * 5 while S is not empty |
| 9812 | * 6 t <- S.pop() |
| 9813 | * 7 if t is what we're looking for: |
| 9814 | * 8 return t |
| 9815 | * 9 for all edges e in G.adjacentEdges(t) do |
| 9816 | * 10 if edge e is already labelled |
| 9817 | * 11 continue with the next edge |
| 9818 | * 12 w <- G.adjacentVertex(t,e) |
| 9819 | * 13 if vertex w is not discovered and not explored |
| 9820 | * 14 label e as tree-edge |
| 9821 | * 15 label w as discovered |
| 9822 | * 16 S.push(w) |
| 9823 | * 17 continue at 5 |
| 9824 | * 18 else if vertex w is discovered |
| 9825 | * 19 label e as back-edge |
| 9826 | * 20 else |
| 9827 | * 21 // vertex w is explored |
| 9828 | * 22 label e as forward- or cross-edge |
| 9829 | * 23 label t as explored |
| 9830 | * 24 S.pop() |
| 9831 | * |
| 9832 | * convention: |
| 9833 | * 0x10 - discovered |
| 9834 | * 0x11 - discovered and fall-through edge labelled |
| 9835 | * 0x12 - discovered and fall-through and branch edges labelled |
| 9836 | * 0x20 - explored |
| 9837 | */ |
| 9838 | |
| 9839 | enum { |
| 9840 | DISCOVERED = 0x10, |
| 9841 | EXPLORED = 0x20, |
| 9842 | FALLTHROUGH = 1, |
| 9843 | BRANCH = 2, |
| 9844 | }; |
| 9845 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 9846 | static u32 state_htab_size(struct bpf_verifier_env *env) |
| 9847 | { |
| 9848 | return env->prog->len; |
| 9849 | } |
| 9850 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 9851 | static struct bpf_verifier_state_list **explored_state( |
| 9852 | struct bpf_verifier_env *env, |
| 9853 | int idx) |
| 9854 | { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 9855 | struct bpf_verifier_state *cur = env->cur_state; |
| 9856 | struct bpf_func_state *state = cur->frame[cur->curframe]; |
| 9857 | |
| 9858 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 9859 | } |
| 9860 | |
| 9861 | static void init_explored_state(struct bpf_verifier_env *env, int idx) |
| 9862 | { |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 9863 | env->insn_aux_data[idx].prune_point = true; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 9864 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9865 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9866 | enum { |
| 9867 | DONE_EXPLORING = 0, |
| 9868 | KEEP_EXPLORING = 1, |
| 9869 | }; |
| 9870 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9871 | /* t, w, e - match pseudo-code above: |
| 9872 | * t - index of current instruction |
| 9873 | * w - next instruction |
| 9874 | * e - edge |
| 9875 | */ |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9876 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
| 9877 | bool loop_ok) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9878 | { |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 9879 | int *insn_stack = env->cfg.insn_stack; |
| 9880 | int *insn_state = env->cfg.insn_state; |
| 9881 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9882 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9883 | return DONE_EXPLORING; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9884 | |
| 9885 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9886 | return DONE_EXPLORING; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9887 | |
| 9888 | if (w < 0 || w >= env->prog->len) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 9889 | verbose_linfo(env, t, "%d: ", t); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9890 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9891 | return -EINVAL; |
| 9892 | } |
| 9893 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9894 | if (e == BRANCH) |
| 9895 | /* mark branch target for state pruning */ |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 9896 | init_explored_state(env, w); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9897 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9898 | if (insn_state[w] == 0) { |
| 9899 | /* tree-edge */ |
| 9900 | insn_state[t] = DISCOVERED | e; |
| 9901 | insn_state[w] = DISCOVERED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 9902 | if (env->cfg.cur_stack >= env->prog->len) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9903 | return -E2BIG; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 9904 | insn_stack[env->cfg.cur_stack++] = w; |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9905 | return KEEP_EXPLORING; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9906 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 9907 | if (loop_ok && env->bpf_capable) |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9908 | return DONE_EXPLORING; |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 9909 | verbose_linfo(env, t, "%d: ", t); |
| 9910 | verbose_linfo(env, w, "%d: ", w); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9911 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9912 | return -EINVAL; |
| 9913 | } else if (insn_state[w] == EXPLORED) { |
| 9914 | /* forward- or cross-edge */ |
| 9915 | insn_state[t] = DISCOVERED | e; |
| 9916 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9917 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9918 | return -EFAULT; |
| 9919 | } |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9920 | return DONE_EXPLORING; |
| 9921 | } |
| 9922 | |
Yonghong Song | efdb22d | 2021-02-26 12:49:20 -0800 | [diff] [blame] | 9923 | static int visit_func_call_insn(int t, int insn_cnt, |
| 9924 | struct bpf_insn *insns, |
| 9925 | struct bpf_verifier_env *env, |
| 9926 | bool visit_callee) |
| 9927 | { |
| 9928 | int ret; |
| 9929 | |
| 9930 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
| 9931 | if (ret) |
| 9932 | return ret; |
| 9933 | |
| 9934 | if (t + 1 < insn_cnt) |
| 9935 | init_explored_state(env, t + 1); |
| 9936 | if (visit_callee) { |
| 9937 | init_explored_state(env, t); |
Alexei Starovoitov | 86fc6ee | 2021-07-14 17:54:13 -0700 | [diff] [blame] | 9938 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env, |
| 9939 | /* It's ok to allow recursion from CFG point of |
| 9940 | * view. __check_func_call() will do the actual |
| 9941 | * check. |
| 9942 | */ |
| 9943 | bpf_pseudo_func(insns + t)); |
Yonghong Song | efdb22d | 2021-02-26 12:49:20 -0800 | [diff] [blame] | 9944 | } |
| 9945 | return ret; |
| 9946 | } |
| 9947 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9948 | /* Visits the instruction at index t and returns one of the following: |
| 9949 | * < 0 - an error occurred |
| 9950 | * DONE_EXPLORING - the instruction was fully explored |
| 9951 | * KEEP_EXPLORING - there is still work to be done before it is fully explored |
| 9952 | */ |
| 9953 | static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env) |
| 9954 | { |
| 9955 | struct bpf_insn *insns = env->prog->insnsi; |
| 9956 | int ret; |
| 9957 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 9958 | if (bpf_pseudo_func(insns + t)) |
| 9959 | return visit_func_call_insn(t, insn_cnt, insns, env, true); |
| 9960 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9961 | /* All non-branch instructions have a single fall-through edge. */ |
| 9962 | if (BPF_CLASS(insns[t].code) != BPF_JMP && |
| 9963 | BPF_CLASS(insns[t].code) != BPF_JMP32) |
| 9964 | return push_insn(t, t + 1, FALLTHROUGH, env, false); |
| 9965 | |
| 9966 | switch (BPF_OP(insns[t].code)) { |
| 9967 | case BPF_EXIT: |
| 9968 | return DONE_EXPLORING; |
| 9969 | |
| 9970 | case BPF_CALL: |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 9971 | if (insns[t].imm == BPF_FUNC_timer_set_callback) |
| 9972 | /* Mark this call insn to trigger is_state_visited() check |
| 9973 | * before call itself is processed by __check_func_call(). |
| 9974 | * Otherwise new async state will be pushed for further |
| 9975 | * exploration. |
| 9976 | */ |
| 9977 | init_explored_state(env, t); |
Yonghong Song | efdb22d | 2021-02-26 12:49:20 -0800 | [diff] [blame] | 9978 | return visit_func_call_insn(t, insn_cnt, insns, env, |
| 9979 | insns[t].src_reg == BPF_PSEUDO_CALL); |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9980 | |
| 9981 | case BPF_JA: |
| 9982 | if (BPF_SRC(insns[t].code) != BPF_K) |
| 9983 | return -EINVAL; |
| 9984 | |
| 9985 | /* unconditional jump with single edge */ |
| 9986 | ret = push_insn(t, t + insns[t].off + 1, FALLTHROUGH, env, |
| 9987 | true); |
| 9988 | if (ret) |
| 9989 | return ret; |
| 9990 | |
| 9991 | /* unconditional jmp is not a good pruning point, |
| 9992 | * but it's marked, since backtracking needs |
| 9993 | * to record jmp history in is_state_visited(). |
| 9994 | */ |
| 9995 | init_explored_state(env, t + insns[t].off + 1); |
| 9996 | /* tell verifier to check for equivalent states |
| 9997 | * after every call and jump |
| 9998 | */ |
| 9999 | if (t + 1 < insn_cnt) |
| 10000 | init_explored_state(env, t + 1); |
| 10001 | |
| 10002 | return ret; |
| 10003 | |
| 10004 | default: |
| 10005 | /* conditional jump with two edges */ |
| 10006 | init_explored_state(env, t); |
| 10007 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
| 10008 | if (ret) |
| 10009 | return ret; |
| 10010 | |
| 10011 | return push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
| 10012 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10013 | } |
| 10014 | |
| 10015 | /* non-recursive depth-first-search to detect loops in BPF program |
| 10016 | * loop == back-edge in directed graph |
| 10017 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 10018 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10019 | { |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10020 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 10021 | int *insn_stack, *insn_state; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10022 | int ret = 0; |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 10023 | int i; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10024 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 10025 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10026 | if (!insn_state) |
| 10027 | return -ENOMEM; |
| 10028 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 10029 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10030 | if (!insn_stack) { |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 10031 | kvfree(insn_state); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10032 | return -ENOMEM; |
| 10033 | } |
| 10034 | |
| 10035 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 10036 | insn_stack[0] = 0; /* 0 is the first instruction */ |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 10037 | env->cfg.cur_stack = 1; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10038 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 10039 | while (env->cfg.cur_stack > 0) { |
| 10040 | int t = insn_stack[env->cfg.cur_stack - 1]; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10041 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 10042 | ret = visit_insn(t, insn_cnt, env); |
| 10043 | switch (ret) { |
| 10044 | case DONE_EXPLORING: |
| 10045 | insn_state[t] = EXPLORED; |
| 10046 | env->cfg.cur_stack--; |
| 10047 | break; |
| 10048 | case KEEP_EXPLORING: |
| 10049 | break; |
| 10050 | default: |
| 10051 | if (ret > 0) { |
| 10052 | verbose(env, "visit_insn internal bug\n"); |
| 10053 | ret = -EFAULT; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 10054 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10055 | goto err_free; |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 10056 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10057 | } |
| 10058 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 10059 | if (env->cfg.cur_stack < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10060 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10061 | ret = -EFAULT; |
| 10062 | goto err_free; |
| 10063 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10064 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10065 | for (i = 0; i < insn_cnt; i++) { |
| 10066 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10067 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10068 | ret = -EINVAL; |
| 10069 | goto err_free; |
| 10070 | } |
| 10071 | } |
| 10072 | ret = 0; /* cfg looks good */ |
| 10073 | |
| 10074 | err_free: |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 10075 | kvfree(insn_state); |
| 10076 | kvfree(insn_stack); |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 10077 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10078 | return ret; |
| 10079 | } |
| 10080 | |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10081 | static int check_abnormal_return(struct bpf_verifier_env *env) |
| 10082 | { |
| 10083 | int i; |
| 10084 | |
| 10085 | for (i = 1; i < env->subprog_cnt; i++) { |
| 10086 | if (env->subprog_info[i].has_ld_abs) { |
| 10087 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); |
| 10088 | return -EINVAL; |
| 10089 | } |
| 10090 | if (env->subprog_info[i].has_tail_call) { |
| 10091 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); |
| 10092 | return -EINVAL; |
| 10093 | } |
| 10094 | } |
| 10095 | return 0; |
| 10096 | } |
| 10097 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10098 | /* The minimum supported BTF func info size */ |
| 10099 | #define MIN_BPF_FUNCINFO_SIZE 8 |
| 10100 | #define MAX_FUNCINFO_REC_SIZE 252 |
| 10101 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10102 | static int check_btf_func(struct bpf_verifier_env *env, |
| 10103 | const union bpf_attr *attr, |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10104 | bpfptr_t uattr) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10105 | { |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10106 | const struct btf_type *type, *func_proto, *ret_type; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 10107 | u32 i, nfuncs, urec_size, min_size; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10108 | u32 krec_size = sizeof(struct bpf_func_info); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10109 | struct bpf_func_info *krecord; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10110 | struct bpf_func_info_aux *info_aux = NULL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10111 | struct bpf_prog *prog; |
| 10112 | const struct btf *btf; |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10113 | bpfptr_t urecord; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 10114 | u32 prev_offset = 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10115 | bool scalar_return; |
Dan Carpenter | e7ed83d | 2020-06-04 11:54:36 +0300 | [diff] [blame] | 10116 | int ret = -ENOMEM; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10117 | |
| 10118 | nfuncs = attr->func_info_cnt; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10119 | if (!nfuncs) { |
| 10120 | if (check_abnormal_return(env)) |
| 10121 | return -EINVAL; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10122 | return 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10123 | } |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10124 | |
| 10125 | if (nfuncs != env->subprog_cnt) { |
| 10126 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); |
| 10127 | return -EINVAL; |
| 10128 | } |
| 10129 | |
| 10130 | urec_size = attr->func_info_rec_size; |
| 10131 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || |
| 10132 | urec_size > MAX_FUNCINFO_REC_SIZE || |
| 10133 | urec_size % sizeof(u32)) { |
| 10134 | verbose(env, "invalid func info rec size %u\n", urec_size); |
| 10135 | return -EINVAL; |
| 10136 | } |
| 10137 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10138 | prog = env->prog; |
| 10139 | btf = prog->aux->btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10140 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10141 | urecord = make_bpfptr(attr->func_info, uattr.is_kernel); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10142 | min_size = min_t(u32, krec_size, urec_size); |
| 10143 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10144 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10145 | if (!krecord) |
| 10146 | return -ENOMEM; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10147 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
| 10148 | if (!info_aux) |
| 10149 | goto err_free; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10150 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10151 | for (i = 0; i < nfuncs; i++) { |
| 10152 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); |
| 10153 | if (ret) { |
| 10154 | if (ret == -E2BIG) { |
| 10155 | verbose(env, "nonzero tailing record in func info"); |
| 10156 | /* set the size kernel expects so loader can zero |
| 10157 | * out the rest of the record. |
| 10158 | */ |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10159 | if (copy_to_bpfptr_offset(uattr, |
| 10160 | offsetof(union bpf_attr, func_info_rec_size), |
| 10161 | &min_size, sizeof(min_size))) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10162 | ret = -EFAULT; |
| 10163 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10164 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10165 | } |
| 10166 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10167 | if (copy_from_bpfptr(&krecord[i], urecord, min_size)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10168 | ret = -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10169 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10170 | } |
| 10171 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10172 | /* check insn_off */ |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10173 | ret = -EINVAL; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10174 | if (i == 0) { |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10175 | if (krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10176 | verbose(env, |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10177 | "nonzero insn_off %u for the first func info record", |
| 10178 | krecord[i].insn_off); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10179 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10180 | } |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10181 | } else if (krecord[i].insn_off <= prev_offset) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10182 | verbose(env, |
| 10183 | "same or smaller insn offset (%u) than previous func info record (%u)", |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10184 | krecord[i].insn_off, prev_offset); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10185 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10186 | } |
| 10187 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10188 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10189 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10190 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10191 | } |
| 10192 | |
| 10193 | /* check type_id */ |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10194 | type = btf_type_by_id(btf, krecord[i].type_id); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 10195 | if (!type || !btf_type_is_func(type)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10196 | verbose(env, "invalid type id %d in func info", |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10197 | krecord[i].type_id); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10198 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10199 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 10200 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10201 | |
| 10202 | func_proto = btf_type_by_id(btf, type->type); |
| 10203 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) |
| 10204 | /* btf_func_check() already verified it during BTF load */ |
| 10205 | goto err_free; |
| 10206 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); |
| 10207 | scalar_return = |
| 10208 | btf_type_is_small_int(ret_type) || btf_type_is_enum(ret_type); |
| 10209 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { |
| 10210 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); |
| 10211 | goto err_free; |
| 10212 | } |
| 10213 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { |
| 10214 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); |
| 10215 | goto err_free; |
| 10216 | } |
| 10217 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10218 | prev_offset = krecord[i].insn_off; |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10219 | bpfptr_add(&urecord, urec_size); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10220 | } |
| 10221 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10222 | prog->aux->func_info = krecord; |
| 10223 | prog->aux->func_info_cnt = nfuncs; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10224 | prog->aux->func_info_aux = info_aux; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10225 | return 0; |
| 10226 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10227 | err_free: |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10228 | kvfree(krecord); |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10229 | kfree(info_aux); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10230 | return ret; |
| 10231 | } |
| 10232 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10233 | static void adjust_btf_func(struct bpf_verifier_env *env) |
| 10234 | { |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10235 | struct bpf_prog_aux *aux = env->prog->aux; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10236 | int i; |
| 10237 | |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10238 | if (!aux->func_info) |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10239 | return; |
| 10240 | |
| 10241 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10242 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10243 | } |
| 10244 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10245 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
| 10246 | sizeof(((struct bpf_line_info *)(0))->line_col)) |
| 10247 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
| 10248 | |
| 10249 | static int check_btf_line(struct bpf_verifier_env *env, |
| 10250 | const union bpf_attr *attr, |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10251 | bpfptr_t uattr) |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10252 | { |
| 10253 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; |
| 10254 | struct bpf_subprog_info *sub; |
| 10255 | struct bpf_line_info *linfo; |
| 10256 | struct bpf_prog *prog; |
| 10257 | const struct btf *btf; |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10258 | bpfptr_t ulinfo; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10259 | int err; |
| 10260 | |
| 10261 | nr_linfo = attr->line_info_cnt; |
| 10262 | if (!nr_linfo) |
| 10263 | return 0; |
Bixuan Cui | 0e6491b | 2021-09-11 08:55:57 +0800 | [diff] [blame] | 10264 | if (nr_linfo > INT_MAX / sizeof(struct bpf_line_info)) |
| 10265 | return -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10266 | |
| 10267 | rec_size = attr->line_info_rec_size; |
| 10268 | if (rec_size < MIN_BPF_LINEINFO_SIZE || |
| 10269 | rec_size > MAX_LINEINFO_REC_SIZE || |
| 10270 | rec_size & (sizeof(u32) - 1)) |
| 10271 | return -EINVAL; |
| 10272 | |
| 10273 | /* Need to zero it in case the userspace may |
| 10274 | * pass in a smaller bpf_line_info object. |
| 10275 | */ |
| 10276 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), |
| 10277 | GFP_KERNEL | __GFP_NOWARN); |
| 10278 | if (!linfo) |
| 10279 | return -ENOMEM; |
| 10280 | |
| 10281 | prog = env->prog; |
| 10282 | btf = prog->aux->btf; |
| 10283 | |
| 10284 | s = 0; |
| 10285 | sub = env->subprog_info; |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10286 | ulinfo = make_bpfptr(attr->line_info, uattr.is_kernel); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10287 | expected_size = sizeof(struct bpf_line_info); |
| 10288 | ncopy = min_t(u32, expected_size, rec_size); |
| 10289 | for (i = 0; i < nr_linfo; i++) { |
| 10290 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); |
| 10291 | if (err) { |
| 10292 | if (err == -E2BIG) { |
| 10293 | verbose(env, "nonzero tailing record in line_info"); |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10294 | if (copy_to_bpfptr_offset(uattr, |
| 10295 | offsetof(union bpf_attr, line_info_rec_size), |
| 10296 | &expected_size, sizeof(expected_size))) |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10297 | err = -EFAULT; |
| 10298 | } |
| 10299 | goto err_free; |
| 10300 | } |
| 10301 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10302 | if (copy_from_bpfptr(&linfo[i], ulinfo, ncopy)) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10303 | err = -EFAULT; |
| 10304 | goto err_free; |
| 10305 | } |
| 10306 | |
| 10307 | /* |
| 10308 | * Check insn_off to ensure |
| 10309 | * 1) strictly increasing AND |
| 10310 | * 2) bounded by prog->len |
| 10311 | * |
| 10312 | * The linfo[0].insn_off == 0 check logically falls into |
| 10313 | * the later "missing bpf_line_info for func..." case |
| 10314 | * because the first linfo[0].insn_off must be the |
| 10315 | * first sub also and the first sub must have |
| 10316 | * subprog_info[0].start == 0. |
| 10317 | */ |
| 10318 | if ((i && linfo[i].insn_off <= prev_offset) || |
| 10319 | linfo[i].insn_off >= prog->len) { |
| 10320 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", |
| 10321 | i, linfo[i].insn_off, prev_offset, |
| 10322 | prog->len); |
| 10323 | err = -EINVAL; |
| 10324 | goto err_free; |
| 10325 | } |
| 10326 | |
Martin KaFai Lau | fdbaa0b | 2018-12-19 13:01:01 -0800 | [diff] [blame] | 10327 | if (!prog->insnsi[linfo[i].insn_off].code) { |
| 10328 | verbose(env, |
| 10329 | "Invalid insn code at line_info[%u].insn_off\n", |
| 10330 | i); |
| 10331 | err = -EINVAL; |
| 10332 | goto err_free; |
| 10333 | } |
| 10334 | |
Martin KaFai Lau | 23127b3 | 2018-12-13 10:41:46 -0800 | [diff] [blame] | 10335 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
| 10336 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10337 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
| 10338 | err = -EINVAL; |
| 10339 | goto err_free; |
| 10340 | } |
| 10341 | |
| 10342 | if (s != env->subprog_cnt) { |
| 10343 | if (linfo[i].insn_off == sub[s].start) { |
| 10344 | sub[s].linfo_idx = i; |
| 10345 | s++; |
| 10346 | } else if (sub[s].start < linfo[i].insn_off) { |
| 10347 | verbose(env, "missing bpf_line_info for func#%u\n", s); |
| 10348 | err = -EINVAL; |
| 10349 | goto err_free; |
| 10350 | } |
| 10351 | } |
| 10352 | |
| 10353 | prev_offset = linfo[i].insn_off; |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10354 | bpfptr_add(&ulinfo, rec_size); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10355 | } |
| 10356 | |
| 10357 | if (s != env->subprog_cnt) { |
| 10358 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", |
| 10359 | env->subprog_cnt - s, s); |
| 10360 | err = -EINVAL; |
| 10361 | goto err_free; |
| 10362 | } |
| 10363 | |
| 10364 | prog->aux->linfo = linfo; |
| 10365 | prog->aux->nr_linfo = nr_linfo; |
| 10366 | |
| 10367 | return 0; |
| 10368 | |
| 10369 | err_free: |
| 10370 | kvfree(linfo); |
| 10371 | return err; |
| 10372 | } |
| 10373 | |
Alexei Starovoitov | fbd94c7 | 2021-12-01 10:10:28 -0800 | [diff] [blame] | 10374 | #define MIN_CORE_RELO_SIZE sizeof(struct bpf_core_relo) |
| 10375 | #define MAX_CORE_RELO_SIZE MAX_FUNCINFO_REC_SIZE |
| 10376 | |
| 10377 | static int check_core_relo(struct bpf_verifier_env *env, |
| 10378 | const union bpf_attr *attr, |
| 10379 | bpfptr_t uattr) |
| 10380 | { |
| 10381 | u32 i, nr_core_relo, ncopy, expected_size, rec_size; |
| 10382 | struct bpf_core_relo core_relo = {}; |
| 10383 | struct bpf_prog *prog = env->prog; |
| 10384 | const struct btf *btf = prog->aux->btf; |
| 10385 | struct bpf_core_ctx ctx = { |
| 10386 | .log = &env->log, |
| 10387 | .btf = btf, |
| 10388 | }; |
| 10389 | bpfptr_t u_core_relo; |
| 10390 | int err; |
| 10391 | |
| 10392 | nr_core_relo = attr->core_relo_cnt; |
| 10393 | if (!nr_core_relo) |
| 10394 | return 0; |
| 10395 | if (nr_core_relo > INT_MAX / sizeof(struct bpf_core_relo)) |
| 10396 | return -EINVAL; |
| 10397 | |
| 10398 | rec_size = attr->core_relo_rec_size; |
| 10399 | if (rec_size < MIN_CORE_RELO_SIZE || |
| 10400 | rec_size > MAX_CORE_RELO_SIZE || |
| 10401 | rec_size % sizeof(u32)) |
| 10402 | return -EINVAL; |
| 10403 | |
| 10404 | u_core_relo = make_bpfptr(attr->core_relos, uattr.is_kernel); |
| 10405 | expected_size = sizeof(struct bpf_core_relo); |
| 10406 | ncopy = min_t(u32, expected_size, rec_size); |
| 10407 | |
| 10408 | /* Unlike func_info and line_info, copy and apply each CO-RE |
| 10409 | * relocation record one at a time. |
| 10410 | */ |
| 10411 | for (i = 0; i < nr_core_relo; i++) { |
| 10412 | /* future proofing when sizeof(bpf_core_relo) changes */ |
| 10413 | err = bpf_check_uarg_tail_zero(u_core_relo, expected_size, rec_size); |
| 10414 | if (err) { |
| 10415 | if (err == -E2BIG) { |
| 10416 | verbose(env, "nonzero tailing record in core_relo"); |
| 10417 | if (copy_to_bpfptr_offset(uattr, |
| 10418 | offsetof(union bpf_attr, core_relo_rec_size), |
| 10419 | &expected_size, sizeof(expected_size))) |
| 10420 | err = -EFAULT; |
| 10421 | } |
| 10422 | break; |
| 10423 | } |
| 10424 | |
| 10425 | if (copy_from_bpfptr(&core_relo, u_core_relo, ncopy)) { |
| 10426 | err = -EFAULT; |
| 10427 | break; |
| 10428 | } |
| 10429 | |
| 10430 | if (core_relo.insn_off % 8 || core_relo.insn_off / 8 >= prog->len) { |
| 10431 | verbose(env, "Invalid core_relo[%u].insn_off:%u prog->len:%u\n", |
| 10432 | i, core_relo.insn_off, prog->len); |
| 10433 | err = -EINVAL; |
| 10434 | break; |
| 10435 | } |
| 10436 | |
| 10437 | err = bpf_core_apply(&ctx, &core_relo, i, |
| 10438 | &prog->insnsi[core_relo.insn_off / 8]); |
| 10439 | if (err) |
| 10440 | break; |
| 10441 | bpfptr_add(&u_core_relo, rec_size); |
| 10442 | } |
| 10443 | return err; |
| 10444 | } |
| 10445 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10446 | static int check_btf_info(struct bpf_verifier_env *env, |
| 10447 | const union bpf_attr *attr, |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10448 | bpfptr_t uattr) |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10449 | { |
| 10450 | struct btf *btf; |
| 10451 | int err; |
| 10452 | |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10453 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
| 10454 | if (check_abnormal_return(env)) |
| 10455 | return -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10456 | return 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10457 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10458 | |
| 10459 | btf = btf_get_by_fd(attr->prog_btf_fd); |
| 10460 | if (IS_ERR(btf)) |
| 10461 | return PTR_ERR(btf); |
Alexei Starovoitov | 350a5c4 | 2021-03-07 14:52:48 -0800 | [diff] [blame] | 10462 | if (btf_is_kernel(btf)) { |
| 10463 | btf_put(btf); |
| 10464 | return -EACCES; |
| 10465 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10466 | env->prog->aux->btf = btf; |
| 10467 | |
| 10468 | err = check_btf_func(env, attr, uattr); |
| 10469 | if (err) |
| 10470 | return err; |
| 10471 | |
| 10472 | err = check_btf_line(env, attr, uattr); |
| 10473 | if (err) |
| 10474 | return err; |
| 10475 | |
Alexei Starovoitov | fbd94c7 | 2021-12-01 10:10:28 -0800 | [diff] [blame] | 10476 | err = check_core_relo(env, attr, uattr); |
| 10477 | if (err) |
| 10478 | return err; |
| 10479 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10480 | return 0; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 10481 | } |
| 10482 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10483 | /* check %cur's range satisfies %old's */ |
| 10484 | static bool range_within(struct bpf_reg_state *old, |
| 10485 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 10486 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 10487 | return old->umin_value <= cur->umin_value && |
| 10488 | old->umax_value >= cur->umax_value && |
| 10489 | old->smin_value <= cur->smin_value && |
Daniel Borkmann | fd67518 | 2021-02-05 20:48:21 +0100 | [diff] [blame] | 10490 | old->smax_value >= cur->smax_value && |
| 10491 | old->u32_min_value <= cur->u32_min_value && |
| 10492 | old->u32_max_value >= cur->u32_max_value && |
| 10493 | old->s32_min_value <= cur->s32_min_value && |
| 10494 | old->s32_max_value >= cur->s32_max_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10495 | } |
| 10496 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10497 | /* If in the old state two registers had the same id, then they need to have |
| 10498 | * the same id in the new state as well. But that id could be different from |
| 10499 | * the old state, so we need to track the mapping from old to new ids. |
| 10500 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 10501 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 10502 | * regs with a different old id could still have new id 9, we don't care about |
| 10503 | * that. |
| 10504 | * So we look through our idmap to see if this old id has been seen before. If |
| 10505 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 10506 | */ |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10507 | static bool check_ids(u32 old_id, u32 cur_id, struct bpf_id_pair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10508 | { |
| 10509 | unsigned int i; |
| 10510 | |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10511 | for (i = 0; i < BPF_ID_MAP_SIZE; i++) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10512 | if (!idmap[i].old) { |
| 10513 | /* Reached an empty slot; haven't seen this id before */ |
| 10514 | idmap[i].old = old_id; |
| 10515 | idmap[i].cur = cur_id; |
| 10516 | return true; |
| 10517 | } |
| 10518 | if (idmap[i].old == old_id) |
| 10519 | return idmap[i].cur == cur_id; |
| 10520 | } |
| 10521 | /* We ran out of idmap slots, which should be impossible */ |
| 10522 | WARN_ON_ONCE(1); |
| 10523 | return false; |
| 10524 | } |
| 10525 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 10526 | static void clean_func_state(struct bpf_verifier_env *env, |
| 10527 | struct bpf_func_state *st) |
| 10528 | { |
| 10529 | enum bpf_reg_liveness live; |
| 10530 | int i, j; |
| 10531 | |
| 10532 | for (i = 0; i < BPF_REG_FP; i++) { |
| 10533 | live = st->regs[i].live; |
| 10534 | /* liveness must not touch this register anymore */ |
| 10535 | st->regs[i].live |= REG_LIVE_DONE; |
| 10536 | if (!(live & REG_LIVE_READ)) |
| 10537 | /* since the register is unused, clear its state |
| 10538 | * to make further comparison simpler |
| 10539 | */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 10540 | __mark_reg_not_init(env, &st->regs[i]); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 10541 | } |
| 10542 | |
| 10543 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { |
| 10544 | live = st->stack[i].spilled_ptr.live; |
| 10545 | /* liveness must not touch this stack slot anymore */ |
| 10546 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; |
| 10547 | if (!(live & REG_LIVE_READ)) { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 10548 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 10549 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 10550 | st->stack[i].slot_type[j] = STACK_INVALID; |
| 10551 | } |
| 10552 | } |
| 10553 | } |
| 10554 | |
| 10555 | static void clean_verifier_state(struct bpf_verifier_env *env, |
| 10556 | struct bpf_verifier_state *st) |
| 10557 | { |
| 10558 | int i; |
| 10559 | |
| 10560 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) |
| 10561 | /* all regs in this state in all frames were already marked */ |
| 10562 | return; |
| 10563 | |
| 10564 | for (i = 0; i <= st->curframe; i++) |
| 10565 | clean_func_state(env, st->frame[i]); |
| 10566 | } |
| 10567 | |
| 10568 | /* the parentage chains form a tree. |
| 10569 | * the verifier states are added to state lists at given insn and |
| 10570 | * pushed into state stack for future exploration. |
| 10571 | * when the verifier reaches bpf_exit insn some of the verifer states |
| 10572 | * stored in the state lists have their final liveness state already, |
| 10573 | * but a lot of states will get revised from liveness point of view when |
| 10574 | * the verifier explores other branches. |
| 10575 | * Example: |
| 10576 | * 1: r0 = 1 |
| 10577 | * 2: if r1 == 100 goto pc+1 |
| 10578 | * 3: r0 = 2 |
| 10579 | * 4: exit |
| 10580 | * when the verifier reaches exit insn the register r0 in the state list of |
| 10581 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch |
| 10582 | * of insn 2 and goes exploring further. At the insn 4 it will walk the |
| 10583 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. |
| 10584 | * |
| 10585 | * Since the verifier pushes the branch states as it sees them while exploring |
| 10586 | * the program the condition of walking the branch instruction for the second |
| 10587 | * time means that all states below this branch were already explored and |
Zhen Lei | 8fb33b6 | 2021-05-25 10:56:59 +0800 | [diff] [blame] | 10588 | * their final liveness marks are already propagated. |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 10589 | * Hence when the verifier completes the search of state list in is_state_visited() |
| 10590 | * we can call this clean_live_states() function to mark all liveness states |
| 10591 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' |
| 10592 | * will not be used. |
| 10593 | * This function also clears the registers and stack for states that !READ |
| 10594 | * to simplify state merging. |
| 10595 | * |
| 10596 | * Important note here that walking the same branch instruction in the callee |
| 10597 | * doesn't meant that the states are DONE. The verifier has to compare |
| 10598 | * the callsites |
| 10599 | */ |
| 10600 | static void clean_live_states(struct bpf_verifier_env *env, int insn, |
| 10601 | struct bpf_verifier_state *cur) |
| 10602 | { |
| 10603 | struct bpf_verifier_state_list *sl; |
| 10604 | int i; |
| 10605 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 10606 | sl = *explored_state(env, insn); |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 10607 | while (sl) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 10608 | if (sl->state.branches) |
| 10609 | goto next; |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 10610 | if (sl->state.insn_idx != insn || |
| 10611 | sl->state.curframe != cur->curframe) |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 10612 | goto next; |
| 10613 | for (i = 0; i <= cur->curframe; i++) |
| 10614 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) |
| 10615 | goto next; |
| 10616 | clean_verifier_state(env, &sl->state); |
| 10617 | next: |
| 10618 | sl = sl->next; |
| 10619 | } |
| 10620 | } |
| 10621 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10622 | /* Returns true if (rold safe implies rcur safe) */ |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 10623 | static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, |
| 10624 | struct bpf_reg_state *rcur, struct bpf_id_pair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10625 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10626 | bool equal; |
| 10627 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10628 | if (!(rold->live & REG_LIVE_READ)) |
| 10629 | /* explored state didn't use this */ |
| 10630 | return true; |
| 10631 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 10632 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10633 | |
| 10634 | if (rold->type == PTR_TO_STACK) |
| 10635 | /* two stack pointers are equal only if they're pointing to |
| 10636 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 10637 | */ |
| 10638 | return equal && rold->frameno == rcur->frameno; |
| 10639 | |
| 10640 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10641 | return true; |
| 10642 | |
| 10643 | if (rold->type == NOT_INIT) |
| 10644 | /* explored state can't have used this */ |
| 10645 | return true; |
| 10646 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 10647 | return false; |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 10648 | switch (base_type(rold->type)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10649 | case SCALAR_VALUE: |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 10650 | if (env->explore_alu_limits) |
| 10651 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10652 | if (rcur->type == SCALAR_VALUE) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 10653 | if (!rold->precise && !rcur->precise) |
| 10654 | return true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10655 | /* new val must satisfy old val knowledge */ |
| 10656 | return range_within(rold, rcur) && |
| 10657 | tnum_in(rold->var_off, rcur->var_off); |
| 10658 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 10659 | /* We're trying to use a pointer in place of a scalar. |
| 10660 | * Even if the scalar was unbounded, this could lead to |
| 10661 | * pointer leaks because scalars are allowed to leak |
| 10662 | * while pointers are not. We could make this safe in |
| 10663 | * special cases if root is calling us, but it's |
| 10664 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10665 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 10666 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10667 | } |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 10668 | case PTR_TO_MAP_KEY: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10669 | case PTR_TO_MAP_VALUE: |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 10670 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 10671 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 10672 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 10673 | * checked, doing so could have affected others with the same |
| 10674 | * id, and we can't check for that because we lost the id when |
| 10675 | * we converted to a PTR_TO_MAP_VALUE. |
| 10676 | */ |
| 10677 | if (type_may_be_null(rold->type)) { |
| 10678 | if (!type_may_be_null(rcur->type)) |
| 10679 | return false; |
| 10680 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 10681 | return false; |
| 10682 | /* Check our ids match any regs they're supposed to */ |
| 10683 | return check_ids(rold->id, rcur->id, idmap); |
| 10684 | } |
| 10685 | |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 10686 | /* If the new min/max/var_off satisfy the old ones and |
| 10687 | * everything else matches, we are OK. |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 10688 | * 'id' is not compared, since it's only used for maps with |
| 10689 | * bpf_spin_lock inside map element and in such cases if |
| 10690 | * the rest of the prog is valid for one map element then |
| 10691 | * it's valid for all map elements regardless of the key |
| 10692 | * used in bpf_map_lookup() |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 10693 | */ |
| 10694 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 10695 | range_within(rold, rcur) && |
| 10696 | tnum_in(rold->var_off, rcur->var_off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 10697 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10698 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 10699 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10700 | return false; |
| 10701 | /* We must have at least as much range as the old ptr |
| 10702 | * did, so that any accesses which were safe before are |
| 10703 | * still safe. This is true even if old range < old off, |
| 10704 | * since someone could have accessed through (ptr - k), or |
| 10705 | * even done ptr -= k in a register, to get a safe access. |
| 10706 | */ |
| 10707 | if (rold->range > rcur->range) |
| 10708 | return false; |
| 10709 | /* If the offsets don't match, we can't trust our alignment; |
| 10710 | * nor can we be sure that we won't fall out of range. |
| 10711 | */ |
| 10712 | if (rold->off != rcur->off) |
| 10713 | return false; |
| 10714 | /* id relations must be preserved */ |
| 10715 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 10716 | return false; |
| 10717 | /* new val must satisfy old val knowledge */ |
| 10718 | return range_within(rold, rcur) && |
| 10719 | tnum_in(rold->var_off, rcur->var_off); |
| 10720 | case PTR_TO_CTX: |
| 10721 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10722 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 10723 | case PTR_TO_FLOW_KEYS: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10724 | case PTR_TO_SOCKET: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 10725 | case PTR_TO_SOCK_COMMON: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 10726 | case PTR_TO_TCP_SOCK: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 10727 | case PTR_TO_XDP_SOCK: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10728 | /* Only valid matches are exact, which memcmp() above |
| 10729 | * would have accepted |
| 10730 | */ |
| 10731 | default: |
| 10732 | /* Don't know what's going on, just say it's not safe */ |
| 10733 | return false; |
| 10734 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 10735 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10736 | /* Shouldn't get here; if we do, say it's not safe */ |
| 10737 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 10738 | return false; |
| 10739 | } |
| 10740 | |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 10741 | static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, |
| 10742 | struct bpf_func_state *cur, struct bpf_id_pair *idmap) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10743 | { |
| 10744 | int i, spi; |
| 10745 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10746 | /* walk slots of the explored stack and ignore any additional |
| 10747 | * slots in the current stack, since explored(safe) state |
| 10748 | * didn't use them |
| 10749 | */ |
| 10750 | for (i = 0; i < old->allocated_stack; i++) { |
| 10751 | spi = i / BPF_REG_SIZE; |
| 10752 | |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 10753 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
| 10754 | i += BPF_REG_SIZE - 1; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 10755 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 10756 | continue; |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 10757 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 10758 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10759 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 10760 | continue; |
Alexei Starovoitov | 19e2dbb | 2018-12-13 11:42:33 -0800 | [diff] [blame] | 10761 | |
| 10762 | /* explored stack has more populated slots than current stack |
| 10763 | * and these slots were used |
| 10764 | */ |
| 10765 | if (i >= cur->allocated_stack) |
| 10766 | return false; |
| 10767 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 10768 | /* if old state was safe with misc data in the stack |
| 10769 | * it will be safe with zero-initialized stack. |
| 10770 | * The opposite is not true |
| 10771 | */ |
| 10772 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 10773 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 10774 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10775 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 10776 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 10777 | /* Ex: old explored (safe) state has STACK_SPILL in |
Randy Dunlap | b8c1a30 | 2020-08-06 20:31:41 -0700 | [diff] [blame] | 10778 | * this stack slot, but current has STACK_MISC -> |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10779 | * this verifier states are not equivalent, |
| 10780 | * return false to continue verification of this path |
| 10781 | */ |
| 10782 | return false; |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 10783 | if (i % BPF_REG_SIZE != BPF_REG_SIZE - 1) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10784 | continue; |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 10785 | if (!is_spilled_reg(&old->stack[spi])) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10786 | continue; |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 10787 | if (!regsafe(env, &old->stack[spi].spilled_ptr, |
| 10788 | &cur->stack[spi].spilled_ptr, idmap)) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10789 | /* when explored and current stack slot are both storing |
| 10790 | * spilled registers, check that stored pointers types |
| 10791 | * are the same as well. |
| 10792 | * Ex: explored safe path could have stored |
| 10793 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 10794 | * but current path has stored: |
| 10795 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 10796 | * such verifier states are not equivalent. |
| 10797 | * return false to continue verification of this path |
| 10798 | */ |
| 10799 | return false; |
| 10800 | } |
| 10801 | return true; |
| 10802 | } |
| 10803 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 10804 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 10805 | { |
| 10806 | if (old->acquired_refs != cur->acquired_refs) |
| 10807 | return false; |
| 10808 | return !memcmp(old->refs, cur->refs, |
| 10809 | sizeof(*old->refs) * old->acquired_refs); |
| 10810 | } |
| 10811 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 10812 | /* compare two verifier states |
| 10813 | * |
| 10814 | * all states stored in state_list are known to be valid, since |
| 10815 | * verifier reached 'bpf_exit' instruction through them |
| 10816 | * |
| 10817 | * this function is called when verifier exploring different branches of |
| 10818 | * execution popped from the state stack. If it sees an old state that has |
| 10819 | * more strict register state and more strict stack state then this execution |
| 10820 | * branch doesn't need to be explored further, since verifier already |
| 10821 | * concluded that more strict state leads to valid finish. |
| 10822 | * |
| 10823 | * Therefore two states are equivalent if register state is more conservative |
| 10824 | * and explored stack state is more conservative than the current one. |
| 10825 | * Example: |
| 10826 | * explored current |
| 10827 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 10828 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 10829 | * |
| 10830 | * In other words if current stack state (one being explored) has more |
| 10831 | * valid slots than old one that already passed validation, it means |
| 10832 | * the verifier can stop exploring and conclude that current state is valid too |
| 10833 | * |
| 10834 | * Similarly with registers. If explored state has register type as invalid |
| 10835 | * whereas register type in current state is meaningful, it means that |
| 10836 | * the current state will reach 'bpf_exit' instruction safely |
| 10837 | */ |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10838 | static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_state *old, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10839 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 10840 | { |
| 10841 | int i; |
| 10842 | |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10843 | memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch)); |
| 10844 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 10845 | if (!regsafe(env, &old->regs[i], &cur->regs[i], |
| 10846 | env->idmap_scratch)) |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10847 | return false; |
| 10848 | |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 10849 | if (!stacksafe(env, old, cur, env->idmap_scratch)) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 10850 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10851 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 10852 | if (!refsafe(old, cur)) |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10853 | return false; |
| 10854 | |
| 10855 | return true; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 10856 | } |
| 10857 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10858 | static bool states_equal(struct bpf_verifier_env *env, |
| 10859 | struct bpf_verifier_state *old, |
| 10860 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10861 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10862 | int i; |
| 10863 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10864 | if (old->curframe != cur->curframe) |
| 10865 | return false; |
| 10866 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 10867 | /* Verification state from speculative execution simulation |
| 10868 | * must never prune a non-speculative execution one. |
| 10869 | */ |
| 10870 | if (old->speculative && !cur->speculative) |
| 10871 | return false; |
| 10872 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 10873 | if (old->active_spin_lock != cur->active_spin_lock) |
| 10874 | return false; |
| 10875 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10876 | /* for states to be equal callsites have to be the same |
| 10877 | * and all frame states need to be equivalent |
| 10878 | */ |
| 10879 | for (i = 0; i <= old->curframe; i++) { |
| 10880 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 10881 | return false; |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10882 | if (!func_states_equal(env, old->frame[i], cur->frame[i])) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10883 | return false; |
| 10884 | } |
| 10885 | return true; |
| 10886 | } |
| 10887 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10888 | /* Return 0 if no propagation happened. Return negative error code if error |
| 10889 | * happened. Otherwise, return the propagated bit. |
| 10890 | */ |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10891 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
| 10892 | struct bpf_reg_state *reg, |
| 10893 | struct bpf_reg_state *parent_reg) |
| 10894 | { |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10895 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
| 10896 | u8 flag = reg->live & REG_LIVE_READ; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10897 | int err; |
| 10898 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10899 | /* When comes here, read flags of PARENT_REG or REG could be any of |
| 10900 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need |
| 10901 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. |
| 10902 | */ |
| 10903 | if (parent_flag == REG_LIVE_READ64 || |
| 10904 | /* Or if there is no read flag from REG. */ |
| 10905 | !flag || |
| 10906 | /* Or if the read flag from REG is the same as PARENT_REG. */ |
| 10907 | parent_flag == flag) |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10908 | return 0; |
| 10909 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10910 | err = mark_reg_read(env, reg, parent_reg, flag); |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10911 | if (err) |
| 10912 | return err; |
| 10913 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10914 | return flag; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10915 | } |
| 10916 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10917 | /* A write screens off any subsequent reads; but write marks come from the |
| 10918 | * straight-line code between a state and its parent. When we arrive at an |
| 10919 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 10920 | * code, so read marks in the state must propagate to the parent regardless |
| 10921 | * of the state's write marks. That's what 'parent == state->parent' comparison |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 10922 | * in mark_reg_read() is for. |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10923 | */ |
| 10924 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 10925 | const struct bpf_verifier_state *vstate, |
| 10926 | struct bpf_verifier_state *vparent) |
| 10927 | { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 10928 | struct bpf_reg_state *state_reg, *parent_reg; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10929 | struct bpf_func_state *state, *parent; |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 10930 | int i, frame, err = 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10931 | |
| 10932 | if (vparent->curframe != vstate->curframe) { |
| 10933 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 10934 | vparent->curframe, vstate->curframe); |
| 10935 | return -EFAULT; |
| 10936 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10937 | /* Propagate read liveness of registers... */ |
| 10938 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 10939 | for (frame = 0; frame <= vstate->curframe; frame++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 10940 | parent = vparent->frame[frame]; |
| 10941 | state = vstate->frame[frame]; |
| 10942 | parent_reg = parent->regs; |
| 10943 | state_reg = state->regs; |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 10944 | /* We don't need to worry about FP liveness, it's read-only */ |
| 10945 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10946 | err = propagate_liveness_reg(env, &state_reg[i], |
| 10947 | &parent_reg[i]); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10948 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 10949 | return err; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10950 | if (err == REG_LIVE_READ64) |
| 10951 | mark_insn_zext(env, &parent_reg[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10952 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10953 | |
Jiong Wang | 1b04aee | 2019-04-12 22:59:34 +0100 | [diff] [blame] | 10954 | /* Propagate stack slots. */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10955 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 10956 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 10957 | parent_reg = &parent->stack[i].spilled_ptr; |
| 10958 | state_reg = &state->stack[i].spilled_ptr; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10959 | err = propagate_liveness_reg(env, state_reg, |
| 10960 | parent_reg); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10961 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 10962 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10963 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10964 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10965 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10966 | } |
| 10967 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 10968 | /* find precise scalars in the previous equivalent state and |
| 10969 | * propagate them into the current state |
| 10970 | */ |
| 10971 | static int propagate_precision(struct bpf_verifier_env *env, |
| 10972 | const struct bpf_verifier_state *old) |
| 10973 | { |
| 10974 | struct bpf_reg_state *state_reg; |
| 10975 | struct bpf_func_state *state; |
| 10976 | int i, err = 0; |
| 10977 | |
| 10978 | state = old->frame[old->curframe]; |
| 10979 | state_reg = state->regs; |
| 10980 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { |
| 10981 | if (state_reg->type != SCALAR_VALUE || |
| 10982 | !state_reg->precise) |
| 10983 | continue; |
| 10984 | if (env->log.level & BPF_LOG_LEVEL2) |
| 10985 | verbose(env, "propagating r%d\n", i); |
| 10986 | err = mark_chain_precision(env, i); |
| 10987 | if (err < 0) |
| 10988 | return err; |
| 10989 | } |
| 10990 | |
| 10991 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 10992 | if (!is_spilled_reg(&state->stack[i])) |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 10993 | continue; |
| 10994 | state_reg = &state->stack[i].spilled_ptr; |
| 10995 | if (state_reg->type != SCALAR_VALUE || |
| 10996 | !state_reg->precise) |
| 10997 | continue; |
| 10998 | if (env->log.level & BPF_LOG_LEVEL2) |
| 10999 | verbose(env, "propagating fp%d\n", |
| 11000 | (-i - 1) * BPF_REG_SIZE); |
| 11001 | err = mark_chain_precision_stack(env, i); |
| 11002 | if (err < 0) |
| 11003 | return err; |
| 11004 | } |
| 11005 | return 0; |
| 11006 | } |
| 11007 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11008 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
| 11009 | struct bpf_verifier_state *cur) |
| 11010 | { |
| 11011 | struct bpf_func_state *fold, *fcur; |
| 11012 | int i, fr = cur->curframe; |
| 11013 | |
| 11014 | if (old->curframe != fr) |
| 11015 | return false; |
| 11016 | |
| 11017 | fold = old->frame[fr]; |
| 11018 | fcur = cur->frame[fr]; |
| 11019 | for (i = 0; i < MAX_BPF_REG; i++) |
| 11020 | if (memcmp(&fold->regs[i], &fcur->regs[i], |
| 11021 | offsetof(struct bpf_reg_state, parent))) |
| 11022 | return false; |
| 11023 | return true; |
| 11024 | } |
| 11025 | |
| 11026 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11027 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11028 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11029 | struct bpf_verifier_state_list *new_sl; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11030 | struct bpf_verifier_state_list *sl, **pprev; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 11031 | struct bpf_verifier_state *cur = env->cur_state, *new; |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 11032 | int i, j, err, states_cnt = 0; |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 11033 | bool add_new_state = env->test_state_freq ? true : false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11034 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11035 | cur->last_insn_idx = env->prev_insn_idx; |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 11036 | if (!env->insn_aux_data[insn_idx].prune_point) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11037 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 11038 | * be doing state search here |
| 11039 | */ |
| 11040 | return 0; |
| 11041 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11042 | /* bpf progs typically have pruning point every 4 instructions |
| 11043 | * http://vger.kernel.org/bpfconf2019.html#session-1 |
| 11044 | * Do not add new state for future pruning if the verifier hasn't seen |
| 11045 | * at least 2 jumps and at least 8 instructions. |
| 11046 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. |
| 11047 | * In tests that amounts to up to 50% reduction into total verifier |
| 11048 | * memory consumption and 20% verifier time speedup. |
| 11049 | */ |
| 11050 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && |
| 11051 | env->insn_processed - env->prev_insn_processed >= 8) |
| 11052 | add_new_state = true; |
| 11053 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 11054 | pprev = explored_state(env, insn_idx); |
| 11055 | sl = *pprev; |
| 11056 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 11057 | clean_live_states(env, insn_idx, cur); |
| 11058 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 11059 | while (sl) { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 11060 | states_cnt++; |
| 11061 | if (sl->state.insn_idx != insn_idx) |
| 11062 | goto next; |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 11063 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11064 | if (sl->state.branches) { |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 11065 | struct bpf_func_state *frame = sl->state.frame[sl->state.curframe]; |
| 11066 | |
| 11067 | if (frame->in_async_callback_fn && |
| 11068 | frame->async_entry_cnt != cur->frame[cur->curframe]->async_entry_cnt) { |
| 11069 | /* Different async_entry_cnt means that the verifier is |
| 11070 | * processing another entry into async callback. |
| 11071 | * Seeing the same state is not an indication of infinite |
| 11072 | * loop or infinite recursion. |
| 11073 | * But finding the same state doesn't mean that it's safe |
| 11074 | * to stop processing the current state. The previous state |
| 11075 | * hasn't yet reached bpf_exit, since state.branches > 0. |
| 11076 | * Checking in_async_callback_fn alone is not enough either. |
| 11077 | * Since the verifier still needs to catch infinite loops |
| 11078 | * inside async callbacks. |
| 11079 | */ |
| 11080 | } else if (states_maybe_looping(&sl->state, cur) && |
| 11081 | states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11082 | verbose_linfo(env, insn_idx, "; "); |
| 11083 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); |
| 11084 | return -EINVAL; |
| 11085 | } |
| 11086 | /* if the verifier is processing a loop, avoid adding new state |
| 11087 | * too often, since different loop iterations have distinct |
| 11088 | * states and may not help future pruning. |
| 11089 | * This threshold shouldn't be too low to make sure that |
| 11090 | * a loop with large bound will be rejected quickly. |
| 11091 | * The most abusive loop will be: |
| 11092 | * r1 += 1 |
| 11093 | * if r1 < 1000000 goto pc-2 |
| 11094 | * 1M insn_procssed limit / 100 == 10k peak states. |
| 11095 | * This threshold shouldn't be too high either, since states |
| 11096 | * at the end of the loop are likely to be useful in pruning. |
| 11097 | */ |
| 11098 | if (env->jmps_processed - env->prev_jmps_processed < 20 && |
| 11099 | env->insn_processed - env->prev_insn_processed < 100) |
| 11100 | add_new_state = false; |
| 11101 | goto miss; |
| 11102 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 11103 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11104 | sl->hit_cnt++; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11105 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11106 | * prune the search. |
| 11107 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 11108 | * If we have any write marks in env->cur_state, they |
| 11109 | * will prevent corresponding reads in the continuation |
| 11110 | * from reaching our parent (an explored_state). Our |
| 11111 | * own state will get the read marks recorded, but |
| 11112 | * they'll be immediately forgotten as we're pruning |
| 11113 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11114 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11115 | err = propagate_liveness(env, &sl->state, cur); |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 11116 | |
| 11117 | /* if previous state reached the exit with precision and |
| 11118 | * current state is equivalent to it (except precsion marks) |
| 11119 | * the precision needs to be propagated back in |
| 11120 | * the current state. |
| 11121 | */ |
| 11122 | err = err ? : push_jmp_history(env, cur); |
| 11123 | err = err ? : propagate_precision(env, &sl->state); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11124 | if (err) |
| 11125 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11126 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11127 | } |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11128 | miss: |
| 11129 | /* when new state is not going to be added do not increase miss count. |
| 11130 | * Otherwise several loop iterations will remove the state |
| 11131 | * recorded earlier. The goal of these heuristics is to have |
| 11132 | * states from some iterations of the loop (some in the beginning |
| 11133 | * and some at the end) to help pruning. |
| 11134 | */ |
| 11135 | if (add_new_state) |
| 11136 | sl->miss_cnt++; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11137 | /* heuristic to determine whether this state is beneficial |
| 11138 | * to keep checking from state equivalence point of view. |
| 11139 | * Higher numbers increase max_states_per_insn and verification time, |
| 11140 | * but do not meaningfully decrease insn_processed. |
| 11141 | */ |
| 11142 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { |
| 11143 | /* the state is unlikely to be useful. Remove it to |
| 11144 | * speed up verification |
| 11145 | */ |
| 11146 | *pprev = sl->next; |
| 11147 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11148 | u32 br = sl->state.branches; |
| 11149 | |
| 11150 | WARN_ONCE(br, |
| 11151 | "BUG live_done but branches_to_explore %d\n", |
| 11152 | br); |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11153 | free_verifier_state(&sl->state, false); |
| 11154 | kfree(sl); |
| 11155 | env->peak_states--; |
| 11156 | } else { |
| 11157 | /* cannot free this state, since parentage chain may |
| 11158 | * walk it later. Add it for free_list instead to |
| 11159 | * be freed at the end of verification |
| 11160 | */ |
| 11161 | sl->next = env->free_list; |
| 11162 | env->free_list = sl; |
| 11163 | } |
| 11164 | sl = *pprev; |
| 11165 | continue; |
| 11166 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 11167 | next: |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11168 | pprev = &sl->next; |
| 11169 | sl = *pprev; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11170 | } |
| 11171 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11172 | if (env->max_states_per_insn < states_cnt) |
| 11173 | env->max_states_per_insn = states_cnt; |
| 11174 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 11175 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11176 | return push_jmp_history(env, cur); |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 11177 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11178 | if (!add_new_state) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11179 | return push_jmp_history(env, cur); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11180 | |
| 11181 | /* There were no equivalent states, remember the current one. |
| 11182 | * Technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11183 | * but it will either reach outer most bpf_exit (which means it's safe) |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11184 | * or it will be rejected. When there are no loops the verifier won't be |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11185 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11186 | * again on the way to bpf_exit. |
| 11187 | * When looping the sl->state.branches will be > 0 and this state |
| 11188 | * will not be considered for equivalence until branches == 0. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11189 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 11190 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11191 | if (!new_sl) |
| 11192 | return -ENOMEM; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11193 | env->total_states++; |
| 11194 | env->peak_states++; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11195 | env->prev_jmps_processed = env->jmps_processed; |
| 11196 | env->prev_insn_processed = env->insn_processed; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11197 | |
| 11198 | /* add new state to the head of linked list */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 11199 | new = &new_sl->state; |
| 11200 | err = copy_verifier_state(new, cur); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 11201 | if (err) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 11202 | free_verifier_state(new, false); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 11203 | kfree(new_sl); |
| 11204 | return err; |
| 11205 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 11206 | new->insn_idx = insn_idx; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11207 | WARN_ONCE(new->branches != 1, |
| 11208 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11209 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11210 | cur->parent = new; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11211 | cur->first_insn_idx = insn_idx; |
| 11212 | clear_jmp_history(cur); |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 11213 | new_sl->next = *explored_state(env, insn_idx); |
| 11214 | *explored_state(env, insn_idx) = new_sl; |
Jakub Kicinski | 7640ead | 2018-12-12 16:29:07 -0800 | [diff] [blame] | 11215 | /* connect new state to parentage chain. Current frame needs all |
| 11216 | * registers connected. Only r6 - r9 of the callers are alive (pushed |
| 11217 | * to the stack implicitly by JITs) so in callers' frames connect just |
| 11218 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to |
| 11219 | * the state of the call instruction (with WRITTEN set), and r0 comes |
| 11220 | * from callee with its full parentage chain, anyway. |
| 11221 | */ |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 11222 | /* clear write marks in current state: the writes we did are not writes |
| 11223 | * our child did, so they don't screen off its reads from us. |
| 11224 | * (There are no read marks in current state, because reads always mark |
| 11225 | * their parent and current state never has children yet. Only |
| 11226 | * explored_states can get read marks.) |
| 11227 | */ |
Alexei Starovoitov | eea1c22 | 2019-06-15 12:12:21 -0700 | [diff] [blame] | 11228 | for (j = 0; j <= cur->curframe; j++) { |
| 11229 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) |
| 11230 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; |
| 11231 | for (i = 0; i < BPF_REG_FP; i++) |
| 11232 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; |
| 11233 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11234 | |
| 11235 | /* all stack frames are accessible from callee, clear them all */ |
| 11236 | for (j = 0; j <= cur->curframe; j++) { |
| 11237 | struct bpf_func_state *frame = cur->frame[j]; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 11238 | struct bpf_func_state *newframe = new->frame[j]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11239 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 11240 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 11241 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 11242 | frame->stack[i].spilled_ptr.parent = |
| 11243 | &newframe->stack[i].spilled_ptr; |
| 11244 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11245 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11246 | return 0; |
| 11247 | } |
| 11248 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 11249 | /* Return true if it's OK to have the same insn return a different type. */ |
| 11250 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 11251 | { |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 11252 | switch (base_type(type)) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 11253 | case PTR_TO_CTX: |
| 11254 | case PTR_TO_SOCKET: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 11255 | case PTR_TO_SOCK_COMMON: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 11256 | case PTR_TO_TCP_SOCK: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 11257 | case PTR_TO_XDP_SOCK: |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 11258 | case PTR_TO_BTF_ID: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 11259 | return false; |
| 11260 | default: |
| 11261 | return true; |
| 11262 | } |
| 11263 | } |
| 11264 | |
| 11265 | /* If an instruction was previously used with particular pointer types, then we |
| 11266 | * need to be careful to avoid cases such as the below, where it may be ok |
| 11267 | * for one branch accessing the pointer, but not ok for the other branch: |
| 11268 | * |
| 11269 | * R1 = sock_ptr |
| 11270 | * goto X; |
| 11271 | * ... |
| 11272 | * R1 = some_other_valid_ptr; |
| 11273 | * goto X; |
| 11274 | * ... |
| 11275 | * R2 = *(u32 *)(R1 + 0); |
| 11276 | */ |
| 11277 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 11278 | { |
| 11279 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 11280 | !reg_type_mismatch_ok(prev)); |
| 11281 | } |
| 11282 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11283 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11284 | { |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 11285 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11286 | struct bpf_verifier_state *state = env->cur_state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11287 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 11288 | struct bpf_reg_state *regs; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11289 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11290 | bool do_print_state = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11291 | int prev_insn_idx = -1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11292 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11293 | for (;;) { |
| 11294 | struct bpf_insn *insn; |
| 11295 | u8 class; |
| 11296 | int err; |
| 11297 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11298 | env->prev_insn_idx = prev_insn_idx; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11299 | if (env->insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11300 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11301 | env->insn_idx, insn_cnt); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11302 | return -EFAULT; |
| 11303 | } |
| 11304 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11305 | insn = &insns[env->insn_idx]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11306 | class = BPF_CLASS(insn->code); |
| 11307 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11308 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11309 | verbose(env, |
| 11310 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11311 | env->insn_processed); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11312 | return -E2BIG; |
| 11313 | } |
| 11314 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11315 | err = is_state_visited(env, env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11316 | if (err < 0) |
| 11317 | return err; |
| 11318 | if (err == 1) { |
| 11319 | /* found equivalent state, can prune the search */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11320 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11321 | if (do_print_state) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 11322 | verbose(env, "\nfrom %d to %d%s: safe\n", |
| 11323 | env->prev_insn_idx, env->insn_idx, |
| 11324 | env->cur_state->speculative ? |
| 11325 | " (speculative execution)" : ""); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11326 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11327 | verbose(env, "%d: safe\n", env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11328 | } |
| 11329 | goto process_bpf_exit; |
| 11330 | } |
| 11331 | |
Alexei Starovoitov | c349480 | 2018-12-03 22:46:04 -0800 | [diff] [blame] | 11332 | if (signal_pending(current)) |
| 11333 | return -EAGAIN; |
| 11334 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 11335 | if (need_resched()) |
| 11336 | cond_resched(); |
| 11337 | |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame] | 11338 | if (env->log.level & BPF_LOG_LEVEL2 && do_print_state) { |
| 11339 | verbose(env, "\nfrom %d to %d%s:", |
| 11340 | env->prev_insn_idx, env->insn_idx, |
| 11341 | env->cur_state->speculative ? |
| 11342 | " (speculative execution)" : ""); |
| 11343 | print_verifier_state(env, state->frame[state->curframe], true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11344 | do_print_state = false; |
| 11345 | } |
| 11346 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11347 | if (env->log.level & BPF_LOG_LEVEL) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 11348 | const struct bpf_insn_cbs cbs = { |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 11349 | .cb_call = disasm_kfunc_name, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 11350 | .cb_print = verbose, |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 11351 | .private_data = env, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 11352 | }; |
| 11353 | |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame] | 11354 | if (verifier_state_scratched(env)) |
| 11355 | print_insn_state(env, state->frame[state->curframe]); |
| 11356 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11357 | verbose_linfo(env, env->insn_idx, "; "); |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame] | 11358 | env->prev_log_len = env->log.len_used; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11359 | verbose(env, "%d: ", env->insn_idx); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 11360 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame] | 11361 | env->prev_insn_print_len = env->log.len_used - env->prev_log_len; |
| 11362 | env->prev_log_len = env->log.len_used; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11363 | } |
| 11364 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 11365 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11366 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
| 11367 | env->prev_insn_idx); |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 11368 | if (err) |
| 11369 | return err; |
| 11370 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 11371 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 11372 | regs = cur_regs(env); |
Daniel Borkmann | fe9a5ca | 2021-05-28 13:47:27 +0000 | [diff] [blame] | 11373 | sanitize_mark_insn_seen(env); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11374 | prev_insn_idx = env->insn_idx; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 11375 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11376 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 11377 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11378 | if (err) |
| 11379 | return err; |
| 11380 | |
| 11381 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11382 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11383 | |
| 11384 | /* check for reserved fields is already done */ |
| 11385 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11386 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11387 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11388 | if (err) |
| 11389 | return err; |
| 11390 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11391 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11392 | if (err) |
| 11393 | return err; |
| 11394 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 11395 | src_reg_type = regs[insn->src_reg].type; |
| 11396 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11397 | /* check that memory (src_reg + off) is readable, |
| 11398 | * the state of dst_reg will be updated by this func |
| 11399 | */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11400 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
| 11401 | insn->off, BPF_SIZE(insn->code), |
| 11402 | BPF_READ, insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11403 | if (err) |
| 11404 | return err; |
| 11405 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11406 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11407 | |
| 11408 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11409 | /* saw a valid insn |
| 11410 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11411 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11412 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11413 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11414 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 11415 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11416 | /* ABuser program is trying to use the same insn |
| 11417 | * dst_reg = *(u32*) (src_reg + off) |
| 11418 | * with different pointer types: |
| 11419 | * src_reg == ctx in one branch and |
| 11420 | * src_reg == stack|map in some other branch. |
| 11421 | * Reject it. |
| 11422 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11423 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11424 | return -EINVAL; |
| 11425 | } |
| 11426 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11427 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11428 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 11429 | |
Brendan Jackman | 91c960b | 2021-01-14 18:17:44 +0000 | [diff] [blame] | 11430 | if (BPF_MODE(insn->code) == BPF_ATOMIC) { |
| 11431 | err = check_atomic(env, env->insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11432 | if (err) |
| 11433 | return err; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11434 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11435 | continue; |
| 11436 | } |
| 11437 | |
Brendan Jackman | 5ca419f | 2021-01-14 18:17:46 +0000 | [diff] [blame] | 11438 | if (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0) { |
| 11439 | verbose(env, "BPF_STX uses reserved fields\n"); |
| 11440 | return -EINVAL; |
| 11441 | } |
| 11442 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11443 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11444 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11445 | if (err) |
| 11446 | return err; |
| 11447 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11448 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11449 | if (err) |
| 11450 | return err; |
| 11451 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 11452 | dst_reg_type = regs[insn->dst_reg].type; |
| 11453 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11454 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11455 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 11456 | insn->off, BPF_SIZE(insn->code), |
| 11457 | BPF_WRITE, insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11458 | if (err) |
| 11459 | return err; |
| 11460 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11461 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11462 | |
| 11463 | if (*prev_dst_type == NOT_INIT) { |
| 11464 | *prev_dst_type = dst_reg_type; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 11465 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11466 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 11467 | return -EINVAL; |
| 11468 | } |
| 11469 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11470 | } else if (class == BPF_ST) { |
| 11471 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 11472 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11473 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11474 | return -EINVAL; |
| 11475 | } |
| 11476 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11477 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11478 | if (err) |
| 11479 | return err; |
| 11480 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 11481 | if (is_ctx_reg(env, insn->dst_reg)) { |
Joe Stringer | 9d2be44 | 2018-10-02 13:35:31 -0700 | [diff] [blame] | 11482 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 11483 | insn->dst_reg, |
Hao Luo | c25b2ae | 2021-12-16 16:31:47 -0800 | [diff] [blame] | 11484 | reg_type_str(env, reg_state(env, insn->dst_reg)->type)); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 11485 | return -EACCES; |
| 11486 | } |
| 11487 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11488 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11489 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 11490 | insn->off, BPF_SIZE(insn->code), |
| 11491 | BPF_WRITE, -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11492 | if (err) |
| 11493 | return err; |
| 11494 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 11495 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11496 | u8 opcode = BPF_OP(insn->code); |
| 11497 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11498 | env->jmps_processed++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11499 | if (opcode == BPF_CALL) { |
| 11500 | if (BPF_SRC(insn->code) != BPF_K || |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 11501 | (insn->src_reg != BPF_PSEUDO_KFUNC_CALL |
| 11502 | && insn->off != 0) || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11503 | (insn->src_reg != BPF_REG_0 && |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 11504 | insn->src_reg != BPF_PSEUDO_CALL && |
| 11505 | insn->src_reg != BPF_PSEUDO_KFUNC_CALL) || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 11506 | insn->dst_reg != BPF_REG_0 || |
| 11507 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11508 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11509 | return -EINVAL; |
| 11510 | } |
| 11511 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 11512 | if (env->cur_state->active_spin_lock && |
| 11513 | (insn->src_reg == BPF_PSEUDO_CALL || |
| 11514 | insn->imm != BPF_FUNC_spin_unlock)) { |
| 11515 | verbose(env, "function calls are not allowed while holding a lock\n"); |
| 11516 | return -EINVAL; |
| 11517 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11518 | if (insn->src_reg == BPF_PSEUDO_CALL) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11519 | err = check_func_call(env, insn, &env->insn_idx); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 11520 | else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) |
| 11521 | err = check_kfunc_call(env, insn); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11522 | else |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 11523 | err = check_helper_call(env, insn, &env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11524 | if (err) |
| 11525 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11526 | } else if (opcode == BPF_JA) { |
| 11527 | if (BPF_SRC(insn->code) != BPF_K || |
| 11528 | insn->imm != 0 || |
| 11529 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 11530 | insn->dst_reg != BPF_REG_0 || |
| 11531 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11532 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11533 | return -EINVAL; |
| 11534 | } |
| 11535 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11536 | env->insn_idx += insn->off + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11537 | continue; |
| 11538 | |
| 11539 | } else if (opcode == BPF_EXIT) { |
| 11540 | if (BPF_SRC(insn->code) != BPF_K || |
| 11541 | insn->imm != 0 || |
| 11542 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 11543 | insn->dst_reg != BPF_REG_0 || |
| 11544 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11545 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11546 | return -EINVAL; |
| 11547 | } |
| 11548 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 11549 | if (env->cur_state->active_spin_lock) { |
| 11550 | verbose(env, "bpf_spin_unlock is missing\n"); |
| 11551 | return -EINVAL; |
| 11552 | } |
| 11553 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11554 | if (state->curframe) { |
| 11555 | /* exit from nested function */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11556 | err = prepare_func_exit(env, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11557 | if (err) |
| 11558 | return err; |
| 11559 | do_print_state = true; |
| 11560 | continue; |
| 11561 | } |
| 11562 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 11563 | err = check_reference_leak(env); |
| 11564 | if (err) |
| 11565 | return err; |
| 11566 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 11567 | err = check_return_code(env); |
| 11568 | if (err) |
| 11569 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11570 | process_bpf_exit: |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 11571 | mark_verifier_state_scratched(env); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11572 | update_branch_counts(env, env->cur_state); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11573 | err = pop_stack(env, &prev_insn_idx, |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 11574 | &env->insn_idx, pop_log); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 11575 | if (err < 0) { |
| 11576 | if (err != -ENOENT) |
| 11577 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11578 | break; |
| 11579 | } else { |
| 11580 | do_print_state = true; |
| 11581 | continue; |
| 11582 | } |
| 11583 | } else { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11584 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11585 | if (err) |
| 11586 | return err; |
| 11587 | } |
| 11588 | } else if (class == BPF_LD) { |
| 11589 | u8 mode = BPF_MODE(insn->code); |
| 11590 | |
| 11591 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 11592 | err = check_ld_abs(env, insn); |
| 11593 | if (err) |
| 11594 | return err; |
| 11595 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11596 | } else if (mode == BPF_IMM) { |
| 11597 | err = check_ld_imm(env, insn); |
| 11598 | if (err) |
| 11599 | return err; |
| 11600 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11601 | env->insn_idx++; |
Daniel Borkmann | fe9a5ca | 2021-05-28 13:47:27 +0000 | [diff] [blame] | 11602 | sanitize_mark_insn_seen(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11603 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11604 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11605 | return -EINVAL; |
| 11606 | } |
| 11607 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11608 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11609 | return -EINVAL; |
| 11610 | } |
| 11611 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11612 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11613 | } |
| 11614 | |
| 11615 | return 0; |
| 11616 | } |
| 11617 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11618 | static int find_btf_percpu_datasec(struct btf *btf) |
| 11619 | { |
| 11620 | const struct btf_type *t; |
| 11621 | const char *tname; |
| 11622 | int i, n; |
| 11623 | |
| 11624 | /* |
| 11625 | * Both vmlinux and module each have their own ".data..percpu" |
| 11626 | * DATASECs in BTF. So for module's case, we need to skip vmlinux BTF |
| 11627 | * types to look at only module's own BTF types. |
| 11628 | */ |
| 11629 | n = btf_nr_types(btf); |
| 11630 | if (btf_is_module(btf)) |
| 11631 | i = btf_nr_types(btf_vmlinux); |
| 11632 | else |
| 11633 | i = 1; |
| 11634 | |
| 11635 | for(; i < n; i++) { |
| 11636 | t = btf_type_by_id(btf, i); |
| 11637 | if (BTF_INFO_KIND(t->info) != BTF_KIND_DATASEC) |
| 11638 | continue; |
| 11639 | |
| 11640 | tname = btf_name_by_offset(btf, t->name_off); |
| 11641 | if (!strcmp(tname, ".data..percpu")) |
| 11642 | return i; |
| 11643 | } |
| 11644 | |
| 11645 | return -ENOENT; |
| 11646 | } |
| 11647 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11648 | /* replace pseudo btf_id with kernel symbol address */ |
| 11649 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, |
| 11650 | struct bpf_insn *insn, |
| 11651 | struct bpf_insn_aux_data *aux) |
| 11652 | { |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 11653 | const struct btf_var_secinfo *vsi; |
| 11654 | const struct btf_type *datasec; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11655 | struct btf_mod_pair *btf_mod; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11656 | const struct btf_type *t; |
| 11657 | const char *sym_name; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 11658 | bool percpu = false; |
Kaixu Xia | f16e631 | 2020-11-11 13:03:46 +0800 | [diff] [blame] | 11659 | u32 type, id = insn->imm; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11660 | struct btf *btf; |
Kaixu Xia | f16e631 | 2020-11-11 13:03:46 +0800 | [diff] [blame] | 11661 | s32 datasec_id; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11662 | u64 addr; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11663 | int i, btf_fd, err; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11664 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11665 | btf_fd = insn[1].imm; |
| 11666 | if (btf_fd) { |
| 11667 | btf = btf_get_by_fd(btf_fd); |
| 11668 | if (IS_ERR(btf)) { |
| 11669 | verbose(env, "invalid module BTF object FD specified.\n"); |
| 11670 | return -EINVAL; |
| 11671 | } |
| 11672 | } else { |
| 11673 | if (!btf_vmlinux) { |
| 11674 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); |
| 11675 | return -EINVAL; |
| 11676 | } |
| 11677 | btf = btf_vmlinux; |
| 11678 | btf_get(btf); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11679 | } |
| 11680 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11681 | t = btf_type_by_id(btf, id); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11682 | if (!t) { |
| 11683 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11684 | err = -ENOENT; |
| 11685 | goto err_put; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11686 | } |
| 11687 | |
| 11688 | if (!btf_type_is_var(t)) { |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11689 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", id); |
| 11690 | err = -EINVAL; |
| 11691 | goto err_put; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11692 | } |
| 11693 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11694 | sym_name = btf_name_by_offset(btf, t->name_off); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11695 | addr = kallsyms_lookup_name(sym_name); |
| 11696 | if (!addr) { |
| 11697 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", |
| 11698 | sym_name); |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11699 | err = -ENOENT; |
| 11700 | goto err_put; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11701 | } |
| 11702 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11703 | datasec_id = find_btf_percpu_datasec(btf); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 11704 | if (datasec_id > 0) { |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11705 | datasec = btf_type_by_id(btf, datasec_id); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 11706 | for_each_vsi(i, datasec, vsi) { |
| 11707 | if (vsi->type == id) { |
| 11708 | percpu = true; |
| 11709 | break; |
| 11710 | } |
| 11711 | } |
| 11712 | } |
| 11713 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11714 | insn[0].imm = (u32)addr; |
| 11715 | insn[1].imm = addr >> 32; |
| 11716 | |
| 11717 | type = t->type; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11718 | t = btf_type_skip_modifiers(btf, type, NULL); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 11719 | if (percpu) { |
| 11720 | aux->btf_var.reg_type = PTR_TO_PERCPU_BTF_ID; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11721 | aux->btf_var.btf = btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 11722 | aux->btf_var.btf_id = type; |
| 11723 | } else if (!btf_type_is_struct(t)) { |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11724 | const struct btf_type *ret; |
| 11725 | const char *tname; |
| 11726 | u32 tsize; |
| 11727 | |
| 11728 | /* resolve the type size of ksym. */ |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11729 | ret = btf_resolve_size(btf, t, &tsize); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11730 | if (IS_ERR(ret)) { |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11731 | tname = btf_name_by_offset(btf, t->name_off); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11732 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
| 11733 | tname, PTR_ERR(ret)); |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11734 | err = -EINVAL; |
| 11735 | goto err_put; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11736 | } |
Hao Luo | 34d3a78 | 2021-12-16 16:31:50 -0800 | [diff] [blame] | 11737 | aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11738 | aux->btf_var.mem_size = tsize; |
| 11739 | } else { |
| 11740 | aux->btf_var.reg_type = PTR_TO_BTF_ID; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11741 | aux->btf_var.btf = btf; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11742 | aux->btf_var.btf_id = type; |
| 11743 | } |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11744 | |
| 11745 | /* check whether we recorded this BTF (and maybe module) already */ |
| 11746 | for (i = 0; i < env->used_btf_cnt; i++) { |
| 11747 | if (env->used_btfs[i].btf == btf) { |
| 11748 | btf_put(btf); |
| 11749 | return 0; |
| 11750 | } |
| 11751 | } |
| 11752 | |
| 11753 | if (env->used_btf_cnt >= MAX_USED_BTFS) { |
| 11754 | err = -E2BIG; |
| 11755 | goto err_put; |
| 11756 | } |
| 11757 | |
| 11758 | btf_mod = &env->used_btfs[env->used_btf_cnt]; |
| 11759 | btf_mod->btf = btf; |
| 11760 | btf_mod->module = NULL; |
| 11761 | |
| 11762 | /* if we reference variables from kernel module, bump its refcount */ |
| 11763 | if (btf_is_module(btf)) { |
| 11764 | btf_mod->module = btf_try_get_module(btf); |
| 11765 | if (!btf_mod->module) { |
| 11766 | err = -ENXIO; |
| 11767 | goto err_put; |
| 11768 | } |
| 11769 | } |
| 11770 | |
| 11771 | env->used_btf_cnt++; |
| 11772 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11773 | return 0; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11774 | err_put: |
| 11775 | btf_put(btf); |
| 11776 | return err; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11777 | } |
| 11778 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 11779 | static int check_map_prealloc(struct bpf_map *map) |
| 11780 | { |
| 11781 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 11782 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 11783 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 11784 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 11785 | } |
| 11786 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 11787 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
| 11788 | { |
| 11789 | switch (type) { |
| 11790 | case BPF_PROG_TYPE_KPROBE: |
| 11791 | case BPF_PROG_TYPE_TRACEPOINT: |
| 11792 | case BPF_PROG_TYPE_PERF_EVENT: |
| 11793 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 11794 | return true; |
| 11795 | default: |
| 11796 | return false; |
| 11797 | } |
| 11798 | } |
| 11799 | |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 11800 | static bool is_preallocated_map(struct bpf_map *map) |
| 11801 | { |
| 11802 | if (!check_map_prealloc(map)) |
| 11803 | return false; |
| 11804 | if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) |
| 11805 | return false; |
| 11806 | return true; |
| 11807 | } |
| 11808 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11809 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 11810 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 11811 | struct bpf_prog *prog) |
| 11812 | |
| 11813 | { |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 11814 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 11815 | /* |
| 11816 | * Validate that trace type programs use preallocated hash maps. |
| 11817 | * |
| 11818 | * For programs attached to PERF events this is mandatory as the |
| 11819 | * perf NMI can hit any arbitrary code sequence. |
| 11820 | * |
| 11821 | * All other trace types using preallocated hash maps are unsafe as |
| 11822 | * well because tracepoint or kprobes can be inside locked regions |
| 11823 | * of the memory allocator or at a place where a recursion into the |
| 11824 | * memory allocator would see inconsistent state. |
| 11825 | * |
Thomas Gleixner | 2ed905c | 2020-02-24 15:01:33 +0100 | [diff] [blame] | 11826 | * On RT enabled kernels run-time allocation of all trace type |
| 11827 | * programs is strictly prohibited due to lock type constraints. On |
| 11828 | * !RT kernels it is allowed for backwards compatibility reasons for |
| 11829 | * now, but warnings are emitted so developers are made aware of |
| 11830 | * the unsafety and can fix their programs before this is enforced. |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 11831 | */ |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 11832 | if (is_tracing_prog_type(prog_type) && !is_preallocated_map(map)) { |
| 11833 | if (prog_type == BPF_PROG_TYPE_PERF_EVENT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11834 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 11835 | return -EINVAL; |
| 11836 | } |
Thomas Gleixner | 2ed905c | 2020-02-24 15:01:33 +0100 | [diff] [blame] | 11837 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
| 11838 | verbose(env, "trace type programs can only use preallocated hash map\n"); |
| 11839 | return -EINVAL; |
| 11840 | } |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 11841 | WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); |
| 11842 | verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 11843 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 11844 | |
KP Singh | 9e7a4d9 | 2020-11-06 10:37:39 +0000 | [diff] [blame] | 11845 | if (map_value_has_spin_lock(map)) { |
| 11846 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { |
| 11847 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); |
| 11848 | return -EINVAL; |
| 11849 | } |
| 11850 | |
| 11851 | if (is_tracing_prog_type(prog_type)) { |
| 11852 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); |
| 11853 | return -EINVAL; |
| 11854 | } |
| 11855 | |
| 11856 | if (prog->aux->sleepable) { |
| 11857 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); |
| 11858 | return -EINVAL; |
| 11859 | } |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 11860 | } |
| 11861 | |
Dmitrii Banshchikov | 5e0bc30 | 2021-11-13 18:22:26 +0400 | [diff] [blame] | 11862 | if (map_value_has_timer(map)) { |
| 11863 | if (is_tracing_prog_type(prog_type)) { |
| 11864 | verbose(env, "tracing progs cannot use bpf_timer yet\n"); |
| 11865 | return -EINVAL; |
| 11866 | } |
| 11867 | } |
| 11868 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 11869 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
Jakub Kicinski | 0972826 | 2018-07-17 10:53:23 -0700 | [diff] [blame] | 11870 | !bpf_offload_prog_map_match(prog, map)) { |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 11871 | verbose(env, "offload device mismatch between prog and map\n"); |
| 11872 | return -EINVAL; |
| 11873 | } |
| 11874 | |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 11875 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
| 11876 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); |
| 11877 | return -EINVAL; |
| 11878 | } |
| 11879 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11880 | if (prog->aux->sleepable) |
| 11881 | switch (map->map_type) { |
| 11882 | case BPF_MAP_TYPE_HASH: |
| 11883 | case BPF_MAP_TYPE_LRU_HASH: |
| 11884 | case BPF_MAP_TYPE_ARRAY: |
Alexei Starovoitov | 638e4b8 | 2021-02-09 19:36:33 -0800 | [diff] [blame] | 11885 | case BPF_MAP_TYPE_PERCPU_HASH: |
| 11886 | case BPF_MAP_TYPE_PERCPU_ARRAY: |
| 11887 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: |
| 11888 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
| 11889 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11890 | if (!is_preallocated_map(map)) { |
| 11891 | verbose(env, |
Alexei Starovoitov | 638e4b8 | 2021-02-09 19:36:33 -0800 | [diff] [blame] | 11892 | "Sleepable programs can only use preallocated maps\n"); |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11893 | return -EINVAL; |
| 11894 | } |
| 11895 | break; |
KP Singh | ba90c2c | 2021-02-04 19:36:21 +0000 | [diff] [blame] | 11896 | case BPF_MAP_TYPE_RINGBUF: |
KP Singh | 0fe4b38 | 2021-12-24 15:29:15 +0000 | [diff] [blame] | 11897 | case BPF_MAP_TYPE_INODE_STORAGE: |
| 11898 | case BPF_MAP_TYPE_SK_STORAGE: |
| 11899 | case BPF_MAP_TYPE_TASK_STORAGE: |
KP Singh | ba90c2c | 2021-02-04 19:36:21 +0000 | [diff] [blame] | 11900 | break; |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11901 | default: |
| 11902 | verbose(env, |
KP Singh | ba90c2c | 2021-02-04 19:36:21 +0000 | [diff] [blame] | 11903 | "Sleepable programs can only use array, hash, and ringbuf maps\n"); |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11904 | return -EINVAL; |
| 11905 | } |
| 11906 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 11907 | return 0; |
| 11908 | } |
| 11909 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 11910 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 11911 | { |
| 11912 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 11913 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 11914 | } |
| 11915 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11916 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
| 11917 | * |
| 11918 | * 1. if it accesses map FD, replace it with actual map pointer. |
| 11919 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. |
| 11920 | * |
| 11921 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11922 | */ |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11923 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11924 | { |
| 11925 | struct bpf_insn *insn = env->prog->insnsi; |
| 11926 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 11927 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11928 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 11929 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 11930 | if (err) |
| 11931 | return err; |
| 11932 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11933 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11934 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 11935 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11936 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11937 | return -EINVAL; |
| 11938 | } |
| 11939 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11940 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 11941 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11942 | struct bpf_map *map; |
| 11943 | struct fd f; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 11944 | u64 addr; |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 11945 | u32 fd; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11946 | |
| 11947 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 11948 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 11949 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11950 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11951 | return -EINVAL; |
| 11952 | } |
| 11953 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 11954 | if (insn[0].src_reg == 0) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11955 | /* valid generic load 64-bit imm */ |
| 11956 | goto next_insn; |
| 11957 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11958 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
| 11959 | aux = &env->insn_aux_data[i]; |
| 11960 | err = check_pseudo_btf_id(env, insn, aux); |
| 11961 | if (err) |
| 11962 | return err; |
| 11963 | goto next_insn; |
| 11964 | } |
| 11965 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 11966 | if (insn[0].src_reg == BPF_PSEUDO_FUNC) { |
| 11967 | aux = &env->insn_aux_data[i]; |
| 11968 | aux->ptr_type = PTR_TO_FUNC; |
| 11969 | goto next_insn; |
| 11970 | } |
| 11971 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 11972 | /* In final convert_pseudo_ld_imm64() step, this is |
| 11973 | * converted into regular 64-bit imm load insn. |
| 11974 | */ |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 11975 | switch (insn[0].src_reg) { |
| 11976 | case BPF_PSEUDO_MAP_VALUE: |
| 11977 | case BPF_PSEUDO_MAP_IDX_VALUE: |
| 11978 | break; |
| 11979 | case BPF_PSEUDO_MAP_FD: |
| 11980 | case BPF_PSEUDO_MAP_IDX: |
| 11981 | if (insn[1].imm == 0) |
| 11982 | break; |
| 11983 | fallthrough; |
| 11984 | default: |
| 11985 | verbose(env, "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11986 | return -EINVAL; |
| 11987 | } |
| 11988 | |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 11989 | switch (insn[0].src_reg) { |
| 11990 | case BPF_PSEUDO_MAP_IDX_VALUE: |
| 11991 | case BPF_PSEUDO_MAP_IDX: |
| 11992 | if (bpfptr_is_null(env->fd_array)) { |
| 11993 | verbose(env, "fd_idx without fd_array is invalid\n"); |
| 11994 | return -EPROTO; |
| 11995 | } |
| 11996 | if (copy_from_bpfptr_offset(&fd, env->fd_array, |
| 11997 | insn[0].imm * sizeof(fd), |
| 11998 | sizeof(fd))) |
| 11999 | return -EFAULT; |
| 12000 | break; |
| 12001 | default: |
| 12002 | fd = insn[0].imm; |
| 12003 | break; |
| 12004 | } |
| 12005 | |
| 12006 | f = fdget(fd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 12007 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12008 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 12009 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 12010 | insn[0].imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12011 | return PTR_ERR(map); |
| 12012 | } |
| 12013 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 12014 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 12015 | if (err) { |
| 12016 | fdput(f); |
| 12017 | return err; |
| 12018 | } |
| 12019 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 12020 | aux = &env->insn_aux_data[i]; |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 12021 | if (insn[0].src_reg == BPF_PSEUDO_MAP_FD || |
| 12022 | insn[0].src_reg == BPF_PSEUDO_MAP_IDX) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 12023 | addr = (unsigned long)map; |
| 12024 | } else { |
| 12025 | u32 off = insn[1].imm; |
| 12026 | |
| 12027 | if (off >= BPF_MAX_VAR_OFF) { |
| 12028 | verbose(env, "direct value offset of %u is not allowed\n", off); |
| 12029 | fdput(f); |
| 12030 | return -EINVAL; |
| 12031 | } |
| 12032 | |
| 12033 | if (!map->ops->map_direct_value_addr) { |
| 12034 | verbose(env, "no direct value access support for this map type\n"); |
| 12035 | fdput(f); |
| 12036 | return -EINVAL; |
| 12037 | } |
| 12038 | |
| 12039 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 12040 | if (err) { |
| 12041 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", |
| 12042 | map->value_size, off); |
| 12043 | fdput(f); |
| 12044 | return err; |
| 12045 | } |
| 12046 | |
| 12047 | aux->map_off = off; |
| 12048 | addr += off; |
| 12049 | } |
| 12050 | |
| 12051 | insn[0].imm = (u32)addr; |
| 12052 | insn[1].imm = addr >> 32; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12053 | |
| 12054 | /* check whether we recorded this map already */ |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 12055 | for (j = 0; j < env->used_map_cnt; j++) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12056 | if (env->used_maps[j] == map) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 12057 | aux->map_index = j; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12058 | fdput(f); |
| 12059 | goto next_insn; |
| 12060 | } |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 12061 | } |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12062 | |
| 12063 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 12064 | fdput(f); |
| 12065 | return -E2BIG; |
| 12066 | } |
| 12067 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12068 | /* hold the map. If the program is rejected by verifier, |
| 12069 | * the map will be released by release_maps() or it |
| 12070 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 12071 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12072 | */ |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 12073 | bpf_map_inc(map); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 12074 | |
| 12075 | aux->map_index = env->used_map_cnt; |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 12076 | env->used_maps[env->used_map_cnt++] = map; |
| 12077 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 12078 | if (bpf_map_is_cgroup_storage(map) && |
Daniel Borkmann | e473042 | 2019-12-17 13:28:16 +0100 | [diff] [blame] | 12079 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 12080 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 12081 | fdput(f); |
| 12082 | return -EBUSY; |
| 12083 | } |
| 12084 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12085 | fdput(f); |
| 12086 | next_insn: |
| 12087 | insn++; |
| 12088 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 12089 | continue; |
| 12090 | } |
| 12091 | |
| 12092 | /* Basic sanity check before we invest more work here. */ |
| 12093 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 12094 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 12095 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12096 | } |
| 12097 | } |
| 12098 | |
| 12099 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 12100 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 12101 | * These pointers will be used later by verifier to validate map access. |
| 12102 | */ |
| 12103 | return 0; |
| 12104 | } |
| 12105 | |
| 12106 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 12107 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12108 | { |
Daniel Borkmann | a2ea074 | 2019-12-16 17:49:00 +0100 | [diff] [blame] | 12109 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
| 12110 | env->used_map_cnt); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12111 | } |
| 12112 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 12113 | /* drop refcnt of maps used by the rejected program */ |
| 12114 | static void release_btfs(struct bpf_verifier_env *env) |
| 12115 | { |
| 12116 | __bpf_free_used_btfs(env->prog->aux, env->used_btfs, |
| 12117 | env->used_btf_cnt); |
| 12118 | } |
| 12119 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12120 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 12121 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12122 | { |
| 12123 | struct bpf_insn *insn = env->prog->insnsi; |
| 12124 | int insn_cnt = env->prog->len; |
| 12125 | int i; |
| 12126 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12127 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 12128 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) |
| 12129 | continue; |
| 12130 | if (insn->src_reg == BPF_PSEUDO_FUNC) |
| 12131 | continue; |
| 12132 | insn->src_reg = 0; |
| 12133 | } |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12134 | } |
| 12135 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12136 | /* single env->prog->insni[off] instruction was replaced with the range |
| 12137 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 12138 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 12139 | */ |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12140 | static void adjust_insn_aux_data(struct bpf_verifier_env *env, |
| 12141 | struct bpf_insn_aux_data *new_data, |
| 12142 | struct bpf_prog *new_prog, u32 off, u32 cnt) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12143 | { |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12144 | struct bpf_insn_aux_data *old_data = env->insn_aux_data; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 12145 | struct bpf_insn *insn = new_prog->insnsi; |
Daniel Borkmann | d203b0f | 2021-05-28 13:03:30 +0000 | [diff] [blame] | 12146 | u32 old_seen = old_data[off].seen; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 12147 | u32 prog_len; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 12148 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12149 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 12150 | /* aux info at OFF always needs adjustment, no matter fast path |
| 12151 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the |
| 12152 | * original insn at old prog. |
| 12153 | */ |
| 12154 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); |
| 12155 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12156 | if (cnt == 1) |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12157 | return; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 12158 | prog_len = new_prog->len; |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12159 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12160 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 12161 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 12162 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 12163 | for (i = off; i < off + cnt - 1; i++) { |
Daniel Borkmann | d203b0f | 2021-05-28 13:03:30 +0000 | [diff] [blame] | 12164 | /* Expand insni[off]'s seen count to the patched range. */ |
| 12165 | new_data[i].seen = old_seen; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 12166 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
| 12167 | } |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12168 | env->insn_aux_data = new_data; |
| 12169 | vfree(old_data); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12170 | } |
| 12171 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 12172 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 12173 | { |
| 12174 | int i; |
| 12175 | |
| 12176 | if (len == 1) |
| 12177 | return; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 12178 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 12179 | for (i = 0; i <= env->subprog_cnt; i++) { |
Edward Cree | afd5942 | 2018-11-16 12:00:07 +0000 | [diff] [blame] | 12180 | if (env->subprog_info[i].start <= off) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 12181 | continue; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 12182 | env->subprog_info[i].start += len - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 12183 | } |
| 12184 | } |
| 12185 | |
John Fastabend | 7506d21 | 2021-06-16 15:55:00 -0700 | [diff] [blame] | 12186 | static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len) |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12187 | { |
| 12188 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; |
| 12189 | int i, sz = prog->aux->size_poke_tab; |
| 12190 | struct bpf_jit_poke_descriptor *desc; |
| 12191 | |
| 12192 | for (i = 0; i < sz; i++) { |
| 12193 | desc = &tab[i]; |
John Fastabend | 7506d21 | 2021-06-16 15:55:00 -0700 | [diff] [blame] | 12194 | if (desc->insn_idx <= off) |
| 12195 | continue; |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12196 | desc->insn_idx += len - 1; |
| 12197 | } |
| 12198 | } |
| 12199 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12200 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 12201 | const struct bpf_insn *patch, u32 len) |
| 12202 | { |
| 12203 | struct bpf_prog *new_prog; |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12204 | struct bpf_insn_aux_data *new_data = NULL; |
| 12205 | |
| 12206 | if (len > 1) { |
| 12207 | new_data = vzalloc(array_size(env->prog->len + len - 1, |
| 12208 | sizeof(struct bpf_insn_aux_data))); |
| 12209 | if (!new_data) |
| 12210 | return NULL; |
| 12211 | } |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12212 | |
| 12213 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 12214 | if (IS_ERR(new_prog)) { |
| 12215 | if (PTR_ERR(new_prog) == -ERANGE) |
| 12216 | verbose(env, |
| 12217 | "insn %d cannot be patched due to 16-bit range\n", |
| 12218 | env->insn_aux_data[off].orig_idx); |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12219 | vfree(new_data); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12220 | return NULL; |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 12221 | } |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12222 | adjust_insn_aux_data(env, new_data, new_prog, off, len); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 12223 | adjust_subprog_starts(env, off, len); |
John Fastabend | 7506d21 | 2021-06-16 15:55:00 -0700 | [diff] [blame] | 12224 | adjust_poke_descs(new_prog, off, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12225 | return new_prog; |
| 12226 | } |
| 12227 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 12228 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
| 12229 | u32 off, u32 cnt) |
| 12230 | { |
| 12231 | int i, j; |
| 12232 | |
| 12233 | /* find first prog starting at or after off (first to remove) */ |
| 12234 | for (i = 0; i < env->subprog_cnt; i++) |
| 12235 | if (env->subprog_info[i].start >= off) |
| 12236 | break; |
| 12237 | /* find first prog starting at or after off + cnt (first to stay) */ |
| 12238 | for (j = i; j < env->subprog_cnt; j++) |
| 12239 | if (env->subprog_info[j].start >= off + cnt) |
| 12240 | break; |
| 12241 | /* if j doesn't start exactly at off + cnt, we are just removing |
| 12242 | * the front of previous prog |
| 12243 | */ |
| 12244 | if (env->subprog_info[j].start != off + cnt) |
| 12245 | j--; |
| 12246 | |
| 12247 | if (j > i) { |
| 12248 | struct bpf_prog_aux *aux = env->prog->aux; |
| 12249 | int move; |
| 12250 | |
| 12251 | /* move fake 'exit' subprog as well */ |
| 12252 | move = env->subprog_cnt + 1 - j; |
| 12253 | |
| 12254 | memmove(env->subprog_info + i, |
| 12255 | env->subprog_info + j, |
| 12256 | sizeof(*env->subprog_info) * move); |
| 12257 | env->subprog_cnt -= j - i; |
| 12258 | |
| 12259 | /* remove func_info */ |
| 12260 | if (aux->func_info) { |
| 12261 | move = aux->func_info_cnt - j; |
| 12262 | |
| 12263 | memmove(aux->func_info + i, |
| 12264 | aux->func_info + j, |
| 12265 | sizeof(*aux->func_info) * move); |
| 12266 | aux->func_info_cnt -= j - i; |
| 12267 | /* func_info->insn_off is set after all code rewrites, |
| 12268 | * in adjust_btf_func() - no need to adjust |
| 12269 | */ |
| 12270 | } |
| 12271 | } else { |
| 12272 | /* convert i from "first prog to remove" to "first to adjust" */ |
| 12273 | if (env->subprog_info[i].start == off) |
| 12274 | i++; |
| 12275 | } |
| 12276 | |
| 12277 | /* update fake 'exit' subprog as well */ |
| 12278 | for (; i <= env->subprog_cnt; i++) |
| 12279 | env->subprog_info[i].start -= cnt; |
| 12280 | |
| 12281 | return 0; |
| 12282 | } |
| 12283 | |
| 12284 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, |
| 12285 | u32 cnt) |
| 12286 | { |
| 12287 | struct bpf_prog *prog = env->prog; |
| 12288 | u32 i, l_off, l_cnt, nr_linfo; |
| 12289 | struct bpf_line_info *linfo; |
| 12290 | |
| 12291 | nr_linfo = prog->aux->nr_linfo; |
| 12292 | if (!nr_linfo) |
| 12293 | return 0; |
| 12294 | |
| 12295 | linfo = prog->aux->linfo; |
| 12296 | |
| 12297 | /* find first line info to remove, count lines to be removed */ |
| 12298 | for (i = 0; i < nr_linfo; i++) |
| 12299 | if (linfo[i].insn_off >= off) |
| 12300 | break; |
| 12301 | |
| 12302 | l_off = i; |
| 12303 | l_cnt = 0; |
| 12304 | for (; i < nr_linfo; i++) |
| 12305 | if (linfo[i].insn_off < off + cnt) |
| 12306 | l_cnt++; |
| 12307 | else |
| 12308 | break; |
| 12309 | |
| 12310 | /* First live insn doesn't match first live linfo, it needs to "inherit" |
| 12311 | * last removed linfo. prog is already modified, so prog->len == off |
| 12312 | * means no live instructions after (tail of the program was removed). |
| 12313 | */ |
| 12314 | if (prog->len != off && l_cnt && |
| 12315 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { |
| 12316 | l_cnt--; |
| 12317 | linfo[--i].insn_off = off + cnt; |
| 12318 | } |
| 12319 | |
| 12320 | /* remove the line info which refer to the removed instructions */ |
| 12321 | if (l_cnt) { |
| 12322 | memmove(linfo + l_off, linfo + i, |
| 12323 | sizeof(*linfo) * (nr_linfo - i)); |
| 12324 | |
| 12325 | prog->aux->nr_linfo -= l_cnt; |
| 12326 | nr_linfo = prog->aux->nr_linfo; |
| 12327 | } |
| 12328 | |
| 12329 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ |
| 12330 | for (i = l_off; i < nr_linfo; i++) |
| 12331 | linfo[i].insn_off -= cnt; |
| 12332 | |
| 12333 | /* fix up all subprogs (incl. 'exit') which start >= off */ |
| 12334 | for (i = 0; i <= env->subprog_cnt; i++) |
| 12335 | if (env->subprog_info[i].linfo_idx > l_off) { |
| 12336 | /* program may have started in the removed region but |
| 12337 | * may not be fully removed |
| 12338 | */ |
| 12339 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) |
| 12340 | env->subprog_info[i].linfo_idx -= l_cnt; |
| 12341 | else |
| 12342 | env->subprog_info[i].linfo_idx = l_off; |
| 12343 | } |
| 12344 | |
| 12345 | return 0; |
| 12346 | } |
| 12347 | |
| 12348 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) |
| 12349 | { |
| 12350 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 12351 | unsigned int orig_prog_len = env->prog->len; |
| 12352 | int err; |
| 12353 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 12354 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 12355 | bpf_prog_offload_remove_insns(env, off, cnt); |
| 12356 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 12357 | err = bpf_remove_insns(env->prog, off, cnt); |
| 12358 | if (err) |
| 12359 | return err; |
| 12360 | |
| 12361 | err = adjust_subprog_starts_after_remove(env, off, cnt); |
| 12362 | if (err) |
| 12363 | return err; |
| 12364 | |
| 12365 | err = bpf_adj_linfo_after_remove(env, off, cnt); |
| 12366 | if (err) |
| 12367 | return err; |
| 12368 | |
| 12369 | memmove(aux_data + off, aux_data + off + cnt, |
| 12370 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); |
| 12371 | |
| 12372 | return 0; |
| 12373 | } |
| 12374 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 12375 | /* The verifier does more data flow analysis than llvm and will not |
| 12376 | * explore branches that are dead at run time. Malicious programs can |
| 12377 | * have dead code too. Therefore replace all dead at-run-time code |
| 12378 | * with 'ja -1'. |
| 12379 | * |
| 12380 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 12381 | * program and through another bug we would manage to jump there, then |
| 12382 | * we'd execute beyond program memory otherwise. Returning exception |
| 12383 | * code also wouldn't work since we can have subprogs where the dead |
| 12384 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 12385 | */ |
| 12386 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 12387 | { |
| 12388 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 12389 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 12390 | struct bpf_insn *insn = env->prog->insnsi; |
| 12391 | const int insn_cnt = env->prog->len; |
| 12392 | int i; |
| 12393 | |
| 12394 | for (i = 0; i < insn_cnt; i++) { |
| 12395 | if (aux_data[i].seen) |
| 12396 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 12397 | memcpy(insn + i, &trap, sizeof(trap)); |
Ilya Leoshkevich | 45c709f | 2021-08-12 17:18:10 +0200 | [diff] [blame] | 12398 | aux_data[i].zext_dst = false; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 12399 | } |
| 12400 | } |
| 12401 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 12402 | static bool insn_is_cond_jump(u8 code) |
| 12403 | { |
| 12404 | u8 op; |
| 12405 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 12406 | if (BPF_CLASS(code) == BPF_JMP32) |
| 12407 | return true; |
| 12408 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 12409 | if (BPF_CLASS(code) != BPF_JMP) |
| 12410 | return false; |
| 12411 | |
| 12412 | op = BPF_OP(code); |
| 12413 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; |
| 12414 | } |
| 12415 | |
| 12416 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) |
| 12417 | { |
| 12418 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 12419 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 12420 | struct bpf_insn *insn = env->prog->insnsi; |
| 12421 | const int insn_cnt = env->prog->len; |
| 12422 | int i; |
| 12423 | |
| 12424 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 12425 | if (!insn_is_cond_jump(insn->code)) |
| 12426 | continue; |
| 12427 | |
| 12428 | if (!aux_data[i + 1].seen) |
| 12429 | ja.off = insn->off; |
| 12430 | else if (!aux_data[i + 1 + insn->off].seen) |
| 12431 | ja.off = 0; |
| 12432 | else |
| 12433 | continue; |
| 12434 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 12435 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 12436 | bpf_prog_offload_replace_insn(env, i, &ja); |
| 12437 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 12438 | memcpy(insn, &ja, sizeof(ja)); |
| 12439 | } |
| 12440 | } |
| 12441 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 12442 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
| 12443 | { |
| 12444 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 12445 | int insn_cnt = env->prog->len; |
| 12446 | int i, err; |
| 12447 | |
| 12448 | for (i = 0; i < insn_cnt; i++) { |
| 12449 | int j; |
| 12450 | |
| 12451 | j = 0; |
| 12452 | while (i + j < insn_cnt && !aux_data[i + j].seen) |
| 12453 | j++; |
| 12454 | if (!j) |
| 12455 | continue; |
| 12456 | |
| 12457 | err = verifier_remove_insns(env, i, j); |
| 12458 | if (err) |
| 12459 | return err; |
| 12460 | insn_cnt = env->prog->len; |
| 12461 | } |
| 12462 | |
| 12463 | return 0; |
| 12464 | } |
| 12465 | |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 12466 | static int opt_remove_nops(struct bpf_verifier_env *env) |
| 12467 | { |
| 12468 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 12469 | struct bpf_insn *insn = env->prog->insnsi; |
| 12470 | int insn_cnt = env->prog->len; |
| 12471 | int i, err; |
| 12472 | |
| 12473 | for (i = 0; i < insn_cnt; i++) { |
| 12474 | if (memcmp(&insn[i], &ja, sizeof(ja))) |
| 12475 | continue; |
| 12476 | |
| 12477 | err = verifier_remove_insns(env, i, 1); |
| 12478 | if (err) |
| 12479 | return err; |
| 12480 | insn_cnt--; |
| 12481 | i--; |
| 12482 | } |
| 12483 | |
| 12484 | return 0; |
| 12485 | } |
| 12486 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12487 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
| 12488 | const union bpf_attr *attr) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12489 | { |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12490 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12491 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12492 | int i, patch_len, delta = 0, len = env->prog->len; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12493 | struct bpf_insn *insns = env->prog->insnsi; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12494 | struct bpf_prog *new_prog; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12495 | bool rnd_hi32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12496 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12497 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12498 | zext_patch[1] = BPF_ZEXT_REG(0); |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12499 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
| 12500 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); |
| 12501 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12502 | for (i = 0; i < len; i++) { |
| 12503 | int adj_idx = i + delta; |
| 12504 | struct bpf_insn insn; |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12505 | int load_reg; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12506 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12507 | insn = insns[adj_idx]; |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12508 | load_reg = insn_def_regno(&insn); |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12509 | if (!aux[adj_idx].zext_dst) { |
| 12510 | u8 code, class; |
| 12511 | u32 imm_rnd; |
| 12512 | |
| 12513 | if (!rnd_hi32) |
| 12514 | continue; |
| 12515 | |
| 12516 | code = insn.code; |
| 12517 | class = BPF_CLASS(code); |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12518 | if (load_reg == -1) |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12519 | continue; |
| 12520 | |
| 12521 | /* NOTE: arg "reg" (the fourth one) is only used for |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12522 | * BPF_STX + SRC_OP, so it is safe to pass NULL |
| 12523 | * here. |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12524 | */ |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12525 | if (is_reg64(env, &insn, load_reg, NULL, DST_OP)) { |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12526 | if (class == BPF_LD && |
| 12527 | BPF_MODE(code) == BPF_IMM) |
| 12528 | i++; |
| 12529 | continue; |
| 12530 | } |
| 12531 | |
| 12532 | /* ctx load could be transformed into wider load. */ |
| 12533 | if (class == BPF_LDX && |
| 12534 | aux[adj_idx].ptr_type == PTR_TO_CTX) |
| 12535 | continue; |
| 12536 | |
| 12537 | imm_rnd = get_random_int(); |
| 12538 | rnd_hi32_patch[0] = insn; |
| 12539 | rnd_hi32_patch[1].imm = imm_rnd; |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12540 | rnd_hi32_patch[3].dst_reg = load_reg; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12541 | patch = rnd_hi32_patch; |
| 12542 | patch_len = 4; |
| 12543 | goto apply_patch_buffer; |
| 12544 | } |
| 12545 | |
Brendan Jackman | 3949186 | 2021-03-04 18:56:46 -0800 | [diff] [blame] | 12546 | /* Add in an zero-extend instruction if a) the JIT has requested |
| 12547 | * it or b) it's a CMPXCHG. |
| 12548 | * |
| 12549 | * The latter is because: BPF_CMPXCHG always loads a value into |
| 12550 | * R0, therefore always zero-extends. However some archs' |
| 12551 | * equivalent instruction only does this load when the |
| 12552 | * comparison is successful. This detail of CMPXCHG is |
| 12553 | * orthogonal to the general zero-extension behaviour of the |
| 12554 | * CPU, so it's treated independently of bpf_jit_needs_zext. |
| 12555 | */ |
| 12556 | if (!bpf_jit_needs_zext() && !is_cmpxchg_insn(&insn)) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12557 | continue; |
| 12558 | |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12559 | if (WARN_ON(load_reg == -1)) { |
| 12560 | verbose(env, "verifier bug. zext_dst is set, but no reg is defined\n"); |
| 12561 | return -EFAULT; |
Ilya Leoshkevich | b2e37a7 | 2021-02-10 21:45:02 +0100 | [diff] [blame] | 12562 | } |
| 12563 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12564 | zext_patch[0] = insn; |
Ilya Leoshkevich | b2e37a7 | 2021-02-10 21:45:02 +0100 | [diff] [blame] | 12565 | zext_patch[1].dst_reg = load_reg; |
| 12566 | zext_patch[1].src_reg = load_reg; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12567 | patch = zext_patch; |
| 12568 | patch_len = 2; |
| 12569 | apply_patch_buffer: |
| 12570 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12571 | if (!new_prog) |
| 12572 | return -ENOMEM; |
| 12573 | env->prog = new_prog; |
| 12574 | insns = new_prog->insnsi; |
| 12575 | aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12576 | delta += patch_len - 1; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12577 | } |
| 12578 | |
| 12579 | return 0; |
| 12580 | } |
| 12581 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12582 | /* convert load instructions that access fields of a context type into a |
| 12583 | * sequence of instructions that access fields of the underlying structure: |
| 12584 | * struct __sk_buff -> struct sk_buff |
| 12585 | * struct bpf_sock_ops -> struct sock |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12586 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 12587 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12588 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 12589 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12590 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12591 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12592 | struct bpf_insn insn_buf[16], *insn; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 12593 | u32 target_size, size_default, off; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12594 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 12595 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12596 | bool is_narrower_load; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12597 | |
Daniel Borkmann | b09928b | 2018-10-24 22:05:49 +0200 | [diff] [blame] | 12598 | if (ops->gen_prologue || env->seen_direct_write) { |
| 12599 | if (!ops->gen_prologue) { |
| 12600 | verbose(env, "bpf verifier is misconfigured\n"); |
| 12601 | return -EINVAL; |
| 12602 | } |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12603 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 12604 | env->prog); |
| 12605 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 12606 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12607 | return -EINVAL; |
| 12608 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12609 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12610 | if (!new_prog) |
| 12611 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12612 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12613 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12614 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12615 | } |
| 12616 | } |
| 12617 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12618 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12619 | return 0; |
| 12620 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12621 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12622 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12623 | for (i = 0; i < insn_cnt; i++, insn++) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12624 | bpf_convert_ctx_access_t convert_ctx_access; |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12625 | bool ctx_access; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12626 | |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 12627 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 12628 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 12629 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12630 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) { |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 12631 | type = BPF_READ; |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12632 | ctx_access = true; |
| 12633 | } else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 12634 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 12635 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
| 12636 | insn->code == (BPF_STX | BPF_MEM | BPF_DW) || |
| 12637 | insn->code == (BPF_ST | BPF_MEM | BPF_B) || |
| 12638 | insn->code == (BPF_ST | BPF_MEM | BPF_H) || |
| 12639 | insn->code == (BPF_ST | BPF_MEM | BPF_W) || |
| 12640 | insn->code == (BPF_ST | BPF_MEM | BPF_DW)) { |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 12641 | type = BPF_WRITE; |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12642 | ctx_access = BPF_CLASS(insn->code) == BPF_STX; |
| 12643 | } else { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12644 | continue; |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12645 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12646 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 12647 | if (type == BPF_WRITE && |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12648 | env->insn_aux_data[i + delta].sanitize_stack_spill) { |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 12649 | struct bpf_insn patch[] = { |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 12650 | *insn, |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12651 | BPF_ST_NOSPEC(), |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 12652 | }; |
| 12653 | |
| 12654 | cnt = ARRAY_SIZE(patch); |
| 12655 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 12656 | if (!new_prog) |
| 12657 | return -ENOMEM; |
| 12658 | |
| 12659 | delta += cnt - 1; |
| 12660 | env->prog = new_prog; |
| 12661 | insn = new_prog->insnsi + i + delta; |
| 12662 | continue; |
| 12663 | } |
| 12664 | |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12665 | if (!ctx_access) |
| 12666 | continue; |
| 12667 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12668 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 12669 | case PTR_TO_CTX: |
| 12670 | if (!ops->convert_ctx_access) |
| 12671 | continue; |
| 12672 | convert_ctx_access = ops->convert_ctx_access; |
| 12673 | break; |
| 12674 | case PTR_TO_SOCKET: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 12675 | case PTR_TO_SOCK_COMMON: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12676 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 12677 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 12678 | case PTR_TO_TCP_SOCK: |
| 12679 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; |
| 12680 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 12681 | case PTR_TO_XDP_SOCK: |
| 12682 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; |
| 12683 | break; |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 12684 | case PTR_TO_BTF_ID: |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 12685 | if (type == BPF_READ) { |
| 12686 | insn->code = BPF_LDX | BPF_PROBE_MEM | |
| 12687 | BPF_SIZE((insn)->code); |
| 12688 | env->prog->aux->num_exentries++; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 12689 | } else if (resolve_prog_type(env->prog) != BPF_PROG_TYPE_STRUCT_OPS) { |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 12690 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
| 12691 | return -EINVAL; |
| 12692 | } |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 12693 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12694 | default: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12695 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12696 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12697 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12698 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12699 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12700 | |
| 12701 | /* If the read access is a narrower load of the field, |
| 12702 | * convert to a 4/8-byte load, to minimum program type specific |
| 12703 | * convert_ctx_access changes. If conversion is successful, |
| 12704 | * we will apply proper mask to the result. |
| 12705 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12706 | is_narrower_load = size < ctx_field_size; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 12707 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
| 12708 | off = insn->off; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12709 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12710 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12711 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12712 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 12713 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12714 | return -EINVAL; |
| 12715 | } |
| 12716 | |
| 12717 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12718 | if (ctx_field_size == 4) |
| 12719 | size_code = BPF_W; |
| 12720 | else if (ctx_field_size == 8) |
| 12721 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12722 | |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 12723 | insn->off = off & ~(size_default - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12724 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 12725 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12726 | |
| 12727 | target_size = 0; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12728 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 12729 | &target_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12730 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 12731 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 12732 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12733 | return -EINVAL; |
| 12734 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12735 | |
| 12736 | if (is_narrower_load && size < target_size) { |
Ilya Leoshkevich | d895a0f | 2019-08-16 12:53:00 +0200 | [diff] [blame] | 12737 | u8 shift = bpf_ctx_narrow_access_offset( |
| 12738 | off, size, size_default) * 8; |
Andrey Ignatov | d7af7e4 | 2021-08-20 09:39:35 -0700 | [diff] [blame] | 12739 | if (shift && cnt + 1 >= ARRAY_SIZE(insn_buf)) { |
| 12740 | verbose(env, "bpf verifier narrow ctx load misconfigured\n"); |
| 12741 | return -EINVAL; |
| 12742 | } |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 12743 | if (ctx_field_size <= 4) { |
| 12744 | if (shift) |
| 12745 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, |
| 12746 | insn->dst_reg, |
| 12747 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12748 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12749 | (1 << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 12750 | } else { |
| 12751 | if (shift) |
| 12752 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, |
| 12753 | insn->dst_reg, |
| 12754 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12755 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Krzesimir Nowak | e2f7fc0 | 2019-05-08 18:08:58 +0200 | [diff] [blame] | 12756 | (1ULL << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 12757 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12758 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12759 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12760 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12761 | if (!new_prog) |
| 12762 | return -ENOMEM; |
| 12763 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12764 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12765 | |
| 12766 | /* keep walking new program and skip insns we just inserted */ |
| 12767 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12768 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12769 | } |
| 12770 | |
| 12771 | return 0; |
| 12772 | } |
| 12773 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12774 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 12775 | { |
| 12776 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 12777 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12778 | struct bpf_map *map_ptr; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 12779 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12780 | void *old_bpf_func; |
Yonghong Song | c4c0bdc | 2020-06-23 17:10:54 -0700 | [diff] [blame] | 12781 | int err, num_exentries; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12782 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12783 | if (env->subprog_cnt <= 1) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12784 | return 0; |
| 12785 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 12786 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 12787 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn)) |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12788 | continue; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12789 | |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 12790 | /* Upon error here we cannot fall back to interpreter but |
| 12791 | * need a hard reject of the program. Thus -EFAULT is |
| 12792 | * propagated in any case. |
| 12793 | */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12794 | subprog = find_subprog(env, i + insn->imm + 1); |
| 12795 | if (subprog < 0) { |
| 12796 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 12797 | i + insn->imm + 1); |
| 12798 | return -EFAULT; |
| 12799 | } |
| 12800 | /* temporarily remember subprog id inside insn instead of |
| 12801 | * aux_data, since next loop will split up all insns into funcs |
| 12802 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12803 | insn->off = subprog; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12804 | /* remember original imm in case JIT fails and fallback |
| 12805 | * to interpreter will be needed |
| 12806 | */ |
| 12807 | env->insn_aux_data[i].call_imm = insn->imm; |
| 12808 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 12809 | insn->imm = 1; |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 12810 | if (bpf_pseudo_func(insn)) |
| 12811 | /* jit (e.g. x86_64) may emit fewer instructions |
| 12812 | * if it learns a u32 imm is the same as a u64 imm. |
| 12813 | * Force a non zero here. |
| 12814 | */ |
| 12815 | insn[1].imm = 1; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12816 | } |
| 12817 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 12818 | err = bpf_prog_alloc_jited_linfo(prog); |
| 12819 | if (err) |
| 12820 | goto out_undo_insn; |
| 12821 | |
| 12822 | err = -ENOMEM; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 12823 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12824 | if (!func) |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 12825 | goto out_undo_insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12826 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12827 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12828 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 12829 | subprog_end = env->subprog_info[i + 1].start; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12830 | |
| 12831 | len = subprog_end - subprog_start; |
Andrii Nakryiko | fb7dd8b | 2021-08-15 00:05:54 -0700 | [diff] [blame] | 12832 | /* bpf_prog_run() doesn't call subprogs directly, |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 12833 | * hence main prog stats include the runtime of subprogs. |
| 12834 | * subprogs don't have IDs and not reachable via prog_get_next_id |
Alexei Starovoitov | 700d479 | 2021-02-09 19:36:26 -0800 | [diff] [blame] | 12835 | * func[i]->stats will never be accessed and stays NULL |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 12836 | */ |
| 12837 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12838 | if (!func[i]) |
| 12839 | goto out_free; |
| 12840 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 12841 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 12842 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12843 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 12844 | if (bpf_prog_calc_tag(func[i])) |
| 12845 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12846 | func[i]->is_func = 1; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 12847 | func[i]->aux->func_idx = i; |
John Fastabend | f263a81 | 2021-07-07 15:38:47 -0700 | [diff] [blame] | 12848 | /* Below members will be freed only at prog->aux */ |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 12849 | func[i]->aux->btf = prog->aux->btf; |
| 12850 | func[i]->aux->func_info = prog->aux->func_info; |
John Fastabend | f263a81 | 2021-07-07 15:38:47 -0700 | [diff] [blame] | 12851 | func[i]->aux->poke_tab = prog->aux->poke_tab; |
| 12852 | func[i]->aux->size_poke_tab = prog->aux->size_poke_tab; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 12853 | |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12854 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
John Fastabend | f263a81 | 2021-07-07 15:38:47 -0700 | [diff] [blame] | 12855 | struct bpf_jit_poke_descriptor *poke; |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12856 | |
John Fastabend | f263a81 | 2021-07-07 15:38:47 -0700 | [diff] [blame] | 12857 | poke = &prog->aux->poke_tab[j]; |
| 12858 | if (poke->insn_idx < subprog_end && |
| 12859 | poke->insn_idx >= subprog_start) |
| 12860 | poke->aux = func[i]->aux; |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12861 | } |
| 12862 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12863 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 12864 | * Long term would need debug info to populate names |
| 12865 | */ |
| 12866 | func[i]->aux->name[0] = 'F'; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 12867 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12868 | func[i]->jit_requested = 1; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 12869 | func[i]->aux->kfunc_tab = prog->aux->kfunc_tab; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 12870 | func[i]->aux->kfunc_btf_tab = prog->aux->kfunc_btf_tab; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 12871 | func[i]->aux->linfo = prog->aux->linfo; |
| 12872 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; |
| 12873 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; |
| 12874 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; |
Yonghong Song | c4c0bdc | 2020-06-23 17:10:54 -0700 | [diff] [blame] | 12875 | num_exentries = 0; |
| 12876 | insn = func[i]->insnsi; |
| 12877 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 12878 | if (BPF_CLASS(insn->code) == BPF_LDX && |
| 12879 | BPF_MODE(insn->code) == BPF_PROBE_MEM) |
| 12880 | num_exentries++; |
| 12881 | } |
| 12882 | func[i]->aux->num_exentries = num_exentries; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 12883 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12884 | func[i] = bpf_int_jit_compile(func[i]); |
| 12885 | if (!func[i]->jited) { |
| 12886 | err = -ENOTSUPP; |
| 12887 | goto out_free; |
| 12888 | } |
| 12889 | cond_resched(); |
| 12890 | } |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12891 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12892 | /* at this point all bpf functions were successfully JITed |
| 12893 | * now populate all bpf_calls with correct addresses and |
| 12894 | * run last pass of JIT |
| 12895 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12896 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12897 | insn = func[i]->insnsi; |
| 12898 | for (j = 0; j < func[i]->len; j++, insn++) { |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12899 | if (bpf_pseudo_func(insn)) { |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 12900 | subprog = insn->off; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12901 | insn[0].imm = (u32)(long)func[subprog]->bpf_func; |
| 12902 | insn[1].imm = ((u64)(long)func[subprog]->bpf_func) >> 32; |
| 12903 | continue; |
| 12904 | } |
Yonghong Song | 23a2d70 | 2021-02-04 15:48:27 -0800 | [diff] [blame] | 12905 | if (!bpf_pseudo_call(insn)) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12906 | continue; |
| 12907 | subprog = insn->off; |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 12908 | insn->imm = BPF_CALL_IMM(func[subprog]->bpf_func); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12909 | } |
Sandipan Das | 2162fed | 2018-05-24 12:26:45 +0530 | [diff] [blame] | 12910 | |
| 12911 | /* we use the aux data to keep a list of the start addresses |
| 12912 | * of the JITed images for each function in the program |
| 12913 | * |
| 12914 | * for some architectures, such as powerpc64, the imm field |
| 12915 | * might not be large enough to hold the offset of the start |
| 12916 | * address of the callee's JITed image from __bpf_call_base |
| 12917 | * |
| 12918 | * in such cases, we can lookup the start address of a callee |
| 12919 | * by using its subprog id, available from the off field of |
| 12920 | * the call instruction, as an index for this list |
| 12921 | */ |
| 12922 | func[i]->aux->func = func; |
| 12923 | func[i]->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12924 | } |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12925 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12926 | old_bpf_func = func[i]->bpf_func; |
| 12927 | tmp = bpf_int_jit_compile(func[i]); |
| 12928 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 12929 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 12930 | err = -ENOTSUPP; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12931 | goto out_free; |
| 12932 | } |
| 12933 | cond_resched(); |
| 12934 | } |
| 12935 | |
| 12936 | /* finally lock prog and jit images for all functions and |
| 12937 | * populate kallsysm |
| 12938 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12939 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12940 | bpf_prog_lock_ro(func[i]); |
| 12941 | bpf_prog_kallsyms_add(func[i]); |
| 12942 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 12943 | |
| 12944 | /* Last step: make now unused interpreter insns from main |
| 12945 | * prog consistent for later dump requests, so they can |
| 12946 | * later look the same as if they were interpreted only. |
| 12947 | */ |
| 12948 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12949 | if (bpf_pseudo_func(insn)) { |
| 12950 | insn[0].imm = env->insn_aux_data[i].call_imm; |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 12951 | insn[1].imm = insn->off; |
| 12952 | insn->off = 0; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12953 | continue; |
| 12954 | } |
Yonghong Song | 23a2d70 | 2021-02-04 15:48:27 -0800 | [diff] [blame] | 12955 | if (!bpf_pseudo_call(insn)) |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 12956 | continue; |
| 12957 | insn->off = env->insn_aux_data[i].call_imm; |
| 12958 | subprog = find_subprog(env, i + insn->off + 1); |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 12959 | insn->imm = subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 12960 | } |
| 12961 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12962 | prog->jited = 1; |
| 12963 | prog->bpf_func = func[0]->bpf_func; |
| 12964 | prog->aux->func = func; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12965 | prog->aux->func_cnt = env->subprog_cnt; |
Martin KaFai Lau | e16301f | 2021-03-24 18:51:30 -0700 | [diff] [blame] | 12966 | bpf_prog_jit_attempt_done(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12967 | return 0; |
| 12968 | out_free: |
John Fastabend | f263a81 | 2021-07-07 15:38:47 -0700 | [diff] [blame] | 12969 | /* We failed JIT'ing, so at this point we need to unregister poke |
| 12970 | * descriptors from subprogs, so that kernel is not attempting to |
| 12971 | * patch it anymore as we're freeing the subprog JIT memory. |
| 12972 | */ |
| 12973 | for (i = 0; i < prog->aux->size_poke_tab; i++) { |
| 12974 | map_ptr = prog->aux->poke_tab[i].tail_call.map; |
| 12975 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); |
| 12976 | } |
| 12977 | /* At this point we're guaranteed that poke descriptors are not |
| 12978 | * live anymore. We can just unlink its descriptor table as it's |
| 12979 | * released with the main prog. |
| 12980 | */ |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12981 | for (i = 0; i < env->subprog_cnt; i++) { |
| 12982 | if (!func[i]) |
| 12983 | continue; |
John Fastabend | f263a81 | 2021-07-07 15:38:47 -0700 | [diff] [blame] | 12984 | func[i]->aux->poke_tab = NULL; |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12985 | bpf_jit_free(func[i]); |
| 12986 | } |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12987 | kfree(func); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 12988 | out_undo_insn: |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12989 | /* cleanup main prog to be interpreted */ |
| 12990 | prog->jit_requested = 0; |
| 12991 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Yonghong Song | 23a2d70 | 2021-02-04 15:48:27 -0800 | [diff] [blame] | 12992 | if (!bpf_pseudo_call(insn)) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12993 | continue; |
| 12994 | insn->off = 0; |
| 12995 | insn->imm = env->insn_aux_data[i].call_imm; |
| 12996 | } |
Martin KaFai Lau | e16301f | 2021-03-24 18:51:30 -0700 | [diff] [blame] | 12997 | bpf_prog_jit_attempt_done(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12998 | return err; |
| 12999 | } |
| 13000 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 13001 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 13002 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 13003 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 13004 | struct bpf_prog *prog = env->prog; |
| 13005 | struct bpf_insn *insn = prog->insnsi; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 13006 | bool has_kfunc_call = bpf_prog_has_kfunc_call(prog); |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 13007 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 13008 | #endif |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 13009 | int err = 0; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 13010 | |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 13011 | if (env->prog->jit_requested && |
| 13012 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 13013 | err = jit_subprogs(env); |
| 13014 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 13015 | return 0; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 13016 | if (err == -EFAULT) |
| 13017 | return err; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 13018 | } |
| 13019 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 13020 | if (has_kfunc_call) { |
| 13021 | verbose(env, "calling kernel functions are not allowed in non-JITed programs\n"); |
| 13022 | return -EINVAL; |
| 13023 | } |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 13024 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
| 13025 | /* When JIT fails the progs with bpf2bpf calls and tail_calls |
| 13026 | * have to be rejected, since interpreter doesn't support them yet. |
| 13027 | */ |
| 13028 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); |
| 13029 | return -EINVAL; |
| 13030 | } |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 13031 | for (i = 0; i < prog->len; i++, insn++) { |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 13032 | if (bpf_pseudo_func(insn)) { |
| 13033 | /* When JIT fails the progs with callback calls |
| 13034 | * have to be rejected, since interpreter doesn't support them yet. |
| 13035 | */ |
| 13036 | verbose(env, "callbacks are not allowed in non-JITed programs\n"); |
| 13037 | return -EINVAL; |
| 13038 | } |
| 13039 | |
Yonghong Song | 23a2d70 | 2021-02-04 15:48:27 -0800 | [diff] [blame] | 13040 | if (!bpf_pseudo_call(insn)) |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 13041 | continue; |
| 13042 | depth = get_callee_stack_depth(env, insn, i); |
| 13043 | if (depth < 0) |
| 13044 | return depth; |
| 13045 | bpf_patch_call_args(insn, depth); |
| 13046 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 13047 | err = 0; |
| 13048 | #endif |
| 13049 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 13050 | } |
| 13051 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 13052 | static int fixup_kfunc_call(struct bpf_verifier_env *env, |
| 13053 | struct bpf_insn *insn) |
| 13054 | { |
| 13055 | const struct bpf_kfunc_desc *desc; |
| 13056 | |
Kumar Kartikeya Dwivedi | a5d8272 | 2021-10-02 06:47:50 +0530 | [diff] [blame] | 13057 | if (!insn->imm) { |
| 13058 | verbose(env, "invalid kernel function call not eliminated in verifier pass\n"); |
| 13059 | return -EINVAL; |
| 13060 | } |
| 13061 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 13062 | /* insn->imm has the btf func_id. Replace it with |
| 13063 | * an address (relative to __bpf_base_call). |
| 13064 | */ |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 13065 | desc = find_kfunc_desc(env->prog, insn->imm, insn->off); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 13066 | if (!desc) { |
| 13067 | verbose(env, "verifier internal error: kernel function descriptor not found for func_id %u\n", |
| 13068 | insn->imm); |
| 13069 | return -EFAULT; |
| 13070 | } |
| 13071 | |
| 13072 | insn->imm = desc->imm; |
| 13073 | |
| 13074 | return 0; |
| 13075 | } |
| 13076 | |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 13077 | /* Do various post-verification rewrites in a single program pass. |
| 13078 | * These rewrites simplify JIT and interpreter implementations. |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13079 | */ |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 13080 | static int do_misc_fixups(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13081 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13082 | struct bpf_prog *prog = env->prog; |
Jiri Olsa | f92c1e1 | 2021-12-08 20:32:44 +0100 | [diff] [blame] | 13083 | enum bpf_attach_type eatype = prog->expected_attach_type; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13084 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 13085 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13086 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13087 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13088 | const int insn_cnt = prog->len; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13089 | const struct bpf_map_ops *ops; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 13090 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 13091 | struct bpf_insn insn_buf[16]; |
| 13092 | struct bpf_prog *new_prog; |
| 13093 | struct bpf_map *map_ptr; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13094 | int i, ret, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13095 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13096 | for (i = 0; i < insn_cnt; i++, insn++) { |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 13097 | /* Make divide-by-zero exceptions impossible. */ |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13098 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 13099 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 13100 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 13101 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13102 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
Daniel Borkmann | e88b2c6 | 2021-02-09 18:46:10 +0000 | [diff] [blame] | 13103 | bool isdiv = BPF_OP(insn->code) == BPF_DIV; |
| 13104 | struct bpf_insn *patchlet; |
| 13105 | struct bpf_insn chk_and_div[] = { |
Daniel Borkmann | 9b00f1b | 2021-02-10 14:14:42 +0100 | [diff] [blame] | 13106 | /* [R,W]x div 0 -> 0 */ |
Daniel Borkmann | e88b2c6 | 2021-02-09 18:46:10 +0000 | [diff] [blame] | 13107 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
| 13108 | BPF_JNE | BPF_K, insn->src_reg, |
| 13109 | 0, 2, 0), |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13110 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 13111 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 13112 | *insn, |
| 13113 | }; |
Daniel Borkmann | e88b2c6 | 2021-02-09 18:46:10 +0000 | [diff] [blame] | 13114 | struct bpf_insn chk_and_mod[] = { |
Daniel Borkmann | 9b00f1b | 2021-02-10 14:14:42 +0100 | [diff] [blame] | 13115 | /* [R,W]x mod 0 -> [R,W]x */ |
Daniel Borkmann | e88b2c6 | 2021-02-09 18:46:10 +0000 | [diff] [blame] | 13116 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
| 13117 | BPF_JEQ | BPF_K, insn->src_reg, |
Daniel Borkmann | 9b00f1b | 2021-02-10 14:14:42 +0100 | [diff] [blame] | 13118 | 0, 1 + (is64 ? 0 : 1), 0), |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13119 | *insn, |
Daniel Borkmann | 9b00f1b | 2021-02-10 14:14:42 +0100 | [diff] [blame] | 13120 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 13121 | BPF_MOV32_REG(insn->dst_reg, insn->dst_reg), |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13122 | }; |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13123 | |
Daniel Borkmann | e88b2c6 | 2021-02-09 18:46:10 +0000 | [diff] [blame] | 13124 | patchlet = isdiv ? chk_and_div : chk_and_mod; |
| 13125 | cnt = isdiv ? ARRAY_SIZE(chk_and_div) : |
Daniel Borkmann | 9b00f1b | 2021-02-10 14:14:42 +0100 | [diff] [blame] | 13126 | ARRAY_SIZE(chk_and_mod) - (is64 ? 2 : 0); |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13127 | |
| 13128 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 13129 | if (!new_prog) |
| 13130 | return -ENOMEM; |
| 13131 | |
| 13132 | delta += cnt - 1; |
| 13133 | env->prog = prog = new_prog; |
| 13134 | insn = new_prog->insnsi + i + delta; |
| 13135 | continue; |
| 13136 | } |
| 13137 | |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 13138 | /* Implement LD_ABS and LD_IND with a rewrite, if supported by the program type. */ |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 13139 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 13140 | (BPF_MODE(insn->code) == BPF_ABS || |
| 13141 | BPF_MODE(insn->code) == BPF_IND)) { |
| 13142 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 13143 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 13144 | verbose(env, "bpf verifier is misconfigured\n"); |
| 13145 | return -EINVAL; |
| 13146 | } |
| 13147 | |
| 13148 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 13149 | if (!new_prog) |
| 13150 | return -ENOMEM; |
| 13151 | |
| 13152 | delta += cnt - 1; |
| 13153 | env->prog = prog = new_prog; |
| 13154 | insn = new_prog->insnsi + i + delta; |
| 13155 | continue; |
| 13156 | } |
| 13157 | |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 13158 | /* Rewrite pointer arithmetic to mitigate speculation attacks. */ |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13159 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
| 13160 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { |
| 13161 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; |
| 13162 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13163 | struct bpf_insn *patch = &insn_buf[0]; |
Daniel Borkmann | 801c605 | 2021-04-29 15:19:37 +0000 | [diff] [blame] | 13164 | bool issrc, isneg, isimm; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13165 | u32 off_reg; |
| 13166 | |
| 13167 | aux = &env->insn_aux_data[i + delta]; |
Daniel Borkmann | 3612af7 | 2019-03-01 22:05:29 +0100 | [diff] [blame] | 13168 | if (!aux->alu_state || |
| 13169 | aux->alu_state == BPF_ALU_NON_POINTER) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13170 | continue; |
| 13171 | |
| 13172 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; |
| 13173 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == |
| 13174 | BPF_ALU_SANITIZE_SRC; |
Daniel Borkmann | 801c605 | 2021-04-29 15:19:37 +0000 | [diff] [blame] | 13175 | isimm = aux->alu_state & BPF_ALU_IMMEDIATE; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13176 | |
| 13177 | off_reg = issrc ? insn->src_reg : insn->dst_reg; |
Daniel Borkmann | 801c605 | 2021-04-29 15:19:37 +0000 | [diff] [blame] | 13178 | if (isimm) { |
| 13179 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); |
| 13180 | } else { |
| 13181 | if (isneg) |
| 13182 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 13183 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); |
| 13184 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); |
| 13185 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); |
| 13186 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); |
| 13187 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); |
| 13188 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); |
| 13189 | } |
Daniel Borkmann | b9b34dd | 2021-04-30 16:21:46 +0200 | [diff] [blame] | 13190 | if (!issrc) |
| 13191 | *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); |
| 13192 | insn->src_reg = BPF_REG_AX; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13193 | if (isneg) |
| 13194 | insn->code = insn->code == code_add ? |
| 13195 | code_sub : code_add; |
| 13196 | *patch++ = *insn; |
Daniel Borkmann | 801c605 | 2021-04-29 15:19:37 +0000 | [diff] [blame] | 13197 | if (issrc && isneg && !isimm) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13198 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 13199 | cnt = patch - insn_buf; |
| 13200 | |
| 13201 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 13202 | if (!new_prog) |
| 13203 | return -ENOMEM; |
| 13204 | |
| 13205 | delta += cnt - 1; |
| 13206 | env->prog = prog = new_prog; |
| 13207 | insn = new_prog->insnsi + i + delta; |
| 13208 | continue; |
| 13209 | } |
| 13210 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13211 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 13212 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 13213 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 13214 | continue; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 13215 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
| 13216 | ret = fixup_kfunc_call(env, insn); |
| 13217 | if (ret) |
| 13218 | return ret; |
| 13219 | continue; |
| 13220 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13221 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13222 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 13223 | prog->dst_needed = 1; |
| 13224 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 13225 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 13226 | if (insn->imm == BPF_FUNC_override_return) |
| 13227 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13228 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 13229 | /* If we tail call into other programs, we |
| 13230 | * cannot make any assumptions since they can |
| 13231 | * be replaced dynamically during runtime in |
| 13232 | * the program array. |
| 13233 | */ |
| 13234 | prog->cb_access = 1; |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 13235 | if (!allow_tail_call_in_subprogs(env)) |
| 13236 | prog->aux->stack_depth = MAX_BPF_STACK; |
| 13237 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 13238 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13239 | /* mark bpf_tail_call as different opcode to avoid |
Zhen Lei | 8fb33b6 | 2021-05-25 10:56:59 +0800 | [diff] [blame] | 13240 | * conditional branch in the interpreter for every normal |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13241 | * call and to prevent accidental JITing by JIT compiler |
| 13242 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13243 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13244 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 13245 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 13246 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 13247 | aux = &env->insn_aux_data[i + delta]; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 13248 | if (env->bpf_capable && !expect_blinding && |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 13249 | prog->jit_requested && |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13250 | !bpf_map_key_poisoned(aux) && |
| 13251 | !bpf_map_ptr_poisoned(aux) && |
| 13252 | !bpf_map_ptr_unpriv(aux)) { |
| 13253 | struct bpf_jit_poke_descriptor desc = { |
| 13254 | .reason = BPF_POKE_REASON_TAIL_CALL, |
| 13255 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), |
| 13256 | .tail_call.key = bpf_map_key_immediate(aux), |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 13257 | .insn_idx = i + delta, |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13258 | }; |
| 13259 | |
| 13260 | ret = bpf_jit_add_poke_descriptor(prog, &desc); |
| 13261 | if (ret < 0) { |
| 13262 | verbose(env, "adding tail call poke descriptor failed\n"); |
| 13263 | return ret; |
| 13264 | } |
| 13265 | |
| 13266 | insn->imm = ret + 1; |
| 13267 | continue; |
| 13268 | } |
| 13269 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 13270 | if (!bpf_map_ptr_unpriv(aux)) |
| 13271 | continue; |
| 13272 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 13273 | /* instead of changing every JIT dealing with tail_call |
| 13274 | * emit two extra insns: |
| 13275 | * if (index >= max_entries) goto out; |
| 13276 | * index &= array->index_mask; |
| 13277 | * to avoid out-of-bounds cpu speculation |
| 13278 | */ |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 13279 | if (bpf_map_ptr_poisoned(aux)) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 13280 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 13281 | return -EINVAL; |
| 13282 | } |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 13283 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13284 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 13285 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 13286 | map_ptr->max_entries, 2); |
| 13287 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 13288 | container_of(map_ptr, |
| 13289 | struct bpf_array, |
| 13290 | map)->index_mask); |
| 13291 | insn_buf[2] = *insn; |
| 13292 | cnt = 3; |
| 13293 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 13294 | if (!new_prog) |
| 13295 | return -ENOMEM; |
| 13296 | |
| 13297 | delta += cnt - 1; |
| 13298 | env->prog = prog = new_prog; |
| 13299 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13300 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13301 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13302 | |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 13303 | if (insn->imm == BPF_FUNC_timer_set_callback) { |
| 13304 | /* The verifier will process callback_fn as many times as necessary |
| 13305 | * with different maps and the register states prepared by |
| 13306 | * set_timer_callback_state will be accurate. |
| 13307 | * |
| 13308 | * The following use case is valid: |
| 13309 | * map1 is shared by prog1, prog2, prog3. |
| 13310 | * prog1 calls bpf_timer_init for some map1 elements |
| 13311 | * prog2 calls bpf_timer_set_callback for some map1 elements. |
| 13312 | * Those that were not bpf_timer_init-ed will return -EINVAL. |
| 13313 | * prog3 calls bpf_timer_start for some map1 elements. |
| 13314 | * Those that were not both bpf_timer_init-ed and |
| 13315 | * bpf_timer_set_callback-ed will return -EINVAL. |
| 13316 | */ |
| 13317 | struct bpf_insn ld_addrs[2] = { |
| 13318 | BPF_LD_IMM64(BPF_REG_3, (long)prog->aux), |
| 13319 | }; |
| 13320 | |
| 13321 | insn_buf[0] = ld_addrs[0]; |
| 13322 | insn_buf[1] = ld_addrs[1]; |
| 13323 | insn_buf[2] = *insn; |
| 13324 | cnt = 3; |
| 13325 | |
| 13326 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 13327 | if (!new_prog) |
| 13328 | return -ENOMEM; |
| 13329 | |
| 13330 | delta += cnt - 1; |
| 13331 | env->prog = prog = new_prog; |
| 13332 | insn = new_prog->insnsi + i + delta; |
| 13333 | goto patch_call_imm; |
| 13334 | } |
| 13335 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 13336 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13337 | * and other inlining handlers are currently limited to 64 bit |
| 13338 | * only. |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 13339 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 13340 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13341 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 13342 | insn->imm == BPF_FUNC_map_update_elem || |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 13343 | insn->imm == BPF_FUNC_map_delete_elem || |
| 13344 | insn->imm == BPF_FUNC_map_push_elem || |
| 13345 | insn->imm == BPF_FUNC_map_pop_elem || |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 13346 | insn->imm == BPF_FUNC_map_peek_elem || |
Andrey Ignatov | 0640c77 | 2021-10-05 17:18:38 -0700 | [diff] [blame] | 13347 | insn->imm == BPF_FUNC_redirect_map || |
| 13348 | insn->imm == BPF_FUNC_for_each_map_elem)) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 13349 | aux = &env->insn_aux_data[i + delta]; |
| 13350 | if (bpf_map_ptr_poisoned(aux)) |
| 13351 | goto patch_call_imm; |
| 13352 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13353 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13354 | ops = map_ptr->ops; |
| 13355 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 13356 | ops->map_gen_lookup) { |
| 13357 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
Daniel Borkmann | 4a8f87e | 2020-10-11 01:40:03 +0200 | [diff] [blame] | 13358 | if (cnt == -EOPNOTSUPP) |
| 13359 | goto patch_map_ops_generic; |
| 13360 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13361 | verbose(env, "bpf verifier is misconfigured\n"); |
| 13362 | return -EINVAL; |
| 13363 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 13364 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13365 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 13366 | insn_buf, cnt); |
| 13367 | if (!new_prog) |
| 13368 | return -ENOMEM; |
| 13369 | |
| 13370 | delta += cnt - 1; |
| 13371 | env->prog = prog = new_prog; |
| 13372 | insn = new_prog->insnsi + i + delta; |
| 13373 | continue; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 13374 | } |
| 13375 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13376 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 13377 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 13378 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 13379 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 13380 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 13381 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 13382 | u64 flags))NULL)); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 13383 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 13384 | (int (*)(struct bpf_map *map, void *value, |
| 13385 | u64 flags))NULL)); |
| 13386 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 13387 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 13388 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 13389 | (int (*)(struct bpf_map *map, void *value))NULL)); |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 13390 | BUILD_BUG_ON(!__same_type(ops->map_redirect, |
| 13391 | (int (*)(struct bpf_map *map, u32 ifindex, u64 flags))NULL)); |
Andrey Ignatov | 0640c77 | 2021-10-05 17:18:38 -0700 | [diff] [blame] | 13392 | BUILD_BUG_ON(!__same_type(ops->map_for_each_callback, |
| 13393 | (int (*)(struct bpf_map *map, |
| 13394 | bpf_callback_t callback_fn, |
| 13395 | void *callback_ctx, |
| 13396 | u64 flags))NULL)); |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 13397 | |
Daniel Borkmann | 4a8f87e | 2020-10-11 01:40:03 +0200 | [diff] [blame] | 13398 | patch_map_ops_generic: |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13399 | switch (insn->imm) { |
| 13400 | case BPF_FUNC_map_lookup_elem: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13401 | insn->imm = BPF_CALL_IMM(ops->map_lookup_elem); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13402 | continue; |
| 13403 | case BPF_FUNC_map_update_elem: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13404 | insn->imm = BPF_CALL_IMM(ops->map_update_elem); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13405 | continue; |
| 13406 | case BPF_FUNC_map_delete_elem: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13407 | insn->imm = BPF_CALL_IMM(ops->map_delete_elem); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13408 | continue; |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 13409 | case BPF_FUNC_map_push_elem: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13410 | insn->imm = BPF_CALL_IMM(ops->map_push_elem); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 13411 | continue; |
| 13412 | case BPF_FUNC_map_pop_elem: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13413 | insn->imm = BPF_CALL_IMM(ops->map_pop_elem); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 13414 | continue; |
| 13415 | case BPF_FUNC_map_peek_elem: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13416 | insn->imm = BPF_CALL_IMM(ops->map_peek_elem); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 13417 | continue; |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 13418 | case BPF_FUNC_redirect_map: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13419 | insn->imm = BPF_CALL_IMM(ops->map_redirect); |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 13420 | continue; |
Andrey Ignatov | 0640c77 | 2021-10-05 17:18:38 -0700 | [diff] [blame] | 13421 | case BPF_FUNC_for_each_map_elem: |
| 13422 | insn->imm = BPF_CALL_IMM(ops->map_for_each_callback); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 13423 | continue; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13424 | } |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 13425 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13426 | goto patch_call_imm; |
| 13427 | } |
| 13428 | |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 13429 | /* Implement bpf_jiffies64 inline. */ |
Martin KaFai Lau | 5576b99 | 2020-01-22 15:36:46 -0800 | [diff] [blame] | 13430 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
| 13431 | insn->imm == BPF_FUNC_jiffies64) { |
| 13432 | struct bpf_insn ld_jiffies_addr[2] = { |
| 13433 | BPF_LD_IMM64(BPF_REG_0, |
| 13434 | (unsigned long)&jiffies), |
| 13435 | }; |
| 13436 | |
| 13437 | insn_buf[0] = ld_jiffies_addr[0]; |
| 13438 | insn_buf[1] = ld_jiffies_addr[1]; |
| 13439 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, |
| 13440 | BPF_REG_0, 0); |
| 13441 | cnt = 3; |
| 13442 | |
| 13443 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, |
| 13444 | cnt); |
| 13445 | if (!new_prog) |
| 13446 | return -ENOMEM; |
| 13447 | |
| 13448 | delta += cnt - 1; |
| 13449 | env->prog = prog = new_prog; |
| 13450 | insn = new_prog->insnsi + i + delta; |
| 13451 | continue; |
| 13452 | } |
| 13453 | |
Jiri Olsa | f92c1e1 | 2021-12-08 20:32:44 +0100 | [diff] [blame] | 13454 | /* Implement bpf_get_func_arg inline. */ |
| 13455 | if (prog_type == BPF_PROG_TYPE_TRACING && |
| 13456 | insn->imm == BPF_FUNC_get_func_arg) { |
| 13457 | /* Load nr_args from ctx - 8 */ |
| 13458 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); |
| 13459 | insn_buf[1] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6); |
| 13460 | insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3); |
| 13461 | insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); |
| 13462 | insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); |
| 13463 | insn_buf[5] = BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); |
| 13464 | insn_buf[6] = BPF_MOV64_IMM(BPF_REG_0, 0); |
| 13465 | insn_buf[7] = BPF_JMP_A(1); |
| 13466 | insn_buf[8] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL); |
| 13467 | cnt = 9; |
| 13468 | |
| 13469 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 13470 | if (!new_prog) |
| 13471 | return -ENOMEM; |
| 13472 | |
| 13473 | delta += cnt - 1; |
| 13474 | env->prog = prog = new_prog; |
| 13475 | insn = new_prog->insnsi + i + delta; |
| 13476 | continue; |
| 13477 | } |
| 13478 | |
| 13479 | /* Implement bpf_get_func_ret inline. */ |
| 13480 | if (prog_type == BPF_PROG_TYPE_TRACING && |
| 13481 | insn->imm == BPF_FUNC_get_func_ret) { |
| 13482 | if (eatype == BPF_TRACE_FEXIT || |
| 13483 | eatype == BPF_MODIFY_RETURN) { |
| 13484 | /* Load nr_args from ctx - 8 */ |
| 13485 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); |
| 13486 | insn_buf[1] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3); |
| 13487 | insn_buf[2] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1); |
| 13488 | insn_buf[3] = BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); |
| 13489 | insn_buf[4] = BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0); |
| 13490 | insn_buf[5] = BPF_MOV64_IMM(BPF_REG_0, 0); |
| 13491 | cnt = 6; |
| 13492 | } else { |
| 13493 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, -EOPNOTSUPP); |
| 13494 | cnt = 1; |
| 13495 | } |
| 13496 | |
| 13497 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 13498 | if (!new_prog) |
| 13499 | return -ENOMEM; |
| 13500 | |
| 13501 | delta += cnt - 1; |
| 13502 | env->prog = prog = new_prog; |
| 13503 | insn = new_prog->insnsi + i + delta; |
| 13504 | continue; |
| 13505 | } |
| 13506 | |
| 13507 | /* Implement get_func_arg_cnt inline. */ |
| 13508 | if (prog_type == BPF_PROG_TYPE_TRACING && |
| 13509 | insn->imm == BPF_FUNC_get_func_arg_cnt) { |
| 13510 | /* Load nr_args from ctx - 8 */ |
| 13511 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); |
| 13512 | |
| 13513 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); |
| 13514 | if (!new_prog) |
| 13515 | return -ENOMEM; |
| 13516 | |
| 13517 | env->prog = prog = new_prog; |
| 13518 | insn = new_prog->insnsi + i + delta; |
| 13519 | continue; |
| 13520 | } |
| 13521 | |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 13522 | /* Implement bpf_get_func_ip inline. */ |
| 13523 | if (prog_type == BPF_PROG_TYPE_TRACING && |
| 13524 | insn->imm == BPF_FUNC_get_func_ip) { |
Jiri Olsa | f92c1e1 | 2021-12-08 20:32:44 +0100 | [diff] [blame] | 13525 | /* Load IP address from ctx - 16 */ |
| 13526 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -16); |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 13527 | |
| 13528 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); |
| 13529 | if (!new_prog) |
| 13530 | return -ENOMEM; |
| 13531 | |
| 13532 | env->prog = prog = new_prog; |
| 13533 | insn = new_prog->insnsi + i + delta; |
| 13534 | continue; |
| 13535 | } |
| 13536 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 13537 | patch_call_imm: |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13538 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
| 13539 | /* all functions that have prototype and verifier allowed |
| 13540 | * programs to call them, must be real in-kernel functions |
| 13541 | */ |
| 13542 | if (!fn->func) { |
| 13543 | verbose(env, |
| 13544 | "kernel subsystem misconfigured func %s#%d\n", |
| 13545 | func_id_name(insn->imm), insn->imm); |
| 13546 | return -EFAULT; |
| 13547 | } |
| 13548 | insn->imm = fn->func - __bpf_call_base; |
| 13549 | } |
| 13550 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13551 | /* Since poke tab is now finalized, publish aux to tracker. */ |
| 13552 | for (i = 0; i < prog->aux->size_poke_tab; i++) { |
| 13553 | map_ptr = prog->aux->poke_tab[i].tail_call.map; |
| 13554 | if (!map_ptr->ops->map_poke_track || |
| 13555 | !map_ptr->ops->map_poke_untrack || |
| 13556 | !map_ptr->ops->map_poke_run) { |
| 13557 | verbose(env, "bpf verifier is misconfigured\n"); |
| 13558 | return -EINVAL; |
| 13559 | } |
| 13560 | |
| 13561 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); |
| 13562 | if (ret < 0) { |
| 13563 | verbose(env, "tracking tail call prog failed\n"); |
| 13564 | return ret; |
| 13565 | } |
| 13566 | } |
| 13567 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 13568 | sort_kfunc_descs_by_imm(env->prog); |
| 13569 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 13570 | return 0; |
| 13571 | } |
| 13572 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13573 | static void free_states(struct bpf_verifier_env *env) |
| 13574 | { |
| 13575 | struct bpf_verifier_state_list *sl, *sln; |
| 13576 | int i; |
| 13577 | |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 13578 | sl = env->free_list; |
| 13579 | while (sl) { |
| 13580 | sln = sl->next; |
| 13581 | free_verifier_state(&sl->state, false); |
| 13582 | kfree(sl); |
| 13583 | sl = sln; |
| 13584 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13585 | env->free_list = NULL; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 13586 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13587 | if (!env->explored_states) |
| 13588 | return; |
| 13589 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 13590 | for (i = 0; i < state_htab_size(env); i++) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13591 | sl = env->explored_states[i]; |
| 13592 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 13593 | while (sl) { |
| 13594 | sln = sl->next; |
| 13595 | free_verifier_state(&sl->state, false); |
| 13596 | kfree(sl); |
| 13597 | sl = sln; |
| 13598 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13599 | env->explored_states[i] = NULL; |
| 13600 | } |
| 13601 | } |
| 13602 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13603 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
| 13604 | { |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 13605 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13606 | struct bpf_verifier_state *state; |
| 13607 | struct bpf_reg_state *regs; |
| 13608 | int ret, i; |
| 13609 | |
| 13610 | env->prev_linfo = NULL; |
| 13611 | env->pass_cnt++; |
| 13612 | |
| 13613 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 13614 | if (!state) |
| 13615 | return -ENOMEM; |
| 13616 | state->curframe = 0; |
| 13617 | state->speculative = false; |
| 13618 | state->branches = 1; |
| 13619 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 13620 | if (!state->frame[0]) { |
| 13621 | kfree(state); |
| 13622 | return -ENOMEM; |
| 13623 | } |
| 13624 | env->cur_state = state; |
| 13625 | init_func_state(env, state->frame[0], |
| 13626 | BPF_MAIN_FUNC /* callsite */, |
| 13627 | 0 /* frameno */, |
| 13628 | subprog); |
| 13629 | |
| 13630 | regs = state->frame[state->curframe]->regs; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13631 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13632 | ret = btf_prepare_func_args(env, subprog, regs); |
| 13633 | if (ret) |
| 13634 | goto out; |
| 13635 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { |
| 13636 | if (regs[i].type == PTR_TO_CTX) |
| 13637 | mark_reg_known_zero(env, regs, i); |
| 13638 | else if (regs[i].type == SCALAR_VALUE) |
| 13639 | mark_reg_unknown(env, regs, i); |
Hao Luo | cf9f2f8 | 2021-12-16 16:31:49 -0800 | [diff] [blame] | 13640 | else if (base_type(regs[i].type) == PTR_TO_MEM) { |
Dmitrii Banshchikov | e5069b9c | 2021-02-13 00:56:41 +0400 | [diff] [blame] | 13641 | const u32 mem_size = regs[i].mem_size; |
| 13642 | |
| 13643 | mark_reg_known_zero(env, regs, i); |
| 13644 | regs[i].mem_size = mem_size; |
| 13645 | regs[i].id = ++env->id_gen; |
| 13646 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13647 | } |
| 13648 | } else { |
| 13649 | /* 1st arg to a function */ |
| 13650 | regs[BPF_REG_1].type = PTR_TO_CTX; |
| 13651 | mark_reg_known_zero(env, regs, BPF_REG_1); |
Martin KaFai Lau | 34747c4 | 2021-03-24 18:51:36 -0700 | [diff] [blame] | 13652 | ret = btf_check_subprog_arg_match(env, subprog, regs); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13653 | if (ret == -EFAULT) |
| 13654 | /* unlikely verifier bug. abort. |
| 13655 | * ret == 0 and ret < 0 are sadly acceptable for |
| 13656 | * main() function due to backward compatibility. |
| 13657 | * Like socket filter program may be written as: |
| 13658 | * int bpf_prog(struct pt_regs *ctx) |
| 13659 | * and never dereference that ctx in the program. |
| 13660 | * 'struct pt_regs' is a type mismatch for socket |
| 13661 | * filter that should be using 'struct __sk_buff'. |
| 13662 | */ |
| 13663 | goto out; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13664 | } |
| 13665 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13666 | ret = do_check(env); |
| 13667 | out: |
Alexei Starovoitov | f59bbfc | 2020-01-21 18:41:38 -0800 | [diff] [blame] | 13668 | /* check for NULL is necessary, since cur_state can be freed inside |
| 13669 | * do_check() under memory pressure. |
| 13670 | */ |
| 13671 | if (env->cur_state) { |
| 13672 | free_verifier_state(env->cur_state, true); |
| 13673 | env->cur_state = NULL; |
| 13674 | } |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 13675 | while (!pop_stack(env, NULL, NULL, false)); |
| 13676 | if (!ret && pop_log) |
| 13677 | bpf_vlog_reset(&env->log, 0); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13678 | free_states(env); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13679 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13680 | } |
| 13681 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13682 | /* Verify all global functions in a BPF program one by one based on their BTF. |
| 13683 | * All global functions must pass verification. Otherwise the whole program is rejected. |
| 13684 | * Consider: |
| 13685 | * int bar(int); |
| 13686 | * int foo(int f) |
| 13687 | * { |
| 13688 | * return bar(f); |
| 13689 | * } |
| 13690 | * int bar(int b) |
| 13691 | * { |
| 13692 | * ... |
| 13693 | * } |
| 13694 | * foo() will be verified first for R1=any_scalar_value. During verification it |
| 13695 | * will be assumed that bar() already verified successfully and call to bar() |
| 13696 | * from foo() will be checked for type match only. Later bar() will be verified |
| 13697 | * independently to check that it's safe for R1=any_scalar_value. |
| 13698 | */ |
| 13699 | static int do_check_subprogs(struct bpf_verifier_env *env) |
| 13700 | { |
| 13701 | struct bpf_prog_aux *aux = env->prog->aux; |
| 13702 | int i, ret; |
| 13703 | |
| 13704 | if (!aux->func_info) |
| 13705 | return 0; |
| 13706 | |
| 13707 | for (i = 1; i < env->subprog_cnt; i++) { |
| 13708 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) |
| 13709 | continue; |
| 13710 | env->insn_idx = env->subprog_info[i].start; |
| 13711 | WARN_ON_ONCE(env->insn_idx == 0); |
| 13712 | ret = do_check_common(env, i); |
| 13713 | if (ret) { |
| 13714 | return ret; |
| 13715 | } else if (env->log.level & BPF_LOG_LEVEL) { |
| 13716 | verbose(env, |
| 13717 | "Func#%d is safe for any args that match its prototype\n", |
| 13718 | i); |
| 13719 | } |
| 13720 | } |
| 13721 | return 0; |
| 13722 | } |
| 13723 | |
| 13724 | static int do_check_main(struct bpf_verifier_env *env) |
| 13725 | { |
| 13726 | int ret; |
| 13727 | |
| 13728 | env->insn_idx = 0; |
| 13729 | ret = do_check_common(env, 0); |
| 13730 | if (!ret) |
| 13731 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
| 13732 | return ret; |
| 13733 | } |
| 13734 | |
| 13735 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 13736 | static void print_verification_stats(struct bpf_verifier_env *env) |
| 13737 | { |
| 13738 | int i; |
| 13739 | |
| 13740 | if (env->log.level & BPF_LOG_STATS) { |
| 13741 | verbose(env, "verification time %lld usec\n", |
| 13742 | div_u64(env->verification_time, 1000)); |
| 13743 | verbose(env, "stack depth "); |
| 13744 | for (i = 0; i < env->subprog_cnt; i++) { |
| 13745 | u32 depth = env->subprog_info[i].stack_depth; |
| 13746 | |
| 13747 | verbose(env, "%d", depth); |
| 13748 | if (i + 1 < env->subprog_cnt) |
| 13749 | verbose(env, "+"); |
| 13750 | } |
| 13751 | verbose(env, "\n"); |
| 13752 | } |
| 13753 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " |
| 13754 | "total_states %d peak_states %d mark_read %d\n", |
| 13755 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, |
| 13756 | env->max_states_per_insn, env->total_states, |
| 13757 | env->peak_states, env->longest_mark_read_walk); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 13758 | } |
| 13759 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 13760 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
| 13761 | { |
| 13762 | const struct btf_type *t, *func_proto; |
| 13763 | const struct bpf_struct_ops *st_ops; |
| 13764 | const struct btf_member *member; |
| 13765 | struct bpf_prog *prog = env->prog; |
| 13766 | u32 btf_id, member_idx; |
| 13767 | const char *mname; |
| 13768 | |
Toke Høiland-Jørgensen | 12aa8a9 | 2021-03-26 11:03:13 +0100 | [diff] [blame] | 13769 | if (!prog->gpl_compatible) { |
| 13770 | verbose(env, "struct ops programs must have a GPL compatible license\n"); |
| 13771 | return -EINVAL; |
| 13772 | } |
| 13773 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 13774 | btf_id = prog->aux->attach_btf_id; |
| 13775 | st_ops = bpf_struct_ops_find(btf_id); |
| 13776 | if (!st_ops) { |
| 13777 | verbose(env, "attach_btf_id %u is not a supported struct\n", |
| 13778 | btf_id); |
| 13779 | return -ENOTSUPP; |
| 13780 | } |
| 13781 | |
| 13782 | t = st_ops->type; |
| 13783 | member_idx = prog->expected_attach_type; |
| 13784 | if (member_idx >= btf_type_vlen(t)) { |
| 13785 | verbose(env, "attach to invalid member idx %u of struct %s\n", |
| 13786 | member_idx, st_ops->name); |
| 13787 | return -EINVAL; |
| 13788 | } |
| 13789 | |
| 13790 | member = &btf_type_member(t)[member_idx]; |
| 13791 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); |
| 13792 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, |
| 13793 | NULL); |
| 13794 | if (!func_proto) { |
| 13795 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", |
| 13796 | mname, member_idx, st_ops->name); |
| 13797 | return -EINVAL; |
| 13798 | } |
| 13799 | |
| 13800 | if (st_ops->check_member) { |
| 13801 | int err = st_ops->check_member(t, member); |
| 13802 | |
| 13803 | if (err) { |
| 13804 | verbose(env, "attach to unsupported member %s of struct %s\n", |
| 13805 | mname, st_ops->name); |
| 13806 | return err; |
| 13807 | } |
| 13808 | } |
| 13809 | |
| 13810 | prog->aux->attach_func_proto = func_proto; |
| 13811 | prog->aux->attach_func_name = mname; |
| 13812 | env->ops = st_ops->verifier_ops; |
| 13813 | |
| 13814 | return 0; |
| 13815 | } |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 13816 | #define SECURITY_PREFIX "security_" |
| 13817 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13818 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 13819 | { |
KP Singh | 6919175 | 2020-03-05 21:49:55 +0100 | [diff] [blame] | 13820 | if (within_error_injection_list(addr) || |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13821 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 13822 | return 0; |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 13823 | |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 13824 | return -EINVAL; |
| 13825 | } |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 13826 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 13827 | /* list of non-sleepable functions that are otherwise on |
| 13828 | * ALLOW_ERROR_INJECTION list |
| 13829 | */ |
| 13830 | BTF_SET_START(btf_non_sleepable_error_inject) |
| 13831 | /* Three functions below can be called from sleepable and non-sleepable context. |
| 13832 | * Assume non-sleepable from bpf safety point of view. |
| 13833 | */ |
Matthew Wilcox (Oracle) | 9dd3d06 | 2020-12-08 08:56:28 -0500 | [diff] [blame] | 13834 | BTF_ID(func, __filemap_add_folio) |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 13835 | BTF_ID(func, should_fail_alloc_page) |
| 13836 | BTF_ID(func, should_failslab) |
| 13837 | BTF_SET_END(btf_non_sleepable_error_inject) |
| 13838 | |
| 13839 | static int check_non_sleepable_error_inject(u32 btf_id) |
| 13840 | { |
| 13841 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); |
| 13842 | } |
| 13843 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13844 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
| 13845 | const struct bpf_prog *prog, |
| 13846 | const struct bpf_prog *tgt_prog, |
| 13847 | u32 btf_id, |
| 13848 | struct bpf_attach_target_info *tgt_info) |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13849 | { |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13850 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13851 | const char prefix[] = "btf_trace_"; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13852 | int ret = 0, subprog = -1, i; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13853 | const struct btf_type *t; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13854 | bool conservative = true; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13855 | const char *tname; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13856 | struct btf *btf; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13857 | long addr = 0; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13858 | |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13859 | if (!btf_id) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13860 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13861 | return -EINVAL; |
| 13862 | } |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 13863 | btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13864 | if (!btf) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13865 | bpf_log(log, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13866 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
| 13867 | return -EINVAL; |
| 13868 | } |
| 13869 | t = btf_type_by_id(btf, btf_id); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13870 | if (!t) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13871 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13872 | return -EINVAL; |
| 13873 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13874 | tname = btf_name_by_offset(btf, t->name_off); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13875 | if (!tname) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13876 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13877 | return -EINVAL; |
| 13878 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13879 | if (tgt_prog) { |
| 13880 | struct bpf_prog_aux *aux = tgt_prog->aux; |
| 13881 | |
| 13882 | for (i = 0; i < aux->func_info_cnt; i++) |
| 13883 | if (aux->func_info[i].type_id == btf_id) { |
| 13884 | subprog = i; |
| 13885 | break; |
| 13886 | } |
| 13887 | if (subprog == -1) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13888 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13889 | return -EINVAL; |
| 13890 | } |
| 13891 | conservative = aux->func_info_aux[subprog].unreliable; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13892 | if (prog_extension) { |
| 13893 | if (conservative) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13894 | bpf_log(log, |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13895 | "Cannot replace static functions\n"); |
| 13896 | return -EINVAL; |
| 13897 | } |
| 13898 | if (!prog->jit_requested) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13899 | bpf_log(log, |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13900 | "Extension programs should be JITed\n"); |
| 13901 | return -EINVAL; |
| 13902 | } |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13903 | } |
| 13904 | if (!tgt_prog->jited) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13905 | bpf_log(log, "Can attach to only JITed progs\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13906 | return -EINVAL; |
| 13907 | } |
| 13908 | if (tgt_prog->type == prog->type) { |
| 13909 | /* Cannot fentry/fexit another fentry/fexit program. |
| 13910 | * Cannot attach program extension to another extension. |
| 13911 | * It's ok to attach fentry/fexit to extension program. |
| 13912 | */ |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13913 | bpf_log(log, "Cannot recursively attach\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13914 | return -EINVAL; |
| 13915 | } |
| 13916 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && |
| 13917 | prog_extension && |
| 13918 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || |
| 13919 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { |
| 13920 | /* Program extensions can extend all program types |
| 13921 | * except fentry/fexit. The reason is the following. |
| 13922 | * The fentry/fexit programs are used for performance |
| 13923 | * analysis, stats and can be attached to any program |
| 13924 | * type except themselves. When extension program is |
| 13925 | * replacing XDP function it is necessary to allow |
| 13926 | * performance analysis of all functions. Both original |
| 13927 | * XDP program and its program extension. Hence |
| 13928 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is |
| 13929 | * allowed. If extending of fentry/fexit was allowed it |
| 13930 | * would be possible to create long call chain |
| 13931 | * fentry->extension->fentry->extension beyond |
| 13932 | * reasonable stack size. Hence extending fentry is not |
| 13933 | * allowed. |
| 13934 | */ |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13935 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13936 | return -EINVAL; |
| 13937 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13938 | } else { |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13939 | if (prog_extension) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13940 | bpf_log(log, "Cannot replace kernel functions\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13941 | return -EINVAL; |
| 13942 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13943 | } |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13944 | |
| 13945 | switch (prog->expected_attach_type) { |
| 13946 | case BPF_TRACE_RAW_TP: |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13947 | if (tgt_prog) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13948 | bpf_log(log, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13949 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
| 13950 | return -EINVAL; |
| 13951 | } |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13952 | if (!btf_type_is_typedef(t)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13953 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13954 | btf_id); |
| 13955 | return -EINVAL; |
| 13956 | } |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13957 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13958 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13959 | btf_id, tname); |
| 13960 | return -EINVAL; |
| 13961 | } |
| 13962 | tname += sizeof(prefix) - 1; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13963 | t = btf_type_by_id(btf, t->type); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13964 | if (!btf_type_is_ptr(t)) |
| 13965 | /* should never happen in valid vmlinux build */ |
| 13966 | return -EINVAL; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13967 | t = btf_type_by_id(btf, t->type); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13968 | if (!btf_type_is_func_proto(t)) |
| 13969 | /* should never happen in valid vmlinux build */ |
| 13970 | return -EINVAL; |
| 13971 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13972 | break; |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 13973 | case BPF_TRACE_ITER: |
| 13974 | if (!btf_type_is_func(t)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13975 | bpf_log(log, "attach_btf_id %u is not a function\n", |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 13976 | btf_id); |
| 13977 | return -EINVAL; |
| 13978 | } |
| 13979 | t = btf_type_by_id(btf, t->type); |
| 13980 | if (!btf_type_is_func_proto(t)) |
| 13981 | return -EINVAL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13982 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
| 13983 | if (ret) |
| 13984 | return ret; |
| 13985 | break; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13986 | default: |
| 13987 | if (!prog_extension) |
| 13988 | return -EINVAL; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 13989 | fallthrough; |
KP Singh | ae24082 | 2020-03-04 20:18:49 +0100 | [diff] [blame] | 13990 | case BPF_MODIFY_RETURN: |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 13991 | case BPF_LSM_MAC: |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 13992 | case BPF_TRACE_FENTRY: |
| 13993 | case BPF_TRACE_FEXIT: |
| 13994 | if (!btf_type_is_func(t)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13995 | bpf_log(log, "attach_btf_id %u is not a function\n", |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 13996 | btf_id); |
| 13997 | return -EINVAL; |
| 13998 | } |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13999 | if (prog_extension && |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 14000 | btf_check_type_match(log, prog, btf, t)) |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 14001 | return -EINVAL; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 14002 | t = btf_type_by_id(btf, t->type); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 14003 | if (!btf_type_is_func_proto(t)) |
| 14004 | return -EINVAL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14005 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 14006 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
| 14007 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || |
| 14008 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) |
| 14009 | return -EINVAL; |
| 14010 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14011 | if (tgt_prog && conservative) |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 14012 | t = NULL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14013 | |
| 14014 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 14015 | if (ret < 0) |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14016 | return ret; |
| 14017 | |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 14018 | if (tgt_prog) { |
Yonghong Song | e9eeec5 | 2019-12-04 17:06:06 -0800 | [diff] [blame] | 14019 | if (subprog == 0) |
| 14020 | addr = (long) tgt_prog->bpf_func; |
| 14021 | else |
| 14022 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 14023 | } else { |
| 14024 | addr = kallsyms_lookup_name(tname); |
| 14025 | if (!addr) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 14026 | bpf_log(log, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 14027 | "The address of function %s cannot be found\n", |
| 14028 | tname); |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14029 | return -ENOENT; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 14030 | } |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 14031 | } |
Alexei Starovoitov | 18644ce | 2020-05-28 21:38:36 -0700 | [diff] [blame] | 14032 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 14033 | if (prog->aux->sleepable) { |
| 14034 | ret = -EINVAL; |
| 14035 | switch (prog->type) { |
| 14036 | case BPF_PROG_TYPE_TRACING: |
| 14037 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are |
| 14038 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. |
| 14039 | */ |
| 14040 | if (!check_non_sleepable_error_inject(btf_id) && |
| 14041 | within_error_injection_list(addr)) |
| 14042 | ret = 0; |
| 14043 | break; |
| 14044 | case BPF_PROG_TYPE_LSM: |
| 14045 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. |
| 14046 | * Only some of them are sleepable. |
| 14047 | */ |
KP Singh | 423f161 | 2020-11-13 00:59:29 +0000 | [diff] [blame] | 14048 | if (bpf_lsm_is_sleepable_hook(btf_id)) |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 14049 | ret = 0; |
| 14050 | break; |
| 14051 | default: |
| 14052 | break; |
| 14053 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14054 | if (ret) { |
| 14055 | bpf_log(log, "%s is not sleepable\n", tname); |
| 14056 | return ret; |
| 14057 | } |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 14058 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
Toke Høiland-Jørgensen | 1af9270 | 2020-09-25 23:25:00 +0200 | [diff] [blame] | 14059 | if (tgt_prog) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 14060 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14061 | return -EINVAL; |
Toke Høiland-Jørgensen | 1af9270 | 2020-09-25 23:25:00 +0200 | [diff] [blame] | 14062 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14063 | ret = check_attach_modify_return(addr, tname); |
| 14064 | if (ret) { |
| 14065 | bpf_log(log, "%s() is not modifiable\n", tname); |
| 14066 | return ret; |
| 14067 | } |
Alexei Starovoitov | 18644ce | 2020-05-28 21:38:36 -0700 | [diff] [blame] | 14068 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14069 | |
| 14070 | break; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 14071 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14072 | tgt_info->tgt_addr = addr; |
| 14073 | tgt_info->tgt_name = tname; |
| 14074 | tgt_info->tgt_type = t; |
| 14075 | return 0; |
| 14076 | } |
| 14077 | |
Jiri Olsa | 35e3815 | 2021-04-29 13:47:12 +0200 | [diff] [blame] | 14078 | BTF_SET_START(btf_id_deny) |
| 14079 | BTF_ID_UNUSED |
| 14080 | #ifdef CONFIG_SMP |
| 14081 | BTF_ID(func, migrate_disable) |
| 14082 | BTF_ID(func, migrate_enable) |
| 14083 | #endif |
| 14084 | #if !defined CONFIG_PREEMPT_RCU && !defined CONFIG_TINY_RCU |
| 14085 | BTF_ID(func, rcu_read_unlock_strict) |
| 14086 | #endif |
| 14087 | BTF_SET_END(btf_id_deny) |
| 14088 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14089 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
| 14090 | { |
| 14091 | struct bpf_prog *prog = env->prog; |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 14092 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14093 | struct bpf_attach_target_info tgt_info = {}; |
| 14094 | u32 btf_id = prog->aux->attach_btf_id; |
| 14095 | struct bpf_trampoline *tr; |
| 14096 | int ret; |
| 14097 | u64 key; |
| 14098 | |
Alexei Starovoitov | 79a7f8b | 2021-05-13 17:36:03 -0700 | [diff] [blame] | 14099 | if (prog->type == BPF_PROG_TYPE_SYSCALL) { |
| 14100 | if (prog->aux->sleepable) |
| 14101 | /* attach_btf_id checked to be zero already */ |
| 14102 | return 0; |
| 14103 | verbose(env, "Syscall programs can only be sleepable\n"); |
| 14104 | return -EINVAL; |
| 14105 | } |
| 14106 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14107 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && |
| 14108 | prog->type != BPF_PROG_TYPE_LSM) { |
| 14109 | verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n"); |
| 14110 | return -EINVAL; |
| 14111 | } |
| 14112 | |
| 14113 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) |
| 14114 | return check_struct_ops_btf_id(env); |
| 14115 | |
| 14116 | if (prog->type != BPF_PROG_TYPE_TRACING && |
| 14117 | prog->type != BPF_PROG_TYPE_LSM && |
| 14118 | prog->type != BPF_PROG_TYPE_EXT) |
| 14119 | return 0; |
| 14120 | |
| 14121 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); |
| 14122 | if (ret) |
| 14123 | return ret; |
| 14124 | |
| 14125 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 14126 | /* to make freplace equivalent to their targets, they need to |
| 14127 | * inherit env->ops and expected_attach_type for the rest of the |
| 14128 | * verification |
| 14129 | */ |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14130 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
| 14131 | prog->expected_attach_type = tgt_prog->expected_attach_type; |
| 14132 | } |
| 14133 | |
| 14134 | /* store info about the attachment target that will be used later */ |
| 14135 | prog->aux->attach_func_proto = tgt_info.tgt_type; |
| 14136 | prog->aux->attach_func_name = tgt_info.tgt_name; |
| 14137 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 14138 | if (tgt_prog) { |
| 14139 | prog->aux->saved_dst_prog_type = tgt_prog->type; |
| 14140 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; |
| 14141 | } |
| 14142 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14143 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
| 14144 | prog->aux->attach_btf_trace = true; |
| 14145 | return 0; |
| 14146 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { |
| 14147 | if (!bpf_iter_prog_supported(prog)) |
| 14148 | return -EINVAL; |
| 14149 | return 0; |
| 14150 | } |
| 14151 | |
| 14152 | if (prog->type == BPF_PROG_TYPE_LSM) { |
| 14153 | ret = bpf_lsm_verify_prog(&env->log, prog); |
| 14154 | if (ret < 0) |
| 14155 | return ret; |
Jiri Olsa | 35e3815 | 2021-04-29 13:47:12 +0200 | [diff] [blame] | 14156 | } else if (prog->type == BPF_PROG_TYPE_TRACING && |
| 14157 | btf_id_set_contains(&btf_id_deny, btf_id)) { |
| 14158 | return -EINVAL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14159 | } |
| 14160 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 14161 | key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id); |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14162 | tr = bpf_trampoline_get(key, &tgt_info); |
| 14163 | if (!tr) |
| 14164 | return -ENOMEM; |
| 14165 | |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 14166 | prog->aux->dst_trampoline = tr; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14167 | return 0; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 14168 | } |
| 14169 | |
Alan Maguire | 76654e6 | 2020-09-28 12:31:03 +0100 | [diff] [blame] | 14170 | struct btf *bpf_get_btf_vmlinux(void) |
| 14171 | { |
| 14172 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { |
| 14173 | mutex_lock(&bpf_verifier_lock); |
| 14174 | if (!btf_vmlinux) |
| 14175 | btf_vmlinux = btf_parse_vmlinux(); |
| 14176 | mutex_unlock(&bpf_verifier_lock); |
| 14177 | } |
| 14178 | return btf_vmlinux; |
| 14179 | } |
| 14180 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 14181 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 14182 | { |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 14183 | u64 start_time = ktime_get_ns(); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 14184 | struct bpf_verifier_env *env; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 14185 | struct bpf_verifier_log *log; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 14186 | int i, len, ret = -EINVAL; |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 14187 | bool is_priv; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 14188 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 14189 | /* no program is valid */ |
| 14190 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 14191 | return -EINVAL; |
| 14192 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 14193 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14194 | * allocate/free it every time bpf_check() is called |
| 14195 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 14196 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14197 | if (!env) |
| 14198 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 14199 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14200 | |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 14201 | len = (*prog)->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 14202 | env->insn_aux_data = |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 14203 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 14204 | ret = -ENOMEM; |
| 14205 | if (!env->insn_aux_data) |
| 14206 | goto err_free_env; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 14207 | for (i = 0; i < len; i++) |
| 14208 | env->insn_aux_data[i].orig_idx = i; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14209 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 14210 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 14211 | env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel); |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 14212 | is_priv = bpf_capable(); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14213 | |
Alan Maguire | 76654e6 | 2020-09-28 12:31:03 +0100 | [diff] [blame] | 14214 | bpf_get_btf_vmlinux(); |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 14215 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14216 | /* grab the mutex to protect few globals used by verifier */ |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 14217 | if (!is_priv) |
| 14218 | mutex_lock(&bpf_verifier_lock); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14219 | |
| 14220 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 14221 | /* user requested verbose verifier output |
| 14222 | * and supplied buffer to store the verification trace |
| 14223 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 14224 | log->level = attr->log_level; |
| 14225 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 14226 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14227 | |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 14228 | /* log attributes have to be sane */ |
Hou Tao | 866de40 | 2021-12-03 13:30:01 +0800 | [diff] [blame] | 14229 | if (!bpf_verifier_log_attr_valid(log)) { |
| 14230 | ret = -EINVAL; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 14231 | goto err_unlock; |
Hou Tao | 866de40 | 2021-12-03 13:30:01 +0800 | [diff] [blame] | 14232 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14233 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 14234 | |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 14235 | mark_verifier_state_clean(env); |
| 14236 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 14237 | if (IS_ERR(btf_vmlinux)) { |
| 14238 | /* Either gcc or pahole or kernel are broken. */ |
| 14239 | verbose(env, "in-kernel BTF is malformed\n"); |
| 14240 | ret = PTR_ERR(btf_vmlinux); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 14241 | goto skip_full_check; |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 14242 | } |
| 14243 | |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 14244 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 14245 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 14246 | env->strict_alignment = true; |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 14247 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
| 14248 | env->strict_alignment = false; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14249 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 14250 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 14251 | env->allow_uninit_stack = bpf_allow_uninit_stack(); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 14252 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 14253 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
| 14254 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); |
| 14255 | env->bpf_capable = bpf_capable(); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 14256 | |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 14257 | if (is_priv) |
| 14258 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; |
| 14259 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 14260 | env->explored_states = kvcalloc(state_htab_size(env), |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 14261 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 14262 | GFP_USER); |
| 14263 | ret = -ENOMEM; |
| 14264 | if (!env->explored_states) |
| 14265 | goto skip_full_check; |
| 14266 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 14267 | ret = add_subprog_and_kfunc(env); |
| 14268 | if (ret < 0) |
| 14269 | goto skip_full_check; |
| 14270 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 14271 | ret = check_subprogs(env); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 14272 | if (ret < 0) |
| 14273 | goto skip_full_check; |
| 14274 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 14275 | ret = check_btf_info(env, attr, uattr); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 14276 | if (ret < 0) |
| 14277 | goto skip_full_check; |
| 14278 | |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 14279 | ret = check_attach_btf_id(env); |
| 14280 | if (ret) |
| 14281 | goto skip_full_check; |
| 14282 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 14283 | ret = resolve_pseudo_ldimm64(env); |
| 14284 | if (ret < 0) |
| 14285 | goto skip_full_check; |
| 14286 | |
Yinjun Zhang | ceb1167 | 2021-05-20 10:58:34 +0200 | [diff] [blame] | 14287 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
| 14288 | ret = bpf_prog_offload_verifier_prep(env->prog); |
| 14289 | if (ret) |
| 14290 | goto skip_full_check; |
| 14291 | } |
| 14292 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 14293 | ret = check_cfg(env); |
| 14294 | if (ret < 0) |
| 14295 | goto skip_full_check; |
| 14296 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 14297 | ret = do_check_subprogs(env); |
| 14298 | ret = ret ?: do_check_main(env); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14299 | |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 14300 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 14301 | ret = bpf_prog_offload_finalize(env); |
| 14302 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14303 | skip_full_check: |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 14304 | kvfree(env->explored_states); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14305 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14306 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 14307 | ret = check_max_stack_depth(env); |
| 14308 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 14309 | /* instruction rewrites happen after this point */ |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 14310 | if (is_priv) { |
| 14311 | if (ret == 0) |
| 14312 | opt_hard_wire_dead_code_branches(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 14313 | if (ret == 0) |
| 14314 | ret = opt_remove_dead_code(env); |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 14315 | if (ret == 0) |
| 14316 | ret = opt_remove_nops(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 14317 | } else { |
| 14318 | if (ret == 0) |
| 14319 | sanitize_dead_code(env); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 14320 | } |
| 14321 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 14322 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14323 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 14324 | ret = convert_ctx_accesses(env); |
| 14325 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 14326 | if (ret == 0) |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 14327 | ret = do_misc_fixups(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 14328 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 14329 | /* do 32-bit optimization after insn patching has done so those patched |
| 14330 | * insns could be handled correctly. |
| 14331 | */ |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 14332 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
| 14333 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); |
| 14334 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret |
| 14335 | : false; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 14336 | } |
| 14337 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 14338 | if (ret == 0) |
| 14339 | ret = fixup_call_args(env); |
| 14340 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 14341 | env->verification_time = ktime_get_ns() - start_time; |
| 14342 | print_verification_stats(env); |
Dave Marchevsky | aba64c7 | 2021-10-20 00:48:17 -0700 | [diff] [blame] | 14343 | env->prog->aux->verified_insns = env->insn_processed; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 14344 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 14345 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14346 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 14347 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14348 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 14349 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14350 | } |
| 14351 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 14352 | if (ret) |
| 14353 | goto err_release_maps; |
| 14354 | |
| 14355 | if (env->used_map_cnt) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14356 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14357 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 14358 | sizeof(env->used_maps[0]), |
| 14359 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14360 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14361 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14362 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 14363 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14364 | } |
| 14365 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14366 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14367 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14368 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 14369 | } |
| 14370 | if (env->used_btf_cnt) { |
| 14371 | /* if program passed verifier, update used_btfs in bpf_prog_aux */ |
| 14372 | env->prog->aux->used_btfs = kmalloc_array(env->used_btf_cnt, |
| 14373 | sizeof(env->used_btfs[0]), |
| 14374 | GFP_KERNEL); |
| 14375 | if (!env->prog->aux->used_btfs) { |
| 14376 | ret = -ENOMEM; |
| 14377 | goto err_release_maps; |
| 14378 | } |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14379 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 14380 | memcpy(env->prog->aux->used_btfs, env->used_btfs, |
| 14381 | sizeof(env->used_btfs[0]) * env->used_btf_cnt); |
| 14382 | env->prog->aux->used_btf_cnt = env->used_btf_cnt; |
| 14383 | } |
| 14384 | if (env->used_map_cnt || env->used_btf_cnt) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14385 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 14386 | * bpf_ld_imm64 instructions |
| 14387 | */ |
| 14388 | convert_pseudo_ld_imm64(env); |
| 14389 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14390 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 14391 | adjust_btf_func(env); |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 14392 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 14393 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14394 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14395 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 14396 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14397 | */ |
| 14398 | release_maps(env); |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 14399 | if (!env->prog->aux->used_btfs) |
| 14400 | release_btfs(env); |
Toke Høiland-Jørgensen | 03f87c0 | 2020-04-24 15:34:27 +0200 | [diff] [blame] | 14401 | |
| 14402 | /* extension progs temporarily inherit the attach_type of their targets |
| 14403 | for verification purposes, so set it back to zero before returning |
| 14404 | */ |
| 14405 | if (env->prog->type == BPF_PROG_TYPE_EXT) |
| 14406 | env->prog->expected_attach_type = 0; |
| 14407 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14408 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 14409 | err_unlock: |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 14410 | if (!is_priv) |
| 14411 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 14412 | vfree(env->insn_aux_data); |
| 14413 | err_free_env: |
| 14414 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 14415 | return ret; |
| 14416 | } |