Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016 Facebook |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4 | * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5 | */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6 | #include <uapi/linux/btf.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/types.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/bpf.h> |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 11 | #include <linux/btf.h> |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 12 | #include <linux/bpf_verifier.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 13 | #include <linux/filter.h> |
| 14 | #include <net/netlink.h> |
| 15 | #include <linux/file.h> |
| 16 | #include <linux/vmalloc.h> |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 17 | #include <linux/stringify.h> |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 18 | #include <linux/bsearch.h> |
| 19 | #include <linux/sort.h> |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 20 | #include <linux/perf_event.h> |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 21 | #include <linux/ctype.h> |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 22 | #include <linux/error-injection.h> |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 23 | #include <linux/bpf_lsm.h> |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 24 | #include <linux/btf_ids.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 25 | |
Jakub Kicinski | f4ac7e0 | 2017-10-09 10:30:12 -0700 | [diff] [blame] | 26 | #include "disasm.h" |
| 27 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 28 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
Alexei Starovoitov | 91cc1a9 | 2019-11-14 10:57:15 -0800 | [diff] [blame] | 29 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 30 | [_id] = & _name ## _verifier_ops, |
| 31 | #define BPF_MAP_TYPE(_id, _ops) |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 32 | #define BPF_LINK_TYPE(_id, _name) |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 33 | #include <linux/bpf_types.h> |
| 34 | #undef BPF_PROG_TYPE |
| 35 | #undef BPF_MAP_TYPE |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 36 | #undef BPF_LINK_TYPE |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 39 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 40 | * instruction by instruction and updates register/stack state. |
| 41 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 42 | * |
| 43 | * The first pass is depth-first-search to check that the program is a DAG. |
| 44 | * It rejects the following programs: |
| 45 | * - larger than BPF_MAXINSNS insns |
| 46 | * - if loop is present (detected via back-edge) |
| 47 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 48 | * - out of bounds or malformed jumps |
| 49 | * The second pass is all possible path descent from the 1st insn. |
| 50 | * Since it's analyzing all pathes through the program, the length of the |
Gary Lin | eba38a9 | 2017-03-01 16:25:51 +0800 | [diff] [blame] | 51 | * analysis is limited to 64k insn, which may be hit even if total number of |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 52 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 53 | * Number of 'branches to be analyzed' is limited to 1k |
| 54 | * |
| 55 | * On entry to each instruction, each register has a type, and the instruction |
| 56 | * changes the types of the registers depending on instruction semantics. |
| 57 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 58 | * copied to R1. |
| 59 | * |
| 60 | * All registers are 64-bit. |
| 61 | * R0 - return register |
| 62 | * R1-R5 argument passing registers |
| 63 | * R6-R9 callee saved registers |
| 64 | * R10 - frame pointer read-only |
| 65 | * |
| 66 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 67 | * and has type PTR_TO_CTX. |
| 68 | * |
| 69 | * Verifier tracks arithmetic operations on pointers in case: |
| 70 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 71 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 72 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 73 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 74 | * that it wants to construct a pointer to some element within stack. |
| 75 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 76 | * (and -20 constant is saved for further stack bounds checking). |
| 77 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 78 | * |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 79 | * Most of the time the registers have SCALAR_VALUE type, which |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 80 | * means the register has some value, but it's not a valid pointer. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 81 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 82 | * |
| 83 | * When verifier sees load or store instructions the type of base register |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 84 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
| 85 | * four pointer types recognized by check_mem_access() function. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 86 | * |
| 87 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 88 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 89 | * |
| 90 | * registers used to pass values to function calls are checked against |
| 91 | * function argument constraints. |
| 92 | * |
| 93 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 94 | * It means that the register type passed to this function must be |
| 95 | * PTR_TO_STACK and it will be used inside the function as |
| 96 | * 'pointer to map element key' |
| 97 | * |
| 98 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 99 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 100 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 101 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 102 | * |
| 103 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 104 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 105 | * 2nd argument should be a pointer to stack, which will be used inside |
| 106 | * the helper function as a pointer to map element key. |
| 107 | * |
| 108 | * On the kernel side the helper function looks like: |
| 109 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 110 | * { |
| 111 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 112 | * void *key = (void *) (unsigned long) r2; |
| 113 | * void *value; |
| 114 | * |
| 115 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 116 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 117 | * the stack of eBPF program. |
| 118 | * } |
| 119 | * |
| 120 | * Corresponding eBPF program may look like: |
| 121 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 122 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 123 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 124 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 125 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 126 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 127 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 128 | * |
| 129 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 130 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 131 | * and were initialized prior to this call. |
| 132 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 133 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 134 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 135 | * returns ether pointer to map value or NULL. |
| 136 | * |
| 137 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 138 | * insn, the register holding that pointer in the true branch changes state to |
| 139 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 140 | * branch. See check_cond_jmp_op(). |
| 141 | * |
| 142 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 143 | * are set to NOT_INIT to indicate that they are no longer readable. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 144 | * |
| 145 | * The following reference types represent a potential reference to a kernel |
| 146 | * resource which, after first being allocated, must be checked and freed by |
| 147 | * the BPF program: |
| 148 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET |
| 149 | * |
| 150 | * When the verifier sees a helper call return a reference type, it allocates a |
| 151 | * pointer id for the reference and stores it in the current function state. |
| 152 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into |
| 153 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type |
| 154 | * passes through a NULL-check conditional. For the branch wherein the state is |
| 155 | * changed to CONST_IMM, the verifier releases the reference. |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 156 | * |
| 157 | * For each helper function that allocates a reference, such as |
| 158 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as |
| 159 | * bpf_sk_release(). When a reference type passes into the release function, |
| 160 | * the verifier also releases the reference. If any unchecked or unreleased |
| 161 | * reference remains at the end of the program, the verifier rejects it. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 162 | */ |
| 163 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 164 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 165 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 166 | /* verifer state is 'st' |
| 167 | * before processing instruction 'insn_idx' |
| 168 | * and after processing instruction 'prev_insn_idx' |
| 169 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 170 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 171 | int insn_idx; |
| 172 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 173 | struct bpf_verifier_stack_elem *next; |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 174 | /* length of verifier log at the time this state was pushed on stack */ |
| 175 | u32 log_pos; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 178 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 179 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 180 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 181 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
| 182 | #define BPF_MAP_KEY_SEEN (1ULL << 62) |
| 183 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 184 | #define BPF_MAP_PTR_UNPRIV 1UL |
| 185 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ |
| 186 | POISON_POINTER_DELTA)) |
| 187 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) |
| 188 | |
| 189 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
| 190 | { |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 191 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) |
| 195 | { |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 196 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, |
| 200 | const struct bpf_map *map, bool unpriv) |
| 201 | { |
| 202 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); |
| 203 | unpriv |= bpf_map_ptr_unpriv(aux); |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 204 | aux->map_ptr_state = (unsigned long)map | |
| 205 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); |
| 206 | } |
| 207 | |
| 208 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) |
| 209 | { |
| 210 | return aux->map_key_state & BPF_MAP_KEY_POISON; |
| 211 | } |
| 212 | |
| 213 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) |
| 214 | { |
| 215 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); |
| 216 | } |
| 217 | |
| 218 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) |
| 219 | { |
| 220 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); |
| 221 | } |
| 222 | |
| 223 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) |
| 224 | { |
| 225 | bool poisoned = bpf_map_key_poisoned(aux); |
| 226 | |
| 227 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | |
| 228 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 229 | } |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 230 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 231 | struct bpf_call_arg_meta { |
| 232 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 233 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 234 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 235 | int regno; |
| 236 | int access_size; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 237 | int mem_size; |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 238 | u64 msize_max_value; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 239 | int ref_obj_id; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 240 | int func_id; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 241 | struct btf *btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 242 | u32 btf_id; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 243 | struct btf *ret_btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 244 | u32 ret_btf_id; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 245 | }; |
| 246 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 247 | struct btf *btf_vmlinux; |
| 248 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 249 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 250 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 251 | static const struct bpf_line_info * |
| 252 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) |
| 253 | { |
| 254 | const struct bpf_line_info *linfo; |
| 255 | const struct bpf_prog *prog; |
| 256 | u32 i, nr_linfo; |
| 257 | |
| 258 | prog = env->prog; |
| 259 | nr_linfo = prog->aux->nr_linfo; |
| 260 | |
| 261 | if (!nr_linfo || insn_off >= prog->len) |
| 262 | return NULL; |
| 263 | |
| 264 | linfo = prog->aux->linfo; |
| 265 | for (i = 1; i < nr_linfo; i++) |
| 266 | if (insn_off < linfo[i].insn_off) |
| 267 | break; |
| 268 | |
| 269 | return &linfo[i - 1]; |
| 270 | } |
| 271 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 272 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
| 273 | va_list args) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 274 | { |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 275 | unsigned int n; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 276 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 277 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 278 | |
| 279 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 280 | "verifier log line truncated - local buffer too short\n"); |
| 281 | |
| 282 | n = min(log->len_total - log->len_used - 1, n); |
| 283 | log->kbuf[n] = '\0'; |
| 284 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 285 | if (log->level == BPF_LOG_KERNEL) { |
| 286 | pr_err("BPF:%s\n", log->kbuf); |
| 287 | return; |
| 288 | } |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 289 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 290 | log->len_used += n; |
| 291 | else |
| 292 | log->ubuf = NULL; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 293 | } |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 294 | |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 295 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
| 296 | { |
| 297 | char zero = 0; |
| 298 | |
| 299 | if (!bpf_verifier_log_needed(log)) |
| 300 | return; |
| 301 | |
| 302 | log->len_used = new_pos; |
| 303 | if (put_user(zero, log->ubuf + new_pos)) |
| 304 | log->ubuf = NULL; |
| 305 | } |
| 306 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 307 | /* log_level controls verbosity level of eBPF verifier. |
| 308 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 309 | * so the user can figure out what's wrong with the program |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 310 | */ |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 311 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 312 | const char *fmt, ...) |
| 313 | { |
| 314 | va_list args; |
| 315 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 316 | if (!bpf_verifier_log_needed(&env->log)) |
| 317 | return; |
| 318 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 319 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 320 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 321 | va_end(args); |
| 322 | } |
| 323 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 324 | |
| 325 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
| 326 | { |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 327 | struct bpf_verifier_env *env = private_data; |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 328 | va_list args; |
| 329 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 330 | if (!bpf_verifier_log_needed(&env->log)) |
| 331 | return; |
| 332 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 333 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 334 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 335 | va_end(args); |
| 336 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 337 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 338 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
| 339 | const char *fmt, ...) |
| 340 | { |
| 341 | va_list args; |
| 342 | |
| 343 | if (!bpf_verifier_log_needed(log)) |
| 344 | return; |
| 345 | |
| 346 | va_start(args, fmt); |
| 347 | bpf_verifier_vlog(log, fmt, args); |
| 348 | va_end(args); |
| 349 | } |
| 350 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 351 | static const char *ltrim(const char *s) |
| 352 | { |
| 353 | while (isspace(*s)) |
| 354 | s++; |
| 355 | |
| 356 | return s; |
| 357 | } |
| 358 | |
| 359 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, |
| 360 | u32 insn_off, |
| 361 | const char *prefix_fmt, ...) |
| 362 | { |
| 363 | const struct bpf_line_info *linfo; |
| 364 | |
| 365 | if (!bpf_verifier_log_needed(&env->log)) |
| 366 | return; |
| 367 | |
| 368 | linfo = find_linfo(env, insn_off); |
| 369 | if (!linfo || linfo == env->prev_linfo) |
| 370 | return; |
| 371 | |
| 372 | if (prefix_fmt) { |
| 373 | va_list args; |
| 374 | |
| 375 | va_start(args, prefix_fmt); |
| 376 | bpf_verifier_vlog(&env->log, prefix_fmt, args); |
| 377 | va_end(args); |
| 378 | } |
| 379 | |
| 380 | verbose(env, "%s\n", |
| 381 | ltrim(btf_name_by_offset(env->prog->aux->btf, |
| 382 | linfo->line_off))); |
| 383 | |
| 384 | env->prev_linfo = linfo; |
| 385 | } |
| 386 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 387 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 388 | { |
| 389 | return type == PTR_TO_PACKET || |
| 390 | type == PTR_TO_PACKET_META; |
| 391 | } |
| 392 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 393 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
| 394 | { |
| 395 | return type == PTR_TO_SOCKET || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 396 | type == PTR_TO_SOCK_COMMON || |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 397 | type == PTR_TO_TCP_SOCK || |
| 398 | type == PTR_TO_XDP_SOCK; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 399 | } |
| 400 | |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 401 | static bool reg_type_not_null(enum bpf_reg_type type) |
| 402 | { |
| 403 | return type == PTR_TO_SOCKET || |
| 404 | type == PTR_TO_TCP_SOCK || |
| 405 | type == PTR_TO_MAP_VALUE || |
Yonghong Song | 01c66c4 | 2020-06-30 10:12:40 -0700 | [diff] [blame] | 406 | type == PTR_TO_SOCK_COMMON; |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 409 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
| 410 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 411 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 412 | type == PTR_TO_SOCKET_OR_NULL || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 413 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 414 | type == PTR_TO_TCP_SOCK_OR_NULL || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 415 | type == PTR_TO_BTF_ID_OR_NULL || |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 416 | type == PTR_TO_MEM_OR_NULL || |
| 417 | type == PTR_TO_RDONLY_BUF_OR_NULL || |
| 418 | type == PTR_TO_RDWR_BUF_OR_NULL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 421 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
| 422 | { |
| 423 | return reg->type == PTR_TO_MAP_VALUE && |
| 424 | map_value_has_spin_lock(reg->map_ptr); |
| 425 | } |
| 426 | |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 427 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
| 428 | { |
| 429 | return type == PTR_TO_SOCKET || |
| 430 | type == PTR_TO_SOCKET_OR_NULL || |
| 431 | type == PTR_TO_TCP_SOCK || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 432 | type == PTR_TO_TCP_SOCK_OR_NULL || |
| 433 | type == PTR_TO_MEM || |
| 434 | type == PTR_TO_MEM_OR_NULL; |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 437 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 438 | { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 439 | return type == ARG_PTR_TO_SOCK_COMMON; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 442 | static bool arg_type_may_be_null(enum bpf_arg_type type) |
| 443 | { |
| 444 | return type == ARG_PTR_TO_MAP_VALUE_OR_NULL || |
| 445 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 446 | type == ARG_PTR_TO_CTX_OR_NULL || |
| 447 | type == ARG_PTR_TO_SOCKET_OR_NULL || |
| 448 | type == ARG_PTR_TO_ALLOC_MEM_OR_NULL; |
| 449 | } |
| 450 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 451 | /* Determine whether the function releases some resources allocated by another |
| 452 | * function call. The first reference type argument will be assumed to be |
| 453 | * released by release_reference(). |
| 454 | */ |
| 455 | static bool is_release_function(enum bpf_func_id func_id) |
| 456 | { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 457 | return func_id == BPF_FUNC_sk_release || |
| 458 | func_id == BPF_FUNC_ringbuf_submit || |
| 459 | func_id == BPF_FUNC_ringbuf_discard; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 462 | static bool may_be_acquire_function(enum bpf_func_id func_id) |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 463 | { |
| 464 | return func_id == BPF_FUNC_sk_lookup_tcp || |
Lorenz Bauer | edbf8c0 | 2019-03-22 09:54:01 +0800 | [diff] [blame] | 465 | func_id == BPF_FUNC_sk_lookup_udp || |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 466 | func_id == BPF_FUNC_skc_lookup_tcp || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 467 | func_id == BPF_FUNC_map_lookup_elem || |
| 468 | func_id == BPF_FUNC_ringbuf_reserve; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | static bool is_acquire_function(enum bpf_func_id func_id, |
| 472 | const struct bpf_map *map) |
| 473 | { |
| 474 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; |
| 475 | |
| 476 | if (func_id == BPF_FUNC_sk_lookup_tcp || |
| 477 | func_id == BPF_FUNC_sk_lookup_udp || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 478 | func_id == BPF_FUNC_skc_lookup_tcp || |
| 479 | func_id == BPF_FUNC_ringbuf_reserve) |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 480 | return true; |
| 481 | |
| 482 | if (func_id == BPF_FUNC_map_lookup_elem && |
| 483 | (map_type == BPF_MAP_TYPE_SOCKMAP || |
| 484 | map_type == BPF_MAP_TYPE_SOCKHASH)) |
| 485 | return true; |
| 486 | |
| 487 | return false; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 490 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
| 491 | { |
| 492 | return func_id == BPF_FUNC_tcp_sock || |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 493 | func_id == BPF_FUNC_sk_fullsock || |
| 494 | func_id == BPF_FUNC_skc_to_tcp_sock || |
| 495 | func_id == BPF_FUNC_skc_to_tcp6_sock || |
| 496 | func_id == BPF_FUNC_skc_to_udp6_sock || |
| 497 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || |
| 498 | func_id == BPF_FUNC_skc_to_tcp_request_sock; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 501 | /* string representation of 'enum bpf_reg_type' */ |
| 502 | static const char * const reg_type_str[] = { |
| 503 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 504 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 505 | [PTR_TO_CTX] = "ctx", |
| 506 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 507 | [PTR_TO_MAP_VALUE] = "map_value", |
| 508 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 509 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 510 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 511 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 512 | [PTR_TO_PACKET_END] = "pkt_end", |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 513 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 514 | [PTR_TO_SOCKET] = "sock", |
| 515 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 516 | [PTR_TO_SOCK_COMMON] = "sock_common", |
| 517 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 518 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
| 519 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 520 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 521 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 522 | [PTR_TO_BTF_ID] = "ptr_", |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 523 | [PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_", |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 524 | [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 525 | [PTR_TO_MEM] = "mem", |
| 526 | [PTR_TO_MEM_OR_NULL] = "mem_or_null", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 527 | [PTR_TO_RDONLY_BUF] = "rdonly_buf", |
| 528 | [PTR_TO_RDONLY_BUF_OR_NULL] = "rdonly_buf_or_null", |
| 529 | [PTR_TO_RDWR_BUF] = "rdwr_buf", |
| 530 | [PTR_TO_RDWR_BUF_OR_NULL] = "rdwr_buf_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 531 | }; |
| 532 | |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 533 | static char slot_type_char[] = { |
| 534 | [STACK_INVALID] = '?', |
| 535 | [STACK_SPILL] = 'r', |
| 536 | [STACK_MISC] = 'm', |
| 537 | [STACK_ZERO] = '0', |
| 538 | }; |
| 539 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 540 | static void print_liveness(struct bpf_verifier_env *env, |
| 541 | enum bpf_reg_liveness live) |
| 542 | { |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 543 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 544 | verbose(env, "_"); |
| 545 | if (live & REG_LIVE_READ) |
| 546 | verbose(env, "r"); |
| 547 | if (live & REG_LIVE_WRITTEN) |
| 548 | verbose(env, "w"); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 549 | if (live & REG_LIVE_DONE) |
| 550 | verbose(env, "D"); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 553 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 554 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 555 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 556 | struct bpf_verifier_state *cur = env->cur_state; |
| 557 | |
| 558 | return cur->frame[reg->frameno]; |
| 559 | } |
| 560 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 561 | static const char *kernel_type_name(const struct btf* btf, u32 id) |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 562 | { |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 563 | return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 566 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 567 | const struct bpf_func_state *state) |
| 568 | { |
| 569 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 570 | enum bpf_reg_type t; |
| 571 | int i; |
| 572 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 573 | if (state->frameno) |
| 574 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 575 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 576 | reg = &state->regs[i]; |
| 577 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 578 | if (t == NOT_INIT) |
| 579 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 580 | verbose(env, " R%d", i); |
| 581 | print_liveness(env, reg->live); |
| 582 | verbose(env, "=%s", reg_type_str[t]); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 583 | if (t == SCALAR_VALUE && reg->precise) |
| 584 | verbose(env, "P"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 585 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 586 | tnum_is_const(reg->var_off)) { |
| 587 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 588 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 589 | } else { |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 590 | if (t == PTR_TO_BTF_ID || |
| 591 | t == PTR_TO_BTF_ID_OR_NULL || |
| 592 | t == PTR_TO_PERCPU_BTF_ID) |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 593 | verbose(env, "%s", kernel_type_name(reg->btf, reg->btf_id)); |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 594 | verbose(env, "(id=%d", reg->id); |
| 595 | if (reg_type_may_be_refcounted_or_null(t)) |
| 596 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 597 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 598 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 599 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 600 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 601 | else if (t == CONST_PTR_TO_MAP || |
| 602 | t == PTR_TO_MAP_VALUE || |
| 603 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 604 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 605 | reg->map_ptr->key_size, |
| 606 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 607 | if (tnum_is_const(reg->var_off)) { |
| 608 | /* Typically an immediate SCALAR_VALUE, but |
| 609 | * could be a pointer whose offset is too big |
| 610 | * for reg->off |
| 611 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 612 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 613 | } else { |
| 614 | if (reg->smin_value != reg->umin_value && |
| 615 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 616 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 617 | (long long)reg->smin_value); |
| 618 | if (reg->smax_value != reg->umax_value && |
| 619 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 620 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 621 | (long long)reg->smax_value); |
| 622 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 623 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 624 | (unsigned long long)reg->umin_value); |
| 625 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 626 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 627 | (unsigned long long)reg->umax_value); |
| 628 | if (!tnum_is_unknown(reg->var_off)) { |
| 629 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 630 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 631 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 632 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 633 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 634 | if (reg->s32_min_value != reg->smin_value && |
| 635 | reg->s32_min_value != S32_MIN) |
| 636 | verbose(env, ",s32_min_value=%d", |
| 637 | (int)(reg->s32_min_value)); |
| 638 | if (reg->s32_max_value != reg->smax_value && |
| 639 | reg->s32_max_value != S32_MAX) |
| 640 | verbose(env, ",s32_max_value=%d", |
| 641 | (int)(reg->s32_max_value)); |
| 642 | if (reg->u32_min_value != reg->umin_value && |
| 643 | reg->u32_min_value != U32_MIN) |
| 644 | verbose(env, ",u32_min_value=%d", |
| 645 | (int)(reg->u32_min_value)); |
| 646 | if (reg->u32_max_value != reg->umax_value && |
| 647 | reg->u32_max_value != U32_MAX) |
| 648 | verbose(env, ",u32_max_value=%d", |
| 649 | (int)(reg->u32_max_value)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 650 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 651 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 652 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 653 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 654 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 655 | char types_buf[BPF_REG_SIZE + 1]; |
| 656 | bool valid = false; |
| 657 | int j; |
| 658 | |
| 659 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 660 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 661 | valid = true; |
| 662 | types_buf[j] = slot_type_char[ |
| 663 | state->stack[i].slot_type[j]]; |
| 664 | } |
| 665 | types_buf[BPF_REG_SIZE] = 0; |
| 666 | if (!valid) |
| 667 | continue; |
| 668 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 669 | print_liveness(env, state->stack[i].spilled_ptr.live); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 670 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
| 671 | reg = &state->stack[i].spilled_ptr; |
| 672 | t = reg->type; |
| 673 | verbose(env, "=%s", reg_type_str[t]); |
| 674 | if (t == SCALAR_VALUE && reg->precise) |
| 675 | verbose(env, "P"); |
| 676 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) |
| 677 | verbose(env, "%lld", reg->var_off.value + reg->off); |
| 678 | } else { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 679 | verbose(env, "=%s", types_buf); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 680 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 681 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 682 | if (state->acquired_refs && state->refs[0].id) { |
| 683 | verbose(env, " refs=%d", state->refs[0].id); |
| 684 | for (i = 1; i < state->acquired_refs; i++) |
| 685 | if (state->refs[i].id) |
| 686 | verbose(env, ",%d", state->refs[i].id); |
| 687 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 688 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 691 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 692 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ |
| 693 | const struct bpf_func_state *src) \ |
| 694 | { \ |
| 695 | if (!src->FIELD) \ |
| 696 | return 0; \ |
| 697 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ |
| 698 | /* internal bug, make state invalid to reject the program */ \ |
| 699 | memset(dst, 0, sizeof(*dst)); \ |
| 700 | return -EFAULT; \ |
| 701 | } \ |
| 702 | memcpy(dst->FIELD, src->FIELD, \ |
| 703 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ |
| 704 | return 0; \ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 705 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 706 | /* copy_reference_state() */ |
| 707 | COPY_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 708 | /* copy_stack_state() */ |
| 709 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 710 | #undef COPY_STATE_FN |
| 711 | |
| 712 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 713 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ |
| 714 | bool copy_old) \ |
| 715 | { \ |
| 716 | u32 old_size = state->COUNT; \ |
| 717 | struct bpf_##NAME##_state *new_##FIELD; \ |
| 718 | int slot = size / SIZE; \ |
| 719 | \ |
| 720 | if (size <= old_size || !size) { \ |
| 721 | if (copy_old) \ |
| 722 | return 0; \ |
| 723 | state->COUNT = slot * SIZE; \ |
| 724 | if (!size && old_size) { \ |
| 725 | kfree(state->FIELD); \ |
| 726 | state->FIELD = NULL; \ |
| 727 | } \ |
| 728 | return 0; \ |
| 729 | } \ |
| 730 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ |
| 731 | GFP_KERNEL); \ |
| 732 | if (!new_##FIELD) \ |
| 733 | return -ENOMEM; \ |
| 734 | if (copy_old) { \ |
| 735 | if (state->FIELD) \ |
| 736 | memcpy(new_##FIELD, state->FIELD, \ |
| 737 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ |
| 738 | memset(new_##FIELD + old_size / SIZE, 0, \ |
| 739 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ |
| 740 | } \ |
| 741 | state->COUNT = slot * SIZE; \ |
| 742 | kfree(state->FIELD); \ |
| 743 | state->FIELD = new_##FIELD; \ |
| 744 | return 0; \ |
| 745 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 746 | /* realloc_reference_state() */ |
| 747 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 748 | /* realloc_stack_state() */ |
| 749 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 750 | #undef REALLOC_STATE_FN |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 751 | |
| 752 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 753 | * make it consume minimal amount of memory. check_stack_write() access from |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 754 | * the program calls into realloc_func_state() to grow the stack size. |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 755 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 756 | * which realloc_stack_state() copies over. It points to previous |
| 757 | * bpf_verifier_state which is never reallocated. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 758 | */ |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 759 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
| 760 | int refs_size, bool copy_old) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 761 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 762 | int err = realloc_reference_state(state, refs_size, copy_old); |
| 763 | if (err) |
| 764 | return err; |
| 765 | return realloc_stack_state(state, stack_size, copy_old); |
| 766 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 767 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 768 | /* Acquire a pointer id from the env and update the state->refs to include |
| 769 | * this new pointer reference. |
| 770 | * On success, returns a valid pointer id to associate with the register |
| 771 | * On failure, returns a negative errno. |
| 772 | */ |
| 773 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 774 | { |
| 775 | struct bpf_func_state *state = cur_func(env); |
| 776 | int new_ofs = state->acquired_refs; |
| 777 | int id, err; |
| 778 | |
| 779 | err = realloc_reference_state(state, state->acquired_refs + 1, true); |
| 780 | if (err) |
| 781 | return err; |
| 782 | id = ++env->id_gen; |
| 783 | state->refs[new_ofs].id = id; |
| 784 | state->refs[new_ofs].insn_idx = insn_idx; |
| 785 | |
| 786 | return id; |
| 787 | } |
| 788 | |
| 789 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 790 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 791 | { |
| 792 | int i, last_idx; |
| 793 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 794 | last_idx = state->acquired_refs - 1; |
| 795 | for (i = 0; i < state->acquired_refs; i++) { |
| 796 | if (state->refs[i].id == ptr_id) { |
| 797 | if (last_idx && i != last_idx) |
| 798 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 799 | sizeof(*state->refs)); |
| 800 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 801 | state->acquired_refs--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 802 | return 0; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 803 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 804 | } |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 805 | return -EINVAL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | static int transfer_reference_state(struct bpf_func_state *dst, |
| 809 | struct bpf_func_state *src) |
| 810 | { |
| 811 | int err = realloc_reference_state(dst, src->acquired_refs, false); |
| 812 | if (err) |
| 813 | return err; |
| 814 | err = copy_reference_state(dst, src); |
| 815 | if (err) |
| 816 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 817 | return 0; |
| 818 | } |
| 819 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 820 | static void free_func_state(struct bpf_func_state *state) |
| 821 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 822 | if (!state) |
| 823 | return; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 824 | kfree(state->refs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 825 | kfree(state->stack); |
| 826 | kfree(state); |
| 827 | } |
| 828 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 829 | static void clear_jmp_history(struct bpf_verifier_state *state) |
| 830 | { |
| 831 | kfree(state->jmp_history); |
| 832 | state->jmp_history = NULL; |
| 833 | state->jmp_history_cnt = 0; |
| 834 | } |
| 835 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 836 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 837 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 838 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 839 | int i; |
| 840 | |
| 841 | for (i = 0; i <= state->curframe; i++) { |
| 842 | free_func_state(state->frame[i]); |
| 843 | state->frame[i] = NULL; |
| 844 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 845 | clear_jmp_history(state); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 846 | if (free_self) |
| 847 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | /* copy verifier state from src to dst growing dst stack space |
| 851 | * when necessary to accommodate larger src stack |
| 852 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 853 | static int copy_func_state(struct bpf_func_state *dst, |
| 854 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 855 | { |
| 856 | int err; |
| 857 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 858 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
| 859 | false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 860 | if (err) |
| 861 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 862 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 863 | err = copy_reference_state(dst, src); |
| 864 | if (err) |
| 865 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 866 | return copy_stack_state(dst, src); |
| 867 | } |
| 868 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 869 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 870 | const struct bpf_verifier_state *src) |
| 871 | { |
| 872 | struct bpf_func_state *dst; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 873 | u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 874 | int i, err; |
| 875 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 876 | if (dst_state->jmp_history_cnt < src->jmp_history_cnt) { |
| 877 | kfree(dst_state->jmp_history); |
| 878 | dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER); |
| 879 | if (!dst_state->jmp_history) |
| 880 | return -ENOMEM; |
| 881 | } |
| 882 | memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz); |
| 883 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
| 884 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 885 | /* if dst has more stack frames then src frame, free them */ |
| 886 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 887 | free_func_state(dst_state->frame[i]); |
| 888 | dst_state->frame[i] = NULL; |
| 889 | } |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 890 | dst_state->speculative = src->speculative; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 891 | dst_state->curframe = src->curframe; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 892 | dst_state->active_spin_lock = src->active_spin_lock; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 893 | dst_state->branches = src->branches; |
| 894 | dst_state->parent = src->parent; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 895 | dst_state->first_insn_idx = src->first_insn_idx; |
| 896 | dst_state->last_insn_idx = src->last_insn_idx; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 897 | for (i = 0; i <= src->curframe; i++) { |
| 898 | dst = dst_state->frame[i]; |
| 899 | if (!dst) { |
| 900 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 901 | if (!dst) |
| 902 | return -ENOMEM; |
| 903 | dst_state->frame[i] = dst; |
| 904 | } |
| 905 | err = copy_func_state(dst, src->frame[i]); |
| 906 | if (err) |
| 907 | return err; |
| 908 | } |
| 909 | return 0; |
| 910 | } |
| 911 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 912 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
| 913 | { |
| 914 | while (st) { |
| 915 | u32 br = --st->branches; |
| 916 | |
| 917 | /* WARN_ON(br > 1) technically makes sense here, |
| 918 | * but see comment in push_stack(), hence: |
| 919 | */ |
| 920 | WARN_ONCE((int)br < 0, |
| 921 | "BUG update_branch_counts:branches_to_explore=%d\n", |
| 922 | br); |
| 923 | if (br) |
| 924 | break; |
| 925 | st = st->parent; |
| 926 | } |
| 927 | } |
| 928 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 929 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 930 | int *insn_idx, bool pop_log) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 931 | { |
| 932 | struct bpf_verifier_state *cur = env->cur_state; |
| 933 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 934 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 935 | |
| 936 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 937 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 938 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 939 | if (cur) { |
| 940 | err = copy_verifier_state(cur, &head->st); |
| 941 | if (err) |
| 942 | return err; |
| 943 | } |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 944 | if (pop_log) |
| 945 | bpf_vlog_reset(&env->log, head->log_pos); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 946 | if (insn_idx) |
| 947 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 948 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 949 | *prev_insn_idx = head->prev_insn_idx; |
| 950 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 951 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 952 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 953 | env->head = elem; |
| 954 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 955 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 958 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 959 | int insn_idx, int prev_insn_idx, |
| 960 | bool speculative) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 961 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 962 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 963 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 964 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 965 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 966 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 967 | if (!elem) |
| 968 | goto err; |
| 969 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 970 | elem->insn_idx = insn_idx; |
| 971 | elem->prev_insn_idx = prev_insn_idx; |
| 972 | elem->next = env->head; |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 973 | elem->log_pos = env->log.len_used; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 974 | env->head = elem; |
| 975 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 976 | err = copy_verifier_state(&elem->st, cur); |
| 977 | if (err) |
| 978 | goto err; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 979 | elem->st.speculative |= speculative; |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 980 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
| 981 | verbose(env, "The sequence of %d jumps is too complex.\n", |
| 982 | env->stack_size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 983 | goto err; |
| 984 | } |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 985 | if (elem->st.parent) { |
| 986 | ++elem->st.parent->branches; |
| 987 | /* WARN_ON(branches > 2) technically makes sense here, |
| 988 | * but |
| 989 | * 1. speculative states will bump 'branches' for non-branch |
| 990 | * instructions |
| 991 | * 2. is_state_visited() heuristics may decide not to create |
| 992 | * a new state for a sequence of branches and all such current |
| 993 | * and cloned states will be pointing to a single parent state |
| 994 | * which might have large 'branches' count. |
| 995 | */ |
| 996 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 997 | return &elem->st; |
| 998 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 999 | free_verifier_state(env->cur_state, true); |
| 1000 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1001 | /* pop all elements and return */ |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 1002 | while (!pop_stack(env, NULL, NULL, false)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1003 | return NULL; |
| 1004 | } |
| 1005 | |
| 1006 | #define CALLER_SAVED_REGS 6 |
| 1007 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 1008 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 1009 | }; |
| 1010 | |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1011 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 1012 | struct bpf_reg_state *reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1013 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 1014 | /* This helper doesn't clear reg->id */ |
| 1015 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1016 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1017 | reg->var_off = tnum_const(imm); |
| 1018 | reg->smin_value = (s64)imm; |
| 1019 | reg->smax_value = (s64)imm; |
| 1020 | reg->umin_value = imm; |
| 1021 | reg->umax_value = imm; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1022 | |
| 1023 | reg->s32_min_value = (s32)imm; |
| 1024 | reg->s32_max_value = (s32)imm; |
| 1025 | reg->u32_min_value = (u32)imm; |
| 1026 | reg->u32_max_value = (u32)imm; |
| 1027 | } |
| 1028 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 1029 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 1030 | * known to have the value @imm. |
| 1031 | */ |
| 1032 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 1033 | { |
| 1034 | /* Clear id, off, and union(map_ptr, range) */ |
| 1035 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 1036 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
| 1037 | ___mark_reg_known(reg, imm); |
| 1038 | } |
| 1039 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1040 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
| 1041 | { |
| 1042 | reg->var_off = tnum_const_subreg(reg->var_off, imm); |
| 1043 | reg->s32_min_value = (s32)imm; |
| 1044 | reg->s32_max_value = (s32)imm; |
| 1045 | reg->u32_min_value = (u32)imm; |
| 1046 | reg->u32_max_value = (u32)imm; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1047 | } |
| 1048 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1049 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 1050 | * used only on registers holding a pointer type. |
| 1051 | */ |
| 1052 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 1053 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1054 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1055 | } |
| 1056 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1057 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 1058 | { |
| 1059 | __mark_reg_known(reg, 0); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1060 | reg->type = SCALAR_VALUE; |
| 1061 | } |
| 1062 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1063 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 1064 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1065 | { |
| 1066 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1067 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1068 | /* Something bad happened, let's kill all regs */ |
| 1069 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1070 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1071 | return; |
| 1072 | } |
| 1073 | __mark_reg_known_zero(regs + regno); |
| 1074 | } |
| 1075 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1076 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 1077 | { |
| 1078 | return type_is_pkt_pointer(reg->type); |
| 1079 | } |
| 1080 | |
| 1081 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 1082 | { |
| 1083 | return reg_is_pkt_pointer(reg) || |
| 1084 | reg->type == PTR_TO_PACKET_END; |
| 1085 | } |
| 1086 | |
| 1087 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 1088 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 1089 | enum bpf_reg_type which) |
| 1090 | { |
| 1091 | /* The register can already have a range from prior markings. |
| 1092 | * This is fine as long as it hasn't been advanced from its |
| 1093 | * origin. |
| 1094 | */ |
| 1095 | return reg->type == which && |
| 1096 | reg->id == 0 && |
| 1097 | reg->off == 0 && |
| 1098 | tnum_equals_const(reg->var_off, 0); |
| 1099 | } |
| 1100 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1101 | /* Reset the min/max bounds of a register */ |
| 1102 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 1103 | { |
| 1104 | reg->smin_value = S64_MIN; |
| 1105 | reg->smax_value = S64_MAX; |
| 1106 | reg->umin_value = 0; |
| 1107 | reg->umax_value = U64_MAX; |
| 1108 | |
| 1109 | reg->s32_min_value = S32_MIN; |
| 1110 | reg->s32_max_value = S32_MAX; |
| 1111 | reg->u32_min_value = 0; |
| 1112 | reg->u32_max_value = U32_MAX; |
| 1113 | } |
| 1114 | |
| 1115 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) |
| 1116 | { |
| 1117 | reg->smin_value = S64_MIN; |
| 1118 | reg->smax_value = S64_MAX; |
| 1119 | reg->umin_value = 0; |
| 1120 | reg->umax_value = U64_MAX; |
| 1121 | } |
| 1122 | |
| 1123 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) |
| 1124 | { |
| 1125 | reg->s32_min_value = S32_MIN; |
| 1126 | reg->s32_max_value = S32_MAX; |
| 1127 | reg->u32_min_value = 0; |
| 1128 | reg->u32_max_value = U32_MAX; |
| 1129 | } |
| 1130 | |
| 1131 | static void __update_reg32_bounds(struct bpf_reg_state *reg) |
| 1132 | { |
| 1133 | struct tnum var32_off = tnum_subreg(reg->var_off); |
| 1134 | |
| 1135 | /* min signed is max(sign bit) | min(other bits) */ |
| 1136 | reg->s32_min_value = max_t(s32, reg->s32_min_value, |
| 1137 | var32_off.value | (var32_off.mask & S32_MIN)); |
| 1138 | /* max signed is min(sign bit) | max(other bits) */ |
| 1139 | reg->s32_max_value = min_t(s32, reg->s32_max_value, |
| 1140 | var32_off.value | (var32_off.mask & S32_MAX)); |
| 1141 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); |
| 1142 | reg->u32_max_value = min(reg->u32_max_value, |
| 1143 | (u32)(var32_off.value | var32_off.mask)); |
| 1144 | } |
| 1145 | |
| 1146 | static void __update_reg64_bounds(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1147 | { |
| 1148 | /* min signed is max(sign bit) | min(other bits) */ |
| 1149 | reg->smin_value = max_t(s64, reg->smin_value, |
| 1150 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 1151 | /* max signed is min(sign bit) | max(other bits) */ |
| 1152 | reg->smax_value = min_t(s64, reg->smax_value, |
| 1153 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 1154 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 1155 | reg->umax_value = min(reg->umax_value, |
| 1156 | reg->var_off.value | reg->var_off.mask); |
| 1157 | } |
| 1158 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1159 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 1160 | { |
| 1161 | __update_reg32_bounds(reg); |
| 1162 | __update_reg64_bounds(reg); |
| 1163 | } |
| 1164 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1165 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1166 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
| 1167 | { |
| 1168 | /* Learn sign from signed bounds. |
| 1169 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 1170 | * are the same, so combine. This works even in the negative case, e.g. |
| 1171 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 1172 | */ |
| 1173 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { |
| 1174 | reg->s32_min_value = reg->u32_min_value = |
| 1175 | max_t(u32, reg->s32_min_value, reg->u32_min_value); |
| 1176 | reg->s32_max_value = reg->u32_max_value = |
| 1177 | min_t(u32, reg->s32_max_value, reg->u32_max_value); |
| 1178 | return; |
| 1179 | } |
| 1180 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 1181 | * boundary, so we must be careful. |
| 1182 | */ |
| 1183 | if ((s32)reg->u32_max_value >= 0) { |
| 1184 | /* Positive. We can't learn anything from the smin, but smax |
| 1185 | * is positive, hence safe. |
| 1186 | */ |
| 1187 | reg->s32_min_value = reg->u32_min_value; |
| 1188 | reg->s32_max_value = reg->u32_max_value = |
| 1189 | min_t(u32, reg->s32_max_value, reg->u32_max_value); |
| 1190 | } else if ((s32)reg->u32_min_value < 0) { |
| 1191 | /* Negative. We can't learn anything from the smax, but smin |
| 1192 | * is negative, hence safe. |
| 1193 | */ |
| 1194 | reg->s32_min_value = reg->u32_min_value = |
| 1195 | max_t(u32, reg->s32_min_value, reg->u32_min_value); |
| 1196 | reg->s32_max_value = reg->u32_max_value; |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1201 | { |
| 1202 | /* Learn sign from signed bounds. |
| 1203 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 1204 | * are the same, so combine. This works even in the negative case, e.g. |
| 1205 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 1206 | */ |
| 1207 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 1208 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1209 | reg->umin_value); |
| 1210 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1211 | reg->umax_value); |
| 1212 | return; |
| 1213 | } |
| 1214 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 1215 | * boundary, so we must be careful. |
| 1216 | */ |
| 1217 | if ((s64)reg->umax_value >= 0) { |
| 1218 | /* Positive. We can't learn anything from the smin, but smax |
| 1219 | * is positive, hence safe. |
| 1220 | */ |
| 1221 | reg->smin_value = reg->umin_value; |
| 1222 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1223 | reg->umax_value); |
| 1224 | } else if ((s64)reg->umin_value < 0) { |
| 1225 | /* Negative. We can't learn anything from the smax, but smin |
| 1226 | * is negative, hence safe. |
| 1227 | */ |
| 1228 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1229 | reg->umin_value); |
| 1230 | reg->smax_value = reg->umax_value; |
| 1231 | } |
| 1232 | } |
| 1233 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1234 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 1235 | { |
| 1236 | __reg32_deduce_bounds(reg); |
| 1237 | __reg64_deduce_bounds(reg); |
| 1238 | } |
| 1239 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1240 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 1241 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 1242 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1243 | struct tnum var64_off = tnum_intersect(reg->var_off, |
| 1244 | tnum_range(reg->umin_value, |
| 1245 | reg->umax_value)); |
| 1246 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), |
| 1247 | tnum_range(reg->u32_min_value, |
| 1248 | reg->u32_max_value)); |
| 1249 | |
| 1250 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1251 | } |
| 1252 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1253 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1254 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1255 | reg->umin_value = reg->u32_min_value; |
| 1256 | reg->umax_value = reg->u32_max_value; |
| 1257 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds |
| 1258 | * but must be positive otherwise set to worse case bounds |
| 1259 | * and refine later from tnum. |
| 1260 | */ |
John Fastabend | 3a71dc3 | 2020-05-29 10:28:40 -0700 | [diff] [blame] | 1261 | if (reg->s32_min_value >= 0 && reg->s32_max_value >= 0) |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1262 | reg->smax_value = reg->s32_max_value; |
| 1263 | else |
| 1264 | reg->smax_value = U32_MAX; |
John Fastabend | 3a71dc3 | 2020-05-29 10:28:40 -0700 | [diff] [blame] | 1265 | if (reg->s32_min_value >= 0) |
| 1266 | reg->smin_value = reg->s32_min_value; |
| 1267 | else |
| 1268 | reg->smin_value = 0; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) |
| 1272 | { |
| 1273 | /* special case when 64-bit register has upper 32-bit register |
| 1274 | * zeroed. Typically happens after zext or <<32, >>32 sequence |
| 1275 | * allowing us to use 32-bit bounds directly, |
| 1276 | */ |
| 1277 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { |
| 1278 | __reg_assign_32_into_64(reg); |
| 1279 | } else { |
| 1280 | /* Otherwise the best we can do is push lower 32bit known and |
| 1281 | * unknown bits into register (var_off set from jmp logic) |
| 1282 | * then learn as much as possible from the 64-bit tnum |
| 1283 | * known and unknown bits. The previous smin/smax bounds are |
| 1284 | * invalid here because of jmp32 compare so mark them unknown |
| 1285 | * so they do not impact tnum bounds calculation. |
| 1286 | */ |
| 1287 | __mark_reg64_unbounded(reg); |
| 1288 | __update_reg_bounds(reg); |
| 1289 | } |
| 1290 | |
| 1291 | /* Intersecting with the old var_off might have improved our bounds |
| 1292 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 1293 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 1294 | */ |
| 1295 | __reg_deduce_bounds(reg); |
| 1296 | __reg_bound_offset(reg); |
| 1297 | __update_reg_bounds(reg); |
| 1298 | } |
| 1299 | |
| 1300 | static bool __reg64_bound_s32(s64 a) |
| 1301 | { |
Alexei Starovoitov | b0270958 | 2020-12-08 19:01:51 +0100 | [diff] [blame] | 1302 | return a > S32_MIN && a < S32_MAX; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | static bool __reg64_bound_u32(u64 a) |
| 1306 | { |
| 1307 | if (a > U32_MIN && a < U32_MAX) |
| 1308 | return true; |
| 1309 | return false; |
| 1310 | } |
| 1311 | |
| 1312 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) |
| 1313 | { |
| 1314 | __mark_reg32_unbounded(reg); |
| 1315 | |
Alexei Starovoitov | b0270958 | 2020-12-08 19:01:51 +0100 | [diff] [blame] | 1316 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1317 | reg->s32_min_value = (s32)reg->smin_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1318 | reg->s32_max_value = (s32)reg->smax_value; |
Alexei Starovoitov | b0270958 | 2020-12-08 19:01:51 +0100 | [diff] [blame] | 1319 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1320 | if (__reg64_bound_u32(reg->umin_value)) |
| 1321 | reg->u32_min_value = (u32)reg->umin_value; |
| 1322 | if (__reg64_bound_u32(reg->umax_value)) |
| 1323 | reg->u32_max_value = (u32)reg->umax_value; |
| 1324 | |
| 1325 | /* Intersecting with the old var_off might have improved our bounds |
| 1326 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 1327 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 1328 | */ |
| 1329 | __reg_deduce_bounds(reg); |
| 1330 | __reg_bound_offset(reg); |
| 1331 | __update_reg_bounds(reg); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1332 | } |
| 1333 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1334 | /* Mark a register as having a completely unknown (scalar) value. */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1335 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
| 1336 | struct bpf_reg_state *reg) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1337 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 1338 | /* |
| 1339 | * Clear type, id, off, and union(map_ptr, range) and |
| 1340 | * padding between 'type' and union |
| 1341 | */ |
| 1342 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1343 | reg->type = SCALAR_VALUE; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1344 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1345 | reg->frameno = 0; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 1346 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1347 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1348 | } |
| 1349 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1350 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 1351 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1352 | { |
| 1353 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1354 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 1355 | /* Something bad happened, let's kill all regs except FP */ |
| 1356 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1357 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1358 | return; |
| 1359 | } |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1360 | __mark_reg_unknown(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1361 | } |
| 1362 | |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1363 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 1364 | struct bpf_reg_state *reg) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1365 | { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1366 | __mark_reg_unknown(env, reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1367 | reg->type = NOT_INIT; |
| 1368 | } |
| 1369 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1370 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 1371 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 1372 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1373 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1374 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 1375 | /* Something bad happened, let's kill all regs except FP */ |
| 1376 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1377 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1378 | return; |
| 1379 | } |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1380 | __mark_reg_not_init(env, regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 1381 | } |
| 1382 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 1383 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
| 1384 | struct bpf_reg_state *regs, u32 regno, |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 1385 | enum bpf_reg_type reg_type, |
| 1386 | struct btf *btf, u32 btf_id) |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 1387 | { |
| 1388 | if (reg_type == SCALAR_VALUE) { |
| 1389 | mark_reg_unknown(env, regs, regno); |
| 1390 | return; |
| 1391 | } |
| 1392 | mark_reg_known_zero(env, regs, regno); |
| 1393 | regs[regno].type = PTR_TO_BTF_ID; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 1394 | regs[regno].btf = btf; |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 1395 | regs[regno].btf_id = btf_id; |
| 1396 | } |
| 1397 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1398 | #define DEF_NOT_SUBREG (0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1399 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1400 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1401 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1402 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1403 | int i; |
| 1404 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1405 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1406 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1407 | regs[i].live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1408 | regs[i].parent = NULL; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1409 | regs[i].subreg_def = DEF_NOT_SUBREG; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1410 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1411 | |
| 1412 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1413 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1414 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1415 | regs[BPF_REG_FP].frameno = state->frameno; |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 1416 | } |
| 1417 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1418 | #define BPF_MAIN_FUNC (-1) |
| 1419 | static void init_func_state(struct bpf_verifier_env *env, |
| 1420 | struct bpf_func_state *state, |
| 1421 | int callsite, int frameno, int subprogno) |
| 1422 | { |
| 1423 | state->callsite = callsite; |
| 1424 | state->frameno = frameno; |
| 1425 | state->subprogno = subprogno; |
| 1426 | init_reg_state(env, state); |
| 1427 | } |
| 1428 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1429 | enum reg_arg_type { |
| 1430 | SRC_OP, /* register is used as source operand */ |
| 1431 | DST_OP, /* register is used as destination operand */ |
| 1432 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 1433 | }; |
| 1434 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1435 | static int cmp_subprogs(const void *a, const void *b) |
| 1436 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1437 | return ((struct bpf_subprog_info *)a)->start - |
| 1438 | ((struct bpf_subprog_info *)b)->start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 1442 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1443 | struct bpf_subprog_info *p; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1444 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1445 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 1446 | sizeof(env->subprog_info[0]), cmp_subprogs); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1447 | if (!p) |
| 1448 | return -ENOENT; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1449 | return p - env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1450 | |
| 1451 | } |
| 1452 | |
| 1453 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 1454 | { |
| 1455 | int insn_cnt = env->prog->len; |
| 1456 | int ret; |
| 1457 | |
| 1458 | if (off >= insn_cnt || off < 0) { |
| 1459 | verbose(env, "call to invalid destination\n"); |
| 1460 | return -EINVAL; |
| 1461 | } |
| 1462 | ret = find_subprog(env, off); |
| 1463 | if (ret >= 0) |
| 1464 | return 0; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1465 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1466 | verbose(env, "too many subprograms\n"); |
| 1467 | return -E2BIG; |
| 1468 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1469 | env->subprog_info[env->subprog_cnt++].start = off; |
| 1470 | sort(env->subprog_info, env->subprog_cnt, |
| 1471 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1472 | return 0; |
| 1473 | } |
| 1474 | |
| 1475 | static int check_subprogs(struct bpf_verifier_env *env) |
| 1476 | { |
| 1477 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1478 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1479 | struct bpf_insn *insn = env->prog->insnsi; |
| 1480 | int insn_cnt = env->prog->len; |
| 1481 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 1482 | /* Add entry function. */ |
| 1483 | ret = add_subprog(env, 0); |
| 1484 | if (ret < 0) |
| 1485 | return ret; |
| 1486 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1487 | /* determine subprog starts. The end is one before the next starts */ |
| 1488 | for (i = 0; i < insn_cnt; i++) { |
| 1489 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1490 | continue; |
| 1491 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1492 | continue; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 1493 | if (!env->bpf_capable) { |
| 1494 | verbose(env, |
| 1495 | "function calls to other bpf functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1496 | return -EPERM; |
| 1497 | } |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1498 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 1499 | if (ret < 0) |
| 1500 | return ret; |
| 1501 | } |
| 1502 | |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1503 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 1504 | * logic. 'subprog_cnt' should not be increased. |
| 1505 | */ |
| 1506 | subprog[env->subprog_cnt].start = insn_cnt; |
| 1507 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1508 | if (env->log.level & BPF_LOG_LEVEL2) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1509 | for (i = 0; i < env->subprog_cnt; i++) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1510 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1511 | |
| 1512 | /* now check that all jumps are within the same subprog */ |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1513 | subprog_start = subprog[cur_subprog].start; |
| 1514 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1515 | for (i = 0; i < insn_cnt; i++) { |
| 1516 | u8 code = insn[i].code; |
| 1517 | |
Maciej Fijalkowski | 7f6e431 | 2020-09-16 23:10:07 +0200 | [diff] [blame] | 1518 | if (code == (BPF_JMP | BPF_CALL) && |
| 1519 | insn[i].imm == BPF_FUNC_tail_call && |
| 1520 | insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1521 | subprog[cur_subprog].has_tail_call = true; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 1522 | if (BPF_CLASS(code) == BPF_LD && |
| 1523 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) |
| 1524 | subprog[cur_subprog].has_ld_abs = true; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 1525 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1526 | goto next; |
| 1527 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 1528 | goto next; |
| 1529 | off = i + insn[i].off + 1; |
| 1530 | if (off < subprog_start || off >= subprog_end) { |
| 1531 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 1532 | return -EINVAL; |
| 1533 | } |
| 1534 | next: |
| 1535 | if (i == subprog_end - 1) { |
| 1536 | /* to avoid fall-through from one subprog into another |
| 1537 | * the last insn of the subprog should be either exit |
| 1538 | * or unconditional jump back |
| 1539 | */ |
| 1540 | if (code != (BPF_JMP | BPF_EXIT) && |
| 1541 | code != (BPF_JMP | BPF_JA)) { |
| 1542 | verbose(env, "last insn is not an exit or jmp\n"); |
| 1543 | return -EINVAL; |
| 1544 | } |
| 1545 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1546 | cur_subprog++; |
| 1547 | if (cur_subprog < env->subprog_cnt) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1548 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | return 0; |
| 1552 | } |
| 1553 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1554 | /* Parentage chain of this register (or stack slot) should take care of all |
| 1555 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 1556 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1557 | static int mark_reg_read(struct bpf_verifier_env *env, |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1558 | const struct bpf_reg_state *state, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1559 | struct bpf_reg_state *parent, u8 flag) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1560 | { |
| 1561 | bool writes = parent == state->parent; /* Observe write marks */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1562 | int cnt = 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1563 | |
| 1564 | while (parent) { |
| 1565 | /* if read wasn't screened by an earlier write ... */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1566 | if (writes && state->live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1567 | break; |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 1568 | if (parent->live & REG_LIVE_DONE) { |
| 1569 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", |
| 1570 | reg_type_str[parent->type], |
| 1571 | parent->var_off.value, parent->off); |
| 1572 | return -EFAULT; |
| 1573 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1574 | /* The first condition is more likely to be true than the |
| 1575 | * second, checked it first. |
| 1576 | */ |
| 1577 | if ((parent->live & REG_LIVE_READ) == flag || |
| 1578 | parent->live & REG_LIVE_READ64) |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 1579 | /* The parentage chain never changes and |
| 1580 | * this parent was already marked as LIVE_READ. |
| 1581 | * There is no need to keep walking the chain again and |
| 1582 | * keep re-marking all parents as LIVE_READ. |
| 1583 | * This case happens when the same register is read |
| 1584 | * multiple times without writes into it in-between. |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1585 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
| 1586 | * then no need to set the weak REG_LIVE_READ32. |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 1587 | */ |
| 1588 | break; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1589 | /* ... then we depend on parent's value */ |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1590 | parent->live |= flag; |
| 1591 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ |
| 1592 | if (flag == REG_LIVE_READ64) |
| 1593 | parent->live &= ~REG_LIVE_READ32; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1594 | state = parent; |
| 1595 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1596 | writes = true; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1597 | cnt++; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1598 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1599 | |
| 1600 | if (env->longest_mark_read_walk < cnt) |
| 1601 | env->longest_mark_read_walk = cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1602 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1603 | } |
| 1604 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1605 | /* This function is supposed to be used by the following 32-bit optimization |
| 1606 | * code only. It returns TRUE if the source or destination register operates |
| 1607 | * on 64-bit, otherwise return FALSE. |
| 1608 | */ |
| 1609 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 1610 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) |
| 1611 | { |
| 1612 | u8 code, class, op; |
| 1613 | |
| 1614 | code = insn->code; |
| 1615 | class = BPF_CLASS(code); |
| 1616 | op = BPF_OP(code); |
| 1617 | if (class == BPF_JMP) { |
| 1618 | /* BPF_EXIT for "main" will reach here. Return TRUE |
| 1619 | * conservatively. |
| 1620 | */ |
| 1621 | if (op == BPF_EXIT) |
| 1622 | return true; |
| 1623 | if (op == BPF_CALL) { |
| 1624 | /* BPF to BPF call will reach here because of marking |
| 1625 | * caller saved clobber with DST_OP_NO_MARK for which we |
| 1626 | * don't care the register def because they are anyway |
| 1627 | * marked as NOT_INIT already. |
| 1628 | */ |
| 1629 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1630 | return false; |
| 1631 | /* Helper call will reach here because of arg type |
| 1632 | * check, conservatively return TRUE. |
| 1633 | */ |
| 1634 | if (t == SRC_OP) |
| 1635 | return true; |
| 1636 | |
| 1637 | return false; |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | if (class == BPF_ALU64 || class == BPF_JMP || |
| 1642 | /* BPF_END always use BPF_ALU class. */ |
| 1643 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) |
| 1644 | return true; |
| 1645 | |
| 1646 | if (class == BPF_ALU || class == BPF_JMP32) |
| 1647 | return false; |
| 1648 | |
| 1649 | if (class == BPF_LDX) { |
| 1650 | if (t != SRC_OP) |
| 1651 | return BPF_SIZE(code) == BPF_DW; |
| 1652 | /* LDX source must be ptr. */ |
| 1653 | return true; |
| 1654 | } |
| 1655 | |
| 1656 | if (class == BPF_STX) { |
| 1657 | if (reg->type != SCALAR_VALUE) |
| 1658 | return true; |
| 1659 | return BPF_SIZE(code) == BPF_DW; |
| 1660 | } |
| 1661 | |
| 1662 | if (class == BPF_LD) { |
| 1663 | u8 mode = BPF_MODE(code); |
| 1664 | |
| 1665 | /* LD_IMM64 */ |
| 1666 | if (mode == BPF_IMM) |
| 1667 | return true; |
| 1668 | |
| 1669 | /* Both LD_IND and LD_ABS return 32-bit data. */ |
| 1670 | if (t != SRC_OP) |
| 1671 | return false; |
| 1672 | |
| 1673 | /* Implicit ctx ptr. */ |
| 1674 | if (regno == BPF_REG_6) |
| 1675 | return true; |
| 1676 | |
| 1677 | /* Explicit source could be any width. */ |
| 1678 | return true; |
| 1679 | } |
| 1680 | |
| 1681 | if (class == BPF_ST) |
| 1682 | /* The only source register for BPF_ST is a ptr. */ |
| 1683 | return true; |
| 1684 | |
| 1685 | /* Conservatively return true at default. */ |
| 1686 | return true; |
| 1687 | } |
| 1688 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 1689 | /* Return TRUE if INSN doesn't have explicit value define. */ |
| 1690 | static bool insn_no_def(struct bpf_insn *insn) |
| 1691 | { |
| 1692 | u8 class = BPF_CLASS(insn->code); |
| 1693 | |
| 1694 | return (class == BPF_JMP || class == BPF_JMP32 || |
| 1695 | class == BPF_STX || class == BPF_ST); |
| 1696 | } |
| 1697 | |
| 1698 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ |
| 1699 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 1700 | { |
| 1701 | if (insn_no_def(insn)) |
| 1702 | return false; |
| 1703 | |
| 1704 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); |
| 1705 | } |
| 1706 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1707 | static void mark_insn_zext(struct bpf_verifier_env *env, |
| 1708 | struct bpf_reg_state *reg) |
| 1709 | { |
| 1710 | s32 def_idx = reg->subreg_def; |
| 1711 | |
| 1712 | if (def_idx == DEF_NOT_SUBREG) |
| 1713 | return; |
| 1714 | |
| 1715 | env->insn_aux_data[def_idx - 1].zext_dst = true; |
| 1716 | /* The dst will be zero extended, so won't be sub-register anymore. */ |
| 1717 | reg->subreg_def = DEF_NOT_SUBREG; |
| 1718 | } |
| 1719 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1720 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1721 | enum reg_arg_type t) |
| 1722 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1723 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1724 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1725 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1726 | struct bpf_reg_state *reg, *regs = state->regs; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1727 | bool rw64; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1728 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1729 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1730 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1731 | return -EINVAL; |
| 1732 | } |
| 1733 | |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1734 | reg = ®s[regno]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1735 | rw64 = is_reg64(env, insn, regno, reg, t); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1736 | if (t == SRC_OP) { |
| 1737 | /* check whether register used as source operand can be read */ |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1738 | if (reg->type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1739 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1740 | return -EACCES; |
| 1741 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1742 | /* We don't need to worry about FP liveness because it's read-only */ |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1743 | if (regno == BPF_REG_FP) |
| 1744 | return 0; |
| 1745 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1746 | if (rw64) |
| 1747 | mark_insn_zext(env, reg); |
| 1748 | |
| 1749 | return mark_reg_read(env, reg, reg->parent, |
| 1750 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1751 | } else { |
| 1752 | /* check whether register used as dest operand can be written to */ |
| 1753 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1754 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1755 | return -EACCES; |
| 1756 | } |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1757 | reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1758 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1759 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1760 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1761 | } |
| 1762 | return 0; |
| 1763 | } |
| 1764 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1765 | /* for any branch, call, exit record the history of jmps in the given state */ |
| 1766 | static int push_jmp_history(struct bpf_verifier_env *env, |
| 1767 | struct bpf_verifier_state *cur) |
| 1768 | { |
| 1769 | u32 cnt = cur->jmp_history_cnt; |
| 1770 | struct bpf_idx_pair *p; |
| 1771 | |
| 1772 | cnt++; |
| 1773 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); |
| 1774 | if (!p) |
| 1775 | return -ENOMEM; |
| 1776 | p[cnt - 1].idx = env->insn_idx; |
| 1777 | p[cnt - 1].prev_idx = env->prev_insn_idx; |
| 1778 | cur->jmp_history = p; |
| 1779 | cur->jmp_history_cnt = cnt; |
| 1780 | return 0; |
| 1781 | } |
| 1782 | |
| 1783 | /* Backtrack one insn at a time. If idx is not at the top of recorded |
| 1784 | * history then previous instruction came from straight line execution. |
| 1785 | */ |
| 1786 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, |
| 1787 | u32 *history) |
| 1788 | { |
| 1789 | u32 cnt = *history; |
| 1790 | |
| 1791 | if (cnt && st->jmp_history[cnt - 1].idx == i) { |
| 1792 | i = st->jmp_history[cnt - 1].prev_idx; |
| 1793 | (*history)--; |
| 1794 | } else { |
| 1795 | i--; |
| 1796 | } |
| 1797 | return i; |
| 1798 | } |
| 1799 | |
| 1800 | /* For given verifier state backtrack_insn() is called from the last insn to |
| 1801 | * the first insn. Its purpose is to compute a bitmask of registers and |
| 1802 | * stack slots that needs precision in the parent verifier state. |
| 1803 | */ |
| 1804 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, |
| 1805 | u32 *reg_mask, u64 *stack_mask) |
| 1806 | { |
| 1807 | const struct bpf_insn_cbs cbs = { |
| 1808 | .cb_print = verbose, |
| 1809 | .private_data = env, |
| 1810 | }; |
| 1811 | struct bpf_insn *insn = env->prog->insnsi + idx; |
| 1812 | u8 class = BPF_CLASS(insn->code); |
| 1813 | u8 opcode = BPF_OP(insn->code); |
| 1814 | u8 mode = BPF_MODE(insn->code); |
| 1815 | u32 dreg = 1u << insn->dst_reg; |
| 1816 | u32 sreg = 1u << insn->src_reg; |
| 1817 | u32 spi; |
| 1818 | |
| 1819 | if (insn->code == 0) |
| 1820 | return 0; |
| 1821 | if (env->log.level & BPF_LOG_LEVEL) { |
| 1822 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
| 1823 | verbose(env, "%d: ", idx); |
| 1824 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
| 1825 | } |
| 1826 | |
| 1827 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 1828 | if (!(*reg_mask & dreg)) |
| 1829 | return 0; |
| 1830 | if (opcode == BPF_MOV) { |
| 1831 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1832 | /* dreg = sreg |
| 1833 | * dreg needs precision after this insn |
| 1834 | * sreg needs precision before this insn |
| 1835 | */ |
| 1836 | *reg_mask &= ~dreg; |
| 1837 | *reg_mask |= sreg; |
| 1838 | } else { |
| 1839 | /* dreg = K |
| 1840 | * dreg needs precision after this insn. |
| 1841 | * Corresponding register is already marked |
| 1842 | * as precise=true in this verifier state. |
| 1843 | * No further markings in parent are necessary |
| 1844 | */ |
| 1845 | *reg_mask &= ~dreg; |
| 1846 | } |
| 1847 | } else { |
| 1848 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1849 | /* dreg += sreg |
| 1850 | * both dreg and sreg need precision |
| 1851 | * before this insn |
| 1852 | */ |
| 1853 | *reg_mask |= sreg; |
| 1854 | } /* else dreg += K |
| 1855 | * dreg still needs precision before this insn |
| 1856 | */ |
| 1857 | } |
| 1858 | } else if (class == BPF_LDX) { |
| 1859 | if (!(*reg_mask & dreg)) |
| 1860 | return 0; |
| 1861 | *reg_mask &= ~dreg; |
| 1862 | |
| 1863 | /* scalars can only be spilled into stack w/o losing precision. |
| 1864 | * Load from any other memory can be zero extended. |
| 1865 | * The desire to keep that precision is already indicated |
| 1866 | * by 'precise' mark in corresponding register of this state. |
| 1867 | * No further tracking necessary. |
| 1868 | */ |
| 1869 | if (insn->src_reg != BPF_REG_FP) |
| 1870 | return 0; |
| 1871 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 1872 | return 0; |
| 1873 | |
| 1874 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. |
| 1875 | * that [fp - off] slot contains scalar that needs to be |
| 1876 | * tracked with precision |
| 1877 | */ |
| 1878 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 1879 | if (spi >= 64) { |
| 1880 | verbose(env, "BUG spi %d\n", spi); |
| 1881 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1882 | return -EFAULT; |
| 1883 | } |
| 1884 | *stack_mask |= 1ull << spi; |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 1885 | } else if (class == BPF_STX || class == BPF_ST) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1886 | if (*reg_mask & dreg) |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 1887 | /* stx & st shouldn't be using _scalar_ dst_reg |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1888 | * to access memory. It means backtracking |
| 1889 | * encountered a case of pointer subtraction. |
| 1890 | */ |
| 1891 | return -ENOTSUPP; |
| 1892 | /* scalars can only be spilled into stack */ |
| 1893 | if (insn->dst_reg != BPF_REG_FP) |
| 1894 | return 0; |
| 1895 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 1896 | return 0; |
| 1897 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 1898 | if (spi >= 64) { |
| 1899 | verbose(env, "BUG spi %d\n", spi); |
| 1900 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1901 | return -EFAULT; |
| 1902 | } |
| 1903 | if (!(*stack_mask & (1ull << spi))) |
| 1904 | return 0; |
| 1905 | *stack_mask &= ~(1ull << spi); |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 1906 | if (class == BPF_STX) |
| 1907 | *reg_mask |= sreg; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1908 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
| 1909 | if (opcode == BPF_CALL) { |
| 1910 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1911 | return -ENOTSUPP; |
| 1912 | /* regular helper call sets R0 */ |
| 1913 | *reg_mask &= ~1; |
| 1914 | if (*reg_mask & 0x3f) { |
| 1915 | /* if backtracing was looking for registers R1-R5 |
| 1916 | * they should have been found already. |
| 1917 | */ |
| 1918 | verbose(env, "BUG regs %x\n", *reg_mask); |
| 1919 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1920 | return -EFAULT; |
| 1921 | } |
| 1922 | } else if (opcode == BPF_EXIT) { |
| 1923 | return -ENOTSUPP; |
| 1924 | } |
| 1925 | } else if (class == BPF_LD) { |
| 1926 | if (!(*reg_mask & dreg)) |
| 1927 | return 0; |
| 1928 | *reg_mask &= ~dreg; |
| 1929 | /* It's ld_imm64 or ld_abs or ld_ind. |
| 1930 | * For ld_imm64 no further tracking of precision |
| 1931 | * into parent is necessary |
| 1932 | */ |
| 1933 | if (mode == BPF_IND || mode == BPF_ABS) |
| 1934 | /* to be analyzed */ |
| 1935 | return -ENOTSUPP; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1936 | } |
| 1937 | return 0; |
| 1938 | } |
| 1939 | |
| 1940 | /* the scalar precision tracking algorithm: |
| 1941 | * . at the start all registers have precise=false. |
| 1942 | * . scalar ranges are tracked as normal through alu and jmp insns. |
| 1943 | * . once precise value of the scalar register is used in: |
| 1944 | * . ptr + scalar alu |
| 1945 | * . if (scalar cond K|scalar) |
| 1946 | * . helper_call(.., scalar, ...) where ARG_CONST is expected |
| 1947 | * backtrack through the verifier states and mark all registers and |
| 1948 | * stack slots with spilled constants that these scalar regisers |
| 1949 | * should be precise. |
| 1950 | * . during state pruning two registers (or spilled stack slots) |
| 1951 | * are equivalent if both are not precise. |
| 1952 | * |
| 1953 | * Note the verifier cannot simply walk register parentage chain, |
| 1954 | * since many different registers and stack slots could have been |
| 1955 | * used to compute single precise scalar. |
| 1956 | * |
| 1957 | * The approach of starting with precise=true for all registers and then |
| 1958 | * backtrack to mark a register as not precise when the verifier detects |
| 1959 | * that program doesn't care about specific value (e.g., when helper |
| 1960 | * takes register as ARG_ANYTHING parameter) is not safe. |
| 1961 | * |
| 1962 | * It's ok to walk single parentage chain of the verifier states. |
| 1963 | * It's possible that this backtracking will go all the way till 1st insn. |
| 1964 | * All other branches will be explored for needing precision later. |
| 1965 | * |
| 1966 | * The backtracking needs to deal with cases like: |
| 1967 | * 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) |
| 1968 | * r9 -= r8 |
| 1969 | * r5 = r9 |
| 1970 | * if r5 > 0x79f goto pc+7 |
| 1971 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) |
| 1972 | * r5 += 1 |
| 1973 | * ... |
| 1974 | * call bpf_perf_event_output#25 |
| 1975 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO |
| 1976 | * |
| 1977 | * and this case: |
| 1978 | * r6 = 1 |
| 1979 | * call foo // uses callee's r6 inside to compute r0 |
| 1980 | * r0 += r6 |
| 1981 | * if r0 == 0 goto |
| 1982 | * |
| 1983 | * to track above reg_mask/stack_mask needs to be independent for each frame. |
| 1984 | * |
| 1985 | * Also if parent's curframe > frame where backtracking started, |
| 1986 | * the verifier need to mark registers in both frames, otherwise callees |
| 1987 | * may incorrectly prune callers. This is similar to |
| 1988 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") |
| 1989 | * |
| 1990 | * For now backtracking falls back into conservative marking. |
| 1991 | */ |
| 1992 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, |
| 1993 | struct bpf_verifier_state *st) |
| 1994 | { |
| 1995 | struct bpf_func_state *func; |
| 1996 | struct bpf_reg_state *reg; |
| 1997 | int i, j; |
| 1998 | |
| 1999 | /* big hammer: mark all scalars precise in this path. |
| 2000 | * pop_stack may still get !precise scalars. |
| 2001 | */ |
| 2002 | for (; st; st = st->parent) |
| 2003 | for (i = 0; i <= st->curframe; i++) { |
| 2004 | func = st->frame[i]; |
| 2005 | for (j = 0; j < BPF_REG_FP; j++) { |
| 2006 | reg = &func->regs[j]; |
| 2007 | if (reg->type != SCALAR_VALUE) |
| 2008 | continue; |
| 2009 | reg->precise = true; |
| 2010 | } |
| 2011 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { |
| 2012 | if (func->stack[j].slot_type[0] != STACK_SPILL) |
| 2013 | continue; |
| 2014 | reg = &func->stack[j].spilled_ptr; |
| 2015 | if (reg->type != SCALAR_VALUE) |
| 2016 | continue; |
| 2017 | reg->precise = true; |
| 2018 | } |
| 2019 | } |
| 2020 | } |
| 2021 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2022 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
| 2023 | int spi) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2024 | { |
| 2025 | struct bpf_verifier_state *st = env->cur_state; |
| 2026 | int first_idx = st->first_insn_idx; |
| 2027 | int last_idx = env->insn_idx; |
| 2028 | struct bpf_func_state *func; |
| 2029 | struct bpf_reg_state *reg; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2030 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
| 2031 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2032 | bool skip_first = true; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2033 | bool new_marks = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2034 | int i, err; |
| 2035 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2036 | if (!env->bpf_capable) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2037 | return 0; |
| 2038 | |
| 2039 | func = st->frame[st->curframe]; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2040 | if (regno >= 0) { |
| 2041 | reg = &func->regs[regno]; |
| 2042 | if (reg->type != SCALAR_VALUE) { |
| 2043 | WARN_ONCE(1, "backtracing misuse"); |
| 2044 | return -EFAULT; |
| 2045 | } |
| 2046 | if (!reg->precise) |
| 2047 | new_marks = true; |
| 2048 | else |
| 2049 | reg_mask = 0; |
| 2050 | reg->precise = true; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2051 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2052 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2053 | while (spi >= 0) { |
| 2054 | if (func->stack[spi].slot_type[0] != STACK_SPILL) { |
| 2055 | stack_mask = 0; |
| 2056 | break; |
| 2057 | } |
| 2058 | reg = &func->stack[spi].spilled_ptr; |
| 2059 | if (reg->type != SCALAR_VALUE) { |
| 2060 | stack_mask = 0; |
| 2061 | break; |
| 2062 | } |
| 2063 | if (!reg->precise) |
| 2064 | new_marks = true; |
| 2065 | else |
| 2066 | stack_mask = 0; |
| 2067 | reg->precise = true; |
| 2068 | break; |
| 2069 | } |
| 2070 | |
| 2071 | if (!new_marks) |
| 2072 | return 0; |
| 2073 | if (!reg_mask && !stack_mask) |
| 2074 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2075 | for (;;) { |
| 2076 | DECLARE_BITMAP(mask, 64); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2077 | u32 history = st->jmp_history_cnt; |
| 2078 | |
| 2079 | if (env->log.level & BPF_LOG_LEVEL) |
| 2080 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
| 2081 | for (i = last_idx;;) { |
| 2082 | if (skip_first) { |
| 2083 | err = 0; |
| 2084 | skip_first = false; |
| 2085 | } else { |
| 2086 | err = backtrack_insn(env, i, ®_mask, &stack_mask); |
| 2087 | } |
| 2088 | if (err == -ENOTSUPP) { |
| 2089 | mark_all_scalars_precise(env, st); |
| 2090 | return 0; |
| 2091 | } else if (err) { |
| 2092 | return err; |
| 2093 | } |
| 2094 | if (!reg_mask && !stack_mask) |
| 2095 | /* Found assignment(s) into tracked register in this state. |
| 2096 | * Since this state is already marked, just return. |
| 2097 | * Nothing to be tracked further in the parent state. |
| 2098 | */ |
| 2099 | return 0; |
| 2100 | if (i == first_idx) |
| 2101 | break; |
| 2102 | i = get_prev_insn_idx(st, i, &history); |
| 2103 | if (i >= env->prog->len) { |
| 2104 | /* This can happen if backtracking reached insn 0 |
| 2105 | * and there are still reg_mask or stack_mask |
| 2106 | * to backtrack. |
| 2107 | * It means the backtracking missed the spot where |
| 2108 | * particular register was initialized with a constant. |
| 2109 | */ |
| 2110 | verbose(env, "BUG backtracking idx %d\n", i); |
| 2111 | WARN_ONCE(1, "verifier backtracking bug"); |
| 2112 | return -EFAULT; |
| 2113 | } |
| 2114 | } |
| 2115 | st = st->parent; |
| 2116 | if (!st) |
| 2117 | break; |
| 2118 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2119 | new_marks = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2120 | func = st->frame[st->curframe]; |
| 2121 | bitmap_from_u64(mask, reg_mask); |
| 2122 | for_each_set_bit(i, mask, 32) { |
| 2123 | reg = &func->regs[i]; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2124 | if (reg->type != SCALAR_VALUE) { |
| 2125 | reg_mask &= ~(1u << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2126 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2127 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2128 | if (!reg->precise) |
| 2129 | new_marks = true; |
| 2130 | reg->precise = true; |
| 2131 | } |
| 2132 | |
| 2133 | bitmap_from_u64(mask, stack_mask); |
| 2134 | for_each_set_bit(i, mask, 64) { |
| 2135 | if (i >= func->allocated_stack / BPF_REG_SIZE) { |
Alexei Starovoitov | 2339cd6 | 2019-09-03 15:16:17 -0700 | [diff] [blame] | 2136 | /* the sequence of instructions: |
| 2137 | * 2: (bf) r3 = r10 |
| 2138 | * 3: (7b) *(u64 *)(r3 -8) = r0 |
| 2139 | * 4: (79) r4 = *(u64 *)(r10 -8) |
| 2140 | * doesn't contain jmps. It's backtracked |
| 2141 | * as a single block. |
| 2142 | * During backtracking insn 3 is not recognized as |
| 2143 | * stack access, so at the end of backtracking |
| 2144 | * stack slot fp-8 is still marked in stack_mask. |
| 2145 | * However the parent state may not have accessed |
| 2146 | * fp-8 and it's "unallocated" stack space. |
| 2147 | * In such case fallback to conservative. |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2148 | */ |
Alexei Starovoitov | 2339cd6 | 2019-09-03 15:16:17 -0700 | [diff] [blame] | 2149 | mark_all_scalars_precise(env, st); |
| 2150 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2153 | if (func->stack[i].slot_type[0] != STACK_SPILL) { |
| 2154 | stack_mask &= ~(1ull << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2155 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2156 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2157 | reg = &func->stack[i].spilled_ptr; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2158 | if (reg->type != SCALAR_VALUE) { |
| 2159 | stack_mask &= ~(1ull << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2160 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2161 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2162 | if (!reg->precise) |
| 2163 | new_marks = true; |
| 2164 | reg->precise = true; |
| 2165 | } |
| 2166 | if (env->log.level & BPF_LOG_LEVEL) { |
| 2167 | print_verifier_state(env, func); |
| 2168 | verbose(env, "parent %s regs=%x stack=%llx marks\n", |
| 2169 | new_marks ? "didn't have" : "already had", |
| 2170 | reg_mask, stack_mask); |
| 2171 | } |
| 2172 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2173 | if (!reg_mask && !stack_mask) |
| 2174 | break; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2175 | if (!new_marks) |
| 2176 | break; |
| 2177 | |
| 2178 | last_idx = st->last_insn_idx; |
| 2179 | first_idx = st->first_insn_idx; |
| 2180 | } |
| 2181 | return 0; |
| 2182 | } |
| 2183 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2184 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
| 2185 | { |
| 2186 | return __mark_chain_precision(env, regno, -1); |
| 2187 | } |
| 2188 | |
| 2189 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) |
| 2190 | { |
| 2191 | return __mark_chain_precision(env, -1, spi); |
| 2192 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2193 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2194 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 2195 | { |
| 2196 | switch (type) { |
| 2197 | case PTR_TO_MAP_VALUE: |
| 2198 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 2199 | case PTR_TO_STACK: |
| 2200 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2201 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2202 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2203 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2204 | case PTR_TO_FLOW_KEYS: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2205 | case CONST_PTR_TO_MAP: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2206 | case PTR_TO_SOCKET: |
| 2207 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2208 | case PTR_TO_SOCK_COMMON: |
| 2209 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2210 | case PTR_TO_TCP_SOCK: |
| 2211 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2212 | case PTR_TO_XDP_SOCK: |
Martin KaFai Lau | 65726b5 | 2020-01-08 16:34:54 -0800 | [diff] [blame] | 2213 | case PTR_TO_BTF_ID: |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 2214 | case PTR_TO_BTF_ID_OR_NULL: |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 2215 | case PTR_TO_RDONLY_BUF: |
| 2216 | case PTR_TO_RDONLY_BUF_OR_NULL: |
| 2217 | case PTR_TO_RDWR_BUF: |
| 2218 | case PTR_TO_RDWR_BUF_OR_NULL: |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 2219 | case PTR_TO_PERCPU_BTF_ID: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2220 | return true; |
| 2221 | default: |
| 2222 | return false; |
| 2223 | } |
| 2224 | } |
| 2225 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2226 | /* Does this register contain a constant zero? */ |
| 2227 | static bool register_is_null(struct bpf_reg_state *reg) |
| 2228 | { |
| 2229 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 2230 | } |
| 2231 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2232 | static bool register_is_const(struct bpf_reg_state *reg) |
| 2233 | { |
| 2234 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); |
| 2235 | } |
| 2236 | |
Yonghong Song | 5689d49 | 2020-10-08 18:12:38 -0700 | [diff] [blame] | 2237 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
| 2238 | { |
| 2239 | return tnum_is_unknown(reg->var_off) && |
| 2240 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && |
| 2241 | reg->umin_value == 0 && reg->umax_value == U64_MAX && |
| 2242 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && |
| 2243 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; |
| 2244 | } |
| 2245 | |
| 2246 | static bool register_is_bounded(struct bpf_reg_state *reg) |
| 2247 | { |
| 2248 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); |
| 2249 | } |
| 2250 | |
Jann Horn | 6e7e63c | 2020-04-17 02:00:06 +0200 | [diff] [blame] | 2251 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 2252 | const struct bpf_reg_state *reg) |
| 2253 | { |
| 2254 | if (allow_ptr_leaks) |
| 2255 | return false; |
| 2256 | |
| 2257 | return reg->type != SCALAR_VALUE; |
| 2258 | } |
| 2259 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2260 | static void save_register_state(struct bpf_func_state *state, |
| 2261 | int spi, struct bpf_reg_state *reg) |
| 2262 | { |
| 2263 | int i; |
| 2264 | |
| 2265 | state->stack[spi].spilled_ptr = *reg; |
| 2266 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 2267 | |
| 2268 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 2269 | state->stack[spi].slot_type[i] = STACK_SPILL; |
| 2270 | } |
| 2271 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2272 | /* check_stack_read/write functions track spill/fill of registers, |
| 2273 | * stack boundary and alignment are checked in check_mem_access() |
| 2274 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2275 | static int check_stack_write(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2276 | struct bpf_func_state *state, /* func where register points to */ |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2277 | int off, int size, int value_regno, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2278 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2279 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2280 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2281 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2282 | struct bpf_reg_state *reg = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2283 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2284 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2285 | state->acquired_refs, true); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2286 | if (err) |
| 2287 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2288 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 2289 | * so it's aligned access and [off, off + size) are within stack limits |
| 2290 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2291 | if (!env->allow_ptr_leaks && |
| 2292 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 2293 | size != BPF_REG_SIZE) { |
| 2294 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 2295 | return -EACCES; |
| 2296 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2297 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2298 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2299 | if (value_regno >= 0) |
| 2300 | reg = &cur->regs[value_regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2301 | |
Yonghong Song | 5689d49 | 2020-10-08 18:12:38 -0700 | [diff] [blame] | 2302 | if (reg && size == BPF_REG_SIZE && register_is_bounded(reg) && |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2303 | !register_is_null(reg) && env->bpf_capable) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2304 | if (dst_reg != BPF_REG_FP) { |
| 2305 | /* The backtracking logic can only recognize explicit |
| 2306 | * stack slot address like [fp - 8]. Other spill of |
| 2307 | * scalar via different register has to be conervative. |
| 2308 | * Backtrack from here and mark all registers as precise |
| 2309 | * that contributed into 'reg' being a constant. |
| 2310 | */ |
| 2311 | err = mark_chain_precision(env, value_regno); |
| 2312 | if (err) |
| 2313 | return err; |
| 2314 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2315 | save_register_state(state, spi, reg); |
| 2316 | } else if (reg && is_spillable_regtype(reg->type)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2317 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2318 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2319 | verbose_linfo(env, insn_idx, "; "); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2320 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2321 | return -EACCES; |
| 2322 | } |
| 2323 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2324 | if (state != cur && reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2325 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 2326 | return -EINVAL; |
| 2327 | } |
| 2328 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2329 | if (!env->bypass_spec_v4) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2330 | bool sanitize = false; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2331 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2332 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 2333 | register_is_const(&state->stack[spi].spilled_ptr)) |
| 2334 | sanitize = true; |
| 2335 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 2336 | if (state->stack[spi].slot_type[i] == STACK_MISC) { |
| 2337 | sanitize = true; |
| 2338 | break; |
| 2339 | } |
| 2340 | if (sanitize) { |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2341 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
| 2342 | int soff = (-spi - 1) * BPF_REG_SIZE; |
| 2343 | |
| 2344 | /* detected reuse of integer stack slot with a pointer |
| 2345 | * which means either llvm is reusing stack slot or |
| 2346 | * an attacker is trying to exploit CVE-2018-3639 |
| 2347 | * (speculative store bypass) |
| 2348 | * Have to sanitize that slot with preemptive |
| 2349 | * store of zero. |
| 2350 | */ |
| 2351 | if (*poff && *poff != soff) { |
| 2352 | /* disallow programs where single insn stores |
| 2353 | * into two different stack slots, since verifier |
| 2354 | * cannot sanitize them |
| 2355 | */ |
| 2356 | verbose(env, |
| 2357 | "insn %d cannot access two stack slots fp%d and fp%d", |
| 2358 | insn_idx, *poff, soff); |
| 2359 | return -EINVAL; |
| 2360 | } |
| 2361 | *poff = soff; |
| 2362 | } |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2363 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2364 | save_register_state(state, spi, reg); |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2365 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2366 | u8 type = STACK_MISC; |
| 2367 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2368 | /* regular write of data into stack destroys any spilled ptr */ |
| 2369 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2370 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
| 2371 | if (state->stack[spi].slot_type[0] == STACK_SPILL) |
| 2372 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 2373 | state->stack[spi].slot_type[i] = STACK_MISC; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2374 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2375 | /* only mark the slot as written if all 8 bytes were written |
| 2376 | * otherwise read propagation may incorrectly stop too soon |
| 2377 | * when stack slots are partially written. |
| 2378 | * This heuristic means that read propagation will be |
| 2379 | * conservative, since it will add reg_live_read marks |
| 2380 | * to stack slots all the way to first state when programs |
| 2381 | * writes+reads less than 8 bytes |
| 2382 | */ |
| 2383 | if (size == BPF_REG_SIZE) |
| 2384 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 2385 | |
| 2386 | /* when we zero initialize stack slots mark them as such */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2387 | if (reg && register_is_null(reg)) { |
| 2388 | /* backtracking doesn't work for STACK_ZERO yet. */ |
| 2389 | err = mark_chain_precision(env, value_regno); |
| 2390 | if (err) |
| 2391 | return err; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2392 | type = STACK_ZERO; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2393 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2394 | |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2395 | /* Mark slots affected by this stack write. */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2396 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2397 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2398 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2399 | } |
| 2400 | return 0; |
| 2401 | } |
| 2402 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2403 | static int check_stack_read(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2404 | struct bpf_func_state *reg_state /* func where register points to */, |
| 2405 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2406 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2407 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2408 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2409 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2410 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2411 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2412 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2413 | if (reg_state->allocated_stack <= slot) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2414 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 2415 | off, size); |
| 2416 | return -EACCES; |
| 2417 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2418 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2419 | reg = ®_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2420 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2421 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2422 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2423 | if (reg->type != SCALAR_VALUE) { |
| 2424 | verbose_linfo(env, env->insn_idx, "; "); |
| 2425 | verbose(env, "invalid size of register fill\n"); |
| 2426 | return -EACCES; |
| 2427 | } |
| 2428 | if (value_regno >= 0) { |
| 2429 | mark_reg_unknown(env, state->regs, value_regno); |
| 2430 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 2431 | } |
| 2432 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
| 2433 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2434 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2435 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2436 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2437 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2438 | return -EACCES; |
| 2439 | } |
| 2440 | } |
| 2441 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2442 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2443 | /* restore register state from stack */ |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2444 | state->regs[value_regno] = *reg; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 2445 | /* mark reg as written since spilled pointer state likely |
| 2446 | * has its liveness marks cleared by is_state_visited() |
| 2447 | * which resets stack/reg liveness for state transitions |
| 2448 | */ |
| 2449 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Jann Horn | 6e7e63c | 2020-04-17 02:00:06 +0200 | [diff] [blame] | 2450 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
| 2451 | /* If value_regno==-1, the caller is asking us whether |
| 2452 | * it is acceptable to use this value as a SCALAR_VALUE |
| 2453 | * (e.g. for XADD). |
| 2454 | * We must not allow unprivileged callers to do that |
| 2455 | * with spilled pointers. |
| 2456 | */ |
| 2457 | verbose(env, "leaking pointer from stack off %d\n", |
| 2458 | off); |
| 2459 | return -EACCES; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2460 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2461 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2462 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2463 | int zeros = 0; |
| 2464 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2465 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2466 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 2467 | continue; |
| 2468 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 2469 | zeros++; |
| 2470 | continue; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2471 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2472 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 2473 | off, i, size); |
| 2474 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2475 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2476 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2477 | if (value_regno >= 0) { |
| 2478 | if (zeros == size) { |
| 2479 | /* any size read into register is zero extended, |
| 2480 | * so the whole register == const_zero |
| 2481 | */ |
| 2482 | __mark_reg_const_zero(&state->regs[value_regno]); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2483 | /* backtracking doesn't support STACK_ZERO yet, |
| 2484 | * so mark it precise here, so that later |
| 2485 | * backtracking can stop here. |
| 2486 | * Backtracking may not need this if this register |
| 2487 | * doesn't participate in pointer adjustment. |
| 2488 | * Forward propagation of precise flag is not |
| 2489 | * necessary either. This mark is only to stop |
| 2490 | * backtracking. Any register that contributed |
| 2491 | * to const 0 was marked precise before spill. |
| 2492 | */ |
| 2493 | state->regs[value_regno].precise = true; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2494 | } else { |
| 2495 | /* have read misc data from the stack */ |
| 2496 | mark_reg_unknown(env, state->regs, value_regno); |
| 2497 | } |
| 2498 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 2499 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2500 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2501 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2502 | } |
| 2503 | |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 2504 | static int check_stack_access(struct bpf_verifier_env *env, |
| 2505 | const struct bpf_reg_state *reg, |
| 2506 | int off, int size) |
| 2507 | { |
| 2508 | /* Stack accesses must be at a fixed offset, so that we |
| 2509 | * can determine what type of data were returned. See |
| 2510 | * check_stack_read(). |
| 2511 | */ |
| 2512 | if (!tnum_is_const(reg->var_off)) { |
| 2513 | char tn_buf[48]; |
| 2514 | |
| 2515 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Andrey Ignatov | 1fbd20f | 2019-04-03 23:22:43 -0700 | [diff] [blame] | 2516 | verbose(env, "variable stack access var_off=%s off=%d size=%d\n", |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 2517 | tn_buf, off, size); |
| 2518 | return -EACCES; |
| 2519 | } |
| 2520 | |
| 2521 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 2522 | verbose(env, "invalid stack off=%d size=%d\n", off, size); |
| 2523 | return -EACCES; |
| 2524 | } |
| 2525 | |
| 2526 | return 0; |
| 2527 | } |
| 2528 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 2529 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
| 2530 | int off, int size, enum bpf_access_type type) |
| 2531 | { |
| 2532 | struct bpf_reg_state *regs = cur_regs(env); |
| 2533 | struct bpf_map *map = regs[regno].map_ptr; |
| 2534 | u32 cap = bpf_map_flags_to_cap(map); |
| 2535 | |
| 2536 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { |
| 2537 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", |
| 2538 | map->value_size, off, size); |
| 2539 | return -EACCES; |
| 2540 | } |
| 2541 | |
| 2542 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { |
| 2543 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", |
| 2544 | map->value_size, off, size); |
| 2545 | return -EACCES; |
| 2546 | } |
| 2547 | |
| 2548 | return 0; |
| 2549 | } |
| 2550 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2551 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
| 2552 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, |
| 2553 | int off, int size, u32 mem_size, |
| 2554 | bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2555 | { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2556 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
| 2557 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2558 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2559 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) |
| 2560 | return 0; |
| 2561 | |
| 2562 | reg = &cur_regs(env)[regno]; |
| 2563 | switch (reg->type) { |
| 2564 | case PTR_TO_MAP_VALUE: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2565 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2566 | mem_size, off, size); |
| 2567 | break; |
| 2568 | case PTR_TO_PACKET: |
| 2569 | case PTR_TO_PACKET_META: |
| 2570 | case PTR_TO_PACKET_END: |
| 2571 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
| 2572 | off, size, regno, reg->id, off, mem_size); |
| 2573 | break; |
| 2574 | case PTR_TO_MEM: |
| 2575 | default: |
| 2576 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", |
| 2577 | mem_size, off, size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2578 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2579 | |
| 2580 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2581 | } |
| 2582 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2583 | /* check read/write into a memory region with possible variable offset */ |
| 2584 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, |
| 2585 | int off, int size, u32 mem_size, |
| 2586 | bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2587 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2588 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2589 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2590 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 2591 | int err; |
| 2592 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2593 | /* We may have adjusted the register pointing to memory region, so we |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2594 | * need to try adding each of min_value and max_value to off |
| 2595 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2596 | */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 2597 | if (env->log.level & BPF_LOG_LEVEL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2598 | print_verifier_state(env, state); |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 2599 | |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2600 | /* The minimum value is only important with signed |
| 2601 | * comparisons where we can't assume the floor of a |
| 2602 | * value is 0. If we are using signed variables for our |
| 2603 | * index'es we need to make sure that whatever we use |
| 2604 | * will have a set floor within our range. |
| 2605 | */ |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 2606 | if (reg->smin_value < 0 && |
| 2607 | (reg->smin_value == S64_MIN || |
| 2608 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || |
| 2609 | reg->smin_value + off < 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2610 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2611 | regno); |
| 2612 | return -EACCES; |
| 2613 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2614 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
| 2615 | mem_size, zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2616 | if (err) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2617 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2618 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2619 | return err; |
| 2620 | } |
| 2621 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2622 | /* If we haven't set a max value then we need to bail since we can't be |
| 2623 | * sure we won't do bad things. |
| 2624 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2625 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2626 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2627 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2628 | regno); |
| 2629 | return -EACCES; |
| 2630 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2631 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
| 2632 | mem_size, zero_size_allowed); |
| 2633 | if (err) { |
| 2634 | verbose(env, "R%d max value is outside of the allowed memory range\n", |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2635 | regno); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2636 | return err; |
| 2637 | } |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 2638 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2639 | return 0; |
| 2640 | } |
| 2641 | |
| 2642 | /* check read/write into a map element with possible variable offset */ |
| 2643 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
| 2644 | int off, int size, bool zero_size_allowed) |
| 2645 | { |
| 2646 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2647 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2648 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 2649 | struct bpf_map *map = reg->map_ptr; |
| 2650 | int err; |
| 2651 | |
| 2652 | err = check_mem_region_access(env, regno, off, size, map->value_size, |
| 2653 | zero_size_allowed); |
| 2654 | if (err) |
| 2655 | return err; |
| 2656 | |
| 2657 | if (map_value_has_spin_lock(map)) { |
| 2658 | u32 lock = map->spin_lock_off; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 2659 | |
| 2660 | /* if any part of struct bpf_spin_lock can be touched by |
| 2661 | * load/store reject this program. |
| 2662 | * To check that [x1, x2) overlaps with [y1, y2) |
| 2663 | * it is sufficient to check x1 < y2 && y1 < x2. |
| 2664 | */ |
| 2665 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && |
| 2666 | lock < reg->umax_value + off + size) { |
| 2667 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); |
| 2668 | return -EACCES; |
| 2669 | } |
| 2670 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2671 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2672 | } |
| 2673 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2674 | #define MAX_PACKET_OFF 0xffff |
| 2675 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 2676 | static enum bpf_prog_type resolve_prog_type(struct bpf_prog *prog) |
| 2677 | { |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 2678 | return prog->aux->dst_prog ? prog->aux->dst_prog->type : prog->type; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 2679 | } |
| 2680 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2681 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2682 | const struct bpf_call_arg_meta *meta, |
| 2683 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2684 | { |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 2685 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
| 2686 | |
| 2687 | switch (prog_type) { |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 2688 | /* Program types only with direct read access go here! */ |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2689 | case BPF_PROG_TYPE_LWT_IN: |
| 2690 | case BPF_PROG_TYPE_LWT_OUT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 2691 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2692 | case BPF_PROG_TYPE_SK_REUSEPORT: |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 2693 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Daniel Borkmann | d5563d3 | 2018-10-24 22:05:46 +0200 | [diff] [blame] | 2694 | case BPF_PROG_TYPE_CGROUP_SKB: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2695 | if (t == BPF_WRITE) |
| 2696 | return false; |
Gustavo A. R. Silva | 8731745 | 2020-10-02 18:42:17 -0500 | [diff] [blame] | 2697 | fallthrough; |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 2698 | |
| 2699 | /* Program types with direct read + write access go here! */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2700 | case BPF_PROG_TYPE_SCHED_CLS: |
| 2701 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2702 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2703 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 2704 | case BPF_PROG_TYPE_SK_SKB: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2705 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2706 | if (meta) |
| 2707 | return meta->pkt_access; |
| 2708 | |
| 2709 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2710 | return true; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2711 | |
| 2712 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 2713 | if (t == BPF_WRITE) |
| 2714 | env->seen_direct_write = true; |
| 2715 | |
| 2716 | return true; |
| 2717 | |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2718 | default: |
| 2719 | return false; |
| 2720 | } |
| 2721 | } |
| 2722 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2723 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2724 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2725 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2726 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2727 | struct bpf_reg_state *reg = ®s[regno]; |
| 2728 | int err; |
| 2729 | |
| 2730 | /* We may have added a variable offset to the packet pointer; but any |
| 2731 | * reg->range we have comes after that. We are only checking the fixed |
| 2732 | * offset. |
| 2733 | */ |
| 2734 | |
| 2735 | /* We don't allow negative numbers, because we aren't tracking enough |
| 2736 | * detail to prove they're safe. |
| 2737 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2738 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2739 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2740 | regno); |
| 2741 | return -EACCES; |
| 2742 | } |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 2743 | |
| 2744 | err = reg->range < 0 ? -EINVAL : |
| 2745 | __check_mem_access(env, regno, off, size, reg->range, |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2746 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2747 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2748 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2749 | return err; |
| 2750 | } |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 2751 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2752 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 2753 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 2754 | * otherwise find_good_pkt_pointers would have refused to set range info |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2755 | * that __check_mem_access would have rejected this pkt access. |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 2756 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 2757 | */ |
| 2758 | env->prog->aux->max_pkt_offset = |
| 2759 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 2760 | off + reg->umax_value + size - 1); |
| 2761 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2762 | return err; |
| 2763 | } |
| 2764 | |
| 2765 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2766 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2767 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 2768 | struct btf **btf, u32 *btf_id) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2769 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 2770 | struct bpf_insn_access_aux info = { |
| 2771 | .reg_type = *reg_type, |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2772 | .log = &env->log, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 2773 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2774 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 2775 | if (env->ops->is_valid_access && |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2776 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 2777 | /* A non zero info.ctx_field_size indicates that this field is a |
| 2778 | * candidate for later verifier transformation to load the whole |
| 2779 | * field and then apply a mask when accessed with a narrower |
| 2780 | * access than actual ctx access size. A zero info.ctx_field_size |
| 2781 | * will only allow for whole field access and rejects any other |
| 2782 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2783 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 2784 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2785 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 2786 | if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL) { |
| 2787 | *btf = info.btf; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2788 | *btf_id = info.btf_id; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 2789 | } else { |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2790 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 2791 | } |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 2792 | /* remember the offset of last byte accessed in ctx */ |
| 2793 | if (env->prog->aux->max_ctx_offset < off + size) |
| 2794 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2795 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 2796 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2797 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2798 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2799 | return -EACCES; |
| 2800 | } |
| 2801 | |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2802 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 2803 | int size) |
| 2804 | { |
| 2805 | if (size < 0 || off < 0 || |
| 2806 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 2807 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 2808 | off, size); |
| 2809 | return -EACCES; |
| 2810 | } |
| 2811 | return 0; |
| 2812 | } |
| 2813 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2814 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
| 2815 | u32 regno, int off, int size, |
| 2816 | enum bpf_access_type t) |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2817 | { |
| 2818 | struct bpf_reg_state *regs = cur_regs(env); |
| 2819 | struct bpf_reg_state *reg = ®s[regno]; |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2820 | struct bpf_insn_access_aux info = {}; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2821 | bool valid; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2822 | |
| 2823 | if (reg->smin_value < 0) { |
| 2824 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 2825 | regno); |
| 2826 | return -EACCES; |
| 2827 | } |
| 2828 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2829 | switch (reg->type) { |
| 2830 | case PTR_TO_SOCK_COMMON: |
| 2831 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); |
| 2832 | break; |
| 2833 | case PTR_TO_SOCKET: |
| 2834 | valid = bpf_sock_is_valid_access(off, size, t, &info); |
| 2835 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2836 | case PTR_TO_TCP_SOCK: |
| 2837 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); |
| 2838 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2839 | case PTR_TO_XDP_SOCK: |
| 2840 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); |
| 2841 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2842 | default: |
| 2843 | valid = false; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2844 | } |
| 2845 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2846 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2847 | if (valid) { |
| 2848 | env->insn_aux_data[insn_idx].ctx_field_size = |
| 2849 | info.ctx_field_size; |
| 2850 | return 0; |
| 2851 | } |
| 2852 | |
| 2853 | verbose(env, "R%d invalid %s access off=%d size=%d\n", |
| 2854 | regno, reg_type_str[reg->type], off, size); |
| 2855 | |
| 2856 | return -EACCES; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2857 | } |
| 2858 | |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2859 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
| 2860 | { |
| 2861 | return cur_regs(env) + regno; |
| 2862 | } |
| 2863 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2864 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 2865 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2866 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2867 | } |
| 2868 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2869 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 2870 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2871 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2872 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2873 | return reg->type == PTR_TO_CTX; |
| 2874 | } |
| 2875 | |
| 2876 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) |
| 2877 | { |
| 2878 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 2879 | |
| 2880 | return type_is_sk_pointer(reg->type); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2881 | } |
| 2882 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2883 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 2884 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2885 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2886 | |
| 2887 | return type_is_pkt_pointer(reg->type); |
| 2888 | } |
| 2889 | |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 2890 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 2891 | { |
| 2892 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 2893 | |
| 2894 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 2895 | return reg->type == PTR_TO_FLOW_KEYS; |
| 2896 | } |
| 2897 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2898 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 2899 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2900 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2901 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2902 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2903 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2904 | |
| 2905 | /* Byte size accesses are always allowed. */ |
| 2906 | if (!strict || size == 1) |
| 2907 | return 0; |
| 2908 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 2909 | /* For platforms that do not have a Kconfig enabling |
| 2910 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 2911 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 2912 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 2913 | * to this code only in strict mode where we want to emulate |
| 2914 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 2915 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2916 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 2917 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2918 | |
| 2919 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 2920 | if (!tnum_is_aligned(reg_off, size)) { |
| 2921 | char tn_buf[48]; |
| 2922 | |
| 2923 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2924 | verbose(env, |
| 2925 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2926 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2927 | return -EACCES; |
| 2928 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2929 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2930 | return 0; |
| 2931 | } |
| 2932 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2933 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 2934 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2935 | const char *pointer_desc, |
| 2936 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2937 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2938 | struct tnum reg_off; |
| 2939 | |
| 2940 | /* Byte size accesses are always allowed. */ |
| 2941 | if (!strict || size == 1) |
| 2942 | return 0; |
| 2943 | |
| 2944 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 2945 | if (!tnum_is_aligned(reg_off, size)) { |
| 2946 | char tn_buf[48]; |
| 2947 | |
| 2948 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2949 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2950 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2951 | return -EACCES; |
| 2952 | } |
| 2953 | |
| 2954 | return 0; |
| 2955 | } |
| 2956 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2957 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2958 | const struct bpf_reg_state *reg, int off, |
| 2959 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2960 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2961 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2962 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2963 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2964 | switch (reg->type) { |
| 2965 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2966 | case PTR_TO_PACKET_META: |
| 2967 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 2968 | * right in front, treat it the very same way. |
| 2969 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2970 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2971 | case PTR_TO_FLOW_KEYS: |
| 2972 | pointer_desc = "flow keys "; |
| 2973 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2974 | case PTR_TO_MAP_VALUE: |
| 2975 | pointer_desc = "value "; |
| 2976 | break; |
| 2977 | case PTR_TO_CTX: |
| 2978 | pointer_desc = "context "; |
| 2979 | break; |
| 2980 | case PTR_TO_STACK: |
| 2981 | pointer_desc = "stack "; |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 2982 | /* The stack spill tracking logic in check_stack_write() |
| 2983 | * and check_stack_read() relies on stack accesses being |
| 2984 | * aligned. |
| 2985 | */ |
| 2986 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2987 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2988 | case PTR_TO_SOCKET: |
| 2989 | pointer_desc = "sock "; |
| 2990 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2991 | case PTR_TO_SOCK_COMMON: |
| 2992 | pointer_desc = "sock_common "; |
| 2993 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2994 | case PTR_TO_TCP_SOCK: |
| 2995 | pointer_desc = "tcp_sock "; |
| 2996 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2997 | case PTR_TO_XDP_SOCK: |
| 2998 | pointer_desc = "xdp_sock "; |
| 2999 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3000 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3001 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3002 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3003 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 3004 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3005 | } |
| 3006 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3007 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 3008 | const struct bpf_func_state *func, |
| 3009 | int off) |
| 3010 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3011 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3012 | |
| 3013 | if (stack >= -off) |
| 3014 | return 0; |
| 3015 | |
| 3016 | /* update known max for given subprogram */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3017 | env->subprog_info[func->subprogno].stack_depth = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3018 | return 0; |
| 3019 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3020 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3021 | /* starting from main bpf function walk all instructions of the function |
| 3022 | * and recursively walk all callees that given function can call. |
| 3023 | * Ignore jump and exit insns. |
| 3024 | * Since recursion is prevented by check_cfg() this algorithm |
| 3025 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 3026 | */ |
| 3027 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 3028 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3029 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 3030 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3031 | struct bpf_insn *insn = env->prog->insnsi; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3032 | bool tail_call_reachable = false; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3033 | int ret_insn[MAX_CALL_FRAMES]; |
| 3034 | int ret_prog[MAX_CALL_FRAMES]; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3035 | int j; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3036 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3037 | process_func: |
Maciej Fijalkowski | 7f6e431 | 2020-09-16 23:10:07 +0200 | [diff] [blame] | 3038 | /* protect against potential stack overflow that might happen when |
| 3039 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack |
| 3040 | * depth for such case down to 256 so that the worst case scenario |
| 3041 | * would result in 8k stack size (32 which is tailcall limit * 256 = |
| 3042 | * 8k). |
| 3043 | * |
| 3044 | * To get the idea what might happen, see an example: |
| 3045 | * func1 -> sub rsp, 128 |
| 3046 | * subfunc1 -> sub rsp, 256 |
| 3047 | * tailcall1 -> add rsp, 256 |
| 3048 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) |
| 3049 | * subfunc2 -> sub rsp, 64 |
| 3050 | * subfunc22 -> sub rsp, 128 |
| 3051 | * tailcall2 -> add rsp, 128 |
| 3052 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) |
| 3053 | * |
| 3054 | * tailcall will unwind the current stack frame but it will not get rid |
| 3055 | * of caller's stack as shown on the example above. |
| 3056 | */ |
| 3057 | if (idx && subprog[idx].has_tail_call && depth >= 256) { |
| 3058 | verbose(env, |
| 3059 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", |
| 3060 | depth); |
| 3061 | return -EACCES; |
| 3062 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3063 | /* round up to 32-bytes, since this is granularity |
| 3064 | * of interpreter stack size |
| 3065 | */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3066 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3067 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3068 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3069 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3070 | return -EACCES; |
| 3071 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3072 | continue_func: |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 3073 | subprog_end = subprog[idx + 1].start; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3074 | for (; i < subprog_end; i++) { |
| 3075 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 3076 | continue; |
| 3077 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 3078 | continue; |
| 3079 | /* remember insn and function to return to */ |
| 3080 | ret_insn[frame] = i + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3081 | ret_prog[frame] = idx; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3082 | |
| 3083 | /* find the callee */ |
| 3084 | i = i + insn[i].imm + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3085 | idx = find_subprog(env, i); |
| 3086 | if (idx < 0) { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3087 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 3088 | i); |
| 3089 | return -EFAULT; |
| 3090 | } |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3091 | |
| 3092 | if (subprog[idx].has_tail_call) |
| 3093 | tail_call_reachable = true; |
| 3094 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3095 | frame++; |
| 3096 | if (frame >= MAX_CALL_FRAMES) { |
Paul Chaignon | 927cb78 | 2019-03-20 13:58:27 +0100 | [diff] [blame] | 3097 | verbose(env, "the call stack of %d frames is too deep !\n", |
| 3098 | frame); |
| 3099 | return -E2BIG; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3100 | } |
| 3101 | goto process_func; |
| 3102 | } |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3103 | /* if tail call got detected across bpf2bpf calls then mark each of the |
| 3104 | * currently present subprog frames as tail call reachable subprogs; |
| 3105 | * this info will be utilized by JIT so that we will be preserving the |
| 3106 | * tail call counter throughout bpf2bpf calls combined with tailcalls |
| 3107 | */ |
| 3108 | if (tail_call_reachable) |
| 3109 | for (j = 0; j < frame; j++) |
| 3110 | subprog[ret_prog[j]].tail_call_reachable = true; |
| 3111 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3112 | /* end of for() loop means the last insn of the 'subprog' |
| 3113 | * was reached. Doesn't matter whether it was JA or EXIT |
| 3114 | */ |
| 3115 | if (frame == 0) |
| 3116 | return 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3117 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3118 | frame--; |
| 3119 | i = ret_insn[frame]; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3120 | idx = ret_prog[frame]; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3121 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3122 | } |
| 3123 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 3124 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 3125 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 3126 | const struct bpf_insn *insn, int idx) |
| 3127 | { |
| 3128 | int start = idx + insn->imm + 1, subprog; |
| 3129 | |
| 3130 | subprog = find_subprog(env, start); |
| 3131 | if (subprog < 0) { |
| 3132 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 3133 | start); |
| 3134 | return -EFAULT; |
| 3135 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3136 | return env->subprog_info[subprog].stack_depth; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 3137 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 3138 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 3139 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 3140 | int check_ctx_reg(struct bpf_verifier_env *env, |
| 3141 | const struct bpf_reg_state *reg, int regno) |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 3142 | { |
| 3143 | /* Access to ctx or passing it to a helper is only allowed in |
| 3144 | * its original, unmodified form. |
| 3145 | */ |
| 3146 | |
| 3147 | if (reg->off) { |
| 3148 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 3149 | regno, reg->off); |
| 3150 | return -EACCES; |
| 3151 | } |
| 3152 | |
| 3153 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 3154 | char tn_buf[48]; |
| 3155 | |
| 3156 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3157 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 3158 | return -EACCES; |
| 3159 | } |
| 3160 | |
| 3161 | return 0; |
| 3162 | } |
| 3163 | |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3164 | static int __check_buffer_access(struct bpf_verifier_env *env, |
| 3165 | const char *buf_info, |
| 3166 | const struct bpf_reg_state *reg, |
| 3167 | int regno, int off, int size) |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3168 | { |
| 3169 | if (off < 0) { |
| 3170 | verbose(env, |
Yonghong Song | 4fc00b7 | 2020-07-28 15:18:01 -0700 | [diff] [blame] | 3171 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3172 | regno, buf_info, off, size); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3173 | return -EACCES; |
| 3174 | } |
| 3175 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 3176 | char tn_buf[48]; |
| 3177 | |
| 3178 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3179 | verbose(env, |
Yonghong Song | 4fc00b7 | 2020-07-28 15:18:01 -0700 | [diff] [blame] | 3180 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3181 | regno, off, tn_buf); |
| 3182 | return -EACCES; |
| 3183 | } |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3184 | |
| 3185 | return 0; |
| 3186 | } |
| 3187 | |
| 3188 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
| 3189 | const struct bpf_reg_state *reg, |
| 3190 | int regno, int off, int size) |
| 3191 | { |
| 3192 | int err; |
| 3193 | |
| 3194 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); |
| 3195 | if (err) |
| 3196 | return err; |
| 3197 | |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3198 | if (off + size > env->prog->aux->max_tp_access) |
| 3199 | env->prog->aux->max_tp_access = off + size; |
| 3200 | |
| 3201 | return 0; |
| 3202 | } |
| 3203 | |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3204 | static int check_buffer_access(struct bpf_verifier_env *env, |
| 3205 | const struct bpf_reg_state *reg, |
| 3206 | int regno, int off, int size, |
| 3207 | bool zero_size_allowed, |
| 3208 | const char *buf_info, |
| 3209 | u32 *max_access) |
| 3210 | { |
| 3211 | int err; |
| 3212 | |
| 3213 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); |
| 3214 | if (err) |
| 3215 | return err; |
| 3216 | |
| 3217 | if (off + size > *max_access) |
| 3218 | *max_access = off + size; |
| 3219 | |
| 3220 | return 0; |
| 3221 | } |
| 3222 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 3223 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
| 3224 | static void zext_32_to_64(struct bpf_reg_state *reg) |
| 3225 | { |
| 3226 | reg->var_off = tnum_subreg(reg->var_off); |
| 3227 | __reg_assign_32_into_64(reg); |
| 3228 | } |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3229 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3230 | /* truncate register to smaller size (in bytes) |
| 3231 | * must be called with size < BPF_REG_SIZE |
| 3232 | */ |
| 3233 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 3234 | { |
| 3235 | u64 mask; |
| 3236 | |
| 3237 | /* clear high bits in bit representation */ |
| 3238 | reg->var_off = tnum_cast(reg->var_off, size); |
| 3239 | |
| 3240 | /* fix arithmetic bounds */ |
| 3241 | mask = ((u64)1 << (size * 8)) - 1; |
| 3242 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 3243 | reg->umin_value &= mask; |
| 3244 | reg->umax_value &= mask; |
| 3245 | } else { |
| 3246 | reg->umin_value = 0; |
| 3247 | reg->umax_value = mask; |
| 3248 | } |
| 3249 | reg->smin_value = reg->umin_value; |
| 3250 | reg->smax_value = reg->umax_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 3251 | |
| 3252 | /* If size is smaller than 32bit register the 32bit register |
| 3253 | * values are also truncated so we push 64-bit bounds into |
| 3254 | * 32-bit bounds. Above were truncated < 32-bits already. |
| 3255 | */ |
| 3256 | if (size >= 4) |
| 3257 | return; |
| 3258 | __reg_combine_64_into_32(reg); |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3259 | } |
| 3260 | |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 3261 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
| 3262 | { |
| 3263 | return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen; |
| 3264 | } |
| 3265 | |
| 3266 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) |
| 3267 | { |
| 3268 | void *ptr; |
| 3269 | u64 addr; |
| 3270 | int err; |
| 3271 | |
| 3272 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 3273 | if (err) |
| 3274 | return err; |
Andrii Nakryiko | 2dedd7d | 2019-10-11 10:20:53 -0700 | [diff] [blame] | 3275 | ptr = (void *)(long)addr + off; |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 3276 | |
| 3277 | switch (size) { |
| 3278 | case sizeof(u8): |
| 3279 | *val = (u64)*(u8 *)ptr; |
| 3280 | break; |
| 3281 | case sizeof(u16): |
| 3282 | *val = (u64)*(u16 *)ptr; |
| 3283 | break; |
| 3284 | case sizeof(u32): |
| 3285 | *val = (u64)*(u32 *)ptr; |
| 3286 | break; |
| 3287 | case sizeof(u64): |
| 3288 | *val = *(u64 *)ptr; |
| 3289 | break; |
| 3290 | default: |
| 3291 | return -EINVAL; |
| 3292 | } |
| 3293 | return 0; |
| 3294 | } |
| 3295 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3296 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
| 3297 | struct bpf_reg_state *regs, |
| 3298 | int regno, int off, int size, |
| 3299 | enum bpf_access_type atype, |
| 3300 | int value_regno) |
| 3301 | { |
| 3302 | struct bpf_reg_state *reg = regs + regno; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3303 | const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); |
| 3304 | const char *tname = btf_name_by_offset(reg->btf, t->name_off); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3305 | u32 btf_id; |
| 3306 | int ret; |
| 3307 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3308 | if (off < 0) { |
| 3309 | verbose(env, |
| 3310 | "R%d is ptr_%s invalid negative access: off=%d\n", |
| 3311 | regno, tname, off); |
| 3312 | return -EACCES; |
| 3313 | } |
| 3314 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 3315 | char tn_buf[48]; |
| 3316 | |
| 3317 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3318 | verbose(env, |
| 3319 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", |
| 3320 | regno, tname, off, tn_buf); |
| 3321 | return -EACCES; |
| 3322 | } |
| 3323 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 3324 | if (env->ops->btf_struct_access) { |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3325 | ret = env->ops->btf_struct_access(&env->log, reg->btf, t, |
| 3326 | off, size, atype, &btf_id); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 3327 | } else { |
| 3328 | if (atype != BPF_READ) { |
| 3329 | verbose(env, "only read is supported\n"); |
| 3330 | return -EACCES; |
| 3331 | } |
| 3332 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3333 | ret = btf_struct_access(&env->log, reg->btf, t, off, size, |
| 3334 | atype, &btf_id); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 3335 | } |
| 3336 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3337 | if (ret < 0) |
| 3338 | return ret; |
| 3339 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 3340 | if (atype == BPF_READ && value_regno >= 0) |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3341 | mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 3342 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3343 | return 0; |
| 3344 | } |
| 3345 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 3346 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, |
| 3347 | struct bpf_reg_state *regs, |
| 3348 | int regno, int off, int size, |
| 3349 | enum bpf_access_type atype, |
| 3350 | int value_regno) |
| 3351 | { |
| 3352 | struct bpf_reg_state *reg = regs + regno; |
| 3353 | struct bpf_map *map = reg->map_ptr; |
| 3354 | const struct btf_type *t; |
| 3355 | const char *tname; |
| 3356 | u32 btf_id; |
| 3357 | int ret; |
| 3358 | |
| 3359 | if (!btf_vmlinux) { |
| 3360 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); |
| 3361 | return -ENOTSUPP; |
| 3362 | } |
| 3363 | |
| 3364 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { |
| 3365 | verbose(env, "map_ptr access not supported for map type %d\n", |
| 3366 | map->map_type); |
| 3367 | return -ENOTSUPP; |
| 3368 | } |
| 3369 | |
| 3370 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); |
| 3371 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 3372 | |
| 3373 | if (!env->allow_ptr_to_map_access) { |
| 3374 | verbose(env, |
| 3375 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", |
| 3376 | tname); |
| 3377 | return -EPERM; |
| 3378 | } |
| 3379 | |
| 3380 | if (off < 0) { |
| 3381 | verbose(env, "R%d is %s invalid negative access: off=%d\n", |
| 3382 | regno, tname, off); |
| 3383 | return -EACCES; |
| 3384 | } |
| 3385 | |
| 3386 | if (atype != BPF_READ) { |
| 3387 | verbose(env, "only read from %s is supported\n", tname); |
| 3388 | return -EACCES; |
| 3389 | } |
| 3390 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3391 | ret = btf_struct_access(&env->log, btf_vmlinux, t, off, size, atype, &btf_id); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 3392 | if (ret < 0) |
| 3393 | return ret; |
| 3394 | |
| 3395 | if (value_regno >= 0) |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3396 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_vmlinux, btf_id); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 3397 | |
| 3398 | return 0; |
| 3399 | } |
| 3400 | |
| 3401 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3402 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 3403 | * if t==write, value_regno is a register which value is stored into memory |
| 3404 | * if t==read, value_regno is a register which will receive the value from memory |
| 3405 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 3406 | * if t==read && value_regno==-1, don't care what we read from memory |
| 3407 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3408 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 3409 | int off, int bpf_size, enum bpf_access_type t, |
| 3410 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3411 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3412 | struct bpf_reg_state *regs = cur_regs(env); |
| 3413 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3414 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3415 | int size, err = 0; |
| 3416 | |
| 3417 | size = bpf_size_to_bytes(bpf_size); |
| 3418 | if (size < 0) |
| 3419 | return size; |
| 3420 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3421 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3422 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3423 | if (err) |
| 3424 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3425 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3426 | /* for access checks, reg->off is just part of off */ |
| 3427 | off += reg->off; |
| 3428 | |
| 3429 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3430 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3431 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3432 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3433 | return -EACCES; |
| 3434 | } |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3435 | err = check_map_access_type(env, regno, off, size, t); |
| 3436 | if (err) |
| 3437 | return err; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3438 | err = check_map_access(env, regno, off, size, false); |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 3439 | if (!err && t == BPF_READ && value_regno >= 0) { |
| 3440 | struct bpf_map *map = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3441 | |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 3442 | /* if map is read-only, track its contents as scalars */ |
| 3443 | if (tnum_is_const(reg->var_off) && |
| 3444 | bpf_map_is_rdonly(map) && |
| 3445 | map->ops->map_direct_value_addr) { |
| 3446 | int map_off = off + reg->var_off.value; |
| 3447 | u64 val = 0; |
| 3448 | |
| 3449 | err = bpf_map_direct_read(map, map_off, size, |
| 3450 | &val); |
| 3451 | if (err) |
| 3452 | return err; |
| 3453 | |
| 3454 | regs[value_regno].type = SCALAR_VALUE; |
| 3455 | __mark_reg_known(®s[value_regno], val); |
| 3456 | } else { |
| 3457 | mark_reg_unknown(env, regs, value_regno); |
| 3458 | } |
| 3459 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3460 | } else if (reg->type == PTR_TO_MEM) { |
| 3461 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3462 | is_pointer_value(env, value_regno)) { |
| 3463 | verbose(env, "R%d leaks addr into mem\n", value_regno); |
| 3464 | return -EACCES; |
| 3465 | } |
| 3466 | err = check_mem_region_access(env, regno, off, size, |
| 3467 | reg->mem_size, false); |
| 3468 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3469 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3470 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3471 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3472 | struct btf *btf = NULL; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3473 | u32 btf_id = 0; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 3474 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3475 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3476 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3477 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3478 | return -EACCES; |
| 3479 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3480 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 3481 | err = check_ctx_reg(env, reg, regno); |
| 3482 | if (err < 0) |
| 3483 | return err; |
| 3484 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3485 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, &btf_id); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3486 | if (err) |
| 3487 | verbose_linfo(env, insn_idx, "; "); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3488 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3489 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3490 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 3491 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3492 | */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3493 | if (reg_type == SCALAR_VALUE) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3494 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3495 | } else { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3496 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3497 | value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3498 | if (reg_type_may_be_null(reg_type)) |
| 3499 | regs[value_regno].id = ++env->id_gen; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 3500 | /* A load of ctx field could have different |
| 3501 | * actual load size with the one encoded in the |
| 3502 | * insn. When the dst is PTR, it is for sure not |
| 3503 | * a sub-register. |
| 3504 | */ |
| 3505 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 3506 | if (reg_type == PTR_TO_BTF_ID || |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3507 | reg_type == PTR_TO_BTF_ID_OR_NULL) { |
| 3508 | regs[value_regno].btf = btf; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3509 | regs[value_regno].btf_id = btf_id; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3510 | } |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3511 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3512 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3513 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3514 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3515 | } else if (reg->type == PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3516 | off += reg->var_off.value; |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3517 | err = check_stack_access(env, reg, off, size); |
| 3518 | if (err) |
| 3519 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 3520 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3521 | state = func(env, reg); |
| 3522 | err = update_stack_depth(env, state, off); |
| 3523 | if (err) |
| 3524 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 3525 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3526 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3527 | err = check_stack_write(env, state, off, size, |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 3528 | value_regno, insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3529 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3530 | err = check_stack_read(env, state, off, size, |
| 3531 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3532 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 3533 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3534 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3535 | return -EACCES; |
| 3536 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3537 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3538 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3539 | verbose(env, "R%d leaks addr into packet\n", |
| 3540 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3541 | return -EACCES; |
| 3542 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3543 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3544 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3545 | mark_reg_unknown(env, regs, value_regno); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 3546 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 3547 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3548 | is_pointer_value(env, value_regno)) { |
| 3549 | verbose(env, "R%d leaks addr into flow keys\n", |
| 3550 | value_regno); |
| 3551 | return -EACCES; |
| 3552 | } |
| 3553 | |
| 3554 | err = check_flow_keys_access(env, off, size); |
| 3555 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3556 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3557 | } else if (type_is_sk_pointer(reg->type)) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3558 | if (t == BPF_WRITE) { |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3559 | verbose(env, "R%d cannot write into %s\n", |
| 3560 | regno, reg_type_str[reg->type]); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3561 | return -EACCES; |
| 3562 | } |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 3563 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3564 | if (!err && value_regno >= 0) |
| 3565 | mark_reg_unknown(env, regs, value_regno); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3566 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
| 3567 | err = check_tp_buffer_access(env, reg, regno, off, size); |
| 3568 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3569 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3570 | } else if (reg->type == PTR_TO_BTF_ID) { |
| 3571 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
| 3572 | value_regno); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 3573 | } else if (reg->type == CONST_PTR_TO_MAP) { |
| 3574 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, |
| 3575 | value_regno); |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3576 | } else if (reg->type == PTR_TO_RDONLY_BUF) { |
| 3577 | if (t == BPF_WRITE) { |
| 3578 | verbose(env, "R%d cannot write into %s\n", |
| 3579 | regno, reg_type_str[reg->type]); |
| 3580 | return -EACCES; |
| 3581 | } |
Colin Ian King | f6dfbe31 | 2020-07-27 18:54:11 +0100 | [diff] [blame] | 3582 | err = check_buffer_access(env, reg, regno, off, size, false, |
| 3583 | "rdonly", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3584 | &env->prog->aux->max_rdonly_access); |
| 3585 | if (!err && value_regno >= 0) |
| 3586 | mark_reg_unknown(env, regs, value_regno); |
| 3587 | } else if (reg->type == PTR_TO_RDWR_BUF) { |
Colin Ian King | f6dfbe31 | 2020-07-27 18:54:11 +0100 | [diff] [blame] | 3588 | err = check_buffer_access(env, reg, regno, off, size, false, |
| 3589 | "rdwr", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3590 | &env->prog->aux->max_rdwr_access); |
| 3591 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3592 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3593 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3594 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 3595 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3596 | return -EACCES; |
| 3597 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3598 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3599 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3600 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3601 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3602 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3603 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3604 | return err; |
| 3605 | } |
| 3606 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3607 | static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3608 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3609 | int err; |
| 3610 | |
| 3611 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 3612 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3613 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3614 | return -EINVAL; |
| 3615 | } |
| 3616 | |
| 3617 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3618 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3619 | if (err) |
| 3620 | return err; |
| 3621 | |
| 3622 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3623 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3624 | if (err) |
| 3625 | return err; |
| 3626 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 3627 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3628 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 3629 | return -EACCES; |
| 3630 | } |
| 3631 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3632 | if (is_ctx_reg(env, insn->dst_reg) || |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 3633 | is_pkt_reg(env, insn->dst_reg) || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3634 | is_flow_key_reg(env, insn->dst_reg) || |
| 3635 | is_sk_reg(env, insn->dst_reg)) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3636 | verbose(env, "BPF_XADD stores into R%d %s is not allowed\n", |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 3637 | insn->dst_reg, |
| 3638 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 3639 | return -EACCES; |
| 3640 | } |
| 3641 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3642 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3643 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3644 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3645 | if (err) |
| 3646 | return err; |
| 3647 | |
| 3648 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3649 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3650 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3651 | } |
| 3652 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3653 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
| 3654 | int off, int access_size, |
| 3655 | bool zero_size_allowed) |
| 3656 | { |
| 3657 | struct bpf_reg_state *reg = reg_state(env, regno); |
| 3658 | |
| 3659 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 3660 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
| 3661 | if (tnum_is_const(reg->var_off)) { |
| 3662 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", |
| 3663 | regno, off, access_size); |
| 3664 | } else { |
| 3665 | char tn_buf[48]; |
| 3666 | |
| 3667 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3668 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", |
| 3669 | regno, tn_buf, access_size); |
| 3670 | } |
| 3671 | return -EACCES; |
| 3672 | } |
| 3673 | return 0; |
| 3674 | } |
| 3675 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3676 | /* when register 'regno' is passed into function that will read 'access_size' |
| 3677 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3678 | * and all elements of stack are initialized. |
| 3679 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 3680 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3681 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3682 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3683 | int access_size, bool zero_size_allowed, |
| 3684 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3685 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 3686 | struct bpf_reg_state *reg = reg_state(env, regno); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3687 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3688 | int err, min_off, max_off, i, j, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3689 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3690 | if (tnum_is_const(reg->var_off)) { |
| 3691 | min_off = max_off = reg->var_off.value + reg->off; |
| 3692 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 3693 | zero_size_allowed); |
| 3694 | if (err) |
| 3695 | return err; |
| 3696 | } else { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3697 | /* Variable offset is prohibited for unprivileged mode for |
| 3698 | * simplicity since it requires corresponding support in |
| 3699 | * Spectre masking for stack ALU. |
| 3700 | * See also retrieve_ptr_limit(). |
| 3701 | */ |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 3702 | if (!env->bypass_spec_v1) { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3703 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3704 | |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3705 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3706 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", |
| 3707 | regno, tn_buf); |
| 3708 | return -EACCES; |
| 3709 | } |
Andrey Ignatov | f2bcd05 | 2019-04-03 23:22:37 -0700 | [diff] [blame] | 3710 | /* Only initialized buffer on stack is allowed to be accessed |
| 3711 | * with variable offset. With uninitialized buffer it's hard to |
| 3712 | * guarantee that whole memory is marked as initialized on |
| 3713 | * helper return since specific bounds are unknown what may |
| 3714 | * cause uninitialized stack leaking. |
| 3715 | */ |
| 3716 | if (meta && meta->raw_mode) |
| 3717 | meta = NULL; |
| 3718 | |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3719 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
| 3720 | reg->smax_value <= -BPF_MAX_VAR_OFF) { |
| 3721 | verbose(env, "R%d unbounded indirect variable offset stack access\n", |
| 3722 | regno); |
| 3723 | return -EACCES; |
| 3724 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3725 | min_off = reg->smin_value + reg->off; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3726 | max_off = reg->smax_value + reg->off; |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3727 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 3728 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3729 | if (err) { |
| 3730 | verbose(env, "R%d min value is outside of stack bound\n", |
| 3731 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3732 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3733 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3734 | err = __check_stack_boundary(env, regno, max_off, access_size, |
| 3735 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3736 | if (err) { |
| 3737 | verbose(env, "R%d max value is outside of stack bound\n", |
| 3738 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3739 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3740 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3741 | } |
| 3742 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3743 | if (meta && meta->raw_mode) { |
| 3744 | meta->access_size = access_size; |
| 3745 | meta->regno = regno; |
| 3746 | return 0; |
| 3747 | } |
| 3748 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3749 | for (i = min_off; i < max_off + access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3750 | u8 *stype; |
| 3751 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3752 | slot = -i - 1; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3753 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3754 | if (state->allocated_stack <= slot) |
| 3755 | goto err; |
| 3756 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 3757 | if (*stype == STACK_MISC) |
| 3758 | goto mark; |
| 3759 | if (*stype == STACK_ZERO) { |
| 3760 | /* helper can write anything into the stack */ |
| 3761 | *stype = STACK_MISC; |
| 3762 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3763 | } |
Yonghong Song | 1d68f22 | 2020-05-09 10:59:15 -0700 | [diff] [blame] | 3764 | |
| 3765 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 3766 | state->stack[spi].spilled_ptr.type == PTR_TO_BTF_ID) |
| 3767 | goto mark; |
| 3768 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3769 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
Yonghong Song | cd17d38 | 2020-12-09 17:33:49 -0800 | [diff] [blame] | 3770 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
| 3771 | env->allow_ptr_leaks)) { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 3772 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3773 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 3774 | state->stack[spi].slot_type[j] = STACK_MISC; |
| 3775 | goto mark; |
| 3776 | } |
| 3777 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3778 | err: |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3779 | if (tnum_is_const(reg->var_off)) { |
| 3780 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 3781 | min_off, i - min_off, access_size); |
| 3782 | } else { |
| 3783 | char tn_buf[48]; |
| 3784 | |
| 3785 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3786 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", |
| 3787 | tn_buf, i - min_off, access_size); |
| 3788 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3789 | return -EACCES; |
| 3790 | mark: |
| 3791 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 3792 | * the whole slot to be marked as 'read' |
| 3793 | */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 3794 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 3795 | state->stack[spi].spilled_ptr.parent, |
| 3796 | REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3797 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3798 | return update_stack_depth(env, state, min_off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3799 | } |
| 3800 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3801 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 3802 | int access_size, bool zero_size_allowed, |
| 3803 | struct bpf_call_arg_meta *meta) |
| 3804 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3805 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3806 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3807 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3808 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3809 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3810 | return check_packet_access(env, regno, reg->off, access_size, |
| 3811 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3812 | case PTR_TO_MAP_VALUE: |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3813 | if (check_map_access_type(env, regno, reg->off, access_size, |
| 3814 | meta && meta->raw_mode ? BPF_WRITE : |
| 3815 | BPF_READ)) |
| 3816 | return -EACCES; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3817 | return check_map_access(env, regno, reg->off, access_size, |
| 3818 | zero_size_allowed); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3819 | case PTR_TO_MEM: |
| 3820 | return check_mem_region_access(env, regno, reg->off, |
| 3821 | access_size, reg->mem_size, |
| 3822 | zero_size_allowed); |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3823 | case PTR_TO_RDONLY_BUF: |
| 3824 | if (meta && meta->raw_mode) |
| 3825 | return -EACCES; |
| 3826 | return check_buffer_access(env, reg, regno, reg->off, |
| 3827 | access_size, zero_size_allowed, |
| 3828 | "rdonly", |
| 3829 | &env->prog->aux->max_rdonly_access); |
| 3830 | case PTR_TO_RDWR_BUF: |
| 3831 | return check_buffer_access(env, reg, regno, reg->off, |
| 3832 | access_size, zero_size_allowed, |
| 3833 | "rdwr", |
| 3834 | &env->prog->aux->max_rdwr_access); |
Lorenz Bauer | 0d004c02 | 2020-09-21 13:12:18 +0100 | [diff] [blame] | 3835 | case PTR_TO_STACK: |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3836 | return check_stack_boundary(env, regno, access_size, |
| 3837 | zero_size_allowed, meta); |
Lorenz Bauer | 0d004c02 | 2020-09-21 13:12:18 +0100 | [diff] [blame] | 3838 | default: /* scalar_value or invalid ptr */ |
| 3839 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
| 3840 | if (zero_size_allowed && access_size == 0 && |
| 3841 | register_is_null(reg)) |
| 3842 | return 0; |
| 3843 | |
| 3844 | verbose(env, "R%d type=%s expected=%s\n", regno, |
| 3845 | reg_type_str[reg->type], |
| 3846 | reg_type_str[PTR_TO_STACK]); |
| 3847 | return -EACCES; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3848 | } |
| 3849 | } |
| 3850 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3851 | /* Implementation details: |
| 3852 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL |
| 3853 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
| 3854 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after |
| 3855 | * value_or_null->value transition, since the verifier only cares about |
| 3856 | * the range of access to valid map value pointer and doesn't care about actual |
| 3857 | * address of the map element. |
| 3858 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
| 3859 | * reg->id > 0 after value_or_null->value transition. By doing so |
| 3860 | * two bpf_map_lookups will be considered two different pointers that |
| 3861 | * point to different bpf_spin_locks. |
| 3862 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
| 3863 | * dead-locks. |
| 3864 | * Since only one bpf_spin_lock is allowed the checks are simpler than |
| 3865 | * reg_is_refcounted() logic. The verifier needs to remember only |
| 3866 | * one spin_lock instead of array of acquired_refs. |
| 3867 | * cur_state->active_spin_lock remembers which map value element got locked |
| 3868 | * and clears it after bpf_spin_unlock. |
| 3869 | */ |
| 3870 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, |
| 3871 | bool is_lock) |
| 3872 | { |
| 3873 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 3874 | struct bpf_verifier_state *cur = env->cur_state; |
| 3875 | bool is_const = tnum_is_const(reg->var_off); |
| 3876 | struct bpf_map *map = reg->map_ptr; |
| 3877 | u64 val = reg->var_off.value; |
| 3878 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3879 | if (!is_const) { |
| 3880 | verbose(env, |
| 3881 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", |
| 3882 | regno); |
| 3883 | return -EINVAL; |
| 3884 | } |
| 3885 | if (!map->btf) { |
| 3886 | verbose(env, |
| 3887 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", |
| 3888 | map->name); |
| 3889 | return -EINVAL; |
| 3890 | } |
| 3891 | if (!map_value_has_spin_lock(map)) { |
| 3892 | if (map->spin_lock_off == -E2BIG) |
| 3893 | verbose(env, |
| 3894 | "map '%s' has more than one 'struct bpf_spin_lock'\n", |
| 3895 | map->name); |
| 3896 | else if (map->spin_lock_off == -ENOENT) |
| 3897 | verbose(env, |
| 3898 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", |
| 3899 | map->name); |
| 3900 | else |
| 3901 | verbose(env, |
| 3902 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", |
| 3903 | map->name); |
| 3904 | return -EINVAL; |
| 3905 | } |
| 3906 | if (map->spin_lock_off != val + reg->off) { |
| 3907 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", |
| 3908 | val + reg->off); |
| 3909 | return -EINVAL; |
| 3910 | } |
| 3911 | if (is_lock) { |
| 3912 | if (cur->active_spin_lock) { |
| 3913 | verbose(env, |
| 3914 | "Locking two bpf_spin_locks are not allowed\n"); |
| 3915 | return -EINVAL; |
| 3916 | } |
| 3917 | cur->active_spin_lock = reg->id; |
| 3918 | } else { |
| 3919 | if (!cur->active_spin_lock) { |
| 3920 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
| 3921 | return -EINVAL; |
| 3922 | } |
| 3923 | if (cur->active_spin_lock != reg->id) { |
| 3924 | verbose(env, "bpf_spin_unlock of different lock\n"); |
| 3925 | return -EINVAL; |
| 3926 | } |
| 3927 | cur->active_spin_lock = 0; |
| 3928 | } |
| 3929 | return 0; |
| 3930 | } |
| 3931 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 3932 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 3933 | { |
| 3934 | return type == ARG_PTR_TO_MEM || |
| 3935 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 3936 | type == ARG_PTR_TO_UNINIT_MEM; |
| 3937 | } |
| 3938 | |
| 3939 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 3940 | { |
| 3941 | return type == ARG_CONST_SIZE || |
| 3942 | type == ARG_CONST_SIZE_OR_ZERO; |
| 3943 | } |
| 3944 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3945 | static bool arg_type_is_alloc_size(enum bpf_arg_type type) |
| 3946 | { |
| 3947 | return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; |
| 3948 | } |
| 3949 | |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 3950 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
| 3951 | { |
| 3952 | return type == ARG_PTR_TO_INT || |
| 3953 | type == ARG_PTR_TO_LONG; |
| 3954 | } |
| 3955 | |
| 3956 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
| 3957 | { |
| 3958 | if (type == ARG_PTR_TO_INT) |
| 3959 | return sizeof(u32); |
| 3960 | else if (type == ARG_PTR_TO_LONG) |
| 3961 | return sizeof(u64); |
| 3962 | |
| 3963 | return -EINVAL; |
| 3964 | } |
| 3965 | |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 3966 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
| 3967 | const struct bpf_call_arg_meta *meta, |
| 3968 | enum bpf_arg_type *arg_type) |
| 3969 | { |
| 3970 | if (!meta->map_ptr) { |
| 3971 | /* kernel subsystem misconfigured verifier */ |
| 3972 | verbose(env, "invalid map_ptr to access map->type\n"); |
| 3973 | return -EACCES; |
| 3974 | } |
| 3975 | |
| 3976 | switch (meta->map_ptr->map_type) { |
| 3977 | case BPF_MAP_TYPE_SOCKMAP: |
| 3978 | case BPF_MAP_TYPE_SOCKHASH: |
| 3979 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { |
Lorenz Bauer | 6550f2d | 2020-09-28 10:08:02 +0100 | [diff] [blame] | 3980 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 3981 | } else { |
| 3982 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); |
| 3983 | return -EINVAL; |
| 3984 | } |
| 3985 | break; |
| 3986 | |
| 3987 | default: |
| 3988 | break; |
| 3989 | } |
| 3990 | return 0; |
| 3991 | } |
| 3992 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 3993 | struct bpf_reg_types { |
| 3994 | const enum bpf_reg_type types[10]; |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 3995 | u32 *btf_id; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 3996 | }; |
| 3997 | |
| 3998 | static const struct bpf_reg_types map_key_value_types = { |
| 3999 | .types = { |
| 4000 | PTR_TO_STACK, |
| 4001 | PTR_TO_PACKET, |
| 4002 | PTR_TO_PACKET_META, |
| 4003 | PTR_TO_MAP_VALUE, |
| 4004 | }, |
| 4005 | }; |
| 4006 | |
| 4007 | static const struct bpf_reg_types sock_types = { |
| 4008 | .types = { |
| 4009 | PTR_TO_SOCK_COMMON, |
| 4010 | PTR_TO_SOCKET, |
| 4011 | PTR_TO_TCP_SOCK, |
| 4012 | PTR_TO_XDP_SOCK, |
| 4013 | }, |
| 4014 | }; |
| 4015 | |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 4016 | #ifdef CONFIG_NET |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4017 | static const struct bpf_reg_types btf_id_sock_common_types = { |
| 4018 | .types = { |
| 4019 | PTR_TO_SOCK_COMMON, |
| 4020 | PTR_TO_SOCKET, |
| 4021 | PTR_TO_TCP_SOCK, |
| 4022 | PTR_TO_XDP_SOCK, |
| 4023 | PTR_TO_BTF_ID, |
| 4024 | }, |
| 4025 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], |
| 4026 | }; |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 4027 | #endif |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4028 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4029 | static const struct bpf_reg_types mem_types = { |
| 4030 | .types = { |
| 4031 | PTR_TO_STACK, |
| 4032 | PTR_TO_PACKET, |
| 4033 | PTR_TO_PACKET_META, |
| 4034 | PTR_TO_MAP_VALUE, |
| 4035 | PTR_TO_MEM, |
| 4036 | PTR_TO_RDONLY_BUF, |
| 4037 | PTR_TO_RDWR_BUF, |
| 4038 | }, |
| 4039 | }; |
| 4040 | |
| 4041 | static const struct bpf_reg_types int_ptr_types = { |
| 4042 | .types = { |
| 4043 | PTR_TO_STACK, |
| 4044 | PTR_TO_PACKET, |
| 4045 | PTR_TO_PACKET_META, |
| 4046 | PTR_TO_MAP_VALUE, |
| 4047 | }, |
| 4048 | }; |
| 4049 | |
| 4050 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; |
| 4051 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; |
| 4052 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; |
| 4053 | static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } }; |
| 4054 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; |
| 4055 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; |
| 4056 | static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 4057 | static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_PERCPU_BTF_ID } }; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4058 | |
Lorenz Bauer | 0789e13b | 2020-09-23 17:01:55 +0100 | [diff] [blame] | 4059 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4060 | [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, |
| 4061 | [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, |
| 4062 | [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, |
| 4063 | [ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types, |
| 4064 | [ARG_CONST_SIZE] = &scalar_types, |
| 4065 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, |
| 4066 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, |
| 4067 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, |
| 4068 | [ARG_PTR_TO_CTX] = &context_types, |
| 4069 | [ARG_PTR_TO_CTX_OR_NULL] = &context_types, |
| 4070 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 4071 | #ifdef CONFIG_NET |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4072 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 4073 | #endif |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4074 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
| 4075 | [ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types, |
| 4076 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
| 4077 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, |
| 4078 | [ARG_PTR_TO_MEM] = &mem_types, |
| 4079 | [ARG_PTR_TO_MEM_OR_NULL] = &mem_types, |
| 4080 | [ARG_PTR_TO_UNINIT_MEM] = &mem_types, |
| 4081 | [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, |
| 4082 | [ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types, |
| 4083 | [ARG_PTR_TO_INT] = &int_ptr_types, |
| 4084 | [ARG_PTR_TO_LONG] = &int_ptr_types, |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 4085 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4086 | }; |
| 4087 | |
| 4088 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4089 | enum bpf_arg_type arg_type, |
| 4090 | const u32 *arg_btf_id) |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4091 | { |
| 4092 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 4093 | enum bpf_reg_type expected, type = reg->type; |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4094 | const struct bpf_reg_types *compatible; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4095 | int i, j; |
| 4096 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4097 | compatible = compatible_reg_types[arg_type]; |
| 4098 | if (!compatible) { |
| 4099 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); |
| 4100 | return -EFAULT; |
| 4101 | } |
| 4102 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4103 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
| 4104 | expected = compatible->types[i]; |
| 4105 | if (expected == NOT_INIT) |
| 4106 | break; |
| 4107 | |
| 4108 | if (type == expected) |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4109 | goto found; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | verbose(env, "R%d type=%s expected=", regno, reg_type_str[type]); |
| 4113 | for (j = 0; j + 1 < i; j++) |
| 4114 | verbose(env, "%s, ", reg_type_str[compatible->types[j]]); |
| 4115 | verbose(env, "%s\n", reg_type_str[compatible->types[j]]); |
| 4116 | return -EACCES; |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4117 | |
| 4118 | found: |
| 4119 | if (type == PTR_TO_BTF_ID) { |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4120 | if (!arg_btf_id) { |
| 4121 | if (!compatible->btf_id) { |
| 4122 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); |
| 4123 | return -EFAULT; |
| 4124 | } |
| 4125 | arg_btf_id = compatible->btf_id; |
| 4126 | } |
| 4127 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4128 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, |
| 4129 | btf_vmlinux, *arg_btf_id)) { |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4130 | verbose(env, "R%d is of type %s but %s is expected\n", |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4131 | regno, kernel_type_name(reg->btf, reg->btf_id), |
| 4132 | kernel_type_name(btf_vmlinux, *arg_btf_id)); |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4133 | return -EACCES; |
| 4134 | } |
| 4135 | |
| 4136 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 4137 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", |
| 4138 | regno); |
| 4139 | return -EACCES; |
| 4140 | } |
| 4141 | } |
| 4142 | |
| 4143 | return 0; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4144 | } |
| 4145 | |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 4146 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
| 4147 | struct bpf_call_arg_meta *meta, |
| 4148 | const struct bpf_func_proto *fn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4149 | { |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 4150 | u32 regno = BPF_REG_1 + arg; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4151 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 4152 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4153 | enum bpf_reg_type type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4154 | int err = 0; |
| 4155 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 4156 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4157 | return 0; |
| 4158 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4159 | err = check_reg_arg(env, regno, SRC_OP); |
| 4160 | if (err) |
| 4161 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4162 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4163 | if (arg_type == ARG_ANYTHING) { |
| 4164 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4165 | verbose(env, "R%d leaks addr into helper function\n", |
| 4166 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4167 | return -EACCES; |
| 4168 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 4169 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4170 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 4171 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4172 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 4173 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4174 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 4175 | return -EACCES; |
| 4176 | } |
| 4177 | |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 4178 | if (arg_type == ARG_PTR_TO_MAP_VALUE || |
| 4179 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
| 4180 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { |
| 4181 | err = resolve_map_arg_type(env, meta, &arg_type); |
| 4182 | if (err) |
| 4183 | return err; |
| 4184 | } |
| 4185 | |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 4186 | if (register_is_null(reg) && arg_type_may_be_null(arg_type)) |
| 4187 | /* A NULL register has a SCALAR_VALUE type, so skip |
| 4188 | * type checking. |
| 4189 | */ |
| 4190 | goto skip_type_check; |
Jiri Olsa | faaf4a7 | 2020-08-25 21:21:18 +0200 | [diff] [blame] | 4191 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4192 | err = check_reg_type(env, regno, arg_type, fn->arg_btf_id[arg]); |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4193 | if (err) |
| 4194 | return err; |
| 4195 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4196 | if (type == PTR_TO_CTX) { |
Lorenz Bauer | feec704 | 2020-09-21 13:12:23 +0100 | [diff] [blame] | 4197 | err = check_ctx_reg(env, reg, regno); |
| 4198 | if (err < 0) |
| 4199 | return err; |
Lorenz Bauer | d7b9454 | 2020-09-21 13:12:21 +0100 | [diff] [blame] | 4200 | } |
| 4201 | |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 4202 | skip_type_check: |
Lorenz Bauer | 02f7c95 | 2020-09-21 13:12:22 +0100 | [diff] [blame] | 4203 | if (reg->ref_obj_id) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4204 | if (meta->ref_obj_id) { |
| 4205 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", |
| 4206 | regno, reg->ref_obj_id, |
| 4207 | meta->ref_obj_id); |
| 4208 | return -EFAULT; |
| 4209 | } |
| 4210 | meta->ref_obj_id = reg->ref_obj_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4211 | } |
| 4212 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4213 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 4214 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4215 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4216 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 4217 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 4218 | * check that [key, key + map->key_size) are within |
| 4219 | * stack limits and initialized |
| 4220 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4221 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4222 | /* in function declaration map_ptr must come before |
| 4223 | * map_key, so that it's verified and known before |
| 4224 | * we have to check map_key here. Otherwise it means |
| 4225 | * that kernel subsystem misconfigured verifier |
| 4226 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4227 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4228 | return -EACCES; |
| 4229 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 4230 | err = check_helper_mem_access(env, regno, |
| 4231 | meta->map_ptr->key_size, false, |
| 4232 | NULL); |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 4233 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 4234 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
| 4235 | !register_is_null(reg)) || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 4236 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4237 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 4238 | * check [value, value + map->value_size) validity |
| 4239 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4240 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4241 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4242 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4243 | return -EACCES; |
| 4244 | } |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 4245 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 4246 | err = check_helper_mem_access(env, regno, |
| 4247 | meta->map_ptr->value_size, false, |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 4248 | meta); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 4249 | } else if (arg_type == ARG_PTR_TO_PERCPU_BTF_ID) { |
| 4250 | if (!reg->btf_id) { |
| 4251 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); |
| 4252 | return -EACCES; |
| 4253 | } |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4254 | meta->ret_btf = reg->btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 4255 | meta->ret_btf_id = reg->btf_id; |
Lorenz Bauer | c18f0b6 | 2020-09-21 13:12:25 +0100 | [diff] [blame] | 4256 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
| 4257 | if (meta->func_id == BPF_FUNC_spin_lock) { |
| 4258 | if (process_spin_lock(env, regno, true)) |
| 4259 | return -EACCES; |
| 4260 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
| 4261 | if (process_spin_lock(env, regno, false)) |
| 4262 | return -EACCES; |
| 4263 | } else { |
| 4264 | verbose(env, "verifier internal error\n"); |
| 4265 | return -EFAULT; |
| 4266 | } |
Lorenz Bauer | a2bbe7c | 2020-09-21 13:12:24 +0100 | [diff] [blame] | 4267 | } else if (arg_type_is_mem_ptr(arg_type)) { |
| 4268 | /* The access to this pointer is only checked when we hit the |
| 4269 | * next is_mem_size argument below. |
| 4270 | */ |
| 4271 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 4272 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4273 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4274 | |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 4275 | /* This is used to refine r0 return value bounds for helpers |
| 4276 | * that enforce this value as an upper bound on return values. |
| 4277 | * See do_refine_retval_range() for helpers that can refine |
| 4278 | * the return value. C type of helper is u32 so we pull register |
| 4279 | * bound from umax_value however, if negative verifier errors |
| 4280 | * out. Only upper bounds can be learned because retval is an |
| 4281 | * int type and negative retvals are allowed. |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4282 | */ |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 4283 | meta->msize_max_value = reg->umax_value; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4284 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4285 | /* The register is SCALAR_VALUE; the access check |
| 4286 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4287 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4288 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4289 | /* For unprivileged variable accesses, disable raw |
| 4290 | * mode so that the program is required to |
| 4291 | * initialize all the memory that the helper could |
| 4292 | * just partially fill up. |
| 4293 | */ |
| 4294 | meta = NULL; |
| 4295 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4296 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4297 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4298 | regno); |
| 4299 | return -EACCES; |
| 4300 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4301 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4302 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4303 | err = check_helper_mem_access(env, regno - 1, 0, |
| 4304 | zero_size_allowed, |
| 4305 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4306 | if (err) |
| 4307 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4308 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4309 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4310 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4311 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4312 | regno); |
| 4313 | return -EACCES; |
| 4314 | } |
| 4315 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4316 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4317 | zero_size_allowed, meta); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 4318 | if (!err) |
| 4319 | err = mark_chain_precision(env, regno); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4320 | } else if (arg_type_is_alloc_size(arg_type)) { |
| 4321 | if (!tnum_is_const(reg->var_off)) { |
Brendan Jackman | 28a8add | 2021-01-12 12:39:13 +0000 | [diff] [blame] | 4322 | verbose(env, "R%d is not a known constant'\n", |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4323 | regno); |
| 4324 | return -EACCES; |
| 4325 | } |
| 4326 | meta->mem_size = reg->var_off.value; |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 4327 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 4328 | int size = int_ptr_type_to_size(arg_type); |
| 4329 | |
| 4330 | err = check_helper_mem_access(env, regno, size, false, meta); |
| 4331 | if (err) |
| 4332 | return err; |
| 4333 | err = check_ptr_alignment(env, reg, 0, size, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | return err; |
| 4337 | } |
| 4338 | |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 4339 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
| 4340 | { |
| 4341 | enum bpf_attach_type eatype = env->prog->expected_attach_type; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 4342 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 4343 | |
| 4344 | if (func_id != BPF_FUNC_map_update_elem) |
| 4345 | return false; |
| 4346 | |
| 4347 | /* It's not possible to get access to a locked struct sock in these |
| 4348 | * contexts, so updating is safe. |
| 4349 | */ |
| 4350 | switch (type) { |
| 4351 | case BPF_PROG_TYPE_TRACING: |
| 4352 | if (eatype == BPF_TRACE_ITER) |
| 4353 | return true; |
| 4354 | break; |
| 4355 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 4356 | case BPF_PROG_TYPE_SCHED_CLS: |
| 4357 | case BPF_PROG_TYPE_SCHED_ACT: |
| 4358 | case BPF_PROG_TYPE_XDP: |
| 4359 | case BPF_PROG_TYPE_SK_REUSEPORT: |
| 4360 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
| 4361 | case BPF_PROG_TYPE_SK_LOOKUP: |
| 4362 | return true; |
| 4363 | default: |
| 4364 | break; |
| 4365 | } |
| 4366 | |
| 4367 | verbose(env, "cannot update sockmap in this context\n"); |
| 4368 | return false; |
| 4369 | } |
| 4370 | |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 4371 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
| 4372 | { |
| 4373 | return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); |
| 4374 | } |
| 4375 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4376 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 4377 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 4378 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 4379 | if (!map) |
| 4380 | return 0; |
| 4381 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4382 | /* We need a two way check, first is from map perspective ... */ |
| 4383 | switch (map->map_type) { |
| 4384 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 4385 | if (func_id != BPF_FUNC_tail_call) |
| 4386 | goto error; |
| 4387 | break; |
| 4388 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 4389 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 4390 | func_id != BPF_FUNC_perf_event_output && |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 4391 | func_id != BPF_FUNC_skb_output && |
Eelco Chaudron | d831ee8 | 2020-03-06 08:59:23 +0000 | [diff] [blame] | 4392 | func_id != BPF_FUNC_perf_event_read_value && |
| 4393 | func_id != BPF_FUNC_xdp_output) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4394 | goto error; |
| 4395 | break; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4396 | case BPF_MAP_TYPE_RINGBUF: |
| 4397 | if (func_id != BPF_FUNC_ringbuf_output && |
| 4398 | func_id != BPF_FUNC_ringbuf_reserve && |
| 4399 | func_id != BPF_FUNC_ringbuf_submit && |
| 4400 | func_id != BPF_FUNC_ringbuf_discard && |
| 4401 | func_id != BPF_FUNC_ringbuf_query) |
| 4402 | goto error; |
| 4403 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4404 | case BPF_MAP_TYPE_STACK_TRACE: |
| 4405 | if (func_id != BPF_FUNC_get_stackid) |
| 4406 | goto error; |
| 4407 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 4408 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 4409 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 4410 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 4411 | goto error; |
| 4412 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 4413 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 4414 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 4415 | if (func_id != BPF_FUNC_get_local_storage) |
| 4416 | goto error; |
| 4417 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 4418 | case BPF_MAP_TYPE_DEVMAP: |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 4419 | case BPF_MAP_TYPE_DEVMAP_HASH: |
Toke Høiland-Jørgensen | 0cdbb4b | 2019-06-28 11:12:35 +0200 | [diff] [blame] | 4420 | if (func_id != BPF_FUNC_redirect_map && |
| 4421 | func_id != BPF_FUNC_map_lookup_elem) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 4422 | goto error; |
| 4423 | break; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 4424 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 4425 | * appear. |
| 4426 | */ |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 4427 | case BPF_MAP_TYPE_CPUMAP: |
| 4428 | if (func_id != BPF_FUNC_redirect_map) |
| 4429 | goto error; |
| 4430 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 4431 | case BPF_MAP_TYPE_XSKMAP: |
| 4432 | if (func_id != BPF_FUNC_redirect_map && |
| 4433 | func_id != BPF_FUNC_map_lookup_elem) |
| 4434 | goto error; |
| 4435 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4436 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 4437 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4438 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 4439 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 4440 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4441 | case BPF_MAP_TYPE_SOCKMAP: |
| 4442 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 4443 | func_id != BPF_FUNC_sock_map_update && |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 4444 | func_id != BPF_FUNC_map_delete_elem && |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 4445 | func_id != BPF_FUNC_msg_redirect_map && |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 4446 | func_id != BPF_FUNC_sk_select_reuseport && |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 4447 | func_id != BPF_FUNC_map_lookup_elem && |
| 4448 | !may_update_sockmap(env, func_id)) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4449 | goto error; |
| 4450 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 4451 | case BPF_MAP_TYPE_SOCKHASH: |
| 4452 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 4453 | func_id != BPF_FUNC_sock_hash_update && |
| 4454 | func_id != BPF_FUNC_map_delete_elem && |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 4455 | func_id != BPF_FUNC_msg_redirect_hash && |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 4456 | func_id != BPF_FUNC_sk_select_reuseport && |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 4457 | func_id != BPF_FUNC_map_lookup_elem && |
| 4458 | !may_update_sockmap(env, func_id)) |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 4459 | goto error; |
| 4460 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 4461 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 4462 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 4463 | goto error; |
| 4464 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 4465 | case BPF_MAP_TYPE_QUEUE: |
| 4466 | case BPF_MAP_TYPE_STACK: |
| 4467 | if (func_id != BPF_FUNC_map_peek_elem && |
| 4468 | func_id != BPF_FUNC_map_pop_elem && |
| 4469 | func_id != BPF_FUNC_map_push_elem) |
| 4470 | goto error; |
| 4471 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 4472 | case BPF_MAP_TYPE_SK_STORAGE: |
| 4473 | if (func_id != BPF_FUNC_sk_storage_get && |
| 4474 | func_id != BPF_FUNC_sk_storage_delete) |
| 4475 | goto error; |
| 4476 | break; |
KP Singh | 8ea6368 | 2020-08-25 20:29:17 +0200 | [diff] [blame] | 4477 | case BPF_MAP_TYPE_INODE_STORAGE: |
| 4478 | if (func_id != BPF_FUNC_inode_storage_get && |
| 4479 | func_id != BPF_FUNC_inode_storage_delete) |
| 4480 | goto error; |
| 4481 | break; |
KP Singh | 4cf1bc1 | 2020-11-06 10:37:40 +0000 | [diff] [blame] | 4482 | case BPF_MAP_TYPE_TASK_STORAGE: |
| 4483 | if (func_id != BPF_FUNC_task_storage_get && |
| 4484 | func_id != BPF_FUNC_task_storage_delete) |
| 4485 | goto error; |
| 4486 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4487 | default: |
| 4488 | break; |
| 4489 | } |
| 4490 | |
| 4491 | /* ... and second from the function itself. */ |
| 4492 | switch (func_id) { |
| 4493 | case BPF_FUNC_tail_call: |
| 4494 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 4495 | goto error; |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 4496 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
| 4497 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4498 | return -EINVAL; |
| 4499 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4500 | break; |
| 4501 | case BPF_FUNC_perf_event_read: |
| 4502 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 4503 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 4504 | case BPF_FUNC_skb_output: |
Eelco Chaudron | d831ee8 | 2020-03-06 08:59:23 +0000 | [diff] [blame] | 4505 | case BPF_FUNC_xdp_output: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4506 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 4507 | goto error; |
| 4508 | break; |
| 4509 | case BPF_FUNC_get_stackid: |
| 4510 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 4511 | goto error; |
| 4512 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 4513 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 4514 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 4515 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 4516 | goto error; |
| 4517 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 4518 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 4519 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 4520 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 4521 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 4522 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 4523 | goto error; |
| 4524 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4525 | case BPF_FUNC_sk_redirect_map: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 4526 | case BPF_FUNC_msg_redirect_map: |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 4527 | case BPF_FUNC_sock_map_update: |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4528 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 4529 | goto error; |
| 4530 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 4531 | case BPF_FUNC_sk_redirect_hash: |
| 4532 | case BPF_FUNC_msg_redirect_hash: |
| 4533 | case BPF_FUNC_sock_hash_update: |
| 4534 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4535 | goto error; |
| 4536 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 4537 | case BPF_FUNC_get_local_storage: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 4538 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 4539 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 4540 | goto error; |
| 4541 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 4542 | case BPF_FUNC_sk_select_reuseport: |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 4543 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
| 4544 | map->map_type != BPF_MAP_TYPE_SOCKMAP && |
| 4545 | map->map_type != BPF_MAP_TYPE_SOCKHASH) |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 4546 | goto error; |
| 4547 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 4548 | case BPF_FUNC_map_peek_elem: |
| 4549 | case BPF_FUNC_map_pop_elem: |
| 4550 | case BPF_FUNC_map_push_elem: |
| 4551 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 4552 | map->map_type != BPF_MAP_TYPE_STACK) |
| 4553 | goto error; |
| 4554 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 4555 | case BPF_FUNC_sk_storage_get: |
| 4556 | case BPF_FUNC_sk_storage_delete: |
| 4557 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
| 4558 | goto error; |
| 4559 | break; |
KP Singh | 8ea6368 | 2020-08-25 20:29:17 +0200 | [diff] [blame] | 4560 | case BPF_FUNC_inode_storage_get: |
| 4561 | case BPF_FUNC_inode_storage_delete: |
| 4562 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) |
| 4563 | goto error; |
| 4564 | break; |
KP Singh | 4cf1bc1 | 2020-11-06 10:37:40 +0000 | [diff] [blame] | 4565 | case BPF_FUNC_task_storage_get: |
| 4566 | case BPF_FUNC_task_storage_delete: |
| 4567 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) |
| 4568 | goto error; |
| 4569 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4570 | default: |
| 4571 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4575 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4576 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 4577 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4578 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 4579 | } |
| 4580 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 4581 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4582 | { |
| 4583 | int count = 0; |
| 4584 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4585 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4586 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4587 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4588 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4589 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4590 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4591 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4592 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4593 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4594 | count++; |
| 4595 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 4596 | /* We only support one arg being in raw mode at the moment, |
| 4597 | * which is sufficient for the helper functions we have |
| 4598 | * right now. |
| 4599 | */ |
| 4600 | return count <= 1; |
| 4601 | } |
| 4602 | |
| 4603 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 4604 | enum bpf_arg_type arg_next) |
| 4605 | { |
| 4606 | return (arg_type_is_mem_ptr(arg_curr) && |
| 4607 | !arg_type_is_mem_size(arg_next)) || |
| 4608 | (!arg_type_is_mem_ptr(arg_curr) && |
| 4609 | arg_type_is_mem_size(arg_next)); |
| 4610 | } |
| 4611 | |
| 4612 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 4613 | { |
| 4614 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 4615 | * bytes from memory 'buf'. Both arg types need |
| 4616 | * to be paired, so make sure there's no buggy |
| 4617 | * helper function specification. |
| 4618 | */ |
| 4619 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 4620 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 4621 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 4622 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 4623 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 4624 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 4625 | return false; |
| 4626 | |
| 4627 | return true; |
| 4628 | } |
| 4629 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4630 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4631 | { |
| 4632 | int count = 0; |
| 4633 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4634 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4635 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4636 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4637 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4638 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4639 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4640 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4641 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4642 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4643 | count++; |
| 4644 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4645 | /* A reference acquiring function cannot acquire |
| 4646 | * another refcounted ptr. |
| 4647 | */ |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 4648 | if (may_be_acquire_function(func_id) && count) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4649 | return false; |
| 4650 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4651 | /* We only support one arg being unreferenced at the moment, |
| 4652 | * which is sufficient for the helper functions we have right now. |
| 4653 | */ |
| 4654 | return count <= 1; |
| 4655 | } |
| 4656 | |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 4657 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
| 4658 | { |
| 4659 | int i; |
| 4660 | |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4661 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 4662 | if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) |
| 4663 | return false; |
| 4664 | |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4665 | if (fn->arg_type[i] != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i]) |
| 4666 | return false; |
| 4667 | } |
| 4668 | |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 4669 | return true; |
| 4670 | } |
| 4671 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4672 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 4673 | { |
| 4674 | return check_raw_mode_ok(fn) && |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4675 | check_arg_pair_ok(fn) && |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 4676 | check_btf_id_ok(fn) && |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4677 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4678 | } |
| 4679 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4680 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 4681 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4682 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4683 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 4684 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4685 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4686 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4687 | int i; |
| 4688 | |
| 4689 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4690 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4691 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4692 | |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 4693 | bpf_for_each_spilled_reg(i, state, reg) { |
| 4694 | if (!reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4695 | continue; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4696 | if (reg_is_pkt_pointer_any(reg)) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 4697 | __mark_reg_unknown(env, reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4698 | } |
| 4699 | } |
| 4700 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4701 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 4702 | { |
| 4703 | struct bpf_verifier_state *vstate = env->cur_state; |
| 4704 | int i; |
| 4705 | |
| 4706 | for (i = 0; i <= vstate->curframe; i++) |
| 4707 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 4708 | } |
| 4709 | |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 4710 | enum { |
| 4711 | AT_PKT_END = -1, |
| 4712 | BEYOND_PKT_END = -2, |
| 4713 | }; |
| 4714 | |
| 4715 | static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range_open) |
| 4716 | { |
| 4717 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 4718 | struct bpf_reg_state *reg = &state->regs[regn]; |
| 4719 | |
| 4720 | if (reg->type != PTR_TO_PACKET) |
| 4721 | /* PTR_TO_PACKET_META is not supported yet */ |
| 4722 | return; |
| 4723 | |
| 4724 | /* The 'reg' is pkt > pkt_end or pkt >= pkt_end. |
| 4725 | * How far beyond pkt_end it goes is unknown. |
| 4726 | * if (!range_open) it's the case of pkt >= pkt_end |
| 4727 | * if (range_open) it's the case of pkt > pkt_end |
| 4728 | * hence this pointer is at least 1 byte bigger than pkt_end |
| 4729 | */ |
| 4730 | if (range_open) |
| 4731 | reg->range = BEYOND_PKT_END; |
| 4732 | else |
| 4733 | reg->range = AT_PKT_END; |
| 4734 | } |
| 4735 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4736 | static void release_reg_references(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4737 | struct bpf_func_state *state, |
| 4738 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4739 | { |
| 4740 | struct bpf_reg_state *regs = state->regs, *reg; |
| 4741 | int i; |
| 4742 | |
| 4743 | for (i = 0; i < MAX_BPF_REG; i++) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4744 | if (regs[i].ref_obj_id == ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4745 | mark_reg_unknown(env, regs, i); |
| 4746 | |
| 4747 | bpf_for_each_spilled_reg(i, state, reg) { |
| 4748 | if (!reg) |
| 4749 | continue; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4750 | if (reg->ref_obj_id == ref_obj_id) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 4751 | __mark_reg_unknown(env, reg); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4752 | } |
| 4753 | } |
| 4754 | |
| 4755 | /* The pointer with the specified id has released its reference to kernel |
| 4756 | * resources. Identify all copies of the same pointer and clear the reference. |
| 4757 | */ |
| 4758 | static int release_reference(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4759 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4760 | { |
| 4761 | struct bpf_verifier_state *vstate = env->cur_state; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4762 | int err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4763 | int i; |
| 4764 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4765 | err = release_reference_state(cur_func(env), ref_obj_id); |
| 4766 | if (err) |
| 4767 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4768 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4769 | for (i = 0; i <= vstate->curframe; i++) |
| 4770 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
| 4771 | |
| 4772 | return 0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4773 | } |
| 4774 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4775 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
| 4776 | struct bpf_reg_state *regs) |
| 4777 | { |
| 4778 | int i; |
| 4779 | |
| 4780 | /* after the call registers r0 - r5 were scratched */ |
| 4781 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 4782 | mark_reg_not_init(env, regs, caller_saved[i]); |
| 4783 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 4784 | } |
| 4785 | } |
| 4786 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4787 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 4788 | int *insn_idx) |
| 4789 | { |
| 4790 | struct bpf_verifier_state *state = env->cur_state; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4791 | struct bpf_func_info_aux *func_info_aux; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4792 | struct bpf_func_state *caller, *callee; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4793 | int i, err, subprog, target_insn; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4794 | bool is_global = false; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4795 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 4796 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4797 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 4798 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4799 | return -E2BIG; |
| 4800 | } |
| 4801 | |
| 4802 | target_insn = *insn_idx + insn->imm; |
| 4803 | subprog = find_subprog(env, target_insn + 1); |
| 4804 | if (subprog < 0) { |
| 4805 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 4806 | target_insn + 1); |
| 4807 | return -EFAULT; |
| 4808 | } |
| 4809 | |
| 4810 | caller = state->frame[state->curframe]; |
| 4811 | if (state->frame[state->curframe + 1]) { |
| 4812 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 4813 | state->curframe + 1); |
| 4814 | return -EFAULT; |
| 4815 | } |
| 4816 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4817 | func_info_aux = env->prog->aux->func_info_aux; |
| 4818 | if (func_info_aux) |
| 4819 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; |
| 4820 | err = btf_check_func_arg_match(env, subprog, caller->regs); |
| 4821 | if (err == -EFAULT) |
| 4822 | return err; |
| 4823 | if (is_global) { |
| 4824 | if (err) { |
| 4825 | verbose(env, "Caller passes invalid args into func#%d\n", |
| 4826 | subprog); |
| 4827 | return err; |
| 4828 | } else { |
| 4829 | if (env->log.level & BPF_LOG_LEVEL) |
| 4830 | verbose(env, |
| 4831 | "Func#%d is global and valid. Skipping.\n", |
| 4832 | subprog); |
| 4833 | clear_caller_saved_regs(env, caller->regs); |
| 4834 | |
| 4835 | /* All global functions return SCALAR_VALUE */ |
| 4836 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
| 4837 | |
| 4838 | /* continue with next insn after call */ |
| 4839 | return 0; |
| 4840 | } |
| 4841 | } |
| 4842 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4843 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 4844 | if (!callee) |
| 4845 | return -ENOMEM; |
| 4846 | state->frame[state->curframe + 1] = callee; |
| 4847 | |
| 4848 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 4849 | * into its own stack before reading from it. |
| 4850 | * callee can read/write into caller's stack |
| 4851 | */ |
| 4852 | init_func_state(env, callee, |
| 4853 | /* remember the callsite, it will be used by bpf_exit */ |
| 4854 | *insn_idx /* callsite */, |
| 4855 | state->curframe + 1 /* frameno within this callchain */, |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 4856 | subprog /* subprog number within this prog */); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4857 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4858 | /* Transfer references to the callee */ |
| 4859 | err = transfer_reference_state(callee, caller); |
| 4860 | if (err) |
| 4861 | return err; |
| 4862 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 4863 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 4864 | * pointers, which connects us up to the liveness chain |
| 4865 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4866 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 4867 | callee->regs[i] = caller->regs[i]; |
| 4868 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4869 | clear_caller_saved_regs(env, caller->regs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4870 | |
| 4871 | /* only increment it after check_reg_arg() finished */ |
| 4872 | state->curframe++; |
| 4873 | |
| 4874 | /* and go analyze first insn of the callee */ |
| 4875 | *insn_idx = target_insn; |
| 4876 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 4877 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4878 | verbose(env, "caller:\n"); |
| 4879 | print_verifier_state(env, caller); |
| 4880 | verbose(env, "callee:\n"); |
| 4881 | print_verifier_state(env, callee); |
| 4882 | } |
| 4883 | return 0; |
| 4884 | } |
| 4885 | |
| 4886 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 4887 | { |
| 4888 | struct bpf_verifier_state *state = env->cur_state; |
| 4889 | struct bpf_func_state *caller, *callee; |
| 4890 | struct bpf_reg_state *r0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4891 | int err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4892 | |
| 4893 | callee = state->frame[state->curframe]; |
| 4894 | r0 = &callee->regs[BPF_REG_0]; |
| 4895 | if (r0->type == PTR_TO_STACK) { |
| 4896 | /* technically it's ok to return caller's stack pointer |
| 4897 | * (or caller's caller's pointer) back to the caller, |
| 4898 | * since these pointers are valid. Only current stack |
| 4899 | * pointer will be invalid as soon as function exits, |
| 4900 | * but let's be conservative |
| 4901 | */ |
| 4902 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 4903 | return -EINVAL; |
| 4904 | } |
| 4905 | |
| 4906 | state->curframe--; |
| 4907 | caller = state->frame[state->curframe]; |
| 4908 | /* return to the caller whatever r0 had in the callee */ |
| 4909 | caller->regs[BPF_REG_0] = *r0; |
| 4910 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4911 | /* Transfer references to the caller */ |
| 4912 | err = transfer_reference_state(caller, callee); |
| 4913 | if (err) |
| 4914 | return err; |
| 4915 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4916 | *insn_idx = callee->callsite + 1; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 4917 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4918 | verbose(env, "returning from callee:\n"); |
| 4919 | print_verifier_state(env, callee); |
| 4920 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 4921 | print_verifier_state(env, caller); |
| 4922 | } |
| 4923 | /* clear everything in the callee */ |
| 4924 | free_func_state(callee); |
| 4925 | state->frame[state->curframe + 1] = NULL; |
| 4926 | return 0; |
| 4927 | } |
| 4928 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4929 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 4930 | int func_id, |
| 4931 | struct bpf_call_arg_meta *meta) |
| 4932 | { |
| 4933 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 4934 | |
| 4935 | if (ret_type != RET_INTEGER || |
| 4936 | (func_id != BPF_FUNC_get_stack && |
Daniel Borkmann | 47cc0ed | 2020-05-15 12:11:17 +0200 | [diff] [blame] | 4937 | func_id != BPF_FUNC_probe_read_str && |
| 4938 | func_id != BPF_FUNC_probe_read_kernel_str && |
| 4939 | func_id != BPF_FUNC_probe_read_user_str)) |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4940 | return; |
| 4941 | |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 4942 | ret_reg->smax_value = meta->msize_max_value; |
John Fastabend | fa123ac | 2020-03-30 14:36:59 -0700 | [diff] [blame] | 4943 | ret_reg->s32_max_value = meta->msize_max_value; |
Alexei Starovoitov | b0270958 | 2020-12-08 19:01:51 +0100 | [diff] [blame] | 4944 | ret_reg->smin_value = -MAX_ERRNO; |
| 4945 | ret_reg->s32_min_value = -MAX_ERRNO; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4946 | __reg_deduce_bounds(ret_reg); |
| 4947 | __reg_bound_offset(ret_reg); |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 4948 | __update_reg_bounds(ret_reg); |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4949 | } |
| 4950 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4951 | static int |
| 4952 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 4953 | int func_id, int insn_idx) |
| 4954 | { |
| 4955 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4956 | struct bpf_map *map = meta->map_ptr; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4957 | |
| 4958 | if (func_id != BPF_FUNC_tail_call && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 4959 | func_id != BPF_FUNC_map_lookup_elem && |
| 4960 | func_id != BPF_FUNC_map_update_elem && |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 4961 | func_id != BPF_FUNC_map_delete_elem && |
| 4962 | func_id != BPF_FUNC_map_push_elem && |
| 4963 | func_id != BPF_FUNC_map_pop_elem && |
| 4964 | func_id != BPF_FUNC_map_peek_elem) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4965 | return 0; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 4966 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4967 | if (map == NULL) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4968 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 4969 | return -EINVAL; |
| 4970 | } |
| 4971 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4972 | /* In case of read-only, some additional restrictions |
| 4973 | * need to be applied in order to prevent altering the |
| 4974 | * state of the map from program side. |
| 4975 | */ |
| 4976 | if ((map->map_flags & BPF_F_RDONLY_PROG) && |
| 4977 | (func_id == BPF_FUNC_map_delete_elem || |
| 4978 | func_id == BPF_FUNC_map_update_elem || |
| 4979 | func_id == BPF_FUNC_map_push_elem || |
| 4980 | func_id == BPF_FUNC_map_pop_elem)) { |
| 4981 | verbose(env, "write into map forbidden\n"); |
| 4982 | return -EACCES; |
| 4983 | } |
| 4984 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4985 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4986 | bpf_map_ptr_store(aux, meta->map_ptr, |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 4987 | !meta->map_ptr->bypass_spec_v1); |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4988 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4989 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 4990 | !meta->map_ptr->bypass_spec_v1); |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4991 | return 0; |
| 4992 | } |
| 4993 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4994 | static int |
| 4995 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 4996 | int func_id, int insn_idx) |
| 4997 | { |
| 4998 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
| 4999 | struct bpf_reg_state *regs = cur_regs(env), *reg; |
| 5000 | struct bpf_map *map = meta->map_ptr; |
| 5001 | struct tnum range; |
| 5002 | u64 val; |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 5003 | int err; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 5004 | |
| 5005 | if (func_id != BPF_FUNC_tail_call) |
| 5006 | return 0; |
| 5007 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { |
| 5008 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 5009 | return -EINVAL; |
| 5010 | } |
| 5011 | |
| 5012 | range = tnum_range(0, map->max_entries - 1); |
| 5013 | reg = ®s[BPF_REG_3]; |
| 5014 | |
| 5015 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { |
| 5016 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
| 5017 | return 0; |
| 5018 | } |
| 5019 | |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 5020 | err = mark_chain_precision(env, BPF_REG_3); |
| 5021 | if (err) |
| 5022 | return err; |
| 5023 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 5024 | val = reg->var_off.value; |
| 5025 | if (bpf_map_key_unseen(aux)) |
| 5026 | bpf_map_key_store(aux, val); |
| 5027 | else if (!bpf_map_key_poisoned(aux) && |
| 5028 | bpf_map_key_immediate(aux) != val) |
| 5029 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
| 5030 | return 0; |
| 5031 | } |
| 5032 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5033 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 5034 | { |
| 5035 | struct bpf_func_state *state = cur_func(env); |
| 5036 | int i; |
| 5037 | |
| 5038 | for (i = 0; i < state->acquired_refs; i++) { |
| 5039 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 5040 | state->refs[i].id, state->refs[i].insn_idx); |
| 5041 | } |
| 5042 | return state->acquired_refs ? -EINVAL : 0; |
| 5043 | } |
| 5044 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5045 | static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5046 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5047 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5048 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5049 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5050 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5051 | int i, err; |
| 5052 | |
| 5053 | /* find function prototype */ |
| 5054 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5055 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 5056 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5057 | return -EINVAL; |
| 5058 | } |
| 5059 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5060 | if (env->ops->get_func_proto) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 5061 | fn = env->ops->get_func_proto(func_id, env->prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5062 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5063 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 5064 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5065 | return -EINVAL; |
| 5066 | } |
| 5067 | |
| 5068 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 5069 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Daniel Borkmann | 3fe2867 | 2018-06-02 23:06:33 +0200 | [diff] [blame] | 5070 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5071 | return -EINVAL; |
| 5072 | } |
| 5073 | |
Jiri Olsa | eae2e83 | 2020-08-25 21:21:19 +0200 | [diff] [blame] | 5074 | if (fn->allowed && !fn->allowed(env->prog)) { |
| 5075 | verbose(env, "helper call is not allowed in probe\n"); |
| 5076 | return -EINVAL; |
| 5077 | } |
| 5078 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 5079 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 5080 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 5081 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 5082 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 5083 | func_id_name(func_id), func_id); |
| 5084 | return -EINVAL; |
| 5085 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5086 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5087 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5088 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5089 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5090 | err = check_func_proto(fn, func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5091 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5092 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 5093 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5094 | return err; |
| 5095 | } |
| 5096 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 5097 | meta.func_id = func_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5098 | /* check args */ |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 5099 | for (i = 0; i < 5; i++) { |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5100 | err = check_func_arg(env, i, &meta, fn); |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 5101 | if (err) |
| 5102 | return err; |
| 5103 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5104 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 5105 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 5106 | if (err) |
| 5107 | return err; |
| 5108 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 5109 | err = record_func_key(env, &meta, func_id, insn_idx); |
| 5110 | if (err) |
| 5111 | return err; |
| 5112 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5113 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 5114 | * is inferred from register state. |
| 5115 | */ |
| 5116 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 5117 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 5118 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5119 | if (err) |
| 5120 | return err; |
| 5121 | } |
| 5122 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5123 | if (func_id == BPF_FUNC_tail_call) { |
| 5124 | err = check_reference_leak(env); |
| 5125 | if (err) { |
| 5126 | verbose(env, "tail_call would lead to reference leak\n"); |
| 5127 | return err; |
| 5128 | } |
| 5129 | } else if (is_release_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5130 | err = release_reference(env, meta.ref_obj_id); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 5131 | if (err) { |
| 5132 | verbose(env, "func %s#%d reference has not been acquired before\n", |
| 5133 | func_id_name(func_id), func_id); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5134 | return err; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 5135 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5136 | } |
| 5137 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5138 | regs = cur_regs(env); |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 5139 | |
| 5140 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 5141 | * this is required because get_local_storage() can't return an error. |
| 5142 | */ |
| 5143 | if (func_id == BPF_FUNC_get_local_storage && |
| 5144 | !register_is_null(®s[BPF_REG_2])) { |
| 5145 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 5146 | return -EINVAL; |
| 5147 | } |
| 5148 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5149 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5150 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5151 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5152 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 5153 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5154 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 5155 | /* helper call returns 64-bit value. */ |
| 5156 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 5157 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5158 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5159 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5160 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5161 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5162 | } else if (fn->ret_type == RET_VOID) { |
| 5163 | regs[BPF_REG_0].type = NOT_INIT; |
Roman Gushchin | 3e6a4b3 | 2018-08-02 14:27:22 -0700 | [diff] [blame] | 5164 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 5165 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5166 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5167 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5168 | /* remember map_ptr, so that check_map_access() |
| 5169 | * can check 'value_size' boundary of memory access |
| 5170 | * to map element returned from bpf_map_lookup_elem() |
| 5171 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5172 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5173 | verbose(env, |
| 5174 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5175 | return -EINVAL; |
| 5176 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5177 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 5178 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 5179 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
Alexei Starovoitov | e16d2f1 | 2019-01-31 15:40:05 -0800 | [diff] [blame] | 5180 | if (map_value_has_spin_lock(meta.map_ptr)) |
| 5181 | regs[BPF_REG_0].id = ++env->id_gen; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 5182 | } else { |
| 5183 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 5184 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5185 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
| 5186 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5187 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; |
Lorenz Bauer | 85a51f8 | 2019-03-22 09:54:00 +0800 | [diff] [blame] | 5188 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
| 5189 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5190 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 5191 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
| 5192 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5193 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5194 | } else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) { |
| 5195 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5196 | regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5197 | regs[BPF_REG_0].mem_size = meta.mem_size; |
Hao Luo | 63d9b80 | 2020-09-29 16:50:48 -0700 | [diff] [blame] | 5198 | } else if (fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL || |
| 5199 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID) { |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5200 | const struct btf_type *t; |
| 5201 | |
| 5202 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 5203 | t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5204 | if (!btf_type_is_struct(t)) { |
| 5205 | u32 tsize; |
| 5206 | const struct btf_type *ret; |
| 5207 | const char *tname; |
| 5208 | |
| 5209 | /* resolve the type size of ksym. */ |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 5210 | ret = btf_resolve_size(meta.ret_btf, t, &tsize); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5211 | if (IS_ERR(ret)) { |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 5212 | tname = btf_name_by_offset(meta.ret_btf, t->name_off); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5213 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
| 5214 | tname, PTR_ERR(ret)); |
| 5215 | return -EINVAL; |
| 5216 | } |
Hao Luo | 63d9b80 | 2020-09-29 16:50:48 -0700 | [diff] [blame] | 5217 | regs[BPF_REG_0].type = |
| 5218 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? |
| 5219 | PTR_TO_MEM : PTR_TO_MEM_OR_NULL; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5220 | regs[BPF_REG_0].mem_size = tsize; |
| 5221 | } else { |
Hao Luo | 63d9b80 | 2020-09-29 16:50:48 -0700 | [diff] [blame] | 5222 | regs[BPF_REG_0].type = |
| 5223 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? |
| 5224 | PTR_TO_BTF_ID : PTR_TO_BTF_ID_OR_NULL; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 5225 | regs[BPF_REG_0].btf = meta.ret_btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5226 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
| 5227 | } |
KP Singh | 3ca1032 | 2020-11-06 10:37:43 +0000 | [diff] [blame] | 5228 | } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL || |
| 5229 | fn->ret_type == RET_PTR_TO_BTF_ID) { |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5230 | int ret_btf_id; |
| 5231 | |
| 5232 | mark_reg_known_zero(env, regs, BPF_REG_0); |
KP Singh | 3ca1032 | 2020-11-06 10:37:43 +0000 | [diff] [blame] | 5233 | regs[BPF_REG_0].type = fn->ret_type == RET_PTR_TO_BTF_ID ? |
| 5234 | PTR_TO_BTF_ID : |
| 5235 | PTR_TO_BTF_ID_OR_NULL; |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5236 | ret_btf_id = *fn->ret_btf_id; |
| 5237 | if (ret_btf_id == 0) { |
| 5238 | verbose(env, "invalid return type %d of func %s#%d\n", |
| 5239 | fn->ret_type, func_id_name(func_id), func_id); |
| 5240 | return -EINVAL; |
| 5241 | } |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 5242 | /* current BPF helper definitions are only coming from |
| 5243 | * built-in code with type IDs from vmlinux BTF |
| 5244 | */ |
| 5245 | regs[BPF_REG_0].btf = btf_vmlinux; |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5246 | regs[BPF_REG_0].btf_id = ret_btf_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5247 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5248 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 5249 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5250 | return -EINVAL; |
| 5251 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 5252 | |
Martin KaFai Lau | 93c230e | 2020-10-19 12:42:12 -0700 | [diff] [blame] | 5253 | if (reg_type_may_be_null(regs[BPF_REG_0].type)) |
| 5254 | regs[BPF_REG_0].id = ++env->id_gen; |
| 5255 | |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 5256 | if (is_ptr_cast_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5257 | /* For release_reference() */ |
| 5258 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 5259 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 5260 | int id = acquire_reference_state(env, insn_idx); |
| 5261 | |
| 5262 | if (id < 0) |
| 5263 | return id; |
| 5264 | /* For mark_ptr_or_null_reg() */ |
| 5265 | regs[BPF_REG_0].id = id; |
| 5266 | /* For release_reference() */ |
| 5267 | regs[BPF_REG_0].ref_obj_id = id; |
| 5268 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5269 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 5270 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
| 5271 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5272 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 5273 | if (err) |
| 5274 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 5275 | |
Song Liu | fa28dcb | 2020-06-29 23:28:44 -0700 | [diff] [blame] | 5276 | if ((func_id == BPF_FUNC_get_stack || |
| 5277 | func_id == BPF_FUNC_get_task_stack) && |
| 5278 | !env->prog->has_callchain_buf) { |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 5279 | const char *err_str; |
| 5280 | |
| 5281 | #ifdef CONFIG_PERF_EVENTS |
| 5282 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 5283 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 5284 | #else |
| 5285 | err = -ENOTSUPP; |
| 5286 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 5287 | #endif |
| 5288 | if (err) { |
| 5289 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 5290 | return err; |
| 5291 | } |
| 5292 | |
| 5293 | env->prog->has_callchain_buf = true; |
| 5294 | } |
| 5295 | |
Song Liu | 5d99cb2c | 2020-07-23 11:06:45 -0700 | [diff] [blame] | 5296 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
| 5297 | env->prog->call_get_stack = true; |
| 5298 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5299 | if (changes_data) |
| 5300 | clear_all_pkt_pointers(env); |
| 5301 | return 0; |
| 5302 | } |
| 5303 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5304 | static bool signed_add_overflows(s64 a, s64 b) |
| 5305 | { |
| 5306 | /* Do the add in u64, where overflow is well-defined */ |
| 5307 | s64 res = (s64)((u64)a + (u64)b); |
| 5308 | |
| 5309 | if (b < 0) |
| 5310 | return res > a; |
| 5311 | return res < a; |
| 5312 | } |
| 5313 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5314 | static bool signed_add32_overflows(s64 a, s64 b) |
| 5315 | { |
| 5316 | /* Do the add in u32, where overflow is well-defined */ |
| 5317 | s32 res = (s32)((u32)a + (u32)b); |
| 5318 | |
| 5319 | if (b < 0) |
| 5320 | return res > a; |
| 5321 | return res < a; |
| 5322 | } |
| 5323 | |
| 5324 | static bool signed_sub_overflows(s32 a, s32 b) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5325 | { |
| 5326 | /* Do the sub in u64, where overflow is well-defined */ |
| 5327 | s64 res = (s64)((u64)a - (u64)b); |
| 5328 | |
| 5329 | if (b < 0) |
| 5330 | return res < a; |
| 5331 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 5332 | } |
| 5333 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5334 | static bool signed_sub32_overflows(s32 a, s32 b) |
| 5335 | { |
| 5336 | /* Do the sub in u64, where overflow is well-defined */ |
| 5337 | s32 res = (s32)((u32)a - (u32)b); |
| 5338 | |
| 5339 | if (b < 0) |
| 5340 | return res < a; |
| 5341 | return res > a; |
| 5342 | } |
| 5343 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 5344 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 5345 | const struct bpf_reg_state *reg, |
| 5346 | enum bpf_reg_type type) |
| 5347 | { |
| 5348 | bool known = tnum_is_const(reg->var_off); |
| 5349 | s64 val = reg->var_off.value; |
| 5350 | s64 smin = reg->smin_value; |
| 5351 | |
| 5352 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 5353 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 5354 | reg_type_str[type], val); |
| 5355 | return false; |
| 5356 | } |
| 5357 | |
| 5358 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 5359 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 5360 | reg_type_str[type], reg->off); |
| 5361 | return false; |
| 5362 | } |
| 5363 | |
| 5364 | if (smin == S64_MIN) { |
| 5365 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 5366 | reg_type_str[type]); |
| 5367 | return false; |
| 5368 | } |
| 5369 | |
| 5370 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 5371 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 5372 | smin, reg_type_str[type]); |
| 5373 | return false; |
| 5374 | } |
| 5375 | |
| 5376 | return true; |
| 5377 | } |
| 5378 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5379 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
| 5380 | { |
| 5381 | return &env->insn_aux_data[env->insn_idx]; |
| 5382 | } |
| 5383 | |
| 5384 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
| 5385 | u32 *ptr_limit, u8 opcode, bool off_is_neg) |
| 5386 | { |
| 5387 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || |
| 5388 | (opcode == BPF_SUB && !off_is_neg); |
| 5389 | u32 off; |
| 5390 | |
| 5391 | switch (ptr_reg->type) { |
| 5392 | case PTR_TO_STACK: |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 5393 | /* Indirect variable offset stack access is prohibited in |
| 5394 | * unprivileged mode so it's not handled here. |
| 5395 | */ |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5396 | off = ptr_reg->off + ptr_reg->var_off.value; |
| 5397 | if (mask_to_left) |
| 5398 | *ptr_limit = MAX_BPF_STACK + off; |
| 5399 | else |
| 5400 | *ptr_limit = -off; |
| 5401 | return 0; |
| 5402 | case PTR_TO_MAP_VALUE: |
| 5403 | if (mask_to_left) { |
| 5404 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; |
| 5405 | } else { |
| 5406 | off = ptr_reg->smin_value + ptr_reg->off; |
| 5407 | *ptr_limit = ptr_reg->map_ptr->value_size - off; |
| 5408 | } |
| 5409 | return 0; |
| 5410 | default: |
| 5411 | return -EINVAL; |
| 5412 | } |
| 5413 | } |
| 5414 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 5415 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
| 5416 | const struct bpf_insn *insn) |
| 5417 | { |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 5418 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 5419 | } |
| 5420 | |
| 5421 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, |
| 5422 | u32 alu_state, u32 alu_limit) |
| 5423 | { |
| 5424 | /* If we arrived here from different branches with different |
| 5425 | * state or limits to sanitize, then this won't work. |
| 5426 | */ |
| 5427 | if (aux->alu_state && |
| 5428 | (aux->alu_state != alu_state || |
| 5429 | aux->alu_limit != alu_limit)) |
| 5430 | return -EACCES; |
| 5431 | |
| 5432 | /* Corresponding fixup done in fixup_bpf_calls(). */ |
| 5433 | aux->alu_state = alu_state; |
| 5434 | aux->alu_limit = alu_limit; |
| 5435 | return 0; |
| 5436 | } |
| 5437 | |
| 5438 | static int sanitize_val_alu(struct bpf_verifier_env *env, |
| 5439 | struct bpf_insn *insn) |
| 5440 | { |
| 5441 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 5442 | |
| 5443 | if (can_skip_alu_sanitation(env, insn)) |
| 5444 | return 0; |
| 5445 | |
| 5446 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); |
| 5447 | } |
| 5448 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5449 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
| 5450 | struct bpf_insn *insn, |
| 5451 | const struct bpf_reg_state *ptr_reg, |
| 5452 | struct bpf_reg_state *dst_reg, |
| 5453 | bool off_is_neg) |
| 5454 | { |
| 5455 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5456 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 5457 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
| 5458 | u8 opcode = BPF_OP(insn->code); |
| 5459 | u32 alu_state, alu_limit; |
| 5460 | struct bpf_reg_state tmp; |
| 5461 | bool ret; |
| 5462 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 5463 | if (can_skip_alu_sanitation(env, insn)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5464 | return 0; |
| 5465 | |
| 5466 | /* We already marked aux for masking from non-speculative |
| 5467 | * paths, thus we got here in the first place. We only care |
| 5468 | * to explore bad access from here. |
| 5469 | */ |
| 5470 | if (vstate->speculative) |
| 5471 | goto do_sim; |
| 5472 | |
| 5473 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; |
| 5474 | alu_state |= ptr_is_dst_reg ? |
| 5475 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; |
| 5476 | |
| 5477 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) |
| 5478 | return 0; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 5479 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5480 | return -EACCES; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5481 | do_sim: |
| 5482 | /* Simulate and find potential out-of-bounds access under |
| 5483 | * speculative execution from truncation as a result of |
| 5484 | * masking when off was not within expected range. If off |
| 5485 | * sits in dst, then we temporarily need to move ptr there |
| 5486 | * to simulate dst (== 0) +/-= ptr. Needed, for example, |
| 5487 | * for cases where we use K-based arithmetic in one direction |
| 5488 | * and truncated reg-based in the other in order to explore |
| 5489 | * bad access. |
| 5490 | */ |
| 5491 | if (!ptr_is_dst_reg) { |
| 5492 | tmp = *dst_reg; |
| 5493 | *dst_reg = *ptr_reg; |
| 5494 | } |
| 5495 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); |
Xu Yu | 0803278 | 2019-03-21 18:00:35 +0800 | [diff] [blame] | 5496 | if (!ptr_is_dst_reg && ret) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5497 | *dst_reg = tmp; |
| 5498 | return !ret ? -EFAULT : 0; |
| 5499 | } |
| 5500 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5501 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5502 | * Caller should also handle BPF_MOV case separately. |
| 5503 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 5504 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 5505 | */ |
| 5506 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 5507 | struct bpf_insn *insn, |
| 5508 | const struct bpf_reg_state *ptr_reg, |
| 5509 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5510 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5511 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5512 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 5513 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5514 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5515 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 5516 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 5517 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 5518 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 5519 | u32 dst = insn->dst_reg, src = insn->src_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5520 | u8 opcode = BPF_OP(insn->code); |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5521 | int ret; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5522 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5523 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5524 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 5525 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 5526 | smin_val > smax_val || umin_val > umax_val) { |
| 5527 | /* Taint dst register if offset had invalid bounds derived from |
| 5528 | * e.g. dead branches. |
| 5529 | */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 5530 | __mark_reg_unknown(env, dst_reg); |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 5531 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5532 | } |
| 5533 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5534 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 5535 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Yonghong Song | 6c69354 | 2020-06-18 16:46:31 -0700 | [diff] [blame] | 5536 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
| 5537 | __mark_reg_unknown(env, dst_reg); |
| 5538 | return 0; |
| 5539 | } |
| 5540 | |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5541 | verbose(env, |
| 5542 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 5543 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5544 | return -EACCES; |
| 5545 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 5546 | |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5547 | switch (ptr_reg->type) { |
| 5548 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 5549 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
| 5550 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5551 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5552 | case CONST_PTR_TO_MAP: |
Yonghong Song | 7c69673 | 2020-09-08 10:57:02 -0700 | [diff] [blame] | 5553 | /* smin_val represents the known value */ |
| 5554 | if (known && smin_val == 0 && opcode == BPF_ADD) |
| 5555 | break; |
Gustavo A. R. Silva | 8731745 | 2020-10-02 18:42:17 -0500 | [diff] [blame] | 5556 | fallthrough; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5557 | case PTR_TO_PACKET_END: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5558 | case PTR_TO_SOCKET: |
| 5559 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 5560 | case PTR_TO_SOCK_COMMON: |
| 5561 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 5562 | case PTR_TO_TCP_SOCK: |
| 5563 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 5564 | case PTR_TO_XDP_SOCK: |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5565 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
| 5566 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5567 | return -EACCES; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 5568 | case PTR_TO_MAP_VALUE: |
| 5569 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { |
| 5570 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", |
| 5571 | off_reg == dst_reg ? dst : src); |
| 5572 | return -EACCES; |
| 5573 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 5574 | fallthrough; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5575 | default: |
| 5576 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 5580 | * The id may be overwritten later if we create a new variable offset. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5581 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5582 | dst_reg->type = ptr_reg->type; |
| 5583 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 5584 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 5585 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 5586 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 5587 | return -EINVAL; |
| 5588 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5589 | /* pointer types do not carry 32-bit bounds at the moment. */ |
| 5590 | __mark_reg32_unbounded(dst_reg); |
| 5591 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5592 | switch (opcode) { |
| 5593 | case BPF_ADD: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5594 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 5595 | if (ret < 0) { |
| 5596 | verbose(env, "R%d tried to add from different maps or paths\n", dst); |
| 5597 | return ret; |
| 5598 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5599 | /* We can take a fixed offset as long as it doesn't overflow |
| 5600 | * the s32 'off' field |
| 5601 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5602 | if (known && (ptr_reg->off + smin_val == |
| 5603 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5604 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5605 | dst_reg->smin_value = smin_ptr; |
| 5606 | dst_reg->smax_value = smax_ptr; |
| 5607 | dst_reg->umin_value = umin_ptr; |
| 5608 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5609 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5610 | dst_reg->off = ptr_reg->off + smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5611 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5612 | break; |
| 5613 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5614 | /* A new variable offset is created. Note that off_reg->off |
| 5615 | * == 0, since it's a scalar. |
| 5616 | * dst_reg gets the pointer type and since some positive |
| 5617 | * integer value was added to the pointer, give it a new 'id' |
| 5618 | * if it's a PTR_TO_PACKET. |
| 5619 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 5620 | * added into the variable offset, and we copy the fixed offset |
| 5621 | * from ptr_reg. |
| 5622 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5623 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 5624 | signed_add_overflows(smax_ptr, smax_val)) { |
| 5625 | dst_reg->smin_value = S64_MIN; |
| 5626 | dst_reg->smax_value = S64_MAX; |
| 5627 | } else { |
| 5628 | dst_reg->smin_value = smin_ptr + smin_val; |
| 5629 | dst_reg->smax_value = smax_ptr + smax_val; |
| 5630 | } |
| 5631 | if (umin_ptr + umin_val < umin_ptr || |
| 5632 | umax_ptr + umax_val < umax_ptr) { |
| 5633 | dst_reg->umin_value = 0; |
| 5634 | dst_reg->umax_value = U64_MAX; |
| 5635 | } else { |
| 5636 | dst_reg->umin_value = umin_ptr + umin_val; |
| 5637 | dst_reg->umax_value = umax_ptr + umax_val; |
| 5638 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5639 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 5640 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5641 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5642 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5643 | dst_reg->id = ++env->id_gen; |
| 5644 | /* something was added to pkt_ptr, set range to zero */ |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 5645 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5646 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5647 | break; |
| 5648 | case BPF_SUB: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5649 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 5650 | if (ret < 0) { |
| 5651 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); |
| 5652 | return ret; |
| 5653 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5654 | if (dst_reg == off_reg) { |
| 5655 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5656 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 5657 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5658 | return -EACCES; |
| 5659 | } |
| 5660 | /* We don't allow subtraction from FP, because (according to |
| 5661 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 5662 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 5663 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5664 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5665 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 5666 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5667 | return -EACCES; |
| 5668 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5669 | if (known && (ptr_reg->off - smin_val == |
| 5670 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5671 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5672 | dst_reg->smin_value = smin_ptr; |
| 5673 | dst_reg->smax_value = smax_ptr; |
| 5674 | dst_reg->umin_value = umin_ptr; |
| 5675 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5676 | dst_reg->var_off = ptr_reg->var_off; |
| 5677 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5678 | dst_reg->off = ptr_reg->off - smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5679 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5680 | break; |
| 5681 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5682 | /* A new variable offset is created. If the subtrahend is known |
| 5683 | * nonnegative, then any reg->range we had before is still good. |
| 5684 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5685 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 5686 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 5687 | /* Overflow possible, we know nothing */ |
| 5688 | dst_reg->smin_value = S64_MIN; |
| 5689 | dst_reg->smax_value = S64_MAX; |
| 5690 | } else { |
| 5691 | dst_reg->smin_value = smin_ptr - smax_val; |
| 5692 | dst_reg->smax_value = smax_ptr - smin_val; |
| 5693 | } |
| 5694 | if (umin_ptr < umax_val) { |
| 5695 | /* Overflow possible, we know nothing */ |
| 5696 | dst_reg->umin_value = 0; |
| 5697 | dst_reg->umax_value = U64_MAX; |
| 5698 | } else { |
| 5699 | /* Cannot overflow (as long as bounds are consistent) */ |
| 5700 | dst_reg->umin_value = umin_ptr - umax_val; |
| 5701 | dst_reg->umax_value = umax_ptr - umin_val; |
| 5702 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5703 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 5704 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5705 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5706 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5707 | dst_reg->id = ++env->id_gen; |
| 5708 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5709 | if (smin_val < 0) |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 5710 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5711 | } |
| 5712 | break; |
| 5713 | case BPF_AND: |
| 5714 | case BPF_OR: |
| 5715 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5716 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 5717 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 5718 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5719 | return -EACCES; |
| 5720 | default: |
| 5721 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5722 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 5723 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5724 | return -EACCES; |
| 5725 | } |
| 5726 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 5727 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 5728 | return -EINVAL; |
| 5729 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5730 | __update_reg_bounds(dst_reg); |
| 5731 | __reg_deduce_bounds(dst_reg); |
| 5732 | __reg_bound_offset(dst_reg); |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 5733 | |
| 5734 | /* For unprivileged we require that resulting offset must be in bounds |
| 5735 | * in order to be able to sanitize access later on. |
| 5736 | */ |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 5737 | if (!env->bypass_spec_v1) { |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 5738 | if (dst_reg->type == PTR_TO_MAP_VALUE && |
| 5739 | check_map_access(env, dst, dst_reg->off, 1, false)) { |
| 5740 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
| 5741 | "prohibited for !root\n", dst); |
| 5742 | return -EACCES; |
| 5743 | } else if (dst_reg->type == PTR_TO_STACK && |
| 5744 | check_stack_access(env, dst_reg, dst_reg->off + |
| 5745 | dst_reg->var_off.value, 1)) { |
| 5746 | verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 5747 | "prohibited for !root\n", dst); |
| 5748 | return -EACCES; |
| 5749 | } |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 5750 | } |
| 5751 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5752 | return 0; |
| 5753 | } |
| 5754 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5755 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
| 5756 | struct bpf_reg_state *src_reg) |
| 5757 | { |
| 5758 | s32 smin_val = src_reg->s32_min_value; |
| 5759 | s32 smax_val = src_reg->s32_max_value; |
| 5760 | u32 umin_val = src_reg->u32_min_value; |
| 5761 | u32 umax_val = src_reg->u32_max_value; |
| 5762 | |
| 5763 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || |
| 5764 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { |
| 5765 | dst_reg->s32_min_value = S32_MIN; |
| 5766 | dst_reg->s32_max_value = S32_MAX; |
| 5767 | } else { |
| 5768 | dst_reg->s32_min_value += smin_val; |
| 5769 | dst_reg->s32_max_value += smax_val; |
| 5770 | } |
| 5771 | if (dst_reg->u32_min_value + umin_val < umin_val || |
| 5772 | dst_reg->u32_max_value + umax_val < umax_val) { |
| 5773 | dst_reg->u32_min_value = 0; |
| 5774 | dst_reg->u32_max_value = U32_MAX; |
| 5775 | } else { |
| 5776 | dst_reg->u32_min_value += umin_val; |
| 5777 | dst_reg->u32_max_value += umax_val; |
| 5778 | } |
| 5779 | } |
| 5780 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5781 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
| 5782 | struct bpf_reg_state *src_reg) |
| 5783 | { |
| 5784 | s64 smin_val = src_reg->smin_value; |
| 5785 | s64 smax_val = src_reg->smax_value; |
| 5786 | u64 umin_val = src_reg->umin_value; |
| 5787 | u64 umax_val = src_reg->umax_value; |
| 5788 | |
| 5789 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 5790 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 5791 | dst_reg->smin_value = S64_MIN; |
| 5792 | dst_reg->smax_value = S64_MAX; |
| 5793 | } else { |
| 5794 | dst_reg->smin_value += smin_val; |
| 5795 | dst_reg->smax_value += smax_val; |
| 5796 | } |
| 5797 | if (dst_reg->umin_value + umin_val < umin_val || |
| 5798 | dst_reg->umax_value + umax_val < umax_val) { |
| 5799 | dst_reg->umin_value = 0; |
| 5800 | dst_reg->umax_value = U64_MAX; |
| 5801 | } else { |
| 5802 | dst_reg->umin_value += umin_val; |
| 5803 | dst_reg->umax_value += umax_val; |
| 5804 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5805 | } |
| 5806 | |
| 5807 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, |
| 5808 | struct bpf_reg_state *src_reg) |
| 5809 | { |
| 5810 | s32 smin_val = src_reg->s32_min_value; |
| 5811 | s32 smax_val = src_reg->s32_max_value; |
| 5812 | u32 umin_val = src_reg->u32_min_value; |
| 5813 | u32 umax_val = src_reg->u32_max_value; |
| 5814 | |
| 5815 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || |
| 5816 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { |
| 5817 | /* Overflow possible, we know nothing */ |
| 5818 | dst_reg->s32_min_value = S32_MIN; |
| 5819 | dst_reg->s32_max_value = S32_MAX; |
| 5820 | } else { |
| 5821 | dst_reg->s32_min_value -= smax_val; |
| 5822 | dst_reg->s32_max_value -= smin_val; |
| 5823 | } |
| 5824 | if (dst_reg->u32_min_value < umax_val) { |
| 5825 | /* Overflow possible, we know nothing */ |
| 5826 | dst_reg->u32_min_value = 0; |
| 5827 | dst_reg->u32_max_value = U32_MAX; |
| 5828 | } else { |
| 5829 | /* Cannot overflow (as long as bounds are consistent) */ |
| 5830 | dst_reg->u32_min_value -= umax_val; |
| 5831 | dst_reg->u32_max_value -= umin_val; |
| 5832 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5833 | } |
| 5834 | |
| 5835 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, |
| 5836 | struct bpf_reg_state *src_reg) |
| 5837 | { |
| 5838 | s64 smin_val = src_reg->smin_value; |
| 5839 | s64 smax_val = src_reg->smax_value; |
| 5840 | u64 umin_val = src_reg->umin_value; |
| 5841 | u64 umax_val = src_reg->umax_value; |
| 5842 | |
| 5843 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 5844 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 5845 | /* Overflow possible, we know nothing */ |
| 5846 | dst_reg->smin_value = S64_MIN; |
| 5847 | dst_reg->smax_value = S64_MAX; |
| 5848 | } else { |
| 5849 | dst_reg->smin_value -= smax_val; |
| 5850 | dst_reg->smax_value -= smin_val; |
| 5851 | } |
| 5852 | if (dst_reg->umin_value < umax_val) { |
| 5853 | /* Overflow possible, we know nothing */ |
| 5854 | dst_reg->umin_value = 0; |
| 5855 | dst_reg->umax_value = U64_MAX; |
| 5856 | } else { |
| 5857 | /* Cannot overflow (as long as bounds are consistent) */ |
| 5858 | dst_reg->umin_value -= umax_val; |
| 5859 | dst_reg->umax_value -= umin_val; |
| 5860 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5861 | } |
| 5862 | |
| 5863 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, |
| 5864 | struct bpf_reg_state *src_reg) |
| 5865 | { |
| 5866 | s32 smin_val = src_reg->s32_min_value; |
| 5867 | u32 umin_val = src_reg->u32_min_value; |
| 5868 | u32 umax_val = src_reg->u32_max_value; |
| 5869 | |
| 5870 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { |
| 5871 | /* Ain't nobody got time to multiply that sign */ |
| 5872 | __mark_reg32_unbounded(dst_reg); |
| 5873 | return; |
| 5874 | } |
| 5875 | /* Both values are positive, so we can work with unsigned and |
| 5876 | * copy the result to signed (unless it exceeds S32_MAX). |
| 5877 | */ |
| 5878 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { |
| 5879 | /* Potential overflow, we know nothing */ |
| 5880 | __mark_reg32_unbounded(dst_reg); |
| 5881 | return; |
| 5882 | } |
| 5883 | dst_reg->u32_min_value *= umin_val; |
| 5884 | dst_reg->u32_max_value *= umax_val; |
| 5885 | if (dst_reg->u32_max_value > S32_MAX) { |
| 5886 | /* Overflow possible, we know nothing */ |
| 5887 | dst_reg->s32_min_value = S32_MIN; |
| 5888 | dst_reg->s32_max_value = S32_MAX; |
| 5889 | } else { |
| 5890 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 5891 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 5892 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5893 | } |
| 5894 | |
| 5895 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, |
| 5896 | struct bpf_reg_state *src_reg) |
| 5897 | { |
| 5898 | s64 smin_val = src_reg->smin_value; |
| 5899 | u64 umin_val = src_reg->umin_value; |
| 5900 | u64 umax_val = src_reg->umax_value; |
| 5901 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5902 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
| 5903 | /* Ain't nobody got time to multiply that sign */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5904 | __mark_reg64_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5905 | return; |
| 5906 | } |
| 5907 | /* Both values are positive, so we can work with unsigned and |
| 5908 | * copy the result to signed (unless it exceeds S64_MAX). |
| 5909 | */ |
| 5910 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 5911 | /* Potential overflow, we know nothing */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5912 | __mark_reg64_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5913 | return; |
| 5914 | } |
| 5915 | dst_reg->umin_value *= umin_val; |
| 5916 | dst_reg->umax_value *= umax_val; |
| 5917 | if (dst_reg->umax_value > S64_MAX) { |
| 5918 | /* Overflow possible, we know nothing */ |
| 5919 | dst_reg->smin_value = S64_MIN; |
| 5920 | dst_reg->smax_value = S64_MAX; |
| 5921 | } else { |
| 5922 | dst_reg->smin_value = dst_reg->umin_value; |
| 5923 | dst_reg->smax_value = dst_reg->umax_value; |
| 5924 | } |
| 5925 | } |
| 5926 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5927 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
| 5928 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5929 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5930 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 5931 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 5932 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 5933 | s32 smin_val = src_reg->s32_min_value; |
| 5934 | u32 umax_val = src_reg->u32_max_value; |
| 5935 | |
| 5936 | /* Assuming scalar64_min_max_and will be called so its safe |
| 5937 | * to skip updating register for known 32-bit case. |
| 5938 | */ |
| 5939 | if (src_known && dst_known) |
| 5940 | return; |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5941 | |
| 5942 | /* We get our minimum from the var_off, since that's inherently |
| 5943 | * bitwise. Our maximum is the minimum of the operands' maxima. |
| 5944 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5945 | dst_reg->u32_min_value = var32_off.value; |
| 5946 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); |
| 5947 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { |
| 5948 | /* Lose signed bounds when ANDing negative numbers, |
| 5949 | * ain't nobody got time for that. |
| 5950 | */ |
| 5951 | dst_reg->s32_min_value = S32_MIN; |
| 5952 | dst_reg->s32_max_value = S32_MAX; |
| 5953 | } else { |
| 5954 | /* ANDing two positives gives a positive, so safe to |
| 5955 | * cast result into s64. |
| 5956 | */ |
| 5957 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 5958 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 5959 | } |
| 5960 | |
| 5961 | } |
| 5962 | |
| 5963 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
| 5964 | struct bpf_reg_state *src_reg) |
| 5965 | { |
| 5966 | bool src_known = tnum_is_const(src_reg->var_off); |
| 5967 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 5968 | s64 smin_val = src_reg->smin_value; |
| 5969 | u64 umax_val = src_reg->umax_value; |
| 5970 | |
| 5971 | if (src_known && dst_known) { |
John Fastabend | 4fbb38a | 2020-09-24 11:45:06 -0700 | [diff] [blame] | 5972 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5973 | return; |
| 5974 | } |
| 5975 | |
| 5976 | /* We get our minimum from the var_off, since that's inherently |
| 5977 | * bitwise. Our maximum is the minimum of the operands' maxima. |
| 5978 | */ |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5979 | dst_reg->umin_value = dst_reg->var_off.value; |
| 5980 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 5981 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 5982 | /* Lose signed bounds when ANDing negative numbers, |
| 5983 | * ain't nobody got time for that. |
| 5984 | */ |
| 5985 | dst_reg->smin_value = S64_MIN; |
| 5986 | dst_reg->smax_value = S64_MAX; |
| 5987 | } else { |
| 5988 | /* ANDing two positives gives a positive, so safe to |
| 5989 | * cast result into s64. |
| 5990 | */ |
| 5991 | dst_reg->smin_value = dst_reg->umin_value; |
| 5992 | dst_reg->smax_value = dst_reg->umax_value; |
| 5993 | } |
| 5994 | /* We may learn something more from the var_off */ |
| 5995 | __update_reg_bounds(dst_reg); |
| 5996 | } |
| 5997 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5998 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
| 5999 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6000 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6001 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 6002 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 6003 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
Daniel Borkmann | 5b9fbeb | 2020-10-07 15:48:58 +0200 | [diff] [blame] | 6004 | s32 smin_val = src_reg->s32_min_value; |
| 6005 | u32 umin_val = src_reg->u32_min_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6006 | |
| 6007 | /* Assuming scalar64_min_max_or will be called so it is safe |
| 6008 | * to skip updating register for known case. |
| 6009 | */ |
| 6010 | if (src_known && dst_known) |
| 6011 | return; |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6012 | |
| 6013 | /* We get our maximum from the var_off, and our minimum is the |
| 6014 | * maximum of the operands' minima |
| 6015 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6016 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); |
| 6017 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; |
| 6018 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { |
| 6019 | /* Lose signed bounds when ORing negative numbers, |
| 6020 | * ain't nobody got time for that. |
| 6021 | */ |
| 6022 | dst_reg->s32_min_value = S32_MIN; |
| 6023 | dst_reg->s32_max_value = S32_MAX; |
| 6024 | } else { |
| 6025 | /* ORing two positives gives a positive, so safe to |
| 6026 | * cast result into s64. |
| 6027 | */ |
Daniel Borkmann | 5b9fbeb | 2020-10-07 15:48:58 +0200 | [diff] [blame] | 6028 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 6029 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6030 | } |
| 6031 | } |
| 6032 | |
| 6033 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
| 6034 | struct bpf_reg_state *src_reg) |
| 6035 | { |
| 6036 | bool src_known = tnum_is_const(src_reg->var_off); |
| 6037 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 6038 | s64 smin_val = src_reg->smin_value; |
| 6039 | u64 umin_val = src_reg->umin_value; |
| 6040 | |
| 6041 | if (src_known && dst_known) { |
John Fastabend | 4fbb38a | 2020-09-24 11:45:06 -0700 | [diff] [blame] | 6042 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6043 | return; |
| 6044 | } |
| 6045 | |
| 6046 | /* We get our maximum from the var_off, and our minimum is the |
| 6047 | * maximum of the operands' minima |
| 6048 | */ |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6049 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 6050 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; |
| 6051 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 6052 | /* Lose signed bounds when ORing negative numbers, |
| 6053 | * ain't nobody got time for that. |
| 6054 | */ |
| 6055 | dst_reg->smin_value = S64_MIN; |
| 6056 | dst_reg->smax_value = S64_MAX; |
| 6057 | } else { |
| 6058 | /* ORing two positives gives a positive, so safe to |
| 6059 | * cast result into s64. |
| 6060 | */ |
| 6061 | dst_reg->smin_value = dst_reg->umin_value; |
| 6062 | dst_reg->smax_value = dst_reg->umax_value; |
| 6063 | } |
| 6064 | /* We may learn something more from the var_off */ |
| 6065 | __update_reg_bounds(dst_reg); |
| 6066 | } |
| 6067 | |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 6068 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
| 6069 | struct bpf_reg_state *src_reg) |
| 6070 | { |
| 6071 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 6072 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 6073 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 6074 | s32 smin_val = src_reg->s32_min_value; |
| 6075 | |
| 6076 | /* Assuming scalar64_min_max_xor will be called so it is safe |
| 6077 | * to skip updating register for known case. |
| 6078 | */ |
| 6079 | if (src_known && dst_known) |
| 6080 | return; |
| 6081 | |
| 6082 | /* We get both minimum and maximum from the var32_off. */ |
| 6083 | dst_reg->u32_min_value = var32_off.value; |
| 6084 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; |
| 6085 | |
| 6086 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { |
| 6087 | /* XORing two positive sign numbers gives a positive, |
| 6088 | * so safe to cast u32 result into s32. |
| 6089 | */ |
| 6090 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 6091 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 6092 | } else { |
| 6093 | dst_reg->s32_min_value = S32_MIN; |
| 6094 | dst_reg->s32_max_value = S32_MAX; |
| 6095 | } |
| 6096 | } |
| 6097 | |
| 6098 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, |
| 6099 | struct bpf_reg_state *src_reg) |
| 6100 | { |
| 6101 | bool src_known = tnum_is_const(src_reg->var_off); |
| 6102 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 6103 | s64 smin_val = src_reg->smin_value; |
| 6104 | |
| 6105 | if (src_known && dst_known) { |
| 6106 | /* dst_reg->var_off.value has been updated earlier */ |
| 6107 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
| 6108 | return; |
| 6109 | } |
| 6110 | |
| 6111 | /* We get both minimum and maximum from the var_off. */ |
| 6112 | dst_reg->umin_value = dst_reg->var_off.value; |
| 6113 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; |
| 6114 | |
| 6115 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { |
| 6116 | /* XORing two positive sign numbers gives a positive, |
| 6117 | * so safe to cast u64 result into s64. |
| 6118 | */ |
| 6119 | dst_reg->smin_value = dst_reg->umin_value; |
| 6120 | dst_reg->smax_value = dst_reg->umax_value; |
| 6121 | } else { |
| 6122 | dst_reg->smin_value = S64_MIN; |
| 6123 | dst_reg->smax_value = S64_MAX; |
| 6124 | } |
| 6125 | |
| 6126 | __update_reg_bounds(dst_reg); |
| 6127 | } |
| 6128 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6129 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6130 | u64 umin_val, u64 umax_val) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6131 | { |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6132 | /* We lose all sign bit information (except what we can pick |
| 6133 | * up from var_off) |
| 6134 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6135 | dst_reg->s32_min_value = S32_MIN; |
| 6136 | dst_reg->s32_max_value = S32_MAX; |
| 6137 | /* If we might shift our top bit out, then we know nothing */ |
| 6138 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { |
| 6139 | dst_reg->u32_min_value = 0; |
| 6140 | dst_reg->u32_max_value = U32_MAX; |
| 6141 | } else { |
| 6142 | dst_reg->u32_min_value <<= umin_val; |
| 6143 | dst_reg->u32_max_value <<= umax_val; |
| 6144 | } |
| 6145 | } |
| 6146 | |
| 6147 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6148 | struct bpf_reg_state *src_reg) |
| 6149 | { |
| 6150 | u32 umax_val = src_reg->u32_max_value; |
| 6151 | u32 umin_val = src_reg->u32_min_value; |
| 6152 | /* u32 alu operation will zext upper bits */ |
| 6153 | struct tnum subreg = tnum_subreg(dst_reg->var_off); |
| 6154 | |
| 6155 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); |
| 6156 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); |
| 6157 | /* Not required but being careful mark reg64 bounds as unknown so |
| 6158 | * that we are forced to pick them up from tnum and zext later and |
| 6159 | * if some path skips this step we are still safe. |
| 6160 | */ |
| 6161 | __mark_reg64_unbounded(dst_reg); |
| 6162 | __update_reg32_bounds(dst_reg); |
| 6163 | } |
| 6164 | |
| 6165 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6166 | u64 umin_val, u64 umax_val) |
| 6167 | { |
| 6168 | /* Special case <<32 because it is a common compiler pattern to sign |
| 6169 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are |
| 6170 | * positive we know this shift will also be positive so we can track |
| 6171 | * bounds correctly. Otherwise we lose all sign bit information except |
| 6172 | * what we can pick up from var_off. Perhaps we can generalize this |
| 6173 | * later to shifts of any length. |
| 6174 | */ |
| 6175 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) |
| 6176 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; |
| 6177 | else |
| 6178 | dst_reg->smax_value = S64_MAX; |
| 6179 | |
| 6180 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) |
| 6181 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; |
| 6182 | else |
| 6183 | dst_reg->smin_value = S64_MIN; |
| 6184 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6185 | /* If we might shift our top bit out, then we know nothing */ |
| 6186 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 6187 | dst_reg->umin_value = 0; |
| 6188 | dst_reg->umax_value = U64_MAX; |
| 6189 | } else { |
| 6190 | dst_reg->umin_value <<= umin_val; |
| 6191 | dst_reg->umax_value <<= umax_val; |
| 6192 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6193 | } |
| 6194 | |
| 6195 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6196 | struct bpf_reg_state *src_reg) |
| 6197 | { |
| 6198 | u64 umax_val = src_reg->umax_value; |
| 6199 | u64 umin_val = src_reg->umin_value; |
| 6200 | |
| 6201 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ |
| 6202 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); |
| 6203 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); |
| 6204 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6205 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
| 6206 | /* We may learn something more from the var_off */ |
| 6207 | __update_reg_bounds(dst_reg); |
| 6208 | } |
| 6209 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6210 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
| 6211 | struct bpf_reg_state *src_reg) |
| 6212 | { |
| 6213 | struct tnum subreg = tnum_subreg(dst_reg->var_off); |
| 6214 | u32 umax_val = src_reg->u32_max_value; |
| 6215 | u32 umin_val = src_reg->u32_min_value; |
| 6216 | |
| 6217 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 6218 | * be negative, then either: |
| 6219 | * 1) src_reg might be zero, so the sign bit of the result is |
| 6220 | * unknown, so we lose our signed bounds |
| 6221 | * 2) it's known negative, thus the unsigned bounds capture the |
| 6222 | * signed bounds |
| 6223 | * 3) the signed bounds cross zero, so they tell us nothing |
| 6224 | * about the result |
| 6225 | * If the value in dst_reg is known nonnegative, then again the |
| 6226 | * unsigned bounts capture the signed bounds. |
| 6227 | * Thus, in all cases it suffices to blow away our signed bounds |
| 6228 | * and rely on inferring new ones from the unsigned bounds and |
| 6229 | * var_off of the result. |
| 6230 | */ |
| 6231 | dst_reg->s32_min_value = S32_MIN; |
| 6232 | dst_reg->s32_max_value = S32_MAX; |
| 6233 | |
| 6234 | dst_reg->var_off = tnum_rshift(subreg, umin_val); |
| 6235 | dst_reg->u32_min_value >>= umax_val; |
| 6236 | dst_reg->u32_max_value >>= umin_val; |
| 6237 | |
| 6238 | __mark_reg64_unbounded(dst_reg); |
| 6239 | __update_reg32_bounds(dst_reg); |
| 6240 | } |
| 6241 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6242 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
| 6243 | struct bpf_reg_state *src_reg) |
| 6244 | { |
| 6245 | u64 umax_val = src_reg->umax_value; |
| 6246 | u64 umin_val = src_reg->umin_value; |
| 6247 | |
| 6248 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 6249 | * be negative, then either: |
| 6250 | * 1) src_reg might be zero, so the sign bit of the result is |
| 6251 | * unknown, so we lose our signed bounds |
| 6252 | * 2) it's known negative, thus the unsigned bounds capture the |
| 6253 | * signed bounds |
| 6254 | * 3) the signed bounds cross zero, so they tell us nothing |
| 6255 | * about the result |
| 6256 | * If the value in dst_reg is known nonnegative, then again the |
| 6257 | * unsigned bounts capture the signed bounds. |
| 6258 | * Thus, in all cases it suffices to blow away our signed bounds |
| 6259 | * and rely on inferring new ones from the unsigned bounds and |
| 6260 | * var_off of the result. |
| 6261 | */ |
| 6262 | dst_reg->smin_value = S64_MIN; |
| 6263 | dst_reg->smax_value = S64_MAX; |
| 6264 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
| 6265 | dst_reg->umin_value >>= umax_val; |
| 6266 | dst_reg->umax_value >>= umin_val; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6267 | |
| 6268 | /* Its not easy to operate on alu32 bounds here because it depends |
| 6269 | * on bits being shifted in. Take easy way out and mark unbounded |
| 6270 | * so we can recalculate later from tnum. |
| 6271 | */ |
| 6272 | __mark_reg32_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6273 | __update_reg_bounds(dst_reg); |
| 6274 | } |
| 6275 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6276 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
| 6277 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6278 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6279 | u64 umin_val = src_reg->u32_min_value; |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6280 | |
| 6281 | /* Upon reaching here, src_known is true and |
| 6282 | * umax_val is equal to umin_val. |
| 6283 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6284 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
| 6285 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6286 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6287 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
| 6288 | |
| 6289 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 6290 | * dst_reg var_off to refine the result. |
| 6291 | */ |
| 6292 | dst_reg->u32_min_value = 0; |
| 6293 | dst_reg->u32_max_value = U32_MAX; |
| 6294 | |
| 6295 | __mark_reg64_unbounded(dst_reg); |
| 6296 | __update_reg32_bounds(dst_reg); |
| 6297 | } |
| 6298 | |
| 6299 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, |
| 6300 | struct bpf_reg_state *src_reg) |
| 6301 | { |
| 6302 | u64 umin_val = src_reg->umin_value; |
| 6303 | |
| 6304 | /* Upon reaching here, src_known is true and umax_val is equal |
| 6305 | * to umin_val. |
| 6306 | */ |
| 6307 | dst_reg->smin_value >>= umin_val; |
| 6308 | dst_reg->smax_value >>= umin_val; |
| 6309 | |
| 6310 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6311 | |
| 6312 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 6313 | * dst_reg var_off to refine the result. |
| 6314 | */ |
| 6315 | dst_reg->umin_value = 0; |
| 6316 | dst_reg->umax_value = U64_MAX; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6317 | |
| 6318 | /* Its not easy to operate on alu32 bounds here because it depends |
| 6319 | * on bits being shifted in from upper 32-bits. Take easy way out |
| 6320 | * and mark unbounded so we can recalculate later from tnum. |
| 6321 | */ |
| 6322 | __mark_reg32_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6323 | __update_reg_bounds(dst_reg); |
| 6324 | } |
| 6325 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6326 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 6327 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 6328 | * need extra checks in the 32-bit case. |
| 6329 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6330 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 6331 | struct bpf_insn *insn, |
| 6332 | struct bpf_reg_state *dst_reg, |
| 6333 | struct bpf_reg_state src_reg) |
| 6334 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6335 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6336 | u8 opcode = BPF_OP(insn->code); |
Mao Wenan | b0b3fb6 | 2020-04-18 09:37:35 +0800 | [diff] [blame] | 6337 | bool src_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6338 | s64 smin_val, smax_val; |
| 6339 | u64 umin_val, umax_val; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6340 | s32 s32_min_val, s32_max_val; |
| 6341 | u32 u32_min_val, u32_max_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6342 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 6343 | u32 dst = insn->dst_reg; |
| 6344 | int ret; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6345 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
Jann Horn | b799207 | 2018-10-05 18:17:59 +0200 | [diff] [blame] | 6346 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6347 | smin_val = src_reg.smin_value; |
| 6348 | smax_val = src_reg.smax_value; |
| 6349 | umin_val = src_reg.umin_value; |
| 6350 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6351 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6352 | s32_min_val = src_reg.s32_min_value; |
| 6353 | s32_max_val = src_reg.s32_max_value; |
| 6354 | u32_min_val = src_reg.u32_min_value; |
| 6355 | u32_max_val = src_reg.u32_max_value; |
| 6356 | |
| 6357 | if (alu32) { |
| 6358 | src_known = tnum_subreg_is_const(src_reg.var_off); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6359 | if ((src_known && |
| 6360 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || |
| 6361 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { |
| 6362 | /* Taint dst register if offset had invalid bounds |
| 6363 | * derived from e.g. dead branches. |
| 6364 | */ |
| 6365 | __mark_reg_unknown(env, dst_reg); |
| 6366 | return 0; |
| 6367 | } |
| 6368 | } else { |
| 6369 | src_known = tnum_is_const(src_reg.var_off); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6370 | if ((src_known && |
| 6371 | (smin_val != smax_val || umin_val != umax_val)) || |
| 6372 | smin_val > smax_val || umin_val > umax_val) { |
| 6373 | /* Taint dst register if offset had invalid bounds |
| 6374 | * derived from e.g. dead branches. |
| 6375 | */ |
| 6376 | __mark_reg_unknown(env, dst_reg); |
| 6377 | return 0; |
| 6378 | } |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 6379 | } |
| 6380 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 6381 | if (!src_known && |
| 6382 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 6383 | __mark_reg_unknown(env, dst_reg); |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 6384 | return 0; |
| 6385 | } |
| 6386 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6387 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
| 6388 | * There are two classes of instructions: The first class we track both |
| 6389 | * alu32 and alu64 sign/unsigned bounds independently this provides the |
| 6390 | * greatest amount of precision when alu operations are mixed with jmp32 |
| 6391 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, |
| 6392 | * and BPF_OR. This is possible because these ops have fairly easy to |
| 6393 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. |
| 6394 | * See alu32 verifier tests for examples. The second class of |
| 6395 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy |
| 6396 | * with regards to tracking sign/unsigned bounds because the bits may |
| 6397 | * cross subreg boundaries in the alu64 case. When this happens we mark |
| 6398 | * the reg unbounded in the subreg bound space and use the resulting |
| 6399 | * tnum to calculate an approximation of the sign/unsigned bounds. |
| 6400 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6401 | switch (opcode) { |
| 6402 | case BPF_ADD: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 6403 | ret = sanitize_val_alu(env, insn); |
| 6404 | if (ret < 0) { |
| 6405 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); |
| 6406 | return ret; |
| 6407 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6408 | scalar32_min_max_add(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6409 | scalar_min_max_add(dst_reg, &src_reg); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6410 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6411 | break; |
| 6412 | case BPF_SUB: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 6413 | ret = sanitize_val_alu(env, insn); |
| 6414 | if (ret < 0) { |
| 6415 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); |
| 6416 | return ret; |
| 6417 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6418 | scalar32_min_max_sub(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6419 | scalar_min_max_sub(dst_reg, &src_reg); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6420 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6421 | break; |
| 6422 | case BPF_MUL: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6423 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 6424 | scalar32_min_max_mul(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6425 | scalar_min_max_mul(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6426 | break; |
| 6427 | case BPF_AND: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6428 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
| 6429 | scalar32_min_max_and(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6430 | scalar_min_max_and(dst_reg, &src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6431 | break; |
| 6432 | case BPF_OR: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6433 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
| 6434 | scalar32_min_max_or(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6435 | scalar_min_max_or(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6436 | break; |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 6437 | case BPF_XOR: |
| 6438 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); |
| 6439 | scalar32_min_max_xor(dst_reg, &src_reg); |
| 6440 | scalar_min_max_xor(dst_reg, &src_reg); |
| 6441 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6442 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6443 | if (umax_val >= insn_bitness) { |
| 6444 | /* Shifts greater than 31 or 63 are undefined. |
| 6445 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6446 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6447 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6448 | break; |
| 6449 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6450 | if (alu32) |
| 6451 | scalar32_min_max_lsh(dst_reg, &src_reg); |
| 6452 | else |
| 6453 | scalar_min_max_lsh(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6454 | break; |
| 6455 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6456 | if (umax_val >= insn_bitness) { |
| 6457 | /* Shifts greater than 31 or 63 are undefined. |
| 6458 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6459 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6460 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6461 | break; |
| 6462 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6463 | if (alu32) |
| 6464 | scalar32_min_max_rsh(dst_reg, &src_reg); |
| 6465 | else |
| 6466 | scalar_min_max_rsh(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6467 | break; |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 6468 | case BPF_ARSH: |
| 6469 | if (umax_val >= insn_bitness) { |
| 6470 | /* Shifts greater than 31 or 63 are undefined. |
| 6471 | * This includes shifts by a negative number. |
| 6472 | */ |
| 6473 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 6474 | break; |
| 6475 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6476 | if (alu32) |
| 6477 | scalar32_min_max_arsh(dst_reg, &src_reg); |
| 6478 | else |
| 6479 | scalar_min_max_arsh(dst_reg, &src_reg); |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 6480 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6481 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6482 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6483 | break; |
| 6484 | } |
| 6485 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6486 | /* ALU32 ops are zero extended into 64bit register */ |
| 6487 | if (alu32) |
| 6488 | zext_32_to_64(dst_reg); |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6489 | |
John Fastabend | 294f2fc | 2020-03-24 10:38:37 -0700 | [diff] [blame] | 6490 | __update_reg_bounds(dst_reg); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6491 | __reg_deduce_bounds(dst_reg); |
| 6492 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6493 | return 0; |
| 6494 | } |
| 6495 | |
| 6496 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 6497 | * and var_off. |
| 6498 | */ |
| 6499 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 6500 | struct bpf_insn *insn) |
| 6501 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6502 | struct bpf_verifier_state *vstate = env->cur_state; |
| 6503 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 6504 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6505 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 6506 | u8 opcode = BPF_OP(insn->code); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 6507 | int err; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6508 | |
| 6509 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6510 | src_reg = NULL; |
| 6511 | if (dst_reg->type != SCALAR_VALUE) |
| 6512 | ptr_reg = dst_reg; |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 6513 | else |
| 6514 | /* Make sure ID is cleared otherwise dst_reg min/max could be |
| 6515 | * incorrectly propagated into other registers by find_equal_scalars() |
| 6516 | */ |
| 6517 | dst_reg->id = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6518 | if (BPF_SRC(insn->code) == BPF_X) { |
| 6519 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6520 | if (src_reg->type != SCALAR_VALUE) { |
| 6521 | if (dst_reg->type != SCALAR_VALUE) { |
| 6522 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6523 | * an arbitrary scalar. Disallow all math except |
| 6524 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6525 | */ |
Alexei Starovoitov | dd06682 | 2018-09-12 14:06:10 -0700 | [diff] [blame] | 6526 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6527 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 6528 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6529 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6530 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 6531 | insn->dst_reg, |
| 6532 | bpf_alu_string[opcode >> 4]); |
| 6533 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6534 | } else { |
| 6535 | /* scalar += pointer |
| 6536 | * This is legal, but we have to reverse our |
| 6537 | * src/dest handling in computing the range |
| 6538 | */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 6539 | err = mark_chain_precision(env, insn->dst_reg); |
| 6540 | if (err) |
| 6541 | return err; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6542 | return adjust_ptr_min_max_vals(env, insn, |
| 6543 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6544 | } |
| 6545 | } else if (ptr_reg) { |
| 6546 | /* pointer += scalar */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 6547 | err = mark_chain_precision(env, insn->src_reg); |
| 6548 | if (err) |
| 6549 | return err; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6550 | return adjust_ptr_min_max_vals(env, insn, |
| 6551 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6552 | } |
| 6553 | } else { |
| 6554 | /* Pretend the src is a reg with a known value, since we only |
| 6555 | * need to be able to read from this state. |
| 6556 | */ |
| 6557 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6558 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6559 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6560 | if (ptr_reg) /* pointer += K */ |
| 6561 | return adjust_ptr_min_max_vals(env, insn, |
| 6562 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6563 | } |
| 6564 | |
| 6565 | /* Got here implies adding two SCALAR_VALUEs */ |
| 6566 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6567 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6568 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6569 | return -EINVAL; |
| 6570 | } |
| 6571 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6572 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6573 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6574 | return -EINVAL; |
| 6575 | } |
| 6576 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6577 | } |
| 6578 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6579 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6580 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6581 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6582 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6583 | u8 opcode = BPF_OP(insn->code); |
| 6584 | int err; |
| 6585 | |
| 6586 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 6587 | if (opcode == BPF_NEG) { |
| 6588 | if (BPF_SRC(insn->code) != 0 || |
| 6589 | insn->src_reg != BPF_REG_0 || |
| 6590 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6591 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6592 | return -EINVAL; |
| 6593 | } |
| 6594 | } else { |
| 6595 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 6596 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 6597 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6598 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6599 | return -EINVAL; |
| 6600 | } |
| 6601 | } |
| 6602 | |
| 6603 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6604 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6605 | if (err) |
| 6606 | return err; |
| 6607 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6608 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6609 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6610 | insn->dst_reg); |
| 6611 | return -EACCES; |
| 6612 | } |
| 6613 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6614 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6615 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6616 | if (err) |
| 6617 | return err; |
| 6618 | |
| 6619 | } else if (opcode == BPF_MOV) { |
| 6620 | |
| 6621 | if (BPF_SRC(insn->code) == BPF_X) { |
| 6622 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6623 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6624 | return -EINVAL; |
| 6625 | } |
| 6626 | |
| 6627 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6628 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6629 | if (err) |
| 6630 | return err; |
| 6631 | } else { |
| 6632 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6633 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6634 | return -EINVAL; |
| 6635 | } |
| 6636 | } |
| 6637 | |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 6638 | /* check dest operand, mark as required later */ |
| 6639 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6640 | if (err) |
| 6641 | return err; |
| 6642 | |
| 6643 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6644 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
| 6645 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; |
| 6646 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6647 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 6648 | /* case: R1 = R2 |
| 6649 | * copy register state to dest reg |
| 6650 | */ |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 6651 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
| 6652 | /* Assign src and dst registers the same ID |
| 6653 | * that will be used by find_equal_scalars() |
| 6654 | * to propagate min/max range. |
| 6655 | */ |
| 6656 | src_reg->id = ++env->id_gen; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6657 | *dst_reg = *src_reg; |
| 6658 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6659 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6660 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6661 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6662 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6663 | verbose(env, |
| 6664 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6665 | insn->src_reg); |
| 6666 | return -EACCES; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6667 | } else if (src_reg->type == SCALAR_VALUE) { |
| 6668 | *dst_reg = *src_reg; |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 6669 | /* Make sure ID is cleared otherwise |
| 6670 | * dst_reg min/max could be incorrectly |
| 6671 | * propagated into src_reg by find_equal_scalars() |
| 6672 | */ |
| 6673 | dst_reg->id = 0; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6674 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6675 | dst_reg->subreg_def = env->insn_idx + 1; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6676 | } else { |
| 6677 | mark_reg_unknown(env, regs, |
| 6678 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6679 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6680 | zext_32_to_64(dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6681 | } |
| 6682 | } else { |
| 6683 | /* case: R = imm |
| 6684 | * remember the value we stored into this reg |
| 6685 | */ |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 6686 | /* clear any state __mark_reg_known doesn't set */ |
| 6687 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6688 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 6689 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 6690 | __mark_reg_known(regs + insn->dst_reg, |
| 6691 | insn->imm); |
| 6692 | } else { |
| 6693 | __mark_reg_known(regs + insn->dst_reg, |
| 6694 | (u32)insn->imm); |
| 6695 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6696 | } |
| 6697 | |
| 6698 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6699 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6700 | return -EINVAL; |
| 6701 | |
| 6702 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 6703 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6704 | if (BPF_SRC(insn->code) == BPF_X) { |
| 6705 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6706 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6707 | return -EINVAL; |
| 6708 | } |
| 6709 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6710 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6711 | if (err) |
| 6712 | return err; |
| 6713 | } else { |
| 6714 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6715 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6716 | return -EINVAL; |
| 6717 | } |
| 6718 | } |
| 6719 | |
| 6720 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6721 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6722 | if (err) |
| 6723 | return err; |
| 6724 | |
| 6725 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 6726 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6727 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6728 | return -EINVAL; |
| 6729 | } |
| 6730 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 6731 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 6732 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 6733 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 6734 | |
| 6735 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6736 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 6737 | return -EINVAL; |
| 6738 | } |
| 6739 | } |
| 6740 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 6741 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6742 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 6743 | if (err) |
| 6744 | return err; |
| 6745 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6746 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6747 | } |
| 6748 | |
| 6749 | return 0; |
| 6750 | } |
| 6751 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 6752 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
| 6753 | struct bpf_reg_state *dst_reg, |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 6754 | enum bpf_reg_type type, int new_range) |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 6755 | { |
| 6756 | struct bpf_reg_state *reg; |
| 6757 | int i; |
| 6758 | |
| 6759 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 6760 | reg = &state->regs[i]; |
| 6761 | if (reg->type == type && reg->id == dst_reg->id) |
| 6762 | /* keep the maximum range already checked */ |
| 6763 | reg->range = max(reg->range, new_range); |
| 6764 | } |
| 6765 | |
| 6766 | bpf_for_each_spilled_reg(i, state, reg) { |
| 6767 | if (!reg) |
| 6768 | continue; |
| 6769 | if (reg->type == type && reg->id == dst_reg->id) |
| 6770 | reg->range = max(reg->range, new_range); |
| 6771 | } |
| 6772 | } |
| 6773 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6774 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 6775 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 6776 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6777 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6778 | { |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 6779 | int new_range, i; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6780 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6781 | if (dst_reg->off < 0 || |
| 6782 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6783 | /* This doesn't give us any range */ |
| 6784 | return; |
| 6785 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6786 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 6787 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6788 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 6789 | * than pkt_end, but that's because it's also less than pkt. |
| 6790 | */ |
| 6791 | return; |
| 6792 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6793 | new_range = dst_reg->off; |
| 6794 | if (range_right_open) |
| 6795 | new_range--; |
| 6796 | |
| 6797 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6798 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6799 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6800 | * |
| 6801 | * r2 = r3; |
| 6802 | * r2 += 8; |
| 6803 | * if (r2 > pkt_end) goto <handle exception> |
| 6804 | * <access okay> |
| 6805 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 6806 | * r2 = r3; |
| 6807 | * r2 += 8; |
| 6808 | * if (r2 < pkt_end) goto <access okay> |
| 6809 | * <handle exception> |
| 6810 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6811 | * Where: |
| 6812 | * r2 == dst_reg, pkt_end == src_reg |
| 6813 | * r2=pkt(id=n,off=8,r=0) |
| 6814 | * r3=pkt(id=n,off=0,r=0) |
| 6815 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6816 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6817 | * |
| 6818 | * r2 = r3; |
| 6819 | * r2 += 8; |
| 6820 | * if (pkt_end >= r2) goto <access okay> |
| 6821 | * <handle exception> |
| 6822 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 6823 | * r2 = r3; |
| 6824 | * r2 += 8; |
| 6825 | * if (pkt_end <= r2) goto <handle exception> |
| 6826 | * <access okay> |
| 6827 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6828 | * Where: |
| 6829 | * pkt_end == dst_reg, r2 == src_reg |
| 6830 | * r2=pkt(id=n,off=8,r=0) |
| 6831 | * r3=pkt(id=n,off=0,r=0) |
| 6832 | * |
| 6833 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6834 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 6835 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 6836 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6837 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6838 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6839 | /* If our ids match, then we must have the same max_value. And we |
| 6840 | * don't care about the other reg's fixed offset, since if it's too big |
| 6841 | * the range won't allow anything. |
| 6842 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 6843 | */ |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 6844 | for (i = 0; i <= vstate->curframe; i++) |
| 6845 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, |
| 6846 | new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6847 | } |
| 6848 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6849 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6850 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6851 | struct tnum subreg = tnum_subreg(reg->var_off); |
| 6852 | s32 sval = (s32)val; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6853 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6854 | switch (opcode) { |
| 6855 | case BPF_JEQ: |
| 6856 | if (tnum_is_const(subreg)) |
| 6857 | return !!tnum_equals_const(subreg, val); |
| 6858 | break; |
| 6859 | case BPF_JNE: |
| 6860 | if (tnum_is_const(subreg)) |
| 6861 | return !tnum_equals_const(subreg, val); |
| 6862 | break; |
| 6863 | case BPF_JSET: |
| 6864 | if ((~subreg.mask & subreg.value) & val) |
| 6865 | return 1; |
| 6866 | if (!((subreg.mask | subreg.value) & val)) |
| 6867 | return 0; |
| 6868 | break; |
| 6869 | case BPF_JGT: |
| 6870 | if (reg->u32_min_value > val) |
| 6871 | return 1; |
| 6872 | else if (reg->u32_max_value <= val) |
| 6873 | return 0; |
| 6874 | break; |
| 6875 | case BPF_JSGT: |
| 6876 | if (reg->s32_min_value > sval) |
| 6877 | return 1; |
| 6878 | else if (reg->s32_max_value < sval) |
| 6879 | return 0; |
| 6880 | break; |
| 6881 | case BPF_JLT: |
| 6882 | if (reg->u32_max_value < val) |
| 6883 | return 1; |
| 6884 | else if (reg->u32_min_value >= val) |
| 6885 | return 0; |
| 6886 | break; |
| 6887 | case BPF_JSLT: |
| 6888 | if (reg->s32_max_value < sval) |
| 6889 | return 1; |
| 6890 | else if (reg->s32_min_value >= sval) |
| 6891 | return 0; |
| 6892 | break; |
| 6893 | case BPF_JGE: |
| 6894 | if (reg->u32_min_value >= val) |
| 6895 | return 1; |
| 6896 | else if (reg->u32_max_value < val) |
| 6897 | return 0; |
| 6898 | break; |
| 6899 | case BPF_JSGE: |
| 6900 | if (reg->s32_min_value >= sval) |
| 6901 | return 1; |
| 6902 | else if (reg->s32_max_value < sval) |
| 6903 | return 0; |
| 6904 | break; |
| 6905 | case BPF_JLE: |
| 6906 | if (reg->u32_max_value <= val) |
| 6907 | return 1; |
| 6908 | else if (reg->u32_min_value > val) |
| 6909 | return 0; |
| 6910 | break; |
| 6911 | case BPF_JSLE: |
| 6912 | if (reg->s32_max_value <= sval) |
| 6913 | return 1; |
| 6914 | else if (reg->s32_min_value > sval) |
| 6915 | return 0; |
| 6916 | break; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6917 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6918 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6919 | return -1; |
| 6920 | } |
| 6921 | |
| 6922 | |
| 6923 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) |
| 6924 | { |
| 6925 | s64 sval = (s64)val; |
| 6926 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6927 | switch (opcode) { |
| 6928 | case BPF_JEQ: |
| 6929 | if (tnum_is_const(reg->var_off)) |
| 6930 | return !!tnum_equals_const(reg->var_off, val); |
| 6931 | break; |
| 6932 | case BPF_JNE: |
| 6933 | if (tnum_is_const(reg->var_off)) |
| 6934 | return !tnum_equals_const(reg->var_off, val); |
| 6935 | break; |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 6936 | case BPF_JSET: |
| 6937 | if ((~reg->var_off.mask & reg->var_off.value) & val) |
| 6938 | return 1; |
| 6939 | if (!((reg->var_off.mask | reg->var_off.value) & val)) |
| 6940 | return 0; |
| 6941 | break; |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6942 | case BPF_JGT: |
| 6943 | if (reg->umin_value > val) |
| 6944 | return 1; |
| 6945 | else if (reg->umax_value <= val) |
| 6946 | return 0; |
| 6947 | break; |
| 6948 | case BPF_JSGT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6949 | if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6950 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6951 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6952 | return 0; |
| 6953 | break; |
| 6954 | case BPF_JLT: |
| 6955 | if (reg->umax_value < val) |
| 6956 | return 1; |
| 6957 | else if (reg->umin_value >= val) |
| 6958 | return 0; |
| 6959 | break; |
| 6960 | case BPF_JSLT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6961 | if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6962 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6963 | else if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6964 | return 0; |
| 6965 | break; |
| 6966 | case BPF_JGE: |
| 6967 | if (reg->umin_value >= val) |
| 6968 | return 1; |
| 6969 | else if (reg->umax_value < val) |
| 6970 | return 0; |
| 6971 | break; |
| 6972 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6973 | if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6974 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6975 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6976 | return 0; |
| 6977 | break; |
| 6978 | case BPF_JLE: |
| 6979 | if (reg->umax_value <= val) |
| 6980 | return 1; |
| 6981 | else if (reg->umin_value > val) |
| 6982 | return 0; |
| 6983 | break; |
| 6984 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6985 | if (reg->smax_value <= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6986 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6987 | else if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6988 | return 0; |
| 6989 | break; |
| 6990 | } |
| 6991 | |
| 6992 | return -1; |
| 6993 | } |
| 6994 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6995 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
| 6996 | * and return: |
| 6997 | * 1 - branch will be taken and "goto target" will be executed |
| 6998 | * 0 - branch will not be taken and fall-through to next insn |
| 6999 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value |
| 7000 | * range [0,10] |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7001 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7002 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
| 7003 | bool is_jmp32) |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7004 | { |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 7005 | if (__is_pointer_value(false, reg)) { |
| 7006 | if (!reg_type_not_null(reg->type)) |
| 7007 | return -1; |
| 7008 | |
| 7009 | /* If pointer is valid tests against zero will fail so we can |
| 7010 | * use this to direct branch taken. |
| 7011 | */ |
| 7012 | if (val != 0) |
| 7013 | return -1; |
| 7014 | |
| 7015 | switch (opcode) { |
| 7016 | case BPF_JEQ: |
| 7017 | return 0; |
| 7018 | case BPF_JNE: |
| 7019 | return 1; |
| 7020 | default: |
| 7021 | return -1; |
| 7022 | } |
| 7023 | } |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7024 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7025 | if (is_jmp32) |
| 7026 | return is_branch32_taken(reg, val, opcode); |
| 7027 | return is_branch64_taken(reg, val, opcode); |
Jann Horn | 604dca5 | 2020-03-30 18:03:23 +0200 | [diff] [blame] | 7028 | } |
| 7029 | |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 7030 | static int flip_opcode(u32 opcode) |
| 7031 | { |
| 7032 | /* How can we transform "a <op> b" into "b <op> a"? */ |
| 7033 | static const u8 opcode_flip[16] = { |
| 7034 | /* these stay the same */ |
| 7035 | [BPF_JEQ >> 4] = BPF_JEQ, |
| 7036 | [BPF_JNE >> 4] = BPF_JNE, |
| 7037 | [BPF_JSET >> 4] = BPF_JSET, |
| 7038 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ |
| 7039 | [BPF_JGE >> 4] = BPF_JLE, |
| 7040 | [BPF_JGT >> 4] = BPF_JLT, |
| 7041 | [BPF_JLE >> 4] = BPF_JGE, |
| 7042 | [BPF_JLT >> 4] = BPF_JGT, |
| 7043 | [BPF_JSGE >> 4] = BPF_JSLE, |
| 7044 | [BPF_JSGT >> 4] = BPF_JSLT, |
| 7045 | [BPF_JSLE >> 4] = BPF_JSGE, |
| 7046 | [BPF_JSLT >> 4] = BPF_JSGT |
| 7047 | }; |
| 7048 | return opcode_flip[opcode >> 4]; |
| 7049 | } |
| 7050 | |
| 7051 | static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg, |
| 7052 | struct bpf_reg_state *src_reg, |
| 7053 | u8 opcode) |
| 7054 | { |
| 7055 | struct bpf_reg_state *pkt; |
| 7056 | |
| 7057 | if (src_reg->type == PTR_TO_PACKET_END) { |
| 7058 | pkt = dst_reg; |
| 7059 | } else if (dst_reg->type == PTR_TO_PACKET_END) { |
| 7060 | pkt = src_reg; |
| 7061 | opcode = flip_opcode(opcode); |
| 7062 | } else { |
| 7063 | return -1; |
| 7064 | } |
| 7065 | |
| 7066 | if (pkt->range >= 0) |
| 7067 | return -1; |
| 7068 | |
| 7069 | switch (opcode) { |
| 7070 | case BPF_JLE: |
| 7071 | /* pkt <= pkt_end */ |
| 7072 | fallthrough; |
| 7073 | case BPF_JGT: |
| 7074 | /* pkt > pkt_end */ |
| 7075 | if (pkt->range == BEYOND_PKT_END) |
| 7076 | /* pkt has at last one extra byte beyond pkt_end */ |
| 7077 | return opcode == BPF_JGT; |
| 7078 | break; |
| 7079 | case BPF_JLT: |
| 7080 | /* pkt < pkt_end */ |
| 7081 | fallthrough; |
| 7082 | case BPF_JGE: |
| 7083 | /* pkt >= pkt_end */ |
| 7084 | if (pkt->range == BEYOND_PKT_END || pkt->range == AT_PKT_END) |
| 7085 | return opcode == BPF_JGE; |
| 7086 | break; |
| 7087 | } |
| 7088 | return -1; |
| 7089 | } |
| 7090 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7091 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 7092 | * variable register that we are working on, and src_reg is a constant or we're |
| 7093 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7094 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7095 | */ |
| 7096 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7097 | struct bpf_reg_state *false_reg, |
| 7098 | u64 val, u32 val32, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7099 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7100 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7101 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
| 7102 | struct tnum false_64off = false_reg->var_off; |
| 7103 | struct tnum true_32off = tnum_subreg(true_reg->var_off); |
| 7104 | struct tnum true_64off = true_reg->var_off; |
| 7105 | s64 sval = (s64)val; |
| 7106 | s32 sval32 = (s32)val32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7107 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7108 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 7109 | * variable offset from the compare (unless src_reg were a pointer into |
| 7110 | * the same object, but we don't bother with that. |
| 7111 | * Since false_reg and true_reg have the same type by construction, we |
| 7112 | * only need to check one of them for pointerness. |
| 7113 | */ |
| 7114 | if (__is_pointer_value(false, false_reg)) |
| 7115 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 7116 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7117 | switch (opcode) { |
| 7118 | case BPF_JEQ: |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7119 | case BPF_JNE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7120 | { |
| 7121 | struct bpf_reg_state *reg = |
| 7122 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 7123 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 7124 | /* JEQ/JNE comparison doesn't change the register equivalence. |
| 7125 | * r1 = r2; |
| 7126 | * if (r1 == 42) goto label; |
| 7127 | * ... |
| 7128 | * label: // here both r1 and r2 are known to be 42. |
| 7129 | * |
| 7130 | * Hence when marking register as known preserve it's ID. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7131 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7132 | if (is_jmp32) |
| 7133 | __mark_reg32_known(reg, val32); |
| 7134 | else |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 7135 | ___mark_reg_known(reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7136 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7137 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 7138 | case BPF_JSET: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7139 | if (is_jmp32) { |
| 7140 | false_32off = tnum_and(false_32off, tnum_const(~val32)); |
| 7141 | if (is_power_of_2(val32)) |
| 7142 | true_32off = tnum_or(true_32off, |
| 7143 | tnum_const(val32)); |
| 7144 | } else { |
| 7145 | false_64off = tnum_and(false_64off, tnum_const(~val)); |
| 7146 | if (is_power_of_2(val)) |
| 7147 | true_64off = tnum_or(true_64off, |
| 7148 | tnum_const(val)); |
| 7149 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 7150 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7151 | case BPF_JGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7152 | case BPF_JGT: |
| 7153 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7154 | if (is_jmp32) { |
| 7155 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; |
| 7156 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; |
| 7157 | |
| 7158 | false_reg->u32_max_value = min(false_reg->u32_max_value, |
| 7159 | false_umax); |
| 7160 | true_reg->u32_min_value = max(true_reg->u32_min_value, |
| 7161 | true_umin); |
| 7162 | } else { |
| 7163 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; |
| 7164 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; |
| 7165 | |
| 7166 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 7167 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
| 7168 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7169 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7170 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7171 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7172 | case BPF_JSGT: |
| 7173 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7174 | if (is_jmp32) { |
| 7175 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; |
| 7176 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7177 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7178 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
| 7179 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); |
| 7180 | } else { |
| 7181 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; |
| 7182 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; |
| 7183 | |
| 7184 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 7185 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
| 7186 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7187 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7188 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 7189 | case BPF_JLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7190 | case BPF_JLT: |
| 7191 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7192 | if (is_jmp32) { |
| 7193 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; |
| 7194 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; |
| 7195 | |
| 7196 | false_reg->u32_min_value = max(false_reg->u32_min_value, |
| 7197 | false_umin); |
| 7198 | true_reg->u32_max_value = min(true_reg->u32_max_value, |
| 7199 | true_umax); |
| 7200 | } else { |
| 7201 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; |
| 7202 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; |
| 7203 | |
| 7204 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 7205 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
| 7206 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 7207 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7208 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 7209 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7210 | case BPF_JSLT: |
| 7211 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7212 | if (is_jmp32) { |
| 7213 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; |
| 7214 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7215 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7216 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
| 7217 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); |
| 7218 | } else { |
| 7219 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; |
| 7220 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; |
| 7221 | |
| 7222 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 7223 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
| 7224 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 7225 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7226 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7227 | default: |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 7228 | return; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7229 | } |
| 7230 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7231 | if (is_jmp32) { |
| 7232 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), |
| 7233 | tnum_subreg(false_32off)); |
| 7234 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), |
| 7235 | tnum_subreg(true_32off)); |
| 7236 | __reg_combine_32_into_64(false_reg); |
| 7237 | __reg_combine_32_into_64(true_reg); |
| 7238 | } else { |
| 7239 | false_reg->var_off = false_64off; |
| 7240 | true_reg->var_off = true_64off; |
| 7241 | __reg_combine_64_into_32(false_reg); |
| 7242 | __reg_combine_64_into_32(true_reg); |
| 7243 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7244 | } |
| 7245 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7246 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 7247 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7248 | */ |
| 7249 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7250 | struct bpf_reg_state *false_reg, |
| 7251 | u64 val, u32 val32, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7252 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7253 | { |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 7254 | opcode = flip_opcode(opcode); |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 7255 | /* This uses zero as "not present in table"; luckily the zero opcode, |
| 7256 | * BPF_JA, can't get here. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7257 | */ |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 7258 | if (opcode) |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7259 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7260 | } |
| 7261 | |
| 7262 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 7263 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 7264 | struct bpf_reg_state *dst_reg) |
| 7265 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7266 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 7267 | dst_reg->umin_value); |
| 7268 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 7269 | dst_reg->umax_value); |
| 7270 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 7271 | dst_reg->smin_value); |
| 7272 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 7273 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7274 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 7275 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7276 | /* We might have learned new bounds from the var_off. */ |
| 7277 | __update_reg_bounds(src_reg); |
| 7278 | __update_reg_bounds(dst_reg); |
| 7279 | /* We might have learned something about the sign bit. */ |
| 7280 | __reg_deduce_bounds(src_reg); |
| 7281 | __reg_deduce_bounds(dst_reg); |
| 7282 | /* We might have learned some bits from the bounds. */ |
| 7283 | __reg_bound_offset(src_reg); |
| 7284 | __reg_bound_offset(dst_reg); |
| 7285 | /* Intersecting with the old var_off might have improved our bounds |
| 7286 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 7287 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 7288 | */ |
| 7289 | __update_reg_bounds(src_reg); |
| 7290 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7291 | } |
| 7292 | |
| 7293 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 7294 | struct bpf_reg_state *true_dst, |
| 7295 | struct bpf_reg_state *false_src, |
| 7296 | struct bpf_reg_state *false_dst, |
| 7297 | u8 opcode) |
| 7298 | { |
| 7299 | switch (opcode) { |
| 7300 | case BPF_JEQ: |
| 7301 | __reg_combine_min_max(true_src, true_dst); |
| 7302 | break; |
| 7303 | case BPF_JNE: |
| 7304 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7305 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 7306 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7307 | } |
| 7308 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7309 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 7310 | struct bpf_reg_state *reg, u32 id, |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7311 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7312 | { |
Martin KaFai Lau | 93c230e | 2020-10-19 12:42:12 -0700 | [diff] [blame] | 7313 | if (reg_type_may_be_null(reg->type) && reg->id == id && |
| 7314 | !WARN_ON_ONCE(!reg->id)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7315 | /* Old offset (both fixed and variable parts) should |
| 7316 | * have been known-zero, because we don't allow pointer |
| 7317 | * arithmetic on pointers that might be NULL. |
| 7318 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7319 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 7320 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7321 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7322 | __mark_reg_known_zero(reg); |
| 7323 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7324 | } |
| 7325 | if (is_null) { |
| 7326 | reg->type = SCALAR_VALUE; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7327 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 7328 | const struct bpf_map *map = reg->map_ptr; |
| 7329 | |
| 7330 | if (map->inner_map_meta) { |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7331 | reg->type = CONST_PTR_TO_MAP; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 7332 | reg->map_ptr = map->inner_map_meta; |
| 7333 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 7334 | reg->type = PTR_TO_XDP_SOCK; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 7335 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || |
| 7336 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { |
| 7337 | reg->type = PTR_TO_SOCKET; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7338 | } else { |
| 7339 | reg->type = PTR_TO_MAP_VALUE; |
| 7340 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7341 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
| 7342 | reg->type = PTR_TO_SOCKET; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 7343 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
| 7344 | reg->type = PTR_TO_SOCK_COMMON; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 7345 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
| 7346 | reg->type = PTR_TO_TCP_SOCK; |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 7347 | } else if (reg->type == PTR_TO_BTF_ID_OR_NULL) { |
| 7348 | reg->type = PTR_TO_BTF_ID; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 7349 | } else if (reg->type == PTR_TO_MEM_OR_NULL) { |
| 7350 | reg->type = PTR_TO_MEM; |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 7351 | } else if (reg->type == PTR_TO_RDONLY_BUF_OR_NULL) { |
| 7352 | reg->type = PTR_TO_RDONLY_BUF; |
| 7353 | } else if (reg->type == PTR_TO_RDWR_BUF_OR_NULL) { |
| 7354 | reg->type = PTR_TO_RDWR_BUF; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 7355 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 7356 | if (is_null) { |
| 7357 | /* We don't need id and ref_obj_id from this point |
| 7358 | * onwards anymore, thus we should better reset it, |
| 7359 | * so that state pruning has chances to take effect. |
| 7360 | */ |
| 7361 | reg->id = 0; |
| 7362 | reg->ref_obj_id = 0; |
| 7363 | } else if (!reg_may_point_to_spin_lock(reg)) { |
| 7364 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
| 7365 | * in release_reg_references(). |
| 7366 | * |
| 7367 | * reg->id is still used by spin_lock ptr. Other |
| 7368 | * than spin_lock ptr type, reg->id can be reset. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7369 | */ |
| 7370 | reg->id = 0; |
| 7371 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7372 | } |
| 7373 | } |
| 7374 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 7375 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
| 7376 | bool is_null) |
| 7377 | { |
| 7378 | struct bpf_reg_state *reg; |
| 7379 | int i; |
| 7380 | |
| 7381 | for (i = 0; i < MAX_BPF_REG; i++) |
| 7382 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); |
| 7383 | |
| 7384 | bpf_for_each_spilled_reg(i, state, reg) { |
| 7385 | if (!reg) |
| 7386 | continue; |
| 7387 | mark_ptr_or_null_reg(state, reg, id, is_null); |
| 7388 | } |
| 7389 | } |
| 7390 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7391 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 7392 | * be folded together at some point. |
| 7393 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7394 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 7395 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7396 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7397 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 7398 | struct bpf_reg_state *regs = state->regs; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 7399 | u32 ref_obj_id = regs[regno].ref_obj_id; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 7400 | u32 id = regs[regno].id; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 7401 | int i; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7402 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 7403 | if (ref_obj_id && ref_obj_id == id && is_null) |
| 7404 | /* regs[regno] is in the " == NULL" branch. |
| 7405 | * No one could have freed the reference state before |
| 7406 | * doing the NULL check. |
| 7407 | */ |
| 7408 | WARN_ON_ONCE(release_reference_state(state, id)); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7409 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 7410 | for (i = 0; i <= vstate->curframe; i++) |
| 7411 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7412 | } |
| 7413 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7414 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 7415 | struct bpf_reg_state *dst_reg, |
| 7416 | struct bpf_reg_state *src_reg, |
| 7417 | struct bpf_verifier_state *this_branch, |
| 7418 | struct bpf_verifier_state *other_branch) |
| 7419 | { |
| 7420 | if (BPF_SRC(insn->code) != BPF_X) |
| 7421 | return false; |
| 7422 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7423 | /* Pointers are always 64-bit. */ |
| 7424 | if (BPF_CLASS(insn->code) == BPF_JMP32) |
| 7425 | return false; |
| 7426 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7427 | switch (BPF_OP(insn->code)) { |
| 7428 | case BPF_JGT: |
| 7429 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7430 | src_reg->type == PTR_TO_PACKET_END) || |
| 7431 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7432 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7433 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 7434 | find_good_pkt_pointers(this_branch, dst_reg, |
| 7435 | dst_reg->type, false); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 7436 | mark_pkt_end(other_branch, insn->dst_reg, true); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7437 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7438 | src_reg->type == PTR_TO_PACKET) || |
| 7439 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7440 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7441 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 7442 | find_good_pkt_pointers(other_branch, src_reg, |
| 7443 | src_reg->type, true); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 7444 | mark_pkt_end(this_branch, insn->src_reg, false); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7445 | } else { |
| 7446 | return false; |
| 7447 | } |
| 7448 | break; |
| 7449 | case BPF_JLT: |
| 7450 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7451 | src_reg->type == PTR_TO_PACKET_END) || |
| 7452 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7453 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7454 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 7455 | find_good_pkt_pointers(other_branch, dst_reg, |
| 7456 | dst_reg->type, true); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 7457 | mark_pkt_end(this_branch, insn->dst_reg, false); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7458 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7459 | src_reg->type == PTR_TO_PACKET) || |
| 7460 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7461 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7462 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 7463 | find_good_pkt_pointers(this_branch, src_reg, |
| 7464 | src_reg->type, false); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 7465 | mark_pkt_end(other_branch, insn->src_reg, true); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7466 | } else { |
| 7467 | return false; |
| 7468 | } |
| 7469 | break; |
| 7470 | case BPF_JGE: |
| 7471 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7472 | src_reg->type == PTR_TO_PACKET_END) || |
| 7473 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7474 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7475 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 7476 | find_good_pkt_pointers(this_branch, dst_reg, |
| 7477 | dst_reg->type, true); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 7478 | mark_pkt_end(other_branch, insn->dst_reg, false); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7479 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7480 | src_reg->type == PTR_TO_PACKET) || |
| 7481 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7482 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7483 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 7484 | find_good_pkt_pointers(other_branch, src_reg, |
| 7485 | src_reg->type, false); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 7486 | mark_pkt_end(this_branch, insn->src_reg, true); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7487 | } else { |
| 7488 | return false; |
| 7489 | } |
| 7490 | break; |
| 7491 | case BPF_JLE: |
| 7492 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7493 | src_reg->type == PTR_TO_PACKET_END) || |
| 7494 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7495 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7496 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 7497 | find_good_pkt_pointers(other_branch, dst_reg, |
| 7498 | dst_reg->type, false); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 7499 | mark_pkt_end(this_branch, insn->dst_reg, true); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7500 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7501 | src_reg->type == PTR_TO_PACKET) || |
| 7502 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7503 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7504 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 7505 | find_good_pkt_pointers(this_branch, src_reg, |
| 7506 | src_reg->type, true); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 7507 | mark_pkt_end(other_branch, insn->src_reg, false); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7508 | } else { |
| 7509 | return false; |
| 7510 | } |
| 7511 | break; |
| 7512 | default: |
| 7513 | return false; |
| 7514 | } |
| 7515 | |
| 7516 | return true; |
| 7517 | } |
| 7518 | |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 7519 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
| 7520 | struct bpf_reg_state *known_reg) |
| 7521 | { |
| 7522 | struct bpf_func_state *state; |
| 7523 | struct bpf_reg_state *reg; |
| 7524 | int i, j; |
| 7525 | |
| 7526 | for (i = 0; i <= vstate->curframe; i++) { |
| 7527 | state = vstate->frame[i]; |
| 7528 | for (j = 0; j < MAX_BPF_REG; j++) { |
| 7529 | reg = &state->regs[j]; |
| 7530 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) |
| 7531 | *reg = *known_reg; |
| 7532 | } |
| 7533 | |
| 7534 | bpf_for_each_spilled_reg(j, state, reg) { |
| 7535 | if (!reg) |
| 7536 | continue; |
| 7537 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) |
| 7538 | *reg = *known_reg; |
| 7539 | } |
| 7540 | } |
| 7541 | } |
| 7542 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7543 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7544 | struct bpf_insn *insn, int *insn_idx) |
| 7545 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7546 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 7547 | struct bpf_verifier_state *other_branch; |
| 7548 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 7549 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7550 | u8 opcode = BPF_OP(insn->code); |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7551 | bool is_jmp32; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 7552 | int pred = -1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7553 | int err; |
| 7554 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7555 | /* Only conditional jumps are expected to reach here. */ |
| 7556 | if (opcode == BPF_JA || opcode > BPF_JSLE) { |
| 7557 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7558 | return -EINVAL; |
| 7559 | } |
| 7560 | |
| 7561 | if (BPF_SRC(insn->code) == BPF_X) { |
| 7562 | if (insn->imm != 0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7563 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7564 | return -EINVAL; |
| 7565 | } |
| 7566 | |
| 7567 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7568 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7569 | if (err) |
| 7570 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7571 | |
| 7572 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7573 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7574 | insn->src_reg); |
| 7575 | return -EACCES; |
| 7576 | } |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 7577 | src_reg = ®s[insn->src_reg]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7578 | } else { |
| 7579 | if (insn->src_reg != BPF_REG_0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7580 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7581 | return -EINVAL; |
| 7582 | } |
| 7583 | } |
| 7584 | |
| 7585 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7586 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7587 | if (err) |
| 7588 | return err; |
| 7589 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 7590 | dst_reg = ®s[insn->dst_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7591 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 7592 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7593 | if (BPF_SRC(insn->code) == BPF_K) { |
| 7594 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); |
| 7595 | } else if (src_reg->type == SCALAR_VALUE && |
| 7596 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { |
| 7597 | pred = is_branch_taken(dst_reg, |
| 7598 | tnum_subreg(src_reg->var_off).value, |
| 7599 | opcode, |
| 7600 | is_jmp32); |
| 7601 | } else if (src_reg->type == SCALAR_VALUE && |
| 7602 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { |
| 7603 | pred = is_branch_taken(dst_reg, |
| 7604 | src_reg->var_off.value, |
| 7605 | opcode, |
| 7606 | is_jmp32); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 7607 | } else if (reg_is_pkt_pointer_any(dst_reg) && |
| 7608 | reg_is_pkt_pointer_any(src_reg) && |
| 7609 | !is_jmp32) { |
| 7610 | pred = is_pkt_ptr_branch_taken(dst_reg, src_reg, opcode); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7611 | } |
| 7612 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7613 | if (pred >= 0) { |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 7614 | /* If we get here with a dst_reg pointer type it is because |
| 7615 | * above is_branch_taken() special cased the 0 comparison. |
| 7616 | */ |
| 7617 | if (!__is_pointer_value(false, dst_reg)) |
| 7618 | err = mark_chain_precision(env, insn->dst_reg); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 7619 | if (BPF_SRC(insn->code) == BPF_X && !err && |
| 7620 | !__is_pointer_value(false, src_reg)) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7621 | err = mark_chain_precision(env, insn->src_reg); |
| 7622 | if (err) |
| 7623 | return err; |
| 7624 | } |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 7625 | if (pred == 1) { |
| 7626 | /* only follow the goto, ignore fall-through */ |
| 7627 | *insn_idx += insn->off; |
| 7628 | return 0; |
| 7629 | } else if (pred == 0) { |
| 7630 | /* only follow fall-through branch, since |
| 7631 | * that's where the program will go |
| 7632 | */ |
| 7633 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7634 | } |
| 7635 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7636 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
| 7637 | false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7638 | if (!other_branch) |
| 7639 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7640 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7641 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7642 | /* detect if we are comparing against a constant value so we can adjust |
| 7643 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7644 | * this is only legit if both are scalars (or pointers to the same |
| 7645 | * object, I suppose, but we don't support that right now), because |
| 7646 | * otherwise the different base pointers mean the offsets aren't |
| 7647 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7648 | */ |
| 7649 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7650 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7651 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7652 | if (dst_reg->type == SCALAR_VALUE && |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7653 | src_reg->type == SCALAR_VALUE) { |
| 7654 | if (tnum_is_const(src_reg->var_off) || |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7655 | (is_jmp32 && |
| 7656 | tnum_is_const(tnum_subreg(src_reg->var_off)))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7657 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7658 | dst_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7659 | src_reg->var_off.value, |
| 7660 | tnum_subreg(src_reg->var_off).value, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7661 | opcode, is_jmp32); |
| 7662 | else if (tnum_is_const(dst_reg->var_off) || |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7663 | (is_jmp32 && |
| 7664 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7665 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7666 | src_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7667 | dst_reg->var_off.value, |
| 7668 | tnum_subreg(dst_reg->var_off).value, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7669 | opcode, is_jmp32); |
| 7670 | else if (!is_jmp32 && |
| 7671 | (opcode == BPF_JEQ || opcode == BPF_JNE)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7672 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7673 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 7674 | &other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7675 | src_reg, dst_reg, opcode); |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 7676 | if (src_reg->id && |
| 7677 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 7678 | find_equal_scalars(this_branch, src_reg); |
| 7679 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); |
| 7680 | } |
| 7681 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7682 | } |
| 7683 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7684 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7685 | dst_reg, insn->imm, (u32)insn->imm, |
| 7686 | opcode, is_jmp32); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7687 | } |
| 7688 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 7689 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
| 7690 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 7691 | find_equal_scalars(this_branch, dst_reg); |
| 7692 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); |
| 7693 | } |
| 7694 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7695 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
| 7696 | * NOTE: these optimizations below are related with pointer comparison |
| 7697 | * which will never be JMP32. |
| 7698 | */ |
| 7699 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 7700 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7701 | reg_type_may_be_null(dst_reg->type)) { |
| 7702 | /* Mark all identical registers in each branch as either |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7703 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 7704 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7705 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 7706 | opcode == BPF_JNE); |
| 7707 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 7708 | opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7709 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 7710 | this_branch, other_branch) && |
| 7711 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7712 | verbose(env, "R%d pointer comparison prohibited\n", |
| 7713 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7714 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7715 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 7716 | if (env->log.level & BPF_LOG_LEVEL) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7717 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7718 | return 0; |
| 7719 | } |
| 7720 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7721 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7722 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7723 | { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7724 | struct bpf_insn_aux_data *aux = cur_aux(env); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7725 | struct bpf_reg_state *regs = cur_regs(env); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7726 | struct bpf_reg_state *dst_reg; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7727 | struct bpf_map *map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7728 | int err; |
| 7729 | |
| 7730 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7731 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7732 | return -EINVAL; |
| 7733 | } |
| 7734 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7735 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7736 | return -EINVAL; |
| 7737 | } |
| 7738 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7739 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7740 | if (err) |
| 7741 | return err; |
| 7742 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7743 | dst_reg = ®s[insn->dst_reg]; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 7744 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 7745 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 7746 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7747 | dst_reg->type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7748 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7749 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 7750 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7751 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7752 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
| 7753 | mark_reg_known_zero(env, regs, insn->dst_reg); |
| 7754 | |
| 7755 | dst_reg->type = aux->btf_var.reg_type; |
| 7756 | switch (dst_reg->type) { |
| 7757 | case PTR_TO_MEM: |
| 7758 | dst_reg->mem_size = aux->btf_var.mem_size; |
| 7759 | break; |
| 7760 | case PTR_TO_BTF_ID: |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 7761 | case PTR_TO_PERCPU_BTF_ID: |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 7762 | dst_reg->btf = aux->btf_var.btf; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7763 | dst_reg->btf_id = aux->btf_var.btf_id; |
| 7764 | break; |
| 7765 | default: |
| 7766 | verbose(env, "bpf verifier is misconfigured\n"); |
| 7767 | return -EFAULT; |
| 7768 | } |
| 7769 | return 0; |
| 7770 | } |
| 7771 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7772 | map = env->used_maps[aux->map_index]; |
| 7773 | mark_reg_known_zero(env, regs, insn->dst_reg); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7774 | dst_reg->map_ptr = map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7775 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7776 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7777 | dst_reg->type = PTR_TO_MAP_VALUE; |
| 7778 | dst_reg->off = aux->map_off; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7779 | if (map_value_has_spin_lock(map)) |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7780 | dst_reg->id = ++env->id_gen; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7781 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7782 | dst_reg->type = CONST_PTR_TO_MAP; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7783 | } else { |
| 7784 | verbose(env, "bpf verifier is misconfigured\n"); |
| 7785 | return -EINVAL; |
| 7786 | } |
| 7787 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7788 | return 0; |
| 7789 | } |
| 7790 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 7791 | static bool may_access_skb(enum bpf_prog_type type) |
| 7792 | { |
| 7793 | switch (type) { |
| 7794 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 7795 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 7796 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 7797 | return true; |
| 7798 | default: |
| 7799 | return false; |
| 7800 | } |
| 7801 | } |
| 7802 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7803 | /* verify safety of LD_ABS|LD_IND instructions: |
| 7804 | * - they can only appear in the programs where ctx == skb |
| 7805 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 7806 | * preserve R6-R9, and store return value into R0 |
| 7807 | * |
| 7808 | * Implicit input: |
| 7809 | * ctx == skb == R6 == CTX |
| 7810 | * |
| 7811 | * Explicit input: |
| 7812 | * SRC == any register |
| 7813 | * IMM == 32-bit immediate |
| 7814 | * |
| 7815 | * Output: |
| 7816 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 7817 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7818 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7819 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7820 | struct bpf_reg_state *regs = cur_regs(env); |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 7821 | static const int ctx_reg = BPF_REG_6; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7822 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7823 | int i, err; |
| 7824 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 7825 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7826 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7827 | return -EINVAL; |
| 7828 | } |
| 7829 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 7830 | if (!env->ops->gen_ld_abs) { |
| 7831 | verbose(env, "bpf verifier is misconfigured\n"); |
| 7832 | return -EINVAL; |
| 7833 | } |
| 7834 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7835 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 7836 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7837 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7838 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7839 | return -EINVAL; |
| 7840 | } |
| 7841 | |
| 7842 | /* check whether implicit source operand (register R6) is readable */ |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 7843 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7844 | if (err) |
| 7845 | return err; |
| 7846 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7847 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 7848 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 7849 | * reference leak. |
| 7850 | */ |
| 7851 | err = check_reference_leak(env); |
| 7852 | if (err) { |
| 7853 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 7854 | return err; |
| 7855 | } |
| 7856 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 7857 | if (env->cur_state->active_spin_lock) { |
| 7858 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
| 7859 | return -EINVAL; |
| 7860 | } |
| 7861 | |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 7862 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7863 | verbose(env, |
| 7864 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7865 | return -EINVAL; |
| 7866 | } |
| 7867 | |
| 7868 | if (mode == BPF_IND) { |
| 7869 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7870 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7871 | if (err) |
| 7872 | return err; |
| 7873 | } |
| 7874 | |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 7875 | err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg); |
| 7876 | if (err < 0) |
| 7877 | return err; |
| 7878 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7879 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7880 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7881 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7882 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 7883 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7884 | |
| 7885 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7886 | * the value fetched from the packet. |
| 7887 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7888 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7889 | mark_reg_unknown(env, regs, BPF_REG_0); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 7890 | /* ld_abs load up to 32-bit skb data. */ |
| 7891 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7892 | return 0; |
| 7893 | } |
| 7894 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7895 | static int check_return_code(struct bpf_verifier_env *env) |
| 7896 | { |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 7897 | struct tnum enforce_attach_type_range = tnum_unknown; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 7898 | const struct bpf_prog *prog = env->prog; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7899 | struct bpf_reg_state *reg; |
| 7900 | struct tnum range = tnum_range(0, 1); |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 7901 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 7902 | int err; |
Dmitrii Banshchikov | f782e2c | 2020-11-13 17:17:56 +0000 | [diff] [blame] | 7903 | const bool is_subprog = env->cur_state->frame[0]->subprogno; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 7904 | |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 7905 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
Dmitrii Banshchikov | f782e2c | 2020-11-13 17:17:56 +0000 | [diff] [blame] | 7906 | if (!is_subprog && |
| 7907 | (prog_type == BPF_PROG_TYPE_STRUCT_OPS || |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 7908 | prog_type == BPF_PROG_TYPE_LSM) && |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 7909 | !prog->aux->attach_func_proto->type) |
| 7910 | return 0; |
| 7911 | |
| 7912 | /* eBPF calling convetion is such that R0 is used |
| 7913 | * to return the value from eBPF program. |
| 7914 | * Make sure that it's readable at this time |
| 7915 | * of bpf_exit, which means that program wrote |
| 7916 | * something into it earlier |
| 7917 | */ |
| 7918 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
| 7919 | if (err) |
| 7920 | return err; |
| 7921 | |
| 7922 | if (is_pointer_value(env, BPF_REG_0)) { |
| 7923 | verbose(env, "R0 leaks addr as return value\n"); |
| 7924 | return -EACCES; |
| 7925 | } |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7926 | |
Dmitrii Banshchikov | f782e2c | 2020-11-13 17:17:56 +0000 | [diff] [blame] | 7927 | reg = cur_regs(env) + BPF_REG_0; |
| 7928 | if (is_subprog) { |
| 7929 | if (reg->type != SCALAR_VALUE) { |
| 7930 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", |
| 7931 | reg_type_str[reg->type]); |
| 7932 | return -EINVAL; |
| 7933 | } |
| 7934 | return 0; |
| 7935 | } |
| 7936 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 7937 | switch (prog_type) { |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 7938 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 7939 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || |
Daniel Borkmann | 1b66d25 | 2020-05-19 00:45:45 +0200 | [diff] [blame] | 7940 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
| 7941 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || |
| 7942 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || |
| 7943 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || |
| 7944 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 7945 | range = tnum_range(1, 1); |
Gustavo A. R. Silva | ed4ed40 | 2019-07-11 11:22:33 -0500 | [diff] [blame] | 7946 | break; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7947 | case BPF_PROG_TYPE_CGROUP_SKB: |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 7948 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
| 7949 | range = tnum_range(0, 3); |
| 7950 | enforce_attach_type_range = tnum_range(2, 3); |
| 7951 | } |
Gustavo A. R. Silva | ed4ed40 | 2019-07-11 11:22:33 -0500 | [diff] [blame] | 7952 | break; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7953 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 7954 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 7955 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 7956 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 7957 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7958 | break; |
Alexei Starovoitov | 15ab09b | 2019-10-28 20:24:26 -0700 | [diff] [blame] | 7959 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 7960 | if (!env->prog->aux->attach_btf_id) |
| 7961 | return 0; |
| 7962 | range = tnum_const(0); |
| 7963 | break; |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 7964 | case BPF_PROG_TYPE_TRACING: |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 7965 | switch (env->prog->expected_attach_type) { |
| 7966 | case BPF_TRACE_FENTRY: |
| 7967 | case BPF_TRACE_FEXIT: |
| 7968 | range = tnum_const(0); |
| 7969 | break; |
| 7970 | case BPF_TRACE_RAW_TP: |
| 7971 | case BPF_MODIFY_RETURN: |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 7972 | return 0; |
Daniel Borkmann | 2ec0616 | 2020-05-16 00:39:18 +0200 | [diff] [blame] | 7973 | case BPF_TRACE_ITER: |
| 7974 | break; |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 7975 | default: |
| 7976 | return -ENOTSUPP; |
| 7977 | } |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 7978 | break; |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 7979 | case BPF_PROG_TYPE_SK_LOOKUP: |
| 7980 | range = tnum_range(SK_DROP, SK_PASS); |
| 7981 | break; |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 7982 | case BPF_PROG_TYPE_EXT: |
| 7983 | /* freplace program can return anything as its return value |
| 7984 | * depends on the to-be-replaced kernel func or bpf program. |
| 7985 | */ |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7986 | default: |
| 7987 | return 0; |
| 7988 | } |
| 7989 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7990 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7991 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7992 | reg_type_str[reg->type]); |
| 7993 | return -EINVAL; |
| 7994 | } |
| 7995 | |
| 7996 | if (!tnum_in(range, reg->var_off)) { |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 7997 | char tn_buf[48]; |
| 7998 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7999 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 8000 | if (!tnum_is_unknown(reg->var_off)) { |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 8001 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8002 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 8003 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8004 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 8005 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 8006 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 8007 | verbose(env, " should have been in %s\n", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 8008 | return -EINVAL; |
| 8009 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 8010 | |
| 8011 | if (!tnum_is_unknown(enforce_attach_type_range) && |
| 8012 | tnum_in(enforce_attach_type_range, reg->var_off)) |
| 8013 | env->prog->enforce_expected_attach_type = 1; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 8014 | return 0; |
| 8015 | } |
| 8016 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8017 | /* non-recursive DFS pseudo code |
| 8018 | * 1 procedure DFS-iterative(G,v): |
| 8019 | * 2 label v as discovered |
| 8020 | * 3 let S be a stack |
| 8021 | * 4 S.push(v) |
| 8022 | * 5 while S is not empty |
| 8023 | * 6 t <- S.pop() |
| 8024 | * 7 if t is what we're looking for: |
| 8025 | * 8 return t |
| 8026 | * 9 for all edges e in G.adjacentEdges(t) do |
| 8027 | * 10 if edge e is already labelled |
| 8028 | * 11 continue with the next edge |
| 8029 | * 12 w <- G.adjacentVertex(t,e) |
| 8030 | * 13 if vertex w is not discovered and not explored |
| 8031 | * 14 label e as tree-edge |
| 8032 | * 15 label w as discovered |
| 8033 | * 16 S.push(w) |
| 8034 | * 17 continue at 5 |
| 8035 | * 18 else if vertex w is discovered |
| 8036 | * 19 label e as back-edge |
| 8037 | * 20 else |
| 8038 | * 21 // vertex w is explored |
| 8039 | * 22 label e as forward- or cross-edge |
| 8040 | * 23 label t as explored |
| 8041 | * 24 S.pop() |
| 8042 | * |
| 8043 | * convention: |
| 8044 | * 0x10 - discovered |
| 8045 | * 0x11 - discovered and fall-through edge labelled |
| 8046 | * 0x12 - discovered and fall-through and branch edges labelled |
| 8047 | * 0x20 - explored |
| 8048 | */ |
| 8049 | |
| 8050 | enum { |
| 8051 | DISCOVERED = 0x10, |
| 8052 | EXPLORED = 0x20, |
| 8053 | FALLTHROUGH = 1, |
| 8054 | BRANCH = 2, |
| 8055 | }; |
| 8056 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 8057 | static u32 state_htab_size(struct bpf_verifier_env *env) |
| 8058 | { |
| 8059 | return env->prog->len; |
| 8060 | } |
| 8061 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 8062 | static struct bpf_verifier_state_list **explored_state( |
| 8063 | struct bpf_verifier_env *env, |
| 8064 | int idx) |
| 8065 | { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 8066 | struct bpf_verifier_state *cur = env->cur_state; |
| 8067 | struct bpf_func_state *state = cur->frame[cur->curframe]; |
| 8068 | |
| 8069 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 8070 | } |
| 8071 | |
| 8072 | static void init_explored_state(struct bpf_verifier_env *env, int idx) |
| 8073 | { |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 8074 | env->insn_aux_data[idx].prune_point = true; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 8075 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8076 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 8077 | enum { |
| 8078 | DONE_EXPLORING = 0, |
| 8079 | KEEP_EXPLORING = 1, |
| 8080 | }; |
| 8081 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8082 | /* t, w, e - match pseudo-code above: |
| 8083 | * t - index of current instruction |
| 8084 | * w - next instruction |
| 8085 | * e - edge |
| 8086 | */ |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8087 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
| 8088 | bool loop_ok) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8089 | { |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8090 | int *insn_stack = env->cfg.insn_stack; |
| 8091 | int *insn_state = env->cfg.insn_state; |
| 8092 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8093 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 8094 | return DONE_EXPLORING; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8095 | |
| 8096 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 8097 | return DONE_EXPLORING; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8098 | |
| 8099 | if (w < 0 || w >= env->prog->len) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 8100 | verbose_linfo(env, t, "%d: ", t); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8101 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8102 | return -EINVAL; |
| 8103 | } |
| 8104 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8105 | if (e == BRANCH) |
| 8106 | /* mark branch target for state pruning */ |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 8107 | init_explored_state(env, w); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8108 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8109 | if (insn_state[w] == 0) { |
| 8110 | /* tree-edge */ |
| 8111 | insn_state[t] = DISCOVERED | e; |
| 8112 | insn_state[w] = DISCOVERED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8113 | if (env->cfg.cur_stack >= env->prog->len) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8114 | return -E2BIG; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8115 | insn_stack[env->cfg.cur_stack++] = w; |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 8116 | return KEEP_EXPLORING; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8117 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 8118 | if (loop_ok && env->bpf_capable) |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 8119 | return DONE_EXPLORING; |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 8120 | verbose_linfo(env, t, "%d: ", t); |
| 8121 | verbose_linfo(env, w, "%d: ", w); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8122 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8123 | return -EINVAL; |
| 8124 | } else if (insn_state[w] == EXPLORED) { |
| 8125 | /* forward- or cross-edge */ |
| 8126 | insn_state[t] = DISCOVERED | e; |
| 8127 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8128 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8129 | return -EFAULT; |
| 8130 | } |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 8131 | return DONE_EXPLORING; |
| 8132 | } |
| 8133 | |
| 8134 | /* Visits the instruction at index t and returns one of the following: |
| 8135 | * < 0 - an error occurred |
| 8136 | * DONE_EXPLORING - the instruction was fully explored |
| 8137 | * KEEP_EXPLORING - there is still work to be done before it is fully explored |
| 8138 | */ |
| 8139 | static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env) |
| 8140 | { |
| 8141 | struct bpf_insn *insns = env->prog->insnsi; |
| 8142 | int ret; |
| 8143 | |
| 8144 | /* All non-branch instructions have a single fall-through edge. */ |
| 8145 | if (BPF_CLASS(insns[t].code) != BPF_JMP && |
| 8146 | BPF_CLASS(insns[t].code) != BPF_JMP32) |
| 8147 | return push_insn(t, t + 1, FALLTHROUGH, env, false); |
| 8148 | |
| 8149 | switch (BPF_OP(insns[t].code)) { |
| 8150 | case BPF_EXIT: |
| 8151 | return DONE_EXPLORING; |
| 8152 | |
| 8153 | case BPF_CALL: |
| 8154 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
| 8155 | if (ret) |
| 8156 | return ret; |
| 8157 | |
| 8158 | if (t + 1 < insn_cnt) |
| 8159 | init_explored_state(env, t + 1); |
| 8160 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
| 8161 | init_explored_state(env, t); |
| 8162 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, |
| 8163 | env, false); |
| 8164 | } |
| 8165 | return ret; |
| 8166 | |
| 8167 | case BPF_JA: |
| 8168 | if (BPF_SRC(insns[t].code) != BPF_K) |
| 8169 | return -EINVAL; |
| 8170 | |
| 8171 | /* unconditional jump with single edge */ |
| 8172 | ret = push_insn(t, t + insns[t].off + 1, FALLTHROUGH, env, |
| 8173 | true); |
| 8174 | if (ret) |
| 8175 | return ret; |
| 8176 | |
| 8177 | /* unconditional jmp is not a good pruning point, |
| 8178 | * but it's marked, since backtracking needs |
| 8179 | * to record jmp history in is_state_visited(). |
| 8180 | */ |
| 8181 | init_explored_state(env, t + insns[t].off + 1); |
| 8182 | /* tell verifier to check for equivalent states |
| 8183 | * after every call and jump |
| 8184 | */ |
| 8185 | if (t + 1 < insn_cnt) |
| 8186 | init_explored_state(env, t + 1); |
| 8187 | |
| 8188 | return ret; |
| 8189 | |
| 8190 | default: |
| 8191 | /* conditional jump with two edges */ |
| 8192 | init_explored_state(env, t); |
| 8193 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
| 8194 | if (ret) |
| 8195 | return ret; |
| 8196 | |
| 8197 | return push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
| 8198 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8199 | } |
| 8200 | |
| 8201 | /* non-recursive depth-first-search to detect loops in BPF program |
| 8202 | * loop == back-edge in directed graph |
| 8203 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8204 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8205 | { |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8206 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8207 | int *insn_stack, *insn_state; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8208 | int ret = 0; |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 8209 | int i; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8210 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8211 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8212 | if (!insn_state) |
| 8213 | return -ENOMEM; |
| 8214 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8215 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8216 | if (!insn_stack) { |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 8217 | kvfree(insn_state); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8218 | return -ENOMEM; |
| 8219 | } |
| 8220 | |
| 8221 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 8222 | insn_stack[0] = 0; /* 0 is the first instruction */ |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8223 | env->cfg.cur_stack = 1; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8224 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 8225 | while (env->cfg.cur_stack > 0) { |
| 8226 | int t = insn_stack[env->cfg.cur_stack - 1]; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8227 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 8228 | ret = visit_insn(t, insn_cnt, env); |
| 8229 | switch (ret) { |
| 8230 | case DONE_EXPLORING: |
| 8231 | insn_state[t] = EXPLORED; |
| 8232 | env->cfg.cur_stack--; |
| 8233 | break; |
| 8234 | case KEEP_EXPLORING: |
| 8235 | break; |
| 8236 | default: |
| 8237 | if (ret > 0) { |
| 8238 | verbose(env, "visit_insn internal bug\n"); |
| 8239 | ret = -EFAULT; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 8240 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8241 | goto err_free; |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 8242 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8243 | } |
| 8244 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 8245 | if (env->cfg.cur_stack < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8246 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8247 | ret = -EFAULT; |
| 8248 | goto err_free; |
| 8249 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8250 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8251 | for (i = 0; i < insn_cnt; i++) { |
| 8252 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8253 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8254 | ret = -EINVAL; |
| 8255 | goto err_free; |
| 8256 | } |
| 8257 | } |
| 8258 | ret = 0; /* cfg looks good */ |
| 8259 | |
| 8260 | err_free: |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 8261 | kvfree(insn_state); |
| 8262 | kvfree(insn_stack); |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8263 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8264 | return ret; |
| 8265 | } |
| 8266 | |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8267 | static int check_abnormal_return(struct bpf_verifier_env *env) |
| 8268 | { |
| 8269 | int i; |
| 8270 | |
| 8271 | for (i = 1; i < env->subprog_cnt; i++) { |
| 8272 | if (env->subprog_info[i].has_ld_abs) { |
| 8273 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); |
| 8274 | return -EINVAL; |
| 8275 | } |
| 8276 | if (env->subprog_info[i].has_tail_call) { |
| 8277 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); |
| 8278 | return -EINVAL; |
| 8279 | } |
| 8280 | } |
| 8281 | return 0; |
| 8282 | } |
| 8283 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8284 | /* The minimum supported BTF func info size */ |
| 8285 | #define MIN_BPF_FUNCINFO_SIZE 8 |
| 8286 | #define MAX_FUNCINFO_REC_SIZE 252 |
| 8287 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8288 | static int check_btf_func(struct bpf_verifier_env *env, |
| 8289 | const union bpf_attr *attr, |
| 8290 | union bpf_attr __user *uattr) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8291 | { |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8292 | const struct btf_type *type, *func_proto, *ret_type; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 8293 | u32 i, nfuncs, urec_size, min_size; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8294 | u32 krec_size = sizeof(struct bpf_func_info); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8295 | struct bpf_func_info *krecord; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8296 | struct bpf_func_info_aux *info_aux = NULL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8297 | struct bpf_prog *prog; |
| 8298 | const struct btf *btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8299 | void __user *urecord; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 8300 | u32 prev_offset = 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8301 | bool scalar_return; |
Dan Carpenter | e7ed83d | 2020-06-04 11:54:36 +0300 | [diff] [blame] | 8302 | int ret = -ENOMEM; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8303 | |
| 8304 | nfuncs = attr->func_info_cnt; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8305 | if (!nfuncs) { |
| 8306 | if (check_abnormal_return(env)) |
| 8307 | return -EINVAL; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8308 | return 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8309 | } |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8310 | |
| 8311 | if (nfuncs != env->subprog_cnt) { |
| 8312 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); |
| 8313 | return -EINVAL; |
| 8314 | } |
| 8315 | |
| 8316 | urec_size = attr->func_info_rec_size; |
| 8317 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || |
| 8318 | urec_size > MAX_FUNCINFO_REC_SIZE || |
| 8319 | urec_size % sizeof(u32)) { |
| 8320 | verbose(env, "invalid func info rec size %u\n", urec_size); |
| 8321 | return -EINVAL; |
| 8322 | } |
| 8323 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8324 | prog = env->prog; |
| 8325 | btf = prog->aux->btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8326 | |
| 8327 | urecord = u64_to_user_ptr(attr->func_info); |
| 8328 | min_size = min_t(u32, krec_size, urec_size); |
| 8329 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8330 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8331 | if (!krecord) |
| 8332 | return -ENOMEM; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8333 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
| 8334 | if (!info_aux) |
| 8335 | goto err_free; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8336 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8337 | for (i = 0; i < nfuncs; i++) { |
| 8338 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); |
| 8339 | if (ret) { |
| 8340 | if (ret == -E2BIG) { |
| 8341 | verbose(env, "nonzero tailing record in func info"); |
| 8342 | /* set the size kernel expects so loader can zero |
| 8343 | * out the rest of the record. |
| 8344 | */ |
| 8345 | if (put_user(min_size, &uattr->func_info_rec_size)) |
| 8346 | ret = -EFAULT; |
| 8347 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8348 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8349 | } |
| 8350 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8351 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8352 | ret = -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8353 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8354 | } |
| 8355 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8356 | /* check insn_off */ |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8357 | ret = -EINVAL; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8358 | if (i == 0) { |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8359 | if (krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8360 | verbose(env, |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8361 | "nonzero insn_off %u for the first func info record", |
| 8362 | krecord[i].insn_off); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8363 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8364 | } |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8365 | } else if (krecord[i].insn_off <= prev_offset) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8366 | verbose(env, |
| 8367 | "same or smaller insn offset (%u) than previous func info record (%u)", |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8368 | krecord[i].insn_off, prev_offset); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8369 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8370 | } |
| 8371 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8372 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8373 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8374 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8375 | } |
| 8376 | |
| 8377 | /* check type_id */ |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8378 | type = btf_type_by_id(btf, krecord[i].type_id); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 8379 | if (!type || !btf_type_is_func(type)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8380 | verbose(env, "invalid type id %d in func info", |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8381 | krecord[i].type_id); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8382 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8383 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 8384 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8385 | |
| 8386 | func_proto = btf_type_by_id(btf, type->type); |
| 8387 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) |
| 8388 | /* btf_func_check() already verified it during BTF load */ |
| 8389 | goto err_free; |
| 8390 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); |
| 8391 | scalar_return = |
| 8392 | btf_type_is_small_int(ret_type) || btf_type_is_enum(ret_type); |
| 8393 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { |
| 8394 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); |
| 8395 | goto err_free; |
| 8396 | } |
| 8397 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { |
| 8398 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); |
| 8399 | goto err_free; |
| 8400 | } |
| 8401 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8402 | prev_offset = krecord[i].insn_off; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8403 | urecord += urec_size; |
| 8404 | } |
| 8405 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8406 | prog->aux->func_info = krecord; |
| 8407 | prog->aux->func_info_cnt = nfuncs; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8408 | prog->aux->func_info_aux = info_aux; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8409 | return 0; |
| 8410 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8411 | err_free: |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8412 | kvfree(krecord); |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8413 | kfree(info_aux); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8414 | return ret; |
| 8415 | } |
| 8416 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8417 | static void adjust_btf_func(struct bpf_verifier_env *env) |
| 8418 | { |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8419 | struct bpf_prog_aux *aux = env->prog->aux; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8420 | int i; |
| 8421 | |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8422 | if (!aux->func_info) |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8423 | return; |
| 8424 | |
| 8425 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8426 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8427 | } |
| 8428 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8429 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
| 8430 | sizeof(((struct bpf_line_info *)(0))->line_col)) |
| 8431 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
| 8432 | |
| 8433 | static int check_btf_line(struct bpf_verifier_env *env, |
| 8434 | const union bpf_attr *attr, |
| 8435 | union bpf_attr __user *uattr) |
| 8436 | { |
| 8437 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; |
| 8438 | struct bpf_subprog_info *sub; |
| 8439 | struct bpf_line_info *linfo; |
| 8440 | struct bpf_prog *prog; |
| 8441 | const struct btf *btf; |
| 8442 | void __user *ulinfo; |
| 8443 | int err; |
| 8444 | |
| 8445 | nr_linfo = attr->line_info_cnt; |
| 8446 | if (!nr_linfo) |
| 8447 | return 0; |
| 8448 | |
| 8449 | rec_size = attr->line_info_rec_size; |
| 8450 | if (rec_size < MIN_BPF_LINEINFO_SIZE || |
| 8451 | rec_size > MAX_LINEINFO_REC_SIZE || |
| 8452 | rec_size & (sizeof(u32) - 1)) |
| 8453 | return -EINVAL; |
| 8454 | |
| 8455 | /* Need to zero it in case the userspace may |
| 8456 | * pass in a smaller bpf_line_info object. |
| 8457 | */ |
| 8458 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), |
| 8459 | GFP_KERNEL | __GFP_NOWARN); |
| 8460 | if (!linfo) |
| 8461 | return -ENOMEM; |
| 8462 | |
| 8463 | prog = env->prog; |
| 8464 | btf = prog->aux->btf; |
| 8465 | |
| 8466 | s = 0; |
| 8467 | sub = env->subprog_info; |
| 8468 | ulinfo = u64_to_user_ptr(attr->line_info); |
| 8469 | expected_size = sizeof(struct bpf_line_info); |
| 8470 | ncopy = min_t(u32, expected_size, rec_size); |
| 8471 | for (i = 0; i < nr_linfo; i++) { |
| 8472 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); |
| 8473 | if (err) { |
| 8474 | if (err == -E2BIG) { |
| 8475 | verbose(env, "nonzero tailing record in line_info"); |
| 8476 | if (put_user(expected_size, |
| 8477 | &uattr->line_info_rec_size)) |
| 8478 | err = -EFAULT; |
| 8479 | } |
| 8480 | goto err_free; |
| 8481 | } |
| 8482 | |
| 8483 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { |
| 8484 | err = -EFAULT; |
| 8485 | goto err_free; |
| 8486 | } |
| 8487 | |
| 8488 | /* |
| 8489 | * Check insn_off to ensure |
| 8490 | * 1) strictly increasing AND |
| 8491 | * 2) bounded by prog->len |
| 8492 | * |
| 8493 | * The linfo[0].insn_off == 0 check logically falls into |
| 8494 | * the later "missing bpf_line_info for func..." case |
| 8495 | * because the first linfo[0].insn_off must be the |
| 8496 | * first sub also and the first sub must have |
| 8497 | * subprog_info[0].start == 0. |
| 8498 | */ |
| 8499 | if ((i && linfo[i].insn_off <= prev_offset) || |
| 8500 | linfo[i].insn_off >= prog->len) { |
| 8501 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", |
| 8502 | i, linfo[i].insn_off, prev_offset, |
| 8503 | prog->len); |
| 8504 | err = -EINVAL; |
| 8505 | goto err_free; |
| 8506 | } |
| 8507 | |
Martin KaFai Lau | fdbaa0b | 2018-12-19 13:01:01 -0800 | [diff] [blame] | 8508 | if (!prog->insnsi[linfo[i].insn_off].code) { |
| 8509 | verbose(env, |
| 8510 | "Invalid insn code at line_info[%u].insn_off\n", |
| 8511 | i); |
| 8512 | err = -EINVAL; |
| 8513 | goto err_free; |
| 8514 | } |
| 8515 | |
Martin KaFai Lau | 23127b3 | 2018-12-13 10:41:46 -0800 | [diff] [blame] | 8516 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
| 8517 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8518 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
| 8519 | err = -EINVAL; |
| 8520 | goto err_free; |
| 8521 | } |
| 8522 | |
| 8523 | if (s != env->subprog_cnt) { |
| 8524 | if (linfo[i].insn_off == sub[s].start) { |
| 8525 | sub[s].linfo_idx = i; |
| 8526 | s++; |
| 8527 | } else if (sub[s].start < linfo[i].insn_off) { |
| 8528 | verbose(env, "missing bpf_line_info for func#%u\n", s); |
| 8529 | err = -EINVAL; |
| 8530 | goto err_free; |
| 8531 | } |
| 8532 | } |
| 8533 | |
| 8534 | prev_offset = linfo[i].insn_off; |
| 8535 | ulinfo += rec_size; |
| 8536 | } |
| 8537 | |
| 8538 | if (s != env->subprog_cnt) { |
| 8539 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", |
| 8540 | env->subprog_cnt - s, s); |
| 8541 | err = -EINVAL; |
| 8542 | goto err_free; |
| 8543 | } |
| 8544 | |
| 8545 | prog->aux->linfo = linfo; |
| 8546 | prog->aux->nr_linfo = nr_linfo; |
| 8547 | |
| 8548 | return 0; |
| 8549 | |
| 8550 | err_free: |
| 8551 | kvfree(linfo); |
| 8552 | return err; |
| 8553 | } |
| 8554 | |
| 8555 | static int check_btf_info(struct bpf_verifier_env *env, |
| 8556 | const union bpf_attr *attr, |
| 8557 | union bpf_attr __user *uattr) |
| 8558 | { |
| 8559 | struct btf *btf; |
| 8560 | int err; |
| 8561 | |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8562 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
| 8563 | if (check_abnormal_return(env)) |
| 8564 | return -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8565 | return 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8566 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8567 | |
| 8568 | btf = btf_get_by_fd(attr->prog_btf_fd); |
| 8569 | if (IS_ERR(btf)) |
| 8570 | return PTR_ERR(btf); |
| 8571 | env->prog->aux->btf = btf; |
| 8572 | |
| 8573 | err = check_btf_func(env, attr, uattr); |
| 8574 | if (err) |
| 8575 | return err; |
| 8576 | |
| 8577 | err = check_btf_line(env, attr, uattr); |
| 8578 | if (err) |
| 8579 | return err; |
| 8580 | |
| 8581 | return 0; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8582 | } |
| 8583 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8584 | /* check %cur's range satisfies %old's */ |
| 8585 | static bool range_within(struct bpf_reg_state *old, |
| 8586 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8587 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8588 | return old->umin_value <= cur->umin_value && |
| 8589 | old->umax_value >= cur->umax_value && |
| 8590 | old->smin_value <= cur->smin_value && |
| 8591 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8592 | } |
| 8593 | |
| 8594 | /* Maximum number of register states that can exist at once */ |
| 8595 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 8596 | struct idpair { |
| 8597 | u32 old; |
| 8598 | u32 cur; |
| 8599 | }; |
| 8600 | |
| 8601 | /* If in the old state two registers had the same id, then they need to have |
| 8602 | * the same id in the new state as well. But that id could be different from |
| 8603 | * the old state, so we need to track the mapping from old to new ids. |
| 8604 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 8605 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 8606 | * regs with a different old id could still have new id 9, we don't care about |
| 8607 | * that. |
| 8608 | * So we look through our idmap to see if this old id has been seen before. If |
| 8609 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 8610 | */ |
| 8611 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 8612 | { |
| 8613 | unsigned int i; |
| 8614 | |
| 8615 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 8616 | if (!idmap[i].old) { |
| 8617 | /* Reached an empty slot; haven't seen this id before */ |
| 8618 | idmap[i].old = old_id; |
| 8619 | idmap[i].cur = cur_id; |
| 8620 | return true; |
| 8621 | } |
| 8622 | if (idmap[i].old == old_id) |
| 8623 | return idmap[i].cur == cur_id; |
| 8624 | } |
| 8625 | /* We ran out of idmap slots, which should be impossible */ |
| 8626 | WARN_ON_ONCE(1); |
| 8627 | return false; |
| 8628 | } |
| 8629 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 8630 | static void clean_func_state(struct bpf_verifier_env *env, |
| 8631 | struct bpf_func_state *st) |
| 8632 | { |
| 8633 | enum bpf_reg_liveness live; |
| 8634 | int i, j; |
| 8635 | |
| 8636 | for (i = 0; i < BPF_REG_FP; i++) { |
| 8637 | live = st->regs[i].live; |
| 8638 | /* liveness must not touch this register anymore */ |
| 8639 | st->regs[i].live |= REG_LIVE_DONE; |
| 8640 | if (!(live & REG_LIVE_READ)) |
| 8641 | /* since the register is unused, clear its state |
| 8642 | * to make further comparison simpler |
| 8643 | */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 8644 | __mark_reg_not_init(env, &st->regs[i]); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 8645 | } |
| 8646 | |
| 8647 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { |
| 8648 | live = st->stack[i].spilled_ptr.live; |
| 8649 | /* liveness must not touch this stack slot anymore */ |
| 8650 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; |
| 8651 | if (!(live & REG_LIVE_READ)) { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 8652 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 8653 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 8654 | st->stack[i].slot_type[j] = STACK_INVALID; |
| 8655 | } |
| 8656 | } |
| 8657 | } |
| 8658 | |
| 8659 | static void clean_verifier_state(struct bpf_verifier_env *env, |
| 8660 | struct bpf_verifier_state *st) |
| 8661 | { |
| 8662 | int i; |
| 8663 | |
| 8664 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) |
| 8665 | /* all regs in this state in all frames were already marked */ |
| 8666 | return; |
| 8667 | |
| 8668 | for (i = 0; i <= st->curframe; i++) |
| 8669 | clean_func_state(env, st->frame[i]); |
| 8670 | } |
| 8671 | |
| 8672 | /* the parentage chains form a tree. |
| 8673 | * the verifier states are added to state lists at given insn and |
| 8674 | * pushed into state stack for future exploration. |
| 8675 | * when the verifier reaches bpf_exit insn some of the verifer states |
| 8676 | * stored in the state lists have their final liveness state already, |
| 8677 | * but a lot of states will get revised from liveness point of view when |
| 8678 | * the verifier explores other branches. |
| 8679 | * Example: |
| 8680 | * 1: r0 = 1 |
| 8681 | * 2: if r1 == 100 goto pc+1 |
| 8682 | * 3: r0 = 2 |
| 8683 | * 4: exit |
| 8684 | * when the verifier reaches exit insn the register r0 in the state list of |
| 8685 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch |
| 8686 | * of insn 2 and goes exploring further. At the insn 4 it will walk the |
| 8687 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. |
| 8688 | * |
| 8689 | * Since the verifier pushes the branch states as it sees them while exploring |
| 8690 | * the program the condition of walking the branch instruction for the second |
| 8691 | * time means that all states below this branch were already explored and |
| 8692 | * their final liveness markes are already propagated. |
| 8693 | * Hence when the verifier completes the search of state list in is_state_visited() |
| 8694 | * we can call this clean_live_states() function to mark all liveness states |
| 8695 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' |
| 8696 | * will not be used. |
| 8697 | * This function also clears the registers and stack for states that !READ |
| 8698 | * to simplify state merging. |
| 8699 | * |
| 8700 | * Important note here that walking the same branch instruction in the callee |
| 8701 | * doesn't meant that the states are DONE. The verifier has to compare |
| 8702 | * the callsites |
| 8703 | */ |
| 8704 | static void clean_live_states(struct bpf_verifier_env *env, int insn, |
| 8705 | struct bpf_verifier_state *cur) |
| 8706 | { |
| 8707 | struct bpf_verifier_state_list *sl; |
| 8708 | int i; |
| 8709 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 8710 | sl = *explored_state(env, insn); |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 8711 | while (sl) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8712 | if (sl->state.branches) |
| 8713 | goto next; |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 8714 | if (sl->state.insn_idx != insn || |
| 8715 | sl->state.curframe != cur->curframe) |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 8716 | goto next; |
| 8717 | for (i = 0; i <= cur->curframe; i++) |
| 8718 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) |
| 8719 | goto next; |
| 8720 | clean_verifier_state(env, &sl->state); |
| 8721 | next: |
| 8722 | sl = sl->next; |
| 8723 | } |
| 8724 | } |
| 8725 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8726 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 8727 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 8728 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8729 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8730 | bool equal; |
| 8731 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8732 | if (!(rold->live & REG_LIVE_READ)) |
| 8733 | /* explored state didn't use this */ |
| 8734 | return true; |
| 8735 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 8736 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8737 | |
| 8738 | if (rold->type == PTR_TO_STACK) |
| 8739 | /* two stack pointers are equal only if they're pointing to |
| 8740 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 8741 | */ |
| 8742 | return equal && rold->frameno == rcur->frameno; |
| 8743 | |
| 8744 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8745 | return true; |
| 8746 | |
| 8747 | if (rold->type == NOT_INIT) |
| 8748 | /* explored state can't have used this */ |
| 8749 | return true; |
| 8750 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8751 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8752 | switch (rold->type) { |
| 8753 | case SCALAR_VALUE: |
| 8754 | if (rcur->type == SCALAR_VALUE) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8755 | if (!rold->precise && !rcur->precise) |
| 8756 | return true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8757 | /* new val must satisfy old val knowledge */ |
| 8758 | return range_within(rold, rcur) && |
| 8759 | tnum_in(rold->var_off, rcur->var_off); |
| 8760 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 8761 | /* We're trying to use a pointer in place of a scalar. |
| 8762 | * Even if the scalar was unbounded, this could lead to |
| 8763 | * pointer leaks because scalars are allowed to leak |
| 8764 | * while pointers are not. We could make this safe in |
| 8765 | * special cases if root is calling us, but it's |
| 8766 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8767 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 8768 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8769 | } |
| 8770 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 8771 | /* If the new min/max/var_off satisfy the old ones and |
| 8772 | * everything else matches, we are OK. |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 8773 | * 'id' is not compared, since it's only used for maps with |
| 8774 | * bpf_spin_lock inside map element and in such cases if |
| 8775 | * the rest of the prog is valid for one map element then |
| 8776 | * it's valid for all map elements regardless of the key |
| 8777 | * used in bpf_map_lookup() |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 8778 | */ |
| 8779 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 8780 | range_within(rold, rcur) && |
| 8781 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8782 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 8783 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 8784 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 8785 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 8786 | * checked, doing so could have affected others with the same |
| 8787 | * id, and we can't check for that because we lost the id when |
| 8788 | * we converted to a PTR_TO_MAP_VALUE. |
| 8789 | */ |
| 8790 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 8791 | return false; |
| 8792 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 8793 | return false; |
| 8794 | /* Check our ids match any regs they're supposed to */ |
| 8795 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 8796 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8797 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 8798 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8799 | return false; |
| 8800 | /* We must have at least as much range as the old ptr |
| 8801 | * did, so that any accesses which were safe before are |
| 8802 | * still safe. This is true even if old range < old off, |
| 8803 | * since someone could have accessed through (ptr - k), or |
| 8804 | * even done ptr -= k in a register, to get a safe access. |
| 8805 | */ |
| 8806 | if (rold->range > rcur->range) |
| 8807 | return false; |
| 8808 | /* If the offsets don't match, we can't trust our alignment; |
| 8809 | * nor can we be sure that we won't fall out of range. |
| 8810 | */ |
| 8811 | if (rold->off != rcur->off) |
| 8812 | return false; |
| 8813 | /* id relations must be preserved */ |
| 8814 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 8815 | return false; |
| 8816 | /* new val must satisfy old val knowledge */ |
| 8817 | return range_within(rold, rcur) && |
| 8818 | tnum_in(rold->var_off, rcur->var_off); |
| 8819 | case PTR_TO_CTX: |
| 8820 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8821 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 8822 | case PTR_TO_FLOW_KEYS: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 8823 | case PTR_TO_SOCKET: |
| 8824 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 8825 | case PTR_TO_SOCK_COMMON: |
| 8826 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 8827 | case PTR_TO_TCP_SOCK: |
| 8828 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 8829 | case PTR_TO_XDP_SOCK: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8830 | /* Only valid matches are exact, which memcmp() above |
| 8831 | * would have accepted |
| 8832 | */ |
| 8833 | default: |
| 8834 | /* Don't know what's going on, just say it's not safe */ |
| 8835 | return false; |
| 8836 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8837 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8838 | /* Shouldn't get here; if we do, say it's not safe */ |
| 8839 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8840 | return false; |
| 8841 | } |
| 8842 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8843 | static bool stacksafe(struct bpf_func_state *old, |
| 8844 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8845 | struct idpair *idmap) |
| 8846 | { |
| 8847 | int i, spi; |
| 8848 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8849 | /* walk slots of the explored stack and ignore any additional |
| 8850 | * slots in the current stack, since explored(safe) state |
| 8851 | * didn't use them |
| 8852 | */ |
| 8853 | for (i = 0; i < old->allocated_stack; i++) { |
| 8854 | spi = i / BPF_REG_SIZE; |
| 8855 | |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 8856 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
| 8857 | i += BPF_REG_SIZE - 1; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 8858 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 8859 | continue; |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 8860 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 8861 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8862 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 8863 | continue; |
Alexei Starovoitov | 19e2dbb | 2018-12-13 11:42:33 -0800 | [diff] [blame] | 8864 | |
| 8865 | /* explored stack has more populated slots than current stack |
| 8866 | * and these slots were used |
| 8867 | */ |
| 8868 | if (i >= cur->allocated_stack) |
| 8869 | return false; |
| 8870 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 8871 | /* if old state was safe with misc data in the stack |
| 8872 | * it will be safe with zero-initialized stack. |
| 8873 | * The opposite is not true |
| 8874 | */ |
| 8875 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 8876 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 8877 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8878 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 8879 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 8880 | /* Ex: old explored (safe) state has STACK_SPILL in |
Randy Dunlap | b8c1a30 | 2020-08-06 20:31:41 -0700 | [diff] [blame] | 8881 | * this stack slot, but current has STACK_MISC -> |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8882 | * this verifier states are not equivalent, |
| 8883 | * return false to continue verification of this path |
| 8884 | */ |
| 8885 | return false; |
| 8886 | if (i % BPF_REG_SIZE) |
| 8887 | continue; |
| 8888 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 8889 | continue; |
| 8890 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 8891 | &cur->stack[spi].spilled_ptr, |
| 8892 | idmap)) |
| 8893 | /* when explored and current stack slot are both storing |
| 8894 | * spilled registers, check that stored pointers types |
| 8895 | * are the same as well. |
| 8896 | * Ex: explored safe path could have stored |
| 8897 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 8898 | * but current path has stored: |
| 8899 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 8900 | * such verifier states are not equivalent. |
| 8901 | * return false to continue verification of this path |
| 8902 | */ |
| 8903 | return false; |
| 8904 | } |
| 8905 | return true; |
| 8906 | } |
| 8907 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 8908 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 8909 | { |
| 8910 | if (old->acquired_refs != cur->acquired_refs) |
| 8911 | return false; |
| 8912 | return !memcmp(old->refs, cur->refs, |
| 8913 | sizeof(*old->refs) * old->acquired_refs); |
| 8914 | } |
| 8915 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8916 | /* compare two verifier states |
| 8917 | * |
| 8918 | * all states stored in state_list are known to be valid, since |
| 8919 | * verifier reached 'bpf_exit' instruction through them |
| 8920 | * |
| 8921 | * this function is called when verifier exploring different branches of |
| 8922 | * execution popped from the state stack. If it sees an old state that has |
| 8923 | * more strict register state and more strict stack state then this execution |
| 8924 | * branch doesn't need to be explored further, since verifier already |
| 8925 | * concluded that more strict state leads to valid finish. |
| 8926 | * |
| 8927 | * Therefore two states are equivalent if register state is more conservative |
| 8928 | * and explored stack state is more conservative than the current one. |
| 8929 | * Example: |
| 8930 | * explored current |
| 8931 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 8932 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 8933 | * |
| 8934 | * In other words if current stack state (one being explored) has more |
| 8935 | * valid slots than old one that already passed validation, it means |
| 8936 | * the verifier can stop exploring and conclude that current state is valid too |
| 8937 | * |
| 8938 | * Similarly with registers. If explored state has register type as invalid |
| 8939 | * whereas register type in current state is meaningful, it means that |
| 8940 | * the current state will reach 'bpf_exit' instruction safely |
| 8941 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8942 | static bool func_states_equal(struct bpf_func_state *old, |
| 8943 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8944 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8945 | struct idpair *idmap; |
| 8946 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8947 | int i; |
| 8948 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8949 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 8950 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 8951 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 8952 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8953 | |
| 8954 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 8955 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8956 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8957 | } |
| 8958 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8959 | if (!stacksafe(old, cur, idmap)) |
| 8960 | goto out_free; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 8961 | |
| 8962 | if (!refsafe(old, cur)) |
| 8963 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8964 | ret = true; |
| 8965 | out_free: |
| 8966 | kfree(idmap); |
| 8967 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8968 | } |
| 8969 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8970 | static bool states_equal(struct bpf_verifier_env *env, |
| 8971 | struct bpf_verifier_state *old, |
| 8972 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8973 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8974 | int i; |
| 8975 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8976 | if (old->curframe != cur->curframe) |
| 8977 | return false; |
| 8978 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 8979 | /* Verification state from speculative execution simulation |
| 8980 | * must never prune a non-speculative execution one. |
| 8981 | */ |
| 8982 | if (old->speculative && !cur->speculative) |
| 8983 | return false; |
| 8984 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 8985 | if (old->active_spin_lock != cur->active_spin_lock) |
| 8986 | return false; |
| 8987 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8988 | /* for states to be equal callsites have to be the same |
| 8989 | * and all frame states need to be equivalent |
| 8990 | */ |
| 8991 | for (i = 0; i <= old->curframe; i++) { |
| 8992 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 8993 | return false; |
| 8994 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 8995 | return false; |
| 8996 | } |
| 8997 | return true; |
| 8998 | } |
| 8999 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 9000 | /* Return 0 if no propagation happened. Return negative error code if error |
| 9001 | * happened. Otherwise, return the propagated bit. |
| 9002 | */ |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 9003 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
| 9004 | struct bpf_reg_state *reg, |
| 9005 | struct bpf_reg_state *parent_reg) |
| 9006 | { |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 9007 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
| 9008 | u8 flag = reg->live & REG_LIVE_READ; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 9009 | int err; |
| 9010 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 9011 | /* When comes here, read flags of PARENT_REG or REG could be any of |
| 9012 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need |
| 9013 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. |
| 9014 | */ |
| 9015 | if (parent_flag == REG_LIVE_READ64 || |
| 9016 | /* Or if there is no read flag from REG. */ |
| 9017 | !flag || |
| 9018 | /* Or if the read flag from REG is the same as PARENT_REG. */ |
| 9019 | parent_flag == flag) |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 9020 | return 0; |
| 9021 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 9022 | err = mark_reg_read(env, reg, parent_reg, flag); |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 9023 | if (err) |
| 9024 | return err; |
| 9025 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 9026 | return flag; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 9027 | } |
| 9028 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9029 | /* A write screens off any subsequent reads; but write marks come from the |
| 9030 | * straight-line code between a state and its parent. When we arrive at an |
| 9031 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 9032 | * code, so read marks in the state must propagate to the parent regardless |
| 9033 | * of the state's write marks. That's what 'parent == state->parent' comparison |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9034 | * in mark_reg_read() is for. |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9035 | */ |
| 9036 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 9037 | const struct bpf_verifier_state *vstate, |
| 9038 | struct bpf_verifier_state *vparent) |
| 9039 | { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 9040 | struct bpf_reg_state *state_reg, *parent_reg; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9041 | struct bpf_func_state *state, *parent; |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 9042 | int i, frame, err = 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9043 | |
| 9044 | if (vparent->curframe != vstate->curframe) { |
| 9045 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 9046 | vparent->curframe, vstate->curframe); |
| 9047 | return -EFAULT; |
| 9048 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9049 | /* Propagate read liveness of registers... */ |
| 9050 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 9051 | for (frame = 0; frame <= vstate->curframe; frame++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 9052 | parent = vparent->frame[frame]; |
| 9053 | state = vstate->frame[frame]; |
| 9054 | parent_reg = parent->regs; |
| 9055 | state_reg = state->regs; |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 9056 | /* We don't need to worry about FP liveness, it's read-only */ |
| 9057 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 9058 | err = propagate_liveness_reg(env, &state_reg[i], |
| 9059 | &parent_reg[i]); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 9060 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 9061 | return err; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 9062 | if (err == REG_LIVE_READ64) |
| 9063 | mark_insn_zext(env, &parent_reg[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9064 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9065 | |
Jiong Wang | 1b04aee | 2019-04-12 22:59:34 +0100 | [diff] [blame] | 9066 | /* Propagate stack slots. */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9067 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 9068 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 9069 | parent_reg = &parent->stack[i].spilled_ptr; |
| 9070 | state_reg = &state->stack[i].spilled_ptr; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 9071 | err = propagate_liveness_reg(env, state_reg, |
| 9072 | parent_reg); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 9073 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 9074 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9075 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9076 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 9077 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9078 | } |
| 9079 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 9080 | /* find precise scalars in the previous equivalent state and |
| 9081 | * propagate them into the current state |
| 9082 | */ |
| 9083 | static int propagate_precision(struct bpf_verifier_env *env, |
| 9084 | const struct bpf_verifier_state *old) |
| 9085 | { |
| 9086 | struct bpf_reg_state *state_reg; |
| 9087 | struct bpf_func_state *state; |
| 9088 | int i, err = 0; |
| 9089 | |
| 9090 | state = old->frame[old->curframe]; |
| 9091 | state_reg = state->regs; |
| 9092 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { |
| 9093 | if (state_reg->type != SCALAR_VALUE || |
| 9094 | !state_reg->precise) |
| 9095 | continue; |
| 9096 | if (env->log.level & BPF_LOG_LEVEL2) |
| 9097 | verbose(env, "propagating r%d\n", i); |
| 9098 | err = mark_chain_precision(env, i); |
| 9099 | if (err < 0) |
| 9100 | return err; |
| 9101 | } |
| 9102 | |
| 9103 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 9104 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 9105 | continue; |
| 9106 | state_reg = &state->stack[i].spilled_ptr; |
| 9107 | if (state_reg->type != SCALAR_VALUE || |
| 9108 | !state_reg->precise) |
| 9109 | continue; |
| 9110 | if (env->log.level & BPF_LOG_LEVEL2) |
| 9111 | verbose(env, "propagating fp%d\n", |
| 9112 | (-i - 1) * BPF_REG_SIZE); |
| 9113 | err = mark_chain_precision_stack(env, i); |
| 9114 | if (err < 0) |
| 9115 | return err; |
| 9116 | } |
| 9117 | return 0; |
| 9118 | } |
| 9119 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9120 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
| 9121 | struct bpf_verifier_state *cur) |
| 9122 | { |
| 9123 | struct bpf_func_state *fold, *fcur; |
| 9124 | int i, fr = cur->curframe; |
| 9125 | |
| 9126 | if (old->curframe != fr) |
| 9127 | return false; |
| 9128 | |
| 9129 | fold = old->frame[fr]; |
| 9130 | fcur = cur->frame[fr]; |
| 9131 | for (i = 0; i < MAX_BPF_REG; i++) |
| 9132 | if (memcmp(&fold->regs[i], &fcur->regs[i], |
| 9133 | offsetof(struct bpf_reg_state, parent))) |
| 9134 | return false; |
| 9135 | return true; |
| 9136 | } |
| 9137 | |
| 9138 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9139 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9140 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9141 | struct bpf_verifier_state_list *new_sl; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 9142 | struct bpf_verifier_state_list *sl, **pprev; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9143 | struct bpf_verifier_state *cur = env->cur_state, *new; |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 9144 | int i, j, err, states_cnt = 0; |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 9145 | bool add_new_state = env->test_state_freq ? true : false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9146 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9147 | cur->last_insn_idx = env->prev_insn_idx; |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 9148 | if (!env->insn_aux_data[insn_idx].prune_point) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9149 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 9150 | * be doing state search here |
| 9151 | */ |
| 9152 | return 0; |
| 9153 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9154 | /* bpf progs typically have pruning point every 4 instructions |
| 9155 | * http://vger.kernel.org/bpfconf2019.html#session-1 |
| 9156 | * Do not add new state for future pruning if the verifier hasn't seen |
| 9157 | * at least 2 jumps and at least 8 instructions. |
| 9158 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. |
| 9159 | * In tests that amounts to up to 50% reduction into total verifier |
| 9160 | * memory consumption and 20% verifier time speedup. |
| 9161 | */ |
| 9162 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && |
| 9163 | env->insn_processed - env->prev_insn_processed >= 8) |
| 9164 | add_new_state = true; |
| 9165 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 9166 | pprev = explored_state(env, insn_idx); |
| 9167 | sl = *pprev; |
| 9168 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 9169 | clean_live_states(env, insn_idx, cur); |
| 9170 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 9171 | while (sl) { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 9172 | states_cnt++; |
| 9173 | if (sl->state.insn_idx != insn_idx) |
| 9174 | goto next; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9175 | if (sl->state.branches) { |
| 9176 | if (states_maybe_looping(&sl->state, cur) && |
| 9177 | states_equal(env, &sl->state, cur)) { |
| 9178 | verbose_linfo(env, insn_idx, "; "); |
| 9179 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); |
| 9180 | return -EINVAL; |
| 9181 | } |
| 9182 | /* if the verifier is processing a loop, avoid adding new state |
| 9183 | * too often, since different loop iterations have distinct |
| 9184 | * states and may not help future pruning. |
| 9185 | * This threshold shouldn't be too low to make sure that |
| 9186 | * a loop with large bound will be rejected quickly. |
| 9187 | * The most abusive loop will be: |
| 9188 | * r1 += 1 |
| 9189 | * if r1 < 1000000 goto pc-2 |
| 9190 | * 1M insn_procssed limit / 100 == 10k peak states. |
| 9191 | * This threshold shouldn't be too high either, since states |
| 9192 | * at the end of the loop are likely to be useful in pruning. |
| 9193 | */ |
| 9194 | if (env->jmps_processed - env->prev_jmps_processed < 20 && |
| 9195 | env->insn_processed - env->prev_insn_processed < 100) |
| 9196 | add_new_state = false; |
| 9197 | goto miss; |
| 9198 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9199 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 9200 | sl->hit_cnt++; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9201 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9202 | * prune the search. |
| 9203 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 9204 | * If we have any write marks in env->cur_state, they |
| 9205 | * will prevent corresponding reads in the continuation |
| 9206 | * from reaching our parent (an explored_state). Our |
| 9207 | * own state will get the read marks recorded, but |
| 9208 | * they'll be immediately forgotten as we're pruning |
| 9209 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9210 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9211 | err = propagate_liveness(env, &sl->state, cur); |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 9212 | |
| 9213 | /* if previous state reached the exit with precision and |
| 9214 | * current state is equivalent to it (except precsion marks) |
| 9215 | * the precision needs to be propagated back in |
| 9216 | * the current state. |
| 9217 | */ |
| 9218 | err = err ? : push_jmp_history(env, cur); |
| 9219 | err = err ? : propagate_precision(env, &sl->state); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9220 | if (err) |
| 9221 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9222 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9223 | } |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9224 | miss: |
| 9225 | /* when new state is not going to be added do not increase miss count. |
| 9226 | * Otherwise several loop iterations will remove the state |
| 9227 | * recorded earlier. The goal of these heuristics is to have |
| 9228 | * states from some iterations of the loop (some in the beginning |
| 9229 | * and some at the end) to help pruning. |
| 9230 | */ |
| 9231 | if (add_new_state) |
| 9232 | sl->miss_cnt++; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 9233 | /* heuristic to determine whether this state is beneficial |
| 9234 | * to keep checking from state equivalence point of view. |
| 9235 | * Higher numbers increase max_states_per_insn and verification time, |
| 9236 | * but do not meaningfully decrease insn_processed. |
| 9237 | */ |
| 9238 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { |
| 9239 | /* the state is unlikely to be useful. Remove it to |
| 9240 | * speed up verification |
| 9241 | */ |
| 9242 | *pprev = sl->next; |
| 9243 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9244 | u32 br = sl->state.branches; |
| 9245 | |
| 9246 | WARN_ONCE(br, |
| 9247 | "BUG live_done but branches_to_explore %d\n", |
| 9248 | br); |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 9249 | free_verifier_state(&sl->state, false); |
| 9250 | kfree(sl); |
| 9251 | env->peak_states--; |
| 9252 | } else { |
| 9253 | /* cannot free this state, since parentage chain may |
| 9254 | * walk it later. Add it for free_list instead to |
| 9255 | * be freed at the end of verification |
| 9256 | */ |
| 9257 | sl->next = env->free_list; |
| 9258 | env->free_list = sl; |
| 9259 | } |
| 9260 | sl = *pprev; |
| 9261 | continue; |
| 9262 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 9263 | next: |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 9264 | pprev = &sl->next; |
| 9265 | sl = *pprev; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9266 | } |
| 9267 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9268 | if (env->max_states_per_insn < states_cnt) |
| 9269 | env->max_states_per_insn = states_cnt; |
| 9270 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 9271 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9272 | return push_jmp_history(env, cur); |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 9273 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9274 | if (!add_new_state) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9275 | return push_jmp_history(env, cur); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9276 | |
| 9277 | /* There were no equivalent states, remember the current one. |
| 9278 | * Technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9279 | * but it will either reach outer most bpf_exit (which means it's safe) |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9280 | * or it will be rejected. When there are no loops the verifier won't be |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9281 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9282 | * again on the way to bpf_exit. |
| 9283 | * When looping the sl->state.branches will be > 0 and this state |
| 9284 | * will not be considered for equivalence until branches == 0. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9285 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9286 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9287 | if (!new_sl) |
| 9288 | return -ENOMEM; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9289 | env->total_states++; |
| 9290 | env->peak_states++; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9291 | env->prev_jmps_processed = env->jmps_processed; |
| 9292 | env->prev_insn_processed = env->insn_processed; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9293 | |
| 9294 | /* add new state to the head of linked list */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9295 | new = &new_sl->state; |
| 9296 | err = copy_verifier_state(new, cur); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 9297 | if (err) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9298 | free_verifier_state(new, false); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 9299 | kfree(new_sl); |
| 9300 | return err; |
| 9301 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 9302 | new->insn_idx = insn_idx; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9303 | WARN_ONCE(new->branches != 1, |
| 9304 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9305 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9306 | cur->parent = new; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9307 | cur->first_insn_idx = insn_idx; |
| 9308 | clear_jmp_history(cur); |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 9309 | new_sl->next = *explored_state(env, insn_idx); |
| 9310 | *explored_state(env, insn_idx) = new_sl; |
Jakub Kicinski | 7640ead | 2018-12-12 16:29:07 -0800 | [diff] [blame] | 9311 | /* connect new state to parentage chain. Current frame needs all |
| 9312 | * registers connected. Only r6 - r9 of the callers are alive (pushed |
| 9313 | * to the stack implicitly by JITs) so in callers' frames connect just |
| 9314 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to |
| 9315 | * the state of the call instruction (with WRITTEN set), and r0 comes |
| 9316 | * from callee with its full parentage chain, anyway. |
| 9317 | */ |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 9318 | /* clear write marks in current state: the writes we did are not writes |
| 9319 | * our child did, so they don't screen off its reads from us. |
| 9320 | * (There are no read marks in current state, because reads always mark |
| 9321 | * their parent and current state never has children yet. Only |
| 9322 | * explored_states can get read marks.) |
| 9323 | */ |
Alexei Starovoitov | eea1c22 | 2019-06-15 12:12:21 -0700 | [diff] [blame] | 9324 | for (j = 0; j <= cur->curframe; j++) { |
| 9325 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) |
| 9326 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; |
| 9327 | for (i = 0; i < BPF_REG_FP; i++) |
| 9328 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; |
| 9329 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9330 | |
| 9331 | /* all stack frames are accessible from callee, clear them all */ |
| 9332 | for (j = 0; j <= cur->curframe; j++) { |
| 9333 | struct bpf_func_state *frame = cur->frame[j]; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9334 | struct bpf_func_state *newframe = new->frame[j]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9335 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9336 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 9337 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9338 | frame->stack[i].spilled_ptr.parent = |
| 9339 | &newframe->stack[i].spilled_ptr; |
| 9340 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9341 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9342 | return 0; |
| 9343 | } |
| 9344 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 9345 | /* Return true if it's OK to have the same insn return a different type. */ |
| 9346 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 9347 | { |
| 9348 | switch (type) { |
| 9349 | case PTR_TO_CTX: |
| 9350 | case PTR_TO_SOCKET: |
| 9351 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 9352 | case PTR_TO_SOCK_COMMON: |
| 9353 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 9354 | case PTR_TO_TCP_SOCK: |
| 9355 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 9356 | case PTR_TO_XDP_SOCK: |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 9357 | case PTR_TO_BTF_ID: |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 9358 | case PTR_TO_BTF_ID_OR_NULL: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 9359 | return false; |
| 9360 | default: |
| 9361 | return true; |
| 9362 | } |
| 9363 | } |
| 9364 | |
| 9365 | /* If an instruction was previously used with particular pointer types, then we |
| 9366 | * need to be careful to avoid cases such as the below, where it may be ok |
| 9367 | * for one branch accessing the pointer, but not ok for the other branch: |
| 9368 | * |
| 9369 | * R1 = sock_ptr |
| 9370 | * goto X; |
| 9371 | * ... |
| 9372 | * R1 = some_other_valid_ptr; |
| 9373 | * goto X; |
| 9374 | * ... |
| 9375 | * R2 = *(u32 *)(R1 + 0); |
| 9376 | */ |
| 9377 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 9378 | { |
| 9379 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 9380 | !reg_type_mismatch_ok(prev)); |
| 9381 | } |
| 9382 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9383 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9384 | { |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 9385 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 9386 | struct bpf_verifier_state *state = env->cur_state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9387 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9388 | struct bpf_reg_state *regs; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9389 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9390 | bool do_print_state = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9391 | int prev_insn_idx = -1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9392 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9393 | for (;;) { |
| 9394 | struct bpf_insn *insn; |
| 9395 | u8 class; |
| 9396 | int err; |
| 9397 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9398 | env->prev_insn_idx = prev_insn_idx; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9399 | if (env->insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9400 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9401 | env->insn_idx, insn_cnt); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9402 | return -EFAULT; |
| 9403 | } |
| 9404 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9405 | insn = &insns[env->insn_idx]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9406 | class = BPF_CLASS(insn->code); |
| 9407 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9408 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9409 | verbose(env, |
| 9410 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9411 | env->insn_processed); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9412 | return -E2BIG; |
| 9413 | } |
| 9414 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9415 | err = is_state_visited(env, env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9416 | if (err < 0) |
| 9417 | return err; |
| 9418 | if (err == 1) { |
| 9419 | /* found equivalent state, can prune the search */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9420 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9421 | if (do_print_state) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 9422 | verbose(env, "\nfrom %d to %d%s: safe\n", |
| 9423 | env->prev_insn_idx, env->insn_idx, |
| 9424 | env->cur_state->speculative ? |
| 9425 | " (speculative execution)" : ""); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9426 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9427 | verbose(env, "%d: safe\n", env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9428 | } |
| 9429 | goto process_bpf_exit; |
| 9430 | } |
| 9431 | |
Alexei Starovoitov | c349480 | 2018-12-03 22:46:04 -0800 | [diff] [blame] | 9432 | if (signal_pending(current)) |
| 9433 | return -EAGAIN; |
| 9434 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 9435 | if (need_resched()) |
| 9436 | cond_resched(); |
| 9437 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9438 | if (env->log.level & BPF_LOG_LEVEL2 || |
| 9439 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { |
| 9440 | if (env->log.level & BPF_LOG_LEVEL2) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9441 | verbose(env, "%d:", env->insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 9442 | else |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 9443 | verbose(env, "\nfrom %d to %d%s:", |
| 9444 | env->prev_insn_idx, env->insn_idx, |
| 9445 | env->cur_state->speculative ? |
| 9446 | " (speculative execution)" : ""); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9447 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9448 | do_print_state = false; |
| 9449 | } |
| 9450 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9451 | if (env->log.level & BPF_LOG_LEVEL) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 9452 | const struct bpf_insn_cbs cbs = { |
| 9453 | .cb_print = verbose, |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 9454 | .private_data = env, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 9455 | }; |
| 9456 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9457 | verbose_linfo(env, env->insn_idx, "; "); |
| 9458 | verbose(env, "%d: ", env->insn_idx); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 9459 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9460 | } |
| 9461 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 9462 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9463 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
| 9464 | env->prev_insn_idx); |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 9465 | if (err) |
| 9466 | return err; |
| 9467 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 9468 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9469 | regs = cur_regs(env); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 9470 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9471 | prev_insn_idx = env->insn_idx; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9472 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9473 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 9474 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9475 | if (err) |
| 9476 | return err; |
| 9477 | |
| 9478 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9479 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9480 | |
| 9481 | /* check for reserved fields is already done */ |
| 9482 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9483 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9484 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9485 | if (err) |
| 9486 | return err; |
| 9487 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9488 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9489 | if (err) |
| 9490 | return err; |
| 9491 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 9492 | src_reg_type = regs[insn->src_reg].type; |
| 9493 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9494 | /* check that memory (src_reg + off) is readable, |
| 9495 | * the state of dst_reg will be updated by this func |
| 9496 | */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9497 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
| 9498 | insn->off, BPF_SIZE(insn->code), |
| 9499 | BPF_READ, insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9500 | if (err) |
| 9501 | return err; |
| 9502 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9503 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9504 | |
| 9505 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9506 | /* saw a valid insn |
| 9507 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9508 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9509 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9510 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9511 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 9512 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9513 | /* ABuser program is trying to use the same insn |
| 9514 | * dst_reg = *(u32*) (src_reg + off) |
| 9515 | * with different pointer types: |
| 9516 | * src_reg == ctx in one branch and |
| 9517 | * src_reg == stack|map in some other branch. |
| 9518 | * Reject it. |
| 9519 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9520 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9521 | return -EINVAL; |
| 9522 | } |
| 9523 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9524 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9525 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9526 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9527 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9528 | err = check_xadd(env, env->insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9529 | if (err) |
| 9530 | return err; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9531 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9532 | continue; |
| 9533 | } |
| 9534 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9535 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9536 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9537 | if (err) |
| 9538 | return err; |
| 9539 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9540 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9541 | if (err) |
| 9542 | return err; |
| 9543 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9544 | dst_reg_type = regs[insn->dst_reg].type; |
| 9545 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9546 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9547 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 9548 | insn->off, BPF_SIZE(insn->code), |
| 9549 | BPF_WRITE, insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9550 | if (err) |
| 9551 | return err; |
| 9552 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9553 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9554 | |
| 9555 | if (*prev_dst_type == NOT_INIT) { |
| 9556 | *prev_dst_type = dst_reg_type; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 9557 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9558 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9559 | return -EINVAL; |
| 9560 | } |
| 9561 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9562 | } else if (class == BPF_ST) { |
| 9563 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 9564 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9565 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9566 | return -EINVAL; |
| 9567 | } |
| 9568 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9569 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9570 | if (err) |
| 9571 | return err; |
| 9572 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 9573 | if (is_ctx_reg(env, insn->dst_reg)) { |
Joe Stringer | 9d2be44 | 2018-10-02 13:35:31 -0700 | [diff] [blame] | 9574 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 9575 | insn->dst_reg, |
| 9576 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 9577 | return -EACCES; |
| 9578 | } |
| 9579 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9580 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9581 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 9582 | insn->off, BPF_SIZE(insn->code), |
| 9583 | BPF_WRITE, -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9584 | if (err) |
| 9585 | return err; |
| 9586 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9587 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9588 | u8 opcode = BPF_OP(insn->code); |
| 9589 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9590 | env->jmps_processed++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9591 | if (opcode == BPF_CALL) { |
| 9592 | if (BPF_SRC(insn->code) != BPF_K || |
| 9593 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9594 | (insn->src_reg != BPF_REG_0 && |
| 9595 | insn->src_reg != BPF_PSEUDO_CALL) || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9596 | insn->dst_reg != BPF_REG_0 || |
| 9597 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9598 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9599 | return -EINVAL; |
| 9600 | } |
| 9601 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9602 | if (env->cur_state->active_spin_lock && |
| 9603 | (insn->src_reg == BPF_PSEUDO_CALL || |
| 9604 | insn->imm != BPF_FUNC_spin_unlock)) { |
| 9605 | verbose(env, "function calls are not allowed while holding a lock\n"); |
| 9606 | return -EINVAL; |
| 9607 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9608 | if (insn->src_reg == BPF_PSEUDO_CALL) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9609 | err = check_func_call(env, insn, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9610 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9611 | err = check_helper_call(env, insn->imm, env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9612 | if (err) |
| 9613 | return err; |
| 9614 | |
| 9615 | } else if (opcode == BPF_JA) { |
| 9616 | if (BPF_SRC(insn->code) != BPF_K || |
| 9617 | insn->imm != 0 || |
| 9618 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9619 | insn->dst_reg != BPF_REG_0 || |
| 9620 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9621 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9622 | return -EINVAL; |
| 9623 | } |
| 9624 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9625 | env->insn_idx += insn->off + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9626 | continue; |
| 9627 | |
| 9628 | } else if (opcode == BPF_EXIT) { |
| 9629 | if (BPF_SRC(insn->code) != BPF_K || |
| 9630 | insn->imm != 0 || |
| 9631 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9632 | insn->dst_reg != BPF_REG_0 || |
| 9633 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9634 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9635 | return -EINVAL; |
| 9636 | } |
| 9637 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9638 | if (env->cur_state->active_spin_lock) { |
| 9639 | verbose(env, "bpf_spin_unlock is missing\n"); |
| 9640 | return -EINVAL; |
| 9641 | } |
| 9642 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9643 | if (state->curframe) { |
| 9644 | /* exit from nested function */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9645 | err = prepare_func_exit(env, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9646 | if (err) |
| 9647 | return err; |
| 9648 | do_print_state = true; |
| 9649 | continue; |
| 9650 | } |
| 9651 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9652 | err = check_reference_leak(env); |
| 9653 | if (err) |
| 9654 | return err; |
| 9655 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9656 | err = check_return_code(env); |
| 9657 | if (err) |
| 9658 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9659 | process_bpf_exit: |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9660 | update_branch_counts(env, env->cur_state); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9661 | err = pop_stack(env, &prev_insn_idx, |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 9662 | &env->insn_idx, pop_log); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9663 | if (err < 0) { |
| 9664 | if (err != -ENOENT) |
| 9665 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9666 | break; |
| 9667 | } else { |
| 9668 | do_print_state = true; |
| 9669 | continue; |
| 9670 | } |
| 9671 | } else { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9672 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9673 | if (err) |
| 9674 | return err; |
| 9675 | } |
| 9676 | } else if (class == BPF_LD) { |
| 9677 | u8 mode = BPF_MODE(insn->code); |
| 9678 | |
| 9679 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9680 | err = check_ld_abs(env, insn); |
| 9681 | if (err) |
| 9682 | return err; |
| 9683 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9684 | } else if (mode == BPF_IMM) { |
| 9685 | err = check_ld_imm(env, insn); |
| 9686 | if (err) |
| 9687 | return err; |
| 9688 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9689 | env->insn_idx++; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 9690 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9691 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9692 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9693 | return -EINVAL; |
| 9694 | } |
| 9695 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9696 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9697 | return -EINVAL; |
| 9698 | } |
| 9699 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9700 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9701 | } |
| 9702 | |
| 9703 | return 0; |
| 9704 | } |
| 9705 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9706 | /* replace pseudo btf_id with kernel symbol address */ |
| 9707 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, |
| 9708 | struct bpf_insn *insn, |
| 9709 | struct bpf_insn_aux_data *aux) |
| 9710 | { |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 9711 | const struct btf_var_secinfo *vsi; |
| 9712 | const struct btf_type *datasec; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9713 | const struct btf_type *t; |
| 9714 | const char *sym_name; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 9715 | bool percpu = false; |
Kaixu Xia | f16e631 | 2020-11-11 13:03:46 +0800 | [diff] [blame] | 9716 | u32 type, id = insn->imm; |
| 9717 | s32 datasec_id; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9718 | u64 addr; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 9719 | int i; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9720 | |
| 9721 | if (!btf_vmlinux) { |
| 9722 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); |
| 9723 | return -EINVAL; |
| 9724 | } |
| 9725 | |
| 9726 | if (insn[1].imm != 0) { |
| 9727 | verbose(env, "reserved field (insn[1].imm) is used in pseudo_btf_id ldimm64 insn.\n"); |
| 9728 | return -EINVAL; |
| 9729 | } |
| 9730 | |
| 9731 | t = btf_type_by_id(btf_vmlinux, id); |
| 9732 | if (!t) { |
| 9733 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); |
| 9734 | return -ENOENT; |
| 9735 | } |
| 9736 | |
| 9737 | if (!btf_type_is_var(t)) { |
| 9738 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", |
| 9739 | id); |
| 9740 | return -EINVAL; |
| 9741 | } |
| 9742 | |
| 9743 | sym_name = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 9744 | addr = kallsyms_lookup_name(sym_name); |
| 9745 | if (!addr) { |
| 9746 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", |
| 9747 | sym_name); |
| 9748 | return -ENOENT; |
| 9749 | } |
| 9750 | |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 9751 | datasec_id = btf_find_by_name_kind(btf_vmlinux, ".data..percpu", |
| 9752 | BTF_KIND_DATASEC); |
| 9753 | if (datasec_id > 0) { |
| 9754 | datasec = btf_type_by_id(btf_vmlinux, datasec_id); |
| 9755 | for_each_vsi(i, datasec, vsi) { |
| 9756 | if (vsi->type == id) { |
| 9757 | percpu = true; |
| 9758 | break; |
| 9759 | } |
| 9760 | } |
| 9761 | } |
| 9762 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9763 | insn[0].imm = (u32)addr; |
| 9764 | insn[1].imm = addr >> 32; |
| 9765 | |
| 9766 | type = t->type; |
| 9767 | t = btf_type_skip_modifiers(btf_vmlinux, type, NULL); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 9768 | if (percpu) { |
| 9769 | aux->btf_var.reg_type = PTR_TO_PERCPU_BTF_ID; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 9770 | aux->btf_var.btf = btf_vmlinux; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 9771 | aux->btf_var.btf_id = type; |
| 9772 | } else if (!btf_type_is_struct(t)) { |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9773 | const struct btf_type *ret; |
| 9774 | const char *tname; |
| 9775 | u32 tsize; |
| 9776 | |
| 9777 | /* resolve the type size of ksym. */ |
| 9778 | ret = btf_resolve_size(btf_vmlinux, t, &tsize); |
| 9779 | if (IS_ERR(ret)) { |
| 9780 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 9781 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
| 9782 | tname, PTR_ERR(ret)); |
| 9783 | return -EINVAL; |
| 9784 | } |
| 9785 | aux->btf_var.reg_type = PTR_TO_MEM; |
| 9786 | aux->btf_var.mem_size = tsize; |
| 9787 | } else { |
| 9788 | aux->btf_var.reg_type = PTR_TO_BTF_ID; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 9789 | aux->btf_var.btf = btf_vmlinux; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9790 | aux->btf_var.btf_id = type; |
| 9791 | } |
| 9792 | return 0; |
| 9793 | } |
| 9794 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 9795 | static int check_map_prealloc(struct bpf_map *map) |
| 9796 | { |
| 9797 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 9798 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 9799 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 9800 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 9801 | } |
| 9802 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9803 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
| 9804 | { |
| 9805 | switch (type) { |
| 9806 | case BPF_PROG_TYPE_KPROBE: |
| 9807 | case BPF_PROG_TYPE_TRACEPOINT: |
| 9808 | case BPF_PROG_TYPE_PERF_EVENT: |
| 9809 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 9810 | return true; |
| 9811 | default: |
| 9812 | return false; |
| 9813 | } |
| 9814 | } |
| 9815 | |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 9816 | static bool is_preallocated_map(struct bpf_map *map) |
| 9817 | { |
| 9818 | if (!check_map_prealloc(map)) |
| 9819 | return false; |
| 9820 | if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) |
| 9821 | return false; |
| 9822 | return true; |
| 9823 | } |
| 9824 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9825 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 9826 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9827 | struct bpf_prog *prog) |
| 9828 | |
| 9829 | { |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9830 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 9831 | /* |
| 9832 | * Validate that trace type programs use preallocated hash maps. |
| 9833 | * |
| 9834 | * For programs attached to PERF events this is mandatory as the |
| 9835 | * perf NMI can hit any arbitrary code sequence. |
| 9836 | * |
| 9837 | * All other trace types using preallocated hash maps are unsafe as |
| 9838 | * well because tracepoint or kprobes can be inside locked regions |
| 9839 | * of the memory allocator or at a place where a recursion into the |
| 9840 | * memory allocator would see inconsistent state. |
| 9841 | * |
Thomas Gleixner | 2ed905c | 2020-02-24 15:01:33 +0100 | [diff] [blame] | 9842 | * On RT enabled kernels run-time allocation of all trace type |
| 9843 | * programs is strictly prohibited due to lock type constraints. On |
| 9844 | * !RT kernels it is allowed for backwards compatibility reasons for |
| 9845 | * now, but warnings are emitted so developers are made aware of |
| 9846 | * the unsafety and can fix their programs before this is enforced. |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 9847 | */ |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9848 | if (is_tracing_prog_type(prog_type) && !is_preallocated_map(map)) { |
| 9849 | if (prog_type == BPF_PROG_TYPE_PERF_EVENT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9850 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 9851 | return -EINVAL; |
| 9852 | } |
Thomas Gleixner | 2ed905c | 2020-02-24 15:01:33 +0100 | [diff] [blame] | 9853 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
| 9854 | verbose(env, "trace type programs can only use preallocated hash map\n"); |
| 9855 | return -EINVAL; |
| 9856 | } |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 9857 | WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); |
| 9858 | verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9859 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 9860 | |
KP Singh | 9e7a4d9 | 2020-11-06 10:37:39 +0000 | [diff] [blame] | 9861 | if (map_value_has_spin_lock(map)) { |
| 9862 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { |
| 9863 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); |
| 9864 | return -EINVAL; |
| 9865 | } |
| 9866 | |
| 9867 | if (is_tracing_prog_type(prog_type)) { |
| 9868 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); |
| 9869 | return -EINVAL; |
| 9870 | } |
| 9871 | |
| 9872 | if (prog->aux->sleepable) { |
| 9873 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); |
| 9874 | return -EINVAL; |
| 9875 | } |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9876 | } |
| 9877 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 9878 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
Jakub Kicinski | 0972826 | 2018-07-17 10:53:23 -0700 | [diff] [blame] | 9879 | !bpf_offload_prog_map_match(prog, map)) { |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 9880 | verbose(env, "offload device mismatch between prog and map\n"); |
| 9881 | return -EINVAL; |
| 9882 | } |
| 9883 | |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 9884 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
| 9885 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); |
| 9886 | return -EINVAL; |
| 9887 | } |
| 9888 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 9889 | if (prog->aux->sleepable) |
| 9890 | switch (map->map_type) { |
| 9891 | case BPF_MAP_TYPE_HASH: |
| 9892 | case BPF_MAP_TYPE_LRU_HASH: |
| 9893 | case BPF_MAP_TYPE_ARRAY: |
| 9894 | if (!is_preallocated_map(map)) { |
| 9895 | verbose(env, |
| 9896 | "Sleepable programs can only use preallocated hash maps\n"); |
| 9897 | return -EINVAL; |
| 9898 | } |
| 9899 | break; |
| 9900 | default: |
| 9901 | verbose(env, |
| 9902 | "Sleepable programs can only use array and hash maps\n"); |
| 9903 | return -EINVAL; |
| 9904 | } |
| 9905 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9906 | return 0; |
| 9907 | } |
| 9908 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 9909 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 9910 | { |
| 9911 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 9912 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 9913 | } |
| 9914 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9915 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
| 9916 | * |
| 9917 | * 1. if it accesses map FD, replace it with actual map pointer. |
| 9918 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. |
| 9919 | * |
| 9920 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9921 | */ |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9922 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9923 | { |
| 9924 | struct bpf_insn *insn = env->prog->insnsi; |
| 9925 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9926 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9927 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 9928 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 9929 | if (err) |
| 9930 | return err; |
| 9931 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9932 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9933 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9934 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9935 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9936 | return -EINVAL; |
| 9937 | } |
| 9938 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9939 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 9940 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 9941 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9942 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9943 | return -EINVAL; |
| 9944 | } |
| 9945 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9946 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9947 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9948 | struct bpf_map *map; |
| 9949 | struct fd f; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9950 | u64 addr; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9951 | |
| 9952 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 9953 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 9954 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9955 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9956 | return -EINVAL; |
| 9957 | } |
| 9958 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9959 | if (insn[0].src_reg == 0) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9960 | /* valid generic load 64-bit imm */ |
| 9961 | goto next_insn; |
| 9962 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9963 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
| 9964 | aux = &env->insn_aux_data[i]; |
| 9965 | err = check_pseudo_btf_id(env, insn, aux); |
| 9966 | if (err) |
| 9967 | return err; |
| 9968 | goto next_insn; |
| 9969 | } |
| 9970 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9971 | /* In final convert_pseudo_ld_imm64() step, this is |
| 9972 | * converted into regular 64-bit imm load insn. |
| 9973 | */ |
| 9974 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && |
| 9975 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || |
| 9976 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && |
| 9977 | insn[1].imm != 0)) { |
| 9978 | verbose(env, |
| 9979 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9980 | return -EINVAL; |
| 9981 | } |
| 9982 | |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 9983 | f = fdget(insn[0].imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 9984 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9985 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9986 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 9987 | insn[0].imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9988 | return PTR_ERR(map); |
| 9989 | } |
| 9990 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9991 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9992 | if (err) { |
| 9993 | fdput(f); |
| 9994 | return err; |
| 9995 | } |
| 9996 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9997 | aux = &env->insn_aux_data[i]; |
| 9998 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 9999 | addr = (unsigned long)map; |
| 10000 | } else { |
| 10001 | u32 off = insn[1].imm; |
| 10002 | |
| 10003 | if (off >= BPF_MAX_VAR_OFF) { |
| 10004 | verbose(env, "direct value offset of %u is not allowed\n", off); |
| 10005 | fdput(f); |
| 10006 | return -EINVAL; |
| 10007 | } |
| 10008 | |
| 10009 | if (!map->ops->map_direct_value_addr) { |
| 10010 | verbose(env, "no direct value access support for this map type\n"); |
| 10011 | fdput(f); |
| 10012 | return -EINVAL; |
| 10013 | } |
| 10014 | |
| 10015 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 10016 | if (err) { |
| 10017 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", |
| 10018 | map->value_size, off); |
| 10019 | fdput(f); |
| 10020 | return err; |
| 10021 | } |
| 10022 | |
| 10023 | aux->map_off = off; |
| 10024 | addr += off; |
| 10025 | } |
| 10026 | |
| 10027 | insn[0].imm = (u32)addr; |
| 10028 | insn[1].imm = addr >> 32; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 10029 | |
| 10030 | /* check whether we recorded this map already */ |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 10031 | for (j = 0; j < env->used_map_cnt; j++) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 10032 | if (env->used_maps[j] == map) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 10033 | aux->map_index = j; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 10034 | fdput(f); |
| 10035 | goto next_insn; |
| 10036 | } |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 10037 | } |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 10038 | |
| 10039 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 10040 | fdput(f); |
| 10041 | return -E2BIG; |
| 10042 | } |
| 10043 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 10044 | /* hold the map. If the program is rejected by verifier, |
| 10045 | * the map will be released by release_maps() or it |
| 10046 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 10047 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 10048 | */ |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 10049 | bpf_map_inc(map); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 10050 | |
| 10051 | aux->map_index = env->used_map_cnt; |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 10052 | env->used_maps[env->used_map_cnt++] = map; |
| 10053 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 10054 | if (bpf_map_is_cgroup_storage(map) && |
Daniel Borkmann | e473042 | 2019-12-17 13:28:16 +0100 | [diff] [blame] | 10055 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 10056 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 10057 | fdput(f); |
| 10058 | return -EBUSY; |
| 10059 | } |
| 10060 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 10061 | fdput(f); |
| 10062 | next_insn: |
| 10063 | insn++; |
| 10064 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 10065 | continue; |
| 10066 | } |
| 10067 | |
| 10068 | /* Basic sanity check before we invest more work here. */ |
| 10069 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 10070 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 10071 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 10072 | } |
| 10073 | } |
| 10074 | |
| 10075 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 10076 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 10077 | * These pointers will be used later by verifier to validate map access. |
| 10078 | */ |
| 10079 | return 0; |
| 10080 | } |
| 10081 | |
| 10082 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 10083 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 10084 | { |
Daniel Borkmann | a2ea074 | 2019-12-16 17:49:00 +0100 | [diff] [blame] | 10085 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
| 10086 | env->used_map_cnt); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 10087 | } |
| 10088 | |
| 10089 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 10090 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 10091 | { |
| 10092 | struct bpf_insn *insn = env->prog->insnsi; |
| 10093 | int insn_cnt = env->prog->len; |
| 10094 | int i; |
| 10095 | |
| 10096 | for (i = 0; i < insn_cnt; i++, insn++) |
| 10097 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 10098 | insn->src_reg = 0; |
| 10099 | } |
| 10100 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10101 | /* single env->prog->insni[off] instruction was replaced with the range |
| 10102 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 10103 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 10104 | */ |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 10105 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, |
| 10106 | struct bpf_prog *new_prog, u32 off, u32 cnt) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10107 | { |
| 10108 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 10109 | struct bpf_insn *insn = new_prog->insnsi; |
| 10110 | u32 prog_len; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 10111 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10112 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 10113 | /* aux info at OFF always needs adjustment, no matter fast path |
| 10114 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the |
| 10115 | * original insn at old prog. |
| 10116 | */ |
| 10117 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); |
| 10118 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10119 | if (cnt == 1) |
| 10120 | return 0; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 10121 | prog_len = new_prog->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 10122 | new_data = vzalloc(array_size(prog_len, |
| 10123 | sizeof(struct bpf_insn_aux_data))); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10124 | if (!new_data) |
| 10125 | return -ENOMEM; |
| 10126 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 10127 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 10128 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 10129 | for (i = off; i < off + cnt - 1; i++) { |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 10130 | new_data[i].seen = env->pass_cnt; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 10131 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
| 10132 | } |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10133 | env->insn_aux_data = new_data; |
| 10134 | vfree(old_data); |
| 10135 | return 0; |
| 10136 | } |
| 10137 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 10138 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 10139 | { |
| 10140 | int i; |
| 10141 | |
| 10142 | if (len == 1) |
| 10143 | return; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 10144 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 10145 | for (i = 0; i <= env->subprog_cnt; i++) { |
Edward Cree | afd5942 | 2018-11-16 12:00:07 +0000 | [diff] [blame] | 10146 | if (env->subprog_info[i].start <= off) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 10147 | continue; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 10148 | env->subprog_info[i].start += len - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 10149 | } |
| 10150 | } |
| 10151 | |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10152 | static void adjust_poke_descs(struct bpf_prog *prog, u32 len) |
| 10153 | { |
| 10154 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; |
| 10155 | int i, sz = prog->aux->size_poke_tab; |
| 10156 | struct bpf_jit_poke_descriptor *desc; |
| 10157 | |
| 10158 | for (i = 0; i < sz; i++) { |
| 10159 | desc = &tab[i]; |
| 10160 | desc->insn_idx += len - 1; |
| 10161 | } |
| 10162 | } |
| 10163 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10164 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 10165 | const struct bpf_insn *patch, u32 len) |
| 10166 | { |
| 10167 | struct bpf_prog *new_prog; |
| 10168 | |
| 10169 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 10170 | if (IS_ERR(new_prog)) { |
| 10171 | if (PTR_ERR(new_prog) == -ERANGE) |
| 10172 | verbose(env, |
| 10173 | "insn %d cannot be patched due to 16-bit range\n", |
| 10174 | env->insn_aux_data[off].orig_idx); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10175 | return NULL; |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 10176 | } |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 10177 | if (adjust_insn_aux_data(env, new_prog, off, len)) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10178 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 10179 | adjust_subprog_starts(env, off, len); |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10180 | adjust_poke_descs(new_prog, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10181 | return new_prog; |
| 10182 | } |
| 10183 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 10184 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
| 10185 | u32 off, u32 cnt) |
| 10186 | { |
| 10187 | int i, j; |
| 10188 | |
| 10189 | /* find first prog starting at or after off (first to remove) */ |
| 10190 | for (i = 0; i < env->subprog_cnt; i++) |
| 10191 | if (env->subprog_info[i].start >= off) |
| 10192 | break; |
| 10193 | /* find first prog starting at or after off + cnt (first to stay) */ |
| 10194 | for (j = i; j < env->subprog_cnt; j++) |
| 10195 | if (env->subprog_info[j].start >= off + cnt) |
| 10196 | break; |
| 10197 | /* if j doesn't start exactly at off + cnt, we are just removing |
| 10198 | * the front of previous prog |
| 10199 | */ |
| 10200 | if (env->subprog_info[j].start != off + cnt) |
| 10201 | j--; |
| 10202 | |
| 10203 | if (j > i) { |
| 10204 | struct bpf_prog_aux *aux = env->prog->aux; |
| 10205 | int move; |
| 10206 | |
| 10207 | /* move fake 'exit' subprog as well */ |
| 10208 | move = env->subprog_cnt + 1 - j; |
| 10209 | |
| 10210 | memmove(env->subprog_info + i, |
| 10211 | env->subprog_info + j, |
| 10212 | sizeof(*env->subprog_info) * move); |
| 10213 | env->subprog_cnt -= j - i; |
| 10214 | |
| 10215 | /* remove func_info */ |
| 10216 | if (aux->func_info) { |
| 10217 | move = aux->func_info_cnt - j; |
| 10218 | |
| 10219 | memmove(aux->func_info + i, |
| 10220 | aux->func_info + j, |
| 10221 | sizeof(*aux->func_info) * move); |
| 10222 | aux->func_info_cnt -= j - i; |
| 10223 | /* func_info->insn_off is set after all code rewrites, |
| 10224 | * in adjust_btf_func() - no need to adjust |
| 10225 | */ |
| 10226 | } |
| 10227 | } else { |
| 10228 | /* convert i from "first prog to remove" to "first to adjust" */ |
| 10229 | if (env->subprog_info[i].start == off) |
| 10230 | i++; |
| 10231 | } |
| 10232 | |
| 10233 | /* update fake 'exit' subprog as well */ |
| 10234 | for (; i <= env->subprog_cnt; i++) |
| 10235 | env->subprog_info[i].start -= cnt; |
| 10236 | |
| 10237 | return 0; |
| 10238 | } |
| 10239 | |
| 10240 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, |
| 10241 | u32 cnt) |
| 10242 | { |
| 10243 | struct bpf_prog *prog = env->prog; |
| 10244 | u32 i, l_off, l_cnt, nr_linfo; |
| 10245 | struct bpf_line_info *linfo; |
| 10246 | |
| 10247 | nr_linfo = prog->aux->nr_linfo; |
| 10248 | if (!nr_linfo) |
| 10249 | return 0; |
| 10250 | |
| 10251 | linfo = prog->aux->linfo; |
| 10252 | |
| 10253 | /* find first line info to remove, count lines to be removed */ |
| 10254 | for (i = 0; i < nr_linfo; i++) |
| 10255 | if (linfo[i].insn_off >= off) |
| 10256 | break; |
| 10257 | |
| 10258 | l_off = i; |
| 10259 | l_cnt = 0; |
| 10260 | for (; i < nr_linfo; i++) |
| 10261 | if (linfo[i].insn_off < off + cnt) |
| 10262 | l_cnt++; |
| 10263 | else |
| 10264 | break; |
| 10265 | |
| 10266 | /* First live insn doesn't match first live linfo, it needs to "inherit" |
| 10267 | * last removed linfo. prog is already modified, so prog->len == off |
| 10268 | * means no live instructions after (tail of the program was removed). |
| 10269 | */ |
| 10270 | if (prog->len != off && l_cnt && |
| 10271 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { |
| 10272 | l_cnt--; |
| 10273 | linfo[--i].insn_off = off + cnt; |
| 10274 | } |
| 10275 | |
| 10276 | /* remove the line info which refer to the removed instructions */ |
| 10277 | if (l_cnt) { |
| 10278 | memmove(linfo + l_off, linfo + i, |
| 10279 | sizeof(*linfo) * (nr_linfo - i)); |
| 10280 | |
| 10281 | prog->aux->nr_linfo -= l_cnt; |
| 10282 | nr_linfo = prog->aux->nr_linfo; |
| 10283 | } |
| 10284 | |
| 10285 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ |
| 10286 | for (i = l_off; i < nr_linfo; i++) |
| 10287 | linfo[i].insn_off -= cnt; |
| 10288 | |
| 10289 | /* fix up all subprogs (incl. 'exit') which start >= off */ |
| 10290 | for (i = 0; i <= env->subprog_cnt; i++) |
| 10291 | if (env->subprog_info[i].linfo_idx > l_off) { |
| 10292 | /* program may have started in the removed region but |
| 10293 | * may not be fully removed |
| 10294 | */ |
| 10295 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) |
| 10296 | env->subprog_info[i].linfo_idx -= l_cnt; |
| 10297 | else |
| 10298 | env->subprog_info[i].linfo_idx = l_off; |
| 10299 | } |
| 10300 | |
| 10301 | return 0; |
| 10302 | } |
| 10303 | |
| 10304 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) |
| 10305 | { |
| 10306 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 10307 | unsigned int orig_prog_len = env->prog->len; |
| 10308 | int err; |
| 10309 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 10310 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 10311 | bpf_prog_offload_remove_insns(env, off, cnt); |
| 10312 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 10313 | err = bpf_remove_insns(env->prog, off, cnt); |
| 10314 | if (err) |
| 10315 | return err; |
| 10316 | |
| 10317 | err = adjust_subprog_starts_after_remove(env, off, cnt); |
| 10318 | if (err) |
| 10319 | return err; |
| 10320 | |
| 10321 | err = bpf_adj_linfo_after_remove(env, off, cnt); |
| 10322 | if (err) |
| 10323 | return err; |
| 10324 | |
| 10325 | memmove(aux_data + off, aux_data + off + cnt, |
| 10326 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); |
| 10327 | |
| 10328 | return 0; |
| 10329 | } |
| 10330 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 10331 | /* The verifier does more data flow analysis than llvm and will not |
| 10332 | * explore branches that are dead at run time. Malicious programs can |
| 10333 | * have dead code too. Therefore replace all dead at-run-time code |
| 10334 | * with 'ja -1'. |
| 10335 | * |
| 10336 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 10337 | * program and through another bug we would manage to jump there, then |
| 10338 | * we'd execute beyond program memory otherwise. Returning exception |
| 10339 | * code also wouldn't work since we can have subprogs where the dead |
| 10340 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 10341 | */ |
| 10342 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 10343 | { |
| 10344 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 10345 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 10346 | struct bpf_insn *insn = env->prog->insnsi; |
| 10347 | const int insn_cnt = env->prog->len; |
| 10348 | int i; |
| 10349 | |
| 10350 | for (i = 0; i < insn_cnt; i++) { |
| 10351 | if (aux_data[i].seen) |
| 10352 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 10353 | memcpy(insn + i, &trap, sizeof(trap)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 10354 | } |
| 10355 | } |
| 10356 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 10357 | static bool insn_is_cond_jump(u8 code) |
| 10358 | { |
| 10359 | u8 op; |
| 10360 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 10361 | if (BPF_CLASS(code) == BPF_JMP32) |
| 10362 | return true; |
| 10363 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 10364 | if (BPF_CLASS(code) != BPF_JMP) |
| 10365 | return false; |
| 10366 | |
| 10367 | op = BPF_OP(code); |
| 10368 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; |
| 10369 | } |
| 10370 | |
| 10371 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) |
| 10372 | { |
| 10373 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 10374 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 10375 | struct bpf_insn *insn = env->prog->insnsi; |
| 10376 | const int insn_cnt = env->prog->len; |
| 10377 | int i; |
| 10378 | |
| 10379 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 10380 | if (!insn_is_cond_jump(insn->code)) |
| 10381 | continue; |
| 10382 | |
| 10383 | if (!aux_data[i + 1].seen) |
| 10384 | ja.off = insn->off; |
| 10385 | else if (!aux_data[i + 1 + insn->off].seen) |
| 10386 | ja.off = 0; |
| 10387 | else |
| 10388 | continue; |
| 10389 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 10390 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 10391 | bpf_prog_offload_replace_insn(env, i, &ja); |
| 10392 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 10393 | memcpy(insn, &ja, sizeof(ja)); |
| 10394 | } |
| 10395 | } |
| 10396 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 10397 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
| 10398 | { |
| 10399 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 10400 | int insn_cnt = env->prog->len; |
| 10401 | int i, err; |
| 10402 | |
| 10403 | for (i = 0; i < insn_cnt; i++) { |
| 10404 | int j; |
| 10405 | |
| 10406 | j = 0; |
| 10407 | while (i + j < insn_cnt && !aux_data[i + j].seen) |
| 10408 | j++; |
| 10409 | if (!j) |
| 10410 | continue; |
| 10411 | |
| 10412 | err = verifier_remove_insns(env, i, j); |
| 10413 | if (err) |
| 10414 | return err; |
| 10415 | insn_cnt = env->prog->len; |
| 10416 | } |
| 10417 | |
| 10418 | return 0; |
| 10419 | } |
| 10420 | |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 10421 | static int opt_remove_nops(struct bpf_verifier_env *env) |
| 10422 | { |
| 10423 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 10424 | struct bpf_insn *insn = env->prog->insnsi; |
| 10425 | int insn_cnt = env->prog->len; |
| 10426 | int i, err; |
| 10427 | |
| 10428 | for (i = 0; i < insn_cnt; i++) { |
| 10429 | if (memcmp(&insn[i], &ja, sizeof(ja))) |
| 10430 | continue; |
| 10431 | |
| 10432 | err = verifier_remove_insns(env, i, 1); |
| 10433 | if (err) |
| 10434 | return err; |
| 10435 | insn_cnt--; |
| 10436 | i--; |
| 10437 | } |
| 10438 | |
| 10439 | return 0; |
| 10440 | } |
| 10441 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10442 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
| 10443 | const union bpf_attr *attr) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10444 | { |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10445 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10446 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10447 | int i, patch_len, delta = 0, len = env->prog->len; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10448 | struct bpf_insn *insns = env->prog->insnsi; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10449 | struct bpf_prog *new_prog; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10450 | bool rnd_hi32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10451 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10452 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10453 | zext_patch[1] = BPF_ZEXT_REG(0); |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10454 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
| 10455 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); |
| 10456 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10457 | for (i = 0; i < len; i++) { |
| 10458 | int adj_idx = i + delta; |
| 10459 | struct bpf_insn insn; |
| 10460 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10461 | insn = insns[adj_idx]; |
| 10462 | if (!aux[adj_idx].zext_dst) { |
| 10463 | u8 code, class; |
| 10464 | u32 imm_rnd; |
| 10465 | |
| 10466 | if (!rnd_hi32) |
| 10467 | continue; |
| 10468 | |
| 10469 | code = insn.code; |
| 10470 | class = BPF_CLASS(code); |
| 10471 | if (insn_no_def(&insn)) |
| 10472 | continue; |
| 10473 | |
| 10474 | /* NOTE: arg "reg" (the fourth one) is only used for |
| 10475 | * BPF_STX which has been ruled out in above |
| 10476 | * check, it is safe to pass NULL here. |
| 10477 | */ |
| 10478 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { |
| 10479 | if (class == BPF_LD && |
| 10480 | BPF_MODE(code) == BPF_IMM) |
| 10481 | i++; |
| 10482 | continue; |
| 10483 | } |
| 10484 | |
| 10485 | /* ctx load could be transformed into wider load. */ |
| 10486 | if (class == BPF_LDX && |
| 10487 | aux[adj_idx].ptr_type == PTR_TO_CTX) |
| 10488 | continue; |
| 10489 | |
| 10490 | imm_rnd = get_random_int(); |
| 10491 | rnd_hi32_patch[0] = insn; |
| 10492 | rnd_hi32_patch[1].imm = imm_rnd; |
| 10493 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; |
| 10494 | patch = rnd_hi32_patch; |
| 10495 | patch_len = 4; |
| 10496 | goto apply_patch_buffer; |
| 10497 | } |
| 10498 | |
| 10499 | if (!bpf_jit_needs_zext()) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10500 | continue; |
| 10501 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10502 | zext_patch[0] = insn; |
| 10503 | zext_patch[1].dst_reg = insn.dst_reg; |
| 10504 | zext_patch[1].src_reg = insn.dst_reg; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10505 | patch = zext_patch; |
| 10506 | patch_len = 2; |
| 10507 | apply_patch_buffer: |
| 10508 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10509 | if (!new_prog) |
| 10510 | return -ENOMEM; |
| 10511 | env->prog = new_prog; |
| 10512 | insns = new_prog->insnsi; |
| 10513 | aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10514 | delta += patch_len - 1; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10515 | } |
| 10516 | |
| 10517 | return 0; |
| 10518 | } |
| 10519 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10520 | /* convert load instructions that access fields of a context type into a |
| 10521 | * sequence of instructions that access fields of the underlying structure: |
| 10522 | * struct __sk_buff -> struct sk_buff |
| 10523 | * struct bpf_sock_ops -> struct sock |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10524 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 10525 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10526 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 10527 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10528 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10529 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10530 | struct bpf_insn insn_buf[16], *insn; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10531 | u32 target_size, size_default, off; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10532 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 10533 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10534 | bool is_narrower_load; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10535 | |
Daniel Borkmann | b09928b | 2018-10-24 22:05:49 +0200 | [diff] [blame] | 10536 | if (ops->gen_prologue || env->seen_direct_write) { |
| 10537 | if (!ops->gen_prologue) { |
| 10538 | verbose(env, "bpf verifier is misconfigured\n"); |
| 10539 | return -EINVAL; |
| 10540 | } |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10541 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 10542 | env->prog); |
| 10543 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10544 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10545 | return -EINVAL; |
| 10546 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10547 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10548 | if (!new_prog) |
| 10549 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10550 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10551 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10552 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10553 | } |
| 10554 | } |
| 10555 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10556 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10557 | return 0; |
| 10558 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10559 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10560 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10561 | for (i = 0; i < insn_cnt; i++, insn++) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10562 | bpf_convert_ctx_access_t convert_ctx_access; |
| 10563 | |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 10564 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 10565 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 10566 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 10567 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 10568 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 10569 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 10570 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 10571 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 10572 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 10573 | type = BPF_WRITE; |
| 10574 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10575 | continue; |
| 10576 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 10577 | if (type == BPF_WRITE && |
| 10578 | env->insn_aux_data[i + delta].sanitize_stack_off) { |
| 10579 | struct bpf_insn patch[] = { |
| 10580 | /* Sanitize suspicious stack slot with zero. |
| 10581 | * There are no memory dependencies for this store, |
| 10582 | * since it's only using frame pointer and immediate |
| 10583 | * constant of zero |
| 10584 | */ |
| 10585 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, |
| 10586 | env->insn_aux_data[i + delta].sanitize_stack_off, |
| 10587 | 0), |
| 10588 | /* the original STX instruction will immediately |
| 10589 | * overwrite the same stack slot with appropriate value |
| 10590 | */ |
| 10591 | *insn, |
| 10592 | }; |
| 10593 | |
| 10594 | cnt = ARRAY_SIZE(patch); |
| 10595 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 10596 | if (!new_prog) |
| 10597 | return -ENOMEM; |
| 10598 | |
| 10599 | delta += cnt - 1; |
| 10600 | env->prog = new_prog; |
| 10601 | insn = new_prog->insnsi + i + delta; |
| 10602 | continue; |
| 10603 | } |
| 10604 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10605 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 10606 | case PTR_TO_CTX: |
| 10607 | if (!ops->convert_ctx_access) |
| 10608 | continue; |
| 10609 | convert_ctx_access = ops->convert_ctx_access; |
| 10610 | break; |
| 10611 | case PTR_TO_SOCKET: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 10612 | case PTR_TO_SOCK_COMMON: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10613 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 10614 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 10615 | case PTR_TO_TCP_SOCK: |
| 10616 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; |
| 10617 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 10618 | case PTR_TO_XDP_SOCK: |
| 10619 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; |
| 10620 | break; |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 10621 | case PTR_TO_BTF_ID: |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 10622 | if (type == BPF_READ) { |
| 10623 | insn->code = BPF_LDX | BPF_PROBE_MEM | |
| 10624 | BPF_SIZE((insn)->code); |
| 10625 | env->prog->aux->num_exentries++; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 10626 | } else if (resolve_prog_type(env->prog) != BPF_PROG_TYPE_STRUCT_OPS) { |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 10627 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
| 10628 | return -EINVAL; |
| 10629 | } |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 10630 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10631 | default: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10632 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10633 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10634 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10635 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10636 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10637 | |
| 10638 | /* If the read access is a narrower load of the field, |
| 10639 | * convert to a 4/8-byte load, to minimum program type specific |
| 10640 | * convert_ctx_access changes. If conversion is successful, |
| 10641 | * we will apply proper mask to the result. |
| 10642 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10643 | is_narrower_load = size < ctx_field_size; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10644 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
| 10645 | off = insn->off; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10646 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10647 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10648 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10649 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10650 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10651 | return -EINVAL; |
| 10652 | } |
| 10653 | |
| 10654 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10655 | if (ctx_field_size == 4) |
| 10656 | size_code = BPF_W; |
| 10657 | else if (ctx_field_size == 8) |
| 10658 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10659 | |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 10660 | insn->off = off & ~(size_default - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10661 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 10662 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10663 | |
| 10664 | target_size = 0; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10665 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 10666 | &target_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10667 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 10668 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10669 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10670 | return -EINVAL; |
| 10671 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10672 | |
| 10673 | if (is_narrower_load && size < target_size) { |
Ilya Leoshkevich | d895a0f | 2019-08-16 12:53:00 +0200 | [diff] [blame] | 10674 | u8 shift = bpf_ctx_narrow_access_offset( |
| 10675 | off, size, size_default) * 8; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10676 | if (ctx_field_size <= 4) { |
| 10677 | if (shift) |
| 10678 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, |
| 10679 | insn->dst_reg, |
| 10680 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10681 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10682 | (1 << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10683 | } else { |
| 10684 | if (shift) |
| 10685 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, |
| 10686 | insn->dst_reg, |
| 10687 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10688 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Krzesimir Nowak | e2f7fc0 | 2019-05-08 18:08:58 +0200 | [diff] [blame] | 10689 | (1ULL << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10690 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10691 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10692 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10693 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10694 | if (!new_prog) |
| 10695 | return -ENOMEM; |
| 10696 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10697 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10698 | |
| 10699 | /* keep walking new program and skip insns we just inserted */ |
| 10700 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10701 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10702 | } |
| 10703 | |
| 10704 | return 0; |
| 10705 | } |
| 10706 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10707 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 10708 | { |
| 10709 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 10710 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10711 | struct bpf_map *map_ptr; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10712 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10713 | void *old_bpf_func; |
Yonghong Song | c4c0bdc | 2020-06-23 17:10:54 -0700 | [diff] [blame] | 10714 | int err, num_exentries; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10715 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10716 | if (env->subprog_cnt <= 1) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10717 | return 0; |
| 10718 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10719 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10720 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10721 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10722 | continue; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10723 | /* Upon error here we cannot fall back to interpreter but |
| 10724 | * need a hard reject of the program. Thus -EFAULT is |
| 10725 | * propagated in any case. |
| 10726 | */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10727 | subprog = find_subprog(env, i + insn->imm + 1); |
| 10728 | if (subprog < 0) { |
| 10729 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 10730 | i + insn->imm + 1); |
| 10731 | return -EFAULT; |
| 10732 | } |
| 10733 | /* temporarily remember subprog id inside insn instead of |
| 10734 | * aux_data, since next loop will split up all insns into funcs |
| 10735 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10736 | insn->off = subprog; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10737 | /* remember original imm in case JIT fails and fallback |
| 10738 | * to interpreter will be needed |
| 10739 | */ |
| 10740 | env->insn_aux_data[i].call_imm = insn->imm; |
| 10741 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 10742 | insn->imm = 1; |
| 10743 | } |
| 10744 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10745 | err = bpf_prog_alloc_jited_linfo(prog); |
| 10746 | if (err) |
| 10747 | goto out_undo_insn; |
| 10748 | |
| 10749 | err = -ENOMEM; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 10750 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10751 | if (!func) |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10752 | goto out_undo_insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10753 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10754 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10755 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 10756 | subprog_end = env->subprog_info[i + 1].start; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10757 | |
| 10758 | len = subprog_end - subprog_start; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 10759 | /* BPF_PROG_RUN doesn't call subprogs directly, |
| 10760 | * hence main prog stats include the runtime of subprogs. |
| 10761 | * subprogs don't have IDs and not reachable via prog_get_next_id |
| 10762 | * func[i]->aux->stats will never be accessed and stays NULL |
| 10763 | */ |
| 10764 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10765 | if (!func[i]) |
| 10766 | goto out_free; |
| 10767 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 10768 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 10769 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10770 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 10771 | if (bpf_prog_calc_tag(func[i])) |
| 10772 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10773 | func[i]->is_func = 1; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10774 | func[i]->aux->func_idx = i; |
| 10775 | /* the btf and func_info will be freed only at prog->aux */ |
| 10776 | func[i]->aux->btf = prog->aux->btf; |
| 10777 | func[i]->aux->func_info = prog->aux->func_info; |
| 10778 | |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10779 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
| 10780 | u32 insn_idx = prog->aux->poke_tab[j].insn_idx; |
| 10781 | int ret; |
| 10782 | |
| 10783 | if (!(insn_idx >= subprog_start && |
| 10784 | insn_idx <= subprog_end)) |
| 10785 | continue; |
| 10786 | |
| 10787 | ret = bpf_jit_add_poke_descriptor(func[i], |
| 10788 | &prog->aux->poke_tab[j]); |
| 10789 | if (ret < 0) { |
| 10790 | verbose(env, "adding tail call poke descriptor failed\n"); |
| 10791 | goto out_free; |
| 10792 | } |
| 10793 | |
| 10794 | func[i]->insnsi[insn_idx - subprog_start].imm = ret + 1; |
| 10795 | |
| 10796 | map_ptr = func[i]->aux->poke_tab[ret].tail_call.map; |
| 10797 | ret = map_ptr->ops->map_poke_track(map_ptr, func[i]->aux); |
| 10798 | if (ret < 0) { |
| 10799 | verbose(env, "tracking tail call prog failed\n"); |
| 10800 | goto out_free; |
| 10801 | } |
| 10802 | } |
| 10803 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10804 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 10805 | * Long term would need debug info to populate names |
| 10806 | */ |
| 10807 | func[i]->aux->name[0] = 'F'; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 10808 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10809 | func[i]->jit_requested = 1; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10810 | func[i]->aux->linfo = prog->aux->linfo; |
| 10811 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; |
| 10812 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; |
| 10813 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; |
Yonghong Song | c4c0bdc | 2020-06-23 17:10:54 -0700 | [diff] [blame] | 10814 | num_exentries = 0; |
| 10815 | insn = func[i]->insnsi; |
| 10816 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 10817 | if (BPF_CLASS(insn->code) == BPF_LDX && |
| 10818 | BPF_MODE(insn->code) == BPF_PROBE_MEM) |
| 10819 | num_exentries++; |
| 10820 | } |
| 10821 | func[i]->aux->num_exentries = num_exentries; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 10822 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10823 | func[i] = bpf_int_jit_compile(func[i]); |
| 10824 | if (!func[i]->jited) { |
| 10825 | err = -ENOTSUPP; |
| 10826 | goto out_free; |
| 10827 | } |
| 10828 | cond_resched(); |
| 10829 | } |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10830 | |
| 10831 | /* Untrack main program's aux structs so that during map_poke_run() |
| 10832 | * we will not stumble upon the unfilled poke descriptors; each |
| 10833 | * of the main program's poke descs got distributed across subprogs |
| 10834 | * and got tracked onto map, so we are sure that none of them will |
| 10835 | * be missed after the operation below |
| 10836 | */ |
| 10837 | for (i = 0; i < prog->aux->size_poke_tab; i++) { |
| 10838 | map_ptr = prog->aux->poke_tab[i].tail_call.map; |
| 10839 | |
| 10840 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); |
| 10841 | } |
| 10842 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10843 | /* at this point all bpf functions were successfully JITed |
| 10844 | * now populate all bpf_calls with correct addresses and |
| 10845 | * run last pass of JIT |
| 10846 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10847 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10848 | insn = func[i]->insnsi; |
| 10849 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 10850 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10851 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10852 | continue; |
| 10853 | subprog = insn->off; |
Prashant Bhole | 0d306c3 | 2019-04-16 18:13:01 +0900 | [diff] [blame] | 10854 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
| 10855 | __bpf_call_base; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10856 | } |
Sandipan Das | 2162fed | 2018-05-24 12:26:45 +0530 | [diff] [blame] | 10857 | |
| 10858 | /* we use the aux data to keep a list of the start addresses |
| 10859 | * of the JITed images for each function in the program |
| 10860 | * |
| 10861 | * for some architectures, such as powerpc64, the imm field |
| 10862 | * might not be large enough to hold the offset of the start |
| 10863 | * address of the callee's JITed image from __bpf_call_base |
| 10864 | * |
| 10865 | * in such cases, we can lookup the start address of a callee |
| 10866 | * by using its subprog id, available from the off field of |
| 10867 | * the call instruction, as an index for this list |
| 10868 | */ |
| 10869 | func[i]->aux->func = func; |
| 10870 | func[i]->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10871 | } |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10872 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10873 | old_bpf_func = func[i]->bpf_func; |
| 10874 | tmp = bpf_int_jit_compile(func[i]); |
| 10875 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 10876 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10877 | err = -ENOTSUPP; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10878 | goto out_free; |
| 10879 | } |
| 10880 | cond_resched(); |
| 10881 | } |
| 10882 | |
| 10883 | /* finally lock prog and jit images for all functions and |
| 10884 | * populate kallsysm |
| 10885 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10886 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10887 | bpf_prog_lock_ro(func[i]); |
| 10888 | bpf_prog_kallsyms_add(func[i]); |
| 10889 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10890 | |
| 10891 | /* Last step: make now unused interpreter insns from main |
| 10892 | * prog consistent for later dump requests, so they can |
| 10893 | * later look the same as if they were interpreted only. |
| 10894 | */ |
| 10895 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10896 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10897 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10898 | continue; |
| 10899 | insn->off = env->insn_aux_data[i].call_imm; |
| 10900 | subprog = find_subprog(env, i + insn->off + 1); |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 10901 | insn->imm = subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10902 | } |
| 10903 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10904 | prog->jited = 1; |
| 10905 | prog->bpf_func = func[0]->bpf_func; |
| 10906 | prog->aux->func = func; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10907 | prog->aux->func_cnt = env->subprog_cnt; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10908 | bpf_prog_free_unused_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10909 | return 0; |
| 10910 | out_free: |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10911 | for (i = 0; i < env->subprog_cnt; i++) { |
| 10912 | if (!func[i]) |
| 10913 | continue; |
| 10914 | |
| 10915 | for (j = 0; j < func[i]->aux->size_poke_tab; j++) { |
| 10916 | map_ptr = func[i]->aux->poke_tab[j].tail_call.map; |
| 10917 | map_ptr->ops->map_poke_untrack(map_ptr, func[i]->aux); |
| 10918 | } |
| 10919 | bpf_jit_free(func[i]); |
| 10920 | } |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10921 | kfree(func); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10922 | out_undo_insn: |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10923 | /* cleanup main prog to be interpreted */ |
| 10924 | prog->jit_requested = 0; |
| 10925 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 10926 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10927 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10928 | continue; |
| 10929 | insn->off = 0; |
| 10930 | insn->imm = env->insn_aux_data[i].call_imm; |
| 10931 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10932 | bpf_prog_free_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10933 | return err; |
| 10934 | } |
| 10935 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10936 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 10937 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10938 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10939 | struct bpf_prog *prog = env->prog; |
| 10940 | struct bpf_insn *insn = prog->insnsi; |
| 10941 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10942 | #endif |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 10943 | int err = 0; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10944 | |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 10945 | if (env->prog->jit_requested && |
| 10946 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10947 | err = jit_subprogs(env); |
| 10948 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10949 | return 0; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10950 | if (err == -EFAULT) |
| 10951 | return err; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10952 | } |
| 10953 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 10954 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
| 10955 | /* When JIT fails the progs with bpf2bpf calls and tail_calls |
| 10956 | * have to be rejected, since interpreter doesn't support them yet. |
| 10957 | */ |
| 10958 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); |
| 10959 | return -EINVAL; |
| 10960 | } |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10961 | for (i = 0; i < prog->len; i++, insn++) { |
| 10962 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10963 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10964 | continue; |
| 10965 | depth = get_callee_stack_depth(env, insn, i); |
| 10966 | if (depth < 0) |
| 10967 | return depth; |
| 10968 | bpf_patch_call_args(insn, depth); |
| 10969 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10970 | err = 0; |
| 10971 | #endif |
| 10972 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10973 | } |
| 10974 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10975 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 10976 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10977 | * |
| 10978 | * this function is called after eBPF program passed verification |
| 10979 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10980 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10981 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10982 | struct bpf_prog *prog = env->prog; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 10983 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10984 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10985 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10986 | const int insn_cnt = prog->len; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 10987 | const struct bpf_map_ops *ops; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 10988 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 10989 | struct bpf_insn insn_buf[16]; |
| 10990 | struct bpf_prog *new_prog; |
| 10991 | struct bpf_map *map_ptr; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 10992 | int i, ret, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10993 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10994 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 10995 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 10996 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 10997 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 10998 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 10999 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
| 11000 | struct bpf_insn mask_and_div[] = { |
| 11001 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 11002 | /* Rx div 0 -> 0 */ |
| 11003 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), |
| 11004 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 11005 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 11006 | *insn, |
| 11007 | }; |
| 11008 | struct bpf_insn mask_and_mod[] = { |
| 11009 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 11010 | /* Rx mod 0 -> Rx */ |
| 11011 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), |
| 11012 | *insn, |
| 11013 | }; |
| 11014 | struct bpf_insn *patchlet; |
| 11015 | |
| 11016 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 11017 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 11018 | patchlet = mask_and_div + (is64 ? 1 : 0); |
| 11019 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); |
| 11020 | } else { |
| 11021 | patchlet = mask_and_mod + (is64 ? 1 : 0); |
| 11022 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); |
| 11023 | } |
| 11024 | |
| 11025 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 11026 | if (!new_prog) |
| 11027 | return -ENOMEM; |
| 11028 | |
| 11029 | delta += cnt - 1; |
| 11030 | env->prog = prog = new_prog; |
| 11031 | insn = new_prog->insnsi + i + delta; |
| 11032 | continue; |
| 11033 | } |
| 11034 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 11035 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 11036 | (BPF_MODE(insn->code) == BPF_ABS || |
| 11037 | BPF_MODE(insn->code) == BPF_IND)) { |
| 11038 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 11039 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 11040 | verbose(env, "bpf verifier is misconfigured\n"); |
| 11041 | return -EINVAL; |
| 11042 | } |
| 11043 | |
| 11044 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 11045 | if (!new_prog) |
| 11046 | return -ENOMEM; |
| 11047 | |
| 11048 | delta += cnt - 1; |
| 11049 | env->prog = prog = new_prog; |
| 11050 | insn = new_prog->insnsi + i + delta; |
| 11051 | continue; |
| 11052 | } |
| 11053 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 11054 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
| 11055 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { |
| 11056 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; |
| 11057 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; |
| 11058 | struct bpf_insn insn_buf[16]; |
| 11059 | struct bpf_insn *patch = &insn_buf[0]; |
| 11060 | bool issrc, isneg; |
| 11061 | u32 off_reg; |
| 11062 | |
| 11063 | aux = &env->insn_aux_data[i + delta]; |
Daniel Borkmann | 3612af7 | 2019-03-01 22:05:29 +0100 | [diff] [blame] | 11064 | if (!aux->alu_state || |
| 11065 | aux->alu_state == BPF_ALU_NON_POINTER) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 11066 | continue; |
| 11067 | |
| 11068 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; |
| 11069 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == |
| 11070 | BPF_ALU_SANITIZE_SRC; |
| 11071 | |
| 11072 | off_reg = issrc ? insn->src_reg : insn->dst_reg; |
| 11073 | if (isneg) |
| 11074 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 11075 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); |
| 11076 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); |
| 11077 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); |
| 11078 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); |
| 11079 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); |
| 11080 | if (issrc) { |
| 11081 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, |
| 11082 | off_reg); |
| 11083 | insn->src_reg = BPF_REG_AX; |
| 11084 | } else { |
| 11085 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, |
| 11086 | BPF_REG_AX); |
| 11087 | } |
| 11088 | if (isneg) |
| 11089 | insn->code = insn->code == code_add ? |
| 11090 | code_sub : code_add; |
| 11091 | *patch++ = *insn; |
| 11092 | if (issrc && isneg) |
| 11093 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 11094 | cnt = patch - insn_buf; |
| 11095 | |
| 11096 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 11097 | if (!new_prog) |
| 11098 | return -ENOMEM; |
| 11099 | |
| 11100 | delta += cnt - 1; |
| 11101 | env->prog = prog = new_prog; |
| 11102 | insn = new_prog->insnsi + i + delta; |
| 11103 | continue; |
| 11104 | } |
| 11105 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11106 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 11107 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 11108 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 11109 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 11110 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11111 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 11112 | prog->dst_needed = 1; |
| 11113 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 11114 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 11115 | if (insn->imm == BPF_FUNC_override_return) |
| 11116 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11117 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 11118 | /* If we tail call into other programs, we |
| 11119 | * cannot make any assumptions since they can |
| 11120 | * be replaced dynamically during runtime in |
| 11121 | * the program array. |
| 11122 | */ |
| 11123 | prog->cb_access = 1; |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 11124 | if (!allow_tail_call_in_subprogs(env)) |
| 11125 | prog->aux->stack_depth = MAX_BPF_STACK; |
| 11126 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 11127 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11128 | /* mark bpf_tail_call as different opcode to avoid |
| 11129 | * conditional branch in the interpeter for every normal |
| 11130 | * call and to prevent accidental JITing by JIT compiler |
| 11131 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 11132 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11133 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 11134 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 11135 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 11136 | aux = &env->insn_aux_data[i + delta]; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 11137 | if (env->bpf_capable && !expect_blinding && |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 11138 | prog->jit_requested && |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 11139 | !bpf_map_key_poisoned(aux) && |
| 11140 | !bpf_map_ptr_poisoned(aux) && |
| 11141 | !bpf_map_ptr_unpriv(aux)) { |
| 11142 | struct bpf_jit_poke_descriptor desc = { |
| 11143 | .reason = BPF_POKE_REASON_TAIL_CALL, |
| 11144 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), |
| 11145 | .tail_call.key = bpf_map_key_immediate(aux), |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 11146 | .insn_idx = i + delta, |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 11147 | }; |
| 11148 | |
| 11149 | ret = bpf_jit_add_poke_descriptor(prog, &desc); |
| 11150 | if (ret < 0) { |
| 11151 | verbose(env, "adding tail call poke descriptor failed\n"); |
| 11152 | return ret; |
| 11153 | } |
| 11154 | |
| 11155 | insn->imm = ret + 1; |
| 11156 | continue; |
| 11157 | } |
| 11158 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 11159 | if (!bpf_map_ptr_unpriv(aux)) |
| 11160 | continue; |
| 11161 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 11162 | /* instead of changing every JIT dealing with tail_call |
| 11163 | * emit two extra insns: |
| 11164 | * if (index >= max_entries) goto out; |
| 11165 | * index &= array->index_mask; |
| 11166 | * to avoid out-of-bounds cpu speculation |
| 11167 | */ |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 11168 | if (bpf_map_ptr_poisoned(aux)) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 11169 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 11170 | return -EINVAL; |
| 11171 | } |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 11172 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 11173 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 11174 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 11175 | map_ptr->max_entries, 2); |
| 11176 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 11177 | container_of(map_ptr, |
| 11178 | struct bpf_array, |
| 11179 | map)->index_mask); |
| 11180 | insn_buf[2] = *insn; |
| 11181 | cnt = 3; |
| 11182 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 11183 | if (!new_prog) |
| 11184 | return -ENOMEM; |
| 11185 | |
| 11186 | delta += cnt - 1; |
| 11187 | env->prog = prog = new_prog; |
| 11188 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11189 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 11190 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 11191 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 11192 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11193 | * and other inlining handlers are currently limited to 64 bit |
| 11194 | * only. |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 11195 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 11196 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11197 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 11198 | insn->imm == BPF_FUNC_map_update_elem || |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 11199 | insn->imm == BPF_FUNC_map_delete_elem || |
| 11200 | insn->imm == BPF_FUNC_map_push_elem || |
| 11201 | insn->imm == BPF_FUNC_map_pop_elem || |
| 11202 | insn->imm == BPF_FUNC_map_peek_elem)) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 11203 | aux = &env->insn_aux_data[i + delta]; |
| 11204 | if (bpf_map_ptr_poisoned(aux)) |
| 11205 | goto patch_call_imm; |
| 11206 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 11207 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11208 | ops = map_ptr->ops; |
| 11209 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 11210 | ops->map_gen_lookup) { |
| 11211 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
Daniel Borkmann | 4a8f87e | 2020-10-11 01:40:03 +0200 | [diff] [blame] | 11212 | if (cnt == -EOPNOTSUPP) |
| 11213 | goto patch_map_ops_generic; |
| 11214 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11215 | verbose(env, "bpf verifier is misconfigured\n"); |
| 11216 | return -EINVAL; |
| 11217 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 11218 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11219 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 11220 | insn_buf, cnt); |
| 11221 | if (!new_prog) |
| 11222 | return -ENOMEM; |
| 11223 | |
| 11224 | delta += cnt - 1; |
| 11225 | env->prog = prog = new_prog; |
| 11226 | insn = new_prog->insnsi + i + delta; |
| 11227 | continue; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 11228 | } |
| 11229 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11230 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 11231 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 11232 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 11233 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 11234 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 11235 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 11236 | u64 flags))NULL)); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 11237 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 11238 | (int (*)(struct bpf_map *map, void *value, |
| 11239 | u64 flags))NULL)); |
| 11240 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 11241 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 11242 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 11243 | (int (*)(struct bpf_map *map, void *value))NULL)); |
Daniel Borkmann | 4a8f87e | 2020-10-11 01:40:03 +0200 | [diff] [blame] | 11244 | patch_map_ops_generic: |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11245 | switch (insn->imm) { |
| 11246 | case BPF_FUNC_map_lookup_elem: |
| 11247 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - |
| 11248 | __bpf_call_base; |
| 11249 | continue; |
| 11250 | case BPF_FUNC_map_update_elem: |
| 11251 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - |
| 11252 | __bpf_call_base; |
| 11253 | continue; |
| 11254 | case BPF_FUNC_map_delete_elem: |
| 11255 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - |
| 11256 | __bpf_call_base; |
| 11257 | continue; |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 11258 | case BPF_FUNC_map_push_elem: |
| 11259 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - |
| 11260 | __bpf_call_base; |
| 11261 | continue; |
| 11262 | case BPF_FUNC_map_pop_elem: |
| 11263 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - |
| 11264 | __bpf_call_base; |
| 11265 | continue; |
| 11266 | case BPF_FUNC_map_peek_elem: |
| 11267 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - |
| 11268 | __bpf_call_base; |
| 11269 | continue; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11270 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 11271 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11272 | goto patch_call_imm; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 11273 | } |
| 11274 | |
Martin KaFai Lau | 5576b99 | 2020-01-22 15:36:46 -0800 | [diff] [blame] | 11275 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
| 11276 | insn->imm == BPF_FUNC_jiffies64) { |
| 11277 | struct bpf_insn ld_jiffies_addr[2] = { |
| 11278 | BPF_LD_IMM64(BPF_REG_0, |
| 11279 | (unsigned long)&jiffies), |
| 11280 | }; |
| 11281 | |
| 11282 | insn_buf[0] = ld_jiffies_addr[0]; |
| 11283 | insn_buf[1] = ld_jiffies_addr[1]; |
| 11284 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, |
| 11285 | BPF_REG_0, 0); |
| 11286 | cnt = 3; |
| 11287 | |
| 11288 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, |
| 11289 | cnt); |
| 11290 | if (!new_prog) |
| 11291 | return -ENOMEM; |
| 11292 | |
| 11293 | delta += cnt - 1; |
| 11294 | env->prog = prog = new_prog; |
| 11295 | insn = new_prog->insnsi + i + delta; |
| 11296 | continue; |
| 11297 | } |
| 11298 | |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 11299 | patch_call_imm: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 11300 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11301 | /* all functions that have prototype and verifier allowed |
| 11302 | * programs to call them, must be real in-kernel functions |
| 11303 | */ |
| 11304 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11305 | verbose(env, |
| 11306 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11307 | func_id_name(insn->imm), insn->imm); |
| 11308 | return -EFAULT; |
| 11309 | } |
| 11310 | insn->imm = fn->func - __bpf_call_base; |
| 11311 | } |
| 11312 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 11313 | /* Since poke tab is now finalized, publish aux to tracker. */ |
| 11314 | for (i = 0; i < prog->aux->size_poke_tab; i++) { |
| 11315 | map_ptr = prog->aux->poke_tab[i].tail_call.map; |
| 11316 | if (!map_ptr->ops->map_poke_track || |
| 11317 | !map_ptr->ops->map_poke_untrack || |
| 11318 | !map_ptr->ops->map_poke_run) { |
| 11319 | verbose(env, "bpf verifier is misconfigured\n"); |
| 11320 | return -EINVAL; |
| 11321 | } |
| 11322 | |
| 11323 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); |
| 11324 | if (ret < 0) { |
| 11325 | verbose(env, "tracking tail call prog failed\n"); |
| 11326 | return ret; |
| 11327 | } |
| 11328 | } |
| 11329 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11330 | return 0; |
| 11331 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 11332 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11333 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11334 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11335 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11336 | int i; |
| 11337 | |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11338 | sl = env->free_list; |
| 11339 | while (sl) { |
| 11340 | sln = sl->next; |
| 11341 | free_verifier_state(&sl->state, false); |
| 11342 | kfree(sl); |
| 11343 | sl = sln; |
| 11344 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11345 | env->free_list = NULL; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11346 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11347 | if (!env->explored_states) |
| 11348 | return; |
| 11349 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 11350 | for (i = 0; i < state_htab_size(env); i++) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11351 | sl = env->explored_states[i]; |
| 11352 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 11353 | while (sl) { |
| 11354 | sln = sl->next; |
| 11355 | free_verifier_state(&sl->state, false); |
| 11356 | kfree(sl); |
| 11357 | sl = sln; |
| 11358 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11359 | env->explored_states[i] = NULL; |
| 11360 | } |
| 11361 | } |
| 11362 | |
| 11363 | /* The verifier is using insn_aux_data[] to store temporary data during |
| 11364 | * verification and to store information for passes that run after the |
| 11365 | * verification like dead code sanitization. do_check_common() for subprogram N |
| 11366 | * may analyze many other subprograms. sanitize_insn_aux_data() clears all |
| 11367 | * temporary data after do_check_common() finds that subprogram N cannot be |
| 11368 | * verified independently. pass_cnt counts the number of times |
| 11369 | * do_check_common() was run and insn->aux->seen tells the pass number |
| 11370 | * insn_aux_data was touched. These variables are compared to clear temporary |
| 11371 | * data from failed pass. For testing and experiments do_check_common() can be |
| 11372 | * run multiple times even when prior attempt to verify is unsuccessful. |
| 11373 | */ |
| 11374 | static void sanitize_insn_aux_data(struct bpf_verifier_env *env) |
| 11375 | { |
| 11376 | struct bpf_insn *insn = env->prog->insnsi; |
| 11377 | struct bpf_insn_aux_data *aux; |
| 11378 | int i, class; |
| 11379 | |
| 11380 | for (i = 0; i < env->prog->len; i++) { |
| 11381 | class = BPF_CLASS(insn[i].code); |
| 11382 | if (class != BPF_LDX && class != BPF_STX) |
| 11383 | continue; |
| 11384 | aux = &env->insn_aux_data[i]; |
| 11385 | if (aux->seen != env->pass_cnt) |
| 11386 | continue; |
| 11387 | memset(aux, 0, offsetof(typeof(*aux), orig_idx)); |
| 11388 | } |
| 11389 | } |
| 11390 | |
| 11391 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
| 11392 | { |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 11393 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11394 | struct bpf_verifier_state *state; |
| 11395 | struct bpf_reg_state *regs; |
| 11396 | int ret, i; |
| 11397 | |
| 11398 | env->prev_linfo = NULL; |
| 11399 | env->pass_cnt++; |
| 11400 | |
| 11401 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 11402 | if (!state) |
| 11403 | return -ENOMEM; |
| 11404 | state->curframe = 0; |
| 11405 | state->speculative = false; |
| 11406 | state->branches = 1; |
| 11407 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 11408 | if (!state->frame[0]) { |
| 11409 | kfree(state); |
| 11410 | return -ENOMEM; |
| 11411 | } |
| 11412 | env->cur_state = state; |
| 11413 | init_func_state(env, state->frame[0], |
| 11414 | BPF_MAIN_FUNC /* callsite */, |
| 11415 | 0 /* frameno */, |
| 11416 | subprog); |
| 11417 | |
| 11418 | regs = state->frame[state->curframe]->regs; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11419 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11420 | ret = btf_prepare_func_args(env, subprog, regs); |
| 11421 | if (ret) |
| 11422 | goto out; |
| 11423 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { |
| 11424 | if (regs[i].type == PTR_TO_CTX) |
| 11425 | mark_reg_known_zero(env, regs, i); |
| 11426 | else if (regs[i].type == SCALAR_VALUE) |
| 11427 | mark_reg_unknown(env, regs, i); |
| 11428 | } |
| 11429 | } else { |
| 11430 | /* 1st arg to a function */ |
| 11431 | regs[BPF_REG_1].type = PTR_TO_CTX; |
| 11432 | mark_reg_known_zero(env, regs, BPF_REG_1); |
| 11433 | ret = btf_check_func_arg_match(env, subprog, regs); |
| 11434 | if (ret == -EFAULT) |
| 11435 | /* unlikely verifier bug. abort. |
| 11436 | * ret == 0 and ret < 0 are sadly acceptable for |
| 11437 | * main() function due to backward compatibility. |
| 11438 | * Like socket filter program may be written as: |
| 11439 | * int bpf_prog(struct pt_regs *ctx) |
| 11440 | * and never dereference that ctx in the program. |
| 11441 | * 'struct pt_regs' is a type mismatch for socket |
| 11442 | * filter that should be using 'struct __sk_buff'. |
| 11443 | */ |
| 11444 | goto out; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11445 | } |
| 11446 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11447 | ret = do_check(env); |
| 11448 | out: |
Alexei Starovoitov | f59bbfc | 2020-01-21 18:41:38 -0800 | [diff] [blame] | 11449 | /* check for NULL is necessary, since cur_state can be freed inside |
| 11450 | * do_check() under memory pressure. |
| 11451 | */ |
| 11452 | if (env->cur_state) { |
| 11453 | free_verifier_state(env->cur_state, true); |
| 11454 | env->cur_state = NULL; |
| 11455 | } |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 11456 | while (!pop_stack(env, NULL, NULL, false)); |
| 11457 | if (!ret && pop_log) |
| 11458 | bpf_vlog_reset(&env->log, 0); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11459 | free_states(env); |
| 11460 | if (ret) |
| 11461 | /* clean aux data in case subprog was rejected */ |
| 11462 | sanitize_insn_aux_data(env); |
| 11463 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11464 | } |
| 11465 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11466 | /* Verify all global functions in a BPF program one by one based on their BTF. |
| 11467 | * All global functions must pass verification. Otherwise the whole program is rejected. |
| 11468 | * Consider: |
| 11469 | * int bar(int); |
| 11470 | * int foo(int f) |
| 11471 | * { |
| 11472 | * return bar(f); |
| 11473 | * } |
| 11474 | * int bar(int b) |
| 11475 | * { |
| 11476 | * ... |
| 11477 | * } |
| 11478 | * foo() will be verified first for R1=any_scalar_value. During verification it |
| 11479 | * will be assumed that bar() already verified successfully and call to bar() |
| 11480 | * from foo() will be checked for type match only. Later bar() will be verified |
| 11481 | * independently to check that it's safe for R1=any_scalar_value. |
| 11482 | */ |
| 11483 | static int do_check_subprogs(struct bpf_verifier_env *env) |
| 11484 | { |
| 11485 | struct bpf_prog_aux *aux = env->prog->aux; |
| 11486 | int i, ret; |
| 11487 | |
| 11488 | if (!aux->func_info) |
| 11489 | return 0; |
| 11490 | |
| 11491 | for (i = 1; i < env->subprog_cnt; i++) { |
| 11492 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) |
| 11493 | continue; |
| 11494 | env->insn_idx = env->subprog_info[i].start; |
| 11495 | WARN_ON_ONCE(env->insn_idx == 0); |
| 11496 | ret = do_check_common(env, i); |
| 11497 | if (ret) { |
| 11498 | return ret; |
| 11499 | } else if (env->log.level & BPF_LOG_LEVEL) { |
| 11500 | verbose(env, |
| 11501 | "Func#%d is safe for any args that match its prototype\n", |
| 11502 | i); |
| 11503 | } |
| 11504 | } |
| 11505 | return 0; |
| 11506 | } |
| 11507 | |
| 11508 | static int do_check_main(struct bpf_verifier_env *env) |
| 11509 | { |
| 11510 | int ret; |
| 11511 | |
| 11512 | env->insn_idx = 0; |
| 11513 | ret = do_check_common(env, 0); |
| 11514 | if (!ret) |
| 11515 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
| 11516 | return ret; |
| 11517 | } |
| 11518 | |
| 11519 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11520 | static void print_verification_stats(struct bpf_verifier_env *env) |
| 11521 | { |
| 11522 | int i; |
| 11523 | |
| 11524 | if (env->log.level & BPF_LOG_STATS) { |
| 11525 | verbose(env, "verification time %lld usec\n", |
| 11526 | div_u64(env->verification_time, 1000)); |
| 11527 | verbose(env, "stack depth "); |
| 11528 | for (i = 0; i < env->subprog_cnt; i++) { |
| 11529 | u32 depth = env->subprog_info[i].stack_depth; |
| 11530 | |
| 11531 | verbose(env, "%d", depth); |
| 11532 | if (i + 1 < env->subprog_cnt) |
| 11533 | verbose(env, "+"); |
| 11534 | } |
| 11535 | verbose(env, "\n"); |
| 11536 | } |
| 11537 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " |
| 11538 | "total_states %d peak_states %d mark_read %d\n", |
| 11539 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, |
| 11540 | env->max_states_per_insn, env->total_states, |
| 11541 | env->peak_states, env->longest_mark_read_walk); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11542 | } |
| 11543 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 11544 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
| 11545 | { |
| 11546 | const struct btf_type *t, *func_proto; |
| 11547 | const struct bpf_struct_ops *st_ops; |
| 11548 | const struct btf_member *member; |
| 11549 | struct bpf_prog *prog = env->prog; |
| 11550 | u32 btf_id, member_idx; |
| 11551 | const char *mname; |
| 11552 | |
| 11553 | btf_id = prog->aux->attach_btf_id; |
| 11554 | st_ops = bpf_struct_ops_find(btf_id); |
| 11555 | if (!st_ops) { |
| 11556 | verbose(env, "attach_btf_id %u is not a supported struct\n", |
| 11557 | btf_id); |
| 11558 | return -ENOTSUPP; |
| 11559 | } |
| 11560 | |
| 11561 | t = st_ops->type; |
| 11562 | member_idx = prog->expected_attach_type; |
| 11563 | if (member_idx >= btf_type_vlen(t)) { |
| 11564 | verbose(env, "attach to invalid member idx %u of struct %s\n", |
| 11565 | member_idx, st_ops->name); |
| 11566 | return -EINVAL; |
| 11567 | } |
| 11568 | |
| 11569 | member = &btf_type_member(t)[member_idx]; |
| 11570 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); |
| 11571 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, |
| 11572 | NULL); |
| 11573 | if (!func_proto) { |
| 11574 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", |
| 11575 | mname, member_idx, st_ops->name); |
| 11576 | return -EINVAL; |
| 11577 | } |
| 11578 | |
| 11579 | if (st_ops->check_member) { |
| 11580 | int err = st_ops->check_member(t, member); |
| 11581 | |
| 11582 | if (err) { |
| 11583 | verbose(env, "attach to unsupported member %s of struct %s\n", |
| 11584 | mname, st_ops->name); |
| 11585 | return err; |
| 11586 | } |
| 11587 | } |
| 11588 | |
| 11589 | prog->aux->attach_func_proto = func_proto; |
| 11590 | prog->aux->attach_func_name = mname; |
| 11591 | env->ops = st_ops->verifier_ops; |
| 11592 | |
| 11593 | return 0; |
| 11594 | } |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11595 | #define SECURITY_PREFIX "security_" |
| 11596 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11597 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11598 | { |
KP Singh | 6919175 | 2020-03-05 21:49:55 +0100 | [diff] [blame] | 11599 | if (within_error_injection_list(addr) || |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11600 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11601 | return 0; |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11602 | |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11603 | return -EINVAL; |
| 11604 | } |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 11605 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11606 | /* list of non-sleepable functions that are otherwise on |
| 11607 | * ALLOW_ERROR_INJECTION list |
| 11608 | */ |
| 11609 | BTF_SET_START(btf_non_sleepable_error_inject) |
| 11610 | /* Three functions below can be called from sleepable and non-sleepable context. |
| 11611 | * Assume non-sleepable from bpf safety point of view. |
| 11612 | */ |
| 11613 | BTF_ID(func, __add_to_page_cache_locked) |
| 11614 | BTF_ID(func, should_fail_alloc_page) |
| 11615 | BTF_ID(func, should_failslab) |
| 11616 | BTF_SET_END(btf_non_sleepable_error_inject) |
| 11617 | |
| 11618 | static int check_non_sleepable_error_inject(u32 btf_id) |
| 11619 | { |
| 11620 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); |
| 11621 | } |
| 11622 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11623 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
| 11624 | const struct bpf_prog *prog, |
| 11625 | const struct bpf_prog *tgt_prog, |
| 11626 | u32 btf_id, |
| 11627 | struct bpf_attach_target_info *tgt_info) |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11628 | { |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11629 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11630 | const char prefix[] = "btf_trace_"; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11631 | int ret = 0, subprog = -1, i; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11632 | const struct btf_type *t; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11633 | bool conservative = true; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11634 | const char *tname; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11635 | struct btf *btf; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11636 | long addr = 0; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11637 | |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11638 | if (!btf_id) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11639 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11640 | return -EINVAL; |
| 11641 | } |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 11642 | btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11643 | if (!btf) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11644 | bpf_log(log, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11645 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
| 11646 | return -EINVAL; |
| 11647 | } |
| 11648 | t = btf_type_by_id(btf, btf_id); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11649 | if (!t) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11650 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11651 | return -EINVAL; |
| 11652 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11653 | tname = btf_name_by_offset(btf, t->name_off); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11654 | if (!tname) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11655 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11656 | return -EINVAL; |
| 11657 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11658 | if (tgt_prog) { |
| 11659 | struct bpf_prog_aux *aux = tgt_prog->aux; |
| 11660 | |
| 11661 | for (i = 0; i < aux->func_info_cnt; i++) |
| 11662 | if (aux->func_info[i].type_id == btf_id) { |
| 11663 | subprog = i; |
| 11664 | break; |
| 11665 | } |
| 11666 | if (subprog == -1) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11667 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11668 | return -EINVAL; |
| 11669 | } |
| 11670 | conservative = aux->func_info_aux[subprog].unreliable; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11671 | if (prog_extension) { |
| 11672 | if (conservative) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11673 | bpf_log(log, |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11674 | "Cannot replace static functions\n"); |
| 11675 | return -EINVAL; |
| 11676 | } |
| 11677 | if (!prog->jit_requested) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11678 | bpf_log(log, |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11679 | "Extension programs should be JITed\n"); |
| 11680 | return -EINVAL; |
| 11681 | } |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11682 | } |
| 11683 | if (!tgt_prog->jited) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11684 | bpf_log(log, "Can attach to only JITed progs\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11685 | return -EINVAL; |
| 11686 | } |
| 11687 | if (tgt_prog->type == prog->type) { |
| 11688 | /* Cannot fentry/fexit another fentry/fexit program. |
| 11689 | * Cannot attach program extension to another extension. |
| 11690 | * It's ok to attach fentry/fexit to extension program. |
| 11691 | */ |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11692 | bpf_log(log, "Cannot recursively attach\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11693 | return -EINVAL; |
| 11694 | } |
| 11695 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && |
| 11696 | prog_extension && |
| 11697 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || |
| 11698 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { |
| 11699 | /* Program extensions can extend all program types |
| 11700 | * except fentry/fexit. The reason is the following. |
| 11701 | * The fentry/fexit programs are used for performance |
| 11702 | * analysis, stats and can be attached to any program |
| 11703 | * type except themselves. When extension program is |
| 11704 | * replacing XDP function it is necessary to allow |
| 11705 | * performance analysis of all functions. Both original |
| 11706 | * XDP program and its program extension. Hence |
| 11707 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is |
| 11708 | * allowed. If extending of fentry/fexit was allowed it |
| 11709 | * would be possible to create long call chain |
| 11710 | * fentry->extension->fentry->extension beyond |
| 11711 | * reasonable stack size. Hence extending fentry is not |
| 11712 | * allowed. |
| 11713 | */ |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11714 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11715 | return -EINVAL; |
| 11716 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11717 | } else { |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11718 | if (prog_extension) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11719 | bpf_log(log, "Cannot replace kernel functions\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11720 | return -EINVAL; |
| 11721 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11722 | } |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11723 | |
| 11724 | switch (prog->expected_attach_type) { |
| 11725 | case BPF_TRACE_RAW_TP: |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11726 | if (tgt_prog) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11727 | bpf_log(log, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11728 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
| 11729 | return -EINVAL; |
| 11730 | } |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11731 | if (!btf_type_is_typedef(t)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11732 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11733 | btf_id); |
| 11734 | return -EINVAL; |
| 11735 | } |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11736 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11737 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11738 | btf_id, tname); |
| 11739 | return -EINVAL; |
| 11740 | } |
| 11741 | tname += sizeof(prefix) - 1; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11742 | t = btf_type_by_id(btf, t->type); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11743 | if (!btf_type_is_ptr(t)) |
| 11744 | /* should never happen in valid vmlinux build */ |
| 11745 | return -EINVAL; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11746 | t = btf_type_by_id(btf, t->type); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11747 | if (!btf_type_is_func_proto(t)) |
| 11748 | /* should never happen in valid vmlinux build */ |
| 11749 | return -EINVAL; |
| 11750 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11751 | break; |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 11752 | case BPF_TRACE_ITER: |
| 11753 | if (!btf_type_is_func(t)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11754 | bpf_log(log, "attach_btf_id %u is not a function\n", |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 11755 | btf_id); |
| 11756 | return -EINVAL; |
| 11757 | } |
| 11758 | t = btf_type_by_id(btf, t->type); |
| 11759 | if (!btf_type_is_func_proto(t)) |
| 11760 | return -EINVAL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11761 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
| 11762 | if (ret) |
| 11763 | return ret; |
| 11764 | break; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11765 | default: |
| 11766 | if (!prog_extension) |
| 11767 | return -EINVAL; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 11768 | fallthrough; |
KP Singh | ae24082 | 2020-03-04 20:18:49 +0100 | [diff] [blame] | 11769 | case BPF_MODIFY_RETURN: |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 11770 | case BPF_LSM_MAC: |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11771 | case BPF_TRACE_FENTRY: |
| 11772 | case BPF_TRACE_FEXIT: |
| 11773 | if (!btf_type_is_func(t)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11774 | bpf_log(log, "attach_btf_id %u is not a function\n", |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11775 | btf_id); |
| 11776 | return -EINVAL; |
| 11777 | } |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11778 | if (prog_extension && |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11779 | btf_check_type_match(log, prog, btf, t)) |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11780 | return -EINVAL; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11781 | t = btf_type_by_id(btf, t->type); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11782 | if (!btf_type_is_func_proto(t)) |
| 11783 | return -EINVAL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11784 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 11785 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
| 11786 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || |
| 11787 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) |
| 11788 | return -EINVAL; |
| 11789 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11790 | if (tgt_prog && conservative) |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11791 | t = NULL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11792 | |
| 11793 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11794 | if (ret < 0) |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11795 | return ret; |
| 11796 | |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11797 | if (tgt_prog) { |
Yonghong Song | e9eeec5 | 2019-12-04 17:06:06 -0800 | [diff] [blame] | 11798 | if (subprog == 0) |
| 11799 | addr = (long) tgt_prog->bpf_func; |
| 11800 | else |
| 11801 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11802 | } else { |
| 11803 | addr = kallsyms_lookup_name(tname); |
| 11804 | if (!addr) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11805 | bpf_log(log, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11806 | "The address of function %s cannot be found\n", |
| 11807 | tname); |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11808 | return -ENOENT; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11809 | } |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11810 | } |
Alexei Starovoitov | 18644ce | 2020-05-28 21:38:36 -0700 | [diff] [blame] | 11811 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11812 | if (prog->aux->sleepable) { |
| 11813 | ret = -EINVAL; |
| 11814 | switch (prog->type) { |
| 11815 | case BPF_PROG_TYPE_TRACING: |
| 11816 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are |
| 11817 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. |
| 11818 | */ |
| 11819 | if (!check_non_sleepable_error_inject(btf_id) && |
| 11820 | within_error_injection_list(addr)) |
| 11821 | ret = 0; |
| 11822 | break; |
| 11823 | case BPF_PROG_TYPE_LSM: |
| 11824 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. |
| 11825 | * Only some of them are sleepable. |
| 11826 | */ |
KP Singh | 423f161 | 2020-11-13 00:59:29 +0000 | [diff] [blame] | 11827 | if (bpf_lsm_is_sleepable_hook(btf_id)) |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11828 | ret = 0; |
| 11829 | break; |
| 11830 | default: |
| 11831 | break; |
| 11832 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11833 | if (ret) { |
| 11834 | bpf_log(log, "%s is not sleepable\n", tname); |
| 11835 | return ret; |
| 11836 | } |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11837 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
Toke Høiland-Jørgensen | 1af9270 | 2020-09-25 23:25:00 +0200 | [diff] [blame] | 11838 | if (tgt_prog) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11839 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11840 | return -EINVAL; |
Toke Høiland-Jørgensen | 1af9270 | 2020-09-25 23:25:00 +0200 | [diff] [blame] | 11841 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11842 | ret = check_attach_modify_return(addr, tname); |
| 11843 | if (ret) { |
| 11844 | bpf_log(log, "%s() is not modifiable\n", tname); |
| 11845 | return ret; |
| 11846 | } |
Alexei Starovoitov | 18644ce | 2020-05-28 21:38:36 -0700 | [diff] [blame] | 11847 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11848 | |
| 11849 | break; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11850 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11851 | tgt_info->tgt_addr = addr; |
| 11852 | tgt_info->tgt_name = tname; |
| 11853 | tgt_info->tgt_type = t; |
| 11854 | return 0; |
| 11855 | } |
| 11856 | |
| 11857 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
| 11858 | { |
| 11859 | struct bpf_prog *prog = env->prog; |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 11860 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11861 | struct bpf_attach_target_info tgt_info = {}; |
| 11862 | u32 btf_id = prog->aux->attach_btf_id; |
| 11863 | struct bpf_trampoline *tr; |
| 11864 | int ret; |
| 11865 | u64 key; |
| 11866 | |
| 11867 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && |
| 11868 | prog->type != BPF_PROG_TYPE_LSM) { |
| 11869 | verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n"); |
| 11870 | return -EINVAL; |
| 11871 | } |
| 11872 | |
| 11873 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) |
| 11874 | return check_struct_ops_btf_id(env); |
| 11875 | |
| 11876 | if (prog->type != BPF_PROG_TYPE_TRACING && |
| 11877 | prog->type != BPF_PROG_TYPE_LSM && |
| 11878 | prog->type != BPF_PROG_TYPE_EXT) |
| 11879 | return 0; |
| 11880 | |
| 11881 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); |
| 11882 | if (ret) |
| 11883 | return ret; |
| 11884 | |
| 11885 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 11886 | /* to make freplace equivalent to their targets, they need to |
| 11887 | * inherit env->ops and expected_attach_type for the rest of the |
| 11888 | * verification |
| 11889 | */ |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11890 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
| 11891 | prog->expected_attach_type = tgt_prog->expected_attach_type; |
| 11892 | } |
| 11893 | |
| 11894 | /* store info about the attachment target that will be used later */ |
| 11895 | prog->aux->attach_func_proto = tgt_info.tgt_type; |
| 11896 | prog->aux->attach_func_name = tgt_info.tgt_name; |
| 11897 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 11898 | if (tgt_prog) { |
| 11899 | prog->aux->saved_dst_prog_type = tgt_prog->type; |
| 11900 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; |
| 11901 | } |
| 11902 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11903 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
| 11904 | prog->aux->attach_btf_trace = true; |
| 11905 | return 0; |
| 11906 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { |
| 11907 | if (!bpf_iter_prog_supported(prog)) |
| 11908 | return -EINVAL; |
| 11909 | return 0; |
| 11910 | } |
| 11911 | |
| 11912 | if (prog->type == BPF_PROG_TYPE_LSM) { |
| 11913 | ret = bpf_lsm_verify_prog(&env->log, prog); |
| 11914 | if (ret < 0) |
| 11915 | return ret; |
| 11916 | } |
| 11917 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 11918 | key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id); |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11919 | tr = bpf_trampoline_get(key, &tgt_info); |
| 11920 | if (!tr) |
| 11921 | return -ENOMEM; |
| 11922 | |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 11923 | prog->aux->dst_trampoline = tr; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11924 | return 0; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11925 | } |
| 11926 | |
Alan Maguire | 76654e6 | 2020-09-28 12:31:03 +0100 | [diff] [blame] | 11927 | struct btf *bpf_get_btf_vmlinux(void) |
| 11928 | { |
| 11929 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { |
| 11930 | mutex_lock(&bpf_verifier_lock); |
| 11931 | if (!btf_vmlinux) |
| 11932 | btf_vmlinux = btf_parse_vmlinux(); |
| 11933 | mutex_unlock(&bpf_verifier_lock); |
| 11934 | } |
| 11935 | return btf_vmlinux; |
| 11936 | } |
| 11937 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 11938 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
| 11939 | union bpf_attr __user *uattr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 11940 | { |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11941 | u64 start_time = ktime_get_ns(); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11942 | struct bpf_verifier_env *env; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 11943 | struct bpf_verifier_log *log; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 11944 | int i, len, ret = -EINVAL; |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 11945 | bool is_priv; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 11946 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 11947 | /* no program is valid */ |
| 11948 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 11949 | return -EINVAL; |
| 11950 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11951 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11952 | * allocate/free it every time bpf_check() is called |
| 11953 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11954 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11955 | if (!env) |
| 11956 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11957 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11958 | |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 11959 | len = (*prog)->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 11960 | env->insn_aux_data = |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 11961 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11962 | ret = -ENOMEM; |
| 11963 | if (!env->insn_aux_data) |
| 11964 | goto err_free_env; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 11965 | for (i = 0; i < len; i++) |
| 11966 | env->insn_aux_data[i].orig_idx = i; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11967 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 11968 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 11969 | is_priv = bpf_capable(); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11970 | |
Alan Maguire | 76654e6 | 2020-09-28 12:31:03 +0100 | [diff] [blame] | 11971 | bpf_get_btf_vmlinux(); |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 11972 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11973 | /* grab the mutex to protect few globals used by verifier */ |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 11974 | if (!is_priv) |
| 11975 | mutex_lock(&bpf_verifier_lock); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11976 | |
| 11977 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 11978 | /* user requested verbose verifier output |
| 11979 | * and supplied buffer to store the verification trace |
| 11980 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 11981 | log->level = attr->log_level; |
| 11982 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 11983 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11984 | |
| 11985 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 11986 | /* log attributes have to be sane */ |
Alexei Starovoitov | 7a9f5c6 | 2019-04-01 21:27:46 -0700 | [diff] [blame] | 11987 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11988 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11989 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11990 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 11991 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 11992 | if (IS_ERR(btf_vmlinux)) { |
| 11993 | /* Either gcc or pahole or kernel are broken. */ |
| 11994 | verbose(env, "in-kernel BTF is malformed\n"); |
| 11995 | ret = PTR_ERR(btf_vmlinux); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11996 | goto skip_full_check; |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 11997 | } |
| 11998 | |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 11999 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 12000 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 12001 | env->strict_alignment = true; |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 12002 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
| 12003 | env->strict_alignment = false; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 12004 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 12005 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 12006 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 12007 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
| 12008 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); |
| 12009 | env->bpf_capable = bpf_capable(); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 12010 | |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 12011 | if (is_priv) |
| 12012 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; |
| 12013 | |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 12014 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Quentin Monnet | a40a263 | 2018-11-09 13:03:31 +0000 | [diff] [blame] | 12015 | ret = bpf_prog_offload_verifier_prep(env->prog); |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 12016 | if (ret) |
| 12017 | goto skip_full_check; |
| 12018 | } |
| 12019 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 12020 | env->explored_states = kvcalloc(state_htab_size(env), |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 12021 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 12022 | GFP_USER); |
| 12023 | ret = -ENOMEM; |
| 12024 | if (!env->explored_states) |
| 12025 | goto skip_full_check; |
| 12026 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 12027 | ret = check_subprogs(env); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 12028 | if (ret < 0) |
| 12029 | goto skip_full_check; |
| 12030 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 12031 | ret = check_btf_info(env, attr, uattr); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 12032 | if (ret < 0) |
| 12033 | goto skip_full_check; |
| 12034 | |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 12035 | ret = check_attach_btf_id(env); |
| 12036 | if (ret) |
| 12037 | goto skip_full_check; |
| 12038 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 12039 | ret = resolve_pseudo_ldimm64(env); |
| 12040 | if (ret < 0) |
| 12041 | goto skip_full_check; |
| 12042 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 12043 | ret = check_cfg(env); |
| 12044 | if (ret < 0) |
| 12045 | goto skip_full_check; |
| 12046 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 12047 | ret = do_check_subprogs(env); |
| 12048 | ret = ret ?: do_check_main(env); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 12049 | |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 12050 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 12051 | ret = bpf_prog_offload_finalize(env); |
| 12052 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12053 | skip_full_check: |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 12054 | kvfree(env->explored_states); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12055 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12056 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 12057 | ret = check_max_stack_depth(env); |
| 12058 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 12059 | /* instruction rewrites happen after this point */ |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 12060 | if (is_priv) { |
| 12061 | if (ret == 0) |
| 12062 | opt_hard_wire_dead_code_branches(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 12063 | if (ret == 0) |
| 12064 | ret = opt_remove_dead_code(env); |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 12065 | if (ret == 0) |
| 12066 | ret = opt_remove_nops(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 12067 | } else { |
| 12068 | if (ret == 0) |
| 12069 | sanitize_dead_code(env); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 12070 | } |
| 12071 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 12072 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12073 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 12074 | ret = convert_ctx_accesses(env); |
| 12075 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 12076 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 12077 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 12078 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12079 | /* do 32-bit optimization after insn patching has done so those patched |
| 12080 | * insns could be handled correctly. |
| 12081 | */ |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12082 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
| 12083 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); |
| 12084 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret |
| 12085 | : false; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12086 | } |
| 12087 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 12088 | if (ret == 0) |
| 12089 | ret = fixup_call_args(env); |
| 12090 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 12091 | env->verification_time = ktime_get_ns() - start_time; |
| 12092 | print_verification_stats(env); |
| 12093 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 12094 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 12095 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 12096 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 12097 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 12098 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 12099 | } |
| 12100 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12101 | if (ret == 0 && env->used_map_cnt) { |
| 12102 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12103 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 12104 | sizeof(env->used_maps[0]), |
| 12105 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12106 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12107 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12108 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 12109 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12110 | } |
| 12111 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12112 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12113 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12114 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12115 | |
| 12116 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 12117 | * bpf_ld_imm64 instructions |
| 12118 | */ |
| 12119 | convert_pseudo_ld_imm64(env); |
| 12120 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 12121 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 12122 | if (ret == 0) |
| 12123 | adjust_btf_func(env); |
| 12124 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 12125 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12126 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12127 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 12128 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12129 | */ |
| 12130 | release_maps(env); |
Toke Høiland-Jørgensen | 03f87c0 | 2020-04-24 15:34:27 +0200 | [diff] [blame] | 12131 | |
| 12132 | /* extension progs temporarily inherit the attach_type of their targets |
| 12133 | for verification purposes, so set it back to zero before returning |
| 12134 | */ |
| 12135 | if (env->prog->type == BPF_PROG_TYPE_EXT) |
| 12136 | env->prog->expected_attach_type = 0; |
| 12137 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12138 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12139 | err_unlock: |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 12140 | if (!is_priv) |
| 12141 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12142 | vfree(env->insn_aux_data); |
| 12143 | err_free_env: |
| 12144 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 12145 | return ret; |
| 12146 | } |