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> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 22 | |
| 23 | #include "disasm.h" |
| 24 | |
| 25 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
| 26 | #define BPF_PROG_TYPE(_id, _name) \ |
| 27 | [_id] = & _name ## _verifier_ops, |
| 28 | #define BPF_MAP_TYPE(_id, _ops) |
| 29 | #include <linux/bpf_types.h> |
| 30 | #undef BPF_PROG_TYPE |
| 31 | #undef BPF_MAP_TYPE |
| 32 | }; |
| 33 | |
| 34 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 35 | * instruction by instruction and updates register/stack state. |
| 36 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 37 | * |
| 38 | * The first pass is depth-first-search to check that the program is a DAG. |
| 39 | * It rejects the following programs: |
| 40 | * - larger than BPF_MAXINSNS insns |
| 41 | * - if loop is present (detected via back-edge) |
| 42 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 43 | * - out of bounds or malformed jumps |
| 44 | * The second pass is all possible path descent from the 1st insn. |
| 45 | * Since it's analyzing all pathes through the program, the length of the |
| 46 | * analysis is limited to 64k insn, which may be hit even if total number of |
| 47 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 48 | * Number of 'branches to be analyzed' is limited to 1k |
| 49 | * |
| 50 | * On entry to each instruction, each register has a type, and the instruction |
| 51 | * changes the types of the registers depending on instruction semantics. |
| 52 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 53 | * copied to R1. |
| 54 | * |
| 55 | * All registers are 64-bit. |
| 56 | * R0 - return register |
| 57 | * R1-R5 argument passing registers |
| 58 | * R6-R9 callee saved registers |
| 59 | * R10 - frame pointer read-only |
| 60 | * |
| 61 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 62 | * and has type PTR_TO_CTX. |
| 63 | * |
| 64 | * Verifier tracks arithmetic operations on pointers in case: |
| 65 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 66 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 67 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 68 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 69 | * that it wants to construct a pointer to some element within stack. |
| 70 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 71 | * (and -20 constant is saved for further stack bounds checking). |
| 72 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 73 | * |
| 74 | * Most of the time the registers have SCALAR_VALUE type, which |
| 75 | * means the register has some value, but it's not a valid pointer. |
| 76 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
| 77 | * |
| 78 | * When verifier sees load or store instructions the type of base register |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 79 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
| 80 | * four pointer types recognized by check_mem_access() function. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 81 | * |
| 82 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 83 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 84 | * |
| 85 | * registers used to pass values to function calls are checked against |
| 86 | * function argument constraints. |
| 87 | * |
| 88 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 89 | * It means that the register type passed to this function must be |
| 90 | * PTR_TO_STACK and it will be used inside the function as |
| 91 | * 'pointer to map element key' |
| 92 | * |
| 93 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 94 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 95 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 96 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 97 | * |
| 98 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 99 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 100 | * 2nd argument should be a pointer to stack, which will be used inside |
| 101 | * the helper function as a pointer to map element key. |
| 102 | * |
| 103 | * On the kernel side the helper function looks like: |
| 104 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 105 | * { |
| 106 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 107 | * void *key = (void *) (unsigned long) r2; |
| 108 | * void *value; |
| 109 | * |
| 110 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 111 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 112 | * the stack of eBPF program. |
| 113 | * } |
| 114 | * |
| 115 | * Corresponding eBPF program may look like: |
| 116 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 117 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 118 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 119 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 120 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 121 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 122 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 123 | * |
| 124 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 125 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 126 | * and were initialized prior to this call. |
| 127 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 128 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 129 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 130 | * returns ether pointer to map value or NULL. |
| 131 | * |
| 132 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 133 | * insn, the register holding that pointer in the true branch changes state to |
| 134 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 135 | * branch. See check_cond_jmp_op(). |
| 136 | * |
| 137 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 138 | * 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] | 139 | * |
| 140 | * The following reference types represent a potential reference to a kernel |
| 141 | * resource which, after first being allocated, must be checked and freed by |
| 142 | * the BPF program: |
| 143 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET |
| 144 | * |
| 145 | * When the verifier sees a helper call return a reference type, it allocates a |
| 146 | * pointer id for the reference and stores it in the current function state. |
| 147 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into |
| 148 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type |
| 149 | * passes through a NULL-check conditional. For the branch wherein the state is |
| 150 | * changed to CONST_IMM, the verifier releases the reference. |
| 151 | * |
| 152 | * For each helper function that allocates a reference, such as |
| 153 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as |
| 154 | * bpf_sk_release(). When a reference type passes into the release function, |
| 155 | * the verifier also releases the reference. If any unchecked or unreleased |
| 156 | * reference remains at the end of the program, the verifier rejects it. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 157 | */ |
| 158 | |
| 159 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
| 160 | struct bpf_verifier_stack_elem { |
| 161 | /* verifer state is 'st' |
| 162 | * before processing instruction 'insn_idx' |
| 163 | * and after processing instruction 'prev_insn_idx' |
| 164 | */ |
| 165 | struct bpf_verifier_state st; |
| 166 | int insn_idx; |
| 167 | int prev_insn_idx; |
| 168 | struct bpf_verifier_stack_elem *next; |
| 169 | }; |
| 170 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 171 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
| 172 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 173 | |
| 174 | #define BPF_MAP_PTR_UNPRIV 1UL |
| 175 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ |
| 176 | POISON_POINTER_DELTA)) |
| 177 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) |
| 178 | |
| 179 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
| 180 | { |
| 181 | return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON; |
| 182 | } |
| 183 | |
| 184 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) |
| 185 | { |
| 186 | return aux->map_state & BPF_MAP_PTR_UNPRIV; |
| 187 | } |
| 188 | |
| 189 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, |
| 190 | const struct bpf_map *map, bool unpriv) |
| 191 | { |
| 192 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); |
| 193 | unpriv |= bpf_map_ptr_unpriv(aux); |
| 194 | aux->map_state = (unsigned long)map | |
| 195 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); |
| 196 | } |
| 197 | |
| 198 | struct bpf_call_arg_meta { |
| 199 | struct bpf_map *map_ptr; |
| 200 | bool raw_mode; |
| 201 | bool pkt_access; |
| 202 | int regno; |
| 203 | int access_size; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 204 | u64 msize_max_value; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 205 | int ref_obj_id; |
| 206 | int func_id; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 210 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 211 | static const struct bpf_line_info * |
| 212 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) |
| 213 | { |
| 214 | const struct bpf_line_info *linfo; |
| 215 | const struct bpf_prog *prog; |
| 216 | u32 i, nr_linfo; |
| 217 | |
| 218 | prog = env->prog; |
| 219 | nr_linfo = prog->aux->nr_linfo; |
| 220 | |
| 221 | if (!nr_linfo || insn_off >= prog->len) |
| 222 | return NULL; |
| 223 | |
| 224 | linfo = prog->aux->linfo; |
| 225 | for (i = 1; i < nr_linfo; i++) |
| 226 | if (insn_off < linfo[i].insn_off) |
| 227 | break; |
| 228 | |
| 229 | return &linfo[i - 1]; |
| 230 | } |
| 231 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 232 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
| 233 | va_list args) |
| 234 | { |
| 235 | unsigned int n; |
| 236 | |
| 237 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
| 238 | |
| 239 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 240 | "verifier log line truncated - local buffer too short\n"); |
| 241 | |
| 242 | n = min(log->len_total - log->len_used - 1, n); |
| 243 | log->kbuf[n] = '\0'; |
| 244 | |
| 245 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 246 | log->len_used += n; |
| 247 | else |
| 248 | log->ubuf = NULL; |
| 249 | } |
| 250 | |
| 251 | /* log_level controls verbosity level of eBPF verifier. |
| 252 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 253 | * so the user can figure out what's wrong with the program |
| 254 | */ |
| 255 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 256 | const char *fmt, ...) |
| 257 | { |
| 258 | va_list args; |
| 259 | |
| 260 | if (!bpf_verifier_log_needed(&env->log)) |
| 261 | return; |
| 262 | |
| 263 | va_start(args, fmt); |
| 264 | bpf_verifier_vlog(&env->log, fmt, args); |
| 265 | va_end(args); |
| 266 | } |
| 267 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 268 | |
| 269 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
| 270 | { |
| 271 | struct bpf_verifier_env *env = private_data; |
| 272 | va_list args; |
| 273 | |
| 274 | if (!bpf_verifier_log_needed(&env->log)) |
| 275 | return; |
| 276 | |
| 277 | va_start(args, fmt); |
| 278 | bpf_verifier_vlog(&env->log, fmt, args); |
| 279 | va_end(args); |
| 280 | } |
| 281 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 282 | static const char *ltrim(const char *s) |
| 283 | { |
| 284 | while (isspace(*s)) |
| 285 | s++; |
| 286 | |
| 287 | return s; |
| 288 | } |
| 289 | |
| 290 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, |
| 291 | u32 insn_off, |
| 292 | const char *prefix_fmt, ...) |
| 293 | { |
| 294 | const struct bpf_line_info *linfo; |
| 295 | |
| 296 | if (!bpf_verifier_log_needed(&env->log)) |
| 297 | return; |
| 298 | |
| 299 | linfo = find_linfo(env, insn_off); |
| 300 | if (!linfo || linfo == env->prev_linfo) |
| 301 | return; |
| 302 | |
| 303 | if (prefix_fmt) { |
| 304 | va_list args; |
| 305 | |
| 306 | va_start(args, prefix_fmt); |
| 307 | bpf_verifier_vlog(&env->log, prefix_fmt, args); |
| 308 | va_end(args); |
| 309 | } |
| 310 | |
| 311 | verbose(env, "%s\n", |
| 312 | ltrim(btf_name_by_offset(env->prog->aux->btf, |
| 313 | linfo->line_off))); |
| 314 | |
| 315 | env->prev_linfo = linfo; |
| 316 | } |
| 317 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 318 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 319 | { |
| 320 | return type == PTR_TO_PACKET || |
| 321 | type == PTR_TO_PACKET_META; |
| 322 | } |
| 323 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 324 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
| 325 | { |
| 326 | return type == PTR_TO_SOCKET || |
| 327 | type == PTR_TO_SOCK_COMMON || |
| 328 | type == PTR_TO_TCP_SOCK || |
| 329 | type == PTR_TO_XDP_SOCK; |
| 330 | } |
| 331 | |
| 332 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
| 333 | { |
| 334 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
| 335 | type == PTR_TO_SOCKET_OR_NULL || |
| 336 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
| 337 | type == PTR_TO_TCP_SOCK_OR_NULL; |
| 338 | } |
| 339 | |
| 340 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
| 341 | { |
| 342 | return reg->type == PTR_TO_MAP_VALUE && |
| 343 | map_value_has_spin_lock(reg->map_ptr); |
| 344 | } |
| 345 | |
| 346 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
| 347 | { |
| 348 | return type == PTR_TO_SOCKET || |
| 349 | type == PTR_TO_SOCKET_OR_NULL || |
| 350 | type == PTR_TO_TCP_SOCK || |
| 351 | type == PTR_TO_TCP_SOCK_OR_NULL; |
| 352 | } |
| 353 | |
| 354 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
| 355 | { |
| 356 | return type == ARG_PTR_TO_SOCK_COMMON; |
| 357 | } |
| 358 | |
| 359 | /* Determine whether the function releases some resources allocated by another |
| 360 | * function call. The first reference type argument will be assumed to be |
| 361 | * released by release_reference(). |
| 362 | */ |
| 363 | static bool is_release_function(enum bpf_func_id func_id) |
| 364 | { |
| 365 | return func_id == BPF_FUNC_sk_release; |
| 366 | } |
| 367 | |
| 368 | static bool is_acquire_function(enum bpf_func_id func_id) |
| 369 | { |
| 370 | return func_id == BPF_FUNC_sk_lookup_tcp || |
| 371 | func_id == BPF_FUNC_sk_lookup_udp || |
| 372 | func_id == BPF_FUNC_skc_lookup_tcp; |
| 373 | } |
| 374 | |
| 375 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
| 376 | { |
| 377 | return func_id == BPF_FUNC_tcp_sock || |
| 378 | func_id == BPF_FUNC_sk_fullsock; |
| 379 | } |
| 380 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 381 | /* string representation of 'enum bpf_reg_type' */ |
| 382 | static const char * const reg_type_str[] = { |
| 383 | [NOT_INIT] = "?", |
| 384 | [SCALAR_VALUE] = "inv", |
| 385 | [PTR_TO_CTX] = "ctx", |
| 386 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 387 | [PTR_TO_MAP_VALUE] = "map_value", |
| 388 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
| 389 | [PTR_TO_STACK] = "fp", |
| 390 | [PTR_TO_PACKET] = "pkt", |
| 391 | [PTR_TO_PACKET_META] = "pkt_meta", |
| 392 | [PTR_TO_PACKET_END] = "pkt_end", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 393 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
| 394 | [PTR_TO_SOCKET] = "sock", |
| 395 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", |
| 396 | [PTR_TO_SOCK_COMMON] = "sock_common", |
| 397 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", |
| 398 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
| 399 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", |
| 400 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
| 401 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
| 402 | }; |
| 403 | |
| 404 | static char slot_type_char[] = { |
| 405 | [STACK_INVALID] = '?', |
| 406 | [STACK_SPILL] = 'r', |
| 407 | [STACK_MISC] = 'm', |
| 408 | [STACK_ZERO] = '0', |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | static void print_liveness(struct bpf_verifier_env *env, |
| 412 | enum bpf_reg_liveness live) |
| 413 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 414 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 415 | verbose(env, "_"); |
| 416 | if (live & REG_LIVE_READ) |
| 417 | verbose(env, "r"); |
| 418 | if (live & REG_LIVE_WRITTEN) |
| 419 | verbose(env, "w"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 420 | if (live & REG_LIVE_DONE) |
| 421 | verbose(env, "D"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 425 | const struct bpf_reg_state *reg) |
| 426 | { |
| 427 | struct bpf_verifier_state *cur = env->cur_state; |
| 428 | |
| 429 | return cur->frame[reg->frameno]; |
| 430 | } |
| 431 | |
| 432 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 433 | const struct bpf_func_state *state) |
| 434 | { |
| 435 | const struct bpf_reg_state *reg; |
| 436 | enum bpf_reg_type t; |
| 437 | int i; |
| 438 | |
| 439 | if (state->frameno) |
| 440 | verbose(env, " frame%d:", state->frameno); |
| 441 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 442 | reg = &state->regs[i]; |
| 443 | t = reg->type; |
| 444 | if (t == NOT_INIT) |
| 445 | continue; |
| 446 | verbose(env, " R%d", i); |
| 447 | print_liveness(env, reg->live); |
| 448 | verbose(env, "=%s", reg_type_str[t]); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 449 | if (t == SCALAR_VALUE && reg->precise) |
| 450 | verbose(env, "P"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 451 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 452 | tnum_is_const(reg->var_off)) { |
| 453 | /* reg->off should be 0 for SCALAR_VALUE */ |
| 454 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 455 | } else { |
| 456 | verbose(env, "(id=%d", reg->id); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 457 | if (reg_type_may_be_refcounted_or_null(t)) |
| 458 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 459 | if (t != SCALAR_VALUE) |
| 460 | verbose(env, ",off=%d", reg->off); |
| 461 | if (type_is_pkt_pointer(t)) |
| 462 | verbose(env, ",r=%d", reg->range); |
| 463 | else if (t == CONST_PTR_TO_MAP || |
| 464 | t == PTR_TO_MAP_VALUE || |
| 465 | t == PTR_TO_MAP_VALUE_OR_NULL) |
| 466 | verbose(env, ",ks=%d,vs=%d", |
| 467 | reg->map_ptr->key_size, |
| 468 | reg->map_ptr->value_size); |
| 469 | if (tnum_is_const(reg->var_off)) { |
| 470 | /* Typically an immediate SCALAR_VALUE, but |
| 471 | * could be a pointer whose offset is too big |
| 472 | * for reg->off |
| 473 | */ |
| 474 | verbose(env, ",imm=%llx", reg->var_off.value); |
| 475 | } else { |
| 476 | if (reg->smin_value != reg->umin_value && |
| 477 | reg->smin_value != S64_MIN) |
| 478 | verbose(env, ",smin_value=%lld", |
| 479 | (long long)reg->smin_value); |
| 480 | if (reg->smax_value != reg->umax_value && |
| 481 | reg->smax_value != S64_MAX) |
| 482 | verbose(env, ",smax_value=%lld", |
| 483 | (long long)reg->smax_value); |
| 484 | if (reg->umin_value != 0) |
| 485 | verbose(env, ",umin_value=%llu", |
| 486 | (unsigned long long)reg->umin_value); |
| 487 | if (reg->umax_value != U64_MAX) |
| 488 | verbose(env, ",umax_value=%llu", |
| 489 | (unsigned long long)reg->umax_value); |
| 490 | if (!tnum_is_unknown(reg->var_off)) { |
| 491 | char tn_buf[48]; |
| 492 | |
| 493 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 494 | verbose(env, ",var_off=%s", tn_buf); |
| 495 | } |
| 496 | } |
| 497 | verbose(env, ")"); |
| 498 | } |
| 499 | } |
| 500 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 501 | char types_buf[BPF_REG_SIZE + 1]; |
| 502 | bool valid = false; |
| 503 | int j; |
| 504 | |
| 505 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 506 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 507 | valid = true; |
| 508 | types_buf[j] = slot_type_char[ |
| 509 | state->stack[i].slot_type[j]]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 510 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 511 | types_buf[BPF_REG_SIZE] = 0; |
| 512 | if (!valid) |
| 513 | continue; |
| 514 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 515 | print_liveness(env, state->stack[i].spilled_ptr.live); |
| 516 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
| 517 | reg = &state->stack[i].spilled_ptr; |
| 518 | t = reg->type; |
| 519 | verbose(env, "=%s", reg_type_str[t]); |
| 520 | if (t == SCALAR_VALUE && reg->precise) |
| 521 | verbose(env, "P"); |
| 522 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) |
| 523 | verbose(env, "%lld", reg->var_off.value + reg->off); |
| 524 | } else { |
| 525 | verbose(env, "=%s", types_buf); |
| 526 | } |
| 527 | } |
| 528 | if (state->acquired_refs && state->refs[0].id) { |
| 529 | verbose(env, " refs=%d", state->refs[0].id); |
| 530 | for (i = 1; i < state->acquired_refs; i++) |
| 531 | if (state->refs[i].id) |
| 532 | verbose(env, ",%d", state->refs[i].id); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 533 | } |
| 534 | verbose(env, "\n"); |
| 535 | } |
| 536 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 537 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 538 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ |
| 539 | const struct bpf_func_state *src) \ |
| 540 | { \ |
| 541 | if (!src->FIELD) \ |
| 542 | return 0; \ |
| 543 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ |
| 544 | /* internal bug, make state invalid to reject the program */ \ |
| 545 | memset(dst, 0, sizeof(*dst)); \ |
| 546 | return -EFAULT; \ |
| 547 | } \ |
| 548 | memcpy(dst->FIELD, src->FIELD, \ |
| 549 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ |
| 550 | return 0; \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 551 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 552 | /* copy_reference_state() */ |
| 553 | COPY_STATE_FN(reference, acquired_refs, refs, 1) |
| 554 | /* copy_stack_state() */ |
| 555 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 556 | #undef COPY_STATE_FN |
| 557 | |
| 558 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 559 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ |
| 560 | bool copy_old) \ |
| 561 | { \ |
| 562 | u32 old_size = state->COUNT; \ |
| 563 | struct bpf_##NAME##_state *new_##FIELD; \ |
| 564 | int slot = size / SIZE; \ |
| 565 | \ |
| 566 | if (size <= old_size || !size) { \ |
| 567 | if (copy_old) \ |
| 568 | return 0; \ |
| 569 | state->COUNT = slot * SIZE; \ |
| 570 | if (!size && old_size) { \ |
| 571 | kfree(state->FIELD); \ |
| 572 | state->FIELD = NULL; \ |
| 573 | } \ |
| 574 | return 0; \ |
| 575 | } \ |
| 576 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ |
| 577 | GFP_KERNEL); \ |
| 578 | if (!new_##FIELD) \ |
| 579 | return -ENOMEM; \ |
| 580 | if (copy_old) { \ |
| 581 | if (state->FIELD) \ |
| 582 | memcpy(new_##FIELD, state->FIELD, \ |
| 583 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ |
| 584 | memset(new_##FIELD + old_size / SIZE, 0, \ |
| 585 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ |
| 586 | } \ |
| 587 | state->COUNT = slot * SIZE; \ |
| 588 | kfree(state->FIELD); \ |
| 589 | state->FIELD = new_##FIELD; \ |
| 590 | return 0; \ |
| 591 | } |
| 592 | /* realloc_reference_state() */ |
| 593 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) |
| 594 | /* realloc_stack_state() */ |
| 595 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 596 | #undef REALLOC_STATE_FN |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 597 | |
| 598 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 599 | * make it consume minimal amount of memory. check_stack_write() access from |
| 600 | * the program calls into realloc_func_state() to grow the stack size. |
| 601 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 602 | * which realloc_stack_state() copies over. It points to previous |
| 603 | * bpf_verifier_state which is never reallocated. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 604 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 605 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
| 606 | int refs_size, bool copy_old) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 607 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 608 | int err = realloc_reference_state(state, refs_size, copy_old); |
| 609 | if (err) |
| 610 | return err; |
| 611 | return realloc_stack_state(state, stack_size, copy_old); |
| 612 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 613 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 614 | /* Acquire a pointer id from the env and update the state->refs to include |
| 615 | * this new pointer reference. |
| 616 | * On success, returns a valid pointer id to associate with the register |
| 617 | * On failure, returns a negative errno. |
| 618 | */ |
| 619 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 620 | { |
| 621 | struct bpf_func_state *state = cur_func(env); |
| 622 | int new_ofs = state->acquired_refs; |
| 623 | int id, err; |
| 624 | |
| 625 | err = realloc_reference_state(state, state->acquired_refs + 1, true); |
| 626 | if (err) |
| 627 | return err; |
| 628 | id = ++env->id_gen; |
| 629 | state->refs[new_ofs].id = id; |
| 630 | state->refs[new_ofs].insn_idx = insn_idx; |
| 631 | |
| 632 | return id; |
| 633 | } |
| 634 | |
| 635 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
| 636 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
| 637 | { |
| 638 | int i, last_idx; |
| 639 | |
| 640 | last_idx = state->acquired_refs - 1; |
| 641 | for (i = 0; i < state->acquired_refs; i++) { |
| 642 | if (state->refs[i].id == ptr_id) { |
| 643 | if (last_idx && i != last_idx) |
| 644 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 645 | sizeof(*state->refs)); |
| 646 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 647 | state->acquired_refs--; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 648 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 649 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 650 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 651 | return -EINVAL; |
| 652 | } |
| 653 | |
| 654 | static int transfer_reference_state(struct bpf_func_state *dst, |
| 655 | struct bpf_func_state *src) |
| 656 | { |
| 657 | int err = realloc_reference_state(dst, src->acquired_refs, false); |
| 658 | if (err) |
| 659 | return err; |
| 660 | err = copy_reference_state(dst, src); |
| 661 | if (err) |
| 662 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static void free_func_state(struct bpf_func_state *state) |
| 667 | { |
| 668 | if (!state) |
| 669 | return; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 670 | kfree(state->refs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 671 | kfree(state->stack); |
| 672 | kfree(state); |
| 673 | } |
| 674 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 675 | static void clear_jmp_history(struct bpf_verifier_state *state) |
| 676 | { |
| 677 | kfree(state->jmp_history); |
| 678 | state->jmp_history = NULL; |
| 679 | state->jmp_history_cnt = 0; |
| 680 | } |
| 681 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 682 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 683 | bool free_self) |
| 684 | { |
| 685 | int i; |
| 686 | |
| 687 | for (i = 0; i <= state->curframe; i++) { |
| 688 | free_func_state(state->frame[i]); |
| 689 | state->frame[i] = NULL; |
| 690 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 691 | clear_jmp_history(state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 692 | if (free_self) |
| 693 | kfree(state); |
| 694 | } |
| 695 | |
| 696 | /* copy verifier state from src to dst growing dst stack space |
| 697 | * when necessary to accommodate larger src stack |
| 698 | */ |
| 699 | static int copy_func_state(struct bpf_func_state *dst, |
| 700 | const struct bpf_func_state *src) |
| 701 | { |
| 702 | int err; |
| 703 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 704 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
| 705 | false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 706 | if (err) |
| 707 | return err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 708 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 709 | err = copy_reference_state(dst, src); |
| 710 | if (err) |
| 711 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 712 | return copy_stack_state(dst, src); |
| 713 | } |
| 714 | |
| 715 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 716 | const struct bpf_verifier_state *src) |
| 717 | { |
| 718 | struct bpf_func_state *dst; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 719 | u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 720 | int i, err; |
| 721 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 722 | if (dst_state->jmp_history_cnt < src->jmp_history_cnt) { |
| 723 | kfree(dst_state->jmp_history); |
| 724 | dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER); |
| 725 | if (!dst_state->jmp_history) |
| 726 | return -ENOMEM; |
| 727 | } |
| 728 | memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz); |
| 729 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
| 730 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 731 | /* if dst has more stack frames then src frame, free them */ |
| 732 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 733 | free_func_state(dst_state->frame[i]); |
| 734 | dst_state->frame[i] = NULL; |
| 735 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 736 | dst_state->speculative = src->speculative; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 737 | dst_state->curframe = src->curframe; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 738 | dst_state->active_spin_lock = src->active_spin_lock; |
| 739 | dst_state->branches = src->branches; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 740 | dst_state->parent = src->parent; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 741 | dst_state->first_insn_idx = src->first_insn_idx; |
| 742 | dst_state->last_insn_idx = src->last_insn_idx; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 743 | for (i = 0; i <= src->curframe; i++) { |
| 744 | dst = dst_state->frame[i]; |
| 745 | if (!dst) { |
| 746 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 747 | if (!dst) |
| 748 | return -ENOMEM; |
| 749 | dst_state->frame[i] = dst; |
| 750 | } |
| 751 | err = copy_func_state(dst, src->frame[i]); |
| 752 | if (err) |
| 753 | return err; |
| 754 | } |
| 755 | return 0; |
| 756 | } |
| 757 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 758 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
| 759 | { |
| 760 | while (st) { |
| 761 | u32 br = --st->branches; |
| 762 | |
| 763 | /* WARN_ON(br > 1) technically makes sense here, |
| 764 | * but see comment in push_stack(), hence: |
| 765 | */ |
| 766 | WARN_ONCE((int)br < 0, |
| 767 | "BUG update_branch_counts:branches_to_explore=%d\n", |
| 768 | br); |
| 769 | if (br) |
| 770 | break; |
| 771 | st = st->parent; |
| 772 | } |
| 773 | } |
| 774 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 775 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
| 776 | int *insn_idx) |
| 777 | { |
| 778 | struct bpf_verifier_state *cur = env->cur_state; |
| 779 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 780 | int err; |
| 781 | |
| 782 | if (env->head == NULL) |
| 783 | return -ENOENT; |
| 784 | |
| 785 | if (cur) { |
| 786 | err = copy_verifier_state(cur, &head->st); |
| 787 | if (err) |
| 788 | return err; |
| 789 | } |
| 790 | if (insn_idx) |
| 791 | *insn_idx = head->insn_idx; |
| 792 | if (prev_insn_idx) |
| 793 | *prev_insn_idx = head->prev_insn_idx; |
| 794 | elem = head->next; |
| 795 | free_verifier_state(&head->st, false); |
| 796 | kfree(head); |
| 797 | env->head = elem; |
| 798 | env->stack_size--; |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 803 | int insn_idx, int prev_insn_idx, |
| 804 | bool speculative) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 805 | { |
| 806 | struct bpf_verifier_state *cur = env->cur_state; |
| 807 | struct bpf_verifier_stack_elem *elem; |
| 808 | int err; |
| 809 | |
| 810 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
| 811 | if (!elem) |
| 812 | goto err; |
| 813 | |
| 814 | elem->insn_idx = insn_idx; |
| 815 | elem->prev_insn_idx = prev_insn_idx; |
| 816 | elem->next = env->head; |
| 817 | env->head = elem; |
| 818 | env->stack_size++; |
| 819 | err = copy_verifier_state(&elem->st, cur); |
| 820 | if (err) |
| 821 | goto err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 822 | elem->st.speculative |= speculative; |
| 823 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
| 824 | verbose(env, "The sequence of %d jumps is too complex.\n", |
| 825 | env->stack_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 826 | goto err; |
| 827 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 828 | if (elem->st.parent) { |
| 829 | ++elem->st.parent->branches; |
| 830 | /* WARN_ON(branches > 2) technically makes sense here, |
| 831 | * but |
| 832 | * 1. speculative states will bump 'branches' for non-branch |
| 833 | * instructions |
| 834 | * 2. is_state_visited() heuristics may decide not to create |
| 835 | * a new state for a sequence of branches and all such current |
| 836 | * and cloned states will be pointing to a single parent state |
| 837 | * which might have large 'branches' count. |
| 838 | */ |
| 839 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 840 | return &elem->st; |
| 841 | err: |
| 842 | free_verifier_state(env->cur_state, true); |
| 843 | env->cur_state = NULL; |
| 844 | /* pop all elements and return */ |
| 845 | while (!pop_stack(env, NULL, NULL)); |
| 846 | return NULL; |
| 847 | } |
| 848 | |
| 849 | #define CALLER_SAVED_REGS 6 |
| 850 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 851 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 852 | }; |
| 853 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 854 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 855 | struct bpf_reg_state *reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 856 | |
| 857 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 858 | * known to have the value @imm. |
| 859 | */ |
| 860 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 861 | { |
| 862 | /* Clear id, off, and union(map_ptr, range) */ |
| 863 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 864 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
| 865 | reg->var_off = tnum_const(imm); |
| 866 | reg->smin_value = (s64)imm; |
| 867 | reg->smax_value = (s64)imm; |
| 868 | reg->umin_value = imm; |
| 869 | reg->umax_value = imm; |
| 870 | } |
| 871 | |
| 872 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 873 | * used only on registers holding a pointer type. |
| 874 | */ |
| 875 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 876 | { |
| 877 | __mark_reg_known(reg, 0); |
| 878 | } |
| 879 | |
| 880 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 881 | { |
| 882 | __mark_reg_known(reg, 0); |
| 883 | reg->type = SCALAR_VALUE; |
| 884 | } |
| 885 | |
| 886 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 887 | struct bpf_reg_state *regs, u32 regno) |
| 888 | { |
| 889 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
| 890 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
| 891 | /* Something bad happened, let's kill all regs */ |
| 892 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 893 | __mark_reg_not_init(env, regs + regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 894 | return; |
| 895 | } |
| 896 | __mark_reg_known_zero(regs + regno); |
| 897 | } |
| 898 | |
| 899 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 900 | { |
| 901 | return type_is_pkt_pointer(reg->type); |
| 902 | } |
| 903 | |
| 904 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 905 | { |
| 906 | return reg_is_pkt_pointer(reg) || |
| 907 | reg->type == PTR_TO_PACKET_END; |
| 908 | } |
| 909 | |
| 910 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 911 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 912 | enum bpf_reg_type which) |
| 913 | { |
| 914 | /* The register can already have a range from prior markings. |
| 915 | * This is fine as long as it hasn't been advanced from its |
| 916 | * origin. |
| 917 | */ |
| 918 | return reg->type == which && |
| 919 | reg->id == 0 && |
| 920 | reg->off == 0 && |
| 921 | tnum_equals_const(reg->var_off, 0); |
| 922 | } |
| 923 | |
| 924 | /* Attempts to improve min/max values based on var_off information */ |
| 925 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 926 | { |
| 927 | /* min signed is max(sign bit) | min(other bits) */ |
| 928 | reg->smin_value = max_t(s64, reg->smin_value, |
| 929 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 930 | /* max signed is min(sign bit) | max(other bits) */ |
| 931 | reg->smax_value = min_t(s64, reg->smax_value, |
| 932 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 933 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 934 | reg->umax_value = min(reg->umax_value, |
| 935 | reg->var_off.value | reg->var_off.mask); |
| 936 | } |
| 937 | |
| 938 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
| 939 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 940 | { |
| 941 | /* Learn sign from signed bounds. |
| 942 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 943 | * are the same, so combine. This works even in the negative case, e.g. |
| 944 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 945 | */ |
| 946 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 947 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 948 | reg->umin_value); |
| 949 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 950 | reg->umax_value); |
| 951 | return; |
| 952 | } |
| 953 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 954 | * boundary, so we must be careful. |
| 955 | */ |
| 956 | if ((s64)reg->umax_value >= 0) { |
| 957 | /* Positive. We can't learn anything from the smin, but smax |
| 958 | * is positive, hence safe. |
| 959 | */ |
| 960 | reg->smin_value = reg->umin_value; |
| 961 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 962 | reg->umax_value); |
| 963 | } else if ((s64)reg->umin_value < 0) { |
| 964 | /* Negative. We can't learn anything from the smax, but smin |
| 965 | * is negative, hence safe. |
| 966 | */ |
| 967 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 968 | reg->umin_value); |
| 969 | reg->smax_value = reg->umax_value; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 974 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 975 | { |
| 976 | reg->var_off = tnum_intersect(reg->var_off, |
| 977 | tnum_range(reg->umin_value, |
| 978 | reg->umax_value)); |
| 979 | } |
| 980 | |
| 981 | /* Reset the min/max bounds of a register */ |
| 982 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 983 | { |
| 984 | reg->smin_value = S64_MIN; |
| 985 | reg->smax_value = S64_MAX; |
| 986 | reg->umin_value = 0; |
| 987 | reg->umax_value = U64_MAX; |
| 988 | } |
| 989 | |
| 990 | /* Mark a register as having a completely unknown (scalar) value. */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 991 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
| 992 | struct bpf_reg_state *reg) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 993 | { |
| 994 | /* |
| 995 | * Clear type, id, off, and union(map_ptr, range) and |
| 996 | * padding between 'type' and union |
| 997 | */ |
| 998 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
| 999 | reg->type = SCALAR_VALUE; |
| 1000 | reg->var_off = tnum_unknown; |
| 1001 | reg->frameno = 0; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1002 | reg->precise = env->subprog_cnt > 1 || !env->allow_ptr_leaks ? |
| 1003 | true : false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1004 | __mark_reg_unbounded(reg); |
| 1005 | } |
| 1006 | |
| 1007 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 1008 | struct bpf_reg_state *regs, u32 regno) |
| 1009 | { |
| 1010 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
| 1011 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
| 1012 | /* Something bad happened, let's kill all regs except FP */ |
| 1013 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1014 | __mark_reg_not_init(env, regs + regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1015 | return; |
| 1016 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1017 | __mark_reg_unknown(env, regs + regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1020 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 1021 | struct bpf_reg_state *reg) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1022 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1023 | __mark_reg_unknown(env, reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1024 | reg->type = NOT_INIT; |
| 1025 | } |
| 1026 | |
| 1027 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 1028 | struct bpf_reg_state *regs, u32 regno) |
| 1029 | { |
| 1030 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
| 1031 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
| 1032 | /* Something bad happened, let's kill all regs except FP */ |
| 1033 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1034 | __mark_reg_not_init(env, regs + regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1035 | return; |
| 1036 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1037 | __mark_reg_not_init(env, regs + regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1040 | #define DEF_NOT_SUBREG (0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1041 | static void init_reg_state(struct bpf_verifier_env *env, |
| 1042 | struct bpf_func_state *state) |
| 1043 | { |
| 1044 | struct bpf_reg_state *regs = state->regs; |
| 1045 | int i; |
| 1046 | |
| 1047 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 1048 | mark_reg_not_init(env, regs, i); |
| 1049 | regs[i].live = REG_LIVE_NONE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1050 | regs[i].parent = NULL; |
| 1051 | regs[i].subreg_def = DEF_NOT_SUBREG; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* frame pointer */ |
| 1055 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
| 1056 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
| 1057 | regs[BPF_REG_FP].frameno = state->frameno; |
| 1058 | |
| 1059 | /* 1st arg to a function */ |
| 1060 | regs[BPF_REG_1].type = PTR_TO_CTX; |
| 1061 | mark_reg_known_zero(env, regs, BPF_REG_1); |
| 1062 | } |
| 1063 | |
| 1064 | #define BPF_MAIN_FUNC (-1) |
| 1065 | static void init_func_state(struct bpf_verifier_env *env, |
| 1066 | struct bpf_func_state *state, |
| 1067 | int callsite, int frameno, int subprogno) |
| 1068 | { |
| 1069 | state->callsite = callsite; |
| 1070 | state->frameno = frameno; |
| 1071 | state->subprogno = subprogno; |
| 1072 | init_reg_state(env, state); |
| 1073 | } |
| 1074 | |
| 1075 | enum reg_arg_type { |
| 1076 | SRC_OP, /* register is used as source operand */ |
| 1077 | DST_OP, /* register is used as destination operand */ |
| 1078 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 1079 | }; |
| 1080 | |
| 1081 | static int cmp_subprogs(const void *a, const void *b) |
| 1082 | { |
| 1083 | return ((struct bpf_subprog_info *)a)->start - |
| 1084 | ((struct bpf_subprog_info *)b)->start; |
| 1085 | } |
| 1086 | |
| 1087 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 1088 | { |
| 1089 | struct bpf_subprog_info *p; |
| 1090 | |
| 1091 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 1092 | sizeof(env->subprog_info[0]), cmp_subprogs); |
| 1093 | if (!p) |
| 1094 | return -ENOENT; |
| 1095 | return p - env->subprog_info; |
| 1096 | |
| 1097 | } |
| 1098 | |
| 1099 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 1100 | { |
| 1101 | int insn_cnt = env->prog->len; |
| 1102 | int ret; |
| 1103 | |
| 1104 | if (off >= insn_cnt || off < 0) { |
| 1105 | verbose(env, "call to invalid destination\n"); |
| 1106 | return -EINVAL; |
| 1107 | } |
| 1108 | ret = find_subprog(env, off); |
| 1109 | if (ret >= 0) |
| 1110 | return 0; |
| 1111 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
| 1112 | verbose(env, "too many subprograms\n"); |
| 1113 | return -E2BIG; |
| 1114 | } |
| 1115 | env->subprog_info[env->subprog_cnt++].start = off; |
| 1116 | sort(env->subprog_info, env->subprog_cnt, |
| 1117 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | static int check_subprogs(struct bpf_verifier_env *env) |
| 1122 | { |
| 1123 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
| 1124 | struct bpf_subprog_info *subprog = env->subprog_info; |
| 1125 | struct bpf_insn *insn = env->prog->insnsi; |
| 1126 | int insn_cnt = env->prog->len; |
| 1127 | |
| 1128 | /* Add entry function. */ |
| 1129 | ret = add_subprog(env, 0); |
| 1130 | if (ret < 0) |
| 1131 | return ret; |
| 1132 | |
| 1133 | /* determine subprog starts. The end is one before the next starts */ |
| 1134 | for (i = 0; i < insn_cnt; i++) { |
| 1135 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1136 | continue; |
| 1137 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1138 | continue; |
| 1139 | if (!env->allow_ptr_leaks) { |
| 1140 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); |
| 1141 | return -EPERM; |
| 1142 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1143 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 1144 | if (ret < 0) |
| 1145 | return ret; |
| 1146 | } |
| 1147 | |
| 1148 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 1149 | * logic. 'subprog_cnt' should not be increased. |
| 1150 | */ |
| 1151 | subprog[env->subprog_cnt].start = insn_cnt; |
| 1152 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1153 | if (env->log.level & BPF_LOG_LEVEL2) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1154 | for (i = 0; i < env->subprog_cnt; i++) |
| 1155 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
| 1156 | |
| 1157 | /* now check that all jumps are within the same subprog */ |
| 1158 | subprog_start = subprog[cur_subprog].start; |
| 1159 | subprog_end = subprog[cur_subprog + 1].start; |
| 1160 | for (i = 0; i < insn_cnt; i++) { |
| 1161 | u8 code = insn[i].code; |
| 1162 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1163 | if (code == (BPF_JMP | BPF_CALL) && |
| 1164 | insn[i].imm == BPF_FUNC_tail_call && |
| 1165 | insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1166 | subprog[cur_subprog].has_tail_call = true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1167 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1168 | goto next; |
| 1169 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 1170 | goto next; |
| 1171 | off = i + insn[i].off + 1; |
| 1172 | if (off < subprog_start || off >= subprog_end) { |
| 1173 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 1174 | return -EINVAL; |
| 1175 | } |
| 1176 | next: |
| 1177 | if (i == subprog_end - 1) { |
| 1178 | /* to avoid fall-through from one subprog into another |
| 1179 | * the last insn of the subprog should be either exit |
| 1180 | * or unconditional jump back |
| 1181 | */ |
| 1182 | if (code != (BPF_JMP | BPF_EXIT) && |
| 1183 | code != (BPF_JMP | BPF_JA)) { |
| 1184 | verbose(env, "last insn is not an exit or jmp\n"); |
| 1185 | return -EINVAL; |
| 1186 | } |
| 1187 | subprog_start = subprog_end; |
| 1188 | cur_subprog++; |
| 1189 | if (cur_subprog < env->subprog_cnt) |
| 1190 | subprog_end = subprog[cur_subprog + 1].start; |
| 1191 | } |
| 1192 | } |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1196 | /* Parentage chain of this register (or stack slot) should take care of all |
| 1197 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 1198 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1199 | static int mark_reg_read(struct bpf_verifier_env *env, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1200 | const struct bpf_reg_state *state, |
| 1201 | struct bpf_reg_state *parent, u8 flag) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1202 | { |
| 1203 | bool writes = parent == state->parent; /* Observe write marks */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1204 | int cnt = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1205 | |
| 1206 | while (parent) { |
| 1207 | /* if read wasn't screened by an earlier write ... */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1208 | if (writes && state->live & REG_LIVE_WRITTEN) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1209 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1210 | if (parent->live & REG_LIVE_DONE) { |
| 1211 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", |
| 1212 | reg_type_str[parent->type], |
| 1213 | parent->var_off.value, parent->off); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1214 | return -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1215 | } |
| 1216 | /* The first condition is more likely to be true than the |
| 1217 | * second, checked it first. |
| 1218 | */ |
| 1219 | if ((parent->live & REG_LIVE_READ) == flag || |
| 1220 | parent->live & REG_LIVE_READ64) |
| 1221 | /* The parentage chain never changes and |
| 1222 | * this parent was already marked as LIVE_READ. |
| 1223 | * There is no need to keep walking the chain again and |
| 1224 | * keep re-marking all parents as LIVE_READ. |
| 1225 | * This case happens when the same register is read |
| 1226 | * multiple times without writes into it in-between. |
| 1227 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
| 1228 | * then no need to set the weak REG_LIVE_READ32. |
| 1229 | */ |
| 1230 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1231 | /* ... then we depend on parent's value */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1232 | parent->live |= flag; |
| 1233 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ |
| 1234 | if (flag == REG_LIVE_READ64) |
| 1235 | parent->live &= ~REG_LIVE_READ32; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1236 | state = parent; |
| 1237 | parent = state->parent; |
| 1238 | writes = true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1239 | cnt++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1240 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1241 | |
| 1242 | if (env->longest_mark_read_walk < cnt) |
| 1243 | env->longest_mark_read_walk = cnt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1244 | return 0; |
| 1245 | } |
| 1246 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1247 | /* This function is supposed to be used by the following 32-bit optimization |
| 1248 | * code only. It returns TRUE if the source or destination register operates |
| 1249 | * on 64-bit, otherwise return FALSE. |
| 1250 | */ |
| 1251 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 1252 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) |
| 1253 | { |
| 1254 | u8 code, class, op; |
| 1255 | |
| 1256 | code = insn->code; |
| 1257 | class = BPF_CLASS(code); |
| 1258 | op = BPF_OP(code); |
| 1259 | if (class == BPF_JMP) { |
| 1260 | /* BPF_EXIT for "main" will reach here. Return TRUE |
| 1261 | * conservatively. |
| 1262 | */ |
| 1263 | if (op == BPF_EXIT) |
| 1264 | return true; |
| 1265 | if (op == BPF_CALL) { |
| 1266 | /* BPF to BPF call will reach here because of marking |
| 1267 | * caller saved clobber with DST_OP_NO_MARK for which we |
| 1268 | * don't care the register def because they are anyway |
| 1269 | * marked as NOT_INIT already. |
| 1270 | */ |
| 1271 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1272 | return false; |
| 1273 | /* Helper call will reach here because of arg type |
| 1274 | * check, conservatively return TRUE. |
| 1275 | */ |
| 1276 | if (t == SRC_OP) |
| 1277 | return true; |
| 1278 | |
| 1279 | return false; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | if (class == BPF_ALU64 || class == BPF_JMP || |
| 1284 | /* BPF_END always use BPF_ALU class. */ |
| 1285 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) |
| 1286 | return true; |
| 1287 | |
| 1288 | if (class == BPF_ALU || class == BPF_JMP32) |
| 1289 | return false; |
| 1290 | |
| 1291 | if (class == BPF_LDX) { |
| 1292 | if (t != SRC_OP) |
| 1293 | return BPF_SIZE(code) == BPF_DW; |
| 1294 | /* LDX source must be ptr. */ |
| 1295 | return true; |
| 1296 | } |
| 1297 | |
| 1298 | if (class == BPF_STX) { |
| 1299 | if (reg->type != SCALAR_VALUE) |
| 1300 | return true; |
| 1301 | return BPF_SIZE(code) == BPF_DW; |
| 1302 | } |
| 1303 | |
| 1304 | if (class == BPF_LD) { |
| 1305 | u8 mode = BPF_MODE(code); |
| 1306 | |
| 1307 | /* LD_IMM64 */ |
| 1308 | if (mode == BPF_IMM) |
| 1309 | return true; |
| 1310 | |
| 1311 | /* Both LD_IND and LD_ABS return 32-bit data. */ |
| 1312 | if (t != SRC_OP) |
| 1313 | return false; |
| 1314 | |
| 1315 | /* Implicit ctx ptr. */ |
| 1316 | if (regno == BPF_REG_6) |
| 1317 | return true; |
| 1318 | |
| 1319 | /* Explicit source could be any width. */ |
| 1320 | return true; |
| 1321 | } |
| 1322 | |
| 1323 | if (class == BPF_ST) |
| 1324 | /* The only source register for BPF_ST is a ptr. */ |
| 1325 | return true; |
| 1326 | |
| 1327 | /* Conservatively return true at default. */ |
| 1328 | return true; |
| 1329 | } |
| 1330 | |
| 1331 | /* Return TRUE if INSN doesn't have explicit value define. */ |
| 1332 | static bool insn_no_def(struct bpf_insn *insn) |
| 1333 | { |
| 1334 | u8 class = BPF_CLASS(insn->code); |
| 1335 | |
| 1336 | return (class == BPF_JMP || class == BPF_JMP32 || |
| 1337 | class == BPF_STX || class == BPF_ST); |
| 1338 | } |
| 1339 | |
| 1340 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ |
| 1341 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 1342 | { |
| 1343 | if (insn_no_def(insn)) |
| 1344 | return false; |
| 1345 | |
| 1346 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); |
| 1347 | } |
| 1348 | |
| 1349 | static void mark_insn_zext(struct bpf_verifier_env *env, |
| 1350 | struct bpf_reg_state *reg) |
| 1351 | { |
| 1352 | s32 def_idx = reg->subreg_def; |
| 1353 | |
| 1354 | if (def_idx == DEF_NOT_SUBREG) |
| 1355 | return; |
| 1356 | |
| 1357 | env->insn_aux_data[def_idx - 1].zext_dst = true; |
| 1358 | /* The dst will be zero extended, so won't be sub-register anymore. */ |
| 1359 | reg->subreg_def = DEF_NOT_SUBREG; |
| 1360 | } |
| 1361 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1362 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
| 1363 | enum reg_arg_type t) |
| 1364 | { |
| 1365 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1366 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1367 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
| 1368 | struct bpf_reg_state *reg, *regs = state->regs; |
| 1369 | bool rw64; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1370 | |
| 1371 | if (regno >= MAX_BPF_REG) { |
| 1372 | verbose(env, "R%d is invalid\n", regno); |
| 1373 | return -EINVAL; |
| 1374 | } |
| 1375 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1376 | reg = ®s[regno]; |
| 1377 | rw64 = is_reg64(env, insn, regno, reg, t); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1378 | if (t == SRC_OP) { |
| 1379 | /* check whether register used as source operand can be read */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1380 | if (reg->type == NOT_INIT) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1381 | verbose(env, "R%d !read_ok\n", regno); |
| 1382 | return -EACCES; |
| 1383 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1384 | /* We don't need to worry about FP liveness because it's read-only */ |
| 1385 | if (regno == BPF_REG_FP) |
| 1386 | return 0; |
| 1387 | |
| 1388 | if (rw64) |
| 1389 | mark_insn_zext(env, reg); |
| 1390 | |
| 1391 | return mark_reg_read(env, reg, reg->parent, |
| 1392 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1393 | } else { |
| 1394 | /* check whether register used as dest operand can be written to */ |
| 1395 | if (regno == BPF_REG_FP) { |
| 1396 | verbose(env, "frame pointer is read only\n"); |
| 1397 | return -EACCES; |
| 1398 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1399 | reg->live |= REG_LIVE_WRITTEN; |
| 1400 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1401 | if (t == DST_OP) |
| 1402 | mark_reg_unknown(env, regs, regno); |
| 1403 | } |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1407 | /* for any branch, call, exit record the history of jmps in the given state */ |
| 1408 | static int push_jmp_history(struct bpf_verifier_env *env, |
| 1409 | struct bpf_verifier_state *cur) |
| 1410 | { |
| 1411 | u32 cnt = cur->jmp_history_cnt; |
| 1412 | struct bpf_idx_pair *p; |
| 1413 | |
| 1414 | cnt++; |
| 1415 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); |
| 1416 | if (!p) |
| 1417 | return -ENOMEM; |
| 1418 | p[cnt - 1].idx = env->insn_idx; |
| 1419 | p[cnt - 1].prev_idx = env->prev_insn_idx; |
| 1420 | cur->jmp_history = p; |
| 1421 | cur->jmp_history_cnt = cnt; |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
| 1425 | /* Backtrack one insn at a time. If idx is not at the top of recorded |
| 1426 | * history then previous instruction came from straight line execution. |
| 1427 | */ |
| 1428 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, |
| 1429 | u32 *history) |
| 1430 | { |
| 1431 | u32 cnt = *history; |
| 1432 | |
| 1433 | if (cnt && st->jmp_history[cnt - 1].idx == i) { |
| 1434 | i = st->jmp_history[cnt - 1].prev_idx; |
| 1435 | (*history)--; |
| 1436 | } else { |
| 1437 | i--; |
| 1438 | } |
| 1439 | return i; |
| 1440 | } |
| 1441 | |
| 1442 | /* For given verifier state backtrack_insn() is called from the last insn to |
| 1443 | * the first insn. Its purpose is to compute a bitmask of registers and |
| 1444 | * stack slots that needs precision in the parent verifier state. |
| 1445 | */ |
| 1446 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, |
| 1447 | u32 *reg_mask, u64 *stack_mask) |
| 1448 | { |
| 1449 | const struct bpf_insn_cbs cbs = { |
| 1450 | .cb_print = verbose, |
| 1451 | .private_data = env, |
| 1452 | }; |
| 1453 | struct bpf_insn *insn = env->prog->insnsi + idx; |
| 1454 | u8 class = BPF_CLASS(insn->code); |
| 1455 | u8 opcode = BPF_OP(insn->code); |
| 1456 | u8 mode = BPF_MODE(insn->code); |
| 1457 | u32 dreg = 1u << insn->dst_reg; |
| 1458 | u32 sreg = 1u << insn->src_reg; |
| 1459 | u32 spi; |
| 1460 | |
| 1461 | if (insn->code == 0) |
| 1462 | return 0; |
| 1463 | if (env->log.level & BPF_LOG_LEVEL) { |
| 1464 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
| 1465 | verbose(env, "%d: ", idx); |
| 1466 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
| 1467 | } |
| 1468 | |
| 1469 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 1470 | if (!(*reg_mask & dreg)) |
| 1471 | return 0; |
| 1472 | if (opcode == BPF_MOV) { |
| 1473 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1474 | /* dreg = sreg |
| 1475 | * dreg needs precision after this insn |
| 1476 | * sreg needs precision before this insn |
| 1477 | */ |
| 1478 | *reg_mask &= ~dreg; |
| 1479 | *reg_mask |= sreg; |
| 1480 | } else { |
| 1481 | /* dreg = K |
| 1482 | * dreg needs precision after this insn. |
| 1483 | * Corresponding register is already marked |
| 1484 | * as precise=true in this verifier state. |
| 1485 | * No further markings in parent are necessary |
| 1486 | */ |
| 1487 | *reg_mask &= ~dreg; |
| 1488 | } |
| 1489 | } else { |
| 1490 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1491 | /* dreg += sreg |
| 1492 | * both dreg and sreg need precision |
| 1493 | * before this insn |
| 1494 | */ |
| 1495 | *reg_mask |= sreg; |
| 1496 | } /* else dreg += K |
| 1497 | * dreg still needs precision before this insn |
| 1498 | */ |
| 1499 | } |
| 1500 | } else if (class == BPF_LDX) { |
| 1501 | if (!(*reg_mask & dreg)) |
| 1502 | return 0; |
| 1503 | *reg_mask &= ~dreg; |
| 1504 | |
| 1505 | /* scalars can only be spilled into stack w/o losing precision. |
| 1506 | * Load from any other memory can be zero extended. |
| 1507 | * The desire to keep that precision is already indicated |
| 1508 | * by 'precise' mark in corresponding register of this state. |
| 1509 | * No further tracking necessary. |
| 1510 | */ |
| 1511 | if (insn->src_reg != BPF_REG_FP) |
| 1512 | return 0; |
| 1513 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 1514 | return 0; |
| 1515 | |
| 1516 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. |
| 1517 | * that [fp - off] slot contains scalar that needs to be |
| 1518 | * tracked with precision |
| 1519 | */ |
| 1520 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 1521 | if (spi >= 64) { |
| 1522 | verbose(env, "BUG spi %d\n", spi); |
| 1523 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1524 | return -EFAULT; |
| 1525 | } |
| 1526 | *stack_mask |= 1ull << spi; |
| 1527 | } else if (class == BPF_STX || class == BPF_ST) { |
| 1528 | if (*reg_mask & dreg) |
| 1529 | /* stx & st shouldn't be using _scalar_ dst_reg |
| 1530 | * to access memory. It means backtracking |
| 1531 | * encountered a case of pointer subtraction. |
| 1532 | */ |
| 1533 | return -ENOTSUPP; |
| 1534 | /* scalars can only be spilled into stack */ |
| 1535 | if (insn->dst_reg != BPF_REG_FP) |
| 1536 | return 0; |
| 1537 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 1538 | return 0; |
| 1539 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 1540 | if (spi >= 64) { |
| 1541 | verbose(env, "BUG spi %d\n", spi); |
| 1542 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1543 | return -EFAULT; |
| 1544 | } |
| 1545 | if (!(*stack_mask & (1ull << spi))) |
| 1546 | return 0; |
| 1547 | *stack_mask &= ~(1ull << spi); |
| 1548 | if (class == BPF_STX) |
| 1549 | *reg_mask |= sreg; |
| 1550 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
| 1551 | if (opcode == BPF_CALL) { |
| 1552 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1553 | return -ENOTSUPP; |
| 1554 | /* regular helper call sets R0 */ |
| 1555 | *reg_mask &= ~1; |
| 1556 | if (*reg_mask & 0x3f) { |
| 1557 | /* if backtracing was looking for registers R1-R5 |
| 1558 | * they should have been found already. |
| 1559 | */ |
| 1560 | verbose(env, "BUG regs %x\n", *reg_mask); |
| 1561 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1562 | return -EFAULT; |
| 1563 | } |
| 1564 | } else if (opcode == BPF_EXIT) { |
| 1565 | return -ENOTSUPP; |
| 1566 | } |
| 1567 | } else if (class == BPF_LD) { |
| 1568 | if (!(*reg_mask & dreg)) |
| 1569 | return 0; |
| 1570 | *reg_mask &= ~dreg; |
| 1571 | /* It's ld_imm64 or ld_abs or ld_ind. |
| 1572 | * For ld_imm64 no further tracking of precision |
| 1573 | * into parent is necessary |
| 1574 | */ |
| 1575 | if (mode == BPF_IND || mode == BPF_ABS) |
| 1576 | /* to be analyzed */ |
| 1577 | return -ENOTSUPP; |
| 1578 | } |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
| 1582 | /* the scalar precision tracking algorithm: |
| 1583 | * . at the start all registers have precise=false. |
| 1584 | * . scalar ranges are tracked as normal through alu and jmp insns. |
| 1585 | * . once precise value of the scalar register is used in: |
| 1586 | * . ptr + scalar alu |
| 1587 | * . if (scalar cond K|scalar) |
| 1588 | * . helper_call(.., scalar, ...) where ARG_CONST is expected |
| 1589 | * backtrack through the verifier states and mark all registers and |
| 1590 | * stack slots with spilled constants that these scalar regisers |
| 1591 | * should be precise. |
| 1592 | * . during state pruning two registers (or spilled stack slots) |
| 1593 | * are equivalent if both are not precise. |
| 1594 | * |
| 1595 | * Note the verifier cannot simply walk register parentage chain, |
| 1596 | * since many different registers and stack slots could have been |
| 1597 | * used to compute single precise scalar. |
| 1598 | * |
| 1599 | * The approach of starting with precise=true for all registers and then |
| 1600 | * backtrack to mark a register as not precise when the verifier detects |
| 1601 | * that program doesn't care about specific value (e.g., when helper |
| 1602 | * takes register as ARG_ANYTHING parameter) is not safe. |
| 1603 | * |
| 1604 | * It's ok to walk single parentage chain of the verifier states. |
| 1605 | * It's possible that this backtracking will go all the way till 1st insn. |
| 1606 | * All other branches will be explored for needing precision later. |
| 1607 | * |
| 1608 | * The backtracking needs to deal with cases like: |
| 1609 | * 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) |
| 1610 | * r9 -= r8 |
| 1611 | * r5 = r9 |
| 1612 | * if r5 > 0x79f goto pc+7 |
| 1613 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) |
| 1614 | * r5 += 1 |
| 1615 | * ... |
| 1616 | * call bpf_perf_event_output#25 |
| 1617 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO |
| 1618 | * |
| 1619 | * and this case: |
| 1620 | * r6 = 1 |
| 1621 | * call foo // uses callee's r6 inside to compute r0 |
| 1622 | * r0 += r6 |
| 1623 | * if r0 == 0 goto |
| 1624 | * |
| 1625 | * to track above reg_mask/stack_mask needs to be independent for each frame. |
| 1626 | * |
| 1627 | * Also if parent's curframe > frame where backtracking started, |
| 1628 | * the verifier need to mark registers in both frames, otherwise callees |
| 1629 | * may incorrectly prune callers. This is similar to |
| 1630 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") |
| 1631 | * |
| 1632 | * For now backtracking falls back into conservative marking. |
| 1633 | */ |
| 1634 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, |
| 1635 | struct bpf_verifier_state *st) |
| 1636 | { |
| 1637 | struct bpf_func_state *func; |
| 1638 | struct bpf_reg_state *reg; |
| 1639 | int i, j; |
| 1640 | |
| 1641 | /* big hammer: mark all scalars precise in this path. |
| 1642 | * pop_stack may still get !precise scalars. |
| 1643 | */ |
| 1644 | for (; st; st = st->parent) |
| 1645 | for (i = 0; i <= st->curframe; i++) { |
| 1646 | func = st->frame[i]; |
| 1647 | for (j = 0; j < BPF_REG_FP; j++) { |
| 1648 | reg = &func->regs[j]; |
| 1649 | if (reg->type != SCALAR_VALUE) |
| 1650 | continue; |
| 1651 | reg->precise = true; |
| 1652 | } |
| 1653 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { |
| 1654 | if (func->stack[j].slot_type[0] != STACK_SPILL) |
| 1655 | continue; |
| 1656 | reg = &func->stack[j].spilled_ptr; |
| 1657 | if (reg->type != SCALAR_VALUE) |
| 1658 | continue; |
| 1659 | reg->precise = true; |
| 1660 | } |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
| 1665 | int spi) |
| 1666 | { |
| 1667 | struct bpf_verifier_state *st = env->cur_state; |
| 1668 | int first_idx = st->first_insn_idx; |
| 1669 | int last_idx = env->insn_idx; |
| 1670 | struct bpf_func_state *func; |
| 1671 | struct bpf_reg_state *reg; |
| 1672 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
| 1673 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; |
| 1674 | bool skip_first = true; |
| 1675 | bool new_marks = false; |
| 1676 | int i, err; |
| 1677 | |
| 1678 | if (!env->allow_ptr_leaks) |
| 1679 | /* backtracking is root only for now */ |
| 1680 | return 0; |
| 1681 | |
| 1682 | func = st->frame[st->curframe]; |
| 1683 | if (regno >= 0) { |
| 1684 | reg = &func->regs[regno]; |
| 1685 | if (reg->type != SCALAR_VALUE) { |
| 1686 | WARN_ONCE(1, "backtracing misuse"); |
| 1687 | return -EFAULT; |
| 1688 | } |
| 1689 | if (!reg->precise) |
| 1690 | new_marks = true; |
| 1691 | else |
| 1692 | reg_mask = 0; |
| 1693 | reg->precise = true; |
| 1694 | } |
| 1695 | |
| 1696 | while (spi >= 0) { |
| 1697 | if (func->stack[spi].slot_type[0] != STACK_SPILL) { |
| 1698 | stack_mask = 0; |
| 1699 | break; |
| 1700 | } |
| 1701 | reg = &func->stack[spi].spilled_ptr; |
| 1702 | if (reg->type != SCALAR_VALUE) { |
| 1703 | stack_mask = 0; |
| 1704 | break; |
| 1705 | } |
| 1706 | if (!reg->precise) |
| 1707 | new_marks = true; |
| 1708 | else |
| 1709 | stack_mask = 0; |
| 1710 | reg->precise = true; |
| 1711 | break; |
| 1712 | } |
| 1713 | |
| 1714 | if (!new_marks) |
| 1715 | return 0; |
| 1716 | if (!reg_mask && !stack_mask) |
| 1717 | return 0; |
| 1718 | for (;;) { |
| 1719 | DECLARE_BITMAP(mask, 64); |
| 1720 | u32 history = st->jmp_history_cnt; |
| 1721 | |
| 1722 | if (env->log.level & BPF_LOG_LEVEL) |
| 1723 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
| 1724 | for (i = last_idx;;) { |
| 1725 | if (skip_first) { |
| 1726 | err = 0; |
| 1727 | skip_first = false; |
| 1728 | } else { |
| 1729 | err = backtrack_insn(env, i, ®_mask, &stack_mask); |
| 1730 | } |
| 1731 | if (err == -ENOTSUPP) { |
| 1732 | mark_all_scalars_precise(env, st); |
| 1733 | return 0; |
| 1734 | } else if (err) { |
| 1735 | return err; |
| 1736 | } |
| 1737 | if (!reg_mask && !stack_mask) |
| 1738 | /* Found assignment(s) into tracked register in this state. |
| 1739 | * Since this state is already marked, just return. |
| 1740 | * Nothing to be tracked further in the parent state. |
| 1741 | */ |
| 1742 | return 0; |
| 1743 | if (i == first_idx) |
| 1744 | break; |
| 1745 | i = get_prev_insn_idx(st, i, &history); |
| 1746 | if (i >= env->prog->len) { |
| 1747 | /* This can happen if backtracking reached insn 0 |
| 1748 | * and there are still reg_mask or stack_mask |
| 1749 | * to backtrack. |
| 1750 | * It means the backtracking missed the spot where |
| 1751 | * particular register was initialized with a constant. |
| 1752 | */ |
| 1753 | verbose(env, "BUG backtracking idx %d\n", i); |
| 1754 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1755 | return -EFAULT; |
| 1756 | } |
| 1757 | } |
| 1758 | st = st->parent; |
| 1759 | if (!st) |
| 1760 | break; |
| 1761 | |
| 1762 | new_marks = false; |
| 1763 | func = st->frame[st->curframe]; |
| 1764 | bitmap_from_u64(mask, reg_mask); |
| 1765 | for_each_set_bit(i, mask, 32) { |
| 1766 | reg = &func->regs[i]; |
| 1767 | if (reg->type != SCALAR_VALUE) { |
| 1768 | reg_mask &= ~(1u << i); |
| 1769 | continue; |
| 1770 | } |
| 1771 | if (!reg->precise) |
| 1772 | new_marks = true; |
| 1773 | reg->precise = true; |
| 1774 | } |
| 1775 | |
| 1776 | bitmap_from_u64(mask, stack_mask); |
| 1777 | for_each_set_bit(i, mask, 64) { |
| 1778 | if (i >= func->allocated_stack / BPF_REG_SIZE) { |
| 1779 | /* the sequence of instructions: |
| 1780 | * 2: (bf) r3 = r10 |
| 1781 | * 3: (7b) *(u64 *)(r3 -8) = r0 |
| 1782 | * 4: (79) r4 = *(u64 *)(r10 -8) |
| 1783 | * doesn't contain jmps. It's backtracked |
| 1784 | * as a single block. |
| 1785 | * During backtracking insn 3 is not recognized as |
| 1786 | * stack access, so at the end of backtracking |
| 1787 | * stack slot fp-8 is still marked in stack_mask. |
| 1788 | * However the parent state may not have accessed |
| 1789 | * fp-8 and it's "unallocated" stack space. |
| 1790 | * In such case fallback to conservative. |
| 1791 | */ |
| 1792 | mark_all_scalars_precise(env, st); |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
| 1796 | if (func->stack[i].slot_type[0] != STACK_SPILL) { |
| 1797 | stack_mask &= ~(1ull << i); |
| 1798 | continue; |
| 1799 | } |
| 1800 | reg = &func->stack[i].spilled_ptr; |
| 1801 | if (reg->type != SCALAR_VALUE) { |
| 1802 | stack_mask &= ~(1ull << i); |
| 1803 | continue; |
| 1804 | } |
| 1805 | if (!reg->precise) |
| 1806 | new_marks = true; |
| 1807 | reg->precise = true; |
| 1808 | } |
| 1809 | if (env->log.level & BPF_LOG_LEVEL) { |
| 1810 | print_verifier_state(env, func); |
| 1811 | verbose(env, "parent %s regs=%x stack=%llx marks\n", |
| 1812 | new_marks ? "didn't have" : "already had", |
| 1813 | reg_mask, stack_mask); |
| 1814 | } |
| 1815 | |
| 1816 | if (!reg_mask && !stack_mask) |
| 1817 | break; |
| 1818 | if (!new_marks) |
| 1819 | break; |
| 1820 | |
| 1821 | last_idx = st->last_insn_idx; |
| 1822 | first_idx = st->first_insn_idx; |
| 1823 | } |
| 1824 | return 0; |
| 1825 | } |
| 1826 | |
| 1827 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
| 1828 | { |
| 1829 | return __mark_chain_precision(env, regno, -1); |
| 1830 | } |
| 1831 | |
| 1832 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) |
| 1833 | { |
| 1834 | return __mark_chain_precision(env, -1, spi); |
| 1835 | } |
| 1836 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1837 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 1838 | { |
| 1839 | switch (type) { |
| 1840 | case PTR_TO_MAP_VALUE: |
| 1841 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 1842 | case PTR_TO_STACK: |
| 1843 | case PTR_TO_CTX: |
| 1844 | case PTR_TO_PACKET: |
| 1845 | case PTR_TO_PACKET_META: |
| 1846 | case PTR_TO_PACKET_END: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1847 | case PTR_TO_FLOW_KEYS: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1848 | case CONST_PTR_TO_MAP: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1849 | case PTR_TO_SOCKET: |
| 1850 | case PTR_TO_SOCKET_OR_NULL: |
| 1851 | case PTR_TO_SOCK_COMMON: |
| 1852 | case PTR_TO_SOCK_COMMON_OR_NULL: |
| 1853 | case PTR_TO_TCP_SOCK: |
| 1854 | case PTR_TO_TCP_SOCK_OR_NULL: |
| 1855 | case PTR_TO_XDP_SOCK: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1856 | return true; |
| 1857 | default: |
| 1858 | return false; |
| 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | /* Does this register contain a constant zero? */ |
| 1863 | static bool register_is_null(struct bpf_reg_state *reg) |
| 1864 | { |
| 1865 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 1866 | } |
| 1867 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1868 | static bool register_is_const(struct bpf_reg_state *reg) |
| 1869 | { |
| 1870 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); |
| 1871 | } |
| 1872 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1873 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 1874 | const struct bpf_reg_state *reg) |
| 1875 | { |
| 1876 | if (allow_ptr_leaks) |
| 1877 | return false; |
| 1878 | |
| 1879 | return reg->type != SCALAR_VALUE; |
| 1880 | } |
| 1881 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1882 | static void save_register_state(struct bpf_func_state *state, |
| 1883 | int spi, struct bpf_reg_state *reg) |
| 1884 | { |
| 1885 | int i; |
| 1886 | |
| 1887 | state->stack[spi].spilled_ptr = *reg; |
| 1888 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 1889 | |
| 1890 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 1891 | state->stack[spi].slot_type[i] = STACK_SPILL; |
| 1892 | } |
| 1893 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1894 | /* check_stack_read/write functions track spill/fill of registers, |
| 1895 | * stack boundary and alignment are checked in check_mem_access() |
| 1896 | */ |
| 1897 | static int check_stack_write(struct bpf_verifier_env *env, |
| 1898 | struct bpf_func_state *state, /* func where register points to */ |
| 1899 | int off, int size, int value_regno, int insn_idx) |
| 1900 | { |
| 1901 | struct bpf_func_state *cur; /* state of the current function */ |
| 1902 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1903 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
| 1904 | struct bpf_reg_state *reg = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1905 | |
| 1906 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1907 | state->acquired_refs, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1908 | if (err) |
| 1909 | return err; |
| 1910 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 1911 | * so it's aligned access and [off, off + size) are within stack limits |
| 1912 | */ |
| 1913 | if (!env->allow_ptr_leaks && |
| 1914 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 1915 | size != BPF_REG_SIZE) { |
| 1916 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 1917 | return -EACCES; |
| 1918 | } |
| 1919 | |
| 1920 | cur = env->cur_state->frame[env->cur_state->curframe]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1921 | if (value_regno >= 0) |
| 1922 | reg = &cur->regs[value_regno]; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1923 | if (!env->allow_ptr_leaks) { |
| 1924 | bool sanitize = reg && is_spillable_regtype(reg->type); |
| 1925 | |
| 1926 | for (i = 0; i < size; i++) { |
| 1927 | if (state->stack[spi].slot_type[i] == STACK_INVALID) { |
| 1928 | sanitize = true; |
| 1929 | break; |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | if (sanitize) |
| 1934 | env->insn_aux_data[insn_idx].sanitize_stack_spill = true; |
| 1935 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1936 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1937 | if (reg && size == BPF_REG_SIZE && register_is_const(reg) && |
| 1938 | !register_is_null(reg) && env->allow_ptr_leaks) { |
| 1939 | if (dst_reg != BPF_REG_FP) { |
| 1940 | /* The backtracking logic can only recognize explicit |
| 1941 | * stack slot address like [fp - 8]. Other spill of |
| 1942 | * scalar via different register has to be conervative. |
| 1943 | * Backtrack from here and mark all registers as precise |
| 1944 | * that contributed into 'reg' being a constant. |
| 1945 | */ |
| 1946 | err = mark_chain_precision(env, value_regno); |
| 1947 | if (err) |
| 1948 | return err; |
| 1949 | } |
| 1950 | save_register_state(state, spi, reg); |
| 1951 | } else if (reg && is_spillable_regtype(reg->type)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1952 | /* register containing pointer is being spilled into stack */ |
| 1953 | if (size != BPF_REG_SIZE) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1954 | verbose_linfo(env, insn_idx, "; "); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1955 | verbose(env, "invalid size of register spill\n"); |
| 1956 | return -EACCES; |
| 1957 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1958 | if (state != cur && reg->type == PTR_TO_STACK) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1959 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 1960 | return -EINVAL; |
| 1961 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1962 | save_register_state(state, spi, reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1963 | } else { |
| 1964 | u8 type = STACK_MISC; |
| 1965 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1966 | /* regular write of data into stack destroys any spilled ptr */ |
| 1967 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
| 1968 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
| 1969 | if (state->stack[spi].slot_type[0] == STACK_SPILL) |
| 1970 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 1971 | state->stack[spi].slot_type[i] = STACK_MISC; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1972 | |
| 1973 | /* only mark the slot as written if all 8 bytes were written |
| 1974 | * otherwise read propagation may incorrectly stop too soon |
| 1975 | * when stack slots are partially written. |
| 1976 | * This heuristic means that read propagation will be |
| 1977 | * conservative, since it will add reg_live_read marks |
| 1978 | * to stack slots all the way to first state when programs |
| 1979 | * writes+reads less than 8 bytes |
| 1980 | */ |
| 1981 | if (size == BPF_REG_SIZE) |
| 1982 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 1983 | |
| 1984 | /* when we zero initialize stack slots mark them as such */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1985 | if (reg && register_is_null(reg)) { |
| 1986 | /* backtracking doesn't work for STACK_ZERO yet. */ |
| 1987 | err = mark_chain_precision(env, value_regno); |
| 1988 | if (err) |
| 1989 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1990 | type = STACK_ZERO; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1991 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1992 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1993 | /* Mark slots affected by this stack write. */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1994 | for (i = 0; i < size; i++) |
| 1995 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
| 1996 | type; |
| 1997 | } |
| 1998 | return 0; |
| 1999 | } |
| 2000 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2001 | static int check_stack_read(struct bpf_verifier_env *env, |
| 2002 | struct bpf_func_state *reg_state /* func where register points to */, |
| 2003 | int off, int size, int value_regno) |
| 2004 | { |
| 2005 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2006 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2007 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2008 | struct bpf_reg_state *reg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2009 | u8 *stype; |
| 2010 | |
| 2011 | if (reg_state->allocated_stack <= slot) { |
| 2012 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 2013 | off, size); |
| 2014 | return -EACCES; |
| 2015 | } |
| 2016 | stype = reg_state->stack[spi].slot_type; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2017 | reg = ®_state->stack[spi].spilled_ptr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2018 | |
| 2019 | if (stype[0] == STACK_SPILL) { |
| 2020 | if (size != BPF_REG_SIZE) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2021 | if (reg->type != SCALAR_VALUE) { |
| 2022 | verbose_linfo(env, env->insn_idx, "; "); |
| 2023 | verbose(env, "invalid size of register fill\n"); |
| 2024 | return -EACCES; |
| 2025 | } |
| 2026 | if (value_regno >= 0) { |
| 2027 | mark_reg_unknown(env, state->regs, value_regno); |
| 2028 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 2029 | } |
| 2030 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
| 2031 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2032 | } |
| 2033 | for (i = 1; i < BPF_REG_SIZE; i++) { |
| 2034 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
| 2035 | verbose(env, "corrupted spill memory\n"); |
| 2036 | return -EACCES; |
| 2037 | } |
| 2038 | } |
| 2039 | |
| 2040 | if (value_regno >= 0) { |
| 2041 | /* restore register state from stack */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2042 | state->regs[value_regno] = *reg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2043 | /* mark reg as written since spilled pointer state likely |
| 2044 | * has its liveness marks cleared by is_state_visited() |
| 2045 | * which resets stack/reg liveness for state transitions |
| 2046 | */ |
| 2047 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2048 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
| 2049 | /* If value_regno==-1, the caller is asking us whether |
| 2050 | * it is acceptable to use this value as a SCALAR_VALUE |
| 2051 | * (e.g. for XADD). |
| 2052 | * We must not allow unprivileged callers to do that |
| 2053 | * with spilled pointers. |
| 2054 | */ |
| 2055 | verbose(env, "leaking pointer from stack off %d\n", |
| 2056 | off); |
| 2057 | return -EACCES; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2058 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2059 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2060 | } else { |
| 2061 | int zeros = 0; |
| 2062 | |
| 2063 | for (i = 0; i < size; i++) { |
| 2064 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 2065 | continue; |
| 2066 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 2067 | zeros++; |
| 2068 | continue; |
| 2069 | } |
| 2070 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 2071 | off, i, size); |
| 2072 | return -EACCES; |
| 2073 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2074 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2075 | if (value_regno >= 0) { |
| 2076 | if (zeros == size) { |
| 2077 | /* any size read into register is zero extended, |
| 2078 | * so the whole register == const_zero |
| 2079 | */ |
| 2080 | __mark_reg_const_zero(&state->regs[value_regno]); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2081 | /* backtracking doesn't support STACK_ZERO yet, |
| 2082 | * so mark it precise here, so that later |
| 2083 | * backtracking can stop here. |
| 2084 | * Backtracking may not need this if this register |
| 2085 | * doesn't participate in pointer adjustment. |
| 2086 | * Forward propagation of precise flag is not |
| 2087 | * necessary either. This mark is only to stop |
| 2088 | * backtracking. Any register that contributed |
| 2089 | * to const 0 was marked precise before spill. |
| 2090 | */ |
| 2091 | state->regs[value_regno].precise = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2092 | } else { |
| 2093 | /* have read misc data from the stack */ |
| 2094 | mark_reg_unknown(env, state->regs, value_regno); |
| 2095 | } |
| 2096 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 2097 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2098 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2099 | return 0; |
| 2100 | } |
| 2101 | |
| 2102 | static int check_stack_access(struct bpf_verifier_env *env, |
| 2103 | const struct bpf_reg_state *reg, |
| 2104 | int off, int size) |
| 2105 | { |
| 2106 | /* Stack accesses must be at a fixed offset, so that we |
| 2107 | * can determine what type of data were returned. See |
| 2108 | * check_stack_read(). |
| 2109 | */ |
| 2110 | if (!tnum_is_const(reg->var_off)) { |
| 2111 | char tn_buf[48]; |
| 2112 | |
| 2113 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2114 | verbose(env, "variable stack access var_off=%s off=%d size=%d\n", |
| 2115 | tn_buf, off, size); |
| 2116 | return -EACCES; |
| 2117 | } |
| 2118 | |
| 2119 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 2120 | verbose(env, "invalid stack off=%d size=%d\n", off, size); |
| 2121 | return -EACCES; |
| 2122 | } |
| 2123 | |
| 2124 | return 0; |
| 2125 | } |
| 2126 | |
| 2127 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
| 2128 | int off, int size, enum bpf_access_type type) |
| 2129 | { |
| 2130 | struct bpf_reg_state *regs = cur_regs(env); |
| 2131 | struct bpf_map *map = regs[regno].map_ptr; |
| 2132 | u32 cap = bpf_map_flags_to_cap(map); |
| 2133 | |
| 2134 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { |
| 2135 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", |
| 2136 | map->value_size, off, size); |
| 2137 | return -EACCES; |
| 2138 | } |
| 2139 | |
| 2140 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { |
| 2141 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", |
| 2142 | map->value_size, off, size); |
| 2143 | return -EACCES; |
| 2144 | } |
| 2145 | |
| 2146 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
| 2150 | static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
| 2151 | int size, bool zero_size_allowed) |
| 2152 | { |
| 2153 | struct bpf_reg_state *regs = cur_regs(env); |
| 2154 | struct bpf_map *map = regs[regno].map_ptr; |
| 2155 | |
| 2156 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 2157 | off + size > map->value_size) { |
| 2158 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
| 2159 | map->value_size, off, size); |
| 2160 | return -EACCES; |
| 2161 | } |
| 2162 | return 0; |
| 2163 | } |
| 2164 | |
| 2165 | /* check read/write into a map element with possible variable offset */ |
| 2166 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
| 2167 | int off, int size, bool zero_size_allowed) |
| 2168 | { |
| 2169 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2170 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2171 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 2172 | int err; |
| 2173 | |
| 2174 | /* We may have adjusted the register to this map value, so we |
| 2175 | * need to try adding each of min_value and max_value to off |
| 2176 | * to make sure our theoretical access will be safe. |
| 2177 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2178 | if (env->log.level & BPF_LOG_LEVEL) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2179 | print_verifier_state(env, state); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2180 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2181 | /* The minimum value is only important with signed |
| 2182 | * comparisons where we can't assume the floor of a |
| 2183 | * value is 0. If we are using signed variables for our |
| 2184 | * index'es we need to make sure that whatever we use |
| 2185 | * will have a set floor within our range. |
| 2186 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2187 | if (reg->smin_value < 0 && |
| 2188 | (reg->smin_value == S64_MIN || |
| 2189 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || |
| 2190 | reg->smin_value + off < 0)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2191 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 2192 | regno); |
| 2193 | return -EACCES; |
| 2194 | } |
| 2195 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
| 2196 | zero_size_allowed); |
| 2197 | if (err) { |
| 2198 | verbose(env, "R%d min value is outside of the array range\n", |
| 2199 | regno); |
| 2200 | return err; |
| 2201 | } |
| 2202 | |
| 2203 | /* If we haven't set a max value then we need to bail since we can't be |
| 2204 | * sure we won't do bad things. |
| 2205 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
| 2206 | */ |
| 2207 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
| 2208 | verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n", |
| 2209 | regno); |
| 2210 | return -EACCES; |
| 2211 | } |
| 2212 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
| 2213 | zero_size_allowed); |
| 2214 | if (err) |
| 2215 | verbose(env, "R%d max value is outside of the array range\n", |
| 2216 | regno); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2217 | |
| 2218 | if (map_value_has_spin_lock(reg->map_ptr)) { |
| 2219 | u32 lock = reg->map_ptr->spin_lock_off; |
| 2220 | |
| 2221 | /* if any part of struct bpf_spin_lock can be touched by |
| 2222 | * load/store reject this program. |
| 2223 | * To check that [x1, x2) overlaps with [y1, y2) |
| 2224 | * it is sufficient to check x1 < y2 && y1 < x2. |
| 2225 | */ |
| 2226 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && |
| 2227 | lock < reg->umax_value + off + size) { |
| 2228 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); |
| 2229 | return -EACCES; |
| 2230 | } |
| 2231 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2232 | return err; |
| 2233 | } |
| 2234 | |
| 2235 | #define MAX_PACKET_OFF 0xffff |
| 2236 | |
| 2237 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
| 2238 | const struct bpf_call_arg_meta *meta, |
| 2239 | enum bpf_access_type t) |
| 2240 | { |
| 2241 | switch (env->prog->type) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2242 | /* Program types only with direct read access go here! */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2243 | case BPF_PROG_TYPE_LWT_IN: |
| 2244 | case BPF_PROG_TYPE_LWT_OUT: |
| 2245 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
| 2246 | case BPF_PROG_TYPE_SK_REUSEPORT: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2247 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
| 2248 | case BPF_PROG_TYPE_CGROUP_SKB: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2249 | if (t == BPF_WRITE) |
| 2250 | return false; |
| 2251 | /* fallthrough */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2252 | |
| 2253 | /* Program types with direct read + write access go here! */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2254 | case BPF_PROG_TYPE_SCHED_CLS: |
| 2255 | case BPF_PROG_TYPE_SCHED_ACT: |
| 2256 | case BPF_PROG_TYPE_XDP: |
| 2257 | case BPF_PROG_TYPE_LWT_XMIT: |
| 2258 | case BPF_PROG_TYPE_SK_SKB: |
| 2259 | case BPF_PROG_TYPE_SK_MSG: |
| 2260 | if (meta) |
| 2261 | return meta->pkt_access; |
| 2262 | |
| 2263 | env->seen_direct_write = true; |
| 2264 | return true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2265 | |
| 2266 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 2267 | if (t == BPF_WRITE) |
| 2268 | env->seen_direct_write = true; |
| 2269 | |
| 2270 | return true; |
| 2271 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2272 | default: |
| 2273 | return false; |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
| 2278 | int off, int size, bool zero_size_allowed) |
| 2279 | { |
| 2280 | struct bpf_reg_state *regs = cur_regs(env); |
| 2281 | struct bpf_reg_state *reg = ®s[regno]; |
| 2282 | |
| 2283 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 2284 | (u64)off + size > reg->range) { |
| 2285 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
| 2286 | off, size, regno, reg->id, reg->off, reg->range); |
| 2287 | return -EACCES; |
| 2288 | } |
| 2289 | return 0; |
| 2290 | } |
| 2291 | |
| 2292 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
| 2293 | int size, bool zero_size_allowed) |
| 2294 | { |
| 2295 | struct bpf_reg_state *regs = cur_regs(env); |
| 2296 | struct bpf_reg_state *reg = ®s[regno]; |
| 2297 | int err; |
| 2298 | |
| 2299 | /* We may have added a variable offset to the packet pointer; but any |
| 2300 | * reg->range we have comes after that. We are only checking the fixed |
| 2301 | * offset. |
| 2302 | */ |
| 2303 | |
| 2304 | /* We don't allow negative numbers, because we aren't tracking enough |
| 2305 | * detail to prove they're safe. |
| 2306 | */ |
| 2307 | if (reg->smin_value < 0) { |
| 2308 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 2309 | regno); |
| 2310 | return -EACCES; |
| 2311 | } |
| 2312 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
| 2313 | if (err) { |
| 2314 | verbose(env, "R%d offset is outside of the packet\n", regno); |
| 2315 | return err; |
| 2316 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2317 | |
| 2318 | /* __check_packet_access has made sure "off + size - 1" is within u16. |
| 2319 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 2320 | * otherwise find_good_pkt_pointers would have refused to set range info |
| 2321 | * that __check_packet_access would have rejected this pkt access. |
| 2322 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 2323 | */ |
| 2324 | env->prog->aux->max_pkt_offset = |
| 2325 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 2326 | off + reg->umax_value + size - 1); |
| 2327 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2328 | return err; |
| 2329 | } |
| 2330 | |
| 2331 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
| 2332 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
| 2333 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
| 2334 | { |
| 2335 | struct bpf_insn_access_aux info = { |
| 2336 | .reg_type = *reg_type, |
| 2337 | }; |
| 2338 | |
| 2339 | if (env->ops->is_valid_access && |
| 2340 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
| 2341 | /* A non zero info.ctx_field_size indicates that this field is a |
| 2342 | * candidate for later verifier transformation to load the whole |
| 2343 | * field and then apply a mask when accessed with a narrower |
| 2344 | * access than actual ctx access size. A zero info.ctx_field_size |
| 2345 | * will only allow for whole field access and rejects any other |
| 2346 | * type of narrower access. |
| 2347 | */ |
| 2348 | *reg_type = info.reg_type; |
| 2349 | |
| 2350 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
| 2351 | /* remember the offset of last byte accessed in ctx */ |
| 2352 | if (env->prog->aux->max_ctx_offset < off + size) |
| 2353 | env->prog->aux->max_ctx_offset = off + size; |
| 2354 | return 0; |
| 2355 | } |
| 2356 | |
| 2357 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
| 2358 | return -EACCES; |
| 2359 | } |
| 2360 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2361 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 2362 | int size) |
| 2363 | { |
| 2364 | if (size < 0 || off < 0 || |
| 2365 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 2366 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 2367 | off, size); |
| 2368 | return -EACCES; |
| 2369 | } |
| 2370 | return 0; |
| 2371 | } |
| 2372 | |
| 2373 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
| 2374 | u32 regno, int off, int size, |
| 2375 | enum bpf_access_type t) |
| 2376 | { |
| 2377 | struct bpf_reg_state *regs = cur_regs(env); |
| 2378 | struct bpf_reg_state *reg = ®s[regno]; |
| 2379 | struct bpf_insn_access_aux info = {}; |
| 2380 | bool valid; |
| 2381 | |
| 2382 | if (reg->smin_value < 0) { |
| 2383 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 2384 | regno); |
| 2385 | return -EACCES; |
| 2386 | } |
| 2387 | |
| 2388 | switch (reg->type) { |
| 2389 | case PTR_TO_SOCK_COMMON: |
| 2390 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); |
| 2391 | break; |
| 2392 | case PTR_TO_SOCKET: |
| 2393 | valid = bpf_sock_is_valid_access(off, size, t, &info); |
| 2394 | break; |
| 2395 | case PTR_TO_TCP_SOCK: |
| 2396 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); |
| 2397 | break; |
| 2398 | case PTR_TO_XDP_SOCK: |
| 2399 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); |
| 2400 | break; |
| 2401 | default: |
| 2402 | valid = false; |
| 2403 | } |
| 2404 | |
| 2405 | |
| 2406 | if (valid) { |
| 2407 | env->insn_aux_data[insn_idx].ctx_field_size = |
| 2408 | info.ctx_field_size; |
| 2409 | return 0; |
| 2410 | } |
| 2411 | |
| 2412 | verbose(env, "R%d invalid %s access off=%d size=%d\n", |
| 2413 | regno, reg_type_str[reg->type], off, size); |
| 2414 | |
| 2415 | return -EACCES; |
| 2416 | } |
| 2417 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2418 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
| 2419 | { |
| 2420 | return cur_regs(env) + regno; |
| 2421 | } |
| 2422 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2423 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 2424 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2425 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 2429 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2430 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2431 | |
| 2432 | return reg->type == PTR_TO_CTX; |
| 2433 | } |
| 2434 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2435 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) |
| 2436 | { |
| 2437 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 2438 | |
| 2439 | return type_is_sk_pointer(reg->type); |
| 2440 | } |
| 2441 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2442 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 2443 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2444 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2445 | |
| 2446 | return type_is_pkt_pointer(reg->type); |
| 2447 | } |
| 2448 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2449 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 2450 | { |
| 2451 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 2452 | |
| 2453 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 2454 | return reg->type == PTR_TO_FLOW_KEYS; |
| 2455 | } |
| 2456 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2457 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 2458 | const struct bpf_reg_state *reg, |
| 2459 | int off, int size, bool strict) |
| 2460 | { |
| 2461 | struct tnum reg_off; |
| 2462 | int ip_align; |
| 2463 | |
| 2464 | /* Byte size accesses are always allowed. */ |
| 2465 | if (!strict || size == 1) |
| 2466 | return 0; |
| 2467 | |
| 2468 | /* For platforms that do not have a Kconfig enabling |
| 2469 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 2470 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 2471 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 2472 | * to this code only in strict mode where we want to emulate |
| 2473 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 2474 | * unconditional IP align value of '2'. |
| 2475 | */ |
| 2476 | ip_align = 2; |
| 2477 | |
| 2478 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 2479 | if (!tnum_is_aligned(reg_off, size)) { |
| 2480 | char tn_buf[48]; |
| 2481 | |
| 2482 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2483 | verbose(env, |
| 2484 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
| 2485 | ip_align, tn_buf, reg->off, off, size); |
| 2486 | return -EACCES; |
| 2487 | } |
| 2488 | |
| 2489 | return 0; |
| 2490 | } |
| 2491 | |
| 2492 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 2493 | const struct bpf_reg_state *reg, |
| 2494 | const char *pointer_desc, |
| 2495 | int off, int size, bool strict) |
| 2496 | { |
| 2497 | struct tnum reg_off; |
| 2498 | |
| 2499 | /* Byte size accesses are always allowed. */ |
| 2500 | if (!strict || size == 1) |
| 2501 | return 0; |
| 2502 | |
| 2503 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 2504 | if (!tnum_is_aligned(reg_off, size)) { |
| 2505 | char tn_buf[48]; |
| 2506 | |
| 2507 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2508 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
| 2509 | pointer_desc, tn_buf, reg->off, off, size); |
| 2510 | return -EACCES; |
| 2511 | } |
| 2512 | |
| 2513 | return 0; |
| 2514 | } |
| 2515 | |
| 2516 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
| 2517 | const struct bpf_reg_state *reg, int off, |
| 2518 | int size, bool strict_alignment_once) |
| 2519 | { |
| 2520 | bool strict = env->strict_alignment || strict_alignment_once; |
| 2521 | const char *pointer_desc = ""; |
| 2522 | |
| 2523 | switch (reg->type) { |
| 2524 | case PTR_TO_PACKET: |
| 2525 | case PTR_TO_PACKET_META: |
| 2526 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 2527 | * right in front, treat it the very same way. |
| 2528 | */ |
| 2529 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2530 | case PTR_TO_FLOW_KEYS: |
| 2531 | pointer_desc = "flow keys "; |
| 2532 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2533 | case PTR_TO_MAP_VALUE: |
| 2534 | pointer_desc = "value "; |
| 2535 | break; |
| 2536 | case PTR_TO_CTX: |
| 2537 | pointer_desc = "context "; |
| 2538 | break; |
| 2539 | case PTR_TO_STACK: |
| 2540 | pointer_desc = "stack "; |
| 2541 | /* The stack spill tracking logic in check_stack_write() |
| 2542 | * and check_stack_read() relies on stack accesses being |
| 2543 | * aligned. |
| 2544 | */ |
| 2545 | strict = true; |
| 2546 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2547 | case PTR_TO_SOCKET: |
| 2548 | pointer_desc = "sock "; |
| 2549 | break; |
| 2550 | case PTR_TO_SOCK_COMMON: |
| 2551 | pointer_desc = "sock_common "; |
| 2552 | break; |
| 2553 | case PTR_TO_TCP_SOCK: |
| 2554 | pointer_desc = "tcp_sock "; |
| 2555 | break; |
| 2556 | case PTR_TO_XDP_SOCK: |
| 2557 | pointer_desc = "xdp_sock "; |
| 2558 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2559 | default: |
| 2560 | break; |
| 2561 | } |
| 2562 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 2563 | strict); |
| 2564 | } |
| 2565 | |
| 2566 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 2567 | const struct bpf_func_state *func, |
| 2568 | int off) |
| 2569 | { |
| 2570 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
| 2571 | |
| 2572 | if (stack >= -off) |
| 2573 | return 0; |
| 2574 | |
| 2575 | /* update known max for given subprogram */ |
| 2576 | env->subprog_info[func->subprogno].stack_depth = -off; |
| 2577 | return 0; |
| 2578 | } |
| 2579 | |
| 2580 | /* starting from main bpf function walk all instructions of the function |
| 2581 | * and recursively walk all callees that given function can call. |
| 2582 | * Ignore jump and exit insns. |
| 2583 | * Since recursion is prevented by check_cfg() this algorithm |
| 2584 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 2585 | */ |
| 2586 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 2587 | { |
| 2588 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 2589 | struct bpf_subprog_info *subprog = env->subprog_info; |
| 2590 | struct bpf_insn *insn = env->prog->insnsi; |
| 2591 | int ret_insn[MAX_CALL_FRAMES]; |
| 2592 | int ret_prog[MAX_CALL_FRAMES]; |
| 2593 | |
| 2594 | process_func: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2595 | /* protect against potential stack overflow that might happen when |
| 2596 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack |
| 2597 | * depth for such case down to 256 so that the worst case scenario |
| 2598 | * would result in 8k stack size (32 which is tailcall limit * 256 = |
| 2599 | * 8k). |
| 2600 | * |
| 2601 | * To get the idea what might happen, see an example: |
| 2602 | * func1 -> sub rsp, 128 |
| 2603 | * subfunc1 -> sub rsp, 256 |
| 2604 | * tailcall1 -> add rsp, 256 |
| 2605 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) |
| 2606 | * subfunc2 -> sub rsp, 64 |
| 2607 | * subfunc22 -> sub rsp, 128 |
| 2608 | * tailcall2 -> add rsp, 128 |
| 2609 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) |
| 2610 | * |
| 2611 | * tailcall will unwind the current stack frame but it will not get rid |
| 2612 | * of caller's stack as shown on the example above. |
| 2613 | */ |
| 2614 | if (idx && subprog[idx].has_tail_call && depth >= 256) { |
| 2615 | verbose(env, |
| 2616 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", |
| 2617 | depth); |
| 2618 | return -EACCES; |
| 2619 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2620 | /* round up to 32-bytes, since this is granularity |
| 2621 | * of interpreter stack size |
| 2622 | */ |
| 2623 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
| 2624 | if (depth > MAX_BPF_STACK) { |
| 2625 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
| 2626 | frame + 1, depth); |
| 2627 | return -EACCES; |
| 2628 | } |
| 2629 | continue_func: |
| 2630 | subprog_end = subprog[idx + 1].start; |
| 2631 | for (; i < subprog_end; i++) { |
| 2632 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 2633 | continue; |
| 2634 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 2635 | continue; |
| 2636 | /* remember insn and function to return to */ |
| 2637 | ret_insn[frame] = i + 1; |
| 2638 | ret_prog[frame] = idx; |
| 2639 | |
| 2640 | /* find the callee */ |
| 2641 | i = i + insn[i].imm + 1; |
| 2642 | idx = find_subprog(env, i); |
| 2643 | if (idx < 0) { |
| 2644 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 2645 | i); |
| 2646 | return -EFAULT; |
| 2647 | } |
| 2648 | frame++; |
| 2649 | if (frame >= MAX_CALL_FRAMES) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2650 | verbose(env, "the call stack of %d frames is too deep !\n", |
| 2651 | frame); |
| 2652 | return -E2BIG; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2653 | } |
| 2654 | goto process_func; |
| 2655 | } |
| 2656 | /* end of for() loop means the last insn of the 'subprog' |
| 2657 | * was reached. Doesn't matter whether it was JA or EXIT |
| 2658 | */ |
| 2659 | if (frame == 0) |
| 2660 | return 0; |
| 2661 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
| 2662 | frame--; |
| 2663 | i = ret_insn[frame]; |
| 2664 | idx = ret_prog[frame]; |
| 2665 | goto continue_func; |
| 2666 | } |
| 2667 | |
| 2668 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
| 2669 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 2670 | const struct bpf_insn *insn, int idx) |
| 2671 | { |
| 2672 | int start = idx + insn->imm + 1, subprog; |
| 2673 | |
| 2674 | subprog = find_subprog(env, start); |
| 2675 | if (subprog < 0) { |
| 2676 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 2677 | start); |
| 2678 | return -EFAULT; |
| 2679 | } |
| 2680 | return env->subprog_info[subprog].stack_depth; |
| 2681 | } |
| 2682 | #endif |
| 2683 | |
| 2684 | static int check_ctx_reg(struct bpf_verifier_env *env, |
| 2685 | const struct bpf_reg_state *reg, int regno) |
| 2686 | { |
| 2687 | /* Access to ctx or passing it to a helper is only allowed in |
| 2688 | * its original, unmodified form. |
| 2689 | */ |
| 2690 | |
| 2691 | if (reg->off) { |
| 2692 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 2693 | regno, reg->off); |
| 2694 | return -EACCES; |
| 2695 | } |
| 2696 | |
| 2697 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 2698 | char tn_buf[48]; |
| 2699 | |
| 2700 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2701 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 2702 | return -EACCES; |
| 2703 | } |
| 2704 | |
| 2705 | return 0; |
| 2706 | } |
| 2707 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2708 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
| 2709 | const struct bpf_reg_state *reg, |
| 2710 | int regno, int off, int size) |
| 2711 | { |
| 2712 | if (off < 0) { |
| 2713 | verbose(env, |
| 2714 | "R%d invalid tracepoint buffer access: off=%d, size=%d", |
| 2715 | regno, off, size); |
| 2716 | return -EACCES; |
| 2717 | } |
| 2718 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 2719 | char tn_buf[48]; |
| 2720 | |
| 2721 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2722 | verbose(env, |
| 2723 | "R%d invalid variable buffer offset: off=%d, var_off=%s", |
| 2724 | regno, off, tn_buf); |
| 2725 | return -EACCES; |
| 2726 | } |
| 2727 | if (off + size > env->prog->aux->max_tp_access) |
| 2728 | env->prog->aux->max_tp_access = off + size; |
| 2729 | |
| 2730 | return 0; |
| 2731 | } |
| 2732 | |
| 2733 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2734 | /* truncate register to smaller size (in bytes) |
| 2735 | * must be called with size < BPF_REG_SIZE |
| 2736 | */ |
| 2737 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 2738 | { |
| 2739 | u64 mask; |
| 2740 | |
| 2741 | /* clear high bits in bit representation */ |
| 2742 | reg->var_off = tnum_cast(reg->var_off, size); |
| 2743 | |
| 2744 | /* fix arithmetic bounds */ |
| 2745 | mask = ((u64)1 << (size * 8)) - 1; |
| 2746 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 2747 | reg->umin_value &= mask; |
| 2748 | reg->umax_value &= mask; |
| 2749 | } else { |
| 2750 | reg->umin_value = 0; |
| 2751 | reg->umax_value = mask; |
| 2752 | } |
| 2753 | reg->smin_value = reg->umin_value; |
| 2754 | reg->smax_value = reg->umax_value; |
| 2755 | } |
| 2756 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2757 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
| 2758 | { |
| 2759 | return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen; |
| 2760 | } |
| 2761 | |
| 2762 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) |
| 2763 | { |
| 2764 | void *ptr; |
| 2765 | u64 addr; |
| 2766 | int err; |
| 2767 | |
| 2768 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 2769 | if (err) |
| 2770 | return err; |
| 2771 | ptr = (void *)(long)addr + off; |
| 2772 | |
| 2773 | switch (size) { |
| 2774 | case sizeof(u8): |
| 2775 | *val = (u64)*(u8 *)ptr; |
| 2776 | break; |
| 2777 | case sizeof(u16): |
| 2778 | *val = (u64)*(u16 *)ptr; |
| 2779 | break; |
| 2780 | case sizeof(u32): |
| 2781 | *val = (u64)*(u32 *)ptr; |
| 2782 | break; |
| 2783 | case sizeof(u64): |
| 2784 | *val = *(u64 *)ptr; |
| 2785 | break; |
| 2786 | default: |
| 2787 | return -EINVAL; |
| 2788 | } |
| 2789 | return 0; |
| 2790 | } |
| 2791 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2792 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 2793 | * if t==write, value_regno is a register which value is stored into memory |
| 2794 | * if t==read, value_regno is a register which will receive the value from memory |
| 2795 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 2796 | * if t==read && value_regno==-1, don't care what we read from memory |
| 2797 | */ |
| 2798 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 2799 | int off, int bpf_size, enum bpf_access_type t, |
| 2800 | int value_regno, bool strict_alignment_once) |
| 2801 | { |
| 2802 | struct bpf_reg_state *regs = cur_regs(env); |
| 2803 | struct bpf_reg_state *reg = regs + regno; |
| 2804 | struct bpf_func_state *state; |
| 2805 | int size, err = 0; |
| 2806 | |
| 2807 | size = bpf_size_to_bytes(bpf_size); |
| 2808 | if (size < 0) |
| 2809 | return size; |
| 2810 | |
| 2811 | /* alignment checks will add in reg->off themselves */ |
| 2812 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
| 2813 | if (err) |
| 2814 | return err; |
| 2815 | |
| 2816 | /* for access checks, reg->off is just part of off */ |
| 2817 | off += reg->off; |
| 2818 | |
| 2819 | if (reg->type == PTR_TO_MAP_VALUE) { |
| 2820 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2821 | is_pointer_value(env, value_regno)) { |
| 2822 | verbose(env, "R%d leaks addr into map\n", value_regno); |
| 2823 | return -EACCES; |
| 2824 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2825 | err = check_map_access_type(env, regno, off, size, t); |
| 2826 | if (err) |
| 2827 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2828 | err = check_map_access(env, regno, off, size, false); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2829 | if (!err && t == BPF_READ && value_regno >= 0) { |
| 2830 | struct bpf_map *map = reg->map_ptr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2831 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2832 | /* if map is read-only, track its contents as scalars */ |
| 2833 | if (tnum_is_const(reg->var_off) && |
| 2834 | bpf_map_is_rdonly(map) && |
| 2835 | map->ops->map_direct_value_addr) { |
| 2836 | int map_off = off + reg->var_off.value; |
| 2837 | u64 val = 0; |
| 2838 | |
| 2839 | err = bpf_map_direct_read(map, map_off, size, |
| 2840 | &val); |
| 2841 | if (err) |
| 2842 | return err; |
| 2843 | |
| 2844 | regs[value_regno].type = SCALAR_VALUE; |
| 2845 | __mark_reg_known(®s[value_regno], val); |
| 2846 | } else { |
| 2847 | mark_reg_unknown(env, regs, value_regno); |
| 2848 | } |
| 2849 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2850 | } else if (reg->type == PTR_TO_CTX) { |
| 2851 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
| 2852 | |
| 2853 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2854 | is_pointer_value(env, value_regno)) { |
| 2855 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
| 2856 | return -EACCES; |
| 2857 | } |
| 2858 | |
| 2859 | err = check_ctx_reg(env, reg, regno); |
| 2860 | if (err < 0) |
| 2861 | return err; |
| 2862 | |
| 2863 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
| 2864 | if (!err && t == BPF_READ && value_regno >= 0) { |
| 2865 | /* ctx access returns either a scalar, or a |
| 2866 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 2867 | * case, we know the offset is zero. |
| 2868 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2869 | if (reg_type == SCALAR_VALUE) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2870 | mark_reg_unknown(env, regs, value_regno); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2871 | } else { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2872 | mark_reg_known_zero(env, regs, |
| 2873 | value_regno); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2874 | if (reg_type_may_be_null(reg_type)) |
| 2875 | regs[value_regno].id = ++env->id_gen; |
| 2876 | /* A load of ctx field could have different |
| 2877 | * actual load size with the one encoded in the |
| 2878 | * insn. When the dst is PTR, it is for sure not |
| 2879 | * a sub-register. |
| 2880 | */ |
| 2881 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; |
| 2882 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2883 | regs[value_regno].type = reg_type; |
| 2884 | } |
| 2885 | |
| 2886 | } else if (reg->type == PTR_TO_STACK) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2887 | off += reg->var_off.value; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2888 | err = check_stack_access(env, reg, off, size); |
| 2889 | if (err) |
| 2890 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2891 | |
| 2892 | state = func(env, reg); |
| 2893 | err = update_stack_depth(env, state, off); |
| 2894 | if (err) |
| 2895 | return err; |
| 2896 | |
| 2897 | if (t == BPF_WRITE) |
| 2898 | err = check_stack_write(env, state, off, size, |
| 2899 | value_regno, insn_idx); |
| 2900 | else |
| 2901 | err = check_stack_read(env, state, off, size, |
| 2902 | value_regno); |
| 2903 | } else if (reg_is_pkt_pointer(reg)) { |
| 2904 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
| 2905 | verbose(env, "cannot write into packet\n"); |
| 2906 | return -EACCES; |
| 2907 | } |
| 2908 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2909 | is_pointer_value(env, value_regno)) { |
| 2910 | verbose(env, "R%d leaks addr into packet\n", |
| 2911 | value_regno); |
| 2912 | return -EACCES; |
| 2913 | } |
| 2914 | err = check_packet_access(env, regno, off, size, false); |
| 2915 | if (!err && t == BPF_READ && value_regno >= 0) |
| 2916 | mark_reg_unknown(env, regs, value_regno); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2917 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 2918 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2919 | is_pointer_value(env, value_regno)) { |
| 2920 | verbose(env, "R%d leaks addr into flow keys\n", |
| 2921 | value_regno); |
| 2922 | return -EACCES; |
| 2923 | } |
| 2924 | |
| 2925 | err = check_flow_keys_access(env, off, size); |
| 2926 | if (!err && t == BPF_READ && value_regno >= 0) |
| 2927 | mark_reg_unknown(env, regs, value_regno); |
| 2928 | } else if (type_is_sk_pointer(reg->type)) { |
| 2929 | if (t == BPF_WRITE) { |
| 2930 | verbose(env, "R%d cannot write into %s\n", |
| 2931 | regno, reg_type_str[reg->type]); |
| 2932 | return -EACCES; |
| 2933 | } |
| 2934 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
| 2935 | if (!err && value_regno >= 0) |
| 2936 | mark_reg_unknown(env, regs, value_regno); |
| 2937 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
| 2938 | err = check_tp_buffer_access(env, reg, regno, off, size); |
| 2939 | if (!err && t == BPF_READ && value_regno >= 0) |
| 2940 | mark_reg_unknown(env, regs, value_regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2941 | } else { |
| 2942 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 2943 | reg_type_str[reg->type]); |
| 2944 | return -EACCES; |
| 2945 | } |
| 2946 | |
| 2947 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
| 2948 | regs[value_regno].type == SCALAR_VALUE) { |
| 2949 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
| 2950 | coerce_reg_to_size(®s[value_regno], size); |
| 2951 | } |
| 2952 | return err; |
| 2953 | } |
| 2954 | |
| 2955 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
| 2956 | { |
| 2957 | int err; |
| 2958 | |
| 2959 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 2960 | insn->imm != 0) { |
| 2961 | verbose(env, "BPF_XADD uses reserved fields\n"); |
| 2962 | return -EINVAL; |
| 2963 | } |
| 2964 | |
| 2965 | /* check src1 operand */ |
| 2966 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 2967 | if (err) |
| 2968 | return err; |
| 2969 | |
| 2970 | /* check src2 operand */ |
| 2971 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
| 2972 | if (err) |
| 2973 | return err; |
| 2974 | |
| 2975 | if (is_pointer_value(env, insn->src_reg)) { |
| 2976 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
| 2977 | return -EACCES; |
| 2978 | } |
| 2979 | |
| 2980 | if (is_ctx_reg(env, insn->dst_reg) || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2981 | is_pkt_reg(env, insn->dst_reg) || |
| 2982 | is_flow_key_reg(env, insn->dst_reg) || |
| 2983 | is_sk_reg(env, insn->dst_reg)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2984 | 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] | 2985 | insn->dst_reg, |
| 2986 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2987 | return -EACCES; |
| 2988 | } |
| 2989 | |
| 2990 | /* check whether atomic_add can read the memory */ |
| 2991 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
| 2992 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
| 2993 | if (err) |
| 2994 | return err; |
| 2995 | |
| 2996 | /* check whether atomic_add can write into the same memory */ |
| 2997 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
| 2998 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
| 2999 | } |
| 3000 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3001 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
| 3002 | int off, int access_size, |
| 3003 | bool zero_size_allowed) |
| 3004 | { |
| 3005 | struct bpf_reg_state *reg = reg_state(env, regno); |
| 3006 | |
| 3007 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 3008 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
| 3009 | if (tnum_is_const(reg->var_off)) { |
| 3010 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", |
| 3011 | regno, off, access_size); |
| 3012 | } else { |
| 3013 | char tn_buf[48]; |
| 3014 | |
| 3015 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3016 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", |
| 3017 | regno, tn_buf, access_size); |
| 3018 | } |
| 3019 | return -EACCES; |
| 3020 | } |
| 3021 | return 0; |
| 3022 | } |
| 3023 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3024 | /* when register 'regno' is passed into function that will read 'access_size' |
| 3025 | * bytes from that pointer, make sure that it's within stack boundary |
| 3026 | * and all elements of stack are initialized. |
| 3027 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 3028 | * 'off' argument, so it has to add in reg->off itself. |
| 3029 | */ |
| 3030 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
| 3031 | int access_size, bool zero_size_allowed, |
| 3032 | struct bpf_call_arg_meta *meta) |
| 3033 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3034 | struct bpf_reg_state *reg = reg_state(env, regno); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3035 | struct bpf_func_state *state = func(env, reg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3036 | int err, min_off, max_off, i, j, slot, spi; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3037 | |
| 3038 | if (reg->type != PTR_TO_STACK) { |
| 3039 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
| 3040 | if (zero_size_allowed && access_size == 0 && |
| 3041 | register_is_null(reg)) |
| 3042 | return 0; |
| 3043 | |
| 3044 | verbose(env, "R%d type=%s expected=%s\n", regno, |
| 3045 | reg_type_str[reg->type], |
| 3046 | reg_type_str[PTR_TO_STACK]); |
| 3047 | return -EACCES; |
| 3048 | } |
| 3049 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3050 | if (tnum_is_const(reg->var_off)) { |
| 3051 | min_off = max_off = reg->var_off.value + reg->off; |
| 3052 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 3053 | zero_size_allowed); |
| 3054 | if (err) |
| 3055 | return err; |
| 3056 | } else { |
| 3057 | /* Variable offset is prohibited for unprivileged mode for |
| 3058 | * simplicity since it requires corresponding support in |
| 3059 | * Spectre masking for stack ALU. |
| 3060 | * See also retrieve_ptr_limit(). |
| 3061 | */ |
| 3062 | if (!env->allow_ptr_leaks) { |
| 3063 | char tn_buf[48]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3064 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3065 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3066 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", |
| 3067 | regno, tn_buf); |
| 3068 | return -EACCES; |
| 3069 | } |
| 3070 | /* Only initialized buffer on stack is allowed to be accessed |
| 3071 | * with variable offset. With uninitialized buffer it's hard to |
| 3072 | * guarantee that whole memory is marked as initialized on |
| 3073 | * helper return since specific bounds are unknown what may |
| 3074 | * cause uninitialized stack leaking. |
| 3075 | */ |
| 3076 | if (meta && meta->raw_mode) |
| 3077 | meta = NULL; |
| 3078 | |
| 3079 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
| 3080 | reg->smax_value <= -BPF_MAX_VAR_OFF) { |
| 3081 | verbose(env, "R%d unbounded indirect variable offset stack access\n", |
| 3082 | regno); |
| 3083 | return -EACCES; |
| 3084 | } |
| 3085 | min_off = reg->smin_value + reg->off; |
| 3086 | max_off = reg->smax_value + reg->off; |
| 3087 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 3088 | zero_size_allowed); |
| 3089 | if (err) { |
| 3090 | verbose(env, "R%d min value is outside of stack bound\n", |
| 3091 | regno); |
| 3092 | return err; |
| 3093 | } |
| 3094 | err = __check_stack_boundary(env, regno, max_off, access_size, |
| 3095 | zero_size_allowed); |
| 3096 | if (err) { |
| 3097 | verbose(env, "R%d max value is outside of stack bound\n", |
| 3098 | regno); |
| 3099 | return err; |
| 3100 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | if (meta && meta->raw_mode) { |
| 3104 | meta->access_size = access_size; |
| 3105 | meta->regno = regno; |
| 3106 | return 0; |
| 3107 | } |
| 3108 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3109 | for (i = min_off; i < max_off + access_size; i++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3110 | u8 *stype; |
| 3111 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3112 | slot = -i - 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3113 | spi = slot / BPF_REG_SIZE; |
| 3114 | if (state->allocated_stack <= slot) |
| 3115 | goto err; |
| 3116 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 3117 | if (*stype == STACK_MISC) |
| 3118 | goto mark; |
| 3119 | if (*stype == STACK_ZERO) { |
| 3120 | /* helper can write anything into the stack */ |
| 3121 | *stype = STACK_MISC; |
| 3122 | goto mark; |
| 3123 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3124 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 3125 | state->stack[spi].spilled_ptr.type == SCALAR_VALUE) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3126 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3127 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 3128 | state->stack[spi].slot_type[j] = STACK_MISC; |
| 3129 | goto mark; |
| 3130 | } |
| 3131 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3132 | err: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3133 | if (tnum_is_const(reg->var_off)) { |
| 3134 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 3135 | min_off, i - min_off, access_size); |
| 3136 | } else { |
| 3137 | char tn_buf[48]; |
| 3138 | |
| 3139 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3140 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", |
| 3141 | tn_buf, i - min_off, access_size); |
| 3142 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3143 | return -EACCES; |
| 3144 | mark: |
| 3145 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 3146 | * the whole slot to be marked as 'read' |
| 3147 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3148 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
| 3149 | state->stack[spi].spilled_ptr.parent, |
| 3150 | REG_LIVE_READ64); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3151 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3152 | return update_stack_depth(env, state, min_off); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
| 3155 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 3156 | int access_size, bool zero_size_allowed, |
| 3157 | struct bpf_call_arg_meta *meta) |
| 3158 | { |
| 3159 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 3160 | |
| 3161 | switch (reg->type) { |
| 3162 | case PTR_TO_PACKET: |
| 3163 | case PTR_TO_PACKET_META: |
| 3164 | return check_packet_access(env, regno, reg->off, access_size, |
| 3165 | zero_size_allowed); |
| 3166 | case PTR_TO_MAP_VALUE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3167 | if (check_map_access_type(env, regno, reg->off, access_size, |
| 3168 | meta && meta->raw_mode ? BPF_WRITE : |
| 3169 | BPF_READ)) |
| 3170 | return -EACCES; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3171 | return check_map_access(env, regno, reg->off, access_size, |
| 3172 | zero_size_allowed); |
| 3173 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
| 3174 | return check_stack_boundary(env, regno, access_size, |
| 3175 | zero_size_allowed, meta); |
| 3176 | } |
| 3177 | } |
| 3178 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3179 | /* Implementation details: |
| 3180 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL |
| 3181 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
| 3182 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after |
| 3183 | * value_or_null->value transition, since the verifier only cares about |
| 3184 | * the range of access to valid map value pointer and doesn't care about actual |
| 3185 | * address of the map element. |
| 3186 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
| 3187 | * reg->id > 0 after value_or_null->value transition. By doing so |
| 3188 | * two bpf_map_lookups will be considered two different pointers that |
| 3189 | * point to different bpf_spin_locks. |
| 3190 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
| 3191 | * dead-locks. |
| 3192 | * Since only one bpf_spin_lock is allowed the checks are simpler than |
| 3193 | * reg_is_refcounted() logic. The verifier needs to remember only |
| 3194 | * one spin_lock instead of array of acquired_refs. |
| 3195 | * cur_state->active_spin_lock remembers which map value element got locked |
| 3196 | * and clears it after bpf_spin_unlock. |
| 3197 | */ |
| 3198 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, |
| 3199 | bool is_lock) |
| 3200 | { |
| 3201 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 3202 | struct bpf_verifier_state *cur = env->cur_state; |
| 3203 | bool is_const = tnum_is_const(reg->var_off); |
| 3204 | struct bpf_map *map = reg->map_ptr; |
| 3205 | u64 val = reg->var_off.value; |
| 3206 | |
| 3207 | if (reg->type != PTR_TO_MAP_VALUE) { |
| 3208 | verbose(env, "R%d is not a pointer to map_value\n", regno); |
| 3209 | return -EINVAL; |
| 3210 | } |
| 3211 | if (!is_const) { |
| 3212 | verbose(env, |
| 3213 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", |
| 3214 | regno); |
| 3215 | return -EINVAL; |
| 3216 | } |
| 3217 | if (!map->btf) { |
| 3218 | verbose(env, |
| 3219 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", |
| 3220 | map->name); |
| 3221 | return -EINVAL; |
| 3222 | } |
| 3223 | if (!map_value_has_spin_lock(map)) { |
| 3224 | if (map->spin_lock_off == -E2BIG) |
| 3225 | verbose(env, |
| 3226 | "map '%s' has more than one 'struct bpf_spin_lock'\n", |
| 3227 | map->name); |
| 3228 | else if (map->spin_lock_off == -ENOENT) |
| 3229 | verbose(env, |
| 3230 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", |
| 3231 | map->name); |
| 3232 | else |
| 3233 | verbose(env, |
| 3234 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", |
| 3235 | map->name); |
| 3236 | return -EINVAL; |
| 3237 | } |
| 3238 | if (map->spin_lock_off != val + reg->off) { |
| 3239 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", |
| 3240 | val + reg->off); |
| 3241 | return -EINVAL; |
| 3242 | } |
| 3243 | if (is_lock) { |
| 3244 | if (cur->active_spin_lock) { |
| 3245 | verbose(env, |
| 3246 | "Locking two bpf_spin_locks are not allowed\n"); |
| 3247 | return -EINVAL; |
| 3248 | } |
| 3249 | cur->active_spin_lock = reg->id; |
| 3250 | } else { |
| 3251 | if (!cur->active_spin_lock) { |
| 3252 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
| 3253 | return -EINVAL; |
| 3254 | } |
| 3255 | if (cur->active_spin_lock != reg->id) { |
| 3256 | verbose(env, "bpf_spin_unlock of different lock\n"); |
| 3257 | return -EINVAL; |
| 3258 | } |
| 3259 | cur->active_spin_lock = 0; |
| 3260 | } |
| 3261 | return 0; |
| 3262 | } |
| 3263 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3264 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 3265 | { |
| 3266 | return type == ARG_PTR_TO_MEM || |
| 3267 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 3268 | type == ARG_PTR_TO_UNINIT_MEM; |
| 3269 | } |
| 3270 | |
| 3271 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 3272 | { |
| 3273 | return type == ARG_CONST_SIZE || |
| 3274 | type == ARG_CONST_SIZE_OR_ZERO; |
| 3275 | } |
| 3276 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3277 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
| 3278 | { |
| 3279 | return type == ARG_PTR_TO_INT || |
| 3280 | type == ARG_PTR_TO_LONG; |
| 3281 | } |
| 3282 | |
| 3283 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
| 3284 | { |
| 3285 | if (type == ARG_PTR_TO_INT) |
| 3286 | return sizeof(u32); |
| 3287 | else if (type == ARG_PTR_TO_LONG) |
| 3288 | return sizeof(u64); |
| 3289 | |
| 3290 | return -EINVAL; |
| 3291 | } |
| 3292 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3293 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
| 3294 | enum bpf_arg_type arg_type, |
| 3295 | struct bpf_call_arg_meta *meta) |
| 3296 | { |
| 3297 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 3298 | enum bpf_reg_type expected_type, type = reg->type; |
| 3299 | int err = 0; |
| 3300 | |
| 3301 | if (arg_type == ARG_DONTCARE) |
| 3302 | return 0; |
| 3303 | |
| 3304 | err = check_reg_arg(env, regno, SRC_OP); |
| 3305 | if (err) |
| 3306 | return err; |
| 3307 | |
| 3308 | if (arg_type == ARG_ANYTHING) { |
| 3309 | if (is_pointer_value(env, regno)) { |
| 3310 | verbose(env, "R%d leaks addr into helper function\n", |
| 3311 | regno); |
| 3312 | return -EACCES; |
| 3313 | } |
| 3314 | return 0; |
| 3315 | } |
| 3316 | |
| 3317 | if (type_is_pkt_pointer(type) && |
| 3318 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
| 3319 | verbose(env, "helper access to the packet is not allowed\n"); |
| 3320 | return -EACCES; |
| 3321 | } |
| 3322 | |
| 3323 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3324 | arg_type == ARG_PTR_TO_MAP_VALUE || |
| 3325 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
| 3326 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3327 | expected_type = PTR_TO_STACK; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3328 | if (register_is_null(reg) && |
| 3329 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) |
| 3330 | /* final test in check_stack_boundary() */; |
| 3331 | else if (!type_is_pkt_pointer(type) && |
| 3332 | type != PTR_TO_MAP_VALUE && |
| 3333 | type != expected_type) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3334 | goto err_type; |
| 3335 | } else if (arg_type == ARG_CONST_SIZE || |
| 3336 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
| 3337 | expected_type = SCALAR_VALUE; |
| 3338 | if (type != expected_type) |
| 3339 | goto err_type; |
| 3340 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 3341 | expected_type = CONST_PTR_TO_MAP; |
| 3342 | if (type != expected_type) |
| 3343 | goto err_type; |
| 3344 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 3345 | expected_type = PTR_TO_CTX; |
| 3346 | if (type != expected_type) |
| 3347 | goto err_type; |
| 3348 | err = check_ctx_reg(env, reg, regno); |
| 3349 | if (err < 0) |
| 3350 | return err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3351 | } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) { |
| 3352 | expected_type = PTR_TO_SOCK_COMMON; |
| 3353 | /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */ |
| 3354 | if (!type_is_sk_pointer(type)) |
| 3355 | goto err_type; |
| 3356 | if (reg->ref_obj_id) { |
| 3357 | if (meta->ref_obj_id) { |
| 3358 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", |
| 3359 | regno, reg->ref_obj_id, |
| 3360 | meta->ref_obj_id); |
| 3361 | return -EFAULT; |
| 3362 | } |
| 3363 | meta->ref_obj_id = reg->ref_obj_id; |
| 3364 | } |
| 3365 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
| 3366 | expected_type = PTR_TO_SOCKET; |
| 3367 | if (type != expected_type) |
| 3368 | goto err_type; |
| 3369 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
| 3370 | if (meta->func_id == BPF_FUNC_spin_lock) { |
| 3371 | if (process_spin_lock(env, regno, true)) |
| 3372 | return -EACCES; |
| 3373 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
| 3374 | if (process_spin_lock(env, regno, false)) |
| 3375 | return -EACCES; |
| 3376 | } else { |
| 3377 | verbose(env, "verifier internal error\n"); |
| 3378 | return -EFAULT; |
| 3379 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3380 | } else if (arg_type_is_mem_ptr(arg_type)) { |
| 3381 | expected_type = PTR_TO_STACK; |
| 3382 | /* One exception here. In case function allows for NULL to be |
| 3383 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
| 3384 | * happens during stack boundary checking. |
| 3385 | */ |
| 3386 | if (register_is_null(reg) && |
| 3387 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
| 3388 | /* final test in check_stack_boundary() */; |
| 3389 | else if (!type_is_pkt_pointer(type) && |
| 3390 | type != PTR_TO_MAP_VALUE && |
| 3391 | type != expected_type) |
| 3392 | goto err_type; |
| 3393 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3394 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 3395 | expected_type = PTR_TO_STACK; |
| 3396 | if (!type_is_pkt_pointer(type) && |
| 3397 | type != PTR_TO_MAP_VALUE && |
| 3398 | type != expected_type) |
| 3399 | goto err_type; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3400 | } else { |
| 3401 | verbose(env, "unsupported arg_type %d\n", arg_type); |
| 3402 | return -EFAULT; |
| 3403 | } |
| 3404 | |
| 3405 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 3406 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
| 3407 | meta->map_ptr = reg->map_ptr; |
| 3408 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 3409 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 3410 | * check that [key, key + map->key_size) are within |
| 3411 | * stack limits and initialized |
| 3412 | */ |
| 3413 | if (!meta->map_ptr) { |
| 3414 | /* in function declaration map_ptr must come before |
| 3415 | * map_key, so that it's verified and known before |
| 3416 | * we have to check map_key here. Otherwise it means |
| 3417 | * that kernel subsystem misconfigured verifier |
| 3418 | */ |
| 3419 | verbose(env, "invalid map_ptr to access map->key\n"); |
| 3420 | return -EACCES; |
| 3421 | } |
| 3422 | err = check_helper_mem_access(env, regno, |
| 3423 | meta->map_ptr->key_size, false, |
| 3424 | NULL); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3425 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
| 3426 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
| 3427 | !register_is_null(reg)) || |
| 3428 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3429 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 3430 | * check [value, value + map->value_size) validity |
| 3431 | */ |
| 3432 | if (!meta->map_ptr) { |
| 3433 | /* kernel subsystem misconfigured verifier */ |
| 3434 | verbose(env, "invalid map_ptr to access map->value\n"); |
| 3435 | return -EACCES; |
| 3436 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3437 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3438 | err = check_helper_mem_access(env, regno, |
| 3439 | meta->map_ptr->value_size, false, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3440 | meta); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3441 | } else if (arg_type_is_mem_size(arg_type)) { |
| 3442 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
| 3443 | |
| 3444 | /* remember the mem_size which may be used later |
| 3445 | * to refine return values. |
| 3446 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3447 | meta->msize_max_value = reg->umax_value; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3448 | |
| 3449 | /* The register is SCALAR_VALUE; the access check |
| 3450 | * happens using its boundaries. |
| 3451 | */ |
| 3452 | if (!tnum_is_const(reg->var_off)) |
| 3453 | /* For unprivileged variable accesses, disable raw |
| 3454 | * mode so that the program is required to |
| 3455 | * initialize all the memory that the helper could |
| 3456 | * just partially fill up. |
| 3457 | */ |
| 3458 | meta = NULL; |
| 3459 | |
| 3460 | if (reg->smin_value < 0) { |
| 3461 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
| 3462 | regno); |
| 3463 | return -EACCES; |
| 3464 | } |
| 3465 | |
| 3466 | if (reg->umin_value == 0) { |
| 3467 | err = check_helper_mem_access(env, regno - 1, 0, |
| 3468 | zero_size_allowed, |
| 3469 | meta); |
| 3470 | if (err) |
| 3471 | return err; |
| 3472 | } |
| 3473 | |
| 3474 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
| 3475 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
| 3476 | regno); |
| 3477 | return -EACCES; |
| 3478 | } |
| 3479 | err = check_helper_mem_access(env, regno - 1, |
| 3480 | reg->umax_value, |
| 3481 | zero_size_allowed, meta); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3482 | if (!err) |
| 3483 | err = mark_chain_precision(env, regno); |
| 3484 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 3485 | int size = int_ptr_type_to_size(arg_type); |
| 3486 | |
| 3487 | err = check_helper_mem_access(env, regno, size, false, meta); |
| 3488 | if (err) |
| 3489 | return err; |
| 3490 | err = check_ptr_alignment(env, reg, 0, size, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | return err; |
| 3494 | err_type: |
| 3495 | verbose(env, "R%d type=%s expected=%s\n", regno, |
| 3496 | reg_type_str[type], reg_type_str[expected_type]); |
| 3497 | return -EACCES; |
| 3498 | } |
| 3499 | |
| 3500 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 3501 | struct bpf_map *map, int func_id) |
| 3502 | { |
| 3503 | if (!map) |
| 3504 | return 0; |
| 3505 | |
| 3506 | /* We need a two way check, first is from map perspective ... */ |
| 3507 | switch (map->map_type) { |
| 3508 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 3509 | if (func_id != BPF_FUNC_tail_call) |
| 3510 | goto error; |
| 3511 | break; |
| 3512 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 3513 | if (func_id != BPF_FUNC_perf_event_read && |
| 3514 | func_id != BPF_FUNC_perf_event_output && |
| 3515 | func_id != BPF_FUNC_perf_event_read_value) |
| 3516 | goto error; |
| 3517 | break; |
| 3518 | case BPF_MAP_TYPE_STACK_TRACE: |
| 3519 | if (func_id != BPF_FUNC_get_stackid) |
| 3520 | goto error; |
| 3521 | break; |
| 3522 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
| 3523 | if (func_id != BPF_FUNC_skb_under_cgroup && |
| 3524 | func_id != BPF_FUNC_current_task_under_cgroup) |
| 3525 | goto error; |
| 3526 | break; |
| 3527 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3528 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3529 | if (func_id != BPF_FUNC_get_local_storage) |
| 3530 | goto error; |
| 3531 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3532 | case BPF_MAP_TYPE_DEVMAP: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3533 | case BPF_MAP_TYPE_DEVMAP_HASH: |
| 3534 | if (func_id != BPF_FUNC_redirect_map && |
| 3535 | func_id != BPF_FUNC_map_lookup_elem) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3536 | goto error; |
| 3537 | break; |
| 3538 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 3539 | * appear. |
| 3540 | */ |
| 3541 | case BPF_MAP_TYPE_CPUMAP: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3542 | if (func_id != BPF_FUNC_redirect_map) |
| 3543 | goto error; |
| 3544 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3545 | case BPF_MAP_TYPE_XSKMAP: |
| 3546 | if (func_id != BPF_FUNC_redirect_map && |
| 3547 | func_id != BPF_FUNC_map_lookup_elem) |
| 3548 | goto error; |
| 3549 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3550 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
| 3551 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
| 3552 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 3553 | goto error; |
| 3554 | break; |
| 3555 | case BPF_MAP_TYPE_SOCKMAP: |
| 3556 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 3557 | func_id != BPF_FUNC_sock_map_update && |
| 3558 | func_id != BPF_FUNC_map_delete_elem && |
| 3559 | func_id != BPF_FUNC_msg_redirect_map) |
| 3560 | goto error; |
| 3561 | break; |
| 3562 | case BPF_MAP_TYPE_SOCKHASH: |
| 3563 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 3564 | func_id != BPF_FUNC_sock_hash_update && |
| 3565 | func_id != BPF_FUNC_map_delete_elem && |
| 3566 | func_id != BPF_FUNC_msg_redirect_hash) |
| 3567 | goto error; |
| 3568 | break; |
| 3569 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 3570 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 3571 | goto error; |
| 3572 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3573 | case BPF_MAP_TYPE_QUEUE: |
| 3574 | case BPF_MAP_TYPE_STACK: |
| 3575 | if (func_id != BPF_FUNC_map_peek_elem && |
| 3576 | func_id != BPF_FUNC_map_pop_elem && |
| 3577 | func_id != BPF_FUNC_map_push_elem) |
| 3578 | goto error; |
| 3579 | break; |
| 3580 | case BPF_MAP_TYPE_SK_STORAGE: |
| 3581 | if (func_id != BPF_FUNC_sk_storage_get && |
| 3582 | func_id != BPF_FUNC_sk_storage_delete) |
| 3583 | goto error; |
| 3584 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3585 | default: |
| 3586 | break; |
| 3587 | } |
| 3588 | |
| 3589 | /* ... and second from the function itself. */ |
| 3590 | switch (func_id) { |
| 3591 | case BPF_FUNC_tail_call: |
| 3592 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 3593 | goto error; |
| 3594 | if (env->subprog_cnt > 1) { |
| 3595 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
| 3596 | return -EINVAL; |
| 3597 | } |
| 3598 | break; |
| 3599 | case BPF_FUNC_perf_event_read: |
| 3600 | case BPF_FUNC_perf_event_output: |
| 3601 | case BPF_FUNC_perf_event_read_value: |
| 3602 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 3603 | goto error; |
| 3604 | break; |
| 3605 | case BPF_FUNC_get_stackid: |
| 3606 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 3607 | goto error; |
| 3608 | break; |
| 3609 | case BPF_FUNC_current_task_under_cgroup: |
| 3610 | case BPF_FUNC_skb_under_cgroup: |
| 3611 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 3612 | goto error; |
| 3613 | break; |
| 3614 | case BPF_FUNC_redirect_map: |
| 3615 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3616 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3617 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 3618 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
| 3619 | goto error; |
| 3620 | break; |
| 3621 | case BPF_FUNC_sk_redirect_map: |
| 3622 | case BPF_FUNC_msg_redirect_map: |
| 3623 | case BPF_FUNC_sock_map_update: |
| 3624 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 3625 | goto error; |
| 3626 | break; |
| 3627 | case BPF_FUNC_sk_redirect_hash: |
| 3628 | case BPF_FUNC_msg_redirect_hash: |
| 3629 | case BPF_FUNC_sock_hash_update: |
| 3630 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
| 3631 | goto error; |
| 3632 | break; |
| 3633 | case BPF_FUNC_get_local_storage: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3634 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 3635 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3636 | goto error; |
| 3637 | break; |
| 3638 | case BPF_FUNC_sk_select_reuseport: |
| 3639 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) |
| 3640 | goto error; |
| 3641 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3642 | case BPF_FUNC_map_peek_elem: |
| 3643 | case BPF_FUNC_map_pop_elem: |
| 3644 | case BPF_FUNC_map_push_elem: |
| 3645 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 3646 | map->map_type != BPF_MAP_TYPE_STACK) |
| 3647 | goto error; |
| 3648 | break; |
| 3649 | case BPF_FUNC_sk_storage_get: |
| 3650 | case BPF_FUNC_sk_storage_delete: |
| 3651 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
| 3652 | goto error; |
| 3653 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3654 | default: |
| 3655 | break; |
| 3656 | } |
| 3657 | |
| 3658 | return 0; |
| 3659 | error: |
| 3660 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
| 3661 | map->map_type, func_id_name(func_id), func_id); |
| 3662 | return -EINVAL; |
| 3663 | } |
| 3664 | |
| 3665 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
| 3666 | { |
| 3667 | int count = 0; |
| 3668 | |
| 3669 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
| 3670 | count++; |
| 3671 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
| 3672 | count++; |
| 3673 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
| 3674 | count++; |
| 3675 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
| 3676 | count++; |
| 3677 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
| 3678 | count++; |
| 3679 | |
| 3680 | /* We only support one arg being in raw mode at the moment, |
| 3681 | * which is sufficient for the helper functions we have |
| 3682 | * right now. |
| 3683 | */ |
| 3684 | return count <= 1; |
| 3685 | } |
| 3686 | |
| 3687 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 3688 | enum bpf_arg_type arg_next) |
| 3689 | { |
| 3690 | return (arg_type_is_mem_ptr(arg_curr) && |
| 3691 | !arg_type_is_mem_size(arg_next)) || |
| 3692 | (!arg_type_is_mem_ptr(arg_curr) && |
| 3693 | arg_type_is_mem_size(arg_next)); |
| 3694 | } |
| 3695 | |
| 3696 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 3697 | { |
| 3698 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 3699 | * bytes from memory 'buf'. Both arg types need |
| 3700 | * to be paired, so make sure there's no buggy |
| 3701 | * helper function specification. |
| 3702 | */ |
| 3703 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 3704 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 3705 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 3706 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 3707 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 3708 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 3709 | return false; |
| 3710 | |
| 3711 | return true; |
| 3712 | } |
| 3713 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3714 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
| 3715 | { |
| 3716 | int count = 0; |
| 3717 | |
| 3718 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
| 3719 | count++; |
| 3720 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
| 3721 | count++; |
| 3722 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
| 3723 | count++; |
| 3724 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
| 3725 | count++; |
| 3726 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
| 3727 | count++; |
| 3728 | |
| 3729 | /* A reference acquiring function cannot acquire |
| 3730 | * another refcounted ptr. |
| 3731 | */ |
| 3732 | if (is_acquire_function(func_id) && count) |
| 3733 | return false; |
| 3734 | |
| 3735 | /* We only support one arg being unreferenced at the moment, |
| 3736 | * which is sufficient for the helper functions we have right now. |
| 3737 | */ |
| 3738 | return count <= 1; |
| 3739 | } |
| 3740 | |
| 3741 | 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] | 3742 | { |
| 3743 | return check_raw_mode_ok(fn) && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3744 | check_arg_pair_ok(fn) && |
| 3745 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 3749 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
| 3750 | */ |
| 3751 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 3752 | struct bpf_func_state *state) |
| 3753 | { |
| 3754 | struct bpf_reg_state *regs = state->regs, *reg; |
| 3755 | int i; |
| 3756 | |
| 3757 | for (i = 0; i < MAX_BPF_REG; i++) |
| 3758 | if (reg_is_pkt_pointer_any(®s[i])) |
| 3759 | mark_reg_unknown(env, regs, i); |
| 3760 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3761 | bpf_for_each_spilled_reg(i, state, reg) { |
| 3762 | if (!reg) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3763 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3764 | if (reg_is_pkt_pointer_any(reg)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3765 | __mark_reg_unknown(env, reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3766 | } |
| 3767 | } |
| 3768 | |
| 3769 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 3770 | { |
| 3771 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3772 | int i; |
| 3773 | |
| 3774 | for (i = 0; i <= vstate->curframe; i++) |
| 3775 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 3776 | } |
| 3777 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3778 | static void release_reg_references(struct bpf_verifier_env *env, |
| 3779 | struct bpf_func_state *state, |
| 3780 | int ref_obj_id) |
| 3781 | { |
| 3782 | struct bpf_reg_state *regs = state->regs, *reg; |
| 3783 | int i; |
| 3784 | |
| 3785 | for (i = 0; i < MAX_BPF_REG; i++) |
| 3786 | if (regs[i].ref_obj_id == ref_obj_id) |
| 3787 | mark_reg_unknown(env, regs, i); |
| 3788 | |
| 3789 | bpf_for_each_spilled_reg(i, state, reg) { |
| 3790 | if (!reg) |
| 3791 | continue; |
| 3792 | if (reg->ref_obj_id == ref_obj_id) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3793 | __mark_reg_unknown(env, reg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3794 | } |
| 3795 | } |
| 3796 | |
| 3797 | /* The pointer with the specified id has released its reference to kernel |
| 3798 | * resources. Identify all copies of the same pointer and clear the reference. |
| 3799 | */ |
| 3800 | static int release_reference(struct bpf_verifier_env *env, |
| 3801 | int ref_obj_id) |
| 3802 | { |
| 3803 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3804 | int err; |
| 3805 | int i; |
| 3806 | |
| 3807 | err = release_reference_state(cur_func(env), ref_obj_id); |
| 3808 | if (err) |
| 3809 | return err; |
| 3810 | |
| 3811 | for (i = 0; i <= vstate->curframe; i++) |
| 3812 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
| 3813 | |
| 3814 | return 0; |
| 3815 | } |
| 3816 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3817 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 3818 | int *insn_idx) |
| 3819 | { |
| 3820 | struct bpf_verifier_state *state = env->cur_state; |
| 3821 | struct bpf_func_state *caller, *callee; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3822 | int i, err, subprog, target_insn; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3823 | |
| 3824 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
| 3825 | verbose(env, "the call stack of %d frames is too deep\n", |
| 3826 | state->curframe + 2); |
| 3827 | return -E2BIG; |
| 3828 | } |
| 3829 | |
| 3830 | target_insn = *insn_idx + insn->imm; |
| 3831 | subprog = find_subprog(env, target_insn + 1); |
| 3832 | if (subprog < 0) { |
| 3833 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 3834 | target_insn + 1); |
| 3835 | return -EFAULT; |
| 3836 | } |
| 3837 | |
| 3838 | caller = state->frame[state->curframe]; |
| 3839 | if (state->frame[state->curframe + 1]) { |
| 3840 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 3841 | state->curframe + 1); |
| 3842 | return -EFAULT; |
| 3843 | } |
| 3844 | |
| 3845 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 3846 | if (!callee) |
| 3847 | return -ENOMEM; |
| 3848 | state->frame[state->curframe + 1] = callee; |
| 3849 | |
| 3850 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 3851 | * into its own stack before reading from it. |
| 3852 | * callee can read/write into caller's stack |
| 3853 | */ |
| 3854 | init_func_state(env, callee, |
| 3855 | /* remember the callsite, it will be used by bpf_exit */ |
| 3856 | *insn_idx /* callsite */, |
| 3857 | state->curframe + 1 /* frameno within this callchain */, |
| 3858 | subprog /* subprog number within this prog */); |
| 3859 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3860 | /* Transfer references to the callee */ |
| 3861 | err = transfer_reference_state(callee, caller); |
| 3862 | if (err) |
| 3863 | return err; |
| 3864 | |
| 3865 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 3866 | * pointers, which connects us up to the liveness chain |
| 3867 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3868 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 3869 | callee->regs[i] = caller->regs[i]; |
| 3870 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3871 | /* after the call registers r0 - r5 were scratched */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3872 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 3873 | mark_reg_not_init(env, caller->regs, caller_saved[i]); |
| 3874 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 3875 | } |
| 3876 | |
| 3877 | /* only increment it after check_reg_arg() finished */ |
| 3878 | state->curframe++; |
| 3879 | |
| 3880 | /* and go analyze first insn of the callee */ |
| 3881 | *insn_idx = target_insn; |
| 3882 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3883 | if (env->log.level & BPF_LOG_LEVEL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3884 | verbose(env, "caller:\n"); |
| 3885 | print_verifier_state(env, caller); |
| 3886 | verbose(env, "callee:\n"); |
| 3887 | print_verifier_state(env, callee); |
| 3888 | } |
| 3889 | return 0; |
| 3890 | } |
| 3891 | |
| 3892 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 3893 | { |
| 3894 | struct bpf_verifier_state *state = env->cur_state; |
| 3895 | struct bpf_func_state *caller, *callee; |
| 3896 | struct bpf_reg_state *r0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3897 | int err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3898 | |
| 3899 | callee = state->frame[state->curframe]; |
| 3900 | r0 = &callee->regs[BPF_REG_0]; |
| 3901 | if (r0->type == PTR_TO_STACK) { |
| 3902 | /* technically it's ok to return caller's stack pointer |
| 3903 | * (or caller's caller's pointer) back to the caller, |
| 3904 | * since these pointers are valid. Only current stack |
| 3905 | * pointer will be invalid as soon as function exits, |
| 3906 | * but let's be conservative |
| 3907 | */ |
| 3908 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 3909 | return -EINVAL; |
| 3910 | } |
| 3911 | |
| 3912 | state->curframe--; |
| 3913 | caller = state->frame[state->curframe]; |
| 3914 | /* return to the caller whatever r0 had in the callee */ |
| 3915 | caller->regs[BPF_REG_0] = *r0; |
| 3916 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3917 | /* Transfer references to the caller */ |
| 3918 | err = transfer_reference_state(caller, callee); |
| 3919 | if (err) |
| 3920 | return err; |
| 3921 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3922 | *insn_idx = callee->callsite + 1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3923 | if (env->log.level & BPF_LOG_LEVEL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3924 | verbose(env, "returning from callee:\n"); |
| 3925 | print_verifier_state(env, callee); |
| 3926 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 3927 | print_verifier_state(env, caller); |
| 3928 | } |
| 3929 | /* clear everything in the callee */ |
| 3930 | free_func_state(callee); |
| 3931 | state->frame[state->curframe + 1] = NULL; |
| 3932 | return 0; |
| 3933 | } |
| 3934 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3935 | static int do_refine_retval_range(struct bpf_verifier_env *env, |
| 3936 | struct bpf_reg_state *regs, int ret_type, |
| 3937 | int func_id, struct bpf_call_arg_meta *meta) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3938 | { |
| 3939 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3940 | struct bpf_reg_state tmp_reg = *ret_reg; |
| 3941 | bool ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3942 | |
| 3943 | if (ret_type != RET_INTEGER || |
| 3944 | (func_id != BPF_FUNC_get_stack && |
| 3945 | func_id != BPF_FUNC_probe_read_str)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3946 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3947 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3948 | /* Error case where ret is in interval [S32MIN, -1]. */ |
| 3949 | ret_reg->smin_value = S32_MIN; |
| 3950 | ret_reg->smax_value = -1; |
| 3951 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3952 | __reg_deduce_bounds(ret_reg); |
| 3953 | __reg_bound_offset(ret_reg); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 3954 | __update_reg_bounds(ret_reg); |
| 3955 | |
| 3956 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, false); |
| 3957 | if (!ret) |
| 3958 | return -EFAULT; |
| 3959 | |
| 3960 | *ret_reg = tmp_reg; |
| 3961 | |
| 3962 | /* Success case where ret is in range [0, msize_max_value]. */ |
| 3963 | ret_reg->smin_value = 0; |
| 3964 | ret_reg->smax_value = meta->msize_max_value; |
| 3965 | ret_reg->umin_value = ret_reg->smin_value; |
| 3966 | ret_reg->umax_value = ret_reg->smax_value; |
| 3967 | |
| 3968 | __reg_deduce_bounds(ret_reg); |
| 3969 | __reg_bound_offset(ret_reg); |
| 3970 | __update_reg_bounds(ret_reg); |
| 3971 | |
| 3972 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
| 3975 | static int |
| 3976 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 3977 | int func_id, int insn_idx) |
| 3978 | { |
| 3979 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3980 | struct bpf_map *map = meta->map_ptr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3981 | |
| 3982 | if (func_id != BPF_FUNC_tail_call && |
| 3983 | func_id != BPF_FUNC_map_lookup_elem && |
| 3984 | func_id != BPF_FUNC_map_update_elem && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3985 | func_id != BPF_FUNC_map_delete_elem && |
| 3986 | func_id != BPF_FUNC_map_push_elem && |
| 3987 | func_id != BPF_FUNC_map_pop_elem && |
| 3988 | func_id != BPF_FUNC_map_peek_elem) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3989 | return 0; |
| 3990 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3991 | if (map == NULL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3992 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 3993 | return -EINVAL; |
| 3994 | } |
| 3995 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3996 | /* In case of read-only, some additional restrictions |
| 3997 | * need to be applied in order to prevent altering the |
| 3998 | * state of the map from program side. |
| 3999 | */ |
| 4000 | if ((map->map_flags & BPF_F_RDONLY_PROG) && |
| 4001 | (func_id == BPF_FUNC_map_delete_elem || |
| 4002 | func_id == BPF_FUNC_map_update_elem || |
| 4003 | func_id == BPF_FUNC_map_push_elem || |
| 4004 | func_id == BPF_FUNC_map_pop_elem)) { |
| 4005 | verbose(env, "write into map forbidden\n"); |
| 4006 | return -EACCES; |
| 4007 | } |
| 4008 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4009 | if (!BPF_MAP_PTR(aux->map_state)) |
| 4010 | bpf_map_ptr_store(aux, meta->map_ptr, |
| 4011 | meta->map_ptr->unpriv_array); |
| 4012 | else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr) |
| 4013 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
| 4014 | meta->map_ptr->unpriv_array); |
| 4015 | return 0; |
| 4016 | } |
| 4017 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4018 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 4019 | { |
| 4020 | struct bpf_func_state *state = cur_func(env); |
| 4021 | int i; |
| 4022 | |
| 4023 | for (i = 0; i < state->acquired_refs; i++) { |
| 4024 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 4025 | state->refs[i].id, state->refs[i].insn_idx); |
| 4026 | } |
| 4027 | return state->acquired_refs ? -EINVAL : 0; |
| 4028 | } |
| 4029 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4030 | static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
| 4031 | { |
| 4032 | const struct bpf_func_proto *fn = NULL; |
| 4033 | struct bpf_reg_state *regs; |
| 4034 | struct bpf_call_arg_meta meta; |
| 4035 | bool changes_data; |
| 4036 | int i, err; |
| 4037 | |
| 4038 | /* find function prototype */ |
| 4039 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
| 4040 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 4041 | func_id); |
| 4042 | return -EINVAL; |
| 4043 | } |
| 4044 | |
| 4045 | if (env->ops->get_func_proto) |
| 4046 | fn = env->ops->get_func_proto(func_id, env->prog); |
| 4047 | if (!fn) { |
| 4048 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 4049 | func_id); |
| 4050 | return -EINVAL; |
| 4051 | } |
| 4052 | |
| 4053 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
| 4054 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
| 4055 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
| 4056 | return -EINVAL; |
| 4057 | } |
| 4058 | |
| 4059 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
| 4060 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
| 4061 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 4062 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 4063 | func_id_name(func_id), func_id); |
| 4064 | return -EINVAL; |
| 4065 | } |
| 4066 | |
| 4067 | memset(&meta, 0, sizeof(meta)); |
| 4068 | meta.pkt_access = fn->pkt_access; |
| 4069 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4070 | err = check_func_proto(fn, func_id); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4071 | if (err) { |
| 4072 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
| 4073 | func_id_name(func_id), func_id); |
| 4074 | return err; |
| 4075 | } |
| 4076 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4077 | meta.func_id = func_id; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4078 | /* check args */ |
| 4079 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
| 4080 | if (err) |
| 4081 | return err; |
| 4082 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
| 4083 | if (err) |
| 4084 | return err; |
| 4085 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
| 4086 | if (err) |
| 4087 | return err; |
| 4088 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
| 4089 | if (err) |
| 4090 | return err; |
| 4091 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
| 4092 | if (err) |
| 4093 | return err; |
| 4094 | |
| 4095 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 4096 | if (err) |
| 4097 | return err; |
| 4098 | |
| 4099 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 4100 | * is inferred from register state. |
| 4101 | */ |
| 4102 | for (i = 0; i < meta.access_size; i++) { |
| 4103 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 4104 | BPF_WRITE, -1, false); |
| 4105 | if (err) |
| 4106 | return err; |
| 4107 | } |
| 4108 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4109 | if (func_id == BPF_FUNC_tail_call) { |
| 4110 | err = check_reference_leak(env); |
| 4111 | if (err) { |
| 4112 | verbose(env, "tail_call would lead to reference leak\n"); |
| 4113 | return err; |
| 4114 | } |
| 4115 | } else if (is_release_function(func_id)) { |
| 4116 | err = release_reference(env, meta.ref_obj_id); |
| 4117 | if (err) { |
| 4118 | verbose(env, "func %s#%d reference has not been acquired before\n", |
| 4119 | func_id_name(func_id), func_id); |
| 4120 | return err; |
| 4121 | } |
| 4122 | } |
| 4123 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4124 | regs = cur_regs(env); |
| 4125 | |
| 4126 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 4127 | * this is required because get_local_storage() can't return an error. |
| 4128 | */ |
| 4129 | if (func_id == BPF_FUNC_get_local_storage && |
| 4130 | !register_is_null(®s[BPF_REG_2])) { |
| 4131 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 4132 | return -EINVAL; |
| 4133 | } |
| 4134 | |
| 4135 | /* reset caller saved regs */ |
| 4136 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 4137 | mark_reg_not_init(env, regs, caller_saved[i]); |
| 4138 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 4139 | } |
| 4140 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4141 | /* helper call returns 64-bit value. */ |
| 4142 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 4143 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4144 | /* update return register (already marked as written above) */ |
| 4145 | if (fn->ret_type == RET_INTEGER) { |
| 4146 | /* sets type to SCALAR_VALUE */ |
| 4147 | mark_reg_unknown(env, regs, BPF_REG_0); |
| 4148 | } else if (fn->ret_type == RET_VOID) { |
| 4149 | regs[BPF_REG_0].type = NOT_INIT; |
| 4150 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 4151 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4152 | /* There is no offset yet applied, variable or fixed */ |
| 4153 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 4154 | /* remember map_ptr, so that check_map_access() |
| 4155 | * can check 'value_size' boundary of memory access |
| 4156 | * to map element returned from bpf_map_lookup_elem() |
| 4157 | */ |
| 4158 | if (meta.map_ptr == NULL) { |
| 4159 | verbose(env, |
| 4160 | "kernel subsystem misconfigured verifier\n"); |
| 4161 | return -EINVAL; |
| 4162 | } |
| 4163 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4164 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 4165 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
| 4166 | if (map_value_has_spin_lock(meta.map_ptr)) |
| 4167 | regs[BPF_REG_0].id = ++env->id_gen; |
| 4168 | } else { |
| 4169 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
| 4170 | regs[BPF_REG_0].id = ++env->id_gen; |
| 4171 | } |
| 4172 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
| 4173 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 4174 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; |
| 4175 | regs[BPF_REG_0].id = ++env->id_gen; |
| 4176 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
| 4177 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 4178 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; |
| 4179 | regs[BPF_REG_0].id = ++env->id_gen; |
| 4180 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
| 4181 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 4182 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4183 | regs[BPF_REG_0].id = ++env->id_gen; |
| 4184 | } else { |
| 4185 | verbose(env, "unknown return type %d of func %s#%d\n", |
| 4186 | fn->ret_type, func_id_name(func_id), func_id); |
| 4187 | return -EINVAL; |
| 4188 | } |
| 4189 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4190 | if (is_ptr_cast_function(func_id)) { |
| 4191 | /* For release_reference() */ |
| 4192 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; |
| 4193 | } else if (is_acquire_function(func_id)) { |
| 4194 | int id = acquire_reference_state(env, insn_idx); |
| 4195 | |
| 4196 | if (id < 0) |
| 4197 | return id; |
| 4198 | /* For mark_ptr_or_null_reg() */ |
| 4199 | regs[BPF_REG_0].id = id; |
| 4200 | /* For release_reference() */ |
| 4201 | regs[BPF_REG_0].ref_obj_id = id; |
| 4202 | } |
| 4203 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4204 | err = do_refine_retval_range(env, regs, fn->ret_type, func_id, &meta); |
| 4205 | if (err) |
| 4206 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4207 | |
| 4208 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
| 4209 | if (err) |
| 4210 | return err; |
| 4211 | |
| 4212 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
| 4213 | const char *err_str; |
| 4214 | |
| 4215 | #ifdef CONFIG_PERF_EVENTS |
| 4216 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 4217 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 4218 | #else |
| 4219 | err = -ENOTSUPP; |
| 4220 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 4221 | #endif |
| 4222 | if (err) { |
| 4223 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 4224 | return err; |
| 4225 | } |
| 4226 | |
| 4227 | env->prog->has_callchain_buf = true; |
| 4228 | } |
| 4229 | |
| 4230 | if (changes_data) |
| 4231 | clear_all_pkt_pointers(env); |
| 4232 | return 0; |
| 4233 | } |
| 4234 | |
| 4235 | static bool signed_add_overflows(s64 a, s64 b) |
| 4236 | { |
| 4237 | /* Do the add in u64, where overflow is well-defined */ |
| 4238 | s64 res = (s64)((u64)a + (u64)b); |
| 4239 | |
| 4240 | if (b < 0) |
| 4241 | return res > a; |
| 4242 | return res < a; |
| 4243 | } |
| 4244 | |
| 4245 | static bool signed_sub_overflows(s64 a, s64 b) |
| 4246 | { |
| 4247 | /* Do the sub in u64, where overflow is well-defined */ |
| 4248 | s64 res = (s64)((u64)a - (u64)b); |
| 4249 | |
| 4250 | if (b < 0) |
| 4251 | return res < a; |
| 4252 | return res > a; |
| 4253 | } |
| 4254 | |
| 4255 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 4256 | const struct bpf_reg_state *reg, |
| 4257 | enum bpf_reg_type type) |
| 4258 | { |
| 4259 | bool known = tnum_is_const(reg->var_off); |
| 4260 | s64 val = reg->var_off.value; |
| 4261 | s64 smin = reg->smin_value; |
| 4262 | |
| 4263 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 4264 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 4265 | reg_type_str[type], val); |
| 4266 | return false; |
| 4267 | } |
| 4268 | |
| 4269 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 4270 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 4271 | reg_type_str[type], reg->off); |
| 4272 | return false; |
| 4273 | } |
| 4274 | |
| 4275 | if (smin == S64_MIN) { |
| 4276 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 4277 | reg_type_str[type]); |
| 4278 | return false; |
| 4279 | } |
| 4280 | |
| 4281 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 4282 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 4283 | smin, reg_type_str[type]); |
| 4284 | return false; |
| 4285 | } |
| 4286 | |
| 4287 | return true; |
| 4288 | } |
| 4289 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4290 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
| 4291 | { |
| 4292 | return &env->insn_aux_data[env->insn_idx]; |
| 4293 | } |
| 4294 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4295 | enum { |
| 4296 | REASON_BOUNDS = -1, |
| 4297 | REASON_TYPE = -2, |
| 4298 | REASON_PATHS = -3, |
| 4299 | REASON_LIMIT = -4, |
| 4300 | REASON_STACK = -5, |
| 4301 | }; |
| 4302 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4303 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4304 | u32 *alu_limit, bool mask_to_left) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4305 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4306 | u32 max = 0, ptr_limit = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4307 | |
| 4308 | switch (ptr_reg->type) { |
| 4309 | case PTR_TO_STACK: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4310 | /* Offset 0 is out-of-bounds, but acceptable start for the |
| 4311 | * left direction, see BPF_REG_FP. Also, unknown scalar |
| 4312 | * offset where we would need to deal with min/max bounds is |
| 4313 | * currently prohibited for unprivileged. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4314 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4315 | max = MAX_BPF_STACK + mask_to_left; |
| 4316 | ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off); |
| 4317 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4318 | case PTR_TO_MAP_VALUE: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4319 | max = ptr_reg->map_ptr->value_size; |
| 4320 | ptr_limit = (mask_to_left ? |
| 4321 | ptr_reg->smin_value : |
| 4322 | ptr_reg->umax_value) + ptr_reg->off; |
| 4323 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4324 | default: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4325 | return REASON_TYPE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4326 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4327 | |
| 4328 | if (ptr_limit >= max) |
| 4329 | return REASON_LIMIT; |
| 4330 | *alu_limit = ptr_limit; |
| 4331 | return 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
| 4335 | const struct bpf_insn *insn) |
| 4336 | { |
| 4337 | return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K; |
| 4338 | } |
| 4339 | |
| 4340 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, |
| 4341 | u32 alu_state, u32 alu_limit) |
| 4342 | { |
| 4343 | /* If we arrived here from different branches with different |
| 4344 | * state or limits to sanitize, then this won't work. |
| 4345 | */ |
| 4346 | if (aux->alu_state && |
| 4347 | (aux->alu_state != alu_state || |
| 4348 | aux->alu_limit != alu_limit)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4349 | return REASON_PATHS; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4350 | |
| 4351 | /* Corresponding fixup done in fixup_bpf_calls(). */ |
| 4352 | aux->alu_state = alu_state; |
| 4353 | aux->alu_limit = alu_limit; |
| 4354 | return 0; |
| 4355 | } |
| 4356 | |
| 4357 | static int sanitize_val_alu(struct bpf_verifier_env *env, |
| 4358 | struct bpf_insn *insn) |
| 4359 | { |
| 4360 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 4361 | |
| 4362 | if (can_skip_alu_sanitation(env, insn)) |
| 4363 | return 0; |
| 4364 | |
| 4365 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); |
| 4366 | } |
| 4367 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4368 | static bool sanitize_needed(u8 opcode) |
| 4369 | { |
| 4370 | return opcode == BPF_ADD || opcode == BPF_SUB; |
| 4371 | } |
| 4372 | |
| 4373 | struct bpf_sanitize_info { |
| 4374 | struct bpf_insn_aux_data aux; |
| 4375 | bool mask_to_left; |
| 4376 | }; |
| 4377 | |
| 4378 | static struct bpf_verifier_state * |
| 4379 | sanitize_speculative_path(struct bpf_verifier_env *env, |
| 4380 | const struct bpf_insn *insn, |
| 4381 | u32 next_idx, u32 curr_idx) |
| 4382 | { |
| 4383 | struct bpf_verifier_state *branch; |
| 4384 | struct bpf_reg_state *regs; |
| 4385 | |
| 4386 | branch = push_stack(env, next_idx, curr_idx, true); |
| 4387 | if (branch && insn) { |
| 4388 | regs = branch->frame[branch->curframe]->regs; |
| 4389 | if (BPF_SRC(insn->code) == BPF_K) { |
| 4390 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 4391 | } else if (BPF_SRC(insn->code) == BPF_X) { |
| 4392 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 4393 | mark_reg_unknown(env, regs, insn->src_reg); |
| 4394 | } |
| 4395 | } |
| 4396 | return branch; |
| 4397 | } |
| 4398 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4399 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
| 4400 | struct bpf_insn *insn, |
| 4401 | const struct bpf_reg_state *ptr_reg, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4402 | const struct bpf_reg_state *off_reg, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4403 | struct bpf_reg_state *dst_reg, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4404 | struct bpf_sanitize_info *info, |
| 4405 | const bool commit_window) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4406 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4407 | 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] | 4408 | struct bpf_verifier_state *vstate = env->cur_state; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4409 | bool off_is_imm = tnum_is_const(off_reg->var_off); |
| 4410 | bool off_is_neg = off_reg->smin_value < 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4411 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
| 4412 | u8 opcode = BPF_OP(insn->code); |
| 4413 | u32 alu_state, alu_limit; |
| 4414 | struct bpf_reg_state tmp; |
| 4415 | bool ret; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4416 | int err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4417 | |
| 4418 | if (can_skip_alu_sanitation(env, insn)) |
| 4419 | return 0; |
| 4420 | |
| 4421 | /* We already marked aux for masking from non-speculative |
| 4422 | * paths, thus we got here in the first place. We only care |
| 4423 | * to explore bad access from here. |
| 4424 | */ |
| 4425 | if (vstate->speculative) |
| 4426 | goto do_sim; |
| 4427 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4428 | if (!commit_window) { |
| 4429 | if (!tnum_is_const(off_reg->var_off) && |
| 4430 | (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) |
| 4431 | return REASON_BOUNDS; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4432 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4433 | info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || |
| 4434 | (opcode == BPF_SUB && !off_is_neg); |
| 4435 | } |
| 4436 | |
| 4437 | err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); |
| 4438 | if (err < 0) |
| 4439 | return err; |
| 4440 | |
| 4441 | if (commit_window) { |
| 4442 | /* In commit phase we narrow the masking window based on |
| 4443 | * the observed pointer move after the simulated operation. |
| 4444 | */ |
| 4445 | alu_state = info->aux.alu_state; |
| 4446 | alu_limit = abs(info->aux.alu_limit - alu_limit); |
| 4447 | } else { |
| 4448 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; |
| 4449 | alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0; |
| 4450 | alu_state |= ptr_is_dst_reg ? |
| 4451 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; |
| 4452 | |
| 4453 | /* Limit pruning on unknown scalars to enable deep search for |
| 4454 | * potential masking differences from other program paths. |
| 4455 | */ |
| 4456 | if (!off_is_imm) |
| 4457 | env->explore_alu_limits = true; |
| 4458 | } |
| 4459 | |
| 4460 | err = update_alu_sanitation_state(aux, alu_state, alu_limit); |
| 4461 | if (err < 0) |
| 4462 | return err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4463 | do_sim: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4464 | /* If we're in commit phase, we're done here given we already |
| 4465 | * pushed the truncated dst_reg into the speculative verification |
| 4466 | * stack. |
| 4467 | * |
| 4468 | * Also, when register is a known constant, we rewrite register-based |
| 4469 | * operation to immediate-based, and thus do not need masking (and as |
| 4470 | * a consequence, do not need to simulate the zero-truncation either). |
| 4471 | */ |
| 4472 | if (commit_window || off_is_imm) |
| 4473 | return 0; |
| 4474 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4475 | /* Simulate and find potential out-of-bounds access under |
| 4476 | * speculative execution from truncation as a result of |
| 4477 | * masking when off was not within expected range. If off |
| 4478 | * sits in dst, then we temporarily need to move ptr there |
| 4479 | * to simulate dst (== 0) +/-= ptr. Needed, for example, |
| 4480 | * for cases where we use K-based arithmetic in one direction |
| 4481 | * and truncated reg-based in the other in order to explore |
| 4482 | * bad access. |
| 4483 | */ |
| 4484 | if (!ptr_is_dst_reg) { |
| 4485 | tmp = *dst_reg; |
| 4486 | *dst_reg = *ptr_reg; |
| 4487 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4488 | ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, |
| 4489 | env->insn_idx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4490 | if (!ptr_is_dst_reg && ret) |
| 4491 | *dst_reg = tmp; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4492 | return !ret ? REASON_STACK : 0; |
| 4493 | } |
| 4494 | |
| 4495 | static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) |
| 4496 | { |
| 4497 | struct bpf_verifier_state *vstate = env->cur_state; |
| 4498 | |
| 4499 | /* If we simulate paths under speculation, we don't update the |
| 4500 | * insn as 'seen' such that when we verify unreachable paths in |
| 4501 | * the non-speculative domain, sanitize_dead_code() can still |
| 4502 | * rewrite/sanitize them. |
| 4503 | */ |
| 4504 | if (!vstate->speculative) |
| 4505 | env->insn_aux_data[env->insn_idx].seen = true; |
| 4506 | } |
| 4507 | |
| 4508 | static int sanitize_err(struct bpf_verifier_env *env, |
| 4509 | const struct bpf_insn *insn, int reason, |
| 4510 | const struct bpf_reg_state *off_reg, |
| 4511 | const struct bpf_reg_state *dst_reg) |
| 4512 | { |
| 4513 | static const char *err = "pointer arithmetic with it prohibited for !root"; |
| 4514 | const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; |
| 4515 | u32 dst = insn->dst_reg, src = insn->src_reg; |
| 4516 | |
| 4517 | switch (reason) { |
| 4518 | case REASON_BOUNDS: |
| 4519 | verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", |
| 4520 | off_reg == dst_reg ? dst : src, err); |
| 4521 | break; |
| 4522 | case REASON_TYPE: |
| 4523 | verbose(env, "R%d has pointer with unsupported alu operation, %s\n", |
| 4524 | off_reg == dst_reg ? src : dst, err); |
| 4525 | break; |
| 4526 | case REASON_PATHS: |
| 4527 | verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", |
| 4528 | dst, op, err); |
| 4529 | break; |
| 4530 | case REASON_LIMIT: |
| 4531 | verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", |
| 4532 | dst, op, err); |
| 4533 | break; |
| 4534 | case REASON_STACK: |
| 4535 | verbose(env, "R%d could not be pushed for speculative verification, %s\n", |
| 4536 | dst, err); |
| 4537 | break; |
| 4538 | default: |
| 4539 | verbose(env, "verifier internal error: unknown reason (%d)\n", |
| 4540 | reason); |
| 4541 | break; |
| 4542 | } |
| 4543 | |
| 4544 | return -EACCES; |
| 4545 | } |
| 4546 | |
| 4547 | static int sanitize_check_bounds(struct bpf_verifier_env *env, |
| 4548 | const struct bpf_insn *insn, |
| 4549 | const struct bpf_reg_state *dst_reg) |
| 4550 | { |
| 4551 | u32 dst = insn->dst_reg; |
| 4552 | |
| 4553 | /* For unprivileged we require that resulting offset must be in bounds |
| 4554 | * in order to be able to sanitize access later on. |
| 4555 | */ |
| 4556 | if (env->allow_ptr_leaks) |
| 4557 | return 0; |
| 4558 | |
| 4559 | switch (dst_reg->type) { |
| 4560 | case PTR_TO_STACK: |
| 4561 | if (check_stack_access(env, dst_reg, dst_reg->off + |
| 4562 | dst_reg->var_off.value, 1)) { |
| 4563 | verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 4564 | "prohibited for !root\n", dst); |
| 4565 | return -EACCES; |
| 4566 | } |
| 4567 | break; |
| 4568 | case PTR_TO_MAP_VALUE: |
| 4569 | if (check_map_access(env, dst, dst_reg->off, 1, false)) { |
| 4570 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
| 4571 | "prohibited for !root\n", dst); |
| 4572 | return -EACCES; |
| 4573 | } |
| 4574 | break; |
| 4575 | default: |
| 4576 | break; |
| 4577 | } |
| 4578 | |
| 4579 | return 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4580 | } |
| 4581 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4582 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
| 4583 | * Caller should also handle BPF_MOV case separately. |
| 4584 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 4585 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 4586 | */ |
| 4587 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 4588 | struct bpf_insn *insn, |
| 4589 | const struct bpf_reg_state *ptr_reg, |
| 4590 | const struct bpf_reg_state *off_reg) |
| 4591 | { |
| 4592 | struct bpf_verifier_state *vstate = env->cur_state; |
| 4593 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 4594 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
| 4595 | bool known = tnum_is_const(off_reg->var_off); |
| 4596 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 4597 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 4598 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 4599 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4600 | struct bpf_sanitize_info info = {}; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4601 | u8 opcode = BPF_OP(insn->code); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4602 | u32 dst = insn->dst_reg; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4603 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4604 | |
| 4605 | dst_reg = ®s[dst]; |
| 4606 | |
| 4607 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 4608 | smin_val > smax_val || umin_val > umax_val) { |
| 4609 | /* Taint dst register if offset had invalid bounds derived from |
| 4610 | * e.g. dead branches. |
| 4611 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4612 | __mark_reg_unknown(env, dst_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4613 | return 0; |
| 4614 | } |
| 4615 | |
| 4616 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 4617 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
| 4618 | verbose(env, |
| 4619 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 4620 | dst); |
| 4621 | return -EACCES; |
| 4622 | } |
| 4623 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4624 | switch (ptr_reg->type) { |
| 4625 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 4626 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
| 4627 | dst, reg_type_str[ptr_reg->type]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4628 | return -EACCES; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4629 | case CONST_PTR_TO_MAP: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4630 | /* smin_val represents the known value */ |
| 4631 | if (known && smin_val == 0 && opcode == BPF_ADD) |
| 4632 | break; |
| 4633 | /* fall-through */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4634 | case PTR_TO_PACKET_END: |
| 4635 | case PTR_TO_SOCKET: |
| 4636 | case PTR_TO_SOCKET_OR_NULL: |
| 4637 | case PTR_TO_SOCK_COMMON: |
| 4638 | case PTR_TO_SOCK_COMMON_OR_NULL: |
| 4639 | case PTR_TO_TCP_SOCK: |
| 4640 | case PTR_TO_TCP_SOCK_OR_NULL: |
| 4641 | case PTR_TO_XDP_SOCK: |
| 4642 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
| 4643 | dst, reg_type_str[ptr_reg->type]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4644 | return -EACCES; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4645 | default: |
| 4646 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4647 | } |
| 4648 | |
| 4649 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 4650 | * The id may be overwritten later if we create a new variable offset. |
| 4651 | */ |
| 4652 | dst_reg->type = ptr_reg->type; |
| 4653 | dst_reg->id = ptr_reg->id; |
| 4654 | |
| 4655 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 4656 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 4657 | return -EINVAL; |
| 4658 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4659 | if (sanitize_needed(opcode)) { |
| 4660 | ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg, |
| 4661 | &info, false); |
| 4662 | if (ret < 0) |
| 4663 | return sanitize_err(env, insn, ret, off_reg, dst_reg); |
| 4664 | } |
| 4665 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4666 | switch (opcode) { |
| 4667 | case BPF_ADD: |
| 4668 | /* We can take a fixed offset as long as it doesn't overflow |
| 4669 | * the s32 'off' field |
| 4670 | */ |
| 4671 | if (known && (ptr_reg->off + smin_val == |
| 4672 | (s64)(s32)(ptr_reg->off + smin_val))) { |
| 4673 | /* pointer += K. Accumulate it into fixed offset */ |
| 4674 | dst_reg->smin_value = smin_ptr; |
| 4675 | dst_reg->smax_value = smax_ptr; |
| 4676 | dst_reg->umin_value = umin_ptr; |
| 4677 | dst_reg->umax_value = umax_ptr; |
| 4678 | dst_reg->var_off = ptr_reg->var_off; |
| 4679 | dst_reg->off = ptr_reg->off + smin_val; |
| 4680 | dst_reg->raw = ptr_reg->raw; |
| 4681 | break; |
| 4682 | } |
| 4683 | /* A new variable offset is created. Note that off_reg->off |
| 4684 | * == 0, since it's a scalar. |
| 4685 | * dst_reg gets the pointer type and since some positive |
| 4686 | * integer value was added to the pointer, give it a new 'id' |
| 4687 | * if it's a PTR_TO_PACKET. |
| 4688 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 4689 | * added into the variable offset, and we copy the fixed offset |
| 4690 | * from ptr_reg. |
| 4691 | */ |
| 4692 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 4693 | signed_add_overflows(smax_ptr, smax_val)) { |
| 4694 | dst_reg->smin_value = S64_MIN; |
| 4695 | dst_reg->smax_value = S64_MAX; |
| 4696 | } else { |
| 4697 | dst_reg->smin_value = smin_ptr + smin_val; |
| 4698 | dst_reg->smax_value = smax_ptr + smax_val; |
| 4699 | } |
| 4700 | if (umin_ptr + umin_val < umin_ptr || |
| 4701 | umax_ptr + umax_val < umax_ptr) { |
| 4702 | dst_reg->umin_value = 0; |
| 4703 | dst_reg->umax_value = U64_MAX; |
| 4704 | } else { |
| 4705 | dst_reg->umin_value = umin_ptr + umin_val; |
| 4706 | dst_reg->umax_value = umax_ptr + umax_val; |
| 4707 | } |
| 4708 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 4709 | dst_reg->off = ptr_reg->off; |
| 4710 | dst_reg->raw = ptr_reg->raw; |
| 4711 | if (reg_is_pkt_pointer(ptr_reg)) { |
| 4712 | dst_reg->id = ++env->id_gen; |
| 4713 | /* something was added to pkt_ptr, set range to zero */ |
| 4714 | dst_reg->raw = 0; |
| 4715 | } |
| 4716 | break; |
| 4717 | case BPF_SUB: |
| 4718 | if (dst_reg == off_reg) { |
| 4719 | /* scalar -= pointer. Creates an unknown scalar */ |
| 4720 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 4721 | dst); |
| 4722 | return -EACCES; |
| 4723 | } |
| 4724 | /* We don't allow subtraction from FP, because (according to |
| 4725 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 4726 | * be able to deal with it. |
| 4727 | */ |
| 4728 | if (ptr_reg->type == PTR_TO_STACK) { |
| 4729 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 4730 | dst); |
| 4731 | return -EACCES; |
| 4732 | } |
| 4733 | if (known && (ptr_reg->off - smin_val == |
| 4734 | (s64)(s32)(ptr_reg->off - smin_val))) { |
| 4735 | /* pointer -= K. Subtract it from fixed offset */ |
| 4736 | dst_reg->smin_value = smin_ptr; |
| 4737 | dst_reg->smax_value = smax_ptr; |
| 4738 | dst_reg->umin_value = umin_ptr; |
| 4739 | dst_reg->umax_value = umax_ptr; |
| 4740 | dst_reg->var_off = ptr_reg->var_off; |
| 4741 | dst_reg->id = ptr_reg->id; |
| 4742 | dst_reg->off = ptr_reg->off - smin_val; |
| 4743 | dst_reg->raw = ptr_reg->raw; |
| 4744 | break; |
| 4745 | } |
| 4746 | /* A new variable offset is created. If the subtrahend is known |
| 4747 | * nonnegative, then any reg->range we had before is still good. |
| 4748 | */ |
| 4749 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 4750 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 4751 | /* Overflow possible, we know nothing */ |
| 4752 | dst_reg->smin_value = S64_MIN; |
| 4753 | dst_reg->smax_value = S64_MAX; |
| 4754 | } else { |
| 4755 | dst_reg->smin_value = smin_ptr - smax_val; |
| 4756 | dst_reg->smax_value = smax_ptr - smin_val; |
| 4757 | } |
| 4758 | if (umin_ptr < umax_val) { |
| 4759 | /* Overflow possible, we know nothing */ |
| 4760 | dst_reg->umin_value = 0; |
| 4761 | dst_reg->umax_value = U64_MAX; |
| 4762 | } else { |
| 4763 | /* Cannot overflow (as long as bounds are consistent) */ |
| 4764 | dst_reg->umin_value = umin_ptr - umax_val; |
| 4765 | dst_reg->umax_value = umax_ptr - umin_val; |
| 4766 | } |
| 4767 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 4768 | dst_reg->off = ptr_reg->off; |
| 4769 | dst_reg->raw = ptr_reg->raw; |
| 4770 | if (reg_is_pkt_pointer(ptr_reg)) { |
| 4771 | dst_reg->id = ++env->id_gen; |
| 4772 | /* something was added to pkt_ptr, set range to zero */ |
| 4773 | if (smin_val < 0) |
| 4774 | dst_reg->raw = 0; |
| 4775 | } |
| 4776 | break; |
| 4777 | case BPF_AND: |
| 4778 | case BPF_OR: |
| 4779 | case BPF_XOR: |
| 4780 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 4781 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 4782 | dst, bpf_alu_string[opcode >> 4]); |
| 4783 | return -EACCES; |
| 4784 | default: |
| 4785 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
| 4786 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 4787 | dst, bpf_alu_string[opcode >> 4]); |
| 4788 | return -EACCES; |
| 4789 | } |
| 4790 | |
| 4791 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 4792 | return -EINVAL; |
| 4793 | |
| 4794 | __update_reg_bounds(dst_reg); |
| 4795 | __reg_deduce_bounds(dst_reg); |
| 4796 | __reg_bound_offset(dst_reg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4797 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4798 | if (sanitize_check_bounds(env, insn, dst_reg) < 0) |
| 4799 | return -EACCES; |
| 4800 | if (sanitize_needed(opcode)) { |
| 4801 | ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg, |
| 4802 | &info, true); |
| 4803 | if (ret < 0) |
| 4804 | return sanitize_err(env, insn, ret, off_reg, dst_reg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4805 | } |
| 4806 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4807 | return 0; |
| 4808 | } |
| 4809 | |
| 4810 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 4811 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 4812 | * need extra checks in the 32-bit case. |
| 4813 | */ |
| 4814 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 4815 | struct bpf_insn *insn, |
| 4816 | struct bpf_reg_state *dst_reg, |
| 4817 | struct bpf_reg_state src_reg) |
| 4818 | { |
| 4819 | struct bpf_reg_state *regs = cur_regs(env); |
| 4820 | u8 opcode = BPF_OP(insn->code); |
| 4821 | bool src_known, dst_known; |
| 4822 | s64 smin_val, smax_val; |
| 4823 | u64 umin_val, umax_val; |
| 4824 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4825 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4826 | |
| 4827 | if (insn_bitness == 32) { |
| 4828 | /* Relevant for 32-bit RSH: Information can propagate towards |
| 4829 | * LSB, so it isn't sufficient to only truncate the output to |
| 4830 | * 32 bits. |
| 4831 | */ |
| 4832 | coerce_reg_to_size(dst_reg, 4); |
| 4833 | coerce_reg_to_size(&src_reg, 4); |
| 4834 | } |
| 4835 | |
| 4836 | smin_val = src_reg.smin_value; |
| 4837 | smax_val = src_reg.smax_value; |
| 4838 | umin_val = src_reg.umin_value; |
| 4839 | umax_val = src_reg.umax_value; |
| 4840 | src_known = tnum_is_const(src_reg.var_off); |
| 4841 | dst_known = tnum_is_const(dst_reg->var_off); |
| 4842 | |
| 4843 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
| 4844 | smin_val > smax_val || umin_val > umax_val) { |
| 4845 | /* Taint dst register if offset had invalid bounds derived from |
| 4846 | * e.g. dead branches. |
| 4847 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4848 | __mark_reg_unknown(env, dst_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4849 | return 0; |
| 4850 | } |
| 4851 | |
| 4852 | if (!src_known && |
| 4853 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4854 | __mark_reg_unknown(env, dst_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4855 | return 0; |
| 4856 | } |
| 4857 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 4858 | if (sanitize_needed(opcode)) { |
| 4859 | ret = sanitize_val_alu(env, insn); |
| 4860 | if (ret < 0) |
| 4861 | return sanitize_err(env, insn, ret, NULL, NULL); |
| 4862 | } |
| 4863 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4864 | switch (opcode) { |
| 4865 | case BPF_ADD: |
| 4866 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 4867 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 4868 | dst_reg->smin_value = S64_MIN; |
| 4869 | dst_reg->smax_value = S64_MAX; |
| 4870 | } else { |
| 4871 | dst_reg->smin_value += smin_val; |
| 4872 | dst_reg->smax_value += smax_val; |
| 4873 | } |
| 4874 | if (dst_reg->umin_value + umin_val < umin_val || |
| 4875 | dst_reg->umax_value + umax_val < umax_val) { |
| 4876 | dst_reg->umin_value = 0; |
| 4877 | dst_reg->umax_value = U64_MAX; |
| 4878 | } else { |
| 4879 | dst_reg->umin_value += umin_val; |
| 4880 | dst_reg->umax_value += umax_val; |
| 4881 | } |
| 4882 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 4883 | break; |
| 4884 | case BPF_SUB: |
| 4885 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 4886 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 4887 | /* Overflow possible, we know nothing */ |
| 4888 | dst_reg->smin_value = S64_MIN; |
| 4889 | dst_reg->smax_value = S64_MAX; |
| 4890 | } else { |
| 4891 | dst_reg->smin_value -= smax_val; |
| 4892 | dst_reg->smax_value -= smin_val; |
| 4893 | } |
| 4894 | if (dst_reg->umin_value < umax_val) { |
| 4895 | /* Overflow possible, we know nothing */ |
| 4896 | dst_reg->umin_value = 0; |
| 4897 | dst_reg->umax_value = U64_MAX; |
| 4898 | } else { |
| 4899 | /* Cannot overflow (as long as bounds are consistent) */ |
| 4900 | dst_reg->umin_value -= umax_val; |
| 4901 | dst_reg->umax_value -= umin_val; |
| 4902 | } |
| 4903 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
| 4904 | break; |
| 4905 | case BPF_MUL: |
| 4906 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 4907 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
| 4908 | /* Ain't nobody got time to multiply that sign */ |
| 4909 | __mark_reg_unbounded(dst_reg); |
| 4910 | __update_reg_bounds(dst_reg); |
| 4911 | break; |
| 4912 | } |
| 4913 | /* Both values are positive, so we can work with unsigned and |
| 4914 | * copy the result to signed (unless it exceeds S64_MAX). |
| 4915 | */ |
| 4916 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 4917 | /* Potential overflow, we know nothing */ |
| 4918 | __mark_reg_unbounded(dst_reg); |
| 4919 | /* (except what we can learn from the var_off) */ |
| 4920 | __update_reg_bounds(dst_reg); |
| 4921 | break; |
| 4922 | } |
| 4923 | dst_reg->umin_value *= umin_val; |
| 4924 | dst_reg->umax_value *= umax_val; |
| 4925 | if (dst_reg->umax_value > S64_MAX) { |
| 4926 | /* Overflow possible, we know nothing */ |
| 4927 | dst_reg->smin_value = S64_MIN; |
| 4928 | dst_reg->smax_value = S64_MAX; |
| 4929 | } else { |
| 4930 | dst_reg->smin_value = dst_reg->umin_value; |
| 4931 | dst_reg->smax_value = dst_reg->umax_value; |
| 4932 | } |
| 4933 | break; |
| 4934 | case BPF_AND: |
| 4935 | if (src_known && dst_known) { |
| 4936 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 4937 | src_reg.var_off.value); |
| 4938 | break; |
| 4939 | } |
| 4940 | /* We get our minimum from the var_off, since that's inherently |
| 4941 | * bitwise. Our maximum is the minimum of the operands' maxima. |
| 4942 | */ |
| 4943 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
| 4944 | dst_reg->umin_value = dst_reg->var_off.value; |
| 4945 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 4946 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 4947 | /* Lose signed bounds when ANDing negative numbers, |
| 4948 | * ain't nobody got time for that. |
| 4949 | */ |
| 4950 | dst_reg->smin_value = S64_MIN; |
| 4951 | dst_reg->smax_value = S64_MAX; |
| 4952 | } else { |
| 4953 | /* ANDing two positives gives a positive, so safe to |
| 4954 | * cast result into s64. |
| 4955 | */ |
| 4956 | dst_reg->smin_value = dst_reg->umin_value; |
| 4957 | dst_reg->smax_value = dst_reg->umax_value; |
| 4958 | } |
| 4959 | /* We may learn something more from the var_off */ |
| 4960 | __update_reg_bounds(dst_reg); |
| 4961 | break; |
| 4962 | case BPF_OR: |
| 4963 | if (src_known && dst_known) { |
| 4964 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
| 4965 | src_reg.var_off.value); |
| 4966 | break; |
| 4967 | } |
| 4968 | /* We get our maximum from the var_off, and our minimum is the |
| 4969 | * maximum of the operands' minima |
| 4970 | */ |
| 4971 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
| 4972 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 4973 | dst_reg->umax_value = dst_reg->var_off.value | |
| 4974 | dst_reg->var_off.mask; |
| 4975 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 4976 | /* Lose signed bounds when ORing negative numbers, |
| 4977 | * ain't nobody got time for that. |
| 4978 | */ |
| 4979 | dst_reg->smin_value = S64_MIN; |
| 4980 | dst_reg->smax_value = S64_MAX; |
| 4981 | } else { |
| 4982 | /* ORing two positives gives a positive, so safe to |
| 4983 | * cast result into s64. |
| 4984 | */ |
| 4985 | dst_reg->smin_value = dst_reg->umin_value; |
| 4986 | dst_reg->smax_value = dst_reg->umax_value; |
| 4987 | } |
| 4988 | /* We may learn something more from the var_off */ |
| 4989 | __update_reg_bounds(dst_reg); |
| 4990 | break; |
| 4991 | case BPF_LSH: |
| 4992 | if (umax_val >= insn_bitness) { |
| 4993 | /* Shifts greater than 31 or 63 are undefined. |
| 4994 | * This includes shifts by a negative number. |
| 4995 | */ |
| 4996 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 4997 | break; |
| 4998 | } |
| 4999 | /* We lose all sign bit information (except what we can pick |
| 5000 | * up from var_off) |
| 5001 | */ |
| 5002 | dst_reg->smin_value = S64_MIN; |
| 5003 | dst_reg->smax_value = S64_MAX; |
| 5004 | /* If we might shift our top bit out, then we know nothing */ |
| 5005 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 5006 | dst_reg->umin_value = 0; |
| 5007 | dst_reg->umax_value = U64_MAX; |
| 5008 | } else { |
| 5009 | dst_reg->umin_value <<= umin_val; |
| 5010 | dst_reg->umax_value <<= umax_val; |
| 5011 | } |
| 5012 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
| 5013 | /* We may learn something more from the var_off */ |
| 5014 | __update_reg_bounds(dst_reg); |
| 5015 | break; |
| 5016 | case BPF_RSH: |
| 5017 | if (umax_val >= insn_bitness) { |
| 5018 | /* Shifts greater than 31 or 63 are undefined. |
| 5019 | * This includes shifts by a negative number. |
| 5020 | */ |
| 5021 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 5022 | break; |
| 5023 | } |
| 5024 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 5025 | * be negative, then either: |
| 5026 | * 1) src_reg might be zero, so the sign bit of the result is |
| 5027 | * unknown, so we lose our signed bounds |
| 5028 | * 2) it's known negative, thus the unsigned bounds capture the |
| 5029 | * signed bounds |
| 5030 | * 3) the signed bounds cross zero, so they tell us nothing |
| 5031 | * about the result |
| 5032 | * If the value in dst_reg is known nonnegative, then again the |
| 5033 | * unsigned bounts capture the signed bounds. |
| 5034 | * Thus, in all cases it suffices to blow away our signed bounds |
| 5035 | * and rely on inferring new ones from the unsigned bounds and |
| 5036 | * var_off of the result. |
| 5037 | */ |
| 5038 | dst_reg->smin_value = S64_MIN; |
| 5039 | dst_reg->smax_value = S64_MAX; |
| 5040 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
| 5041 | dst_reg->umin_value >>= umax_val; |
| 5042 | dst_reg->umax_value >>= umin_val; |
| 5043 | /* We may learn something more from the var_off */ |
| 5044 | __update_reg_bounds(dst_reg); |
| 5045 | break; |
| 5046 | case BPF_ARSH: |
| 5047 | if (umax_val >= insn_bitness) { |
| 5048 | /* Shifts greater than 31 or 63 are undefined. |
| 5049 | * This includes shifts by a negative number. |
| 5050 | */ |
| 5051 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 5052 | break; |
| 5053 | } |
| 5054 | |
| 5055 | /* Upon reaching here, src_known is true and |
| 5056 | * umax_val is equal to umin_val. |
| 5057 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 5058 | if (insn_bitness == 32) { |
| 5059 | dst_reg->smin_value = (u32)(((s32)dst_reg->smin_value) >> umin_val); |
| 5060 | dst_reg->smax_value = (u32)(((s32)dst_reg->smax_value) >> umin_val); |
| 5061 | } else { |
| 5062 | dst_reg->smin_value >>= umin_val; |
| 5063 | dst_reg->smax_value >>= umin_val; |
| 5064 | } |
| 5065 | |
| 5066 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, |
| 5067 | insn_bitness); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5068 | |
| 5069 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 5070 | * dst_reg var_off to refine the result. |
| 5071 | */ |
| 5072 | dst_reg->umin_value = 0; |
| 5073 | dst_reg->umax_value = U64_MAX; |
| 5074 | __update_reg_bounds(dst_reg); |
| 5075 | break; |
| 5076 | default: |
| 5077 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 5078 | break; |
| 5079 | } |
| 5080 | |
| 5081 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 5082 | /* 32-bit ALU ops are (32,32)->32 */ |
| 5083 | coerce_reg_to_size(dst_reg, 4); |
| 5084 | } |
| 5085 | |
| 5086 | __reg_deduce_bounds(dst_reg); |
| 5087 | __reg_bound_offset(dst_reg); |
| 5088 | return 0; |
| 5089 | } |
| 5090 | |
| 5091 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 5092 | * and var_off. |
| 5093 | */ |
| 5094 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 5095 | struct bpf_insn *insn) |
| 5096 | { |
| 5097 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5098 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 5099 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
| 5100 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 5101 | u8 opcode = BPF_OP(insn->code); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5102 | int err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5103 | |
| 5104 | dst_reg = ®s[insn->dst_reg]; |
| 5105 | src_reg = NULL; |
| 5106 | if (dst_reg->type != SCALAR_VALUE) |
| 5107 | ptr_reg = dst_reg; |
| 5108 | if (BPF_SRC(insn->code) == BPF_X) { |
| 5109 | src_reg = ®s[insn->src_reg]; |
| 5110 | if (src_reg->type != SCALAR_VALUE) { |
| 5111 | if (dst_reg->type != SCALAR_VALUE) { |
| 5112 | /* Combining two pointers by any ALU op yields |
| 5113 | * an arbitrary scalar. Disallow all math except |
| 5114 | * pointer subtraction |
| 5115 | */ |
| 5116 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
| 5117 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 5118 | return 0; |
| 5119 | } |
| 5120 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 5121 | insn->dst_reg, |
| 5122 | bpf_alu_string[opcode >> 4]); |
| 5123 | return -EACCES; |
| 5124 | } else { |
| 5125 | /* scalar += pointer |
| 5126 | * This is legal, but we have to reverse our |
| 5127 | * src/dest handling in computing the range |
| 5128 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5129 | err = mark_chain_precision(env, insn->dst_reg); |
| 5130 | if (err) |
| 5131 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5132 | return adjust_ptr_min_max_vals(env, insn, |
| 5133 | src_reg, dst_reg); |
| 5134 | } |
| 5135 | } else if (ptr_reg) { |
| 5136 | /* pointer += scalar */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5137 | err = mark_chain_precision(env, insn->src_reg); |
| 5138 | if (err) |
| 5139 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5140 | return adjust_ptr_min_max_vals(env, insn, |
| 5141 | dst_reg, src_reg); |
| 5142 | } |
| 5143 | } else { |
| 5144 | /* Pretend the src is a reg with a known value, since we only |
| 5145 | * need to be able to read from this state. |
| 5146 | */ |
| 5147 | off_reg.type = SCALAR_VALUE; |
| 5148 | __mark_reg_known(&off_reg, insn->imm); |
| 5149 | src_reg = &off_reg; |
| 5150 | if (ptr_reg) /* pointer += K */ |
| 5151 | return adjust_ptr_min_max_vals(env, insn, |
| 5152 | ptr_reg, src_reg); |
| 5153 | } |
| 5154 | |
| 5155 | /* Got here implies adding two SCALAR_VALUEs */ |
| 5156 | if (WARN_ON_ONCE(ptr_reg)) { |
| 5157 | print_verifier_state(env, state); |
| 5158 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
| 5159 | return -EINVAL; |
| 5160 | } |
| 5161 | if (WARN_ON(!src_reg)) { |
| 5162 | print_verifier_state(env, state); |
| 5163 | verbose(env, "verifier internal error: no src_reg\n"); |
| 5164 | return -EINVAL; |
| 5165 | } |
| 5166 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
| 5167 | } |
| 5168 | |
| 5169 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
| 5170 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 5171 | { |
| 5172 | struct bpf_reg_state *regs = cur_regs(env); |
| 5173 | u8 opcode = BPF_OP(insn->code); |
| 5174 | int err; |
| 5175 | |
| 5176 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 5177 | if (opcode == BPF_NEG) { |
| 5178 | if (BPF_SRC(insn->code) != 0 || |
| 5179 | insn->src_reg != BPF_REG_0 || |
| 5180 | insn->off != 0 || insn->imm != 0) { |
| 5181 | verbose(env, "BPF_NEG uses reserved fields\n"); |
| 5182 | return -EINVAL; |
| 5183 | } |
| 5184 | } else { |
| 5185 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
| 5186 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 5187 | BPF_CLASS(insn->code) == BPF_ALU64) { |
| 5188 | verbose(env, "BPF_END uses reserved fields\n"); |
| 5189 | return -EINVAL; |
| 5190 | } |
| 5191 | } |
| 5192 | |
| 5193 | /* check src operand */ |
| 5194 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
| 5195 | if (err) |
| 5196 | return err; |
| 5197 | |
| 5198 | if (is_pointer_value(env, insn->dst_reg)) { |
| 5199 | verbose(env, "R%d pointer arithmetic prohibited\n", |
| 5200 | insn->dst_reg); |
| 5201 | return -EACCES; |
| 5202 | } |
| 5203 | |
| 5204 | /* check dest operand */ |
| 5205 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
| 5206 | if (err) |
| 5207 | return err; |
| 5208 | |
| 5209 | } else if (opcode == BPF_MOV) { |
| 5210 | |
| 5211 | if (BPF_SRC(insn->code) == BPF_X) { |
| 5212 | if (insn->imm != 0 || insn->off != 0) { |
| 5213 | verbose(env, "BPF_MOV uses reserved fields\n"); |
| 5214 | return -EINVAL; |
| 5215 | } |
| 5216 | |
| 5217 | /* check src operand */ |
| 5218 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 5219 | if (err) |
| 5220 | return err; |
| 5221 | } else { |
| 5222 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 5223 | verbose(env, "BPF_MOV uses reserved fields\n"); |
| 5224 | return -EINVAL; |
| 5225 | } |
| 5226 | } |
| 5227 | |
| 5228 | /* check dest operand, mark as required later */ |
| 5229 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
| 5230 | if (err) |
| 5231 | return err; |
| 5232 | |
| 5233 | if (BPF_SRC(insn->code) == BPF_X) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5234 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
| 5235 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; |
| 5236 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5237 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 5238 | /* case: R1 = R2 |
| 5239 | * copy register state to dest reg |
| 5240 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5241 | *dst_reg = *src_reg; |
| 5242 | dst_reg->live |= REG_LIVE_WRITTEN; |
| 5243 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5244 | } else { |
| 5245 | /* R1 = (u32) R2 */ |
| 5246 | if (is_pointer_value(env, insn->src_reg)) { |
| 5247 | verbose(env, |
| 5248 | "R%d partial copy of pointer\n", |
| 5249 | insn->src_reg); |
| 5250 | return -EACCES; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5251 | } else if (src_reg->type == SCALAR_VALUE) { |
| 5252 | *dst_reg = *src_reg; |
| 5253 | dst_reg->live |= REG_LIVE_WRITTEN; |
| 5254 | dst_reg->subreg_def = env->insn_idx + 1; |
| 5255 | } else { |
| 5256 | mark_reg_unknown(env, regs, |
| 5257 | insn->dst_reg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5258 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5259 | coerce_reg_to_size(dst_reg, 4); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5260 | } |
| 5261 | } else { |
| 5262 | /* case: R = imm |
| 5263 | * remember the value we stored into this reg |
| 5264 | */ |
| 5265 | /* clear any state __mark_reg_known doesn't set */ |
| 5266 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 5267 | regs[insn->dst_reg].type = SCALAR_VALUE; |
| 5268 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 5269 | __mark_reg_known(regs + insn->dst_reg, |
| 5270 | insn->imm); |
| 5271 | } else { |
| 5272 | __mark_reg_known(regs + insn->dst_reg, |
| 5273 | (u32)insn->imm); |
| 5274 | } |
| 5275 | } |
| 5276 | |
| 5277 | } else if (opcode > BPF_END) { |
| 5278 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
| 5279 | return -EINVAL; |
| 5280 | |
| 5281 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 5282 | |
| 5283 | if (BPF_SRC(insn->code) == BPF_X) { |
| 5284 | if (insn->imm != 0 || insn->off != 0) { |
| 5285 | verbose(env, "BPF_ALU uses reserved fields\n"); |
| 5286 | return -EINVAL; |
| 5287 | } |
| 5288 | /* check src1 operand */ |
| 5289 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 5290 | if (err) |
| 5291 | return err; |
| 5292 | } else { |
| 5293 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 5294 | verbose(env, "BPF_ALU uses reserved fields\n"); |
| 5295 | return -EINVAL; |
| 5296 | } |
| 5297 | } |
| 5298 | |
| 5299 | /* check src2 operand */ |
| 5300 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
| 5301 | if (err) |
| 5302 | return err; |
| 5303 | |
| 5304 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 5305 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
| 5306 | verbose(env, "div by zero\n"); |
| 5307 | return -EINVAL; |
| 5308 | } |
| 5309 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5310 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 5311 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 5312 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 5313 | |
| 5314 | if (insn->imm < 0 || insn->imm >= size) { |
| 5315 | verbose(env, "invalid shift %d\n", insn->imm); |
| 5316 | return -EINVAL; |
| 5317 | } |
| 5318 | } |
| 5319 | |
| 5320 | /* check dest operand */ |
| 5321 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
| 5322 | if (err) |
| 5323 | return err; |
| 5324 | |
| 5325 | return adjust_reg_min_max_vals(env, insn); |
| 5326 | } |
| 5327 | |
| 5328 | return 0; |
| 5329 | } |
| 5330 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5331 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
| 5332 | struct bpf_reg_state *dst_reg, |
| 5333 | enum bpf_reg_type type, u16 new_range) |
| 5334 | { |
| 5335 | struct bpf_reg_state *reg; |
| 5336 | int i; |
| 5337 | |
| 5338 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 5339 | reg = &state->regs[i]; |
| 5340 | if (reg->type == type && reg->id == dst_reg->id) |
| 5341 | /* keep the maximum range already checked */ |
| 5342 | reg->range = max(reg->range, new_range); |
| 5343 | } |
| 5344 | |
| 5345 | bpf_for_each_spilled_reg(i, state, reg) { |
| 5346 | if (!reg) |
| 5347 | continue; |
| 5348 | if (reg->type == type && reg->id == dst_reg->id) |
| 5349 | reg->range = max(reg->range, new_range); |
| 5350 | } |
| 5351 | } |
| 5352 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5353 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
| 5354 | struct bpf_reg_state *dst_reg, |
| 5355 | enum bpf_reg_type type, |
| 5356 | bool range_right_open) |
| 5357 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5358 | u16 new_range; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5359 | int i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5360 | |
| 5361 | if (dst_reg->off < 0 || |
| 5362 | (dst_reg->off == 0 && range_right_open)) |
| 5363 | /* This doesn't give us any range */ |
| 5364 | return; |
| 5365 | |
| 5366 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 5367 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
| 5368 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 5369 | * than pkt_end, but that's because it's also less than pkt. |
| 5370 | */ |
| 5371 | return; |
| 5372 | |
| 5373 | new_range = dst_reg->off; |
| 5374 | if (range_right_open) |
| 5375 | new_range--; |
| 5376 | |
| 5377 | /* Examples for register markings: |
| 5378 | * |
| 5379 | * pkt_data in dst register: |
| 5380 | * |
| 5381 | * r2 = r3; |
| 5382 | * r2 += 8; |
| 5383 | * if (r2 > pkt_end) goto <handle exception> |
| 5384 | * <access okay> |
| 5385 | * |
| 5386 | * r2 = r3; |
| 5387 | * r2 += 8; |
| 5388 | * if (r2 < pkt_end) goto <access okay> |
| 5389 | * <handle exception> |
| 5390 | * |
| 5391 | * Where: |
| 5392 | * r2 == dst_reg, pkt_end == src_reg |
| 5393 | * r2=pkt(id=n,off=8,r=0) |
| 5394 | * r3=pkt(id=n,off=0,r=0) |
| 5395 | * |
| 5396 | * pkt_data in src register: |
| 5397 | * |
| 5398 | * r2 = r3; |
| 5399 | * r2 += 8; |
| 5400 | * if (pkt_end >= r2) goto <access okay> |
| 5401 | * <handle exception> |
| 5402 | * |
| 5403 | * r2 = r3; |
| 5404 | * r2 += 8; |
| 5405 | * if (pkt_end <= r2) goto <handle exception> |
| 5406 | * <access okay> |
| 5407 | * |
| 5408 | * Where: |
| 5409 | * pkt_end == dst_reg, r2 == src_reg |
| 5410 | * r2=pkt(id=n,off=8,r=0) |
| 5411 | * r3=pkt(id=n,off=0,r=0) |
| 5412 | * |
| 5413 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) |
| 5414 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 5415 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 5416 | * the check. |
| 5417 | */ |
| 5418 | |
| 5419 | /* If our ids match, then we must have the same max_value. And we |
| 5420 | * don't care about the other reg's fixed offset, since if it's too big |
| 5421 | * the range won't allow anything. |
| 5422 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 5423 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5424 | for (i = 0; i <= vstate->curframe; i++) |
| 5425 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, |
| 5426 | new_range); |
| 5427 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5428 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5429 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
| 5430 | * and return: |
| 5431 | * 1 - branch will be taken and "goto target" will be executed |
| 5432 | * 0 - branch will not be taken and fall-through to next insn |
| 5433 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10] |
| 5434 | */ |
| 5435 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
| 5436 | bool is_jmp32) |
| 5437 | { |
| 5438 | struct bpf_reg_state reg_lo; |
| 5439 | s64 sval; |
| 5440 | |
| 5441 | if (__is_pointer_value(false, reg)) |
| 5442 | return -1; |
| 5443 | |
| 5444 | if (is_jmp32) { |
| 5445 | reg_lo = *reg; |
| 5446 | reg = ®_lo; |
| 5447 | /* For JMP32, only low 32 bits are compared, coerce_reg_to_size |
| 5448 | * could truncate high bits and update umin/umax according to |
| 5449 | * information of low bits. |
| 5450 | */ |
| 5451 | coerce_reg_to_size(reg, 4); |
| 5452 | /* smin/smax need special handling. For example, after coerce, |
| 5453 | * if smin_value is 0x00000000ffffffffLL, the value is -1 when |
| 5454 | * used as operand to JMP32. It is a negative number from s32's |
| 5455 | * point of view, while it is a positive number when seen as |
| 5456 | * s64. The smin/smax are kept as s64, therefore, when used with |
| 5457 | * JMP32, they need to be transformed into s32, then sign |
| 5458 | * extended back to s64. |
| 5459 | * |
| 5460 | * Also, smin/smax were copied from umin/umax. If umin/umax has |
| 5461 | * different sign bit, then min/max relationship doesn't |
| 5462 | * maintain after casting into s32, for this case, set smin/smax |
| 5463 | * to safest range. |
| 5464 | */ |
| 5465 | if ((reg->umax_value ^ reg->umin_value) & |
| 5466 | (1ULL << 31)) { |
| 5467 | reg->smin_value = S32_MIN; |
| 5468 | reg->smax_value = S32_MAX; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5469 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5470 | reg->smin_value = (s64)(s32)reg->smin_value; |
| 5471 | reg->smax_value = (s64)(s32)reg->smax_value; |
| 5472 | |
| 5473 | val = (u32)val; |
| 5474 | sval = (s64)(s32)val; |
| 5475 | } else { |
| 5476 | sval = (s64)val; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5477 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5478 | |
| 5479 | switch (opcode) { |
| 5480 | case BPF_JEQ: |
| 5481 | if (tnum_is_const(reg->var_off)) |
| 5482 | return !!tnum_equals_const(reg->var_off, val); |
| 5483 | break; |
| 5484 | case BPF_JNE: |
| 5485 | if (tnum_is_const(reg->var_off)) |
| 5486 | return !tnum_equals_const(reg->var_off, val); |
| 5487 | break; |
| 5488 | case BPF_JSET: |
| 5489 | if ((~reg->var_off.mask & reg->var_off.value) & val) |
| 5490 | return 1; |
| 5491 | if (!((reg->var_off.mask | reg->var_off.value) & val)) |
| 5492 | return 0; |
| 5493 | break; |
| 5494 | case BPF_JGT: |
| 5495 | if (reg->umin_value > val) |
| 5496 | return 1; |
| 5497 | else if (reg->umax_value <= val) |
| 5498 | return 0; |
| 5499 | break; |
| 5500 | case BPF_JSGT: |
| 5501 | if (reg->smin_value > sval) |
| 5502 | return 1; |
| 5503 | else if (reg->smax_value < sval) |
| 5504 | return 0; |
| 5505 | break; |
| 5506 | case BPF_JLT: |
| 5507 | if (reg->umax_value < val) |
| 5508 | return 1; |
| 5509 | else if (reg->umin_value >= val) |
| 5510 | return 0; |
| 5511 | break; |
| 5512 | case BPF_JSLT: |
| 5513 | if (reg->smax_value < sval) |
| 5514 | return 1; |
| 5515 | else if (reg->smin_value >= sval) |
| 5516 | return 0; |
| 5517 | break; |
| 5518 | case BPF_JGE: |
| 5519 | if (reg->umin_value >= val) |
| 5520 | return 1; |
| 5521 | else if (reg->umax_value < val) |
| 5522 | return 0; |
| 5523 | break; |
| 5524 | case BPF_JSGE: |
| 5525 | if (reg->smin_value >= sval) |
| 5526 | return 1; |
| 5527 | else if (reg->smax_value < sval) |
| 5528 | return 0; |
| 5529 | break; |
| 5530 | case BPF_JLE: |
| 5531 | if (reg->umax_value <= val) |
| 5532 | return 1; |
| 5533 | else if (reg->umin_value > val) |
| 5534 | return 0; |
| 5535 | break; |
| 5536 | case BPF_JSLE: |
| 5537 | if (reg->smax_value <= sval) |
| 5538 | return 1; |
| 5539 | else if (reg->smin_value > sval) |
| 5540 | return 0; |
| 5541 | break; |
| 5542 | } |
| 5543 | |
| 5544 | return -1; |
| 5545 | } |
| 5546 | |
| 5547 | /* Generate min value of the high 32-bit from TNUM info. */ |
| 5548 | static u64 gen_hi_min(struct tnum var) |
| 5549 | { |
| 5550 | return var.value & ~0xffffffffULL; |
| 5551 | } |
| 5552 | |
| 5553 | /* Generate max value of the high 32-bit from TNUM info. */ |
| 5554 | static u64 gen_hi_max(struct tnum var) |
| 5555 | { |
| 5556 | return (var.value | var.mask) & ~0xffffffffULL; |
| 5557 | } |
| 5558 | |
| 5559 | /* Return true if VAL is compared with a s64 sign extended from s32, and they |
| 5560 | * are with the same signedness. |
| 5561 | */ |
| 5562 | static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg) |
| 5563 | { |
| 5564 | return ((s32)sval >= 0 && |
| 5565 | reg->smin_value >= 0 && reg->smax_value <= S32_MAX) || |
| 5566 | ((s32)sval < 0 && |
| 5567 | reg->smax_value <= 0 && reg->smin_value >= S32_MIN); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5568 | } |
| 5569 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 5570 | /* Constrain the possible values of @reg with unsigned upper bound @bound. |
| 5571 | * If @is_exclusive, @bound is an exclusive limit, otherwise it is inclusive. |
| 5572 | * If @is_jmp32, @bound is a 32-bit value that only constrains the low 32 bits |
| 5573 | * of @reg. |
| 5574 | */ |
| 5575 | static void set_upper_bound(struct bpf_reg_state *reg, u64 bound, bool is_jmp32, |
| 5576 | bool is_exclusive) |
| 5577 | { |
| 5578 | if (is_exclusive) { |
| 5579 | /* There are no values for `reg` that make `reg<0` true. */ |
| 5580 | if (bound == 0) |
| 5581 | return; |
| 5582 | bound--; |
| 5583 | } |
| 5584 | if (is_jmp32) { |
| 5585 | /* Constrain the register's value in the tnum representation. |
| 5586 | * For 64-bit comparisons this happens later in |
| 5587 | * __reg_bound_offset(), but for 32-bit comparisons, we can be |
| 5588 | * more precise than what can be derived from the updated |
| 5589 | * numeric bounds. |
| 5590 | */ |
| 5591 | struct tnum t = tnum_range(0, bound); |
| 5592 | |
| 5593 | t.mask |= ~0xffffffffULL; /* upper half is unknown */ |
| 5594 | reg->var_off = tnum_intersect(reg->var_off, t); |
| 5595 | |
| 5596 | /* Compute the 64-bit bound from the 32-bit bound. */ |
| 5597 | bound += gen_hi_max(reg->var_off); |
| 5598 | } |
| 5599 | reg->umax_value = min(reg->umax_value, bound); |
| 5600 | } |
| 5601 | |
| 5602 | /* Constrain the possible values of @reg with unsigned lower bound @bound. |
| 5603 | * If @is_exclusive, @bound is an exclusive limit, otherwise it is inclusive. |
| 5604 | * If @is_jmp32, @bound is a 32-bit value that only constrains the low 32 bits |
| 5605 | * of @reg. |
| 5606 | */ |
| 5607 | static void set_lower_bound(struct bpf_reg_state *reg, u64 bound, bool is_jmp32, |
| 5608 | bool is_exclusive) |
| 5609 | { |
| 5610 | if (is_exclusive) { |
| 5611 | /* There are no values for `reg` that make `reg>MAX` true. */ |
| 5612 | if (bound == (is_jmp32 ? U32_MAX : U64_MAX)) |
| 5613 | return; |
| 5614 | bound++; |
| 5615 | } |
| 5616 | if (is_jmp32) { |
| 5617 | /* Constrain the register's value in the tnum representation. |
| 5618 | * For 64-bit comparisons this happens later in |
| 5619 | * __reg_bound_offset(), but for 32-bit comparisons, we can be |
| 5620 | * more precise than what can be derived from the updated |
| 5621 | * numeric bounds. |
| 5622 | */ |
| 5623 | struct tnum t = tnum_range(bound, U32_MAX); |
| 5624 | |
| 5625 | t.mask |= ~0xffffffffULL; /* upper half is unknown */ |
| 5626 | reg->var_off = tnum_intersect(reg->var_off, t); |
| 5627 | |
| 5628 | /* Compute the 64-bit bound from the 32-bit bound. */ |
| 5629 | bound += gen_hi_min(reg->var_off); |
| 5630 | } |
| 5631 | reg->umin_value = max(reg->umin_value, bound); |
| 5632 | } |
| 5633 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5634 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 5635 | * variable register that we are working on, and src_reg is a constant or we're |
| 5636 | * simply doing a BPF_K check. |
| 5637 | * In JEQ/JNE cases we also adjust the var_off values. |
| 5638 | */ |
| 5639 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 5640 | struct bpf_reg_state *false_reg, u64 val, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5641 | u8 opcode, bool is_jmp32) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5642 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5643 | s64 sval; |
| 5644 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5645 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 5646 | * variable offset from the compare (unless src_reg were a pointer into |
| 5647 | * the same object, but we don't bother with that. |
| 5648 | * Since false_reg and true_reg have the same type by construction, we |
| 5649 | * only need to check one of them for pointerness. |
| 5650 | */ |
| 5651 | if (__is_pointer_value(false, false_reg)) |
| 5652 | return; |
| 5653 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5654 | val = is_jmp32 ? (u32)val : val; |
| 5655 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; |
| 5656 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5657 | switch (opcode) { |
| 5658 | case BPF_JEQ: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5659 | case BPF_JNE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5660 | { |
| 5661 | struct bpf_reg_state *reg = |
| 5662 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 5663 | |
| 5664 | /* For BPF_JEQ, if this is false we know nothing Jon Snow, but |
| 5665 | * if it is true we know the value for sure. Likewise for |
| 5666 | * BPF_JNE. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5667 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5668 | if (is_jmp32) { |
| 5669 | u64 old_v = reg->var_off.value; |
| 5670 | u64 hi_mask = ~0xffffffffULL; |
| 5671 | |
| 5672 | reg->var_off.value = (old_v & hi_mask) | val; |
| 5673 | reg->var_off.mask &= hi_mask; |
| 5674 | } else { |
| 5675 | __mark_reg_known(reg, val); |
| 5676 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5677 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5678 | } |
| 5679 | case BPF_JSET: |
| 5680 | false_reg->var_off = tnum_and(false_reg->var_off, |
| 5681 | tnum_const(~val)); |
| 5682 | if (is_power_of_2(val)) |
| 5683 | true_reg->var_off = tnum_or(true_reg->var_off, |
| 5684 | tnum_const(val)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5685 | break; |
| 5686 | case BPF_JGE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5687 | case BPF_JGT: |
| 5688 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 5689 | set_upper_bound(false_reg, val, is_jmp32, opcode == BPF_JGE); |
| 5690 | set_lower_bound(true_reg, val, is_jmp32, opcode == BPF_JGT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5691 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5692 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5693 | case BPF_JSGE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5694 | case BPF_JSGT: |
| 5695 | { |
| 5696 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; |
| 5697 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; |
| 5698 | |
| 5699 | /* If the full s64 was not sign-extended from s32 then don't |
| 5700 | * deduct further info. |
| 5701 | */ |
| 5702 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 5703 | break; |
| 5704 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 5705 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5706 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5707 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5708 | case BPF_JLE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5709 | case BPF_JLT: |
| 5710 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 5711 | set_lower_bound(false_reg, val, is_jmp32, opcode == BPF_JLE); |
| 5712 | set_upper_bound(true_reg, val, is_jmp32, opcode == BPF_JLT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5713 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5714 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5715 | case BPF_JSLE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5716 | case BPF_JSLT: |
| 5717 | { |
| 5718 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; |
| 5719 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; |
| 5720 | |
| 5721 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 5722 | break; |
| 5723 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 5724 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5725 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5726 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5727 | default: |
| 5728 | break; |
| 5729 | } |
| 5730 | |
| 5731 | __reg_deduce_bounds(false_reg); |
| 5732 | __reg_deduce_bounds(true_reg); |
| 5733 | /* We might have learned some bits from the bounds. */ |
| 5734 | __reg_bound_offset(false_reg); |
| 5735 | __reg_bound_offset(true_reg); |
| 5736 | /* Intersecting with the old var_off might have improved our bounds |
| 5737 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 5738 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 5739 | */ |
| 5740 | __update_reg_bounds(false_reg); |
| 5741 | __update_reg_bounds(true_reg); |
| 5742 | } |
| 5743 | |
| 5744 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 5745 | * the variable reg. |
| 5746 | */ |
| 5747 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 5748 | struct bpf_reg_state *false_reg, u64 val, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5749 | u8 opcode, bool is_jmp32) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5750 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5751 | s64 sval; |
| 5752 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5753 | if (__is_pointer_value(false, false_reg)) |
| 5754 | return; |
| 5755 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5756 | val = is_jmp32 ? (u32)val : val; |
| 5757 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; |
| 5758 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5759 | switch (opcode) { |
| 5760 | case BPF_JEQ: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5761 | case BPF_JNE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5762 | { |
| 5763 | struct bpf_reg_state *reg = |
| 5764 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 5765 | |
| 5766 | if (is_jmp32) { |
| 5767 | u64 old_v = reg->var_off.value; |
| 5768 | u64 hi_mask = ~0xffffffffULL; |
| 5769 | |
| 5770 | reg->var_off.value = (old_v & hi_mask) | val; |
| 5771 | reg->var_off.mask &= hi_mask; |
| 5772 | } else { |
| 5773 | __mark_reg_known(reg, val); |
| 5774 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5775 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5776 | } |
| 5777 | case BPF_JSET: |
| 5778 | false_reg->var_off = tnum_and(false_reg->var_off, |
| 5779 | tnum_const(~val)); |
| 5780 | if (is_power_of_2(val)) |
| 5781 | true_reg->var_off = tnum_or(true_reg->var_off, |
| 5782 | tnum_const(val)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5783 | break; |
| 5784 | case BPF_JGE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5785 | case BPF_JGT: |
| 5786 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 5787 | set_lower_bound(false_reg, val, is_jmp32, opcode == BPF_JGE); |
| 5788 | set_upper_bound(true_reg, val, is_jmp32, opcode == BPF_JGT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5789 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5790 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5791 | case BPF_JSGE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5792 | case BPF_JSGT: |
| 5793 | { |
| 5794 | s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1; |
| 5795 | s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval; |
| 5796 | |
| 5797 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 5798 | break; |
| 5799 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 5800 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5801 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5802 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5803 | case BPF_JLE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5804 | case BPF_JLT: |
| 5805 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 5806 | set_upper_bound(false_reg, val, is_jmp32, opcode == BPF_JLE); |
| 5807 | set_lower_bound(true_reg, val, is_jmp32, opcode == BPF_JLT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5808 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5809 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5810 | case BPF_JSLE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5811 | case BPF_JSLT: |
| 5812 | { |
| 5813 | s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1; |
| 5814 | s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval; |
| 5815 | |
| 5816 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 5817 | break; |
| 5818 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 5819 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5820 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5821 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5822 | default: |
| 5823 | break; |
| 5824 | } |
| 5825 | |
| 5826 | __reg_deduce_bounds(false_reg); |
| 5827 | __reg_deduce_bounds(true_reg); |
| 5828 | /* We might have learned some bits from the bounds. */ |
| 5829 | __reg_bound_offset(false_reg); |
| 5830 | __reg_bound_offset(true_reg); |
| 5831 | /* Intersecting with the old var_off might have improved our bounds |
| 5832 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 5833 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 5834 | */ |
| 5835 | __update_reg_bounds(false_reg); |
| 5836 | __update_reg_bounds(true_reg); |
| 5837 | } |
| 5838 | |
| 5839 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 5840 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 5841 | struct bpf_reg_state *dst_reg) |
| 5842 | { |
| 5843 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 5844 | dst_reg->umin_value); |
| 5845 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 5846 | dst_reg->umax_value); |
| 5847 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 5848 | dst_reg->smin_value); |
| 5849 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 5850 | dst_reg->smax_value); |
| 5851 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 5852 | dst_reg->var_off); |
| 5853 | /* We might have learned new bounds from the var_off. */ |
| 5854 | __update_reg_bounds(src_reg); |
| 5855 | __update_reg_bounds(dst_reg); |
| 5856 | /* We might have learned something about the sign bit. */ |
| 5857 | __reg_deduce_bounds(src_reg); |
| 5858 | __reg_deduce_bounds(dst_reg); |
| 5859 | /* We might have learned some bits from the bounds. */ |
| 5860 | __reg_bound_offset(src_reg); |
| 5861 | __reg_bound_offset(dst_reg); |
| 5862 | /* Intersecting with the old var_off might have improved our bounds |
| 5863 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 5864 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 5865 | */ |
| 5866 | __update_reg_bounds(src_reg); |
| 5867 | __update_reg_bounds(dst_reg); |
| 5868 | } |
| 5869 | |
| 5870 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 5871 | struct bpf_reg_state *true_dst, |
| 5872 | struct bpf_reg_state *false_src, |
| 5873 | struct bpf_reg_state *false_dst, |
| 5874 | u8 opcode) |
| 5875 | { |
| 5876 | switch (opcode) { |
| 5877 | case BPF_JEQ: |
| 5878 | __reg_combine_min_max(true_src, true_dst); |
| 5879 | break; |
| 5880 | case BPF_JNE: |
| 5881 | __reg_combine_min_max(false_src, false_dst); |
| 5882 | break; |
| 5883 | } |
| 5884 | } |
| 5885 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5886 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 5887 | struct bpf_reg_state *reg, u32 id, |
| 5888 | bool is_null) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5889 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5890 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5891 | /* Old offset (both fixed and variable parts) should |
| 5892 | * have been known-zero, because we don't allow pointer |
| 5893 | * arithmetic on pointers that might be NULL. |
| 5894 | */ |
| 5895 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 5896 | !tnum_equals_const(reg->var_off, 0) || |
| 5897 | reg->off)) { |
| 5898 | __mark_reg_known_zero(reg); |
| 5899 | reg->off = 0; |
| 5900 | } |
| 5901 | if (is_null) { |
| 5902 | reg->type = SCALAR_VALUE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5903 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
| 5904 | if (reg->map_ptr->inner_map_meta) { |
| 5905 | reg->type = CONST_PTR_TO_MAP; |
| 5906 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
| 5907 | } else if (reg->map_ptr->map_type == |
| 5908 | BPF_MAP_TYPE_XSKMAP) { |
| 5909 | reg->type = PTR_TO_XDP_SOCK; |
| 5910 | } else { |
| 5911 | reg->type = PTR_TO_MAP_VALUE; |
| 5912 | } |
| 5913 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
| 5914 | reg->type = PTR_TO_SOCKET; |
| 5915 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
| 5916 | reg->type = PTR_TO_SOCK_COMMON; |
| 5917 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
| 5918 | reg->type = PTR_TO_TCP_SOCK; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5919 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5920 | if (is_null) { |
| 5921 | /* We don't need id and ref_obj_id from this point |
| 5922 | * onwards anymore, thus we should better reset it, |
| 5923 | * so that state pruning has chances to take effect. |
| 5924 | */ |
| 5925 | reg->id = 0; |
| 5926 | reg->ref_obj_id = 0; |
| 5927 | } else if (!reg_may_point_to_spin_lock(reg)) { |
| 5928 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
| 5929 | * in release_reg_references(). |
| 5930 | * |
| 5931 | * reg->id is still used by spin_lock ptr. Other |
| 5932 | * than spin_lock ptr type, reg->id can be reset. |
| 5933 | */ |
| 5934 | reg->id = 0; |
| 5935 | } |
| 5936 | } |
| 5937 | } |
| 5938 | |
| 5939 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
| 5940 | bool is_null) |
| 5941 | { |
| 5942 | struct bpf_reg_state *reg; |
| 5943 | int i; |
| 5944 | |
| 5945 | for (i = 0; i < MAX_BPF_REG; i++) |
| 5946 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); |
| 5947 | |
| 5948 | bpf_for_each_spilled_reg(i, state, reg) { |
| 5949 | if (!reg) |
| 5950 | continue; |
| 5951 | mark_ptr_or_null_reg(state, reg, id, is_null); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5952 | } |
| 5953 | } |
| 5954 | |
| 5955 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 5956 | * be folded together at some point. |
| 5957 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5958 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 5959 | bool is_null) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5960 | { |
| 5961 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 5962 | struct bpf_reg_state *regs = state->regs; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5963 | u32 ref_obj_id = regs[regno].ref_obj_id; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5964 | u32 id = regs[regno].id; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5965 | int i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5966 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5967 | if (ref_obj_id && ref_obj_id == id && is_null) |
| 5968 | /* regs[regno] is in the " == NULL" branch. |
| 5969 | * No one could have freed the reference state before |
| 5970 | * doing the NULL check. |
| 5971 | */ |
| 5972 | WARN_ON_ONCE(release_reference_state(state, id)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5973 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5974 | for (i = 0; i <= vstate->curframe; i++) |
| 5975 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5976 | } |
| 5977 | |
| 5978 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 5979 | struct bpf_reg_state *dst_reg, |
| 5980 | struct bpf_reg_state *src_reg, |
| 5981 | struct bpf_verifier_state *this_branch, |
| 5982 | struct bpf_verifier_state *other_branch) |
| 5983 | { |
| 5984 | if (BPF_SRC(insn->code) != BPF_X) |
| 5985 | return false; |
| 5986 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5987 | /* Pointers are always 64-bit. */ |
| 5988 | if (BPF_CLASS(insn->code) == BPF_JMP32) |
| 5989 | return false; |
| 5990 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5991 | switch (BPF_OP(insn->code)) { |
| 5992 | case BPF_JGT: |
| 5993 | if ((dst_reg->type == PTR_TO_PACKET && |
| 5994 | src_reg->type == PTR_TO_PACKET_END) || |
| 5995 | (dst_reg->type == PTR_TO_PACKET_META && |
| 5996 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 5997 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 5998 | find_good_pkt_pointers(this_branch, dst_reg, |
| 5999 | dst_reg->type, false); |
| 6000 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 6001 | src_reg->type == PTR_TO_PACKET) || |
| 6002 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 6003 | src_reg->type == PTR_TO_PACKET_META)) { |
| 6004 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 6005 | find_good_pkt_pointers(other_branch, src_reg, |
| 6006 | src_reg->type, true); |
| 6007 | } else { |
| 6008 | return false; |
| 6009 | } |
| 6010 | break; |
| 6011 | case BPF_JLT: |
| 6012 | if ((dst_reg->type == PTR_TO_PACKET && |
| 6013 | src_reg->type == PTR_TO_PACKET_END) || |
| 6014 | (dst_reg->type == PTR_TO_PACKET_META && |
| 6015 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 6016 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 6017 | find_good_pkt_pointers(other_branch, dst_reg, |
| 6018 | dst_reg->type, true); |
| 6019 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 6020 | src_reg->type == PTR_TO_PACKET) || |
| 6021 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 6022 | src_reg->type == PTR_TO_PACKET_META)) { |
| 6023 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 6024 | find_good_pkt_pointers(this_branch, src_reg, |
| 6025 | src_reg->type, false); |
| 6026 | } else { |
| 6027 | return false; |
| 6028 | } |
| 6029 | break; |
| 6030 | case BPF_JGE: |
| 6031 | if ((dst_reg->type == PTR_TO_PACKET && |
| 6032 | src_reg->type == PTR_TO_PACKET_END) || |
| 6033 | (dst_reg->type == PTR_TO_PACKET_META && |
| 6034 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 6035 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 6036 | find_good_pkt_pointers(this_branch, dst_reg, |
| 6037 | dst_reg->type, true); |
| 6038 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 6039 | src_reg->type == PTR_TO_PACKET) || |
| 6040 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 6041 | src_reg->type == PTR_TO_PACKET_META)) { |
| 6042 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 6043 | find_good_pkt_pointers(other_branch, src_reg, |
| 6044 | src_reg->type, false); |
| 6045 | } else { |
| 6046 | return false; |
| 6047 | } |
| 6048 | break; |
| 6049 | case BPF_JLE: |
| 6050 | if ((dst_reg->type == PTR_TO_PACKET && |
| 6051 | src_reg->type == PTR_TO_PACKET_END) || |
| 6052 | (dst_reg->type == PTR_TO_PACKET_META && |
| 6053 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 6054 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 6055 | find_good_pkt_pointers(other_branch, dst_reg, |
| 6056 | dst_reg->type, false); |
| 6057 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 6058 | src_reg->type == PTR_TO_PACKET) || |
| 6059 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 6060 | src_reg->type == PTR_TO_PACKET_META)) { |
| 6061 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 6062 | find_good_pkt_pointers(this_branch, src_reg, |
| 6063 | src_reg->type, true); |
| 6064 | } else { |
| 6065 | return false; |
| 6066 | } |
| 6067 | break; |
| 6068 | default: |
| 6069 | return false; |
| 6070 | } |
| 6071 | |
| 6072 | return true; |
| 6073 | } |
| 6074 | |
| 6075 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
| 6076 | struct bpf_insn *insn, int *insn_idx) |
| 6077 | { |
| 6078 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 6079 | struct bpf_verifier_state *other_branch; |
| 6080 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6081 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6082 | u8 opcode = BPF_OP(insn->code); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6083 | bool is_jmp32; |
| 6084 | int pred = -1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6085 | int err; |
| 6086 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6087 | /* Only conditional jumps are expected to reach here. */ |
| 6088 | if (opcode == BPF_JA || opcode > BPF_JSLE) { |
| 6089 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6090 | return -EINVAL; |
| 6091 | } |
| 6092 | |
| 6093 | if (BPF_SRC(insn->code) == BPF_X) { |
| 6094 | if (insn->imm != 0) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6095 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6096 | return -EINVAL; |
| 6097 | } |
| 6098 | |
| 6099 | /* check src1 operand */ |
| 6100 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 6101 | if (err) |
| 6102 | return err; |
| 6103 | |
| 6104 | if (is_pointer_value(env, insn->src_reg)) { |
| 6105 | verbose(env, "R%d pointer comparison prohibited\n", |
| 6106 | insn->src_reg); |
| 6107 | return -EACCES; |
| 6108 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6109 | src_reg = ®s[insn->src_reg]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6110 | } else { |
| 6111 | if (insn->src_reg != BPF_REG_0) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6112 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6113 | return -EINVAL; |
| 6114 | } |
| 6115 | } |
| 6116 | |
| 6117 | /* check src2 operand */ |
| 6118 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
| 6119 | if (err) |
| 6120 | return err; |
| 6121 | |
| 6122 | dst_reg = ®s[insn->dst_reg]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6123 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6124 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6125 | if (BPF_SRC(insn->code) == BPF_K) |
| 6126 | pred = is_branch_taken(dst_reg, insn->imm, |
| 6127 | opcode, is_jmp32); |
| 6128 | else if (src_reg->type == SCALAR_VALUE && |
| 6129 | tnum_is_const(src_reg->var_off)) |
| 6130 | pred = is_branch_taken(dst_reg, src_reg->var_off.value, |
| 6131 | opcode, is_jmp32); |
| 6132 | if (pred >= 0) { |
| 6133 | err = mark_chain_precision(env, insn->dst_reg); |
| 6134 | if (BPF_SRC(insn->code) == BPF_X && !err) |
| 6135 | err = mark_chain_precision(env, insn->src_reg); |
| 6136 | if (err) |
| 6137 | return err; |
| 6138 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 6139 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6140 | if (pred == 1) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 6141 | /* Only follow the goto, ignore fall-through. If needed, push |
| 6142 | * the fall-through branch for simulation under speculative |
| 6143 | * execution. |
| 6144 | */ |
| 6145 | if (!env->allow_ptr_leaks && |
| 6146 | !sanitize_speculative_path(env, insn, *insn_idx + 1, |
| 6147 | *insn_idx)) |
| 6148 | return -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6149 | *insn_idx += insn->off; |
| 6150 | return 0; |
| 6151 | } else if (pred == 0) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 6152 | /* Only follow the fall-through branch, since that's where the |
| 6153 | * program will go. If needed, push the goto branch for |
| 6154 | * simulation under speculative execution. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6155 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 6156 | if (!env->allow_ptr_leaks && |
| 6157 | !sanitize_speculative_path(env, insn, |
| 6158 | *insn_idx + insn->off + 1, |
| 6159 | *insn_idx)) |
| 6160 | return -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6161 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6162 | } |
| 6163 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6164 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
| 6165 | false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6166 | if (!other_branch) |
| 6167 | return -EFAULT; |
| 6168 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
| 6169 | |
| 6170 | /* detect if we are comparing against a constant value so we can adjust |
| 6171 | * our min/max values for our dst register. |
| 6172 | * this is only legit if both are scalars (or pointers to the same |
| 6173 | * object, I suppose, but we don't support that right now), because |
| 6174 | * otherwise the different base pointers mean the offsets aren't |
| 6175 | * comparable. |
| 6176 | */ |
| 6177 | if (BPF_SRC(insn->code) == BPF_X) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6178 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
| 6179 | struct bpf_reg_state lo_reg0 = *dst_reg; |
| 6180 | struct bpf_reg_state lo_reg1 = *src_reg; |
| 6181 | struct bpf_reg_state *src_lo, *dst_lo; |
| 6182 | |
| 6183 | dst_lo = &lo_reg0; |
| 6184 | src_lo = &lo_reg1; |
| 6185 | coerce_reg_to_size(dst_lo, 4); |
| 6186 | coerce_reg_to_size(src_lo, 4); |
| 6187 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6188 | if (dst_reg->type == SCALAR_VALUE && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6189 | src_reg->type == SCALAR_VALUE) { |
| 6190 | if (tnum_is_const(src_reg->var_off) || |
| 6191 | (is_jmp32 && tnum_is_const(src_lo->var_off))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6192 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6193 | dst_reg, |
| 6194 | is_jmp32 |
| 6195 | ? src_lo->var_off.value |
| 6196 | : src_reg->var_off.value, |
| 6197 | opcode, is_jmp32); |
| 6198 | else if (tnum_is_const(dst_reg->var_off) || |
| 6199 | (is_jmp32 && tnum_is_const(dst_lo->var_off))) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6200 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6201 | src_reg, |
| 6202 | is_jmp32 |
| 6203 | ? dst_lo->var_off.value |
| 6204 | : dst_reg->var_off.value, |
| 6205 | opcode, is_jmp32); |
| 6206 | else if (!is_jmp32 && |
| 6207 | (opcode == BPF_JEQ || opcode == BPF_JNE)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6208 | /* Comparing for equality, we can combine knowledge */ |
| 6209 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 6210 | &other_branch_regs[insn->dst_reg], |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6211 | src_reg, dst_reg, opcode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6212 | } |
| 6213 | } else if (dst_reg->type == SCALAR_VALUE) { |
| 6214 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6215 | dst_reg, insn->imm, opcode, is_jmp32); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6216 | } |
| 6217 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6218 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
| 6219 | * NOTE: these optimizations below are related with pointer comparison |
| 6220 | * which will never be JMP32. |
| 6221 | */ |
| 6222 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6223 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6224 | reg_type_may_be_null(dst_reg->type)) { |
| 6225 | /* Mark all identical registers in each branch as either |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6226 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 6227 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6228 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 6229 | opcode == BPF_JNE); |
| 6230 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 6231 | opcode == BPF_JEQ); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6232 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 6233 | this_branch, other_branch) && |
| 6234 | is_pointer_value(env, insn->dst_reg)) { |
| 6235 | verbose(env, "R%d pointer comparison prohibited\n", |
| 6236 | insn->dst_reg); |
| 6237 | return -EACCES; |
| 6238 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6239 | if (env->log.level & BPF_LOG_LEVEL) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6240 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
| 6241 | return 0; |
| 6242 | } |
| 6243 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6244 | /* verify BPF_LD_IMM64 instruction */ |
| 6245 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 6246 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6247 | struct bpf_insn_aux_data *aux = cur_aux(env); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6248 | struct bpf_reg_state *regs = cur_regs(env); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6249 | struct bpf_map *map; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6250 | int err; |
| 6251 | |
| 6252 | if (BPF_SIZE(insn->code) != BPF_DW) { |
| 6253 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
| 6254 | return -EINVAL; |
| 6255 | } |
| 6256 | if (insn->off != 0) { |
| 6257 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
| 6258 | return -EINVAL; |
| 6259 | } |
| 6260 | |
| 6261 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
| 6262 | if (err) |
| 6263 | return err; |
| 6264 | |
| 6265 | if (insn->src_reg == 0) { |
| 6266 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 6267 | |
| 6268 | regs[insn->dst_reg].type = SCALAR_VALUE; |
| 6269 | __mark_reg_known(®s[insn->dst_reg], imm); |
| 6270 | return 0; |
| 6271 | } |
| 6272 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6273 | map = env->used_maps[aux->map_index]; |
| 6274 | mark_reg_known_zero(env, regs, insn->dst_reg); |
| 6275 | regs[insn->dst_reg].map_ptr = map; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6276 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6277 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { |
| 6278 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; |
| 6279 | regs[insn->dst_reg].off = aux->map_off; |
| 6280 | if (map_value_has_spin_lock(map)) |
| 6281 | regs[insn->dst_reg].id = ++env->id_gen; |
| 6282 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 6283 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 6284 | } else { |
| 6285 | verbose(env, "bpf verifier is misconfigured\n"); |
| 6286 | return -EINVAL; |
| 6287 | } |
| 6288 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6289 | return 0; |
| 6290 | } |
| 6291 | |
| 6292 | static bool may_access_skb(enum bpf_prog_type type) |
| 6293 | { |
| 6294 | switch (type) { |
| 6295 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 6296 | case BPF_PROG_TYPE_SCHED_CLS: |
| 6297 | case BPF_PROG_TYPE_SCHED_ACT: |
| 6298 | return true; |
| 6299 | default: |
| 6300 | return false; |
| 6301 | } |
| 6302 | } |
| 6303 | |
| 6304 | /* verify safety of LD_ABS|LD_IND instructions: |
| 6305 | * - they can only appear in the programs where ctx == skb |
| 6306 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 6307 | * preserve R6-R9, and store return value into R0 |
| 6308 | * |
| 6309 | * Implicit input: |
| 6310 | * ctx == skb == R6 == CTX |
| 6311 | * |
| 6312 | * Explicit input: |
| 6313 | * SRC == any register |
| 6314 | * IMM == 32-bit immediate |
| 6315 | * |
| 6316 | * Output: |
| 6317 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 6318 | */ |
| 6319 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 6320 | { |
| 6321 | struct bpf_reg_state *regs = cur_regs(env); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 6322 | static const int ctx_reg = BPF_REG_6; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6323 | u8 mode = BPF_MODE(insn->code); |
| 6324 | int i, err; |
| 6325 | |
| 6326 | if (!may_access_skb(env->prog->type)) { |
| 6327 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
| 6328 | return -EINVAL; |
| 6329 | } |
| 6330 | |
| 6331 | if (!env->ops->gen_ld_abs) { |
| 6332 | verbose(env, "bpf verifier is misconfigured\n"); |
| 6333 | return -EINVAL; |
| 6334 | } |
| 6335 | |
| 6336 | if (env->subprog_cnt > 1) { |
| 6337 | /* when program has LD_ABS insn JITs and interpreter assume |
| 6338 | * that r1 == ctx == skb which is not the case for callees |
| 6339 | * that can have arbitrary arguments. It's problematic |
| 6340 | * for main prog as well since JITs would need to analyze |
| 6341 | * all functions in order to make proper register save/restore |
| 6342 | * decisions in the main prog. Hence disallow LD_ABS with calls |
| 6343 | */ |
| 6344 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); |
| 6345 | return -EINVAL; |
| 6346 | } |
| 6347 | |
| 6348 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
| 6349 | BPF_SIZE(insn->code) == BPF_DW || |
| 6350 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
| 6351 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
| 6352 | return -EINVAL; |
| 6353 | } |
| 6354 | |
| 6355 | /* check whether implicit source operand (register R6) is readable */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 6356 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6357 | if (err) |
| 6358 | return err; |
| 6359 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6360 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 6361 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 6362 | * reference leak. |
| 6363 | */ |
| 6364 | err = check_reference_leak(env); |
| 6365 | if (err) { |
| 6366 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 6367 | return err; |
| 6368 | } |
| 6369 | |
| 6370 | if (env->cur_state->active_spin_lock) { |
| 6371 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
| 6372 | return -EINVAL; |
| 6373 | } |
| 6374 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 6375 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6376 | verbose(env, |
| 6377 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); |
| 6378 | return -EINVAL; |
| 6379 | } |
| 6380 | |
| 6381 | if (mode == BPF_IND) { |
| 6382 | /* check explicit source operand */ |
| 6383 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 6384 | if (err) |
| 6385 | return err; |
| 6386 | } |
| 6387 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 6388 | err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg); |
| 6389 | if (err < 0) |
| 6390 | return err; |
| 6391 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6392 | /* reset caller saved regs to unreadable */ |
| 6393 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 6394 | mark_reg_not_init(env, regs, caller_saved[i]); |
| 6395 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 6396 | } |
| 6397 | |
| 6398 | /* mark destination R0 register as readable, since it contains |
| 6399 | * the value fetched from the packet. |
| 6400 | * Already marked as written above. |
| 6401 | */ |
| 6402 | mark_reg_unknown(env, regs, BPF_REG_0); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6403 | /* ld_abs load up to 32-bit skb data. */ |
| 6404 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6405 | return 0; |
| 6406 | } |
| 6407 | |
| 6408 | static int check_return_code(struct bpf_verifier_env *env) |
| 6409 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6410 | struct tnum enforce_attach_type_range = tnum_unknown; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6411 | struct bpf_reg_state *reg; |
| 6412 | struct tnum range = tnum_range(0, 1); |
| 6413 | |
| 6414 | switch (env->prog->type) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6415 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6416 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || |
| 6417 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG) |
| 6418 | range = tnum_range(1, 1); |
| 6419 | break; |
| 6420 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 6421 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
| 6422 | range = tnum_range(0, 3); |
| 6423 | enforce_attach_type_range = tnum_range(2, 3); |
| 6424 | } |
| 6425 | break; |
| 6426 | case BPF_PROG_TYPE_CGROUP_SOCK: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6427 | case BPF_PROG_TYPE_SOCK_OPS: |
| 6428 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6429 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
| 6430 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6431 | break; |
| 6432 | default: |
| 6433 | return 0; |
| 6434 | } |
| 6435 | |
| 6436 | reg = cur_regs(env) + BPF_REG_0; |
| 6437 | if (reg->type != SCALAR_VALUE) { |
| 6438 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
| 6439 | reg_type_str[reg->type]); |
| 6440 | return -EINVAL; |
| 6441 | } |
| 6442 | |
| 6443 | if (!tnum_in(range, reg->var_off)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6444 | char tn_buf[48]; |
| 6445 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6446 | verbose(env, "At program exit the register R0 "); |
| 6447 | if (!tnum_is_unknown(reg->var_off)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6448 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 6449 | verbose(env, "has value %s", tn_buf); |
| 6450 | } else { |
| 6451 | verbose(env, "has unknown scalar value"); |
| 6452 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6453 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
| 6454 | verbose(env, " should have been in %s\n", tn_buf); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6455 | return -EINVAL; |
| 6456 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6457 | |
| 6458 | if (!tnum_is_unknown(enforce_attach_type_range) && |
| 6459 | tnum_in(enforce_attach_type_range, reg->var_off)) |
| 6460 | env->prog->enforce_expected_attach_type = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6461 | return 0; |
| 6462 | } |
| 6463 | |
| 6464 | /* non-recursive DFS pseudo code |
| 6465 | * 1 procedure DFS-iterative(G,v): |
| 6466 | * 2 label v as discovered |
| 6467 | * 3 let S be a stack |
| 6468 | * 4 S.push(v) |
| 6469 | * 5 while S is not empty |
| 6470 | * 6 t <- S.pop() |
| 6471 | * 7 if t is what we're looking for: |
| 6472 | * 8 return t |
| 6473 | * 9 for all edges e in G.adjacentEdges(t) do |
| 6474 | * 10 if edge e is already labelled |
| 6475 | * 11 continue with the next edge |
| 6476 | * 12 w <- G.adjacentVertex(t,e) |
| 6477 | * 13 if vertex w is not discovered and not explored |
| 6478 | * 14 label e as tree-edge |
| 6479 | * 15 label w as discovered |
| 6480 | * 16 S.push(w) |
| 6481 | * 17 continue at 5 |
| 6482 | * 18 else if vertex w is discovered |
| 6483 | * 19 label e as back-edge |
| 6484 | * 20 else |
| 6485 | * 21 // vertex w is explored |
| 6486 | * 22 label e as forward- or cross-edge |
| 6487 | * 23 label t as explored |
| 6488 | * 24 S.pop() |
| 6489 | * |
| 6490 | * convention: |
| 6491 | * 0x10 - discovered |
| 6492 | * 0x11 - discovered and fall-through edge labelled |
| 6493 | * 0x12 - discovered and fall-through and branch edges labelled |
| 6494 | * 0x20 - explored |
| 6495 | */ |
| 6496 | |
| 6497 | enum { |
| 6498 | DISCOVERED = 0x10, |
| 6499 | EXPLORED = 0x20, |
| 6500 | FALLTHROUGH = 1, |
| 6501 | BRANCH = 2, |
| 6502 | }; |
| 6503 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6504 | static u32 state_htab_size(struct bpf_verifier_env *env) |
| 6505 | { |
| 6506 | return env->prog->len; |
| 6507 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6508 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6509 | static struct bpf_verifier_state_list **explored_state( |
| 6510 | struct bpf_verifier_env *env, |
| 6511 | int idx) |
| 6512 | { |
| 6513 | struct bpf_verifier_state *cur = env->cur_state; |
| 6514 | struct bpf_func_state *state = cur->frame[cur->curframe]; |
| 6515 | |
| 6516 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; |
| 6517 | } |
| 6518 | |
| 6519 | static void init_explored_state(struct bpf_verifier_env *env, int idx) |
| 6520 | { |
| 6521 | env->insn_aux_data[idx].prune_point = true; |
| 6522 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6523 | |
| 6524 | /* t, w, e - match pseudo-code above: |
| 6525 | * t - index of current instruction |
| 6526 | * w - next instruction |
| 6527 | * e - edge |
| 6528 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6529 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
| 6530 | bool loop_ok) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6531 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6532 | int *insn_stack = env->cfg.insn_stack; |
| 6533 | int *insn_state = env->cfg.insn_state; |
| 6534 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6535 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 6536 | return 0; |
| 6537 | |
| 6538 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 6539 | return 0; |
| 6540 | |
| 6541 | if (w < 0 || w >= env->prog->len) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6542 | verbose_linfo(env, t, "%d: ", t); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6543 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
| 6544 | return -EINVAL; |
| 6545 | } |
| 6546 | |
| 6547 | if (e == BRANCH) |
| 6548 | /* mark branch target for state pruning */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6549 | init_explored_state(env, w); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6550 | |
| 6551 | if (insn_state[w] == 0) { |
| 6552 | /* tree-edge */ |
| 6553 | insn_state[t] = DISCOVERED | e; |
| 6554 | insn_state[w] = DISCOVERED; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6555 | if (env->cfg.cur_stack >= env->prog->len) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6556 | return -E2BIG; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6557 | insn_stack[env->cfg.cur_stack++] = w; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6558 | return 1; |
| 6559 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6560 | if (loop_ok && env->allow_ptr_leaks) |
| 6561 | return 0; |
| 6562 | verbose_linfo(env, t, "%d: ", t); |
| 6563 | verbose_linfo(env, w, "%d: ", w); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6564 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
| 6565 | return -EINVAL; |
| 6566 | } else if (insn_state[w] == EXPLORED) { |
| 6567 | /* forward- or cross-edge */ |
| 6568 | insn_state[t] = DISCOVERED | e; |
| 6569 | } else { |
| 6570 | verbose(env, "insn state internal bug\n"); |
| 6571 | return -EFAULT; |
| 6572 | } |
| 6573 | return 0; |
| 6574 | } |
| 6575 | |
| 6576 | /* non-recursive depth-first-search to detect loops in BPF program |
| 6577 | * loop == back-edge in directed graph |
| 6578 | */ |
| 6579 | static int check_cfg(struct bpf_verifier_env *env) |
| 6580 | { |
| 6581 | struct bpf_insn *insns = env->prog->insnsi; |
| 6582 | int insn_cnt = env->prog->len; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6583 | int *insn_stack, *insn_state; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6584 | int ret = 0; |
| 6585 | int i, t; |
| 6586 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6587 | 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] | 6588 | if (!insn_state) |
| 6589 | return -ENOMEM; |
| 6590 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6591 | 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] | 6592 | if (!insn_stack) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6593 | kvfree(insn_state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6594 | return -ENOMEM; |
| 6595 | } |
| 6596 | |
| 6597 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 6598 | insn_stack[0] = 0; /* 0 is the first instruction */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6599 | env->cfg.cur_stack = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6600 | |
| 6601 | peek_stack: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6602 | if (env->cfg.cur_stack == 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6603 | goto check_state; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6604 | t = insn_stack[env->cfg.cur_stack - 1]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6605 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6606 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
| 6607 | BPF_CLASS(insns[t].code) == BPF_JMP32) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6608 | u8 opcode = BPF_OP(insns[t].code); |
| 6609 | |
| 6610 | if (opcode == BPF_EXIT) { |
| 6611 | goto mark_explored; |
| 6612 | } else if (opcode == BPF_CALL) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6613 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6614 | if (ret == 1) |
| 6615 | goto peek_stack; |
| 6616 | else if (ret < 0) |
| 6617 | goto err_free; |
| 6618 | if (t + 1 < insn_cnt) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6619 | init_explored_state(env, t + 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6620 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6621 | init_explored_state(env, t); |
| 6622 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, |
| 6623 | env, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6624 | if (ret == 1) |
| 6625 | goto peek_stack; |
| 6626 | else if (ret < 0) |
| 6627 | goto err_free; |
| 6628 | } |
| 6629 | } else if (opcode == BPF_JA) { |
| 6630 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 6631 | ret = -EINVAL; |
| 6632 | goto err_free; |
| 6633 | } |
| 6634 | /* unconditional jump with single edge */ |
| 6635 | ret = push_insn(t, t + insns[t].off + 1, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6636 | FALLTHROUGH, env, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6637 | if (ret == 1) |
| 6638 | goto peek_stack; |
| 6639 | else if (ret < 0) |
| 6640 | goto err_free; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6641 | /* unconditional jmp is not a good pruning point, |
| 6642 | * but it's marked, since backtracking needs |
| 6643 | * to record jmp history in is_state_visited(). |
| 6644 | */ |
| 6645 | init_explored_state(env, t + insns[t].off + 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6646 | /* tell verifier to check for equivalent states |
| 6647 | * after every call and jump |
| 6648 | */ |
| 6649 | if (t + 1 < insn_cnt) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6650 | init_explored_state(env, t + 1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6651 | } else { |
| 6652 | /* conditional jump with two edges */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6653 | init_explored_state(env, t); |
| 6654 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6655 | if (ret == 1) |
| 6656 | goto peek_stack; |
| 6657 | else if (ret < 0) |
| 6658 | goto err_free; |
| 6659 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6660 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6661 | if (ret == 1) |
| 6662 | goto peek_stack; |
| 6663 | else if (ret < 0) |
| 6664 | goto err_free; |
| 6665 | } |
| 6666 | } else { |
| 6667 | /* all other non-branch instructions with single |
| 6668 | * fall-through edge |
| 6669 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6670 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6671 | if (ret == 1) |
| 6672 | goto peek_stack; |
| 6673 | else if (ret < 0) |
| 6674 | goto err_free; |
| 6675 | } |
| 6676 | |
| 6677 | mark_explored: |
| 6678 | insn_state[t] = EXPLORED; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6679 | if (env->cfg.cur_stack-- <= 0) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6680 | verbose(env, "pop stack internal bug\n"); |
| 6681 | ret = -EFAULT; |
| 6682 | goto err_free; |
| 6683 | } |
| 6684 | goto peek_stack; |
| 6685 | |
| 6686 | check_state: |
| 6687 | for (i = 0; i < insn_cnt; i++) { |
| 6688 | if (insn_state[i] != EXPLORED) { |
| 6689 | verbose(env, "unreachable insn %d\n", i); |
| 6690 | ret = -EINVAL; |
| 6691 | goto err_free; |
| 6692 | } |
| 6693 | } |
| 6694 | ret = 0; /* cfg looks good */ |
| 6695 | |
| 6696 | err_free: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6697 | kvfree(insn_state); |
| 6698 | kvfree(insn_stack); |
| 6699 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6700 | return ret; |
| 6701 | } |
| 6702 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 6703 | /* The minimum supported BTF func info size */ |
| 6704 | #define MIN_BPF_FUNCINFO_SIZE 8 |
| 6705 | #define MAX_FUNCINFO_REC_SIZE 252 |
| 6706 | |
| 6707 | static int check_btf_func(struct bpf_verifier_env *env, |
| 6708 | const union bpf_attr *attr, |
| 6709 | union bpf_attr __user *uattr) |
| 6710 | { |
| 6711 | u32 i, nfuncs, urec_size, min_size; |
| 6712 | u32 krec_size = sizeof(struct bpf_func_info); |
| 6713 | struct bpf_func_info *krecord; |
| 6714 | const struct btf_type *type; |
| 6715 | struct bpf_prog *prog; |
| 6716 | const struct btf *btf; |
| 6717 | void __user *urecord; |
| 6718 | u32 prev_offset = 0; |
| 6719 | int ret = 0; |
| 6720 | |
| 6721 | nfuncs = attr->func_info_cnt; |
| 6722 | if (!nfuncs) |
| 6723 | return 0; |
| 6724 | |
| 6725 | if (nfuncs != env->subprog_cnt) { |
| 6726 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); |
| 6727 | return -EINVAL; |
| 6728 | } |
| 6729 | |
| 6730 | urec_size = attr->func_info_rec_size; |
| 6731 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || |
| 6732 | urec_size > MAX_FUNCINFO_REC_SIZE || |
| 6733 | urec_size % sizeof(u32)) { |
| 6734 | verbose(env, "invalid func info rec size %u\n", urec_size); |
| 6735 | return -EINVAL; |
| 6736 | } |
| 6737 | |
| 6738 | prog = env->prog; |
| 6739 | btf = prog->aux->btf; |
| 6740 | |
| 6741 | urecord = u64_to_user_ptr(attr->func_info); |
| 6742 | min_size = min_t(u32, krec_size, urec_size); |
| 6743 | |
| 6744 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
| 6745 | if (!krecord) |
| 6746 | return -ENOMEM; |
| 6747 | |
| 6748 | for (i = 0; i < nfuncs; i++) { |
| 6749 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); |
| 6750 | if (ret) { |
| 6751 | if (ret == -E2BIG) { |
| 6752 | verbose(env, "nonzero tailing record in func info"); |
| 6753 | /* set the size kernel expects so loader can zero |
| 6754 | * out the rest of the record. |
| 6755 | */ |
| 6756 | if (put_user(min_size, &uattr->func_info_rec_size)) |
| 6757 | ret = -EFAULT; |
| 6758 | } |
| 6759 | goto err_free; |
| 6760 | } |
| 6761 | |
| 6762 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
| 6763 | ret = -EFAULT; |
| 6764 | goto err_free; |
| 6765 | } |
| 6766 | |
| 6767 | /* check insn_off */ |
| 6768 | if (i == 0) { |
| 6769 | if (krecord[i].insn_off) { |
| 6770 | verbose(env, |
| 6771 | "nonzero insn_off %u for the first func info record", |
| 6772 | krecord[i].insn_off); |
| 6773 | ret = -EINVAL; |
| 6774 | goto err_free; |
| 6775 | } |
| 6776 | } else if (krecord[i].insn_off <= prev_offset) { |
| 6777 | verbose(env, |
| 6778 | "same or smaller insn offset (%u) than previous func info record (%u)", |
| 6779 | krecord[i].insn_off, prev_offset); |
| 6780 | ret = -EINVAL; |
| 6781 | goto err_free; |
| 6782 | } |
| 6783 | |
| 6784 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
| 6785 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
| 6786 | ret = -EINVAL; |
| 6787 | goto err_free; |
| 6788 | } |
| 6789 | |
| 6790 | /* check type_id */ |
| 6791 | type = btf_type_by_id(btf, krecord[i].type_id); |
| 6792 | if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) { |
| 6793 | verbose(env, "invalid type id %d in func info", |
| 6794 | krecord[i].type_id); |
| 6795 | ret = -EINVAL; |
| 6796 | goto err_free; |
| 6797 | } |
| 6798 | |
| 6799 | prev_offset = krecord[i].insn_off; |
| 6800 | urecord += urec_size; |
| 6801 | } |
| 6802 | |
| 6803 | prog->aux->func_info = krecord; |
| 6804 | prog->aux->func_info_cnt = nfuncs; |
| 6805 | return 0; |
| 6806 | |
| 6807 | err_free: |
| 6808 | kvfree(krecord); |
| 6809 | return ret; |
| 6810 | } |
| 6811 | |
| 6812 | static void adjust_btf_func(struct bpf_verifier_env *env) |
| 6813 | { |
| 6814 | int i; |
| 6815 | |
| 6816 | if (!env->prog->aux->func_info) |
| 6817 | return; |
| 6818 | |
| 6819 | for (i = 0; i < env->subprog_cnt; i++) |
| 6820 | env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start; |
| 6821 | } |
| 6822 | |
| 6823 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
| 6824 | sizeof(((struct bpf_line_info *)(0))->line_col)) |
| 6825 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
| 6826 | |
| 6827 | static int check_btf_line(struct bpf_verifier_env *env, |
| 6828 | const union bpf_attr *attr, |
| 6829 | union bpf_attr __user *uattr) |
| 6830 | { |
| 6831 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; |
| 6832 | struct bpf_subprog_info *sub; |
| 6833 | struct bpf_line_info *linfo; |
| 6834 | struct bpf_prog *prog; |
| 6835 | const struct btf *btf; |
| 6836 | void __user *ulinfo; |
| 6837 | int err; |
| 6838 | |
| 6839 | nr_linfo = attr->line_info_cnt; |
| 6840 | if (!nr_linfo) |
| 6841 | return 0; |
| 6842 | |
| 6843 | rec_size = attr->line_info_rec_size; |
| 6844 | if (rec_size < MIN_BPF_LINEINFO_SIZE || |
| 6845 | rec_size > MAX_LINEINFO_REC_SIZE || |
| 6846 | rec_size & (sizeof(u32) - 1)) |
| 6847 | return -EINVAL; |
| 6848 | |
| 6849 | /* Need to zero it in case the userspace may |
| 6850 | * pass in a smaller bpf_line_info object. |
| 6851 | */ |
| 6852 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), |
| 6853 | GFP_KERNEL | __GFP_NOWARN); |
| 6854 | if (!linfo) |
| 6855 | return -ENOMEM; |
| 6856 | |
| 6857 | prog = env->prog; |
| 6858 | btf = prog->aux->btf; |
| 6859 | |
| 6860 | s = 0; |
| 6861 | sub = env->subprog_info; |
| 6862 | ulinfo = u64_to_user_ptr(attr->line_info); |
| 6863 | expected_size = sizeof(struct bpf_line_info); |
| 6864 | ncopy = min_t(u32, expected_size, rec_size); |
| 6865 | for (i = 0; i < nr_linfo; i++) { |
| 6866 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); |
| 6867 | if (err) { |
| 6868 | if (err == -E2BIG) { |
| 6869 | verbose(env, "nonzero tailing record in line_info"); |
| 6870 | if (put_user(expected_size, |
| 6871 | &uattr->line_info_rec_size)) |
| 6872 | err = -EFAULT; |
| 6873 | } |
| 6874 | goto err_free; |
| 6875 | } |
| 6876 | |
| 6877 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { |
| 6878 | err = -EFAULT; |
| 6879 | goto err_free; |
| 6880 | } |
| 6881 | |
| 6882 | /* |
| 6883 | * Check insn_off to ensure |
| 6884 | * 1) strictly increasing AND |
| 6885 | * 2) bounded by prog->len |
| 6886 | * |
| 6887 | * The linfo[0].insn_off == 0 check logically falls into |
| 6888 | * the later "missing bpf_line_info for func..." case |
| 6889 | * because the first linfo[0].insn_off must be the |
| 6890 | * first sub also and the first sub must have |
| 6891 | * subprog_info[0].start == 0. |
| 6892 | */ |
| 6893 | if ((i && linfo[i].insn_off <= prev_offset) || |
| 6894 | linfo[i].insn_off >= prog->len) { |
| 6895 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", |
| 6896 | i, linfo[i].insn_off, prev_offset, |
| 6897 | prog->len); |
| 6898 | err = -EINVAL; |
| 6899 | goto err_free; |
| 6900 | } |
| 6901 | |
| 6902 | if (!prog->insnsi[linfo[i].insn_off].code) { |
| 6903 | verbose(env, |
| 6904 | "Invalid insn code at line_info[%u].insn_off\n", |
| 6905 | i); |
| 6906 | err = -EINVAL; |
| 6907 | goto err_free; |
| 6908 | } |
| 6909 | |
| 6910 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
| 6911 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { |
| 6912 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
| 6913 | err = -EINVAL; |
| 6914 | goto err_free; |
| 6915 | } |
| 6916 | |
| 6917 | if (s != env->subprog_cnt) { |
| 6918 | if (linfo[i].insn_off == sub[s].start) { |
| 6919 | sub[s].linfo_idx = i; |
| 6920 | s++; |
| 6921 | } else if (sub[s].start < linfo[i].insn_off) { |
| 6922 | verbose(env, "missing bpf_line_info for func#%u\n", s); |
| 6923 | err = -EINVAL; |
| 6924 | goto err_free; |
| 6925 | } |
| 6926 | } |
| 6927 | |
| 6928 | prev_offset = linfo[i].insn_off; |
| 6929 | ulinfo += rec_size; |
| 6930 | } |
| 6931 | |
| 6932 | if (s != env->subprog_cnt) { |
| 6933 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", |
| 6934 | env->subprog_cnt - s, s); |
| 6935 | err = -EINVAL; |
| 6936 | goto err_free; |
| 6937 | } |
| 6938 | |
| 6939 | prog->aux->linfo = linfo; |
| 6940 | prog->aux->nr_linfo = nr_linfo; |
| 6941 | |
| 6942 | return 0; |
| 6943 | |
| 6944 | err_free: |
| 6945 | kvfree(linfo); |
| 6946 | return err; |
| 6947 | } |
| 6948 | |
| 6949 | static int check_btf_info(struct bpf_verifier_env *env, |
| 6950 | const union bpf_attr *attr, |
| 6951 | union bpf_attr __user *uattr) |
| 6952 | { |
| 6953 | struct btf *btf; |
| 6954 | int err; |
| 6955 | |
| 6956 | if (!attr->func_info_cnt && !attr->line_info_cnt) |
| 6957 | return 0; |
| 6958 | |
| 6959 | btf = btf_get_by_fd(attr->prog_btf_fd); |
| 6960 | if (IS_ERR(btf)) |
| 6961 | return PTR_ERR(btf); |
| 6962 | env->prog->aux->btf = btf; |
| 6963 | |
| 6964 | err = check_btf_func(env, attr, uattr); |
| 6965 | if (err) |
| 6966 | return err; |
| 6967 | |
| 6968 | err = check_btf_line(env, attr, uattr); |
| 6969 | if (err) |
| 6970 | return err; |
| 6971 | |
| 6972 | return 0; |
| 6973 | } |
| 6974 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6975 | /* check %cur's range satisfies %old's */ |
| 6976 | static bool range_within(struct bpf_reg_state *old, |
| 6977 | struct bpf_reg_state *cur) |
| 6978 | { |
| 6979 | return old->umin_value <= cur->umin_value && |
| 6980 | old->umax_value >= cur->umax_value && |
| 6981 | old->smin_value <= cur->smin_value && |
| 6982 | old->smax_value >= cur->smax_value; |
| 6983 | } |
| 6984 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6985 | /* If in the old state two registers had the same id, then they need to have |
| 6986 | * the same id in the new state as well. But that id could be different from |
| 6987 | * the old state, so we need to track the mapping from old to new ids. |
| 6988 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 6989 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 6990 | * regs with a different old id could still have new id 9, we don't care about |
| 6991 | * that. |
| 6992 | * So we look through our idmap to see if this old id has been seen before. If |
| 6993 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 6994 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 6995 | 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] | 6996 | { |
| 6997 | unsigned int i; |
| 6998 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 6999 | for (i = 0; i < BPF_ID_MAP_SIZE; i++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7000 | if (!idmap[i].old) { |
| 7001 | /* Reached an empty slot; haven't seen this id before */ |
| 7002 | idmap[i].old = old_id; |
| 7003 | idmap[i].cur = cur_id; |
| 7004 | return true; |
| 7005 | } |
| 7006 | if (idmap[i].old == old_id) |
| 7007 | return idmap[i].cur == cur_id; |
| 7008 | } |
| 7009 | /* We ran out of idmap slots, which should be impossible */ |
| 7010 | WARN_ON_ONCE(1); |
| 7011 | return false; |
| 7012 | } |
| 7013 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7014 | static void clean_func_state(struct bpf_verifier_env *env, |
| 7015 | struct bpf_func_state *st) |
| 7016 | { |
| 7017 | enum bpf_reg_liveness live; |
| 7018 | int i, j; |
| 7019 | |
| 7020 | for (i = 0; i < BPF_REG_FP; i++) { |
| 7021 | live = st->regs[i].live; |
| 7022 | /* liveness must not touch this register anymore */ |
| 7023 | st->regs[i].live |= REG_LIVE_DONE; |
| 7024 | if (!(live & REG_LIVE_READ)) |
| 7025 | /* since the register is unused, clear its state |
| 7026 | * to make further comparison simpler |
| 7027 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 7028 | __mark_reg_not_init(env, &st->regs[i]); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7029 | } |
| 7030 | |
| 7031 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { |
| 7032 | live = st->stack[i].spilled_ptr.live; |
| 7033 | /* liveness must not touch this stack slot anymore */ |
| 7034 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; |
| 7035 | if (!(live & REG_LIVE_READ)) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 7036 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7037 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 7038 | st->stack[i].slot_type[j] = STACK_INVALID; |
| 7039 | } |
| 7040 | } |
| 7041 | } |
| 7042 | |
| 7043 | static void clean_verifier_state(struct bpf_verifier_env *env, |
| 7044 | struct bpf_verifier_state *st) |
| 7045 | { |
| 7046 | int i; |
| 7047 | |
| 7048 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) |
| 7049 | /* all regs in this state in all frames were already marked */ |
| 7050 | return; |
| 7051 | |
| 7052 | for (i = 0; i <= st->curframe; i++) |
| 7053 | clean_func_state(env, st->frame[i]); |
| 7054 | } |
| 7055 | |
| 7056 | /* the parentage chains form a tree. |
| 7057 | * the verifier states are added to state lists at given insn and |
| 7058 | * pushed into state stack for future exploration. |
| 7059 | * when the verifier reaches bpf_exit insn some of the verifer states |
| 7060 | * stored in the state lists have their final liveness state already, |
| 7061 | * but a lot of states will get revised from liveness point of view when |
| 7062 | * the verifier explores other branches. |
| 7063 | * Example: |
| 7064 | * 1: r0 = 1 |
| 7065 | * 2: if r1 == 100 goto pc+1 |
| 7066 | * 3: r0 = 2 |
| 7067 | * 4: exit |
| 7068 | * when the verifier reaches exit insn the register r0 in the state list of |
| 7069 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch |
| 7070 | * of insn 2 and goes exploring further. At the insn 4 it will walk the |
| 7071 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. |
| 7072 | * |
| 7073 | * Since the verifier pushes the branch states as it sees them while exploring |
| 7074 | * the program the condition of walking the branch instruction for the second |
| 7075 | * time means that all states below this branch were already explored and |
| 7076 | * their final liveness markes are already propagated. |
| 7077 | * Hence when the verifier completes the search of state list in is_state_visited() |
| 7078 | * we can call this clean_live_states() function to mark all liveness states |
| 7079 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' |
| 7080 | * will not be used. |
| 7081 | * This function also clears the registers and stack for states that !READ |
| 7082 | * to simplify state merging. |
| 7083 | * |
| 7084 | * Important note here that walking the same branch instruction in the callee |
| 7085 | * doesn't meant that the states are DONE. The verifier has to compare |
| 7086 | * the callsites |
| 7087 | */ |
| 7088 | static void clean_live_states(struct bpf_verifier_env *env, int insn, |
| 7089 | struct bpf_verifier_state *cur) |
| 7090 | { |
| 7091 | struct bpf_verifier_state_list *sl; |
| 7092 | int i; |
| 7093 | |
| 7094 | sl = *explored_state(env, insn); |
| 7095 | while (sl) { |
| 7096 | if (sl->state.branches) |
| 7097 | goto next; |
| 7098 | if (sl->state.insn_idx != insn || |
| 7099 | sl->state.curframe != cur->curframe) |
| 7100 | goto next; |
| 7101 | for (i = 0; i <= cur->curframe; i++) |
| 7102 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) |
| 7103 | goto next; |
| 7104 | clean_verifier_state(env, &sl->state); |
| 7105 | next: |
| 7106 | sl = sl->next; |
| 7107 | } |
| 7108 | } |
| 7109 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7110 | /* Returns true if (rold safe implies rcur safe) */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 7111 | static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, |
| 7112 | struct bpf_reg_state *rcur, struct bpf_id_pair *idmap) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7113 | { |
| 7114 | bool equal; |
| 7115 | |
| 7116 | if (!(rold->live & REG_LIVE_READ)) |
| 7117 | /* explored state didn't use this */ |
| 7118 | return true; |
| 7119 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7120 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7121 | |
| 7122 | if (rold->type == PTR_TO_STACK) |
| 7123 | /* two stack pointers are equal only if they're pointing to |
| 7124 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 7125 | */ |
| 7126 | return equal && rold->frameno == rcur->frameno; |
| 7127 | |
| 7128 | if (equal) |
| 7129 | return true; |
| 7130 | |
| 7131 | if (rold->type == NOT_INIT) |
| 7132 | /* explored state can't have used this */ |
| 7133 | return true; |
| 7134 | if (rcur->type == NOT_INIT) |
| 7135 | return false; |
| 7136 | switch (rold->type) { |
| 7137 | case SCALAR_VALUE: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 7138 | if (env->explore_alu_limits) |
| 7139 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7140 | if (rcur->type == SCALAR_VALUE) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7141 | if (!rold->precise && !rcur->precise) |
| 7142 | return true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7143 | /* new val must satisfy old val knowledge */ |
| 7144 | return range_within(rold, rcur) && |
| 7145 | tnum_in(rold->var_off, rcur->var_off); |
| 7146 | } else { |
| 7147 | /* We're trying to use a pointer in place of a scalar. |
| 7148 | * Even if the scalar was unbounded, this could lead to |
| 7149 | * pointer leaks because scalars are allowed to leak |
| 7150 | * while pointers are not. We could make this safe in |
| 7151 | * special cases if root is calling us, but it's |
| 7152 | * probably not worth the hassle. |
| 7153 | */ |
| 7154 | return false; |
| 7155 | } |
| 7156 | case PTR_TO_MAP_VALUE: |
| 7157 | /* If the new min/max/var_off satisfy the old ones and |
| 7158 | * everything else matches, we are OK. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7159 | * 'id' is not compared, since it's only used for maps with |
| 7160 | * bpf_spin_lock inside map element and in such cases if |
| 7161 | * the rest of the prog is valid for one map element then |
| 7162 | * it's valid for all map elements regardless of the key |
| 7163 | * used in bpf_map_lookup() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7164 | */ |
| 7165 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 7166 | range_within(rold, rcur) && |
| 7167 | tnum_in(rold->var_off, rcur->var_off); |
| 7168 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 7169 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 7170 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 7171 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 7172 | * checked, doing so could have affected others with the same |
| 7173 | * id, and we can't check for that because we lost the id when |
| 7174 | * we converted to a PTR_TO_MAP_VALUE. |
| 7175 | */ |
| 7176 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 7177 | return false; |
| 7178 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 7179 | return false; |
| 7180 | /* Check our ids match any regs they're supposed to */ |
| 7181 | return check_ids(rold->id, rcur->id, idmap); |
| 7182 | case PTR_TO_PACKET_META: |
| 7183 | case PTR_TO_PACKET: |
| 7184 | if (rcur->type != rold->type) |
| 7185 | return false; |
| 7186 | /* We must have at least as much range as the old ptr |
| 7187 | * did, so that any accesses which were safe before are |
| 7188 | * still safe. This is true even if old range < old off, |
| 7189 | * since someone could have accessed through (ptr - k), or |
| 7190 | * even done ptr -= k in a register, to get a safe access. |
| 7191 | */ |
| 7192 | if (rold->range > rcur->range) |
| 7193 | return false; |
| 7194 | /* If the offsets don't match, we can't trust our alignment; |
| 7195 | * nor can we be sure that we won't fall out of range. |
| 7196 | */ |
| 7197 | if (rold->off != rcur->off) |
| 7198 | return false; |
| 7199 | /* id relations must be preserved */ |
| 7200 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 7201 | return false; |
| 7202 | /* new val must satisfy old val knowledge */ |
| 7203 | return range_within(rold, rcur) && |
| 7204 | tnum_in(rold->var_off, rcur->var_off); |
| 7205 | case PTR_TO_CTX: |
| 7206 | case CONST_PTR_TO_MAP: |
| 7207 | case PTR_TO_PACKET_END: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7208 | case PTR_TO_FLOW_KEYS: |
| 7209 | case PTR_TO_SOCKET: |
| 7210 | case PTR_TO_SOCKET_OR_NULL: |
| 7211 | case PTR_TO_SOCK_COMMON: |
| 7212 | case PTR_TO_SOCK_COMMON_OR_NULL: |
| 7213 | case PTR_TO_TCP_SOCK: |
| 7214 | case PTR_TO_TCP_SOCK_OR_NULL: |
| 7215 | case PTR_TO_XDP_SOCK: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7216 | /* Only valid matches are exact, which memcmp() above |
| 7217 | * would have accepted |
| 7218 | */ |
| 7219 | default: |
| 7220 | /* Don't know what's going on, just say it's not safe */ |
| 7221 | return false; |
| 7222 | } |
| 7223 | |
| 7224 | /* Shouldn't get here; if we do, say it's not safe */ |
| 7225 | WARN_ON_ONCE(1); |
| 7226 | return false; |
| 7227 | } |
| 7228 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 7229 | static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, |
| 7230 | struct bpf_func_state *cur, struct bpf_id_pair *idmap) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7231 | { |
| 7232 | int i, spi; |
| 7233 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7234 | /* walk slots of the explored stack and ignore any additional |
| 7235 | * slots in the current stack, since explored(safe) state |
| 7236 | * didn't use them |
| 7237 | */ |
| 7238 | for (i = 0; i < old->allocated_stack; i++) { |
| 7239 | spi = i / BPF_REG_SIZE; |
| 7240 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7241 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
| 7242 | i += BPF_REG_SIZE - 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7243 | /* explored state didn't use this */ |
| 7244 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7245 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7246 | |
| 7247 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 7248 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7249 | |
| 7250 | /* explored stack has more populated slots than current stack |
| 7251 | * and these slots were used |
| 7252 | */ |
| 7253 | if (i >= cur->allocated_stack) |
| 7254 | return false; |
| 7255 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7256 | /* if old state was safe with misc data in the stack |
| 7257 | * it will be safe with zero-initialized stack. |
| 7258 | * The opposite is not true |
| 7259 | */ |
| 7260 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 7261 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 7262 | continue; |
| 7263 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 7264 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 7265 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 7266 | * this stack slot, but current has has STACK_MISC -> |
| 7267 | * this verifier states are not equivalent, |
| 7268 | * return false to continue verification of this path |
| 7269 | */ |
| 7270 | return false; |
| 7271 | if (i % BPF_REG_SIZE) |
| 7272 | continue; |
| 7273 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 7274 | continue; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 7275 | if (!regsafe(env, &old->stack[spi].spilled_ptr, |
| 7276 | &cur->stack[spi].spilled_ptr, idmap)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7277 | /* when explored and current stack slot are both storing |
| 7278 | * spilled registers, check that stored pointers types |
| 7279 | * are the same as well. |
| 7280 | * Ex: explored safe path could have stored |
| 7281 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 7282 | * but current path has stored: |
| 7283 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 7284 | * such verifier states are not equivalent. |
| 7285 | * return false to continue verification of this path |
| 7286 | */ |
| 7287 | return false; |
| 7288 | } |
| 7289 | return true; |
| 7290 | } |
| 7291 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7292 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 7293 | { |
| 7294 | if (old->acquired_refs != cur->acquired_refs) |
| 7295 | return false; |
| 7296 | return !memcmp(old->refs, cur->refs, |
| 7297 | sizeof(*old->refs) * old->acquired_refs); |
| 7298 | } |
| 7299 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7300 | /* compare two verifier states |
| 7301 | * |
| 7302 | * all states stored in state_list are known to be valid, since |
| 7303 | * verifier reached 'bpf_exit' instruction through them |
| 7304 | * |
| 7305 | * this function is called when verifier exploring different branches of |
| 7306 | * execution popped from the state stack. If it sees an old state that has |
| 7307 | * more strict register state and more strict stack state then this execution |
| 7308 | * branch doesn't need to be explored further, since verifier already |
| 7309 | * concluded that more strict state leads to valid finish. |
| 7310 | * |
| 7311 | * Therefore two states are equivalent if register state is more conservative |
| 7312 | * and explored stack state is more conservative than the current one. |
| 7313 | * Example: |
| 7314 | * explored current |
| 7315 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 7316 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 7317 | * |
| 7318 | * In other words if current stack state (one being explored) has more |
| 7319 | * valid slots than old one that already passed validation, it means |
| 7320 | * the verifier can stop exploring and conclude that current state is valid too |
| 7321 | * |
| 7322 | * Similarly with registers. If explored state has register type as invalid |
| 7323 | * whereas register type in current state is meaningful, it means that |
| 7324 | * the current state will reach 'bpf_exit' instruction safely |
| 7325 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 7326 | 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] | 7327 | struct bpf_func_state *cur) |
| 7328 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7329 | int i; |
| 7330 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 7331 | memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch)); |
| 7332 | for (i = 0; i < MAX_BPF_REG; i++) |
| 7333 | if (!regsafe(env, &old->regs[i], &cur->regs[i], |
| 7334 | env->idmap_scratch)) |
| 7335 | return false; |
| 7336 | |
| 7337 | if (!stacksafe(env, old, cur, env->idmap_scratch)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7338 | return false; |
| 7339 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7340 | if (!refsafe(old, cur)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 7341 | return false; |
| 7342 | |
| 7343 | return true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7344 | } |
| 7345 | |
| 7346 | static bool states_equal(struct bpf_verifier_env *env, |
| 7347 | struct bpf_verifier_state *old, |
| 7348 | struct bpf_verifier_state *cur) |
| 7349 | { |
| 7350 | int i; |
| 7351 | |
| 7352 | if (old->curframe != cur->curframe) |
| 7353 | return false; |
| 7354 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7355 | /* Verification state from speculative execution simulation |
| 7356 | * must never prune a non-speculative execution one. |
| 7357 | */ |
| 7358 | if (old->speculative && !cur->speculative) |
| 7359 | return false; |
| 7360 | |
| 7361 | if (old->active_spin_lock != cur->active_spin_lock) |
| 7362 | return false; |
| 7363 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7364 | /* for states to be equal callsites have to be the same |
| 7365 | * and all frame states need to be equivalent |
| 7366 | */ |
| 7367 | for (i = 0; i <= old->curframe; i++) { |
| 7368 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 7369 | return false; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 7370 | if (!func_states_equal(env, old->frame[i], cur->frame[i])) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7371 | return false; |
| 7372 | } |
| 7373 | return true; |
| 7374 | } |
| 7375 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7376 | /* Return 0 if no propagation happened. Return negative error code if error |
| 7377 | * happened. Otherwise, return the propagated bit. |
| 7378 | */ |
| 7379 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
| 7380 | struct bpf_reg_state *reg, |
| 7381 | struct bpf_reg_state *parent_reg) |
| 7382 | { |
| 7383 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
| 7384 | u8 flag = reg->live & REG_LIVE_READ; |
| 7385 | int err; |
| 7386 | |
| 7387 | /* When comes here, read flags of PARENT_REG or REG could be any of |
| 7388 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need |
| 7389 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. |
| 7390 | */ |
| 7391 | if (parent_flag == REG_LIVE_READ64 || |
| 7392 | /* Or if there is no read flag from REG. */ |
| 7393 | !flag || |
| 7394 | /* Or if the read flag from REG is the same as PARENT_REG. */ |
| 7395 | parent_flag == flag) |
| 7396 | return 0; |
| 7397 | |
| 7398 | err = mark_reg_read(env, reg, parent_reg, flag); |
| 7399 | if (err) |
| 7400 | return err; |
| 7401 | |
| 7402 | return flag; |
| 7403 | } |
| 7404 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7405 | /* A write screens off any subsequent reads; but write marks come from the |
| 7406 | * straight-line code between a state and its parent. When we arrive at an |
| 7407 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 7408 | * code, so read marks in the state must propagate to the parent regardless |
| 7409 | * 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] | 7410 | * in mark_reg_read() is for. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7411 | */ |
| 7412 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 7413 | const struct bpf_verifier_state *vstate, |
| 7414 | struct bpf_verifier_state *vparent) |
| 7415 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7416 | struct bpf_reg_state *state_reg, *parent_reg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7417 | struct bpf_func_state *state, *parent; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7418 | int i, frame, err = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7419 | |
| 7420 | if (vparent->curframe != vstate->curframe) { |
| 7421 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 7422 | vparent->curframe, vstate->curframe); |
| 7423 | return -EFAULT; |
| 7424 | } |
| 7425 | /* Propagate read liveness of registers... */ |
| 7426 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7427 | for (frame = 0; frame <= vstate->curframe; frame++) { |
| 7428 | parent = vparent->frame[frame]; |
| 7429 | state = vstate->frame[frame]; |
| 7430 | parent_reg = parent->regs; |
| 7431 | state_reg = state->regs; |
| 7432 | /* We don't need to worry about FP liveness, it's read-only */ |
| 7433 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { |
| 7434 | err = propagate_liveness_reg(env, &state_reg[i], |
| 7435 | &parent_reg[i]); |
| 7436 | if (err < 0) |
| 7437 | return err; |
| 7438 | if (err == REG_LIVE_READ64) |
| 7439 | mark_insn_zext(env, &parent_reg[i]); |
| 7440 | } |
| 7441 | |
| 7442 | /* Propagate stack slots. */ |
| 7443 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 7444 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
| 7445 | parent_reg = &parent->stack[i].spilled_ptr; |
| 7446 | state_reg = &state->stack[i].spilled_ptr; |
| 7447 | err = propagate_liveness_reg(env, state_reg, |
| 7448 | parent_reg); |
| 7449 | if (err < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7450 | return err; |
| 7451 | } |
| 7452 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7453 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7454 | } |
| 7455 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7456 | /* find precise scalars in the previous equivalent state and |
| 7457 | * propagate them into the current state |
| 7458 | */ |
| 7459 | static int propagate_precision(struct bpf_verifier_env *env, |
| 7460 | const struct bpf_verifier_state *old) |
| 7461 | { |
| 7462 | struct bpf_reg_state *state_reg; |
| 7463 | struct bpf_func_state *state; |
| 7464 | int i, err = 0; |
| 7465 | |
| 7466 | state = old->frame[old->curframe]; |
| 7467 | state_reg = state->regs; |
| 7468 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { |
| 7469 | if (state_reg->type != SCALAR_VALUE || |
| 7470 | !state_reg->precise) |
| 7471 | continue; |
| 7472 | if (env->log.level & BPF_LOG_LEVEL2) |
| 7473 | verbose(env, "propagating r%d\n", i); |
| 7474 | err = mark_chain_precision(env, i); |
| 7475 | if (err < 0) |
| 7476 | return err; |
| 7477 | } |
| 7478 | |
| 7479 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 7480 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 7481 | continue; |
| 7482 | state_reg = &state->stack[i].spilled_ptr; |
| 7483 | if (state_reg->type != SCALAR_VALUE || |
| 7484 | !state_reg->precise) |
| 7485 | continue; |
| 7486 | if (env->log.level & BPF_LOG_LEVEL2) |
| 7487 | verbose(env, "propagating fp%d\n", |
| 7488 | (-i - 1) * BPF_REG_SIZE); |
| 7489 | err = mark_chain_precision_stack(env, i); |
| 7490 | if (err < 0) |
| 7491 | return err; |
| 7492 | } |
| 7493 | return 0; |
| 7494 | } |
| 7495 | |
| 7496 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
| 7497 | struct bpf_verifier_state *cur) |
| 7498 | { |
| 7499 | struct bpf_func_state *fold, *fcur; |
| 7500 | int i, fr = cur->curframe; |
| 7501 | |
| 7502 | if (old->curframe != fr) |
| 7503 | return false; |
| 7504 | |
| 7505 | fold = old->frame[fr]; |
| 7506 | fcur = cur->frame[fr]; |
| 7507 | for (i = 0; i < MAX_BPF_REG; i++) |
| 7508 | if (memcmp(&fold->regs[i], &fcur->regs[i], |
| 7509 | offsetof(struct bpf_reg_state, parent))) |
| 7510 | return false; |
| 7511 | return true; |
| 7512 | } |
| 7513 | |
| 7514 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7515 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
| 7516 | { |
| 7517 | struct bpf_verifier_state_list *new_sl; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7518 | struct bpf_verifier_state_list *sl, **pprev; |
| 7519 | struct bpf_verifier_state *cur = env->cur_state, *new; |
| 7520 | int i, j, err, states_cnt = 0; |
| 7521 | bool add_new_state = env->test_state_freq ? true : false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7522 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7523 | cur->last_insn_idx = env->prev_insn_idx; |
| 7524 | if (!env->insn_aux_data[insn_idx].prune_point) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7525 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 7526 | * be doing state search here |
| 7527 | */ |
| 7528 | return 0; |
| 7529 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7530 | /* bpf progs typically have pruning point every 4 instructions |
| 7531 | * http://vger.kernel.org/bpfconf2019.html#session-1 |
| 7532 | * Do not add new state for future pruning if the verifier hasn't seen |
| 7533 | * at least 2 jumps and at least 8 instructions. |
| 7534 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. |
| 7535 | * In tests that amounts to up to 50% reduction into total verifier |
| 7536 | * memory consumption and 20% verifier time speedup. |
| 7537 | */ |
| 7538 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && |
| 7539 | env->insn_processed - env->prev_insn_processed >= 8) |
| 7540 | add_new_state = true; |
| 7541 | |
| 7542 | pprev = explored_state(env, insn_idx); |
| 7543 | sl = *pprev; |
| 7544 | |
| 7545 | clean_live_states(env, insn_idx, cur); |
| 7546 | |
| 7547 | while (sl) { |
| 7548 | states_cnt++; |
| 7549 | if (sl->state.insn_idx != insn_idx) |
| 7550 | goto next; |
| 7551 | if (sl->state.branches) { |
| 7552 | if (states_maybe_looping(&sl->state, cur) && |
| 7553 | states_equal(env, &sl->state, cur)) { |
| 7554 | verbose_linfo(env, insn_idx, "; "); |
| 7555 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); |
| 7556 | return -EINVAL; |
| 7557 | } |
| 7558 | /* if the verifier is processing a loop, avoid adding new state |
| 7559 | * too often, since different loop iterations have distinct |
| 7560 | * states and may not help future pruning. |
| 7561 | * This threshold shouldn't be too low to make sure that |
| 7562 | * a loop with large bound will be rejected quickly. |
| 7563 | * The most abusive loop will be: |
| 7564 | * r1 += 1 |
| 7565 | * if r1 < 1000000 goto pc-2 |
| 7566 | * 1M insn_procssed limit / 100 == 10k peak states. |
| 7567 | * This threshold shouldn't be too high either, since states |
| 7568 | * at the end of the loop are likely to be useful in pruning. |
| 7569 | */ |
| 7570 | if (env->jmps_processed - env->prev_jmps_processed < 20 && |
| 7571 | env->insn_processed - env->prev_insn_processed < 100) |
| 7572 | add_new_state = false; |
| 7573 | goto miss; |
| 7574 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7575 | if (states_equal(env, &sl->state, cur)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7576 | sl->hit_cnt++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7577 | /* reached equivalent register/stack state, |
| 7578 | * prune the search. |
| 7579 | * Registers read by the continuation are read by us. |
| 7580 | * If we have any write marks in env->cur_state, they |
| 7581 | * will prevent corresponding reads in the continuation |
| 7582 | * from reaching our parent (an explored_state). Our |
| 7583 | * own state will get the read marks recorded, but |
| 7584 | * they'll be immediately forgotten as we're pruning |
| 7585 | * this state and will pop a new one. |
| 7586 | */ |
| 7587 | err = propagate_liveness(env, &sl->state, cur); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7588 | |
| 7589 | /* if previous state reached the exit with precision and |
| 7590 | * current state is equivalent to it (except precsion marks) |
| 7591 | * the precision needs to be propagated back in |
| 7592 | * the current state. |
| 7593 | */ |
| 7594 | err = err ? : push_jmp_history(env, cur); |
| 7595 | err = err ? : propagate_precision(env, &sl->state); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7596 | if (err) |
| 7597 | return err; |
| 7598 | return 1; |
| 7599 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7600 | miss: |
| 7601 | /* when new state is not going to be added do not increase miss count. |
| 7602 | * Otherwise several loop iterations will remove the state |
| 7603 | * recorded earlier. The goal of these heuristics is to have |
| 7604 | * states from some iterations of the loop (some in the beginning |
| 7605 | * and some at the end) to help pruning. |
| 7606 | */ |
| 7607 | if (add_new_state) |
| 7608 | sl->miss_cnt++; |
| 7609 | /* heuristic to determine whether this state is beneficial |
| 7610 | * to keep checking from state equivalence point of view. |
| 7611 | * Higher numbers increase max_states_per_insn and verification time, |
| 7612 | * but do not meaningfully decrease insn_processed. |
| 7613 | */ |
| 7614 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { |
| 7615 | /* the state is unlikely to be useful. Remove it to |
| 7616 | * speed up verification |
| 7617 | */ |
| 7618 | *pprev = sl->next; |
| 7619 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { |
| 7620 | u32 br = sl->state.branches; |
| 7621 | |
| 7622 | WARN_ONCE(br, |
| 7623 | "BUG live_done but branches_to_explore %d\n", |
| 7624 | br); |
| 7625 | free_verifier_state(&sl->state, false); |
| 7626 | kfree(sl); |
| 7627 | env->peak_states--; |
| 7628 | } else { |
| 7629 | /* cannot free this state, since parentage chain may |
| 7630 | * walk it later. Add it for free_list instead to |
| 7631 | * be freed at the end of verification |
| 7632 | */ |
| 7633 | sl->next = env->free_list; |
| 7634 | env->free_list = sl; |
| 7635 | } |
| 7636 | sl = *pprev; |
| 7637 | continue; |
| 7638 | } |
| 7639 | next: |
| 7640 | pprev = &sl->next; |
| 7641 | sl = *pprev; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7642 | } |
| 7643 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7644 | if (env->max_states_per_insn < states_cnt) |
| 7645 | env->max_states_per_insn = states_cnt; |
| 7646 | |
| 7647 | if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
| 7648 | return push_jmp_history(env, cur); |
| 7649 | |
| 7650 | if (!add_new_state) |
| 7651 | return push_jmp_history(env, cur); |
| 7652 | |
| 7653 | /* There were no equivalent states, remember the current one. |
| 7654 | * Technically the current state is not proven to be safe yet, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7655 | * 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] | 7656 | * 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] | 7657 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7658 | * again on the way to bpf_exit. |
| 7659 | * When looping the sl->state.branches will be > 0 and this state |
| 7660 | * will not be considered for equivalence until branches == 0. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7661 | */ |
| 7662 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
| 7663 | if (!new_sl) |
| 7664 | return -ENOMEM; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7665 | env->total_states++; |
| 7666 | env->peak_states++; |
| 7667 | env->prev_jmps_processed = env->jmps_processed; |
| 7668 | env->prev_insn_processed = env->insn_processed; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7669 | |
| 7670 | /* add new state to the head of linked list */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7671 | new = &new_sl->state; |
| 7672 | err = copy_verifier_state(new, cur); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7673 | if (err) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7674 | free_verifier_state(new, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7675 | kfree(new_sl); |
| 7676 | return err; |
| 7677 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7678 | new->insn_idx = insn_idx; |
| 7679 | WARN_ONCE(new->branches != 1, |
| 7680 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); |
| 7681 | |
| 7682 | cur->parent = new; |
| 7683 | cur->first_insn_idx = insn_idx; |
| 7684 | clear_jmp_history(cur); |
| 7685 | new_sl->next = *explored_state(env, insn_idx); |
| 7686 | *explored_state(env, insn_idx) = new_sl; |
| 7687 | /* connect new state to parentage chain. Current frame needs all |
| 7688 | * registers connected. Only r6 - r9 of the callers are alive (pushed |
| 7689 | * to the stack implicitly by JITs) so in callers' frames connect just |
| 7690 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to |
| 7691 | * the state of the call instruction (with WRITTEN set), and r0 comes |
| 7692 | * from callee with its full parentage chain, anyway. |
| 7693 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7694 | /* clear write marks in current state: the writes we did are not writes |
| 7695 | * our child did, so they don't screen off its reads from us. |
| 7696 | * (There are no read marks in current state, because reads always mark |
| 7697 | * their parent and current state never has children yet. Only |
| 7698 | * explored_states can get read marks.) |
| 7699 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7700 | for (j = 0; j <= cur->curframe; j++) { |
| 7701 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) |
| 7702 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; |
| 7703 | for (i = 0; i < BPF_REG_FP; i++) |
| 7704 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; |
| 7705 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7706 | |
| 7707 | /* all stack frames are accessible from callee, clear them all */ |
| 7708 | for (j = 0; j <= cur->curframe; j++) { |
| 7709 | struct bpf_func_state *frame = cur->frame[j]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7710 | struct bpf_func_state *newframe = new->frame[j]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7711 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7712 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7713 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7714 | frame->stack[i].spilled_ptr.parent = |
| 7715 | &newframe->stack[i].spilled_ptr; |
| 7716 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7717 | } |
| 7718 | return 0; |
| 7719 | } |
| 7720 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7721 | /* Return true if it's OK to have the same insn return a different type. */ |
| 7722 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 7723 | { |
| 7724 | switch (type) { |
| 7725 | case PTR_TO_CTX: |
| 7726 | case PTR_TO_SOCKET: |
| 7727 | case PTR_TO_SOCKET_OR_NULL: |
| 7728 | case PTR_TO_SOCK_COMMON: |
| 7729 | case PTR_TO_SOCK_COMMON_OR_NULL: |
| 7730 | case PTR_TO_TCP_SOCK: |
| 7731 | case PTR_TO_TCP_SOCK_OR_NULL: |
| 7732 | case PTR_TO_XDP_SOCK: |
| 7733 | return false; |
| 7734 | default: |
| 7735 | return true; |
| 7736 | } |
| 7737 | } |
| 7738 | |
| 7739 | /* If an instruction was previously used with particular pointer types, then we |
| 7740 | * need to be careful to avoid cases such as the below, where it may be ok |
| 7741 | * for one branch accessing the pointer, but not ok for the other branch: |
| 7742 | * |
| 7743 | * R1 = sock_ptr |
| 7744 | * goto X; |
| 7745 | * ... |
| 7746 | * R1 = some_other_valid_ptr; |
| 7747 | * goto X; |
| 7748 | * ... |
| 7749 | * R2 = *(u32 *)(R1 + 0); |
| 7750 | */ |
| 7751 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 7752 | { |
| 7753 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 7754 | !reg_type_mismatch_ok(prev)); |
| 7755 | } |
| 7756 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7757 | static int do_check(struct bpf_verifier_env *env) |
| 7758 | { |
| 7759 | struct bpf_verifier_state *state; |
| 7760 | struct bpf_insn *insns = env->prog->insnsi; |
| 7761 | struct bpf_reg_state *regs; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7762 | int insn_cnt = env->prog->len; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7763 | bool do_print_state = false; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7764 | int prev_insn_idx = -1; |
| 7765 | |
| 7766 | env->prev_linfo = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7767 | |
| 7768 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 7769 | if (!state) |
| 7770 | return -ENOMEM; |
| 7771 | state->curframe = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7772 | state->speculative = false; |
| 7773 | state->branches = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7774 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 7775 | if (!state->frame[0]) { |
| 7776 | kfree(state); |
| 7777 | return -ENOMEM; |
| 7778 | } |
| 7779 | env->cur_state = state; |
| 7780 | init_func_state(env, state->frame[0], |
| 7781 | BPF_MAIN_FUNC /* callsite */, |
| 7782 | 0 /* frameno */, |
| 7783 | 0 /* subprogno, zero == main subprog */); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7784 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7785 | for (;;) { |
| 7786 | struct bpf_insn *insn; |
| 7787 | u8 class; |
| 7788 | int err; |
| 7789 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7790 | env->prev_insn_idx = prev_insn_idx; |
| 7791 | if (env->insn_idx >= insn_cnt) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7792 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7793 | env->insn_idx, insn_cnt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7794 | return -EFAULT; |
| 7795 | } |
| 7796 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7797 | insn = &insns[env->insn_idx]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7798 | class = BPF_CLASS(insn->code); |
| 7799 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7800 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7801 | verbose(env, |
| 7802 | "BPF program is too large. Processed %d insn\n", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7803 | env->insn_processed); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7804 | return -E2BIG; |
| 7805 | } |
| 7806 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7807 | err = is_state_visited(env, env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7808 | if (err < 0) |
| 7809 | return err; |
| 7810 | if (err == 1) { |
| 7811 | /* found equivalent state, can prune the search */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7812 | if (env->log.level & BPF_LOG_LEVEL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7813 | if (do_print_state) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7814 | verbose(env, "\nfrom %d to %d%s: safe\n", |
| 7815 | env->prev_insn_idx, env->insn_idx, |
| 7816 | env->cur_state->speculative ? |
| 7817 | " (speculative execution)" : ""); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7818 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7819 | verbose(env, "%d: safe\n", env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7820 | } |
| 7821 | goto process_bpf_exit; |
| 7822 | } |
| 7823 | |
| 7824 | if (signal_pending(current)) |
| 7825 | return -EAGAIN; |
| 7826 | |
| 7827 | if (need_resched()) |
| 7828 | cond_resched(); |
| 7829 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7830 | if (env->log.level & BPF_LOG_LEVEL2 || |
| 7831 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { |
| 7832 | if (env->log.level & BPF_LOG_LEVEL2) |
| 7833 | verbose(env, "%d:", env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7834 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7835 | verbose(env, "\nfrom %d to %d%s:", |
| 7836 | env->prev_insn_idx, env->insn_idx, |
| 7837 | env->cur_state->speculative ? |
| 7838 | " (speculative execution)" : ""); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7839 | print_verifier_state(env, state->frame[state->curframe]); |
| 7840 | do_print_state = false; |
| 7841 | } |
| 7842 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7843 | if (env->log.level & BPF_LOG_LEVEL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7844 | const struct bpf_insn_cbs cbs = { |
| 7845 | .cb_print = verbose, |
| 7846 | .private_data = env, |
| 7847 | }; |
| 7848 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7849 | verbose_linfo(env, env->insn_idx, "; "); |
| 7850 | verbose(env, "%d: ", env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7851 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
| 7852 | } |
| 7853 | |
| 7854 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7855 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
| 7856 | env->prev_insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7857 | if (err) |
| 7858 | return err; |
| 7859 | } |
| 7860 | |
| 7861 | regs = cur_regs(env); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 7862 | sanitize_mark_insn_seen(env); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7863 | prev_insn_idx = env->insn_idx; |
| 7864 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7865 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 7866 | err = check_alu_op(env, insn); |
| 7867 | if (err) |
| 7868 | return err; |
| 7869 | |
| 7870 | } else if (class == BPF_LDX) { |
| 7871 | enum bpf_reg_type *prev_src_type, src_reg_type; |
| 7872 | |
| 7873 | /* check for reserved fields is already done */ |
| 7874 | |
| 7875 | /* check src operand */ |
| 7876 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 7877 | if (err) |
| 7878 | return err; |
| 7879 | |
| 7880 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
| 7881 | if (err) |
| 7882 | return err; |
| 7883 | |
| 7884 | src_reg_type = regs[insn->src_reg].type; |
| 7885 | |
| 7886 | /* check that memory (src_reg + off) is readable, |
| 7887 | * the state of dst_reg will be updated by this func |
| 7888 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7889 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
| 7890 | insn->off, BPF_SIZE(insn->code), |
| 7891 | BPF_READ, insn->dst_reg, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7892 | if (err) |
| 7893 | return err; |
| 7894 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7895 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7896 | |
| 7897 | if (*prev_src_type == NOT_INIT) { |
| 7898 | /* saw a valid insn |
| 7899 | * dst_reg = *(u32 *)(src_reg + off) |
| 7900 | * save type to validate intersecting paths |
| 7901 | */ |
| 7902 | *prev_src_type = src_reg_type; |
| 7903 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7904 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7905 | /* ABuser program is trying to use the same insn |
| 7906 | * dst_reg = *(u32*) (src_reg + off) |
| 7907 | * with different pointer types: |
| 7908 | * src_reg == ctx in one branch and |
| 7909 | * src_reg == stack|map in some other branch. |
| 7910 | * Reject it. |
| 7911 | */ |
| 7912 | verbose(env, "same insn cannot be used with different pointers\n"); |
| 7913 | return -EINVAL; |
| 7914 | } |
| 7915 | |
| 7916 | } else if (class == BPF_STX) { |
| 7917 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
| 7918 | |
| 7919 | if (BPF_MODE(insn->code) == BPF_XADD) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7920 | err = check_xadd(env, env->insn_idx, insn); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7921 | if (err) |
| 7922 | return err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7923 | env->insn_idx++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7924 | continue; |
| 7925 | } |
| 7926 | |
| 7927 | /* check src1 operand */ |
| 7928 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
| 7929 | if (err) |
| 7930 | return err; |
| 7931 | /* check src2 operand */ |
| 7932 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
| 7933 | if (err) |
| 7934 | return err; |
| 7935 | |
| 7936 | dst_reg_type = regs[insn->dst_reg].type; |
| 7937 | |
| 7938 | /* check that memory (dst_reg + off) is writeable */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7939 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 7940 | insn->off, BPF_SIZE(insn->code), |
| 7941 | BPF_WRITE, insn->src_reg, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7942 | if (err) |
| 7943 | return err; |
| 7944 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7945 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7946 | |
| 7947 | if (*prev_dst_type == NOT_INIT) { |
| 7948 | *prev_dst_type = dst_reg_type; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7949 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7950 | verbose(env, "same insn cannot be used with different pointers\n"); |
| 7951 | return -EINVAL; |
| 7952 | } |
| 7953 | |
| 7954 | } else if (class == BPF_ST) { |
| 7955 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 7956 | insn->src_reg != BPF_REG_0) { |
| 7957 | verbose(env, "BPF_ST uses reserved fields\n"); |
| 7958 | return -EINVAL; |
| 7959 | } |
| 7960 | /* check src operand */ |
| 7961 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
| 7962 | if (err) |
| 7963 | return err; |
| 7964 | |
| 7965 | if (is_ctx_reg(env, insn->dst_reg)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7966 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
| 7967 | insn->dst_reg, |
| 7968 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7969 | return -EACCES; |
| 7970 | } |
| 7971 | |
| 7972 | /* check that memory (dst_reg + off) is writeable */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7973 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 7974 | insn->off, BPF_SIZE(insn->code), |
| 7975 | BPF_WRITE, -1, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7976 | if (err) |
| 7977 | return err; |
| 7978 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7979 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7980 | u8 opcode = BPF_OP(insn->code); |
| 7981 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7982 | env->jmps_processed++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7983 | if (opcode == BPF_CALL) { |
| 7984 | if (BPF_SRC(insn->code) != BPF_K || |
| 7985 | insn->off != 0 || |
| 7986 | (insn->src_reg != BPF_REG_0 && |
| 7987 | insn->src_reg != BPF_PSEUDO_CALL) || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7988 | insn->dst_reg != BPF_REG_0 || |
| 7989 | class == BPF_JMP32) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7990 | verbose(env, "BPF_CALL uses reserved fields\n"); |
| 7991 | return -EINVAL; |
| 7992 | } |
| 7993 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7994 | if (env->cur_state->active_spin_lock && |
| 7995 | (insn->src_reg == BPF_PSEUDO_CALL || |
| 7996 | insn->imm != BPF_FUNC_spin_unlock)) { |
| 7997 | verbose(env, "function calls are not allowed while holding a lock\n"); |
| 7998 | return -EINVAL; |
| 7999 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8000 | if (insn->src_reg == BPF_PSEUDO_CALL) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8001 | err = check_func_call(env, insn, &env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8002 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8003 | err = check_helper_call(env, insn->imm, env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8004 | if (err) |
| 8005 | return err; |
| 8006 | |
| 8007 | } else if (opcode == BPF_JA) { |
| 8008 | if (BPF_SRC(insn->code) != BPF_K || |
| 8009 | insn->imm != 0 || |
| 8010 | insn->src_reg != BPF_REG_0 || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8011 | insn->dst_reg != BPF_REG_0 || |
| 8012 | class == BPF_JMP32) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8013 | verbose(env, "BPF_JA uses reserved fields\n"); |
| 8014 | return -EINVAL; |
| 8015 | } |
| 8016 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8017 | env->insn_idx += insn->off + 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8018 | continue; |
| 8019 | |
| 8020 | } else if (opcode == BPF_EXIT) { |
| 8021 | if (BPF_SRC(insn->code) != BPF_K || |
| 8022 | insn->imm != 0 || |
| 8023 | insn->src_reg != BPF_REG_0 || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8024 | insn->dst_reg != BPF_REG_0 || |
| 8025 | class == BPF_JMP32) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8026 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
| 8027 | return -EINVAL; |
| 8028 | } |
| 8029 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8030 | if (env->cur_state->active_spin_lock) { |
| 8031 | verbose(env, "bpf_spin_unlock is missing\n"); |
| 8032 | return -EINVAL; |
| 8033 | } |
| 8034 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8035 | if (state->curframe) { |
| 8036 | /* exit from nested function */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8037 | err = prepare_func_exit(env, &env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8038 | if (err) |
| 8039 | return err; |
| 8040 | do_print_state = true; |
| 8041 | continue; |
| 8042 | } |
| 8043 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8044 | err = check_reference_leak(env); |
| 8045 | if (err) |
| 8046 | return err; |
| 8047 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8048 | /* eBPF calling convetion is such that R0 is used |
| 8049 | * to return the value from eBPF program. |
| 8050 | * Make sure that it's readable at this time |
| 8051 | * of bpf_exit, which means that program wrote |
| 8052 | * something into it earlier |
| 8053 | */ |
| 8054 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
| 8055 | if (err) |
| 8056 | return err; |
| 8057 | |
| 8058 | if (is_pointer_value(env, BPF_REG_0)) { |
| 8059 | verbose(env, "R0 leaks addr as return value\n"); |
| 8060 | return -EACCES; |
| 8061 | } |
| 8062 | |
| 8063 | err = check_return_code(env); |
| 8064 | if (err) |
| 8065 | return err; |
| 8066 | process_bpf_exit: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8067 | update_branch_counts(env, env->cur_state); |
| 8068 | err = pop_stack(env, &prev_insn_idx, |
| 8069 | &env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8070 | if (err < 0) { |
| 8071 | if (err != -ENOENT) |
| 8072 | return err; |
| 8073 | break; |
| 8074 | } else { |
| 8075 | do_print_state = true; |
| 8076 | continue; |
| 8077 | } |
| 8078 | } else { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8079 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8080 | if (err) |
| 8081 | return err; |
| 8082 | } |
| 8083 | } else if (class == BPF_LD) { |
| 8084 | u8 mode = BPF_MODE(insn->code); |
| 8085 | |
| 8086 | if (mode == BPF_ABS || mode == BPF_IND) { |
| 8087 | err = check_ld_abs(env, insn); |
| 8088 | if (err) |
| 8089 | return err; |
| 8090 | |
| 8091 | } else if (mode == BPF_IMM) { |
| 8092 | err = check_ld_imm(env, insn); |
| 8093 | if (err) |
| 8094 | return err; |
| 8095 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8096 | env->insn_idx++; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8097 | sanitize_mark_insn_seen(env); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8098 | } else { |
| 8099 | verbose(env, "invalid BPF_LD mode\n"); |
| 8100 | return -EINVAL; |
| 8101 | } |
| 8102 | } else { |
| 8103 | verbose(env, "unknown insn class %d\n", class); |
| 8104 | return -EINVAL; |
| 8105 | } |
| 8106 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8107 | env->insn_idx++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8108 | } |
| 8109 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8110 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
| 8111 | return 0; |
| 8112 | } |
| 8113 | |
| 8114 | static int check_map_prealloc(struct bpf_map *map) |
| 8115 | { |
| 8116 | return (map->map_type != BPF_MAP_TYPE_HASH && |
| 8117 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 8118 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
| 8119 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 8120 | } |
| 8121 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8122 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
| 8123 | { |
| 8124 | switch (type) { |
| 8125 | case BPF_PROG_TYPE_KPROBE: |
| 8126 | case BPF_PROG_TYPE_TRACEPOINT: |
| 8127 | case BPF_PROG_TYPE_PERF_EVENT: |
| 8128 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 8129 | return true; |
| 8130 | default: |
| 8131 | return false; |
| 8132 | } |
| 8133 | } |
| 8134 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8135 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 8136 | struct bpf_map *map, |
| 8137 | struct bpf_prog *prog) |
| 8138 | |
| 8139 | { |
| 8140 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 8141 | * preallocated hash maps, since doing memory allocation |
| 8142 | * in overflow_handler can crash depending on where nmi got |
| 8143 | * triggered. |
| 8144 | */ |
| 8145 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 8146 | if (!check_map_prealloc(map)) { |
| 8147 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
| 8148 | return -EINVAL; |
| 8149 | } |
| 8150 | if (map->inner_map_meta && |
| 8151 | !check_map_prealloc(map->inner_map_meta)) { |
| 8152 | verbose(env, "perf_event programs can only use preallocated inner hash map\n"); |
| 8153 | return -EINVAL; |
| 8154 | } |
| 8155 | } |
| 8156 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8157 | if ((is_tracing_prog_type(prog->type) || |
| 8158 | prog->type == BPF_PROG_TYPE_SOCKET_FILTER) && |
| 8159 | map_value_has_spin_lock(map)) { |
| 8160 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); |
| 8161 | return -EINVAL; |
| 8162 | } |
| 8163 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8164 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
| 8165 | !bpf_offload_prog_map_match(prog, map)) { |
| 8166 | verbose(env, "offload device mismatch between prog and map\n"); |
| 8167 | return -EINVAL; |
| 8168 | } |
| 8169 | |
| 8170 | return 0; |
| 8171 | } |
| 8172 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8173 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 8174 | { |
| 8175 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 8176 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 8177 | } |
| 8178 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8179 | /* look for pseudo eBPF instructions that access map FDs and |
| 8180 | * replace them with actual map pointers |
| 8181 | */ |
| 8182 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
| 8183 | { |
| 8184 | struct bpf_insn *insn = env->prog->insnsi; |
| 8185 | int insn_cnt = env->prog->len; |
| 8186 | int i, j, err; |
| 8187 | |
| 8188 | err = bpf_prog_calc_tag(env->prog); |
| 8189 | if (err) |
| 8190 | return err; |
| 8191 | |
| 8192 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 8193 | if (BPF_CLASS(insn->code) == BPF_LDX && |
| 8194 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
| 8195 | verbose(env, "BPF_LDX uses reserved fields\n"); |
| 8196 | return -EINVAL; |
| 8197 | } |
| 8198 | |
| 8199 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 8200 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 8201 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
| 8202 | verbose(env, "BPF_STX uses reserved fields\n"); |
| 8203 | return -EINVAL; |
| 8204 | } |
| 8205 | |
| 8206 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8207 | struct bpf_insn_aux_data *aux; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8208 | struct bpf_map *map; |
| 8209 | struct fd f; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8210 | u64 addr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8211 | |
| 8212 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 8213 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 8214 | insn[1].off != 0) { |
| 8215 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
| 8216 | return -EINVAL; |
| 8217 | } |
| 8218 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8219 | if (insn[0].src_reg == 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8220 | /* valid generic load 64-bit imm */ |
| 8221 | goto next_insn; |
| 8222 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8223 | /* In final convert_pseudo_ld_imm64() step, this is |
| 8224 | * converted into regular 64-bit imm load insn. |
| 8225 | */ |
| 8226 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && |
| 8227 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || |
| 8228 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && |
| 8229 | insn[1].imm != 0)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8230 | verbose(env, |
| 8231 | "unrecognized bpf_ld_imm64 insn\n"); |
| 8232 | return -EINVAL; |
| 8233 | } |
| 8234 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8235 | f = fdget(insn[0].imm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8236 | map = __bpf_map_get(f); |
| 8237 | if (IS_ERR(map)) { |
| 8238 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8239 | insn[0].imm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8240 | return PTR_ERR(map); |
| 8241 | } |
| 8242 | |
| 8243 | err = check_map_prog_compatibility(env, map, env->prog); |
| 8244 | if (err) { |
| 8245 | fdput(f); |
| 8246 | return err; |
| 8247 | } |
| 8248 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8249 | aux = &env->insn_aux_data[i]; |
| 8250 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 8251 | addr = (unsigned long)map; |
| 8252 | } else { |
| 8253 | u32 off = insn[1].imm; |
| 8254 | |
| 8255 | if (off >= BPF_MAX_VAR_OFF) { |
| 8256 | verbose(env, "direct value offset of %u is not allowed\n", off); |
| 8257 | fdput(f); |
| 8258 | return -EINVAL; |
| 8259 | } |
| 8260 | |
| 8261 | if (!map->ops->map_direct_value_addr) { |
| 8262 | verbose(env, "no direct value access support for this map type\n"); |
| 8263 | fdput(f); |
| 8264 | return -EINVAL; |
| 8265 | } |
| 8266 | |
| 8267 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 8268 | if (err) { |
| 8269 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", |
| 8270 | map->value_size, off); |
| 8271 | fdput(f); |
| 8272 | return err; |
| 8273 | } |
| 8274 | |
| 8275 | aux->map_off = off; |
| 8276 | addr += off; |
| 8277 | } |
| 8278 | |
| 8279 | insn[0].imm = (u32)addr; |
| 8280 | insn[1].imm = addr >> 32; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8281 | |
| 8282 | /* check whether we recorded this map already */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8283 | for (j = 0; j < env->used_map_cnt; j++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8284 | if (env->used_maps[j] == map) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8285 | aux->map_index = j; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8286 | fdput(f); |
| 8287 | goto next_insn; |
| 8288 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8289 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8290 | |
| 8291 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 8292 | fdput(f); |
| 8293 | return -E2BIG; |
| 8294 | } |
| 8295 | |
| 8296 | /* hold the map. If the program is rejected by verifier, |
| 8297 | * the map will be released by release_maps() or it |
| 8298 | * will be used by the valid program until it's unloaded |
| 8299 | * and all maps are released in free_used_maps() |
| 8300 | */ |
| 8301 | map = bpf_map_inc(map, false); |
| 8302 | if (IS_ERR(map)) { |
| 8303 | fdput(f); |
| 8304 | return PTR_ERR(map); |
| 8305 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8306 | |
| 8307 | aux->map_index = env->used_map_cnt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8308 | env->used_maps[env->used_map_cnt++] = map; |
| 8309 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8310 | if (bpf_map_is_cgroup_storage(map) && |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8311 | bpf_cgroup_storage_assign(env->prog, map)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8312 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8313 | fdput(f); |
| 8314 | return -EBUSY; |
| 8315 | } |
| 8316 | |
| 8317 | fdput(f); |
| 8318 | next_insn: |
| 8319 | insn++; |
| 8320 | i++; |
| 8321 | continue; |
| 8322 | } |
| 8323 | |
| 8324 | /* Basic sanity check before we invest more work here. */ |
| 8325 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 8326 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 8327 | return -EINVAL; |
| 8328 | } |
| 8329 | } |
| 8330 | |
| 8331 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 8332 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 8333 | * These pointers will be used later by verifier to validate map access. |
| 8334 | */ |
| 8335 | return 0; |
| 8336 | } |
| 8337 | |
| 8338 | /* drop refcnt of maps used by the rejected program */ |
| 8339 | static void release_maps(struct bpf_verifier_env *env) |
| 8340 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8341 | enum bpf_cgroup_storage_type stype; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8342 | int i; |
| 8343 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8344 | for_each_cgroup_storage_type(stype) { |
| 8345 | if (!env->prog->aux->cgroup_storage[stype]) |
| 8346 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8347 | bpf_cgroup_storage_release(env->prog, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8348 | env->prog->aux->cgroup_storage[stype]); |
| 8349 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8350 | |
| 8351 | for (i = 0; i < env->used_map_cnt; i++) |
| 8352 | bpf_map_put(env->used_maps[i]); |
| 8353 | } |
| 8354 | |
| 8355 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
| 8356 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
| 8357 | { |
| 8358 | struct bpf_insn *insn = env->prog->insnsi; |
| 8359 | int insn_cnt = env->prog->len; |
| 8360 | int i; |
| 8361 | |
| 8362 | for (i = 0; i < insn_cnt; i++, insn++) |
| 8363 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 8364 | insn->src_reg = 0; |
| 8365 | } |
| 8366 | |
| 8367 | /* single env->prog->insni[off] instruction was replaced with the range |
| 8368 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 8369 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 8370 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8371 | static void adjust_insn_aux_data(struct bpf_verifier_env *env, |
| 8372 | struct bpf_insn_aux_data *new_data, |
| 8373 | struct bpf_prog *new_prog, u32 off, u32 cnt) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8374 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8375 | struct bpf_insn_aux_data *old_data = env->insn_aux_data; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8376 | struct bpf_insn *insn = new_prog->insnsi; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8377 | bool old_seen = old_data[off].seen; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8378 | u32 prog_len; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8379 | int i; |
| 8380 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8381 | /* aux info at OFF always needs adjustment, no matter fast path |
| 8382 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the |
| 8383 | * original insn at old prog. |
| 8384 | */ |
| 8385 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); |
| 8386 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8387 | if (cnt == 1) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8388 | return; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8389 | prog_len = new_prog->len; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8390 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8391 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 8392 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 8393 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8394 | for (i = off; i < off + cnt - 1; i++) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8395 | /* Expand insni[off]'s seen count to the patched range. */ |
| 8396 | new_data[i].seen = old_seen; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8397 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
| 8398 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8399 | env->insn_aux_data = new_data; |
| 8400 | vfree(old_data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8401 | } |
| 8402 | |
| 8403 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 8404 | { |
| 8405 | int i; |
| 8406 | |
| 8407 | if (len == 1) |
| 8408 | return; |
| 8409 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 8410 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 8411 | if (env->subprog_info[i].start <= off) |
| 8412 | continue; |
| 8413 | env->subprog_info[i].start += len - 1; |
| 8414 | } |
| 8415 | } |
| 8416 | |
| 8417 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 8418 | const struct bpf_insn *patch, u32 len) |
| 8419 | { |
| 8420 | struct bpf_prog *new_prog; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8421 | struct bpf_insn_aux_data *new_data = NULL; |
| 8422 | |
| 8423 | if (len > 1) { |
| 8424 | new_data = vzalloc(array_size(env->prog->len + len - 1, |
| 8425 | sizeof(struct bpf_insn_aux_data))); |
| 8426 | if (!new_data) |
| 8427 | return NULL; |
| 8428 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8429 | |
| 8430 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8431 | if (IS_ERR(new_prog)) { |
| 8432 | if (PTR_ERR(new_prog) == -ERANGE) |
| 8433 | verbose(env, |
| 8434 | "insn %d cannot be patched due to 16-bit range\n", |
| 8435 | env->insn_aux_data[off].orig_idx); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8436 | vfree(new_data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8437 | return NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8438 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8439 | adjust_insn_aux_data(env, new_data, new_prog, off, len); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8440 | adjust_subprog_starts(env, off, len); |
| 8441 | return new_prog; |
| 8442 | } |
| 8443 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8444 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
| 8445 | u32 off, u32 cnt) |
| 8446 | { |
| 8447 | int i, j; |
| 8448 | |
| 8449 | /* find first prog starting at or after off (first to remove) */ |
| 8450 | for (i = 0; i < env->subprog_cnt; i++) |
| 8451 | if (env->subprog_info[i].start >= off) |
| 8452 | break; |
| 8453 | /* find first prog starting at or after off + cnt (first to stay) */ |
| 8454 | for (j = i; j < env->subprog_cnt; j++) |
| 8455 | if (env->subprog_info[j].start >= off + cnt) |
| 8456 | break; |
| 8457 | /* if j doesn't start exactly at off + cnt, we are just removing |
| 8458 | * the front of previous prog |
| 8459 | */ |
| 8460 | if (env->subprog_info[j].start != off + cnt) |
| 8461 | j--; |
| 8462 | |
| 8463 | if (j > i) { |
| 8464 | struct bpf_prog_aux *aux = env->prog->aux; |
| 8465 | int move; |
| 8466 | |
| 8467 | /* move fake 'exit' subprog as well */ |
| 8468 | move = env->subprog_cnt + 1 - j; |
| 8469 | |
| 8470 | memmove(env->subprog_info + i, |
| 8471 | env->subprog_info + j, |
| 8472 | sizeof(*env->subprog_info) * move); |
| 8473 | env->subprog_cnt -= j - i; |
| 8474 | |
| 8475 | /* remove func_info */ |
| 8476 | if (aux->func_info) { |
| 8477 | move = aux->func_info_cnt - j; |
| 8478 | |
| 8479 | memmove(aux->func_info + i, |
| 8480 | aux->func_info + j, |
| 8481 | sizeof(*aux->func_info) * move); |
| 8482 | aux->func_info_cnt -= j - i; |
| 8483 | /* func_info->insn_off is set after all code rewrites, |
| 8484 | * in adjust_btf_func() - no need to adjust |
| 8485 | */ |
| 8486 | } |
| 8487 | } else { |
| 8488 | /* convert i from "first prog to remove" to "first to adjust" */ |
| 8489 | if (env->subprog_info[i].start == off) |
| 8490 | i++; |
| 8491 | } |
| 8492 | |
| 8493 | /* update fake 'exit' subprog as well */ |
| 8494 | for (; i <= env->subprog_cnt; i++) |
| 8495 | env->subprog_info[i].start -= cnt; |
| 8496 | |
| 8497 | return 0; |
| 8498 | } |
| 8499 | |
| 8500 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, |
| 8501 | u32 cnt) |
| 8502 | { |
| 8503 | struct bpf_prog *prog = env->prog; |
| 8504 | u32 i, l_off, l_cnt, nr_linfo; |
| 8505 | struct bpf_line_info *linfo; |
| 8506 | |
| 8507 | nr_linfo = prog->aux->nr_linfo; |
| 8508 | if (!nr_linfo) |
| 8509 | return 0; |
| 8510 | |
| 8511 | linfo = prog->aux->linfo; |
| 8512 | |
| 8513 | /* find first line info to remove, count lines to be removed */ |
| 8514 | for (i = 0; i < nr_linfo; i++) |
| 8515 | if (linfo[i].insn_off >= off) |
| 8516 | break; |
| 8517 | |
| 8518 | l_off = i; |
| 8519 | l_cnt = 0; |
| 8520 | for (; i < nr_linfo; i++) |
| 8521 | if (linfo[i].insn_off < off + cnt) |
| 8522 | l_cnt++; |
| 8523 | else |
| 8524 | break; |
| 8525 | |
| 8526 | /* First live insn doesn't match first live linfo, it needs to "inherit" |
| 8527 | * last removed linfo. prog is already modified, so prog->len == off |
| 8528 | * means no live instructions after (tail of the program was removed). |
| 8529 | */ |
| 8530 | if (prog->len != off && l_cnt && |
| 8531 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { |
| 8532 | l_cnt--; |
| 8533 | linfo[--i].insn_off = off + cnt; |
| 8534 | } |
| 8535 | |
| 8536 | /* remove the line info which refer to the removed instructions */ |
| 8537 | if (l_cnt) { |
| 8538 | memmove(linfo + l_off, linfo + i, |
| 8539 | sizeof(*linfo) * (nr_linfo - i)); |
| 8540 | |
| 8541 | prog->aux->nr_linfo -= l_cnt; |
| 8542 | nr_linfo = prog->aux->nr_linfo; |
| 8543 | } |
| 8544 | |
| 8545 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ |
| 8546 | for (i = l_off; i < nr_linfo; i++) |
| 8547 | linfo[i].insn_off -= cnt; |
| 8548 | |
| 8549 | /* fix up all subprogs (incl. 'exit') which start >= off */ |
| 8550 | for (i = 0; i <= env->subprog_cnt; i++) |
| 8551 | if (env->subprog_info[i].linfo_idx > l_off) { |
| 8552 | /* program may have started in the removed region but |
| 8553 | * may not be fully removed |
| 8554 | */ |
| 8555 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) |
| 8556 | env->subprog_info[i].linfo_idx -= l_cnt; |
| 8557 | else |
| 8558 | env->subprog_info[i].linfo_idx = l_off; |
| 8559 | } |
| 8560 | |
| 8561 | return 0; |
| 8562 | } |
| 8563 | |
| 8564 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) |
| 8565 | { |
| 8566 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 8567 | unsigned int orig_prog_len = env->prog->len; |
| 8568 | int err; |
| 8569 | |
| 8570 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 8571 | bpf_prog_offload_remove_insns(env, off, cnt); |
| 8572 | |
| 8573 | err = bpf_remove_insns(env->prog, off, cnt); |
| 8574 | if (err) |
| 8575 | return err; |
| 8576 | |
| 8577 | err = adjust_subprog_starts_after_remove(env, off, cnt); |
| 8578 | if (err) |
| 8579 | return err; |
| 8580 | |
| 8581 | err = bpf_adj_linfo_after_remove(env, off, cnt); |
| 8582 | if (err) |
| 8583 | return err; |
| 8584 | |
| 8585 | memmove(aux_data + off, aux_data + off + cnt, |
| 8586 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); |
| 8587 | |
| 8588 | return 0; |
| 8589 | } |
| 8590 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8591 | /* The verifier does more data flow analysis than llvm and will not |
| 8592 | * explore branches that are dead at run time. Malicious programs can |
| 8593 | * have dead code too. Therefore replace all dead at-run-time code |
| 8594 | * with 'ja -1'. |
| 8595 | * |
| 8596 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 8597 | * program and through another bug we would manage to jump there, then |
| 8598 | * we'd execute beyond program memory otherwise. Returning exception |
| 8599 | * code also wouldn't work since we can have subprogs where the dead |
| 8600 | * code could be located. |
| 8601 | */ |
| 8602 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 8603 | { |
| 8604 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 8605 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
| 8606 | struct bpf_insn *insn = env->prog->insnsi; |
| 8607 | const int insn_cnt = env->prog->len; |
| 8608 | int i; |
| 8609 | |
| 8610 | for (i = 0; i < insn_cnt; i++) { |
| 8611 | if (aux_data[i].seen) |
| 8612 | continue; |
| 8613 | memcpy(insn + i, &trap, sizeof(trap)); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8614 | aux_data[i].zext_dst = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8615 | } |
| 8616 | } |
| 8617 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8618 | static bool insn_is_cond_jump(u8 code) |
| 8619 | { |
| 8620 | u8 op; |
| 8621 | |
| 8622 | if (BPF_CLASS(code) == BPF_JMP32) |
| 8623 | return true; |
| 8624 | |
| 8625 | if (BPF_CLASS(code) != BPF_JMP) |
| 8626 | return false; |
| 8627 | |
| 8628 | op = BPF_OP(code); |
| 8629 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; |
| 8630 | } |
| 8631 | |
| 8632 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) |
| 8633 | { |
| 8634 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 8635 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 8636 | struct bpf_insn *insn = env->prog->insnsi; |
| 8637 | const int insn_cnt = env->prog->len; |
| 8638 | int i; |
| 8639 | |
| 8640 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 8641 | if (!insn_is_cond_jump(insn->code)) |
| 8642 | continue; |
| 8643 | |
| 8644 | if (!aux_data[i + 1].seen) |
| 8645 | ja.off = insn->off; |
| 8646 | else if (!aux_data[i + 1 + insn->off].seen) |
| 8647 | ja.off = 0; |
| 8648 | else |
| 8649 | continue; |
| 8650 | |
| 8651 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 8652 | bpf_prog_offload_replace_insn(env, i, &ja); |
| 8653 | |
| 8654 | memcpy(insn, &ja, sizeof(ja)); |
| 8655 | } |
| 8656 | } |
| 8657 | |
| 8658 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
| 8659 | { |
| 8660 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 8661 | int insn_cnt = env->prog->len; |
| 8662 | int i, err; |
| 8663 | |
| 8664 | for (i = 0; i < insn_cnt; i++) { |
| 8665 | int j; |
| 8666 | |
| 8667 | j = 0; |
| 8668 | while (i + j < insn_cnt && !aux_data[i + j].seen) |
| 8669 | j++; |
| 8670 | if (!j) |
| 8671 | continue; |
| 8672 | |
| 8673 | err = verifier_remove_insns(env, i, j); |
| 8674 | if (err) |
| 8675 | return err; |
| 8676 | insn_cnt = env->prog->len; |
| 8677 | } |
| 8678 | |
| 8679 | return 0; |
| 8680 | } |
| 8681 | |
| 8682 | static int opt_remove_nops(struct bpf_verifier_env *env) |
| 8683 | { |
| 8684 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 8685 | struct bpf_insn *insn = env->prog->insnsi; |
| 8686 | int insn_cnt = env->prog->len; |
| 8687 | int i, err; |
| 8688 | |
| 8689 | for (i = 0; i < insn_cnt; i++) { |
| 8690 | if (memcmp(&insn[i], &ja, sizeof(ja))) |
| 8691 | continue; |
| 8692 | |
| 8693 | err = verifier_remove_insns(env, i, 1); |
| 8694 | if (err) |
| 8695 | return err; |
| 8696 | insn_cnt--; |
| 8697 | i--; |
| 8698 | } |
| 8699 | |
| 8700 | return 0; |
| 8701 | } |
| 8702 | |
| 8703 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
| 8704 | const union bpf_attr *attr) |
| 8705 | { |
| 8706 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
| 8707 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
| 8708 | int i, patch_len, delta = 0, len = env->prog->len; |
| 8709 | struct bpf_insn *insns = env->prog->insnsi; |
| 8710 | struct bpf_prog *new_prog; |
| 8711 | bool rnd_hi32; |
| 8712 | |
| 8713 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
| 8714 | zext_patch[1] = BPF_ZEXT_REG(0); |
| 8715 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
| 8716 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); |
| 8717 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); |
| 8718 | for (i = 0; i < len; i++) { |
| 8719 | int adj_idx = i + delta; |
| 8720 | struct bpf_insn insn; |
| 8721 | |
| 8722 | insn = insns[adj_idx]; |
| 8723 | if (!aux[adj_idx].zext_dst) { |
| 8724 | u8 code, class; |
| 8725 | u32 imm_rnd; |
| 8726 | |
| 8727 | if (!rnd_hi32) |
| 8728 | continue; |
| 8729 | |
| 8730 | code = insn.code; |
| 8731 | class = BPF_CLASS(code); |
| 8732 | if (insn_no_def(&insn)) |
| 8733 | continue; |
| 8734 | |
| 8735 | /* NOTE: arg "reg" (the fourth one) is only used for |
| 8736 | * BPF_STX which has been ruled out in above |
| 8737 | * check, it is safe to pass NULL here. |
| 8738 | */ |
| 8739 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { |
| 8740 | if (class == BPF_LD && |
| 8741 | BPF_MODE(code) == BPF_IMM) |
| 8742 | i++; |
| 8743 | continue; |
| 8744 | } |
| 8745 | |
| 8746 | /* ctx load could be transformed into wider load. */ |
| 8747 | if (class == BPF_LDX && |
| 8748 | aux[adj_idx].ptr_type == PTR_TO_CTX) |
| 8749 | continue; |
| 8750 | |
| 8751 | imm_rnd = get_random_int(); |
| 8752 | rnd_hi32_patch[0] = insn; |
| 8753 | rnd_hi32_patch[1].imm = imm_rnd; |
| 8754 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; |
| 8755 | patch = rnd_hi32_patch; |
| 8756 | patch_len = 4; |
| 8757 | goto apply_patch_buffer; |
| 8758 | } |
| 8759 | |
| 8760 | if (!bpf_jit_needs_zext()) |
| 8761 | continue; |
| 8762 | |
| 8763 | zext_patch[0] = insn; |
| 8764 | zext_patch[1].dst_reg = insn.dst_reg; |
| 8765 | zext_patch[1].src_reg = insn.dst_reg; |
| 8766 | patch = zext_patch; |
| 8767 | patch_len = 2; |
| 8768 | apply_patch_buffer: |
| 8769 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); |
| 8770 | if (!new_prog) |
| 8771 | return -ENOMEM; |
| 8772 | env->prog = new_prog; |
| 8773 | insns = new_prog->insnsi; |
| 8774 | aux = env->insn_aux_data; |
| 8775 | delta += patch_len - 1; |
| 8776 | } |
| 8777 | |
| 8778 | return 0; |
| 8779 | } |
| 8780 | |
| 8781 | /* convert load instructions that access fields of a context type into a |
| 8782 | * sequence of instructions that access fields of the underlying structure: |
| 8783 | * struct __sk_buff -> struct sk_buff |
| 8784 | * struct bpf_sock_ops -> struct sock |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8785 | */ |
| 8786 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
| 8787 | { |
| 8788 | const struct bpf_verifier_ops *ops = env->ops; |
| 8789 | int i, cnt, size, ctx_field_size, delta = 0; |
| 8790 | const int insn_cnt = env->prog->len; |
| 8791 | struct bpf_insn insn_buf[16], *insn; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8792 | u32 target_size, size_default, off; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8793 | struct bpf_prog *new_prog; |
| 8794 | enum bpf_access_type type; |
| 8795 | bool is_narrower_load; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8796 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8797 | if (ops->gen_prologue || env->seen_direct_write) { |
| 8798 | if (!ops->gen_prologue) { |
| 8799 | verbose(env, "bpf verifier is misconfigured\n"); |
| 8800 | return -EINVAL; |
| 8801 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8802 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 8803 | env->prog); |
| 8804 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
| 8805 | verbose(env, "bpf verifier is misconfigured\n"); |
| 8806 | return -EINVAL; |
| 8807 | } else if (cnt) { |
| 8808 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
| 8809 | if (!new_prog) |
| 8810 | return -ENOMEM; |
| 8811 | |
| 8812 | env->prog = new_prog; |
| 8813 | delta += cnt - 1; |
| 8814 | } |
| 8815 | } |
| 8816 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8817 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8818 | return 0; |
| 8819 | |
| 8820 | insn = env->prog->insnsi + delta; |
| 8821 | |
| 8822 | for (i = 0; i < insn_cnt; i++, insn++) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8823 | bpf_convert_ctx_access_t convert_ctx_access; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8824 | bool ctx_access; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8825 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8826 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 8827 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 8828 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8829 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8830 | type = BPF_READ; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8831 | ctx_access = true; |
| 8832 | } else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 8833 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 8834 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
| 8835 | insn->code == (BPF_STX | BPF_MEM | BPF_DW) || |
| 8836 | insn->code == (BPF_ST | BPF_MEM | BPF_B) || |
| 8837 | insn->code == (BPF_ST | BPF_MEM | BPF_H) || |
| 8838 | insn->code == (BPF_ST | BPF_MEM | BPF_W) || |
| 8839 | insn->code == (BPF_ST | BPF_MEM | BPF_DW)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8840 | type = BPF_WRITE; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8841 | ctx_access = BPF_CLASS(insn->code) == BPF_STX; |
| 8842 | } else { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8843 | continue; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8844 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8845 | |
| 8846 | if (type == BPF_WRITE && |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8847 | env->insn_aux_data[i + delta].sanitize_stack_spill) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8848 | struct bpf_insn patch[] = { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8849 | *insn, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8850 | BPF_ST_NOSPEC(), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8851 | }; |
| 8852 | |
| 8853 | cnt = ARRAY_SIZE(patch); |
| 8854 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 8855 | if (!new_prog) |
| 8856 | return -ENOMEM; |
| 8857 | |
| 8858 | delta += cnt - 1; |
| 8859 | env->prog = new_prog; |
| 8860 | insn = new_prog->insnsi + i + delta; |
| 8861 | continue; |
| 8862 | } |
| 8863 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8864 | if (!ctx_access) |
| 8865 | continue; |
| 8866 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8867 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 8868 | case PTR_TO_CTX: |
| 8869 | if (!ops->convert_ctx_access) |
| 8870 | continue; |
| 8871 | convert_ctx_access = ops->convert_ctx_access; |
| 8872 | break; |
| 8873 | case PTR_TO_SOCKET: |
| 8874 | case PTR_TO_SOCK_COMMON: |
| 8875 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 8876 | break; |
| 8877 | case PTR_TO_TCP_SOCK: |
| 8878 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; |
| 8879 | break; |
| 8880 | case PTR_TO_XDP_SOCK: |
| 8881 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; |
| 8882 | break; |
| 8883 | default: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8884 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8885 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8886 | |
| 8887 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
| 8888 | size = BPF_LDST_BYTES(insn); |
| 8889 | |
| 8890 | /* If the read access is a narrower load of the field, |
| 8891 | * convert to a 4/8-byte load, to minimum program type specific |
| 8892 | * convert_ctx_access changes. If conversion is successful, |
| 8893 | * we will apply proper mask to the result. |
| 8894 | */ |
| 8895 | is_narrower_load = size < ctx_field_size; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8896 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
| 8897 | off = insn->off; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8898 | if (is_narrower_load) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8899 | u8 size_code; |
| 8900 | |
| 8901 | if (type == BPF_WRITE) { |
| 8902 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
| 8903 | return -EINVAL; |
| 8904 | } |
| 8905 | |
| 8906 | size_code = BPF_H; |
| 8907 | if (ctx_field_size == 4) |
| 8908 | size_code = BPF_W; |
| 8909 | else if (ctx_field_size == 8) |
| 8910 | size_code = BPF_DW; |
| 8911 | |
| 8912 | insn->off = off & ~(size_default - 1); |
| 8913 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 8914 | } |
| 8915 | |
| 8916 | target_size = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8917 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 8918 | &target_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8919 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 8920 | (ctx_field_size && !target_size)) { |
| 8921 | verbose(env, "bpf verifier is misconfigured\n"); |
| 8922 | return -EINVAL; |
| 8923 | } |
| 8924 | |
| 8925 | if (is_narrower_load && size < target_size) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8926 | u8 shift = bpf_ctx_narrow_access_offset( |
| 8927 | off, size, size_default) * 8; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 8928 | if (shift && cnt + 1 >= ARRAY_SIZE(insn_buf)) { |
| 8929 | verbose(env, "bpf verifier narrow ctx load misconfigured\n"); |
| 8930 | return -EINVAL; |
| 8931 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8932 | if (ctx_field_size <= 4) { |
| 8933 | if (shift) |
| 8934 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, |
| 8935 | insn->dst_reg, |
| 8936 | shift); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8937 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
| 8938 | (1 << size * 8) - 1); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8939 | } else { |
| 8940 | if (shift) |
| 8941 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, |
| 8942 | insn->dst_reg, |
| 8943 | shift); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8944 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8945 | (1ULL << size * 8) - 1); |
| 8946 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8947 | } |
| 8948 | |
| 8949 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 8950 | if (!new_prog) |
| 8951 | return -ENOMEM; |
| 8952 | |
| 8953 | delta += cnt - 1; |
| 8954 | |
| 8955 | /* keep walking new program and skip insns we just inserted */ |
| 8956 | env->prog = new_prog; |
| 8957 | insn = new_prog->insnsi + i + delta; |
| 8958 | } |
| 8959 | |
| 8960 | return 0; |
| 8961 | } |
| 8962 | |
| 8963 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 8964 | { |
| 8965 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 8966 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
| 8967 | struct bpf_insn *insn; |
| 8968 | void *old_bpf_func; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 8969 | int err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8970 | |
| 8971 | if (env->subprog_cnt <= 1) |
| 8972 | return 0; |
| 8973 | |
| 8974 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 8975 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 8976 | insn->src_reg != BPF_PSEUDO_CALL) |
| 8977 | continue; |
| 8978 | /* Upon error here we cannot fall back to interpreter but |
| 8979 | * need a hard reject of the program. Thus -EFAULT is |
| 8980 | * propagated in any case. |
| 8981 | */ |
| 8982 | subprog = find_subprog(env, i + insn->imm + 1); |
| 8983 | if (subprog < 0) { |
| 8984 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 8985 | i + insn->imm + 1); |
| 8986 | return -EFAULT; |
| 8987 | } |
| 8988 | /* temporarily remember subprog id inside insn instead of |
| 8989 | * aux_data, since next loop will split up all insns into funcs |
| 8990 | */ |
| 8991 | insn->off = subprog; |
| 8992 | /* remember original imm in case JIT fails and fallback |
| 8993 | * to interpreter will be needed |
| 8994 | */ |
| 8995 | env->insn_aux_data[i].call_imm = insn->imm; |
| 8996 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 8997 | insn->imm = 1; |
| 8998 | } |
| 8999 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9000 | err = bpf_prog_alloc_jited_linfo(prog); |
| 9001 | if (err) |
| 9002 | goto out_undo_insn; |
| 9003 | |
| 9004 | err = -ENOMEM; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9005 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
| 9006 | if (!func) |
| 9007 | goto out_undo_insn; |
| 9008 | |
| 9009 | for (i = 0; i < env->subprog_cnt; i++) { |
| 9010 | subprog_start = subprog_end; |
| 9011 | subprog_end = env->subprog_info[i + 1].start; |
| 9012 | |
| 9013 | len = subprog_end - subprog_start; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9014 | /* BPF_PROG_RUN doesn't call subprogs directly, |
| 9015 | * hence main prog stats include the runtime of subprogs. |
| 9016 | * subprogs don't have IDs and not reachable via prog_get_next_id |
| 9017 | * func[i]->aux->stats will never be accessed and stays NULL |
| 9018 | */ |
| 9019 | 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] | 9020 | if (!func[i]) |
| 9021 | goto out_free; |
| 9022 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 9023 | len * sizeof(struct bpf_insn)); |
| 9024 | func[i]->type = prog->type; |
| 9025 | func[i]->len = len; |
| 9026 | if (bpf_prog_calc_tag(func[i])) |
| 9027 | goto out_free; |
| 9028 | func[i]->is_func = 1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9029 | func[i]->aux->func_idx = i; |
| 9030 | /* the btf and func_info will be freed only at prog->aux */ |
| 9031 | func[i]->aux->btf = prog->aux->btf; |
| 9032 | func[i]->aux->func_info = prog->aux->func_info; |
| 9033 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9034 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 9035 | * Long term would need debug info to populate names |
| 9036 | */ |
| 9037 | func[i]->aux->name[0] = 'F'; |
| 9038 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
| 9039 | func[i]->jit_requested = 1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9040 | func[i]->aux->linfo = prog->aux->linfo; |
| 9041 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; |
| 9042 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; |
| 9043 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9044 | func[i] = bpf_int_jit_compile(func[i]); |
| 9045 | if (!func[i]->jited) { |
| 9046 | err = -ENOTSUPP; |
| 9047 | goto out_free; |
| 9048 | } |
| 9049 | cond_resched(); |
| 9050 | } |
| 9051 | /* at this point all bpf functions were successfully JITed |
| 9052 | * now populate all bpf_calls with correct addresses and |
| 9053 | * run last pass of JIT |
| 9054 | */ |
| 9055 | for (i = 0; i < env->subprog_cnt; i++) { |
| 9056 | insn = func[i]->insnsi; |
| 9057 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 9058 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 9059 | insn->src_reg != BPF_PSEUDO_CALL) |
| 9060 | continue; |
| 9061 | subprog = insn->off; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9062 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
| 9063 | __bpf_call_base; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9064 | } |
| 9065 | |
| 9066 | /* we use the aux data to keep a list of the start addresses |
| 9067 | * of the JITed images for each function in the program |
| 9068 | * |
| 9069 | * for some architectures, such as powerpc64, the imm field |
| 9070 | * might not be large enough to hold the offset of the start |
| 9071 | * address of the callee's JITed image from __bpf_call_base |
| 9072 | * |
| 9073 | * in such cases, we can lookup the start address of a callee |
| 9074 | * by using its subprog id, available from the off field of |
| 9075 | * the call instruction, as an index for this list |
| 9076 | */ |
| 9077 | func[i]->aux->func = func; |
| 9078 | func[i]->aux->func_cnt = env->subprog_cnt; |
| 9079 | } |
| 9080 | for (i = 0; i < env->subprog_cnt; i++) { |
| 9081 | old_bpf_func = func[i]->bpf_func; |
| 9082 | tmp = bpf_int_jit_compile(func[i]); |
| 9083 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 9084 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
| 9085 | err = -ENOTSUPP; |
| 9086 | goto out_free; |
| 9087 | } |
| 9088 | cond_resched(); |
| 9089 | } |
| 9090 | |
| 9091 | /* finally lock prog and jit images for all functions and |
| 9092 | * populate kallsysm |
| 9093 | */ |
| 9094 | for (i = 0; i < env->subprog_cnt; i++) { |
| 9095 | bpf_prog_lock_ro(func[i]); |
| 9096 | bpf_prog_kallsyms_add(func[i]); |
| 9097 | } |
| 9098 | |
| 9099 | /* Last step: make now unused interpreter insns from main |
| 9100 | * prog consistent for later dump requests, so they can |
| 9101 | * later look the same as if they were interpreted only. |
| 9102 | */ |
| 9103 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 9104 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 9105 | insn->src_reg != BPF_PSEUDO_CALL) |
| 9106 | continue; |
| 9107 | insn->off = env->insn_aux_data[i].call_imm; |
| 9108 | subprog = find_subprog(env, i + insn->off + 1); |
| 9109 | insn->imm = subprog; |
| 9110 | } |
| 9111 | |
| 9112 | prog->jited = 1; |
| 9113 | prog->bpf_func = func[0]->bpf_func; |
| 9114 | prog->aux->func = func; |
| 9115 | prog->aux->func_cnt = env->subprog_cnt; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9116 | bpf_prog_free_unused_jited_linfo(prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9117 | return 0; |
| 9118 | out_free: |
| 9119 | for (i = 0; i < env->subprog_cnt; i++) |
| 9120 | if (func[i]) |
| 9121 | bpf_jit_free(func[i]); |
| 9122 | kfree(func); |
| 9123 | out_undo_insn: |
| 9124 | /* cleanup main prog to be interpreted */ |
| 9125 | prog->jit_requested = 0; |
| 9126 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 9127 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 9128 | insn->src_reg != BPF_PSEUDO_CALL) |
| 9129 | continue; |
| 9130 | insn->off = 0; |
| 9131 | insn->imm = env->insn_aux_data[i].call_imm; |
| 9132 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9133 | bpf_prog_free_jited_linfo(prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9134 | return err; |
| 9135 | } |
| 9136 | |
| 9137 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 9138 | { |
| 9139 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
| 9140 | struct bpf_prog *prog = env->prog; |
| 9141 | struct bpf_insn *insn = prog->insnsi; |
| 9142 | int i, depth; |
| 9143 | #endif |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9144 | int err = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9145 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9146 | if (env->prog->jit_requested && |
| 9147 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9148 | err = jit_subprogs(env); |
| 9149 | if (err == 0) |
| 9150 | return 0; |
| 9151 | if (err == -EFAULT) |
| 9152 | return err; |
| 9153 | } |
| 9154 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
| 9155 | for (i = 0; i < prog->len; i++, insn++) { |
| 9156 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 9157 | insn->src_reg != BPF_PSEUDO_CALL) |
| 9158 | continue; |
| 9159 | depth = get_callee_stack_depth(env, insn, i); |
| 9160 | if (depth < 0) |
| 9161 | return depth; |
| 9162 | bpf_patch_call_args(insn, depth); |
| 9163 | } |
| 9164 | err = 0; |
| 9165 | #endif |
| 9166 | return err; |
| 9167 | } |
| 9168 | |
| 9169 | /* fixup insn->imm field of bpf_call instructions |
| 9170 | * and inline eligible helpers as explicit sequence of BPF instructions |
| 9171 | * |
| 9172 | * this function is called after eBPF program passed verification |
| 9173 | */ |
| 9174 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
| 9175 | { |
| 9176 | struct bpf_prog *prog = env->prog; |
| 9177 | struct bpf_insn *insn = prog->insnsi; |
| 9178 | const struct bpf_func_proto *fn; |
| 9179 | const int insn_cnt = prog->len; |
| 9180 | const struct bpf_map_ops *ops; |
| 9181 | struct bpf_insn_aux_data *aux; |
| 9182 | struct bpf_insn insn_buf[16]; |
| 9183 | struct bpf_prog *new_prog; |
| 9184 | struct bpf_map *map_ptr; |
| 9185 | int i, cnt, delta = 0; |
| 9186 | |
| 9187 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 9188 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 9189 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 9190 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
| 9191 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 9192 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 9193 | bool isdiv = BPF_OP(insn->code) == BPF_DIV; |
| 9194 | struct bpf_insn *patchlet; |
| 9195 | struct bpf_insn chk_and_div[] = { |
| 9196 | /* [R,W]x div 0 -> 0 */ |
| 9197 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
| 9198 | BPF_JNE | BPF_K, insn->src_reg, |
| 9199 | 0, 2, 0), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9200 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 9201 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 9202 | *insn, |
| 9203 | }; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 9204 | struct bpf_insn chk_and_mod[] = { |
| 9205 | /* [R,W]x mod 0 -> [R,W]x */ |
| 9206 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
| 9207 | BPF_JEQ | BPF_K, insn->src_reg, |
| 9208 | 0, 1 + (is64 ? 0 : 1), 0), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9209 | *insn, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 9210 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 9211 | BPF_MOV32_REG(insn->dst_reg, insn->dst_reg), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9212 | }; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9213 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 9214 | patchlet = isdiv ? chk_and_div : chk_and_mod; |
| 9215 | cnt = isdiv ? ARRAY_SIZE(chk_and_div) : |
| 9216 | ARRAY_SIZE(chk_and_mod) - (is64 ? 2 : 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9217 | |
| 9218 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
| 9219 | if (!new_prog) |
| 9220 | return -ENOMEM; |
| 9221 | |
| 9222 | delta += cnt - 1; |
| 9223 | env->prog = prog = new_prog; |
| 9224 | insn = new_prog->insnsi + i + delta; |
| 9225 | continue; |
| 9226 | } |
| 9227 | |
| 9228 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 9229 | (BPF_MODE(insn->code) == BPF_ABS || |
| 9230 | BPF_MODE(insn->code) == BPF_IND)) { |
| 9231 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 9232 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 9233 | verbose(env, "bpf verifier is misconfigured\n"); |
| 9234 | return -EINVAL; |
| 9235 | } |
| 9236 | |
| 9237 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 9238 | if (!new_prog) |
| 9239 | return -ENOMEM; |
| 9240 | |
| 9241 | delta += cnt - 1; |
| 9242 | env->prog = prog = new_prog; |
| 9243 | insn = new_prog->insnsi + i + delta; |
| 9244 | continue; |
| 9245 | } |
| 9246 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9247 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
| 9248 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { |
| 9249 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; |
| 9250 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; |
| 9251 | struct bpf_insn insn_buf[16]; |
| 9252 | struct bpf_insn *patch = &insn_buf[0]; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 9253 | bool issrc, isneg, isimm; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9254 | u32 off_reg; |
| 9255 | |
| 9256 | aux = &env->insn_aux_data[i + delta]; |
| 9257 | if (!aux->alu_state || |
| 9258 | aux->alu_state == BPF_ALU_NON_POINTER) |
| 9259 | continue; |
| 9260 | |
| 9261 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; |
| 9262 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == |
| 9263 | BPF_ALU_SANITIZE_SRC; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 9264 | isimm = aux->alu_state & BPF_ALU_IMMEDIATE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9265 | |
| 9266 | off_reg = issrc ? insn->src_reg : insn->dst_reg; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 9267 | if (isimm) { |
| 9268 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9269 | } else { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 9270 | if (isneg) |
| 9271 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 9272 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); |
| 9273 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); |
| 9274 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); |
| 9275 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); |
| 9276 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); |
| 9277 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9278 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 9279 | if (!issrc) |
| 9280 | *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); |
| 9281 | insn->src_reg = BPF_REG_AX; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9282 | if (isneg) |
| 9283 | insn->code = insn->code == code_add ? |
| 9284 | code_sub : code_add; |
| 9285 | *patch++ = *insn; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 9286 | if (issrc && isneg && !isimm) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9287 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 9288 | cnt = patch - insn_buf; |
| 9289 | |
| 9290 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 9291 | if (!new_prog) |
| 9292 | return -ENOMEM; |
| 9293 | |
| 9294 | delta += cnt - 1; |
| 9295 | env->prog = prog = new_prog; |
| 9296 | insn = new_prog->insnsi + i + delta; |
| 9297 | continue; |
| 9298 | } |
| 9299 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9300 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 9301 | continue; |
| 9302 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 9303 | continue; |
| 9304 | |
| 9305 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 9306 | prog->dst_needed = 1; |
| 9307 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 9308 | bpf_user_rnd_init_once(); |
| 9309 | if (insn->imm == BPF_FUNC_override_return) |
| 9310 | prog->kprobe_override = 1; |
| 9311 | if (insn->imm == BPF_FUNC_tail_call) { |
| 9312 | /* If we tail call into other programs, we |
| 9313 | * cannot make any assumptions since they can |
| 9314 | * be replaced dynamically during runtime in |
| 9315 | * the program array. |
| 9316 | */ |
| 9317 | prog->cb_access = 1; |
| 9318 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9319 | env->prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9320 | |
| 9321 | /* mark bpf_tail_call as different opcode to avoid |
| 9322 | * conditional branch in the interpeter for every normal |
| 9323 | * call and to prevent accidental JITing by JIT compiler |
| 9324 | * that doesn't support bpf_tail_call yet |
| 9325 | */ |
| 9326 | insn->imm = 0; |
| 9327 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
| 9328 | |
| 9329 | aux = &env->insn_aux_data[i + delta]; |
| 9330 | if (!bpf_map_ptr_unpriv(aux)) |
| 9331 | continue; |
| 9332 | |
| 9333 | /* instead of changing every JIT dealing with tail_call |
| 9334 | * emit two extra insns: |
| 9335 | * if (index >= max_entries) goto out; |
| 9336 | * index &= array->index_mask; |
| 9337 | * to avoid out-of-bounds cpu speculation |
| 9338 | */ |
| 9339 | if (bpf_map_ptr_poisoned(aux)) { |
| 9340 | verbose(env, "tail_call abusing map_ptr\n"); |
| 9341 | return -EINVAL; |
| 9342 | } |
| 9343 | |
| 9344 | map_ptr = BPF_MAP_PTR(aux->map_state); |
| 9345 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 9346 | map_ptr->max_entries, 2); |
| 9347 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 9348 | container_of(map_ptr, |
| 9349 | struct bpf_array, |
| 9350 | map)->index_mask); |
| 9351 | insn_buf[2] = *insn; |
| 9352 | cnt = 3; |
| 9353 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 9354 | if (!new_prog) |
| 9355 | return -ENOMEM; |
| 9356 | |
| 9357 | delta += cnt - 1; |
| 9358 | env->prog = prog = new_prog; |
| 9359 | insn = new_prog->insnsi + i + delta; |
| 9360 | continue; |
| 9361 | } |
| 9362 | |
| 9363 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
| 9364 | * and other inlining handlers are currently limited to 64 bit |
| 9365 | * only. |
| 9366 | */ |
| 9367 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
| 9368 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 9369 | insn->imm == BPF_FUNC_map_update_elem || |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9370 | insn->imm == BPF_FUNC_map_delete_elem || |
| 9371 | insn->imm == BPF_FUNC_map_push_elem || |
| 9372 | insn->imm == BPF_FUNC_map_pop_elem || |
| 9373 | insn->imm == BPF_FUNC_map_peek_elem)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9374 | aux = &env->insn_aux_data[i + delta]; |
| 9375 | if (bpf_map_ptr_poisoned(aux)) |
| 9376 | goto patch_call_imm; |
| 9377 | |
| 9378 | map_ptr = BPF_MAP_PTR(aux->map_state); |
| 9379 | ops = map_ptr->ops; |
| 9380 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 9381 | ops->map_gen_lookup) { |
| 9382 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
| 9383 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 9384 | verbose(env, "bpf verifier is misconfigured\n"); |
| 9385 | return -EINVAL; |
| 9386 | } |
| 9387 | |
| 9388 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 9389 | insn_buf, cnt); |
| 9390 | if (!new_prog) |
| 9391 | return -ENOMEM; |
| 9392 | |
| 9393 | delta += cnt - 1; |
| 9394 | env->prog = prog = new_prog; |
| 9395 | insn = new_prog->insnsi + i + delta; |
| 9396 | continue; |
| 9397 | } |
| 9398 | |
| 9399 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 9400 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 9401 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 9402 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 9403 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 9404 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 9405 | u64 flags))NULL)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9406 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 9407 | (int (*)(struct bpf_map *map, void *value, |
| 9408 | u64 flags))NULL)); |
| 9409 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 9410 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 9411 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 9412 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 9413 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9414 | switch (insn->imm) { |
| 9415 | case BPF_FUNC_map_lookup_elem: |
| 9416 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - |
| 9417 | __bpf_call_base; |
| 9418 | continue; |
| 9419 | case BPF_FUNC_map_update_elem: |
| 9420 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - |
| 9421 | __bpf_call_base; |
| 9422 | continue; |
| 9423 | case BPF_FUNC_map_delete_elem: |
| 9424 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - |
| 9425 | __bpf_call_base; |
| 9426 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9427 | case BPF_FUNC_map_push_elem: |
| 9428 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - |
| 9429 | __bpf_call_base; |
| 9430 | continue; |
| 9431 | case BPF_FUNC_map_pop_elem: |
| 9432 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - |
| 9433 | __bpf_call_base; |
| 9434 | continue; |
| 9435 | case BPF_FUNC_map_peek_elem: |
| 9436 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - |
| 9437 | __bpf_call_base; |
| 9438 | continue; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9439 | } |
| 9440 | |
| 9441 | goto patch_call_imm; |
| 9442 | } |
| 9443 | |
| 9444 | patch_call_imm: |
| 9445 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
| 9446 | /* all functions that have prototype and verifier allowed |
| 9447 | * programs to call them, must be real in-kernel functions |
| 9448 | */ |
| 9449 | if (!fn->func) { |
| 9450 | verbose(env, |
| 9451 | "kernel subsystem misconfigured func %s#%d\n", |
| 9452 | func_id_name(insn->imm), insn->imm); |
| 9453 | return -EFAULT; |
| 9454 | } |
| 9455 | insn->imm = fn->func - __bpf_call_base; |
| 9456 | } |
| 9457 | |
| 9458 | return 0; |
| 9459 | } |
| 9460 | |
| 9461 | static void free_states(struct bpf_verifier_env *env) |
| 9462 | { |
| 9463 | struct bpf_verifier_state_list *sl, *sln; |
| 9464 | int i; |
| 9465 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9466 | sl = env->free_list; |
| 9467 | while (sl) { |
| 9468 | sln = sl->next; |
| 9469 | free_verifier_state(&sl->state, false); |
| 9470 | kfree(sl); |
| 9471 | sl = sln; |
| 9472 | } |
| 9473 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9474 | if (!env->explored_states) |
| 9475 | return; |
| 9476 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9477 | for (i = 0; i < state_htab_size(env); i++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9478 | sl = env->explored_states[i]; |
| 9479 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9480 | while (sl) { |
| 9481 | sln = sl->next; |
| 9482 | free_verifier_state(&sl->state, false); |
| 9483 | kfree(sl); |
| 9484 | sl = sln; |
| 9485 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9486 | } |
| 9487 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9488 | kvfree(env->explored_states); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9489 | } |
| 9490 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9491 | static void print_verification_stats(struct bpf_verifier_env *env) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9492 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9493 | int i; |
| 9494 | |
| 9495 | if (env->log.level & BPF_LOG_STATS) { |
| 9496 | verbose(env, "verification time %lld usec\n", |
| 9497 | div_u64(env->verification_time, 1000)); |
| 9498 | verbose(env, "stack depth "); |
| 9499 | for (i = 0; i < env->subprog_cnt; i++) { |
| 9500 | u32 depth = env->subprog_info[i].stack_depth; |
| 9501 | |
| 9502 | verbose(env, "%d", depth); |
| 9503 | if (i + 1 < env->subprog_cnt) |
| 9504 | verbose(env, "+"); |
| 9505 | } |
| 9506 | verbose(env, "\n"); |
| 9507 | } |
| 9508 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " |
| 9509 | "total_states %d peak_states %d mark_read %d\n", |
| 9510 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, |
| 9511 | env->max_states_per_insn, env->total_states, |
| 9512 | env->peak_states, env->longest_mark_read_walk); |
| 9513 | } |
| 9514 | |
| 9515 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
| 9516 | union bpf_attr __user *uattr) |
| 9517 | { |
| 9518 | u64 start_time = ktime_get_ns(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9519 | struct bpf_verifier_env *env; |
| 9520 | struct bpf_verifier_log *log; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9521 | int i, len, ret = -EINVAL; |
| 9522 | bool is_priv; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9523 | |
| 9524 | /* no program is valid */ |
| 9525 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 9526 | return -EINVAL; |
| 9527 | |
| 9528 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
| 9529 | * allocate/free it every time bpf_check() is called |
| 9530 | */ |
| 9531 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
| 9532 | if (!env) |
| 9533 | return -ENOMEM; |
| 9534 | log = &env->log; |
| 9535 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9536 | len = (*prog)->len; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9537 | env->insn_aux_data = |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9538 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9539 | ret = -ENOMEM; |
| 9540 | if (!env->insn_aux_data) |
| 9541 | goto err_free_env; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9542 | for (i = 0; i < len; i++) |
| 9543 | env->insn_aux_data[i].orig_idx = i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9544 | env->prog = *prog; |
| 9545 | env->ops = bpf_verifier_ops[env->prog->type]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9546 | is_priv = capable(CAP_SYS_ADMIN); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9547 | |
| 9548 | /* grab the mutex to protect few globals used by verifier */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9549 | if (!is_priv) |
| 9550 | mutex_lock(&bpf_verifier_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9551 | |
| 9552 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 9553 | /* user requested verbose verifier output |
| 9554 | * and supplied buffer to store the verification trace |
| 9555 | */ |
| 9556 | log->level = attr->log_level; |
| 9557 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 9558 | log->len_total = attr->log_size; |
| 9559 | |
| 9560 | ret = -EINVAL; |
| 9561 | /* log attributes have to be sane */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9562 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
| 9563 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9564 | goto err_unlock; |
| 9565 | } |
| 9566 | |
| 9567 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 9568 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
| 9569 | env->strict_alignment = true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9570 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
| 9571 | env->strict_alignment = false; |
| 9572 | |
| 9573 | env->allow_ptr_leaks = is_priv; |
| 9574 | |
| 9575 | if (is_priv) |
| 9576 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9577 | |
| 9578 | ret = replace_map_fd_with_map_ptr(env); |
| 9579 | if (ret < 0) |
| 9580 | goto skip_full_check; |
| 9581 | |
| 9582 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9583 | ret = bpf_prog_offload_verifier_prep(env->prog); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9584 | if (ret) |
| 9585 | goto skip_full_check; |
| 9586 | } |
| 9587 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9588 | env->explored_states = kvcalloc(state_htab_size(env), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9589 | sizeof(struct bpf_verifier_state_list *), |
| 9590 | GFP_USER); |
| 9591 | ret = -ENOMEM; |
| 9592 | if (!env->explored_states) |
| 9593 | goto skip_full_check; |
| 9594 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9595 | ret = check_subprogs(env); |
| 9596 | if (ret < 0) |
| 9597 | goto skip_full_check; |
| 9598 | |
| 9599 | ret = check_btf_info(env, attr, uattr); |
| 9600 | if (ret < 0) |
| 9601 | goto skip_full_check; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9602 | |
| 9603 | ret = check_cfg(env); |
| 9604 | if (ret < 0) |
| 9605 | goto skip_full_check; |
| 9606 | |
| 9607 | ret = do_check(env); |
| 9608 | if (env->cur_state) { |
| 9609 | free_verifier_state(env->cur_state, true); |
| 9610 | env->cur_state = NULL; |
| 9611 | } |
| 9612 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9613 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 9614 | ret = bpf_prog_offload_finalize(env); |
| 9615 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9616 | skip_full_check: |
| 9617 | while (!pop_stack(env, NULL, NULL)); |
| 9618 | free_states(env); |
| 9619 | |
| 9620 | if (ret == 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9621 | ret = check_max_stack_depth(env); |
| 9622 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9623 | /* instruction rewrites happen after this point */ |
| 9624 | if (is_priv) { |
| 9625 | if (ret == 0) |
| 9626 | opt_hard_wire_dead_code_branches(env); |
| 9627 | if (ret == 0) |
| 9628 | ret = opt_remove_dead_code(env); |
| 9629 | if (ret == 0) |
| 9630 | ret = opt_remove_nops(env); |
| 9631 | } else { |
| 9632 | if (ret == 0) |
| 9633 | sanitize_dead_code(env); |
| 9634 | } |
| 9635 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9636 | if (ret == 0) |
| 9637 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 9638 | ret = convert_ctx_accesses(env); |
| 9639 | |
| 9640 | if (ret == 0) |
| 9641 | ret = fixup_bpf_calls(env); |
| 9642 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9643 | /* do 32-bit optimization after insn patching has done so those patched |
| 9644 | * insns could be handled correctly. |
| 9645 | */ |
| 9646 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
| 9647 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); |
| 9648 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret |
| 9649 | : false; |
| 9650 | } |
| 9651 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9652 | if (ret == 0) |
| 9653 | ret = fixup_call_args(env); |
| 9654 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9655 | env->verification_time = ktime_get_ns() - start_time; |
| 9656 | print_verification_stats(env); |
| 9657 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9658 | if (log->level && bpf_verifier_log_full(log)) |
| 9659 | ret = -ENOSPC; |
| 9660 | if (log->level && !log->ubuf) { |
| 9661 | ret = -EFAULT; |
| 9662 | goto err_release_maps; |
| 9663 | } |
| 9664 | |
| 9665 | if (ret == 0 && env->used_map_cnt) { |
| 9666 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
| 9667 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 9668 | sizeof(env->used_maps[0]), |
| 9669 | GFP_KERNEL); |
| 9670 | |
| 9671 | if (!env->prog->aux->used_maps) { |
| 9672 | ret = -ENOMEM; |
| 9673 | goto err_release_maps; |
| 9674 | } |
| 9675 | |
| 9676 | memcpy(env->prog->aux->used_maps, env->used_maps, |
| 9677 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
| 9678 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
| 9679 | |
| 9680 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 9681 | * bpf_ld_imm64 instructions |
| 9682 | */ |
| 9683 | convert_pseudo_ld_imm64(env); |
| 9684 | } |
| 9685 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9686 | if (ret == 0) |
| 9687 | adjust_btf_func(env); |
| 9688 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9689 | err_release_maps: |
| 9690 | if (!env->prog->aux->used_maps) |
| 9691 | /* if we didn't copy map pointers into bpf_prog_info, release |
| 9692 | * them now. Otherwise free_used_maps() will release them. |
| 9693 | */ |
| 9694 | release_maps(env); |
| 9695 | *prog = env->prog; |
| 9696 | err_unlock: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9697 | if (!is_priv) |
| 9698 | mutex_unlock(&bpf_verifier_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9699 | vfree(env->insn_aux_data); |
| 9700 | err_free_env: |
| 9701 | kfree(env); |
| 9702 | return ret; |
| 9703 | } |