David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
| 3 | * Copyright (c) 2016 Facebook |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4 | * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6 | #include <uapi/linux/btf.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/types.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/bpf.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11 | #include <linux/btf.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12 | #include <linux/bpf_verifier.h> |
| 13 | #include <linux/filter.h> |
| 14 | #include <net/netlink.h> |
| 15 | #include <linux/file.h> |
| 16 | #include <linux/vmalloc.h> |
| 17 | #include <linux/stringify.h> |
| 18 | #include <linux/bsearch.h> |
| 19 | #include <linux/sort.h> |
| 20 | #include <linux/perf_event.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 21 | #include <linux/ctype.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 22 | #include <linux/error-injection.h> |
| 23 | #include <linux/bpf_lsm.h> |
| 24 | #include <linux/btf_ids.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 25 | |
| 26 | #include "disasm.h" |
| 27 | |
| 28 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 29 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 30 | [_id] = & _name ## _verifier_ops, |
| 31 | #define BPF_MAP_TYPE(_id, _ops) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 32 | #define BPF_LINK_TYPE(_id, _name) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 33 | #include <linux/bpf_types.h> |
| 34 | #undef BPF_PROG_TYPE |
| 35 | #undef BPF_MAP_TYPE |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 36 | #undef BPF_LINK_TYPE |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 40 | * instruction by instruction and updates register/stack state. |
| 41 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 42 | * |
| 43 | * The first pass is depth-first-search to check that the program is a DAG. |
| 44 | * It rejects the following programs: |
| 45 | * - larger than BPF_MAXINSNS insns |
| 46 | * - if loop is present (detected via back-edge) |
| 47 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 48 | * - out of bounds or malformed jumps |
| 49 | * The second pass is all possible path descent from the 1st insn. |
| 50 | * Since it's analyzing all pathes through the program, the length of the |
| 51 | * analysis is limited to 64k insn, which may be hit even if total number of |
| 52 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 53 | * Number of 'branches to be analyzed' is limited to 1k |
| 54 | * |
| 55 | * On entry to each instruction, each register has a type, and the instruction |
| 56 | * changes the types of the registers depending on instruction semantics. |
| 57 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 58 | * copied to R1. |
| 59 | * |
| 60 | * All registers are 64-bit. |
| 61 | * R0 - return register |
| 62 | * R1-R5 argument passing registers |
| 63 | * R6-R9 callee saved registers |
| 64 | * R10 - frame pointer read-only |
| 65 | * |
| 66 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 67 | * and has type PTR_TO_CTX. |
| 68 | * |
| 69 | * Verifier tracks arithmetic operations on pointers in case: |
| 70 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 71 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 72 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 73 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 74 | * that it wants to construct a pointer to some element within stack. |
| 75 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 76 | * (and -20 constant is saved for further stack bounds checking). |
| 77 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 78 | * |
| 79 | * Most of the time the registers have SCALAR_VALUE type, which |
| 80 | * means the register has some value, but it's not a valid pointer. |
| 81 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
| 82 | * |
| 83 | * When verifier sees load or store instructions the type of base register |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 84 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
| 85 | * four pointer types recognized by check_mem_access() function. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 86 | * |
| 87 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 88 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 89 | * |
| 90 | * registers used to pass values to function calls are checked against |
| 91 | * function argument constraints. |
| 92 | * |
| 93 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 94 | * It means that the register type passed to this function must be |
| 95 | * PTR_TO_STACK and it will be used inside the function as |
| 96 | * 'pointer to map element key' |
| 97 | * |
| 98 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 99 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 100 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 101 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 102 | * |
| 103 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 104 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 105 | * 2nd argument should be a pointer to stack, which will be used inside |
| 106 | * the helper function as a pointer to map element key. |
| 107 | * |
| 108 | * On the kernel side the helper function looks like: |
| 109 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 110 | * { |
| 111 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 112 | * void *key = (void *) (unsigned long) r2; |
| 113 | * void *value; |
| 114 | * |
| 115 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 116 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 117 | * the stack of eBPF program. |
| 118 | * } |
| 119 | * |
| 120 | * Corresponding eBPF program may look like: |
| 121 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 122 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 123 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 124 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 125 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 126 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 127 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 128 | * |
| 129 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 130 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 131 | * and were initialized prior to this call. |
| 132 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 133 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 134 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 135 | * returns ether pointer to map value or NULL. |
| 136 | * |
| 137 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 138 | * insn, the register holding that pointer in the true branch changes state to |
| 139 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 140 | * branch. See check_cond_jmp_op(). |
| 141 | * |
| 142 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 143 | * are set to NOT_INIT to indicate that they are no longer readable. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 144 | * |
| 145 | * The following reference types represent a potential reference to a kernel |
| 146 | * resource which, after first being allocated, must be checked and freed by |
| 147 | * the BPF program: |
| 148 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET |
| 149 | * |
| 150 | * When the verifier sees a helper call return a reference type, it allocates a |
| 151 | * pointer id for the reference and stores it in the current function state. |
| 152 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into |
| 153 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type |
| 154 | * passes through a NULL-check conditional. For the branch wherein the state is |
| 155 | * changed to CONST_IMM, the verifier releases the reference. |
| 156 | * |
| 157 | * For each helper function that allocates a reference, such as |
| 158 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as |
| 159 | * bpf_sk_release(). When a reference type passes into the release function, |
| 160 | * the verifier also releases the reference. If any unchecked or unreleased |
| 161 | * reference remains at the end of the program, the verifier rejects it. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 162 | */ |
| 163 | |
| 164 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
| 165 | struct bpf_verifier_stack_elem { |
| 166 | /* verifer state is 'st' |
| 167 | * before processing instruction 'insn_idx' |
| 168 | * and after processing instruction 'prev_insn_idx' |
| 169 | */ |
| 170 | struct bpf_verifier_state st; |
| 171 | int insn_idx; |
| 172 | int prev_insn_idx; |
| 173 | struct bpf_verifier_stack_elem *next; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 174 | /* length of verifier log at the time this state was pushed on stack */ |
| 175 | u32 log_pos; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 178 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
| 179 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 180 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 181 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
| 182 | #define BPF_MAP_KEY_SEEN (1ULL << 62) |
| 183 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 184 | #define BPF_MAP_PTR_UNPRIV 1UL |
| 185 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ |
| 186 | POISON_POINTER_DELTA)) |
| 187 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) |
| 188 | |
| 189 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
| 190 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 191 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) |
| 195 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 196 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, |
| 200 | const struct bpf_map *map, bool unpriv) |
| 201 | { |
| 202 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); |
| 203 | unpriv |= bpf_map_ptr_unpriv(aux); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 204 | aux->map_ptr_state = (unsigned long)map | |
| 205 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); |
| 206 | } |
| 207 | |
| 208 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) |
| 209 | { |
| 210 | return aux->map_key_state & BPF_MAP_KEY_POISON; |
| 211 | } |
| 212 | |
| 213 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) |
| 214 | { |
| 215 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); |
| 216 | } |
| 217 | |
| 218 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) |
| 219 | { |
| 220 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); |
| 221 | } |
| 222 | |
| 223 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) |
| 224 | { |
| 225 | bool poisoned = bpf_map_key_poisoned(aux); |
| 226 | |
| 227 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | |
| 228 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | struct bpf_call_arg_meta { |
| 232 | struct bpf_map *map_ptr; |
| 233 | bool raw_mode; |
| 234 | bool pkt_access; |
| 235 | int regno; |
| 236 | int access_size; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 237 | int mem_size; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 238 | u64 msize_max_value; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 239 | int ref_obj_id; |
| 240 | int func_id; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 241 | u32 btf_id; |
| 242 | u32 ret_btf_id; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 243 | }; |
| 244 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 245 | struct btf *btf_vmlinux; |
| 246 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 247 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 248 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 249 | static const struct bpf_line_info * |
| 250 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) |
| 251 | { |
| 252 | const struct bpf_line_info *linfo; |
| 253 | const struct bpf_prog *prog; |
| 254 | u32 i, nr_linfo; |
| 255 | |
| 256 | prog = env->prog; |
| 257 | nr_linfo = prog->aux->nr_linfo; |
| 258 | |
| 259 | if (!nr_linfo || insn_off >= prog->len) |
| 260 | return NULL; |
| 261 | |
| 262 | linfo = prog->aux->linfo; |
| 263 | for (i = 1; i < nr_linfo; i++) |
| 264 | if (insn_off < linfo[i].insn_off) |
| 265 | break; |
| 266 | |
| 267 | return &linfo[i - 1]; |
| 268 | } |
| 269 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 270 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
| 271 | va_list args) |
| 272 | { |
| 273 | unsigned int n; |
| 274 | |
| 275 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
| 276 | |
| 277 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 278 | "verifier log line truncated - local buffer too short\n"); |
| 279 | |
| 280 | n = min(log->len_total - log->len_used - 1, n); |
| 281 | log->kbuf[n] = '\0'; |
| 282 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 283 | if (log->level == BPF_LOG_KERNEL) { |
| 284 | pr_err("BPF:%s\n", log->kbuf); |
| 285 | return; |
| 286 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 287 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 288 | log->len_used += n; |
| 289 | else |
| 290 | log->ubuf = NULL; |
| 291 | } |
| 292 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 293 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
| 294 | { |
| 295 | char zero = 0; |
| 296 | |
| 297 | if (!bpf_verifier_log_needed(log)) |
| 298 | return; |
| 299 | |
| 300 | log->len_used = new_pos; |
| 301 | if (put_user(zero, log->ubuf + new_pos)) |
| 302 | log->ubuf = NULL; |
| 303 | } |
| 304 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 305 | /* log_level controls verbosity level of eBPF verifier. |
| 306 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 307 | * so the user can figure out what's wrong with the program |
| 308 | */ |
| 309 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 310 | const char *fmt, ...) |
| 311 | { |
| 312 | va_list args; |
| 313 | |
| 314 | if (!bpf_verifier_log_needed(&env->log)) |
| 315 | return; |
| 316 | |
| 317 | va_start(args, fmt); |
| 318 | bpf_verifier_vlog(&env->log, fmt, args); |
| 319 | va_end(args); |
| 320 | } |
| 321 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 322 | |
| 323 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
| 324 | { |
| 325 | struct bpf_verifier_env *env = private_data; |
| 326 | va_list args; |
| 327 | |
| 328 | if (!bpf_verifier_log_needed(&env->log)) |
| 329 | return; |
| 330 | |
| 331 | va_start(args, fmt); |
| 332 | bpf_verifier_vlog(&env->log, fmt, args); |
| 333 | va_end(args); |
| 334 | } |
| 335 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 336 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
| 337 | const char *fmt, ...) |
| 338 | { |
| 339 | va_list args; |
| 340 | |
| 341 | if (!bpf_verifier_log_needed(log)) |
| 342 | return; |
| 343 | |
| 344 | va_start(args, fmt); |
| 345 | bpf_verifier_vlog(log, fmt, args); |
| 346 | va_end(args); |
| 347 | } |
| 348 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 349 | static const char *ltrim(const char *s) |
| 350 | { |
| 351 | while (isspace(*s)) |
| 352 | s++; |
| 353 | |
| 354 | return s; |
| 355 | } |
| 356 | |
| 357 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, |
| 358 | u32 insn_off, |
| 359 | const char *prefix_fmt, ...) |
| 360 | { |
| 361 | const struct bpf_line_info *linfo; |
| 362 | |
| 363 | if (!bpf_verifier_log_needed(&env->log)) |
| 364 | return; |
| 365 | |
| 366 | linfo = find_linfo(env, insn_off); |
| 367 | if (!linfo || linfo == env->prev_linfo) |
| 368 | return; |
| 369 | |
| 370 | if (prefix_fmt) { |
| 371 | va_list args; |
| 372 | |
| 373 | va_start(args, prefix_fmt); |
| 374 | bpf_verifier_vlog(&env->log, prefix_fmt, args); |
| 375 | va_end(args); |
| 376 | } |
| 377 | |
| 378 | verbose(env, "%s\n", |
| 379 | ltrim(btf_name_by_offset(env->prog->aux->btf, |
| 380 | linfo->line_off))); |
| 381 | |
| 382 | env->prev_linfo = linfo; |
| 383 | } |
| 384 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 385 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 386 | { |
| 387 | return type == PTR_TO_PACKET || |
| 388 | type == PTR_TO_PACKET_META; |
| 389 | } |
| 390 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 391 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
| 392 | { |
| 393 | return type == PTR_TO_SOCKET || |
| 394 | type == PTR_TO_SOCK_COMMON || |
| 395 | type == PTR_TO_TCP_SOCK || |
| 396 | type == PTR_TO_XDP_SOCK; |
| 397 | } |
| 398 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 399 | static bool reg_type_not_null(enum bpf_reg_type type) |
| 400 | { |
| 401 | return type == PTR_TO_SOCKET || |
| 402 | type == PTR_TO_TCP_SOCK || |
| 403 | type == PTR_TO_MAP_VALUE || |
| 404 | type == PTR_TO_SOCK_COMMON; |
| 405 | } |
| 406 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 407 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
| 408 | { |
| 409 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
| 410 | type == PTR_TO_SOCKET_OR_NULL || |
| 411 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 412 | type == PTR_TO_TCP_SOCK_OR_NULL || |
| 413 | type == PTR_TO_BTF_ID_OR_NULL || |
| 414 | type == PTR_TO_MEM_OR_NULL || |
| 415 | type == PTR_TO_RDONLY_BUF_OR_NULL || |
| 416 | type == PTR_TO_RDWR_BUF_OR_NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
| 420 | { |
| 421 | return reg->type == PTR_TO_MAP_VALUE && |
| 422 | map_value_has_spin_lock(reg->map_ptr); |
| 423 | } |
| 424 | |
| 425 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
| 426 | { |
| 427 | return type == PTR_TO_SOCKET || |
| 428 | type == PTR_TO_SOCKET_OR_NULL || |
| 429 | type == PTR_TO_TCP_SOCK || |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 430 | type == PTR_TO_TCP_SOCK_OR_NULL || |
| 431 | type == PTR_TO_MEM || |
| 432 | type == PTR_TO_MEM_OR_NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
| 436 | { |
| 437 | return type == ARG_PTR_TO_SOCK_COMMON; |
| 438 | } |
| 439 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 440 | static bool arg_type_may_be_null(enum bpf_arg_type type) |
| 441 | { |
| 442 | return type == ARG_PTR_TO_MAP_VALUE_OR_NULL || |
| 443 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 444 | type == ARG_PTR_TO_CTX_OR_NULL || |
| 445 | type == ARG_PTR_TO_SOCKET_OR_NULL || |
| 446 | type == ARG_PTR_TO_ALLOC_MEM_OR_NULL; |
| 447 | } |
| 448 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 449 | /* Determine whether the function releases some resources allocated by another |
| 450 | * function call. The first reference type argument will be assumed to be |
| 451 | * released by release_reference(). |
| 452 | */ |
| 453 | static bool is_release_function(enum bpf_func_id func_id) |
| 454 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 455 | return func_id == BPF_FUNC_sk_release || |
| 456 | func_id == BPF_FUNC_ringbuf_submit || |
| 457 | func_id == BPF_FUNC_ringbuf_discard; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 460 | static bool may_be_acquire_function(enum bpf_func_id func_id) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 461 | { |
| 462 | return func_id == BPF_FUNC_sk_lookup_tcp || |
| 463 | func_id == BPF_FUNC_sk_lookup_udp || |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 464 | func_id == BPF_FUNC_skc_lookup_tcp || |
| 465 | func_id == BPF_FUNC_map_lookup_elem || |
| 466 | func_id == BPF_FUNC_ringbuf_reserve; |
| 467 | } |
| 468 | |
| 469 | static bool is_acquire_function(enum bpf_func_id func_id, |
| 470 | const struct bpf_map *map) |
| 471 | { |
| 472 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; |
| 473 | |
| 474 | if (func_id == BPF_FUNC_sk_lookup_tcp || |
| 475 | func_id == BPF_FUNC_sk_lookup_udp || |
| 476 | func_id == BPF_FUNC_skc_lookup_tcp || |
| 477 | func_id == BPF_FUNC_ringbuf_reserve) |
| 478 | return true; |
| 479 | |
| 480 | if (func_id == BPF_FUNC_map_lookup_elem && |
| 481 | (map_type == BPF_MAP_TYPE_SOCKMAP || |
| 482 | map_type == BPF_MAP_TYPE_SOCKHASH)) |
| 483 | return true; |
| 484 | |
| 485 | return false; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
| 489 | { |
| 490 | return func_id == BPF_FUNC_tcp_sock || |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 491 | func_id == BPF_FUNC_sk_fullsock || |
| 492 | func_id == BPF_FUNC_skc_to_tcp_sock || |
| 493 | func_id == BPF_FUNC_skc_to_tcp6_sock || |
| 494 | func_id == BPF_FUNC_skc_to_udp6_sock || |
| 495 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || |
| 496 | func_id == BPF_FUNC_skc_to_tcp_request_sock; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 499 | /* string representation of 'enum bpf_reg_type' */ |
| 500 | static const char * const reg_type_str[] = { |
| 501 | [NOT_INIT] = "?", |
| 502 | [SCALAR_VALUE] = "inv", |
| 503 | [PTR_TO_CTX] = "ctx", |
| 504 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 505 | [PTR_TO_MAP_VALUE] = "map_value", |
| 506 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
| 507 | [PTR_TO_STACK] = "fp", |
| 508 | [PTR_TO_PACKET] = "pkt", |
| 509 | [PTR_TO_PACKET_META] = "pkt_meta", |
| 510 | [PTR_TO_PACKET_END] = "pkt_end", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 511 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
| 512 | [PTR_TO_SOCKET] = "sock", |
| 513 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", |
| 514 | [PTR_TO_SOCK_COMMON] = "sock_common", |
| 515 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", |
| 516 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
| 517 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", |
| 518 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
| 519 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 520 | [PTR_TO_BTF_ID] = "ptr_", |
| 521 | [PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_", |
| 522 | [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", |
| 523 | [PTR_TO_MEM] = "mem", |
| 524 | [PTR_TO_MEM_OR_NULL] = "mem_or_null", |
| 525 | [PTR_TO_RDONLY_BUF] = "rdonly_buf", |
| 526 | [PTR_TO_RDONLY_BUF_OR_NULL] = "rdonly_buf_or_null", |
| 527 | [PTR_TO_RDWR_BUF] = "rdwr_buf", |
| 528 | [PTR_TO_RDWR_BUF_OR_NULL] = "rdwr_buf_or_null", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 529 | }; |
| 530 | |
| 531 | static char slot_type_char[] = { |
| 532 | [STACK_INVALID] = '?', |
| 533 | [STACK_SPILL] = 'r', |
| 534 | [STACK_MISC] = 'm', |
| 535 | [STACK_ZERO] = '0', |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 536 | }; |
| 537 | |
| 538 | static void print_liveness(struct bpf_verifier_env *env, |
| 539 | enum bpf_reg_liveness live) |
| 540 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 541 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 542 | verbose(env, "_"); |
| 543 | if (live & REG_LIVE_READ) |
| 544 | verbose(env, "r"); |
| 545 | if (live & REG_LIVE_WRITTEN) |
| 546 | verbose(env, "w"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 547 | if (live & REG_LIVE_DONE) |
| 548 | verbose(env, "D"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 552 | const struct bpf_reg_state *reg) |
| 553 | { |
| 554 | struct bpf_verifier_state *cur = env->cur_state; |
| 555 | |
| 556 | return cur->frame[reg->frameno]; |
| 557 | } |
| 558 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 559 | const char *kernel_type_name(u32 id) |
| 560 | { |
| 561 | return btf_name_by_offset(btf_vmlinux, |
| 562 | btf_type_by_id(btf_vmlinux, id)->name_off); |
| 563 | } |
| 564 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 565 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 566 | const struct bpf_func_state *state) |
| 567 | { |
| 568 | const struct bpf_reg_state *reg; |
| 569 | enum bpf_reg_type t; |
| 570 | int i; |
| 571 | |
| 572 | if (state->frameno) |
| 573 | verbose(env, " frame%d:", state->frameno); |
| 574 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 575 | reg = &state->regs[i]; |
| 576 | t = reg->type; |
| 577 | if (t == NOT_INIT) |
| 578 | continue; |
| 579 | verbose(env, " R%d", i); |
| 580 | print_liveness(env, reg->live); |
| 581 | verbose(env, "=%s", reg_type_str[t]); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 582 | if (t == SCALAR_VALUE && reg->precise) |
| 583 | verbose(env, "P"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 584 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 585 | tnum_is_const(reg->var_off)) { |
| 586 | /* reg->off should be 0 for SCALAR_VALUE */ |
| 587 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 588 | } else { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 589 | if (t == PTR_TO_BTF_ID || |
| 590 | t == PTR_TO_BTF_ID_OR_NULL || |
| 591 | t == PTR_TO_PERCPU_BTF_ID) |
| 592 | verbose(env, "%s", kernel_type_name(reg->btf_id)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 593 | verbose(env, "(id=%d", reg->id); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 594 | if (reg_type_may_be_refcounted_or_null(t)) |
| 595 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 596 | if (t != SCALAR_VALUE) |
| 597 | verbose(env, ",off=%d", reg->off); |
| 598 | if (type_is_pkt_pointer(t)) |
| 599 | verbose(env, ",r=%d", reg->range); |
| 600 | else if (t == CONST_PTR_TO_MAP || |
| 601 | t == PTR_TO_MAP_VALUE || |
| 602 | t == PTR_TO_MAP_VALUE_OR_NULL) |
| 603 | verbose(env, ",ks=%d,vs=%d", |
| 604 | reg->map_ptr->key_size, |
| 605 | reg->map_ptr->value_size); |
| 606 | if (tnum_is_const(reg->var_off)) { |
| 607 | /* Typically an immediate SCALAR_VALUE, but |
| 608 | * could be a pointer whose offset is too big |
| 609 | * for reg->off |
| 610 | */ |
| 611 | verbose(env, ",imm=%llx", reg->var_off.value); |
| 612 | } else { |
| 613 | if (reg->smin_value != reg->umin_value && |
| 614 | reg->smin_value != S64_MIN) |
| 615 | verbose(env, ",smin_value=%lld", |
| 616 | (long long)reg->smin_value); |
| 617 | if (reg->smax_value != reg->umax_value && |
| 618 | reg->smax_value != S64_MAX) |
| 619 | verbose(env, ",smax_value=%lld", |
| 620 | (long long)reg->smax_value); |
| 621 | if (reg->umin_value != 0) |
| 622 | verbose(env, ",umin_value=%llu", |
| 623 | (unsigned long long)reg->umin_value); |
| 624 | if (reg->umax_value != U64_MAX) |
| 625 | verbose(env, ",umax_value=%llu", |
| 626 | (unsigned long long)reg->umax_value); |
| 627 | if (!tnum_is_unknown(reg->var_off)) { |
| 628 | char tn_buf[48]; |
| 629 | |
| 630 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 631 | verbose(env, ",var_off=%s", tn_buf); |
| 632 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 633 | if (reg->s32_min_value != reg->smin_value && |
| 634 | reg->s32_min_value != S32_MIN) |
| 635 | verbose(env, ",s32_min_value=%d", |
| 636 | (int)(reg->s32_min_value)); |
| 637 | if (reg->s32_max_value != reg->smax_value && |
| 638 | reg->s32_max_value != S32_MAX) |
| 639 | verbose(env, ",s32_max_value=%d", |
| 640 | (int)(reg->s32_max_value)); |
| 641 | if (reg->u32_min_value != reg->umin_value && |
| 642 | reg->u32_min_value != U32_MIN) |
| 643 | verbose(env, ",u32_min_value=%d", |
| 644 | (int)(reg->u32_min_value)); |
| 645 | if (reg->u32_max_value != reg->umax_value && |
| 646 | reg->u32_max_value != U32_MAX) |
| 647 | verbose(env, ",u32_max_value=%d", |
| 648 | (int)(reg->u32_max_value)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 649 | } |
| 650 | verbose(env, ")"); |
| 651 | } |
| 652 | } |
| 653 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 654 | char types_buf[BPF_REG_SIZE + 1]; |
| 655 | bool valid = false; |
| 656 | int j; |
| 657 | |
| 658 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 659 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 660 | valid = true; |
| 661 | types_buf[j] = slot_type_char[ |
| 662 | state->stack[i].slot_type[j]]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 663 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 664 | types_buf[BPF_REG_SIZE] = 0; |
| 665 | if (!valid) |
| 666 | continue; |
| 667 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 668 | print_liveness(env, state->stack[i].spilled_ptr.live); |
| 669 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
| 670 | reg = &state->stack[i].spilled_ptr; |
| 671 | t = reg->type; |
| 672 | verbose(env, "=%s", reg_type_str[t]); |
| 673 | if (t == SCALAR_VALUE && reg->precise) |
| 674 | verbose(env, "P"); |
| 675 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) |
| 676 | verbose(env, "%lld", reg->var_off.value + reg->off); |
| 677 | } else { |
| 678 | verbose(env, "=%s", types_buf); |
| 679 | } |
| 680 | } |
| 681 | if (state->acquired_refs && state->refs[0].id) { |
| 682 | verbose(env, " refs=%d", state->refs[0].id); |
| 683 | for (i = 1; i < state->acquired_refs; i++) |
| 684 | if (state->refs[i].id) |
| 685 | verbose(env, ",%d", state->refs[i].id); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 686 | } |
| 687 | verbose(env, "\n"); |
| 688 | } |
| 689 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 690 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 691 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ |
| 692 | const struct bpf_func_state *src) \ |
| 693 | { \ |
| 694 | if (!src->FIELD) \ |
| 695 | return 0; \ |
| 696 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ |
| 697 | /* internal bug, make state invalid to reject the program */ \ |
| 698 | memset(dst, 0, sizeof(*dst)); \ |
| 699 | return -EFAULT; \ |
| 700 | } \ |
| 701 | memcpy(dst->FIELD, src->FIELD, \ |
| 702 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ |
| 703 | return 0; \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 704 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 705 | /* copy_reference_state() */ |
| 706 | COPY_STATE_FN(reference, acquired_refs, refs, 1) |
| 707 | /* copy_stack_state() */ |
| 708 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 709 | #undef COPY_STATE_FN |
| 710 | |
| 711 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 712 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ |
| 713 | bool copy_old) \ |
| 714 | { \ |
| 715 | u32 old_size = state->COUNT; \ |
| 716 | struct bpf_##NAME##_state *new_##FIELD; \ |
| 717 | int slot = size / SIZE; \ |
| 718 | \ |
| 719 | if (size <= old_size || !size) { \ |
| 720 | if (copy_old) \ |
| 721 | return 0; \ |
| 722 | state->COUNT = slot * SIZE; \ |
| 723 | if (!size && old_size) { \ |
| 724 | kfree(state->FIELD); \ |
| 725 | state->FIELD = NULL; \ |
| 726 | } \ |
| 727 | return 0; \ |
| 728 | } \ |
| 729 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ |
| 730 | GFP_KERNEL); \ |
| 731 | if (!new_##FIELD) \ |
| 732 | return -ENOMEM; \ |
| 733 | if (copy_old) { \ |
| 734 | if (state->FIELD) \ |
| 735 | memcpy(new_##FIELD, state->FIELD, \ |
| 736 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ |
| 737 | memset(new_##FIELD + old_size / SIZE, 0, \ |
| 738 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ |
| 739 | } \ |
| 740 | state->COUNT = slot * SIZE; \ |
| 741 | kfree(state->FIELD); \ |
| 742 | state->FIELD = new_##FIELD; \ |
| 743 | return 0; \ |
| 744 | } |
| 745 | /* realloc_reference_state() */ |
| 746 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) |
| 747 | /* realloc_stack_state() */ |
| 748 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 749 | #undef REALLOC_STATE_FN |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 750 | |
| 751 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 752 | * make it consume minimal amount of memory. check_stack_write() access from |
| 753 | * the program calls into realloc_func_state() to grow the stack size. |
| 754 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 755 | * which realloc_stack_state() copies over. It points to previous |
| 756 | * bpf_verifier_state which is never reallocated. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 757 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 758 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
| 759 | int refs_size, bool copy_old) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 760 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 761 | int err = realloc_reference_state(state, refs_size, copy_old); |
| 762 | if (err) |
| 763 | return err; |
| 764 | return realloc_stack_state(state, stack_size, copy_old); |
| 765 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 766 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 767 | /* Acquire a pointer id from the env and update the state->refs to include |
| 768 | * this new pointer reference. |
| 769 | * On success, returns a valid pointer id to associate with the register |
| 770 | * On failure, returns a negative errno. |
| 771 | */ |
| 772 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 773 | { |
| 774 | struct bpf_func_state *state = cur_func(env); |
| 775 | int new_ofs = state->acquired_refs; |
| 776 | int id, err; |
| 777 | |
| 778 | err = realloc_reference_state(state, state->acquired_refs + 1, true); |
| 779 | if (err) |
| 780 | return err; |
| 781 | id = ++env->id_gen; |
| 782 | state->refs[new_ofs].id = id; |
| 783 | state->refs[new_ofs].insn_idx = insn_idx; |
| 784 | |
| 785 | return id; |
| 786 | } |
| 787 | |
| 788 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
| 789 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
| 790 | { |
| 791 | int i, last_idx; |
| 792 | |
| 793 | last_idx = state->acquired_refs - 1; |
| 794 | for (i = 0; i < state->acquired_refs; i++) { |
| 795 | if (state->refs[i].id == ptr_id) { |
| 796 | if (last_idx && i != last_idx) |
| 797 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 798 | sizeof(*state->refs)); |
| 799 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 800 | state->acquired_refs--; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 801 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 802 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 803 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 804 | return -EINVAL; |
| 805 | } |
| 806 | |
| 807 | static int transfer_reference_state(struct bpf_func_state *dst, |
| 808 | struct bpf_func_state *src) |
| 809 | { |
| 810 | int err = realloc_reference_state(dst, src->acquired_refs, false); |
| 811 | if (err) |
| 812 | return err; |
| 813 | err = copy_reference_state(dst, src); |
| 814 | if (err) |
| 815 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | static void free_func_state(struct bpf_func_state *state) |
| 820 | { |
| 821 | if (!state) |
| 822 | return; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 823 | kfree(state->refs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 824 | kfree(state->stack); |
| 825 | kfree(state); |
| 826 | } |
| 827 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 828 | static void clear_jmp_history(struct bpf_verifier_state *state) |
| 829 | { |
| 830 | kfree(state->jmp_history); |
| 831 | state->jmp_history = NULL; |
| 832 | state->jmp_history_cnt = 0; |
| 833 | } |
| 834 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 835 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 836 | bool free_self) |
| 837 | { |
| 838 | int i; |
| 839 | |
| 840 | for (i = 0; i <= state->curframe; i++) { |
| 841 | free_func_state(state->frame[i]); |
| 842 | state->frame[i] = NULL; |
| 843 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 844 | clear_jmp_history(state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 845 | if (free_self) |
| 846 | kfree(state); |
| 847 | } |
| 848 | |
| 849 | /* copy verifier state from src to dst growing dst stack space |
| 850 | * when necessary to accommodate larger src stack |
| 851 | */ |
| 852 | static int copy_func_state(struct bpf_func_state *dst, |
| 853 | const struct bpf_func_state *src) |
| 854 | { |
| 855 | int err; |
| 856 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 857 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
| 858 | false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 859 | if (err) |
| 860 | return err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 861 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 862 | err = copy_reference_state(dst, src); |
| 863 | if (err) |
| 864 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 865 | return copy_stack_state(dst, src); |
| 866 | } |
| 867 | |
| 868 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 869 | const struct bpf_verifier_state *src) |
| 870 | { |
| 871 | struct bpf_func_state *dst; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 872 | u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 873 | int i, err; |
| 874 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 875 | if (dst_state->jmp_history_cnt < src->jmp_history_cnt) { |
| 876 | kfree(dst_state->jmp_history); |
| 877 | dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER); |
| 878 | if (!dst_state->jmp_history) |
| 879 | return -ENOMEM; |
| 880 | } |
| 881 | memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz); |
| 882 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
| 883 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 884 | /* if dst has more stack frames then src frame, free them */ |
| 885 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 886 | free_func_state(dst_state->frame[i]); |
| 887 | dst_state->frame[i] = NULL; |
| 888 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 889 | dst_state->speculative = src->speculative; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 890 | dst_state->curframe = src->curframe; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 891 | dst_state->active_spin_lock = src->active_spin_lock; |
| 892 | dst_state->branches = src->branches; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 893 | dst_state->parent = src->parent; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 894 | dst_state->first_insn_idx = src->first_insn_idx; |
| 895 | dst_state->last_insn_idx = src->last_insn_idx; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 896 | for (i = 0; i <= src->curframe; i++) { |
| 897 | dst = dst_state->frame[i]; |
| 898 | if (!dst) { |
| 899 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 900 | if (!dst) |
| 901 | return -ENOMEM; |
| 902 | dst_state->frame[i] = dst; |
| 903 | } |
| 904 | err = copy_func_state(dst, src->frame[i]); |
| 905 | if (err) |
| 906 | return err; |
| 907 | } |
| 908 | return 0; |
| 909 | } |
| 910 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 911 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
| 912 | { |
| 913 | while (st) { |
| 914 | u32 br = --st->branches; |
| 915 | |
| 916 | /* WARN_ON(br > 1) technically makes sense here, |
| 917 | * but see comment in push_stack(), hence: |
| 918 | */ |
| 919 | WARN_ONCE((int)br < 0, |
| 920 | "BUG update_branch_counts:branches_to_explore=%d\n", |
| 921 | br); |
| 922 | if (br) |
| 923 | break; |
| 924 | st = st->parent; |
| 925 | } |
| 926 | } |
| 927 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 928 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 929 | int *insn_idx, bool pop_log) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 930 | { |
| 931 | struct bpf_verifier_state *cur = env->cur_state; |
| 932 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 933 | int err; |
| 934 | |
| 935 | if (env->head == NULL) |
| 936 | return -ENOENT; |
| 937 | |
| 938 | if (cur) { |
| 939 | err = copy_verifier_state(cur, &head->st); |
| 940 | if (err) |
| 941 | return err; |
| 942 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 943 | if (pop_log) |
| 944 | bpf_vlog_reset(&env->log, head->log_pos); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 945 | if (insn_idx) |
| 946 | *insn_idx = head->insn_idx; |
| 947 | if (prev_insn_idx) |
| 948 | *prev_insn_idx = head->prev_insn_idx; |
| 949 | elem = head->next; |
| 950 | free_verifier_state(&head->st, false); |
| 951 | kfree(head); |
| 952 | env->head = elem; |
| 953 | env->stack_size--; |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 958 | int insn_idx, int prev_insn_idx, |
| 959 | bool speculative) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 960 | { |
| 961 | struct bpf_verifier_state *cur = env->cur_state; |
| 962 | struct bpf_verifier_stack_elem *elem; |
| 963 | int err; |
| 964 | |
| 965 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
| 966 | if (!elem) |
| 967 | goto err; |
| 968 | |
| 969 | elem->insn_idx = insn_idx; |
| 970 | elem->prev_insn_idx = prev_insn_idx; |
| 971 | elem->next = env->head; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 972 | elem->log_pos = env->log.len_used; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 973 | env->head = elem; |
| 974 | env->stack_size++; |
| 975 | err = copy_verifier_state(&elem->st, cur); |
| 976 | if (err) |
| 977 | goto err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 978 | elem->st.speculative |= speculative; |
| 979 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
| 980 | verbose(env, "The sequence of %d jumps is too complex.\n", |
| 981 | env->stack_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 982 | goto err; |
| 983 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 984 | if (elem->st.parent) { |
| 985 | ++elem->st.parent->branches; |
| 986 | /* WARN_ON(branches > 2) technically makes sense here, |
| 987 | * but |
| 988 | * 1. speculative states will bump 'branches' for non-branch |
| 989 | * instructions |
| 990 | * 2. is_state_visited() heuristics may decide not to create |
| 991 | * a new state for a sequence of branches and all such current |
| 992 | * and cloned states will be pointing to a single parent state |
| 993 | * which might have large 'branches' count. |
| 994 | */ |
| 995 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 996 | return &elem->st; |
| 997 | err: |
| 998 | free_verifier_state(env->cur_state, true); |
| 999 | env->cur_state = NULL; |
| 1000 | /* pop all elements and return */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1001 | while (!pop_stack(env, NULL, NULL, false)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1002 | return NULL; |
| 1003 | } |
| 1004 | |
| 1005 | #define CALLER_SAVED_REGS 6 |
| 1006 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 1007 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 1008 | }; |
| 1009 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1010 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 1011 | struct bpf_reg_state *reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1012 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1013 | /* This helper doesn't clear reg->id */ |
| 1014 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 1015 | { |
| 1016 | reg->var_off = tnum_const(imm); |
| 1017 | reg->smin_value = (s64)imm; |
| 1018 | reg->smax_value = (s64)imm; |
| 1019 | reg->umin_value = imm; |
| 1020 | reg->umax_value = imm; |
| 1021 | |
| 1022 | reg->s32_min_value = (s32)imm; |
| 1023 | reg->s32_max_value = (s32)imm; |
| 1024 | reg->u32_min_value = (u32)imm; |
| 1025 | reg->u32_max_value = (u32)imm; |
| 1026 | } |
| 1027 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1028 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 1029 | * known to have the value @imm. |
| 1030 | */ |
| 1031 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 1032 | { |
| 1033 | /* Clear id, off, and union(map_ptr, range) */ |
| 1034 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 1035 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1036 | ___mark_reg_known(reg, imm); |
| 1037 | } |
| 1038 | |
| 1039 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
| 1040 | { |
| 1041 | reg->var_off = tnum_const_subreg(reg->var_off, imm); |
| 1042 | reg->s32_min_value = (s32)imm; |
| 1043 | reg->s32_max_value = (s32)imm; |
| 1044 | reg->u32_min_value = (u32)imm; |
| 1045 | reg->u32_max_value = (u32)imm; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 1049 | * used only on registers holding a pointer type. |
| 1050 | */ |
| 1051 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 1052 | { |
| 1053 | __mark_reg_known(reg, 0); |
| 1054 | } |
| 1055 | |
| 1056 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 1057 | { |
| 1058 | __mark_reg_known(reg, 0); |
| 1059 | reg->type = SCALAR_VALUE; |
| 1060 | } |
| 1061 | |
| 1062 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 1063 | struct bpf_reg_state *regs, u32 regno) |
| 1064 | { |
| 1065 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
| 1066 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
| 1067 | /* Something bad happened, let's kill all regs */ |
| 1068 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1069 | __mark_reg_not_init(env, regs + regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1070 | return; |
| 1071 | } |
| 1072 | __mark_reg_known_zero(regs + regno); |
| 1073 | } |
| 1074 | |
| 1075 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 1076 | { |
| 1077 | return type_is_pkt_pointer(reg->type); |
| 1078 | } |
| 1079 | |
| 1080 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 1081 | { |
| 1082 | return reg_is_pkt_pointer(reg) || |
| 1083 | reg->type == PTR_TO_PACKET_END; |
| 1084 | } |
| 1085 | |
| 1086 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 1087 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 1088 | enum bpf_reg_type which) |
| 1089 | { |
| 1090 | /* The register can already have a range from prior markings. |
| 1091 | * This is fine as long as it hasn't been advanced from its |
| 1092 | * origin. |
| 1093 | */ |
| 1094 | return reg->type == which && |
| 1095 | reg->id == 0 && |
| 1096 | reg->off == 0 && |
| 1097 | tnum_equals_const(reg->var_off, 0); |
| 1098 | } |
| 1099 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1100 | /* Reset the min/max bounds of a register */ |
| 1101 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 1102 | { |
| 1103 | reg->smin_value = S64_MIN; |
| 1104 | reg->smax_value = S64_MAX; |
| 1105 | reg->umin_value = 0; |
| 1106 | reg->umax_value = U64_MAX; |
| 1107 | |
| 1108 | reg->s32_min_value = S32_MIN; |
| 1109 | reg->s32_max_value = S32_MAX; |
| 1110 | reg->u32_min_value = 0; |
| 1111 | reg->u32_max_value = U32_MAX; |
| 1112 | } |
| 1113 | |
| 1114 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) |
| 1115 | { |
| 1116 | reg->smin_value = S64_MIN; |
| 1117 | reg->smax_value = S64_MAX; |
| 1118 | reg->umin_value = 0; |
| 1119 | reg->umax_value = U64_MAX; |
| 1120 | } |
| 1121 | |
| 1122 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) |
| 1123 | { |
| 1124 | reg->s32_min_value = S32_MIN; |
| 1125 | reg->s32_max_value = S32_MAX; |
| 1126 | reg->u32_min_value = 0; |
| 1127 | reg->u32_max_value = U32_MAX; |
| 1128 | } |
| 1129 | |
| 1130 | static void __update_reg32_bounds(struct bpf_reg_state *reg) |
| 1131 | { |
| 1132 | struct tnum var32_off = tnum_subreg(reg->var_off); |
| 1133 | |
| 1134 | /* min signed is max(sign bit) | min(other bits) */ |
| 1135 | reg->s32_min_value = max_t(s32, reg->s32_min_value, |
| 1136 | var32_off.value | (var32_off.mask & S32_MIN)); |
| 1137 | /* max signed is min(sign bit) | max(other bits) */ |
| 1138 | reg->s32_max_value = min_t(s32, reg->s32_max_value, |
| 1139 | var32_off.value | (var32_off.mask & S32_MAX)); |
| 1140 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); |
| 1141 | reg->u32_max_value = min(reg->u32_max_value, |
| 1142 | (u32)(var32_off.value | var32_off.mask)); |
| 1143 | } |
| 1144 | |
| 1145 | static void __update_reg64_bounds(struct bpf_reg_state *reg) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1146 | { |
| 1147 | /* min signed is max(sign bit) | min(other bits) */ |
| 1148 | reg->smin_value = max_t(s64, reg->smin_value, |
| 1149 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 1150 | /* max signed is min(sign bit) | max(other bits) */ |
| 1151 | reg->smax_value = min_t(s64, reg->smax_value, |
| 1152 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 1153 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 1154 | reg->umax_value = min(reg->umax_value, |
| 1155 | reg->var_off.value | reg->var_off.mask); |
| 1156 | } |
| 1157 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1158 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 1159 | { |
| 1160 | __update_reg32_bounds(reg); |
| 1161 | __update_reg64_bounds(reg); |
| 1162 | } |
| 1163 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1164 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1165 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
| 1166 | { |
| 1167 | /* Learn sign from signed bounds. |
| 1168 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 1169 | * are the same, so combine. This works even in the negative case, e.g. |
| 1170 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 1171 | */ |
| 1172 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { |
| 1173 | reg->s32_min_value = reg->u32_min_value = |
| 1174 | max_t(u32, reg->s32_min_value, reg->u32_min_value); |
| 1175 | reg->s32_max_value = reg->u32_max_value = |
| 1176 | min_t(u32, reg->s32_max_value, reg->u32_max_value); |
| 1177 | return; |
| 1178 | } |
| 1179 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 1180 | * boundary, so we must be careful. |
| 1181 | */ |
| 1182 | if ((s32)reg->u32_max_value >= 0) { |
| 1183 | /* Positive. We can't learn anything from the smin, but smax |
| 1184 | * is positive, hence safe. |
| 1185 | */ |
| 1186 | reg->s32_min_value = reg->u32_min_value; |
| 1187 | reg->s32_max_value = reg->u32_max_value = |
| 1188 | min_t(u32, reg->s32_max_value, reg->u32_max_value); |
| 1189 | } else if ((s32)reg->u32_min_value < 0) { |
| 1190 | /* Negative. We can't learn anything from the smax, but smin |
| 1191 | * is negative, hence safe. |
| 1192 | */ |
| 1193 | reg->s32_min_value = reg->u32_min_value = |
| 1194 | max_t(u32, reg->s32_min_value, reg->u32_min_value); |
| 1195 | reg->s32_max_value = reg->u32_max_value; |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1200 | { |
| 1201 | /* Learn sign from signed bounds. |
| 1202 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 1203 | * are the same, so combine. This works even in the negative case, e.g. |
| 1204 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 1205 | */ |
| 1206 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 1207 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1208 | reg->umin_value); |
| 1209 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1210 | reg->umax_value); |
| 1211 | return; |
| 1212 | } |
| 1213 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 1214 | * boundary, so we must be careful. |
| 1215 | */ |
| 1216 | if ((s64)reg->umax_value >= 0) { |
| 1217 | /* Positive. We can't learn anything from the smin, but smax |
| 1218 | * is positive, hence safe. |
| 1219 | */ |
| 1220 | reg->smin_value = reg->umin_value; |
| 1221 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1222 | reg->umax_value); |
| 1223 | } else if ((s64)reg->umin_value < 0) { |
| 1224 | /* Negative. We can't learn anything from the smax, but smin |
| 1225 | * is negative, hence safe. |
| 1226 | */ |
| 1227 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1228 | reg->umin_value); |
| 1229 | reg->smax_value = reg->umax_value; |
| 1230 | } |
| 1231 | } |
| 1232 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1233 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 1234 | { |
| 1235 | __reg32_deduce_bounds(reg); |
| 1236 | __reg64_deduce_bounds(reg); |
| 1237 | } |
| 1238 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1239 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 1240 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 1241 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1242 | struct tnum var64_off = tnum_intersect(reg->var_off, |
| 1243 | tnum_range(reg->umin_value, |
| 1244 | reg->umax_value)); |
| 1245 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), |
| 1246 | tnum_range(reg->u32_min_value, |
| 1247 | reg->u32_max_value)); |
| 1248 | |
| 1249 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1252 | static bool __reg32_bound_s64(s32 a) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1253 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1254 | return a >= 0 && a <= S32_MAX; |
| 1255 | } |
| 1256 | |
| 1257 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
| 1258 | { |
| 1259 | reg->umin_value = reg->u32_min_value; |
| 1260 | reg->umax_value = reg->u32_max_value; |
| 1261 | |
| 1262 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds but must |
| 1263 | * be positive otherwise set to worse case bounds and refine later |
| 1264 | * from tnum. |
| 1265 | */ |
| 1266 | if (__reg32_bound_s64(reg->s32_min_value) && |
| 1267 | __reg32_bound_s64(reg->s32_max_value)) { |
| 1268 | reg->smin_value = reg->s32_min_value; |
| 1269 | reg->smax_value = reg->s32_max_value; |
| 1270 | } else { |
| 1271 | reg->smin_value = 0; |
| 1272 | reg->smax_value = U32_MAX; |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) |
| 1277 | { |
| 1278 | /* special case when 64-bit register has upper 32-bit register |
| 1279 | * zeroed. Typically happens after zext or <<32, >>32 sequence |
| 1280 | * allowing us to use 32-bit bounds directly, |
| 1281 | */ |
| 1282 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { |
| 1283 | __reg_assign_32_into_64(reg); |
| 1284 | } else { |
| 1285 | /* Otherwise the best we can do is push lower 32bit known and |
| 1286 | * unknown bits into register (var_off set from jmp logic) |
| 1287 | * then learn as much as possible from the 64-bit tnum |
| 1288 | * known and unknown bits. The previous smin/smax bounds are |
| 1289 | * invalid here because of jmp32 compare so mark them unknown |
| 1290 | * so they do not impact tnum bounds calculation. |
| 1291 | */ |
| 1292 | __mark_reg64_unbounded(reg); |
| 1293 | __update_reg_bounds(reg); |
| 1294 | } |
| 1295 | |
| 1296 | /* Intersecting with the old var_off might have improved our bounds |
| 1297 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 1298 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 1299 | */ |
| 1300 | __reg_deduce_bounds(reg); |
| 1301 | __reg_bound_offset(reg); |
| 1302 | __update_reg_bounds(reg); |
| 1303 | } |
| 1304 | |
| 1305 | static bool __reg64_bound_s32(s64 a) |
| 1306 | { |
| 1307 | return a >= S32_MIN && a <= S32_MAX; |
| 1308 | } |
| 1309 | |
| 1310 | static bool __reg64_bound_u32(u64 a) |
| 1311 | { |
| 1312 | return a >= U32_MIN && a <= U32_MAX; |
| 1313 | } |
| 1314 | |
| 1315 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) |
| 1316 | { |
| 1317 | __mark_reg32_unbounded(reg); |
| 1318 | |
| 1319 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
| 1320 | reg->s32_min_value = (s32)reg->smin_value; |
| 1321 | reg->s32_max_value = (s32)reg->smax_value; |
| 1322 | } |
| 1323 | if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value)) { |
| 1324 | reg->u32_min_value = (u32)reg->umin_value; |
| 1325 | reg->u32_max_value = (u32)reg->umax_value; |
| 1326 | } |
| 1327 | |
| 1328 | /* Intersecting with the old var_off might have improved our bounds |
| 1329 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 1330 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 1331 | */ |
| 1332 | __reg_deduce_bounds(reg); |
| 1333 | __reg_bound_offset(reg); |
| 1334 | __update_reg_bounds(reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /* Mark a register as having a completely unknown (scalar) value. */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1338 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
| 1339 | struct bpf_reg_state *reg) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1340 | { |
| 1341 | /* |
| 1342 | * Clear type, id, off, and union(map_ptr, range) and |
| 1343 | * padding between 'type' and union |
| 1344 | */ |
| 1345 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
| 1346 | reg->type = SCALAR_VALUE; |
| 1347 | reg->var_off = tnum_unknown; |
| 1348 | reg->frameno = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1349 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1350 | __mark_reg_unbounded(reg); |
| 1351 | } |
| 1352 | |
| 1353 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 1354 | struct bpf_reg_state *regs, u32 regno) |
| 1355 | { |
| 1356 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
| 1357 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
| 1358 | /* Something bad happened, let's kill all regs except FP */ |
| 1359 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1360 | __mark_reg_not_init(env, regs + regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1361 | return; |
| 1362 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1363 | __mark_reg_unknown(env, regs + regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1366 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 1367 | struct bpf_reg_state *reg) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1368 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1369 | __mark_reg_unknown(env, reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1370 | reg->type = NOT_INIT; |
| 1371 | } |
| 1372 | |
| 1373 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 1374 | struct bpf_reg_state *regs, u32 regno) |
| 1375 | { |
| 1376 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
| 1377 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
| 1378 | /* Something bad happened, let's kill all regs except FP */ |
| 1379 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1380 | __mark_reg_not_init(env, regs + regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1381 | return; |
| 1382 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1383 | __mark_reg_not_init(env, regs + regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1386 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
| 1387 | struct bpf_reg_state *regs, u32 regno, |
| 1388 | enum bpf_reg_type reg_type, u32 btf_id) |
| 1389 | { |
| 1390 | if (reg_type == SCALAR_VALUE) { |
| 1391 | mark_reg_unknown(env, regs, regno); |
| 1392 | return; |
| 1393 | } |
| 1394 | mark_reg_known_zero(env, regs, regno); |
| 1395 | regs[regno].type = PTR_TO_BTF_ID; |
| 1396 | regs[regno].btf_id = btf_id; |
| 1397 | } |
| 1398 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1399 | #define DEF_NOT_SUBREG (0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1400 | static void init_reg_state(struct bpf_verifier_env *env, |
| 1401 | struct bpf_func_state *state) |
| 1402 | { |
| 1403 | struct bpf_reg_state *regs = state->regs; |
| 1404 | int i; |
| 1405 | |
| 1406 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 1407 | mark_reg_not_init(env, regs, i); |
| 1408 | regs[i].live = REG_LIVE_NONE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1409 | regs[i].parent = NULL; |
| 1410 | regs[i].subreg_def = DEF_NOT_SUBREG; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | /* frame pointer */ |
| 1414 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
| 1415 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
| 1416 | regs[BPF_REG_FP].frameno = state->frameno; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | #define BPF_MAIN_FUNC (-1) |
| 1420 | static void init_func_state(struct bpf_verifier_env *env, |
| 1421 | struct bpf_func_state *state, |
| 1422 | int callsite, int frameno, int subprogno) |
| 1423 | { |
| 1424 | state->callsite = callsite; |
| 1425 | state->frameno = frameno; |
| 1426 | state->subprogno = subprogno; |
| 1427 | init_reg_state(env, state); |
| 1428 | } |
| 1429 | |
| 1430 | enum reg_arg_type { |
| 1431 | SRC_OP, /* register is used as source operand */ |
| 1432 | DST_OP, /* register is used as destination operand */ |
| 1433 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 1434 | }; |
| 1435 | |
| 1436 | static int cmp_subprogs(const void *a, const void *b) |
| 1437 | { |
| 1438 | return ((struct bpf_subprog_info *)a)->start - |
| 1439 | ((struct bpf_subprog_info *)b)->start; |
| 1440 | } |
| 1441 | |
| 1442 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 1443 | { |
| 1444 | struct bpf_subprog_info *p; |
| 1445 | |
| 1446 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 1447 | sizeof(env->subprog_info[0]), cmp_subprogs); |
| 1448 | if (!p) |
| 1449 | return -ENOENT; |
| 1450 | return p - env->subprog_info; |
| 1451 | |
| 1452 | } |
| 1453 | |
| 1454 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 1455 | { |
| 1456 | int insn_cnt = env->prog->len; |
| 1457 | int ret; |
| 1458 | |
| 1459 | if (off >= insn_cnt || off < 0) { |
| 1460 | verbose(env, "call to invalid destination\n"); |
| 1461 | return -EINVAL; |
| 1462 | } |
| 1463 | ret = find_subprog(env, off); |
| 1464 | if (ret >= 0) |
| 1465 | return 0; |
| 1466 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
| 1467 | verbose(env, "too many subprograms\n"); |
| 1468 | return -E2BIG; |
| 1469 | } |
| 1470 | env->subprog_info[env->subprog_cnt++].start = off; |
| 1471 | sort(env->subprog_info, env->subprog_cnt, |
| 1472 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
| 1473 | return 0; |
| 1474 | } |
| 1475 | |
| 1476 | static int check_subprogs(struct bpf_verifier_env *env) |
| 1477 | { |
| 1478 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
| 1479 | struct bpf_subprog_info *subprog = env->subprog_info; |
| 1480 | struct bpf_insn *insn = env->prog->insnsi; |
| 1481 | int insn_cnt = env->prog->len; |
| 1482 | |
| 1483 | /* Add entry function. */ |
| 1484 | ret = add_subprog(env, 0); |
| 1485 | if (ret < 0) |
| 1486 | return ret; |
| 1487 | |
| 1488 | /* determine subprog starts. The end is one before the next starts */ |
| 1489 | for (i = 0; i < insn_cnt; i++) { |
| 1490 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1491 | continue; |
| 1492 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1493 | continue; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1494 | if (!env->bpf_capable) { |
| 1495 | verbose(env, |
| 1496 | "function calls to other bpf functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1497 | return -EPERM; |
| 1498 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1499 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 1500 | if (ret < 0) |
| 1501 | return ret; |
| 1502 | } |
| 1503 | |
| 1504 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 1505 | * logic. 'subprog_cnt' should not be increased. |
| 1506 | */ |
| 1507 | subprog[env->subprog_cnt].start = insn_cnt; |
| 1508 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1509 | if (env->log.level & BPF_LOG_LEVEL2) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1510 | for (i = 0; i < env->subprog_cnt; i++) |
| 1511 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
| 1512 | |
| 1513 | /* now check that all jumps are within the same subprog */ |
| 1514 | subprog_start = subprog[cur_subprog].start; |
| 1515 | subprog_end = subprog[cur_subprog + 1].start; |
| 1516 | for (i = 0; i < insn_cnt; i++) { |
| 1517 | u8 code = insn[i].code; |
| 1518 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1519 | if (code == (BPF_JMP | BPF_CALL) && |
| 1520 | insn[i].imm == BPF_FUNC_tail_call && |
| 1521 | insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1522 | subprog[cur_subprog].has_tail_call = true; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1523 | if (BPF_CLASS(code) == BPF_LD && |
| 1524 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) |
| 1525 | subprog[cur_subprog].has_ld_abs = true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1526 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1527 | goto next; |
| 1528 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 1529 | goto next; |
| 1530 | off = i + insn[i].off + 1; |
| 1531 | if (off < subprog_start || off >= subprog_end) { |
| 1532 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 1533 | return -EINVAL; |
| 1534 | } |
| 1535 | next: |
| 1536 | if (i == subprog_end - 1) { |
| 1537 | /* to avoid fall-through from one subprog into another |
| 1538 | * the last insn of the subprog should be either exit |
| 1539 | * or unconditional jump back |
| 1540 | */ |
| 1541 | if (code != (BPF_JMP | BPF_EXIT) && |
| 1542 | code != (BPF_JMP | BPF_JA)) { |
| 1543 | verbose(env, "last insn is not an exit or jmp\n"); |
| 1544 | return -EINVAL; |
| 1545 | } |
| 1546 | subprog_start = subprog_end; |
| 1547 | cur_subprog++; |
| 1548 | if (cur_subprog < env->subprog_cnt) |
| 1549 | subprog_end = subprog[cur_subprog + 1].start; |
| 1550 | } |
| 1551 | } |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1555 | /* Parentage chain of this register (or stack slot) should take care of all |
| 1556 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 1557 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1558 | static int mark_reg_read(struct bpf_verifier_env *env, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1559 | const struct bpf_reg_state *state, |
| 1560 | struct bpf_reg_state *parent, u8 flag) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1561 | { |
| 1562 | bool writes = parent == state->parent; /* Observe write marks */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1563 | int cnt = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1564 | |
| 1565 | while (parent) { |
| 1566 | /* if read wasn't screened by an earlier write ... */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1567 | if (writes && state->live & REG_LIVE_WRITTEN) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1568 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1569 | if (parent->live & REG_LIVE_DONE) { |
| 1570 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", |
| 1571 | reg_type_str[parent->type], |
| 1572 | parent->var_off.value, parent->off); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1573 | return -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1574 | } |
| 1575 | /* The first condition is more likely to be true than the |
| 1576 | * second, checked it first. |
| 1577 | */ |
| 1578 | if ((parent->live & REG_LIVE_READ) == flag || |
| 1579 | parent->live & REG_LIVE_READ64) |
| 1580 | /* The parentage chain never changes and |
| 1581 | * this parent was already marked as LIVE_READ. |
| 1582 | * There is no need to keep walking the chain again and |
| 1583 | * keep re-marking all parents as LIVE_READ. |
| 1584 | * This case happens when the same register is read |
| 1585 | * multiple times without writes into it in-between. |
| 1586 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
| 1587 | * then no need to set the weak REG_LIVE_READ32. |
| 1588 | */ |
| 1589 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1590 | /* ... then we depend on parent's value */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1591 | parent->live |= flag; |
| 1592 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ |
| 1593 | if (flag == REG_LIVE_READ64) |
| 1594 | parent->live &= ~REG_LIVE_READ32; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1595 | state = parent; |
| 1596 | parent = state->parent; |
| 1597 | writes = true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1598 | cnt++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1599 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1600 | |
| 1601 | if (env->longest_mark_read_walk < cnt) |
| 1602 | env->longest_mark_read_walk = cnt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1603 | return 0; |
| 1604 | } |
| 1605 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1606 | /* This function is supposed to be used by the following 32-bit optimization |
| 1607 | * code only. It returns TRUE if the source or destination register operates |
| 1608 | * on 64-bit, otherwise return FALSE. |
| 1609 | */ |
| 1610 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 1611 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) |
| 1612 | { |
| 1613 | u8 code, class, op; |
| 1614 | |
| 1615 | code = insn->code; |
| 1616 | class = BPF_CLASS(code); |
| 1617 | op = BPF_OP(code); |
| 1618 | if (class == BPF_JMP) { |
| 1619 | /* BPF_EXIT for "main" will reach here. Return TRUE |
| 1620 | * conservatively. |
| 1621 | */ |
| 1622 | if (op == BPF_EXIT) |
| 1623 | return true; |
| 1624 | if (op == BPF_CALL) { |
| 1625 | /* BPF to BPF call will reach here because of marking |
| 1626 | * caller saved clobber with DST_OP_NO_MARK for which we |
| 1627 | * don't care the register def because they are anyway |
| 1628 | * marked as NOT_INIT already. |
| 1629 | */ |
| 1630 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1631 | return false; |
| 1632 | /* Helper call will reach here because of arg type |
| 1633 | * check, conservatively return TRUE. |
| 1634 | */ |
| 1635 | if (t == SRC_OP) |
| 1636 | return true; |
| 1637 | |
| 1638 | return false; |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | if (class == BPF_ALU64 || class == BPF_JMP || |
| 1643 | /* BPF_END always use BPF_ALU class. */ |
| 1644 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) |
| 1645 | return true; |
| 1646 | |
| 1647 | if (class == BPF_ALU || class == BPF_JMP32) |
| 1648 | return false; |
| 1649 | |
| 1650 | if (class == BPF_LDX) { |
| 1651 | if (t != SRC_OP) |
| 1652 | return BPF_SIZE(code) == BPF_DW; |
| 1653 | /* LDX source must be ptr. */ |
| 1654 | return true; |
| 1655 | } |
| 1656 | |
| 1657 | if (class == BPF_STX) { |
| 1658 | if (reg->type != SCALAR_VALUE) |
| 1659 | return true; |
| 1660 | return BPF_SIZE(code) == BPF_DW; |
| 1661 | } |
| 1662 | |
| 1663 | if (class == BPF_LD) { |
| 1664 | u8 mode = BPF_MODE(code); |
| 1665 | |
| 1666 | /* LD_IMM64 */ |
| 1667 | if (mode == BPF_IMM) |
| 1668 | return true; |
| 1669 | |
| 1670 | /* Both LD_IND and LD_ABS return 32-bit data. */ |
| 1671 | if (t != SRC_OP) |
| 1672 | return false; |
| 1673 | |
| 1674 | /* Implicit ctx ptr. */ |
| 1675 | if (regno == BPF_REG_6) |
| 1676 | return true; |
| 1677 | |
| 1678 | /* Explicit source could be any width. */ |
| 1679 | return true; |
| 1680 | } |
| 1681 | |
| 1682 | if (class == BPF_ST) |
| 1683 | /* The only source register for BPF_ST is a ptr. */ |
| 1684 | return true; |
| 1685 | |
| 1686 | /* Conservatively return true at default. */ |
| 1687 | return true; |
| 1688 | } |
| 1689 | |
| 1690 | /* Return TRUE if INSN doesn't have explicit value define. */ |
| 1691 | static bool insn_no_def(struct bpf_insn *insn) |
| 1692 | { |
| 1693 | u8 class = BPF_CLASS(insn->code); |
| 1694 | |
| 1695 | return (class == BPF_JMP || class == BPF_JMP32 || |
| 1696 | class == BPF_STX || class == BPF_ST); |
| 1697 | } |
| 1698 | |
| 1699 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ |
| 1700 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 1701 | { |
| 1702 | if (insn_no_def(insn)) |
| 1703 | return false; |
| 1704 | |
| 1705 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); |
| 1706 | } |
| 1707 | |
| 1708 | static void mark_insn_zext(struct bpf_verifier_env *env, |
| 1709 | struct bpf_reg_state *reg) |
| 1710 | { |
| 1711 | s32 def_idx = reg->subreg_def; |
| 1712 | |
| 1713 | if (def_idx == DEF_NOT_SUBREG) |
| 1714 | return; |
| 1715 | |
| 1716 | env->insn_aux_data[def_idx - 1].zext_dst = true; |
| 1717 | /* The dst will be zero extended, so won't be sub-register anymore. */ |
| 1718 | reg->subreg_def = DEF_NOT_SUBREG; |
| 1719 | } |
| 1720 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1721 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
| 1722 | enum reg_arg_type t) |
| 1723 | { |
| 1724 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1725 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1726 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
| 1727 | struct bpf_reg_state *reg, *regs = state->regs; |
| 1728 | bool rw64; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1729 | |
| 1730 | if (regno >= MAX_BPF_REG) { |
| 1731 | verbose(env, "R%d is invalid\n", regno); |
| 1732 | return -EINVAL; |
| 1733 | } |
| 1734 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1735 | reg = ®s[regno]; |
| 1736 | rw64 = is_reg64(env, insn, regno, reg, t); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1737 | if (t == SRC_OP) { |
| 1738 | /* check whether register used as source operand can be read */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1739 | if (reg->type == NOT_INIT) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1740 | verbose(env, "R%d !read_ok\n", regno); |
| 1741 | return -EACCES; |
| 1742 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1743 | /* We don't need to worry about FP liveness because it's read-only */ |
| 1744 | if (regno == BPF_REG_FP) |
| 1745 | return 0; |
| 1746 | |
| 1747 | if (rw64) |
| 1748 | mark_insn_zext(env, reg); |
| 1749 | |
| 1750 | return mark_reg_read(env, reg, reg->parent, |
| 1751 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1752 | } else { |
| 1753 | /* check whether register used as dest operand can be written to */ |
| 1754 | if (regno == BPF_REG_FP) { |
| 1755 | verbose(env, "frame pointer is read only\n"); |
| 1756 | return -EACCES; |
| 1757 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1758 | reg->live |= REG_LIVE_WRITTEN; |
| 1759 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1760 | if (t == DST_OP) |
| 1761 | mark_reg_unknown(env, regs, regno); |
| 1762 | } |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1766 | /* for any branch, call, exit record the history of jmps in the given state */ |
| 1767 | static int push_jmp_history(struct bpf_verifier_env *env, |
| 1768 | struct bpf_verifier_state *cur) |
| 1769 | { |
| 1770 | u32 cnt = cur->jmp_history_cnt; |
| 1771 | struct bpf_idx_pair *p; |
| 1772 | |
| 1773 | cnt++; |
| 1774 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); |
| 1775 | if (!p) |
| 1776 | return -ENOMEM; |
| 1777 | p[cnt - 1].idx = env->insn_idx; |
| 1778 | p[cnt - 1].prev_idx = env->prev_insn_idx; |
| 1779 | cur->jmp_history = p; |
| 1780 | cur->jmp_history_cnt = cnt; |
| 1781 | return 0; |
| 1782 | } |
| 1783 | |
| 1784 | /* Backtrack one insn at a time. If idx is not at the top of recorded |
| 1785 | * history then previous instruction came from straight line execution. |
| 1786 | */ |
| 1787 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, |
| 1788 | u32 *history) |
| 1789 | { |
| 1790 | u32 cnt = *history; |
| 1791 | |
| 1792 | if (cnt && st->jmp_history[cnt - 1].idx == i) { |
| 1793 | i = st->jmp_history[cnt - 1].prev_idx; |
| 1794 | (*history)--; |
| 1795 | } else { |
| 1796 | i--; |
| 1797 | } |
| 1798 | return i; |
| 1799 | } |
| 1800 | |
| 1801 | /* For given verifier state backtrack_insn() is called from the last insn to |
| 1802 | * the first insn. Its purpose is to compute a bitmask of registers and |
| 1803 | * stack slots that needs precision in the parent verifier state. |
| 1804 | */ |
| 1805 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, |
| 1806 | u32 *reg_mask, u64 *stack_mask) |
| 1807 | { |
| 1808 | const struct bpf_insn_cbs cbs = { |
| 1809 | .cb_print = verbose, |
| 1810 | .private_data = env, |
| 1811 | }; |
| 1812 | struct bpf_insn *insn = env->prog->insnsi + idx; |
| 1813 | u8 class = BPF_CLASS(insn->code); |
| 1814 | u8 opcode = BPF_OP(insn->code); |
| 1815 | u8 mode = BPF_MODE(insn->code); |
| 1816 | u32 dreg = 1u << insn->dst_reg; |
| 1817 | u32 sreg = 1u << insn->src_reg; |
| 1818 | u32 spi; |
| 1819 | |
| 1820 | if (insn->code == 0) |
| 1821 | return 0; |
| 1822 | if (env->log.level & BPF_LOG_LEVEL) { |
| 1823 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
| 1824 | verbose(env, "%d: ", idx); |
| 1825 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
| 1826 | } |
| 1827 | |
| 1828 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 1829 | if (!(*reg_mask & dreg)) |
| 1830 | return 0; |
| 1831 | if (opcode == BPF_MOV) { |
| 1832 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1833 | /* dreg = sreg |
| 1834 | * dreg needs precision after this insn |
| 1835 | * sreg needs precision before this insn |
| 1836 | */ |
| 1837 | *reg_mask &= ~dreg; |
| 1838 | *reg_mask |= sreg; |
| 1839 | } else { |
| 1840 | /* dreg = K |
| 1841 | * dreg needs precision after this insn. |
| 1842 | * Corresponding register is already marked |
| 1843 | * as precise=true in this verifier state. |
| 1844 | * No further markings in parent are necessary |
| 1845 | */ |
| 1846 | *reg_mask &= ~dreg; |
| 1847 | } |
| 1848 | } else { |
| 1849 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1850 | /* dreg += sreg |
| 1851 | * both dreg and sreg need precision |
| 1852 | * before this insn |
| 1853 | */ |
| 1854 | *reg_mask |= sreg; |
| 1855 | } /* else dreg += K |
| 1856 | * dreg still needs precision before this insn |
| 1857 | */ |
| 1858 | } |
| 1859 | } else if (class == BPF_LDX) { |
| 1860 | if (!(*reg_mask & dreg)) |
| 1861 | return 0; |
| 1862 | *reg_mask &= ~dreg; |
| 1863 | |
| 1864 | /* scalars can only be spilled into stack w/o losing precision. |
| 1865 | * Load from any other memory can be zero extended. |
| 1866 | * The desire to keep that precision is already indicated |
| 1867 | * by 'precise' mark in corresponding register of this state. |
| 1868 | * No further tracking necessary. |
| 1869 | */ |
| 1870 | if (insn->src_reg != BPF_REG_FP) |
| 1871 | return 0; |
| 1872 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 1873 | return 0; |
| 1874 | |
| 1875 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. |
| 1876 | * that [fp - off] slot contains scalar that needs to be |
| 1877 | * tracked with precision |
| 1878 | */ |
| 1879 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 1880 | if (spi >= 64) { |
| 1881 | verbose(env, "BUG spi %d\n", spi); |
| 1882 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1883 | return -EFAULT; |
| 1884 | } |
| 1885 | *stack_mask |= 1ull << spi; |
| 1886 | } else if (class == BPF_STX || class == BPF_ST) { |
| 1887 | if (*reg_mask & dreg) |
| 1888 | /* stx & st shouldn't be using _scalar_ dst_reg |
| 1889 | * to access memory. It means backtracking |
| 1890 | * encountered a case of pointer subtraction. |
| 1891 | */ |
| 1892 | return -ENOTSUPP; |
| 1893 | /* scalars can only be spilled into stack */ |
| 1894 | if (insn->dst_reg != BPF_REG_FP) |
| 1895 | return 0; |
| 1896 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 1897 | return 0; |
| 1898 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 1899 | if (spi >= 64) { |
| 1900 | verbose(env, "BUG spi %d\n", spi); |
| 1901 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1902 | return -EFAULT; |
| 1903 | } |
| 1904 | if (!(*stack_mask & (1ull << spi))) |
| 1905 | return 0; |
| 1906 | *stack_mask &= ~(1ull << spi); |
| 1907 | if (class == BPF_STX) |
| 1908 | *reg_mask |= sreg; |
| 1909 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
| 1910 | if (opcode == BPF_CALL) { |
| 1911 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1912 | return -ENOTSUPP; |
| 1913 | /* regular helper call sets R0 */ |
| 1914 | *reg_mask &= ~1; |
| 1915 | if (*reg_mask & 0x3f) { |
| 1916 | /* if backtracing was looking for registers R1-R5 |
| 1917 | * they should have been found already. |
| 1918 | */ |
| 1919 | verbose(env, "BUG regs %x\n", *reg_mask); |
| 1920 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1921 | return -EFAULT; |
| 1922 | } |
| 1923 | } else if (opcode == BPF_EXIT) { |
| 1924 | return -ENOTSUPP; |
| 1925 | } |
| 1926 | } else if (class == BPF_LD) { |
| 1927 | if (!(*reg_mask & dreg)) |
| 1928 | return 0; |
| 1929 | *reg_mask &= ~dreg; |
| 1930 | /* It's ld_imm64 or ld_abs or ld_ind. |
| 1931 | * For ld_imm64 no further tracking of precision |
| 1932 | * into parent is necessary |
| 1933 | */ |
| 1934 | if (mode == BPF_IND || mode == BPF_ABS) |
| 1935 | /* to be analyzed */ |
| 1936 | return -ENOTSUPP; |
| 1937 | } |
| 1938 | return 0; |
| 1939 | } |
| 1940 | |
| 1941 | /* the scalar precision tracking algorithm: |
| 1942 | * . at the start all registers have precise=false. |
| 1943 | * . scalar ranges are tracked as normal through alu and jmp insns. |
| 1944 | * . once precise value of the scalar register is used in: |
| 1945 | * . ptr + scalar alu |
| 1946 | * . if (scalar cond K|scalar) |
| 1947 | * . helper_call(.., scalar, ...) where ARG_CONST is expected |
| 1948 | * backtrack through the verifier states and mark all registers and |
| 1949 | * stack slots with spilled constants that these scalar regisers |
| 1950 | * should be precise. |
| 1951 | * . during state pruning two registers (or spilled stack slots) |
| 1952 | * are equivalent if both are not precise. |
| 1953 | * |
| 1954 | * Note the verifier cannot simply walk register parentage chain, |
| 1955 | * since many different registers and stack slots could have been |
| 1956 | * used to compute single precise scalar. |
| 1957 | * |
| 1958 | * The approach of starting with precise=true for all registers and then |
| 1959 | * backtrack to mark a register as not precise when the verifier detects |
| 1960 | * that program doesn't care about specific value (e.g., when helper |
| 1961 | * takes register as ARG_ANYTHING parameter) is not safe. |
| 1962 | * |
| 1963 | * It's ok to walk single parentage chain of the verifier states. |
| 1964 | * It's possible that this backtracking will go all the way till 1st insn. |
| 1965 | * All other branches will be explored for needing precision later. |
| 1966 | * |
| 1967 | * The backtracking needs to deal with cases like: |
| 1968 | * 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) |
| 1969 | * r9 -= r8 |
| 1970 | * r5 = r9 |
| 1971 | * if r5 > 0x79f goto pc+7 |
| 1972 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) |
| 1973 | * r5 += 1 |
| 1974 | * ... |
| 1975 | * call bpf_perf_event_output#25 |
| 1976 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO |
| 1977 | * |
| 1978 | * and this case: |
| 1979 | * r6 = 1 |
| 1980 | * call foo // uses callee's r6 inside to compute r0 |
| 1981 | * r0 += r6 |
| 1982 | * if r0 == 0 goto |
| 1983 | * |
| 1984 | * to track above reg_mask/stack_mask needs to be independent for each frame. |
| 1985 | * |
| 1986 | * Also if parent's curframe > frame where backtracking started, |
| 1987 | * the verifier need to mark registers in both frames, otherwise callees |
| 1988 | * may incorrectly prune callers. This is similar to |
| 1989 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") |
| 1990 | * |
| 1991 | * For now backtracking falls back into conservative marking. |
| 1992 | */ |
| 1993 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, |
| 1994 | struct bpf_verifier_state *st) |
| 1995 | { |
| 1996 | struct bpf_func_state *func; |
| 1997 | struct bpf_reg_state *reg; |
| 1998 | int i, j; |
| 1999 | |
| 2000 | /* big hammer: mark all scalars precise in this path. |
| 2001 | * pop_stack may still get !precise scalars. |
| 2002 | */ |
| 2003 | for (; st; st = st->parent) |
| 2004 | for (i = 0; i <= st->curframe; i++) { |
| 2005 | func = st->frame[i]; |
| 2006 | for (j = 0; j < BPF_REG_FP; j++) { |
| 2007 | reg = &func->regs[j]; |
| 2008 | if (reg->type != SCALAR_VALUE) |
| 2009 | continue; |
| 2010 | reg->precise = true; |
| 2011 | } |
| 2012 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { |
| 2013 | if (func->stack[j].slot_type[0] != STACK_SPILL) |
| 2014 | continue; |
| 2015 | reg = &func->stack[j].spilled_ptr; |
| 2016 | if (reg->type != SCALAR_VALUE) |
| 2017 | continue; |
| 2018 | reg->precise = true; |
| 2019 | } |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
| 2024 | int spi) |
| 2025 | { |
| 2026 | struct bpf_verifier_state *st = env->cur_state; |
| 2027 | int first_idx = st->first_insn_idx; |
| 2028 | int last_idx = env->insn_idx; |
| 2029 | struct bpf_func_state *func; |
| 2030 | struct bpf_reg_state *reg; |
| 2031 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
| 2032 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; |
| 2033 | bool skip_first = true; |
| 2034 | bool new_marks = false; |
| 2035 | int i, err; |
| 2036 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2037 | if (!env->bpf_capable) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2038 | return 0; |
| 2039 | |
| 2040 | func = st->frame[st->curframe]; |
| 2041 | if (regno >= 0) { |
| 2042 | reg = &func->regs[regno]; |
| 2043 | if (reg->type != SCALAR_VALUE) { |
| 2044 | WARN_ONCE(1, "backtracing misuse"); |
| 2045 | return -EFAULT; |
| 2046 | } |
| 2047 | if (!reg->precise) |
| 2048 | new_marks = true; |
| 2049 | else |
| 2050 | reg_mask = 0; |
| 2051 | reg->precise = true; |
| 2052 | } |
| 2053 | |
| 2054 | while (spi >= 0) { |
| 2055 | if (func->stack[spi].slot_type[0] != STACK_SPILL) { |
| 2056 | stack_mask = 0; |
| 2057 | break; |
| 2058 | } |
| 2059 | reg = &func->stack[spi].spilled_ptr; |
| 2060 | if (reg->type != SCALAR_VALUE) { |
| 2061 | stack_mask = 0; |
| 2062 | break; |
| 2063 | } |
| 2064 | if (!reg->precise) |
| 2065 | new_marks = true; |
| 2066 | else |
| 2067 | stack_mask = 0; |
| 2068 | reg->precise = true; |
| 2069 | break; |
| 2070 | } |
| 2071 | |
| 2072 | if (!new_marks) |
| 2073 | return 0; |
| 2074 | if (!reg_mask && !stack_mask) |
| 2075 | return 0; |
| 2076 | for (;;) { |
| 2077 | DECLARE_BITMAP(mask, 64); |
| 2078 | u32 history = st->jmp_history_cnt; |
| 2079 | |
| 2080 | if (env->log.level & BPF_LOG_LEVEL) |
| 2081 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
| 2082 | for (i = last_idx;;) { |
| 2083 | if (skip_first) { |
| 2084 | err = 0; |
| 2085 | skip_first = false; |
| 2086 | } else { |
| 2087 | err = backtrack_insn(env, i, ®_mask, &stack_mask); |
| 2088 | } |
| 2089 | if (err == -ENOTSUPP) { |
| 2090 | mark_all_scalars_precise(env, st); |
| 2091 | return 0; |
| 2092 | } else if (err) { |
| 2093 | return err; |
| 2094 | } |
| 2095 | if (!reg_mask && !stack_mask) |
| 2096 | /* Found assignment(s) into tracked register in this state. |
| 2097 | * Since this state is already marked, just return. |
| 2098 | * Nothing to be tracked further in the parent state. |
| 2099 | */ |
| 2100 | return 0; |
| 2101 | if (i == first_idx) |
| 2102 | break; |
| 2103 | i = get_prev_insn_idx(st, i, &history); |
| 2104 | if (i >= env->prog->len) { |
| 2105 | /* This can happen if backtracking reached insn 0 |
| 2106 | * and there are still reg_mask or stack_mask |
| 2107 | * to backtrack. |
| 2108 | * It means the backtracking missed the spot where |
| 2109 | * particular register was initialized with a constant. |
| 2110 | */ |
| 2111 | verbose(env, "BUG backtracking idx %d\n", i); |
| 2112 | WARN_ONCE(1, "verifier backtracking bug"); |
| 2113 | return -EFAULT; |
| 2114 | } |
| 2115 | } |
| 2116 | st = st->parent; |
| 2117 | if (!st) |
| 2118 | break; |
| 2119 | |
| 2120 | new_marks = false; |
| 2121 | func = st->frame[st->curframe]; |
| 2122 | bitmap_from_u64(mask, reg_mask); |
| 2123 | for_each_set_bit(i, mask, 32) { |
| 2124 | reg = &func->regs[i]; |
| 2125 | if (reg->type != SCALAR_VALUE) { |
| 2126 | reg_mask &= ~(1u << i); |
| 2127 | continue; |
| 2128 | } |
| 2129 | if (!reg->precise) |
| 2130 | new_marks = true; |
| 2131 | reg->precise = true; |
| 2132 | } |
| 2133 | |
| 2134 | bitmap_from_u64(mask, stack_mask); |
| 2135 | for_each_set_bit(i, mask, 64) { |
| 2136 | if (i >= func->allocated_stack / BPF_REG_SIZE) { |
| 2137 | /* the sequence of instructions: |
| 2138 | * 2: (bf) r3 = r10 |
| 2139 | * 3: (7b) *(u64 *)(r3 -8) = r0 |
| 2140 | * 4: (79) r4 = *(u64 *)(r10 -8) |
| 2141 | * doesn't contain jmps. It's backtracked |
| 2142 | * as a single block. |
| 2143 | * During backtracking insn 3 is not recognized as |
| 2144 | * stack access, so at the end of backtracking |
| 2145 | * stack slot fp-8 is still marked in stack_mask. |
| 2146 | * However the parent state may not have accessed |
| 2147 | * fp-8 and it's "unallocated" stack space. |
| 2148 | * In such case fallback to conservative. |
| 2149 | */ |
| 2150 | mark_all_scalars_precise(env, st); |
| 2151 | return 0; |
| 2152 | } |
| 2153 | |
| 2154 | if (func->stack[i].slot_type[0] != STACK_SPILL) { |
| 2155 | stack_mask &= ~(1ull << i); |
| 2156 | continue; |
| 2157 | } |
| 2158 | reg = &func->stack[i].spilled_ptr; |
| 2159 | if (reg->type != SCALAR_VALUE) { |
| 2160 | stack_mask &= ~(1ull << i); |
| 2161 | continue; |
| 2162 | } |
| 2163 | if (!reg->precise) |
| 2164 | new_marks = true; |
| 2165 | reg->precise = true; |
| 2166 | } |
| 2167 | if (env->log.level & BPF_LOG_LEVEL) { |
| 2168 | print_verifier_state(env, func); |
| 2169 | verbose(env, "parent %s regs=%x stack=%llx marks\n", |
| 2170 | new_marks ? "didn't have" : "already had", |
| 2171 | reg_mask, stack_mask); |
| 2172 | } |
| 2173 | |
| 2174 | if (!reg_mask && !stack_mask) |
| 2175 | break; |
| 2176 | if (!new_marks) |
| 2177 | break; |
| 2178 | |
| 2179 | last_idx = st->last_insn_idx; |
| 2180 | first_idx = st->first_insn_idx; |
| 2181 | } |
| 2182 | return 0; |
| 2183 | } |
| 2184 | |
| 2185 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
| 2186 | { |
| 2187 | return __mark_chain_precision(env, regno, -1); |
| 2188 | } |
| 2189 | |
| 2190 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) |
| 2191 | { |
| 2192 | return __mark_chain_precision(env, -1, spi); |
| 2193 | } |
| 2194 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2195 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 2196 | { |
| 2197 | switch (type) { |
| 2198 | case PTR_TO_MAP_VALUE: |
| 2199 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 2200 | case PTR_TO_STACK: |
| 2201 | case PTR_TO_CTX: |
| 2202 | case PTR_TO_PACKET: |
| 2203 | case PTR_TO_PACKET_META: |
| 2204 | case PTR_TO_PACKET_END: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2205 | case PTR_TO_FLOW_KEYS: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2206 | case CONST_PTR_TO_MAP: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2207 | case PTR_TO_SOCKET: |
| 2208 | case PTR_TO_SOCKET_OR_NULL: |
| 2209 | case PTR_TO_SOCK_COMMON: |
| 2210 | case PTR_TO_SOCK_COMMON_OR_NULL: |
| 2211 | case PTR_TO_TCP_SOCK: |
| 2212 | case PTR_TO_TCP_SOCK_OR_NULL: |
| 2213 | case PTR_TO_XDP_SOCK: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2214 | case PTR_TO_BTF_ID: |
| 2215 | case PTR_TO_BTF_ID_OR_NULL: |
| 2216 | case PTR_TO_RDONLY_BUF: |
| 2217 | case PTR_TO_RDONLY_BUF_OR_NULL: |
| 2218 | case PTR_TO_RDWR_BUF: |
| 2219 | case PTR_TO_RDWR_BUF_OR_NULL: |
| 2220 | case PTR_TO_PERCPU_BTF_ID: |
| 2221 | case PTR_TO_MEM: |
| 2222 | case PTR_TO_MEM_OR_NULL: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2223 | return true; |
| 2224 | default: |
| 2225 | return false; |
| 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | /* Does this register contain a constant zero? */ |
| 2230 | static bool register_is_null(struct bpf_reg_state *reg) |
| 2231 | { |
| 2232 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 2233 | } |
| 2234 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2235 | static bool register_is_const(struct bpf_reg_state *reg) |
| 2236 | { |
| 2237 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); |
| 2238 | } |
| 2239 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2240 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
| 2241 | { |
| 2242 | return tnum_is_unknown(reg->var_off) && |
| 2243 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && |
| 2244 | reg->umin_value == 0 && reg->umax_value == U64_MAX && |
| 2245 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && |
| 2246 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; |
| 2247 | } |
| 2248 | |
| 2249 | static bool register_is_bounded(struct bpf_reg_state *reg) |
| 2250 | { |
| 2251 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); |
| 2252 | } |
| 2253 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2254 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 2255 | const struct bpf_reg_state *reg) |
| 2256 | { |
| 2257 | if (allow_ptr_leaks) |
| 2258 | return false; |
| 2259 | |
| 2260 | return reg->type != SCALAR_VALUE; |
| 2261 | } |
| 2262 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2263 | static void save_register_state(struct bpf_func_state *state, |
| 2264 | int spi, struct bpf_reg_state *reg) |
| 2265 | { |
| 2266 | int i; |
| 2267 | |
| 2268 | state->stack[spi].spilled_ptr = *reg; |
| 2269 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 2270 | |
| 2271 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 2272 | state->stack[spi].slot_type[i] = STACK_SPILL; |
| 2273 | } |
| 2274 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2275 | /* check_stack_{read,write}_fixed_off functions track spill/fill of registers, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2276 | * stack boundary and alignment are checked in check_mem_access() |
| 2277 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2278 | static int check_stack_write_fixed_off(struct bpf_verifier_env *env, |
| 2279 | /* stack frame we're writing to */ |
| 2280 | struct bpf_func_state *state, |
| 2281 | int off, int size, int value_regno, |
| 2282 | int insn_idx) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2283 | { |
| 2284 | struct bpf_func_state *cur; /* state of the current function */ |
| 2285 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2286 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
| 2287 | struct bpf_reg_state *reg = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2288 | |
| 2289 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2290 | state->acquired_refs, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2291 | if (err) |
| 2292 | return err; |
| 2293 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 2294 | * so it's aligned access and [off, off + size) are within stack limits |
| 2295 | */ |
| 2296 | if (!env->allow_ptr_leaks && |
| 2297 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 2298 | size != BPF_REG_SIZE) { |
| 2299 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 2300 | return -EACCES; |
| 2301 | } |
| 2302 | |
| 2303 | cur = env->cur_state->frame[env->cur_state->curframe]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2304 | if (value_regno >= 0) |
| 2305 | reg = &cur->regs[value_regno]; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2306 | if (!env->bypass_spec_v4) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2307 | bool sanitize = reg && is_spillable_regtype(reg->type); |
| 2308 | |
| 2309 | for (i = 0; i < size; i++) { |
| 2310 | if (state->stack[spi].slot_type[i] == STACK_INVALID) { |
| 2311 | sanitize = true; |
| 2312 | break; |
| 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | if (sanitize) |
| 2317 | env->insn_aux_data[insn_idx].sanitize_stack_spill = true; |
| 2318 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2319 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2320 | if (reg && size == BPF_REG_SIZE && register_is_bounded(reg) && |
| 2321 | !register_is_null(reg) && env->bpf_capable) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2322 | if (dst_reg != BPF_REG_FP) { |
| 2323 | /* The backtracking logic can only recognize explicit |
| 2324 | * stack slot address like [fp - 8]. Other spill of |
| 2325 | * scalar via different register has to be conervative. |
| 2326 | * Backtrack from here and mark all registers as precise |
| 2327 | * that contributed into 'reg' being a constant. |
| 2328 | */ |
| 2329 | err = mark_chain_precision(env, value_regno); |
| 2330 | if (err) |
| 2331 | return err; |
| 2332 | } |
| 2333 | save_register_state(state, spi, reg); |
| 2334 | } else if (reg && is_spillable_regtype(reg->type)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2335 | /* register containing pointer is being spilled into stack */ |
| 2336 | if (size != BPF_REG_SIZE) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2337 | verbose_linfo(env, insn_idx, "; "); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2338 | verbose(env, "invalid size of register spill\n"); |
| 2339 | return -EACCES; |
| 2340 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2341 | if (state != cur && reg->type == PTR_TO_STACK) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2342 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 2343 | return -EINVAL; |
| 2344 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2345 | save_register_state(state, spi, reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2346 | } else { |
| 2347 | u8 type = STACK_MISC; |
| 2348 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2349 | /* regular write of data into stack destroys any spilled ptr */ |
| 2350 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
| 2351 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
| 2352 | if (state->stack[spi].slot_type[0] == STACK_SPILL) |
| 2353 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 2354 | state->stack[spi].slot_type[i] = STACK_MISC; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2355 | |
| 2356 | /* only mark the slot as written if all 8 bytes were written |
| 2357 | * otherwise read propagation may incorrectly stop too soon |
| 2358 | * when stack slots are partially written. |
| 2359 | * This heuristic means that read propagation will be |
| 2360 | * conservative, since it will add reg_live_read marks |
| 2361 | * to stack slots all the way to first state when programs |
| 2362 | * writes+reads less than 8 bytes |
| 2363 | */ |
| 2364 | if (size == BPF_REG_SIZE) |
| 2365 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 2366 | |
| 2367 | /* when we zero initialize stack slots mark them as such */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2368 | if (reg && register_is_null(reg)) { |
| 2369 | /* backtracking doesn't work for STACK_ZERO yet. */ |
| 2370 | err = mark_chain_precision(env, value_regno); |
| 2371 | if (err) |
| 2372 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2373 | type = STACK_ZERO; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2374 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2375 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2376 | /* Mark slots affected by this stack write. */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2377 | for (i = 0; i < size; i++) |
| 2378 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
| 2379 | type; |
| 2380 | } |
| 2381 | return 0; |
| 2382 | } |
| 2383 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2384 | /* Write the stack: 'stack[ptr_regno + off] = value_regno'. 'ptr_regno' is |
| 2385 | * known to contain a variable offset. |
| 2386 | * This function checks whether the write is permitted and conservatively |
| 2387 | * tracks the effects of the write, considering that each stack slot in the |
| 2388 | * dynamic range is potentially written to. |
| 2389 | * |
| 2390 | * 'off' includes 'regno->off'. |
| 2391 | * 'value_regno' can be -1, meaning that an unknown value is being written to |
| 2392 | * the stack. |
| 2393 | * |
| 2394 | * Spilled pointers in range are not marked as written because we don't know |
| 2395 | * what's going to be actually written. This means that read propagation for |
| 2396 | * future reads cannot be terminated by this write. |
| 2397 | * |
| 2398 | * For privileged programs, uninitialized stack slots are considered |
| 2399 | * initialized by this write (even though we don't know exactly what offsets |
| 2400 | * are going to be written to). The idea is that we don't want the verifier to |
| 2401 | * reject future reads that access slots written to through variable offsets. |
| 2402 | */ |
| 2403 | static int check_stack_write_var_off(struct bpf_verifier_env *env, |
| 2404 | /* func where register points to */ |
| 2405 | struct bpf_func_state *state, |
| 2406 | int ptr_regno, int off, int size, |
| 2407 | int value_regno, int insn_idx) |
| 2408 | { |
| 2409 | struct bpf_func_state *cur; /* state of the current function */ |
| 2410 | int min_off, max_off; |
| 2411 | int i, err; |
| 2412 | struct bpf_reg_state *ptr_reg = NULL, *value_reg = NULL; |
| 2413 | bool writing_zero = false; |
| 2414 | /* set if the fact that we're writing a zero is used to let any |
| 2415 | * stack slots remain STACK_ZERO |
| 2416 | */ |
| 2417 | bool zero_used = false; |
| 2418 | |
| 2419 | cur = env->cur_state->frame[env->cur_state->curframe]; |
| 2420 | ptr_reg = &cur->regs[ptr_regno]; |
| 2421 | min_off = ptr_reg->smin_value + off; |
| 2422 | max_off = ptr_reg->smax_value + off + size; |
| 2423 | if (value_regno >= 0) |
| 2424 | value_reg = &cur->regs[value_regno]; |
| 2425 | if (value_reg && register_is_null(value_reg)) |
| 2426 | writing_zero = true; |
| 2427 | |
| 2428 | err = realloc_func_state(state, round_up(-min_off, BPF_REG_SIZE), |
| 2429 | state->acquired_refs, true); |
| 2430 | if (err) |
| 2431 | return err; |
| 2432 | |
| 2433 | |
| 2434 | /* Variable offset writes destroy any spilled pointers in range. */ |
| 2435 | for (i = min_off; i < max_off; i++) { |
| 2436 | u8 new_type, *stype; |
| 2437 | int slot, spi; |
| 2438 | |
| 2439 | slot = -i - 1; |
| 2440 | spi = slot / BPF_REG_SIZE; |
| 2441 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 2442 | |
| 2443 | if (!env->allow_ptr_leaks |
| 2444 | && *stype != NOT_INIT |
| 2445 | && *stype != SCALAR_VALUE) { |
| 2446 | /* Reject the write if there's are spilled pointers in |
| 2447 | * range. If we didn't reject here, the ptr status |
| 2448 | * would be erased below (even though not all slots are |
| 2449 | * actually overwritten), possibly opening the door to |
| 2450 | * leaks. |
| 2451 | */ |
| 2452 | verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d", |
| 2453 | insn_idx, i); |
| 2454 | return -EINVAL; |
| 2455 | } |
| 2456 | |
| 2457 | /* Erase all spilled pointers. */ |
| 2458 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
| 2459 | |
| 2460 | /* Update the slot type. */ |
| 2461 | new_type = STACK_MISC; |
| 2462 | if (writing_zero && *stype == STACK_ZERO) { |
| 2463 | new_type = STACK_ZERO; |
| 2464 | zero_used = true; |
| 2465 | } |
| 2466 | /* If the slot is STACK_INVALID, we check whether it's OK to |
| 2467 | * pretend that it will be initialized by this write. The slot |
| 2468 | * might not actually be written to, and so if we mark it as |
| 2469 | * initialized future reads might leak uninitialized memory. |
| 2470 | * For privileged programs, we will accept such reads to slots |
| 2471 | * that may or may not be written because, if we're reject |
| 2472 | * them, the error would be too confusing. |
| 2473 | */ |
| 2474 | if (*stype == STACK_INVALID && !env->allow_uninit_stack) { |
| 2475 | verbose(env, "uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d", |
| 2476 | insn_idx, i); |
| 2477 | return -EINVAL; |
| 2478 | } |
| 2479 | *stype = new_type; |
| 2480 | } |
| 2481 | if (zero_used) { |
| 2482 | /* backtracking doesn't work for STACK_ZERO yet. */ |
| 2483 | err = mark_chain_precision(env, value_regno); |
| 2484 | if (err) |
| 2485 | return err; |
| 2486 | } |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
| 2490 | /* When register 'dst_regno' is assigned some values from stack[min_off, |
| 2491 | * max_off), we set the register's type according to the types of the |
| 2492 | * respective stack slots. If all the stack values are known to be zeros, then |
| 2493 | * so is the destination reg. Otherwise, the register is considered to be |
| 2494 | * SCALAR. This function does not deal with register filling; the caller must |
| 2495 | * ensure that all spilled registers in the stack range have been marked as |
| 2496 | * read. |
| 2497 | */ |
| 2498 | static void mark_reg_stack_read(struct bpf_verifier_env *env, |
| 2499 | /* func where src register points to */ |
| 2500 | struct bpf_func_state *ptr_state, |
| 2501 | int min_off, int max_off, int dst_regno) |
| 2502 | { |
| 2503 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2504 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2505 | int i, slot, spi; |
| 2506 | u8 *stype; |
| 2507 | int zeros = 0; |
| 2508 | |
| 2509 | for (i = min_off; i < max_off; i++) { |
| 2510 | slot = -i - 1; |
| 2511 | spi = slot / BPF_REG_SIZE; |
| 2512 | stype = ptr_state->stack[spi].slot_type; |
| 2513 | if (stype[slot % BPF_REG_SIZE] != STACK_ZERO) |
| 2514 | break; |
| 2515 | zeros++; |
| 2516 | } |
| 2517 | if (zeros == max_off - min_off) { |
| 2518 | /* any access_size read into register is zero extended, |
| 2519 | * so the whole register == const_zero |
| 2520 | */ |
| 2521 | __mark_reg_const_zero(&state->regs[dst_regno]); |
| 2522 | /* backtracking doesn't support STACK_ZERO yet, |
| 2523 | * so mark it precise here, so that later |
| 2524 | * backtracking can stop here. |
| 2525 | * Backtracking may not need this if this register |
| 2526 | * doesn't participate in pointer adjustment. |
| 2527 | * Forward propagation of precise flag is not |
| 2528 | * necessary either. This mark is only to stop |
| 2529 | * backtracking. Any register that contributed |
| 2530 | * to const 0 was marked precise before spill. |
| 2531 | */ |
| 2532 | state->regs[dst_regno].precise = true; |
| 2533 | } else { |
| 2534 | /* have read misc data from the stack */ |
| 2535 | mark_reg_unknown(env, state->regs, dst_regno); |
| 2536 | } |
| 2537 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
| 2538 | } |
| 2539 | |
| 2540 | /* Read the stack at 'off' and put the results into the register indicated by |
| 2541 | * 'dst_regno'. It handles reg filling if the addressed stack slot is a |
| 2542 | * spilled reg. |
| 2543 | * |
| 2544 | * 'dst_regno' can be -1, meaning that the read value is not going to a |
| 2545 | * register. |
| 2546 | * |
| 2547 | * The access is assumed to be within the current stack bounds. |
| 2548 | */ |
| 2549 | static int check_stack_read_fixed_off(struct bpf_verifier_env *env, |
| 2550 | /* func where src register points to */ |
| 2551 | struct bpf_func_state *reg_state, |
| 2552 | int off, int size, int dst_regno) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2553 | { |
| 2554 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2555 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2556 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2557 | struct bpf_reg_state *reg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2558 | u8 *stype; |
| 2559 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2560 | stype = reg_state->stack[spi].slot_type; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2561 | reg = ®_state->stack[spi].spilled_ptr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2562 | |
| 2563 | if (stype[0] == STACK_SPILL) { |
| 2564 | if (size != BPF_REG_SIZE) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2565 | if (reg->type != SCALAR_VALUE) { |
| 2566 | verbose_linfo(env, env->insn_idx, "; "); |
| 2567 | verbose(env, "invalid size of register fill\n"); |
| 2568 | return -EACCES; |
| 2569 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2570 | if (dst_regno >= 0) { |
| 2571 | mark_reg_unknown(env, state->regs, dst_regno); |
| 2572 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2573 | } |
| 2574 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
| 2575 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2576 | } |
| 2577 | for (i = 1; i < BPF_REG_SIZE; i++) { |
| 2578 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
| 2579 | verbose(env, "corrupted spill memory\n"); |
| 2580 | return -EACCES; |
| 2581 | } |
| 2582 | } |
| 2583 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2584 | if (dst_regno >= 0) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2585 | /* restore register state from stack */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2586 | state->regs[dst_regno] = *reg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2587 | /* mark reg as written since spilled pointer state likely |
| 2588 | * has its liveness marks cleared by is_state_visited() |
| 2589 | * which resets stack/reg liveness for state transitions |
| 2590 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2591 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2592 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2593 | /* If dst_regno==-1, the caller is asking us whether |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2594 | * it is acceptable to use this value as a SCALAR_VALUE |
| 2595 | * (e.g. for XADD). |
| 2596 | * We must not allow unprivileged callers to do that |
| 2597 | * with spilled pointers. |
| 2598 | */ |
| 2599 | verbose(env, "leaking pointer from stack off %d\n", |
| 2600 | off); |
| 2601 | return -EACCES; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2602 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2603 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2604 | } else { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2605 | u8 type; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2606 | |
| 2607 | for (i = 0; i < size; i++) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2608 | type = stype[(slot - i) % BPF_REG_SIZE]; |
| 2609 | if (type == STACK_MISC) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2610 | continue; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2611 | if (type == STACK_ZERO) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2612 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2613 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 2614 | off, i, size); |
| 2615 | return -EACCES; |
| 2616 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2617 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2618 | if (dst_regno >= 0) |
| 2619 | mark_reg_stack_read(env, reg_state, off, off + size, dst_regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2620 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2621 | return 0; |
| 2622 | } |
| 2623 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2624 | enum stack_access_src { |
| 2625 | ACCESS_DIRECT = 1, /* the access is performed by an instruction */ |
| 2626 | ACCESS_HELPER = 2, /* the access is performed by a helper */ |
| 2627 | }; |
| 2628 | |
| 2629 | static int check_stack_range_initialized(struct bpf_verifier_env *env, |
| 2630 | int regno, int off, int access_size, |
| 2631 | bool zero_size_allowed, |
| 2632 | enum stack_access_src type, |
| 2633 | struct bpf_call_arg_meta *meta); |
| 2634 | |
| 2635 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2636 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2637 | return cur_regs(env) + regno; |
| 2638 | } |
| 2639 | |
| 2640 | /* Read the stack at 'ptr_regno + off' and put the result into the register |
| 2641 | * 'dst_regno'. |
| 2642 | * 'off' includes the pointer register's fixed offset(i.e. 'ptr_regno.off'), |
| 2643 | * but not its variable offset. |
| 2644 | * 'size' is assumed to be <= reg size and the access is assumed to be aligned. |
| 2645 | * |
| 2646 | * As opposed to check_stack_read_fixed_off, this function doesn't deal with |
| 2647 | * filling registers (i.e. reads of spilled register cannot be detected when |
| 2648 | * the offset is not fixed). We conservatively mark 'dst_regno' as containing |
| 2649 | * SCALAR_VALUE. That's why we assert that the 'ptr_regno' has a variable |
| 2650 | * offset; for a fixed offset check_stack_read_fixed_off should be used |
| 2651 | * instead. |
| 2652 | */ |
| 2653 | static int check_stack_read_var_off(struct bpf_verifier_env *env, |
| 2654 | int ptr_regno, int off, int size, int dst_regno) |
| 2655 | { |
| 2656 | /* The state of the source register. */ |
| 2657 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); |
| 2658 | struct bpf_func_state *ptr_state = func(env, reg); |
| 2659 | int err; |
| 2660 | int min_off, max_off; |
| 2661 | |
| 2662 | /* Note that we pass a NULL meta, so raw access will not be permitted. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2663 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2664 | err = check_stack_range_initialized(env, ptr_regno, off, size, |
| 2665 | false, ACCESS_DIRECT, NULL); |
| 2666 | if (err) |
| 2667 | return err; |
| 2668 | |
| 2669 | min_off = reg->smin_value + off; |
| 2670 | max_off = reg->smax_value + off; |
| 2671 | mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); |
| 2672 | return 0; |
| 2673 | } |
| 2674 | |
| 2675 | /* check_stack_read dispatches to check_stack_read_fixed_off or |
| 2676 | * check_stack_read_var_off. |
| 2677 | * |
| 2678 | * The caller must ensure that the offset falls within the allocated stack |
| 2679 | * bounds. |
| 2680 | * |
| 2681 | * 'dst_regno' is a register which will receive the value from the stack. It |
| 2682 | * can be -1, meaning that the read value is not going to a register. |
| 2683 | */ |
| 2684 | static int check_stack_read(struct bpf_verifier_env *env, |
| 2685 | int ptr_regno, int off, int size, |
| 2686 | int dst_regno) |
| 2687 | { |
| 2688 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); |
| 2689 | struct bpf_func_state *state = func(env, reg); |
| 2690 | int err; |
| 2691 | /* Some accesses are only permitted with a static offset. */ |
| 2692 | bool var_off = !tnum_is_const(reg->var_off); |
| 2693 | |
| 2694 | /* The offset is required to be static when reads don't go to a |
| 2695 | * register, in order to not leak pointers (see |
| 2696 | * check_stack_read_fixed_off). |
| 2697 | */ |
| 2698 | if (dst_regno < 0 && var_off) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2699 | char tn_buf[48]; |
| 2700 | |
| 2701 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2702 | verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2703 | tn_buf, off, size); |
| 2704 | return -EACCES; |
| 2705 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2706 | /* Variable offset is prohibited for unprivileged mode for simplicity |
| 2707 | * since it requires corresponding support in Spectre masking for stack |
| 2708 | * ALU. See also retrieve_ptr_limit(). |
| 2709 | */ |
| 2710 | if (!env->bypass_spec_v1 && var_off) { |
| 2711 | char tn_buf[48]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2712 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2713 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2714 | verbose(env, "R%d variable offset stack access prohibited for !root, var_off=%s\n", |
| 2715 | ptr_regno, tn_buf); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2716 | return -EACCES; |
| 2717 | } |
| 2718 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2719 | if (!var_off) { |
| 2720 | off += reg->var_off.value; |
| 2721 | err = check_stack_read_fixed_off(env, state, off, size, |
| 2722 | dst_regno); |
| 2723 | } else { |
| 2724 | /* Variable offset stack reads need more conservative handling |
| 2725 | * than fixed offset ones. Note that dst_regno >= 0 on this |
| 2726 | * branch. |
| 2727 | */ |
| 2728 | err = check_stack_read_var_off(env, ptr_regno, off, size, |
| 2729 | dst_regno); |
| 2730 | } |
| 2731 | return err; |
| 2732 | } |
| 2733 | |
| 2734 | |
| 2735 | /* check_stack_write dispatches to check_stack_write_fixed_off or |
| 2736 | * check_stack_write_var_off. |
| 2737 | * |
| 2738 | * 'ptr_regno' is the register used as a pointer into the stack. |
| 2739 | * 'off' includes 'ptr_regno->off', but not its variable offset (if any). |
| 2740 | * 'value_regno' is the register whose value we're writing to the stack. It can |
| 2741 | * be -1, meaning that we're not writing from a register. |
| 2742 | * |
| 2743 | * The caller must ensure that the offset falls within the maximum stack size. |
| 2744 | */ |
| 2745 | static int check_stack_write(struct bpf_verifier_env *env, |
| 2746 | int ptr_regno, int off, int size, |
| 2747 | int value_regno, int insn_idx) |
| 2748 | { |
| 2749 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); |
| 2750 | struct bpf_func_state *state = func(env, reg); |
| 2751 | int err; |
| 2752 | |
| 2753 | if (tnum_is_const(reg->var_off)) { |
| 2754 | off += reg->var_off.value; |
| 2755 | err = check_stack_write_fixed_off(env, state, off, size, |
| 2756 | value_regno, insn_idx); |
| 2757 | } else { |
| 2758 | /* Variable offset stack reads need more conservative handling |
| 2759 | * than fixed offset ones. |
| 2760 | */ |
| 2761 | err = check_stack_write_var_off(env, state, |
| 2762 | ptr_regno, off, size, |
| 2763 | value_regno, insn_idx); |
| 2764 | } |
| 2765 | return err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
| 2769 | int off, int size, enum bpf_access_type type) |
| 2770 | { |
| 2771 | struct bpf_reg_state *regs = cur_regs(env); |
| 2772 | struct bpf_map *map = regs[regno].map_ptr; |
| 2773 | u32 cap = bpf_map_flags_to_cap(map); |
| 2774 | |
| 2775 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { |
| 2776 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", |
| 2777 | map->value_size, off, size); |
| 2778 | return -EACCES; |
| 2779 | } |
| 2780 | |
| 2781 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { |
| 2782 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", |
| 2783 | map->value_size, off, size); |
| 2784 | return -EACCES; |
| 2785 | } |
| 2786 | |
| 2787 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2790 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
| 2791 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, |
| 2792 | int off, int size, u32 mem_size, |
| 2793 | bool zero_size_allowed) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2794 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2795 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
| 2796 | struct bpf_reg_state *reg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2797 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2798 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) |
| 2799 | return 0; |
| 2800 | |
| 2801 | reg = &cur_regs(env)[regno]; |
| 2802 | switch (reg->type) { |
| 2803 | case PTR_TO_MAP_VALUE: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2804 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2805 | mem_size, off, size); |
| 2806 | break; |
| 2807 | case PTR_TO_PACKET: |
| 2808 | case PTR_TO_PACKET_META: |
| 2809 | case PTR_TO_PACKET_END: |
| 2810 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
| 2811 | off, size, regno, reg->id, off, mem_size); |
| 2812 | break; |
| 2813 | case PTR_TO_MEM: |
| 2814 | default: |
| 2815 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", |
| 2816 | mem_size, off, size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2817 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2818 | |
| 2819 | return -EACCES; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2822 | /* check read/write into a memory region with possible variable offset */ |
| 2823 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, |
| 2824 | int off, int size, u32 mem_size, |
| 2825 | bool zero_size_allowed) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2826 | { |
| 2827 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2828 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2829 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 2830 | int err; |
| 2831 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2832 | /* We may have adjusted the register pointing to memory region, so we |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2833 | * need to try adding each of min_value and max_value to off |
| 2834 | * to make sure our theoretical access will be safe. |
| 2835 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2836 | if (env->log.level & BPF_LOG_LEVEL) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2837 | print_verifier_state(env, state); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2838 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2839 | /* The minimum value is only important with signed |
| 2840 | * comparisons where we can't assume the floor of a |
| 2841 | * value is 0. If we are using signed variables for our |
| 2842 | * index'es we need to make sure that whatever we use |
| 2843 | * will have a set floor within our range. |
| 2844 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2845 | if (reg->smin_value < 0 && |
| 2846 | (reg->smin_value == S64_MIN || |
| 2847 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || |
| 2848 | reg->smin_value + off < 0)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2849 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 2850 | regno); |
| 2851 | return -EACCES; |
| 2852 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2853 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
| 2854 | mem_size, zero_size_allowed); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2855 | if (err) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2856 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2857 | regno); |
| 2858 | return err; |
| 2859 | } |
| 2860 | |
| 2861 | /* If we haven't set a max value then we need to bail since we can't be |
| 2862 | * sure we won't do bad things. |
| 2863 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
| 2864 | */ |
| 2865 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2866 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2867 | regno); |
| 2868 | return -EACCES; |
| 2869 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2870 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
| 2871 | mem_size, zero_size_allowed); |
| 2872 | if (err) { |
| 2873 | verbose(env, "R%d max value is outside of the allowed memory range\n", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2874 | regno); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2875 | return err; |
| 2876 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2877 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2878 | return 0; |
| 2879 | } |
| 2880 | |
| 2881 | /* check read/write into a map element with possible variable offset */ |
| 2882 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
| 2883 | int off, int size, bool zero_size_allowed) |
| 2884 | { |
| 2885 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2886 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2887 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 2888 | struct bpf_map *map = reg->map_ptr; |
| 2889 | int err; |
| 2890 | |
| 2891 | err = check_mem_region_access(env, regno, off, size, map->value_size, |
| 2892 | zero_size_allowed); |
| 2893 | if (err) |
| 2894 | return err; |
| 2895 | |
| 2896 | if (map_value_has_spin_lock(map)) { |
| 2897 | u32 lock = map->spin_lock_off; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2898 | |
| 2899 | /* if any part of struct bpf_spin_lock can be touched by |
| 2900 | * load/store reject this program. |
| 2901 | * To check that [x1, x2) overlaps with [y1, y2) |
| 2902 | * it is sufficient to check x1 < y2 && y1 < x2. |
| 2903 | */ |
| 2904 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && |
| 2905 | lock < reg->umax_value + off + size) { |
| 2906 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); |
| 2907 | return -EACCES; |
| 2908 | } |
| 2909 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2910 | return err; |
| 2911 | } |
| 2912 | |
| 2913 | #define MAX_PACKET_OFF 0xffff |
| 2914 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2915 | static enum bpf_prog_type resolve_prog_type(struct bpf_prog *prog) |
| 2916 | { |
| 2917 | return prog->aux->dst_prog ? prog->aux->dst_prog->type : prog->type; |
| 2918 | } |
| 2919 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2920 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
| 2921 | const struct bpf_call_arg_meta *meta, |
| 2922 | enum bpf_access_type t) |
| 2923 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2924 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
| 2925 | |
| 2926 | switch (prog_type) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2927 | /* Program types only with direct read access go here! */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2928 | case BPF_PROG_TYPE_LWT_IN: |
| 2929 | case BPF_PROG_TYPE_LWT_OUT: |
| 2930 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
| 2931 | case BPF_PROG_TYPE_SK_REUSEPORT: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2932 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
| 2933 | case BPF_PROG_TYPE_CGROUP_SKB: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2934 | if (t == BPF_WRITE) |
| 2935 | return false; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2936 | fallthrough; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2937 | |
| 2938 | /* Program types with direct read + write access go here! */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2939 | case BPF_PROG_TYPE_SCHED_CLS: |
| 2940 | case BPF_PROG_TYPE_SCHED_ACT: |
| 2941 | case BPF_PROG_TYPE_XDP: |
| 2942 | case BPF_PROG_TYPE_LWT_XMIT: |
| 2943 | case BPF_PROG_TYPE_SK_SKB: |
| 2944 | case BPF_PROG_TYPE_SK_MSG: |
| 2945 | if (meta) |
| 2946 | return meta->pkt_access; |
| 2947 | |
| 2948 | env->seen_direct_write = true; |
| 2949 | return true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2950 | |
| 2951 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 2952 | if (t == BPF_WRITE) |
| 2953 | env->seen_direct_write = true; |
| 2954 | |
| 2955 | return true; |
| 2956 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2957 | default: |
| 2958 | return false; |
| 2959 | } |
| 2960 | } |
| 2961 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2962 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
| 2963 | int size, bool zero_size_allowed) |
| 2964 | { |
| 2965 | struct bpf_reg_state *regs = cur_regs(env); |
| 2966 | struct bpf_reg_state *reg = ®s[regno]; |
| 2967 | int err; |
| 2968 | |
| 2969 | /* We may have added a variable offset to the packet pointer; but any |
| 2970 | * reg->range we have comes after that. We are only checking the fixed |
| 2971 | * offset. |
| 2972 | */ |
| 2973 | |
| 2974 | /* We don't allow negative numbers, because we aren't tracking enough |
| 2975 | * detail to prove they're safe. |
| 2976 | */ |
| 2977 | if (reg->smin_value < 0) { |
| 2978 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 2979 | regno); |
| 2980 | return -EACCES; |
| 2981 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2982 | err = __check_mem_access(env, regno, off, size, reg->range, |
| 2983 | zero_size_allowed); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2984 | if (err) { |
| 2985 | verbose(env, "R%d offset is outside of the packet\n", regno); |
| 2986 | return err; |
| 2987 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2988 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2989 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2990 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 2991 | * otherwise find_good_pkt_pointers would have refused to set range info |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2992 | * that __check_mem_access would have rejected this pkt access. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2993 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 2994 | */ |
| 2995 | env->prog->aux->max_pkt_offset = |
| 2996 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 2997 | off + reg->umax_value + size - 1); |
| 2998 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2999 | return err; |
| 3000 | } |
| 3001 | |
| 3002 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
| 3003 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3004 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
| 3005 | u32 *btf_id) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3006 | { |
| 3007 | struct bpf_insn_access_aux info = { |
| 3008 | .reg_type = *reg_type, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3009 | .log = &env->log, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3010 | }; |
| 3011 | |
| 3012 | if (env->ops->is_valid_access && |
| 3013 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
| 3014 | /* A non zero info.ctx_field_size indicates that this field is a |
| 3015 | * candidate for later verifier transformation to load the whole |
| 3016 | * field and then apply a mask when accessed with a narrower |
| 3017 | * access than actual ctx access size. A zero info.ctx_field_size |
| 3018 | * will only allow for whole field access and rejects any other |
| 3019 | * type of narrower access. |
| 3020 | */ |
| 3021 | *reg_type = info.reg_type; |
| 3022 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3023 | if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL) |
| 3024 | *btf_id = info.btf_id; |
| 3025 | else |
| 3026 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3027 | /* remember the offset of last byte accessed in ctx */ |
| 3028 | if (env->prog->aux->max_ctx_offset < off + size) |
| 3029 | env->prog->aux->max_ctx_offset = off + size; |
| 3030 | return 0; |
| 3031 | } |
| 3032 | |
| 3033 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
| 3034 | return -EACCES; |
| 3035 | } |
| 3036 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3037 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 3038 | int size) |
| 3039 | { |
| 3040 | if (size < 0 || off < 0 || |
| 3041 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 3042 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 3043 | off, size); |
| 3044 | return -EACCES; |
| 3045 | } |
| 3046 | return 0; |
| 3047 | } |
| 3048 | |
| 3049 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
| 3050 | u32 regno, int off, int size, |
| 3051 | enum bpf_access_type t) |
| 3052 | { |
| 3053 | struct bpf_reg_state *regs = cur_regs(env); |
| 3054 | struct bpf_reg_state *reg = ®s[regno]; |
| 3055 | struct bpf_insn_access_aux info = {}; |
| 3056 | bool valid; |
| 3057 | |
| 3058 | if (reg->smin_value < 0) { |
| 3059 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 3060 | regno); |
| 3061 | return -EACCES; |
| 3062 | } |
| 3063 | |
| 3064 | switch (reg->type) { |
| 3065 | case PTR_TO_SOCK_COMMON: |
| 3066 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); |
| 3067 | break; |
| 3068 | case PTR_TO_SOCKET: |
| 3069 | valid = bpf_sock_is_valid_access(off, size, t, &info); |
| 3070 | break; |
| 3071 | case PTR_TO_TCP_SOCK: |
| 3072 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); |
| 3073 | break; |
| 3074 | case PTR_TO_XDP_SOCK: |
| 3075 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); |
| 3076 | break; |
| 3077 | default: |
| 3078 | valid = false; |
| 3079 | } |
| 3080 | |
| 3081 | |
| 3082 | if (valid) { |
| 3083 | env->insn_aux_data[insn_idx].ctx_field_size = |
| 3084 | info.ctx_field_size; |
| 3085 | return 0; |
| 3086 | } |
| 3087 | |
| 3088 | verbose(env, "R%d invalid %s access off=%d size=%d\n", |
| 3089 | regno, reg_type_str[reg->type], off, size); |
| 3090 | |
| 3091 | return -EACCES; |
| 3092 | } |
| 3093 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3094 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 3095 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3096 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 3100 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3101 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3102 | |
| 3103 | return reg->type == PTR_TO_CTX; |
| 3104 | } |
| 3105 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3106 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) |
| 3107 | { |
| 3108 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 3109 | |
| 3110 | return type_is_sk_pointer(reg->type); |
| 3111 | } |
| 3112 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3113 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 3114 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3115 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3116 | |
| 3117 | return type_is_pkt_pointer(reg->type); |
| 3118 | } |
| 3119 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3120 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 3121 | { |
| 3122 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 3123 | |
| 3124 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 3125 | return reg->type == PTR_TO_FLOW_KEYS; |
| 3126 | } |
| 3127 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3128 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 3129 | const struct bpf_reg_state *reg, |
| 3130 | int off, int size, bool strict) |
| 3131 | { |
| 3132 | struct tnum reg_off; |
| 3133 | int ip_align; |
| 3134 | |
| 3135 | /* Byte size accesses are always allowed. */ |
| 3136 | if (!strict || size == 1) |
| 3137 | return 0; |
| 3138 | |
| 3139 | /* For platforms that do not have a Kconfig enabling |
| 3140 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 3141 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 3142 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 3143 | * to this code only in strict mode where we want to emulate |
| 3144 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 3145 | * unconditional IP align value of '2'. |
| 3146 | */ |
| 3147 | ip_align = 2; |
| 3148 | |
| 3149 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 3150 | if (!tnum_is_aligned(reg_off, size)) { |
| 3151 | char tn_buf[48]; |
| 3152 | |
| 3153 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3154 | verbose(env, |
| 3155 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
| 3156 | ip_align, tn_buf, reg->off, off, size); |
| 3157 | return -EACCES; |
| 3158 | } |
| 3159 | |
| 3160 | return 0; |
| 3161 | } |
| 3162 | |
| 3163 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 3164 | const struct bpf_reg_state *reg, |
| 3165 | const char *pointer_desc, |
| 3166 | int off, int size, bool strict) |
| 3167 | { |
| 3168 | struct tnum reg_off; |
| 3169 | |
| 3170 | /* Byte size accesses are always allowed. */ |
| 3171 | if (!strict || size == 1) |
| 3172 | return 0; |
| 3173 | |
| 3174 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 3175 | if (!tnum_is_aligned(reg_off, size)) { |
| 3176 | char tn_buf[48]; |
| 3177 | |
| 3178 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3179 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
| 3180 | pointer_desc, tn_buf, reg->off, off, size); |
| 3181 | return -EACCES; |
| 3182 | } |
| 3183 | |
| 3184 | return 0; |
| 3185 | } |
| 3186 | |
| 3187 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
| 3188 | const struct bpf_reg_state *reg, int off, |
| 3189 | int size, bool strict_alignment_once) |
| 3190 | { |
| 3191 | bool strict = env->strict_alignment || strict_alignment_once; |
| 3192 | const char *pointer_desc = ""; |
| 3193 | |
| 3194 | switch (reg->type) { |
| 3195 | case PTR_TO_PACKET: |
| 3196 | case PTR_TO_PACKET_META: |
| 3197 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 3198 | * right in front, treat it the very same way. |
| 3199 | */ |
| 3200 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3201 | case PTR_TO_FLOW_KEYS: |
| 3202 | pointer_desc = "flow keys "; |
| 3203 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3204 | case PTR_TO_MAP_VALUE: |
| 3205 | pointer_desc = "value "; |
| 3206 | break; |
| 3207 | case PTR_TO_CTX: |
| 3208 | pointer_desc = "context "; |
| 3209 | break; |
| 3210 | case PTR_TO_STACK: |
| 3211 | pointer_desc = "stack "; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3212 | /* The stack spill tracking logic in check_stack_write_fixed_off() |
| 3213 | * and check_stack_read_fixed_off() relies on stack accesses being |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3214 | * aligned. |
| 3215 | */ |
| 3216 | strict = true; |
| 3217 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3218 | case PTR_TO_SOCKET: |
| 3219 | pointer_desc = "sock "; |
| 3220 | break; |
| 3221 | case PTR_TO_SOCK_COMMON: |
| 3222 | pointer_desc = "sock_common "; |
| 3223 | break; |
| 3224 | case PTR_TO_TCP_SOCK: |
| 3225 | pointer_desc = "tcp_sock "; |
| 3226 | break; |
| 3227 | case PTR_TO_XDP_SOCK: |
| 3228 | pointer_desc = "xdp_sock "; |
| 3229 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3230 | default: |
| 3231 | break; |
| 3232 | } |
| 3233 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 3234 | strict); |
| 3235 | } |
| 3236 | |
| 3237 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 3238 | const struct bpf_func_state *func, |
| 3239 | int off) |
| 3240 | { |
| 3241 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
| 3242 | |
| 3243 | if (stack >= -off) |
| 3244 | return 0; |
| 3245 | |
| 3246 | /* update known max for given subprogram */ |
| 3247 | env->subprog_info[func->subprogno].stack_depth = -off; |
| 3248 | return 0; |
| 3249 | } |
| 3250 | |
| 3251 | /* starting from main bpf function walk all instructions of the function |
| 3252 | * and recursively walk all callees that given function can call. |
| 3253 | * Ignore jump and exit insns. |
| 3254 | * Since recursion is prevented by check_cfg() this algorithm |
| 3255 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 3256 | */ |
| 3257 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 3258 | { |
| 3259 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 3260 | struct bpf_subprog_info *subprog = env->subprog_info; |
| 3261 | struct bpf_insn *insn = env->prog->insnsi; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3262 | bool tail_call_reachable = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3263 | int ret_insn[MAX_CALL_FRAMES]; |
| 3264 | int ret_prog[MAX_CALL_FRAMES]; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3265 | int j; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3266 | |
| 3267 | process_func: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3268 | /* protect against potential stack overflow that might happen when |
| 3269 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack |
| 3270 | * depth for such case down to 256 so that the worst case scenario |
| 3271 | * would result in 8k stack size (32 which is tailcall limit * 256 = |
| 3272 | * 8k). |
| 3273 | * |
| 3274 | * To get the idea what might happen, see an example: |
| 3275 | * func1 -> sub rsp, 128 |
| 3276 | * subfunc1 -> sub rsp, 256 |
| 3277 | * tailcall1 -> add rsp, 256 |
| 3278 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) |
| 3279 | * subfunc2 -> sub rsp, 64 |
| 3280 | * subfunc22 -> sub rsp, 128 |
| 3281 | * tailcall2 -> add rsp, 128 |
| 3282 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) |
| 3283 | * |
| 3284 | * tailcall will unwind the current stack frame but it will not get rid |
| 3285 | * of caller's stack as shown on the example above. |
| 3286 | */ |
| 3287 | if (idx && subprog[idx].has_tail_call && depth >= 256) { |
| 3288 | verbose(env, |
| 3289 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", |
| 3290 | depth); |
| 3291 | return -EACCES; |
| 3292 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3293 | /* round up to 32-bytes, since this is granularity |
| 3294 | * of interpreter stack size |
| 3295 | */ |
| 3296 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
| 3297 | if (depth > MAX_BPF_STACK) { |
| 3298 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
| 3299 | frame + 1, depth); |
| 3300 | return -EACCES; |
| 3301 | } |
| 3302 | continue_func: |
| 3303 | subprog_end = subprog[idx + 1].start; |
| 3304 | for (; i < subprog_end; i++) { |
| 3305 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 3306 | continue; |
| 3307 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 3308 | continue; |
| 3309 | /* remember insn and function to return to */ |
| 3310 | ret_insn[frame] = i + 1; |
| 3311 | ret_prog[frame] = idx; |
| 3312 | |
| 3313 | /* find the callee */ |
| 3314 | i = i + insn[i].imm + 1; |
| 3315 | idx = find_subprog(env, i); |
| 3316 | if (idx < 0) { |
| 3317 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 3318 | i); |
| 3319 | return -EFAULT; |
| 3320 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3321 | |
| 3322 | if (subprog[idx].has_tail_call) |
| 3323 | tail_call_reachable = true; |
| 3324 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3325 | frame++; |
| 3326 | if (frame >= MAX_CALL_FRAMES) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3327 | verbose(env, "the call stack of %d frames is too deep !\n", |
| 3328 | frame); |
| 3329 | return -E2BIG; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3330 | } |
| 3331 | goto process_func; |
| 3332 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3333 | /* if tail call got detected across bpf2bpf calls then mark each of the |
| 3334 | * currently present subprog frames as tail call reachable subprogs; |
| 3335 | * this info will be utilized by JIT so that we will be preserving the |
| 3336 | * tail call counter throughout bpf2bpf calls combined with tailcalls |
| 3337 | */ |
| 3338 | if (tail_call_reachable) |
| 3339 | for (j = 0; j < frame; j++) |
| 3340 | subprog[ret_prog[j]].tail_call_reachable = true; |
| 3341 | if (subprog[0].tail_call_reachable) |
| 3342 | env->prog->aux->tail_call_reachable = true; |
| 3343 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3344 | /* end of for() loop means the last insn of the 'subprog' |
| 3345 | * was reached. Doesn't matter whether it was JA or EXIT |
| 3346 | */ |
| 3347 | if (frame == 0) |
| 3348 | return 0; |
| 3349 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
| 3350 | frame--; |
| 3351 | i = ret_insn[frame]; |
| 3352 | idx = ret_prog[frame]; |
| 3353 | goto continue_func; |
| 3354 | } |
| 3355 | |
| 3356 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
| 3357 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 3358 | const struct bpf_insn *insn, int idx) |
| 3359 | { |
| 3360 | int start = idx + insn->imm + 1, subprog; |
| 3361 | |
| 3362 | subprog = find_subprog(env, start); |
| 3363 | if (subprog < 0) { |
| 3364 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 3365 | start); |
| 3366 | return -EFAULT; |
| 3367 | } |
| 3368 | return env->subprog_info[subprog].stack_depth; |
| 3369 | } |
| 3370 | #endif |
| 3371 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3372 | int check_ctx_reg(struct bpf_verifier_env *env, |
| 3373 | const struct bpf_reg_state *reg, int regno) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3374 | { |
| 3375 | /* Access to ctx or passing it to a helper is only allowed in |
| 3376 | * its original, unmodified form. |
| 3377 | */ |
| 3378 | |
| 3379 | if (reg->off) { |
| 3380 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 3381 | regno, reg->off); |
| 3382 | return -EACCES; |
| 3383 | } |
| 3384 | |
| 3385 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 3386 | char tn_buf[48]; |
| 3387 | |
| 3388 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3389 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 3390 | return -EACCES; |
| 3391 | } |
| 3392 | |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3396 | static int __check_buffer_access(struct bpf_verifier_env *env, |
| 3397 | const char *buf_info, |
| 3398 | const struct bpf_reg_state *reg, |
| 3399 | int regno, int off, int size) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3400 | { |
| 3401 | if (off < 0) { |
| 3402 | verbose(env, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3403 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
| 3404 | regno, buf_info, off, size); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3405 | return -EACCES; |
| 3406 | } |
| 3407 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 3408 | char tn_buf[48]; |
| 3409 | |
| 3410 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3411 | verbose(env, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3412 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3413 | regno, off, tn_buf); |
| 3414 | return -EACCES; |
| 3415 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3416 | |
| 3417 | return 0; |
| 3418 | } |
| 3419 | |
| 3420 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
| 3421 | const struct bpf_reg_state *reg, |
| 3422 | int regno, int off, int size) |
| 3423 | { |
| 3424 | int err; |
| 3425 | |
| 3426 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); |
| 3427 | if (err) |
| 3428 | return err; |
| 3429 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3430 | if (off + size > env->prog->aux->max_tp_access) |
| 3431 | env->prog->aux->max_tp_access = off + size; |
| 3432 | |
| 3433 | return 0; |
| 3434 | } |
| 3435 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3436 | static int check_buffer_access(struct bpf_verifier_env *env, |
| 3437 | const struct bpf_reg_state *reg, |
| 3438 | int regno, int off, int size, |
| 3439 | bool zero_size_allowed, |
| 3440 | const char *buf_info, |
| 3441 | u32 *max_access) |
| 3442 | { |
| 3443 | int err; |
| 3444 | |
| 3445 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); |
| 3446 | if (err) |
| 3447 | return err; |
| 3448 | |
| 3449 | if (off + size > *max_access) |
| 3450 | *max_access = off + size; |
| 3451 | |
| 3452 | return 0; |
| 3453 | } |
| 3454 | |
| 3455 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
| 3456 | static void zext_32_to_64(struct bpf_reg_state *reg) |
| 3457 | { |
| 3458 | reg->var_off = tnum_subreg(reg->var_off); |
| 3459 | __reg_assign_32_into_64(reg); |
| 3460 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3461 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3462 | /* truncate register to smaller size (in bytes) |
| 3463 | * must be called with size < BPF_REG_SIZE |
| 3464 | */ |
| 3465 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 3466 | { |
| 3467 | u64 mask; |
| 3468 | |
| 3469 | /* clear high bits in bit representation */ |
| 3470 | reg->var_off = tnum_cast(reg->var_off, size); |
| 3471 | |
| 3472 | /* fix arithmetic bounds */ |
| 3473 | mask = ((u64)1 << (size * 8)) - 1; |
| 3474 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 3475 | reg->umin_value &= mask; |
| 3476 | reg->umax_value &= mask; |
| 3477 | } else { |
| 3478 | reg->umin_value = 0; |
| 3479 | reg->umax_value = mask; |
| 3480 | } |
| 3481 | reg->smin_value = reg->umin_value; |
| 3482 | reg->smax_value = reg->umax_value; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3483 | |
| 3484 | /* If size is smaller than 32bit register the 32bit register |
| 3485 | * values are also truncated so we push 64-bit bounds into |
| 3486 | * 32-bit bounds. Above were truncated < 32-bits already. |
| 3487 | */ |
| 3488 | if (size >= 4) |
| 3489 | return; |
| 3490 | __reg_combine_64_into_32(reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3493 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
| 3494 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3495 | /* A map is considered read-only if the following condition are true: |
| 3496 | * |
| 3497 | * 1) BPF program side cannot change any of the map content. The |
| 3498 | * BPF_F_RDONLY_PROG flag is throughout the lifetime of a map |
| 3499 | * and was set at map creation time. |
| 3500 | * 2) The map value(s) have been initialized from user space by a |
| 3501 | * loader and then "frozen", such that no new map update/delete |
| 3502 | * operations from syscall side are possible for the rest of |
| 3503 | * the map's lifetime from that point onwards. |
| 3504 | * 3) Any parallel/pending map update/delete operations from syscall |
| 3505 | * side have been completed. Only after that point, it's safe to |
| 3506 | * assume that map value(s) are immutable. |
| 3507 | */ |
| 3508 | return (map->map_flags & BPF_F_RDONLY_PROG) && |
| 3509 | READ_ONCE(map->frozen) && |
| 3510 | !bpf_map_write_active(map); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) |
| 3514 | { |
| 3515 | void *ptr; |
| 3516 | u64 addr; |
| 3517 | int err; |
| 3518 | |
| 3519 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 3520 | if (err) |
| 3521 | return err; |
| 3522 | ptr = (void *)(long)addr + off; |
| 3523 | |
| 3524 | switch (size) { |
| 3525 | case sizeof(u8): |
| 3526 | *val = (u64)*(u8 *)ptr; |
| 3527 | break; |
| 3528 | case sizeof(u16): |
| 3529 | *val = (u64)*(u16 *)ptr; |
| 3530 | break; |
| 3531 | case sizeof(u32): |
| 3532 | *val = (u64)*(u32 *)ptr; |
| 3533 | break; |
| 3534 | case sizeof(u64): |
| 3535 | *val = *(u64 *)ptr; |
| 3536 | break; |
| 3537 | default: |
| 3538 | return -EINVAL; |
| 3539 | } |
| 3540 | return 0; |
| 3541 | } |
| 3542 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3543 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
| 3544 | struct bpf_reg_state *regs, |
| 3545 | int regno, int off, int size, |
| 3546 | enum bpf_access_type atype, |
| 3547 | int value_regno) |
| 3548 | { |
| 3549 | struct bpf_reg_state *reg = regs + regno; |
| 3550 | const struct btf_type *t = btf_type_by_id(btf_vmlinux, reg->btf_id); |
| 3551 | const char *tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 3552 | u32 btf_id; |
| 3553 | int ret; |
| 3554 | |
| 3555 | if (off < 0) { |
| 3556 | verbose(env, |
| 3557 | "R%d is ptr_%s invalid negative access: off=%d\n", |
| 3558 | regno, tname, off); |
| 3559 | return -EACCES; |
| 3560 | } |
| 3561 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 3562 | char tn_buf[48]; |
| 3563 | |
| 3564 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3565 | verbose(env, |
| 3566 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", |
| 3567 | regno, tname, off, tn_buf); |
| 3568 | return -EACCES; |
| 3569 | } |
| 3570 | |
| 3571 | if (env->ops->btf_struct_access) { |
| 3572 | ret = env->ops->btf_struct_access(&env->log, t, off, size, |
| 3573 | atype, &btf_id); |
| 3574 | } else { |
| 3575 | if (atype != BPF_READ) { |
| 3576 | verbose(env, "only read is supported\n"); |
| 3577 | return -EACCES; |
| 3578 | } |
| 3579 | |
| 3580 | ret = btf_struct_access(&env->log, t, off, size, atype, |
| 3581 | &btf_id); |
| 3582 | } |
| 3583 | |
| 3584 | if (ret < 0) |
| 3585 | return ret; |
| 3586 | |
| 3587 | if (atype == BPF_READ && value_regno >= 0) |
| 3588 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_id); |
| 3589 | |
| 3590 | return 0; |
| 3591 | } |
| 3592 | |
| 3593 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, |
| 3594 | struct bpf_reg_state *regs, |
| 3595 | int regno, int off, int size, |
| 3596 | enum bpf_access_type atype, |
| 3597 | int value_regno) |
| 3598 | { |
| 3599 | struct bpf_reg_state *reg = regs + regno; |
| 3600 | struct bpf_map *map = reg->map_ptr; |
| 3601 | const struct btf_type *t; |
| 3602 | const char *tname; |
| 3603 | u32 btf_id; |
| 3604 | int ret; |
| 3605 | |
| 3606 | if (!btf_vmlinux) { |
| 3607 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); |
| 3608 | return -ENOTSUPP; |
| 3609 | } |
| 3610 | |
| 3611 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { |
| 3612 | verbose(env, "map_ptr access not supported for map type %d\n", |
| 3613 | map->map_type); |
| 3614 | return -ENOTSUPP; |
| 3615 | } |
| 3616 | |
| 3617 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); |
| 3618 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 3619 | |
| 3620 | if (!env->allow_ptr_to_map_access) { |
| 3621 | verbose(env, |
| 3622 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", |
| 3623 | tname); |
| 3624 | return -EPERM; |
| 3625 | } |
| 3626 | |
| 3627 | if (off < 0) { |
| 3628 | verbose(env, "R%d is %s invalid negative access: off=%d\n", |
| 3629 | regno, tname, off); |
| 3630 | return -EACCES; |
| 3631 | } |
| 3632 | |
| 3633 | if (atype != BPF_READ) { |
| 3634 | verbose(env, "only read from %s is supported\n", tname); |
| 3635 | return -EACCES; |
| 3636 | } |
| 3637 | |
| 3638 | ret = btf_struct_access(&env->log, t, off, size, atype, &btf_id); |
| 3639 | if (ret < 0) |
| 3640 | return ret; |
| 3641 | |
| 3642 | if (value_regno >= 0) |
| 3643 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_id); |
| 3644 | |
| 3645 | return 0; |
| 3646 | } |
| 3647 | |
| 3648 | /* Check that the stack access at the given offset is within bounds. The |
| 3649 | * maximum valid offset is -1. |
| 3650 | * |
| 3651 | * The minimum valid offset is -MAX_BPF_STACK for writes, and |
| 3652 | * -state->allocated_stack for reads. |
| 3653 | */ |
| 3654 | static int check_stack_slot_within_bounds(int off, |
| 3655 | struct bpf_func_state *state, |
| 3656 | enum bpf_access_type t) |
| 3657 | { |
| 3658 | int min_valid_off; |
| 3659 | |
| 3660 | if (t == BPF_WRITE) |
| 3661 | min_valid_off = -MAX_BPF_STACK; |
| 3662 | else |
| 3663 | min_valid_off = -state->allocated_stack; |
| 3664 | |
| 3665 | if (off < min_valid_off || off > -1) |
| 3666 | return -EACCES; |
| 3667 | return 0; |
| 3668 | } |
| 3669 | |
| 3670 | /* Check that the stack access at 'regno + off' falls within the maximum stack |
| 3671 | * bounds. |
| 3672 | * |
| 3673 | * 'off' includes `regno->offset`, but not its dynamic part (if any). |
| 3674 | */ |
| 3675 | static int check_stack_access_within_bounds( |
| 3676 | struct bpf_verifier_env *env, |
| 3677 | int regno, int off, int access_size, |
| 3678 | enum stack_access_src src, enum bpf_access_type type) |
| 3679 | { |
| 3680 | struct bpf_reg_state *regs = cur_regs(env); |
| 3681 | struct bpf_reg_state *reg = regs + regno; |
| 3682 | struct bpf_func_state *state = func(env, reg); |
| 3683 | int min_off, max_off; |
| 3684 | int err; |
| 3685 | char *err_extra; |
| 3686 | |
| 3687 | if (src == ACCESS_HELPER) |
| 3688 | /* We don't know if helpers are reading or writing (or both). */ |
| 3689 | err_extra = " indirect access to"; |
| 3690 | else if (type == BPF_READ) |
| 3691 | err_extra = " read from"; |
| 3692 | else |
| 3693 | err_extra = " write to"; |
| 3694 | |
| 3695 | if (tnum_is_const(reg->var_off)) { |
| 3696 | min_off = reg->var_off.value + off; |
| 3697 | if (access_size > 0) |
| 3698 | max_off = min_off + access_size - 1; |
| 3699 | else |
| 3700 | max_off = min_off; |
| 3701 | } else { |
| 3702 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
| 3703 | reg->smin_value <= -BPF_MAX_VAR_OFF) { |
| 3704 | verbose(env, "invalid unbounded variable-offset%s stack R%d\n", |
| 3705 | err_extra, regno); |
| 3706 | return -EACCES; |
| 3707 | } |
| 3708 | min_off = reg->smin_value + off; |
| 3709 | if (access_size > 0) |
| 3710 | max_off = reg->smax_value + off + access_size - 1; |
| 3711 | else |
| 3712 | max_off = min_off; |
| 3713 | } |
| 3714 | |
| 3715 | err = check_stack_slot_within_bounds(min_off, state, type); |
| 3716 | if (!err) |
| 3717 | err = check_stack_slot_within_bounds(max_off, state, type); |
| 3718 | |
| 3719 | if (err) { |
| 3720 | if (tnum_is_const(reg->var_off)) { |
| 3721 | verbose(env, "invalid%s stack R%d off=%d size=%d\n", |
| 3722 | err_extra, regno, off, access_size); |
| 3723 | } else { |
| 3724 | char tn_buf[48]; |
| 3725 | |
| 3726 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3727 | verbose(env, "invalid variable-offset%s stack R%d var_off=%s size=%d\n", |
| 3728 | err_extra, regno, tn_buf, access_size); |
| 3729 | } |
| 3730 | } |
| 3731 | return err; |
| 3732 | } |
| 3733 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3734 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 3735 | * if t==write, value_regno is a register which value is stored into memory |
| 3736 | * if t==read, value_regno is a register which will receive the value from memory |
| 3737 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 3738 | * if t==read && value_regno==-1, don't care what we read from memory |
| 3739 | */ |
| 3740 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 3741 | int off, int bpf_size, enum bpf_access_type t, |
| 3742 | int value_regno, bool strict_alignment_once) |
| 3743 | { |
| 3744 | struct bpf_reg_state *regs = cur_regs(env); |
| 3745 | struct bpf_reg_state *reg = regs + regno; |
| 3746 | struct bpf_func_state *state; |
| 3747 | int size, err = 0; |
| 3748 | |
| 3749 | size = bpf_size_to_bytes(bpf_size); |
| 3750 | if (size < 0) |
| 3751 | return size; |
| 3752 | |
| 3753 | /* alignment checks will add in reg->off themselves */ |
| 3754 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
| 3755 | if (err) |
| 3756 | return err; |
| 3757 | |
| 3758 | /* for access checks, reg->off is just part of off */ |
| 3759 | off += reg->off; |
| 3760 | |
| 3761 | if (reg->type == PTR_TO_MAP_VALUE) { |
| 3762 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3763 | is_pointer_value(env, value_regno)) { |
| 3764 | verbose(env, "R%d leaks addr into map\n", value_regno); |
| 3765 | return -EACCES; |
| 3766 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3767 | err = check_map_access_type(env, regno, off, size, t); |
| 3768 | if (err) |
| 3769 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3770 | err = check_map_access(env, regno, off, size, false); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3771 | if (!err && t == BPF_READ && value_regno >= 0) { |
| 3772 | struct bpf_map *map = reg->map_ptr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3773 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3774 | /* if map is read-only, track its contents as scalars */ |
| 3775 | if (tnum_is_const(reg->var_off) && |
| 3776 | bpf_map_is_rdonly(map) && |
| 3777 | map->ops->map_direct_value_addr) { |
| 3778 | int map_off = off + reg->var_off.value; |
| 3779 | u64 val = 0; |
| 3780 | |
| 3781 | err = bpf_map_direct_read(map, map_off, size, |
| 3782 | &val); |
| 3783 | if (err) |
| 3784 | return err; |
| 3785 | |
| 3786 | regs[value_regno].type = SCALAR_VALUE; |
| 3787 | __mark_reg_known(®s[value_regno], val); |
| 3788 | } else { |
| 3789 | mark_reg_unknown(env, regs, value_regno); |
| 3790 | } |
| 3791 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3792 | } else if (reg->type == PTR_TO_MEM) { |
| 3793 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3794 | is_pointer_value(env, value_regno)) { |
| 3795 | verbose(env, "R%d leaks addr into mem\n", value_regno); |
| 3796 | return -EACCES; |
| 3797 | } |
| 3798 | err = check_mem_region_access(env, regno, off, size, |
| 3799 | reg->mem_size, false); |
| 3800 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3801 | mark_reg_unknown(env, regs, value_regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3802 | } else if (reg->type == PTR_TO_CTX) { |
| 3803 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3804 | u32 btf_id = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3805 | |
| 3806 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3807 | is_pointer_value(env, value_regno)) { |
| 3808 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
| 3809 | return -EACCES; |
| 3810 | } |
| 3811 | |
| 3812 | err = check_ctx_reg(env, reg, regno); |
| 3813 | if (err < 0) |
| 3814 | return err; |
| 3815 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3816 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf_id); |
| 3817 | if (err) |
| 3818 | verbose_linfo(env, insn_idx, "; "); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3819 | if (!err && t == BPF_READ && value_regno >= 0) { |
| 3820 | /* ctx access returns either a scalar, or a |
| 3821 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 3822 | * case, we know the offset is zero. |
| 3823 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3824 | if (reg_type == SCALAR_VALUE) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3825 | mark_reg_unknown(env, regs, value_regno); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3826 | } else { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3827 | mark_reg_known_zero(env, regs, |
| 3828 | value_regno); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3829 | if (reg_type_may_be_null(reg_type)) |
| 3830 | regs[value_regno].id = ++env->id_gen; |
| 3831 | /* A load of ctx field could have different |
| 3832 | * actual load size with the one encoded in the |
| 3833 | * insn. When the dst is PTR, it is for sure not |
| 3834 | * a sub-register. |
| 3835 | */ |
| 3836 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3837 | if (reg_type == PTR_TO_BTF_ID || |
| 3838 | reg_type == PTR_TO_BTF_ID_OR_NULL) |
| 3839 | regs[value_regno].btf_id = btf_id; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3840 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3841 | regs[value_regno].type = reg_type; |
| 3842 | } |
| 3843 | |
| 3844 | } else if (reg->type == PTR_TO_STACK) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3845 | /* Basic bounds checks. */ |
| 3846 | err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3847 | if (err) |
| 3848 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3849 | |
| 3850 | state = func(env, reg); |
| 3851 | err = update_stack_depth(env, state, off); |
| 3852 | if (err) |
| 3853 | return err; |
| 3854 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3855 | if (t == BPF_READ) |
| 3856 | err = check_stack_read(env, regno, off, size, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3857 | value_regno); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3858 | else |
| 3859 | err = check_stack_write(env, regno, off, size, |
| 3860 | value_regno, insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3861 | } else if (reg_is_pkt_pointer(reg)) { |
| 3862 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
| 3863 | verbose(env, "cannot write into packet\n"); |
| 3864 | return -EACCES; |
| 3865 | } |
| 3866 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3867 | is_pointer_value(env, value_regno)) { |
| 3868 | verbose(env, "R%d leaks addr into packet\n", |
| 3869 | value_regno); |
| 3870 | return -EACCES; |
| 3871 | } |
| 3872 | err = check_packet_access(env, regno, off, size, false); |
| 3873 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3874 | mark_reg_unknown(env, regs, value_regno); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3875 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 3876 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3877 | is_pointer_value(env, value_regno)) { |
| 3878 | verbose(env, "R%d leaks addr into flow keys\n", |
| 3879 | value_regno); |
| 3880 | return -EACCES; |
| 3881 | } |
| 3882 | |
| 3883 | err = check_flow_keys_access(env, off, size); |
| 3884 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3885 | mark_reg_unknown(env, regs, value_regno); |
| 3886 | } else if (type_is_sk_pointer(reg->type)) { |
| 3887 | if (t == BPF_WRITE) { |
| 3888 | verbose(env, "R%d cannot write into %s\n", |
| 3889 | regno, reg_type_str[reg->type]); |
| 3890 | return -EACCES; |
| 3891 | } |
| 3892 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
| 3893 | if (!err && value_regno >= 0) |
| 3894 | mark_reg_unknown(env, regs, value_regno); |
| 3895 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
| 3896 | err = check_tp_buffer_access(env, reg, regno, off, size); |
| 3897 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3898 | mark_reg_unknown(env, regs, value_regno); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3899 | } else if (reg->type == PTR_TO_BTF_ID) { |
| 3900 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
| 3901 | value_regno); |
| 3902 | } else if (reg->type == CONST_PTR_TO_MAP) { |
| 3903 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, |
| 3904 | value_regno); |
| 3905 | } else if (reg->type == PTR_TO_RDONLY_BUF) { |
| 3906 | if (t == BPF_WRITE) { |
| 3907 | verbose(env, "R%d cannot write into %s\n", |
| 3908 | regno, reg_type_str[reg->type]); |
| 3909 | return -EACCES; |
| 3910 | } |
| 3911 | err = check_buffer_access(env, reg, regno, off, size, false, |
| 3912 | "rdonly", |
| 3913 | &env->prog->aux->max_rdonly_access); |
| 3914 | if (!err && value_regno >= 0) |
| 3915 | mark_reg_unknown(env, regs, value_regno); |
| 3916 | } else if (reg->type == PTR_TO_RDWR_BUF) { |
| 3917 | err = check_buffer_access(env, reg, regno, off, size, false, |
| 3918 | "rdwr", |
| 3919 | &env->prog->aux->max_rdwr_access); |
| 3920 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3921 | mark_reg_unknown(env, regs, value_regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3922 | } else { |
| 3923 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 3924 | reg_type_str[reg->type]); |
| 3925 | return -EACCES; |
| 3926 | } |
| 3927 | |
| 3928 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
| 3929 | regs[value_regno].type == SCALAR_VALUE) { |
| 3930 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
| 3931 | coerce_reg_to_size(®s[value_regno], size); |
| 3932 | } |
| 3933 | return err; |
| 3934 | } |
| 3935 | |
| 3936 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
| 3937 | { |
| 3938 | int err; |
| 3939 | |
| 3940 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 3941 | insn->imm != 0) { |
| 3942 | verbose(env, "BPF_XADD uses reserved fields\n"); |
| 3943 | return -EINVAL; |
| 3944 | } |
| 3945 | |
| 3946 | /* check src1 operand */ |
| 3947 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 3948 | if (err) |
| 3949 | return err; |
| 3950 | |
| 3951 | /* check src2 operand */ |
| 3952 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
| 3953 | if (err) |
| 3954 | return err; |
| 3955 | |
| 3956 | if (is_pointer_value(env, insn->src_reg)) { |
| 3957 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
| 3958 | return -EACCES; |
| 3959 | } |
| 3960 | |
| 3961 | if (is_ctx_reg(env, insn->dst_reg) || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3962 | is_pkt_reg(env, insn->dst_reg) || |
| 3963 | is_flow_key_reg(env, insn->dst_reg) || |
| 3964 | is_sk_reg(env, insn->dst_reg)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3965 | verbose(env, "BPF_XADD stores into R%d %s is not allowed\n", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3966 | insn->dst_reg, |
| 3967 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3968 | return -EACCES; |
| 3969 | } |
| 3970 | |
| 3971 | /* check whether atomic_add can read the memory */ |
| 3972 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
| 3973 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
| 3974 | if (err) |
| 3975 | return err; |
| 3976 | |
| 3977 | /* check whether atomic_add can write into the same memory */ |
| 3978 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
| 3979 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
| 3980 | } |
| 3981 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3982 | /* When register 'regno' is used to read the stack (either directly or through |
| 3983 | * a helper function) make sure that it's within stack boundary and, depending |
| 3984 | * on the access type, that all elements of the stack are initialized. |
| 3985 | * |
| 3986 | * 'off' includes 'regno->off', but not its dynamic part (if any). |
| 3987 | * |
| 3988 | * All registers that have been spilled on the stack in the slots within the |
| 3989 | * read offsets are marked as read. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3990 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3991 | static int check_stack_range_initialized( |
| 3992 | struct bpf_verifier_env *env, int regno, int off, |
| 3993 | int access_size, bool zero_size_allowed, |
| 3994 | enum stack_access_src type, struct bpf_call_arg_meta *meta) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3995 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3996 | struct bpf_reg_state *reg = reg_state(env, regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3997 | struct bpf_func_state *state = func(env, reg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3998 | int err, min_off, max_off, i, j, slot, spi; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3999 | char *err_extra = type == ACCESS_HELPER ? " indirect" : ""; |
| 4000 | enum bpf_access_type bounds_check_type; |
| 4001 | /* Some accesses can write anything into the stack, others are |
| 4002 | * read-only. |
| 4003 | */ |
| 4004 | bool clobber = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4005 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4006 | if (access_size == 0 && !zero_size_allowed) { |
| 4007 | verbose(env, "invalid zero-sized read\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4008 | return -EACCES; |
| 4009 | } |
| 4010 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4011 | if (type == ACCESS_HELPER) { |
| 4012 | /* The bounds checks for writes are more permissive than for |
| 4013 | * reads. However, if raw_mode is not set, we'll do extra |
| 4014 | * checks below. |
| 4015 | */ |
| 4016 | bounds_check_type = BPF_WRITE; |
| 4017 | clobber = true; |
| 4018 | } else { |
| 4019 | bounds_check_type = BPF_READ; |
| 4020 | } |
| 4021 | err = check_stack_access_within_bounds(env, regno, off, access_size, |
| 4022 | type, bounds_check_type); |
| 4023 | if (err) |
| 4024 | return err; |
| 4025 | |
| 4026 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4027 | if (tnum_is_const(reg->var_off)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4028 | min_off = max_off = reg->var_off.value + off; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4029 | } else { |
| 4030 | /* Variable offset is prohibited for unprivileged mode for |
| 4031 | * simplicity since it requires corresponding support in |
| 4032 | * Spectre masking for stack ALU. |
| 4033 | * See also retrieve_ptr_limit(). |
| 4034 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4035 | if (!env->bypass_spec_v1) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4036 | char tn_buf[48]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4037 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4038 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4039 | verbose(env, "R%d%s variable offset stack access prohibited for !root, var_off=%s\n", |
| 4040 | regno, err_extra, tn_buf); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4041 | return -EACCES; |
| 4042 | } |
| 4043 | /* Only initialized buffer on stack is allowed to be accessed |
| 4044 | * with variable offset. With uninitialized buffer it's hard to |
| 4045 | * guarantee that whole memory is marked as initialized on |
| 4046 | * helper return since specific bounds are unknown what may |
| 4047 | * cause uninitialized stack leaking. |
| 4048 | */ |
| 4049 | if (meta && meta->raw_mode) |
| 4050 | meta = NULL; |
| 4051 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4052 | min_off = reg->smin_value + off; |
| 4053 | max_off = reg->smax_value + off; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | if (meta && meta->raw_mode) { |
| 4057 | meta->access_size = access_size; |
| 4058 | meta->regno = regno; |
| 4059 | return 0; |
| 4060 | } |
| 4061 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4062 | for (i = min_off; i < max_off + access_size; i++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4063 | u8 *stype; |
| 4064 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4065 | slot = -i - 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4066 | spi = slot / BPF_REG_SIZE; |
| 4067 | if (state->allocated_stack <= slot) |
| 4068 | goto err; |
| 4069 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 4070 | if (*stype == STACK_MISC) |
| 4071 | goto mark; |
| 4072 | if (*stype == STACK_ZERO) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4073 | if (clobber) { |
| 4074 | /* helper can write anything into the stack */ |
| 4075 | *stype = STACK_MISC; |
| 4076 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4077 | goto mark; |
| 4078 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4079 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4080 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4081 | state->stack[spi].spilled_ptr.type == PTR_TO_BTF_ID) |
| 4082 | goto mark; |
| 4083 | |
| 4084 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 4085 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
| 4086 | env->allow_ptr_leaks)) { |
| 4087 | if (clobber) { |
| 4088 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
| 4089 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 4090 | state->stack[spi].slot_type[j] = STACK_MISC; |
| 4091 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4092 | goto mark; |
| 4093 | } |
| 4094 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4095 | err: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4096 | if (tnum_is_const(reg->var_off)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4097 | verbose(env, "invalid%s read from stack R%d off %d+%d size %d\n", |
| 4098 | err_extra, regno, min_off, i - min_off, access_size); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4099 | } else { |
| 4100 | char tn_buf[48]; |
| 4101 | |
| 4102 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4103 | verbose(env, "invalid%s read from stack R%d var_off %s+%d size %d\n", |
| 4104 | err_extra, regno, tn_buf, i - min_off, access_size); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4105 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4106 | return -EACCES; |
| 4107 | mark: |
| 4108 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 4109 | * the whole slot to be marked as 'read' |
| 4110 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4111 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
| 4112 | state->stack[spi].spilled_ptr.parent, |
| 4113 | REG_LIVE_READ64); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4114 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4115 | return update_stack_depth(env, state, min_off); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 4119 | int access_size, bool zero_size_allowed, |
| 4120 | struct bpf_call_arg_meta *meta) |
| 4121 | { |
| 4122 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 4123 | |
| 4124 | switch (reg->type) { |
| 4125 | case PTR_TO_PACKET: |
| 4126 | case PTR_TO_PACKET_META: |
| 4127 | return check_packet_access(env, regno, reg->off, access_size, |
| 4128 | zero_size_allowed); |
| 4129 | case PTR_TO_MAP_VALUE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4130 | if (check_map_access_type(env, regno, reg->off, access_size, |
| 4131 | meta && meta->raw_mode ? BPF_WRITE : |
| 4132 | BPF_READ)) |
| 4133 | return -EACCES; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4134 | return check_map_access(env, regno, reg->off, access_size, |
| 4135 | zero_size_allowed); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4136 | case PTR_TO_MEM: |
| 4137 | return check_mem_region_access(env, regno, reg->off, |
| 4138 | access_size, reg->mem_size, |
| 4139 | zero_size_allowed); |
| 4140 | case PTR_TO_RDONLY_BUF: |
| 4141 | if (meta && meta->raw_mode) |
| 4142 | return -EACCES; |
| 4143 | return check_buffer_access(env, reg, regno, reg->off, |
| 4144 | access_size, zero_size_allowed, |
| 4145 | "rdonly", |
| 4146 | &env->prog->aux->max_rdonly_access); |
| 4147 | case PTR_TO_RDWR_BUF: |
| 4148 | return check_buffer_access(env, reg, regno, reg->off, |
| 4149 | access_size, zero_size_allowed, |
| 4150 | "rdwr", |
| 4151 | &env->prog->aux->max_rdwr_access); |
| 4152 | case PTR_TO_STACK: |
| 4153 | return check_stack_range_initialized( |
| 4154 | env, |
| 4155 | regno, reg->off, access_size, |
| 4156 | zero_size_allowed, ACCESS_HELPER, meta); |
| 4157 | default: /* scalar_value or invalid ptr */ |
| 4158 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
| 4159 | if (zero_size_allowed && access_size == 0 && |
| 4160 | register_is_null(reg)) |
| 4161 | return 0; |
| 4162 | |
| 4163 | verbose(env, "R%d type=%s expected=%s\n", regno, |
| 4164 | reg_type_str[reg->type], |
| 4165 | reg_type_str[PTR_TO_STACK]); |
| 4166 | return -EACCES; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4167 | } |
| 4168 | } |
| 4169 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4170 | /* Implementation details: |
| 4171 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL |
| 4172 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
| 4173 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after |
| 4174 | * value_or_null->value transition, since the verifier only cares about |
| 4175 | * the range of access to valid map value pointer and doesn't care about actual |
| 4176 | * address of the map element. |
| 4177 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
| 4178 | * reg->id > 0 after value_or_null->value transition. By doing so |
| 4179 | * two bpf_map_lookups will be considered two different pointers that |
| 4180 | * point to different bpf_spin_locks. |
| 4181 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
| 4182 | * dead-locks. |
| 4183 | * Since only one bpf_spin_lock is allowed the checks are simpler than |
| 4184 | * reg_is_refcounted() logic. The verifier needs to remember only |
| 4185 | * one spin_lock instead of array of acquired_refs. |
| 4186 | * cur_state->active_spin_lock remembers which map value element got locked |
| 4187 | * and clears it after bpf_spin_unlock. |
| 4188 | */ |
| 4189 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, |
| 4190 | bool is_lock) |
| 4191 | { |
| 4192 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 4193 | struct bpf_verifier_state *cur = env->cur_state; |
| 4194 | bool is_const = tnum_is_const(reg->var_off); |
| 4195 | struct bpf_map *map = reg->map_ptr; |
| 4196 | u64 val = reg->var_off.value; |
| 4197 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4198 | if (!is_const) { |
| 4199 | verbose(env, |
| 4200 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", |
| 4201 | regno); |
| 4202 | return -EINVAL; |
| 4203 | } |
| 4204 | if (!map->btf) { |
| 4205 | verbose(env, |
| 4206 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", |
| 4207 | map->name); |
| 4208 | return -EINVAL; |
| 4209 | } |
| 4210 | if (!map_value_has_spin_lock(map)) { |
| 4211 | if (map->spin_lock_off == -E2BIG) |
| 4212 | verbose(env, |
| 4213 | "map '%s' has more than one 'struct bpf_spin_lock'\n", |
| 4214 | map->name); |
| 4215 | else if (map->spin_lock_off == -ENOENT) |
| 4216 | verbose(env, |
| 4217 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", |
| 4218 | map->name); |
| 4219 | else |
| 4220 | verbose(env, |
| 4221 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", |
| 4222 | map->name); |
| 4223 | return -EINVAL; |
| 4224 | } |
| 4225 | if (map->spin_lock_off != val + reg->off) { |
| 4226 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", |
| 4227 | val + reg->off); |
| 4228 | return -EINVAL; |
| 4229 | } |
| 4230 | if (is_lock) { |
| 4231 | if (cur->active_spin_lock) { |
| 4232 | verbose(env, |
| 4233 | "Locking two bpf_spin_locks are not allowed\n"); |
| 4234 | return -EINVAL; |
| 4235 | } |
| 4236 | cur->active_spin_lock = reg->id; |
| 4237 | } else { |
| 4238 | if (!cur->active_spin_lock) { |
| 4239 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
| 4240 | return -EINVAL; |
| 4241 | } |
| 4242 | if (cur->active_spin_lock != reg->id) { |
| 4243 | verbose(env, "bpf_spin_unlock of different lock\n"); |
| 4244 | return -EINVAL; |
| 4245 | } |
| 4246 | cur->active_spin_lock = 0; |
| 4247 | } |
| 4248 | return 0; |
| 4249 | } |
| 4250 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4251 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 4252 | { |
| 4253 | return type == ARG_PTR_TO_MEM || |
| 4254 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 4255 | type == ARG_PTR_TO_UNINIT_MEM; |
| 4256 | } |
| 4257 | |
| 4258 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 4259 | { |
| 4260 | return type == ARG_CONST_SIZE || |
| 4261 | type == ARG_CONST_SIZE_OR_ZERO; |
| 4262 | } |
| 4263 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4264 | static bool arg_type_is_alloc_size(enum bpf_arg_type type) |
| 4265 | { |
| 4266 | return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; |
| 4267 | } |
| 4268 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4269 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
| 4270 | { |
| 4271 | return type == ARG_PTR_TO_INT || |
| 4272 | type == ARG_PTR_TO_LONG; |
| 4273 | } |
| 4274 | |
| 4275 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
| 4276 | { |
| 4277 | if (type == ARG_PTR_TO_INT) |
| 4278 | return sizeof(u32); |
| 4279 | else if (type == ARG_PTR_TO_LONG) |
| 4280 | return sizeof(u64); |
| 4281 | |
| 4282 | return -EINVAL; |
| 4283 | } |
| 4284 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4285 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
| 4286 | const struct bpf_call_arg_meta *meta, |
| 4287 | enum bpf_arg_type *arg_type) |
| 4288 | { |
| 4289 | if (!meta->map_ptr) { |
| 4290 | /* kernel subsystem misconfigured verifier */ |
| 4291 | verbose(env, "invalid map_ptr to access map->type\n"); |
| 4292 | return -EACCES; |
| 4293 | } |
| 4294 | |
| 4295 | switch (meta->map_ptr->map_type) { |
| 4296 | case BPF_MAP_TYPE_SOCKMAP: |
| 4297 | case BPF_MAP_TYPE_SOCKHASH: |
| 4298 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 4299 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
| 4300 | } else { |
| 4301 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); |
| 4302 | return -EINVAL; |
| 4303 | } |
| 4304 | break; |
| 4305 | |
| 4306 | default: |
| 4307 | break; |
| 4308 | } |
| 4309 | return 0; |
| 4310 | } |
| 4311 | |
| 4312 | struct bpf_reg_types { |
| 4313 | const enum bpf_reg_type types[10]; |
| 4314 | u32 *btf_id; |
| 4315 | }; |
| 4316 | |
| 4317 | static const struct bpf_reg_types map_key_value_types = { |
| 4318 | .types = { |
| 4319 | PTR_TO_STACK, |
| 4320 | PTR_TO_PACKET, |
| 4321 | PTR_TO_PACKET_META, |
| 4322 | PTR_TO_MAP_VALUE, |
| 4323 | }, |
| 4324 | }; |
| 4325 | |
| 4326 | static const struct bpf_reg_types sock_types = { |
| 4327 | .types = { |
| 4328 | PTR_TO_SOCK_COMMON, |
| 4329 | PTR_TO_SOCKET, |
| 4330 | PTR_TO_TCP_SOCK, |
| 4331 | PTR_TO_XDP_SOCK, |
| 4332 | }, |
| 4333 | }; |
| 4334 | |
| 4335 | #ifdef CONFIG_NET |
| 4336 | static const struct bpf_reg_types btf_id_sock_common_types = { |
| 4337 | .types = { |
| 4338 | PTR_TO_SOCK_COMMON, |
| 4339 | PTR_TO_SOCKET, |
| 4340 | PTR_TO_TCP_SOCK, |
| 4341 | PTR_TO_XDP_SOCK, |
| 4342 | PTR_TO_BTF_ID, |
| 4343 | }, |
| 4344 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], |
| 4345 | }; |
| 4346 | #endif |
| 4347 | |
| 4348 | static const struct bpf_reg_types mem_types = { |
| 4349 | .types = { |
| 4350 | PTR_TO_STACK, |
| 4351 | PTR_TO_PACKET, |
| 4352 | PTR_TO_PACKET_META, |
| 4353 | PTR_TO_MAP_VALUE, |
| 4354 | PTR_TO_MEM, |
| 4355 | PTR_TO_RDONLY_BUF, |
| 4356 | PTR_TO_RDWR_BUF, |
| 4357 | }, |
| 4358 | }; |
| 4359 | |
| 4360 | static const struct bpf_reg_types int_ptr_types = { |
| 4361 | .types = { |
| 4362 | PTR_TO_STACK, |
| 4363 | PTR_TO_PACKET, |
| 4364 | PTR_TO_PACKET_META, |
| 4365 | PTR_TO_MAP_VALUE, |
| 4366 | }, |
| 4367 | }; |
| 4368 | |
| 4369 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; |
| 4370 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; |
| 4371 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; |
| 4372 | static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } }; |
| 4373 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; |
| 4374 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; |
| 4375 | static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; |
| 4376 | static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_PERCPU_BTF_ID } }; |
| 4377 | |
| 4378 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
| 4379 | [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, |
| 4380 | [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, |
| 4381 | [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, |
| 4382 | [ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types, |
| 4383 | [ARG_CONST_SIZE] = &scalar_types, |
| 4384 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, |
| 4385 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, |
| 4386 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, |
| 4387 | [ARG_PTR_TO_CTX] = &context_types, |
| 4388 | [ARG_PTR_TO_CTX_OR_NULL] = &context_types, |
| 4389 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
| 4390 | #ifdef CONFIG_NET |
| 4391 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
| 4392 | #endif |
| 4393 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
| 4394 | [ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types, |
| 4395 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
| 4396 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, |
| 4397 | [ARG_PTR_TO_MEM] = &mem_types, |
| 4398 | [ARG_PTR_TO_MEM_OR_NULL] = &mem_types, |
| 4399 | [ARG_PTR_TO_UNINIT_MEM] = &mem_types, |
| 4400 | [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, |
| 4401 | [ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types, |
| 4402 | [ARG_PTR_TO_INT] = &int_ptr_types, |
| 4403 | [ARG_PTR_TO_LONG] = &int_ptr_types, |
| 4404 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
| 4405 | }; |
| 4406 | |
| 4407 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4408 | enum bpf_arg_type arg_type, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4409 | const u32 *arg_btf_id) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4410 | { |
| 4411 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4412 | enum bpf_reg_type expected, type = reg->type; |
| 4413 | const struct bpf_reg_types *compatible; |
| 4414 | int i, j; |
| 4415 | |
| 4416 | compatible = compatible_reg_types[arg_type]; |
| 4417 | if (!compatible) { |
| 4418 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); |
| 4419 | return -EFAULT; |
| 4420 | } |
| 4421 | |
| 4422 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
| 4423 | expected = compatible->types[i]; |
| 4424 | if (expected == NOT_INIT) |
| 4425 | break; |
| 4426 | |
| 4427 | if (type == expected) |
| 4428 | goto found; |
| 4429 | } |
| 4430 | |
| 4431 | verbose(env, "R%d type=%s expected=", regno, reg_type_str[type]); |
| 4432 | for (j = 0; j + 1 < i; j++) |
| 4433 | verbose(env, "%s, ", reg_type_str[compatible->types[j]]); |
| 4434 | verbose(env, "%s\n", reg_type_str[compatible->types[j]]); |
| 4435 | return -EACCES; |
| 4436 | |
| 4437 | found: |
| 4438 | if (type == PTR_TO_BTF_ID) { |
| 4439 | if (!arg_btf_id) { |
| 4440 | if (!compatible->btf_id) { |
| 4441 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); |
| 4442 | return -EFAULT; |
| 4443 | } |
| 4444 | arg_btf_id = compatible->btf_id; |
| 4445 | } |
| 4446 | |
| 4447 | if (!btf_struct_ids_match(&env->log, reg->off, reg->btf_id, |
| 4448 | *arg_btf_id)) { |
| 4449 | verbose(env, "R%d is of type %s but %s is expected\n", |
| 4450 | regno, kernel_type_name(reg->btf_id), |
| 4451 | kernel_type_name(*arg_btf_id)); |
| 4452 | return -EACCES; |
| 4453 | } |
| 4454 | |
| 4455 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 4456 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", |
| 4457 | regno); |
| 4458 | return -EACCES; |
| 4459 | } |
| 4460 | } |
| 4461 | |
| 4462 | return 0; |
| 4463 | } |
| 4464 | |
| 4465 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
| 4466 | struct bpf_call_arg_meta *meta, |
| 4467 | const struct bpf_func_proto *fn) |
| 4468 | { |
| 4469 | u32 regno = BPF_REG_1 + arg; |
| 4470 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 4471 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
| 4472 | enum bpf_reg_type type = reg->type; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4473 | int err = 0; |
| 4474 | |
| 4475 | if (arg_type == ARG_DONTCARE) |
| 4476 | return 0; |
| 4477 | |
| 4478 | err = check_reg_arg(env, regno, SRC_OP); |
| 4479 | if (err) |
| 4480 | return err; |
| 4481 | |
| 4482 | if (arg_type == ARG_ANYTHING) { |
| 4483 | if (is_pointer_value(env, regno)) { |
| 4484 | verbose(env, "R%d leaks addr into helper function\n", |
| 4485 | regno); |
| 4486 | return -EACCES; |
| 4487 | } |
| 4488 | return 0; |
| 4489 | } |
| 4490 | |
| 4491 | if (type_is_pkt_pointer(type) && |
| 4492 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
| 4493 | verbose(env, "helper access to the packet is not allowed\n"); |
| 4494 | return -EACCES; |
| 4495 | } |
| 4496 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4497 | if (arg_type == ARG_PTR_TO_MAP_VALUE || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4498 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
| 4499 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4500 | err = resolve_map_arg_type(env, meta, &arg_type); |
| 4501 | if (err) |
| 4502 | return err; |
| 4503 | } |
| 4504 | |
| 4505 | if (register_is_null(reg) && arg_type_may_be_null(arg_type)) |
| 4506 | /* A NULL register has a SCALAR_VALUE type, so skip |
| 4507 | * type checking. |
| 4508 | */ |
| 4509 | goto skip_type_check; |
| 4510 | |
| 4511 | err = check_reg_type(env, regno, arg_type, fn->arg_btf_id[arg]); |
| 4512 | if (err) |
| 4513 | return err; |
| 4514 | |
| 4515 | if (type == PTR_TO_CTX) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4516 | err = check_ctx_reg(env, reg, regno); |
| 4517 | if (err < 0) |
| 4518 | return err; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4519 | } |
| 4520 | |
| 4521 | skip_type_check: |
| 4522 | if (reg->ref_obj_id) { |
| 4523 | if (meta->ref_obj_id) { |
| 4524 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", |
| 4525 | regno, reg->ref_obj_id, |
| 4526 | meta->ref_obj_id); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4527 | return -EFAULT; |
| 4528 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4529 | meta->ref_obj_id = reg->ref_obj_id; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 4533 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
| 4534 | meta->map_ptr = reg->map_ptr; |
| 4535 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 4536 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 4537 | * check that [key, key + map->key_size) are within |
| 4538 | * stack limits and initialized |
| 4539 | */ |
| 4540 | if (!meta->map_ptr) { |
| 4541 | /* in function declaration map_ptr must come before |
| 4542 | * map_key, so that it's verified and known before |
| 4543 | * we have to check map_key here. Otherwise it means |
| 4544 | * that kernel subsystem misconfigured verifier |
| 4545 | */ |
| 4546 | verbose(env, "invalid map_ptr to access map->key\n"); |
| 4547 | return -EACCES; |
| 4548 | } |
| 4549 | err = check_helper_mem_access(env, regno, |
| 4550 | meta->map_ptr->key_size, false, |
| 4551 | NULL); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4552 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
| 4553 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
| 4554 | !register_is_null(reg)) || |
| 4555 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4556 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 4557 | * check [value, value + map->value_size) validity |
| 4558 | */ |
| 4559 | if (!meta->map_ptr) { |
| 4560 | /* kernel subsystem misconfigured verifier */ |
| 4561 | verbose(env, "invalid map_ptr to access map->value\n"); |
| 4562 | return -EACCES; |
| 4563 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4564 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4565 | err = check_helper_mem_access(env, regno, |
| 4566 | meta->map_ptr->value_size, false, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4567 | meta); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4568 | } else if (arg_type == ARG_PTR_TO_PERCPU_BTF_ID) { |
| 4569 | if (!reg->btf_id) { |
| 4570 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); |
| 4571 | return -EACCES; |
| 4572 | } |
| 4573 | meta->ret_btf_id = reg->btf_id; |
| 4574 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
| 4575 | if (meta->func_id == BPF_FUNC_spin_lock) { |
| 4576 | if (process_spin_lock(env, regno, true)) |
| 4577 | return -EACCES; |
| 4578 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
| 4579 | if (process_spin_lock(env, regno, false)) |
| 4580 | return -EACCES; |
| 4581 | } else { |
| 4582 | verbose(env, "verifier internal error\n"); |
| 4583 | return -EFAULT; |
| 4584 | } |
| 4585 | } else if (arg_type_is_mem_ptr(arg_type)) { |
| 4586 | /* The access to this pointer is only checked when we hit the |
| 4587 | * next is_mem_size argument below. |
| 4588 | */ |
| 4589 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4590 | } else if (arg_type_is_mem_size(arg_type)) { |
| 4591 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
| 4592 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4593 | /* This is used to refine r0 return value bounds for helpers |
| 4594 | * that enforce this value as an upper bound on return values. |
| 4595 | * See do_refine_retval_range() for helpers that can refine |
| 4596 | * the return value. C type of helper is u32 so we pull register |
| 4597 | * bound from umax_value however, if negative verifier errors |
| 4598 | * out. Only upper bounds can be learned because retval is an |
| 4599 | * int type and negative retvals are allowed. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4600 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 4601 | meta->msize_max_value = reg->umax_value; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4602 | |
| 4603 | /* The register is SCALAR_VALUE; the access check |
| 4604 | * happens using its boundaries. |
| 4605 | */ |
| 4606 | if (!tnum_is_const(reg->var_off)) |
| 4607 | /* For unprivileged variable accesses, disable raw |
| 4608 | * mode so that the program is required to |
| 4609 | * initialize all the memory that the helper could |
| 4610 | * just partially fill up. |
| 4611 | */ |
| 4612 | meta = NULL; |
| 4613 | |
| 4614 | if (reg->smin_value < 0) { |
| 4615 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
| 4616 | regno); |
| 4617 | return -EACCES; |
| 4618 | } |
| 4619 | |
| 4620 | if (reg->umin_value == 0) { |
| 4621 | err = check_helper_mem_access(env, regno - 1, 0, |
| 4622 | zero_size_allowed, |
| 4623 | meta); |
| 4624 | if (err) |
| 4625 | return err; |
| 4626 | } |
| 4627 | |
| 4628 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
| 4629 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
| 4630 | regno); |
| 4631 | return -EACCES; |
| 4632 | } |
| 4633 | err = check_helper_mem_access(env, regno - 1, |
| 4634 | reg->umax_value, |
| 4635 | zero_size_allowed, meta); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4636 | if (!err) |
| 4637 | err = mark_chain_precision(env, regno); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4638 | } else if (arg_type_is_alloc_size(arg_type)) { |
| 4639 | if (!tnum_is_const(reg->var_off)) { |
| 4640 | verbose(env, "R%d unbounded size, use 'var &= const' or 'if (var < const)'\n", |
| 4641 | regno); |
| 4642 | return -EACCES; |
| 4643 | } |
| 4644 | meta->mem_size = reg->var_off.value; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4645 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 4646 | int size = int_ptr_type_to_size(arg_type); |
| 4647 | |
| 4648 | err = check_helper_mem_access(env, regno, size, false, meta); |
| 4649 | if (err) |
| 4650 | return err; |
| 4651 | err = check_ptr_alignment(env, reg, 0, size, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | return err; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4655 | } |
| 4656 | |
| 4657 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
| 4658 | { |
| 4659 | enum bpf_attach_type eatype = env->prog->expected_attach_type; |
| 4660 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
| 4661 | |
| 4662 | if (func_id != BPF_FUNC_map_update_elem) |
| 4663 | return false; |
| 4664 | |
| 4665 | /* It's not possible to get access to a locked struct sock in these |
| 4666 | * contexts, so updating is safe. |
| 4667 | */ |
| 4668 | switch (type) { |
| 4669 | case BPF_PROG_TYPE_TRACING: |
| 4670 | if (eatype == BPF_TRACE_ITER) |
| 4671 | return true; |
| 4672 | break; |
| 4673 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 4674 | case BPF_PROG_TYPE_SCHED_CLS: |
| 4675 | case BPF_PROG_TYPE_SCHED_ACT: |
| 4676 | case BPF_PROG_TYPE_XDP: |
| 4677 | case BPF_PROG_TYPE_SK_REUSEPORT: |
| 4678 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
| 4679 | case BPF_PROG_TYPE_SK_LOOKUP: |
| 4680 | return true; |
| 4681 | default: |
| 4682 | break; |
| 4683 | } |
| 4684 | |
| 4685 | verbose(env, "cannot update sockmap in this context\n"); |
| 4686 | return false; |
| 4687 | } |
| 4688 | |
| 4689 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
| 4690 | { |
| 4691 | return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 4695 | struct bpf_map *map, int func_id) |
| 4696 | { |
| 4697 | if (!map) |
| 4698 | return 0; |
| 4699 | |
| 4700 | /* We need a two way check, first is from map perspective ... */ |
| 4701 | switch (map->map_type) { |
| 4702 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 4703 | if (func_id != BPF_FUNC_tail_call) |
| 4704 | goto error; |
| 4705 | break; |
| 4706 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 4707 | if (func_id != BPF_FUNC_perf_event_read && |
| 4708 | func_id != BPF_FUNC_perf_event_output && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4709 | func_id != BPF_FUNC_skb_output && |
| 4710 | func_id != BPF_FUNC_perf_event_read_value && |
| 4711 | func_id != BPF_FUNC_xdp_output) |
| 4712 | goto error; |
| 4713 | break; |
| 4714 | case BPF_MAP_TYPE_RINGBUF: |
| 4715 | if (func_id != BPF_FUNC_ringbuf_output && |
| 4716 | func_id != BPF_FUNC_ringbuf_reserve && |
| 4717 | func_id != BPF_FUNC_ringbuf_query) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4718 | goto error; |
| 4719 | break; |
| 4720 | case BPF_MAP_TYPE_STACK_TRACE: |
| 4721 | if (func_id != BPF_FUNC_get_stackid) |
| 4722 | goto error; |
| 4723 | break; |
| 4724 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
| 4725 | if (func_id != BPF_FUNC_skb_under_cgroup && |
| 4726 | func_id != BPF_FUNC_current_task_under_cgroup) |
| 4727 | goto error; |
| 4728 | break; |
| 4729 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4730 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4731 | if (func_id != BPF_FUNC_get_local_storage) |
| 4732 | goto error; |
| 4733 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4734 | case BPF_MAP_TYPE_DEVMAP: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4735 | case BPF_MAP_TYPE_DEVMAP_HASH: |
| 4736 | if (func_id != BPF_FUNC_redirect_map && |
| 4737 | func_id != BPF_FUNC_map_lookup_elem) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4738 | goto error; |
| 4739 | break; |
| 4740 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 4741 | * appear. |
| 4742 | */ |
| 4743 | case BPF_MAP_TYPE_CPUMAP: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4744 | if (func_id != BPF_FUNC_redirect_map) |
| 4745 | goto error; |
| 4746 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4747 | case BPF_MAP_TYPE_XSKMAP: |
| 4748 | if (func_id != BPF_FUNC_redirect_map && |
| 4749 | func_id != BPF_FUNC_map_lookup_elem) |
| 4750 | goto error; |
| 4751 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4752 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
| 4753 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
| 4754 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 4755 | goto error; |
| 4756 | break; |
| 4757 | case BPF_MAP_TYPE_SOCKMAP: |
| 4758 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 4759 | func_id != BPF_FUNC_sock_map_update && |
| 4760 | func_id != BPF_FUNC_map_delete_elem && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4761 | func_id != BPF_FUNC_msg_redirect_map && |
| 4762 | func_id != BPF_FUNC_sk_select_reuseport && |
| 4763 | func_id != BPF_FUNC_map_lookup_elem && |
| 4764 | !may_update_sockmap(env, func_id)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4765 | goto error; |
| 4766 | break; |
| 4767 | case BPF_MAP_TYPE_SOCKHASH: |
| 4768 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 4769 | func_id != BPF_FUNC_sock_hash_update && |
| 4770 | func_id != BPF_FUNC_map_delete_elem && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4771 | func_id != BPF_FUNC_msg_redirect_hash && |
| 4772 | func_id != BPF_FUNC_sk_select_reuseport && |
| 4773 | func_id != BPF_FUNC_map_lookup_elem && |
| 4774 | !may_update_sockmap(env, func_id)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4775 | goto error; |
| 4776 | break; |
| 4777 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 4778 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 4779 | goto error; |
| 4780 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4781 | case BPF_MAP_TYPE_QUEUE: |
| 4782 | case BPF_MAP_TYPE_STACK: |
| 4783 | if (func_id != BPF_FUNC_map_peek_elem && |
| 4784 | func_id != BPF_FUNC_map_pop_elem && |
| 4785 | func_id != BPF_FUNC_map_push_elem) |
| 4786 | goto error; |
| 4787 | break; |
| 4788 | case BPF_MAP_TYPE_SK_STORAGE: |
| 4789 | if (func_id != BPF_FUNC_sk_storage_get && |
| 4790 | func_id != BPF_FUNC_sk_storage_delete) |
| 4791 | goto error; |
| 4792 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4793 | case BPF_MAP_TYPE_INODE_STORAGE: |
| 4794 | if (func_id != BPF_FUNC_inode_storage_get && |
| 4795 | func_id != BPF_FUNC_inode_storage_delete) |
| 4796 | goto error; |
| 4797 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4798 | default: |
| 4799 | break; |
| 4800 | } |
| 4801 | |
| 4802 | /* ... and second from the function itself. */ |
| 4803 | switch (func_id) { |
| 4804 | case BPF_FUNC_tail_call: |
| 4805 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 4806 | goto error; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4807 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
| 4808 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4809 | return -EINVAL; |
| 4810 | } |
| 4811 | break; |
| 4812 | case BPF_FUNC_perf_event_read: |
| 4813 | case BPF_FUNC_perf_event_output: |
| 4814 | case BPF_FUNC_perf_event_read_value: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4815 | case BPF_FUNC_skb_output: |
| 4816 | case BPF_FUNC_xdp_output: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4817 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 4818 | goto error; |
| 4819 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4820 | case BPF_FUNC_ringbuf_output: |
| 4821 | case BPF_FUNC_ringbuf_reserve: |
| 4822 | case BPF_FUNC_ringbuf_query: |
| 4823 | if (map->map_type != BPF_MAP_TYPE_RINGBUF) |
| 4824 | goto error; |
| 4825 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4826 | case BPF_FUNC_get_stackid: |
| 4827 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 4828 | goto error; |
| 4829 | break; |
| 4830 | case BPF_FUNC_current_task_under_cgroup: |
| 4831 | case BPF_FUNC_skb_under_cgroup: |
| 4832 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 4833 | goto error; |
| 4834 | break; |
| 4835 | case BPF_FUNC_redirect_map: |
| 4836 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4837 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4838 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 4839 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
| 4840 | goto error; |
| 4841 | break; |
| 4842 | case BPF_FUNC_sk_redirect_map: |
| 4843 | case BPF_FUNC_msg_redirect_map: |
| 4844 | case BPF_FUNC_sock_map_update: |
| 4845 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 4846 | goto error; |
| 4847 | break; |
| 4848 | case BPF_FUNC_sk_redirect_hash: |
| 4849 | case BPF_FUNC_msg_redirect_hash: |
| 4850 | case BPF_FUNC_sock_hash_update: |
| 4851 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
| 4852 | goto error; |
| 4853 | break; |
| 4854 | case BPF_FUNC_get_local_storage: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4855 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 4856 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4857 | goto error; |
| 4858 | break; |
| 4859 | case BPF_FUNC_sk_select_reuseport: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4860 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
| 4861 | map->map_type != BPF_MAP_TYPE_SOCKMAP && |
| 4862 | map->map_type != BPF_MAP_TYPE_SOCKHASH) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4863 | goto error; |
| 4864 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4865 | case BPF_FUNC_map_peek_elem: |
| 4866 | case BPF_FUNC_map_pop_elem: |
| 4867 | case BPF_FUNC_map_push_elem: |
| 4868 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 4869 | map->map_type != BPF_MAP_TYPE_STACK) |
| 4870 | goto error; |
| 4871 | break; |
| 4872 | case BPF_FUNC_sk_storage_get: |
| 4873 | case BPF_FUNC_sk_storage_delete: |
| 4874 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
| 4875 | goto error; |
| 4876 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4877 | case BPF_FUNC_inode_storage_get: |
| 4878 | case BPF_FUNC_inode_storage_delete: |
| 4879 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) |
| 4880 | goto error; |
| 4881 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4882 | default: |
| 4883 | break; |
| 4884 | } |
| 4885 | |
| 4886 | return 0; |
| 4887 | error: |
| 4888 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
| 4889 | map->map_type, func_id_name(func_id), func_id); |
| 4890 | return -EINVAL; |
| 4891 | } |
| 4892 | |
| 4893 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
| 4894 | { |
| 4895 | int count = 0; |
| 4896 | |
| 4897 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
| 4898 | count++; |
| 4899 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
| 4900 | count++; |
| 4901 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
| 4902 | count++; |
| 4903 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
| 4904 | count++; |
| 4905 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
| 4906 | count++; |
| 4907 | |
| 4908 | /* We only support one arg being in raw mode at the moment, |
| 4909 | * which is sufficient for the helper functions we have |
| 4910 | * right now. |
| 4911 | */ |
| 4912 | return count <= 1; |
| 4913 | } |
| 4914 | |
| 4915 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 4916 | enum bpf_arg_type arg_next) |
| 4917 | { |
| 4918 | return (arg_type_is_mem_ptr(arg_curr) && |
| 4919 | !arg_type_is_mem_size(arg_next)) || |
| 4920 | (!arg_type_is_mem_ptr(arg_curr) && |
| 4921 | arg_type_is_mem_size(arg_next)); |
| 4922 | } |
| 4923 | |
| 4924 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 4925 | { |
| 4926 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 4927 | * bytes from memory 'buf'. Both arg types need |
| 4928 | * to be paired, so make sure there's no buggy |
| 4929 | * helper function specification. |
| 4930 | */ |
| 4931 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 4932 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 4933 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 4934 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 4935 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 4936 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 4937 | return false; |
| 4938 | |
| 4939 | return true; |
| 4940 | } |
| 4941 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4942 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
| 4943 | { |
| 4944 | int count = 0; |
| 4945 | |
| 4946 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
| 4947 | count++; |
| 4948 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
| 4949 | count++; |
| 4950 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
| 4951 | count++; |
| 4952 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
| 4953 | count++; |
| 4954 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
| 4955 | count++; |
| 4956 | |
| 4957 | /* A reference acquiring function cannot acquire |
| 4958 | * another refcounted ptr. |
| 4959 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4960 | if (may_be_acquire_function(func_id) && count) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4961 | return false; |
| 4962 | |
| 4963 | /* We only support one arg being unreferenced at the moment, |
| 4964 | * which is sufficient for the helper functions we have right now. |
| 4965 | */ |
| 4966 | return count <= 1; |
| 4967 | } |
| 4968 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4969 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
| 4970 | { |
| 4971 | int i; |
| 4972 | |
| 4973 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
| 4974 | if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) |
| 4975 | return false; |
| 4976 | |
| 4977 | if (fn->arg_type[i] != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i]) |
| 4978 | return false; |
| 4979 | } |
| 4980 | |
| 4981 | return true; |
| 4982 | } |
| 4983 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4984 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4985 | { |
| 4986 | return check_raw_mode_ok(fn) && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4987 | check_arg_pair_ok(fn) && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4988 | check_btf_id_ok(fn) && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4989 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
| 4992 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 4993 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
| 4994 | */ |
| 4995 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 4996 | struct bpf_func_state *state) |
| 4997 | { |
| 4998 | struct bpf_reg_state *regs = state->regs, *reg; |
| 4999 | int i; |
| 5000 | |
| 5001 | for (i = 0; i < MAX_BPF_REG; i++) |
| 5002 | if (reg_is_pkt_pointer_any(®s[i])) |
| 5003 | mark_reg_unknown(env, regs, i); |
| 5004 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5005 | bpf_for_each_spilled_reg(i, state, reg) { |
| 5006 | if (!reg) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5007 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5008 | if (reg_is_pkt_pointer_any(reg)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5009 | __mark_reg_unknown(env, reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5010 | } |
| 5011 | } |
| 5012 | |
| 5013 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 5014 | { |
| 5015 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5016 | int i; |
| 5017 | |
| 5018 | for (i = 0; i <= vstate->curframe; i++) |
| 5019 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 5020 | } |
| 5021 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5022 | static void release_reg_references(struct bpf_verifier_env *env, |
| 5023 | struct bpf_func_state *state, |
| 5024 | int ref_obj_id) |
| 5025 | { |
| 5026 | struct bpf_reg_state *regs = state->regs, *reg; |
| 5027 | int i; |
| 5028 | |
| 5029 | for (i = 0; i < MAX_BPF_REG; i++) |
| 5030 | if (regs[i].ref_obj_id == ref_obj_id) |
| 5031 | mark_reg_unknown(env, regs, i); |
| 5032 | |
| 5033 | bpf_for_each_spilled_reg(i, state, reg) { |
| 5034 | if (!reg) |
| 5035 | continue; |
| 5036 | if (reg->ref_obj_id == ref_obj_id) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5037 | __mark_reg_unknown(env, reg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5038 | } |
| 5039 | } |
| 5040 | |
| 5041 | /* The pointer with the specified id has released its reference to kernel |
| 5042 | * resources. Identify all copies of the same pointer and clear the reference. |
| 5043 | */ |
| 5044 | static int release_reference(struct bpf_verifier_env *env, |
| 5045 | int ref_obj_id) |
| 5046 | { |
| 5047 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5048 | int err; |
| 5049 | int i; |
| 5050 | |
| 5051 | err = release_reference_state(cur_func(env), ref_obj_id); |
| 5052 | if (err) |
| 5053 | return err; |
| 5054 | |
| 5055 | for (i = 0; i <= vstate->curframe; i++) |
| 5056 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
| 5057 | |
| 5058 | return 0; |
| 5059 | } |
| 5060 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5061 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
| 5062 | struct bpf_reg_state *regs) |
| 5063 | { |
| 5064 | int i; |
| 5065 | |
| 5066 | /* after the call registers r0 - r5 were scratched */ |
| 5067 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 5068 | mark_reg_not_init(env, regs, caller_saved[i]); |
| 5069 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 5070 | } |
| 5071 | } |
| 5072 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5073 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 5074 | int *insn_idx) |
| 5075 | { |
| 5076 | struct bpf_verifier_state *state = env->cur_state; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5077 | struct bpf_func_info_aux *func_info_aux; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5078 | struct bpf_func_state *caller, *callee; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5079 | int i, err, subprog, target_insn; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5080 | bool is_global = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5081 | |
| 5082 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
| 5083 | verbose(env, "the call stack of %d frames is too deep\n", |
| 5084 | state->curframe + 2); |
| 5085 | return -E2BIG; |
| 5086 | } |
| 5087 | |
| 5088 | target_insn = *insn_idx + insn->imm; |
| 5089 | subprog = find_subprog(env, target_insn + 1); |
| 5090 | if (subprog < 0) { |
| 5091 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 5092 | target_insn + 1); |
| 5093 | return -EFAULT; |
| 5094 | } |
| 5095 | |
| 5096 | caller = state->frame[state->curframe]; |
| 5097 | if (state->frame[state->curframe + 1]) { |
| 5098 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 5099 | state->curframe + 1); |
| 5100 | return -EFAULT; |
| 5101 | } |
| 5102 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5103 | func_info_aux = env->prog->aux->func_info_aux; |
| 5104 | if (func_info_aux) |
| 5105 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; |
| 5106 | err = btf_check_func_arg_match(env, subprog, caller->regs); |
| 5107 | if (err == -EFAULT) |
| 5108 | return err; |
| 5109 | if (is_global) { |
| 5110 | if (err) { |
| 5111 | verbose(env, "Caller passes invalid args into func#%d\n", |
| 5112 | subprog); |
| 5113 | return err; |
| 5114 | } else { |
| 5115 | if (env->log.level & BPF_LOG_LEVEL) |
| 5116 | verbose(env, |
| 5117 | "Func#%d is global and valid. Skipping.\n", |
| 5118 | subprog); |
| 5119 | clear_caller_saved_regs(env, caller->regs); |
| 5120 | |
| 5121 | /* All global functions return a 64-bit SCALAR_VALUE */ |
| 5122 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
| 5123 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 5124 | |
| 5125 | /* continue with next insn after call */ |
| 5126 | return 0; |
| 5127 | } |
| 5128 | } |
| 5129 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5130 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 5131 | if (!callee) |
| 5132 | return -ENOMEM; |
| 5133 | state->frame[state->curframe + 1] = callee; |
| 5134 | |
| 5135 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 5136 | * into its own stack before reading from it. |
| 5137 | * callee can read/write into caller's stack |
| 5138 | */ |
| 5139 | init_func_state(env, callee, |
| 5140 | /* remember the callsite, it will be used by bpf_exit */ |
| 5141 | *insn_idx /* callsite */, |
| 5142 | state->curframe + 1 /* frameno within this callchain */, |
| 5143 | subprog /* subprog number within this prog */); |
| 5144 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5145 | /* Transfer references to the callee */ |
| 5146 | err = transfer_reference_state(callee, caller); |
| 5147 | if (err) |
| 5148 | return err; |
| 5149 | |
| 5150 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 5151 | * pointers, which connects us up to the liveness chain |
| 5152 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5153 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 5154 | callee->regs[i] = caller->regs[i]; |
| 5155 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5156 | clear_caller_saved_regs(env, caller->regs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5157 | |
| 5158 | /* only increment it after check_reg_arg() finished */ |
| 5159 | state->curframe++; |
| 5160 | |
| 5161 | /* and go analyze first insn of the callee */ |
| 5162 | *insn_idx = target_insn; |
| 5163 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5164 | if (env->log.level & BPF_LOG_LEVEL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5165 | verbose(env, "caller:\n"); |
| 5166 | print_verifier_state(env, caller); |
| 5167 | verbose(env, "callee:\n"); |
| 5168 | print_verifier_state(env, callee); |
| 5169 | } |
| 5170 | return 0; |
| 5171 | } |
| 5172 | |
| 5173 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 5174 | { |
| 5175 | struct bpf_verifier_state *state = env->cur_state; |
| 5176 | struct bpf_func_state *caller, *callee; |
| 5177 | struct bpf_reg_state *r0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5178 | int err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5179 | |
| 5180 | callee = state->frame[state->curframe]; |
| 5181 | r0 = &callee->regs[BPF_REG_0]; |
| 5182 | if (r0->type == PTR_TO_STACK) { |
| 5183 | /* technically it's ok to return caller's stack pointer |
| 5184 | * (or caller's caller's pointer) back to the caller, |
| 5185 | * since these pointers are valid. Only current stack |
| 5186 | * pointer will be invalid as soon as function exits, |
| 5187 | * but let's be conservative |
| 5188 | */ |
| 5189 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 5190 | return -EINVAL; |
| 5191 | } |
| 5192 | |
| 5193 | state->curframe--; |
| 5194 | caller = state->frame[state->curframe]; |
| 5195 | /* return to the caller whatever r0 had in the callee */ |
| 5196 | caller->regs[BPF_REG_0] = *r0; |
| 5197 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5198 | /* Transfer references to the caller */ |
| 5199 | err = transfer_reference_state(caller, callee); |
| 5200 | if (err) |
| 5201 | return err; |
| 5202 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5203 | *insn_idx = callee->callsite + 1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5204 | if (env->log.level & BPF_LOG_LEVEL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5205 | verbose(env, "returning from callee:\n"); |
| 5206 | print_verifier_state(env, callee); |
| 5207 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 5208 | print_verifier_state(env, caller); |
| 5209 | } |
| 5210 | /* clear everything in the callee */ |
| 5211 | free_func_state(callee); |
| 5212 | state->frame[state->curframe + 1] = NULL; |
| 5213 | return 0; |
| 5214 | } |
| 5215 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5216 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 5217 | int func_id, |
| 5218 | struct bpf_call_arg_meta *meta) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5219 | { |
| 5220 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 5221 | |
| 5222 | if (ret_type != RET_INTEGER || |
| 5223 | (func_id != BPF_FUNC_get_stack && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5224 | func_id != BPF_FUNC_probe_read_str && |
| 5225 | func_id != BPF_FUNC_probe_read_kernel_str && |
| 5226 | func_id != BPF_FUNC_probe_read_user_str)) |
| 5227 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5228 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5229 | ret_reg->smax_value = meta->msize_max_value; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5230 | ret_reg->s32_max_value = meta->msize_max_value; |
| 5231 | ret_reg->smin_value = -MAX_ERRNO; |
| 5232 | ret_reg->s32_min_value = -MAX_ERRNO; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5233 | __reg_deduce_bounds(ret_reg); |
| 5234 | __reg_bound_offset(ret_reg); |
| 5235 | __update_reg_bounds(ret_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5236 | } |
| 5237 | |
| 5238 | static int |
| 5239 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 5240 | int func_id, int insn_idx) |
| 5241 | { |
| 5242 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5243 | struct bpf_map *map = meta->map_ptr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5244 | |
| 5245 | if (func_id != BPF_FUNC_tail_call && |
| 5246 | func_id != BPF_FUNC_map_lookup_elem && |
| 5247 | func_id != BPF_FUNC_map_update_elem && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5248 | func_id != BPF_FUNC_map_delete_elem && |
| 5249 | func_id != BPF_FUNC_map_push_elem && |
| 5250 | func_id != BPF_FUNC_map_pop_elem && |
| 5251 | func_id != BPF_FUNC_map_peek_elem) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5252 | return 0; |
| 5253 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5254 | if (map == NULL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5255 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 5256 | return -EINVAL; |
| 5257 | } |
| 5258 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5259 | /* In case of read-only, some additional restrictions |
| 5260 | * need to be applied in order to prevent altering the |
| 5261 | * state of the map from program side. |
| 5262 | */ |
| 5263 | if ((map->map_flags & BPF_F_RDONLY_PROG) && |
| 5264 | (func_id == BPF_FUNC_map_delete_elem || |
| 5265 | func_id == BPF_FUNC_map_update_elem || |
| 5266 | func_id == BPF_FUNC_map_push_elem || |
| 5267 | func_id == BPF_FUNC_map_pop_elem)) { |
| 5268 | verbose(env, "write into map forbidden\n"); |
| 5269 | return -EACCES; |
| 5270 | } |
| 5271 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5272 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5273 | bpf_map_ptr_store(aux, meta->map_ptr, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5274 | !meta->map_ptr->bypass_spec_v1); |
| 5275 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5276 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5277 | !meta->map_ptr->bypass_spec_v1); |
| 5278 | return 0; |
| 5279 | } |
| 5280 | |
| 5281 | static int |
| 5282 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 5283 | int func_id, int insn_idx) |
| 5284 | { |
| 5285 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
| 5286 | struct bpf_reg_state *regs = cur_regs(env), *reg; |
| 5287 | struct bpf_map *map = meta->map_ptr; |
| 5288 | struct tnum range; |
| 5289 | u64 val; |
| 5290 | int err; |
| 5291 | |
| 5292 | if (func_id != BPF_FUNC_tail_call) |
| 5293 | return 0; |
| 5294 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { |
| 5295 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 5296 | return -EINVAL; |
| 5297 | } |
| 5298 | |
| 5299 | range = tnum_range(0, map->max_entries - 1); |
| 5300 | reg = ®s[BPF_REG_3]; |
| 5301 | |
| 5302 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { |
| 5303 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
| 5304 | return 0; |
| 5305 | } |
| 5306 | |
| 5307 | err = mark_chain_precision(env, BPF_REG_3); |
| 5308 | if (err) |
| 5309 | return err; |
| 5310 | |
| 5311 | val = reg->var_off.value; |
| 5312 | if (bpf_map_key_unseen(aux)) |
| 5313 | bpf_map_key_store(aux, val); |
| 5314 | else if (!bpf_map_key_poisoned(aux) && |
| 5315 | bpf_map_key_immediate(aux) != val) |
| 5316 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5317 | return 0; |
| 5318 | } |
| 5319 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5320 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 5321 | { |
| 5322 | struct bpf_func_state *state = cur_func(env); |
| 5323 | int i; |
| 5324 | |
| 5325 | for (i = 0; i < state->acquired_refs; i++) { |
| 5326 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 5327 | state->refs[i].id, state->refs[i].insn_idx); |
| 5328 | } |
| 5329 | return state->acquired_refs ? -EINVAL : 0; |
| 5330 | } |
| 5331 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5332 | static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
| 5333 | { |
| 5334 | const struct bpf_func_proto *fn = NULL; |
| 5335 | struct bpf_reg_state *regs; |
| 5336 | struct bpf_call_arg_meta meta; |
| 5337 | bool changes_data; |
| 5338 | int i, err; |
| 5339 | |
| 5340 | /* find function prototype */ |
| 5341 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
| 5342 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 5343 | func_id); |
| 5344 | return -EINVAL; |
| 5345 | } |
| 5346 | |
| 5347 | if (env->ops->get_func_proto) |
| 5348 | fn = env->ops->get_func_proto(func_id, env->prog); |
| 5349 | if (!fn) { |
| 5350 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 5351 | func_id); |
| 5352 | return -EINVAL; |
| 5353 | } |
| 5354 | |
| 5355 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
| 5356 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
| 5357 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
| 5358 | return -EINVAL; |
| 5359 | } |
| 5360 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5361 | if (fn->allowed && !fn->allowed(env->prog)) { |
| 5362 | verbose(env, "helper call is not allowed in probe\n"); |
| 5363 | return -EINVAL; |
| 5364 | } |
| 5365 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5366 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
| 5367 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
| 5368 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 5369 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 5370 | func_id_name(func_id), func_id); |
| 5371 | return -EINVAL; |
| 5372 | } |
| 5373 | |
| 5374 | memset(&meta, 0, sizeof(meta)); |
| 5375 | meta.pkt_access = fn->pkt_access; |
| 5376 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5377 | err = check_func_proto(fn, func_id); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5378 | if (err) { |
| 5379 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
| 5380 | func_id_name(func_id), func_id); |
| 5381 | return err; |
| 5382 | } |
| 5383 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5384 | meta.func_id = func_id; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5385 | /* check args */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5386 | for (i = 0; i < 5; i++) { |
| 5387 | err = check_func_arg(env, i, &meta, fn); |
| 5388 | if (err) |
| 5389 | return err; |
| 5390 | } |
| 5391 | |
| 5392 | err = record_func_map(env, &meta, func_id, insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5393 | if (err) |
| 5394 | return err; |
| 5395 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5396 | err = record_func_key(env, &meta, func_id, insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5397 | if (err) |
| 5398 | return err; |
| 5399 | |
| 5400 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 5401 | * is inferred from register state. |
| 5402 | */ |
| 5403 | for (i = 0; i < meta.access_size; i++) { |
| 5404 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 5405 | BPF_WRITE, -1, false); |
| 5406 | if (err) |
| 5407 | return err; |
| 5408 | } |
| 5409 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5410 | if (func_id == BPF_FUNC_tail_call) { |
| 5411 | err = check_reference_leak(env); |
| 5412 | if (err) { |
| 5413 | verbose(env, "tail_call would lead to reference leak\n"); |
| 5414 | return err; |
| 5415 | } |
| 5416 | } else if (is_release_function(func_id)) { |
| 5417 | err = release_reference(env, meta.ref_obj_id); |
| 5418 | if (err) { |
| 5419 | verbose(env, "func %s#%d reference has not been acquired before\n", |
| 5420 | func_id_name(func_id), func_id); |
| 5421 | return err; |
| 5422 | } |
| 5423 | } |
| 5424 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5425 | regs = cur_regs(env); |
| 5426 | |
| 5427 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 5428 | * this is required because get_local_storage() can't return an error. |
| 5429 | */ |
| 5430 | if (func_id == BPF_FUNC_get_local_storage && |
| 5431 | !register_is_null(®s[BPF_REG_2])) { |
| 5432 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 5433 | return -EINVAL; |
| 5434 | } |
| 5435 | |
| 5436 | /* reset caller saved regs */ |
| 5437 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 5438 | mark_reg_not_init(env, regs, caller_saved[i]); |
| 5439 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 5440 | } |
| 5441 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5442 | /* helper call returns 64-bit value. */ |
| 5443 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 5444 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5445 | /* update return register (already marked as written above) */ |
| 5446 | if (fn->ret_type == RET_INTEGER) { |
| 5447 | /* sets type to SCALAR_VALUE */ |
| 5448 | mark_reg_unknown(env, regs, BPF_REG_0); |
| 5449 | } else if (fn->ret_type == RET_VOID) { |
| 5450 | regs[BPF_REG_0].type = NOT_INIT; |
| 5451 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 5452 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5453 | /* There is no offset yet applied, variable or fixed */ |
| 5454 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5455 | /* remember map_ptr, so that check_map_access() |
| 5456 | * can check 'value_size' boundary of memory access |
| 5457 | * to map element returned from bpf_map_lookup_elem() |
| 5458 | */ |
| 5459 | if (meta.map_ptr == NULL) { |
| 5460 | verbose(env, |
| 5461 | "kernel subsystem misconfigured verifier\n"); |
| 5462 | return -EINVAL; |
| 5463 | } |
| 5464 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5465 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 5466 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
| 5467 | if (map_value_has_spin_lock(meta.map_ptr)) |
| 5468 | regs[BPF_REG_0].id = ++env->id_gen; |
| 5469 | } else { |
| 5470 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5471 | } |
| 5472 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
| 5473 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5474 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5475 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
| 5476 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5477 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5478 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
| 5479 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5480 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5481 | } else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) { |
| 5482 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5483 | regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL; |
| 5484 | regs[BPF_REG_0].mem_size = meta.mem_size; |
| 5485 | } else if (fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL || |
| 5486 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID) { |
| 5487 | const struct btf_type *t; |
| 5488 | |
| 5489 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5490 | t = btf_type_skip_modifiers(btf_vmlinux, meta.ret_btf_id, NULL); |
| 5491 | if (!btf_type_is_struct(t)) { |
| 5492 | u32 tsize; |
| 5493 | const struct btf_type *ret; |
| 5494 | const char *tname; |
| 5495 | |
| 5496 | /* resolve the type size of ksym. */ |
| 5497 | ret = btf_resolve_size(btf_vmlinux, t, &tsize); |
| 5498 | if (IS_ERR(ret)) { |
| 5499 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 5500 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
| 5501 | tname, PTR_ERR(ret)); |
| 5502 | return -EINVAL; |
| 5503 | } |
| 5504 | regs[BPF_REG_0].type = |
| 5505 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? |
| 5506 | PTR_TO_MEM : PTR_TO_MEM_OR_NULL; |
| 5507 | regs[BPF_REG_0].mem_size = tsize; |
| 5508 | } else { |
| 5509 | regs[BPF_REG_0].type = |
| 5510 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? |
| 5511 | PTR_TO_BTF_ID : PTR_TO_BTF_ID_OR_NULL; |
| 5512 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
| 5513 | } |
| 5514 | } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL) { |
| 5515 | int ret_btf_id; |
| 5516 | |
| 5517 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5518 | regs[BPF_REG_0].type = PTR_TO_BTF_ID_OR_NULL; |
| 5519 | ret_btf_id = *fn->ret_btf_id; |
| 5520 | if (ret_btf_id == 0) { |
| 5521 | verbose(env, "invalid return type %d of func %s#%d\n", |
| 5522 | fn->ret_type, func_id_name(func_id), func_id); |
| 5523 | return -EINVAL; |
| 5524 | } |
| 5525 | regs[BPF_REG_0].btf_id = ret_btf_id; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5526 | } else { |
| 5527 | verbose(env, "unknown return type %d of func %s#%d\n", |
| 5528 | fn->ret_type, func_id_name(func_id), func_id); |
| 5529 | return -EINVAL; |
| 5530 | } |
| 5531 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5532 | if (reg_type_may_be_null(regs[BPF_REG_0].type)) |
| 5533 | regs[BPF_REG_0].id = ++env->id_gen; |
| 5534 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5535 | if (is_ptr_cast_function(func_id)) { |
| 5536 | /* For release_reference() */ |
| 5537 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5538 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5539 | int id = acquire_reference_state(env, insn_idx); |
| 5540 | |
| 5541 | if (id < 0) |
| 5542 | return id; |
| 5543 | /* For mark_ptr_or_null_reg() */ |
| 5544 | regs[BPF_REG_0].id = id; |
| 5545 | /* For release_reference() */ |
| 5546 | regs[BPF_REG_0].ref_obj_id = id; |
| 5547 | } |
| 5548 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5549 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5550 | |
| 5551 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
| 5552 | if (err) |
| 5553 | return err; |
| 5554 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5555 | if ((func_id == BPF_FUNC_get_stack || |
| 5556 | func_id == BPF_FUNC_get_task_stack) && |
| 5557 | !env->prog->has_callchain_buf) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5558 | const char *err_str; |
| 5559 | |
| 5560 | #ifdef CONFIG_PERF_EVENTS |
| 5561 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 5562 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 5563 | #else |
| 5564 | err = -ENOTSUPP; |
| 5565 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 5566 | #endif |
| 5567 | if (err) { |
| 5568 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 5569 | return err; |
| 5570 | } |
| 5571 | |
| 5572 | env->prog->has_callchain_buf = true; |
| 5573 | } |
| 5574 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5575 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
| 5576 | env->prog->call_get_stack = true; |
| 5577 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5578 | if (changes_data) |
| 5579 | clear_all_pkt_pointers(env); |
| 5580 | return 0; |
| 5581 | } |
| 5582 | |
| 5583 | static bool signed_add_overflows(s64 a, s64 b) |
| 5584 | { |
| 5585 | /* Do the add in u64, where overflow is well-defined */ |
| 5586 | s64 res = (s64)((u64)a + (u64)b); |
| 5587 | |
| 5588 | if (b < 0) |
| 5589 | return res > a; |
| 5590 | return res < a; |
| 5591 | } |
| 5592 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5593 | static bool signed_add32_overflows(s32 a, s32 b) |
| 5594 | { |
| 5595 | /* Do the add in u32, where overflow is well-defined */ |
| 5596 | s32 res = (s32)((u32)a + (u32)b); |
| 5597 | |
| 5598 | if (b < 0) |
| 5599 | return res > a; |
| 5600 | return res < a; |
| 5601 | } |
| 5602 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5603 | static bool signed_sub_overflows(s64 a, s64 b) |
| 5604 | { |
| 5605 | /* Do the sub in u64, where overflow is well-defined */ |
| 5606 | s64 res = (s64)((u64)a - (u64)b); |
| 5607 | |
| 5608 | if (b < 0) |
| 5609 | return res < a; |
| 5610 | return res > a; |
| 5611 | } |
| 5612 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5613 | static bool signed_sub32_overflows(s32 a, s32 b) |
| 5614 | { |
| 5615 | /* Do the sub in u32, where overflow is well-defined */ |
| 5616 | s32 res = (s32)((u32)a - (u32)b); |
| 5617 | |
| 5618 | if (b < 0) |
| 5619 | return res < a; |
| 5620 | return res > a; |
| 5621 | } |
| 5622 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5623 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 5624 | const struct bpf_reg_state *reg, |
| 5625 | enum bpf_reg_type type) |
| 5626 | { |
| 5627 | bool known = tnum_is_const(reg->var_off); |
| 5628 | s64 val = reg->var_off.value; |
| 5629 | s64 smin = reg->smin_value; |
| 5630 | |
| 5631 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 5632 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 5633 | reg_type_str[type], val); |
| 5634 | return false; |
| 5635 | } |
| 5636 | |
| 5637 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 5638 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 5639 | reg_type_str[type], reg->off); |
| 5640 | return false; |
| 5641 | } |
| 5642 | |
| 5643 | if (smin == S64_MIN) { |
| 5644 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 5645 | reg_type_str[type]); |
| 5646 | return false; |
| 5647 | } |
| 5648 | |
| 5649 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 5650 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 5651 | smin, reg_type_str[type]); |
| 5652 | return false; |
| 5653 | } |
| 5654 | |
| 5655 | return true; |
| 5656 | } |
| 5657 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5658 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
| 5659 | { |
| 5660 | return &env->insn_aux_data[env->insn_idx]; |
| 5661 | } |
| 5662 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5663 | enum { |
| 5664 | REASON_BOUNDS = -1, |
| 5665 | REASON_TYPE = -2, |
| 5666 | REASON_PATHS = -3, |
| 5667 | REASON_LIMIT = -4, |
| 5668 | REASON_STACK = -5, |
| 5669 | }; |
| 5670 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5671 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5672 | u32 *alu_limit, bool mask_to_left) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5673 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5674 | u32 max = 0, ptr_limit = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5675 | |
| 5676 | switch (ptr_reg->type) { |
| 5677 | case PTR_TO_STACK: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5678 | /* Offset 0 is out-of-bounds, but acceptable start for the |
| 5679 | * left direction, see BPF_REG_FP. Also, unknown scalar |
| 5680 | * offset where we would need to deal with min/max bounds is |
| 5681 | * currently prohibited for unprivileged. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5682 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5683 | max = MAX_BPF_STACK + mask_to_left; |
| 5684 | ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off); |
| 5685 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5686 | case PTR_TO_MAP_VALUE: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5687 | max = ptr_reg->map_ptr->value_size; |
| 5688 | ptr_limit = (mask_to_left ? |
| 5689 | ptr_reg->smin_value : |
| 5690 | ptr_reg->umax_value) + ptr_reg->off; |
| 5691 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5692 | default: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5693 | return REASON_TYPE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5694 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5695 | |
| 5696 | if (ptr_limit >= max) |
| 5697 | return REASON_LIMIT; |
| 5698 | *alu_limit = ptr_limit; |
| 5699 | return 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5700 | } |
| 5701 | |
| 5702 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
| 5703 | const struct bpf_insn *insn) |
| 5704 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5705 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5706 | } |
| 5707 | |
| 5708 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, |
| 5709 | u32 alu_state, u32 alu_limit) |
| 5710 | { |
| 5711 | /* If we arrived here from different branches with different |
| 5712 | * state or limits to sanitize, then this won't work. |
| 5713 | */ |
| 5714 | if (aux->alu_state && |
| 5715 | (aux->alu_state != alu_state || |
| 5716 | aux->alu_limit != alu_limit)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5717 | return REASON_PATHS; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5718 | |
| 5719 | /* Corresponding fixup done in fixup_bpf_calls(). */ |
| 5720 | aux->alu_state = alu_state; |
| 5721 | aux->alu_limit = alu_limit; |
| 5722 | return 0; |
| 5723 | } |
| 5724 | |
| 5725 | static int sanitize_val_alu(struct bpf_verifier_env *env, |
| 5726 | struct bpf_insn *insn) |
| 5727 | { |
| 5728 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 5729 | |
| 5730 | if (can_skip_alu_sanitation(env, insn)) |
| 5731 | return 0; |
| 5732 | |
| 5733 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); |
| 5734 | } |
| 5735 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5736 | static bool sanitize_needed(u8 opcode) |
| 5737 | { |
| 5738 | return opcode == BPF_ADD || opcode == BPF_SUB; |
| 5739 | } |
| 5740 | |
| 5741 | struct bpf_sanitize_info { |
| 5742 | struct bpf_insn_aux_data aux; |
| 5743 | bool mask_to_left; |
| 5744 | }; |
| 5745 | |
| 5746 | static struct bpf_verifier_state * |
| 5747 | sanitize_speculative_path(struct bpf_verifier_env *env, |
| 5748 | const struct bpf_insn *insn, |
| 5749 | u32 next_idx, u32 curr_idx) |
| 5750 | { |
| 5751 | struct bpf_verifier_state *branch; |
| 5752 | struct bpf_reg_state *regs; |
| 5753 | |
| 5754 | branch = push_stack(env, next_idx, curr_idx, true); |
| 5755 | if (branch && insn) { |
| 5756 | regs = branch->frame[branch->curframe]->regs; |
| 5757 | if (BPF_SRC(insn->code) == BPF_K) { |
| 5758 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 5759 | } else if (BPF_SRC(insn->code) == BPF_X) { |
| 5760 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 5761 | mark_reg_unknown(env, regs, insn->src_reg); |
| 5762 | } |
| 5763 | } |
| 5764 | return branch; |
| 5765 | } |
| 5766 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5767 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
| 5768 | struct bpf_insn *insn, |
| 5769 | const struct bpf_reg_state *ptr_reg, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5770 | const struct bpf_reg_state *off_reg, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5771 | struct bpf_reg_state *dst_reg, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5772 | struct bpf_sanitize_info *info, |
| 5773 | const bool commit_window) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5774 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5775 | struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5776 | struct bpf_verifier_state *vstate = env->cur_state; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5777 | bool off_is_imm = tnum_is_const(off_reg->var_off); |
| 5778 | bool off_is_neg = off_reg->smin_value < 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5779 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
| 5780 | u8 opcode = BPF_OP(insn->code); |
| 5781 | u32 alu_state, alu_limit; |
| 5782 | struct bpf_reg_state tmp; |
| 5783 | bool ret; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5784 | int err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5785 | |
| 5786 | if (can_skip_alu_sanitation(env, insn)) |
| 5787 | return 0; |
| 5788 | |
| 5789 | /* We already marked aux for masking from non-speculative |
| 5790 | * paths, thus we got here in the first place. We only care |
| 5791 | * to explore bad access from here. |
| 5792 | */ |
| 5793 | if (vstate->speculative) |
| 5794 | goto do_sim; |
| 5795 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5796 | if (!commit_window) { |
| 5797 | if (!tnum_is_const(off_reg->var_off) && |
| 5798 | (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) |
| 5799 | return REASON_BOUNDS; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5800 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5801 | info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || |
| 5802 | (opcode == BPF_SUB && !off_is_neg); |
| 5803 | } |
| 5804 | |
| 5805 | err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); |
| 5806 | if (err < 0) |
| 5807 | return err; |
| 5808 | |
| 5809 | if (commit_window) { |
| 5810 | /* In commit phase we narrow the masking window based on |
| 5811 | * the observed pointer move after the simulated operation. |
| 5812 | */ |
| 5813 | alu_state = info->aux.alu_state; |
| 5814 | alu_limit = abs(info->aux.alu_limit - alu_limit); |
| 5815 | } else { |
| 5816 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; |
| 5817 | alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0; |
| 5818 | alu_state |= ptr_is_dst_reg ? |
| 5819 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; |
| 5820 | |
| 5821 | /* Limit pruning on unknown scalars to enable deep search for |
| 5822 | * potential masking differences from other program paths. |
| 5823 | */ |
| 5824 | if (!off_is_imm) |
| 5825 | env->explore_alu_limits = true; |
| 5826 | } |
| 5827 | |
| 5828 | err = update_alu_sanitation_state(aux, alu_state, alu_limit); |
| 5829 | if (err < 0) |
| 5830 | return err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5831 | do_sim: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5832 | /* If we're in commit phase, we're done here given we already |
| 5833 | * pushed the truncated dst_reg into the speculative verification |
| 5834 | * stack. |
| 5835 | * |
| 5836 | * Also, when register is a known constant, we rewrite register-based |
| 5837 | * operation to immediate-based, and thus do not need masking (and as |
| 5838 | * a consequence, do not need to simulate the zero-truncation either). |
| 5839 | */ |
| 5840 | if (commit_window || off_is_imm) |
| 5841 | return 0; |
| 5842 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5843 | /* Simulate and find potential out-of-bounds access under |
| 5844 | * speculative execution from truncation as a result of |
| 5845 | * masking when off was not within expected range. If off |
| 5846 | * sits in dst, then we temporarily need to move ptr there |
| 5847 | * to simulate dst (== 0) +/-= ptr. Needed, for example, |
| 5848 | * for cases where we use K-based arithmetic in one direction |
| 5849 | * and truncated reg-based in the other in order to explore |
| 5850 | * bad access. |
| 5851 | */ |
| 5852 | if (!ptr_is_dst_reg) { |
| 5853 | tmp = *dst_reg; |
| 5854 | *dst_reg = *ptr_reg; |
| 5855 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5856 | ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, |
| 5857 | env->insn_idx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5858 | if (!ptr_is_dst_reg && ret) |
| 5859 | *dst_reg = tmp; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5860 | return !ret ? REASON_STACK : 0; |
| 5861 | } |
| 5862 | |
| 5863 | static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) |
| 5864 | { |
| 5865 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5866 | |
| 5867 | /* If we simulate paths under speculation, we don't update the |
| 5868 | * insn as 'seen' such that when we verify unreachable paths in |
| 5869 | * the non-speculative domain, sanitize_dead_code() can still |
| 5870 | * rewrite/sanitize them. |
| 5871 | */ |
| 5872 | if (!vstate->speculative) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5873 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5874 | } |
| 5875 | |
| 5876 | static int sanitize_err(struct bpf_verifier_env *env, |
| 5877 | const struct bpf_insn *insn, int reason, |
| 5878 | const struct bpf_reg_state *off_reg, |
| 5879 | const struct bpf_reg_state *dst_reg) |
| 5880 | { |
| 5881 | static const char *err = "pointer arithmetic with it prohibited for !root"; |
| 5882 | const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; |
| 5883 | u32 dst = insn->dst_reg, src = insn->src_reg; |
| 5884 | |
| 5885 | switch (reason) { |
| 5886 | case REASON_BOUNDS: |
| 5887 | verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", |
| 5888 | off_reg == dst_reg ? dst : src, err); |
| 5889 | break; |
| 5890 | case REASON_TYPE: |
| 5891 | verbose(env, "R%d has pointer with unsupported alu operation, %s\n", |
| 5892 | off_reg == dst_reg ? src : dst, err); |
| 5893 | break; |
| 5894 | case REASON_PATHS: |
| 5895 | verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", |
| 5896 | dst, op, err); |
| 5897 | break; |
| 5898 | case REASON_LIMIT: |
| 5899 | verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", |
| 5900 | dst, op, err); |
| 5901 | break; |
| 5902 | case REASON_STACK: |
| 5903 | verbose(env, "R%d could not be pushed for speculative verification, %s\n", |
| 5904 | dst, err); |
| 5905 | break; |
| 5906 | default: |
| 5907 | verbose(env, "verifier internal error: unknown reason (%d)\n", |
| 5908 | reason); |
| 5909 | break; |
| 5910 | } |
| 5911 | |
| 5912 | return -EACCES; |
| 5913 | } |
| 5914 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5915 | /* check that stack access falls within stack limits and that 'reg' doesn't |
| 5916 | * have a variable offset. |
| 5917 | * |
| 5918 | * Variable offset is prohibited for unprivileged mode for simplicity since it |
| 5919 | * requires corresponding support in Spectre masking for stack ALU. See also |
| 5920 | * retrieve_ptr_limit(). |
| 5921 | * |
| 5922 | * |
| 5923 | * 'off' includes 'reg->off'. |
| 5924 | */ |
| 5925 | static int check_stack_access_for_ptr_arithmetic( |
| 5926 | struct bpf_verifier_env *env, |
| 5927 | int regno, |
| 5928 | const struct bpf_reg_state *reg, |
| 5929 | int off) |
| 5930 | { |
| 5931 | if (!tnum_is_const(reg->var_off)) { |
| 5932 | char tn_buf[48]; |
| 5933 | |
| 5934 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 5935 | verbose(env, "R%d variable stack access prohibited for !root, var_off=%s off=%d\n", |
| 5936 | regno, tn_buf, off); |
| 5937 | return -EACCES; |
| 5938 | } |
| 5939 | |
| 5940 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 5941 | verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 5942 | "prohibited for !root; off=%d\n", regno, off); |
| 5943 | return -EACCES; |
| 5944 | } |
| 5945 | |
| 5946 | return 0; |
| 5947 | } |
| 5948 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5949 | static int sanitize_check_bounds(struct bpf_verifier_env *env, |
| 5950 | const struct bpf_insn *insn, |
| 5951 | const struct bpf_reg_state *dst_reg) |
| 5952 | { |
| 5953 | u32 dst = insn->dst_reg; |
| 5954 | |
| 5955 | /* For unprivileged we require that resulting offset must be in bounds |
| 5956 | * in order to be able to sanitize access later on. |
| 5957 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5958 | if (env->bypass_spec_v1) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5959 | return 0; |
| 5960 | |
| 5961 | switch (dst_reg->type) { |
| 5962 | case PTR_TO_STACK: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 5963 | if (check_stack_access_for_ptr_arithmetic(env, dst, dst_reg, |
| 5964 | dst_reg->off + dst_reg->var_off.value)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5965 | return -EACCES; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5966 | break; |
| 5967 | case PTR_TO_MAP_VALUE: |
| 5968 | if (check_map_access(env, dst, dst_reg->off, 1, false)) { |
| 5969 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
| 5970 | "prohibited for !root\n", dst); |
| 5971 | return -EACCES; |
| 5972 | } |
| 5973 | break; |
| 5974 | default: |
| 5975 | break; |
| 5976 | } |
| 5977 | |
| 5978 | return 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5979 | } |
| 5980 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5981 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
| 5982 | * Caller should also handle BPF_MOV case separately. |
| 5983 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 5984 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 5985 | */ |
| 5986 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 5987 | struct bpf_insn *insn, |
| 5988 | const struct bpf_reg_state *ptr_reg, |
| 5989 | const struct bpf_reg_state *off_reg) |
| 5990 | { |
| 5991 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5992 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 5993 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
| 5994 | bool known = tnum_is_const(off_reg->var_off); |
| 5995 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 5996 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 5997 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 5998 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5999 | struct bpf_sanitize_info info = {}; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6000 | u8 opcode = BPF_OP(insn->code); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 6001 | u32 dst = insn->dst_reg; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6002 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6003 | |
| 6004 | dst_reg = ®s[dst]; |
| 6005 | |
| 6006 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 6007 | smin_val > smax_val || umin_val > umax_val) { |
| 6008 | /* Taint dst register if offset had invalid bounds derived from |
| 6009 | * e.g. dead branches. |
| 6010 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 6011 | __mark_reg_unknown(env, dst_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6012 | return 0; |
| 6013 | } |
| 6014 | |
| 6015 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 6016 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6017 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
| 6018 | __mark_reg_unknown(env, dst_reg); |
| 6019 | return 0; |
| 6020 | } |
| 6021 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6022 | verbose(env, |
| 6023 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 6024 | dst); |
| 6025 | return -EACCES; |
| 6026 | } |
| 6027 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6028 | switch (ptr_reg->type) { |
| 6029 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 6030 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
| 6031 | dst, reg_type_str[ptr_reg->type]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6032 | return -EACCES; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6033 | case CONST_PTR_TO_MAP: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 6034 | /* smin_val represents the known value */ |
| 6035 | if (known && smin_val == 0 && opcode == BPF_ADD) |
| 6036 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6037 | fallthrough; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6038 | case PTR_TO_PACKET_END: |
| 6039 | case PTR_TO_SOCKET: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6040 | case PTR_TO_SOCK_COMMON: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6041 | case PTR_TO_TCP_SOCK: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6042 | case PTR_TO_XDP_SOCK: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6043 | reject: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6044 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
| 6045 | dst, reg_type_str[ptr_reg->type]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6046 | return -EACCES; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6047 | default: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6048 | if (reg_type_may_be_null(ptr_reg->type)) |
| 6049 | goto reject; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6050 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6051 | } |
| 6052 | |
| 6053 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 6054 | * The id may be overwritten later if we create a new variable offset. |
| 6055 | */ |
| 6056 | dst_reg->type = ptr_reg->type; |
| 6057 | dst_reg->id = ptr_reg->id; |
| 6058 | |
| 6059 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 6060 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 6061 | return -EINVAL; |
| 6062 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6063 | /* pointer types do not carry 32-bit bounds at the moment. */ |
| 6064 | __mark_reg32_unbounded(dst_reg); |
| 6065 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 6066 | if (sanitize_needed(opcode)) { |
| 6067 | ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg, |
| 6068 | &info, false); |
| 6069 | if (ret < 0) |
| 6070 | return sanitize_err(env, insn, ret, off_reg, dst_reg); |
| 6071 | } |
| 6072 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6073 | switch (opcode) { |
| 6074 | case BPF_ADD: |
| 6075 | /* We can take a fixed offset as long as it doesn't overflow |
| 6076 | * the s32 'off' field |
| 6077 | */ |
| 6078 | if (known && (ptr_reg->off + smin_val == |
| 6079 | (s64)(s32)(ptr_reg->off + smin_val))) { |
| 6080 | /* pointer += K. Accumulate it into fixed offset */ |
| 6081 | dst_reg->smin_value = smin_ptr; |
| 6082 | dst_reg->smax_value = smax_ptr; |
| 6083 | dst_reg->umin_value = umin_ptr; |
| 6084 | dst_reg->umax_value = umax_ptr; |
| 6085 | dst_reg->var_off = ptr_reg->var_off; |
| 6086 | dst_reg->off = ptr_reg->off + smin_val; |
| 6087 | dst_reg->raw = ptr_reg->raw; |
| 6088 | break; |
| 6089 | } |
| 6090 | /* A new variable offset is created. Note that off_reg->off |
| 6091 | * == 0, since it's a scalar. |
| 6092 | * dst_reg gets the pointer type and since some positive |
| 6093 | * integer value was added to the pointer, give it a new 'id' |
| 6094 | * if it's a PTR_TO_PACKET. |
| 6095 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 6096 | * added into the variable offset, and we copy the fixed offset |
| 6097 | * from ptr_reg. |
| 6098 | */ |
| 6099 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 6100 | signed_add_overflows(smax_ptr, smax_val)) { |
| 6101 | dst_reg->smin_value = S64_MIN; |
| 6102 | dst_reg->smax_value = S64_MAX; |
| 6103 | } else { |
| 6104 | dst_reg->smin_value = smin_ptr + smin_val; |
| 6105 | dst_reg->smax_value = smax_ptr + smax_val; |
| 6106 | } |
| 6107 | if (umin_ptr + umin_val < umin_ptr || |
| 6108 | umax_ptr + umax_val < umax_ptr) { |
| 6109 | dst_reg->umin_value = 0; |
| 6110 | dst_reg->umax_value = U64_MAX; |
| 6111 | } else { |
| 6112 | dst_reg->umin_value = umin_ptr + umin_val; |
| 6113 | dst_reg->umax_value = umax_ptr + umax_val; |
| 6114 | } |
| 6115 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 6116 | dst_reg->off = ptr_reg->off; |
| 6117 | dst_reg->raw = ptr_reg->raw; |
| 6118 | if (reg_is_pkt_pointer(ptr_reg)) { |
| 6119 | dst_reg->id = ++env->id_gen; |
| 6120 | /* something was added to pkt_ptr, set range to zero */ |
| 6121 | dst_reg->raw = 0; |
| 6122 | } |
| 6123 | break; |
| 6124 | case BPF_SUB: |
| 6125 | if (dst_reg == off_reg) { |
| 6126 | /* scalar -= pointer. Creates an unknown scalar */ |
| 6127 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 6128 | dst); |
| 6129 | return -EACCES; |
| 6130 | } |
| 6131 | /* We don't allow subtraction from FP, because (according to |
| 6132 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 6133 | * be able to deal with it. |
| 6134 | */ |
| 6135 | if (ptr_reg->type == PTR_TO_STACK) { |
| 6136 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 6137 | dst); |
| 6138 | return -EACCES; |
| 6139 | } |
| 6140 | if (known && (ptr_reg->off - smin_val == |
| 6141 | (s64)(s32)(ptr_reg->off - smin_val))) { |
| 6142 | /* pointer -= K. Subtract it from fixed offset */ |
| 6143 | dst_reg->smin_value = smin_ptr; |
| 6144 | dst_reg->smax_value = smax_ptr; |
| 6145 | dst_reg->umin_value = umin_ptr; |
| 6146 | dst_reg->umax_value = umax_ptr; |
| 6147 | dst_reg->var_off = ptr_reg->var_off; |
| 6148 | dst_reg->id = ptr_reg->id; |
| 6149 | dst_reg->off = ptr_reg->off - smin_val; |
| 6150 | dst_reg->raw = ptr_reg->raw; |
| 6151 | break; |
| 6152 | } |
| 6153 | /* A new variable offset is created. If the subtrahend is known |
| 6154 | * nonnegative, then any reg->range we had before is still good. |
| 6155 | */ |
| 6156 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 6157 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 6158 | /* Overflow possible, we know nothing */ |
| 6159 | dst_reg->smin_value = S64_MIN; |
| 6160 | dst_reg->smax_value = S64_MAX; |
| 6161 | } else { |
| 6162 | dst_reg->smin_value = smin_ptr - smax_val; |
| 6163 | dst_reg->smax_value = smax_ptr - smin_val; |
| 6164 | } |
| 6165 | if (umin_ptr < umax_val) { |
| 6166 | /* Overflow possible, we know nothing */ |
| 6167 | dst_reg->umin_value = 0; |
| 6168 | dst_reg->umax_value = U64_MAX; |
| 6169 | } else { |
| 6170 | /* Cannot overflow (as long as bounds are consistent) */ |
| 6171 | dst_reg->umin_value = umin_ptr - umax_val; |
| 6172 | dst_reg->umax_value = umax_ptr - umin_val; |
| 6173 | } |
| 6174 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 6175 | dst_reg->off = ptr_reg->off; |
| 6176 | dst_reg->raw = ptr_reg->raw; |
| 6177 | if (reg_is_pkt_pointer(ptr_reg)) { |
| 6178 | dst_reg->id = ++env->id_gen; |
| 6179 | /* something was added to pkt_ptr, set range to zero */ |
| 6180 | if (smin_val < 0) |
| 6181 | dst_reg->raw = 0; |
| 6182 | } |
| 6183 | break; |
| 6184 | case BPF_AND: |
| 6185 | case BPF_OR: |
| 6186 | case BPF_XOR: |
| 6187 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 6188 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 6189 | dst, bpf_alu_string[opcode >> 4]); |
| 6190 | return -EACCES; |
| 6191 | default: |
| 6192 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
| 6193 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 6194 | dst, bpf_alu_string[opcode >> 4]); |
| 6195 | return -EACCES; |
| 6196 | } |
| 6197 | |
| 6198 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 6199 | return -EINVAL; |
| 6200 | |
| 6201 | __update_reg_bounds(dst_reg); |
| 6202 | __reg_deduce_bounds(dst_reg); |
| 6203 | __reg_bound_offset(dst_reg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6204 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 6205 | if (sanitize_check_bounds(env, insn, dst_reg) < 0) |
| 6206 | return -EACCES; |
| 6207 | if (sanitize_needed(opcode)) { |
| 6208 | ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg, |
| 6209 | &info, true); |
| 6210 | if (ret < 0) |
| 6211 | return sanitize_err(env, insn, ret, off_reg, dst_reg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6212 | } |
| 6213 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6214 | return 0; |
| 6215 | } |
| 6216 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6217 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
| 6218 | struct bpf_reg_state *src_reg) |
| 6219 | { |
| 6220 | s32 smin_val = src_reg->s32_min_value; |
| 6221 | s32 smax_val = src_reg->s32_max_value; |
| 6222 | u32 umin_val = src_reg->u32_min_value; |
| 6223 | u32 umax_val = src_reg->u32_max_value; |
| 6224 | |
| 6225 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || |
| 6226 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { |
| 6227 | dst_reg->s32_min_value = S32_MIN; |
| 6228 | dst_reg->s32_max_value = S32_MAX; |
| 6229 | } else { |
| 6230 | dst_reg->s32_min_value += smin_val; |
| 6231 | dst_reg->s32_max_value += smax_val; |
| 6232 | } |
| 6233 | if (dst_reg->u32_min_value + umin_val < umin_val || |
| 6234 | dst_reg->u32_max_value + umax_val < umax_val) { |
| 6235 | dst_reg->u32_min_value = 0; |
| 6236 | dst_reg->u32_max_value = U32_MAX; |
| 6237 | } else { |
| 6238 | dst_reg->u32_min_value += umin_val; |
| 6239 | dst_reg->u32_max_value += umax_val; |
| 6240 | } |
| 6241 | } |
| 6242 | |
| 6243 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
| 6244 | struct bpf_reg_state *src_reg) |
| 6245 | { |
| 6246 | s64 smin_val = src_reg->smin_value; |
| 6247 | s64 smax_val = src_reg->smax_value; |
| 6248 | u64 umin_val = src_reg->umin_value; |
| 6249 | u64 umax_val = src_reg->umax_value; |
| 6250 | |
| 6251 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 6252 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 6253 | dst_reg->smin_value = S64_MIN; |
| 6254 | dst_reg->smax_value = S64_MAX; |
| 6255 | } else { |
| 6256 | dst_reg->smin_value += smin_val; |
| 6257 | dst_reg->smax_value += smax_val; |
| 6258 | } |
| 6259 | if (dst_reg->umin_value + umin_val < umin_val || |
| 6260 | dst_reg->umax_value + umax_val < umax_val) { |
| 6261 | dst_reg->umin_value = 0; |
| 6262 | dst_reg->umax_value = U64_MAX; |
| 6263 | } else { |
| 6264 | dst_reg->umin_value += umin_val; |
| 6265 | dst_reg->umax_value += umax_val; |
| 6266 | } |
| 6267 | } |
| 6268 | |
| 6269 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, |
| 6270 | struct bpf_reg_state *src_reg) |
| 6271 | { |
| 6272 | s32 smin_val = src_reg->s32_min_value; |
| 6273 | s32 smax_val = src_reg->s32_max_value; |
| 6274 | u32 umin_val = src_reg->u32_min_value; |
| 6275 | u32 umax_val = src_reg->u32_max_value; |
| 6276 | |
| 6277 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || |
| 6278 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { |
| 6279 | /* Overflow possible, we know nothing */ |
| 6280 | dst_reg->s32_min_value = S32_MIN; |
| 6281 | dst_reg->s32_max_value = S32_MAX; |
| 6282 | } else { |
| 6283 | dst_reg->s32_min_value -= smax_val; |
| 6284 | dst_reg->s32_max_value -= smin_val; |
| 6285 | } |
| 6286 | if (dst_reg->u32_min_value < umax_val) { |
| 6287 | /* Overflow possible, we know nothing */ |
| 6288 | dst_reg->u32_min_value = 0; |
| 6289 | dst_reg->u32_max_value = U32_MAX; |
| 6290 | } else { |
| 6291 | /* Cannot overflow (as long as bounds are consistent) */ |
| 6292 | dst_reg->u32_min_value -= umax_val; |
| 6293 | dst_reg->u32_max_value -= umin_val; |
| 6294 | } |
| 6295 | } |
| 6296 | |
| 6297 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, |
| 6298 | struct bpf_reg_state *src_reg) |
| 6299 | { |
| 6300 | s64 smin_val = src_reg->smin_value; |
| 6301 | s64 smax_val = src_reg->smax_value; |
| 6302 | u64 umin_val = src_reg->umin_value; |
| 6303 | u64 umax_val = src_reg->umax_value; |
| 6304 | |
| 6305 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 6306 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 6307 | /* Overflow possible, we know nothing */ |
| 6308 | dst_reg->smin_value = S64_MIN; |
| 6309 | dst_reg->smax_value = S64_MAX; |
| 6310 | } else { |
| 6311 | dst_reg->smin_value -= smax_val; |
| 6312 | dst_reg->smax_value -= smin_val; |
| 6313 | } |
| 6314 | if (dst_reg->umin_value < umax_val) { |
| 6315 | /* Overflow possible, we know nothing */ |
| 6316 | dst_reg->umin_value = 0; |
| 6317 | dst_reg->umax_value = U64_MAX; |
| 6318 | } else { |
| 6319 | /* Cannot overflow (as long as bounds are consistent) */ |
| 6320 | dst_reg->umin_value -= umax_val; |
| 6321 | dst_reg->umax_value -= umin_val; |
| 6322 | } |
| 6323 | } |
| 6324 | |
| 6325 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, |
| 6326 | struct bpf_reg_state *src_reg) |
| 6327 | { |
| 6328 | s32 smin_val = src_reg->s32_min_value; |
| 6329 | u32 umin_val = src_reg->u32_min_value; |
| 6330 | u32 umax_val = src_reg->u32_max_value; |
| 6331 | |
| 6332 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { |
| 6333 | /* Ain't nobody got time to multiply that sign */ |
| 6334 | __mark_reg32_unbounded(dst_reg); |
| 6335 | return; |
| 6336 | } |
| 6337 | /* Both values are positive, so we can work with unsigned and |
| 6338 | * copy the result to signed (unless it exceeds S32_MAX). |
| 6339 | */ |
| 6340 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { |
| 6341 | /* Potential overflow, we know nothing */ |
| 6342 | __mark_reg32_unbounded(dst_reg); |
| 6343 | return; |
| 6344 | } |
| 6345 | dst_reg->u32_min_value *= umin_val; |
| 6346 | dst_reg->u32_max_value *= umax_val; |
| 6347 | if (dst_reg->u32_max_value > S32_MAX) { |
| 6348 | /* Overflow possible, we know nothing */ |
| 6349 | dst_reg->s32_min_value = S32_MIN; |
| 6350 | dst_reg->s32_max_value = S32_MAX; |
| 6351 | } else { |
| 6352 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 6353 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 6354 | } |
| 6355 | } |
| 6356 | |
| 6357 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, |
| 6358 | struct bpf_reg_state *src_reg) |
| 6359 | { |
| 6360 | s64 smin_val = src_reg->smin_value; |
| 6361 | u64 umin_val = src_reg->umin_value; |
| 6362 | u64 umax_val = src_reg->umax_value; |
| 6363 | |
| 6364 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
| 6365 | /* Ain't nobody got time to multiply that sign */ |
| 6366 | __mark_reg64_unbounded(dst_reg); |
| 6367 | return; |
| 6368 | } |
| 6369 | /* Both values are positive, so we can work with unsigned and |
| 6370 | * copy the result to signed (unless it exceeds S64_MAX). |
| 6371 | */ |
| 6372 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 6373 | /* Potential overflow, we know nothing */ |
| 6374 | __mark_reg64_unbounded(dst_reg); |
| 6375 | return; |
| 6376 | } |
| 6377 | dst_reg->umin_value *= umin_val; |
| 6378 | dst_reg->umax_value *= umax_val; |
| 6379 | if (dst_reg->umax_value > S64_MAX) { |
| 6380 | /* Overflow possible, we know nothing */ |
| 6381 | dst_reg->smin_value = S64_MIN; |
| 6382 | dst_reg->smax_value = S64_MAX; |
| 6383 | } else { |
| 6384 | dst_reg->smin_value = dst_reg->umin_value; |
| 6385 | dst_reg->smax_value = dst_reg->umax_value; |
| 6386 | } |
| 6387 | } |
| 6388 | |
| 6389 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
| 6390 | struct bpf_reg_state *src_reg) |
| 6391 | { |
| 6392 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 6393 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 6394 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 6395 | s32 smin_val = src_reg->s32_min_value; |
| 6396 | u32 umax_val = src_reg->u32_max_value; |
| 6397 | |
| 6398 | if (src_known && dst_known) { |
| 6399 | __mark_reg32_known(dst_reg, var32_off.value); |
| 6400 | return; |
| 6401 | } |
| 6402 | |
| 6403 | /* We get our minimum from the var_off, since that's inherently |
| 6404 | * bitwise. Our maximum is the minimum of the operands' maxima. |
| 6405 | */ |
| 6406 | dst_reg->u32_min_value = var32_off.value; |
| 6407 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); |
| 6408 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { |
| 6409 | /* Lose signed bounds when ANDing negative numbers, |
| 6410 | * ain't nobody got time for that. |
| 6411 | */ |
| 6412 | dst_reg->s32_min_value = S32_MIN; |
| 6413 | dst_reg->s32_max_value = S32_MAX; |
| 6414 | } else { |
| 6415 | /* ANDing two positives gives a positive, so safe to |
| 6416 | * cast result into s64. |
| 6417 | */ |
| 6418 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 6419 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 6420 | } |
| 6421 | } |
| 6422 | |
| 6423 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
| 6424 | struct bpf_reg_state *src_reg) |
| 6425 | { |
| 6426 | bool src_known = tnum_is_const(src_reg->var_off); |
| 6427 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 6428 | s64 smin_val = src_reg->smin_value; |
| 6429 | u64 umax_val = src_reg->umax_value; |
| 6430 | |
| 6431 | if (src_known && dst_known) { |
| 6432 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
| 6433 | return; |
| 6434 | } |
| 6435 | |
| 6436 | /* We get our minimum from the var_off, since that's inherently |
| 6437 | * bitwise. Our maximum is the minimum of the operands' maxima. |
| 6438 | */ |
| 6439 | dst_reg->umin_value = dst_reg->var_off.value; |
| 6440 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 6441 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 6442 | /* Lose signed bounds when ANDing negative numbers, |
| 6443 | * ain't nobody got time for that. |
| 6444 | */ |
| 6445 | dst_reg->smin_value = S64_MIN; |
| 6446 | dst_reg->smax_value = S64_MAX; |
| 6447 | } else { |
| 6448 | /* ANDing two positives gives a positive, so safe to |
| 6449 | * cast result into s64. |
| 6450 | */ |
| 6451 | dst_reg->smin_value = dst_reg->umin_value; |
| 6452 | dst_reg->smax_value = dst_reg->umax_value; |
| 6453 | } |
| 6454 | /* We may learn something more from the var_off */ |
| 6455 | __update_reg_bounds(dst_reg); |
| 6456 | } |
| 6457 | |
| 6458 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
| 6459 | struct bpf_reg_state *src_reg) |
| 6460 | { |
| 6461 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 6462 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 6463 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 6464 | s32 smin_val = src_reg->s32_min_value; |
| 6465 | u32 umin_val = src_reg->u32_min_value; |
| 6466 | |
| 6467 | if (src_known && dst_known) { |
| 6468 | __mark_reg32_known(dst_reg, var32_off.value); |
| 6469 | return; |
| 6470 | } |
| 6471 | |
| 6472 | /* We get our maximum from the var_off, and our minimum is the |
| 6473 | * maximum of the operands' minima |
| 6474 | */ |
| 6475 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); |
| 6476 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; |
| 6477 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { |
| 6478 | /* Lose signed bounds when ORing negative numbers, |
| 6479 | * ain't nobody got time for that. |
| 6480 | */ |
| 6481 | dst_reg->s32_min_value = S32_MIN; |
| 6482 | dst_reg->s32_max_value = S32_MAX; |
| 6483 | } else { |
| 6484 | /* ORing two positives gives a positive, so safe to |
| 6485 | * cast result into s64. |
| 6486 | */ |
| 6487 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 6488 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 6489 | } |
| 6490 | } |
| 6491 | |
| 6492 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
| 6493 | struct bpf_reg_state *src_reg) |
| 6494 | { |
| 6495 | bool src_known = tnum_is_const(src_reg->var_off); |
| 6496 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 6497 | s64 smin_val = src_reg->smin_value; |
| 6498 | u64 umin_val = src_reg->umin_value; |
| 6499 | |
| 6500 | if (src_known && dst_known) { |
| 6501 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
| 6502 | return; |
| 6503 | } |
| 6504 | |
| 6505 | /* We get our maximum from the var_off, and our minimum is the |
| 6506 | * maximum of the operands' minima |
| 6507 | */ |
| 6508 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 6509 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; |
| 6510 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 6511 | /* Lose signed bounds when ORing negative numbers, |
| 6512 | * ain't nobody got time for that. |
| 6513 | */ |
| 6514 | dst_reg->smin_value = S64_MIN; |
| 6515 | dst_reg->smax_value = S64_MAX; |
| 6516 | } else { |
| 6517 | /* ORing two positives gives a positive, so safe to |
| 6518 | * cast result into s64. |
| 6519 | */ |
| 6520 | dst_reg->smin_value = dst_reg->umin_value; |
| 6521 | dst_reg->smax_value = dst_reg->umax_value; |
| 6522 | } |
| 6523 | /* We may learn something more from the var_off */ |
| 6524 | __update_reg_bounds(dst_reg); |
| 6525 | } |
| 6526 | |
| 6527 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
| 6528 | struct bpf_reg_state *src_reg) |
| 6529 | { |
| 6530 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 6531 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 6532 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 6533 | s32 smin_val = src_reg->s32_min_value; |
| 6534 | |
| 6535 | if (src_known && dst_known) { |
| 6536 | __mark_reg32_known(dst_reg, var32_off.value); |
| 6537 | return; |
| 6538 | } |
| 6539 | |
| 6540 | /* We get both minimum and maximum from the var32_off. */ |
| 6541 | dst_reg->u32_min_value = var32_off.value; |
| 6542 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; |
| 6543 | |
| 6544 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { |
| 6545 | /* XORing two positive sign numbers gives a positive, |
| 6546 | * so safe to cast u32 result into s32. |
| 6547 | */ |
| 6548 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 6549 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 6550 | } else { |
| 6551 | dst_reg->s32_min_value = S32_MIN; |
| 6552 | dst_reg->s32_max_value = S32_MAX; |
| 6553 | } |
| 6554 | } |
| 6555 | |
| 6556 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, |
| 6557 | struct bpf_reg_state *src_reg) |
| 6558 | { |
| 6559 | bool src_known = tnum_is_const(src_reg->var_off); |
| 6560 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 6561 | s64 smin_val = src_reg->smin_value; |
| 6562 | |
| 6563 | if (src_known && dst_known) { |
| 6564 | /* dst_reg->var_off.value has been updated earlier */ |
| 6565 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
| 6566 | return; |
| 6567 | } |
| 6568 | |
| 6569 | /* We get both minimum and maximum from the var_off. */ |
| 6570 | dst_reg->umin_value = dst_reg->var_off.value; |
| 6571 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; |
| 6572 | |
| 6573 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { |
| 6574 | /* XORing two positive sign numbers gives a positive, |
| 6575 | * so safe to cast u64 result into s64. |
| 6576 | */ |
| 6577 | dst_reg->smin_value = dst_reg->umin_value; |
| 6578 | dst_reg->smax_value = dst_reg->umax_value; |
| 6579 | } else { |
| 6580 | dst_reg->smin_value = S64_MIN; |
| 6581 | dst_reg->smax_value = S64_MAX; |
| 6582 | } |
| 6583 | |
| 6584 | __update_reg_bounds(dst_reg); |
| 6585 | } |
| 6586 | |
| 6587 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6588 | u64 umin_val, u64 umax_val) |
| 6589 | { |
| 6590 | /* We lose all sign bit information (except what we can pick |
| 6591 | * up from var_off) |
| 6592 | */ |
| 6593 | dst_reg->s32_min_value = S32_MIN; |
| 6594 | dst_reg->s32_max_value = S32_MAX; |
| 6595 | /* If we might shift our top bit out, then we know nothing */ |
| 6596 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { |
| 6597 | dst_reg->u32_min_value = 0; |
| 6598 | dst_reg->u32_max_value = U32_MAX; |
| 6599 | } else { |
| 6600 | dst_reg->u32_min_value <<= umin_val; |
| 6601 | dst_reg->u32_max_value <<= umax_val; |
| 6602 | } |
| 6603 | } |
| 6604 | |
| 6605 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6606 | struct bpf_reg_state *src_reg) |
| 6607 | { |
| 6608 | u32 umax_val = src_reg->u32_max_value; |
| 6609 | u32 umin_val = src_reg->u32_min_value; |
| 6610 | /* u32 alu operation will zext upper bits */ |
| 6611 | struct tnum subreg = tnum_subreg(dst_reg->var_off); |
| 6612 | |
| 6613 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); |
| 6614 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); |
| 6615 | /* Not required but being careful mark reg64 bounds as unknown so |
| 6616 | * that we are forced to pick them up from tnum and zext later and |
| 6617 | * if some path skips this step we are still safe. |
| 6618 | */ |
| 6619 | __mark_reg64_unbounded(dst_reg); |
| 6620 | __update_reg32_bounds(dst_reg); |
| 6621 | } |
| 6622 | |
| 6623 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6624 | u64 umin_val, u64 umax_val) |
| 6625 | { |
| 6626 | /* Special case <<32 because it is a common compiler pattern to sign |
| 6627 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are |
| 6628 | * positive we know this shift will also be positive so we can track |
| 6629 | * bounds correctly. Otherwise we lose all sign bit information except |
| 6630 | * what we can pick up from var_off. Perhaps we can generalize this |
| 6631 | * later to shifts of any length. |
| 6632 | */ |
| 6633 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) |
| 6634 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; |
| 6635 | else |
| 6636 | dst_reg->smax_value = S64_MAX; |
| 6637 | |
| 6638 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) |
| 6639 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; |
| 6640 | else |
| 6641 | dst_reg->smin_value = S64_MIN; |
| 6642 | |
| 6643 | /* If we might shift our top bit out, then we know nothing */ |
| 6644 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 6645 | dst_reg->umin_value = 0; |
| 6646 | dst_reg->umax_value = U64_MAX; |
| 6647 | } else { |
| 6648 | dst_reg->umin_value <<= umin_val; |
| 6649 | dst_reg->umax_value <<= umax_val; |
| 6650 | } |
| 6651 | } |
| 6652 | |
| 6653 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6654 | struct bpf_reg_state *src_reg) |
| 6655 | { |
| 6656 | u64 umax_val = src_reg->umax_value; |
| 6657 | u64 umin_val = src_reg->umin_value; |
| 6658 | |
| 6659 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ |
| 6660 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); |
| 6661 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); |
| 6662 | |
| 6663 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
| 6664 | /* We may learn something more from the var_off */ |
| 6665 | __update_reg_bounds(dst_reg); |
| 6666 | } |
| 6667 | |
| 6668 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
| 6669 | struct bpf_reg_state *src_reg) |
| 6670 | { |
| 6671 | struct tnum subreg = tnum_subreg(dst_reg->var_off); |
| 6672 | u32 umax_val = src_reg->u32_max_value; |
| 6673 | u32 umin_val = src_reg->u32_min_value; |
| 6674 | |
| 6675 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 6676 | * be negative, then either: |
| 6677 | * 1) src_reg might be zero, so the sign bit of the result is |
| 6678 | * unknown, so we lose our signed bounds |
| 6679 | * 2) it's known negative, thus the unsigned bounds capture the |
| 6680 | * signed bounds |
| 6681 | * 3) the signed bounds cross zero, so they tell us nothing |
| 6682 | * about the result |
| 6683 | * If the value in dst_reg is known nonnegative, then again the |
| 6684 | * unsigned bounts capture the signed bounds. |
| 6685 | * Thus, in all cases it suffices to blow away our signed bounds |
| 6686 | * and rely on inferring new ones from the unsigned bounds and |
| 6687 | * var_off of the result. |
| 6688 | */ |
| 6689 | dst_reg->s32_min_value = S32_MIN; |
| 6690 | dst_reg->s32_max_value = S32_MAX; |
| 6691 | |
| 6692 | dst_reg->var_off = tnum_rshift(subreg, umin_val); |
| 6693 | dst_reg->u32_min_value >>= umax_val; |
| 6694 | dst_reg->u32_max_value >>= umin_val; |
| 6695 | |
| 6696 | __mark_reg64_unbounded(dst_reg); |
| 6697 | __update_reg32_bounds(dst_reg); |
| 6698 | } |
| 6699 | |
| 6700 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
| 6701 | struct bpf_reg_state *src_reg) |
| 6702 | { |
| 6703 | u64 umax_val = src_reg->umax_value; |
| 6704 | u64 umin_val = src_reg->umin_value; |
| 6705 | |
| 6706 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 6707 | * be negative, then either: |
| 6708 | * 1) src_reg might be zero, so the sign bit of the result is |
| 6709 | * unknown, so we lose our signed bounds |
| 6710 | * 2) it's known negative, thus the unsigned bounds capture the |
| 6711 | * signed bounds |
| 6712 | * 3) the signed bounds cross zero, so they tell us nothing |
| 6713 | * about the result |
| 6714 | * If the value in dst_reg is known nonnegative, then again the |
| 6715 | * unsigned bounts capture the signed bounds. |
| 6716 | * Thus, in all cases it suffices to blow away our signed bounds |
| 6717 | * and rely on inferring new ones from the unsigned bounds and |
| 6718 | * var_off of the result. |
| 6719 | */ |
| 6720 | dst_reg->smin_value = S64_MIN; |
| 6721 | dst_reg->smax_value = S64_MAX; |
| 6722 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
| 6723 | dst_reg->umin_value >>= umax_val; |
| 6724 | dst_reg->umax_value >>= umin_val; |
| 6725 | |
| 6726 | /* Its not easy to operate on alu32 bounds here because it depends |
| 6727 | * on bits being shifted in. Take easy way out and mark unbounded |
| 6728 | * so we can recalculate later from tnum. |
| 6729 | */ |
| 6730 | __mark_reg32_unbounded(dst_reg); |
| 6731 | __update_reg_bounds(dst_reg); |
| 6732 | } |
| 6733 | |
| 6734 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
| 6735 | struct bpf_reg_state *src_reg) |
| 6736 | { |
| 6737 | u64 umin_val = src_reg->u32_min_value; |
| 6738 | |
| 6739 | /* Upon reaching here, src_known is true and |
| 6740 | * umax_val is equal to umin_val. |
| 6741 | */ |
| 6742 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
| 6743 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); |
| 6744 | |
| 6745 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
| 6746 | |
| 6747 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 6748 | * dst_reg var_off to refine the result. |
| 6749 | */ |
| 6750 | dst_reg->u32_min_value = 0; |
| 6751 | dst_reg->u32_max_value = U32_MAX; |
| 6752 | |
| 6753 | __mark_reg64_unbounded(dst_reg); |
| 6754 | __update_reg32_bounds(dst_reg); |
| 6755 | } |
| 6756 | |
| 6757 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, |
| 6758 | struct bpf_reg_state *src_reg) |
| 6759 | { |
| 6760 | u64 umin_val = src_reg->umin_value; |
| 6761 | |
| 6762 | /* Upon reaching here, src_known is true and umax_val is equal |
| 6763 | * to umin_val. |
| 6764 | */ |
| 6765 | dst_reg->smin_value >>= umin_val; |
| 6766 | dst_reg->smax_value >>= umin_val; |
| 6767 | |
| 6768 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); |
| 6769 | |
| 6770 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 6771 | * dst_reg var_off to refine the result. |
| 6772 | */ |
| 6773 | dst_reg->umin_value = 0; |
| 6774 | dst_reg->umax_value = U64_MAX; |
| 6775 | |
| 6776 | /* Its not easy to operate on alu32 bounds here because it depends |
| 6777 | * on bits being shifted in from upper 32-bits. Take easy way out |
| 6778 | * and mark unbounded so we can recalculate later from tnum. |
| 6779 | */ |
| 6780 | __mark_reg32_unbounded(dst_reg); |
| 6781 | __update_reg_bounds(dst_reg); |
| 6782 | } |
| 6783 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6784 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 6785 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 6786 | * need extra checks in the 32-bit case. |
| 6787 | */ |
| 6788 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 6789 | struct bpf_insn *insn, |
| 6790 | struct bpf_reg_state *dst_reg, |
| 6791 | struct bpf_reg_state src_reg) |
| 6792 | { |
| 6793 | struct bpf_reg_state *regs = cur_regs(env); |
| 6794 | u8 opcode = BPF_OP(insn->code); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6795 | bool src_known; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6796 | s64 smin_val, smax_val; |
| 6797 | u64 umin_val, umax_val; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6798 | s32 s32_min_val, s32_max_val; |
| 6799 | u32 u32_min_val, u32_max_val; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6800 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6801 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6802 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6803 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6804 | smin_val = src_reg.smin_value; |
| 6805 | smax_val = src_reg.smax_value; |
| 6806 | umin_val = src_reg.umin_value; |
| 6807 | umax_val = src_reg.umax_value; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6808 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6809 | s32_min_val = src_reg.s32_min_value; |
| 6810 | s32_max_val = src_reg.s32_max_value; |
| 6811 | u32_min_val = src_reg.u32_min_value; |
| 6812 | u32_max_val = src_reg.u32_max_value; |
| 6813 | |
| 6814 | if (alu32) { |
| 6815 | src_known = tnum_subreg_is_const(src_reg.var_off); |
| 6816 | if ((src_known && |
| 6817 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || |
| 6818 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { |
| 6819 | /* Taint dst register if offset had invalid bounds |
| 6820 | * derived from e.g. dead branches. |
| 6821 | */ |
| 6822 | __mark_reg_unknown(env, dst_reg); |
| 6823 | return 0; |
| 6824 | } |
| 6825 | } else { |
| 6826 | src_known = tnum_is_const(src_reg.var_off); |
| 6827 | if ((src_known && |
| 6828 | (smin_val != smax_val || umin_val != umax_val)) || |
| 6829 | smin_val > smax_val || umin_val > umax_val) { |
| 6830 | /* Taint dst register if offset had invalid bounds |
| 6831 | * derived from e.g. dead branches. |
| 6832 | */ |
| 6833 | __mark_reg_unknown(env, dst_reg); |
| 6834 | return 0; |
| 6835 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6836 | } |
| 6837 | |
| 6838 | if (!src_known && |
| 6839 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 6840 | __mark_reg_unknown(env, dst_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6841 | return 0; |
| 6842 | } |
| 6843 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 6844 | if (sanitize_needed(opcode)) { |
| 6845 | ret = sanitize_val_alu(env, insn); |
| 6846 | if (ret < 0) |
| 6847 | return sanitize_err(env, insn, ret, NULL, NULL); |
| 6848 | } |
| 6849 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6850 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
| 6851 | * There are two classes of instructions: The first class we track both |
| 6852 | * alu32 and alu64 sign/unsigned bounds independently this provides the |
| 6853 | * greatest amount of precision when alu operations are mixed with jmp32 |
| 6854 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, |
| 6855 | * and BPF_OR. This is possible because these ops have fairly easy to |
| 6856 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. |
| 6857 | * See alu32 verifier tests for examples. The second class of |
| 6858 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy |
| 6859 | * with regards to tracking sign/unsigned bounds because the bits may |
| 6860 | * cross subreg boundaries in the alu64 case. When this happens we mark |
| 6861 | * the reg unbounded in the subreg bound space and use the resulting |
| 6862 | * tnum to calculate an approximation of the sign/unsigned bounds. |
| 6863 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6864 | switch (opcode) { |
| 6865 | case BPF_ADD: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6866 | scalar32_min_max_add(dst_reg, &src_reg); |
| 6867 | scalar_min_max_add(dst_reg, &src_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6868 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 6869 | break; |
| 6870 | case BPF_SUB: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6871 | scalar32_min_max_sub(dst_reg, &src_reg); |
| 6872 | scalar_min_max_sub(dst_reg, &src_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6873 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
| 6874 | break; |
| 6875 | case BPF_MUL: |
| 6876 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6877 | scalar32_min_max_mul(dst_reg, &src_reg); |
| 6878 | scalar_min_max_mul(dst_reg, &src_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6879 | break; |
| 6880 | case BPF_AND: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6881 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6882 | scalar32_min_max_and(dst_reg, &src_reg); |
| 6883 | scalar_min_max_and(dst_reg, &src_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6884 | break; |
| 6885 | case BPF_OR: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6886 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6887 | scalar32_min_max_or(dst_reg, &src_reg); |
| 6888 | scalar_min_max_or(dst_reg, &src_reg); |
| 6889 | break; |
| 6890 | case BPF_XOR: |
| 6891 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); |
| 6892 | scalar32_min_max_xor(dst_reg, &src_reg); |
| 6893 | scalar_min_max_xor(dst_reg, &src_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6894 | break; |
| 6895 | case BPF_LSH: |
| 6896 | if (umax_val >= insn_bitness) { |
| 6897 | /* Shifts greater than 31 or 63 are undefined. |
| 6898 | * This includes shifts by a negative number. |
| 6899 | */ |
| 6900 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 6901 | break; |
| 6902 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6903 | if (alu32) |
| 6904 | scalar32_min_max_lsh(dst_reg, &src_reg); |
| 6905 | else |
| 6906 | scalar_min_max_lsh(dst_reg, &src_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6907 | break; |
| 6908 | case BPF_RSH: |
| 6909 | if (umax_val >= insn_bitness) { |
| 6910 | /* Shifts greater than 31 or 63 are undefined. |
| 6911 | * This includes shifts by a negative number. |
| 6912 | */ |
| 6913 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 6914 | break; |
| 6915 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6916 | if (alu32) |
| 6917 | scalar32_min_max_rsh(dst_reg, &src_reg); |
| 6918 | else |
| 6919 | scalar_min_max_rsh(dst_reg, &src_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6920 | break; |
| 6921 | case BPF_ARSH: |
| 6922 | if (umax_val >= insn_bitness) { |
| 6923 | /* Shifts greater than 31 or 63 are undefined. |
| 6924 | * This includes shifts by a negative number. |
| 6925 | */ |
| 6926 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 6927 | break; |
| 6928 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6929 | if (alu32) |
| 6930 | scalar32_min_max_arsh(dst_reg, &src_reg); |
| 6931 | else |
| 6932 | scalar_min_max_arsh(dst_reg, &src_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6933 | break; |
| 6934 | default: |
| 6935 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 6936 | break; |
| 6937 | } |
| 6938 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6939 | /* ALU32 ops are zero extended into 64bit register */ |
| 6940 | if (alu32) |
| 6941 | zext_32_to_64(dst_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6942 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6943 | __update_reg_bounds(dst_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6944 | __reg_deduce_bounds(dst_reg); |
| 6945 | __reg_bound_offset(dst_reg); |
| 6946 | return 0; |
| 6947 | } |
| 6948 | |
| 6949 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 6950 | * and var_off. |
| 6951 | */ |
| 6952 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 6953 | struct bpf_insn *insn) |
| 6954 | { |
| 6955 | struct bpf_verifier_state *vstate = env->cur_state; |
| 6956 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 6957 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
| 6958 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 6959 | u8 opcode = BPF_OP(insn->code); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6960 | int err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6961 | |
| 6962 | dst_reg = ®s[insn->dst_reg]; |
| 6963 | src_reg = NULL; |
| 6964 | if (dst_reg->type != SCALAR_VALUE) |
| 6965 | ptr_reg = dst_reg; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 6966 | else |
| 6967 | /* Make sure ID is cleared otherwise dst_reg min/max could be |
| 6968 | * incorrectly propagated into other registers by find_equal_scalars() |
| 6969 | */ |
| 6970 | dst_reg->id = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6971 | if (BPF_SRC(insn->code) == BPF_X) { |
| 6972 | src_reg = ®s[insn->src_reg]; |
| 6973 | if (src_reg->type != SCALAR_VALUE) { |
| 6974 | if (dst_reg->type != SCALAR_VALUE) { |
| 6975 | /* Combining two pointers by any ALU op yields |
| 6976 | * an arbitrary scalar. Disallow all math except |
| 6977 | * pointer subtraction |
| 6978 | */ |
| 6979 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
| 6980 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 6981 | return 0; |
| 6982 | } |
| 6983 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 6984 | insn->dst_reg, |
| 6985 | bpf_alu_string[opcode >> 4]); |
| 6986 | return -EACCES; |
| 6987 | } else { |
| 6988 | /* scalar += pointer |
| 6989 | * This is legal, but we have to reverse our |
| 6990 | * src/dest handling in computing the range |
| 6991 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6992 | err = mark_chain_precision(env, insn->dst_reg); |
| 6993 | if (err) |
| 6994 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6995 | return adjust_ptr_min_max_vals(env, insn, |
| 6996 | src_reg, dst_reg); |
| 6997 | } |
| 6998 | } else if (ptr_reg) { |
| 6999 | /* pointer += scalar */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7000 | err = mark_chain_precision(env, insn->src_reg); |
| 7001 | if (err) |
| 7002 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7003 | return adjust_ptr_min_max_vals(env, insn, |
| 7004 | dst_reg, src_reg); |
| 7005 | } |
| 7006 | } else { |
| 7007 | /* Pretend the src is a reg with a known value, since we only |
| 7008 | * need to be able to read from this state. |
| 7009 | */ |
| 7010 | off_reg.type = SCALAR_VALUE; |
| 7011 | __mark_reg_known(&off_reg, insn->imm); |
| 7012 | src_reg = &off_reg; |
| 7013 | if (ptr_reg) /* pointer += K */ |
| 7014 | return adjust_ptr_min_max_vals(env, insn, |
| 7015 | ptr_reg, src_reg); |
| 7016 | } |
| 7017 | |
| 7018 | /* Got here implies adding two SCALAR_VALUEs */ |
| 7019 | if (WARN_ON_ONCE(ptr_reg)) { |
| 7020 | print_verifier_state(env, state); |
| 7021 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
| 7022 | return -EINVAL; |
| 7023 | } |
| 7024 | if (WARN_ON(!src_reg)) { |
| 7025 | print_verifier_state(env, state); |
| 7026 | verbose(env, "verifier internal error: no src_reg\n"); |
| 7027 | return -EINVAL; |
| 7028 | } |
| 7029 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
| 7030 | } |
| 7031 | |
| 7032 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
| 7033 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 7034 | { |
| 7035 | struct bpf_reg_state *regs = cur_regs(env); |
| 7036 | u8 opcode = BPF_OP(insn->code); |
| 7037 | int err; |
| 7038 | |
| 7039 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 7040 | if (opcode == BPF_NEG) { |
| 7041 | if (BPF_SRC(insn->code) != 0 || |
| 7042 | insn->src_reg != BPF_REG_0 || |
| 7043 | insn->off != 0 || insn->imm != 0) { |
| 7044 | verbose(env, "BPF_NEG uses reserved fields\n"); |
| 7045 | return -EINVAL; |
| 7046 | } |
| 7047 | } else { |
| 7048 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
| 7049 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 7050 | BPF_CLASS(insn->code) == BPF_ALU64) { |
| 7051 | verbose(env, "BPF_END uses reserved fields\n"); |
| 7052 | return -EINVAL; |
| 7053 | } |
| 7054 | } |
| 7055 | |
| 7056 | /* check src operand */ |
| 7057 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
| 7058 | if (err) |
| 7059 | return err; |
| 7060 | |
| 7061 | if (is_pointer_value(env, insn->dst_reg)) { |
| 7062 | verbose(env, "R%d pointer arithmetic prohibited\n", |
| 7063 | insn->dst_reg); |
| 7064 | return -EACCES; |
| 7065 | } |
| 7066 | |
| 7067 | /* check dest operand */ |
| 7068 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
| 7069 | if (err) |
| 7070 | return err; |
| 7071 | |
| 7072 | } else if (opcode == BPF_MOV) { |
| 7073 | |
| 7074 | if (BPF_SRC(insn->code) == BPF_X) { |
| 7075 | if (insn->imm != 0 || insn->off != 0) { |
| 7076 | verbose(env, "BPF_MOV uses reserved fields\n"); |
| 7077 | return -EINVAL; |
| 7078 | } |
| 7079 | |
| 7080 | /* check src operand */ |
| 7081 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 7082 | if (err) |
| 7083 | return err; |
| 7084 | } else { |
| 7085 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 7086 | verbose(env, "BPF_MOV uses reserved fields\n"); |
| 7087 | return -EINVAL; |
| 7088 | } |
| 7089 | } |
| 7090 | |
| 7091 | /* check dest operand, mark as required later */ |
| 7092 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
| 7093 | if (err) |
| 7094 | return err; |
| 7095 | |
| 7096 | if (BPF_SRC(insn->code) == BPF_X) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7097 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
| 7098 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; |
| 7099 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7100 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 7101 | /* case: R1 = R2 |
| 7102 | * copy register state to dest reg |
| 7103 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7104 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
| 7105 | /* Assign src and dst registers the same ID |
| 7106 | * that will be used by find_equal_scalars() |
| 7107 | * to propagate min/max range. |
| 7108 | */ |
| 7109 | src_reg->id = ++env->id_gen; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7110 | *dst_reg = *src_reg; |
| 7111 | dst_reg->live |= REG_LIVE_WRITTEN; |
| 7112 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7113 | } else { |
| 7114 | /* R1 = (u32) R2 */ |
| 7115 | if (is_pointer_value(env, insn->src_reg)) { |
| 7116 | verbose(env, |
| 7117 | "R%d partial copy of pointer\n", |
| 7118 | insn->src_reg); |
| 7119 | return -EACCES; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7120 | } else if (src_reg->type == SCALAR_VALUE) { |
| 7121 | *dst_reg = *src_reg; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7122 | /* Make sure ID is cleared otherwise |
| 7123 | * dst_reg min/max could be incorrectly |
| 7124 | * propagated into src_reg by find_equal_scalars() |
| 7125 | */ |
| 7126 | dst_reg->id = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7127 | dst_reg->live |= REG_LIVE_WRITTEN; |
| 7128 | dst_reg->subreg_def = env->insn_idx + 1; |
| 7129 | } else { |
| 7130 | mark_reg_unknown(env, regs, |
| 7131 | insn->dst_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7132 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7133 | zext_32_to_64(dst_reg); |
| 7134 | |
| 7135 | __update_reg_bounds(dst_reg); |
| 7136 | __reg_deduce_bounds(dst_reg); |
| 7137 | __reg_bound_offset(dst_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7138 | } |
| 7139 | } else { |
| 7140 | /* case: R = imm |
| 7141 | * remember the value we stored into this reg |
| 7142 | */ |
| 7143 | /* clear any state __mark_reg_known doesn't set */ |
| 7144 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 7145 | regs[insn->dst_reg].type = SCALAR_VALUE; |
| 7146 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 7147 | __mark_reg_known(regs + insn->dst_reg, |
| 7148 | insn->imm); |
| 7149 | } else { |
| 7150 | __mark_reg_known(regs + insn->dst_reg, |
| 7151 | (u32)insn->imm); |
| 7152 | } |
| 7153 | } |
| 7154 | |
| 7155 | } else if (opcode > BPF_END) { |
| 7156 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
| 7157 | return -EINVAL; |
| 7158 | |
| 7159 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 7160 | |
| 7161 | if (BPF_SRC(insn->code) == BPF_X) { |
| 7162 | if (insn->imm != 0 || insn->off != 0) { |
| 7163 | verbose(env, "BPF_ALU uses reserved fields\n"); |
| 7164 | return -EINVAL; |
| 7165 | } |
| 7166 | /* check src1 operand */ |
| 7167 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 7168 | if (err) |
| 7169 | return err; |
| 7170 | } else { |
| 7171 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 7172 | verbose(env, "BPF_ALU uses reserved fields\n"); |
| 7173 | return -EINVAL; |
| 7174 | } |
| 7175 | } |
| 7176 | |
| 7177 | /* check src2 operand */ |
| 7178 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
| 7179 | if (err) |
| 7180 | return err; |
| 7181 | |
| 7182 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 7183 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
| 7184 | verbose(env, "div by zero\n"); |
| 7185 | return -EINVAL; |
| 7186 | } |
| 7187 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7188 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 7189 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 7190 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 7191 | |
| 7192 | if (insn->imm < 0 || insn->imm >= size) { |
| 7193 | verbose(env, "invalid shift %d\n", insn->imm); |
| 7194 | return -EINVAL; |
| 7195 | } |
| 7196 | } |
| 7197 | |
| 7198 | /* check dest operand */ |
| 7199 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
| 7200 | if (err) |
| 7201 | return err; |
| 7202 | |
| 7203 | return adjust_reg_min_max_vals(env, insn); |
| 7204 | } |
| 7205 | |
| 7206 | return 0; |
| 7207 | } |
| 7208 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7209 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
| 7210 | struct bpf_reg_state *dst_reg, |
| 7211 | enum bpf_reg_type type, u16 new_range) |
| 7212 | { |
| 7213 | struct bpf_reg_state *reg; |
| 7214 | int i; |
| 7215 | |
| 7216 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 7217 | reg = &state->regs[i]; |
| 7218 | if (reg->type == type && reg->id == dst_reg->id) |
| 7219 | /* keep the maximum range already checked */ |
| 7220 | reg->range = max(reg->range, new_range); |
| 7221 | } |
| 7222 | |
| 7223 | bpf_for_each_spilled_reg(i, state, reg) { |
| 7224 | if (!reg) |
| 7225 | continue; |
| 7226 | if (reg->type == type && reg->id == dst_reg->id) |
| 7227 | reg->range = max(reg->range, new_range); |
| 7228 | } |
| 7229 | } |
| 7230 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7231 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
| 7232 | struct bpf_reg_state *dst_reg, |
| 7233 | enum bpf_reg_type type, |
| 7234 | bool range_right_open) |
| 7235 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7236 | u16 new_range; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7237 | int i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7238 | |
| 7239 | if (dst_reg->off < 0 || |
| 7240 | (dst_reg->off == 0 && range_right_open)) |
| 7241 | /* This doesn't give us any range */ |
| 7242 | return; |
| 7243 | |
| 7244 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 7245 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
| 7246 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 7247 | * than pkt_end, but that's because it's also less than pkt. |
| 7248 | */ |
| 7249 | return; |
| 7250 | |
| 7251 | new_range = dst_reg->off; |
| 7252 | if (range_right_open) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7253 | new_range++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7254 | |
| 7255 | /* Examples for register markings: |
| 7256 | * |
| 7257 | * pkt_data in dst register: |
| 7258 | * |
| 7259 | * r2 = r3; |
| 7260 | * r2 += 8; |
| 7261 | * if (r2 > pkt_end) goto <handle exception> |
| 7262 | * <access okay> |
| 7263 | * |
| 7264 | * r2 = r3; |
| 7265 | * r2 += 8; |
| 7266 | * if (r2 < pkt_end) goto <access okay> |
| 7267 | * <handle exception> |
| 7268 | * |
| 7269 | * Where: |
| 7270 | * r2 == dst_reg, pkt_end == src_reg |
| 7271 | * r2=pkt(id=n,off=8,r=0) |
| 7272 | * r3=pkt(id=n,off=0,r=0) |
| 7273 | * |
| 7274 | * pkt_data in src register: |
| 7275 | * |
| 7276 | * r2 = r3; |
| 7277 | * r2 += 8; |
| 7278 | * if (pkt_end >= r2) goto <access okay> |
| 7279 | * <handle exception> |
| 7280 | * |
| 7281 | * r2 = r3; |
| 7282 | * r2 += 8; |
| 7283 | * if (pkt_end <= r2) goto <handle exception> |
| 7284 | * <access okay> |
| 7285 | * |
| 7286 | * Where: |
| 7287 | * pkt_end == dst_reg, r2 == src_reg |
| 7288 | * r2=pkt(id=n,off=8,r=0) |
| 7289 | * r3=pkt(id=n,off=0,r=0) |
| 7290 | * |
| 7291 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) |
| 7292 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 7293 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 7294 | * the check. |
| 7295 | */ |
| 7296 | |
| 7297 | /* If our ids match, then we must have the same max_value. And we |
| 7298 | * don't care about the other reg's fixed offset, since if it's too big |
| 7299 | * the range won't allow anything. |
| 7300 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 7301 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7302 | for (i = 0; i <= vstate->curframe; i++) |
| 7303 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, |
| 7304 | new_range); |
| 7305 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7306 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7307 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7308 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7309 | struct tnum subreg = tnum_subreg(reg->var_off); |
| 7310 | s32 sval = (s32)val; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7311 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7312 | switch (opcode) { |
| 7313 | case BPF_JEQ: |
| 7314 | if (tnum_is_const(subreg)) |
| 7315 | return !!tnum_equals_const(subreg, val); |
| 7316 | break; |
| 7317 | case BPF_JNE: |
| 7318 | if (tnum_is_const(subreg)) |
| 7319 | return !tnum_equals_const(subreg, val); |
| 7320 | break; |
| 7321 | case BPF_JSET: |
| 7322 | if ((~subreg.mask & subreg.value) & val) |
| 7323 | return 1; |
| 7324 | if (!((subreg.mask | subreg.value) & val)) |
| 7325 | return 0; |
| 7326 | break; |
| 7327 | case BPF_JGT: |
| 7328 | if (reg->u32_min_value > val) |
| 7329 | return 1; |
| 7330 | else if (reg->u32_max_value <= val) |
| 7331 | return 0; |
| 7332 | break; |
| 7333 | case BPF_JSGT: |
| 7334 | if (reg->s32_min_value > sval) |
| 7335 | return 1; |
| 7336 | else if (reg->s32_max_value <= sval) |
| 7337 | return 0; |
| 7338 | break; |
| 7339 | case BPF_JLT: |
| 7340 | if (reg->u32_max_value < val) |
| 7341 | return 1; |
| 7342 | else if (reg->u32_min_value >= val) |
| 7343 | return 0; |
| 7344 | break; |
| 7345 | case BPF_JSLT: |
| 7346 | if (reg->s32_max_value < sval) |
| 7347 | return 1; |
| 7348 | else if (reg->s32_min_value >= sval) |
| 7349 | return 0; |
| 7350 | break; |
| 7351 | case BPF_JGE: |
| 7352 | if (reg->u32_min_value >= val) |
| 7353 | return 1; |
| 7354 | else if (reg->u32_max_value < val) |
| 7355 | return 0; |
| 7356 | break; |
| 7357 | case BPF_JSGE: |
| 7358 | if (reg->s32_min_value >= sval) |
| 7359 | return 1; |
| 7360 | else if (reg->s32_max_value < sval) |
| 7361 | return 0; |
| 7362 | break; |
| 7363 | case BPF_JLE: |
| 7364 | if (reg->u32_max_value <= val) |
| 7365 | return 1; |
| 7366 | else if (reg->u32_min_value > val) |
| 7367 | return 0; |
| 7368 | break; |
| 7369 | case BPF_JSLE: |
| 7370 | if (reg->s32_max_value <= sval) |
| 7371 | return 1; |
| 7372 | else if (reg->s32_min_value > sval) |
| 7373 | return 0; |
| 7374 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7375 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7376 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7377 | return -1; |
| 7378 | } |
| 7379 | |
| 7380 | |
| 7381 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) |
| 7382 | { |
| 7383 | s64 sval = (s64)val; |
| 7384 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7385 | switch (opcode) { |
| 7386 | case BPF_JEQ: |
| 7387 | if (tnum_is_const(reg->var_off)) |
| 7388 | return !!tnum_equals_const(reg->var_off, val); |
| 7389 | break; |
| 7390 | case BPF_JNE: |
| 7391 | if (tnum_is_const(reg->var_off)) |
| 7392 | return !tnum_equals_const(reg->var_off, val); |
| 7393 | break; |
| 7394 | case BPF_JSET: |
| 7395 | if ((~reg->var_off.mask & reg->var_off.value) & val) |
| 7396 | return 1; |
| 7397 | if (!((reg->var_off.mask | reg->var_off.value) & val)) |
| 7398 | return 0; |
| 7399 | break; |
| 7400 | case BPF_JGT: |
| 7401 | if (reg->umin_value > val) |
| 7402 | return 1; |
| 7403 | else if (reg->umax_value <= val) |
| 7404 | return 0; |
| 7405 | break; |
| 7406 | case BPF_JSGT: |
| 7407 | if (reg->smin_value > sval) |
| 7408 | return 1; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7409 | else if (reg->smax_value <= sval) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7410 | return 0; |
| 7411 | break; |
| 7412 | case BPF_JLT: |
| 7413 | if (reg->umax_value < val) |
| 7414 | return 1; |
| 7415 | else if (reg->umin_value >= val) |
| 7416 | return 0; |
| 7417 | break; |
| 7418 | case BPF_JSLT: |
| 7419 | if (reg->smax_value < sval) |
| 7420 | return 1; |
| 7421 | else if (reg->smin_value >= sval) |
| 7422 | return 0; |
| 7423 | break; |
| 7424 | case BPF_JGE: |
| 7425 | if (reg->umin_value >= val) |
| 7426 | return 1; |
| 7427 | else if (reg->umax_value < val) |
| 7428 | return 0; |
| 7429 | break; |
| 7430 | case BPF_JSGE: |
| 7431 | if (reg->smin_value >= sval) |
| 7432 | return 1; |
| 7433 | else if (reg->smax_value < sval) |
| 7434 | return 0; |
| 7435 | break; |
| 7436 | case BPF_JLE: |
| 7437 | if (reg->umax_value <= val) |
| 7438 | return 1; |
| 7439 | else if (reg->umin_value > val) |
| 7440 | return 0; |
| 7441 | break; |
| 7442 | case BPF_JSLE: |
| 7443 | if (reg->smax_value <= sval) |
| 7444 | return 1; |
| 7445 | else if (reg->smin_value > sval) |
| 7446 | return 0; |
| 7447 | break; |
| 7448 | } |
| 7449 | |
| 7450 | return -1; |
| 7451 | } |
| 7452 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7453 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
| 7454 | * and return: |
| 7455 | * 1 - branch will be taken and "goto target" will be executed |
| 7456 | * 0 - branch will not be taken and fall-through to next insn |
| 7457 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value |
| 7458 | * range [0,10] |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7459 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7460 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
| 7461 | bool is_jmp32) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7462 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7463 | if (__is_pointer_value(false, reg)) { |
| 7464 | if (!reg_type_not_null(reg->type)) |
| 7465 | return -1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7466 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7467 | /* If pointer is valid tests against zero will fail so we can |
| 7468 | * use this to direct branch taken. |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7469 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7470 | if (val != 0) |
| 7471 | return -1; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7472 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7473 | switch (opcode) { |
| 7474 | case BPF_JEQ: |
| 7475 | return 0; |
| 7476 | case BPF_JNE: |
| 7477 | return 1; |
| 7478 | default: |
| 7479 | return -1; |
| 7480 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7481 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7482 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7483 | if (is_jmp32) |
| 7484 | return is_branch32_taken(reg, val, opcode); |
| 7485 | return is_branch64_taken(reg, val, opcode); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 7486 | } |
| 7487 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7488 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 7489 | * variable register that we are working on, and src_reg is a constant or we're |
| 7490 | * simply doing a BPF_K check. |
| 7491 | * In JEQ/JNE cases we also adjust the var_off values. |
| 7492 | */ |
| 7493 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7494 | struct bpf_reg_state *false_reg, |
| 7495 | u64 val, u32 val32, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7496 | u8 opcode, bool is_jmp32) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7497 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7498 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
| 7499 | struct tnum false_64off = false_reg->var_off; |
| 7500 | struct tnum true_32off = tnum_subreg(true_reg->var_off); |
| 7501 | struct tnum true_64off = true_reg->var_off; |
| 7502 | s64 sval = (s64)val; |
| 7503 | s32 sval32 = (s32)val32; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7504 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7505 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 7506 | * variable offset from the compare (unless src_reg were a pointer into |
| 7507 | * the same object, but we don't bother with that. |
| 7508 | * Since false_reg and true_reg have the same type by construction, we |
| 7509 | * only need to check one of them for pointerness. |
| 7510 | */ |
| 7511 | if (__is_pointer_value(false, false_reg)) |
| 7512 | return; |
| 7513 | |
| 7514 | switch (opcode) { |
| 7515 | case BPF_JEQ: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7516 | case BPF_JNE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7517 | { |
| 7518 | struct bpf_reg_state *reg = |
| 7519 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 7520 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7521 | /* JEQ/JNE comparison doesn't change the register equivalence. |
| 7522 | * r1 = r2; |
| 7523 | * if (r1 == 42) goto label; |
| 7524 | * ... |
| 7525 | * label: // here both r1 and r2 are known to be 42. |
| 7526 | * |
| 7527 | * Hence when marking register as known preserve it's ID. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7528 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7529 | if (is_jmp32) |
| 7530 | __mark_reg32_known(reg, val32); |
| 7531 | else |
| 7532 | ___mark_reg_known(reg, val); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7533 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7534 | } |
| 7535 | case BPF_JSET: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7536 | if (is_jmp32) { |
| 7537 | false_32off = tnum_and(false_32off, tnum_const(~val32)); |
| 7538 | if (is_power_of_2(val32)) |
| 7539 | true_32off = tnum_or(true_32off, |
| 7540 | tnum_const(val32)); |
| 7541 | } else { |
| 7542 | false_64off = tnum_and(false_64off, tnum_const(~val)); |
| 7543 | if (is_power_of_2(val)) |
| 7544 | true_64off = tnum_or(true_64off, |
| 7545 | tnum_const(val)); |
| 7546 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7547 | break; |
| 7548 | case BPF_JGE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7549 | case BPF_JGT: |
| 7550 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7551 | if (is_jmp32) { |
| 7552 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; |
| 7553 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; |
| 7554 | |
| 7555 | false_reg->u32_max_value = min(false_reg->u32_max_value, |
| 7556 | false_umax); |
| 7557 | true_reg->u32_min_value = max(true_reg->u32_min_value, |
| 7558 | true_umin); |
| 7559 | } else { |
| 7560 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; |
| 7561 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; |
| 7562 | |
| 7563 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 7564 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
| 7565 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7566 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7567 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7568 | case BPF_JSGE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7569 | case BPF_JSGT: |
| 7570 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7571 | if (is_jmp32) { |
| 7572 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; |
| 7573 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7574 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7575 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
| 7576 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); |
| 7577 | } else { |
| 7578 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; |
| 7579 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; |
| 7580 | |
| 7581 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 7582 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
| 7583 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7584 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7585 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7586 | case BPF_JLE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7587 | case BPF_JLT: |
| 7588 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7589 | if (is_jmp32) { |
| 7590 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; |
| 7591 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; |
| 7592 | |
| 7593 | false_reg->u32_min_value = max(false_reg->u32_min_value, |
| 7594 | false_umin); |
| 7595 | true_reg->u32_max_value = min(true_reg->u32_max_value, |
| 7596 | true_umax); |
| 7597 | } else { |
| 7598 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; |
| 7599 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; |
| 7600 | |
| 7601 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 7602 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
| 7603 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7604 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7605 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7606 | case BPF_JSLE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7607 | case BPF_JSLT: |
| 7608 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7609 | if (is_jmp32) { |
| 7610 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; |
| 7611 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7612 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7613 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
| 7614 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); |
| 7615 | } else { |
| 7616 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; |
| 7617 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; |
| 7618 | |
| 7619 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 7620 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
| 7621 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7622 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7623 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7624 | default: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7625 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7626 | } |
| 7627 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7628 | if (is_jmp32) { |
| 7629 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), |
| 7630 | tnum_subreg(false_32off)); |
| 7631 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), |
| 7632 | tnum_subreg(true_32off)); |
| 7633 | __reg_combine_32_into_64(false_reg); |
| 7634 | __reg_combine_32_into_64(true_reg); |
| 7635 | } else { |
| 7636 | false_reg->var_off = false_64off; |
| 7637 | true_reg->var_off = true_64off; |
| 7638 | __reg_combine_64_into_32(false_reg); |
| 7639 | __reg_combine_64_into_32(true_reg); |
| 7640 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7641 | } |
| 7642 | |
| 7643 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 7644 | * the variable reg. |
| 7645 | */ |
| 7646 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7647 | struct bpf_reg_state *false_reg, |
| 7648 | u64 val, u32 val32, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7649 | u8 opcode, bool is_jmp32) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7650 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7651 | /* How can we transform "a <op> b" into "b <op> a"? */ |
| 7652 | static const u8 opcode_flip[16] = { |
| 7653 | /* these stay the same */ |
| 7654 | [BPF_JEQ >> 4] = BPF_JEQ, |
| 7655 | [BPF_JNE >> 4] = BPF_JNE, |
| 7656 | [BPF_JSET >> 4] = BPF_JSET, |
| 7657 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ |
| 7658 | [BPF_JGE >> 4] = BPF_JLE, |
| 7659 | [BPF_JGT >> 4] = BPF_JLT, |
| 7660 | [BPF_JLE >> 4] = BPF_JGE, |
| 7661 | [BPF_JLT >> 4] = BPF_JGT, |
| 7662 | [BPF_JSGE >> 4] = BPF_JSLE, |
| 7663 | [BPF_JSGT >> 4] = BPF_JSLT, |
| 7664 | [BPF_JSLE >> 4] = BPF_JSGE, |
| 7665 | [BPF_JSLT >> 4] = BPF_JSGT |
| 7666 | }; |
| 7667 | opcode = opcode_flip[opcode >> 4]; |
| 7668 | /* This uses zero as "not present in table"; luckily the zero opcode, |
| 7669 | * BPF_JA, can't get here. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7670 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7671 | if (opcode) |
| 7672 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7673 | } |
| 7674 | |
| 7675 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 7676 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 7677 | struct bpf_reg_state *dst_reg) |
| 7678 | { |
| 7679 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 7680 | dst_reg->umin_value); |
| 7681 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 7682 | dst_reg->umax_value); |
| 7683 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 7684 | dst_reg->smin_value); |
| 7685 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 7686 | dst_reg->smax_value); |
| 7687 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 7688 | dst_reg->var_off); |
| 7689 | /* We might have learned new bounds from the var_off. */ |
| 7690 | __update_reg_bounds(src_reg); |
| 7691 | __update_reg_bounds(dst_reg); |
| 7692 | /* We might have learned something about the sign bit. */ |
| 7693 | __reg_deduce_bounds(src_reg); |
| 7694 | __reg_deduce_bounds(dst_reg); |
| 7695 | /* We might have learned some bits from the bounds. */ |
| 7696 | __reg_bound_offset(src_reg); |
| 7697 | __reg_bound_offset(dst_reg); |
| 7698 | /* Intersecting with the old var_off might have improved our bounds |
| 7699 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 7700 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 7701 | */ |
| 7702 | __update_reg_bounds(src_reg); |
| 7703 | __update_reg_bounds(dst_reg); |
| 7704 | } |
| 7705 | |
| 7706 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 7707 | struct bpf_reg_state *true_dst, |
| 7708 | struct bpf_reg_state *false_src, |
| 7709 | struct bpf_reg_state *false_dst, |
| 7710 | u8 opcode) |
| 7711 | { |
| 7712 | switch (opcode) { |
| 7713 | case BPF_JEQ: |
| 7714 | __reg_combine_min_max(true_src, true_dst); |
| 7715 | break; |
| 7716 | case BPF_JNE: |
| 7717 | __reg_combine_min_max(false_src, false_dst); |
| 7718 | break; |
| 7719 | } |
| 7720 | } |
| 7721 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7722 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 7723 | struct bpf_reg_state *reg, u32 id, |
| 7724 | bool is_null) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7725 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7726 | if (reg_type_may_be_null(reg->type) && reg->id == id && |
| 7727 | !WARN_ON_ONCE(!reg->id)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7728 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 7729 | !tnum_equals_const(reg->var_off, 0) || |
| 7730 | reg->off)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7731 | /* Old offset (both fixed and variable parts) should |
| 7732 | * have been known-zero, because we don't allow pointer |
| 7733 | * arithmetic on pointers that might be NULL. If we |
| 7734 | * see this happening, don't convert the register. |
| 7735 | */ |
| 7736 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7737 | } |
| 7738 | if (is_null) { |
| 7739 | reg->type = SCALAR_VALUE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7740 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7741 | const struct bpf_map *map = reg->map_ptr; |
| 7742 | |
| 7743 | if (map->inner_map_meta) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7744 | reg->type = CONST_PTR_TO_MAP; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7745 | reg->map_ptr = map->inner_map_meta; |
| 7746 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7747 | reg->type = PTR_TO_XDP_SOCK; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7748 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || |
| 7749 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { |
| 7750 | reg->type = PTR_TO_SOCKET; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7751 | } else { |
| 7752 | reg->type = PTR_TO_MAP_VALUE; |
| 7753 | } |
| 7754 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
| 7755 | reg->type = PTR_TO_SOCKET; |
| 7756 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
| 7757 | reg->type = PTR_TO_SOCK_COMMON; |
| 7758 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
| 7759 | reg->type = PTR_TO_TCP_SOCK; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7760 | } else if (reg->type == PTR_TO_BTF_ID_OR_NULL) { |
| 7761 | reg->type = PTR_TO_BTF_ID; |
| 7762 | } else if (reg->type == PTR_TO_MEM_OR_NULL) { |
| 7763 | reg->type = PTR_TO_MEM; |
| 7764 | } else if (reg->type == PTR_TO_RDONLY_BUF_OR_NULL) { |
| 7765 | reg->type = PTR_TO_RDONLY_BUF; |
| 7766 | } else if (reg->type == PTR_TO_RDWR_BUF_OR_NULL) { |
| 7767 | reg->type = PTR_TO_RDWR_BUF; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7768 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7769 | if (is_null) { |
| 7770 | /* We don't need id and ref_obj_id from this point |
| 7771 | * onwards anymore, thus we should better reset it, |
| 7772 | * so that state pruning has chances to take effect. |
| 7773 | */ |
| 7774 | reg->id = 0; |
| 7775 | reg->ref_obj_id = 0; |
| 7776 | } else if (!reg_may_point_to_spin_lock(reg)) { |
| 7777 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
| 7778 | * in release_reg_references(). |
| 7779 | * |
| 7780 | * reg->id is still used by spin_lock ptr. Other |
| 7781 | * than spin_lock ptr type, reg->id can be reset. |
| 7782 | */ |
| 7783 | reg->id = 0; |
| 7784 | } |
| 7785 | } |
| 7786 | } |
| 7787 | |
| 7788 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
| 7789 | bool is_null) |
| 7790 | { |
| 7791 | struct bpf_reg_state *reg; |
| 7792 | int i; |
| 7793 | |
| 7794 | for (i = 0; i < MAX_BPF_REG; i++) |
| 7795 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); |
| 7796 | |
| 7797 | bpf_for_each_spilled_reg(i, state, reg) { |
| 7798 | if (!reg) |
| 7799 | continue; |
| 7800 | mark_ptr_or_null_reg(state, reg, id, is_null); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7801 | } |
| 7802 | } |
| 7803 | |
| 7804 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 7805 | * be folded together at some point. |
| 7806 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7807 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 7808 | bool is_null) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7809 | { |
| 7810 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 7811 | struct bpf_reg_state *regs = state->regs; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7812 | u32 ref_obj_id = regs[regno].ref_obj_id; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7813 | u32 id = regs[regno].id; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7814 | int i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7815 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7816 | if (ref_obj_id && ref_obj_id == id && is_null) |
| 7817 | /* regs[regno] is in the " == NULL" branch. |
| 7818 | * No one could have freed the reference state before |
| 7819 | * doing the NULL check. |
| 7820 | */ |
| 7821 | WARN_ON_ONCE(release_reference_state(state, id)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7822 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7823 | for (i = 0; i <= vstate->curframe; i++) |
| 7824 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7825 | } |
| 7826 | |
| 7827 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 7828 | struct bpf_reg_state *dst_reg, |
| 7829 | struct bpf_reg_state *src_reg, |
| 7830 | struct bpf_verifier_state *this_branch, |
| 7831 | struct bpf_verifier_state *other_branch) |
| 7832 | { |
| 7833 | if (BPF_SRC(insn->code) != BPF_X) |
| 7834 | return false; |
| 7835 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7836 | /* Pointers are always 64-bit. */ |
| 7837 | if (BPF_CLASS(insn->code) == BPF_JMP32) |
| 7838 | return false; |
| 7839 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7840 | switch (BPF_OP(insn->code)) { |
| 7841 | case BPF_JGT: |
| 7842 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7843 | src_reg->type == PTR_TO_PACKET_END) || |
| 7844 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7845 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7846 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 7847 | find_good_pkt_pointers(this_branch, dst_reg, |
| 7848 | dst_reg->type, false); |
| 7849 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7850 | src_reg->type == PTR_TO_PACKET) || |
| 7851 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7852 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7853 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 7854 | find_good_pkt_pointers(other_branch, src_reg, |
| 7855 | src_reg->type, true); |
| 7856 | } else { |
| 7857 | return false; |
| 7858 | } |
| 7859 | break; |
| 7860 | case BPF_JLT: |
| 7861 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7862 | src_reg->type == PTR_TO_PACKET_END) || |
| 7863 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7864 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7865 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 7866 | find_good_pkt_pointers(other_branch, dst_reg, |
| 7867 | dst_reg->type, true); |
| 7868 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7869 | src_reg->type == PTR_TO_PACKET) || |
| 7870 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7871 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7872 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 7873 | find_good_pkt_pointers(this_branch, src_reg, |
| 7874 | src_reg->type, false); |
| 7875 | } else { |
| 7876 | return false; |
| 7877 | } |
| 7878 | break; |
| 7879 | case BPF_JGE: |
| 7880 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7881 | src_reg->type == PTR_TO_PACKET_END) || |
| 7882 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7883 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7884 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 7885 | find_good_pkt_pointers(this_branch, dst_reg, |
| 7886 | dst_reg->type, true); |
| 7887 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7888 | src_reg->type == PTR_TO_PACKET) || |
| 7889 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7890 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7891 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 7892 | find_good_pkt_pointers(other_branch, src_reg, |
| 7893 | src_reg->type, false); |
| 7894 | } else { |
| 7895 | return false; |
| 7896 | } |
| 7897 | break; |
| 7898 | case BPF_JLE: |
| 7899 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7900 | src_reg->type == PTR_TO_PACKET_END) || |
| 7901 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7902 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7903 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 7904 | find_good_pkt_pointers(other_branch, dst_reg, |
| 7905 | dst_reg->type, false); |
| 7906 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7907 | src_reg->type == PTR_TO_PACKET) || |
| 7908 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7909 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7910 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 7911 | find_good_pkt_pointers(this_branch, src_reg, |
| 7912 | src_reg->type, true); |
| 7913 | } else { |
| 7914 | return false; |
| 7915 | } |
| 7916 | break; |
| 7917 | default: |
| 7918 | return false; |
| 7919 | } |
| 7920 | |
| 7921 | return true; |
| 7922 | } |
| 7923 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7924 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
| 7925 | struct bpf_reg_state *known_reg) |
| 7926 | { |
| 7927 | struct bpf_func_state *state; |
| 7928 | struct bpf_reg_state *reg; |
| 7929 | int i, j; |
| 7930 | |
| 7931 | for (i = 0; i <= vstate->curframe; i++) { |
| 7932 | state = vstate->frame[i]; |
| 7933 | for (j = 0; j < MAX_BPF_REG; j++) { |
| 7934 | reg = &state->regs[j]; |
| 7935 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) |
| 7936 | *reg = *known_reg; |
| 7937 | } |
| 7938 | |
| 7939 | bpf_for_each_spilled_reg(j, state, reg) { |
| 7940 | if (!reg) |
| 7941 | continue; |
| 7942 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) |
| 7943 | *reg = *known_reg; |
| 7944 | } |
| 7945 | } |
| 7946 | } |
| 7947 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7948 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
| 7949 | struct bpf_insn *insn, int *insn_idx) |
| 7950 | { |
| 7951 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 7952 | struct bpf_verifier_state *other_branch; |
| 7953 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7954 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7955 | u8 opcode = BPF_OP(insn->code); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7956 | bool is_jmp32; |
| 7957 | int pred = -1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7958 | int err; |
| 7959 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7960 | /* Only conditional jumps are expected to reach here. */ |
| 7961 | if (opcode == BPF_JA || opcode > BPF_JSLE) { |
| 7962 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7963 | return -EINVAL; |
| 7964 | } |
| 7965 | |
| 7966 | if (BPF_SRC(insn->code) == BPF_X) { |
| 7967 | if (insn->imm != 0) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7968 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7969 | return -EINVAL; |
| 7970 | } |
| 7971 | |
| 7972 | /* check src1 operand */ |
| 7973 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 7974 | if (err) |
| 7975 | return err; |
| 7976 | |
| 7977 | if (is_pointer_value(env, insn->src_reg)) { |
| 7978 | verbose(env, "R%d pointer comparison prohibited\n", |
| 7979 | insn->src_reg); |
| 7980 | return -EACCES; |
| 7981 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7982 | src_reg = ®s[insn->src_reg]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7983 | } else { |
| 7984 | if (insn->src_reg != BPF_REG_0) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7985 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7986 | return -EINVAL; |
| 7987 | } |
| 7988 | } |
| 7989 | |
| 7990 | /* check src2 operand */ |
| 7991 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
| 7992 | if (err) |
| 7993 | return err; |
| 7994 | |
| 7995 | dst_reg = ®s[insn->dst_reg]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7996 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7997 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 7998 | if (BPF_SRC(insn->code) == BPF_K) { |
| 7999 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); |
| 8000 | } else if (src_reg->type == SCALAR_VALUE && |
| 8001 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { |
| 8002 | pred = is_branch_taken(dst_reg, |
| 8003 | tnum_subreg(src_reg->var_off).value, |
| 8004 | opcode, |
| 8005 | is_jmp32); |
| 8006 | } else if (src_reg->type == SCALAR_VALUE && |
| 8007 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { |
| 8008 | pred = is_branch_taken(dst_reg, |
| 8009 | src_reg->var_off.value, |
| 8010 | opcode, |
| 8011 | is_jmp32); |
| 8012 | } |
| 8013 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8014 | if (pred >= 0) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8015 | /* If we get here with a dst_reg pointer type it is because |
| 8016 | * above is_branch_taken() special cased the 0 comparison. |
| 8017 | */ |
| 8018 | if (!__is_pointer_value(false, dst_reg)) |
| 8019 | err = mark_chain_precision(env, insn->dst_reg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8020 | if (BPF_SRC(insn->code) == BPF_X && !err) |
| 8021 | err = mark_chain_precision(env, insn->src_reg); |
| 8022 | if (err) |
| 8023 | return err; |
| 8024 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8025 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8026 | if (pred == 1) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8027 | /* Only follow the goto, ignore fall-through. If needed, push |
| 8028 | * the fall-through branch for simulation under speculative |
| 8029 | * execution. |
| 8030 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8031 | if (!env->bypass_spec_v1 && |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8032 | !sanitize_speculative_path(env, insn, *insn_idx + 1, |
| 8033 | *insn_idx)) |
| 8034 | return -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8035 | *insn_idx += insn->off; |
| 8036 | return 0; |
| 8037 | } else if (pred == 0) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8038 | /* Only follow the fall-through branch, since that's where the |
| 8039 | * program will go. If needed, push the goto branch for |
| 8040 | * simulation under speculative execution. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8041 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8042 | if (!env->bypass_spec_v1 && |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8043 | !sanitize_speculative_path(env, insn, |
| 8044 | *insn_idx + insn->off + 1, |
| 8045 | *insn_idx)) |
| 8046 | return -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8047 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8048 | } |
| 8049 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8050 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
| 8051 | false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8052 | if (!other_branch) |
| 8053 | return -EFAULT; |
| 8054 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
| 8055 | |
| 8056 | /* detect if we are comparing against a constant value so we can adjust |
| 8057 | * our min/max values for our dst register. |
| 8058 | * this is only legit if both are scalars (or pointers to the same |
| 8059 | * object, I suppose, but we don't support that right now), because |
| 8060 | * otherwise the different base pointers mean the offsets aren't |
| 8061 | * comparable. |
| 8062 | */ |
| 8063 | if (BPF_SRC(insn->code) == BPF_X) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8064 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8065 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8066 | if (dst_reg->type == SCALAR_VALUE && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8067 | src_reg->type == SCALAR_VALUE) { |
| 8068 | if (tnum_is_const(src_reg->var_off) || |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8069 | (is_jmp32 && |
| 8070 | tnum_is_const(tnum_subreg(src_reg->var_off)))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8071 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8072 | dst_reg, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8073 | src_reg->var_off.value, |
| 8074 | tnum_subreg(src_reg->var_off).value, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8075 | opcode, is_jmp32); |
| 8076 | else if (tnum_is_const(dst_reg->var_off) || |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8077 | (is_jmp32 && |
| 8078 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8079 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8080 | src_reg, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8081 | dst_reg->var_off.value, |
| 8082 | tnum_subreg(dst_reg->var_off).value, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8083 | opcode, is_jmp32); |
| 8084 | else if (!is_jmp32 && |
| 8085 | (opcode == BPF_JEQ || opcode == BPF_JNE)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8086 | /* Comparing for equality, we can combine knowledge */ |
| 8087 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 8088 | &other_branch_regs[insn->dst_reg], |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8089 | src_reg, dst_reg, opcode); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8090 | if (src_reg->id && |
| 8091 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { |
| 8092 | find_equal_scalars(this_branch, src_reg); |
| 8093 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); |
| 8094 | } |
| 8095 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8096 | } |
| 8097 | } else if (dst_reg->type == SCALAR_VALUE) { |
| 8098 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8099 | dst_reg, insn->imm, (u32)insn->imm, |
| 8100 | opcode, is_jmp32); |
| 8101 | } |
| 8102 | |
| 8103 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
| 8104 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { |
| 8105 | find_equal_scalars(this_branch, dst_reg); |
| 8106 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8107 | } |
| 8108 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8109 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
| 8110 | * NOTE: these optimizations below are related with pointer comparison |
| 8111 | * which will never be JMP32. |
| 8112 | */ |
| 8113 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8114 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8115 | reg_type_may_be_null(dst_reg->type)) { |
| 8116 | /* Mark all identical registers in each branch as either |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8117 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 8118 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8119 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 8120 | opcode == BPF_JNE); |
| 8121 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 8122 | opcode == BPF_JEQ); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8123 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 8124 | this_branch, other_branch) && |
| 8125 | is_pointer_value(env, insn->dst_reg)) { |
| 8126 | verbose(env, "R%d pointer comparison prohibited\n", |
| 8127 | insn->dst_reg); |
| 8128 | return -EACCES; |
| 8129 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8130 | if (env->log.level & BPF_LOG_LEVEL) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8131 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
| 8132 | return 0; |
| 8133 | } |
| 8134 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8135 | /* verify BPF_LD_IMM64 instruction */ |
| 8136 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 8137 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8138 | struct bpf_insn_aux_data *aux = cur_aux(env); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8139 | struct bpf_reg_state *regs = cur_regs(env); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8140 | struct bpf_reg_state *dst_reg; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8141 | struct bpf_map *map; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8142 | int err; |
| 8143 | |
| 8144 | if (BPF_SIZE(insn->code) != BPF_DW) { |
| 8145 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
| 8146 | return -EINVAL; |
| 8147 | } |
| 8148 | if (insn->off != 0) { |
| 8149 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
| 8150 | return -EINVAL; |
| 8151 | } |
| 8152 | |
| 8153 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
| 8154 | if (err) |
| 8155 | return err; |
| 8156 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8157 | dst_reg = ®s[insn->dst_reg]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8158 | if (insn->src_reg == 0) { |
| 8159 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 8160 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8161 | dst_reg->type = SCALAR_VALUE; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8162 | __mark_reg_known(®s[insn->dst_reg], imm); |
| 8163 | return 0; |
| 8164 | } |
| 8165 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8166 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
| 8167 | mark_reg_known_zero(env, regs, insn->dst_reg); |
| 8168 | |
| 8169 | dst_reg->type = aux->btf_var.reg_type; |
| 8170 | switch (dst_reg->type) { |
| 8171 | case PTR_TO_MEM: |
| 8172 | dst_reg->mem_size = aux->btf_var.mem_size; |
| 8173 | break; |
| 8174 | case PTR_TO_BTF_ID: |
| 8175 | case PTR_TO_PERCPU_BTF_ID: |
| 8176 | dst_reg->btf_id = aux->btf_var.btf_id; |
| 8177 | break; |
| 8178 | default: |
| 8179 | verbose(env, "bpf verifier is misconfigured\n"); |
| 8180 | return -EFAULT; |
| 8181 | } |
| 8182 | return 0; |
| 8183 | } |
| 8184 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8185 | map = env->used_maps[aux->map_index]; |
| 8186 | mark_reg_known_zero(env, regs, insn->dst_reg); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8187 | dst_reg->map_ptr = map; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8188 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8189 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8190 | dst_reg->type = PTR_TO_MAP_VALUE; |
| 8191 | dst_reg->off = aux->map_off; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8192 | if (map_value_has_spin_lock(map)) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8193 | dst_reg->id = ++env->id_gen; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8194 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8195 | dst_reg->type = CONST_PTR_TO_MAP; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8196 | } else { |
| 8197 | verbose(env, "bpf verifier is misconfigured\n"); |
| 8198 | return -EINVAL; |
| 8199 | } |
| 8200 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8201 | return 0; |
| 8202 | } |
| 8203 | |
| 8204 | static bool may_access_skb(enum bpf_prog_type type) |
| 8205 | { |
| 8206 | switch (type) { |
| 8207 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 8208 | case BPF_PROG_TYPE_SCHED_CLS: |
| 8209 | case BPF_PROG_TYPE_SCHED_ACT: |
| 8210 | return true; |
| 8211 | default: |
| 8212 | return false; |
| 8213 | } |
| 8214 | } |
| 8215 | |
| 8216 | /* verify safety of LD_ABS|LD_IND instructions: |
| 8217 | * - they can only appear in the programs where ctx == skb |
| 8218 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 8219 | * preserve R6-R9, and store return value into R0 |
| 8220 | * |
| 8221 | * Implicit input: |
| 8222 | * ctx == skb == R6 == CTX |
| 8223 | * |
| 8224 | * Explicit input: |
| 8225 | * SRC == any register |
| 8226 | * IMM == 32-bit immediate |
| 8227 | * |
| 8228 | * Output: |
| 8229 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 8230 | */ |
| 8231 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 8232 | { |
| 8233 | struct bpf_reg_state *regs = cur_regs(env); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8234 | static const int ctx_reg = BPF_REG_6; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8235 | u8 mode = BPF_MODE(insn->code); |
| 8236 | int i, err; |
| 8237 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8238 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8239 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
| 8240 | return -EINVAL; |
| 8241 | } |
| 8242 | |
| 8243 | if (!env->ops->gen_ld_abs) { |
| 8244 | verbose(env, "bpf verifier is misconfigured\n"); |
| 8245 | return -EINVAL; |
| 8246 | } |
| 8247 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8248 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
| 8249 | BPF_SIZE(insn->code) == BPF_DW || |
| 8250 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
| 8251 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
| 8252 | return -EINVAL; |
| 8253 | } |
| 8254 | |
| 8255 | /* check whether implicit source operand (register R6) is readable */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8256 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8257 | if (err) |
| 8258 | return err; |
| 8259 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8260 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 8261 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 8262 | * reference leak. |
| 8263 | */ |
| 8264 | err = check_reference_leak(env); |
| 8265 | if (err) { |
| 8266 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 8267 | return err; |
| 8268 | } |
| 8269 | |
| 8270 | if (env->cur_state->active_spin_lock) { |
| 8271 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
| 8272 | return -EINVAL; |
| 8273 | } |
| 8274 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8275 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8276 | verbose(env, |
| 8277 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); |
| 8278 | return -EINVAL; |
| 8279 | } |
| 8280 | |
| 8281 | if (mode == BPF_IND) { |
| 8282 | /* check explicit source operand */ |
| 8283 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 8284 | if (err) |
| 8285 | return err; |
| 8286 | } |
| 8287 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 8288 | err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg); |
| 8289 | if (err < 0) |
| 8290 | return err; |
| 8291 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8292 | /* reset caller saved regs to unreadable */ |
| 8293 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 8294 | mark_reg_not_init(env, regs, caller_saved[i]); |
| 8295 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 8296 | } |
| 8297 | |
| 8298 | /* mark destination R0 register as readable, since it contains |
| 8299 | * the value fetched from the packet. |
| 8300 | * Already marked as written above. |
| 8301 | */ |
| 8302 | mark_reg_unknown(env, regs, BPF_REG_0); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8303 | /* ld_abs load up to 32-bit skb data. */ |
| 8304 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8305 | return 0; |
| 8306 | } |
| 8307 | |
| 8308 | static int check_return_code(struct bpf_verifier_env *env) |
| 8309 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8310 | struct tnum enforce_attach_type_range = tnum_unknown; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8311 | const struct bpf_prog *prog = env->prog; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8312 | struct bpf_reg_state *reg; |
| 8313 | struct tnum range = tnum_range(0, 1); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8314 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
| 8315 | int err; |
| 8316 | const bool is_subprog = env->cur_state->frame[0]->subprogno; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8317 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8318 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
| 8319 | if (!is_subprog && |
| 8320 | (prog_type == BPF_PROG_TYPE_STRUCT_OPS || |
| 8321 | prog_type == BPF_PROG_TYPE_LSM) && |
| 8322 | !prog->aux->attach_func_proto->type) |
| 8323 | return 0; |
| 8324 | |
| 8325 | /* eBPF calling convetion is such that R0 is used |
| 8326 | * to return the value from eBPF program. |
| 8327 | * Make sure that it's readable at this time |
| 8328 | * of bpf_exit, which means that program wrote |
| 8329 | * something into it earlier |
| 8330 | */ |
| 8331 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
| 8332 | if (err) |
| 8333 | return err; |
| 8334 | |
| 8335 | if (is_pointer_value(env, BPF_REG_0)) { |
| 8336 | verbose(env, "R0 leaks addr as return value\n"); |
| 8337 | return -EACCES; |
| 8338 | } |
| 8339 | |
| 8340 | reg = cur_regs(env) + BPF_REG_0; |
| 8341 | if (is_subprog) { |
| 8342 | if (reg->type != SCALAR_VALUE) { |
| 8343 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", |
| 8344 | reg_type_str[reg->type]); |
| 8345 | return -EINVAL; |
| 8346 | } |
| 8347 | return 0; |
| 8348 | } |
| 8349 | |
| 8350 | switch (prog_type) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8351 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8352 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8353 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
| 8354 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || |
| 8355 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || |
| 8356 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || |
| 8357 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8358 | range = tnum_range(1, 1); |
| 8359 | break; |
| 8360 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 8361 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
| 8362 | range = tnum_range(0, 3); |
| 8363 | enforce_attach_type_range = tnum_range(2, 3); |
| 8364 | } |
| 8365 | break; |
| 8366 | case BPF_PROG_TYPE_CGROUP_SOCK: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8367 | case BPF_PROG_TYPE_SOCK_OPS: |
| 8368 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8369 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
| 8370 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8371 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8372 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 8373 | if (!env->prog->aux->attach_btf_id) |
| 8374 | return 0; |
| 8375 | range = tnum_const(0); |
| 8376 | break; |
| 8377 | case BPF_PROG_TYPE_TRACING: |
| 8378 | switch (env->prog->expected_attach_type) { |
| 8379 | case BPF_TRACE_FENTRY: |
| 8380 | case BPF_TRACE_FEXIT: |
| 8381 | range = tnum_const(0); |
| 8382 | break; |
| 8383 | case BPF_TRACE_RAW_TP: |
| 8384 | case BPF_MODIFY_RETURN: |
| 8385 | return 0; |
| 8386 | case BPF_TRACE_ITER: |
| 8387 | break; |
| 8388 | default: |
| 8389 | return -ENOTSUPP; |
| 8390 | } |
| 8391 | break; |
| 8392 | case BPF_PROG_TYPE_SK_LOOKUP: |
| 8393 | range = tnum_range(SK_DROP, SK_PASS); |
| 8394 | break; |
| 8395 | case BPF_PROG_TYPE_EXT: |
| 8396 | /* freplace program can return anything as its return value |
| 8397 | * depends on the to-be-replaced kernel func or bpf program. |
| 8398 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8399 | default: |
| 8400 | return 0; |
| 8401 | } |
| 8402 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8403 | if (reg->type != SCALAR_VALUE) { |
| 8404 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
| 8405 | reg_type_str[reg->type]); |
| 8406 | return -EINVAL; |
| 8407 | } |
| 8408 | |
| 8409 | if (!tnum_in(range, reg->var_off)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8410 | char tn_buf[48]; |
| 8411 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8412 | verbose(env, "At program exit the register R0 "); |
| 8413 | if (!tnum_is_unknown(reg->var_off)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8414 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 8415 | verbose(env, "has value %s", tn_buf); |
| 8416 | } else { |
| 8417 | verbose(env, "has unknown scalar value"); |
| 8418 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8419 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
| 8420 | verbose(env, " should have been in %s\n", tn_buf); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8421 | return -EINVAL; |
| 8422 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8423 | |
| 8424 | if (!tnum_is_unknown(enforce_attach_type_range) && |
| 8425 | tnum_in(enforce_attach_type_range, reg->var_off)) |
| 8426 | env->prog->enforce_expected_attach_type = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8427 | return 0; |
| 8428 | } |
| 8429 | |
| 8430 | /* non-recursive DFS pseudo code |
| 8431 | * 1 procedure DFS-iterative(G,v): |
| 8432 | * 2 label v as discovered |
| 8433 | * 3 let S be a stack |
| 8434 | * 4 S.push(v) |
| 8435 | * 5 while S is not empty |
| 8436 | * 6 t <- S.pop() |
| 8437 | * 7 if t is what we're looking for: |
| 8438 | * 8 return t |
| 8439 | * 9 for all edges e in G.adjacentEdges(t) do |
| 8440 | * 10 if edge e is already labelled |
| 8441 | * 11 continue with the next edge |
| 8442 | * 12 w <- G.adjacentVertex(t,e) |
| 8443 | * 13 if vertex w is not discovered and not explored |
| 8444 | * 14 label e as tree-edge |
| 8445 | * 15 label w as discovered |
| 8446 | * 16 S.push(w) |
| 8447 | * 17 continue at 5 |
| 8448 | * 18 else if vertex w is discovered |
| 8449 | * 19 label e as back-edge |
| 8450 | * 20 else |
| 8451 | * 21 // vertex w is explored |
| 8452 | * 22 label e as forward- or cross-edge |
| 8453 | * 23 label t as explored |
| 8454 | * 24 S.pop() |
| 8455 | * |
| 8456 | * convention: |
| 8457 | * 0x10 - discovered |
| 8458 | * 0x11 - discovered and fall-through edge labelled |
| 8459 | * 0x12 - discovered and fall-through and branch edges labelled |
| 8460 | * 0x20 - explored |
| 8461 | */ |
| 8462 | |
| 8463 | enum { |
| 8464 | DISCOVERED = 0x10, |
| 8465 | EXPLORED = 0x20, |
| 8466 | FALLTHROUGH = 1, |
| 8467 | BRANCH = 2, |
| 8468 | }; |
| 8469 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8470 | static u32 state_htab_size(struct bpf_verifier_env *env) |
| 8471 | { |
| 8472 | return env->prog->len; |
| 8473 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8474 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8475 | static struct bpf_verifier_state_list **explored_state( |
| 8476 | struct bpf_verifier_env *env, |
| 8477 | int idx) |
| 8478 | { |
| 8479 | struct bpf_verifier_state *cur = env->cur_state; |
| 8480 | struct bpf_func_state *state = cur->frame[cur->curframe]; |
| 8481 | |
| 8482 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; |
| 8483 | } |
| 8484 | |
| 8485 | static void init_explored_state(struct bpf_verifier_env *env, int idx) |
| 8486 | { |
| 8487 | env->insn_aux_data[idx].prune_point = true; |
| 8488 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8489 | |
| 8490 | /* t, w, e - match pseudo-code above: |
| 8491 | * t - index of current instruction |
| 8492 | * w - next instruction |
| 8493 | * e - edge |
| 8494 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8495 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
| 8496 | bool loop_ok) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8497 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8498 | int *insn_stack = env->cfg.insn_stack; |
| 8499 | int *insn_state = env->cfg.insn_state; |
| 8500 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8501 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 8502 | return 0; |
| 8503 | |
| 8504 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 8505 | return 0; |
| 8506 | |
| 8507 | if (w < 0 || w >= env->prog->len) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8508 | verbose_linfo(env, t, "%d: ", t); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8509 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
| 8510 | return -EINVAL; |
| 8511 | } |
| 8512 | |
| 8513 | if (e == BRANCH) |
| 8514 | /* mark branch target for state pruning */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8515 | init_explored_state(env, w); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8516 | |
| 8517 | if (insn_state[w] == 0) { |
| 8518 | /* tree-edge */ |
| 8519 | insn_state[t] = DISCOVERED | e; |
| 8520 | insn_state[w] = DISCOVERED; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8521 | if (env->cfg.cur_stack >= env->prog->len) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8522 | return -E2BIG; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8523 | insn_stack[env->cfg.cur_stack++] = w; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8524 | return 1; |
| 8525 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8526 | if (loop_ok && env->bpf_capable) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8527 | return 0; |
| 8528 | verbose_linfo(env, t, "%d: ", t); |
| 8529 | verbose_linfo(env, w, "%d: ", w); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8530 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
| 8531 | return -EINVAL; |
| 8532 | } else if (insn_state[w] == EXPLORED) { |
| 8533 | /* forward- or cross-edge */ |
| 8534 | insn_state[t] = DISCOVERED | e; |
| 8535 | } else { |
| 8536 | verbose(env, "insn state internal bug\n"); |
| 8537 | return -EFAULT; |
| 8538 | } |
| 8539 | return 0; |
| 8540 | } |
| 8541 | |
| 8542 | /* non-recursive depth-first-search to detect loops in BPF program |
| 8543 | * loop == back-edge in directed graph |
| 8544 | */ |
| 8545 | static int check_cfg(struct bpf_verifier_env *env) |
| 8546 | { |
| 8547 | struct bpf_insn *insns = env->prog->insnsi; |
| 8548 | int insn_cnt = env->prog->len; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8549 | int *insn_stack, *insn_state; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8550 | int ret = 0; |
| 8551 | int i, t; |
| 8552 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8553 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8554 | if (!insn_state) |
| 8555 | return -ENOMEM; |
| 8556 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8557 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8558 | if (!insn_stack) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8559 | kvfree(insn_state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8560 | return -ENOMEM; |
| 8561 | } |
| 8562 | |
| 8563 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 8564 | insn_stack[0] = 0; /* 0 is the first instruction */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8565 | env->cfg.cur_stack = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8566 | |
| 8567 | peek_stack: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8568 | if (env->cfg.cur_stack == 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8569 | goto check_state; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8570 | t = insn_stack[env->cfg.cur_stack - 1]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8571 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8572 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
| 8573 | BPF_CLASS(insns[t].code) == BPF_JMP32) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8574 | u8 opcode = BPF_OP(insns[t].code); |
| 8575 | |
| 8576 | if (opcode == BPF_EXIT) { |
| 8577 | goto mark_explored; |
| 8578 | } else if (opcode == BPF_CALL) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8579 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8580 | if (ret == 1) |
| 8581 | goto peek_stack; |
| 8582 | else if (ret < 0) |
| 8583 | goto err_free; |
| 8584 | if (t + 1 < insn_cnt) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8585 | init_explored_state(env, t + 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8586 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8587 | init_explored_state(env, t); |
| 8588 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, |
| 8589 | env, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8590 | if (ret == 1) |
| 8591 | goto peek_stack; |
| 8592 | else if (ret < 0) |
| 8593 | goto err_free; |
| 8594 | } |
| 8595 | } else if (opcode == BPF_JA) { |
| 8596 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 8597 | ret = -EINVAL; |
| 8598 | goto err_free; |
| 8599 | } |
| 8600 | /* unconditional jump with single edge */ |
| 8601 | ret = push_insn(t, t + insns[t].off + 1, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8602 | FALLTHROUGH, env, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8603 | if (ret == 1) |
| 8604 | goto peek_stack; |
| 8605 | else if (ret < 0) |
| 8606 | goto err_free; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8607 | /* unconditional jmp is not a good pruning point, |
| 8608 | * but it's marked, since backtracking needs |
| 8609 | * to record jmp history in is_state_visited(). |
| 8610 | */ |
| 8611 | init_explored_state(env, t + insns[t].off + 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8612 | /* tell verifier to check for equivalent states |
| 8613 | * after every call and jump |
| 8614 | */ |
| 8615 | if (t + 1 < insn_cnt) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8616 | init_explored_state(env, t + 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8617 | } else { |
| 8618 | /* conditional jump with two edges */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8619 | init_explored_state(env, t); |
| 8620 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8621 | if (ret == 1) |
| 8622 | goto peek_stack; |
| 8623 | else if (ret < 0) |
| 8624 | goto err_free; |
| 8625 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8626 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8627 | if (ret == 1) |
| 8628 | goto peek_stack; |
| 8629 | else if (ret < 0) |
| 8630 | goto err_free; |
| 8631 | } |
| 8632 | } else { |
| 8633 | /* all other non-branch instructions with single |
| 8634 | * fall-through edge |
| 8635 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8636 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8637 | if (ret == 1) |
| 8638 | goto peek_stack; |
| 8639 | else if (ret < 0) |
| 8640 | goto err_free; |
| 8641 | } |
| 8642 | |
| 8643 | mark_explored: |
| 8644 | insn_state[t] = EXPLORED; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8645 | if (env->cfg.cur_stack-- <= 0) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8646 | verbose(env, "pop stack internal bug\n"); |
| 8647 | ret = -EFAULT; |
| 8648 | goto err_free; |
| 8649 | } |
| 8650 | goto peek_stack; |
| 8651 | |
| 8652 | check_state: |
| 8653 | for (i = 0; i < insn_cnt; i++) { |
| 8654 | if (insn_state[i] != EXPLORED) { |
| 8655 | verbose(env, "unreachable insn %d\n", i); |
| 8656 | ret = -EINVAL; |
| 8657 | goto err_free; |
| 8658 | } |
| 8659 | } |
| 8660 | ret = 0; /* cfg looks good */ |
| 8661 | |
| 8662 | err_free: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8663 | kvfree(insn_state); |
| 8664 | kvfree(insn_stack); |
| 8665 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8666 | return ret; |
| 8667 | } |
| 8668 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8669 | static int check_abnormal_return(struct bpf_verifier_env *env) |
| 8670 | { |
| 8671 | int i; |
| 8672 | |
| 8673 | for (i = 1; i < env->subprog_cnt; i++) { |
| 8674 | if (env->subprog_info[i].has_ld_abs) { |
| 8675 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); |
| 8676 | return -EINVAL; |
| 8677 | } |
| 8678 | if (env->subprog_info[i].has_tail_call) { |
| 8679 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); |
| 8680 | return -EINVAL; |
| 8681 | } |
| 8682 | } |
| 8683 | return 0; |
| 8684 | } |
| 8685 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8686 | /* The minimum supported BTF func info size */ |
| 8687 | #define MIN_BPF_FUNCINFO_SIZE 8 |
| 8688 | #define MAX_FUNCINFO_REC_SIZE 252 |
| 8689 | |
| 8690 | static int check_btf_func(struct bpf_verifier_env *env, |
| 8691 | const union bpf_attr *attr, |
| 8692 | union bpf_attr __user *uattr) |
| 8693 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8694 | const struct btf_type *type, *func_proto, *ret_type; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8695 | u32 i, nfuncs, urec_size, min_size; |
| 8696 | u32 krec_size = sizeof(struct bpf_func_info); |
| 8697 | struct bpf_func_info *krecord; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8698 | struct bpf_func_info_aux *info_aux = NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8699 | struct bpf_prog *prog; |
| 8700 | const struct btf *btf; |
| 8701 | void __user *urecord; |
| 8702 | u32 prev_offset = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8703 | bool scalar_return; |
| 8704 | int ret = -ENOMEM; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8705 | |
| 8706 | nfuncs = attr->func_info_cnt; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8707 | if (!nfuncs) { |
| 8708 | if (check_abnormal_return(env)) |
| 8709 | return -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8710 | return 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8711 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8712 | |
| 8713 | if (nfuncs != env->subprog_cnt) { |
| 8714 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); |
| 8715 | return -EINVAL; |
| 8716 | } |
| 8717 | |
| 8718 | urec_size = attr->func_info_rec_size; |
| 8719 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || |
| 8720 | urec_size > MAX_FUNCINFO_REC_SIZE || |
| 8721 | urec_size % sizeof(u32)) { |
| 8722 | verbose(env, "invalid func info rec size %u\n", urec_size); |
| 8723 | return -EINVAL; |
| 8724 | } |
| 8725 | |
| 8726 | prog = env->prog; |
| 8727 | btf = prog->aux->btf; |
| 8728 | |
| 8729 | urecord = u64_to_user_ptr(attr->func_info); |
| 8730 | min_size = min_t(u32, krec_size, urec_size); |
| 8731 | |
| 8732 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
| 8733 | if (!krecord) |
| 8734 | return -ENOMEM; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8735 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
| 8736 | if (!info_aux) |
| 8737 | goto err_free; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8738 | |
| 8739 | for (i = 0; i < nfuncs; i++) { |
| 8740 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); |
| 8741 | if (ret) { |
| 8742 | if (ret == -E2BIG) { |
| 8743 | verbose(env, "nonzero tailing record in func info"); |
| 8744 | /* set the size kernel expects so loader can zero |
| 8745 | * out the rest of the record. |
| 8746 | */ |
| 8747 | if (put_user(min_size, &uattr->func_info_rec_size)) |
| 8748 | ret = -EFAULT; |
| 8749 | } |
| 8750 | goto err_free; |
| 8751 | } |
| 8752 | |
| 8753 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
| 8754 | ret = -EFAULT; |
| 8755 | goto err_free; |
| 8756 | } |
| 8757 | |
| 8758 | /* check insn_off */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8759 | ret = -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8760 | if (i == 0) { |
| 8761 | if (krecord[i].insn_off) { |
| 8762 | verbose(env, |
| 8763 | "nonzero insn_off %u for the first func info record", |
| 8764 | krecord[i].insn_off); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8765 | goto err_free; |
| 8766 | } |
| 8767 | } else if (krecord[i].insn_off <= prev_offset) { |
| 8768 | verbose(env, |
| 8769 | "same or smaller insn offset (%u) than previous func info record (%u)", |
| 8770 | krecord[i].insn_off, prev_offset); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8771 | goto err_free; |
| 8772 | } |
| 8773 | |
| 8774 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
| 8775 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8776 | goto err_free; |
| 8777 | } |
| 8778 | |
| 8779 | /* check type_id */ |
| 8780 | type = btf_type_by_id(btf, krecord[i].type_id); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8781 | if (!type || !btf_type_is_func(type)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8782 | verbose(env, "invalid type id %d in func info", |
| 8783 | krecord[i].type_id); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8784 | goto err_free; |
| 8785 | } |
| 8786 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
| 8787 | |
| 8788 | func_proto = btf_type_by_id(btf, type->type); |
| 8789 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) |
| 8790 | /* btf_func_check() already verified it during BTF load */ |
| 8791 | goto err_free; |
| 8792 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); |
| 8793 | scalar_return = |
| 8794 | btf_type_is_small_int(ret_type) || btf_type_is_enum(ret_type); |
| 8795 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { |
| 8796 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); |
| 8797 | goto err_free; |
| 8798 | } |
| 8799 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { |
| 8800 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8801 | goto err_free; |
| 8802 | } |
| 8803 | |
| 8804 | prev_offset = krecord[i].insn_off; |
| 8805 | urecord += urec_size; |
| 8806 | } |
| 8807 | |
| 8808 | prog->aux->func_info = krecord; |
| 8809 | prog->aux->func_info_cnt = nfuncs; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8810 | prog->aux->func_info_aux = info_aux; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8811 | return 0; |
| 8812 | |
| 8813 | err_free: |
| 8814 | kvfree(krecord); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8815 | kfree(info_aux); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8816 | return ret; |
| 8817 | } |
| 8818 | |
| 8819 | static void adjust_btf_func(struct bpf_verifier_env *env) |
| 8820 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8821 | struct bpf_prog_aux *aux = env->prog->aux; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8822 | int i; |
| 8823 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8824 | if (!aux->func_info) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8825 | return; |
| 8826 | |
| 8827 | for (i = 0; i < env->subprog_cnt; i++) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8828 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8829 | } |
| 8830 | |
| 8831 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
| 8832 | sizeof(((struct bpf_line_info *)(0))->line_col)) |
| 8833 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
| 8834 | |
| 8835 | static int check_btf_line(struct bpf_verifier_env *env, |
| 8836 | const union bpf_attr *attr, |
| 8837 | union bpf_attr __user *uattr) |
| 8838 | { |
| 8839 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; |
| 8840 | struct bpf_subprog_info *sub; |
| 8841 | struct bpf_line_info *linfo; |
| 8842 | struct bpf_prog *prog; |
| 8843 | const struct btf *btf; |
| 8844 | void __user *ulinfo; |
| 8845 | int err; |
| 8846 | |
| 8847 | nr_linfo = attr->line_info_cnt; |
| 8848 | if (!nr_linfo) |
| 8849 | return 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8850 | if (nr_linfo > INT_MAX / sizeof(struct bpf_line_info)) |
| 8851 | return -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8852 | |
| 8853 | rec_size = attr->line_info_rec_size; |
| 8854 | if (rec_size < MIN_BPF_LINEINFO_SIZE || |
| 8855 | rec_size > MAX_LINEINFO_REC_SIZE || |
| 8856 | rec_size & (sizeof(u32) - 1)) |
| 8857 | return -EINVAL; |
| 8858 | |
| 8859 | /* Need to zero it in case the userspace may |
| 8860 | * pass in a smaller bpf_line_info object. |
| 8861 | */ |
| 8862 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), |
| 8863 | GFP_KERNEL | __GFP_NOWARN); |
| 8864 | if (!linfo) |
| 8865 | return -ENOMEM; |
| 8866 | |
| 8867 | prog = env->prog; |
| 8868 | btf = prog->aux->btf; |
| 8869 | |
| 8870 | s = 0; |
| 8871 | sub = env->subprog_info; |
| 8872 | ulinfo = u64_to_user_ptr(attr->line_info); |
| 8873 | expected_size = sizeof(struct bpf_line_info); |
| 8874 | ncopy = min_t(u32, expected_size, rec_size); |
| 8875 | for (i = 0; i < nr_linfo; i++) { |
| 8876 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); |
| 8877 | if (err) { |
| 8878 | if (err == -E2BIG) { |
| 8879 | verbose(env, "nonzero tailing record in line_info"); |
| 8880 | if (put_user(expected_size, |
| 8881 | &uattr->line_info_rec_size)) |
| 8882 | err = -EFAULT; |
| 8883 | } |
| 8884 | goto err_free; |
| 8885 | } |
| 8886 | |
| 8887 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { |
| 8888 | err = -EFAULT; |
| 8889 | goto err_free; |
| 8890 | } |
| 8891 | |
| 8892 | /* |
| 8893 | * Check insn_off to ensure |
| 8894 | * 1) strictly increasing AND |
| 8895 | * 2) bounded by prog->len |
| 8896 | * |
| 8897 | * The linfo[0].insn_off == 0 check logically falls into |
| 8898 | * the later "missing bpf_line_info for func..." case |
| 8899 | * because the first linfo[0].insn_off must be the |
| 8900 | * first sub also and the first sub must have |
| 8901 | * subprog_info[0].start == 0. |
| 8902 | */ |
| 8903 | if ((i && linfo[i].insn_off <= prev_offset) || |
| 8904 | linfo[i].insn_off >= prog->len) { |
| 8905 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", |
| 8906 | i, linfo[i].insn_off, prev_offset, |
| 8907 | prog->len); |
| 8908 | err = -EINVAL; |
| 8909 | goto err_free; |
| 8910 | } |
| 8911 | |
| 8912 | if (!prog->insnsi[linfo[i].insn_off].code) { |
| 8913 | verbose(env, |
| 8914 | "Invalid insn code at line_info[%u].insn_off\n", |
| 8915 | i); |
| 8916 | err = -EINVAL; |
| 8917 | goto err_free; |
| 8918 | } |
| 8919 | |
| 8920 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
| 8921 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { |
| 8922 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
| 8923 | err = -EINVAL; |
| 8924 | goto err_free; |
| 8925 | } |
| 8926 | |
| 8927 | if (s != env->subprog_cnt) { |
| 8928 | if (linfo[i].insn_off == sub[s].start) { |
| 8929 | sub[s].linfo_idx = i; |
| 8930 | s++; |
| 8931 | } else if (sub[s].start < linfo[i].insn_off) { |
| 8932 | verbose(env, "missing bpf_line_info for func#%u\n", s); |
| 8933 | err = -EINVAL; |
| 8934 | goto err_free; |
| 8935 | } |
| 8936 | } |
| 8937 | |
| 8938 | prev_offset = linfo[i].insn_off; |
| 8939 | ulinfo += rec_size; |
| 8940 | } |
| 8941 | |
| 8942 | if (s != env->subprog_cnt) { |
| 8943 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", |
| 8944 | env->subprog_cnt - s, s); |
| 8945 | err = -EINVAL; |
| 8946 | goto err_free; |
| 8947 | } |
| 8948 | |
| 8949 | prog->aux->linfo = linfo; |
| 8950 | prog->aux->nr_linfo = nr_linfo; |
| 8951 | |
| 8952 | return 0; |
| 8953 | |
| 8954 | err_free: |
| 8955 | kvfree(linfo); |
| 8956 | return err; |
| 8957 | } |
| 8958 | |
| 8959 | static int check_btf_info(struct bpf_verifier_env *env, |
| 8960 | const union bpf_attr *attr, |
| 8961 | union bpf_attr __user *uattr) |
| 8962 | { |
| 8963 | struct btf *btf; |
| 8964 | int err; |
| 8965 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8966 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
| 8967 | if (check_abnormal_return(env)) |
| 8968 | return -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8969 | return 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8970 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8971 | |
| 8972 | btf = btf_get_by_fd(attr->prog_btf_fd); |
| 8973 | if (IS_ERR(btf)) |
| 8974 | return PTR_ERR(btf); |
| 8975 | env->prog->aux->btf = btf; |
| 8976 | |
| 8977 | err = check_btf_func(env, attr, uattr); |
| 8978 | if (err) |
| 8979 | return err; |
| 8980 | |
| 8981 | err = check_btf_line(env, attr, uattr); |
| 8982 | if (err) |
| 8983 | return err; |
| 8984 | |
| 8985 | return 0; |
| 8986 | } |
| 8987 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8988 | /* check %cur's range satisfies %old's */ |
| 8989 | static bool range_within(struct bpf_reg_state *old, |
| 8990 | struct bpf_reg_state *cur) |
| 8991 | { |
| 8992 | return old->umin_value <= cur->umin_value && |
| 8993 | old->umax_value >= cur->umax_value && |
| 8994 | old->smin_value <= cur->smin_value && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8995 | old->smax_value >= cur->smax_value && |
| 8996 | old->u32_min_value <= cur->u32_min_value && |
| 8997 | old->u32_max_value >= cur->u32_max_value && |
| 8998 | old->s32_min_value <= cur->s32_min_value && |
| 8999 | old->s32_max_value >= cur->s32_max_value; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9000 | } |
| 9001 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9002 | /* If in the old state two registers had the same id, then they need to have |
| 9003 | * the same id in the new state as well. But that id could be different from |
| 9004 | * the old state, so we need to track the mapping from old to new ids. |
| 9005 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 9006 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 9007 | * regs with a different old id could still have new id 9, we don't care about |
| 9008 | * that. |
| 9009 | * So we look through our idmap to see if this old id has been seen before. If |
| 9010 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 9011 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9012 | static bool check_ids(u32 old_id, u32 cur_id, struct bpf_id_pair *idmap) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9013 | { |
| 9014 | unsigned int i; |
| 9015 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9016 | for (i = 0; i < BPF_ID_MAP_SIZE; i++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9017 | if (!idmap[i].old) { |
| 9018 | /* Reached an empty slot; haven't seen this id before */ |
| 9019 | idmap[i].old = old_id; |
| 9020 | idmap[i].cur = cur_id; |
| 9021 | return true; |
| 9022 | } |
| 9023 | if (idmap[i].old == old_id) |
| 9024 | return idmap[i].cur == cur_id; |
| 9025 | } |
| 9026 | /* We ran out of idmap slots, which should be impossible */ |
| 9027 | WARN_ON_ONCE(1); |
| 9028 | return false; |
| 9029 | } |
| 9030 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9031 | static void clean_func_state(struct bpf_verifier_env *env, |
| 9032 | struct bpf_func_state *st) |
| 9033 | { |
| 9034 | enum bpf_reg_liveness live; |
| 9035 | int i, j; |
| 9036 | |
| 9037 | for (i = 0; i < BPF_REG_FP; i++) { |
| 9038 | live = st->regs[i].live; |
| 9039 | /* liveness must not touch this register anymore */ |
| 9040 | st->regs[i].live |= REG_LIVE_DONE; |
| 9041 | if (!(live & REG_LIVE_READ)) |
| 9042 | /* since the register is unused, clear its state |
| 9043 | * to make further comparison simpler |
| 9044 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9045 | __mark_reg_not_init(env, &st->regs[i]); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9046 | } |
| 9047 | |
| 9048 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { |
| 9049 | live = st->stack[i].spilled_ptr.live; |
| 9050 | /* liveness must not touch this stack slot anymore */ |
| 9051 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; |
| 9052 | if (!(live & REG_LIVE_READ)) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9053 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9054 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 9055 | st->stack[i].slot_type[j] = STACK_INVALID; |
| 9056 | } |
| 9057 | } |
| 9058 | } |
| 9059 | |
| 9060 | static void clean_verifier_state(struct bpf_verifier_env *env, |
| 9061 | struct bpf_verifier_state *st) |
| 9062 | { |
| 9063 | int i; |
| 9064 | |
| 9065 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) |
| 9066 | /* all regs in this state in all frames were already marked */ |
| 9067 | return; |
| 9068 | |
| 9069 | for (i = 0; i <= st->curframe; i++) |
| 9070 | clean_func_state(env, st->frame[i]); |
| 9071 | } |
| 9072 | |
| 9073 | /* the parentage chains form a tree. |
| 9074 | * the verifier states are added to state lists at given insn and |
| 9075 | * pushed into state stack for future exploration. |
| 9076 | * when the verifier reaches bpf_exit insn some of the verifer states |
| 9077 | * stored in the state lists have their final liveness state already, |
| 9078 | * but a lot of states will get revised from liveness point of view when |
| 9079 | * the verifier explores other branches. |
| 9080 | * Example: |
| 9081 | * 1: r0 = 1 |
| 9082 | * 2: if r1 == 100 goto pc+1 |
| 9083 | * 3: r0 = 2 |
| 9084 | * 4: exit |
| 9085 | * when the verifier reaches exit insn the register r0 in the state list of |
| 9086 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch |
| 9087 | * of insn 2 and goes exploring further. At the insn 4 it will walk the |
| 9088 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. |
| 9089 | * |
| 9090 | * Since the verifier pushes the branch states as it sees them while exploring |
| 9091 | * the program the condition of walking the branch instruction for the second |
| 9092 | * time means that all states below this branch were already explored and |
| 9093 | * their final liveness markes are already propagated. |
| 9094 | * Hence when the verifier completes the search of state list in is_state_visited() |
| 9095 | * we can call this clean_live_states() function to mark all liveness states |
| 9096 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' |
| 9097 | * will not be used. |
| 9098 | * This function also clears the registers and stack for states that !READ |
| 9099 | * to simplify state merging. |
| 9100 | * |
| 9101 | * Important note here that walking the same branch instruction in the callee |
| 9102 | * doesn't meant that the states are DONE. The verifier has to compare |
| 9103 | * the callsites |
| 9104 | */ |
| 9105 | static void clean_live_states(struct bpf_verifier_env *env, int insn, |
| 9106 | struct bpf_verifier_state *cur) |
| 9107 | { |
| 9108 | struct bpf_verifier_state_list *sl; |
| 9109 | int i; |
| 9110 | |
| 9111 | sl = *explored_state(env, insn); |
| 9112 | while (sl) { |
| 9113 | if (sl->state.branches) |
| 9114 | goto next; |
| 9115 | if (sl->state.insn_idx != insn || |
| 9116 | sl->state.curframe != cur->curframe) |
| 9117 | goto next; |
| 9118 | for (i = 0; i <= cur->curframe; i++) |
| 9119 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) |
| 9120 | goto next; |
| 9121 | clean_verifier_state(env, &sl->state); |
| 9122 | next: |
| 9123 | sl = sl->next; |
| 9124 | } |
| 9125 | } |
| 9126 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9127 | /* Returns true if (rold safe implies rcur safe) */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9128 | static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, |
| 9129 | struct bpf_reg_state *rcur, struct bpf_id_pair *idmap) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9130 | { |
| 9131 | bool equal; |
| 9132 | |
| 9133 | if (!(rold->live & REG_LIVE_READ)) |
| 9134 | /* explored state didn't use this */ |
| 9135 | return true; |
| 9136 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9137 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9138 | |
| 9139 | if (rold->type == PTR_TO_STACK) |
| 9140 | /* two stack pointers are equal only if they're pointing to |
| 9141 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 9142 | */ |
| 9143 | return equal && rold->frameno == rcur->frameno; |
| 9144 | |
| 9145 | if (equal) |
| 9146 | return true; |
| 9147 | |
| 9148 | if (rold->type == NOT_INIT) |
| 9149 | /* explored state can't have used this */ |
| 9150 | return true; |
| 9151 | if (rcur->type == NOT_INIT) |
| 9152 | return false; |
| 9153 | switch (rold->type) { |
| 9154 | case SCALAR_VALUE: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9155 | if (env->explore_alu_limits) |
| 9156 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9157 | if (rcur->type == SCALAR_VALUE) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9158 | if (!rold->precise && !rcur->precise) |
| 9159 | return true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9160 | /* new val must satisfy old val knowledge */ |
| 9161 | return range_within(rold, rcur) && |
| 9162 | tnum_in(rold->var_off, rcur->var_off); |
| 9163 | } else { |
| 9164 | /* We're trying to use a pointer in place of a scalar. |
| 9165 | * Even if the scalar was unbounded, this could lead to |
| 9166 | * pointer leaks because scalars are allowed to leak |
| 9167 | * while pointers are not. We could make this safe in |
| 9168 | * special cases if root is calling us, but it's |
| 9169 | * probably not worth the hassle. |
| 9170 | */ |
| 9171 | return false; |
| 9172 | } |
| 9173 | case PTR_TO_MAP_VALUE: |
| 9174 | /* If the new min/max/var_off satisfy the old ones and |
| 9175 | * everything else matches, we are OK. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9176 | * 'id' is not compared, since it's only used for maps with |
| 9177 | * bpf_spin_lock inside map element and in such cases if |
| 9178 | * the rest of the prog is valid for one map element then |
| 9179 | * it's valid for all map elements regardless of the key |
| 9180 | * used in bpf_map_lookup() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9181 | */ |
| 9182 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 9183 | range_within(rold, rcur) && |
| 9184 | tnum_in(rold->var_off, rcur->var_off); |
| 9185 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 9186 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 9187 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 9188 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 9189 | * checked, doing so could have affected others with the same |
| 9190 | * id, and we can't check for that because we lost the id when |
| 9191 | * we converted to a PTR_TO_MAP_VALUE. |
| 9192 | */ |
| 9193 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 9194 | return false; |
| 9195 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 9196 | return false; |
| 9197 | /* Check our ids match any regs they're supposed to */ |
| 9198 | return check_ids(rold->id, rcur->id, idmap); |
| 9199 | case PTR_TO_PACKET_META: |
| 9200 | case PTR_TO_PACKET: |
| 9201 | if (rcur->type != rold->type) |
| 9202 | return false; |
| 9203 | /* We must have at least as much range as the old ptr |
| 9204 | * did, so that any accesses which were safe before are |
| 9205 | * still safe. This is true even if old range < old off, |
| 9206 | * since someone could have accessed through (ptr - k), or |
| 9207 | * even done ptr -= k in a register, to get a safe access. |
| 9208 | */ |
| 9209 | if (rold->range > rcur->range) |
| 9210 | return false; |
| 9211 | /* If the offsets don't match, we can't trust our alignment; |
| 9212 | * nor can we be sure that we won't fall out of range. |
| 9213 | */ |
| 9214 | if (rold->off != rcur->off) |
| 9215 | return false; |
| 9216 | /* id relations must be preserved */ |
| 9217 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 9218 | return false; |
| 9219 | /* new val must satisfy old val knowledge */ |
| 9220 | return range_within(rold, rcur) && |
| 9221 | tnum_in(rold->var_off, rcur->var_off); |
| 9222 | case PTR_TO_CTX: |
| 9223 | case CONST_PTR_TO_MAP: |
| 9224 | case PTR_TO_PACKET_END: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9225 | case PTR_TO_FLOW_KEYS: |
| 9226 | case PTR_TO_SOCKET: |
| 9227 | case PTR_TO_SOCKET_OR_NULL: |
| 9228 | case PTR_TO_SOCK_COMMON: |
| 9229 | case PTR_TO_SOCK_COMMON_OR_NULL: |
| 9230 | case PTR_TO_TCP_SOCK: |
| 9231 | case PTR_TO_TCP_SOCK_OR_NULL: |
| 9232 | case PTR_TO_XDP_SOCK: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9233 | /* Only valid matches are exact, which memcmp() above |
| 9234 | * would have accepted |
| 9235 | */ |
| 9236 | default: |
| 9237 | /* Don't know what's going on, just say it's not safe */ |
| 9238 | return false; |
| 9239 | } |
| 9240 | |
| 9241 | /* Shouldn't get here; if we do, say it's not safe */ |
| 9242 | WARN_ON_ONCE(1); |
| 9243 | return false; |
| 9244 | } |
| 9245 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9246 | static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, |
| 9247 | struct bpf_func_state *cur, struct bpf_id_pair *idmap) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9248 | { |
| 9249 | int i, spi; |
| 9250 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9251 | /* walk slots of the explored stack and ignore any additional |
| 9252 | * slots in the current stack, since explored(safe) state |
| 9253 | * didn't use them |
| 9254 | */ |
| 9255 | for (i = 0; i < old->allocated_stack; i++) { |
| 9256 | spi = i / BPF_REG_SIZE; |
| 9257 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9258 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
| 9259 | i += BPF_REG_SIZE - 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9260 | /* explored state didn't use this */ |
| 9261 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9262 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9263 | |
| 9264 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 9265 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9266 | |
| 9267 | /* explored stack has more populated slots than current stack |
| 9268 | * and these slots were used |
| 9269 | */ |
| 9270 | if (i >= cur->allocated_stack) |
| 9271 | return false; |
| 9272 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9273 | /* if old state was safe with misc data in the stack |
| 9274 | * it will be safe with zero-initialized stack. |
| 9275 | * The opposite is not true |
| 9276 | */ |
| 9277 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 9278 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 9279 | continue; |
| 9280 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 9281 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 9282 | /* Ex: old explored (safe) state has STACK_SPILL in |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9283 | * this stack slot, but current has STACK_MISC -> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9284 | * this verifier states are not equivalent, |
| 9285 | * return false to continue verification of this path |
| 9286 | */ |
| 9287 | return false; |
| 9288 | if (i % BPF_REG_SIZE) |
| 9289 | continue; |
| 9290 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 9291 | continue; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9292 | if (!regsafe(env, &old->stack[spi].spilled_ptr, |
| 9293 | &cur->stack[spi].spilled_ptr, idmap)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9294 | /* when explored and current stack slot are both storing |
| 9295 | * spilled registers, check that stored pointers types |
| 9296 | * are the same as well. |
| 9297 | * Ex: explored safe path could have stored |
| 9298 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 9299 | * but current path has stored: |
| 9300 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 9301 | * such verifier states are not equivalent. |
| 9302 | * return false to continue verification of this path |
| 9303 | */ |
| 9304 | return false; |
| 9305 | } |
| 9306 | return true; |
| 9307 | } |
| 9308 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9309 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 9310 | { |
| 9311 | if (old->acquired_refs != cur->acquired_refs) |
| 9312 | return false; |
| 9313 | return !memcmp(old->refs, cur->refs, |
| 9314 | sizeof(*old->refs) * old->acquired_refs); |
| 9315 | } |
| 9316 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9317 | /* compare two verifier states |
| 9318 | * |
| 9319 | * all states stored in state_list are known to be valid, since |
| 9320 | * verifier reached 'bpf_exit' instruction through them |
| 9321 | * |
| 9322 | * this function is called when verifier exploring different branches of |
| 9323 | * execution popped from the state stack. If it sees an old state that has |
| 9324 | * more strict register state and more strict stack state then this execution |
| 9325 | * branch doesn't need to be explored further, since verifier already |
| 9326 | * concluded that more strict state leads to valid finish. |
| 9327 | * |
| 9328 | * Therefore two states are equivalent if register state is more conservative |
| 9329 | * and explored stack state is more conservative than the current one. |
| 9330 | * Example: |
| 9331 | * explored current |
| 9332 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 9333 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 9334 | * |
| 9335 | * In other words if current stack state (one being explored) has more |
| 9336 | * valid slots than old one that already passed validation, it means |
| 9337 | * the verifier can stop exploring and conclude that current state is valid too |
| 9338 | * |
| 9339 | * Similarly with registers. If explored state has register type as invalid |
| 9340 | * whereas register type in current state is meaningful, it means that |
| 9341 | * the current state will reach 'bpf_exit' instruction safely |
| 9342 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9343 | static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_state *old, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9344 | struct bpf_func_state *cur) |
| 9345 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9346 | int i; |
| 9347 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9348 | memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch)); |
| 9349 | for (i = 0; i < MAX_BPF_REG; i++) |
| 9350 | if (!regsafe(env, &old->regs[i], &cur->regs[i], |
| 9351 | env->idmap_scratch)) |
| 9352 | return false; |
| 9353 | |
| 9354 | if (!stacksafe(env, old, cur, env->idmap_scratch)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9355 | return false; |
| 9356 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9357 | if (!refsafe(old, cur)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9358 | return false; |
| 9359 | |
| 9360 | return true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9361 | } |
| 9362 | |
| 9363 | static bool states_equal(struct bpf_verifier_env *env, |
| 9364 | struct bpf_verifier_state *old, |
| 9365 | struct bpf_verifier_state *cur) |
| 9366 | { |
| 9367 | int i; |
| 9368 | |
| 9369 | if (old->curframe != cur->curframe) |
| 9370 | return false; |
| 9371 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9372 | /* Verification state from speculative execution simulation |
| 9373 | * must never prune a non-speculative execution one. |
| 9374 | */ |
| 9375 | if (old->speculative && !cur->speculative) |
| 9376 | return false; |
| 9377 | |
| 9378 | if (old->active_spin_lock != cur->active_spin_lock) |
| 9379 | return false; |
| 9380 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9381 | /* for states to be equal callsites have to be the same |
| 9382 | * and all frame states need to be equivalent |
| 9383 | */ |
| 9384 | for (i = 0; i <= old->curframe; i++) { |
| 9385 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 9386 | return false; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9387 | if (!func_states_equal(env, old->frame[i], cur->frame[i])) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9388 | return false; |
| 9389 | } |
| 9390 | return true; |
| 9391 | } |
| 9392 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9393 | /* Return 0 if no propagation happened. Return negative error code if error |
| 9394 | * happened. Otherwise, return the propagated bit. |
| 9395 | */ |
| 9396 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
| 9397 | struct bpf_reg_state *reg, |
| 9398 | struct bpf_reg_state *parent_reg) |
| 9399 | { |
| 9400 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
| 9401 | u8 flag = reg->live & REG_LIVE_READ; |
| 9402 | int err; |
| 9403 | |
| 9404 | /* When comes here, read flags of PARENT_REG or REG could be any of |
| 9405 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need |
| 9406 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. |
| 9407 | */ |
| 9408 | if (parent_flag == REG_LIVE_READ64 || |
| 9409 | /* Or if there is no read flag from REG. */ |
| 9410 | !flag || |
| 9411 | /* Or if the read flag from REG is the same as PARENT_REG. */ |
| 9412 | parent_flag == flag) |
| 9413 | return 0; |
| 9414 | |
| 9415 | err = mark_reg_read(env, reg, parent_reg, flag); |
| 9416 | if (err) |
| 9417 | return err; |
| 9418 | |
| 9419 | return flag; |
| 9420 | } |
| 9421 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9422 | /* A write screens off any subsequent reads; but write marks come from the |
| 9423 | * straight-line code between a state and its parent. When we arrive at an |
| 9424 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 9425 | * code, so read marks in the state must propagate to the parent regardless |
| 9426 | * of the state's write marks. That's what 'parent == state->parent' comparison |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9427 | * in mark_reg_read() is for. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9428 | */ |
| 9429 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 9430 | const struct bpf_verifier_state *vstate, |
| 9431 | struct bpf_verifier_state *vparent) |
| 9432 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9433 | struct bpf_reg_state *state_reg, *parent_reg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9434 | struct bpf_func_state *state, *parent; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9435 | int i, frame, err = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9436 | |
| 9437 | if (vparent->curframe != vstate->curframe) { |
| 9438 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 9439 | vparent->curframe, vstate->curframe); |
| 9440 | return -EFAULT; |
| 9441 | } |
| 9442 | /* Propagate read liveness of registers... */ |
| 9443 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9444 | for (frame = 0; frame <= vstate->curframe; frame++) { |
| 9445 | parent = vparent->frame[frame]; |
| 9446 | state = vstate->frame[frame]; |
| 9447 | parent_reg = parent->regs; |
| 9448 | state_reg = state->regs; |
| 9449 | /* We don't need to worry about FP liveness, it's read-only */ |
| 9450 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { |
| 9451 | err = propagate_liveness_reg(env, &state_reg[i], |
| 9452 | &parent_reg[i]); |
| 9453 | if (err < 0) |
| 9454 | return err; |
| 9455 | if (err == REG_LIVE_READ64) |
| 9456 | mark_insn_zext(env, &parent_reg[i]); |
| 9457 | } |
| 9458 | |
| 9459 | /* Propagate stack slots. */ |
| 9460 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 9461 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
| 9462 | parent_reg = &parent->stack[i].spilled_ptr; |
| 9463 | state_reg = &state->stack[i].spilled_ptr; |
| 9464 | err = propagate_liveness_reg(env, state_reg, |
| 9465 | parent_reg); |
| 9466 | if (err < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9467 | return err; |
| 9468 | } |
| 9469 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9470 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9471 | } |
| 9472 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9473 | /* find precise scalars in the previous equivalent state and |
| 9474 | * propagate them into the current state |
| 9475 | */ |
| 9476 | static int propagate_precision(struct bpf_verifier_env *env, |
| 9477 | const struct bpf_verifier_state *old) |
| 9478 | { |
| 9479 | struct bpf_reg_state *state_reg; |
| 9480 | struct bpf_func_state *state; |
| 9481 | int i, err = 0; |
| 9482 | |
| 9483 | state = old->frame[old->curframe]; |
| 9484 | state_reg = state->regs; |
| 9485 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { |
| 9486 | if (state_reg->type != SCALAR_VALUE || |
| 9487 | !state_reg->precise) |
| 9488 | continue; |
| 9489 | if (env->log.level & BPF_LOG_LEVEL2) |
| 9490 | verbose(env, "propagating r%d\n", i); |
| 9491 | err = mark_chain_precision(env, i); |
| 9492 | if (err < 0) |
| 9493 | return err; |
| 9494 | } |
| 9495 | |
| 9496 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 9497 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 9498 | continue; |
| 9499 | state_reg = &state->stack[i].spilled_ptr; |
| 9500 | if (state_reg->type != SCALAR_VALUE || |
| 9501 | !state_reg->precise) |
| 9502 | continue; |
| 9503 | if (env->log.level & BPF_LOG_LEVEL2) |
| 9504 | verbose(env, "propagating fp%d\n", |
| 9505 | (-i - 1) * BPF_REG_SIZE); |
| 9506 | err = mark_chain_precision_stack(env, i); |
| 9507 | if (err < 0) |
| 9508 | return err; |
| 9509 | } |
| 9510 | return 0; |
| 9511 | } |
| 9512 | |
| 9513 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
| 9514 | struct bpf_verifier_state *cur) |
| 9515 | { |
| 9516 | struct bpf_func_state *fold, *fcur; |
| 9517 | int i, fr = cur->curframe; |
| 9518 | |
| 9519 | if (old->curframe != fr) |
| 9520 | return false; |
| 9521 | |
| 9522 | fold = old->frame[fr]; |
| 9523 | fcur = cur->frame[fr]; |
| 9524 | for (i = 0; i < MAX_BPF_REG; i++) |
| 9525 | if (memcmp(&fold->regs[i], &fcur->regs[i], |
| 9526 | offsetof(struct bpf_reg_state, parent))) |
| 9527 | return false; |
| 9528 | return true; |
| 9529 | } |
| 9530 | |
| 9531 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9532 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
| 9533 | { |
| 9534 | struct bpf_verifier_state_list *new_sl; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9535 | struct bpf_verifier_state_list *sl, **pprev; |
| 9536 | struct bpf_verifier_state *cur = env->cur_state, *new; |
| 9537 | int i, j, err, states_cnt = 0; |
| 9538 | bool add_new_state = env->test_state_freq ? true : false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9539 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9540 | cur->last_insn_idx = env->prev_insn_idx; |
| 9541 | if (!env->insn_aux_data[insn_idx].prune_point) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9542 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 9543 | * be doing state search here |
| 9544 | */ |
| 9545 | return 0; |
| 9546 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9547 | /* bpf progs typically have pruning point every 4 instructions |
| 9548 | * http://vger.kernel.org/bpfconf2019.html#session-1 |
| 9549 | * Do not add new state for future pruning if the verifier hasn't seen |
| 9550 | * at least 2 jumps and at least 8 instructions. |
| 9551 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. |
| 9552 | * In tests that amounts to up to 50% reduction into total verifier |
| 9553 | * memory consumption and 20% verifier time speedup. |
| 9554 | */ |
| 9555 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && |
| 9556 | env->insn_processed - env->prev_insn_processed >= 8) |
| 9557 | add_new_state = true; |
| 9558 | |
| 9559 | pprev = explored_state(env, insn_idx); |
| 9560 | sl = *pprev; |
| 9561 | |
| 9562 | clean_live_states(env, insn_idx, cur); |
| 9563 | |
| 9564 | while (sl) { |
| 9565 | states_cnt++; |
| 9566 | if (sl->state.insn_idx != insn_idx) |
| 9567 | goto next; |
| 9568 | if (sl->state.branches) { |
| 9569 | if (states_maybe_looping(&sl->state, cur) && |
| 9570 | states_equal(env, &sl->state, cur)) { |
| 9571 | verbose_linfo(env, insn_idx, "; "); |
| 9572 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); |
| 9573 | return -EINVAL; |
| 9574 | } |
| 9575 | /* if the verifier is processing a loop, avoid adding new state |
| 9576 | * too often, since different loop iterations have distinct |
| 9577 | * states and may not help future pruning. |
| 9578 | * This threshold shouldn't be too low to make sure that |
| 9579 | * a loop with large bound will be rejected quickly. |
| 9580 | * The most abusive loop will be: |
| 9581 | * r1 += 1 |
| 9582 | * if r1 < 1000000 goto pc-2 |
| 9583 | * 1M insn_procssed limit / 100 == 10k peak states. |
| 9584 | * This threshold shouldn't be too high either, since states |
| 9585 | * at the end of the loop are likely to be useful in pruning. |
| 9586 | */ |
| 9587 | if (env->jmps_processed - env->prev_jmps_processed < 20 && |
| 9588 | env->insn_processed - env->prev_insn_processed < 100) |
| 9589 | add_new_state = false; |
| 9590 | goto miss; |
| 9591 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9592 | if (states_equal(env, &sl->state, cur)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9593 | sl->hit_cnt++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9594 | /* reached equivalent register/stack state, |
| 9595 | * prune the search. |
| 9596 | * Registers read by the continuation are read by us. |
| 9597 | * If we have any write marks in env->cur_state, they |
| 9598 | * will prevent corresponding reads in the continuation |
| 9599 | * from reaching our parent (an explored_state). Our |
| 9600 | * own state will get the read marks recorded, but |
| 9601 | * they'll be immediately forgotten as we're pruning |
| 9602 | * this state and will pop a new one. |
| 9603 | */ |
| 9604 | err = propagate_liveness(env, &sl->state, cur); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9605 | |
| 9606 | /* if previous state reached the exit with precision and |
| 9607 | * current state is equivalent to it (except precsion marks) |
| 9608 | * the precision needs to be propagated back in |
| 9609 | * the current state. |
| 9610 | */ |
| 9611 | err = err ? : push_jmp_history(env, cur); |
| 9612 | err = err ? : propagate_precision(env, &sl->state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9613 | if (err) |
| 9614 | return err; |
| 9615 | return 1; |
| 9616 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9617 | miss: |
| 9618 | /* when new state is not going to be added do not increase miss count. |
| 9619 | * Otherwise several loop iterations will remove the state |
| 9620 | * recorded earlier. The goal of these heuristics is to have |
| 9621 | * states from some iterations of the loop (some in the beginning |
| 9622 | * and some at the end) to help pruning. |
| 9623 | */ |
| 9624 | if (add_new_state) |
| 9625 | sl->miss_cnt++; |
| 9626 | /* heuristic to determine whether this state is beneficial |
| 9627 | * to keep checking from state equivalence point of view. |
| 9628 | * Higher numbers increase max_states_per_insn and verification time, |
| 9629 | * but do not meaningfully decrease insn_processed. |
| 9630 | */ |
| 9631 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { |
| 9632 | /* the state is unlikely to be useful. Remove it to |
| 9633 | * speed up verification |
| 9634 | */ |
| 9635 | *pprev = sl->next; |
| 9636 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { |
| 9637 | u32 br = sl->state.branches; |
| 9638 | |
| 9639 | WARN_ONCE(br, |
| 9640 | "BUG live_done but branches_to_explore %d\n", |
| 9641 | br); |
| 9642 | free_verifier_state(&sl->state, false); |
| 9643 | kfree(sl); |
| 9644 | env->peak_states--; |
| 9645 | } else { |
| 9646 | /* cannot free this state, since parentage chain may |
| 9647 | * walk it later. Add it for free_list instead to |
| 9648 | * be freed at the end of verification |
| 9649 | */ |
| 9650 | sl->next = env->free_list; |
| 9651 | env->free_list = sl; |
| 9652 | } |
| 9653 | sl = *pprev; |
| 9654 | continue; |
| 9655 | } |
| 9656 | next: |
| 9657 | pprev = &sl->next; |
| 9658 | sl = *pprev; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9659 | } |
| 9660 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9661 | if (env->max_states_per_insn < states_cnt) |
| 9662 | env->max_states_per_insn = states_cnt; |
| 9663 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9664 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9665 | return push_jmp_history(env, cur); |
| 9666 | |
| 9667 | if (!add_new_state) |
| 9668 | return push_jmp_history(env, cur); |
| 9669 | |
| 9670 | /* There were no equivalent states, remember the current one. |
| 9671 | * Technically the current state is not proven to be safe yet, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9672 | * but it will either reach outer most bpf_exit (which means it's safe) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9673 | * or it will be rejected. When there are no loops the verifier won't be |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9674 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9675 | * again on the way to bpf_exit. |
| 9676 | * When looping the sl->state.branches will be > 0 and this state |
| 9677 | * will not be considered for equivalence until branches == 0. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9678 | */ |
| 9679 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
| 9680 | if (!new_sl) |
| 9681 | return -ENOMEM; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9682 | env->total_states++; |
| 9683 | env->peak_states++; |
| 9684 | env->prev_jmps_processed = env->jmps_processed; |
| 9685 | env->prev_insn_processed = env->insn_processed; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9686 | |
| 9687 | /* add new state to the head of linked list */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9688 | new = &new_sl->state; |
| 9689 | err = copy_verifier_state(new, cur); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9690 | if (err) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9691 | free_verifier_state(new, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9692 | kfree(new_sl); |
| 9693 | return err; |
| 9694 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9695 | new->insn_idx = insn_idx; |
| 9696 | WARN_ONCE(new->branches != 1, |
| 9697 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); |
| 9698 | |
| 9699 | cur->parent = new; |
| 9700 | cur->first_insn_idx = insn_idx; |
| 9701 | clear_jmp_history(cur); |
| 9702 | new_sl->next = *explored_state(env, insn_idx); |
| 9703 | *explored_state(env, insn_idx) = new_sl; |
| 9704 | /* connect new state to parentage chain. Current frame needs all |
| 9705 | * registers connected. Only r6 - r9 of the callers are alive (pushed |
| 9706 | * to the stack implicitly by JITs) so in callers' frames connect just |
| 9707 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to |
| 9708 | * the state of the call instruction (with WRITTEN set), and r0 comes |
| 9709 | * from callee with its full parentage chain, anyway. |
| 9710 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9711 | /* clear write marks in current state: the writes we did are not writes |
| 9712 | * our child did, so they don't screen off its reads from us. |
| 9713 | * (There are no read marks in current state, because reads always mark |
| 9714 | * their parent and current state never has children yet. Only |
| 9715 | * explored_states can get read marks.) |
| 9716 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9717 | for (j = 0; j <= cur->curframe; j++) { |
| 9718 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) |
| 9719 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; |
| 9720 | for (i = 0; i < BPF_REG_FP; i++) |
| 9721 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; |
| 9722 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9723 | |
| 9724 | /* all stack frames are accessible from callee, clear them all */ |
| 9725 | for (j = 0; j <= cur->curframe; j++) { |
| 9726 | struct bpf_func_state *frame = cur->frame[j]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9727 | struct bpf_func_state *newframe = new->frame[j]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9728 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9729 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9730 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9731 | frame->stack[i].spilled_ptr.parent = |
| 9732 | &newframe->stack[i].spilled_ptr; |
| 9733 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9734 | } |
| 9735 | return 0; |
| 9736 | } |
| 9737 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9738 | /* Return true if it's OK to have the same insn return a different type. */ |
| 9739 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 9740 | { |
| 9741 | switch (type) { |
| 9742 | case PTR_TO_CTX: |
| 9743 | case PTR_TO_SOCKET: |
| 9744 | case PTR_TO_SOCKET_OR_NULL: |
| 9745 | case PTR_TO_SOCK_COMMON: |
| 9746 | case PTR_TO_SOCK_COMMON_OR_NULL: |
| 9747 | case PTR_TO_TCP_SOCK: |
| 9748 | case PTR_TO_TCP_SOCK_OR_NULL: |
| 9749 | case PTR_TO_XDP_SOCK: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9750 | case PTR_TO_BTF_ID: |
| 9751 | case PTR_TO_BTF_ID_OR_NULL: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9752 | return false; |
| 9753 | default: |
| 9754 | return true; |
| 9755 | } |
| 9756 | } |
| 9757 | |
| 9758 | /* If an instruction was previously used with particular pointer types, then we |
| 9759 | * need to be careful to avoid cases such as the below, where it may be ok |
| 9760 | * for one branch accessing the pointer, but not ok for the other branch: |
| 9761 | * |
| 9762 | * R1 = sock_ptr |
| 9763 | * goto X; |
| 9764 | * ... |
| 9765 | * R1 = some_other_valid_ptr; |
| 9766 | * goto X; |
| 9767 | * ... |
| 9768 | * R2 = *(u32 *)(R1 + 0); |
| 9769 | */ |
| 9770 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 9771 | { |
| 9772 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 9773 | !reg_type_mismatch_ok(prev)); |
| 9774 | } |
| 9775 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9776 | static int do_check(struct bpf_verifier_env *env) |
| 9777 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 9778 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
| 9779 | struct bpf_verifier_state *state = env->cur_state; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9780 | struct bpf_insn *insns = env->prog->insnsi; |
| 9781 | struct bpf_reg_state *regs; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9782 | int insn_cnt = env->prog->len; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9783 | bool do_print_state = false; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9784 | int prev_insn_idx = -1; |
| 9785 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9786 | for (;;) { |
| 9787 | struct bpf_insn *insn; |
| 9788 | u8 class; |
| 9789 | int err; |
| 9790 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9791 | env->prev_insn_idx = prev_insn_idx; |
| 9792 | if (env->insn_idx >= insn_cnt) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9793 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9794 | env->insn_idx, insn_cnt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9795 | return -EFAULT; |
| 9796 | } |
| 9797 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9798 | insn = &insns[env->insn_idx]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9799 | class = BPF_CLASS(insn->code); |
| 9800 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9801 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9802 | verbose(env, |
| 9803 | "BPF program is too large. Processed %d insn\n", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9804 | env->insn_processed); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9805 | return -E2BIG; |
| 9806 | } |
| 9807 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9808 | err = is_state_visited(env, env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9809 | if (err < 0) |
| 9810 | return err; |
| 9811 | if (err == 1) { |
| 9812 | /* found equivalent state, can prune the search */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9813 | if (env->log.level & BPF_LOG_LEVEL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9814 | if (do_print_state) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9815 | verbose(env, "\nfrom %d to %d%s: safe\n", |
| 9816 | env->prev_insn_idx, env->insn_idx, |
| 9817 | env->cur_state->speculative ? |
| 9818 | " (speculative execution)" : ""); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9819 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9820 | verbose(env, "%d: safe\n", env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9821 | } |
| 9822 | goto process_bpf_exit; |
| 9823 | } |
| 9824 | |
| 9825 | if (signal_pending(current)) |
| 9826 | return -EAGAIN; |
| 9827 | |
| 9828 | if (need_resched()) |
| 9829 | cond_resched(); |
| 9830 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9831 | if (env->log.level & BPF_LOG_LEVEL2 || |
| 9832 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { |
| 9833 | if (env->log.level & BPF_LOG_LEVEL2) |
| 9834 | verbose(env, "%d:", env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9835 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9836 | verbose(env, "\nfrom %d to %d%s:", |
| 9837 | env->prev_insn_idx, env->insn_idx, |
| 9838 | env->cur_state->speculative ? |
| 9839 | " (speculative execution)" : ""); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9840 | print_verifier_state(env, state->frame[state->curframe]); |
| 9841 | do_print_state = false; |
| 9842 | } |
| 9843 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9844 | if (env->log.level & BPF_LOG_LEVEL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9845 | const struct bpf_insn_cbs cbs = { |
| 9846 | .cb_print = verbose, |
| 9847 | .private_data = env, |
| 9848 | }; |
| 9849 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9850 | verbose_linfo(env, env->insn_idx, "; "); |
| 9851 | verbose(env, "%d: ", env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9852 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
| 9853 | } |
| 9854 | |
| 9855 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9856 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
| 9857 | env->prev_insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9858 | if (err) |
| 9859 | return err; |
| 9860 | } |
| 9861 | |
| 9862 | regs = cur_regs(env); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 9863 | sanitize_mark_insn_seen(env); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9864 | prev_insn_idx = env->insn_idx; |
| 9865 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9866 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 9867 | err = check_alu_op(env, insn); |
| 9868 | if (err) |
| 9869 | return err; |
| 9870 | |
| 9871 | } else if (class == BPF_LDX) { |
| 9872 | enum bpf_reg_type *prev_src_type, src_reg_type; |
| 9873 | |
| 9874 | /* check for reserved fields is already done */ |
| 9875 | |
| 9876 | /* check src operand */ |
| 9877 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 9878 | if (err) |
| 9879 | return err; |
| 9880 | |
| 9881 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
| 9882 | if (err) |
| 9883 | return err; |
| 9884 | |
| 9885 | src_reg_type = regs[insn->src_reg].type; |
| 9886 | |
| 9887 | /* check that memory (src_reg + off) is readable, |
| 9888 | * the state of dst_reg will be updated by this func |
| 9889 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9890 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
| 9891 | insn->off, BPF_SIZE(insn->code), |
| 9892 | BPF_READ, insn->dst_reg, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9893 | if (err) |
| 9894 | return err; |
| 9895 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9896 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9897 | |
| 9898 | if (*prev_src_type == NOT_INIT) { |
| 9899 | /* saw a valid insn |
| 9900 | * dst_reg = *(u32 *)(src_reg + off) |
| 9901 | * save type to validate intersecting paths |
| 9902 | */ |
| 9903 | *prev_src_type = src_reg_type; |
| 9904 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9905 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9906 | /* ABuser program is trying to use the same insn |
| 9907 | * dst_reg = *(u32*) (src_reg + off) |
| 9908 | * with different pointer types: |
| 9909 | * src_reg == ctx in one branch and |
| 9910 | * src_reg == stack|map in some other branch. |
| 9911 | * Reject it. |
| 9912 | */ |
| 9913 | verbose(env, "same insn cannot be used with different pointers\n"); |
| 9914 | return -EINVAL; |
| 9915 | } |
| 9916 | |
| 9917 | } else if (class == BPF_STX) { |
| 9918 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
| 9919 | |
| 9920 | if (BPF_MODE(insn->code) == BPF_XADD) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9921 | err = check_xadd(env, env->insn_idx, insn); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9922 | if (err) |
| 9923 | return err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9924 | env->insn_idx++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9925 | continue; |
| 9926 | } |
| 9927 | |
| 9928 | /* check src1 operand */ |
| 9929 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 9930 | if (err) |
| 9931 | return err; |
| 9932 | /* check src2 operand */ |
| 9933 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
| 9934 | if (err) |
| 9935 | return err; |
| 9936 | |
| 9937 | dst_reg_type = regs[insn->dst_reg].type; |
| 9938 | |
| 9939 | /* check that memory (dst_reg + off) is writeable */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9940 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 9941 | insn->off, BPF_SIZE(insn->code), |
| 9942 | BPF_WRITE, insn->src_reg, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9943 | if (err) |
| 9944 | return err; |
| 9945 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9946 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9947 | |
| 9948 | if (*prev_dst_type == NOT_INIT) { |
| 9949 | *prev_dst_type = dst_reg_type; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9950 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9951 | verbose(env, "same insn cannot be used with different pointers\n"); |
| 9952 | return -EINVAL; |
| 9953 | } |
| 9954 | |
| 9955 | } else if (class == BPF_ST) { |
| 9956 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 9957 | insn->src_reg != BPF_REG_0) { |
| 9958 | verbose(env, "BPF_ST uses reserved fields\n"); |
| 9959 | return -EINVAL; |
| 9960 | } |
| 9961 | /* check src operand */ |
| 9962 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
| 9963 | if (err) |
| 9964 | return err; |
| 9965 | |
| 9966 | if (is_ctx_reg(env, insn->dst_reg)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9967 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
| 9968 | insn->dst_reg, |
| 9969 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9970 | return -EACCES; |
| 9971 | } |
| 9972 | |
| 9973 | /* check that memory (dst_reg + off) is writeable */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9974 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 9975 | insn->off, BPF_SIZE(insn->code), |
| 9976 | BPF_WRITE, -1, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9977 | if (err) |
| 9978 | return err; |
| 9979 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9980 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9981 | u8 opcode = BPF_OP(insn->code); |
| 9982 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9983 | env->jmps_processed++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9984 | if (opcode == BPF_CALL) { |
| 9985 | if (BPF_SRC(insn->code) != BPF_K || |
| 9986 | insn->off != 0 || |
| 9987 | (insn->src_reg != BPF_REG_0 && |
| 9988 | insn->src_reg != BPF_PSEUDO_CALL) || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9989 | insn->dst_reg != BPF_REG_0 || |
| 9990 | class == BPF_JMP32) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9991 | verbose(env, "BPF_CALL uses reserved fields\n"); |
| 9992 | return -EINVAL; |
| 9993 | } |
| 9994 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9995 | if (env->cur_state->active_spin_lock && |
| 9996 | (insn->src_reg == BPF_PSEUDO_CALL || |
| 9997 | insn->imm != BPF_FUNC_spin_unlock)) { |
| 9998 | verbose(env, "function calls are not allowed while holding a lock\n"); |
| 9999 | return -EINVAL; |
| 10000 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10001 | if (insn->src_reg == BPF_PSEUDO_CALL) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10002 | err = check_func_call(env, insn, &env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10003 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10004 | err = check_helper_call(env, insn->imm, env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10005 | if (err) |
| 10006 | return err; |
| 10007 | |
| 10008 | } else if (opcode == BPF_JA) { |
| 10009 | if (BPF_SRC(insn->code) != BPF_K || |
| 10010 | insn->imm != 0 || |
| 10011 | insn->src_reg != BPF_REG_0 || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10012 | insn->dst_reg != BPF_REG_0 || |
| 10013 | class == BPF_JMP32) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10014 | verbose(env, "BPF_JA uses reserved fields\n"); |
| 10015 | return -EINVAL; |
| 10016 | } |
| 10017 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10018 | env->insn_idx += insn->off + 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10019 | continue; |
| 10020 | |
| 10021 | } else if (opcode == BPF_EXIT) { |
| 10022 | if (BPF_SRC(insn->code) != BPF_K || |
| 10023 | insn->imm != 0 || |
| 10024 | insn->src_reg != BPF_REG_0 || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10025 | insn->dst_reg != BPF_REG_0 || |
| 10026 | class == BPF_JMP32) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10027 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
| 10028 | return -EINVAL; |
| 10029 | } |
| 10030 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10031 | if (env->cur_state->active_spin_lock) { |
| 10032 | verbose(env, "bpf_spin_unlock is missing\n"); |
| 10033 | return -EINVAL; |
| 10034 | } |
| 10035 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10036 | if (state->curframe) { |
| 10037 | /* exit from nested function */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10038 | err = prepare_func_exit(env, &env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10039 | if (err) |
| 10040 | return err; |
| 10041 | do_print_state = true; |
| 10042 | continue; |
| 10043 | } |
| 10044 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10045 | err = check_reference_leak(env); |
| 10046 | if (err) |
| 10047 | return err; |
| 10048 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10049 | err = check_return_code(env); |
| 10050 | if (err) |
| 10051 | return err; |
| 10052 | process_bpf_exit: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10053 | update_branch_counts(env, env->cur_state); |
| 10054 | err = pop_stack(env, &prev_insn_idx, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10055 | &env->insn_idx, pop_log); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10056 | if (err < 0) { |
| 10057 | if (err != -ENOENT) |
| 10058 | return err; |
| 10059 | break; |
| 10060 | } else { |
| 10061 | do_print_state = true; |
| 10062 | continue; |
| 10063 | } |
| 10064 | } else { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10065 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10066 | if (err) |
| 10067 | return err; |
| 10068 | } |
| 10069 | } else if (class == BPF_LD) { |
| 10070 | u8 mode = BPF_MODE(insn->code); |
| 10071 | |
| 10072 | if (mode == BPF_ABS || mode == BPF_IND) { |
| 10073 | err = check_ld_abs(env, insn); |
| 10074 | if (err) |
| 10075 | return err; |
| 10076 | |
| 10077 | } else if (mode == BPF_IMM) { |
| 10078 | err = check_ld_imm(env, insn); |
| 10079 | if (err) |
| 10080 | return err; |
| 10081 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10082 | env->insn_idx++; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10083 | sanitize_mark_insn_seen(env); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10084 | } else { |
| 10085 | verbose(env, "invalid BPF_LD mode\n"); |
| 10086 | return -EINVAL; |
| 10087 | } |
| 10088 | } else { |
| 10089 | verbose(env, "unknown insn class %d\n", class); |
| 10090 | return -EINVAL; |
| 10091 | } |
| 10092 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10093 | env->insn_idx++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10094 | } |
| 10095 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10096 | return 0; |
| 10097 | } |
| 10098 | |
| 10099 | /* replace pseudo btf_id with kernel symbol address */ |
| 10100 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, |
| 10101 | struct bpf_insn *insn, |
| 10102 | struct bpf_insn_aux_data *aux) |
| 10103 | { |
| 10104 | const struct btf_var_secinfo *vsi; |
| 10105 | const struct btf_type *datasec; |
| 10106 | const struct btf_type *t; |
| 10107 | const char *sym_name; |
| 10108 | bool percpu = false; |
| 10109 | u32 type, id = insn->imm; |
| 10110 | s32 datasec_id; |
| 10111 | u64 addr; |
| 10112 | int i; |
| 10113 | |
| 10114 | if (!btf_vmlinux) { |
| 10115 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); |
| 10116 | return -EINVAL; |
| 10117 | } |
| 10118 | |
| 10119 | if (insn[1].imm != 0) { |
| 10120 | verbose(env, "reserved field (insn[1].imm) is used in pseudo_btf_id ldimm64 insn.\n"); |
| 10121 | return -EINVAL; |
| 10122 | } |
| 10123 | |
| 10124 | t = btf_type_by_id(btf_vmlinux, id); |
| 10125 | if (!t) { |
| 10126 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); |
| 10127 | return -ENOENT; |
| 10128 | } |
| 10129 | |
| 10130 | if (!btf_type_is_var(t)) { |
| 10131 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", |
| 10132 | id); |
| 10133 | return -EINVAL; |
| 10134 | } |
| 10135 | |
| 10136 | sym_name = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 10137 | addr = kallsyms_lookup_name(sym_name); |
| 10138 | if (!addr) { |
| 10139 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", |
| 10140 | sym_name); |
| 10141 | return -ENOENT; |
| 10142 | } |
| 10143 | |
| 10144 | datasec_id = btf_find_by_name_kind(btf_vmlinux, ".data..percpu", |
| 10145 | BTF_KIND_DATASEC); |
| 10146 | if (datasec_id > 0) { |
| 10147 | datasec = btf_type_by_id(btf_vmlinux, datasec_id); |
| 10148 | for_each_vsi(i, datasec, vsi) { |
| 10149 | if (vsi->type == id) { |
| 10150 | percpu = true; |
| 10151 | break; |
| 10152 | } |
| 10153 | } |
| 10154 | } |
| 10155 | |
| 10156 | insn[0].imm = (u32)addr; |
| 10157 | insn[1].imm = addr >> 32; |
| 10158 | |
| 10159 | type = t->type; |
| 10160 | t = btf_type_skip_modifiers(btf_vmlinux, type, NULL); |
| 10161 | if (percpu) { |
| 10162 | aux->btf_var.reg_type = PTR_TO_PERCPU_BTF_ID; |
| 10163 | aux->btf_var.btf_id = type; |
| 10164 | } else if (!btf_type_is_struct(t)) { |
| 10165 | const struct btf_type *ret; |
| 10166 | const char *tname; |
| 10167 | u32 tsize; |
| 10168 | |
| 10169 | /* resolve the type size of ksym. */ |
| 10170 | ret = btf_resolve_size(btf_vmlinux, t, &tsize); |
| 10171 | if (IS_ERR(ret)) { |
| 10172 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 10173 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
| 10174 | tname, PTR_ERR(ret)); |
| 10175 | return -EINVAL; |
| 10176 | } |
| 10177 | aux->btf_var.reg_type = PTR_TO_MEM; |
| 10178 | aux->btf_var.mem_size = tsize; |
| 10179 | } else { |
| 10180 | aux->btf_var.reg_type = PTR_TO_BTF_ID; |
| 10181 | aux->btf_var.btf_id = type; |
| 10182 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10183 | return 0; |
| 10184 | } |
| 10185 | |
| 10186 | static int check_map_prealloc(struct bpf_map *map) |
| 10187 | { |
| 10188 | return (map->map_type != BPF_MAP_TYPE_HASH && |
| 10189 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 10190 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
| 10191 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 10192 | } |
| 10193 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10194 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
| 10195 | { |
| 10196 | switch (type) { |
| 10197 | case BPF_PROG_TYPE_KPROBE: |
| 10198 | case BPF_PROG_TYPE_TRACEPOINT: |
| 10199 | case BPF_PROG_TYPE_PERF_EVENT: |
| 10200 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 10201 | return true; |
| 10202 | default: |
| 10203 | return false; |
| 10204 | } |
| 10205 | } |
| 10206 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10207 | static bool is_preallocated_map(struct bpf_map *map) |
| 10208 | { |
| 10209 | if (!check_map_prealloc(map)) |
| 10210 | return false; |
| 10211 | if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) |
| 10212 | return false; |
| 10213 | return true; |
| 10214 | } |
| 10215 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10216 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 10217 | struct bpf_map *map, |
| 10218 | struct bpf_prog *prog) |
| 10219 | |
| 10220 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10221 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
| 10222 | /* |
| 10223 | * Validate that trace type programs use preallocated hash maps. |
| 10224 | * |
| 10225 | * For programs attached to PERF events this is mandatory as the |
| 10226 | * perf NMI can hit any arbitrary code sequence. |
| 10227 | * |
| 10228 | * All other trace types using preallocated hash maps are unsafe as |
| 10229 | * well because tracepoint or kprobes can be inside locked regions |
| 10230 | * of the memory allocator or at a place where a recursion into the |
| 10231 | * memory allocator would see inconsistent state. |
| 10232 | * |
| 10233 | * On RT enabled kernels run-time allocation of all trace type |
| 10234 | * programs is strictly prohibited due to lock type constraints. On |
| 10235 | * !RT kernels it is allowed for backwards compatibility reasons for |
| 10236 | * now, but warnings are emitted so developers are made aware of |
| 10237 | * the unsafety and can fix their programs before this is enforced. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10238 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10239 | if (is_tracing_prog_type(prog_type) && !is_preallocated_map(map)) { |
| 10240 | if (prog_type == BPF_PROG_TYPE_PERF_EVENT) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10241 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
| 10242 | return -EINVAL; |
| 10243 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10244 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
| 10245 | verbose(env, "trace type programs can only use preallocated hash map\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10246 | return -EINVAL; |
| 10247 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10248 | WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); |
| 10249 | verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10250 | } |
| 10251 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10252 | if ((is_tracing_prog_type(prog_type) || |
| 10253 | prog_type == BPF_PROG_TYPE_SOCKET_FILTER) && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10254 | map_value_has_spin_lock(map)) { |
| 10255 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); |
| 10256 | return -EINVAL; |
| 10257 | } |
| 10258 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10259 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
| 10260 | !bpf_offload_prog_map_match(prog, map)) { |
| 10261 | verbose(env, "offload device mismatch between prog and map\n"); |
| 10262 | return -EINVAL; |
| 10263 | } |
| 10264 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10265 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
| 10266 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); |
| 10267 | return -EINVAL; |
| 10268 | } |
| 10269 | |
| 10270 | if (prog->aux->sleepable) |
| 10271 | switch (map->map_type) { |
| 10272 | case BPF_MAP_TYPE_HASH: |
| 10273 | case BPF_MAP_TYPE_LRU_HASH: |
| 10274 | case BPF_MAP_TYPE_ARRAY: |
| 10275 | if (!is_preallocated_map(map)) { |
| 10276 | verbose(env, |
| 10277 | "Sleepable programs can only use preallocated hash maps\n"); |
| 10278 | return -EINVAL; |
| 10279 | } |
| 10280 | break; |
| 10281 | default: |
| 10282 | verbose(env, |
| 10283 | "Sleepable programs can only use array and hash maps\n"); |
| 10284 | return -EINVAL; |
| 10285 | } |
| 10286 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10287 | return 0; |
| 10288 | } |
| 10289 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10290 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 10291 | { |
| 10292 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 10293 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 10294 | } |
| 10295 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10296 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
| 10297 | * |
| 10298 | * 1. if it accesses map FD, replace it with actual map pointer. |
| 10299 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. |
| 10300 | * |
| 10301 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10302 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10303 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10304 | { |
| 10305 | struct bpf_insn *insn = env->prog->insnsi; |
| 10306 | int insn_cnt = env->prog->len; |
| 10307 | int i, j, err; |
| 10308 | |
| 10309 | err = bpf_prog_calc_tag(env->prog); |
| 10310 | if (err) |
| 10311 | return err; |
| 10312 | |
| 10313 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 10314 | if (BPF_CLASS(insn->code) == BPF_LDX && |
| 10315 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
| 10316 | verbose(env, "BPF_LDX uses reserved fields\n"); |
| 10317 | return -EINVAL; |
| 10318 | } |
| 10319 | |
| 10320 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 10321 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 10322 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
| 10323 | verbose(env, "BPF_STX uses reserved fields\n"); |
| 10324 | return -EINVAL; |
| 10325 | } |
| 10326 | |
| 10327 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10328 | struct bpf_insn_aux_data *aux; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10329 | struct bpf_map *map; |
| 10330 | struct fd f; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10331 | u64 addr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10332 | |
| 10333 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 10334 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 10335 | insn[1].off != 0) { |
| 10336 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
| 10337 | return -EINVAL; |
| 10338 | } |
| 10339 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10340 | if (insn[0].src_reg == 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10341 | /* valid generic load 64-bit imm */ |
| 10342 | goto next_insn; |
| 10343 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10344 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
| 10345 | aux = &env->insn_aux_data[i]; |
| 10346 | err = check_pseudo_btf_id(env, insn, aux); |
| 10347 | if (err) |
| 10348 | return err; |
| 10349 | goto next_insn; |
| 10350 | } |
| 10351 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10352 | /* In final convert_pseudo_ld_imm64() step, this is |
| 10353 | * converted into regular 64-bit imm load insn. |
| 10354 | */ |
| 10355 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && |
| 10356 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || |
| 10357 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && |
| 10358 | insn[1].imm != 0)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10359 | verbose(env, |
| 10360 | "unrecognized bpf_ld_imm64 insn\n"); |
| 10361 | return -EINVAL; |
| 10362 | } |
| 10363 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10364 | f = fdget(insn[0].imm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10365 | map = __bpf_map_get(f); |
| 10366 | if (IS_ERR(map)) { |
| 10367 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10368 | insn[0].imm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10369 | return PTR_ERR(map); |
| 10370 | } |
| 10371 | |
| 10372 | err = check_map_prog_compatibility(env, map, env->prog); |
| 10373 | if (err) { |
| 10374 | fdput(f); |
| 10375 | return err; |
| 10376 | } |
| 10377 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10378 | aux = &env->insn_aux_data[i]; |
| 10379 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 10380 | addr = (unsigned long)map; |
| 10381 | } else { |
| 10382 | u32 off = insn[1].imm; |
| 10383 | |
| 10384 | if (off >= BPF_MAX_VAR_OFF) { |
| 10385 | verbose(env, "direct value offset of %u is not allowed\n", off); |
| 10386 | fdput(f); |
| 10387 | return -EINVAL; |
| 10388 | } |
| 10389 | |
| 10390 | if (!map->ops->map_direct_value_addr) { |
| 10391 | verbose(env, "no direct value access support for this map type\n"); |
| 10392 | fdput(f); |
| 10393 | return -EINVAL; |
| 10394 | } |
| 10395 | |
| 10396 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 10397 | if (err) { |
| 10398 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", |
| 10399 | map->value_size, off); |
| 10400 | fdput(f); |
| 10401 | return err; |
| 10402 | } |
| 10403 | |
| 10404 | aux->map_off = off; |
| 10405 | addr += off; |
| 10406 | } |
| 10407 | |
| 10408 | insn[0].imm = (u32)addr; |
| 10409 | insn[1].imm = addr >> 32; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10410 | |
| 10411 | /* check whether we recorded this map already */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10412 | for (j = 0; j < env->used_map_cnt; j++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10413 | if (env->used_maps[j] == map) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10414 | aux->map_index = j; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10415 | fdput(f); |
| 10416 | goto next_insn; |
| 10417 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10418 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10419 | |
| 10420 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 10421 | fdput(f); |
| 10422 | return -E2BIG; |
| 10423 | } |
| 10424 | |
| 10425 | /* hold the map. If the program is rejected by verifier, |
| 10426 | * the map will be released by release_maps() or it |
| 10427 | * will be used by the valid program until it's unloaded |
| 10428 | * and all maps are released in free_used_maps() |
| 10429 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10430 | bpf_map_inc(map); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10431 | |
| 10432 | aux->map_index = env->used_map_cnt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10433 | env->used_maps[env->used_map_cnt++] = map; |
| 10434 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10435 | if (bpf_map_is_cgroup_storage(map) && |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10436 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10437 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10438 | fdput(f); |
| 10439 | return -EBUSY; |
| 10440 | } |
| 10441 | |
| 10442 | fdput(f); |
| 10443 | next_insn: |
| 10444 | insn++; |
| 10445 | i++; |
| 10446 | continue; |
| 10447 | } |
| 10448 | |
| 10449 | /* Basic sanity check before we invest more work here. */ |
| 10450 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 10451 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 10452 | return -EINVAL; |
| 10453 | } |
| 10454 | } |
| 10455 | |
| 10456 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 10457 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 10458 | * These pointers will be used later by verifier to validate map access. |
| 10459 | */ |
| 10460 | return 0; |
| 10461 | } |
| 10462 | |
| 10463 | /* drop refcnt of maps used by the rejected program */ |
| 10464 | static void release_maps(struct bpf_verifier_env *env) |
| 10465 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10466 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
| 10467 | env->used_map_cnt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10468 | } |
| 10469 | |
| 10470 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
| 10471 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
| 10472 | { |
| 10473 | struct bpf_insn *insn = env->prog->insnsi; |
| 10474 | int insn_cnt = env->prog->len; |
| 10475 | int i; |
| 10476 | |
| 10477 | for (i = 0; i < insn_cnt; i++, insn++) |
| 10478 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 10479 | insn->src_reg = 0; |
| 10480 | } |
| 10481 | |
| 10482 | /* single env->prog->insni[off] instruction was replaced with the range |
| 10483 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 10484 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 10485 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10486 | static void adjust_insn_aux_data(struct bpf_verifier_env *env, |
| 10487 | struct bpf_insn_aux_data *new_data, |
| 10488 | struct bpf_prog *new_prog, u32 off, u32 cnt) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10489 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10490 | struct bpf_insn_aux_data *old_data = env->insn_aux_data; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10491 | struct bpf_insn *insn = new_prog->insnsi; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10492 | u32 old_seen = old_data[off].seen; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10493 | u32 prog_len; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10494 | int i; |
| 10495 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10496 | /* aux info at OFF always needs adjustment, no matter fast path |
| 10497 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the |
| 10498 | * original insn at old prog. |
| 10499 | */ |
| 10500 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); |
| 10501 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10502 | if (cnt == 1) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10503 | return; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10504 | prog_len = new_prog->len; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10505 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10506 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 10507 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 10508 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10509 | for (i = off; i < off + cnt - 1; i++) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10510 | /* Expand insni[off]'s seen count to the patched range. */ |
| 10511 | new_data[i].seen = old_seen; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10512 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
| 10513 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10514 | env->insn_aux_data = new_data; |
| 10515 | vfree(old_data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10516 | } |
| 10517 | |
| 10518 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 10519 | { |
| 10520 | int i; |
| 10521 | |
| 10522 | if (len == 1) |
| 10523 | return; |
| 10524 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 10525 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 10526 | if (env->subprog_info[i].start <= off) |
| 10527 | continue; |
| 10528 | env->subprog_info[i].start += len - 1; |
| 10529 | } |
| 10530 | } |
| 10531 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10532 | static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len) |
| 10533 | { |
| 10534 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; |
| 10535 | int i, sz = prog->aux->size_poke_tab; |
| 10536 | struct bpf_jit_poke_descriptor *desc; |
| 10537 | |
| 10538 | for (i = 0; i < sz; i++) { |
| 10539 | desc = &tab[i]; |
| 10540 | if (desc->insn_idx <= off) |
| 10541 | continue; |
| 10542 | desc->insn_idx += len - 1; |
| 10543 | } |
| 10544 | } |
| 10545 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10546 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 10547 | const struct bpf_insn *patch, u32 len) |
| 10548 | { |
| 10549 | struct bpf_prog *new_prog; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10550 | struct bpf_insn_aux_data *new_data = NULL; |
| 10551 | |
| 10552 | if (len > 1) { |
| 10553 | new_data = vzalloc(array_size(env->prog->len + len - 1, |
| 10554 | sizeof(struct bpf_insn_aux_data))); |
| 10555 | if (!new_data) |
| 10556 | return NULL; |
| 10557 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10558 | |
| 10559 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10560 | if (IS_ERR(new_prog)) { |
| 10561 | if (PTR_ERR(new_prog) == -ERANGE) |
| 10562 | verbose(env, |
| 10563 | "insn %d cannot be patched due to 16-bit range\n", |
| 10564 | env->insn_aux_data[off].orig_idx); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10565 | vfree(new_data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10566 | return NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10567 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10568 | adjust_insn_aux_data(env, new_data, new_prog, off, len); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10569 | adjust_subprog_starts(env, off, len); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10570 | adjust_poke_descs(new_prog, off, len); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10571 | return new_prog; |
| 10572 | } |
| 10573 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10574 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
| 10575 | u32 off, u32 cnt) |
| 10576 | { |
| 10577 | int i, j; |
| 10578 | |
| 10579 | /* find first prog starting at or after off (first to remove) */ |
| 10580 | for (i = 0; i < env->subprog_cnt; i++) |
| 10581 | if (env->subprog_info[i].start >= off) |
| 10582 | break; |
| 10583 | /* find first prog starting at or after off + cnt (first to stay) */ |
| 10584 | for (j = i; j < env->subprog_cnt; j++) |
| 10585 | if (env->subprog_info[j].start >= off + cnt) |
| 10586 | break; |
| 10587 | /* if j doesn't start exactly at off + cnt, we are just removing |
| 10588 | * the front of previous prog |
| 10589 | */ |
| 10590 | if (env->subprog_info[j].start != off + cnt) |
| 10591 | j--; |
| 10592 | |
| 10593 | if (j > i) { |
| 10594 | struct bpf_prog_aux *aux = env->prog->aux; |
| 10595 | int move; |
| 10596 | |
| 10597 | /* move fake 'exit' subprog as well */ |
| 10598 | move = env->subprog_cnt + 1 - j; |
| 10599 | |
| 10600 | memmove(env->subprog_info + i, |
| 10601 | env->subprog_info + j, |
| 10602 | sizeof(*env->subprog_info) * move); |
| 10603 | env->subprog_cnt -= j - i; |
| 10604 | |
| 10605 | /* remove func_info */ |
| 10606 | if (aux->func_info) { |
| 10607 | move = aux->func_info_cnt - j; |
| 10608 | |
| 10609 | memmove(aux->func_info + i, |
| 10610 | aux->func_info + j, |
| 10611 | sizeof(*aux->func_info) * move); |
| 10612 | aux->func_info_cnt -= j - i; |
| 10613 | /* func_info->insn_off is set after all code rewrites, |
| 10614 | * in adjust_btf_func() - no need to adjust |
| 10615 | */ |
| 10616 | } |
| 10617 | } else { |
| 10618 | /* convert i from "first prog to remove" to "first to adjust" */ |
| 10619 | if (env->subprog_info[i].start == off) |
| 10620 | i++; |
| 10621 | } |
| 10622 | |
| 10623 | /* update fake 'exit' subprog as well */ |
| 10624 | for (; i <= env->subprog_cnt; i++) |
| 10625 | env->subprog_info[i].start -= cnt; |
| 10626 | |
| 10627 | return 0; |
| 10628 | } |
| 10629 | |
| 10630 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, |
| 10631 | u32 cnt) |
| 10632 | { |
| 10633 | struct bpf_prog *prog = env->prog; |
| 10634 | u32 i, l_off, l_cnt, nr_linfo; |
| 10635 | struct bpf_line_info *linfo; |
| 10636 | |
| 10637 | nr_linfo = prog->aux->nr_linfo; |
| 10638 | if (!nr_linfo) |
| 10639 | return 0; |
| 10640 | |
| 10641 | linfo = prog->aux->linfo; |
| 10642 | |
| 10643 | /* find first line info to remove, count lines to be removed */ |
| 10644 | for (i = 0; i < nr_linfo; i++) |
| 10645 | if (linfo[i].insn_off >= off) |
| 10646 | break; |
| 10647 | |
| 10648 | l_off = i; |
| 10649 | l_cnt = 0; |
| 10650 | for (; i < nr_linfo; i++) |
| 10651 | if (linfo[i].insn_off < off + cnt) |
| 10652 | l_cnt++; |
| 10653 | else |
| 10654 | break; |
| 10655 | |
| 10656 | /* First live insn doesn't match first live linfo, it needs to "inherit" |
| 10657 | * last removed linfo. prog is already modified, so prog->len == off |
| 10658 | * means no live instructions after (tail of the program was removed). |
| 10659 | */ |
| 10660 | if (prog->len != off && l_cnt && |
| 10661 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { |
| 10662 | l_cnt--; |
| 10663 | linfo[--i].insn_off = off + cnt; |
| 10664 | } |
| 10665 | |
| 10666 | /* remove the line info which refer to the removed instructions */ |
| 10667 | if (l_cnt) { |
| 10668 | memmove(linfo + l_off, linfo + i, |
| 10669 | sizeof(*linfo) * (nr_linfo - i)); |
| 10670 | |
| 10671 | prog->aux->nr_linfo -= l_cnt; |
| 10672 | nr_linfo = prog->aux->nr_linfo; |
| 10673 | } |
| 10674 | |
| 10675 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ |
| 10676 | for (i = l_off; i < nr_linfo; i++) |
| 10677 | linfo[i].insn_off -= cnt; |
| 10678 | |
| 10679 | /* fix up all subprogs (incl. 'exit') which start >= off */ |
| 10680 | for (i = 0; i <= env->subprog_cnt; i++) |
| 10681 | if (env->subprog_info[i].linfo_idx > l_off) { |
| 10682 | /* program may have started in the removed region but |
| 10683 | * may not be fully removed |
| 10684 | */ |
| 10685 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) |
| 10686 | env->subprog_info[i].linfo_idx -= l_cnt; |
| 10687 | else |
| 10688 | env->subprog_info[i].linfo_idx = l_off; |
| 10689 | } |
| 10690 | |
| 10691 | return 0; |
| 10692 | } |
| 10693 | |
| 10694 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) |
| 10695 | { |
| 10696 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 10697 | unsigned int orig_prog_len = env->prog->len; |
| 10698 | int err; |
| 10699 | |
| 10700 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 10701 | bpf_prog_offload_remove_insns(env, off, cnt); |
| 10702 | |
| 10703 | err = bpf_remove_insns(env->prog, off, cnt); |
| 10704 | if (err) |
| 10705 | return err; |
| 10706 | |
| 10707 | err = adjust_subprog_starts_after_remove(env, off, cnt); |
| 10708 | if (err) |
| 10709 | return err; |
| 10710 | |
| 10711 | err = bpf_adj_linfo_after_remove(env, off, cnt); |
| 10712 | if (err) |
| 10713 | return err; |
| 10714 | |
| 10715 | memmove(aux_data + off, aux_data + off + cnt, |
| 10716 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); |
| 10717 | |
| 10718 | return 0; |
| 10719 | } |
| 10720 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10721 | /* The verifier does more data flow analysis than llvm and will not |
| 10722 | * explore branches that are dead at run time. Malicious programs can |
| 10723 | * have dead code too. Therefore replace all dead at-run-time code |
| 10724 | * with 'ja -1'. |
| 10725 | * |
| 10726 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 10727 | * program and through another bug we would manage to jump there, then |
| 10728 | * we'd execute beyond program memory otherwise. Returning exception |
| 10729 | * code also wouldn't work since we can have subprogs where the dead |
| 10730 | * code could be located. |
| 10731 | */ |
| 10732 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 10733 | { |
| 10734 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 10735 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
| 10736 | struct bpf_insn *insn = env->prog->insnsi; |
| 10737 | const int insn_cnt = env->prog->len; |
| 10738 | int i; |
| 10739 | |
| 10740 | for (i = 0; i < insn_cnt; i++) { |
| 10741 | if (aux_data[i].seen) |
| 10742 | continue; |
| 10743 | memcpy(insn + i, &trap, sizeof(trap)); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10744 | aux_data[i].zext_dst = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10745 | } |
| 10746 | } |
| 10747 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10748 | static bool insn_is_cond_jump(u8 code) |
| 10749 | { |
| 10750 | u8 op; |
| 10751 | |
| 10752 | if (BPF_CLASS(code) == BPF_JMP32) |
| 10753 | return true; |
| 10754 | |
| 10755 | if (BPF_CLASS(code) != BPF_JMP) |
| 10756 | return false; |
| 10757 | |
| 10758 | op = BPF_OP(code); |
| 10759 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; |
| 10760 | } |
| 10761 | |
| 10762 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) |
| 10763 | { |
| 10764 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 10765 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 10766 | struct bpf_insn *insn = env->prog->insnsi; |
| 10767 | const int insn_cnt = env->prog->len; |
| 10768 | int i; |
| 10769 | |
| 10770 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 10771 | if (!insn_is_cond_jump(insn->code)) |
| 10772 | continue; |
| 10773 | |
| 10774 | if (!aux_data[i + 1].seen) |
| 10775 | ja.off = insn->off; |
| 10776 | else if (!aux_data[i + 1 + insn->off].seen) |
| 10777 | ja.off = 0; |
| 10778 | else |
| 10779 | continue; |
| 10780 | |
| 10781 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 10782 | bpf_prog_offload_replace_insn(env, i, &ja); |
| 10783 | |
| 10784 | memcpy(insn, &ja, sizeof(ja)); |
| 10785 | } |
| 10786 | } |
| 10787 | |
| 10788 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
| 10789 | { |
| 10790 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 10791 | int insn_cnt = env->prog->len; |
| 10792 | int i, err; |
| 10793 | |
| 10794 | for (i = 0; i < insn_cnt; i++) { |
| 10795 | int j; |
| 10796 | |
| 10797 | j = 0; |
| 10798 | while (i + j < insn_cnt && !aux_data[i + j].seen) |
| 10799 | j++; |
| 10800 | if (!j) |
| 10801 | continue; |
| 10802 | |
| 10803 | err = verifier_remove_insns(env, i, j); |
| 10804 | if (err) |
| 10805 | return err; |
| 10806 | insn_cnt = env->prog->len; |
| 10807 | } |
| 10808 | |
| 10809 | return 0; |
| 10810 | } |
| 10811 | |
| 10812 | static int opt_remove_nops(struct bpf_verifier_env *env) |
| 10813 | { |
| 10814 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 10815 | struct bpf_insn *insn = env->prog->insnsi; |
| 10816 | int insn_cnt = env->prog->len; |
| 10817 | int i, err; |
| 10818 | |
| 10819 | for (i = 0; i < insn_cnt; i++) { |
| 10820 | if (memcmp(&insn[i], &ja, sizeof(ja))) |
| 10821 | continue; |
| 10822 | |
| 10823 | err = verifier_remove_insns(env, i, 1); |
| 10824 | if (err) |
| 10825 | return err; |
| 10826 | insn_cnt--; |
| 10827 | i--; |
| 10828 | } |
| 10829 | |
| 10830 | return 0; |
| 10831 | } |
| 10832 | |
| 10833 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
| 10834 | const union bpf_attr *attr) |
| 10835 | { |
| 10836 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
| 10837 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
| 10838 | int i, patch_len, delta = 0, len = env->prog->len; |
| 10839 | struct bpf_insn *insns = env->prog->insnsi; |
| 10840 | struct bpf_prog *new_prog; |
| 10841 | bool rnd_hi32; |
| 10842 | |
| 10843 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
| 10844 | zext_patch[1] = BPF_ZEXT_REG(0); |
| 10845 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
| 10846 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); |
| 10847 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); |
| 10848 | for (i = 0; i < len; i++) { |
| 10849 | int adj_idx = i + delta; |
| 10850 | struct bpf_insn insn; |
| 10851 | |
| 10852 | insn = insns[adj_idx]; |
| 10853 | if (!aux[adj_idx].zext_dst) { |
| 10854 | u8 code, class; |
| 10855 | u32 imm_rnd; |
| 10856 | |
| 10857 | if (!rnd_hi32) |
| 10858 | continue; |
| 10859 | |
| 10860 | code = insn.code; |
| 10861 | class = BPF_CLASS(code); |
| 10862 | if (insn_no_def(&insn)) |
| 10863 | continue; |
| 10864 | |
| 10865 | /* NOTE: arg "reg" (the fourth one) is only used for |
| 10866 | * BPF_STX which has been ruled out in above |
| 10867 | * check, it is safe to pass NULL here. |
| 10868 | */ |
| 10869 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { |
| 10870 | if (class == BPF_LD && |
| 10871 | BPF_MODE(code) == BPF_IMM) |
| 10872 | i++; |
| 10873 | continue; |
| 10874 | } |
| 10875 | |
| 10876 | /* ctx load could be transformed into wider load. */ |
| 10877 | if (class == BPF_LDX && |
| 10878 | aux[adj_idx].ptr_type == PTR_TO_CTX) |
| 10879 | continue; |
| 10880 | |
| 10881 | imm_rnd = get_random_int(); |
| 10882 | rnd_hi32_patch[0] = insn; |
| 10883 | rnd_hi32_patch[1].imm = imm_rnd; |
| 10884 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; |
| 10885 | patch = rnd_hi32_patch; |
| 10886 | patch_len = 4; |
| 10887 | goto apply_patch_buffer; |
| 10888 | } |
| 10889 | |
| 10890 | if (!bpf_jit_needs_zext()) |
| 10891 | continue; |
| 10892 | |
| 10893 | zext_patch[0] = insn; |
| 10894 | zext_patch[1].dst_reg = insn.dst_reg; |
| 10895 | zext_patch[1].src_reg = insn.dst_reg; |
| 10896 | patch = zext_patch; |
| 10897 | patch_len = 2; |
| 10898 | apply_patch_buffer: |
| 10899 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); |
| 10900 | if (!new_prog) |
| 10901 | return -ENOMEM; |
| 10902 | env->prog = new_prog; |
| 10903 | insns = new_prog->insnsi; |
| 10904 | aux = env->insn_aux_data; |
| 10905 | delta += patch_len - 1; |
| 10906 | } |
| 10907 | |
| 10908 | return 0; |
| 10909 | } |
| 10910 | |
| 10911 | /* convert load instructions that access fields of a context type into a |
| 10912 | * sequence of instructions that access fields of the underlying structure: |
| 10913 | * struct __sk_buff -> struct sk_buff |
| 10914 | * struct bpf_sock_ops -> struct sock |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10915 | */ |
| 10916 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
| 10917 | { |
| 10918 | const struct bpf_verifier_ops *ops = env->ops; |
| 10919 | int i, cnt, size, ctx_field_size, delta = 0; |
| 10920 | const int insn_cnt = env->prog->len; |
| 10921 | struct bpf_insn insn_buf[16], *insn; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10922 | u32 target_size, size_default, off; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10923 | struct bpf_prog *new_prog; |
| 10924 | enum bpf_access_type type; |
| 10925 | bool is_narrower_load; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10926 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10927 | if (ops->gen_prologue || env->seen_direct_write) { |
| 10928 | if (!ops->gen_prologue) { |
| 10929 | verbose(env, "bpf verifier is misconfigured\n"); |
| 10930 | return -EINVAL; |
| 10931 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10932 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 10933 | env->prog); |
| 10934 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
| 10935 | verbose(env, "bpf verifier is misconfigured\n"); |
| 10936 | return -EINVAL; |
| 10937 | } else if (cnt) { |
| 10938 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
| 10939 | if (!new_prog) |
| 10940 | return -ENOMEM; |
| 10941 | |
| 10942 | env->prog = new_prog; |
| 10943 | delta += cnt - 1; |
| 10944 | } |
| 10945 | } |
| 10946 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10947 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10948 | return 0; |
| 10949 | |
| 10950 | insn = env->prog->insnsi + delta; |
| 10951 | |
| 10952 | for (i = 0; i < insn_cnt; i++, insn++) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10953 | bpf_convert_ctx_access_t convert_ctx_access; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10954 | bool ctx_access; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10955 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10956 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 10957 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 10958 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10959 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10960 | type = BPF_READ; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10961 | ctx_access = true; |
| 10962 | } else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 10963 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 10964 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
| 10965 | insn->code == (BPF_STX | BPF_MEM | BPF_DW) || |
| 10966 | insn->code == (BPF_ST | BPF_MEM | BPF_B) || |
| 10967 | insn->code == (BPF_ST | BPF_MEM | BPF_H) || |
| 10968 | insn->code == (BPF_ST | BPF_MEM | BPF_W) || |
| 10969 | insn->code == (BPF_ST | BPF_MEM | BPF_DW)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10970 | type = BPF_WRITE; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10971 | ctx_access = BPF_CLASS(insn->code) == BPF_STX; |
| 10972 | } else { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10973 | continue; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10974 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10975 | |
| 10976 | if (type == BPF_WRITE && |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10977 | env->insn_aux_data[i + delta].sanitize_stack_spill) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10978 | struct bpf_insn patch[] = { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10979 | *insn, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10980 | BPF_ST_NOSPEC(), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10981 | }; |
| 10982 | |
| 10983 | cnt = ARRAY_SIZE(patch); |
| 10984 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 10985 | if (!new_prog) |
| 10986 | return -ENOMEM; |
| 10987 | |
| 10988 | delta += cnt - 1; |
| 10989 | env->prog = new_prog; |
| 10990 | insn = new_prog->insnsi + i + delta; |
| 10991 | continue; |
| 10992 | } |
| 10993 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 10994 | if (!ctx_access) |
| 10995 | continue; |
| 10996 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10997 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 10998 | case PTR_TO_CTX: |
| 10999 | if (!ops->convert_ctx_access) |
| 11000 | continue; |
| 11001 | convert_ctx_access = ops->convert_ctx_access; |
| 11002 | break; |
| 11003 | case PTR_TO_SOCKET: |
| 11004 | case PTR_TO_SOCK_COMMON: |
| 11005 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 11006 | break; |
| 11007 | case PTR_TO_TCP_SOCK: |
| 11008 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; |
| 11009 | break; |
| 11010 | case PTR_TO_XDP_SOCK: |
| 11011 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; |
| 11012 | break; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11013 | case PTR_TO_BTF_ID: |
| 11014 | if (type == BPF_READ) { |
| 11015 | insn->code = BPF_LDX | BPF_PROBE_MEM | |
| 11016 | BPF_SIZE((insn)->code); |
| 11017 | env->prog->aux->num_exentries++; |
| 11018 | } else if (resolve_prog_type(env->prog) != BPF_PROG_TYPE_STRUCT_OPS) { |
| 11019 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
| 11020 | return -EINVAL; |
| 11021 | } |
| 11022 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11023 | default: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11024 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11025 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11026 | |
| 11027 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
| 11028 | size = BPF_LDST_BYTES(insn); |
| 11029 | |
| 11030 | /* If the read access is a narrower load of the field, |
| 11031 | * convert to a 4/8-byte load, to minimum program type specific |
| 11032 | * convert_ctx_access changes. If conversion is successful, |
| 11033 | * we will apply proper mask to the result. |
| 11034 | */ |
| 11035 | is_narrower_load = size < ctx_field_size; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11036 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
| 11037 | off = insn->off; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11038 | if (is_narrower_load) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11039 | u8 size_code; |
| 11040 | |
| 11041 | if (type == BPF_WRITE) { |
| 11042 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
| 11043 | return -EINVAL; |
| 11044 | } |
| 11045 | |
| 11046 | size_code = BPF_H; |
| 11047 | if (ctx_field_size == 4) |
| 11048 | size_code = BPF_W; |
| 11049 | else if (ctx_field_size == 8) |
| 11050 | size_code = BPF_DW; |
| 11051 | |
| 11052 | insn->off = off & ~(size_default - 1); |
| 11053 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 11054 | } |
| 11055 | |
| 11056 | target_size = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11057 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 11058 | &target_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11059 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 11060 | (ctx_field_size && !target_size)) { |
| 11061 | verbose(env, "bpf verifier is misconfigured\n"); |
| 11062 | return -EINVAL; |
| 11063 | } |
| 11064 | |
| 11065 | if (is_narrower_load && size < target_size) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11066 | u8 shift = bpf_ctx_narrow_access_offset( |
| 11067 | off, size, size_default) * 8; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11068 | if (shift && cnt + 1 >= ARRAY_SIZE(insn_buf)) { |
| 11069 | verbose(env, "bpf verifier narrow ctx load misconfigured\n"); |
| 11070 | return -EINVAL; |
| 11071 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11072 | if (ctx_field_size <= 4) { |
| 11073 | if (shift) |
| 11074 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, |
| 11075 | insn->dst_reg, |
| 11076 | shift); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11077 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
| 11078 | (1 << size * 8) - 1); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11079 | } else { |
| 11080 | if (shift) |
| 11081 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, |
| 11082 | insn->dst_reg, |
| 11083 | shift); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11084 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11085 | (1ULL << size * 8) - 1); |
| 11086 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11087 | } |
| 11088 | |
| 11089 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 11090 | if (!new_prog) |
| 11091 | return -ENOMEM; |
| 11092 | |
| 11093 | delta += cnt - 1; |
| 11094 | |
| 11095 | /* keep walking new program and skip insns we just inserted */ |
| 11096 | env->prog = new_prog; |
| 11097 | insn = new_prog->insnsi + i + delta; |
| 11098 | } |
| 11099 | |
| 11100 | return 0; |
| 11101 | } |
| 11102 | |
| 11103 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 11104 | { |
| 11105 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 11106 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11107 | struct bpf_map *map_ptr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11108 | struct bpf_insn *insn; |
| 11109 | void *old_bpf_func; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11110 | int err, num_exentries; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11111 | |
| 11112 | if (env->subprog_cnt <= 1) |
| 11113 | return 0; |
| 11114 | |
| 11115 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 11116 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 11117 | insn->src_reg != BPF_PSEUDO_CALL) |
| 11118 | continue; |
| 11119 | /* Upon error here we cannot fall back to interpreter but |
| 11120 | * need a hard reject of the program. Thus -EFAULT is |
| 11121 | * propagated in any case. |
| 11122 | */ |
| 11123 | subprog = find_subprog(env, i + insn->imm + 1); |
| 11124 | if (subprog < 0) { |
| 11125 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 11126 | i + insn->imm + 1); |
| 11127 | return -EFAULT; |
| 11128 | } |
| 11129 | /* temporarily remember subprog id inside insn instead of |
| 11130 | * aux_data, since next loop will split up all insns into funcs |
| 11131 | */ |
| 11132 | insn->off = subprog; |
| 11133 | /* remember original imm in case JIT fails and fallback |
| 11134 | * to interpreter will be needed |
| 11135 | */ |
| 11136 | env->insn_aux_data[i].call_imm = insn->imm; |
| 11137 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 11138 | insn->imm = 1; |
| 11139 | } |
| 11140 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11141 | err = bpf_prog_alloc_jited_linfo(prog); |
| 11142 | if (err) |
| 11143 | goto out_undo_insn; |
| 11144 | |
| 11145 | err = -ENOMEM; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11146 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
| 11147 | if (!func) |
| 11148 | goto out_undo_insn; |
| 11149 | |
| 11150 | for (i = 0; i < env->subprog_cnt; i++) { |
| 11151 | subprog_start = subprog_end; |
| 11152 | subprog_end = env->subprog_info[i + 1].start; |
| 11153 | |
| 11154 | len = subprog_end - subprog_start; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11155 | /* BPF_PROG_RUN doesn't call subprogs directly, |
| 11156 | * hence main prog stats include the runtime of subprogs. |
| 11157 | * subprogs don't have IDs and not reachable via prog_get_next_id |
| 11158 | * func[i]->aux->stats will never be accessed and stays NULL |
| 11159 | */ |
| 11160 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11161 | if (!func[i]) |
| 11162 | goto out_free; |
| 11163 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 11164 | len * sizeof(struct bpf_insn)); |
| 11165 | func[i]->type = prog->type; |
| 11166 | func[i]->len = len; |
| 11167 | if (bpf_prog_calc_tag(func[i])) |
| 11168 | goto out_free; |
| 11169 | func[i]->is_func = 1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11170 | func[i]->aux->func_idx = i; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11171 | /* Below members will be freed only at prog->aux */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11172 | func[i]->aux->btf = prog->aux->btf; |
| 11173 | func[i]->aux->func_info = prog->aux->func_info; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11174 | func[i]->aux->poke_tab = prog->aux->poke_tab; |
| 11175 | func[i]->aux->size_poke_tab = prog->aux->size_poke_tab; |
| 11176 | |
| 11177 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
| 11178 | struct bpf_jit_poke_descriptor *poke; |
| 11179 | |
| 11180 | poke = &prog->aux->poke_tab[j]; |
| 11181 | if (poke->insn_idx < subprog_end && |
| 11182 | poke->insn_idx >= subprog_start) |
| 11183 | poke->aux = func[i]->aux; |
| 11184 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11185 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11186 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 11187 | * Long term would need debug info to populate names |
| 11188 | */ |
| 11189 | func[i]->aux->name[0] = 'F'; |
| 11190 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
| 11191 | func[i]->jit_requested = 1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11192 | func[i]->aux->linfo = prog->aux->linfo; |
| 11193 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; |
| 11194 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; |
| 11195 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11196 | num_exentries = 0; |
| 11197 | insn = func[i]->insnsi; |
| 11198 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 11199 | if (BPF_CLASS(insn->code) == BPF_LDX && |
| 11200 | BPF_MODE(insn->code) == BPF_PROBE_MEM) |
| 11201 | num_exentries++; |
| 11202 | } |
| 11203 | func[i]->aux->num_exentries = num_exentries; |
| 11204 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11205 | func[i] = bpf_int_jit_compile(func[i]); |
| 11206 | if (!func[i]->jited) { |
| 11207 | err = -ENOTSUPP; |
| 11208 | goto out_free; |
| 11209 | } |
| 11210 | cond_resched(); |
| 11211 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11212 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11213 | /* at this point all bpf functions were successfully JITed |
| 11214 | * now populate all bpf_calls with correct addresses and |
| 11215 | * run last pass of JIT |
| 11216 | */ |
| 11217 | for (i = 0; i < env->subprog_cnt; i++) { |
| 11218 | insn = func[i]->insnsi; |
| 11219 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 11220 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 11221 | insn->src_reg != BPF_PSEUDO_CALL) |
| 11222 | continue; |
| 11223 | subprog = insn->off; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11224 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
| 11225 | __bpf_call_base; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11226 | } |
| 11227 | |
| 11228 | /* we use the aux data to keep a list of the start addresses |
| 11229 | * of the JITed images for each function in the program |
| 11230 | * |
| 11231 | * for some architectures, such as powerpc64, the imm field |
| 11232 | * might not be large enough to hold the offset of the start |
| 11233 | * address of the callee's JITed image from __bpf_call_base |
| 11234 | * |
| 11235 | * in such cases, we can lookup the start address of a callee |
| 11236 | * by using its subprog id, available from the off field of |
| 11237 | * the call instruction, as an index for this list |
| 11238 | */ |
| 11239 | func[i]->aux->func = func; |
| 11240 | func[i]->aux->func_cnt = env->subprog_cnt; |
| 11241 | } |
| 11242 | for (i = 0; i < env->subprog_cnt; i++) { |
| 11243 | old_bpf_func = func[i]->bpf_func; |
| 11244 | tmp = bpf_int_jit_compile(func[i]); |
| 11245 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 11246 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
| 11247 | err = -ENOTSUPP; |
| 11248 | goto out_free; |
| 11249 | } |
| 11250 | cond_resched(); |
| 11251 | } |
| 11252 | |
| 11253 | /* finally lock prog and jit images for all functions and |
| 11254 | * populate kallsysm |
| 11255 | */ |
| 11256 | for (i = 0; i < env->subprog_cnt; i++) { |
| 11257 | bpf_prog_lock_ro(func[i]); |
| 11258 | bpf_prog_kallsyms_add(func[i]); |
| 11259 | } |
| 11260 | |
| 11261 | /* Last step: make now unused interpreter insns from main |
| 11262 | * prog consistent for later dump requests, so they can |
| 11263 | * later look the same as if they were interpreted only. |
| 11264 | */ |
| 11265 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 11266 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 11267 | insn->src_reg != BPF_PSEUDO_CALL) |
| 11268 | continue; |
| 11269 | insn->off = env->insn_aux_data[i].call_imm; |
| 11270 | subprog = find_subprog(env, i + insn->off + 1); |
| 11271 | insn->imm = subprog; |
| 11272 | } |
| 11273 | |
| 11274 | prog->jited = 1; |
| 11275 | prog->bpf_func = func[0]->bpf_func; |
| 11276 | prog->aux->func = func; |
| 11277 | prog->aux->func_cnt = env->subprog_cnt; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11278 | bpf_prog_free_unused_jited_linfo(prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11279 | return 0; |
| 11280 | out_free: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11281 | /* We failed JIT'ing, so at this point we need to unregister poke |
| 11282 | * descriptors from subprogs, so that kernel is not attempting to |
| 11283 | * patch it anymore as we're freeing the subprog JIT memory. |
| 11284 | */ |
| 11285 | for (i = 0; i < prog->aux->size_poke_tab; i++) { |
| 11286 | map_ptr = prog->aux->poke_tab[i].tail_call.map; |
| 11287 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); |
| 11288 | } |
| 11289 | /* At this point we're guaranteed that poke descriptors are not |
| 11290 | * live anymore. We can just unlink its descriptor table as it's |
| 11291 | * released with the main prog. |
| 11292 | */ |
| 11293 | for (i = 0; i < env->subprog_cnt; i++) { |
| 11294 | if (!func[i]) |
| 11295 | continue; |
| 11296 | func[i]->aux->poke_tab = NULL; |
| 11297 | bpf_jit_free(func[i]); |
| 11298 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11299 | kfree(func); |
| 11300 | out_undo_insn: |
| 11301 | /* cleanup main prog to be interpreted */ |
| 11302 | prog->jit_requested = 0; |
| 11303 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 11304 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 11305 | insn->src_reg != BPF_PSEUDO_CALL) |
| 11306 | continue; |
| 11307 | insn->off = 0; |
| 11308 | insn->imm = env->insn_aux_data[i].call_imm; |
| 11309 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11310 | bpf_prog_free_jited_linfo(prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11311 | return err; |
| 11312 | } |
| 11313 | |
| 11314 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 11315 | { |
| 11316 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
| 11317 | struct bpf_prog *prog = env->prog; |
| 11318 | struct bpf_insn *insn = prog->insnsi; |
| 11319 | int i, depth; |
| 11320 | #endif |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11321 | int err = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11322 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11323 | if (env->prog->jit_requested && |
| 11324 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11325 | err = jit_subprogs(env); |
| 11326 | if (err == 0) |
| 11327 | return 0; |
| 11328 | if (err == -EFAULT) |
| 11329 | return err; |
| 11330 | } |
| 11331 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11332 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
| 11333 | /* When JIT fails the progs with bpf2bpf calls and tail_calls |
| 11334 | * have to be rejected, since interpreter doesn't support them yet. |
| 11335 | */ |
| 11336 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); |
| 11337 | return -EINVAL; |
| 11338 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11339 | for (i = 0; i < prog->len; i++, insn++) { |
| 11340 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 11341 | insn->src_reg != BPF_PSEUDO_CALL) |
| 11342 | continue; |
| 11343 | depth = get_callee_stack_depth(env, insn, i); |
| 11344 | if (depth < 0) |
| 11345 | return depth; |
| 11346 | bpf_patch_call_args(insn, depth); |
| 11347 | } |
| 11348 | err = 0; |
| 11349 | #endif |
| 11350 | return err; |
| 11351 | } |
| 11352 | |
| 11353 | /* fixup insn->imm field of bpf_call instructions |
| 11354 | * and inline eligible helpers as explicit sequence of BPF instructions |
| 11355 | * |
| 11356 | * this function is called after eBPF program passed verification |
| 11357 | */ |
| 11358 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
| 11359 | { |
| 11360 | struct bpf_prog *prog = env->prog; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11361 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11362 | struct bpf_insn *insn = prog->insnsi; |
| 11363 | const struct bpf_func_proto *fn; |
| 11364 | const int insn_cnt = prog->len; |
| 11365 | const struct bpf_map_ops *ops; |
| 11366 | struct bpf_insn_aux_data *aux; |
| 11367 | struct bpf_insn insn_buf[16]; |
| 11368 | struct bpf_prog *new_prog; |
| 11369 | struct bpf_map *map_ptr; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11370 | int i, ret, cnt, delta = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11371 | |
| 11372 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 11373 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 11374 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 11375 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
| 11376 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 11377 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11378 | bool isdiv = BPF_OP(insn->code) == BPF_DIV; |
| 11379 | struct bpf_insn *patchlet; |
| 11380 | struct bpf_insn chk_and_div[] = { |
| 11381 | /* [R,W]x div 0 -> 0 */ |
| 11382 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
| 11383 | BPF_JNE | BPF_K, insn->src_reg, |
| 11384 | 0, 2, 0), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11385 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 11386 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 11387 | *insn, |
| 11388 | }; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11389 | struct bpf_insn chk_and_mod[] = { |
| 11390 | /* [R,W]x mod 0 -> [R,W]x */ |
| 11391 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
| 11392 | BPF_JEQ | BPF_K, insn->src_reg, |
| 11393 | 0, 1 + (is64 ? 0 : 1), 0), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11394 | *insn, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11395 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 11396 | BPF_MOV32_REG(insn->dst_reg, insn->dst_reg), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11397 | }; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11398 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11399 | patchlet = isdiv ? chk_and_div : chk_and_mod; |
| 11400 | cnt = isdiv ? ARRAY_SIZE(chk_and_div) : |
| 11401 | ARRAY_SIZE(chk_and_mod) - (is64 ? 2 : 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11402 | |
| 11403 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
| 11404 | if (!new_prog) |
| 11405 | return -ENOMEM; |
| 11406 | |
| 11407 | delta += cnt - 1; |
| 11408 | env->prog = prog = new_prog; |
| 11409 | insn = new_prog->insnsi + i + delta; |
| 11410 | continue; |
| 11411 | } |
| 11412 | |
| 11413 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 11414 | (BPF_MODE(insn->code) == BPF_ABS || |
| 11415 | BPF_MODE(insn->code) == BPF_IND)) { |
| 11416 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 11417 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 11418 | verbose(env, "bpf verifier is misconfigured\n"); |
| 11419 | return -EINVAL; |
| 11420 | } |
| 11421 | |
| 11422 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 11423 | if (!new_prog) |
| 11424 | return -ENOMEM; |
| 11425 | |
| 11426 | delta += cnt - 1; |
| 11427 | env->prog = prog = new_prog; |
| 11428 | insn = new_prog->insnsi + i + delta; |
| 11429 | continue; |
| 11430 | } |
| 11431 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11432 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
| 11433 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { |
| 11434 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; |
| 11435 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; |
| 11436 | struct bpf_insn insn_buf[16]; |
| 11437 | struct bpf_insn *patch = &insn_buf[0]; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11438 | bool issrc, isneg, isimm; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11439 | u32 off_reg; |
| 11440 | |
| 11441 | aux = &env->insn_aux_data[i + delta]; |
| 11442 | if (!aux->alu_state || |
| 11443 | aux->alu_state == BPF_ALU_NON_POINTER) |
| 11444 | continue; |
| 11445 | |
| 11446 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; |
| 11447 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == |
| 11448 | BPF_ALU_SANITIZE_SRC; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11449 | isimm = aux->alu_state & BPF_ALU_IMMEDIATE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11450 | |
| 11451 | off_reg = issrc ? insn->src_reg : insn->dst_reg; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11452 | if (isimm) { |
| 11453 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11454 | } else { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11455 | if (isneg) |
| 11456 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 11457 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); |
| 11458 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); |
| 11459 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); |
| 11460 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); |
| 11461 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); |
| 11462 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11463 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11464 | if (!issrc) |
| 11465 | *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); |
| 11466 | insn->src_reg = BPF_REG_AX; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11467 | if (isneg) |
| 11468 | insn->code = insn->code == code_add ? |
| 11469 | code_sub : code_add; |
| 11470 | *patch++ = *insn; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 11471 | if (issrc && isneg && !isimm) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11472 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 11473 | cnt = patch - insn_buf; |
| 11474 | |
| 11475 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 11476 | if (!new_prog) |
| 11477 | return -ENOMEM; |
| 11478 | |
| 11479 | delta += cnt - 1; |
| 11480 | env->prog = prog = new_prog; |
| 11481 | insn = new_prog->insnsi + i + delta; |
| 11482 | continue; |
| 11483 | } |
| 11484 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11485 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 11486 | continue; |
| 11487 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 11488 | continue; |
| 11489 | |
| 11490 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 11491 | prog->dst_needed = 1; |
| 11492 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 11493 | bpf_user_rnd_init_once(); |
| 11494 | if (insn->imm == BPF_FUNC_override_return) |
| 11495 | prog->kprobe_override = 1; |
| 11496 | if (insn->imm == BPF_FUNC_tail_call) { |
| 11497 | /* If we tail call into other programs, we |
| 11498 | * cannot make any assumptions since they can |
| 11499 | * be replaced dynamically during runtime in |
| 11500 | * the program array. |
| 11501 | */ |
| 11502 | prog->cb_access = 1; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11503 | if (!allow_tail_call_in_subprogs(env)) |
| 11504 | prog->aux->stack_depth = MAX_BPF_STACK; |
| 11505 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11506 | |
| 11507 | /* mark bpf_tail_call as different opcode to avoid |
| 11508 | * conditional branch in the interpeter for every normal |
| 11509 | * call and to prevent accidental JITing by JIT compiler |
| 11510 | * that doesn't support bpf_tail_call yet |
| 11511 | */ |
| 11512 | insn->imm = 0; |
| 11513 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
| 11514 | |
| 11515 | aux = &env->insn_aux_data[i + delta]; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11516 | if (env->bpf_capable && !expect_blinding && |
| 11517 | prog->jit_requested && |
| 11518 | !bpf_map_key_poisoned(aux) && |
| 11519 | !bpf_map_ptr_poisoned(aux) && |
| 11520 | !bpf_map_ptr_unpriv(aux)) { |
| 11521 | struct bpf_jit_poke_descriptor desc = { |
| 11522 | .reason = BPF_POKE_REASON_TAIL_CALL, |
| 11523 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), |
| 11524 | .tail_call.key = bpf_map_key_immediate(aux), |
| 11525 | .insn_idx = i + delta, |
| 11526 | }; |
| 11527 | |
| 11528 | ret = bpf_jit_add_poke_descriptor(prog, &desc); |
| 11529 | if (ret < 0) { |
| 11530 | verbose(env, "adding tail call poke descriptor failed\n"); |
| 11531 | return ret; |
| 11532 | } |
| 11533 | |
| 11534 | insn->imm = ret + 1; |
| 11535 | continue; |
| 11536 | } |
| 11537 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11538 | if (!bpf_map_ptr_unpriv(aux)) |
| 11539 | continue; |
| 11540 | |
| 11541 | /* instead of changing every JIT dealing with tail_call |
| 11542 | * emit two extra insns: |
| 11543 | * if (index >= max_entries) goto out; |
| 11544 | * index &= array->index_mask; |
| 11545 | * to avoid out-of-bounds cpu speculation |
| 11546 | */ |
| 11547 | if (bpf_map_ptr_poisoned(aux)) { |
| 11548 | verbose(env, "tail_call abusing map_ptr\n"); |
| 11549 | return -EINVAL; |
| 11550 | } |
| 11551 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11552 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11553 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 11554 | map_ptr->max_entries, 2); |
| 11555 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 11556 | container_of(map_ptr, |
| 11557 | struct bpf_array, |
| 11558 | map)->index_mask); |
| 11559 | insn_buf[2] = *insn; |
| 11560 | cnt = 3; |
| 11561 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 11562 | if (!new_prog) |
| 11563 | return -ENOMEM; |
| 11564 | |
| 11565 | delta += cnt - 1; |
| 11566 | env->prog = prog = new_prog; |
| 11567 | insn = new_prog->insnsi + i + delta; |
| 11568 | continue; |
| 11569 | } |
| 11570 | |
| 11571 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
| 11572 | * and other inlining handlers are currently limited to 64 bit |
| 11573 | * only. |
| 11574 | */ |
| 11575 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
| 11576 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 11577 | insn->imm == BPF_FUNC_map_update_elem || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11578 | insn->imm == BPF_FUNC_map_delete_elem || |
| 11579 | insn->imm == BPF_FUNC_map_push_elem || |
| 11580 | insn->imm == BPF_FUNC_map_pop_elem || |
| 11581 | insn->imm == BPF_FUNC_map_peek_elem)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11582 | aux = &env->insn_aux_data[i + delta]; |
| 11583 | if (bpf_map_ptr_poisoned(aux)) |
| 11584 | goto patch_call_imm; |
| 11585 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11586 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11587 | ops = map_ptr->ops; |
| 11588 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 11589 | ops->map_gen_lookup) { |
| 11590 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11591 | if (cnt == -EOPNOTSUPP) |
| 11592 | goto patch_map_ops_generic; |
| 11593 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11594 | verbose(env, "bpf verifier is misconfigured\n"); |
| 11595 | return -EINVAL; |
| 11596 | } |
| 11597 | |
| 11598 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 11599 | insn_buf, cnt); |
| 11600 | if (!new_prog) |
| 11601 | return -ENOMEM; |
| 11602 | |
| 11603 | delta += cnt - 1; |
| 11604 | env->prog = prog = new_prog; |
| 11605 | insn = new_prog->insnsi + i + delta; |
| 11606 | continue; |
| 11607 | } |
| 11608 | |
| 11609 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 11610 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 11611 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 11612 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 11613 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 11614 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 11615 | u64 flags))NULL)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11616 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 11617 | (int (*)(struct bpf_map *map, void *value, |
| 11618 | u64 flags))NULL)); |
| 11619 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 11620 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 11621 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 11622 | (int (*)(struct bpf_map *map, void *value))NULL)); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11623 | patch_map_ops_generic: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11624 | switch (insn->imm) { |
| 11625 | case BPF_FUNC_map_lookup_elem: |
| 11626 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - |
| 11627 | __bpf_call_base; |
| 11628 | continue; |
| 11629 | case BPF_FUNC_map_update_elem: |
| 11630 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - |
| 11631 | __bpf_call_base; |
| 11632 | continue; |
| 11633 | case BPF_FUNC_map_delete_elem: |
| 11634 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - |
| 11635 | __bpf_call_base; |
| 11636 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11637 | case BPF_FUNC_map_push_elem: |
| 11638 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - |
| 11639 | __bpf_call_base; |
| 11640 | continue; |
| 11641 | case BPF_FUNC_map_pop_elem: |
| 11642 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - |
| 11643 | __bpf_call_base; |
| 11644 | continue; |
| 11645 | case BPF_FUNC_map_peek_elem: |
| 11646 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - |
| 11647 | __bpf_call_base; |
| 11648 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11649 | } |
| 11650 | |
| 11651 | goto patch_call_imm; |
| 11652 | } |
| 11653 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11654 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
| 11655 | insn->imm == BPF_FUNC_jiffies64) { |
| 11656 | struct bpf_insn ld_jiffies_addr[2] = { |
| 11657 | BPF_LD_IMM64(BPF_REG_0, |
| 11658 | (unsigned long)&jiffies), |
| 11659 | }; |
| 11660 | |
| 11661 | insn_buf[0] = ld_jiffies_addr[0]; |
| 11662 | insn_buf[1] = ld_jiffies_addr[1]; |
| 11663 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, |
| 11664 | BPF_REG_0, 0); |
| 11665 | cnt = 3; |
| 11666 | |
| 11667 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, |
| 11668 | cnt); |
| 11669 | if (!new_prog) |
| 11670 | return -ENOMEM; |
| 11671 | |
| 11672 | delta += cnt - 1; |
| 11673 | env->prog = prog = new_prog; |
| 11674 | insn = new_prog->insnsi + i + delta; |
| 11675 | continue; |
| 11676 | } |
| 11677 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11678 | patch_call_imm: |
| 11679 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
| 11680 | /* all functions that have prototype and verifier allowed |
| 11681 | * programs to call them, must be real in-kernel functions |
| 11682 | */ |
| 11683 | if (!fn->func) { |
| 11684 | verbose(env, |
| 11685 | "kernel subsystem misconfigured func %s#%d\n", |
| 11686 | func_id_name(insn->imm), insn->imm); |
| 11687 | return -EFAULT; |
| 11688 | } |
| 11689 | insn->imm = fn->func - __bpf_call_base; |
| 11690 | } |
| 11691 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11692 | /* Since poke tab is now finalized, publish aux to tracker. */ |
| 11693 | for (i = 0; i < prog->aux->size_poke_tab; i++) { |
| 11694 | map_ptr = prog->aux->poke_tab[i].tail_call.map; |
| 11695 | if (!map_ptr->ops->map_poke_track || |
| 11696 | !map_ptr->ops->map_poke_untrack || |
| 11697 | !map_ptr->ops->map_poke_run) { |
| 11698 | verbose(env, "bpf verifier is misconfigured\n"); |
| 11699 | return -EINVAL; |
| 11700 | } |
| 11701 | |
| 11702 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); |
| 11703 | if (ret < 0) { |
| 11704 | verbose(env, "tracking tail call prog failed\n"); |
| 11705 | return ret; |
| 11706 | } |
| 11707 | } |
| 11708 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11709 | return 0; |
| 11710 | } |
| 11711 | |
| 11712 | static void free_states(struct bpf_verifier_env *env) |
| 11713 | { |
| 11714 | struct bpf_verifier_state_list *sl, *sln; |
| 11715 | int i; |
| 11716 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11717 | sl = env->free_list; |
| 11718 | while (sl) { |
| 11719 | sln = sl->next; |
| 11720 | free_verifier_state(&sl->state, false); |
| 11721 | kfree(sl); |
| 11722 | sl = sln; |
| 11723 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11724 | env->free_list = NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11725 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11726 | if (!env->explored_states) |
| 11727 | return; |
| 11728 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11729 | for (i = 0; i < state_htab_size(env); i++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11730 | sl = env->explored_states[i]; |
| 11731 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11732 | while (sl) { |
| 11733 | sln = sl->next; |
| 11734 | free_verifier_state(&sl->state, false); |
| 11735 | kfree(sl); |
| 11736 | sl = sln; |
| 11737 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11738 | env->explored_states[i] = NULL; |
| 11739 | } |
| 11740 | } |
| 11741 | |
| 11742 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
| 11743 | { |
| 11744 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
| 11745 | struct bpf_verifier_state *state; |
| 11746 | struct bpf_reg_state *regs; |
| 11747 | int ret, i; |
| 11748 | |
| 11749 | env->prev_linfo = NULL; |
| 11750 | env->pass_cnt++; |
| 11751 | |
| 11752 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 11753 | if (!state) |
| 11754 | return -ENOMEM; |
| 11755 | state->curframe = 0; |
| 11756 | state->speculative = false; |
| 11757 | state->branches = 1; |
| 11758 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 11759 | if (!state->frame[0]) { |
| 11760 | kfree(state); |
| 11761 | return -ENOMEM; |
| 11762 | } |
| 11763 | env->cur_state = state; |
| 11764 | init_func_state(env, state->frame[0], |
| 11765 | BPF_MAIN_FUNC /* callsite */, |
| 11766 | 0 /* frameno */, |
| 11767 | subprog); |
| 11768 | |
| 11769 | regs = state->frame[state->curframe]->regs; |
| 11770 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
| 11771 | ret = btf_prepare_func_args(env, subprog, regs); |
| 11772 | if (ret) |
| 11773 | goto out; |
| 11774 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { |
| 11775 | if (regs[i].type == PTR_TO_CTX) |
| 11776 | mark_reg_known_zero(env, regs, i); |
| 11777 | else if (regs[i].type == SCALAR_VALUE) |
| 11778 | mark_reg_unknown(env, regs, i); |
| 11779 | } |
| 11780 | } else { |
| 11781 | /* 1st arg to a function */ |
| 11782 | regs[BPF_REG_1].type = PTR_TO_CTX; |
| 11783 | mark_reg_known_zero(env, regs, BPF_REG_1); |
| 11784 | ret = btf_check_func_arg_match(env, subprog, regs); |
| 11785 | if (ret == -EFAULT) |
| 11786 | /* unlikely verifier bug. abort. |
| 11787 | * ret == 0 and ret < 0 are sadly acceptable for |
| 11788 | * main() function due to backward compatibility. |
| 11789 | * Like socket filter program may be written as: |
| 11790 | * int bpf_prog(struct pt_regs *ctx) |
| 11791 | * and never dereference that ctx in the program. |
| 11792 | * 'struct pt_regs' is a type mismatch for socket |
| 11793 | * filter that should be using 'struct __sk_buff'. |
| 11794 | */ |
| 11795 | goto out; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11796 | } |
| 11797 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11798 | ret = do_check(env); |
| 11799 | out: |
| 11800 | /* check for NULL is necessary, since cur_state can be freed inside |
| 11801 | * do_check() under memory pressure. |
| 11802 | */ |
| 11803 | if (env->cur_state) { |
| 11804 | free_verifier_state(env->cur_state, true); |
| 11805 | env->cur_state = NULL; |
| 11806 | } |
| 11807 | while (!pop_stack(env, NULL, NULL, false)); |
| 11808 | if (!ret && pop_log) |
| 11809 | bpf_vlog_reset(&env->log, 0); |
| 11810 | free_states(env); |
| 11811 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11812 | } |
| 11813 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11814 | /* Verify all global functions in a BPF program one by one based on their BTF. |
| 11815 | * All global functions must pass verification. Otherwise the whole program is rejected. |
| 11816 | * Consider: |
| 11817 | * int bar(int); |
| 11818 | * int foo(int f) |
| 11819 | * { |
| 11820 | * return bar(f); |
| 11821 | * } |
| 11822 | * int bar(int b) |
| 11823 | * { |
| 11824 | * ... |
| 11825 | * } |
| 11826 | * foo() will be verified first for R1=any_scalar_value. During verification it |
| 11827 | * will be assumed that bar() already verified successfully and call to bar() |
| 11828 | * from foo() will be checked for type match only. Later bar() will be verified |
| 11829 | * independently to check that it's safe for R1=any_scalar_value. |
| 11830 | */ |
| 11831 | static int do_check_subprogs(struct bpf_verifier_env *env) |
| 11832 | { |
| 11833 | struct bpf_prog_aux *aux = env->prog->aux; |
| 11834 | int i, ret; |
| 11835 | |
| 11836 | if (!aux->func_info) |
| 11837 | return 0; |
| 11838 | |
| 11839 | for (i = 1; i < env->subprog_cnt; i++) { |
| 11840 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) |
| 11841 | continue; |
| 11842 | env->insn_idx = env->subprog_info[i].start; |
| 11843 | WARN_ON_ONCE(env->insn_idx == 0); |
| 11844 | ret = do_check_common(env, i); |
| 11845 | if (ret) { |
| 11846 | return ret; |
| 11847 | } else if (env->log.level & BPF_LOG_LEVEL) { |
| 11848 | verbose(env, |
| 11849 | "Func#%d is safe for any args that match its prototype\n", |
| 11850 | i); |
| 11851 | } |
| 11852 | } |
| 11853 | return 0; |
| 11854 | } |
| 11855 | |
| 11856 | static int do_check_main(struct bpf_verifier_env *env) |
| 11857 | { |
| 11858 | int ret; |
| 11859 | |
| 11860 | env->insn_idx = 0; |
| 11861 | ret = do_check_common(env, 0); |
| 11862 | if (!ret) |
| 11863 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
| 11864 | return ret; |
| 11865 | } |
| 11866 | |
| 11867 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11868 | static void print_verification_stats(struct bpf_verifier_env *env) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11869 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11870 | int i; |
| 11871 | |
| 11872 | if (env->log.level & BPF_LOG_STATS) { |
| 11873 | verbose(env, "verification time %lld usec\n", |
| 11874 | div_u64(env->verification_time, 1000)); |
| 11875 | verbose(env, "stack depth "); |
| 11876 | for (i = 0; i < env->subprog_cnt; i++) { |
| 11877 | u32 depth = env->subprog_info[i].stack_depth; |
| 11878 | |
| 11879 | verbose(env, "%d", depth); |
| 11880 | if (i + 1 < env->subprog_cnt) |
| 11881 | verbose(env, "+"); |
| 11882 | } |
| 11883 | verbose(env, "\n"); |
| 11884 | } |
| 11885 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " |
| 11886 | "total_states %d peak_states %d mark_read %d\n", |
| 11887 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, |
| 11888 | env->max_states_per_insn, env->total_states, |
| 11889 | env->peak_states, env->longest_mark_read_walk); |
| 11890 | } |
| 11891 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 11892 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
| 11893 | { |
| 11894 | const struct btf_type *t, *func_proto; |
| 11895 | const struct bpf_struct_ops *st_ops; |
| 11896 | const struct btf_member *member; |
| 11897 | struct bpf_prog *prog = env->prog; |
| 11898 | u32 btf_id, member_idx; |
| 11899 | const char *mname; |
| 11900 | |
| 11901 | if (!prog->gpl_compatible) { |
| 11902 | verbose(env, "struct ops programs must have a GPL compatible license\n"); |
| 11903 | return -EINVAL; |
| 11904 | } |
| 11905 | |
| 11906 | btf_id = prog->aux->attach_btf_id; |
| 11907 | st_ops = bpf_struct_ops_find(btf_id); |
| 11908 | if (!st_ops) { |
| 11909 | verbose(env, "attach_btf_id %u is not a supported struct\n", |
| 11910 | btf_id); |
| 11911 | return -ENOTSUPP; |
| 11912 | } |
| 11913 | |
| 11914 | t = st_ops->type; |
| 11915 | member_idx = prog->expected_attach_type; |
| 11916 | if (member_idx >= btf_type_vlen(t)) { |
| 11917 | verbose(env, "attach to invalid member idx %u of struct %s\n", |
| 11918 | member_idx, st_ops->name); |
| 11919 | return -EINVAL; |
| 11920 | } |
| 11921 | |
| 11922 | member = &btf_type_member(t)[member_idx]; |
| 11923 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); |
| 11924 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, |
| 11925 | NULL); |
| 11926 | if (!func_proto) { |
| 11927 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", |
| 11928 | mname, member_idx, st_ops->name); |
| 11929 | return -EINVAL; |
| 11930 | } |
| 11931 | |
| 11932 | if (st_ops->check_member) { |
| 11933 | int err = st_ops->check_member(t, member); |
| 11934 | |
| 11935 | if (err) { |
| 11936 | verbose(env, "attach to unsupported member %s of struct %s\n", |
| 11937 | mname, st_ops->name); |
| 11938 | return err; |
| 11939 | } |
| 11940 | } |
| 11941 | |
| 11942 | prog->aux->attach_func_proto = func_proto; |
| 11943 | prog->aux->attach_func_name = mname; |
| 11944 | env->ops = st_ops->verifier_ops; |
| 11945 | |
| 11946 | return 0; |
| 11947 | } |
| 11948 | #define SECURITY_PREFIX "security_" |
| 11949 | |
| 11950 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
| 11951 | { |
| 11952 | if (within_error_injection_list(addr) || |
| 11953 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
| 11954 | return 0; |
| 11955 | |
| 11956 | return -EINVAL; |
| 11957 | } |
| 11958 | |
| 11959 | /* non exhaustive list of sleepable bpf_lsm_*() functions */ |
| 11960 | BTF_SET_START(btf_sleepable_lsm_hooks) |
| 11961 | #ifdef CONFIG_BPF_LSM |
| 11962 | BTF_ID(func, bpf_lsm_bprm_committed_creds) |
| 11963 | #else |
| 11964 | BTF_ID_UNUSED |
| 11965 | #endif |
| 11966 | BTF_SET_END(btf_sleepable_lsm_hooks) |
| 11967 | |
| 11968 | static int check_sleepable_lsm_hook(u32 btf_id) |
| 11969 | { |
| 11970 | return btf_id_set_contains(&btf_sleepable_lsm_hooks, btf_id); |
| 11971 | } |
| 11972 | |
| 11973 | /* list of non-sleepable functions that are otherwise on |
| 11974 | * ALLOW_ERROR_INJECTION list |
| 11975 | */ |
| 11976 | BTF_SET_START(btf_non_sleepable_error_inject) |
| 11977 | /* Three functions below can be called from sleepable and non-sleepable context. |
| 11978 | * Assume non-sleepable from bpf safety point of view. |
| 11979 | */ |
| 11980 | BTF_ID(func, __add_to_page_cache_locked) |
| 11981 | BTF_ID(func, should_fail_alloc_page) |
| 11982 | BTF_ID(func, should_failslab) |
| 11983 | BTF_SET_END(btf_non_sleepable_error_inject) |
| 11984 | |
| 11985 | static int check_non_sleepable_error_inject(u32 btf_id) |
| 11986 | { |
| 11987 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); |
| 11988 | } |
| 11989 | |
| 11990 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
| 11991 | const struct bpf_prog *prog, |
| 11992 | const struct bpf_prog *tgt_prog, |
| 11993 | u32 btf_id, |
| 11994 | struct bpf_attach_target_info *tgt_info) |
| 11995 | { |
| 11996 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
| 11997 | const char prefix[] = "btf_trace_"; |
| 11998 | int ret = 0, subprog = -1, i; |
| 11999 | const struct btf_type *t; |
| 12000 | bool conservative = true; |
| 12001 | const char *tname; |
| 12002 | struct btf *btf; |
| 12003 | long addr = 0; |
| 12004 | |
| 12005 | if (!btf_id) { |
| 12006 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
| 12007 | return -EINVAL; |
| 12008 | } |
| 12009 | btf = tgt_prog ? tgt_prog->aux->btf : btf_vmlinux; |
| 12010 | if (!btf) { |
| 12011 | bpf_log(log, |
| 12012 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
| 12013 | return -EINVAL; |
| 12014 | } |
| 12015 | t = btf_type_by_id(btf, btf_id); |
| 12016 | if (!t) { |
| 12017 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
| 12018 | return -EINVAL; |
| 12019 | } |
| 12020 | tname = btf_name_by_offset(btf, t->name_off); |
| 12021 | if (!tname) { |
| 12022 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
| 12023 | return -EINVAL; |
| 12024 | } |
| 12025 | if (tgt_prog) { |
| 12026 | struct bpf_prog_aux *aux = tgt_prog->aux; |
| 12027 | |
| 12028 | for (i = 0; i < aux->func_info_cnt; i++) |
| 12029 | if (aux->func_info[i].type_id == btf_id) { |
| 12030 | subprog = i; |
| 12031 | break; |
| 12032 | } |
| 12033 | if (subprog == -1) { |
| 12034 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
| 12035 | return -EINVAL; |
| 12036 | } |
| 12037 | conservative = aux->func_info_aux[subprog].unreliable; |
| 12038 | if (prog_extension) { |
| 12039 | if (conservative) { |
| 12040 | bpf_log(log, |
| 12041 | "Cannot replace static functions\n"); |
| 12042 | return -EINVAL; |
| 12043 | } |
| 12044 | if (!prog->jit_requested) { |
| 12045 | bpf_log(log, |
| 12046 | "Extension programs should be JITed\n"); |
| 12047 | return -EINVAL; |
| 12048 | } |
| 12049 | } |
| 12050 | if (!tgt_prog->jited) { |
| 12051 | bpf_log(log, "Can attach to only JITed progs\n"); |
| 12052 | return -EINVAL; |
| 12053 | } |
| 12054 | if (tgt_prog->type == prog->type) { |
| 12055 | /* Cannot fentry/fexit another fentry/fexit program. |
| 12056 | * Cannot attach program extension to another extension. |
| 12057 | * It's ok to attach fentry/fexit to extension program. |
| 12058 | */ |
| 12059 | bpf_log(log, "Cannot recursively attach\n"); |
| 12060 | return -EINVAL; |
| 12061 | } |
| 12062 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && |
| 12063 | prog_extension && |
| 12064 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || |
| 12065 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { |
| 12066 | /* Program extensions can extend all program types |
| 12067 | * except fentry/fexit. The reason is the following. |
| 12068 | * The fentry/fexit programs are used for performance |
| 12069 | * analysis, stats and can be attached to any program |
| 12070 | * type except themselves. When extension program is |
| 12071 | * replacing XDP function it is necessary to allow |
| 12072 | * performance analysis of all functions. Both original |
| 12073 | * XDP program and its program extension. Hence |
| 12074 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is |
| 12075 | * allowed. If extending of fentry/fexit was allowed it |
| 12076 | * would be possible to create long call chain |
| 12077 | * fentry->extension->fentry->extension beyond |
| 12078 | * reasonable stack size. Hence extending fentry is not |
| 12079 | * allowed. |
| 12080 | */ |
| 12081 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
| 12082 | return -EINVAL; |
| 12083 | } |
| 12084 | } else { |
| 12085 | if (prog_extension) { |
| 12086 | bpf_log(log, "Cannot replace kernel functions\n"); |
| 12087 | return -EINVAL; |
| 12088 | } |
| 12089 | } |
| 12090 | |
| 12091 | switch (prog->expected_attach_type) { |
| 12092 | case BPF_TRACE_RAW_TP: |
| 12093 | if (tgt_prog) { |
| 12094 | bpf_log(log, |
| 12095 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
| 12096 | return -EINVAL; |
| 12097 | } |
| 12098 | if (!btf_type_is_typedef(t)) { |
| 12099 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
| 12100 | btf_id); |
| 12101 | return -EINVAL; |
| 12102 | } |
| 12103 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
| 12104 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
| 12105 | btf_id, tname); |
| 12106 | return -EINVAL; |
| 12107 | } |
| 12108 | tname += sizeof(prefix) - 1; |
| 12109 | t = btf_type_by_id(btf, t->type); |
| 12110 | if (!btf_type_is_ptr(t)) |
| 12111 | /* should never happen in valid vmlinux build */ |
| 12112 | return -EINVAL; |
| 12113 | t = btf_type_by_id(btf, t->type); |
| 12114 | if (!btf_type_is_func_proto(t)) |
| 12115 | /* should never happen in valid vmlinux build */ |
| 12116 | return -EINVAL; |
| 12117 | |
| 12118 | break; |
| 12119 | case BPF_TRACE_ITER: |
| 12120 | if (!btf_type_is_func(t)) { |
| 12121 | bpf_log(log, "attach_btf_id %u is not a function\n", |
| 12122 | btf_id); |
| 12123 | return -EINVAL; |
| 12124 | } |
| 12125 | t = btf_type_by_id(btf, t->type); |
| 12126 | if (!btf_type_is_func_proto(t)) |
| 12127 | return -EINVAL; |
| 12128 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
| 12129 | if (ret) |
| 12130 | return ret; |
| 12131 | break; |
| 12132 | default: |
| 12133 | if (!prog_extension) |
| 12134 | return -EINVAL; |
| 12135 | fallthrough; |
| 12136 | case BPF_MODIFY_RETURN: |
| 12137 | case BPF_LSM_MAC: |
| 12138 | case BPF_TRACE_FENTRY: |
| 12139 | case BPF_TRACE_FEXIT: |
| 12140 | if (!btf_type_is_func(t)) { |
| 12141 | bpf_log(log, "attach_btf_id %u is not a function\n", |
| 12142 | btf_id); |
| 12143 | return -EINVAL; |
| 12144 | } |
| 12145 | if (prog_extension && |
| 12146 | btf_check_type_match(log, prog, btf, t)) |
| 12147 | return -EINVAL; |
| 12148 | t = btf_type_by_id(btf, t->type); |
| 12149 | if (!btf_type_is_func_proto(t)) |
| 12150 | return -EINVAL; |
| 12151 | |
| 12152 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
| 12153 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || |
| 12154 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) |
| 12155 | return -EINVAL; |
| 12156 | |
| 12157 | if (tgt_prog && conservative) |
| 12158 | t = NULL; |
| 12159 | |
| 12160 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
| 12161 | if (ret < 0) |
| 12162 | return ret; |
| 12163 | |
| 12164 | if (tgt_prog) { |
| 12165 | if (subprog == 0) |
| 12166 | addr = (long) tgt_prog->bpf_func; |
| 12167 | else |
| 12168 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; |
| 12169 | } else { |
| 12170 | addr = kallsyms_lookup_name(tname); |
| 12171 | if (!addr) { |
| 12172 | bpf_log(log, |
| 12173 | "The address of function %s cannot be found\n", |
| 12174 | tname); |
| 12175 | return -ENOENT; |
| 12176 | } |
| 12177 | } |
| 12178 | |
| 12179 | if (prog->aux->sleepable) { |
| 12180 | ret = -EINVAL; |
| 12181 | switch (prog->type) { |
| 12182 | case BPF_PROG_TYPE_TRACING: |
| 12183 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are |
| 12184 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. |
| 12185 | */ |
| 12186 | if (!check_non_sleepable_error_inject(btf_id) && |
| 12187 | within_error_injection_list(addr)) |
| 12188 | ret = 0; |
| 12189 | break; |
| 12190 | case BPF_PROG_TYPE_LSM: |
| 12191 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. |
| 12192 | * Only some of them are sleepable. |
| 12193 | */ |
| 12194 | if (check_sleepable_lsm_hook(btf_id)) |
| 12195 | ret = 0; |
| 12196 | break; |
| 12197 | default: |
| 12198 | break; |
| 12199 | } |
| 12200 | if (ret) { |
| 12201 | bpf_log(log, "%s is not sleepable\n", tname); |
| 12202 | return ret; |
| 12203 | } |
| 12204 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
| 12205 | if (tgt_prog) { |
| 12206 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
| 12207 | return -EINVAL; |
| 12208 | } |
| 12209 | ret = check_attach_modify_return(addr, tname); |
| 12210 | if (ret) { |
| 12211 | bpf_log(log, "%s() is not modifiable\n", tname); |
| 12212 | return ret; |
| 12213 | } |
| 12214 | } |
| 12215 | |
| 12216 | break; |
| 12217 | } |
| 12218 | tgt_info->tgt_addr = addr; |
| 12219 | tgt_info->tgt_name = tname; |
| 12220 | tgt_info->tgt_type = t; |
| 12221 | return 0; |
| 12222 | } |
| 12223 | |
| 12224 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
| 12225 | { |
| 12226 | struct bpf_prog *prog = env->prog; |
| 12227 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
| 12228 | struct bpf_attach_target_info tgt_info = {}; |
| 12229 | u32 btf_id = prog->aux->attach_btf_id; |
| 12230 | struct bpf_trampoline *tr; |
| 12231 | int ret; |
| 12232 | u64 key; |
| 12233 | |
| 12234 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && |
| 12235 | prog->type != BPF_PROG_TYPE_LSM) { |
| 12236 | verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n"); |
| 12237 | return -EINVAL; |
| 12238 | } |
| 12239 | |
| 12240 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) |
| 12241 | return check_struct_ops_btf_id(env); |
| 12242 | |
| 12243 | if (prog->type != BPF_PROG_TYPE_TRACING && |
| 12244 | prog->type != BPF_PROG_TYPE_LSM && |
| 12245 | prog->type != BPF_PROG_TYPE_EXT) |
| 12246 | return 0; |
| 12247 | |
| 12248 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); |
| 12249 | if (ret) |
| 12250 | return ret; |
| 12251 | |
| 12252 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { |
| 12253 | /* to make freplace equivalent to their targets, they need to |
| 12254 | * inherit env->ops and expected_attach_type for the rest of the |
| 12255 | * verification |
| 12256 | */ |
| 12257 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
| 12258 | prog->expected_attach_type = tgt_prog->expected_attach_type; |
| 12259 | } |
| 12260 | |
| 12261 | /* store info about the attachment target that will be used later */ |
| 12262 | prog->aux->attach_func_proto = tgt_info.tgt_type; |
| 12263 | prog->aux->attach_func_name = tgt_info.tgt_name; |
| 12264 | |
| 12265 | if (tgt_prog) { |
| 12266 | prog->aux->saved_dst_prog_type = tgt_prog->type; |
| 12267 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; |
| 12268 | } |
| 12269 | |
| 12270 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
| 12271 | prog->aux->attach_btf_trace = true; |
| 12272 | return 0; |
| 12273 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { |
| 12274 | if (!bpf_iter_prog_supported(prog)) |
| 12275 | return -EINVAL; |
| 12276 | return 0; |
| 12277 | } |
| 12278 | |
| 12279 | if (prog->type == BPF_PROG_TYPE_LSM) { |
| 12280 | ret = bpf_lsm_verify_prog(&env->log, prog); |
| 12281 | if (ret < 0) |
| 12282 | return ret; |
| 12283 | } |
| 12284 | |
| 12285 | key = bpf_trampoline_compute_key(tgt_prog, btf_id); |
| 12286 | tr = bpf_trampoline_get(key, &tgt_info); |
| 12287 | if (!tr) |
| 12288 | return -ENOMEM; |
| 12289 | |
| 12290 | prog->aux->dst_trampoline = tr; |
| 12291 | return 0; |
| 12292 | } |
| 12293 | |
| 12294 | struct btf *bpf_get_btf_vmlinux(void) |
| 12295 | { |
| 12296 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { |
| 12297 | mutex_lock(&bpf_verifier_lock); |
| 12298 | if (!btf_vmlinux) |
| 12299 | btf_vmlinux = btf_parse_vmlinux(); |
| 12300 | mutex_unlock(&bpf_verifier_lock); |
| 12301 | } |
| 12302 | return btf_vmlinux; |
| 12303 | } |
| 12304 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12305 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
| 12306 | union bpf_attr __user *uattr) |
| 12307 | { |
| 12308 | u64 start_time = ktime_get_ns(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12309 | struct bpf_verifier_env *env; |
| 12310 | struct bpf_verifier_log *log; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12311 | int i, len, ret = -EINVAL; |
| 12312 | bool is_priv; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12313 | |
| 12314 | /* no program is valid */ |
| 12315 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 12316 | return -EINVAL; |
| 12317 | |
| 12318 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
| 12319 | * allocate/free it every time bpf_check() is called |
| 12320 | */ |
| 12321 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
| 12322 | if (!env) |
| 12323 | return -ENOMEM; |
| 12324 | log = &env->log; |
| 12325 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12326 | len = (*prog)->len; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12327 | env->insn_aux_data = |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12328 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12329 | ret = -ENOMEM; |
| 12330 | if (!env->insn_aux_data) |
| 12331 | goto err_free_env; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12332 | for (i = 0; i < len; i++) |
| 12333 | env->insn_aux_data[i].orig_idx = i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12334 | env->prog = *prog; |
| 12335 | env->ops = bpf_verifier_ops[env->prog->type]; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 12336 | is_priv = bpf_capable(); |
| 12337 | |
| 12338 | bpf_get_btf_vmlinux(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12339 | |
| 12340 | /* grab the mutex to protect few globals used by verifier */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12341 | if (!is_priv) |
| 12342 | mutex_lock(&bpf_verifier_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12343 | |
| 12344 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 12345 | /* user requested verbose verifier output |
| 12346 | * and supplied buffer to store the verification trace |
| 12347 | */ |
| 12348 | log->level = attr->log_level; |
| 12349 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 12350 | log->len_total = attr->log_size; |
| 12351 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12352 | /* log attributes have to be sane */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 12353 | if (!bpf_verifier_log_attr_valid(log)) { |
| 12354 | ret = -EINVAL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12355 | goto err_unlock; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 12356 | } |
| 12357 | } |
| 12358 | |
| 12359 | if (IS_ERR(btf_vmlinux)) { |
| 12360 | /* Either gcc or pahole or kernel are broken. */ |
| 12361 | verbose(env, "in-kernel BTF is malformed\n"); |
| 12362 | ret = PTR_ERR(btf_vmlinux); |
| 12363 | goto skip_full_check; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12364 | } |
| 12365 | |
| 12366 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 12367 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
| 12368 | env->strict_alignment = true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12369 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
| 12370 | env->strict_alignment = false; |
| 12371 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 12372 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
| 12373 | env->allow_uninit_stack = bpf_allow_uninit_stack(); |
| 12374 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
| 12375 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
| 12376 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); |
| 12377 | env->bpf_capable = bpf_capable(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12378 | |
| 12379 | if (is_priv) |
| 12380 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12381 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12382 | env->explored_states = kvcalloc(state_htab_size(env), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12383 | sizeof(struct bpf_verifier_state_list *), |
| 12384 | GFP_USER); |
| 12385 | ret = -ENOMEM; |
| 12386 | if (!env->explored_states) |
| 12387 | goto skip_full_check; |
| 12388 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12389 | ret = check_subprogs(env); |
| 12390 | if (ret < 0) |
| 12391 | goto skip_full_check; |
| 12392 | |
| 12393 | ret = check_btf_info(env, attr, uattr); |
| 12394 | if (ret < 0) |
| 12395 | goto skip_full_check; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12396 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 12397 | ret = check_attach_btf_id(env); |
| 12398 | if (ret) |
| 12399 | goto skip_full_check; |
| 12400 | |
| 12401 | ret = resolve_pseudo_ldimm64(env); |
| 12402 | if (ret < 0) |
| 12403 | goto skip_full_check; |
| 12404 | |
| 12405 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
| 12406 | ret = bpf_prog_offload_verifier_prep(env->prog); |
| 12407 | if (ret) |
| 12408 | goto skip_full_check; |
| 12409 | } |
| 12410 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12411 | ret = check_cfg(env); |
| 12412 | if (ret < 0) |
| 12413 | goto skip_full_check; |
| 12414 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 12415 | ret = do_check_subprogs(env); |
| 12416 | ret = ret ?: do_check_main(env); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12417 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12418 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 12419 | ret = bpf_prog_offload_finalize(env); |
| 12420 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12421 | skip_full_check: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 12422 | kvfree(env->explored_states); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12423 | |
| 12424 | if (ret == 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12425 | ret = check_max_stack_depth(env); |
| 12426 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12427 | /* instruction rewrites happen after this point */ |
| 12428 | if (is_priv) { |
| 12429 | if (ret == 0) |
| 12430 | opt_hard_wire_dead_code_branches(env); |
| 12431 | if (ret == 0) |
| 12432 | ret = opt_remove_dead_code(env); |
| 12433 | if (ret == 0) |
| 12434 | ret = opt_remove_nops(env); |
| 12435 | } else { |
| 12436 | if (ret == 0) |
| 12437 | sanitize_dead_code(env); |
| 12438 | } |
| 12439 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12440 | if (ret == 0) |
| 12441 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 12442 | ret = convert_ctx_accesses(env); |
| 12443 | |
| 12444 | if (ret == 0) |
| 12445 | ret = fixup_bpf_calls(env); |
| 12446 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12447 | /* do 32-bit optimization after insn patching has done so those patched |
| 12448 | * insns could be handled correctly. |
| 12449 | */ |
| 12450 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
| 12451 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); |
| 12452 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret |
| 12453 | : false; |
| 12454 | } |
| 12455 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12456 | if (ret == 0) |
| 12457 | ret = fixup_call_args(env); |
| 12458 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12459 | env->verification_time = ktime_get_ns() - start_time; |
| 12460 | print_verification_stats(env); |
| 12461 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12462 | if (log->level && bpf_verifier_log_full(log)) |
| 12463 | ret = -ENOSPC; |
| 12464 | if (log->level && !log->ubuf) { |
| 12465 | ret = -EFAULT; |
| 12466 | goto err_release_maps; |
| 12467 | } |
| 12468 | |
| 12469 | if (ret == 0 && env->used_map_cnt) { |
| 12470 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
| 12471 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 12472 | sizeof(env->used_maps[0]), |
| 12473 | GFP_KERNEL); |
| 12474 | |
| 12475 | if (!env->prog->aux->used_maps) { |
| 12476 | ret = -ENOMEM; |
| 12477 | goto err_release_maps; |
| 12478 | } |
| 12479 | |
| 12480 | memcpy(env->prog->aux->used_maps, env->used_maps, |
| 12481 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
| 12482 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
| 12483 | |
| 12484 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 12485 | * bpf_ld_imm64 instructions |
| 12486 | */ |
| 12487 | convert_pseudo_ld_imm64(env); |
| 12488 | } |
| 12489 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12490 | if (ret == 0) |
| 12491 | adjust_btf_func(env); |
| 12492 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12493 | err_release_maps: |
| 12494 | if (!env->prog->aux->used_maps) |
| 12495 | /* if we didn't copy map pointers into bpf_prog_info, release |
| 12496 | * them now. Otherwise free_used_maps() will release them. |
| 12497 | */ |
| 12498 | release_maps(env); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 12499 | |
| 12500 | /* extension progs temporarily inherit the attach_type of their targets |
| 12501 | for verification purposes, so set it back to zero before returning |
| 12502 | */ |
| 12503 | if (env->prog->type == BPF_PROG_TYPE_EXT) |
| 12504 | env->prog->expected_attach_type = 0; |
| 12505 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12506 | *prog = env->prog; |
| 12507 | err_unlock: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 12508 | if (!is_priv) |
| 12509 | mutex_unlock(&bpf_verifier_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12510 | vfree(env->insn_aux_data); |
| 12511 | err_free_env: |
| 12512 | kfree(env); |
| 12513 | return ret; |
| 12514 | } |