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; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 241 | u32 btf_id; |
| 242 | u32 ret_btf_id; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 243 | }; |
| 244 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 245 | struct btf *btf_vmlinux; |
| 246 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 247 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 248 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 249 | static const struct bpf_line_info * |
| 250 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) |
| 251 | { |
| 252 | const struct bpf_line_info *linfo; |
| 253 | const struct bpf_prog *prog; |
| 254 | u32 i, nr_linfo; |
| 255 | |
| 256 | prog = env->prog; |
| 257 | nr_linfo = prog->aux->nr_linfo; |
| 258 | |
| 259 | if (!nr_linfo || insn_off >= prog->len) |
| 260 | return NULL; |
| 261 | |
| 262 | linfo = prog->aux->linfo; |
| 263 | for (i = 1; i < nr_linfo; i++) |
| 264 | if (insn_off < linfo[i].insn_off) |
| 265 | break; |
| 266 | |
| 267 | return &linfo[i - 1]; |
| 268 | } |
| 269 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 270 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
| 271 | va_list args) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 272 | { |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 273 | unsigned int n; |
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 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 276 | |
| 277 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 278 | "verifier log line truncated - local buffer too short\n"); |
| 279 | |
| 280 | n = min(log->len_total - log->len_used - 1, n); |
| 281 | log->kbuf[n] = '\0'; |
| 282 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 283 | if (log->level == BPF_LOG_KERNEL) { |
| 284 | pr_err("BPF:%s\n", log->kbuf); |
| 285 | return; |
| 286 | } |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 287 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 288 | log->len_used += n; |
| 289 | else |
| 290 | log->ubuf = NULL; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 291 | } |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 292 | |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 293 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
| 294 | { |
| 295 | char zero = 0; |
| 296 | |
| 297 | if (!bpf_verifier_log_needed(log)) |
| 298 | return; |
| 299 | |
| 300 | log->len_used = new_pos; |
| 301 | if (put_user(zero, log->ubuf + new_pos)) |
| 302 | log->ubuf = NULL; |
| 303 | } |
| 304 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 305 | /* log_level controls verbosity level of eBPF verifier. |
| 306 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 307 | * so the user can figure out what's wrong with the program |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 308 | */ |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 309 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 310 | const char *fmt, ...) |
| 311 | { |
| 312 | va_list args; |
| 313 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 314 | if (!bpf_verifier_log_needed(&env->log)) |
| 315 | return; |
| 316 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 317 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 318 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 319 | va_end(args); |
| 320 | } |
| 321 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 322 | |
| 323 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
| 324 | { |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 325 | struct bpf_verifier_env *env = private_data; |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 326 | va_list args; |
| 327 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 328 | if (!bpf_verifier_log_needed(&env->log)) |
| 329 | return; |
| 330 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 331 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 332 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 333 | va_end(args); |
| 334 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 335 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 336 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
| 337 | const char *fmt, ...) |
| 338 | { |
| 339 | va_list args; |
| 340 | |
| 341 | if (!bpf_verifier_log_needed(log)) |
| 342 | return; |
| 343 | |
| 344 | va_start(args, fmt); |
| 345 | bpf_verifier_vlog(log, fmt, args); |
| 346 | va_end(args); |
| 347 | } |
| 348 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 349 | static const char *ltrim(const char *s) |
| 350 | { |
| 351 | while (isspace(*s)) |
| 352 | s++; |
| 353 | |
| 354 | return s; |
| 355 | } |
| 356 | |
| 357 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, |
| 358 | u32 insn_off, |
| 359 | const char *prefix_fmt, ...) |
| 360 | { |
| 361 | const struct bpf_line_info *linfo; |
| 362 | |
| 363 | if (!bpf_verifier_log_needed(&env->log)) |
| 364 | return; |
| 365 | |
| 366 | linfo = find_linfo(env, insn_off); |
| 367 | if (!linfo || linfo == env->prev_linfo) |
| 368 | return; |
| 369 | |
| 370 | if (prefix_fmt) { |
| 371 | va_list args; |
| 372 | |
| 373 | va_start(args, prefix_fmt); |
| 374 | bpf_verifier_vlog(&env->log, prefix_fmt, args); |
| 375 | va_end(args); |
| 376 | } |
| 377 | |
| 378 | verbose(env, "%s\n", |
| 379 | ltrim(btf_name_by_offset(env->prog->aux->btf, |
| 380 | linfo->line_off))); |
| 381 | |
| 382 | env->prev_linfo = linfo; |
| 383 | } |
| 384 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 385 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 386 | { |
| 387 | return type == PTR_TO_PACKET || |
| 388 | type == PTR_TO_PACKET_META; |
| 389 | } |
| 390 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 391 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
| 392 | { |
| 393 | return type == PTR_TO_SOCKET || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 394 | type == PTR_TO_SOCK_COMMON || |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 395 | type == PTR_TO_TCP_SOCK || |
| 396 | type == PTR_TO_XDP_SOCK; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 397 | } |
| 398 | |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 399 | static bool reg_type_not_null(enum bpf_reg_type type) |
| 400 | { |
| 401 | return type == PTR_TO_SOCKET || |
| 402 | type == PTR_TO_TCP_SOCK || |
| 403 | type == PTR_TO_MAP_VALUE || |
Yonghong Song | 01c66c4 | 2020-06-30 10:12:40 -0700 | [diff] [blame] | 404 | type == PTR_TO_SOCK_COMMON; |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 407 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
| 408 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 409 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 410 | type == PTR_TO_SOCKET_OR_NULL || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 411 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 412 | type == PTR_TO_TCP_SOCK_OR_NULL || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 413 | type == PTR_TO_BTF_ID_OR_NULL || |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 414 | type == PTR_TO_MEM_OR_NULL || |
| 415 | type == PTR_TO_RDONLY_BUF_OR_NULL || |
| 416 | type == PTR_TO_RDWR_BUF_OR_NULL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 419 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
| 420 | { |
| 421 | return reg->type == PTR_TO_MAP_VALUE && |
| 422 | map_value_has_spin_lock(reg->map_ptr); |
| 423 | } |
| 424 | |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 425 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
| 426 | { |
| 427 | return type == PTR_TO_SOCKET || |
| 428 | type == PTR_TO_SOCKET_OR_NULL || |
| 429 | type == PTR_TO_TCP_SOCK || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 430 | type == PTR_TO_TCP_SOCK_OR_NULL || |
| 431 | type == PTR_TO_MEM || |
| 432 | type == PTR_TO_MEM_OR_NULL; |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 435 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 436 | { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 437 | return type == ARG_PTR_TO_SOCK_COMMON; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 440 | static bool arg_type_may_be_null(enum bpf_arg_type type) |
| 441 | { |
| 442 | return type == ARG_PTR_TO_MAP_VALUE_OR_NULL || |
| 443 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 444 | type == ARG_PTR_TO_CTX_OR_NULL || |
| 445 | type == ARG_PTR_TO_SOCKET_OR_NULL || |
| 446 | type == ARG_PTR_TO_ALLOC_MEM_OR_NULL; |
| 447 | } |
| 448 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 449 | /* Determine whether the function releases some resources allocated by another |
| 450 | * function call. The first reference type argument will be assumed to be |
| 451 | * released by release_reference(). |
| 452 | */ |
| 453 | static bool is_release_function(enum bpf_func_id func_id) |
| 454 | { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 455 | return func_id == BPF_FUNC_sk_release || |
| 456 | func_id == BPF_FUNC_ringbuf_submit || |
| 457 | func_id == BPF_FUNC_ringbuf_discard; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 460 | 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] | 461 | { |
| 462 | return func_id == BPF_FUNC_sk_lookup_tcp || |
Lorenz Bauer | edbf8c0 | 2019-03-22 09:54:01 +0800 | [diff] [blame] | 463 | func_id == BPF_FUNC_sk_lookup_udp || |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 464 | func_id == BPF_FUNC_skc_lookup_tcp || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 465 | func_id == BPF_FUNC_map_lookup_elem || |
| 466 | func_id == BPF_FUNC_ringbuf_reserve; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static bool is_acquire_function(enum bpf_func_id func_id, |
| 470 | const struct bpf_map *map) |
| 471 | { |
| 472 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; |
| 473 | |
| 474 | if (func_id == BPF_FUNC_sk_lookup_tcp || |
| 475 | func_id == BPF_FUNC_sk_lookup_udp || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 476 | func_id == BPF_FUNC_skc_lookup_tcp || |
| 477 | func_id == BPF_FUNC_ringbuf_reserve) |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 478 | return true; |
| 479 | |
| 480 | if (func_id == BPF_FUNC_map_lookup_elem && |
| 481 | (map_type == BPF_MAP_TYPE_SOCKMAP || |
| 482 | map_type == BPF_MAP_TYPE_SOCKHASH)) |
| 483 | return true; |
| 484 | |
| 485 | return false; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 488 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
| 489 | { |
| 490 | return func_id == BPF_FUNC_tcp_sock || |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 491 | func_id == BPF_FUNC_sk_fullsock || |
| 492 | func_id == BPF_FUNC_skc_to_tcp_sock || |
| 493 | func_id == BPF_FUNC_skc_to_tcp6_sock || |
| 494 | func_id == BPF_FUNC_skc_to_udp6_sock || |
| 495 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || |
| 496 | func_id == BPF_FUNC_skc_to_tcp_request_sock; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 499 | /* string representation of 'enum bpf_reg_type' */ |
| 500 | static const char * const reg_type_str[] = { |
| 501 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 502 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 503 | [PTR_TO_CTX] = "ctx", |
| 504 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 505 | [PTR_TO_MAP_VALUE] = "map_value", |
| 506 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 507 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 508 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 509 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 510 | [PTR_TO_PACKET_END] = "pkt_end", |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 511 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 512 | [PTR_TO_SOCKET] = "sock", |
| 513 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 514 | [PTR_TO_SOCK_COMMON] = "sock_common", |
| 515 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 516 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
| 517 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 518 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 519 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 520 | [PTR_TO_BTF_ID] = "ptr_", |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 521 | [PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_", |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 522 | [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 523 | [PTR_TO_MEM] = "mem", |
| 524 | [PTR_TO_MEM_OR_NULL] = "mem_or_null", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 525 | [PTR_TO_RDONLY_BUF] = "rdonly_buf", |
| 526 | [PTR_TO_RDONLY_BUF_OR_NULL] = "rdonly_buf_or_null", |
| 527 | [PTR_TO_RDWR_BUF] = "rdwr_buf", |
| 528 | [PTR_TO_RDWR_BUF_OR_NULL] = "rdwr_buf_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 529 | }; |
| 530 | |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 531 | static char slot_type_char[] = { |
| 532 | [STACK_INVALID] = '?', |
| 533 | [STACK_SPILL] = 'r', |
| 534 | [STACK_MISC] = 'm', |
| 535 | [STACK_ZERO] = '0', |
| 536 | }; |
| 537 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 538 | static void print_liveness(struct bpf_verifier_env *env, |
| 539 | enum bpf_reg_liveness live) |
| 540 | { |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 541 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 542 | verbose(env, "_"); |
| 543 | if (live & REG_LIVE_READ) |
| 544 | verbose(env, "r"); |
| 545 | if (live & REG_LIVE_WRITTEN) |
| 546 | verbose(env, "w"); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 547 | if (live & REG_LIVE_DONE) |
| 548 | verbose(env, "D"); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 551 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 552 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 553 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 554 | struct bpf_verifier_state *cur = env->cur_state; |
| 555 | |
| 556 | return cur->frame[reg->frameno]; |
| 557 | } |
| 558 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 559 | const char *kernel_type_name(u32 id) |
| 560 | { |
| 561 | return btf_name_by_offset(btf_vmlinux, |
| 562 | btf_type_by_id(btf_vmlinux, id)->name_off); |
| 563 | } |
| 564 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 565 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 566 | const struct bpf_func_state *state) |
| 567 | { |
| 568 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 569 | enum bpf_reg_type t; |
| 570 | int i; |
| 571 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 572 | if (state->frameno) |
| 573 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 574 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 575 | reg = &state->regs[i]; |
| 576 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 577 | if (t == NOT_INIT) |
| 578 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 579 | verbose(env, " R%d", i); |
| 580 | print_liveness(env, reg->live); |
| 581 | verbose(env, "=%s", reg_type_str[t]); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 582 | if (t == SCALAR_VALUE && reg->precise) |
| 583 | verbose(env, "P"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 584 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 585 | tnum_is_const(reg->var_off)) { |
| 586 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 587 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 588 | } else { |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 589 | if (t == PTR_TO_BTF_ID || |
| 590 | t == PTR_TO_BTF_ID_OR_NULL || |
| 591 | t == PTR_TO_PERCPU_BTF_ID) |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 592 | verbose(env, "%s", kernel_type_name(reg->btf_id)); |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 593 | verbose(env, "(id=%d", reg->id); |
| 594 | if (reg_type_may_be_refcounted_or_null(t)) |
| 595 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 596 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 597 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 598 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 599 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 600 | else if (t == CONST_PTR_TO_MAP || |
| 601 | t == PTR_TO_MAP_VALUE || |
| 602 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 603 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 604 | reg->map_ptr->key_size, |
| 605 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 606 | if (tnum_is_const(reg->var_off)) { |
| 607 | /* Typically an immediate SCALAR_VALUE, but |
| 608 | * could be a pointer whose offset is too big |
| 609 | * for reg->off |
| 610 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 611 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 612 | } else { |
| 613 | if (reg->smin_value != reg->umin_value && |
| 614 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 615 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 616 | (long long)reg->smin_value); |
| 617 | if (reg->smax_value != reg->umax_value && |
| 618 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 619 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 620 | (long long)reg->smax_value); |
| 621 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 622 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 623 | (unsigned long long)reg->umin_value); |
| 624 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 625 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 626 | (unsigned long long)reg->umax_value); |
| 627 | if (!tnum_is_unknown(reg->var_off)) { |
| 628 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 629 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 630 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 631 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 632 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 633 | if (reg->s32_min_value != reg->smin_value && |
| 634 | reg->s32_min_value != S32_MIN) |
| 635 | verbose(env, ",s32_min_value=%d", |
| 636 | (int)(reg->s32_min_value)); |
| 637 | if (reg->s32_max_value != reg->smax_value && |
| 638 | reg->s32_max_value != S32_MAX) |
| 639 | verbose(env, ",s32_max_value=%d", |
| 640 | (int)(reg->s32_max_value)); |
| 641 | if (reg->u32_min_value != reg->umin_value && |
| 642 | reg->u32_min_value != U32_MIN) |
| 643 | verbose(env, ",u32_min_value=%d", |
| 644 | (int)(reg->u32_min_value)); |
| 645 | if (reg->u32_max_value != reg->umax_value && |
| 646 | reg->u32_max_value != U32_MAX) |
| 647 | verbose(env, ",u32_max_value=%d", |
| 648 | (int)(reg->u32_max_value)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 649 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 650 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 651 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 652 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 653 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 654 | char types_buf[BPF_REG_SIZE + 1]; |
| 655 | bool valid = false; |
| 656 | int j; |
| 657 | |
| 658 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 659 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 660 | valid = true; |
| 661 | types_buf[j] = slot_type_char[ |
| 662 | state->stack[i].slot_type[j]]; |
| 663 | } |
| 664 | types_buf[BPF_REG_SIZE] = 0; |
| 665 | if (!valid) |
| 666 | continue; |
| 667 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 668 | print_liveness(env, state->stack[i].spilled_ptr.live); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 669 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
| 670 | reg = &state->stack[i].spilled_ptr; |
| 671 | t = reg->type; |
| 672 | verbose(env, "=%s", reg_type_str[t]); |
| 673 | if (t == SCALAR_VALUE && reg->precise) |
| 674 | verbose(env, "P"); |
| 675 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) |
| 676 | verbose(env, "%lld", reg->var_off.value + reg->off); |
| 677 | } else { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 678 | verbose(env, "=%s", types_buf); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 679 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 680 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 681 | if (state->acquired_refs && state->refs[0].id) { |
| 682 | verbose(env, " refs=%d", state->refs[0].id); |
| 683 | for (i = 1; i < state->acquired_refs; i++) |
| 684 | if (state->refs[i].id) |
| 685 | verbose(env, ",%d", state->refs[i].id); |
| 686 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 687 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 690 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 691 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ |
| 692 | const struct bpf_func_state *src) \ |
| 693 | { \ |
| 694 | if (!src->FIELD) \ |
| 695 | return 0; \ |
| 696 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ |
| 697 | /* internal bug, make state invalid to reject the program */ \ |
| 698 | memset(dst, 0, sizeof(*dst)); \ |
| 699 | return -EFAULT; \ |
| 700 | } \ |
| 701 | memcpy(dst->FIELD, src->FIELD, \ |
| 702 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ |
| 703 | return 0; \ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 704 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 705 | /* copy_reference_state() */ |
| 706 | COPY_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 707 | /* copy_stack_state() */ |
| 708 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 709 | #undef COPY_STATE_FN |
| 710 | |
| 711 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 712 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ |
| 713 | bool copy_old) \ |
| 714 | { \ |
| 715 | u32 old_size = state->COUNT; \ |
| 716 | struct bpf_##NAME##_state *new_##FIELD; \ |
| 717 | int slot = size / SIZE; \ |
| 718 | \ |
| 719 | if (size <= old_size || !size) { \ |
| 720 | if (copy_old) \ |
| 721 | return 0; \ |
| 722 | state->COUNT = slot * SIZE; \ |
| 723 | if (!size && old_size) { \ |
| 724 | kfree(state->FIELD); \ |
| 725 | state->FIELD = NULL; \ |
| 726 | } \ |
| 727 | return 0; \ |
| 728 | } \ |
| 729 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ |
| 730 | GFP_KERNEL); \ |
| 731 | if (!new_##FIELD) \ |
| 732 | return -ENOMEM; \ |
| 733 | if (copy_old) { \ |
| 734 | if (state->FIELD) \ |
| 735 | memcpy(new_##FIELD, state->FIELD, \ |
| 736 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ |
| 737 | memset(new_##FIELD + old_size / SIZE, 0, \ |
| 738 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ |
| 739 | } \ |
| 740 | state->COUNT = slot * SIZE; \ |
| 741 | kfree(state->FIELD); \ |
| 742 | state->FIELD = new_##FIELD; \ |
| 743 | return 0; \ |
| 744 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 745 | /* realloc_reference_state() */ |
| 746 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 747 | /* realloc_stack_state() */ |
| 748 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 749 | #undef REALLOC_STATE_FN |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 750 | |
| 751 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 752 | * make it consume minimal amount of memory. check_stack_write() access from |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 753 | * the program calls into realloc_func_state() to grow the stack size. |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 754 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 755 | * which realloc_stack_state() copies over. It points to previous |
| 756 | * bpf_verifier_state which is never reallocated. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 757 | */ |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 758 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
| 759 | int refs_size, bool copy_old) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 760 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 761 | int err = realloc_reference_state(state, refs_size, copy_old); |
| 762 | if (err) |
| 763 | return err; |
| 764 | return realloc_stack_state(state, stack_size, copy_old); |
| 765 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 766 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 767 | /* Acquire a pointer id from the env and update the state->refs to include |
| 768 | * this new pointer reference. |
| 769 | * On success, returns a valid pointer id to associate with the register |
| 770 | * On failure, returns a negative errno. |
| 771 | */ |
| 772 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 773 | { |
| 774 | struct bpf_func_state *state = cur_func(env); |
| 775 | int new_ofs = state->acquired_refs; |
| 776 | int id, err; |
| 777 | |
| 778 | err = realloc_reference_state(state, state->acquired_refs + 1, true); |
| 779 | if (err) |
| 780 | return err; |
| 781 | id = ++env->id_gen; |
| 782 | state->refs[new_ofs].id = id; |
| 783 | state->refs[new_ofs].insn_idx = insn_idx; |
| 784 | |
| 785 | return id; |
| 786 | } |
| 787 | |
| 788 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 789 | 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] | 790 | { |
| 791 | int i, last_idx; |
| 792 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 793 | last_idx = state->acquired_refs - 1; |
| 794 | for (i = 0; i < state->acquired_refs; i++) { |
| 795 | if (state->refs[i].id == ptr_id) { |
| 796 | if (last_idx && i != last_idx) |
| 797 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 798 | sizeof(*state->refs)); |
| 799 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 800 | state->acquired_refs--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 801 | return 0; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 802 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 803 | } |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 804 | return -EINVAL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | static int transfer_reference_state(struct bpf_func_state *dst, |
| 808 | struct bpf_func_state *src) |
| 809 | { |
| 810 | int err = realloc_reference_state(dst, src->acquired_refs, false); |
| 811 | if (err) |
| 812 | return err; |
| 813 | err = copy_reference_state(dst, src); |
| 814 | if (err) |
| 815 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 816 | return 0; |
| 817 | } |
| 818 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 819 | static void free_func_state(struct bpf_func_state *state) |
| 820 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 821 | if (!state) |
| 822 | return; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 823 | kfree(state->refs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 824 | kfree(state->stack); |
| 825 | kfree(state); |
| 826 | } |
| 827 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 828 | static void clear_jmp_history(struct bpf_verifier_state *state) |
| 829 | { |
| 830 | kfree(state->jmp_history); |
| 831 | state->jmp_history = NULL; |
| 832 | state->jmp_history_cnt = 0; |
| 833 | } |
| 834 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 835 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 836 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 837 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 838 | int i; |
| 839 | |
| 840 | for (i = 0; i <= state->curframe; i++) { |
| 841 | free_func_state(state->frame[i]); |
| 842 | state->frame[i] = NULL; |
| 843 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 844 | clear_jmp_history(state); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 845 | if (free_self) |
| 846 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /* copy verifier state from src to dst growing dst stack space |
| 850 | * when necessary to accommodate larger src stack |
| 851 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 852 | static int copy_func_state(struct bpf_func_state *dst, |
| 853 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 854 | { |
| 855 | int err; |
| 856 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 857 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
| 858 | false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 859 | if (err) |
| 860 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 861 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 862 | err = copy_reference_state(dst, src); |
| 863 | if (err) |
| 864 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 865 | return copy_stack_state(dst, src); |
| 866 | } |
| 867 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 868 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 869 | const struct bpf_verifier_state *src) |
| 870 | { |
| 871 | struct bpf_func_state *dst; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 872 | u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 873 | int i, err; |
| 874 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 875 | if (dst_state->jmp_history_cnt < src->jmp_history_cnt) { |
| 876 | kfree(dst_state->jmp_history); |
| 877 | dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER); |
| 878 | if (!dst_state->jmp_history) |
| 879 | return -ENOMEM; |
| 880 | } |
| 881 | memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz); |
| 882 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
| 883 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 884 | /* if dst has more stack frames then src frame, free them */ |
| 885 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 886 | free_func_state(dst_state->frame[i]); |
| 887 | dst_state->frame[i] = NULL; |
| 888 | } |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 889 | dst_state->speculative = src->speculative; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 890 | dst_state->curframe = src->curframe; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 891 | dst_state->active_spin_lock = src->active_spin_lock; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 892 | dst_state->branches = src->branches; |
| 893 | dst_state->parent = src->parent; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 894 | dst_state->first_insn_idx = src->first_insn_idx; |
| 895 | dst_state->last_insn_idx = src->last_insn_idx; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 896 | for (i = 0; i <= src->curframe; i++) { |
| 897 | dst = dst_state->frame[i]; |
| 898 | if (!dst) { |
| 899 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 900 | if (!dst) |
| 901 | return -ENOMEM; |
| 902 | dst_state->frame[i] = dst; |
| 903 | } |
| 904 | err = copy_func_state(dst, src->frame[i]); |
| 905 | if (err) |
| 906 | return err; |
| 907 | } |
| 908 | return 0; |
| 909 | } |
| 910 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 911 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
| 912 | { |
| 913 | while (st) { |
| 914 | u32 br = --st->branches; |
| 915 | |
| 916 | /* WARN_ON(br > 1) technically makes sense here, |
| 917 | * but see comment in push_stack(), hence: |
| 918 | */ |
| 919 | WARN_ONCE((int)br < 0, |
| 920 | "BUG update_branch_counts:branches_to_explore=%d\n", |
| 921 | br); |
| 922 | if (br) |
| 923 | break; |
| 924 | st = st->parent; |
| 925 | } |
| 926 | } |
| 927 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 928 | 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] | 929 | int *insn_idx, bool pop_log) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 930 | { |
| 931 | struct bpf_verifier_state *cur = env->cur_state; |
| 932 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 933 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 934 | |
| 935 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 936 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 937 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 938 | if (cur) { |
| 939 | err = copy_verifier_state(cur, &head->st); |
| 940 | if (err) |
| 941 | return err; |
| 942 | } |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 943 | if (pop_log) |
| 944 | bpf_vlog_reset(&env->log, head->log_pos); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 945 | if (insn_idx) |
| 946 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 947 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 948 | *prev_insn_idx = head->prev_insn_idx; |
| 949 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 950 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 951 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 952 | env->head = elem; |
| 953 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 954 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 957 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 958 | int insn_idx, int prev_insn_idx, |
| 959 | bool speculative) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 960 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 961 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 962 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 963 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 964 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 965 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 966 | if (!elem) |
| 967 | goto err; |
| 968 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 969 | elem->insn_idx = insn_idx; |
| 970 | elem->prev_insn_idx = prev_insn_idx; |
| 971 | elem->next = env->head; |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 972 | elem->log_pos = env->log.len_used; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 973 | env->head = elem; |
| 974 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 975 | err = copy_verifier_state(&elem->st, cur); |
| 976 | if (err) |
| 977 | goto err; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 978 | elem->st.speculative |= speculative; |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 979 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
| 980 | verbose(env, "The sequence of %d jumps is too complex.\n", |
| 981 | env->stack_size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 982 | goto err; |
| 983 | } |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 984 | if (elem->st.parent) { |
| 985 | ++elem->st.parent->branches; |
| 986 | /* WARN_ON(branches > 2) technically makes sense here, |
| 987 | * but |
| 988 | * 1. speculative states will bump 'branches' for non-branch |
| 989 | * instructions |
| 990 | * 2. is_state_visited() heuristics may decide not to create |
| 991 | * a new state for a sequence of branches and all such current |
| 992 | * and cloned states will be pointing to a single parent state |
| 993 | * which might have large 'branches' count. |
| 994 | */ |
| 995 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 996 | return &elem->st; |
| 997 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 998 | free_verifier_state(env->cur_state, true); |
| 999 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1000 | /* pop all elements and return */ |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 1001 | while (!pop_stack(env, NULL, NULL, false)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1002 | return NULL; |
| 1003 | } |
| 1004 | |
| 1005 | #define CALLER_SAVED_REGS 6 |
| 1006 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 1007 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 1008 | }; |
| 1009 | |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1010 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 1011 | struct bpf_reg_state *reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1012 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 1013 | /* This helper doesn't clear reg->id */ |
| 1014 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1015 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1016 | reg->var_off = tnum_const(imm); |
| 1017 | reg->smin_value = (s64)imm; |
| 1018 | reg->smax_value = (s64)imm; |
| 1019 | reg->umin_value = imm; |
| 1020 | reg->umax_value = imm; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1021 | |
| 1022 | reg->s32_min_value = (s32)imm; |
| 1023 | reg->s32_max_value = (s32)imm; |
| 1024 | reg->u32_min_value = (u32)imm; |
| 1025 | reg->u32_max_value = (u32)imm; |
| 1026 | } |
| 1027 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 1028 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 1029 | * known to have the value @imm. |
| 1030 | */ |
| 1031 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 1032 | { |
| 1033 | /* Clear id, off, and union(map_ptr, range) */ |
| 1034 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 1035 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
| 1036 | ___mark_reg_known(reg, imm); |
| 1037 | } |
| 1038 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1039 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
| 1040 | { |
| 1041 | reg->var_off = tnum_const_subreg(reg->var_off, imm); |
| 1042 | reg->s32_min_value = (s32)imm; |
| 1043 | reg->s32_max_value = (s32)imm; |
| 1044 | reg->u32_min_value = (u32)imm; |
| 1045 | reg->u32_max_value = (u32)imm; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1046 | } |
| 1047 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1048 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 1049 | * used only on registers holding a pointer type. |
| 1050 | */ |
| 1051 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 1052 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1053 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1054 | } |
| 1055 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1056 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 1057 | { |
| 1058 | __mark_reg_known(reg, 0); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1059 | reg->type = SCALAR_VALUE; |
| 1060 | } |
| 1061 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1062 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 1063 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1064 | { |
| 1065 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1066 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1067 | /* Something bad happened, let's kill all regs */ |
| 1068 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1069 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1070 | return; |
| 1071 | } |
| 1072 | __mark_reg_known_zero(regs + regno); |
| 1073 | } |
| 1074 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1075 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 1076 | { |
| 1077 | return type_is_pkt_pointer(reg->type); |
| 1078 | } |
| 1079 | |
| 1080 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 1081 | { |
| 1082 | return reg_is_pkt_pointer(reg) || |
| 1083 | reg->type == PTR_TO_PACKET_END; |
| 1084 | } |
| 1085 | |
| 1086 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 1087 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 1088 | enum bpf_reg_type which) |
| 1089 | { |
| 1090 | /* The register can already have a range from prior markings. |
| 1091 | * This is fine as long as it hasn't been advanced from its |
| 1092 | * origin. |
| 1093 | */ |
| 1094 | return reg->type == which && |
| 1095 | reg->id == 0 && |
| 1096 | reg->off == 0 && |
| 1097 | tnum_equals_const(reg->var_off, 0); |
| 1098 | } |
| 1099 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1100 | /* Reset the min/max bounds of a register */ |
| 1101 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 1102 | { |
| 1103 | reg->smin_value = S64_MIN; |
| 1104 | reg->smax_value = S64_MAX; |
| 1105 | reg->umin_value = 0; |
| 1106 | reg->umax_value = U64_MAX; |
| 1107 | |
| 1108 | reg->s32_min_value = S32_MIN; |
| 1109 | reg->s32_max_value = S32_MAX; |
| 1110 | reg->u32_min_value = 0; |
| 1111 | reg->u32_max_value = U32_MAX; |
| 1112 | } |
| 1113 | |
| 1114 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) |
| 1115 | { |
| 1116 | reg->smin_value = S64_MIN; |
| 1117 | reg->smax_value = S64_MAX; |
| 1118 | reg->umin_value = 0; |
| 1119 | reg->umax_value = U64_MAX; |
| 1120 | } |
| 1121 | |
| 1122 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) |
| 1123 | { |
| 1124 | reg->s32_min_value = S32_MIN; |
| 1125 | reg->s32_max_value = S32_MAX; |
| 1126 | reg->u32_min_value = 0; |
| 1127 | reg->u32_max_value = U32_MAX; |
| 1128 | } |
| 1129 | |
| 1130 | static void __update_reg32_bounds(struct bpf_reg_state *reg) |
| 1131 | { |
| 1132 | struct tnum var32_off = tnum_subreg(reg->var_off); |
| 1133 | |
| 1134 | /* min signed is max(sign bit) | min(other bits) */ |
| 1135 | reg->s32_min_value = max_t(s32, reg->s32_min_value, |
| 1136 | var32_off.value | (var32_off.mask & S32_MIN)); |
| 1137 | /* max signed is min(sign bit) | max(other bits) */ |
| 1138 | reg->s32_max_value = min_t(s32, reg->s32_max_value, |
| 1139 | var32_off.value | (var32_off.mask & S32_MAX)); |
| 1140 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); |
| 1141 | reg->u32_max_value = min(reg->u32_max_value, |
| 1142 | (u32)(var32_off.value | var32_off.mask)); |
| 1143 | } |
| 1144 | |
| 1145 | static void __update_reg64_bounds(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1146 | { |
| 1147 | /* min signed is max(sign bit) | min(other bits) */ |
| 1148 | reg->smin_value = max_t(s64, reg->smin_value, |
| 1149 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 1150 | /* max signed is min(sign bit) | max(other bits) */ |
| 1151 | reg->smax_value = min_t(s64, reg->smax_value, |
| 1152 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 1153 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 1154 | reg->umax_value = min(reg->umax_value, |
| 1155 | reg->var_off.value | reg->var_off.mask); |
| 1156 | } |
| 1157 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1158 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 1159 | { |
| 1160 | __update_reg32_bounds(reg); |
| 1161 | __update_reg64_bounds(reg); |
| 1162 | } |
| 1163 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1164 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1165 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
| 1166 | { |
| 1167 | /* Learn sign from signed bounds. |
| 1168 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 1169 | * are the same, so combine. This works even in the negative case, e.g. |
| 1170 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 1171 | */ |
| 1172 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { |
| 1173 | reg->s32_min_value = reg->u32_min_value = |
| 1174 | max_t(u32, reg->s32_min_value, reg->u32_min_value); |
| 1175 | reg->s32_max_value = reg->u32_max_value = |
| 1176 | min_t(u32, reg->s32_max_value, reg->u32_max_value); |
| 1177 | return; |
| 1178 | } |
| 1179 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 1180 | * boundary, so we must be careful. |
| 1181 | */ |
| 1182 | if ((s32)reg->u32_max_value >= 0) { |
| 1183 | /* Positive. We can't learn anything from the smin, but smax |
| 1184 | * is positive, hence safe. |
| 1185 | */ |
| 1186 | reg->s32_min_value = reg->u32_min_value; |
| 1187 | reg->s32_max_value = reg->u32_max_value = |
| 1188 | min_t(u32, reg->s32_max_value, reg->u32_max_value); |
| 1189 | } else if ((s32)reg->u32_min_value < 0) { |
| 1190 | /* Negative. We can't learn anything from the smax, but smin |
| 1191 | * is negative, hence safe. |
| 1192 | */ |
| 1193 | reg->s32_min_value = reg->u32_min_value = |
| 1194 | max_t(u32, reg->s32_min_value, reg->u32_min_value); |
| 1195 | reg->s32_max_value = reg->u32_max_value; |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1200 | { |
| 1201 | /* Learn sign from signed bounds. |
| 1202 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 1203 | * are the same, so combine. This works even in the negative case, e.g. |
| 1204 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 1205 | */ |
| 1206 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 1207 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1208 | reg->umin_value); |
| 1209 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1210 | reg->umax_value); |
| 1211 | return; |
| 1212 | } |
| 1213 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 1214 | * boundary, so we must be careful. |
| 1215 | */ |
| 1216 | if ((s64)reg->umax_value >= 0) { |
| 1217 | /* Positive. We can't learn anything from the smin, but smax |
| 1218 | * is positive, hence safe. |
| 1219 | */ |
| 1220 | reg->smin_value = reg->umin_value; |
| 1221 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1222 | reg->umax_value); |
| 1223 | } else if ((s64)reg->umin_value < 0) { |
| 1224 | /* Negative. We can't learn anything from the smax, but smin |
| 1225 | * is negative, hence safe. |
| 1226 | */ |
| 1227 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1228 | reg->umin_value); |
| 1229 | reg->smax_value = reg->umax_value; |
| 1230 | } |
| 1231 | } |
| 1232 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1233 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 1234 | { |
| 1235 | __reg32_deduce_bounds(reg); |
| 1236 | __reg64_deduce_bounds(reg); |
| 1237 | } |
| 1238 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1239 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 1240 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 1241 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1242 | struct tnum var64_off = tnum_intersect(reg->var_off, |
| 1243 | tnum_range(reg->umin_value, |
| 1244 | reg->umax_value)); |
| 1245 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), |
| 1246 | tnum_range(reg->u32_min_value, |
| 1247 | reg->u32_max_value)); |
| 1248 | |
| 1249 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1250 | } |
| 1251 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1252 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1253 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1254 | reg->umin_value = reg->u32_min_value; |
| 1255 | reg->umax_value = reg->u32_max_value; |
| 1256 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds |
| 1257 | * but must be positive otherwise set to worse case bounds |
| 1258 | * and refine later from tnum. |
| 1259 | */ |
John Fastabend | 3a71dc3 | 2020-05-29 10:28:40 -0700 | [diff] [blame] | 1260 | if (reg->s32_min_value >= 0 && reg->s32_max_value >= 0) |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1261 | reg->smax_value = reg->s32_max_value; |
| 1262 | else |
| 1263 | reg->smax_value = U32_MAX; |
John Fastabend | 3a71dc3 | 2020-05-29 10:28:40 -0700 | [diff] [blame] | 1264 | if (reg->s32_min_value >= 0) |
| 1265 | reg->smin_value = reg->s32_min_value; |
| 1266 | else |
| 1267 | reg->smin_value = 0; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) |
| 1271 | { |
| 1272 | /* special case when 64-bit register has upper 32-bit register |
| 1273 | * zeroed. Typically happens after zext or <<32, >>32 sequence |
| 1274 | * allowing us to use 32-bit bounds directly, |
| 1275 | */ |
| 1276 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { |
| 1277 | __reg_assign_32_into_64(reg); |
| 1278 | } else { |
| 1279 | /* Otherwise the best we can do is push lower 32bit known and |
| 1280 | * unknown bits into register (var_off set from jmp logic) |
| 1281 | * then learn as much as possible from the 64-bit tnum |
| 1282 | * known and unknown bits. The previous smin/smax bounds are |
| 1283 | * invalid here because of jmp32 compare so mark them unknown |
| 1284 | * so they do not impact tnum bounds calculation. |
| 1285 | */ |
| 1286 | __mark_reg64_unbounded(reg); |
| 1287 | __update_reg_bounds(reg); |
| 1288 | } |
| 1289 | |
| 1290 | /* Intersecting with the old var_off might have improved our bounds |
| 1291 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 1292 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 1293 | */ |
| 1294 | __reg_deduce_bounds(reg); |
| 1295 | __reg_bound_offset(reg); |
| 1296 | __update_reg_bounds(reg); |
| 1297 | } |
| 1298 | |
| 1299 | static bool __reg64_bound_s32(s64 a) |
| 1300 | { |
| 1301 | if (a > S32_MIN && a < S32_MAX) |
| 1302 | return true; |
| 1303 | return false; |
| 1304 | } |
| 1305 | |
| 1306 | static bool __reg64_bound_u32(u64 a) |
| 1307 | { |
| 1308 | if (a > U32_MIN && a < U32_MAX) |
| 1309 | return true; |
| 1310 | return false; |
| 1311 | } |
| 1312 | |
| 1313 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) |
| 1314 | { |
| 1315 | __mark_reg32_unbounded(reg); |
| 1316 | |
| 1317 | if (__reg64_bound_s32(reg->smin_value)) |
| 1318 | reg->s32_min_value = (s32)reg->smin_value; |
| 1319 | if (__reg64_bound_s32(reg->smax_value)) |
| 1320 | reg->s32_max_value = (s32)reg->smax_value; |
| 1321 | if (__reg64_bound_u32(reg->umin_value)) |
| 1322 | reg->u32_min_value = (u32)reg->umin_value; |
| 1323 | if (__reg64_bound_u32(reg->umax_value)) |
| 1324 | reg->u32_max_value = (u32)reg->umax_value; |
| 1325 | |
| 1326 | /* Intersecting with the old var_off might have improved our bounds |
| 1327 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 1328 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 1329 | */ |
| 1330 | __reg_deduce_bounds(reg); |
| 1331 | __reg_bound_offset(reg); |
| 1332 | __update_reg_bounds(reg); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1333 | } |
| 1334 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1335 | /* Mark a register as having a completely unknown (scalar) value. */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1336 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
| 1337 | struct bpf_reg_state *reg) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1338 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 1339 | /* |
| 1340 | * Clear type, id, off, and union(map_ptr, range) and |
| 1341 | * padding between 'type' and union |
| 1342 | */ |
| 1343 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1344 | reg->type = SCALAR_VALUE; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1345 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1346 | reg->frameno = 0; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 1347 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1348 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1349 | } |
| 1350 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1351 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 1352 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1353 | { |
| 1354 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1355 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 1356 | /* Something bad happened, let's kill all regs except FP */ |
| 1357 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1358 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1359 | return; |
| 1360 | } |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1361 | __mark_reg_unknown(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1362 | } |
| 1363 | |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1364 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 1365 | struct bpf_reg_state *reg) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1366 | { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1367 | __mark_reg_unknown(env, reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1368 | reg->type = NOT_INIT; |
| 1369 | } |
| 1370 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1371 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 1372 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 1373 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1374 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1375 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 1376 | /* Something bad happened, let's kill all regs except FP */ |
| 1377 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1378 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1379 | return; |
| 1380 | } |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1381 | __mark_reg_not_init(env, regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 1384 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
| 1385 | struct bpf_reg_state *regs, u32 regno, |
| 1386 | enum bpf_reg_type reg_type, u32 btf_id) |
| 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; |
| 1394 | regs[regno].btf_id = btf_id; |
| 1395 | } |
| 1396 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1397 | #define DEF_NOT_SUBREG (0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1398 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1399 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1400 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1401 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1402 | int i; |
| 1403 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1404 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1405 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1406 | regs[i].live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1407 | regs[i].parent = NULL; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1408 | regs[i].subreg_def = DEF_NOT_SUBREG; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1409 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1410 | |
| 1411 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1412 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1413 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1414 | regs[BPF_REG_FP].frameno = state->frameno; |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 1415 | } |
| 1416 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1417 | #define BPF_MAIN_FUNC (-1) |
| 1418 | static void init_func_state(struct bpf_verifier_env *env, |
| 1419 | struct bpf_func_state *state, |
| 1420 | int callsite, int frameno, int subprogno) |
| 1421 | { |
| 1422 | state->callsite = callsite; |
| 1423 | state->frameno = frameno; |
| 1424 | state->subprogno = subprogno; |
| 1425 | init_reg_state(env, state); |
| 1426 | } |
| 1427 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1428 | enum reg_arg_type { |
| 1429 | SRC_OP, /* register is used as source operand */ |
| 1430 | DST_OP, /* register is used as destination operand */ |
| 1431 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 1432 | }; |
| 1433 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1434 | static int cmp_subprogs(const void *a, const void *b) |
| 1435 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1436 | return ((struct bpf_subprog_info *)a)->start - |
| 1437 | ((struct bpf_subprog_info *)b)->start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 1441 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1442 | struct bpf_subprog_info *p; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1443 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1444 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 1445 | sizeof(env->subprog_info[0]), cmp_subprogs); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1446 | if (!p) |
| 1447 | return -ENOENT; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1448 | return p - env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1449 | |
| 1450 | } |
| 1451 | |
| 1452 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 1453 | { |
| 1454 | int insn_cnt = env->prog->len; |
| 1455 | int ret; |
| 1456 | |
| 1457 | if (off >= insn_cnt || off < 0) { |
| 1458 | verbose(env, "call to invalid destination\n"); |
| 1459 | return -EINVAL; |
| 1460 | } |
| 1461 | ret = find_subprog(env, off); |
| 1462 | if (ret >= 0) |
| 1463 | return 0; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1464 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1465 | verbose(env, "too many subprograms\n"); |
| 1466 | return -E2BIG; |
| 1467 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1468 | env->subprog_info[env->subprog_cnt++].start = off; |
| 1469 | sort(env->subprog_info, env->subprog_cnt, |
| 1470 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1471 | return 0; |
| 1472 | } |
| 1473 | |
| 1474 | static int check_subprogs(struct bpf_verifier_env *env) |
| 1475 | { |
| 1476 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1477 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1478 | struct bpf_insn *insn = env->prog->insnsi; |
| 1479 | int insn_cnt = env->prog->len; |
| 1480 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 1481 | /* Add entry function. */ |
| 1482 | ret = add_subprog(env, 0); |
| 1483 | if (ret < 0) |
| 1484 | return ret; |
| 1485 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1486 | /* determine subprog starts. The end is one before the next starts */ |
| 1487 | for (i = 0; i < insn_cnt; i++) { |
| 1488 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1489 | continue; |
| 1490 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1491 | continue; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 1492 | if (!env->bpf_capable) { |
| 1493 | verbose(env, |
| 1494 | "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] | 1495 | return -EPERM; |
| 1496 | } |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1497 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 1498 | if (ret < 0) |
| 1499 | return ret; |
| 1500 | } |
| 1501 | |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1502 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 1503 | * logic. 'subprog_cnt' should not be increased. |
| 1504 | */ |
| 1505 | subprog[env->subprog_cnt].start = insn_cnt; |
| 1506 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1507 | if (env->log.level & BPF_LOG_LEVEL2) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1508 | for (i = 0; i < env->subprog_cnt; i++) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1509 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1510 | |
| 1511 | /* now check that all jumps are within the same subprog */ |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1512 | subprog_start = subprog[cur_subprog].start; |
| 1513 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1514 | for (i = 0; i < insn_cnt; i++) { |
| 1515 | u8 code = insn[i].code; |
| 1516 | |
Maciej Fijalkowski | 7f6e431 | 2020-09-16 23:10:07 +0200 | [diff] [blame] | 1517 | if (code == (BPF_JMP | BPF_CALL) && |
| 1518 | insn[i].imm == BPF_FUNC_tail_call && |
| 1519 | insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1520 | subprog[cur_subprog].has_tail_call = true; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 1521 | if (BPF_CLASS(code) == BPF_LD && |
| 1522 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) |
| 1523 | subprog[cur_subprog].has_ld_abs = true; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 1524 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1525 | goto next; |
| 1526 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 1527 | goto next; |
| 1528 | off = i + insn[i].off + 1; |
| 1529 | if (off < subprog_start || off >= subprog_end) { |
| 1530 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 1531 | return -EINVAL; |
| 1532 | } |
| 1533 | next: |
| 1534 | if (i == subprog_end - 1) { |
| 1535 | /* to avoid fall-through from one subprog into another |
| 1536 | * the last insn of the subprog should be either exit |
| 1537 | * or unconditional jump back |
| 1538 | */ |
| 1539 | if (code != (BPF_JMP | BPF_EXIT) && |
| 1540 | code != (BPF_JMP | BPF_JA)) { |
| 1541 | verbose(env, "last insn is not an exit or jmp\n"); |
| 1542 | return -EINVAL; |
| 1543 | } |
| 1544 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1545 | cur_subprog++; |
| 1546 | if (cur_subprog < env->subprog_cnt) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1547 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1548 | } |
| 1549 | } |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1553 | /* Parentage chain of this register (or stack slot) should take care of all |
| 1554 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 1555 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1556 | static int mark_reg_read(struct bpf_verifier_env *env, |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1557 | const struct bpf_reg_state *state, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1558 | struct bpf_reg_state *parent, u8 flag) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1559 | { |
| 1560 | bool writes = parent == state->parent; /* Observe write marks */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1561 | int cnt = 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1562 | |
| 1563 | while (parent) { |
| 1564 | /* if read wasn't screened by an earlier write ... */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1565 | if (writes && state->live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1566 | break; |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 1567 | if (parent->live & REG_LIVE_DONE) { |
| 1568 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", |
| 1569 | reg_type_str[parent->type], |
| 1570 | parent->var_off.value, parent->off); |
| 1571 | return -EFAULT; |
| 1572 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1573 | /* The first condition is more likely to be true than the |
| 1574 | * second, checked it first. |
| 1575 | */ |
| 1576 | if ((parent->live & REG_LIVE_READ) == flag || |
| 1577 | parent->live & REG_LIVE_READ64) |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 1578 | /* The parentage chain never changes and |
| 1579 | * this parent was already marked as LIVE_READ. |
| 1580 | * There is no need to keep walking the chain again and |
| 1581 | * keep re-marking all parents as LIVE_READ. |
| 1582 | * This case happens when the same register is read |
| 1583 | * multiple times without writes into it in-between. |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1584 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
| 1585 | * then no need to set the weak REG_LIVE_READ32. |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 1586 | */ |
| 1587 | break; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1588 | /* ... then we depend on parent's value */ |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1589 | parent->live |= flag; |
| 1590 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ |
| 1591 | if (flag == REG_LIVE_READ64) |
| 1592 | parent->live &= ~REG_LIVE_READ32; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1593 | state = parent; |
| 1594 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1595 | writes = true; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1596 | cnt++; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1597 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1598 | |
| 1599 | if (env->longest_mark_read_walk < cnt) |
| 1600 | env->longest_mark_read_walk = cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1601 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1602 | } |
| 1603 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1604 | /* This function is supposed to be used by the following 32-bit optimization |
| 1605 | * code only. It returns TRUE if the source or destination register operates |
| 1606 | * on 64-bit, otherwise return FALSE. |
| 1607 | */ |
| 1608 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 1609 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) |
| 1610 | { |
| 1611 | u8 code, class, op; |
| 1612 | |
| 1613 | code = insn->code; |
| 1614 | class = BPF_CLASS(code); |
| 1615 | op = BPF_OP(code); |
| 1616 | if (class == BPF_JMP) { |
| 1617 | /* BPF_EXIT for "main" will reach here. Return TRUE |
| 1618 | * conservatively. |
| 1619 | */ |
| 1620 | if (op == BPF_EXIT) |
| 1621 | return true; |
| 1622 | if (op == BPF_CALL) { |
| 1623 | /* BPF to BPF call will reach here because of marking |
| 1624 | * caller saved clobber with DST_OP_NO_MARK for which we |
| 1625 | * don't care the register def because they are anyway |
| 1626 | * marked as NOT_INIT already. |
| 1627 | */ |
| 1628 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1629 | return false; |
| 1630 | /* Helper call will reach here because of arg type |
| 1631 | * check, conservatively return TRUE. |
| 1632 | */ |
| 1633 | if (t == SRC_OP) |
| 1634 | return true; |
| 1635 | |
| 1636 | return false; |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | if (class == BPF_ALU64 || class == BPF_JMP || |
| 1641 | /* BPF_END always use BPF_ALU class. */ |
| 1642 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) |
| 1643 | return true; |
| 1644 | |
| 1645 | if (class == BPF_ALU || class == BPF_JMP32) |
| 1646 | return false; |
| 1647 | |
| 1648 | if (class == BPF_LDX) { |
| 1649 | if (t != SRC_OP) |
| 1650 | return BPF_SIZE(code) == BPF_DW; |
| 1651 | /* LDX source must be ptr. */ |
| 1652 | return true; |
| 1653 | } |
| 1654 | |
| 1655 | if (class == BPF_STX) { |
| 1656 | if (reg->type != SCALAR_VALUE) |
| 1657 | return true; |
| 1658 | return BPF_SIZE(code) == BPF_DW; |
| 1659 | } |
| 1660 | |
| 1661 | if (class == BPF_LD) { |
| 1662 | u8 mode = BPF_MODE(code); |
| 1663 | |
| 1664 | /* LD_IMM64 */ |
| 1665 | if (mode == BPF_IMM) |
| 1666 | return true; |
| 1667 | |
| 1668 | /* Both LD_IND and LD_ABS return 32-bit data. */ |
| 1669 | if (t != SRC_OP) |
| 1670 | return false; |
| 1671 | |
| 1672 | /* Implicit ctx ptr. */ |
| 1673 | if (regno == BPF_REG_6) |
| 1674 | return true; |
| 1675 | |
| 1676 | /* Explicit source could be any width. */ |
| 1677 | return true; |
| 1678 | } |
| 1679 | |
| 1680 | if (class == BPF_ST) |
| 1681 | /* The only source register for BPF_ST is a ptr. */ |
| 1682 | return true; |
| 1683 | |
| 1684 | /* Conservatively return true at default. */ |
| 1685 | return true; |
| 1686 | } |
| 1687 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 1688 | /* Return TRUE if INSN doesn't have explicit value define. */ |
| 1689 | static bool insn_no_def(struct bpf_insn *insn) |
| 1690 | { |
| 1691 | u8 class = BPF_CLASS(insn->code); |
| 1692 | |
| 1693 | return (class == BPF_JMP || class == BPF_JMP32 || |
| 1694 | class == BPF_STX || class == BPF_ST); |
| 1695 | } |
| 1696 | |
| 1697 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ |
| 1698 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 1699 | { |
| 1700 | if (insn_no_def(insn)) |
| 1701 | return false; |
| 1702 | |
| 1703 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); |
| 1704 | } |
| 1705 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1706 | static void mark_insn_zext(struct bpf_verifier_env *env, |
| 1707 | struct bpf_reg_state *reg) |
| 1708 | { |
| 1709 | s32 def_idx = reg->subreg_def; |
| 1710 | |
| 1711 | if (def_idx == DEF_NOT_SUBREG) |
| 1712 | return; |
| 1713 | |
| 1714 | env->insn_aux_data[def_idx - 1].zext_dst = true; |
| 1715 | /* The dst will be zero extended, so won't be sub-register anymore. */ |
| 1716 | reg->subreg_def = DEF_NOT_SUBREG; |
| 1717 | } |
| 1718 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1719 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1720 | enum reg_arg_type t) |
| 1721 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1722 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1723 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1724 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1725 | struct bpf_reg_state *reg, *regs = state->regs; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1726 | bool rw64; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1727 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1728 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1729 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1730 | return -EINVAL; |
| 1731 | } |
| 1732 | |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1733 | reg = ®s[regno]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1734 | rw64 = is_reg64(env, insn, regno, reg, t); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1735 | if (t == SRC_OP) { |
| 1736 | /* check whether register used as source operand can be read */ |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1737 | if (reg->type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1738 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1739 | return -EACCES; |
| 1740 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1741 | /* 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] | 1742 | if (regno == BPF_REG_FP) |
| 1743 | return 0; |
| 1744 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1745 | if (rw64) |
| 1746 | mark_insn_zext(env, reg); |
| 1747 | |
| 1748 | return mark_reg_read(env, reg, reg->parent, |
| 1749 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1750 | } else { |
| 1751 | /* check whether register used as dest operand can be written to */ |
| 1752 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1753 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1754 | return -EACCES; |
| 1755 | } |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1756 | reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1757 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1758 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1759 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1760 | } |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1764 | /* for any branch, call, exit record the history of jmps in the given state */ |
| 1765 | static int push_jmp_history(struct bpf_verifier_env *env, |
| 1766 | struct bpf_verifier_state *cur) |
| 1767 | { |
| 1768 | u32 cnt = cur->jmp_history_cnt; |
| 1769 | struct bpf_idx_pair *p; |
| 1770 | |
| 1771 | cnt++; |
| 1772 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); |
| 1773 | if (!p) |
| 1774 | return -ENOMEM; |
| 1775 | p[cnt - 1].idx = env->insn_idx; |
| 1776 | p[cnt - 1].prev_idx = env->prev_insn_idx; |
| 1777 | cur->jmp_history = p; |
| 1778 | cur->jmp_history_cnt = cnt; |
| 1779 | return 0; |
| 1780 | } |
| 1781 | |
| 1782 | /* Backtrack one insn at a time. If idx is not at the top of recorded |
| 1783 | * history then previous instruction came from straight line execution. |
| 1784 | */ |
| 1785 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, |
| 1786 | u32 *history) |
| 1787 | { |
| 1788 | u32 cnt = *history; |
| 1789 | |
| 1790 | if (cnt && st->jmp_history[cnt - 1].idx == i) { |
| 1791 | i = st->jmp_history[cnt - 1].prev_idx; |
| 1792 | (*history)--; |
| 1793 | } else { |
| 1794 | i--; |
| 1795 | } |
| 1796 | return i; |
| 1797 | } |
| 1798 | |
| 1799 | /* For given verifier state backtrack_insn() is called from the last insn to |
| 1800 | * the first insn. Its purpose is to compute a bitmask of registers and |
| 1801 | * stack slots that needs precision in the parent verifier state. |
| 1802 | */ |
| 1803 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, |
| 1804 | u32 *reg_mask, u64 *stack_mask) |
| 1805 | { |
| 1806 | const struct bpf_insn_cbs cbs = { |
| 1807 | .cb_print = verbose, |
| 1808 | .private_data = env, |
| 1809 | }; |
| 1810 | struct bpf_insn *insn = env->prog->insnsi + idx; |
| 1811 | u8 class = BPF_CLASS(insn->code); |
| 1812 | u8 opcode = BPF_OP(insn->code); |
| 1813 | u8 mode = BPF_MODE(insn->code); |
| 1814 | u32 dreg = 1u << insn->dst_reg; |
| 1815 | u32 sreg = 1u << insn->src_reg; |
| 1816 | u32 spi; |
| 1817 | |
| 1818 | if (insn->code == 0) |
| 1819 | return 0; |
| 1820 | if (env->log.level & BPF_LOG_LEVEL) { |
| 1821 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
| 1822 | verbose(env, "%d: ", idx); |
| 1823 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
| 1824 | } |
| 1825 | |
| 1826 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 1827 | if (!(*reg_mask & dreg)) |
| 1828 | return 0; |
| 1829 | if (opcode == BPF_MOV) { |
| 1830 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1831 | /* dreg = sreg |
| 1832 | * dreg needs precision after this insn |
| 1833 | * sreg needs precision before this insn |
| 1834 | */ |
| 1835 | *reg_mask &= ~dreg; |
| 1836 | *reg_mask |= sreg; |
| 1837 | } else { |
| 1838 | /* dreg = K |
| 1839 | * dreg needs precision after this insn. |
| 1840 | * Corresponding register is already marked |
| 1841 | * as precise=true in this verifier state. |
| 1842 | * No further markings in parent are necessary |
| 1843 | */ |
| 1844 | *reg_mask &= ~dreg; |
| 1845 | } |
| 1846 | } else { |
| 1847 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1848 | /* dreg += sreg |
| 1849 | * both dreg and sreg need precision |
| 1850 | * before this insn |
| 1851 | */ |
| 1852 | *reg_mask |= sreg; |
| 1853 | } /* else dreg += K |
| 1854 | * dreg still needs precision before this insn |
| 1855 | */ |
| 1856 | } |
| 1857 | } else if (class == BPF_LDX) { |
| 1858 | if (!(*reg_mask & dreg)) |
| 1859 | return 0; |
| 1860 | *reg_mask &= ~dreg; |
| 1861 | |
| 1862 | /* scalars can only be spilled into stack w/o losing precision. |
| 1863 | * Load from any other memory can be zero extended. |
| 1864 | * The desire to keep that precision is already indicated |
| 1865 | * by 'precise' mark in corresponding register of this state. |
| 1866 | * No further tracking necessary. |
| 1867 | */ |
| 1868 | if (insn->src_reg != BPF_REG_FP) |
| 1869 | return 0; |
| 1870 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 1871 | return 0; |
| 1872 | |
| 1873 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. |
| 1874 | * that [fp - off] slot contains scalar that needs to be |
| 1875 | * tracked with precision |
| 1876 | */ |
| 1877 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 1878 | if (spi >= 64) { |
| 1879 | verbose(env, "BUG spi %d\n", spi); |
| 1880 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1881 | return -EFAULT; |
| 1882 | } |
| 1883 | *stack_mask |= 1ull << spi; |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 1884 | } else if (class == BPF_STX || class == BPF_ST) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1885 | if (*reg_mask & dreg) |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 1886 | /* stx & st shouldn't be using _scalar_ dst_reg |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1887 | * to access memory. It means backtracking |
| 1888 | * encountered a case of pointer subtraction. |
| 1889 | */ |
| 1890 | return -ENOTSUPP; |
| 1891 | /* scalars can only be spilled into stack */ |
| 1892 | if (insn->dst_reg != BPF_REG_FP) |
| 1893 | return 0; |
| 1894 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 1895 | return 0; |
| 1896 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 1897 | if (spi >= 64) { |
| 1898 | verbose(env, "BUG spi %d\n", spi); |
| 1899 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1900 | return -EFAULT; |
| 1901 | } |
| 1902 | if (!(*stack_mask & (1ull << spi))) |
| 1903 | return 0; |
| 1904 | *stack_mask &= ~(1ull << spi); |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 1905 | if (class == BPF_STX) |
| 1906 | *reg_mask |= sreg; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1907 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
| 1908 | if (opcode == BPF_CALL) { |
| 1909 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1910 | return -ENOTSUPP; |
| 1911 | /* regular helper call sets R0 */ |
| 1912 | *reg_mask &= ~1; |
| 1913 | if (*reg_mask & 0x3f) { |
| 1914 | /* if backtracing was looking for registers R1-R5 |
| 1915 | * they should have been found already. |
| 1916 | */ |
| 1917 | verbose(env, "BUG regs %x\n", *reg_mask); |
| 1918 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1919 | return -EFAULT; |
| 1920 | } |
| 1921 | } else if (opcode == BPF_EXIT) { |
| 1922 | return -ENOTSUPP; |
| 1923 | } |
| 1924 | } else if (class == BPF_LD) { |
| 1925 | if (!(*reg_mask & dreg)) |
| 1926 | return 0; |
| 1927 | *reg_mask &= ~dreg; |
| 1928 | /* It's ld_imm64 or ld_abs or ld_ind. |
| 1929 | * For ld_imm64 no further tracking of precision |
| 1930 | * into parent is necessary |
| 1931 | */ |
| 1932 | if (mode == BPF_IND || mode == BPF_ABS) |
| 1933 | /* to be analyzed */ |
| 1934 | return -ENOTSUPP; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1935 | } |
| 1936 | return 0; |
| 1937 | } |
| 1938 | |
| 1939 | /* the scalar precision tracking algorithm: |
| 1940 | * . at the start all registers have precise=false. |
| 1941 | * . scalar ranges are tracked as normal through alu and jmp insns. |
| 1942 | * . once precise value of the scalar register is used in: |
| 1943 | * . ptr + scalar alu |
| 1944 | * . if (scalar cond K|scalar) |
| 1945 | * . helper_call(.., scalar, ...) where ARG_CONST is expected |
| 1946 | * backtrack through the verifier states and mark all registers and |
| 1947 | * stack slots with spilled constants that these scalar regisers |
| 1948 | * should be precise. |
| 1949 | * . during state pruning two registers (or spilled stack slots) |
| 1950 | * are equivalent if both are not precise. |
| 1951 | * |
| 1952 | * Note the verifier cannot simply walk register parentage chain, |
| 1953 | * since many different registers and stack slots could have been |
| 1954 | * used to compute single precise scalar. |
| 1955 | * |
| 1956 | * The approach of starting with precise=true for all registers and then |
| 1957 | * backtrack to mark a register as not precise when the verifier detects |
| 1958 | * that program doesn't care about specific value (e.g., when helper |
| 1959 | * takes register as ARG_ANYTHING parameter) is not safe. |
| 1960 | * |
| 1961 | * It's ok to walk single parentage chain of the verifier states. |
| 1962 | * It's possible that this backtracking will go all the way till 1st insn. |
| 1963 | * All other branches will be explored for needing precision later. |
| 1964 | * |
| 1965 | * The backtracking needs to deal with cases like: |
| 1966 | * 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) |
| 1967 | * r9 -= r8 |
| 1968 | * r5 = r9 |
| 1969 | * if r5 > 0x79f goto pc+7 |
| 1970 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) |
| 1971 | * r5 += 1 |
| 1972 | * ... |
| 1973 | * call bpf_perf_event_output#25 |
| 1974 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO |
| 1975 | * |
| 1976 | * and this case: |
| 1977 | * r6 = 1 |
| 1978 | * call foo // uses callee's r6 inside to compute r0 |
| 1979 | * r0 += r6 |
| 1980 | * if r0 == 0 goto |
| 1981 | * |
| 1982 | * to track above reg_mask/stack_mask needs to be independent for each frame. |
| 1983 | * |
| 1984 | * Also if parent's curframe > frame where backtracking started, |
| 1985 | * the verifier need to mark registers in both frames, otherwise callees |
| 1986 | * may incorrectly prune callers. This is similar to |
| 1987 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") |
| 1988 | * |
| 1989 | * For now backtracking falls back into conservative marking. |
| 1990 | */ |
| 1991 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, |
| 1992 | struct bpf_verifier_state *st) |
| 1993 | { |
| 1994 | struct bpf_func_state *func; |
| 1995 | struct bpf_reg_state *reg; |
| 1996 | int i, j; |
| 1997 | |
| 1998 | /* big hammer: mark all scalars precise in this path. |
| 1999 | * pop_stack may still get !precise scalars. |
| 2000 | */ |
| 2001 | for (; st; st = st->parent) |
| 2002 | for (i = 0; i <= st->curframe; i++) { |
| 2003 | func = st->frame[i]; |
| 2004 | for (j = 0; j < BPF_REG_FP; j++) { |
| 2005 | reg = &func->regs[j]; |
| 2006 | if (reg->type != SCALAR_VALUE) |
| 2007 | continue; |
| 2008 | reg->precise = true; |
| 2009 | } |
| 2010 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { |
| 2011 | if (func->stack[j].slot_type[0] != STACK_SPILL) |
| 2012 | continue; |
| 2013 | reg = &func->stack[j].spilled_ptr; |
| 2014 | if (reg->type != SCALAR_VALUE) |
| 2015 | continue; |
| 2016 | reg->precise = true; |
| 2017 | } |
| 2018 | } |
| 2019 | } |
| 2020 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2021 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
| 2022 | int spi) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2023 | { |
| 2024 | struct bpf_verifier_state *st = env->cur_state; |
| 2025 | int first_idx = st->first_insn_idx; |
| 2026 | int last_idx = env->insn_idx; |
| 2027 | struct bpf_func_state *func; |
| 2028 | struct bpf_reg_state *reg; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2029 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
| 2030 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2031 | bool skip_first = true; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2032 | bool new_marks = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2033 | int i, err; |
| 2034 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2035 | if (!env->bpf_capable) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2036 | return 0; |
| 2037 | |
| 2038 | func = st->frame[st->curframe]; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2039 | if (regno >= 0) { |
| 2040 | reg = &func->regs[regno]; |
| 2041 | if (reg->type != SCALAR_VALUE) { |
| 2042 | WARN_ONCE(1, "backtracing misuse"); |
| 2043 | return -EFAULT; |
| 2044 | } |
| 2045 | if (!reg->precise) |
| 2046 | new_marks = true; |
| 2047 | else |
| 2048 | reg_mask = 0; |
| 2049 | reg->precise = true; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2050 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2051 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2052 | while (spi >= 0) { |
| 2053 | if (func->stack[spi].slot_type[0] != STACK_SPILL) { |
| 2054 | stack_mask = 0; |
| 2055 | break; |
| 2056 | } |
| 2057 | reg = &func->stack[spi].spilled_ptr; |
| 2058 | if (reg->type != SCALAR_VALUE) { |
| 2059 | stack_mask = 0; |
| 2060 | break; |
| 2061 | } |
| 2062 | if (!reg->precise) |
| 2063 | new_marks = true; |
| 2064 | else |
| 2065 | stack_mask = 0; |
| 2066 | reg->precise = true; |
| 2067 | break; |
| 2068 | } |
| 2069 | |
| 2070 | if (!new_marks) |
| 2071 | return 0; |
| 2072 | if (!reg_mask && !stack_mask) |
| 2073 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2074 | for (;;) { |
| 2075 | DECLARE_BITMAP(mask, 64); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2076 | u32 history = st->jmp_history_cnt; |
| 2077 | |
| 2078 | if (env->log.level & BPF_LOG_LEVEL) |
| 2079 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
| 2080 | for (i = last_idx;;) { |
| 2081 | if (skip_first) { |
| 2082 | err = 0; |
| 2083 | skip_first = false; |
| 2084 | } else { |
| 2085 | err = backtrack_insn(env, i, ®_mask, &stack_mask); |
| 2086 | } |
| 2087 | if (err == -ENOTSUPP) { |
| 2088 | mark_all_scalars_precise(env, st); |
| 2089 | return 0; |
| 2090 | } else if (err) { |
| 2091 | return err; |
| 2092 | } |
| 2093 | if (!reg_mask && !stack_mask) |
| 2094 | /* Found assignment(s) into tracked register in this state. |
| 2095 | * Since this state is already marked, just return. |
| 2096 | * Nothing to be tracked further in the parent state. |
| 2097 | */ |
| 2098 | return 0; |
| 2099 | if (i == first_idx) |
| 2100 | break; |
| 2101 | i = get_prev_insn_idx(st, i, &history); |
| 2102 | if (i >= env->prog->len) { |
| 2103 | /* This can happen if backtracking reached insn 0 |
| 2104 | * and there are still reg_mask or stack_mask |
| 2105 | * to backtrack. |
| 2106 | * It means the backtracking missed the spot where |
| 2107 | * particular register was initialized with a constant. |
| 2108 | */ |
| 2109 | verbose(env, "BUG backtracking idx %d\n", i); |
| 2110 | WARN_ONCE(1, "verifier backtracking bug"); |
| 2111 | return -EFAULT; |
| 2112 | } |
| 2113 | } |
| 2114 | st = st->parent; |
| 2115 | if (!st) |
| 2116 | break; |
| 2117 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2118 | new_marks = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2119 | func = st->frame[st->curframe]; |
| 2120 | bitmap_from_u64(mask, reg_mask); |
| 2121 | for_each_set_bit(i, mask, 32) { |
| 2122 | reg = &func->regs[i]; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2123 | if (reg->type != SCALAR_VALUE) { |
| 2124 | reg_mask &= ~(1u << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2125 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2126 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2127 | if (!reg->precise) |
| 2128 | new_marks = true; |
| 2129 | reg->precise = true; |
| 2130 | } |
| 2131 | |
| 2132 | bitmap_from_u64(mask, stack_mask); |
| 2133 | for_each_set_bit(i, mask, 64) { |
| 2134 | if (i >= func->allocated_stack / BPF_REG_SIZE) { |
Alexei Starovoitov | 2339cd6 | 2019-09-03 15:16:17 -0700 | [diff] [blame] | 2135 | /* the sequence of instructions: |
| 2136 | * 2: (bf) r3 = r10 |
| 2137 | * 3: (7b) *(u64 *)(r3 -8) = r0 |
| 2138 | * 4: (79) r4 = *(u64 *)(r10 -8) |
| 2139 | * doesn't contain jmps. It's backtracked |
| 2140 | * as a single block. |
| 2141 | * During backtracking insn 3 is not recognized as |
| 2142 | * stack access, so at the end of backtracking |
| 2143 | * stack slot fp-8 is still marked in stack_mask. |
| 2144 | * However the parent state may not have accessed |
| 2145 | * fp-8 and it's "unallocated" stack space. |
| 2146 | * In such case fallback to conservative. |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2147 | */ |
Alexei Starovoitov | 2339cd6 | 2019-09-03 15:16:17 -0700 | [diff] [blame] | 2148 | mark_all_scalars_precise(env, st); |
| 2149 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2150 | } |
| 2151 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2152 | if (func->stack[i].slot_type[0] != STACK_SPILL) { |
| 2153 | stack_mask &= ~(1ull << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2154 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2155 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2156 | reg = &func->stack[i].spilled_ptr; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2157 | if (reg->type != SCALAR_VALUE) { |
| 2158 | stack_mask &= ~(1ull << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2159 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2160 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2161 | if (!reg->precise) |
| 2162 | new_marks = true; |
| 2163 | reg->precise = true; |
| 2164 | } |
| 2165 | if (env->log.level & BPF_LOG_LEVEL) { |
| 2166 | print_verifier_state(env, func); |
| 2167 | verbose(env, "parent %s regs=%x stack=%llx marks\n", |
| 2168 | new_marks ? "didn't have" : "already had", |
| 2169 | reg_mask, stack_mask); |
| 2170 | } |
| 2171 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2172 | if (!reg_mask && !stack_mask) |
| 2173 | break; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2174 | if (!new_marks) |
| 2175 | break; |
| 2176 | |
| 2177 | last_idx = st->last_insn_idx; |
| 2178 | first_idx = st->first_insn_idx; |
| 2179 | } |
| 2180 | return 0; |
| 2181 | } |
| 2182 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2183 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
| 2184 | { |
| 2185 | return __mark_chain_precision(env, regno, -1); |
| 2186 | } |
| 2187 | |
| 2188 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) |
| 2189 | { |
| 2190 | return __mark_chain_precision(env, -1, spi); |
| 2191 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2192 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2193 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 2194 | { |
| 2195 | switch (type) { |
| 2196 | case PTR_TO_MAP_VALUE: |
| 2197 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 2198 | case PTR_TO_STACK: |
| 2199 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2200 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2201 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2202 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2203 | case PTR_TO_FLOW_KEYS: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2204 | case CONST_PTR_TO_MAP: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2205 | case PTR_TO_SOCKET: |
| 2206 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2207 | case PTR_TO_SOCK_COMMON: |
| 2208 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2209 | case PTR_TO_TCP_SOCK: |
| 2210 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2211 | case PTR_TO_XDP_SOCK: |
Martin KaFai Lau | 65726b5 | 2020-01-08 16:34:54 -0800 | [diff] [blame] | 2212 | case PTR_TO_BTF_ID: |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 2213 | case PTR_TO_BTF_ID_OR_NULL: |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 2214 | case PTR_TO_RDONLY_BUF: |
| 2215 | case PTR_TO_RDONLY_BUF_OR_NULL: |
| 2216 | case PTR_TO_RDWR_BUF: |
| 2217 | case PTR_TO_RDWR_BUF_OR_NULL: |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 2218 | case PTR_TO_PERCPU_BTF_ID: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2219 | return true; |
| 2220 | default: |
| 2221 | return false; |
| 2222 | } |
| 2223 | } |
| 2224 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2225 | /* Does this register contain a constant zero? */ |
| 2226 | static bool register_is_null(struct bpf_reg_state *reg) |
| 2227 | { |
| 2228 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 2229 | } |
| 2230 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2231 | static bool register_is_const(struct bpf_reg_state *reg) |
| 2232 | { |
| 2233 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); |
| 2234 | } |
| 2235 | |
Yonghong Song | 5689d49 | 2020-10-08 18:12:38 -0700 | [diff] [blame] | 2236 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
| 2237 | { |
| 2238 | return tnum_is_unknown(reg->var_off) && |
| 2239 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && |
| 2240 | reg->umin_value == 0 && reg->umax_value == U64_MAX && |
| 2241 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && |
| 2242 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; |
| 2243 | } |
| 2244 | |
| 2245 | static bool register_is_bounded(struct bpf_reg_state *reg) |
| 2246 | { |
| 2247 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); |
| 2248 | } |
| 2249 | |
Jann Horn | 6e7e63c | 2020-04-17 02:00:06 +0200 | [diff] [blame] | 2250 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 2251 | const struct bpf_reg_state *reg) |
| 2252 | { |
| 2253 | if (allow_ptr_leaks) |
| 2254 | return false; |
| 2255 | |
| 2256 | return reg->type != SCALAR_VALUE; |
| 2257 | } |
| 2258 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2259 | static void save_register_state(struct bpf_func_state *state, |
| 2260 | int spi, struct bpf_reg_state *reg) |
| 2261 | { |
| 2262 | int i; |
| 2263 | |
| 2264 | state->stack[spi].spilled_ptr = *reg; |
| 2265 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 2266 | |
| 2267 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 2268 | state->stack[spi].slot_type[i] = STACK_SPILL; |
| 2269 | } |
| 2270 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2271 | /* check_stack_read/write functions track spill/fill of registers, |
| 2272 | * stack boundary and alignment are checked in check_mem_access() |
| 2273 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2274 | static int check_stack_write(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2275 | struct bpf_func_state *state, /* func where register points to */ |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2276 | int off, int size, int value_regno, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2277 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2278 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2279 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2280 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2281 | struct bpf_reg_state *reg = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2282 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2283 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2284 | state->acquired_refs, true); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2285 | if (err) |
| 2286 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2287 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 2288 | * so it's aligned access and [off, off + size) are within stack limits |
| 2289 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2290 | if (!env->allow_ptr_leaks && |
| 2291 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 2292 | size != BPF_REG_SIZE) { |
| 2293 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 2294 | return -EACCES; |
| 2295 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2296 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2297 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2298 | if (value_regno >= 0) |
| 2299 | reg = &cur->regs[value_regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2300 | |
Yonghong Song | 5689d49 | 2020-10-08 18:12:38 -0700 | [diff] [blame] | 2301 | if (reg && size == BPF_REG_SIZE && register_is_bounded(reg) && |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2302 | !register_is_null(reg) && env->bpf_capable) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2303 | if (dst_reg != BPF_REG_FP) { |
| 2304 | /* The backtracking logic can only recognize explicit |
| 2305 | * stack slot address like [fp - 8]. Other spill of |
| 2306 | * scalar via different register has to be conervative. |
| 2307 | * Backtrack from here and mark all registers as precise |
| 2308 | * that contributed into 'reg' being a constant. |
| 2309 | */ |
| 2310 | err = mark_chain_precision(env, value_regno); |
| 2311 | if (err) |
| 2312 | return err; |
| 2313 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2314 | save_register_state(state, spi, reg); |
| 2315 | } else if (reg && is_spillable_regtype(reg->type)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2316 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2317 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2318 | verbose_linfo(env, insn_idx, "; "); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2319 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2320 | return -EACCES; |
| 2321 | } |
| 2322 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2323 | if (state != cur && reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2324 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 2325 | return -EINVAL; |
| 2326 | } |
| 2327 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2328 | if (!env->bypass_spec_v4) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2329 | bool sanitize = false; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2330 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2331 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 2332 | register_is_const(&state->stack[spi].spilled_ptr)) |
| 2333 | sanitize = true; |
| 2334 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 2335 | if (state->stack[spi].slot_type[i] == STACK_MISC) { |
| 2336 | sanitize = true; |
| 2337 | break; |
| 2338 | } |
| 2339 | if (sanitize) { |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2340 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
| 2341 | int soff = (-spi - 1) * BPF_REG_SIZE; |
| 2342 | |
| 2343 | /* detected reuse of integer stack slot with a pointer |
| 2344 | * which means either llvm is reusing stack slot or |
| 2345 | * an attacker is trying to exploit CVE-2018-3639 |
| 2346 | * (speculative store bypass) |
| 2347 | * Have to sanitize that slot with preemptive |
| 2348 | * store of zero. |
| 2349 | */ |
| 2350 | if (*poff && *poff != soff) { |
| 2351 | /* disallow programs where single insn stores |
| 2352 | * into two different stack slots, since verifier |
| 2353 | * cannot sanitize them |
| 2354 | */ |
| 2355 | verbose(env, |
| 2356 | "insn %d cannot access two stack slots fp%d and fp%d", |
| 2357 | insn_idx, *poff, soff); |
| 2358 | return -EINVAL; |
| 2359 | } |
| 2360 | *poff = soff; |
| 2361 | } |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2362 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2363 | save_register_state(state, spi, reg); |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2364 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2365 | u8 type = STACK_MISC; |
| 2366 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2367 | /* regular write of data into stack destroys any spilled ptr */ |
| 2368 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2369 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
| 2370 | if (state->stack[spi].slot_type[0] == STACK_SPILL) |
| 2371 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 2372 | state->stack[spi].slot_type[i] = STACK_MISC; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2373 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2374 | /* only mark the slot as written if all 8 bytes were written |
| 2375 | * otherwise read propagation may incorrectly stop too soon |
| 2376 | * when stack slots are partially written. |
| 2377 | * This heuristic means that read propagation will be |
| 2378 | * conservative, since it will add reg_live_read marks |
| 2379 | * to stack slots all the way to first state when programs |
| 2380 | * writes+reads less than 8 bytes |
| 2381 | */ |
| 2382 | if (size == BPF_REG_SIZE) |
| 2383 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 2384 | |
| 2385 | /* when we zero initialize stack slots mark them as such */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2386 | if (reg && register_is_null(reg)) { |
| 2387 | /* backtracking doesn't work for STACK_ZERO yet. */ |
| 2388 | err = mark_chain_precision(env, value_regno); |
| 2389 | if (err) |
| 2390 | return err; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2391 | type = STACK_ZERO; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2392 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2393 | |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2394 | /* Mark slots affected by this stack write. */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2395 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2396 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2397 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2398 | } |
| 2399 | return 0; |
| 2400 | } |
| 2401 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2402 | static int check_stack_read(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2403 | struct bpf_func_state *reg_state /* func where register points to */, |
| 2404 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2405 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2406 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2407 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2408 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2409 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2410 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2411 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2412 | if (reg_state->allocated_stack <= slot) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2413 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 2414 | off, size); |
| 2415 | return -EACCES; |
| 2416 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2417 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2418 | reg = ®_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2419 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2420 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2421 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2422 | if (reg->type != SCALAR_VALUE) { |
| 2423 | verbose_linfo(env, env->insn_idx, "; "); |
| 2424 | verbose(env, "invalid size of register fill\n"); |
| 2425 | return -EACCES; |
| 2426 | } |
| 2427 | if (value_regno >= 0) { |
| 2428 | mark_reg_unknown(env, state->regs, value_regno); |
| 2429 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 2430 | } |
| 2431 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
| 2432 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2433 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2434 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2435 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2436 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2437 | return -EACCES; |
| 2438 | } |
| 2439 | } |
| 2440 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2441 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2442 | /* restore register state from stack */ |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2443 | state->regs[value_regno] = *reg; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 2444 | /* mark reg as written since spilled pointer state likely |
| 2445 | * has its liveness marks cleared by is_state_visited() |
| 2446 | * which resets stack/reg liveness for state transitions |
| 2447 | */ |
| 2448 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Jann Horn | 6e7e63c | 2020-04-17 02:00:06 +0200 | [diff] [blame] | 2449 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
| 2450 | /* If value_regno==-1, the caller is asking us whether |
| 2451 | * it is acceptable to use this value as a SCALAR_VALUE |
| 2452 | * (e.g. for XADD). |
| 2453 | * We must not allow unprivileged callers to do that |
| 2454 | * with spilled pointers. |
| 2455 | */ |
| 2456 | verbose(env, "leaking pointer from stack off %d\n", |
| 2457 | off); |
| 2458 | return -EACCES; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2459 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2460 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2461 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2462 | int zeros = 0; |
| 2463 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2464 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2465 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 2466 | continue; |
| 2467 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 2468 | zeros++; |
| 2469 | continue; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2470 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2471 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 2472 | off, i, size); |
| 2473 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2474 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2475 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2476 | if (value_regno >= 0) { |
| 2477 | if (zeros == size) { |
| 2478 | /* any size read into register is zero extended, |
| 2479 | * so the whole register == const_zero |
| 2480 | */ |
| 2481 | __mark_reg_const_zero(&state->regs[value_regno]); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2482 | /* backtracking doesn't support STACK_ZERO yet, |
| 2483 | * so mark it precise here, so that later |
| 2484 | * backtracking can stop here. |
| 2485 | * Backtracking may not need this if this register |
| 2486 | * doesn't participate in pointer adjustment. |
| 2487 | * Forward propagation of precise flag is not |
| 2488 | * necessary either. This mark is only to stop |
| 2489 | * backtracking. Any register that contributed |
| 2490 | * to const 0 was marked precise before spill. |
| 2491 | */ |
| 2492 | state->regs[value_regno].precise = true; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2493 | } else { |
| 2494 | /* have read misc data from the stack */ |
| 2495 | mark_reg_unknown(env, state->regs, value_regno); |
| 2496 | } |
| 2497 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 2498 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2499 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2500 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 2503 | static int check_stack_access(struct bpf_verifier_env *env, |
| 2504 | const struct bpf_reg_state *reg, |
| 2505 | int off, int size) |
| 2506 | { |
| 2507 | /* Stack accesses must be at a fixed offset, so that we |
| 2508 | * can determine what type of data were returned. See |
| 2509 | * check_stack_read(). |
| 2510 | */ |
| 2511 | if (!tnum_is_const(reg->var_off)) { |
| 2512 | char tn_buf[48]; |
| 2513 | |
| 2514 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Andrey Ignatov | 1fbd20f | 2019-04-03 23:22:43 -0700 | [diff] [blame] | 2515 | 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] | 2516 | tn_buf, off, size); |
| 2517 | return -EACCES; |
| 2518 | } |
| 2519 | |
| 2520 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 2521 | verbose(env, "invalid stack off=%d size=%d\n", off, size); |
| 2522 | return -EACCES; |
| 2523 | } |
| 2524 | |
| 2525 | return 0; |
| 2526 | } |
| 2527 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 2528 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
| 2529 | int off, int size, enum bpf_access_type type) |
| 2530 | { |
| 2531 | struct bpf_reg_state *regs = cur_regs(env); |
| 2532 | struct bpf_map *map = regs[regno].map_ptr; |
| 2533 | u32 cap = bpf_map_flags_to_cap(map); |
| 2534 | |
| 2535 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { |
| 2536 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", |
| 2537 | map->value_size, off, size); |
| 2538 | return -EACCES; |
| 2539 | } |
| 2540 | |
| 2541 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { |
| 2542 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", |
| 2543 | map->value_size, off, size); |
| 2544 | return -EACCES; |
| 2545 | } |
| 2546 | |
| 2547 | return 0; |
| 2548 | } |
| 2549 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2550 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
| 2551 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, |
| 2552 | int off, int size, u32 mem_size, |
| 2553 | bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2554 | { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2555 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
| 2556 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2557 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2558 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) |
| 2559 | return 0; |
| 2560 | |
| 2561 | reg = &cur_regs(env)[regno]; |
| 2562 | switch (reg->type) { |
| 2563 | case PTR_TO_MAP_VALUE: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2564 | 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] | 2565 | mem_size, off, size); |
| 2566 | break; |
| 2567 | case PTR_TO_PACKET: |
| 2568 | case PTR_TO_PACKET_META: |
| 2569 | case PTR_TO_PACKET_END: |
| 2570 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
| 2571 | off, size, regno, reg->id, off, mem_size); |
| 2572 | break; |
| 2573 | case PTR_TO_MEM: |
| 2574 | default: |
| 2575 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", |
| 2576 | mem_size, off, size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2577 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2578 | |
| 2579 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2580 | } |
| 2581 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2582 | /* check read/write into a memory region with possible variable offset */ |
| 2583 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, |
| 2584 | int off, int size, u32 mem_size, |
| 2585 | bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2586 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2587 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2588 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2589 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 2590 | int err; |
| 2591 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2592 | /* We may have adjusted the register pointing to memory region, so we |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2593 | * need to try adding each of min_value and max_value to off |
| 2594 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2595 | */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 2596 | if (env->log.level & BPF_LOG_LEVEL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2597 | print_verifier_state(env, state); |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 2598 | |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2599 | /* The minimum value is only important with signed |
| 2600 | * comparisons where we can't assume the floor of a |
| 2601 | * value is 0. If we are using signed variables for our |
| 2602 | * index'es we need to make sure that whatever we use |
| 2603 | * will have a set floor within our range. |
| 2604 | */ |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 2605 | if (reg->smin_value < 0 && |
| 2606 | (reg->smin_value == S64_MIN || |
| 2607 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || |
| 2608 | reg->smin_value + off < 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2609 | 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] | 2610 | regno); |
| 2611 | return -EACCES; |
| 2612 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2613 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
| 2614 | mem_size, zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2615 | if (err) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2616 | 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] | 2617 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2618 | return err; |
| 2619 | } |
| 2620 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2621 | /* If we haven't set a max value then we need to bail since we can't be |
| 2622 | * sure we won't do bad things. |
| 2623 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2624 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2625 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2626 | 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] | 2627 | regno); |
| 2628 | return -EACCES; |
| 2629 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2630 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
| 2631 | mem_size, zero_size_allowed); |
| 2632 | if (err) { |
| 2633 | 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] | 2634 | regno); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2635 | return err; |
| 2636 | } |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 2637 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2638 | return 0; |
| 2639 | } |
| 2640 | |
| 2641 | /* check read/write into a map element with possible variable offset */ |
| 2642 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
| 2643 | int off, int size, bool zero_size_allowed) |
| 2644 | { |
| 2645 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2646 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2647 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 2648 | struct bpf_map *map = reg->map_ptr; |
| 2649 | int err; |
| 2650 | |
| 2651 | err = check_mem_region_access(env, regno, off, size, map->value_size, |
| 2652 | zero_size_allowed); |
| 2653 | if (err) |
| 2654 | return err; |
| 2655 | |
| 2656 | if (map_value_has_spin_lock(map)) { |
| 2657 | u32 lock = map->spin_lock_off; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 2658 | |
| 2659 | /* if any part of struct bpf_spin_lock can be touched by |
| 2660 | * load/store reject this program. |
| 2661 | * To check that [x1, x2) overlaps with [y1, y2) |
| 2662 | * it is sufficient to check x1 < y2 && y1 < x2. |
| 2663 | */ |
| 2664 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && |
| 2665 | lock < reg->umax_value + off + size) { |
| 2666 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); |
| 2667 | return -EACCES; |
| 2668 | } |
| 2669 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2670 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2671 | } |
| 2672 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2673 | #define MAX_PACKET_OFF 0xffff |
| 2674 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 2675 | static enum bpf_prog_type resolve_prog_type(struct bpf_prog *prog) |
| 2676 | { |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 2677 | return prog->aux->dst_prog ? prog->aux->dst_prog->type : prog->type; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 2678 | } |
| 2679 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2680 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2681 | const struct bpf_call_arg_meta *meta, |
| 2682 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2683 | { |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 2684 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
| 2685 | |
| 2686 | switch (prog_type) { |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 2687 | /* Program types only with direct read access go here! */ |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2688 | case BPF_PROG_TYPE_LWT_IN: |
| 2689 | case BPF_PROG_TYPE_LWT_OUT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 2690 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2691 | case BPF_PROG_TYPE_SK_REUSEPORT: |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 2692 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Daniel Borkmann | d5563d3 | 2018-10-24 22:05:46 +0200 | [diff] [blame] | 2693 | case BPF_PROG_TYPE_CGROUP_SKB: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2694 | if (t == BPF_WRITE) |
| 2695 | return false; |
Gustavo A. R. Silva | 8731745 | 2020-10-02 18:42:17 -0500 | [diff] [blame] | 2696 | fallthrough; |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 2697 | |
| 2698 | /* Program types with direct read + write access go here! */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2699 | case BPF_PROG_TYPE_SCHED_CLS: |
| 2700 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2701 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2702 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 2703 | case BPF_PROG_TYPE_SK_SKB: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2704 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2705 | if (meta) |
| 2706 | return meta->pkt_access; |
| 2707 | |
| 2708 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2709 | return true; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2710 | |
| 2711 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 2712 | if (t == BPF_WRITE) |
| 2713 | env->seen_direct_write = true; |
| 2714 | |
| 2715 | return true; |
| 2716 | |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2717 | default: |
| 2718 | return false; |
| 2719 | } |
| 2720 | } |
| 2721 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2722 | 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] | 2723 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2724 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2725 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2726 | struct bpf_reg_state *reg = ®s[regno]; |
| 2727 | int err; |
| 2728 | |
| 2729 | /* We may have added a variable offset to the packet pointer; but any |
| 2730 | * reg->range we have comes after that. We are only checking the fixed |
| 2731 | * offset. |
| 2732 | */ |
| 2733 | |
| 2734 | /* We don't allow negative numbers, because we aren't tracking enough |
| 2735 | * detail to prove they're safe. |
| 2736 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2737 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2738 | 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] | 2739 | regno); |
| 2740 | return -EACCES; |
| 2741 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2742 | err = __check_mem_access(env, regno, off, size, reg->range, |
| 2743 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2744 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2745 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2746 | return err; |
| 2747 | } |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 2748 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2749 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 2750 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 2751 | * otherwise find_good_pkt_pointers would have refused to set range info |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2752 | * that __check_mem_access would have rejected this pkt access. |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 2753 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 2754 | */ |
| 2755 | env->prog->aux->max_pkt_offset = |
| 2756 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 2757 | off + reg->umax_value + size - 1); |
| 2758 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2759 | return err; |
| 2760 | } |
| 2761 | |
| 2762 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2763 | 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] | 2764 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
| 2765 | u32 *btf_id) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2766 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 2767 | struct bpf_insn_access_aux info = { |
| 2768 | .reg_type = *reg_type, |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2769 | .log = &env->log, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 2770 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2771 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 2772 | if (env->ops->is_valid_access && |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2773 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 2774 | /* A non zero info.ctx_field_size indicates that this field is a |
| 2775 | * candidate for later verifier transformation to load the whole |
| 2776 | * field and then apply a mask when accessed with a narrower |
| 2777 | * access than actual ctx access size. A zero info.ctx_field_size |
| 2778 | * will only allow for whole field access and rejects any other |
| 2779 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2780 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 2781 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2782 | |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 2783 | if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL) |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2784 | *btf_id = info.btf_id; |
| 2785 | else |
| 2786 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 2787 | /* remember the offset of last byte accessed in ctx */ |
| 2788 | if (env->prog->aux->max_ctx_offset < off + size) |
| 2789 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2790 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 2791 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2792 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2793 | 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] | 2794 | return -EACCES; |
| 2795 | } |
| 2796 | |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2797 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 2798 | int size) |
| 2799 | { |
| 2800 | if (size < 0 || off < 0 || |
| 2801 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 2802 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 2803 | off, size); |
| 2804 | return -EACCES; |
| 2805 | } |
| 2806 | return 0; |
| 2807 | } |
| 2808 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2809 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
| 2810 | u32 regno, int off, int size, |
| 2811 | enum bpf_access_type t) |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2812 | { |
| 2813 | struct bpf_reg_state *regs = cur_regs(env); |
| 2814 | struct bpf_reg_state *reg = ®s[regno]; |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2815 | struct bpf_insn_access_aux info = {}; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2816 | bool valid; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2817 | |
| 2818 | if (reg->smin_value < 0) { |
| 2819 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 2820 | regno); |
| 2821 | return -EACCES; |
| 2822 | } |
| 2823 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2824 | switch (reg->type) { |
| 2825 | case PTR_TO_SOCK_COMMON: |
| 2826 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); |
| 2827 | break; |
| 2828 | case PTR_TO_SOCKET: |
| 2829 | valid = bpf_sock_is_valid_access(off, size, t, &info); |
| 2830 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2831 | case PTR_TO_TCP_SOCK: |
| 2832 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); |
| 2833 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2834 | case PTR_TO_XDP_SOCK: |
| 2835 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); |
| 2836 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2837 | default: |
| 2838 | valid = false; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2839 | } |
| 2840 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2841 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2842 | if (valid) { |
| 2843 | env->insn_aux_data[insn_idx].ctx_field_size = |
| 2844 | info.ctx_field_size; |
| 2845 | return 0; |
| 2846 | } |
| 2847 | |
| 2848 | verbose(env, "R%d invalid %s access off=%d size=%d\n", |
| 2849 | regno, reg_type_str[reg->type], off, size); |
| 2850 | |
| 2851 | return -EACCES; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2852 | } |
| 2853 | |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2854 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
| 2855 | { |
| 2856 | return cur_regs(env) + regno; |
| 2857 | } |
| 2858 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2859 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 2860 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2861 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2862 | } |
| 2863 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2864 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 2865 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2866 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2867 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2868 | return reg->type == PTR_TO_CTX; |
| 2869 | } |
| 2870 | |
| 2871 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) |
| 2872 | { |
| 2873 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 2874 | |
| 2875 | return type_is_sk_pointer(reg->type); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2876 | } |
| 2877 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2878 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 2879 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2880 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2881 | |
| 2882 | return type_is_pkt_pointer(reg->type); |
| 2883 | } |
| 2884 | |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 2885 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 2886 | { |
| 2887 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 2888 | |
| 2889 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 2890 | return reg->type == PTR_TO_FLOW_KEYS; |
| 2891 | } |
| 2892 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2893 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 2894 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2895 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2896 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2897 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2898 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2899 | |
| 2900 | /* Byte size accesses are always allowed. */ |
| 2901 | if (!strict || size == 1) |
| 2902 | return 0; |
| 2903 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 2904 | /* For platforms that do not have a Kconfig enabling |
| 2905 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 2906 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 2907 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 2908 | * to this code only in strict mode where we want to emulate |
| 2909 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 2910 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2911 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 2912 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2913 | |
| 2914 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 2915 | if (!tnum_is_aligned(reg_off, size)) { |
| 2916 | char tn_buf[48]; |
| 2917 | |
| 2918 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2919 | verbose(env, |
| 2920 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2921 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2922 | return -EACCES; |
| 2923 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2924 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2925 | return 0; |
| 2926 | } |
| 2927 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2928 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 2929 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2930 | const char *pointer_desc, |
| 2931 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2932 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2933 | struct tnum reg_off; |
| 2934 | |
| 2935 | /* Byte size accesses are always allowed. */ |
| 2936 | if (!strict || size == 1) |
| 2937 | return 0; |
| 2938 | |
| 2939 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 2940 | if (!tnum_is_aligned(reg_off, size)) { |
| 2941 | char tn_buf[48]; |
| 2942 | |
| 2943 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2944 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2945 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2946 | return -EACCES; |
| 2947 | } |
| 2948 | |
| 2949 | return 0; |
| 2950 | } |
| 2951 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2952 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2953 | const struct bpf_reg_state *reg, int off, |
| 2954 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2955 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2956 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2957 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2958 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2959 | switch (reg->type) { |
| 2960 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2961 | case PTR_TO_PACKET_META: |
| 2962 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 2963 | * right in front, treat it the very same way. |
| 2964 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2965 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2966 | case PTR_TO_FLOW_KEYS: |
| 2967 | pointer_desc = "flow keys "; |
| 2968 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2969 | case PTR_TO_MAP_VALUE: |
| 2970 | pointer_desc = "value "; |
| 2971 | break; |
| 2972 | case PTR_TO_CTX: |
| 2973 | pointer_desc = "context "; |
| 2974 | break; |
| 2975 | case PTR_TO_STACK: |
| 2976 | pointer_desc = "stack "; |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 2977 | /* The stack spill tracking logic in check_stack_write() |
| 2978 | * and check_stack_read() relies on stack accesses being |
| 2979 | * aligned. |
| 2980 | */ |
| 2981 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2982 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2983 | case PTR_TO_SOCKET: |
| 2984 | pointer_desc = "sock "; |
| 2985 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2986 | case PTR_TO_SOCK_COMMON: |
| 2987 | pointer_desc = "sock_common "; |
| 2988 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2989 | case PTR_TO_TCP_SOCK: |
| 2990 | pointer_desc = "tcp_sock "; |
| 2991 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2992 | case PTR_TO_XDP_SOCK: |
| 2993 | pointer_desc = "xdp_sock "; |
| 2994 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2995 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2996 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2997 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2998 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 2999 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3000 | } |
| 3001 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3002 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 3003 | const struct bpf_func_state *func, |
| 3004 | int off) |
| 3005 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3006 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3007 | |
| 3008 | if (stack >= -off) |
| 3009 | return 0; |
| 3010 | |
| 3011 | /* update known max for given subprogram */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3012 | env->subprog_info[func->subprogno].stack_depth = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3013 | return 0; |
| 3014 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3015 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3016 | /* starting from main bpf function walk all instructions of the function |
| 3017 | * and recursively walk all callees that given function can call. |
| 3018 | * Ignore jump and exit insns. |
| 3019 | * Since recursion is prevented by check_cfg() this algorithm |
| 3020 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 3021 | */ |
| 3022 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 3023 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3024 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 3025 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3026 | struct bpf_insn *insn = env->prog->insnsi; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3027 | bool tail_call_reachable = false; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3028 | int ret_insn[MAX_CALL_FRAMES]; |
| 3029 | int ret_prog[MAX_CALL_FRAMES]; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3030 | int j; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3031 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3032 | process_func: |
Maciej Fijalkowski | 7f6e431 | 2020-09-16 23:10:07 +0200 | [diff] [blame] | 3033 | /* protect against potential stack overflow that might happen when |
| 3034 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack |
| 3035 | * depth for such case down to 256 so that the worst case scenario |
| 3036 | * would result in 8k stack size (32 which is tailcall limit * 256 = |
| 3037 | * 8k). |
| 3038 | * |
| 3039 | * To get the idea what might happen, see an example: |
| 3040 | * func1 -> sub rsp, 128 |
| 3041 | * subfunc1 -> sub rsp, 256 |
| 3042 | * tailcall1 -> add rsp, 256 |
| 3043 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) |
| 3044 | * subfunc2 -> sub rsp, 64 |
| 3045 | * subfunc22 -> sub rsp, 128 |
| 3046 | * tailcall2 -> add rsp, 128 |
| 3047 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) |
| 3048 | * |
| 3049 | * tailcall will unwind the current stack frame but it will not get rid |
| 3050 | * of caller's stack as shown on the example above. |
| 3051 | */ |
| 3052 | if (idx && subprog[idx].has_tail_call && depth >= 256) { |
| 3053 | verbose(env, |
| 3054 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", |
| 3055 | depth); |
| 3056 | return -EACCES; |
| 3057 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3058 | /* round up to 32-bytes, since this is granularity |
| 3059 | * of interpreter stack size |
| 3060 | */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3061 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3062 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3063 | 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] | 3064 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3065 | return -EACCES; |
| 3066 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3067 | continue_func: |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 3068 | subprog_end = subprog[idx + 1].start; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3069 | for (; i < subprog_end; i++) { |
| 3070 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 3071 | continue; |
| 3072 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 3073 | continue; |
| 3074 | /* remember insn and function to return to */ |
| 3075 | ret_insn[frame] = i + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3076 | ret_prog[frame] = idx; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3077 | |
| 3078 | /* find the callee */ |
| 3079 | i = i + insn[i].imm + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3080 | idx = find_subprog(env, i); |
| 3081 | if (idx < 0) { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3082 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 3083 | i); |
| 3084 | return -EFAULT; |
| 3085 | } |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3086 | |
| 3087 | if (subprog[idx].has_tail_call) |
| 3088 | tail_call_reachable = true; |
| 3089 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3090 | frame++; |
| 3091 | if (frame >= MAX_CALL_FRAMES) { |
Paul Chaignon | 927cb78 | 2019-03-20 13:58:27 +0100 | [diff] [blame] | 3092 | verbose(env, "the call stack of %d frames is too deep !\n", |
| 3093 | frame); |
| 3094 | return -E2BIG; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3095 | } |
| 3096 | goto process_func; |
| 3097 | } |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3098 | /* if tail call got detected across bpf2bpf calls then mark each of the |
| 3099 | * currently present subprog frames as tail call reachable subprogs; |
| 3100 | * this info will be utilized by JIT so that we will be preserving the |
| 3101 | * tail call counter throughout bpf2bpf calls combined with tailcalls |
| 3102 | */ |
| 3103 | if (tail_call_reachable) |
| 3104 | for (j = 0; j < frame; j++) |
| 3105 | subprog[ret_prog[j]].tail_call_reachable = true; |
| 3106 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3107 | /* end of for() loop means the last insn of the 'subprog' |
| 3108 | * was reached. Doesn't matter whether it was JA or EXIT |
| 3109 | */ |
| 3110 | if (frame == 0) |
| 3111 | return 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3112 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3113 | frame--; |
| 3114 | i = ret_insn[frame]; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3115 | idx = ret_prog[frame]; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3116 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3117 | } |
| 3118 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 3119 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 3120 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 3121 | const struct bpf_insn *insn, int idx) |
| 3122 | { |
| 3123 | int start = idx + insn->imm + 1, subprog; |
| 3124 | |
| 3125 | subprog = find_subprog(env, start); |
| 3126 | if (subprog < 0) { |
| 3127 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 3128 | start); |
| 3129 | return -EFAULT; |
| 3130 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3131 | return env->subprog_info[subprog].stack_depth; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 3132 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 3133 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 3134 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 3135 | int check_ctx_reg(struct bpf_verifier_env *env, |
| 3136 | const struct bpf_reg_state *reg, int regno) |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 3137 | { |
| 3138 | /* Access to ctx or passing it to a helper is only allowed in |
| 3139 | * its original, unmodified form. |
| 3140 | */ |
| 3141 | |
| 3142 | if (reg->off) { |
| 3143 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 3144 | regno, reg->off); |
| 3145 | return -EACCES; |
| 3146 | } |
| 3147 | |
| 3148 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 3149 | char tn_buf[48]; |
| 3150 | |
| 3151 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3152 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 3153 | return -EACCES; |
| 3154 | } |
| 3155 | |
| 3156 | return 0; |
| 3157 | } |
| 3158 | |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3159 | static int __check_buffer_access(struct bpf_verifier_env *env, |
| 3160 | const char *buf_info, |
| 3161 | const struct bpf_reg_state *reg, |
| 3162 | int regno, int off, int size) |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3163 | { |
| 3164 | if (off < 0) { |
| 3165 | verbose(env, |
Yonghong Song | 4fc00b7 | 2020-07-28 15:18:01 -0700 | [diff] [blame] | 3166 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3167 | regno, buf_info, off, size); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3168 | return -EACCES; |
| 3169 | } |
| 3170 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 3171 | char tn_buf[48]; |
| 3172 | |
| 3173 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3174 | verbose(env, |
Yonghong Song | 4fc00b7 | 2020-07-28 15:18:01 -0700 | [diff] [blame] | 3175 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3176 | regno, off, tn_buf); |
| 3177 | return -EACCES; |
| 3178 | } |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3179 | |
| 3180 | return 0; |
| 3181 | } |
| 3182 | |
| 3183 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
| 3184 | const struct bpf_reg_state *reg, |
| 3185 | int regno, int off, int size) |
| 3186 | { |
| 3187 | int err; |
| 3188 | |
| 3189 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); |
| 3190 | if (err) |
| 3191 | return err; |
| 3192 | |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3193 | if (off + size > env->prog->aux->max_tp_access) |
| 3194 | env->prog->aux->max_tp_access = off + size; |
| 3195 | |
| 3196 | return 0; |
| 3197 | } |
| 3198 | |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3199 | static int check_buffer_access(struct bpf_verifier_env *env, |
| 3200 | const struct bpf_reg_state *reg, |
| 3201 | int regno, int off, int size, |
| 3202 | bool zero_size_allowed, |
| 3203 | const char *buf_info, |
| 3204 | u32 *max_access) |
| 3205 | { |
| 3206 | int err; |
| 3207 | |
| 3208 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); |
| 3209 | if (err) |
| 3210 | return err; |
| 3211 | |
| 3212 | if (off + size > *max_access) |
| 3213 | *max_access = off + size; |
| 3214 | |
| 3215 | return 0; |
| 3216 | } |
| 3217 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 3218 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
| 3219 | static void zext_32_to_64(struct bpf_reg_state *reg) |
| 3220 | { |
| 3221 | reg->var_off = tnum_subreg(reg->var_off); |
| 3222 | __reg_assign_32_into_64(reg); |
| 3223 | } |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3224 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3225 | /* truncate register to smaller size (in bytes) |
| 3226 | * must be called with size < BPF_REG_SIZE |
| 3227 | */ |
| 3228 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 3229 | { |
| 3230 | u64 mask; |
| 3231 | |
| 3232 | /* clear high bits in bit representation */ |
| 3233 | reg->var_off = tnum_cast(reg->var_off, size); |
| 3234 | |
| 3235 | /* fix arithmetic bounds */ |
| 3236 | mask = ((u64)1 << (size * 8)) - 1; |
| 3237 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 3238 | reg->umin_value &= mask; |
| 3239 | reg->umax_value &= mask; |
| 3240 | } else { |
| 3241 | reg->umin_value = 0; |
| 3242 | reg->umax_value = mask; |
| 3243 | } |
| 3244 | reg->smin_value = reg->umin_value; |
| 3245 | reg->smax_value = reg->umax_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 3246 | |
| 3247 | /* If size is smaller than 32bit register the 32bit register |
| 3248 | * values are also truncated so we push 64-bit bounds into |
| 3249 | * 32-bit bounds. Above were truncated < 32-bits already. |
| 3250 | */ |
| 3251 | if (size >= 4) |
| 3252 | return; |
| 3253 | __reg_combine_64_into_32(reg); |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3254 | } |
| 3255 | |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 3256 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
| 3257 | { |
| 3258 | return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen; |
| 3259 | } |
| 3260 | |
| 3261 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) |
| 3262 | { |
| 3263 | void *ptr; |
| 3264 | u64 addr; |
| 3265 | int err; |
| 3266 | |
| 3267 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 3268 | if (err) |
| 3269 | return err; |
Andrii Nakryiko | 2dedd7d | 2019-10-11 10:20:53 -0700 | [diff] [blame] | 3270 | ptr = (void *)(long)addr + off; |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 3271 | |
| 3272 | switch (size) { |
| 3273 | case sizeof(u8): |
| 3274 | *val = (u64)*(u8 *)ptr; |
| 3275 | break; |
| 3276 | case sizeof(u16): |
| 3277 | *val = (u64)*(u16 *)ptr; |
| 3278 | break; |
| 3279 | case sizeof(u32): |
| 3280 | *val = (u64)*(u32 *)ptr; |
| 3281 | break; |
| 3282 | case sizeof(u64): |
| 3283 | *val = *(u64 *)ptr; |
| 3284 | break; |
| 3285 | default: |
| 3286 | return -EINVAL; |
| 3287 | } |
| 3288 | return 0; |
| 3289 | } |
| 3290 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3291 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
| 3292 | struct bpf_reg_state *regs, |
| 3293 | int regno, int off, int size, |
| 3294 | enum bpf_access_type atype, |
| 3295 | int value_regno) |
| 3296 | { |
| 3297 | struct bpf_reg_state *reg = regs + regno; |
| 3298 | const struct btf_type *t = btf_type_by_id(btf_vmlinux, reg->btf_id); |
| 3299 | const char *tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 3300 | u32 btf_id; |
| 3301 | int ret; |
| 3302 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3303 | if (off < 0) { |
| 3304 | verbose(env, |
| 3305 | "R%d is ptr_%s invalid negative access: off=%d\n", |
| 3306 | regno, tname, off); |
| 3307 | return -EACCES; |
| 3308 | } |
| 3309 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 3310 | char tn_buf[48]; |
| 3311 | |
| 3312 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3313 | verbose(env, |
| 3314 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", |
| 3315 | regno, tname, off, tn_buf); |
| 3316 | return -EACCES; |
| 3317 | } |
| 3318 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 3319 | if (env->ops->btf_struct_access) { |
| 3320 | ret = env->ops->btf_struct_access(&env->log, t, off, size, |
| 3321 | atype, &btf_id); |
| 3322 | } else { |
| 3323 | if (atype != BPF_READ) { |
| 3324 | verbose(env, "only read is supported\n"); |
| 3325 | return -EACCES; |
| 3326 | } |
| 3327 | |
| 3328 | ret = btf_struct_access(&env->log, t, off, size, atype, |
| 3329 | &btf_id); |
| 3330 | } |
| 3331 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3332 | if (ret < 0) |
| 3333 | return ret; |
| 3334 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 3335 | if (atype == BPF_READ && value_regno >= 0) |
| 3336 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_id); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 3337 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3338 | return 0; |
| 3339 | } |
| 3340 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 3341 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, |
| 3342 | struct bpf_reg_state *regs, |
| 3343 | int regno, int off, int size, |
| 3344 | enum bpf_access_type atype, |
| 3345 | int value_regno) |
| 3346 | { |
| 3347 | struct bpf_reg_state *reg = regs + regno; |
| 3348 | struct bpf_map *map = reg->map_ptr; |
| 3349 | const struct btf_type *t; |
| 3350 | const char *tname; |
| 3351 | u32 btf_id; |
| 3352 | int ret; |
| 3353 | |
| 3354 | if (!btf_vmlinux) { |
| 3355 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); |
| 3356 | return -ENOTSUPP; |
| 3357 | } |
| 3358 | |
| 3359 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { |
| 3360 | verbose(env, "map_ptr access not supported for map type %d\n", |
| 3361 | map->map_type); |
| 3362 | return -ENOTSUPP; |
| 3363 | } |
| 3364 | |
| 3365 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); |
| 3366 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 3367 | |
| 3368 | if (!env->allow_ptr_to_map_access) { |
| 3369 | verbose(env, |
| 3370 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", |
| 3371 | tname); |
| 3372 | return -EPERM; |
| 3373 | } |
| 3374 | |
| 3375 | if (off < 0) { |
| 3376 | verbose(env, "R%d is %s invalid negative access: off=%d\n", |
| 3377 | regno, tname, off); |
| 3378 | return -EACCES; |
| 3379 | } |
| 3380 | |
| 3381 | if (atype != BPF_READ) { |
| 3382 | verbose(env, "only read from %s is supported\n", tname); |
| 3383 | return -EACCES; |
| 3384 | } |
| 3385 | |
| 3386 | ret = btf_struct_access(&env->log, t, off, size, atype, &btf_id); |
| 3387 | if (ret < 0) |
| 3388 | return ret; |
| 3389 | |
| 3390 | if (value_regno >= 0) |
| 3391 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_id); |
| 3392 | |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
| 3396 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3397 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 3398 | * if t==write, value_regno is a register which value is stored into memory |
| 3399 | * if t==read, value_regno is a register which will receive the value from memory |
| 3400 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 3401 | * if t==read && value_regno==-1, don't care what we read from memory |
| 3402 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3403 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 3404 | int off, int bpf_size, enum bpf_access_type t, |
| 3405 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3406 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3407 | struct bpf_reg_state *regs = cur_regs(env); |
| 3408 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3409 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3410 | int size, err = 0; |
| 3411 | |
| 3412 | size = bpf_size_to_bytes(bpf_size); |
| 3413 | if (size < 0) |
| 3414 | return size; |
| 3415 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3416 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3417 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3418 | if (err) |
| 3419 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3420 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3421 | /* for access checks, reg->off is just part of off */ |
| 3422 | off += reg->off; |
| 3423 | |
| 3424 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3425 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3426 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3427 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3428 | return -EACCES; |
| 3429 | } |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3430 | err = check_map_access_type(env, regno, off, size, t); |
| 3431 | if (err) |
| 3432 | return err; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3433 | err = check_map_access(env, regno, off, size, false); |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 3434 | if (!err && t == BPF_READ && value_regno >= 0) { |
| 3435 | struct bpf_map *map = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3436 | |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 3437 | /* if map is read-only, track its contents as scalars */ |
| 3438 | if (tnum_is_const(reg->var_off) && |
| 3439 | bpf_map_is_rdonly(map) && |
| 3440 | map->ops->map_direct_value_addr) { |
| 3441 | int map_off = off + reg->var_off.value; |
| 3442 | u64 val = 0; |
| 3443 | |
| 3444 | err = bpf_map_direct_read(map, map_off, size, |
| 3445 | &val); |
| 3446 | if (err) |
| 3447 | return err; |
| 3448 | |
| 3449 | regs[value_regno].type = SCALAR_VALUE; |
| 3450 | __mark_reg_known(®s[value_regno], val); |
| 3451 | } else { |
| 3452 | mark_reg_unknown(env, regs, value_regno); |
| 3453 | } |
| 3454 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3455 | } else if (reg->type == PTR_TO_MEM) { |
| 3456 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3457 | is_pointer_value(env, value_regno)) { |
| 3458 | verbose(env, "R%d leaks addr into mem\n", value_regno); |
| 3459 | return -EACCES; |
| 3460 | } |
| 3461 | err = check_mem_region_access(env, regno, off, size, |
| 3462 | reg->mem_size, false); |
| 3463 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3464 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3465 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3466 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3467 | u32 btf_id = 0; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 3468 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3469 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3470 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3471 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3472 | return -EACCES; |
| 3473 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3474 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 3475 | err = check_ctx_reg(env, reg, regno); |
| 3476 | if (err < 0) |
| 3477 | return err; |
| 3478 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3479 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf_id); |
| 3480 | if (err) |
| 3481 | verbose_linfo(env, insn_idx, "; "); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3482 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3483 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3484 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 3485 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3486 | */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3487 | if (reg_type == SCALAR_VALUE) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3488 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3489 | } else { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3490 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3491 | value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3492 | if (reg_type_may_be_null(reg_type)) |
| 3493 | regs[value_regno].id = ++env->id_gen; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 3494 | /* A load of ctx field could have different |
| 3495 | * actual load size with the one encoded in the |
| 3496 | * insn. When the dst is PTR, it is for sure not |
| 3497 | * a sub-register. |
| 3498 | */ |
| 3499 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 3500 | if (reg_type == PTR_TO_BTF_ID || |
| 3501 | reg_type == PTR_TO_BTF_ID_OR_NULL) |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3502 | regs[value_regno].btf_id = btf_id; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3503 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3504 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3505 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3506 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3507 | } else if (reg->type == PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3508 | off += reg->var_off.value; |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3509 | err = check_stack_access(env, reg, off, size); |
| 3510 | if (err) |
| 3511 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 3512 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3513 | state = func(env, reg); |
| 3514 | err = update_stack_depth(env, state, off); |
| 3515 | if (err) |
| 3516 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 3517 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3518 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3519 | err = check_stack_write(env, state, off, size, |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 3520 | value_regno, insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3521 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3522 | err = check_stack_read(env, state, off, size, |
| 3523 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3524 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 3525 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3526 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3527 | return -EACCES; |
| 3528 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3529 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3530 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3531 | verbose(env, "R%d leaks addr into packet\n", |
| 3532 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3533 | return -EACCES; |
| 3534 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3535 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3536 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3537 | mark_reg_unknown(env, regs, value_regno); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 3538 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 3539 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3540 | is_pointer_value(env, value_regno)) { |
| 3541 | verbose(env, "R%d leaks addr into flow keys\n", |
| 3542 | value_regno); |
| 3543 | return -EACCES; |
| 3544 | } |
| 3545 | |
| 3546 | err = check_flow_keys_access(env, off, size); |
| 3547 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3548 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3549 | } else if (type_is_sk_pointer(reg->type)) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3550 | if (t == BPF_WRITE) { |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3551 | verbose(env, "R%d cannot write into %s\n", |
| 3552 | regno, reg_type_str[reg->type]); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3553 | return -EACCES; |
| 3554 | } |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 3555 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3556 | if (!err && value_regno >= 0) |
| 3557 | mark_reg_unknown(env, regs, value_regno); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3558 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
| 3559 | err = check_tp_buffer_access(env, reg, regno, off, size); |
| 3560 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3561 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3562 | } else if (reg->type == PTR_TO_BTF_ID) { |
| 3563 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
| 3564 | value_regno); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 3565 | } else if (reg->type == CONST_PTR_TO_MAP) { |
| 3566 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, |
| 3567 | value_regno); |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3568 | } else if (reg->type == PTR_TO_RDONLY_BUF) { |
| 3569 | if (t == BPF_WRITE) { |
| 3570 | verbose(env, "R%d cannot write into %s\n", |
| 3571 | regno, reg_type_str[reg->type]); |
| 3572 | return -EACCES; |
| 3573 | } |
Colin Ian King | f6dfbe31 | 2020-07-27 18:54:11 +0100 | [diff] [blame] | 3574 | err = check_buffer_access(env, reg, regno, off, size, false, |
| 3575 | "rdonly", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3576 | &env->prog->aux->max_rdonly_access); |
| 3577 | if (!err && value_regno >= 0) |
| 3578 | mark_reg_unknown(env, regs, value_regno); |
| 3579 | } else if (reg->type == PTR_TO_RDWR_BUF) { |
Colin Ian King | f6dfbe31 | 2020-07-27 18:54:11 +0100 | [diff] [blame] | 3580 | err = check_buffer_access(env, reg, regno, off, size, false, |
| 3581 | "rdwr", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3582 | &env->prog->aux->max_rdwr_access); |
| 3583 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3584 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3585 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3586 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 3587 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3588 | return -EACCES; |
| 3589 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3590 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3591 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3592 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3593 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3594 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3595 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3596 | return err; |
| 3597 | } |
| 3598 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3599 | 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] | 3600 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3601 | int err; |
| 3602 | |
| 3603 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 3604 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3605 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3606 | return -EINVAL; |
| 3607 | } |
| 3608 | |
| 3609 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3610 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3611 | if (err) |
| 3612 | return err; |
| 3613 | |
| 3614 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3615 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3616 | if (err) |
| 3617 | return err; |
| 3618 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 3619 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3620 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 3621 | return -EACCES; |
| 3622 | } |
| 3623 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3624 | if (is_ctx_reg(env, insn->dst_reg) || |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 3625 | is_pkt_reg(env, insn->dst_reg) || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3626 | is_flow_key_reg(env, insn->dst_reg) || |
| 3627 | is_sk_reg(env, insn->dst_reg)) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3628 | 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] | 3629 | insn->dst_reg, |
| 3630 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 3631 | return -EACCES; |
| 3632 | } |
| 3633 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3634 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3635 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3636 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3637 | if (err) |
| 3638 | return err; |
| 3639 | |
| 3640 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3641 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3642 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3643 | } |
| 3644 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3645 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
| 3646 | int off, int access_size, |
| 3647 | bool zero_size_allowed) |
| 3648 | { |
| 3649 | struct bpf_reg_state *reg = reg_state(env, regno); |
| 3650 | |
| 3651 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 3652 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
| 3653 | if (tnum_is_const(reg->var_off)) { |
| 3654 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", |
| 3655 | regno, off, access_size); |
| 3656 | } else { |
| 3657 | char tn_buf[48]; |
| 3658 | |
| 3659 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3660 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", |
| 3661 | regno, tn_buf, access_size); |
| 3662 | } |
| 3663 | return -EACCES; |
| 3664 | } |
| 3665 | return 0; |
| 3666 | } |
| 3667 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3668 | /* when register 'regno' is passed into function that will read 'access_size' |
| 3669 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3670 | * and all elements of stack are initialized. |
| 3671 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 3672 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3673 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3674 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3675 | int access_size, bool zero_size_allowed, |
| 3676 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3677 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 3678 | struct bpf_reg_state *reg = reg_state(env, regno); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3679 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3680 | int err, min_off, max_off, i, j, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3681 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3682 | if (tnum_is_const(reg->var_off)) { |
| 3683 | min_off = max_off = reg->var_off.value + reg->off; |
| 3684 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 3685 | zero_size_allowed); |
| 3686 | if (err) |
| 3687 | return err; |
| 3688 | } else { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3689 | /* Variable offset is prohibited for unprivileged mode for |
| 3690 | * simplicity since it requires corresponding support in |
| 3691 | * Spectre masking for stack ALU. |
| 3692 | * See also retrieve_ptr_limit(). |
| 3693 | */ |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 3694 | if (!env->bypass_spec_v1) { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3695 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3696 | |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3697 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3698 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", |
| 3699 | regno, tn_buf); |
| 3700 | return -EACCES; |
| 3701 | } |
Andrey Ignatov | f2bcd05 | 2019-04-03 23:22:37 -0700 | [diff] [blame] | 3702 | /* Only initialized buffer on stack is allowed to be accessed |
| 3703 | * with variable offset. With uninitialized buffer it's hard to |
| 3704 | * guarantee that whole memory is marked as initialized on |
| 3705 | * helper return since specific bounds are unknown what may |
| 3706 | * cause uninitialized stack leaking. |
| 3707 | */ |
| 3708 | if (meta && meta->raw_mode) |
| 3709 | meta = NULL; |
| 3710 | |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3711 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
| 3712 | reg->smax_value <= -BPF_MAX_VAR_OFF) { |
| 3713 | verbose(env, "R%d unbounded indirect variable offset stack access\n", |
| 3714 | regno); |
| 3715 | return -EACCES; |
| 3716 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3717 | min_off = reg->smin_value + reg->off; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3718 | max_off = reg->smax_value + reg->off; |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3719 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 3720 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3721 | if (err) { |
| 3722 | verbose(env, "R%d min value is outside of stack bound\n", |
| 3723 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3724 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3725 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3726 | err = __check_stack_boundary(env, regno, max_off, access_size, |
| 3727 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3728 | if (err) { |
| 3729 | verbose(env, "R%d max value is outside of stack bound\n", |
| 3730 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3731 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3732 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3733 | } |
| 3734 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3735 | if (meta && meta->raw_mode) { |
| 3736 | meta->access_size = access_size; |
| 3737 | meta->regno = regno; |
| 3738 | return 0; |
| 3739 | } |
| 3740 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3741 | for (i = min_off; i < max_off + access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3742 | u8 *stype; |
| 3743 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3744 | slot = -i - 1; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3745 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3746 | if (state->allocated_stack <= slot) |
| 3747 | goto err; |
| 3748 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 3749 | if (*stype == STACK_MISC) |
| 3750 | goto mark; |
| 3751 | if (*stype == STACK_ZERO) { |
| 3752 | /* helper can write anything into the stack */ |
| 3753 | *stype = STACK_MISC; |
| 3754 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3755 | } |
Yonghong Song | 1d68f22 | 2020-05-09 10:59:15 -0700 | [diff] [blame] | 3756 | |
| 3757 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 3758 | state->stack[spi].spilled_ptr.type == PTR_TO_BTF_ID) |
| 3759 | goto mark; |
| 3760 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3761 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 3762 | state->stack[spi].spilled_ptr.type == SCALAR_VALUE) { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 3763 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3764 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 3765 | state->stack[spi].slot_type[j] = STACK_MISC; |
| 3766 | goto mark; |
| 3767 | } |
| 3768 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3769 | err: |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3770 | if (tnum_is_const(reg->var_off)) { |
| 3771 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 3772 | min_off, i - min_off, access_size); |
| 3773 | } else { |
| 3774 | char tn_buf[48]; |
| 3775 | |
| 3776 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3777 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", |
| 3778 | tn_buf, i - min_off, access_size); |
| 3779 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3780 | return -EACCES; |
| 3781 | mark: |
| 3782 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 3783 | * the whole slot to be marked as 'read' |
| 3784 | */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 3785 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 3786 | state->stack[spi].spilled_ptr.parent, |
| 3787 | REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3788 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3789 | return update_stack_depth(env, state, min_off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3790 | } |
| 3791 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3792 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 3793 | int access_size, bool zero_size_allowed, |
| 3794 | struct bpf_call_arg_meta *meta) |
| 3795 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3796 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3797 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3798 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3799 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3800 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3801 | return check_packet_access(env, regno, reg->off, access_size, |
| 3802 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3803 | case PTR_TO_MAP_VALUE: |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3804 | if (check_map_access_type(env, regno, reg->off, access_size, |
| 3805 | meta && meta->raw_mode ? BPF_WRITE : |
| 3806 | BPF_READ)) |
| 3807 | return -EACCES; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3808 | return check_map_access(env, regno, reg->off, access_size, |
| 3809 | zero_size_allowed); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3810 | case PTR_TO_MEM: |
| 3811 | return check_mem_region_access(env, regno, reg->off, |
| 3812 | access_size, reg->mem_size, |
| 3813 | zero_size_allowed); |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3814 | case PTR_TO_RDONLY_BUF: |
| 3815 | if (meta && meta->raw_mode) |
| 3816 | return -EACCES; |
| 3817 | return check_buffer_access(env, reg, regno, reg->off, |
| 3818 | access_size, zero_size_allowed, |
| 3819 | "rdonly", |
| 3820 | &env->prog->aux->max_rdonly_access); |
| 3821 | case PTR_TO_RDWR_BUF: |
| 3822 | return check_buffer_access(env, reg, regno, reg->off, |
| 3823 | access_size, zero_size_allowed, |
| 3824 | "rdwr", |
| 3825 | &env->prog->aux->max_rdwr_access); |
Lorenz Bauer | 0d004c02 | 2020-09-21 13:12:18 +0100 | [diff] [blame] | 3826 | case PTR_TO_STACK: |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3827 | return check_stack_boundary(env, regno, access_size, |
| 3828 | zero_size_allowed, meta); |
Lorenz Bauer | 0d004c02 | 2020-09-21 13:12:18 +0100 | [diff] [blame] | 3829 | default: /* scalar_value or invalid ptr */ |
| 3830 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
| 3831 | if (zero_size_allowed && access_size == 0 && |
| 3832 | register_is_null(reg)) |
| 3833 | return 0; |
| 3834 | |
| 3835 | verbose(env, "R%d type=%s expected=%s\n", regno, |
| 3836 | reg_type_str[reg->type], |
| 3837 | reg_type_str[PTR_TO_STACK]); |
| 3838 | return -EACCES; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3839 | } |
| 3840 | } |
| 3841 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3842 | /* Implementation details: |
| 3843 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL |
| 3844 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
| 3845 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after |
| 3846 | * value_or_null->value transition, since the verifier only cares about |
| 3847 | * the range of access to valid map value pointer and doesn't care about actual |
| 3848 | * address of the map element. |
| 3849 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
| 3850 | * reg->id > 0 after value_or_null->value transition. By doing so |
| 3851 | * two bpf_map_lookups will be considered two different pointers that |
| 3852 | * point to different bpf_spin_locks. |
| 3853 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
| 3854 | * dead-locks. |
| 3855 | * Since only one bpf_spin_lock is allowed the checks are simpler than |
| 3856 | * reg_is_refcounted() logic. The verifier needs to remember only |
| 3857 | * one spin_lock instead of array of acquired_refs. |
| 3858 | * cur_state->active_spin_lock remembers which map value element got locked |
| 3859 | * and clears it after bpf_spin_unlock. |
| 3860 | */ |
| 3861 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, |
| 3862 | bool is_lock) |
| 3863 | { |
| 3864 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 3865 | struct bpf_verifier_state *cur = env->cur_state; |
| 3866 | bool is_const = tnum_is_const(reg->var_off); |
| 3867 | struct bpf_map *map = reg->map_ptr; |
| 3868 | u64 val = reg->var_off.value; |
| 3869 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3870 | if (!is_const) { |
| 3871 | verbose(env, |
| 3872 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", |
| 3873 | regno); |
| 3874 | return -EINVAL; |
| 3875 | } |
| 3876 | if (!map->btf) { |
| 3877 | verbose(env, |
| 3878 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", |
| 3879 | map->name); |
| 3880 | return -EINVAL; |
| 3881 | } |
| 3882 | if (!map_value_has_spin_lock(map)) { |
| 3883 | if (map->spin_lock_off == -E2BIG) |
| 3884 | verbose(env, |
| 3885 | "map '%s' has more than one 'struct bpf_spin_lock'\n", |
| 3886 | map->name); |
| 3887 | else if (map->spin_lock_off == -ENOENT) |
| 3888 | verbose(env, |
| 3889 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", |
| 3890 | map->name); |
| 3891 | else |
| 3892 | verbose(env, |
| 3893 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", |
| 3894 | map->name); |
| 3895 | return -EINVAL; |
| 3896 | } |
| 3897 | if (map->spin_lock_off != val + reg->off) { |
| 3898 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", |
| 3899 | val + reg->off); |
| 3900 | return -EINVAL; |
| 3901 | } |
| 3902 | if (is_lock) { |
| 3903 | if (cur->active_spin_lock) { |
| 3904 | verbose(env, |
| 3905 | "Locking two bpf_spin_locks are not allowed\n"); |
| 3906 | return -EINVAL; |
| 3907 | } |
| 3908 | cur->active_spin_lock = reg->id; |
| 3909 | } else { |
| 3910 | if (!cur->active_spin_lock) { |
| 3911 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
| 3912 | return -EINVAL; |
| 3913 | } |
| 3914 | if (cur->active_spin_lock != reg->id) { |
| 3915 | verbose(env, "bpf_spin_unlock of different lock\n"); |
| 3916 | return -EINVAL; |
| 3917 | } |
| 3918 | cur->active_spin_lock = 0; |
| 3919 | } |
| 3920 | return 0; |
| 3921 | } |
| 3922 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 3923 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 3924 | { |
| 3925 | return type == ARG_PTR_TO_MEM || |
| 3926 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 3927 | type == ARG_PTR_TO_UNINIT_MEM; |
| 3928 | } |
| 3929 | |
| 3930 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 3931 | { |
| 3932 | return type == ARG_CONST_SIZE || |
| 3933 | type == ARG_CONST_SIZE_OR_ZERO; |
| 3934 | } |
| 3935 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3936 | static bool arg_type_is_alloc_size(enum bpf_arg_type type) |
| 3937 | { |
| 3938 | return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; |
| 3939 | } |
| 3940 | |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 3941 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
| 3942 | { |
| 3943 | return type == ARG_PTR_TO_INT || |
| 3944 | type == ARG_PTR_TO_LONG; |
| 3945 | } |
| 3946 | |
| 3947 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
| 3948 | { |
| 3949 | if (type == ARG_PTR_TO_INT) |
| 3950 | return sizeof(u32); |
| 3951 | else if (type == ARG_PTR_TO_LONG) |
| 3952 | return sizeof(u64); |
| 3953 | |
| 3954 | return -EINVAL; |
| 3955 | } |
| 3956 | |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 3957 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
| 3958 | const struct bpf_call_arg_meta *meta, |
| 3959 | enum bpf_arg_type *arg_type) |
| 3960 | { |
| 3961 | if (!meta->map_ptr) { |
| 3962 | /* kernel subsystem misconfigured verifier */ |
| 3963 | verbose(env, "invalid map_ptr to access map->type\n"); |
| 3964 | return -EACCES; |
| 3965 | } |
| 3966 | |
| 3967 | switch (meta->map_ptr->map_type) { |
| 3968 | case BPF_MAP_TYPE_SOCKMAP: |
| 3969 | case BPF_MAP_TYPE_SOCKHASH: |
| 3970 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { |
Lorenz Bauer | 6550f2d | 2020-09-28 10:08:02 +0100 | [diff] [blame] | 3971 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 3972 | } else { |
| 3973 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); |
| 3974 | return -EINVAL; |
| 3975 | } |
| 3976 | break; |
| 3977 | |
| 3978 | default: |
| 3979 | break; |
| 3980 | } |
| 3981 | return 0; |
| 3982 | } |
| 3983 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 3984 | struct bpf_reg_types { |
| 3985 | const enum bpf_reg_type types[10]; |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 3986 | u32 *btf_id; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 3987 | }; |
| 3988 | |
| 3989 | static const struct bpf_reg_types map_key_value_types = { |
| 3990 | .types = { |
| 3991 | PTR_TO_STACK, |
| 3992 | PTR_TO_PACKET, |
| 3993 | PTR_TO_PACKET_META, |
| 3994 | PTR_TO_MAP_VALUE, |
| 3995 | }, |
| 3996 | }; |
| 3997 | |
| 3998 | static const struct bpf_reg_types sock_types = { |
| 3999 | .types = { |
| 4000 | PTR_TO_SOCK_COMMON, |
| 4001 | PTR_TO_SOCKET, |
| 4002 | PTR_TO_TCP_SOCK, |
| 4003 | PTR_TO_XDP_SOCK, |
| 4004 | }, |
| 4005 | }; |
| 4006 | |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 4007 | #ifdef CONFIG_NET |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4008 | static const struct bpf_reg_types btf_id_sock_common_types = { |
| 4009 | .types = { |
| 4010 | PTR_TO_SOCK_COMMON, |
| 4011 | PTR_TO_SOCKET, |
| 4012 | PTR_TO_TCP_SOCK, |
| 4013 | PTR_TO_XDP_SOCK, |
| 4014 | PTR_TO_BTF_ID, |
| 4015 | }, |
| 4016 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], |
| 4017 | }; |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 4018 | #endif |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4019 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4020 | static const struct bpf_reg_types mem_types = { |
| 4021 | .types = { |
| 4022 | PTR_TO_STACK, |
| 4023 | PTR_TO_PACKET, |
| 4024 | PTR_TO_PACKET_META, |
| 4025 | PTR_TO_MAP_VALUE, |
| 4026 | PTR_TO_MEM, |
| 4027 | PTR_TO_RDONLY_BUF, |
| 4028 | PTR_TO_RDWR_BUF, |
| 4029 | }, |
| 4030 | }; |
| 4031 | |
| 4032 | static const struct bpf_reg_types int_ptr_types = { |
| 4033 | .types = { |
| 4034 | PTR_TO_STACK, |
| 4035 | PTR_TO_PACKET, |
| 4036 | PTR_TO_PACKET_META, |
| 4037 | PTR_TO_MAP_VALUE, |
| 4038 | }, |
| 4039 | }; |
| 4040 | |
| 4041 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; |
| 4042 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; |
| 4043 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; |
| 4044 | static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } }; |
| 4045 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; |
| 4046 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; |
| 4047 | 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] | 4048 | 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] | 4049 | |
Lorenz Bauer | 0789e13b | 2020-09-23 17:01:55 +0100 | [diff] [blame] | 4050 | 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] | 4051 | [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, |
| 4052 | [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, |
| 4053 | [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, |
| 4054 | [ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types, |
| 4055 | [ARG_CONST_SIZE] = &scalar_types, |
| 4056 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, |
| 4057 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, |
| 4058 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, |
| 4059 | [ARG_PTR_TO_CTX] = &context_types, |
| 4060 | [ARG_PTR_TO_CTX_OR_NULL] = &context_types, |
| 4061 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 4062 | #ifdef CONFIG_NET |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4063 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 4064 | #endif |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4065 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
| 4066 | [ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types, |
| 4067 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
| 4068 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, |
| 4069 | [ARG_PTR_TO_MEM] = &mem_types, |
| 4070 | [ARG_PTR_TO_MEM_OR_NULL] = &mem_types, |
| 4071 | [ARG_PTR_TO_UNINIT_MEM] = &mem_types, |
| 4072 | [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, |
| 4073 | [ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types, |
| 4074 | [ARG_PTR_TO_INT] = &int_ptr_types, |
| 4075 | [ARG_PTR_TO_LONG] = &int_ptr_types, |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 4076 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4077 | }; |
| 4078 | |
| 4079 | 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] | 4080 | enum bpf_arg_type arg_type, |
| 4081 | const u32 *arg_btf_id) |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4082 | { |
| 4083 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 4084 | enum bpf_reg_type expected, type = reg->type; |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4085 | const struct bpf_reg_types *compatible; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4086 | int i, j; |
| 4087 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4088 | compatible = compatible_reg_types[arg_type]; |
| 4089 | if (!compatible) { |
| 4090 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); |
| 4091 | return -EFAULT; |
| 4092 | } |
| 4093 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4094 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
| 4095 | expected = compatible->types[i]; |
| 4096 | if (expected == NOT_INIT) |
| 4097 | break; |
| 4098 | |
| 4099 | if (type == expected) |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4100 | goto found; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | verbose(env, "R%d type=%s expected=", regno, reg_type_str[type]); |
| 4104 | for (j = 0; j + 1 < i; j++) |
| 4105 | verbose(env, "%s, ", reg_type_str[compatible->types[j]]); |
| 4106 | verbose(env, "%s\n", reg_type_str[compatible->types[j]]); |
| 4107 | return -EACCES; |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4108 | |
| 4109 | found: |
| 4110 | if (type == PTR_TO_BTF_ID) { |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4111 | if (!arg_btf_id) { |
| 4112 | if (!compatible->btf_id) { |
| 4113 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); |
| 4114 | return -EFAULT; |
| 4115 | } |
| 4116 | arg_btf_id = compatible->btf_id; |
| 4117 | } |
| 4118 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4119 | if (!btf_struct_ids_match(&env->log, reg->off, reg->btf_id, |
| 4120 | *arg_btf_id)) { |
| 4121 | verbose(env, "R%d is of type %s but %s is expected\n", |
| 4122 | regno, kernel_type_name(reg->btf_id), |
| 4123 | kernel_type_name(*arg_btf_id)); |
| 4124 | return -EACCES; |
| 4125 | } |
| 4126 | |
| 4127 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 4128 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", |
| 4129 | regno); |
| 4130 | return -EACCES; |
| 4131 | } |
| 4132 | } |
| 4133 | |
| 4134 | return 0; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4135 | } |
| 4136 | |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 4137 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
| 4138 | struct bpf_call_arg_meta *meta, |
| 4139 | const struct bpf_func_proto *fn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4140 | { |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 4141 | u32 regno = BPF_REG_1 + arg; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4142 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 4143 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4144 | enum bpf_reg_type type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4145 | int err = 0; |
| 4146 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 4147 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4148 | return 0; |
| 4149 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4150 | err = check_reg_arg(env, regno, SRC_OP); |
| 4151 | if (err) |
| 4152 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4153 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4154 | if (arg_type == ARG_ANYTHING) { |
| 4155 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4156 | verbose(env, "R%d leaks addr into helper function\n", |
| 4157 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4158 | return -EACCES; |
| 4159 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 4160 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4161 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 4162 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4163 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 4164 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4165 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 4166 | return -EACCES; |
| 4167 | } |
| 4168 | |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 4169 | if (arg_type == ARG_PTR_TO_MAP_VALUE || |
| 4170 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
| 4171 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { |
| 4172 | err = resolve_map_arg_type(env, meta, &arg_type); |
| 4173 | if (err) |
| 4174 | return err; |
| 4175 | } |
| 4176 | |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 4177 | if (register_is_null(reg) && arg_type_may_be_null(arg_type)) |
| 4178 | /* A NULL register has a SCALAR_VALUE type, so skip |
| 4179 | * type checking. |
| 4180 | */ |
| 4181 | goto skip_type_check; |
Jiri Olsa | faaf4a7 | 2020-08-25 21:21:18 +0200 | [diff] [blame] | 4182 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4183 | 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] | 4184 | if (err) |
| 4185 | return err; |
| 4186 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4187 | if (type == PTR_TO_CTX) { |
Lorenz Bauer | feec704 | 2020-09-21 13:12:23 +0100 | [diff] [blame] | 4188 | err = check_ctx_reg(env, reg, regno); |
| 4189 | if (err < 0) |
| 4190 | return err; |
Lorenz Bauer | d7b9454 | 2020-09-21 13:12:21 +0100 | [diff] [blame] | 4191 | } |
| 4192 | |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 4193 | skip_type_check: |
Lorenz Bauer | 02f7c95 | 2020-09-21 13:12:22 +0100 | [diff] [blame] | 4194 | if (reg->ref_obj_id) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4195 | if (meta->ref_obj_id) { |
| 4196 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", |
| 4197 | regno, reg->ref_obj_id, |
| 4198 | meta->ref_obj_id); |
| 4199 | return -EFAULT; |
| 4200 | } |
| 4201 | meta->ref_obj_id = reg->ref_obj_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4202 | } |
| 4203 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4204 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 4205 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4206 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4207 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 4208 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 4209 | * check that [key, key + map->key_size) are within |
| 4210 | * stack limits and initialized |
| 4211 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4212 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4213 | /* in function declaration map_ptr must come before |
| 4214 | * map_key, so that it's verified and known before |
| 4215 | * we have to check map_key here. Otherwise it means |
| 4216 | * that kernel subsystem misconfigured verifier |
| 4217 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4218 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4219 | return -EACCES; |
| 4220 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 4221 | err = check_helper_mem_access(env, regno, |
| 4222 | meta->map_ptr->key_size, false, |
| 4223 | NULL); |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 4224 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 4225 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
| 4226 | !register_is_null(reg)) || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 4227 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4228 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 4229 | * check [value, value + map->value_size) validity |
| 4230 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4231 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4232 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4233 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4234 | return -EACCES; |
| 4235 | } |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 4236 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 4237 | err = check_helper_mem_access(env, regno, |
| 4238 | meta->map_ptr->value_size, false, |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 4239 | meta); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 4240 | } else if (arg_type == ARG_PTR_TO_PERCPU_BTF_ID) { |
| 4241 | if (!reg->btf_id) { |
| 4242 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); |
| 4243 | return -EACCES; |
| 4244 | } |
| 4245 | meta->ret_btf_id = reg->btf_id; |
Lorenz Bauer | c18f0b6 | 2020-09-21 13:12:25 +0100 | [diff] [blame] | 4246 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
| 4247 | if (meta->func_id == BPF_FUNC_spin_lock) { |
| 4248 | if (process_spin_lock(env, regno, true)) |
| 4249 | return -EACCES; |
| 4250 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
| 4251 | if (process_spin_lock(env, regno, false)) |
| 4252 | return -EACCES; |
| 4253 | } else { |
| 4254 | verbose(env, "verifier internal error\n"); |
| 4255 | return -EFAULT; |
| 4256 | } |
Lorenz Bauer | a2bbe7c | 2020-09-21 13:12:24 +0100 | [diff] [blame] | 4257 | } else if (arg_type_is_mem_ptr(arg_type)) { |
| 4258 | /* The access to this pointer is only checked when we hit the |
| 4259 | * next is_mem_size argument below. |
| 4260 | */ |
| 4261 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 4262 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4263 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4264 | |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 4265 | /* This is used to refine r0 return value bounds for helpers |
| 4266 | * that enforce this value as an upper bound on return values. |
| 4267 | * See do_refine_retval_range() for helpers that can refine |
| 4268 | * the return value. C type of helper is u32 so we pull register |
| 4269 | * bound from umax_value however, if negative verifier errors |
| 4270 | * out. Only upper bounds can be learned because retval is an |
| 4271 | * int type and negative retvals are allowed. |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4272 | */ |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 4273 | meta->msize_max_value = reg->umax_value; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4274 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4275 | /* The register is SCALAR_VALUE; the access check |
| 4276 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4277 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4278 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4279 | /* For unprivileged variable accesses, disable raw |
| 4280 | * mode so that the program is required to |
| 4281 | * initialize all the memory that the helper could |
| 4282 | * just partially fill up. |
| 4283 | */ |
| 4284 | meta = NULL; |
| 4285 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4286 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4287 | 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] | 4288 | regno); |
| 4289 | return -EACCES; |
| 4290 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4291 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4292 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4293 | err = check_helper_mem_access(env, regno - 1, 0, |
| 4294 | zero_size_allowed, |
| 4295 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4296 | if (err) |
| 4297 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4298 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4299 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4300 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4301 | 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] | 4302 | regno); |
| 4303 | return -EACCES; |
| 4304 | } |
| 4305 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4306 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4307 | zero_size_allowed, meta); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 4308 | if (!err) |
| 4309 | err = mark_chain_precision(env, regno); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4310 | } else if (arg_type_is_alloc_size(arg_type)) { |
| 4311 | if (!tnum_is_const(reg->var_off)) { |
| 4312 | verbose(env, "R%d unbounded size, use 'var &= const' or 'if (var < const)'\n", |
| 4313 | regno); |
| 4314 | return -EACCES; |
| 4315 | } |
| 4316 | meta->mem_size = reg->var_off.value; |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 4317 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 4318 | int size = int_ptr_type_to_size(arg_type); |
| 4319 | |
| 4320 | err = check_helper_mem_access(env, regno, size, false, meta); |
| 4321 | if (err) |
| 4322 | return err; |
| 4323 | err = check_ptr_alignment(env, reg, 0, size, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4324 | } |
| 4325 | |
| 4326 | return err; |
| 4327 | } |
| 4328 | |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 4329 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
| 4330 | { |
| 4331 | enum bpf_attach_type eatype = env->prog->expected_attach_type; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 4332 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 4333 | |
| 4334 | if (func_id != BPF_FUNC_map_update_elem) |
| 4335 | return false; |
| 4336 | |
| 4337 | /* It's not possible to get access to a locked struct sock in these |
| 4338 | * contexts, so updating is safe. |
| 4339 | */ |
| 4340 | switch (type) { |
| 4341 | case BPF_PROG_TYPE_TRACING: |
| 4342 | if (eatype == BPF_TRACE_ITER) |
| 4343 | return true; |
| 4344 | break; |
| 4345 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 4346 | case BPF_PROG_TYPE_SCHED_CLS: |
| 4347 | case BPF_PROG_TYPE_SCHED_ACT: |
| 4348 | case BPF_PROG_TYPE_XDP: |
| 4349 | case BPF_PROG_TYPE_SK_REUSEPORT: |
| 4350 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
| 4351 | case BPF_PROG_TYPE_SK_LOOKUP: |
| 4352 | return true; |
| 4353 | default: |
| 4354 | break; |
| 4355 | } |
| 4356 | |
| 4357 | verbose(env, "cannot update sockmap in this context\n"); |
| 4358 | return false; |
| 4359 | } |
| 4360 | |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 4361 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
| 4362 | { |
| 4363 | return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); |
| 4364 | } |
| 4365 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4366 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 4367 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 4368 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 4369 | if (!map) |
| 4370 | return 0; |
| 4371 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4372 | /* We need a two way check, first is from map perspective ... */ |
| 4373 | switch (map->map_type) { |
| 4374 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 4375 | if (func_id != BPF_FUNC_tail_call) |
| 4376 | goto error; |
| 4377 | break; |
| 4378 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 4379 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 4380 | func_id != BPF_FUNC_perf_event_output && |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 4381 | func_id != BPF_FUNC_skb_output && |
Eelco Chaudron | d831ee8 | 2020-03-06 08:59:23 +0000 | [diff] [blame] | 4382 | func_id != BPF_FUNC_perf_event_read_value && |
| 4383 | func_id != BPF_FUNC_xdp_output) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4384 | goto error; |
| 4385 | break; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4386 | case BPF_MAP_TYPE_RINGBUF: |
| 4387 | if (func_id != BPF_FUNC_ringbuf_output && |
| 4388 | func_id != BPF_FUNC_ringbuf_reserve && |
| 4389 | func_id != BPF_FUNC_ringbuf_submit && |
| 4390 | func_id != BPF_FUNC_ringbuf_discard && |
| 4391 | func_id != BPF_FUNC_ringbuf_query) |
| 4392 | goto error; |
| 4393 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4394 | case BPF_MAP_TYPE_STACK_TRACE: |
| 4395 | if (func_id != BPF_FUNC_get_stackid) |
| 4396 | goto error; |
| 4397 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 4398 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 4399 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 4400 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 4401 | goto error; |
| 4402 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 4403 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 4404 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 4405 | if (func_id != BPF_FUNC_get_local_storage) |
| 4406 | goto error; |
| 4407 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 4408 | case BPF_MAP_TYPE_DEVMAP: |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 4409 | case BPF_MAP_TYPE_DEVMAP_HASH: |
Toke Høiland-Jørgensen | 0cdbb4b | 2019-06-28 11:12:35 +0200 | [diff] [blame] | 4410 | if (func_id != BPF_FUNC_redirect_map && |
| 4411 | func_id != BPF_FUNC_map_lookup_elem) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 4412 | goto error; |
| 4413 | break; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 4414 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 4415 | * appear. |
| 4416 | */ |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 4417 | case BPF_MAP_TYPE_CPUMAP: |
| 4418 | if (func_id != BPF_FUNC_redirect_map) |
| 4419 | goto error; |
| 4420 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 4421 | case BPF_MAP_TYPE_XSKMAP: |
| 4422 | if (func_id != BPF_FUNC_redirect_map && |
| 4423 | func_id != BPF_FUNC_map_lookup_elem) |
| 4424 | goto error; |
| 4425 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4426 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 4427 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4428 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 4429 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 4430 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4431 | case BPF_MAP_TYPE_SOCKMAP: |
| 4432 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 4433 | func_id != BPF_FUNC_sock_map_update && |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 4434 | func_id != BPF_FUNC_map_delete_elem && |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 4435 | func_id != BPF_FUNC_msg_redirect_map && |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 4436 | func_id != BPF_FUNC_sk_select_reuseport && |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 4437 | func_id != BPF_FUNC_map_lookup_elem && |
| 4438 | !may_update_sockmap(env, func_id)) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4439 | goto error; |
| 4440 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 4441 | case BPF_MAP_TYPE_SOCKHASH: |
| 4442 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 4443 | func_id != BPF_FUNC_sock_hash_update && |
| 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_hash && |
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 | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 4449 | goto error; |
| 4450 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 4451 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 4452 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 4453 | goto error; |
| 4454 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 4455 | case BPF_MAP_TYPE_QUEUE: |
| 4456 | case BPF_MAP_TYPE_STACK: |
| 4457 | if (func_id != BPF_FUNC_map_peek_elem && |
| 4458 | func_id != BPF_FUNC_map_pop_elem && |
| 4459 | func_id != BPF_FUNC_map_push_elem) |
| 4460 | goto error; |
| 4461 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 4462 | case BPF_MAP_TYPE_SK_STORAGE: |
| 4463 | if (func_id != BPF_FUNC_sk_storage_get && |
| 4464 | func_id != BPF_FUNC_sk_storage_delete) |
| 4465 | goto error; |
| 4466 | break; |
KP Singh | 8ea6368 | 2020-08-25 20:29:17 +0200 | [diff] [blame] | 4467 | case BPF_MAP_TYPE_INODE_STORAGE: |
| 4468 | if (func_id != BPF_FUNC_inode_storage_get && |
| 4469 | func_id != BPF_FUNC_inode_storage_delete) |
| 4470 | goto error; |
| 4471 | break; |
KP Singh | 4cf1bc1 | 2020-11-06 10:37:40 +0000 | [diff] [blame^] | 4472 | case BPF_MAP_TYPE_TASK_STORAGE: |
| 4473 | if (func_id != BPF_FUNC_task_storage_get && |
| 4474 | func_id != BPF_FUNC_task_storage_delete) |
| 4475 | goto error; |
| 4476 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4477 | default: |
| 4478 | break; |
| 4479 | } |
| 4480 | |
| 4481 | /* ... and second from the function itself. */ |
| 4482 | switch (func_id) { |
| 4483 | case BPF_FUNC_tail_call: |
| 4484 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 4485 | goto error; |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 4486 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
| 4487 | 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] | 4488 | return -EINVAL; |
| 4489 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4490 | break; |
| 4491 | case BPF_FUNC_perf_event_read: |
| 4492 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 4493 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 4494 | case BPF_FUNC_skb_output: |
Eelco Chaudron | d831ee8 | 2020-03-06 08:59:23 +0000 | [diff] [blame] | 4495 | case BPF_FUNC_xdp_output: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4496 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 4497 | goto error; |
| 4498 | break; |
| 4499 | case BPF_FUNC_get_stackid: |
| 4500 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 4501 | goto error; |
| 4502 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 4503 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 4504 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 4505 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 4506 | goto error; |
| 4507 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 4508 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 4509 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 4510 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 4511 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 4512 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 4513 | goto error; |
| 4514 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4515 | case BPF_FUNC_sk_redirect_map: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 4516 | case BPF_FUNC_msg_redirect_map: |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 4517 | case BPF_FUNC_sock_map_update: |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4518 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 4519 | goto error; |
| 4520 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 4521 | case BPF_FUNC_sk_redirect_hash: |
| 4522 | case BPF_FUNC_msg_redirect_hash: |
| 4523 | case BPF_FUNC_sock_hash_update: |
| 4524 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4525 | goto error; |
| 4526 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 4527 | case BPF_FUNC_get_local_storage: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 4528 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 4529 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 4530 | goto error; |
| 4531 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 4532 | case BPF_FUNC_sk_select_reuseport: |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 4533 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
| 4534 | map->map_type != BPF_MAP_TYPE_SOCKMAP && |
| 4535 | map->map_type != BPF_MAP_TYPE_SOCKHASH) |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 4536 | goto error; |
| 4537 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 4538 | case BPF_FUNC_map_peek_elem: |
| 4539 | case BPF_FUNC_map_pop_elem: |
| 4540 | case BPF_FUNC_map_push_elem: |
| 4541 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 4542 | map->map_type != BPF_MAP_TYPE_STACK) |
| 4543 | goto error; |
| 4544 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 4545 | case BPF_FUNC_sk_storage_get: |
| 4546 | case BPF_FUNC_sk_storage_delete: |
| 4547 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
| 4548 | goto error; |
| 4549 | break; |
KP Singh | 8ea6368 | 2020-08-25 20:29:17 +0200 | [diff] [blame] | 4550 | case BPF_FUNC_inode_storage_get: |
| 4551 | case BPF_FUNC_inode_storage_delete: |
| 4552 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) |
| 4553 | goto error; |
| 4554 | break; |
KP Singh | 4cf1bc1 | 2020-11-06 10:37:40 +0000 | [diff] [blame^] | 4555 | case BPF_FUNC_task_storage_get: |
| 4556 | case BPF_FUNC_task_storage_delete: |
| 4557 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) |
| 4558 | goto error; |
| 4559 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4560 | default: |
| 4561 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4565 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4566 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 4567 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4568 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 4571 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4572 | { |
| 4573 | int count = 0; |
| 4574 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4575 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4576 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4577 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4578 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4579 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4580 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4581 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4582 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4583 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4584 | count++; |
| 4585 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 4586 | /* We only support one arg being in raw mode at the moment, |
| 4587 | * which is sufficient for the helper functions we have |
| 4588 | * right now. |
| 4589 | */ |
| 4590 | return count <= 1; |
| 4591 | } |
| 4592 | |
| 4593 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 4594 | enum bpf_arg_type arg_next) |
| 4595 | { |
| 4596 | return (arg_type_is_mem_ptr(arg_curr) && |
| 4597 | !arg_type_is_mem_size(arg_next)) || |
| 4598 | (!arg_type_is_mem_ptr(arg_curr) && |
| 4599 | arg_type_is_mem_size(arg_next)); |
| 4600 | } |
| 4601 | |
| 4602 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 4603 | { |
| 4604 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 4605 | * bytes from memory 'buf'. Both arg types need |
| 4606 | * to be paired, so make sure there's no buggy |
| 4607 | * helper function specification. |
| 4608 | */ |
| 4609 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 4610 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 4611 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 4612 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 4613 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 4614 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 4615 | return false; |
| 4616 | |
| 4617 | return true; |
| 4618 | } |
| 4619 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4620 | 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] | 4621 | { |
| 4622 | int count = 0; |
| 4623 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4624 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4625 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4626 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4627 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4628 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4629 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4630 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4631 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4632 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4633 | count++; |
| 4634 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4635 | /* A reference acquiring function cannot acquire |
| 4636 | * another refcounted ptr. |
| 4637 | */ |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 4638 | if (may_be_acquire_function(func_id) && count) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4639 | return false; |
| 4640 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4641 | /* We only support one arg being unreferenced at the moment, |
| 4642 | * which is sufficient for the helper functions we have right now. |
| 4643 | */ |
| 4644 | return count <= 1; |
| 4645 | } |
| 4646 | |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 4647 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
| 4648 | { |
| 4649 | int i; |
| 4650 | |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4651 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 4652 | if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) |
| 4653 | return false; |
| 4654 | |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4655 | if (fn->arg_type[i] != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i]) |
| 4656 | return false; |
| 4657 | } |
| 4658 | |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 4659 | return true; |
| 4660 | } |
| 4661 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4662 | 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] | 4663 | { |
| 4664 | return check_raw_mode_ok(fn) && |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4665 | check_arg_pair_ok(fn) && |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 4666 | check_btf_id_ok(fn) && |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4667 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4668 | } |
| 4669 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4670 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 4671 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4672 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4673 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 4674 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4675 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4676 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4677 | int i; |
| 4678 | |
| 4679 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4680 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4681 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4682 | |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 4683 | bpf_for_each_spilled_reg(i, state, reg) { |
| 4684 | if (!reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4685 | continue; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4686 | if (reg_is_pkt_pointer_any(reg)) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 4687 | __mark_reg_unknown(env, reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4688 | } |
| 4689 | } |
| 4690 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4691 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 4692 | { |
| 4693 | struct bpf_verifier_state *vstate = env->cur_state; |
| 4694 | int i; |
| 4695 | |
| 4696 | for (i = 0; i <= vstate->curframe; i++) |
| 4697 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 4698 | } |
| 4699 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4700 | static void release_reg_references(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4701 | struct bpf_func_state *state, |
| 4702 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4703 | { |
| 4704 | struct bpf_reg_state *regs = state->regs, *reg; |
| 4705 | int i; |
| 4706 | |
| 4707 | for (i = 0; i < MAX_BPF_REG; i++) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4708 | if (regs[i].ref_obj_id == ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4709 | mark_reg_unknown(env, regs, i); |
| 4710 | |
| 4711 | bpf_for_each_spilled_reg(i, state, reg) { |
| 4712 | if (!reg) |
| 4713 | continue; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4714 | if (reg->ref_obj_id == ref_obj_id) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 4715 | __mark_reg_unknown(env, reg); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4716 | } |
| 4717 | } |
| 4718 | |
| 4719 | /* The pointer with the specified id has released its reference to kernel |
| 4720 | * resources. Identify all copies of the same pointer and clear the reference. |
| 4721 | */ |
| 4722 | static int release_reference(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4723 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4724 | { |
| 4725 | struct bpf_verifier_state *vstate = env->cur_state; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4726 | int err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4727 | int i; |
| 4728 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4729 | err = release_reference_state(cur_func(env), ref_obj_id); |
| 4730 | if (err) |
| 4731 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4732 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4733 | for (i = 0; i <= vstate->curframe; i++) |
| 4734 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
| 4735 | |
| 4736 | return 0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4737 | } |
| 4738 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4739 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
| 4740 | struct bpf_reg_state *regs) |
| 4741 | { |
| 4742 | int i; |
| 4743 | |
| 4744 | /* after the call registers r0 - r5 were scratched */ |
| 4745 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 4746 | mark_reg_not_init(env, regs, caller_saved[i]); |
| 4747 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 4748 | } |
| 4749 | } |
| 4750 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4751 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 4752 | int *insn_idx) |
| 4753 | { |
| 4754 | struct bpf_verifier_state *state = env->cur_state; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4755 | struct bpf_func_info_aux *func_info_aux; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4756 | struct bpf_func_state *caller, *callee; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4757 | int i, err, subprog, target_insn; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4758 | bool is_global = false; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4759 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 4760 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4761 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 4762 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4763 | return -E2BIG; |
| 4764 | } |
| 4765 | |
| 4766 | target_insn = *insn_idx + insn->imm; |
| 4767 | subprog = find_subprog(env, target_insn + 1); |
| 4768 | if (subprog < 0) { |
| 4769 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 4770 | target_insn + 1); |
| 4771 | return -EFAULT; |
| 4772 | } |
| 4773 | |
| 4774 | caller = state->frame[state->curframe]; |
| 4775 | if (state->frame[state->curframe + 1]) { |
| 4776 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 4777 | state->curframe + 1); |
| 4778 | return -EFAULT; |
| 4779 | } |
| 4780 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4781 | func_info_aux = env->prog->aux->func_info_aux; |
| 4782 | if (func_info_aux) |
| 4783 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; |
| 4784 | err = btf_check_func_arg_match(env, subprog, caller->regs); |
| 4785 | if (err == -EFAULT) |
| 4786 | return err; |
| 4787 | if (is_global) { |
| 4788 | if (err) { |
| 4789 | verbose(env, "Caller passes invalid args into func#%d\n", |
| 4790 | subprog); |
| 4791 | return err; |
| 4792 | } else { |
| 4793 | if (env->log.level & BPF_LOG_LEVEL) |
| 4794 | verbose(env, |
| 4795 | "Func#%d is global and valid. Skipping.\n", |
| 4796 | subprog); |
| 4797 | clear_caller_saved_regs(env, caller->regs); |
| 4798 | |
| 4799 | /* All global functions return SCALAR_VALUE */ |
| 4800 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
| 4801 | |
| 4802 | /* continue with next insn after call */ |
| 4803 | return 0; |
| 4804 | } |
| 4805 | } |
| 4806 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4807 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 4808 | if (!callee) |
| 4809 | return -ENOMEM; |
| 4810 | state->frame[state->curframe + 1] = callee; |
| 4811 | |
| 4812 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 4813 | * into its own stack before reading from it. |
| 4814 | * callee can read/write into caller's stack |
| 4815 | */ |
| 4816 | init_func_state(env, callee, |
| 4817 | /* remember the callsite, it will be used by bpf_exit */ |
| 4818 | *insn_idx /* callsite */, |
| 4819 | state->curframe + 1 /* frameno within this callchain */, |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 4820 | subprog /* subprog number within this prog */); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4821 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4822 | /* Transfer references to the callee */ |
| 4823 | err = transfer_reference_state(callee, caller); |
| 4824 | if (err) |
| 4825 | return err; |
| 4826 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 4827 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 4828 | * pointers, which connects us up to the liveness chain |
| 4829 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4830 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 4831 | callee->regs[i] = caller->regs[i]; |
| 4832 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4833 | clear_caller_saved_regs(env, caller->regs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4834 | |
| 4835 | /* only increment it after check_reg_arg() finished */ |
| 4836 | state->curframe++; |
| 4837 | |
| 4838 | /* and go analyze first insn of the callee */ |
| 4839 | *insn_idx = target_insn; |
| 4840 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 4841 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4842 | verbose(env, "caller:\n"); |
| 4843 | print_verifier_state(env, caller); |
| 4844 | verbose(env, "callee:\n"); |
| 4845 | print_verifier_state(env, callee); |
| 4846 | } |
| 4847 | return 0; |
| 4848 | } |
| 4849 | |
| 4850 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 4851 | { |
| 4852 | struct bpf_verifier_state *state = env->cur_state; |
| 4853 | struct bpf_func_state *caller, *callee; |
| 4854 | struct bpf_reg_state *r0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4855 | int err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4856 | |
| 4857 | callee = state->frame[state->curframe]; |
| 4858 | r0 = &callee->regs[BPF_REG_0]; |
| 4859 | if (r0->type == PTR_TO_STACK) { |
| 4860 | /* technically it's ok to return caller's stack pointer |
| 4861 | * (or caller's caller's pointer) back to the caller, |
| 4862 | * since these pointers are valid. Only current stack |
| 4863 | * pointer will be invalid as soon as function exits, |
| 4864 | * but let's be conservative |
| 4865 | */ |
| 4866 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 4867 | return -EINVAL; |
| 4868 | } |
| 4869 | |
| 4870 | state->curframe--; |
| 4871 | caller = state->frame[state->curframe]; |
| 4872 | /* return to the caller whatever r0 had in the callee */ |
| 4873 | caller->regs[BPF_REG_0] = *r0; |
| 4874 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4875 | /* Transfer references to the caller */ |
| 4876 | err = transfer_reference_state(caller, callee); |
| 4877 | if (err) |
| 4878 | return err; |
| 4879 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4880 | *insn_idx = callee->callsite + 1; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 4881 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4882 | verbose(env, "returning from callee:\n"); |
| 4883 | print_verifier_state(env, callee); |
| 4884 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 4885 | print_verifier_state(env, caller); |
| 4886 | } |
| 4887 | /* clear everything in the callee */ |
| 4888 | free_func_state(callee); |
| 4889 | state->frame[state->curframe + 1] = NULL; |
| 4890 | return 0; |
| 4891 | } |
| 4892 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4893 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 4894 | int func_id, |
| 4895 | struct bpf_call_arg_meta *meta) |
| 4896 | { |
| 4897 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 4898 | |
| 4899 | if (ret_type != RET_INTEGER || |
| 4900 | (func_id != BPF_FUNC_get_stack && |
Daniel Borkmann | 47cc0ed | 2020-05-15 12:11:17 +0200 | [diff] [blame] | 4901 | func_id != BPF_FUNC_probe_read_str && |
| 4902 | func_id != BPF_FUNC_probe_read_kernel_str && |
| 4903 | func_id != BPF_FUNC_probe_read_user_str)) |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4904 | return; |
| 4905 | |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 4906 | ret_reg->smax_value = meta->msize_max_value; |
John Fastabend | fa123ac | 2020-03-30 14:36:59 -0700 | [diff] [blame] | 4907 | ret_reg->s32_max_value = meta->msize_max_value; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4908 | __reg_deduce_bounds(ret_reg); |
| 4909 | __reg_bound_offset(ret_reg); |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 4910 | __update_reg_bounds(ret_reg); |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4911 | } |
| 4912 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4913 | static int |
| 4914 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 4915 | int func_id, int insn_idx) |
| 4916 | { |
| 4917 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4918 | struct bpf_map *map = meta->map_ptr; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4919 | |
| 4920 | if (func_id != BPF_FUNC_tail_call && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 4921 | func_id != BPF_FUNC_map_lookup_elem && |
| 4922 | func_id != BPF_FUNC_map_update_elem && |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 4923 | func_id != BPF_FUNC_map_delete_elem && |
| 4924 | func_id != BPF_FUNC_map_push_elem && |
| 4925 | func_id != BPF_FUNC_map_pop_elem && |
| 4926 | func_id != BPF_FUNC_map_peek_elem) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4927 | return 0; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 4928 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4929 | if (map == NULL) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4930 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 4931 | return -EINVAL; |
| 4932 | } |
| 4933 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4934 | /* In case of read-only, some additional restrictions |
| 4935 | * need to be applied in order to prevent altering the |
| 4936 | * state of the map from program side. |
| 4937 | */ |
| 4938 | if ((map->map_flags & BPF_F_RDONLY_PROG) && |
| 4939 | (func_id == BPF_FUNC_map_delete_elem || |
| 4940 | func_id == BPF_FUNC_map_update_elem || |
| 4941 | func_id == BPF_FUNC_map_push_elem || |
| 4942 | func_id == BPF_FUNC_map_pop_elem)) { |
| 4943 | verbose(env, "write into map forbidden\n"); |
| 4944 | return -EACCES; |
| 4945 | } |
| 4946 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4947 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4948 | bpf_map_ptr_store(aux, meta->map_ptr, |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 4949 | !meta->map_ptr->bypass_spec_v1); |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4950 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4951 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 4952 | !meta->map_ptr->bypass_spec_v1); |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4953 | return 0; |
| 4954 | } |
| 4955 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4956 | static int |
| 4957 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 4958 | int func_id, int insn_idx) |
| 4959 | { |
| 4960 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
| 4961 | struct bpf_reg_state *regs = cur_regs(env), *reg; |
| 4962 | struct bpf_map *map = meta->map_ptr; |
| 4963 | struct tnum range; |
| 4964 | u64 val; |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 4965 | int err; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4966 | |
| 4967 | if (func_id != BPF_FUNC_tail_call) |
| 4968 | return 0; |
| 4969 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { |
| 4970 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 4971 | return -EINVAL; |
| 4972 | } |
| 4973 | |
| 4974 | range = tnum_range(0, map->max_entries - 1); |
| 4975 | reg = ®s[BPF_REG_3]; |
| 4976 | |
| 4977 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { |
| 4978 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
| 4979 | return 0; |
| 4980 | } |
| 4981 | |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 4982 | err = mark_chain_precision(env, BPF_REG_3); |
| 4983 | if (err) |
| 4984 | return err; |
| 4985 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4986 | val = reg->var_off.value; |
| 4987 | if (bpf_map_key_unseen(aux)) |
| 4988 | bpf_map_key_store(aux, val); |
| 4989 | else if (!bpf_map_key_poisoned(aux) && |
| 4990 | bpf_map_key_immediate(aux) != val) |
| 4991 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
| 4992 | return 0; |
| 4993 | } |
| 4994 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4995 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 4996 | { |
| 4997 | struct bpf_func_state *state = cur_func(env); |
| 4998 | int i; |
| 4999 | |
| 5000 | for (i = 0; i < state->acquired_refs; i++) { |
| 5001 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 5002 | state->refs[i].id, state->refs[i].insn_idx); |
| 5003 | } |
| 5004 | return state->acquired_refs ? -EINVAL : 0; |
| 5005 | } |
| 5006 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5007 | 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] | 5008 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5009 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5010 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5011 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5012 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5013 | int i, err; |
| 5014 | |
| 5015 | /* find function prototype */ |
| 5016 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5017 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 5018 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5019 | return -EINVAL; |
| 5020 | } |
| 5021 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5022 | if (env->ops->get_func_proto) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 5023 | fn = env->ops->get_func_proto(func_id, env->prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5024 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5025 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 5026 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5027 | return -EINVAL; |
| 5028 | } |
| 5029 | |
| 5030 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 5031 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Daniel Borkmann | 3fe2867 | 2018-06-02 23:06:33 +0200 | [diff] [blame] | 5032 | 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] | 5033 | return -EINVAL; |
| 5034 | } |
| 5035 | |
Jiri Olsa | eae2e83 | 2020-08-25 21:21:19 +0200 | [diff] [blame] | 5036 | if (fn->allowed && !fn->allowed(env->prog)) { |
| 5037 | verbose(env, "helper call is not allowed in probe\n"); |
| 5038 | return -EINVAL; |
| 5039 | } |
| 5040 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 5041 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 5042 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 5043 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 5044 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 5045 | func_id_name(func_id), func_id); |
| 5046 | return -EINVAL; |
| 5047 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5048 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5049 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5050 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5051 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5052 | err = check_func_proto(fn, func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5053 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5054 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 5055 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5056 | return err; |
| 5057 | } |
| 5058 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 5059 | meta.func_id = func_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5060 | /* check args */ |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 5061 | for (i = 0; i < 5; i++) { |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5062 | err = check_func_arg(env, i, &meta, fn); |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 5063 | if (err) |
| 5064 | return err; |
| 5065 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5066 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 5067 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 5068 | if (err) |
| 5069 | return err; |
| 5070 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 5071 | err = record_func_key(env, &meta, func_id, insn_idx); |
| 5072 | if (err) |
| 5073 | return err; |
| 5074 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5075 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 5076 | * is inferred from register state. |
| 5077 | */ |
| 5078 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 5079 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 5080 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5081 | if (err) |
| 5082 | return err; |
| 5083 | } |
| 5084 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5085 | if (func_id == BPF_FUNC_tail_call) { |
| 5086 | err = check_reference_leak(env); |
| 5087 | if (err) { |
| 5088 | verbose(env, "tail_call would lead to reference leak\n"); |
| 5089 | return err; |
| 5090 | } |
| 5091 | } else if (is_release_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5092 | err = release_reference(env, meta.ref_obj_id); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 5093 | if (err) { |
| 5094 | verbose(env, "func %s#%d reference has not been acquired before\n", |
| 5095 | func_id_name(func_id), func_id); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5096 | return err; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 5097 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5098 | } |
| 5099 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5100 | regs = cur_regs(env); |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 5101 | |
| 5102 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 5103 | * this is required because get_local_storage() can't return an error. |
| 5104 | */ |
| 5105 | if (func_id == BPF_FUNC_get_local_storage && |
| 5106 | !register_is_null(®s[BPF_REG_2])) { |
| 5107 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 5108 | return -EINVAL; |
| 5109 | } |
| 5110 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5111 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5112 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5113 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5114 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 5115 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5116 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 5117 | /* helper call returns 64-bit value. */ |
| 5118 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 5119 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5120 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5121 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5122 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5123 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5124 | } else if (fn->ret_type == RET_VOID) { |
| 5125 | regs[BPF_REG_0].type = NOT_INIT; |
Roman Gushchin | 3e6a4b3 | 2018-08-02 14:27:22 -0700 | [diff] [blame] | 5126 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 5127 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5128 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5129 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5130 | /* remember map_ptr, so that check_map_access() |
| 5131 | * can check 'value_size' boundary of memory access |
| 5132 | * to map element returned from bpf_map_lookup_elem() |
| 5133 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5134 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5135 | verbose(env, |
| 5136 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5137 | return -EINVAL; |
| 5138 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5139 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 5140 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 5141 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
Alexei Starovoitov | e16d2f1 | 2019-01-31 15:40:05 -0800 | [diff] [blame] | 5142 | if (map_value_has_spin_lock(meta.map_ptr)) |
| 5143 | regs[BPF_REG_0].id = ++env->id_gen; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 5144 | } else { |
| 5145 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 5146 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5147 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
| 5148 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5149 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; |
Lorenz Bauer | 85a51f8 | 2019-03-22 09:54:00 +0800 | [diff] [blame] | 5150 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
| 5151 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5152 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 5153 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
| 5154 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5155 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5156 | } else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) { |
| 5157 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5158 | regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5159 | regs[BPF_REG_0].mem_size = meta.mem_size; |
Hao Luo | 63d9b80 | 2020-09-29 16:50:48 -0700 | [diff] [blame] | 5160 | } else if (fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL || |
| 5161 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID) { |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5162 | const struct btf_type *t; |
| 5163 | |
| 5164 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5165 | t = btf_type_skip_modifiers(btf_vmlinux, meta.ret_btf_id, NULL); |
| 5166 | if (!btf_type_is_struct(t)) { |
| 5167 | u32 tsize; |
| 5168 | const struct btf_type *ret; |
| 5169 | const char *tname; |
| 5170 | |
| 5171 | /* resolve the type size of ksym. */ |
| 5172 | ret = btf_resolve_size(btf_vmlinux, t, &tsize); |
| 5173 | if (IS_ERR(ret)) { |
| 5174 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 5175 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
| 5176 | tname, PTR_ERR(ret)); |
| 5177 | return -EINVAL; |
| 5178 | } |
Hao Luo | 63d9b80 | 2020-09-29 16:50:48 -0700 | [diff] [blame] | 5179 | regs[BPF_REG_0].type = |
| 5180 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? |
| 5181 | PTR_TO_MEM : PTR_TO_MEM_OR_NULL; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5182 | regs[BPF_REG_0].mem_size = tsize; |
| 5183 | } else { |
Hao Luo | 63d9b80 | 2020-09-29 16:50:48 -0700 | [diff] [blame] | 5184 | regs[BPF_REG_0].type = |
| 5185 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? |
| 5186 | PTR_TO_BTF_ID : PTR_TO_BTF_ID_OR_NULL; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5187 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
| 5188 | } |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5189 | } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL) { |
| 5190 | int ret_btf_id; |
| 5191 | |
| 5192 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5193 | regs[BPF_REG_0].type = PTR_TO_BTF_ID_OR_NULL; |
| 5194 | ret_btf_id = *fn->ret_btf_id; |
| 5195 | if (ret_btf_id == 0) { |
| 5196 | verbose(env, "invalid return type %d of func %s#%d\n", |
| 5197 | fn->ret_type, func_id_name(func_id), func_id); |
| 5198 | return -EINVAL; |
| 5199 | } |
| 5200 | regs[BPF_REG_0].btf_id = ret_btf_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5201 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5202 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 5203 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5204 | return -EINVAL; |
| 5205 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 5206 | |
Martin KaFai Lau | 93c230e | 2020-10-19 12:42:12 -0700 | [diff] [blame] | 5207 | if (reg_type_may_be_null(regs[BPF_REG_0].type)) |
| 5208 | regs[BPF_REG_0].id = ++env->id_gen; |
| 5209 | |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 5210 | if (is_ptr_cast_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5211 | /* For release_reference() */ |
| 5212 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 5213 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 5214 | int id = acquire_reference_state(env, insn_idx); |
| 5215 | |
| 5216 | if (id < 0) |
| 5217 | return id; |
| 5218 | /* For mark_ptr_or_null_reg() */ |
| 5219 | regs[BPF_REG_0].id = id; |
| 5220 | /* For release_reference() */ |
| 5221 | regs[BPF_REG_0].ref_obj_id = id; |
| 5222 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5223 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 5224 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
| 5225 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5226 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 5227 | if (err) |
| 5228 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 5229 | |
Song Liu | fa28dcb | 2020-06-29 23:28:44 -0700 | [diff] [blame] | 5230 | if ((func_id == BPF_FUNC_get_stack || |
| 5231 | func_id == BPF_FUNC_get_task_stack) && |
| 5232 | !env->prog->has_callchain_buf) { |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 5233 | const char *err_str; |
| 5234 | |
| 5235 | #ifdef CONFIG_PERF_EVENTS |
| 5236 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 5237 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 5238 | #else |
| 5239 | err = -ENOTSUPP; |
| 5240 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 5241 | #endif |
| 5242 | if (err) { |
| 5243 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 5244 | return err; |
| 5245 | } |
| 5246 | |
| 5247 | env->prog->has_callchain_buf = true; |
| 5248 | } |
| 5249 | |
Song Liu | 5d99cb2c | 2020-07-23 11:06:45 -0700 | [diff] [blame] | 5250 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
| 5251 | env->prog->call_get_stack = true; |
| 5252 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5253 | if (changes_data) |
| 5254 | clear_all_pkt_pointers(env); |
| 5255 | return 0; |
| 5256 | } |
| 5257 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5258 | static bool signed_add_overflows(s64 a, s64 b) |
| 5259 | { |
| 5260 | /* Do the add in u64, where overflow is well-defined */ |
| 5261 | s64 res = (s64)((u64)a + (u64)b); |
| 5262 | |
| 5263 | if (b < 0) |
| 5264 | return res > a; |
| 5265 | return res < a; |
| 5266 | } |
| 5267 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5268 | static bool signed_add32_overflows(s64 a, s64 b) |
| 5269 | { |
| 5270 | /* Do the add in u32, where overflow is well-defined */ |
| 5271 | s32 res = (s32)((u32)a + (u32)b); |
| 5272 | |
| 5273 | if (b < 0) |
| 5274 | return res > a; |
| 5275 | return res < a; |
| 5276 | } |
| 5277 | |
| 5278 | static bool signed_sub_overflows(s32 a, s32 b) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5279 | { |
| 5280 | /* Do the sub in u64, where overflow is well-defined */ |
| 5281 | s64 res = (s64)((u64)a - (u64)b); |
| 5282 | |
| 5283 | if (b < 0) |
| 5284 | return res < a; |
| 5285 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 5286 | } |
| 5287 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5288 | static bool signed_sub32_overflows(s32 a, s32 b) |
| 5289 | { |
| 5290 | /* Do the sub in u64, where overflow is well-defined */ |
| 5291 | s32 res = (s32)((u32)a - (u32)b); |
| 5292 | |
| 5293 | if (b < 0) |
| 5294 | return res < a; |
| 5295 | return res > a; |
| 5296 | } |
| 5297 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 5298 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 5299 | const struct bpf_reg_state *reg, |
| 5300 | enum bpf_reg_type type) |
| 5301 | { |
| 5302 | bool known = tnum_is_const(reg->var_off); |
| 5303 | s64 val = reg->var_off.value; |
| 5304 | s64 smin = reg->smin_value; |
| 5305 | |
| 5306 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 5307 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 5308 | reg_type_str[type], val); |
| 5309 | return false; |
| 5310 | } |
| 5311 | |
| 5312 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 5313 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 5314 | reg_type_str[type], reg->off); |
| 5315 | return false; |
| 5316 | } |
| 5317 | |
| 5318 | if (smin == S64_MIN) { |
| 5319 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 5320 | reg_type_str[type]); |
| 5321 | return false; |
| 5322 | } |
| 5323 | |
| 5324 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 5325 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 5326 | smin, reg_type_str[type]); |
| 5327 | return false; |
| 5328 | } |
| 5329 | |
| 5330 | return true; |
| 5331 | } |
| 5332 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5333 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
| 5334 | { |
| 5335 | return &env->insn_aux_data[env->insn_idx]; |
| 5336 | } |
| 5337 | |
| 5338 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
| 5339 | u32 *ptr_limit, u8 opcode, bool off_is_neg) |
| 5340 | { |
| 5341 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || |
| 5342 | (opcode == BPF_SUB && !off_is_neg); |
| 5343 | u32 off; |
| 5344 | |
| 5345 | switch (ptr_reg->type) { |
| 5346 | case PTR_TO_STACK: |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 5347 | /* Indirect variable offset stack access is prohibited in |
| 5348 | * unprivileged mode so it's not handled here. |
| 5349 | */ |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5350 | off = ptr_reg->off + ptr_reg->var_off.value; |
| 5351 | if (mask_to_left) |
| 5352 | *ptr_limit = MAX_BPF_STACK + off; |
| 5353 | else |
| 5354 | *ptr_limit = -off; |
| 5355 | return 0; |
| 5356 | case PTR_TO_MAP_VALUE: |
| 5357 | if (mask_to_left) { |
| 5358 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; |
| 5359 | } else { |
| 5360 | off = ptr_reg->smin_value + ptr_reg->off; |
| 5361 | *ptr_limit = ptr_reg->map_ptr->value_size - off; |
| 5362 | } |
| 5363 | return 0; |
| 5364 | default: |
| 5365 | return -EINVAL; |
| 5366 | } |
| 5367 | } |
| 5368 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 5369 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
| 5370 | const struct bpf_insn *insn) |
| 5371 | { |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 5372 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 5373 | } |
| 5374 | |
| 5375 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, |
| 5376 | u32 alu_state, u32 alu_limit) |
| 5377 | { |
| 5378 | /* If we arrived here from different branches with different |
| 5379 | * state or limits to sanitize, then this won't work. |
| 5380 | */ |
| 5381 | if (aux->alu_state && |
| 5382 | (aux->alu_state != alu_state || |
| 5383 | aux->alu_limit != alu_limit)) |
| 5384 | return -EACCES; |
| 5385 | |
| 5386 | /* Corresponding fixup done in fixup_bpf_calls(). */ |
| 5387 | aux->alu_state = alu_state; |
| 5388 | aux->alu_limit = alu_limit; |
| 5389 | return 0; |
| 5390 | } |
| 5391 | |
| 5392 | static int sanitize_val_alu(struct bpf_verifier_env *env, |
| 5393 | struct bpf_insn *insn) |
| 5394 | { |
| 5395 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 5396 | |
| 5397 | if (can_skip_alu_sanitation(env, insn)) |
| 5398 | return 0; |
| 5399 | |
| 5400 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); |
| 5401 | } |
| 5402 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5403 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
| 5404 | struct bpf_insn *insn, |
| 5405 | const struct bpf_reg_state *ptr_reg, |
| 5406 | struct bpf_reg_state *dst_reg, |
| 5407 | bool off_is_neg) |
| 5408 | { |
| 5409 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5410 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 5411 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
| 5412 | u8 opcode = BPF_OP(insn->code); |
| 5413 | u32 alu_state, alu_limit; |
| 5414 | struct bpf_reg_state tmp; |
| 5415 | bool ret; |
| 5416 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 5417 | if (can_skip_alu_sanitation(env, insn)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5418 | return 0; |
| 5419 | |
| 5420 | /* We already marked aux for masking from non-speculative |
| 5421 | * paths, thus we got here in the first place. We only care |
| 5422 | * to explore bad access from here. |
| 5423 | */ |
| 5424 | if (vstate->speculative) |
| 5425 | goto do_sim; |
| 5426 | |
| 5427 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; |
| 5428 | alu_state |= ptr_is_dst_reg ? |
| 5429 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; |
| 5430 | |
| 5431 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) |
| 5432 | return 0; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 5433 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5434 | return -EACCES; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5435 | do_sim: |
| 5436 | /* Simulate and find potential out-of-bounds access under |
| 5437 | * speculative execution from truncation as a result of |
| 5438 | * masking when off was not within expected range. If off |
| 5439 | * sits in dst, then we temporarily need to move ptr there |
| 5440 | * to simulate dst (== 0) +/-= ptr. Needed, for example, |
| 5441 | * for cases where we use K-based arithmetic in one direction |
| 5442 | * and truncated reg-based in the other in order to explore |
| 5443 | * bad access. |
| 5444 | */ |
| 5445 | if (!ptr_is_dst_reg) { |
| 5446 | tmp = *dst_reg; |
| 5447 | *dst_reg = *ptr_reg; |
| 5448 | } |
| 5449 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); |
Xu Yu | 0803278 | 2019-03-21 18:00:35 +0800 | [diff] [blame] | 5450 | if (!ptr_is_dst_reg && ret) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5451 | *dst_reg = tmp; |
| 5452 | return !ret ? -EFAULT : 0; |
| 5453 | } |
| 5454 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5455 | /* 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] | 5456 | * Caller should also handle BPF_MOV case separately. |
| 5457 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 5458 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 5459 | */ |
| 5460 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 5461 | struct bpf_insn *insn, |
| 5462 | const struct bpf_reg_state *ptr_reg, |
| 5463 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5464 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5465 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5466 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 5467 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5468 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5469 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 5470 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 5471 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 5472 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 5473 | u32 dst = insn->dst_reg, src = insn->src_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5474 | u8 opcode = BPF_OP(insn->code); |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5475 | int ret; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5476 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5477 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5478 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 5479 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 5480 | smin_val > smax_val || umin_val > umax_val) { |
| 5481 | /* Taint dst register if offset had invalid bounds derived from |
| 5482 | * e.g. dead branches. |
| 5483 | */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 5484 | __mark_reg_unknown(env, dst_reg); |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 5485 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5486 | } |
| 5487 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5488 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 5489 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Yonghong Song | 6c69354 | 2020-06-18 16:46:31 -0700 | [diff] [blame] | 5490 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
| 5491 | __mark_reg_unknown(env, dst_reg); |
| 5492 | return 0; |
| 5493 | } |
| 5494 | |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5495 | verbose(env, |
| 5496 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 5497 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5498 | return -EACCES; |
| 5499 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 5500 | |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5501 | switch (ptr_reg->type) { |
| 5502 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 5503 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
| 5504 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5505 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5506 | case CONST_PTR_TO_MAP: |
Yonghong Song | 7c69673 | 2020-09-08 10:57:02 -0700 | [diff] [blame] | 5507 | /* smin_val represents the known value */ |
| 5508 | if (known && smin_val == 0 && opcode == BPF_ADD) |
| 5509 | break; |
Gustavo A. R. Silva | 8731745 | 2020-10-02 18:42:17 -0500 | [diff] [blame] | 5510 | fallthrough; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5511 | case PTR_TO_PACKET_END: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5512 | case PTR_TO_SOCKET: |
| 5513 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 5514 | case PTR_TO_SOCK_COMMON: |
| 5515 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 5516 | case PTR_TO_TCP_SOCK: |
| 5517 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 5518 | case PTR_TO_XDP_SOCK: |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5519 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
| 5520 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5521 | return -EACCES; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 5522 | case PTR_TO_MAP_VALUE: |
| 5523 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { |
| 5524 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", |
| 5525 | off_reg == dst_reg ? dst : src); |
| 5526 | return -EACCES; |
| 5527 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 5528 | fallthrough; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5529 | default: |
| 5530 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5531 | } |
| 5532 | |
| 5533 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 5534 | * 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] | 5535 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5536 | dst_reg->type = ptr_reg->type; |
| 5537 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 5538 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 5539 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 5540 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 5541 | return -EINVAL; |
| 5542 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5543 | /* pointer types do not carry 32-bit bounds at the moment. */ |
| 5544 | __mark_reg32_unbounded(dst_reg); |
| 5545 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5546 | switch (opcode) { |
| 5547 | case BPF_ADD: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5548 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 5549 | if (ret < 0) { |
| 5550 | verbose(env, "R%d tried to add from different maps or paths\n", dst); |
| 5551 | return ret; |
| 5552 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5553 | /* We can take a fixed offset as long as it doesn't overflow |
| 5554 | * the s32 'off' field |
| 5555 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5556 | if (known && (ptr_reg->off + smin_val == |
| 5557 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5558 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5559 | dst_reg->smin_value = smin_ptr; |
| 5560 | dst_reg->smax_value = smax_ptr; |
| 5561 | dst_reg->umin_value = umin_ptr; |
| 5562 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5563 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5564 | dst_reg->off = ptr_reg->off + smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5565 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5566 | break; |
| 5567 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5568 | /* A new variable offset is created. Note that off_reg->off |
| 5569 | * == 0, since it's a scalar. |
| 5570 | * dst_reg gets the pointer type and since some positive |
| 5571 | * integer value was added to the pointer, give it a new 'id' |
| 5572 | * if it's a PTR_TO_PACKET. |
| 5573 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 5574 | * added into the variable offset, and we copy the fixed offset |
| 5575 | * from ptr_reg. |
| 5576 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5577 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 5578 | signed_add_overflows(smax_ptr, smax_val)) { |
| 5579 | dst_reg->smin_value = S64_MIN; |
| 5580 | dst_reg->smax_value = S64_MAX; |
| 5581 | } else { |
| 5582 | dst_reg->smin_value = smin_ptr + smin_val; |
| 5583 | dst_reg->smax_value = smax_ptr + smax_val; |
| 5584 | } |
| 5585 | if (umin_ptr + umin_val < umin_ptr || |
| 5586 | umax_ptr + umax_val < umax_ptr) { |
| 5587 | dst_reg->umin_value = 0; |
| 5588 | dst_reg->umax_value = U64_MAX; |
| 5589 | } else { |
| 5590 | dst_reg->umin_value = umin_ptr + umin_val; |
| 5591 | dst_reg->umax_value = umax_ptr + umax_val; |
| 5592 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5593 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 5594 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5595 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5596 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5597 | dst_reg->id = ++env->id_gen; |
| 5598 | /* something was added to pkt_ptr, set range to zero */ |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5599 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5600 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5601 | break; |
| 5602 | case BPF_SUB: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5603 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 5604 | if (ret < 0) { |
| 5605 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); |
| 5606 | return ret; |
| 5607 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5608 | if (dst_reg == off_reg) { |
| 5609 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5610 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 5611 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5612 | return -EACCES; |
| 5613 | } |
| 5614 | /* We don't allow subtraction from FP, because (according to |
| 5615 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 5616 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 5617 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5618 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5619 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 5620 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5621 | return -EACCES; |
| 5622 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5623 | if (known && (ptr_reg->off - smin_val == |
| 5624 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5625 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5626 | dst_reg->smin_value = smin_ptr; |
| 5627 | dst_reg->smax_value = smax_ptr; |
| 5628 | dst_reg->umin_value = umin_ptr; |
| 5629 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5630 | dst_reg->var_off = ptr_reg->var_off; |
| 5631 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5632 | dst_reg->off = ptr_reg->off - smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5633 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5634 | break; |
| 5635 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5636 | /* A new variable offset is created. If the subtrahend is known |
| 5637 | * nonnegative, then any reg->range we had before is still good. |
| 5638 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5639 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 5640 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 5641 | /* Overflow possible, we know nothing */ |
| 5642 | dst_reg->smin_value = S64_MIN; |
| 5643 | dst_reg->smax_value = S64_MAX; |
| 5644 | } else { |
| 5645 | dst_reg->smin_value = smin_ptr - smax_val; |
| 5646 | dst_reg->smax_value = smax_ptr - smin_val; |
| 5647 | } |
| 5648 | if (umin_ptr < umax_val) { |
| 5649 | /* Overflow possible, we know nothing */ |
| 5650 | dst_reg->umin_value = 0; |
| 5651 | dst_reg->umax_value = U64_MAX; |
| 5652 | } else { |
| 5653 | /* Cannot overflow (as long as bounds are consistent) */ |
| 5654 | dst_reg->umin_value = umin_ptr - umax_val; |
| 5655 | dst_reg->umax_value = umax_ptr - umin_val; |
| 5656 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5657 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 5658 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5659 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5660 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5661 | dst_reg->id = ++env->id_gen; |
| 5662 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5663 | if (smin_val < 0) |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5664 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5665 | } |
| 5666 | break; |
| 5667 | case BPF_AND: |
| 5668 | case BPF_OR: |
| 5669 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5670 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 5671 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 5672 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5673 | return -EACCES; |
| 5674 | default: |
| 5675 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5676 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 5677 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5678 | return -EACCES; |
| 5679 | } |
| 5680 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 5681 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 5682 | return -EINVAL; |
| 5683 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5684 | __update_reg_bounds(dst_reg); |
| 5685 | __reg_deduce_bounds(dst_reg); |
| 5686 | __reg_bound_offset(dst_reg); |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 5687 | |
| 5688 | /* For unprivileged we require that resulting offset must be in bounds |
| 5689 | * in order to be able to sanitize access later on. |
| 5690 | */ |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 5691 | if (!env->bypass_spec_v1) { |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 5692 | if (dst_reg->type == PTR_TO_MAP_VALUE && |
| 5693 | check_map_access(env, dst, dst_reg->off, 1, false)) { |
| 5694 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
| 5695 | "prohibited for !root\n", dst); |
| 5696 | return -EACCES; |
| 5697 | } else if (dst_reg->type == PTR_TO_STACK && |
| 5698 | check_stack_access(env, dst_reg, dst_reg->off + |
| 5699 | dst_reg->var_off.value, 1)) { |
| 5700 | verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 5701 | "prohibited for !root\n", dst); |
| 5702 | return -EACCES; |
| 5703 | } |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 5704 | } |
| 5705 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5706 | return 0; |
| 5707 | } |
| 5708 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5709 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
| 5710 | struct bpf_reg_state *src_reg) |
| 5711 | { |
| 5712 | s32 smin_val = src_reg->s32_min_value; |
| 5713 | s32 smax_val = src_reg->s32_max_value; |
| 5714 | u32 umin_val = src_reg->u32_min_value; |
| 5715 | u32 umax_val = src_reg->u32_max_value; |
| 5716 | |
| 5717 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || |
| 5718 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { |
| 5719 | dst_reg->s32_min_value = S32_MIN; |
| 5720 | dst_reg->s32_max_value = S32_MAX; |
| 5721 | } else { |
| 5722 | dst_reg->s32_min_value += smin_val; |
| 5723 | dst_reg->s32_max_value += smax_val; |
| 5724 | } |
| 5725 | if (dst_reg->u32_min_value + umin_val < umin_val || |
| 5726 | dst_reg->u32_max_value + umax_val < umax_val) { |
| 5727 | dst_reg->u32_min_value = 0; |
| 5728 | dst_reg->u32_max_value = U32_MAX; |
| 5729 | } else { |
| 5730 | dst_reg->u32_min_value += umin_val; |
| 5731 | dst_reg->u32_max_value += umax_val; |
| 5732 | } |
| 5733 | } |
| 5734 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5735 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
| 5736 | struct bpf_reg_state *src_reg) |
| 5737 | { |
| 5738 | s64 smin_val = src_reg->smin_value; |
| 5739 | s64 smax_val = src_reg->smax_value; |
| 5740 | u64 umin_val = src_reg->umin_value; |
| 5741 | u64 umax_val = src_reg->umax_value; |
| 5742 | |
| 5743 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 5744 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 5745 | dst_reg->smin_value = S64_MIN; |
| 5746 | dst_reg->smax_value = S64_MAX; |
| 5747 | } else { |
| 5748 | dst_reg->smin_value += smin_val; |
| 5749 | dst_reg->smax_value += smax_val; |
| 5750 | } |
| 5751 | if (dst_reg->umin_value + umin_val < umin_val || |
| 5752 | dst_reg->umax_value + umax_val < umax_val) { |
| 5753 | dst_reg->umin_value = 0; |
| 5754 | dst_reg->umax_value = U64_MAX; |
| 5755 | } else { |
| 5756 | dst_reg->umin_value += umin_val; |
| 5757 | dst_reg->umax_value += umax_val; |
| 5758 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5759 | } |
| 5760 | |
| 5761 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, |
| 5762 | struct bpf_reg_state *src_reg) |
| 5763 | { |
| 5764 | s32 smin_val = src_reg->s32_min_value; |
| 5765 | s32 smax_val = src_reg->s32_max_value; |
| 5766 | u32 umin_val = src_reg->u32_min_value; |
| 5767 | u32 umax_val = src_reg->u32_max_value; |
| 5768 | |
| 5769 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || |
| 5770 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { |
| 5771 | /* Overflow possible, we know nothing */ |
| 5772 | dst_reg->s32_min_value = S32_MIN; |
| 5773 | dst_reg->s32_max_value = S32_MAX; |
| 5774 | } else { |
| 5775 | dst_reg->s32_min_value -= smax_val; |
| 5776 | dst_reg->s32_max_value -= smin_val; |
| 5777 | } |
| 5778 | if (dst_reg->u32_min_value < umax_val) { |
| 5779 | /* Overflow possible, we know nothing */ |
| 5780 | dst_reg->u32_min_value = 0; |
| 5781 | dst_reg->u32_max_value = U32_MAX; |
| 5782 | } else { |
| 5783 | /* Cannot overflow (as long as bounds are consistent) */ |
| 5784 | dst_reg->u32_min_value -= umax_val; |
| 5785 | dst_reg->u32_max_value -= umin_val; |
| 5786 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5787 | } |
| 5788 | |
| 5789 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, |
| 5790 | struct bpf_reg_state *src_reg) |
| 5791 | { |
| 5792 | s64 smin_val = src_reg->smin_value; |
| 5793 | s64 smax_val = src_reg->smax_value; |
| 5794 | u64 umin_val = src_reg->umin_value; |
| 5795 | u64 umax_val = src_reg->umax_value; |
| 5796 | |
| 5797 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 5798 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 5799 | /* Overflow possible, we know nothing */ |
| 5800 | dst_reg->smin_value = S64_MIN; |
| 5801 | dst_reg->smax_value = S64_MAX; |
| 5802 | } else { |
| 5803 | dst_reg->smin_value -= smax_val; |
| 5804 | dst_reg->smax_value -= smin_val; |
| 5805 | } |
| 5806 | if (dst_reg->umin_value < umax_val) { |
| 5807 | /* Overflow possible, we know nothing */ |
| 5808 | dst_reg->umin_value = 0; |
| 5809 | dst_reg->umax_value = U64_MAX; |
| 5810 | } else { |
| 5811 | /* Cannot overflow (as long as bounds are consistent) */ |
| 5812 | dst_reg->umin_value -= umax_val; |
| 5813 | dst_reg->umax_value -= umin_val; |
| 5814 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5815 | } |
| 5816 | |
| 5817 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, |
| 5818 | struct bpf_reg_state *src_reg) |
| 5819 | { |
| 5820 | s32 smin_val = src_reg->s32_min_value; |
| 5821 | u32 umin_val = src_reg->u32_min_value; |
| 5822 | u32 umax_val = src_reg->u32_max_value; |
| 5823 | |
| 5824 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { |
| 5825 | /* Ain't nobody got time to multiply that sign */ |
| 5826 | __mark_reg32_unbounded(dst_reg); |
| 5827 | return; |
| 5828 | } |
| 5829 | /* Both values are positive, so we can work with unsigned and |
| 5830 | * copy the result to signed (unless it exceeds S32_MAX). |
| 5831 | */ |
| 5832 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { |
| 5833 | /* Potential overflow, we know nothing */ |
| 5834 | __mark_reg32_unbounded(dst_reg); |
| 5835 | return; |
| 5836 | } |
| 5837 | dst_reg->u32_min_value *= umin_val; |
| 5838 | dst_reg->u32_max_value *= umax_val; |
| 5839 | if (dst_reg->u32_max_value > S32_MAX) { |
| 5840 | /* Overflow possible, we know nothing */ |
| 5841 | dst_reg->s32_min_value = S32_MIN; |
| 5842 | dst_reg->s32_max_value = S32_MAX; |
| 5843 | } else { |
| 5844 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 5845 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 5846 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5847 | } |
| 5848 | |
| 5849 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, |
| 5850 | struct bpf_reg_state *src_reg) |
| 5851 | { |
| 5852 | s64 smin_val = src_reg->smin_value; |
| 5853 | u64 umin_val = src_reg->umin_value; |
| 5854 | u64 umax_val = src_reg->umax_value; |
| 5855 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5856 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
| 5857 | /* Ain't nobody got time to multiply that sign */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5858 | __mark_reg64_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5859 | return; |
| 5860 | } |
| 5861 | /* Both values are positive, so we can work with unsigned and |
| 5862 | * copy the result to signed (unless it exceeds S64_MAX). |
| 5863 | */ |
| 5864 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 5865 | /* Potential overflow, we know nothing */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5866 | __mark_reg64_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5867 | return; |
| 5868 | } |
| 5869 | dst_reg->umin_value *= umin_val; |
| 5870 | dst_reg->umax_value *= umax_val; |
| 5871 | if (dst_reg->umax_value > S64_MAX) { |
| 5872 | /* Overflow possible, we know nothing */ |
| 5873 | dst_reg->smin_value = S64_MIN; |
| 5874 | dst_reg->smax_value = S64_MAX; |
| 5875 | } else { |
| 5876 | dst_reg->smin_value = dst_reg->umin_value; |
| 5877 | dst_reg->smax_value = dst_reg->umax_value; |
| 5878 | } |
| 5879 | } |
| 5880 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5881 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
| 5882 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5883 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5884 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 5885 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 5886 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 5887 | s32 smin_val = src_reg->s32_min_value; |
| 5888 | u32 umax_val = src_reg->u32_max_value; |
| 5889 | |
| 5890 | /* Assuming scalar64_min_max_and will be called so its safe |
| 5891 | * to skip updating register for known 32-bit case. |
| 5892 | */ |
| 5893 | if (src_known && dst_known) |
| 5894 | return; |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5895 | |
| 5896 | /* We get our minimum from the var_off, since that's inherently |
| 5897 | * bitwise. Our maximum is the minimum of the operands' maxima. |
| 5898 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5899 | dst_reg->u32_min_value = var32_off.value; |
| 5900 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); |
| 5901 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { |
| 5902 | /* Lose signed bounds when ANDing negative numbers, |
| 5903 | * ain't nobody got time for that. |
| 5904 | */ |
| 5905 | dst_reg->s32_min_value = S32_MIN; |
| 5906 | dst_reg->s32_max_value = S32_MAX; |
| 5907 | } else { |
| 5908 | /* ANDing two positives gives a positive, so safe to |
| 5909 | * cast result into s64. |
| 5910 | */ |
| 5911 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 5912 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 5913 | } |
| 5914 | |
| 5915 | } |
| 5916 | |
| 5917 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
| 5918 | struct bpf_reg_state *src_reg) |
| 5919 | { |
| 5920 | bool src_known = tnum_is_const(src_reg->var_off); |
| 5921 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 5922 | s64 smin_val = src_reg->smin_value; |
| 5923 | u64 umax_val = src_reg->umax_value; |
| 5924 | |
| 5925 | if (src_known && dst_known) { |
John Fastabend | 4fbb38a | 2020-09-24 11:45:06 -0700 | [diff] [blame] | 5926 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5927 | return; |
| 5928 | } |
| 5929 | |
| 5930 | /* We get our minimum from the var_off, since that's inherently |
| 5931 | * bitwise. Our maximum is the minimum of the operands' maxima. |
| 5932 | */ |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5933 | dst_reg->umin_value = dst_reg->var_off.value; |
| 5934 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 5935 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 5936 | /* Lose signed bounds when ANDing negative numbers, |
| 5937 | * ain't nobody got time for that. |
| 5938 | */ |
| 5939 | dst_reg->smin_value = S64_MIN; |
| 5940 | dst_reg->smax_value = S64_MAX; |
| 5941 | } else { |
| 5942 | /* ANDing two positives gives a positive, so safe to |
| 5943 | * cast result into s64. |
| 5944 | */ |
| 5945 | dst_reg->smin_value = dst_reg->umin_value; |
| 5946 | dst_reg->smax_value = dst_reg->umax_value; |
| 5947 | } |
| 5948 | /* We may learn something more from the var_off */ |
| 5949 | __update_reg_bounds(dst_reg); |
| 5950 | } |
| 5951 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5952 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
| 5953 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5954 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5955 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 5956 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 5957 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
Daniel Borkmann | 5b9fbeb | 2020-10-07 15:48:58 +0200 | [diff] [blame] | 5958 | s32 smin_val = src_reg->s32_min_value; |
| 5959 | u32 umin_val = src_reg->u32_min_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5960 | |
| 5961 | /* Assuming scalar64_min_max_or will be called so it is safe |
| 5962 | * to skip updating register for known case. |
| 5963 | */ |
| 5964 | if (src_known && dst_known) |
| 5965 | return; |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5966 | |
| 5967 | /* We get our maximum from the var_off, and our minimum is the |
| 5968 | * maximum of the operands' minima |
| 5969 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5970 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); |
| 5971 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; |
| 5972 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { |
| 5973 | /* Lose signed bounds when ORing negative numbers, |
| 5974 | * ain't nobody got time for that. |
| 5975 | */ |
| 5976 | dst_reg->s32_min_value = S32_MIN; |
| 5977 | dst_reg->s32_max_value = S32_MAX; |
| 5978 | } else { |
| 5979 | /* ORing two positives gives a positive, so safe to |
| 5980 | * cast result into s64. |
| 5981 | */ |
Daniel Borkmann | 5b9fbeb | 2020-10-07 15:48:58 +0200 | [diff] [blame] | 5982 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 5983 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5984 | } |
| 5985 | } |
| 5986 | |
| 5987 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
| 5988 | struct bpf_reg_state *src_reg) |
| 5989 | { |
| 5990 | bool src_known = tnum_is_const(src_reg->var_off); |
| 5991 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 5992 | s64 smin_val = src_reg->smin_value; |
| 5993 | u64 umin_val = src_reg->umin_value; |
| 5994 | |
| 5995 | if (src_known && dst_known) { |
John Fastabend | 4fbb38a | 2020-09-24 11:45:06 -0700 | [diff] [blame] | 5996 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5997 | return; |
| 5998 | } |
| 5999 | |
| 6000 | /* We get our maximum from the var_off, and our minimum is the |
| 6001 | * maximum of the operands' minima |
| 6002 | */ |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6003 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 6004 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; |
| 6005 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 6006 | /* Lose signed bounds when ORing negative numbers, |
| 6007 | * ain't nobody got time for that. |
| 6008 | */ |
| 6009 | dst_reg->smin_value = S64_MIN; |
| 6010 | dst_reg->smax_value = S64_MAX; |
| 6011 | } else { |
| 6012 | /* ORing two positives gives a positive, so safe to |
| 6013 | * cast result into s64. |
| 6014 | */ |
| 6015 | dst_reg->smin_value = dst_reg->umin_value; |
| 6016 | dst_reg->smax_value = dst_reg->umax_value; |
| 6017 | } |
| 6018 | /* We may learn something more from the var_off */ |
| 6019 | __update_reg_bounds(dst_reg); |
| 6020 | } |
| 6021 | |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 6022 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
| 6023 | struct bpf_reg_state *src_reg) |
| 6024 | { |
| 6025 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 6026 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 6027 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 6028 | s32 smin_val = src_reg->s32_min_value; |
| 6029 | |
| 6030 | /* Assuming scalar64_min_max_xor will be called so it is safe |
| 6031 | * to skip updating register for known case. |
| 6032 | */ |
| 6033 | if (src_known && dst_known) |
| 6034 | return; |
| 6035 | |
| 6036 | /* We get both minimum and maximum from the var32_off. */ |
| 6037 | dst_reg->u32_min_value = var32_off.value; |
| 6038 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; |
| 6039 | |
| 6040 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { |
| 6041 | /* XORing two positive sign numbers gives a positive, |
| 6042 | * so safe to cast u32 result into s32. |
| 6043 | */ |
| 6044 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 6045 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 6046 | } else { |
| 6047 | dst_reg->s32_min_value = S32_MIN; |
| 6048 | dst_reg->s32_max_value = S32_MAX; |
| 6049 | } |
| 6050 | } |
| 6051 | |
| 6052 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, |
| 6053 | struct bpf_reg_state *src_reg) |
| 6054 | { |
| 6055 | bool src_known = tnum_is_const(src_reg->var_off); |
| 6056 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 6057 | s64 smin_val = src_reg->smin_value; |
| 6058 | |
| 6059 | if (src_known && dst_known) { |
| 6060 | /* dst_reg->var_off.value has been updated earlier */ |
| 6061 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
| 6062 | return; |
| 6063 | } |
| 6064 | |
| 6065 | /* We get both minimum and maximum from the var_off. */ |
| 6066 | dst_reg->umin_value = dst_reg->var_off.value; |
| 6067 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; |
| 6068 | |
| 6069 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { |
| 6070 | /* XORing two positive sign numbers gives a positive, |
| 6071 | * so safe to cast u64 result into s64. |
| 6072 | */ |
| 6073 | dst_reg->smin_value = dst_reg->umin_value; |
| 6074 | dst_reg->smax_value = dst_reg->umax_value; |
| 6075 | } else { |
| 6076 | dst_reg->smin_value = S64_MIN; |
| 6077 | dst_reg->smax_value = S64_MAX; |
| 6078 | } |
| 6079 | |
| 6080 | __update_reg_bounds(dst_reg); |
| 6081 | } |
| 6082 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6083 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6084 | u64 umin_val, u64 umax_val) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6085 | { |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6086 | /* We lose all sign bit information (except what we can pick |
| 6087 | * up from var_off) |
| 6088 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6089 | dst_reg->s32_min_value = S32_MIN; |
| 6090 | dst_reg->s32_max_value = S32_MAX; |
| 6091 | /* If we might shift our top bit out, then we know nothing */ |
| 6092 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { |
| 6093 | dst_reg->u32_min_value = 0; |
| 6094 | dst_reg->u32_max_value = U32_MAX; |
| 6095 | } else { |
| 6096 | dst_reg->u32_min_value <<= umin_val; |
| 6097 | dst_reg->u32_max_value <<= umax_val; |
| 6098 | } |
| 6099 | } |
| 6100 | |
| 6101 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6102 | struct bpf_reg_state *src_reg) |
| 6103 | { |
| 6104 | u32 umax_val = src_reg->u32_max_value; |
| 6105 | u32 umin_val = src_reg->u32_min_value; |
| 6106 | /* u32 alu operation will zext upper bits */ |
| 6107 | struct tnum subreg = tnum_subreg(dst_reg->var_off); |
| 6108 | |
| 6109 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); |
| 6110 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); |
| 6111 | /* Not required but being careful mark reg64 bounds as unknown so |
| 6112 | * that we are forced to pick them up from tnum and zext later and |
| 6113 | * if some path skips this step we are still safe. |
| 6114 | */ |
| 6115 | __mark_reg64_unbounded(dst_reg); |
| 6116 | __update_reg32_bounds(dst_reg); |
| 6117 | } |
| 6118 | |
| 6119 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6120 | u64 umin_val, u64 umax_val) |
| 6121 | { |
| 6122 | /* Special case <<32 because it is a common compiler pattern to sign |
| 6123 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are |
| 6124 | * positive we know this shift will also be positive so we can track |
| 6125 | * bounds correctly. Otherwise we lose all sign bit information except |
| 6126 | * what we can pick up from var_off. Perhaps we can generalize this |
| 6127 | * later to shifts of any length. |
| 6128 | */ |
| 6129 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) |
| 6130 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; |
| 6131 | else |
| 6132 | dst_reg->smax_value = S64_MAX; |
| 6133 | |
| 6134 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) |
| 6135 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; |
| 6136 | else |
| 6137 | dst_reg->smin_value = S64_MIN; |
| 6138 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6139 | /* If we might shift our top bit out, then we know nothing */ |
| 6140 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 6141 | dst_reg->umin_value = 0; |
| 6142 | dst_reg->umax_value = U64_MAX; |
| 6143 | } else { |
| 6144 | dst_reg->umin_value <<= umin_val; |
| 6145 | dst_reg->umax_value <<= umax_val; |
| 6146 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6147 | } |
| 6148 | |
| 6149 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6150 | struct bpf_reg_state *src_reg) |
| 6151 | { |
| 6152 | u64 umax_val = src_reg->umax_value; |
| 6153 | u64 umin_val = src_reg->umin_value; |
| 6154 | |
| 6155 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ |
| 6156 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); |
| 6157 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); |
| 6158 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6159 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
| 6160 | /* We may learn something more from the var_off */ |
| 6161 | __update_reg_bounds(dst_reg); |
| 6162 | } |
| 6163 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6164 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
| 6165 | struct bpf_reg_state *src_reg) |
| 6166 | { |
| 6167 | struct tnum subreg = tnum_subreg(dst_reg->var_off); |
| 6168 | u32 umax_val = src_reg->u32_max_value; |
| 6169 | u32 umin_val = src_reg->u32_min_value; |
| 6170 | |
| 6171 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 6172 | * be negative, then either: |
| 6173 | * 1) src_reg might be zero, so the sign bit of the result is |
| 6174 | * unknown, so we lose our signed bounds |
| 6175 | * 2) it's known negative, thus the unsigned bounds capture the |
| 6176 | * signed bounds |
| 6177 | * 3) the signed bounds cross zero, so they tell us nothing |
| 6178 | * about the result |
| 6179 | * If the value in dst_reg is known nonnegative, then again the |
| 6180 | * unsigned bounts capture the signed bounds. |
| 6181 | * Thus, in all cases it suffices to blow away our signed bounds |
| 6182 | * and rely on inferring new ones from the unsigned bounds and |
| 6183 | * var_off of the result. |
| 6184 | */ |
| 6185 | dst_reg->s32_min_value = S32_MIN; |
| 6186 | dst_reg->s32_max_value = S32_MAX; |
| 6187 | |
| 6188 | dst_reg->var_off = tnum_rshift(subreg, umin_val); |
| 6189 | dst_reg->u32_min_value >>= umax_val; |
| 6190 | dst_reg->u32_max_value >>= umin_val; |
| 6191 | |
| 6192 | __mark_reg64_unbounded(dst_reg); |
| 6193 | __update_reg32_bounds(dst_reg); |
| 6194 | } |
| 6195 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6196 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
| 6197 | struct bpf_reg_state *src_reg) |
| 6198 | { |
| 6199 | u64 umax_val = src_reg->umax_value; |
| 6200 | u64 umin_val = src_reg->umin_value; |
| 6201 | |
| 6202 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 6203 | * be negative, then either: |
| 6204 | * 1) src_reg might be zero, so the sign bit of the result is |
| 6205 | * unknown, so we lose our signed bounds |
| 6206 | * 2) it's known negative, thus the unsigned bounds capture the |
| 6207 | * signed bounds |
| 6208 | * 3) the signed bounds cross zero, so they tell us nothing |
| 6209 | * about the result |
| 6210 | * If the value in dst_reg is known nonnegative, then again the |
| 6211 | * unsigned bounts capture the signed bounds. |
| 6212 | * Thus, in all cases it suffices to blow away our signed bounds |
| 6213 | * and rely on inferring new ones from the unsigned bounds and |
| 6214 | * var_off of the result. |
| 6215 | */ |
| 6216 | dst_reg->smin_value = S64_MIN; |
| 6217 | dst_reg->smax_value = S64_MAX; |
| 6218 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
| 6219 | dst_reg->umin_value >>= umax_val; |
| 6220 | dst_reg->umax_value >>= umin_val; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6221 | |
| 6222 | /* Its not easy to operate on alu32 bounds here because it depends |
| 6223 | * on bits being shifted in. Take easy way out and mark unbounded |
| 6224 | * so we can recalculate later from tnum. |
| 6225 | */ |
| 6226 | __mark_reg32_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6227 | __update_reg_bounds(dst_reg); |
| 6228 | } |
| 6229 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6230 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
| 6231 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6232 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6233 | u64 umin_val = src_reg->u32_min_value; |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6234 | |
| 6235 | /* Upon reaching here, src_known is true and |
| 6236 | * umax_val is equal to umin_val. |
| 6237 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6238 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
| 6239 | 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] | 6240 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6241 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
| 6242 | |
| 6243 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 6244 | * dst_reg var_off to refine the result. |
| 6245 | */ |
| 6246 | dst_reg->u32_min_value = 0; |
| 6247 | dst_reg->u32_max_value = U32_MAX; |
| 6248 | |
| 6249 | __mark_reg64_unbounded(dst_reg); |
| 6250 | __update_reg32_bounds(dst_reg); |
| 6251 | } |
| 6252 | |
| 6253 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, |
| 6254 | struct bpf_reg_state *src_reg) |
| 6255 | { |
| 6256 | u64 umin_val = src_reg->umin_value; |
| 6257 | |
| 6258 | /* Upon reaching here, src_known is true and umax_val is equal |
| 6259 | * to umin_val. |
| 6260 | */ |
| 6261 | dst_reg->smin_value >>= umin_val; |
| 6262 | dst_reg->smax_value >>= umin_val; |
| 6263 | |
| 6264 | 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] | 6265 | |
| 6266 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 6267 | * dst_reg var_off to refine the result. |
| 6268 | */ |
| 6269 | dst_reg->umin_value = 0; |
| 6270 | dst_reg->umax_value = U64_MAX; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6271 | |
| 6272 | /* Its not easy to operate on alu32 bounds here because it depends |
| 6273 | * on bits being shifted in from upper 32-bits. Take easy way out |
| 6274 | * and mark unbounded so we can recalculate later from tnum. |
| 6275 | */ |
| 6276 | __mark_reg32_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6277 | __update_reg_bounds(dst_reg); |
| 6278 | } |
| 6279 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6280 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 6281 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 6282 | * need extra checks in the 32-bit case. |
| 6283 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6284 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 6285 | struct bpf_insn *insn, |
| 6286 | struct bpf_reg_state *dst_reg, |
| 6287 | struct bpf_reg_state src_reg) |
| 6288 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6289 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6290 | u8 opcode = BPF_OP(insn->code); |
Mao Wenan | b0b3fb6 | 2020-04-18 09:37:35 +0800 | [diff] [blame] | 6291 | bool src_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6292 | s64 smin_val, smax_val; |
| 6293 | u64 umin_val, umax_val; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6294 | s32 s32_min_val, s32_max_val; |
| 6295 | u32 u32_min_val, u32_max_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6296 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 6297 | u32 dst = insn->dst_reg; |
| 6298 | int ret; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6299 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
Jann Horn | b799207 | 2018-10-05 18:17:59 +0200 | [diff] [blame] | 6300 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6301 | smin_val = src_reg.smin_value; |
| 6302 | smax_val = src_reg.smax_value; |
| 6303 | umin_val = src_reg.umin_value; |
| 6304 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6305 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6306 | s32_min_val = src_reg.s32_min_value; |
| 6307 | s32_max_val = src_reg.s32_max_value; |
| 6308 | u32_min_val = src_reg.u32_min_value; |
| 6309 | u32_max_val = src_reg.u32_max_value; |
| 6310 | |
| 6311 | if (alu32) { |
| 6312 | src_known = tnum_subreg_is_const(src_reg.var_off); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6313 | if ((src_known && |
| 6314 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || |
| 6315 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { |
| 6316 | /* Taint dst register if offset had invalid bounds |
| 6317 | * derived from e.g. dead branches. |
| 6318 | */ |
| 6319 | __mark_reg_unknown(env, dst_reg); |
| 6320 | return 0; |
| 6321 | } |
| 6322 | } else { |
| 6323 | src_known = tnum_is_const(src_reg.var_off); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6324 | if ((src_known && |
| 6325 | (smin_val != smax_val || umin_val != umax_val)) || |
| 6326 | smin_val > smax_val || umin_val > umax_val) { |
| 6327 | /* Taint dst register if offset had invalid bounds |
| 6328 | * derived from e.g. dead branches. |
| 6329 | */ |
| 6330 | __mark_reg_unknown(env, dst_reg); |
| 6331 | return 0; |
| 6332 | } |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 6333 | } |
| 6334 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 6335 | if (!src_known && |
| 6336 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 6337 | __mark_reg_unknown(env, dst_reg); |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 6338 | return 0; |
| 6339 | } |
| 6340 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6341 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
| 6342 | * There are two classes of instructions: The first class we track both |
| 6343 | * alu32 and alu64 sign/unsigned bounds independently this provides the |
| 6344 | * greatest amount of precision when alu operations are mixed with jmp32 |
| 6345 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, |
| 6346 | * and BPF_OR. This is possible because these ops have fairly easy to |
| 6347 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. |
| 6348 | * See alu32 verifier tests for examples. The second class of |
| 6349 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy |
| 6350 | * with regards to tracking sign/unsigned bounds because the bits may |
| 6351 | * cross subreg boundaries in the alu64 case. When this happens we mark |
| 6352 | * the reg unbounded in the subreg bound space and use the resulting |
| 6353 | * tnum to calculate an approximation of the sign/unsigned bounds. |
| 6354 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6355 | switch (opcode) { |
| 6356 | case BPF_ADD: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 6357 | ret = sanitize_val_alu(env, insn); |
| 6358 | if (ret < 0) { |
| 6359 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); |
| 6360 | return ret; |
| 6361 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6362 | scalar32_min_max_add(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6363 | scalar_min_max_add(dst_reg, &src_reg); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6364 | 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] | 6365 | break; |
| 6366 | case BPF_SUB: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 6367 | ret = sanitize_val_alu(env, insn); |
| 6368 | if (ret < 0) { |
| 6369 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); |
| 6370 | return ret; |
| 6371 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6372 | scalar32_min_max_sub(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6373 | scalar_min_max_sub(dst_reg, &src_reg); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6374 | 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] | 6375 | break; |
| 6376 | case BPF_MUL: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6377 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 6378 | scalar32_min_max_mul(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6379 | scalar_min_max_mul(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6380 | break; |
| 6381 | case BPF_AND: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6382 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
| 6383 | scalar32_min_max_and(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6384 | scalar_min_max_and(dst_reg, &src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6385 | break; |
| 6386 | case BPF_OR: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6387 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
| 6388 | scalar32_min_max_or(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6389 | scalar_min_max_or(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6390 | break; |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 6391 | case BPF_XOR: |
| 6392 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); |
| 6393 | scalar32_min_max_xor(dst_reg, &src_reg); |
| 6394 | scalar_min_max_xor(dst_reg, &src_reg); |
| 6395 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6396 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6397 | if (umax_val >= insn_bitness) { |
| 6398 | /* Shifts greater than 31 or 63 are undefined. |
| 6399 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6400 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6401 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6402 | break; |
| 6403 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6404 | if (alu32) |
| 6405 | scalar32_min_max_lsh(dst_reg, &src_reg); |
| 6406 | else |
| 6407 | scalar_min_max_lsh(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6408 | break; |
| 6409 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6410 | if (umax_val >= insn_bitness) { |
| 6411 | /* Shifts greater than 31 or 63 are undefined. |
| 6412 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6413 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6414 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6415 | break; |
| 6416 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6417 | if (alu32) |
| 6418 | scalar32_min_max_rsh(dst_reg, &src_reg); |
| 6419 | else |
| 6420 | scalar_min_max_rsh(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6421 | break; |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 6422 | case BPF_ARSH: |
| 6423 | if (umax_val >= insn_bitness) { |
| 6424 | /* Shifts greater than 31 or 63 are undefined. |
| 6425 | * This includes shifts by a negative number. |
| 6426 | */ |
| 6427 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 6428 | break; |
| 6429 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6430 | if (alu32) |
| 6431 | scalar32_min_max_arsh(dst_reg, &src_reg); |
| 6432 | else |
| 6433 | scalar_min_max_arsh(dst_reg, &src_reg); |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 6434 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6435 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6436 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6437 | break; |
| 6438 | } |
| 6439 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6440 | /* ALU32 ops are zero extended into 64bit register */ |
| 6441 | if (alu32) |
| 6442 | zext_32_to_64(dst_reg); |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6443 | |
John Fastabend | 294f2fc | 2020-03-24 10:38:37 -0700 | [diff] [blame] | 6444 | __update_reg_bounds(dst_reg); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6445 | __reg_deduce_bounds(dst_reg); |
| 6446 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6447 | return 0; |
| 6448 | } |
| 6449 | |
| 6450 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 6451 | * and var_off. |
| 6452 | */ |
| 6453 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 6454 | struct bpf_insn *insn) |
| 6455 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6456 | struct bpf_verifier_state *vstate = env->cur_state; |
| 6457 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 6458 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6459 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 6460 | u8 opcode = BPF_OP(insn->code); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 6461 | int err; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6462 | |
| 6463 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6464 | src_reg = NULL; |
| 6465 | if (dst_reg->type != SCALAR_VALUE) |
| 6466 | ptr_reg = dst_reg; |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 6467 | else |
| 6468 | /* Make sure ID is cleared otherwise dst_reg min/max could be |
| 6469 | * incorrectly propagated into other registers by find_equal_scalars() |
| 6470 | */ |
| 6471 | dst_reg->id = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6472 | if (BPF_SRC(insn->code) == BPF_X) { |
| 6473 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6474 | if (src_reg->type != SCALAR_VALUE) { |
| 6475 | if (dst_reg->type != SCALAR_VALUE) { |
| 6476 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6477 | * an arbitrary scalar. Disallow all math except |
| 6478 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6479 | */ |
Alexei Starovoitov | dd06682 | 2018-09-12 14:06:10 -0700 | [diff] [blame] | 6480 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6481 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 6482 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6483 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6484 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 6485 | insn->dst_reg, |
| 6486 | bpf_alu_string[opcode >> 4]); |
| 6487 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6488 | } else { |
| 6489 | /* scalar += pointer |
| 6490 | * This is legal, but we have to reverse our |
| 6491 | * src/dest handling in computing the range |
| 6492 | */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 6493 | err = mark_chain_precision(env, insn->dst_reg); |
| 6494 | if (err) |
| 6495 | return err; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6496 | return adjust_ptr_min_max_vals(env, insn, |
| 6497 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6498 | } |
| 6499 | } else if (ptr_reg) { |
| 6500 | /* pointer += scalar */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 6501 | err = mark_chain_precision(env, insn->src_reg); |
| 6502 | if (err) |
| 6503 | return err; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6504 | return adjust_ptr_min_max_vals(env, insn, |
| 6505 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6506 | } |
| 6507 | } else { |
| 6508 | /* Pretend the src is a reg with a known value, since we only |
| 6509 | * need to be able to read from this state. |
| 6510 | */ |
| 6511 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6512 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6513 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6514 | if (ptr_reg) /* pointer += K */ |
| 6515 | return adjust_ptr_min_max_vals(env, insn, |
| 6516 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6517 | } |
| 6518 | |
| 6519 | /* Got here implies adding two SCALAR_VALUEs */ |
| 6520 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6521 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6522 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6523 | return -EINVAL; |
| 6524 | } |
| 6525 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6526 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6527 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6528 | return -EINVAL; |
| 6529 | } |
| 6530 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6531 | } |
| 6532 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6533 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6534 | 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] | 6535 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6536 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6537 | u8 opcode = BPF_OP(insn->code); |
| 6538 | int err; |
| 6539 | |
| 6540 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 6541 | if (opcode == BPF_NEG) { |
| 6542 | if (BPF_SRC(insn->code) != 0 || |
| 6543 | insn->src_reg != BPF_REG_0 || |
| 6544 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6545 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6546 | return -EINVAL; |
| 6547 | } |
| 6548 | } else { |
| 6549 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 6550 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 6551 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6552 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6553 | return -EINVAL; |
| 6554 | } |
| 6555 | } |
| 6556 | |
| 6557 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6558 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6559 | if (err) |
| 6560 | return err; |
| 6561 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6562 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6563 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6564 | insn->dst_reg); |
| 6565 | return -EACCES; |
| 6566 | } |
| 6567 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6568 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6569 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6570 | if (err) |
| 6571 | return err; |
| 6572 | |
| 6573 | } else if (opcode == BPF_MOV) { |
| 6574 | |
| 6575 | if (BPF_SRC(insn->code) == BPF_X) { |
| 6576 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6577 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6578 | return -EINVAL; |
| 6579 | } |
| 6580 | |
| 6581 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6582 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6583 | if (err) |
| 6584 | return err; |
| 6585 | } else { |
| 6586 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6587 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6588 | return -EINVAL; |
| 6589 | } |
| 6590 | } |
| 6591 | |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 6592 | /* check dest operand, mark as required later */ |
| 6593 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6594 | if (err) |
| 6595 | return err; |
| 6596 | |
| 6597 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6598 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
| 6599 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; |
| 6600 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6601 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 6602 | /* case: R1 = R2 |
| 6603 | * copy register state to dest reg |
| 6604 | */ |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 6605 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
| 6606 | /* Assign src and dst registers the same ID |
| 6607 | * that will be used by find_equal_scalars() |
| 6608 | * to propagate min/max range. |
| 6609 | */ |
| 6610 | src_reg->id = ++env->id_gen; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6611 | *dst_reg = *src_reg; |
| 6612 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6613 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6614 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6615 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6616 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6617 | verbose(env, |
| 6618 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6619 | insn->src_reg); |
| 6620 | return -EACCES; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6621 | } else if (src_reg->type == SCALAR_VALUE) { |
| 6622 | *dst_reg = *src_reg; |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 6623 | /* Make sure ID is cleared otherwise |
| 6624 | * dst_reg min/max could be incorrectly |
| 6625 | * propagated into src_reg by find_equal_scalars() |
| 6626 | */ |
| 6627 | dst_reg->id = 0; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6628 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6629 | dst_reg->subreg_def = env->insn_idx + 1; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6630 | } else { |
| 6631 | mark_reg_unknown(env, regs, |
| 6632 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6633 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6634 | zext_32_to_64(dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6635 | } |
| 6636 | } else { |
| 6637 | /* case: R = imm |
| 6638 | * remember the value we stored into this reg |
| 6639 | */ |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 6640 | /* clear any state __mark_reg_known doesn't set */ |
| 6641 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6642 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 6643 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 6644 | __mark_reg_known(regs + insn->dst_reg, |
| 6645 | insn->imm); |
| 6646 | } else { |
| 6647 | __mark_reg_known(regs + insn->dst_reg, |
| 6648 | (u32)insn->imm); |
| 6649 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6650 | } |
| 6651 | |
| 6652 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6653 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6654 | return -EINVAL; |
| 6655 | |
| 6656 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 6657 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6658 | if (BPF_SRC(insn->code) == BPF_X) { |
| 6659 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6660 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6661 | return -EINVAL; |
| 6662 | } |
| 6663 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6664 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6665 | if (err) |
| 6666 | return err; |
| 6667 | } else { |
| 6668 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6669 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6670 | return -EINVAL; |
| 6671 | } |
| 6672 | } |
| 6673 | |
| 6674 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6675 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6676 | if (err) |
| 6677 | return err; |
| 6678 | |
| 6679 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 6680 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6681 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6682 | return -EINVAL; |
| 6683 | } |
| 6684 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 6685 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 6686 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 6687 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 6688 | |
| 6689 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6690 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 6691 | return -EINVAL; |
| 6692 | } |
| 6693 | } |
| 6694 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 6695 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6696 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 6697 | if (err) |
| 6698 | return err; |
| 6699 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6700 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6701 | } |
| 6702 | |
| 6703 | return 0; |
| 6704 | } |
| 6705 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 6706 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
| 6707 | struct bpf_reg_state *dst_reg, |
| 6708 | enum bpf_reg_type type, u16 new_range) |
| 6709 | { |
| 6710 | struct bpf_reg_state *reg; |
| 6711 | int i; |
| 6712 | |
| 6713 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 6714 | reg = &state->regs[i]; |
| 6715 | if (reg->type == type && reg->id == dst_reg->id) |
| 6716 | /* keep the maximum range already checked */ |
| 6717 | reg->range = max(reg->range, new_range); |
| 6718 | } |
| 6719 | |
| 6720 | bpf_for_each_spilled_reg(i, state, reg) { |
| 6721 | if (!reg) |
| 6722 | continue; |
| 6723 | if (reg->type == type && reg->id == dst_reg->id) |
| 6724 | reg->range = max(reg->range, new_range); |
| 6725 | } |
| 6726 | } |
| 6727 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6728 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 6729 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 6730 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6731 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6732 | { |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6733 | u16 new_range; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 6734 | int i; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6735 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6736 | if (dst_reg->off < 0 || |
| 6737 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6738 | /* This doesn't give us any range */ |
| 6739 | return; |
| 6740 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6741 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 6742 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6743 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 6744 | * than pkt_end, but that's because it's also less than pkt. |
| 6745 | */ |
| 6746 | return; |
| 6747 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6748 | new_range = dst_reg->off; |
| 6749 | if (range_right_open) |
| 6750 | new_range--; |
| 6751 | |
| 6752 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6753 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6754 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6755 | * |
| 6756 | * r2 = r3; |
| 6757 | * r2 += 8; |
| 6758 | * if (r2 > pkt_end) goto <handle exception> |
| 6759 | * <access okay> |
| 6760 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 6761 | * r2 = r3; |
| 6762 | * r2 += 8; |
| 6763 | * if (r2 < pkt_end) goto <access okay> |
| 6764 | * <handle exception> |
| 6765 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6766 | * Where: |
| 6767 | * r2 == dst_reg, pkt_end == src_reg |
| 6768 | * r2=pkt(id=n,off=8,r=0) |
| 6769 | * r3=pkt(id=n,off=0,r=0) |
| 6770 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6771 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6772 | * |
| 6773 | * r2 = r3; |
| 6774 | * r2 += 8; |
| 6775 | * if (pkt_end >= r2) goto <access okay> |
| 6776 | * <handle exception> |
| 6777 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 6778 | * r2 = r3; |
| 6779 | * r2 += 8; |
| 6780 | * if (pkt_end <= r2) goto <handle exception> |
| 6781 | * <access okay> |
| 6782 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6783 | * Where: |
| 6784 | * pkt_end == dst_reg, r2 == src_reg |
| 6785 | * r2=pkt(id=n,off=8,r=0) |
| 6786 | * r3=pkt(id=n,off=0,r=0) |
| 6787 | * |
| 6788 | * 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] | 6789 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 6790 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 6791 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6792 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6793 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6794 | /* If our ids match, then we must have the same max_value. And we |
| 6795 | * don't care about the other reg's fixed offset, since if it's too big |
| 6796 | * the range won't allow anything. |
| 6797 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 6798 | */ |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 6799 | for (i = 0; i <= vstate->curframe; i++) |
| 6800 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, |
| 6801 | new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6802 | } |
| 6803 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6804 | 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] | 6805 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6806 | struct tnum subreg = tnum_subreg(reg->var_off); |
| 6807 | s32 sval = (s32)val; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6808 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6809 | switch (opcode) { |
| 6810 | case BPF_JEQ: |
| 6811 | if (tnum_is_const(subreg)) |
| 6812 | return !!tnum_equals_const(subreg, val); |
| 6813 | break; |
| 6814 | case BPF_JNE: |
| 6815 | if (tnum_is_const(subreg)) |
| 6816 | return !tnum_equals_const(subreg, val); |
| 6817 | break; |
| 6818 | case BPF_JSET: |
| 6819 | if ((~subreg.mask & subreg.value) & val) |
| 6820 | return 1; |
| 6821 | if (!((subreg.mask | subreg.value) & val)) |
| 6822 | return 0; |
| 6823 | break; |
| 6824 | case BPF_JGT: |
| 6825 | if (reg->u32_min_value > val) |
| 6826 | return 1; |
| 6827 | else if (reg->u32_max_value <= val) |
| 6828 | return 0; |
| 6829 | break; |
| 6830 | case BPF_JSGT: |
| 6831 | if (reg->s32_min_value > sval) |
| 6832 | return 1; |
| 6833 | else if (reg->s32_max_value < sval) |
| 6834 | return 0; |
| 6835 | break; |
| 6836 | case BPF_JLT: |
| 6837 | if (reg->u32_max_value < val) |
| 6838 | return 1; |
| 6839 | else if (reg->u32_min_value >= val) |
| 6840 | return 0; |
| 6841 | break; |
| 6842 | case BPF_JSLT: |
| 6843 | if (reg->s32_max_value < sval) |
| 6844 | return 1; |
| 6845 | else if (reg->s32_min_value >= sval) |
| 6846 | return 0; |
| 6847 | break; |
| 6848 | case BPF_JGE: |
| 6849 | if (reg->u32_min_value >= val) |
| 6850 | return 1; |
| 6851 | else if (reg->u32_max_value < val) |
| 6852 | return 0; |
| 6853 | break; |
| 6854 | case BPF_JSGE: |
| 6855 | if (reg->s32_min_value >= sval) |
| 6856 | return 1; |
| 6857 | else if (reg->s32_max_value < sval) |
| 6858 | return 0; |
| 6859 | break; |
| 6860 | case BPF_JLE: |
| 6861 | if (reg->u32_max_value <= val) |
| 6862 | return 1; |
| 6863 | else if (reg->u32_min_value > val) |
| 6864 | return 0; |
| 6865 | break; |
| 6866 | case BPF_JSLE: |
| 6867 | if (reg->s32_max_value <= sval) |
| 6868 | return 1; |
| 6869 | else if (reg->s32_min_value > sval) |
| 6870 | return 0; |
| 6871 | break; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6872 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6873 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6874 | return -1; |
| 6875 | } |
| 6876 | |
| 6877 | |
| 6878 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) |
| 6879 | { |
| 6880 | s64 sval = (s64)val; |
| 6881 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6882 | switch (opcode) { |
| 6883 | case BPF_JEQ: |
| 6884 | if (tnum_is_const(reg->var_off)) |
| 6885 | return !!tnum_equals_const(reg->var_off, val); |
| 6886 | break; |
| 6887 | case BPF_JNE: |
| 6888 | if (tnum_is_const(reg->var_off)) |
| 6889 | return !tnum_equals_const(reg->var_off, val); |
| 6890 | break; |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 6891 | case BPF_JSET: |
| 6892 | if ((~reg->var_off.mask & reg->var_off.value) & val) |
| 6893 | return 1; |
| 6894 | if (!((reg->var_off.mask | reg->var_off.value) & val)) |
| 6895 | return 0; |
| 6896 | break; |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6897 | case BPF_JGT: |
| 6898 | if (reg->umin_value > val) |
| 6899 | return 1; |
| 6900 | else if (reg->umax_value <= val) |
| 6901 | return 0; |
| 6902 | break; |
| 6903 | case BPF_JSGT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6904 | if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6905 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6906 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6907 | return 0; |
| 6908 | break; |
| 6909 | case BPF_JLT: |
| 6910 | if (reg->umax_value < val) |
| 6911 | return 1; |
| 6912 | else if (reg->umin_value >= val) |
| 6913 | return 0; |
| 6914 | break; |
| 6915 | case BPF_JSLT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6916 | if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6917 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6918 | else if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6919 | return 0; |
| 6920 | break; |
| 6921 | case BPF_JGE: |
| 6922 | if (reg->umin_value >= val) |
| 6923 | return 1; |
| 6924 | else if (reg->umax_value < val) |
| 6925 | return 0; |
| 6926 | break; |
| 6927 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6928 | if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6929 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6930 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6931 | return 0; |
| 6932 | break; |
| 6933 | case BPF_JLE: |
| 6934 | if (reg->umax_value <= val) |
| 6935 | return 1; |
| 6936 | else if (reg->umin_value > val) |
| 6937 | return 0; |
| 6938 | break; |
| 6939 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6940 | if (reg->smax_value <= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6941 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6942 | else if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6943 | return 0; |
| 6944 | break; |
| 6945 | } |
| 6946 | |
| 6947 | return -1; |
| 6948 | } |
| 6949 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6950 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
| 6951 | * and return: |
| 6952 | * 1 - branch will be taken and "goto target" will be executed |
| 6953 | * 0 - branch will not be taken and fall-through to next insn |
| 6954 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value |
| 6955 | * range [0,10] |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6956 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6957 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
| 6958 | bool is_jmp32) |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6959 | { |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 6960 | if (__is_pointer_value(false, reg)) { |
| 6961 | if (!reg_type_not_null(reg->type)) |
| 6962 | return -1; |
| 6963 | |
| 6964 | /* If pointer is valid tests against zero will fail so we can |
| 6965 | * use this to direct branch taken. |
| 6966 | */ |
| 6967 | if (val != 0) |
| 6968 | return -1; |
| 6969 | |
| 6970 | switch (opcode) { |
| 6971 | case BPF_JEQ: |
| 6972 | return 0; |
| 6973 | case BPF_JNE: |
| 6974 | return 1; |
| 6975 | default: |
| 6976 | return -1; |
| 6977 | } |
| 6978 | } |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6979 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6980 | if (is_jmp32) |
| 6981 | return is_branch32_taken(reg, val, opcode); |
| 6982 | return is_branch64_taken(reg, val, opcode); |
Jann Horn | 604dca5 | 2020-03-30 18:03:23 +0200 | [diff] [blame] | 6983 | } |
| 6984 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6985 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 6986 | * variable register that we are working on, and src_reg is a constant or we're |
| 6987 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6988 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6989 | */ |
| 6990 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6991 | struct bpf_reg_state *false_reg, |
| 6992 | u64 val, u32 val32, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6993 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6994 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6995 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
| 6996 | struct tnum false_64off = false_reg->var_off; |
| 6997 | struct tnum true_32off = tnum_subreg(true_reg->var_off); |
| 6998 | struct tnum true_64off = true_reg->var_off; |
| 6999 | s64 sval = (s64)val; |
| 7000 | s32 sval32 = (s32)val32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7001 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7002 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 7003 | * variable offset from the compare (unless src_reg were a pointer into |
| 7004 | * the same object, but we don't bother with that. |
| 7005 | * Since false_reg and true_reg have the same type by construction, we |
| 7006 | * only need to check one of them for pointerness. |
| 7007 | */ |
| 7008 | if (__is_pointer_value(false, false_reg)) |
| 7009 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 7010 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7011 | switch (opcode) { |
| 7012 | case BPF_JEQ: |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7013 | case BPF_JNE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7014 | { |
| 7015 | struct bpf_reg_state *reg = |
| 7016 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 7017 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 7018 | /* JEQ/JNE comparison doesn't change the register equivalence. |
| 7019 | * r1 = r2; |
| 7020 | * if (r1 == 42) goto label; |
| 7021 | * ... |
| 7022 | * label: // here both r1 and r2 are known to be 42. |
| 7023 | * |
| 7024 | * Hence when marking register as known preserve it's ID. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7025 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7026 | if (is_jmp32) |
| 7027 | __mark_reg32_known(reg, val32); |
| 7028 | else |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 7029 | ___mark_reg_known(reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7030 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7031 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 7032 | case BPF_JSET: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7033 | if (is_jmp32) { |
| 7034 | false_32off = tnum_and(false_32off, tnum_const(~val32)); |
| 7035 | if (is_power_of_2(val32)) |
| 7036 | true_32off = tnum_or(true_32off, |
| 7037 | tnum_const(val32)); |
| 7038 | } else { |
| 7039 | false_64off = tnum_and(false_64off, tnum_const(~val)); |
| 7040 | if (is_power_of_2(val)) |
| 7041 | true_64off = tnum_or(true_64off, |
| 7042 | tnum_const(val)); |
| 7043 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 7044 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7045 | case BPF_JGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7046 | case BPF_JGT: |
| 7047 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7048 | if (is_jmp32) { |
| 7049 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; |
| 7050 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; |
| 7051 | |
| 7052 | false_reg->u32_max_value = min(false_reg->u32_max_value, |
| 7053 | false_umax); |
| 7054 | true_reg->u32_min_value = max(true_reg->u32_min_value, |
| 7055 | true_umin); |
| 7056 | } else { |
| 7057 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; |
| 7058 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; |
| 7059 | |
| 7060 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 7061 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
| 7062 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7063 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7064 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7065 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7066 | case BPF_JSGT: |
| 7067 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7068 | if (is_jmp32) { |
| 7069 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; |
| 7070 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7071 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7072 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
| 7073 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); |
| 7074 | } else { |
| 7075 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; |
| 7076 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; |
| 7077 | |
| 7078 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 7079 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
| 7080 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7081 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7082 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 7083 | case BPF_JLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7084 | case BPF_JLT: |
| 7085 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7086 | if (is_jmp32) { |
| 7087 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; |
| 7088 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; |
| 7089 | |
| 7090 | false_reg->u32_min_value = max(false_reg->u32_min_value, |
| 7091 | false_umin); |
| 7092 | true_reg->u32_max_value = min(true_reg->u32_max_value, |
| 7093 | true_umax); |
| 7094 | } else { |
| 7095 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; |
| 7096 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; |
| 7097 | |
| 7098 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 7099 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
| 7100 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 7101 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7102 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 7103 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7104 | case BPF_JSLT: |
| 7105 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7106 | if (is_jmp32) { |
| 7107 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; |
| 7108 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7109 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7110 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
| 7111 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); |
| 7112 | } else { |
| 7113 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; |
| 7114 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; |
| 7115 | |
| 7116 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 7117 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
| 7118 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 7119 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7120 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7121 | default: |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 7122 | return; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7123 | } |
| 7124 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7125 | if (is_jmp32) { |
| 7126 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), |
| 7127 | tnum_subreg(false_32off)); |
| 7128 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), |
| 7129 | tnum_subreg(true_32off)); |
| 7130 | __reg_combine_32_into_64(false_reg); |
| 7131 | __reg_combine_32_into_64(true_reg); |
| 7132 | } else { |
| 7133 | false_reg->var_off = false_64off; |
| 7134 | true_reg->var_off = true_64off; |
| 7135 | __reg_combine_64_into_32(false_reg); |
| 7136 | __reg_combine_64_into_32(true_reg); |
| 7137 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7138 | } |
| 7139 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7140 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 7141 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7142 | */ |
| 7143 | 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] | 7144 | struct bpf_reg_state *false_reg, |
| 7145 | u64 val, u32 val32, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7146 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7147 | { |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 7148 | /* How can we transform "a <op> b" into "b <op> a"? */ |
| 7149 | static const u8 opcode_flip[16] = { |
| 7150 | /* these stay the same */ |
| 7151 | [BPF_JEQ >> 4] = BPF_JEQ, |
| 7152 | [BPF_JNE >> 4] = BPF_JNE, |
| 7153 | [BPF_JSET >> 4] = BPF_JSET, |
| 7154 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ |
| 7155 | [BPF_JGE >> 4] = BPF_JLE, |
| 7156 | [BPF_JGT >> 4] = BPF_JLT, |
| 7157 | [BPF_JLE >> 4] = BPF_JGE, |
| 7158 | [BPF_JLT >> 4] = BPF_JGT, |
| 7159 | [BPF_JSGE >> 4] = BPF_JSLE, |
| 7160 | [BPF_JSGT >> 4] = BPF_JSLT, |
| 7161 | [BPF_JSLE >> 4] = BPF_JSGE, |
| 7162 | [BPF_JSLT >> 4] = BPF_JSGT |
| 7163 | }; |
| 7164 | opcode = opcode_flip[opcode >> 4]; |
| 7165 | /* This uses zero as "not present in table"; luckily the zero opcode, |
| 7166 | * BPF_JA, can't get here. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7167 | */ |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 7168 | if (opcode) |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7169 | 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] | 7170 | } |
| 7171 | |
| 7172 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 7173 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 7174 | struct bpf_reg_state *dst_reg) |
| 7175 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7176 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 7177 | dst_reg->umin_value); |
| 7178 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 7179 | dst_reg->umax_value); |
| 7180 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 7181 | dst_reg->smin_value); |
| 7182 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 7183 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7184 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 7185 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7186 | /* We might have learned new bounds from the var_off. */ |
| 7187 | __update_reg_bounds(src_reg); |
| 7188 | __update_reg_bounds(dst_reg); |
| 7189 | /* We might have learned something about the sign bit. */ |
| 7190 | __reg_deduce_bounds(src_reg); |
| 7191 | __reg_deduce_bounds(dst_reg); |
| 7192 | /* We might have learned some bits from the bounds. */ |
| 7193 | __reg_bound_offset(src_reg); |
| 7194 | __reg_bound_offset(dst_reg); |
| 7195 | /* Intersecting with the old var_off might have improved our bounds |
| 7196 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 7197 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 7198 | */ |
| 7199 | __update_reg_bounds(src_reg); |
| 7200 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7201 | } |
| 7202 | |
| 7203 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 7204 | struct bpf_reg_state *true_dst, |
| 7205 | struct bpf_reg_state *false_src, |
| 7206 | struct bpf_reg_state *false_dst, |
| 7207 | u8 opcode) |
| 7208 | { |
| 7209 | switch (opcode) { |
| 7210 | case BPF_JEQ: |
| 7211 | __reg_combine_min_max(true_src, true_dst); |
| 7212 | break; |
| 7213 | case BPF_JNE: |
| 7214 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7215 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 7216 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7217 | } |
| 7218 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7219 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 7220 | struct bpf_reg_state *reg, u32 id, |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7221 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7222 | { |
Martin KaFai Lau | 93c230e | 2020-10-19 12:42:12 -0700 | [diff] [blame] | 7223 | if (reg_type_may_be_null(reg->type) && reg->id == id && |
| 7224 | !WARN_ON_ONCE(!reg->id)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7225 | /* Old offset (both fixed and variable parts) should |
| 7226 | * have been known-zero, because we don't allow pointer |
| 7227 | * arithmetic on pointers that might be NULL. |
| 7228 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7229 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 7230 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7231 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7232 | __mark_reg_known_zero(reg); |
| 7233 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7234 | } |
| 7235 | if (is_null) { |
| 7236 | reg->type = SCALAR_VALUE; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7237 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 7238 | const struct bpf_map *map = reg->map_ptr; |
| 7239 | |
| 7240 | if (map->inner_map_meta) { |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7241 | reg->type = CONST_PTR_TO_MAP; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 7242 | reg->map_ptr = map->inner_map_meta; |
| 7243 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 7244 | reg->type = PTR_TO_XDP_SOCK; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 7245 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || |
| 7246 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { |
| 7247 | reg->type = PTR_TO_SOCKET; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7248 | } else { |
| 7249 | reg->type = PTR_TO_MAP_VALUE; |
| 7250 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7251 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
| 7252 | reg->type = PTR_TO_SOCKET; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 7253 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
| 7254 | reg->type = PTR_TO_SOCK_COMMON; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 7255 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
| 7256 | reg->type = PTR_TO_TCP_SOCK; |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 7257 | } else if (reg->type == PTR_TO_BTF_ID_OR_NULL) { |
| 7258 | reg->type = PTR_TO_BTF_ID; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 7259 | } else if (reg->type == PTR_TO_MEM_OR_NULL) { |
| 7260 | reg->type = PTR_TO_MEM; |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 7261 | } else if (reg->type == PTR_TO_RDONLY_BUF_OR_NULL) { |
| 7262 | reg->type = PTR_TO_RDONLY_BUF; |
| 7263 | } else if (reg->type == PTR_TO_RDWR_BUF_OR_NULL) { |
| 7264 | reg->type = PTR_TO_RDWR_BUF; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 7265 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 7266 | if (is_null) { |
| 7267 | /* We don't need id and ref_obj_id from this point |
| 7268 | * onwards anymore, thus we should better reset it, |
| 7269 | * so that state pruning has chances to take effect. |
| 7270 | */ |
| 7271 | reg->id = 0; |
| 7272 | reg->ref_obj_id = 0; |
| 7273 | } else if (!reg_may_point_to_spin_lock(reg)) { |
| 7274 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
| 7275 | * in release_reg_references(). |
| 7276 | * |
| 7277 | * reg->id is still used by spin_lock ptr. Other |
| 7278 | * than spin_lock ptr type, reg->id can be reset. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7279 | */ |
| 7280 | reg->id = 0; |
| 7281 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7282 | } |
| 7283 | } |
| 7284 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 7285 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
| 7286 | bool is_null) |
| 7287 | { |
| 7288 | struct bpf_reg_state *reg; |
| 7289 | int i; |
| 7290 | |
| 7291 | for (i = 0; i < MAX_BPF_REG; i++) |
| 7292 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); |
| 7293 | |
| 7294 | bpf_for_each_spilled_reg(i, state, reg) { |
| 7295 | if (!reg) |
| 7296 | continue; |
| 7297 | mark_ptr_or_null_reg(state, reg, id, is_null); |
| 7298 | } |
| 7299 | } |
| 7300 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7301 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 7302 | * be folded together at some point. |
| 7303 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7304 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 7305 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7306 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7307 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 7308 | struct bpf_reg_state *regs = state->regs; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 7309 | u32 ref_obj_id = regs[regno].ref_obj_id; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 7310 | u32 id = regs[regno].id; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 7311 | int i; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7312 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 7313 | if (ref_obj_id && ref_obj_id == id && is_null) |
| 7314 | /* regs[regno] is in the " == NULL" branch. |
| 7315 | * No one could have freed the reference state before |
| 7316 | * doing the NULL check. |
| 7317 | */ |
| 7318 | WARN_ON_ONCE(release_reference_state(state, id)); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7319 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 7320 | for (i = 0; i <= vstate->curframe; i++) |
| 7321 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7322 | } |
| 7323 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7324 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 7325 | struct bpf_reg_state *dst_reg, |
| 7326 | struct bpf_reg_state *src_reg, |
| 7327 | struct bpf_verifier_state *this_branch, |
| 7328 | struct bpf_verifier_state *other_branch) |
| 7329 | { |
| 7330 | if (BPF_SRC(insn->code) != BPF_X) |
| 7331 | return false; |
| 7332 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7333 | /* Pointers are always 64-bit. */ |
| 7334 | if (BPF_CLASS(insn->code) == BPF_JMP32) |
| 7335 | return false; |
| 7336 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7337 | switch (BPF_OP(insn->code)) { |
| 7338 | case BPF_JGT: |
| 7339 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7340 | src_reg->type == PTR_TO_PACKET_END) || |
| 7341 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7342 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7343 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 7344 | find_good_pkt_pointers(this_branch, dst_reg, |
| 7345 | dst_reg->type, false); |
| 7346 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7347 | src_reg->type == PTR_TO_PACKET) || |
| 7348 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7349 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7350 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 7351 | find_good_pkt_pointers(other_branch, src_reg, |
| 7352 | src_reg->type, true); |
| 7353 | } else { |
| 7354 | return false; |
| 7355 | } |
| 7356 | break; |
| 7357 | case BPF_JLT: |
| 7358 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7359 | src_reg->type == PTR_TO_PACKET_END) || |
| 7360 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7361 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7362 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 7363 | find_good_pkt_pointers(other_branch, dst_reg, |
| 7364 | dst_reg->type, true); |
| 7365 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7366 | src_reg->type == PTR_TO_PACKET) || |
| 7367 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7368 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7369 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 7370 | find_good_pkt_pointers(this_branch, src_reg, |
| 7371 | src_reg->type, false); |
| 7372 | } else { |
| 7373 | return false; |
| 7374 | } |
| 7375 | break; |
| 7376 | case BPF_JGE: |
| 7377 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7378 | src_reg->type == PTR_TO_PACKET_END) || |
| 7379 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7380 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7381 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 7382 | find_good_pkt_pointers(this_branch, dst_reg, |
| 7383 | dst_reg->type, true); |
| 7384 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7385 | src_reg->type == PTR_TO_PACKET) || |
| 7386 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7387 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7388 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 7389 | find_good_pkt_pointers(other_branch, src_reg, |
| 7390 | src_reg->type, false); |
| 7391 | } else { |
| 7392 | return false; |
| 7393 | } |
| 7394 | break; |
| 7395 | case BPF_JLE: |
| 7396 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7397 | src_reg->type == PTR_TO_PACKET_END) || |
| 7398 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7399 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7400 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 7401 | find_good_pkt_pointers(other_branch, dst_reg, |
| 7402 | dst_reg->type, false); |
| 7403 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7404 | src_reg->type == PTR_TO_PACKET) || |
| 7405 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7406 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7407 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 7408 | find_good_pkt_pointers(this_branch, src_reg, |
| 7409 | src_reg->type, true); |
| 7410 | } else { |
| 7411 | return false; |
| 7412 | } |
| 7413 | break; |
| 7414 | default: |
| 7415 | return false; |
| 7416 | } |
| 7417 | |
| 7418 | return true; |
| 7419 | } |
| 7420 | |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 7421 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
| 7422 | struct bpf_reg_state *known_reg) |
| 7423 | { |
| 7424 | struct bpf_func_state *state; |
| 7425 | struct bpf_reg_state *reg; |
| 7426 | int i, j; |
| 7427 | |
| 7428 | for (i = 0; i <= vstate->curframe; i++) { |
| 7429 | state = vstate->frame[i]; |
| 7430 | for (j = 0; j < MAX_BPF_REG; j++) { |
| 7431 | reg = &state->regs[j]; |
| 7432 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) |
| 7433 | *reg = *known_reg; |
| 7434 | } |
| 7435 | |
| 7436 | bpf_for_each_spilled_reg(j, state, reg) { |
| 7437 | if (!reg) |
| 7438 | continue; |
| 7439 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) |
| 7440 | *reg = *known_reg; |
| 7441 | } |
| 7442 | } |
| 7443 | } |
| 7444 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7445 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7446 | struct bpf_insn *insn, int *insn_idx) |
| 7447 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7448 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 7449 | struct bpf_verifier_state *other_branch; |
| 7450 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 7451 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7452 | u8 opcode = BPF_OP(insn->code); |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7453 | bool is_jmp32; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 7454 | int pred = -1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7455 | int err; |
| 7456 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7457 | /* Only conditional jumps are expected to reach here. */ |
| 7458 | if (opcode == BPF_JA || opcode > BPF_JSLE) { |
| 7459 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7460 | return -EINVAL; |
| 7461 | } |
| 7462 | |
| 7463 | if (BPF_SRC(insn->code) == BPF_X) { |
| 7464 | if (insn->imm != 0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7465 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7466 | return -EINVAL; |
| 7467 | } |
| 7468 | |
| 7469 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7470 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7471 | if (err) |
| 7472 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7473 | |
| 7474 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7475 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7476 | insn->src_reg); |
| 7477 | return -EACCES; |
| 7478 | } |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 7479 | src_reg = ®s[insn->src_reg]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7480 | } else { |
| 7481 | if (insn->src_reg != BPF_REG_0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7482 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7483 | return -EINVAL; |
| 7484 | } |
| 7485 | } |
| 7486 | |
| 7487 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7488 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7489 | if (err) |
| 7490 | return err; |
| 7491 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 7492 | dst_reg = ®s[insn->dst_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7493 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 7494 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7495 | if (BPF_SRC(insn->code) == BPF_K) { |
| 7496 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); |
| 7497 | } else if (src_reg->type == SCALAR_VALUE && |
| 7498 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { |
| 7499 | pred = is_branch_taken(dst_reg, |
| 7500 | tnum_subreg(src_reg->var_off).value, |
| 7501 | opcode, |
| 7502 | is_jmp32); |
| 7503 | } else if (src_reg->type == SCALAR_VALUE && |
| 7504 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { |
| 7505 | pred = is_branch_taken(dst_reg, |
| 7506 | src_reg->var_off.value, |
| 7507 | opcode, |
| 7508 | is_jmp32); |
| 7509 | } |
| 7510 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7511 | if (pred >= 0) { |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 7512 | /* If we get here with a dst_reg pointer type it is because |
| 7513 | * above is_branch_taken() special cased the 0 comparison. |
| 7514 | */ |
| 7515 | if (!__is_pointer_value(false, dst_reg)) |
| 7516 | err = mark_chain_precision(env, insn->dst_reg); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7517 | if (BPF_SRC(insn->code) == BPF_X && !err) |
| 7518 | err = mark_chain_precision(env, insn->src_reg); |
| 7519 | if (err) |
| 7520 | return err; |
| 7521 | } |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 7522 | if (pred == 1) { |
| 7523 | /* only follow the goto, ignore fall-through */ |
| 7524 | *insn_idx += insn->off; |
| 7525 | return 0; |
| 7526 | } else if (pred == 0) { |
| 7527 | /* only follow fall-through branch, since |
| 7528 | * that's where the program will go |
| 7529 | */ |
| 7530 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7531 | } |
| 7532 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7533 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
| 7534 | false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7535 | if (!other_branch) |
| 7536 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7537 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7538 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7539 | /* detect if we are comparing against a constant value so we can adjust |
| 7540 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7541 | * this is only legit if both are scalars (or pointers to the same |
| 7542 | * object, I suppose, but we don't support that right now), because |
| 7543 | * otherwise the different base pointers mean the offsets aren't |
| 7544 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7545 | */ |
| 7546 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7547 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7548 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7549 | if (dst_reg->type == SCALAR_VALUE && |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7550 | src_reg->type == SCALAR_VALUE) { |
| 7551 | if (tnum_is_const(src_reg->var_off) || |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7552 | (is_jmp32 && |
| 7553 | tnum_is_const(tnum_subreg(src_reg->var_off)))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7554 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7555 | dst_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7556 | src_reg->var_off.value, |
| 7557 | tnum_subreg(src_reg->var_off).value, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7558 | opcode, is_jmp32); |
| 7559 | else if (tnum_is_const(dst_reg->var_off) || |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7560 | (is_jmp32 && |
| 7561 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7562 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7563 | src_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7564 | dst_reg->var_off.value, |
| 7565 | tnum_subreg(dst_reg->var_off).value, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7566 | opcode, is_jmp32); |
| 7567 | else if (!is_jmp32 && |
| 7568 | (opcode == BPF_JEQ || opcode == BPF_JNE)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7569 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7570 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 7571 | &other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7572 | src_reg, dst_reg, opcode); |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 7573 | if (src_reg->id && |
| 7574 | !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] | 7575 | find_equal_scalars(this_branch, src_reg); |
| 7576 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); |
| 7577 | } |
| 7578 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7579 | } |
| 7580 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7581 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7582 | dst_reg, insn->imm, (u32)insn->imm, |
| 7583 | opcode, is_jmp32); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7584 | } |
| 7585 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 7586 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
| 7587 | !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] | 7588 | find_equal_scalars(this_branch, dst_reg); |
| 7589 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); |
| 7590 | } |
| 7591 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7592 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
| 7593 | * NOTE: these optimizations below are related with pointer comparison |
| 7594 | * which will never be JMP32. |
| 7595 | */ |
| 7596 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 7597 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7598 | reg_type_may_be_null(dst_reg->type)) { |
| 7599 | /* Mark all identical registers in each branch as either |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7600 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 7601 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7602 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 7603 | opcode == BPF_JNE); |
| 7604 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 7605 | opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7606 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 7607 | this_branch, other_branch) && |
| 7608 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7609 | verbose(env, "R%d pointer comparison prohibited\n", |
| 7610 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7611 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7612 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 7613 | if (env->log.level & BPF_LOG_LEVEL) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7614 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7615 | return 0; |
| 7616 | } |
| 7617 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7618 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7619 | 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] | 7620 | { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7621 | struct bpf_insn_aux_data *aux = cur_aux(env); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7622 | struct bpf_reg_state *regs = cur_regs(env); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7623 | struct bpf_reg_state *dst_reg; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7624 | struct bpf_map *map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7625 | int err; |
| 7626 | |
| 7627 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7628 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7629 | return -EINVAL; |
| 7630 | } |
| 7631 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7632 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7633 | return -EINVAL; |
| 7634 | } |
| 7635 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7636 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7637 | if (err) |
| 7638 | return err; |
| 7639 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7640 | dst_reg = ®s[insn->dst_reg]; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 7641 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 7642 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 7643 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7644 | dst_reg->type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7645 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7646 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 7647 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7648 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7649 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
| 7650 | mark_reg_known_zero(env, regs, insn->dst_reg); |
| 7651 | |
| 7652 | dst_reg->type = aux->btf_var.reg_type; |
| 7653 | switch (dst_reg->type) { |
| 7654 | case PTR_TO_MEM: |
| 7655 | dst_reg->mem_size = aux->btf_var.mem_size; |
| 7656 | break; |
| 7657 | case PTR_TO_BTF_ID: |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 7658 | case PTR_TO_PERCPU_BTF_ID: |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7659 | dst_reg->btf_id = aux->btf_var.btf_id; |
| 7660 | break; |
| 7661 | default: |
| 7662 | verbose(env, "bpf verifier is misconfigured\n"); |
| 7663 | return -EFAULT; |
| 7664 | } |
| 7665 | return 0; |
| 7666 | } |
| 7667 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7668 | map = env->used_maps[aux->map_index]; |
| 7669 | mark_reg_known_zero(env, regs, insn->dst_reg); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7670 | dst_reg->map_ptr = map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7671 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7672 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7673 | dst_reg->type = PTR_TO_MAP_VALUE; |
| 7674 | dst_reg->off = aux->map_off; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7675 | if (map_value_has_spin_lock(map)) |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7676 | dst_reg->id = ++env->id_gen; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7677 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 7678 | dst_reg->type = CONST_PTR_TO_MAP; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7679 | } else { |
| 7680 | verbose(env, "bpf verifier is misconfigured\n"); |
| 7681 | return -EINVAL; |
| 7682 | } |
| 7683 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7684 | return 0; |
| 7685 | } |
| 7686 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 7687 | static bool may_access_skb(enum bpf_prog_type type) |
| 7688 | { |
| 7689 | switch (type) { |
| 7690 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 7691 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 7692 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 7693 | return true; |
| 7694 | default: |
| 7695 | return false; |
| 7696 | } |
| 7697 | } |
| 7698 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7699 | /* verify safety of LD_ABS|LD_IND instructions: |
| 7700 | * - they can only appear in the programs where ctx == skb |
| 7701 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 7702 | * preserve R6-R9, and store return value into R0 |
| 7703 | * |
| 7704 | * Implicit input: |
| 7705 | * ctx == skb == R6 == CTX |
| 7706 | * |
| 7707 | * Explicit input: |
| 7708 | * SRC == any register |
| 7709 | * IMM == 32-bit immediate |
| 7710 | * |
| 7711 | * Output: |
| 7712 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 7713 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7714 | 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] | 7715 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7716 | struct bpf_reg_state *regs = cur_regs(env); |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 7717 | static const int ctx_reg = BPF_REG_6; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7718 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7719 | int i, err; |
| 7720 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 7721 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7722 | 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] | 7723 | return -EINVAL; |
| 7724 | } |
| 7725 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 7726 | if (!env->ops->gen_ld_abs) { |
| 7727 | verbose(env, "bpf verifier is misconfigured\n"); |
| 7728 | return -EINVAL; |
| 7729 | } |
| 7730 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7731 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 7732 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7733 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7734 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7735 | return -EINVAL; |
| 7736 | } |
| 7737 | |
| 7738 | /* check whether implicit source operand (register R6) is readable */ |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 7739 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7740 | if (err) |
| 7741 | return err; |
| 7742 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7743 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 7744 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 7745 | * reference leak. |
| 7746 | */ |
| 7747 | err = check_reference_leak(env); |
| 7748 | if (err) { |
| 7749 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 7750 | return err; |
| 7751 | } |
| 7752 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 7753 | if (env->cur_state->active_spin_lock) { |
| 7754 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
| 7755 | return -EINVAL; |
| 7756 | } |
| 7757 | |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 7758 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7759 | verbose(env, |
| 7760 | "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] | 7761 | return -EINVAL; |
| 7762 | } |
| 7763 | |
| 7764 | if (mode == BPF_IND) { |
| 7765 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7766 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7767 | if (err) |
| 7768 | return err; |
| 7769 | } |
| 7770 | |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 7771 | err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg); |
| 7772 | if (err < 0) |
| 7773 | return err; |
| 7774 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7775 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7776 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7777 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7778 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 7779 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7780 | |
| 7781 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7782 | * the value fetched from the packet. |
| 7783 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7784 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7785 | mark_reg_unknown(env, regs, BPF_REG_0); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 7786 | /* ld_abs load up to 32-bit skb data. */ |
| 7787 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7788 | return 0; |
| 7789 | } |
| 7790 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7791 | static int check_return_code(struct bpf_verifier_env *env) |
| 7792 | { |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 7793 | struct tnum enforce_attach_type_range = tnum_unknown; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 7794 | const struct bpf_prog *prog = env->prog; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7795 | struct bpf_reg_state *reg; |
| 7796 | struct tnum range = tnum_range(0, 1); |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 7797 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 7798 | int err; |
| 7799 | |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 7800 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 7801 | if ((prog_type == BPF_PROG_TYPE_STRUCT_OPS || |
| 7802 | prog_type == BPF_PROG_TYPE_LSM) && |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 7803 | !prog->aux->attach_func_proto->type) |
| 7804 | return 0; |
| 7805 | |
| 7806 | /* eBPF calling convetion is such that R0 is used |
| 7807 | * to return the value from eBPF program. |
| 7808 | * Make sure that it's readable at this time |
| 7809 | * of bpf_exit, which means that program wrote |
| 7810 | * something into it earlier |
| 7811 | */ |
| 7812 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
| 7813 | if (err) |
| 7814 | return err; |
| 7815 | |
| 7816 | if (is_pointer_value(env, BPF_REG_0)) { |
| 7817 | verbose(env, "R0 leaks addr as return value\n"); |
| 7818 | return -EACCES; |
| 7819 | } |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7820 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 7821 | switch (prog_type) { |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 7822 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 7823 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || |
Daniel Borkmann | 1b66d25 | 2020-05-19 00:45:45 +0200 | [diff] [blame] | 7824 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
| 7825 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || |
| 7826 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || |
| 7827 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || |
| 7828 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 7829 | range = tnum_range(1, 1); |
Gustavo A. R. Silva | ed4ed40 | 2019-07-11 11:22:33 -0500 | [diff] [blame] | 7830 | break; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7831 | case BPF_PROG_TYPE_CGROUP_SKB: |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 7832 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
| 7833 | range = tnum_range(0, 3); |
| 7834 | enforce_attach_type_range = tnum_range(2, 3); |
| 7835 | } |
Gustavo A. R. Silva | ed4ed40 | 2019-07-11 11:22:33 -0500 | [diff] [blame] | 7836 | break; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7837 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 7838 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 7839 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 7840 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 7841 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7842 | break; |
Alexei Starovoitov | 15ab09b | 2019-10-28 20:24:26 -0700 | [diff] [blame] | 7843 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 7844 | if (!env->prog->aux->attach_btf_id) |
| 7845 | return 0; |
| 7846 | range = tnum_const(0); |
| 7847 | break; |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 7848 | case BPF_PROG_TYPE_TRACING: |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 7849 | switch (env->prog->expected_attach_type) { |
| 7850 | case BPF_TRACE_FENTRY: |
| 7851 | case BPF_TRACE_FEXIT: |
| 7852 | range = tnum_const(0); |
| 7853 | break; |
| 7854 | case BPF_TRACE_RAW_TP: |
| 7855 | case BPF_MODIFY_RETURN: |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 7856 | return 0; |
Daniel Borkmann | 2ec0616 | 2020-05-16 00:39:18 +0200 | [diff] [blame] | 7857 | case BPF_TRACE_ITER: |
| 7858 | break; |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 7859 | default: |
| 7860 | return -ENOTSUPP; |
| 7861 | } |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 7862 | break; |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 7863 | case BPF_PROG_TYPE_SK_LOOKUP: |
| 7864 | range = tnum_range(SK_DROP, SK_PASS); |
| 7865 | break; |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 7866 | case BPF_PROG_TYPE_EXT: |
| 7867 | /* freplace program can return anything as its return value |
| 7868 | * depends on the to-be-replaced kernel func or bpf program. |
| 7869 | */ |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7870 | default: |
| 7871 | return 0; |
| 7872 | } |
| 7873 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7874 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7875 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7876 | 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] | 7877 | reg_type_str[reg->type]); |
| 7878 | return -EINVAL; |
| 7879 | } |
| 7880 | |
| 7881 | if (!tnum_in(range, reg->var_off)) { |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 7882 | char tn_buf[48]; |
| 7883 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7884 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7885 | if (!tnum_is_unknown(reg->var_off)) { |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7886 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7887 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7888 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7889 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7890 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 7891 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 7892 | verbose(env, " should have been in %s\n", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7893 | return -EINVAL; |
| 7894 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 7895 | |
| 7896 | if (!tnum_is_unknown(enforce_attach_type_range) && |
| 7897 | tnum_in(enforce_attach_type_range, reg->var_off)) |
| 7898 | env->prog->enforce_expected_attach_type = 1; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7899 | return 0; |
| 7900 | } |
| 7901 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7902 | /* non-recursive DFS pseudo code |
| 7903 | * 1 procedure DFS-iterative(G,v): |
| 7904 | * 2 label v as discovered |
| 7905 | * 3 let S be a stack |
| 7906 | * 4 S.push(v) |
| 7907 | * 5 while S is not empty |
| 7908 | * 6 t <- S.pop() |
| 7909 | * 7 if t is what we're looking for: |
| 7910 | * 8 return t |
| 7911 | * 9 for all edges e in G.adjacentEdges(t) do |
| 7912 | * 10 if edge e is already labelled |
| 7913 | * 11 continue with the next edge |
| 7914 | * 12 w <- G.adjacentVertex(t,e) |
| 7915 | * 13 if vertex w is not discovered and not explored |
| 7916 | * 14 label e as tree-edge |
| 7917 | * 15 label w as discovered |
| 7918 | * 16 S.push(w) |
| 7919 | * 17 continue at 5 |
| 7920 | * 18 else if vertex w is discovered |
| 7921 | * 19 label e as back-edge |
| 7922 | * 20 else |
| 7923 | * 21 // vertex w is explored |
| 7924 | * 22 label e as forward- or cross-edge |
| 7925 | * 23 label t as explored |
| 7926 | * 24 S.pop() |
| 7927 | * |
| 7928 | * convention: |
| 7929 | * 0x10 - discovered |
| 7930 | * 0x11 - discovered and fall-through edge labelled |
| 7931 | * 0x12 - discovered and fall-through and branch edges labelled |
| 7932 | * 0x20 - explored |
| 7933 | */ |
| 7934 | |
| 7935 | enum { |
| 7936 | DISCOVERED = 0x10, |
| 7937 | EXPLORED = 0x20, |
| 7938 | FALLTHROUGH = 1, |
| 7939 | BRANCH = 2, |
| 7940 | }; |
| 7941 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 7942 | static u32 state_htab_size(struct bpf_verifier_env *env) |
| 7943 | { |
| 7944 | return env->prog->len; |
| 7945 | } |
| 7946 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7947 | static struct bpf_verifier_state_list **explored_state( |
| 7948 | struct bpf_verifier_env *env, |
| 7949 | int idx) |
| 7950 | { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 7951 | struct bpf_verifier_state *cur = env->cur_state; |
| 7952 | struct bpf_func_state *state = cur->frame[cur->curframe]; |
| 7953 | |
| 7954 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7955 | } |
| 7956 | |
| 7957 | static void init_explored_state(struct bpf_verifier_env *env, int idx) |
| 7958 | { |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 7959 | env->insn_aux_data[idx].prune_point = true; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7960 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7961 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7962 | /* t, w, e - match pseudo-code above: |
| 7963 | * t - index of current instruction |
| 7964 | * w - next instruction |
| 7965 | * e - edge |
| 7966 | */ |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7967 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
| 7968 | bool loop_ok) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7969 | { |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7970 | int *insn_stack = env->cfg.insn_stack; |
| 7971 | int *insn_state = env->cfg.insn_state; |
| 7972 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7973 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 7974 | return 0; |
| 7975 | |
| 7976 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 7977 | return 0; |
| 7978 | |
| 7979 | if (w < 0 || w >= env->prog->len) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 7980 | verbose_linfo(env, t, "%d: ", t); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7981 | 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] | 7982 | return -EINVAL; |
| 7983 | } |
| 7984 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7985 | if (e == BRANCH) |
| 7986 | /* mark branch target for state pruning */ |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7987 | init_explored_state(env, w); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7988 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7989 | if (insn_state[w] == 0) { |
| 7990 | /* tree-edge */ |
| 7991 | insn_state[t] = DISCOVERED | e; |
| 7992 | insn_state[w] = DISCOVERED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7993 | if (env->cfg.cur_stack >= env->prog->len) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7994 | return -E2BIG; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7995 | insn_stack[env->cfg.cur_stack++] = w; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7996 | return 1; |
| 7997 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 7998 | if (loop_ok && env->bpf_capable) |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7999 | return 0; |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 8000 | verbose_linfo(env, t, "%d: ", t); |
| 8001 | verbose_linfo(env, w, "%d: ", w); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8002 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8003 | return -EINVAL; |
| 8004 | } else if (insn_state[w] == EXPLORED) { |
| 8005 | /* forward- or cross-edge */ |
| 8006 | insn_state[t] = DISCOVERED | e; |
| 8007 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8008 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8009 | return -EFAULT; |
| 8010 | } |
| 8011 | return 0; |
| 8012 | } |
| 8013 | |
| 8014 | /* non-recursive depth-first-search to detect loops in BPF program |
| 8015 | * loop == back-edge in directed graph |
| 8016 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8017 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8018 | { |
| 8019 | struct bpf_insn *insns = env->prog->insnsi; |
| 8020 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8021 | int *insn_stack, *insn_state; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8022 | int ret = 0; |
| 8023 | int i, t; |
| 8024 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8025 | 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] | 8026 | if (!insn_state) |
| 8027 | return -ENOMEM; |
| 8028 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8029 | 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] | 8030 | if (!insn_stack) { |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 8031 | kvfree(insn_state); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8032 | return -ENOMEM; |
| 8033 | } |
| 8034 | |
| 8035 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 8036 | insn_stack[0] = 0; /* 0 is the first instruction */ |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8037 | env->cfg.cur_stack = 1; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8038 | |
| 8039 | peek_stack: |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8040 | if (env->cfg.cur_stack == 0) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8041 | goto check_state; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8042 | t = insn_stack[env->cfg.cur_stack - 1]; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8043 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 8044 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
| 8045 | BPF_CLASS(insns[t].code) == BPF_JMP32) { |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8046 | u8 opcode = BPF_OP(insns[t].code); |
| 8047 | |
| 8048 | if (opcode == BPF_EXIT) { |
| 8049 | goto mark_explored; |
| 8050 | } else if (opcode == BPF_CALL) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8051 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8052 | if (ret == 1) |
| 8053 | goto peek_stack; |
| 8054 | else if (ret < 0) |
| 8055 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 8056 | if (t + 1 < insn_cnt) |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 8057 | init_explored_state(env, t + 1); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 8058 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 8059 | init_explored_state(env, t); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8060 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, |
| 8061 | env, false); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 8062 | if (ret == 1) |
| 8063 | goto peek_stack; |
| 8064 | else if (ret < 0) |
| 8065 | goto err_free; |
| 8066 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8067 | } else if (opcode == BPF_JA) { |
| 8068 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 8069 | ret = -EINVAL; |
| 8070 | goto err_free; |
| 8071 | } |
| 8072 | /* unconditional jump with single edge */ |
| 8073 | ret = push_insn(t, t + insns[t].off + 1, |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8074 | FALLTHROUGH, env, true); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8075 | if (ret == 1) |
| 8076 | goto peek_stack; |
| 8077 | else if (ret < 0) |
| 8078 | goto err_free; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8079 | /* unconditional jmp is not a good pruning point, |
| 8080 | * but it's marked, since backtracking needs |
| 8081 | * to record jmp history in is_state_visited(). |
| 8082 | */ |
| 8083 | init_explored_state(env, t + insns[t].off + 1); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8084 | /* tell verifier to check for equivalent states |
| 8085 | * after every call and jump |
| 8086 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 8087 | if (t + 1 < insn_cnt) |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 8088 | init_explored_state(env, t + 1); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8089 | } else { |
| 8090 | /* conditional jump with two edges */ |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 8091 | init_explored_state(env, t); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8092 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8093 | if (ret == 1) |
| 8094 | goto peek_stack; |
| 8095 | else if (ret < 0) |
| 8096 | goto err_free; |
| 8097 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8098 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8099 | if (ret == 1) |
| 8100 | goto peek_stack; |
| 8101 | else if (ret < 0) |
| 8102 | goto err_free; |
| 8103 | } |
| 8104 | } else { |
| 8105 | /* all other non-branch instructions with single |
| 8106 | * fall-through edge |
| 8107 | */ |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8108 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8109 | if (ret == 1) |
| 8110 | goto peek_stack; |
| 8111 | else if (ret < 0) |
| 8112 | goto err_free; |
| 8113 | } |
| 8114 | |
| 8115 | mark_explored: |
| 8116 | insn_state[t] = EXPLORED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8117 | if (env->cfg.cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8118 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8119 | ret = -EFAULT; |
| 8120 | goto err_free; |
| 8121 | } |
| 8122 | goto peek_stack; |
| 8123 | |
| 8124 | check_state: |
| 8125 | for (i = 0; i < insn_cnt; i++) { |
| 8126 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8127 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8128 | ret = -EINVAL; |
| 8129 | goto err_free; |
| 8130 | } |
| 8131 | } |
| 8132 | ret = 0; /* cfg looks good */ |
| 8133 | |
| 8134 | err_free: |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 8135 | kvfree(insn_state); |
| 8136 | kvfree(insn_stack); |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 8137 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8138 | return ret; |
| 8139 | } |
| 8140 | |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8141 | static int check_abnormal_return(struct bpf_verifier_env *env) |
| 8142 | { |
| 8143 | int i; |
| 8144 | |
| 8145 | for (i = 1; i < env->subprog_cnt; i++) { |
| 8146 | if (env->subprog_info[i].has_ld_abs) { |
| 8147 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); |
| 8148 | return -EINVAL; |
| 8149 | } |
| 8150 | if (env->subprog_info[i].has_tail_call) { |
| 8151 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); |
| 8152 | return -EINVAL; |
| 8153 | } |
| 8154 | } |
| 8155 | return 0; |
| 8156 | } |
| 8157 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8158 | /* The minimum supported BTF func info size */ |
| 8159 | #define MIN_BPF_FUNCINFO_SIZE 8 |
| 8160 | #define MAX_FUNCINFO_REC_SIZE 252 |
| 8161 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8162 | static int check_btf_func(struct bpf_verifier_env *env, |
| 8163 | const union bpf_attr *attr, |
| 8164 | union bpf_attr __user *uattr) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8165 | { |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8166 | const struct btf_type *type, *func_proto, *ret_type; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 8167 | u32 i, nfuncs, urec_size, min_size; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8168 | u32 krec_size = sizeof(struct bpf_func_info); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8169 | struct bpf_func_info *krecord; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8170 | struct bpf_func_info_aux *info_aux = NULL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8171 | struct bpf_prog *prog; |
| 8172 | const struct btf *btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8173 | void __user *urecord; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 8174 | u32 prev_offset = 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8175 | bool scalar_return; |
Dan Carpenter | e7ed83d | 2020-06-04 11:54:36 +0300 | [diff] [blame] | 8176 | int ret = -ENOMEM; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8177 | |
| 8178 | nfuncs = attr->func_info_cnt; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8179 | if (!nfuncs) { |
| 8180 | if (check_abnormal_return(env)) |
| 8181 | return -EINVAL; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8182 | return 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8183 | } |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8184 | |
| 8185 | if (nfuncs != env->subprog_cnt) { |
| 8186 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); |
| 8187 | return -EINVAL; |
| 8188 | } |
| 8189 | |
| 8190 | urec_size = attr->func_info_rec_size; |
| 8191 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || |
| 8192 | urec_size > MAX_FUNCINFO_REC_SIZE || |
| 8193 | urec_size % sizeof(u32)) { |
| 8194 | verbose(env, "invalid func info rec size %u\n", urec_size); |
| 8195 | return -EINVAL; |
| 8196 | } |
| 8197 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8198 | prog = env->prog; |
| 8199 | btf = prog->aux->btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8200 | |
| 8201 | urecord = u64_to_user_ptr(attr->func_info); |
| 8202 | min_size = min_t(u32, krec_size, urec_size); |
| 8203 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8204 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8205 | if (!krecord) |
| 8206 | return -ENOMEM; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8207 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
| 8208 | if (!info_aux) |
| 8209 | goto err_free; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8210 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8211 | for (i = 0; i < nfuncs; i++) { |
| 8212 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); |
| 8213 | if (ret) { |
| 8214 | if (ret == -E2BIG) { |
| 8215 | verbose(env, "nonzero tailing record in func info"); |
| 8216 | /* set the size kernel expects so loader can zero |
| 8217 | * out the rest of the record. |
| 8218 | */ |
| 8219 | if (put_user(min_size, &uattr->func_info_rec_size)) |
| 8220 | ret = -EFAULT; |
| 8221 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8222 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8223 | } |
| 8224 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8225 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8226 | ret = -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8227 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8228 | } |
| 8229 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8230 | /* check insn_off */ |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8231 | ret = -EINVAL; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8232 | if (i == 0) { |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8233 | if (krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8234 | verbose(env, |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8235 | "nonzero insn_off %u for the first func info record", |
| 8236 | krecord[i].insn_off); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8237 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8238 | } |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8239 | } else if (krecord[i].insn_off <= prev_offset) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8240 | verbose(env, |
| 8241 | "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] | 8242 | krecord[i].insn_off, prev_offset); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8243 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8244 | } |
| 8245 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8246 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8247 | 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] | 8248 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8249 | } |
| 8250 | |
| 8251 | /* check type_id */ |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8252 | type = btf_type_by_id(btf, krecord[i].type_id); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 8253 | if (!type || !btf_type_is_func(type)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8254 | verbose(env, "invalid type id %d in func info", |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8255 | krecord[i].type_id); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8256 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8257 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 8258 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8259 | |
| 8260 | func_proto = btf_type_by_id(btf, type->type); |
| 8261 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) |
| 8262 | /* btf_func_check() already verified it during BTF load */ |
| 8263 | goto err_free; |
| 8264 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); |
| 8265 | scalar_return = |
| 8266 | btf_type_is_small_int(ret_type) || btf_type_is_enum(ret_type); |
| 8267 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { |
| 8268 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); |
| 8269 | goto err_free; |
| 8270 | } |
| 8271 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { |
| 8272 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); |
| 8273 | goto err_free; |
| 8274 | } |
| 8275 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8276 | prev_offset = krecord[i].insn_off; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8277 | urecord += urec_size; |
| 8278 | } |
| 8279 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8280 | prog->aux->func_info = krecord; |
| 8281 | prog->aux->func_info_cnt = nfuncs; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8282 | prog->aux->func_info_aux = info_aux; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8283 | return 0; |
| 8284 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8285 | err_free: |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8286 | kvfree(krecord); |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8287 | kfree(info_aux); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8288 | return ret; |
| 8289 | } |
| 8290 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8291 | static void adjust_btf_func(struct bpf_verifier_env *env) |
| 8292 | { |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8293 | struct bpf_prog_aux *aux = env->prog->aux; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8294 | int i; |
| 8295 | |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8296 | if (!aux->func_info) |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8297 | return; |
| 8298 | |
| 8299 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8300 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8301 | } |
| 8302 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8303 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
| 8304 | sizeof(((struct bpf_line_info *)(0))->line_col)) |
| 8305 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
| 8306 | |
| 8307 | static int check_btf_line(struct bpf_verifier_env *env, |
| 8308 | const union bpf_attr *attr, |
| 8309 | union bpf_attr __user *uattr) |
| 8310 | { |
| 8311 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; |
| 8312 | struct bpf_subprog_info *sub; |
| 8313 | struct bpf_line_info *linfo; |
| 8314 | struct bpf_prog *prog; |
| 8315 | const struct btf *btf; |
| 8316 | void __user *ulinfo; |
| 8317 | int err; |
| 8318 | |
| 8319 | nr_linfo = attr->line_info_cnt; |
| 8320 | if (!nr_linfo) |
| 8321 | return 0; |
| 8322 | |
| 8323 | rec_size = attr->line_info_rec_size; |
| 8324 | if (rec_size < MIN_BPF_LINEINFO_SIZE || |
| 8325 | rec_size > MAX_LINEINFO_REC_SIZE || |
| 8326 | rec_size & (sizeof(u32) - 1)) |
| 8327 | return -EINVAL; |
| 8328 | |
| 8329 | /* Need to zero it in case the userspace may |
| 8330 | * pass in a smaller bpf_line_info object. |
| 8331 | */ |
| 8332 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), |
| 8333 | GFP_KERNEL | __GFP_NOWARN); |
| 8334 | if (!linfo) |
| 8335 | return -ENOMEM; |
| 8336 | |
| 8337 | prog = env->prog; |
| 8338 | btf = prog->aux->btf; |
| 8339 | |
| 8340 | s = 0; |
| 8341 | sub = env->subprog_info; |
| 8342 | ulinfo = u64_to_user_ptr(attr->line_info); |
| 8343 | expected_size = sizeof(struct bpf_line_info); |
| 8344 | ncopy = min_t(u32, expected_size, rec_size); |
| 8345 | for (i = 0; i < nr_linfo; i++) { |
| 8346 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); |
| 8347 | if (err) { |
| 8348 | if (err == -E2BIG) { |
| 8349 | verbose(env, "nonzero tailing record in line_info"); |
| 8350 | if (put_user(expected_size, |
| 8351 | &uattr->line_info_rec_size)) |
| 8352 | err = -EFAULT; |
| 8353 | } |
| 8354 | goto err_free; |
| 8355 | } |
| 8356 | |
| 8357 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { |
| 8358 | err = -EFAULT; |
| 8359 | goto err_free; |
| 8360 | } |
| 8361 | |
| 8362 | /* |
| 8363 | * Check insn_off to ensure |
| 8364 | * 1) strictly increasing AND |
| 8365 | * 2) bounded by prog->len |
| 8366 | * |
| 8367 | * The linfo[0].insn_off == 0 check logically falls into |
| 8368 | * the later "missing bpf_line_info for func..." case |
| 8369 | * because the first linfo[0].insn_off must be the |
| 8370 | * first sub also and the first sub must have |
| 8371 | * subprog_info[0].start == 0. |
| 8372 | */ |
| 8373 | if ((i && linfo[i].insn_off <= prev_offset) || |
| 8374 | linfo[i].insn_off >= prog->len) { |
| 8375 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", |
| 8376 | i, linfo[i].insn_off, prev_offset, |
| 8377 | prog->len); |
| 8378 | err = -EINVAL; |
| 8379 | goto err_free; |
| 8380 | } |
| 8381 | |
Martin KaFai Lau | fdbaa0b | 2018-12-19 13:01:01 -0800 | [diff] [blame] | 8382 | if (!prog->insnsi[linfo[i].insn_off].code) { |
| 8383 | verbose(env, |
| 8384 | "Invalid insn code at line_info[%u].insn_off\n", |
| 8385 | i); |
| 8386 | err = -EINVAL; |
| 8387 | goto err_free; |
| 8388 | } |
| 8389 | |
Martin KaFai Lau | 23127b3 | 2018-12-13 10:41:46 -0800 | [diff] [blame] | 8390 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
| 8391 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8392 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
| 8393 | err = -EINVAL; |
| 8394 | goto err_free; |
| 8395 | } |
| 8396 | |
| 8397 | if (s != env->subprog_cnt) { |
| 8398 | if (linfo[i].insn_off == sub[s].start) { |
| 8399 | sub[s].linfo_idx = i; |
| 8400 | s++; |
| 8401 | } else if (sub[s].start < linfo[i].insn_off) { |
| 8402 | verbose(env, "missing bpf_line_info for func#%u\n", s); |
| 8403 | err = -EINVAL; |
| 8404 | goto err_free; |
| 8405 | } |
| 8406 | } |
| 8407 | |
| 8408 | prev_offset = linfo[i].insn_off; |
| 8409 | ulinfo += rec_size; |
| 8410 | } |
| 8411 | |
| 8412 | if (s != env->subprog_cnt) { |
| 8413 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", |
| 8414 | env->subprog_cnt - s, s); |
| 8415 | err = -EINVAL; |
| 8416 | goto err_free; |
| 8417 | } |
| 8418 | |
| 8419 | prog->aux->linfo = linfo; |
| 8420 | prog->aux->nr_linfo = nr_linfo; |
| 8421 | |
| 8422 | return 0; |
| 8423 | |
| 8424 | err_free: |
| 8425 | kvfree(linfo); |
| 8426 | return err; |
| 8427 | } |
| 8428 | |
| 8429 | static int check_btf_info(struct bpf_verifier_env *env, |
| 8430 | const union bpf_attr *attr, |
| 8431 | union bpf_attr __user *uattr) |
| 8432 | { |
| 8433 | struct btf *btf; |
| 8434 | int err; |
| 8435 | |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8436 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
| 8437 | if (check_abnormal_return(env)) |
| 8438 | return -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8439 | return 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8440 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8441 | |
| 8442 | btf = btf_get_by_fd(attr->prog_btf_fd); |
| 8443 | if (IS_ERR(btf)) |
| 8444 | return PTR_ERR(btf); |
| 8445 | env->prog->aux->btf = btf; |
| 8446 | |
| 8447 | err = check_btf_func(env, attr, uattr); |
| 8448 | if (err) |
| 8449 | return err; |
| 8450 | |
| 8451 | err = check_btf_line(env, attr, uattr); |
| 8452 | if (err) |
| 8453 | return err; |
| 8454 | |
| 8455 | return 0; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8456 | } |
| 8457 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8458 | /* check %cur's range satisfies %old's */ |
| 8459 | static bool range_within(struct bpf_reg_state *old, |
| 8460 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8461 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8462 | return old->umin_value <= cur->umin_value && |
| 8463 | old->umax_value >= cur->umax_value && |
| 8464 | old->smin_value <= cur->smin_value && |
| 8465 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8466 | } |
| 8467 | |
| 8468 | /* Maximum number of register states that can exist at once */ |
| 8469 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 8470 | struct idpair { |
| 8471 | u32 old; |
| 8472 | u32 cur; |
| 8473 | }; |
| 8474 | |
| 8475 | /* If in the old state two registers had the same id, then they need to have |
| 8476 | * the same id in the new state as well. But that id could be different from |
| 8477 | * the old state, so we need to track the mapping from old to new ids. |
| 8478 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 8479 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 8480 | * regs with a different old id could still have new id 9, we don't care about |
| 8481 | * that. |
| 8482 | * So we look through our idmap to see if this old id has been seen before. If |
| 8483 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 8484 | */ |
| 8485 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 8486 | { |
| 8487 | unsigned int i; |
| 8488 | |
| 8489 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 8490 | if (!idmap[i].old) { |
| 8491 | /* Reached an empty slot; haven't seen this id before */ |
| 8492 | idmap[i].old = old_id; |
| 8493 | idmap[i].cur = cur_id; |
| 8494 | return true; |
| 8495 | } |
| 8496 | if (idmap[i].old == old_id) |
| 8497 | return idmap[i].cur == cur_id; |
| 8498 | } |
| 8499 | /* We ran out of idmap slots, which should be impossible */ |
| 8500 | WARN_ON_ONCE(1); |
| 8501 | return false; |
| 8502 | } |
| 8503 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 8504 | static void clean_func_state(struct bpf_verifier_env *env, |
| 8505 | struct bpf_func_state *st) |
| 8506 | { |
| 8507 | enum bpf_reg_liveness live; |
| 8508 | int i, j; |
| 8509 | |
| 8510 | for (i = 0; i < BPF_REG_FP; i++) { |
| 8511 | live = st->regs[i].live; |
| 8512 | /* liveness must not touch this register anymore */ |
| 8513 | st->regs[i].live |= REG_LIVE_DONE; |
| 8514 | if (!(live & REG_LIVE_READ)) |
| 8515 | /* since the register is unused, clear its state |
| 8516 | * to make further comparison simpler |
| 8517 | */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 8518 | __mark_reg_not_init(env, &st->regs[i]); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 8519 | } |
| 8520 | |
| 8521 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { |
| 8522 | live = st->stack[i].spilled_ptr.live; |
| 8523 | /* liveness must not touch this stack slot anymore */ |
| 8524 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; |
| 8525 | if (!(live & REG_LIVE_READ)) { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 8526 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 8527 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 8528 | st->stack[i].slot_type[j] = STACK_INVALID; |
| 8529 | } |
| 8530 | } |
| 8531 | } |
| 8532 | |
| 8533 | static void clean_verifier_state(struct bpf_verifier_env *env, |
| 8534 | struct bpf_verifier_state *st) |
| 8535 | { |
| 8536 | int i; |
| 8537 | |
| 8538 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) |
| 8539 | /* all regs in this state in all frames were already marked */ |
| 8540 | return; |
| 8541 | |
| 8542 | for (i = 0; i <= st->curframe; i++) |
| 8543 | clean_func_state(env, st->frame[i]); |
| 8544 | } |
| 8545 | |
| 8546 | /* the parentage chains form a tree. |
| 8547 | * the verifier states are added to state lists at given insn and |
| 8548 | * pushed into state stack for future exploration. |
| 8549 | * when the verifier reaches bpf_exit insn some of the verifer states |
| 8550 | * stored in the state lists have their final liveness state already, |
| 8551 | * but a lot of states will get revised from liveness point of view when |
| 8552 | * the verifier explores other branches. |
| 8553 | * Example: |
| 8554 | * 1: r0 = 1 |
| 8555 | * 2: if r1 == 100 goto pc+1 |
| 8556 | * 3: r0 = 2 |
| 8557 | * 4: exit |
| 8558 | * when the verifier reaches exit insn the register r0 in the state list of |
| 8559 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch |
| 8560 | * of insn 2 and goes exploring further. At the insn 4 it will walk the |
| 8561 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. |
| 8562 | * |
| 8563 | * Since the verifier pushes the branch states as it sees them while exploring |
| 8564 | * the program the condition of walking the branch instruction for the second |
| 8565 | * time means that all states below this branch were already explored and |
| 8566 | * their final liveness markes are already propagated. |
| 8567 | * Hence when the verifier completes the search of state list in is_state_visited() |
| 8568 | * we can call this clean_live_states() function to mark all liveness states |
| 8569 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' |
| 8570 | * will not be used. |
| 8571 | * This function also clears the registers and stack for states that !READ |
| 8572 | * to simplify state merging. |
| 8573 | * |
| 8574 | * Important note here that walking the same branch instruction in the callee |
| 8575 | * doesn't meant that the states are DONE. The verifier has to compare |
| 8576 | * the callsites |
| 8577 | */ |
| 8578 | static void clean_live_states(struct bpf_verifier_env *env, int insn, |
| 8579 | struct bpf_verifier_state *cur) |
| 8580 | { |
| 8581 | struct bpf_verifier_state_list *sl; |
| 8582 | int i; |
| 8583 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 8584 | sl = *explored_state(env, insn); |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 8585 | while (sl) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8586 | if (sl->state.branches) |
| 8587 | goto next; |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 8588 | if (sl->state.insn_idx != insn || |
| 8589 | sl->state.curframe != cur->curframe) |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 8590 | goto next; |
| 8591 | for (i = 0; i <= cur->curframe; i++) |
| 8592 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) |
| 8593 | goto next; |
| 8594 | clean_verifier_state(env, &sl->state); |
| 8595 | next: |
| 8596 | sl = sl->next; |
| 8597 | } |
| 8598 | } |
| 8599 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8600 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 8601 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 8602 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8603 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8604 | bool equal; |
| 8605 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8606 | if (!(rold->live & REG_LIVE_READ)) |
| 8607 | /* explored state didn't use this */ |
| 8608 | return true; |
| 8609 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 8610 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8611 | |
| 8612 | if (rold->type == PTR_TO_STACK) |
| 8613 | /* two stack pointers are equal only if they're pointing to |
| 8614 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 8615 | */ |
| 8616 | return equal && rold->frameno == rcur->frameno; |
| 8617 | |
| 8618 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8619 | return true; |
| 8620 | |
| 8621 | if (rold->type == NOT_INIT) |
| 8622 | /* explored state can't have used this */ |
| 8623 | return true; |
| 8624 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8625 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8626 | switch (rold->type) { |
| 8627 | case SCALAR_VALUE: |
| 8628 | if (rcur->type == SCALAR_VALUE) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8629 | if (!rold->precise && !rcur->precise) |
| 8630 | return true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8631 | /* new val must satisfy old val knowledge */ |
| 8632 | return range_within(rold, rcur) && |
| 8633 | tnum_in(rold->var_off, rcur->var_off); |
| 8634 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 8635 | /* We're trying to use a pointer in place of a scalar. |
| 8636 | * Even if the scalar was unbounded, this could lead to |
| 8637 | * pointer leaks because scalars are allowed to leak |
| 8638 | * while pointers are not. We could make this safe in |
| 8639 | * special cases if root is calling us, but it's |
| 8640 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8641 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 8642 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8643 | } |
| 8644 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 8645 | /* If the new min/max/var_off satisfy the old ones and |
| 8646 | * everything else matches, we are OK. |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 8647 | * 'id' is not compared, since it's only used for maps with |
| 8648 | * bpf_spin_lock inside map element and in such cases if |
| 8649 | * the rest of the prog is valid for one map element then |
| 8650 | * it's valid for all map elements regardless of the key |
| 8651 | * used in bpf_map_lookup() |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 8652 | */ |
| 8653 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 8654 | range_within(rold, rcur) && |
| 8655 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8656 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 8657 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 8658 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 8659 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 8660 | * checked, doing so could have affected others with the same |
| 8661 | * id, and we can't check for that because we lost the id when |
| 8662 | * we converted to a PTR_TO_MAP_VALUE. |
| 8663 | */ |
| 8664 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 8665 | return false; |
| 8666 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 8667 | return false; |
| 8668 | /* Check our ids match any regs they're supposed to */ |
| 8669 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 8670 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8671 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 8672 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8673 | return false; |
| 8674 | /* We must have at least as much range as the old ptr |
| 8675 | * did, so that any accesses which were safe before are |
| 8676 | * still safe. This is true even if old range < old off, |
| 8677 | * since someone could have accessed through (ptr - k), or |
| 8678 | * even done ptr -= k in a register, to get a safe access. |
| 8679 | */ |
| 8680 | if (rold->range > rcur->range) |
| 8681 | return false; |
| 8682 | /* If the offsets don't match, we can't trust our alignment; |
| 8683 | * nor can we be sure that we won't fall out of range. |
| 8684 | */ |
| 8685 | if (rold->off != rcur->off) |
| 8686 | return false; |
| 8687 | /* id relations must be preserved */ |
| 8688 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 8689 | return false; |
| 8690 | /* new val must satisfy old val knowledge */ |
| 8691 | return range_within(rold, rcur) && |
| 8692 | tnum_in(rold->var_off, rcur->var_off); |
| 8693 | case PTR_TO_CTX: |
| 8694 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8695 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 8696 | case PTR_TO_FLOW_KEYS: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 8697 | case PTR_TO_SOCKET: |
| 8698 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 8699 | case PTR_TO_SOCK_COMMON: |
| 8700 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 8701 | case PTR_TO_TCP_SOCK: |
| 8702 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 8703 | case PTR_TO_XDP_SOCK: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8704 | /* Only valid matches are exact, which memcmp() above |
| 8705 | * would have accepted |
| 8706 | */ |
| 8707 | default: |
| 8708 | /* Don't know what's going on, just say it's not safe */ |
| 8709 | return false; |
| 8710 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8711 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8712 | /* Shouldn't get here; if we do, say it's not safe */ |
| 8713 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8714 | return false; |
| 8715 | } |
| 8716 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8717 | static bool stacksafe(struct bpf_func_state *old, |
| 8718 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8719 | struct idpair *idmap) |
| 8720 | { |
| 8721 | int i, spi; |
| 8722 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8723 | /* walk slots of the explored stack and ignore any additional |
| 8724 | * slots in the current stack, since explored(safe) state |
| 8725 | * didn't use them |
| 8726 | */ |
| 8727 | for (i = 0; i < old->allocated_stack; i++) { |
| 8728 | spi = i / BPF_REG_SIZE; |
| 8729 | |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 8730 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
| 8731 | i += BPF_REG_SIZE - 1; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 8732 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 8733 | continue; |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 8734 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 8735 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8736 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 8737 | continue; |
Alexei Starovoitov | 19e2dbb | 2018-12-13 11:42:33 -0800 | [diff] [blame] | 8738 | |
| 8739 | /* explored stack has more populated slots than current stack |
| 8740 | * and these slots were used |
| 8741 | */ |
| 8742 | if (i >= cur->allocated_stack) |
| 8743 | return false; |
| 8744 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 8745 | /* if old state was safe with misc data in the stack |
| 8746 | * it will be safe with zero-initialized stack. |
| 8747 | * The opposite is not true |
| 8748 | */ |
| 8749 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 8750 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 8751 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8752 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 8753 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 8754 | /* Ex: old explored (safe) state has STACK_SPILL in |
Randy Dunlap | b8c1a30 | 2020-08-06 20:31:41 -0700 | [diff] [blame] | 8755 | * this stack slot, but current has STACK_MISC -> |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8756 | * this verifier states are not equivalent, |
| 8757 | * return false to continue verification of this path |
| 8758 | */ |
| 8759 | return false; |
| 8760 | if (i % BPF_REG_SIZE) |
| 8761 | continue; |
| 8762 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 8763 | continue; |
| 8764 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 8765 | &cur->stack[spi].spilled_ptr, |
| 8766 | idmap)) |
| 8767 | /* when explored and current stack slot are both storing |
| 8768 | * spilled registers, check that stored pointers types |
| 8769 | * are the same as well. |
| 8770 | * Ex: explored safe path could have stored |
| 8771 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 8772 | * but current path has stored: |
| 8773 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 8774 | * such verifier states are not equivalent. |
| 8775 | * return false to continue verification of this path |
| 8776 | */ |
| 8777 | return false; |
| 8778 | } |
| 8779 | return true; |
| 8780 | } |
| 8781 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 8782 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 8783 | { |
| 8784 | if (old->acquired_refs != cur->acquired_refs) |
| 8785 | return false; |
| 8786 | return !memcmp(old->refs, cur->refs, |
| 8787 | sizeof(*old->refs) * old->acquired_refs); |
| 8788 | } |
| 8789 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8790 | /* compare two verifier states |
| 8791 | * |
| 8792 | * all states stored in state_list are known to be valid, since |
| 8793 | * verifier reached 'bpf_exit' instruction through them |
| 8794 | * |
| 8795 | * this function is called when verifier exploring different branches of |
| 8796 | * execution popped from the state stack. If it sees an old state that has |
| 8797 | * more strict register state and more strict stack state then this execution |
| 8798 | * branch doesn't need to be explored further, since verifier already |
| 8799 | * concluded that more strict state leads to valid finish. |
| 8800 | * |
| 8801 | * Therefore two states are equivalent if register state is more conservative |
| 8802 | * and explored stack state is more conservative than the current one. |
| 8803 | * Example: |
| 8804 | * explored current |
| 8805 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 8806 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 8807 | * |
| 8808 | * In other words if current stack state (one being explored) has more |
| 8809 | * valid slots than old one that already passed validation, it means |
| 8810 | * the verifier can stop exploring and conclude that current state is valid too |
| 8811 | * |
| 8812 | * Similarly with registers. If explored state has register type as invalid |
| 8813 | * whereas register type in current state is meaningful, it means that |
| 8814 | * the current state will reach 'bpf_exit' instruction safely |
| 8815 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8816 | static bool func_states_equal(struct bpf_func_state *old, |
| 8817 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8818 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8819 | struct idpair *idmap; |
| 8820 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8821 | int i; |
| 8822 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8823 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 8824 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 8825 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 8826 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8827 | |
| 8828 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 8829 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8830 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8831 | } |
| 8832 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8833 | if (!stacksafe(old, cur, idmap)) |
| 8834 | goto out_free; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 8835 | |
| 8836 | if (!refsafe(old, cur)) |
| 8837 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8838 | ret = true; |
| 8839 | out_free: |
| 8840 | kfree(idmap); |
| 8841 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8842 | } |
| 8843 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8844 | static bool states_equal(struct bpf_verifier_env *env, |
| 8845 | struct bpf_verifier_state *old, |
| 8846 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8847 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8848 | int i; |
| 8849 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8850 | if (old->curframe != cur->curframe) |
| 8851 | return false; |
| 8852 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 8853 | /* Verification state from speculative execution simulation |
| 8854 | * must never prune a non-speculative execution one. |
| 8855 | */ |
| 8856 | if (old->speculative && !cur->speculative) |
| 8857 | return false; |
| 8858 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 8859 | if (old->active_spin_lock != cur->active_spin_lock) |
| 8860 | return false; |
| 8861 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8862 | /* for states to be equal callsites have to be the same |
| 8863 | * and all frame states need to be equivalent |
| 8864 | */ |
| 8865 | for (i = 0; i <= old->curframe; i++) { |
| 8866 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 8867 | return false; |
| 8868 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 8869 | return false; |
| 8870 | } |
| 8871 | return true; |
| 8872 | } |
| 8873 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8874 | /* Return 0 if no propagation happened. Return negative error code if error |
| 8875 | * happened. Otherwise, return the propagated bit. |
| 8876 | */ |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 8877 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
| 8878 | struct bpf_reg_state *reg, |
| 8879 | struct bpf_reg_state *parent_reg) |
| 8880 | { |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8881 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
| 8882 | u8 flag = reg->live & REG_LIVE_READ; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 8883 | int err; |
| 8884 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8885 | /* When comes here, read flags of PARENT_REG or REG could be any of |
| 8886 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need |
| 8887 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. |
| 8888 | */ |
| 8889 | if (parent_flag == REG_LIVE_READ64 || |
| 8890 | /* Or if there is no read flag from REG. */ |
| 8891 | !flag || |
| 8892 | /* Or if the read flag from REG is the same as PARENT_REG. */ |
| 8893 | parent_flag == flag) |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 8894 | return 0; |
| 8895 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8896 | err = mark_reg_read(env, reg, parent_reg, flag); |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 8897 | if (err) |
| 8898 | return err; |
| 8899 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8900 | return flag; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 8901 | } |
| 8902 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8903 | /* A write screens off any subsequent reads; but write marks come from the |
| 8904 | * straight-line code between a state and its parent. When we arrive at an |
| 8905 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 8906 | * code, so read marks in the state must propagate to the parent regardless |
| 8907 | * 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] | 8908 | * in mark_reg_read() is for. |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8909 | */ |
| 8910 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 8911 | const struct bpf_verifier_state *vstate, |
| 8912 | struct bpf_verifier_state *vparent) |
| 8913 | { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 8914 | struct bpf_reg_state *state_reg, *parent_reg; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8915 | struct bpf_func_state *state, *parent; |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 8916 | int i, frame, err = 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8917 | |
| 8918 | if (vparent->curframe != vstate->curframe) { |
| 8919 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 8920 | vparent->curframe, vstate->curframe); |
| 8921 | return -EFAULT; |
| 8922 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8923 | /* Propagate read liveness of registers... */ |
| 8924 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 8925 | for (frame = 0; frame <= vstate->curframe; frame++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 8926 | parent = vparent->frame[frame]; |
| 8927 | state = vstate->frame[frame]; |
| 8928 | parent_reg = parent->regs; |
| 8929 | state_reg = state->regs; |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 8930 | /* We don't need to worry about FP liveness, it's read-only */ |
| 8931 | 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] | 8932 | err = propagate_liveness_reg(env, &state_reg[i], |
| 8933 | &parent_reg[i]); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8934 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 8935 | return err; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8936 | if (err == REG_LIVE_READ64) |
| 8937 | mark_insn_zext(env, &parent_reg[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8938 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8939 | |
Jiong Wang | 1b04aee | 2019-04-12 22:59:34 +0100 | [diff] [blame] | 8940 | /* Propagate stack slots. */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8941 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 8942 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 8943 | parent_reg = &parent->stack[i].spilled_ptr; |
| 8944 | state_reg = &state->stack[i].spilled_ptr; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 8945 | err = propagate_liveness_reg(env, state_reg, |
| 8946 | parent_reg); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8947 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 8948 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8949 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8950 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8951 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8952 | } |
| 8953 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 8954 | /* find precise scalars in the previous equivalent state and |
| 8955 | * propagate them into the current state |
| 8956 | */ |
| 8957 | static int propagate_precision(struct bpf_verifier_env *env, |
| 8958 | const struct bpf_verifier_state *old) |
| 8959 | { |
| 8960 | struct bpf_reg_state *state_reg; |
| 8961 | struct bpf_func_state *state; |
| 8962 | int i, err = 0; |
| 8963 | |
| 8964 | state = old->frame[old->curframe]; |
| 8965 | state_reg = state->regs; |
| 8966 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { |
| 8967 | if (state_reg->type != SCALAR_VALUE || |
| 8968 | !state_reg->precise) |
| 8969 | continue; |
| 8970 | if (env->log.level & BPF_LOG_LEVEL2) |
| 8971 | verbose(env, "propagating r%d\n", i); |
| 8972 | err = mark_chain_precision(env, i); |
| 8973 | if (err < 0) |
| 8974 | return err; |
| 8975 | } |
| 8976 | |
| 8977 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 8978 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 8979 | continue; |
| 8980 | state_reg = &state->stack[i].spilled_ptr; |
| 8981 | if (state_reg->type != SCALAR_VALUE || |
| 8982 | !state_reg->precise) |
| 8983 | continue; |
| 8984 | if (env->log.level & BPF_LOG_LEVEL2) |
| 8985 | verbose(env, "propagating fp%d\n", |
| 8986 | (-i - 1) * BPF_REG_SIZE); |
| 8987 | err = mark_chain_precision_stack(env, i); |
| 8988 | if (err < 0) |
| 8989 | return err; |
| 8990 | } |
| 8991 | return 0; |
| 8992 | } |
| 8993 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8994 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
| 8995 | struct bpf_verifier_state *cur) |
| 8996 | { |
| 8997 | struct bpf_func_state *fold, *fcur; |
| 8998 | int i, fr = cur->curframe; |
| 8999 | |
| 9000 | if (old->curframe != fr) |
| 9001 | return false; |
| 9002 | |
| 9003 | fold = old->frame[fr]; |
| 9004 | fcur = cur->frame[fr]; |
| 9005 | for (i = 0; i < MAX_BPF_REG; i++) |
| 9006 | if (memcmp(&fold->regs[i], &fcur->regs[i], |
| 9007 | offsetof(struct bpf_reg_state, parent))) |
| 9008 | return false; |
| 9009 | return true; |
| 9010 | } |
| 9011 | |
| 9012 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9013 | 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] | 9014 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9015 | struct bpf_verifier_state_list *new_sl; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 9016 | struct bpf_verifier_state_list *sl, **pprev; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9017 | struct bpf_verifier_state *cur = env->cur_state, *new; |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 9018 | int i, j, err, states_cnt = 0; |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 9019 | bool add_new_state = env->test_state_freq ? true : false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9020 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9021 | cur->last_insn_idx = env->prev_insn_idx; |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 9022 | if (!env->insn_aux_data[insn_idx].prune_point) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9023 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 9024 | * be doing state search here |
| 9025 | */ |
| 9026 | return 0; |
| 9027 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9028 | /* bpf progs typically have pruning point every 4 instructions |
| 9029 | * http://vger.kernel.org/bpfconf2019.html#session-1 |
| 9030 | * Do not add new state for future pruning if the verifier hasn't seen |
| 9031 | * at least 2 jumps and at least 8 instructions. |
| 9032 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. |
| 9033 | * In tests that amounts to up to 50% reduction into total verifier |
| 9034 | * memory consumption and 20% verifier time speedup. |
| 9035 | */ |
| 9036 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && |
| 9037 | env->insn_processed - env->prev_insn_processed >= 8) |
| 9038 | add_new_state = true; |
| 9039 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 9040 | pprev = explored_state(env, insn_idx); |
| 9041 | sl = *pprev; |
| 9042 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 9043 | clean_live_states(env, insn_idx, cur); |
| 9044 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 9045 | while (sl) { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 9046 | states_cnt++; |
| 9047 | if (sl->state.insn_idx != insn_idx) |
| 9048 | goto next; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9049 | if (sl->state.branches) { |
| 9050 | if (states_maybe_looping(&sl->state, cur) && |
| 9051 | states_equal(env, &sl->state, cur)) { |
| 9052 | verbose_linfo(env, insn_idx, "; "); |
| 9053 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); |
| 9054 | return -EINVAL; |
| 9055 | } |
| 9056 | /* if the verifier is processing a loop, avoid adding new state |
| 9057 | * too often, since different loop iterations have distinct |
| 9058 | * states and may not help future pruning. |
| 9059 | * This threshold shouldn't be too low to make sure that |
| 9060 | * a loop with large bound will be rejected quickly. |
| 9061 | * The most abusive loop will be: |
| 9062 | * r1 += 1 |
| 9063 | * if r1 < 1000000 goto pc-2 |
| 9064 | * 1M insn_procssed limit / 100 == 10k peak states. |
| 9065 | * This threshold shouldn't be too high either, since states |
| 9066 | * at the end of the loop are likely to be useful in pruning. |
| 9067 | */ |
| 9068 | if (env->jmps_processed - env->prev_jmps_processed < 20 && |
| 9069 | env->insn_processed - env->prev_insn_processed < 100) |
| 9070 | add_new_state = false; |
| 9071 | goto miss; |
| 9072 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9073 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 9074 | sl->hit_cnt++; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9075 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9076 | * prune the search. |
| 9077 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 9078 | * If we have any write marks in env->cur_state, they |
| 9079 | * will prevent corresponding reads in the continuation |
| 9080 | * from reaching our parent (an explored_state). Our |
| 9081 | * own state will get the read marks recorded, but |
| 9082 | * they'll be immediately forgotten as we're pruning |
| 9083 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9084 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9085 | err = propagate_liveness(env, &sl->state, cur); |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 9086 | |
| 9087 | /* if previous state reached the exit with precision and |
| 9088 | * current state is equivalent to it (except precsion marks) |
| 9089 | * the precision needs to be propagated back in |
| 9090 | * the current state. |
| 9091 | */ |
| 9092 | err = err ? : push_jmp_history(env, cur); |
| 9093 | err = err ? : propagate_precision(env, &sl->state); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9094 | if (err) |
| 9095 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9096 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9097 | } |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9098 | miss: |
| 9099 | /* when new state is not going to be added do not increase miss count. |
| 9100 | * Otherwise several loop iterations will remove the state |
| 9101 | * recorded earlier. The goal of these heuristics is to have |
| 9102 | * states from some iterations of the loop (some in the beginning |
| 9103 | * and some at the end) to help pruning. |
| 9104 | */ |
| 9105 | if (add_new_state) |
| 9106 | sl->miss_cnt++; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 9107 | /* heuristic to determine whether this state is beneficial |
| 9108 | * to keep checking from state equivalence point of view. |
| 9109 | * Higher numbers increase max_states_per_insn and verification time, |
| 9110 | * but do not meaningfully decrease insn_processed. |
| 9111 | */ |
| 9112 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { |
| 9113 | /* the state is unlikely to be useful. Remove it to |
| 9114 | * speed up verification |
| 9115 | */ |
| 9116 | *pprev = sl->next; |
| 9117 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9118 | u32 br = sl->state.branches; |
| 9119 | |
| 9120 | WARN_ONCE(br, |
| 9121 | "BUG live_done but branches_to_explore %d\n", |
| 9122 | br); |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 9123 | free_verifier_state(&sl->state, false); |
| 9124 | kfree(sl); |
| 9125 | env->peak_states--; |
| 9126 | } else { |
| 9127 | /* cannot free this state, since parentage chain may |
| 9128 | * walk it later. Add it for free_list instead to |
| 9129 | * be freed at the end of verification |
| 9130 | */ |
| 9131 | sl->next = env->free_list; |
| 9132 | env->free_list = sl; |
| 9133 | } |
| 9134 | sl = *pprev; |
| 9135 | continue; |
| 9136 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 9137 | next: |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 9138 | pprev = &sl->next; |
| 9139 | sl = *pprev; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9140 | } |
| 9141 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9142 | if (env->max_states_per_insn < states_cnt) |
| 9143 | env->max_states_per_insn = states_cnt; |
| 9144 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 9145 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9146 | return push_jmp_history(env, cur); |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 9147 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9148 | if (!add_new_state) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9149 | return push_jmp_history(env, cur); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9150 | |
| 9151 | /* There were no equivalent states, remember the current one. |
| 9152 | * Technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9153 | * 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] | 9154 | * 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] | 9155 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9156 | * again on the way to bpf_exit. |
| 9157 | * When looping the sl->state.branches will be > 0 and this state |
| 9158 | * will not be considered for equivalence until branches == 0. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9159 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9160 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9161 | if (!new_sl) |
| 9162 | return -ENOMEM; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9163 | env->total_states++; |
| 9164 | env->peak_states++; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9165 | env->prev_jmps_processed = env->jmps_processed; |
| 9166 | env->prev_insn_processed = env->insn_processed; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9167 | |
| 9168 | /* add new state to the head of linked list */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9169 | new = &new_sl->state; |
| 9170 | err = copy_verifier_state(new, cur); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 9171 | if (err) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9172 | free_verifier_state(new, false); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 9173 | kfree(new_sl); |
| 9174 | return err; |
| 9175 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 9176 | new->insn_idx = insn_idx; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9177 | WARN_ONCE(new->branches != 1, |
| 9178 | "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] | 9179 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9180 | cur->parent = new; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9181 | cur->first_insn_idx = insn_idx; |
| 9182 | clear_jmp_history(cur); |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 9183 | new_sl->next = *explored_state(env, insn_idx); |
| 9184 | *explored_state(env, insn_idx) = new_sl; |
Jakub Kicinski | 7640ead | 2018-12-12 16:29:07 -0800 | [diff] [blame] | 9185 | /* connect new state to parentage chain. Current frame needs all |
| 9186 | * registers connected. Only r6 - r9 of the callers are alive (pushed |
| 9187 | * to the stack implicitly by JITs) so in callers' frames connect just |
| 9188 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to |
| 9189 | * the state of the call instruction (with WRITTEN set), and r0 comes |
| 9190 | * from callee with its full parentage chain, anyway. |
| 9191 | */ |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 9192 | /* clear write marks in current state: the writes we did are not writes |
| 9193 | * our child did, so they don't screen off its reads from us. |
| 9194 | * (There are no read marks in current state, because reads always mark |
| 9195 | * their parent and current state never has children yet. Only |
| 9196 | * explored_states can get read marks.) |
| 9197 | */ |
Alexei Starovoitov | eea1c22 | 2019-06-15 12:12:21 -0700 | [diff] [blame] | 9198 | for (j = 0; j <= cur->curframe; j++) { |
| 9199 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) |
| 9200 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; |
| 9201 | for (i = 0; i < BPF_REG_FP; i++) |
| 9202 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; |
| 9203 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9204 | |
| 9205 | /* all stack frames are accessible from callee, clear them all */ |
| 9206 | for (j = 0; j <= cur->curframe; j++) { |
| 9207 | struct bpf_func_state *frame = cur->frame[j]; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9208 | struct bpf_func_state *newframe = new->frame[j]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9209 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9210 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 9211 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9212 | frame->stack[i].spilled_ptr.parent = |
| 9213 | &newframe->stack[i].spilled_ptr; |
| 9214 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9215 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9216 | return 0; |
| 9217 | } |
| 9218 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 9219 | /* Return true if it's OK to have the same insn return a different type. */ |
| 9220 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 9221 | { |
| 9222 | switch (type) { |
| 9223 | case PTR_TO_CTX: |
| 9224 | case PTR_TO_SOCKET: |
| 9225 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 9226 | case PTR_TO_SOCK_COMMON: |
| 9227 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 9228 | case PTR_TO_TCP_SOCK: |
| 9229 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 9230 | case PTR_TO_XDP_SOCK: |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 9231 | case PTR_TO_BTF_ID: |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 9232 | case PTR_TO_BTF_ID_OR_NULL: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 9233 | return false; |
| 9234 | default: |
| 9235 | return true; |
| 9236 | } |
| 9237 | } |
| 9238 | |
| 9239 | /* If an instruction was previously used with particular pointer types, then we |
| 9240 | * need to be careful to avoid cases such as the below, where it may be ok |
| 9241 | * for one branch accessing the pointer, but not ok for the other branch: |
| 9242 | * |
| 9243 | * R1 = sock_ptr |
| 9244 | * goto X; |
| 9245 | * ... |
| 9246 | * R1 = some_other_valid_ptr; |
| 9247 | * goto X; |
| 9248 | * ... |
| 9249 | * R2 = *(u32 *)(R1 + 0); |
| 9250 | */ |
| 9251 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 9252 | { |
| 9253 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 9254 | !reg_type_mismatch_ok(prev)); |
| 9255 | } |
| 9256 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9257 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9258 | { |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 9259 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 9260 | struct bpf_verifier_state *state = env->cur_state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9261 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9262 | struct bpf_reg_state *regs; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9263 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9264 | bool do_print_state = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9265 | int prev_insn_idx = -1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9266 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9267 | for (;;) { |
| 9268 | struct bpf_insn *insn; |
| 9269 | u8 class; |
| 9270 | int err; |
| 9271 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9272 | env->prev_insn_idx = prev_insn_idx; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9273 | if (env->insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9274 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9275 | env->insn_idx, insn_cnt); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9276 | return -EFAULT; |
| 9277 | } |
| 9278 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9279 | insn = &insns[env->insn_idx]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9280 | class = BPF_CLASS(insn->code); |
| 9281 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9282 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9283 | verbose(env, |
| 9284 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9285 | env->insn_processed); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9286 | return -E2BIG; |
| 9287 | } |
| 9288 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9289 | err = is_state_visited(env, env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9290 | if (err < 0) |
| 9291 | return err; |
| 9292 | if (err == 1) { |
| 9293 | /* found equivalent state, can prune the search */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9294 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9295 | if (do_print_state) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 9296 | verbose(env, "\nfrom %d to %d%s: safe\n", |
| 9297 | env->prev_insn_idx, env->insn_idx, |
| 9298 | env->cur_state->speculative ? |
| 9299 | " (speculative execution)" : ""); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9300 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9301 | verbose(env, "%d: safe\n", env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9302 | } |
| 9303 | goto process_bpf_exit; |
| 9304 | } |
| 9305 | |
Alexei Starovoitov | c349480 | 2018-12-03 22:46:04 -0800 | [diff] [blame] | 9306 | if (signal_pending(current)) |
| 9307 | return -EAGAIN; |
| 9308 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 9309 | if (need_resched()) |
| 9310 | cond_resched(); |
| 9311 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9312 | if (env->log.level & BPF_LOG_LEVEL2 || |
| 9313 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { |
| 9314 | if (env->log.level & BPF_LOG_LEVEL2) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9315 | verbose(env, "%d:", env->insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 9316 | else |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 9317 | verbose(env, "\nfrom %d to %d%s:", |
| 9318 | env->prev_insn_idx, env->insn_idx, |
| 9319 | env->cur_state->speculative ? |
| 9320 | " (speculative execution)" : ""); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9321 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9322 | do_print_state = false; |
| 9323 | } |
| 9324 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9325 | if (env->log.level & BPF_LOG_LEVEL) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 9326 | const struct bpf_insn_cbs cbs = { |
| 9327 | .cb_print = verbose, |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 9328 | .private_data = env, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 9329 | }; |
| 9330 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9331 | verbose_linfo(env, env->insn_idx, "; "); |
| 9332 | verbose(env, "%d: ", env->insn_idx); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 9333 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9334 | } |
| 9335 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 9336 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9337 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
| 9338 | env->prev_insn_idx); |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 9339 | if (err) |
| 9340 | return err; |
| 9341 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 9342 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9343 | regs = cur_regs(env); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 9344 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9345 | prev_insn_idx = env->insn_idx; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9346 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9347 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 9348 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9349 | if (err) |
| 9350 | return err; |
| 9351 | |
| 9352 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9353 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9354 | |
| 9355 | /* check for reserved fields is already done */ |
| 9356 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9357 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9358 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9359 | if (err) |
| 9360 | return err; |
| 9361 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9362 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9363 | if (err) |
| 9364 | return err; |
| 9365 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 9366 | src_reg_type = regs[insn->src_reg].type; |
| 9367 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9368 | /* check that memory (src_reg + off) is readable, |
| 9369 | * the state of dst_reg will be updated by this func |
| 9370 | */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9371 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
| 9372 | insn->off, BPF_SIZE(insn->code), |
| 9373 | BPF_READ, insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9374 | if (err) |
| 9375 | return err; |
| 9376 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9377 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9378 | |
| 9379 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9380 | /* saw a valid insn |
| 9381 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9382 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9383 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9384 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9385 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 9386 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9387 | /* ABuser program is trying to use the same insn |
| 9388 | * dst_reg = *(u32*) (src_reg + off) |
| 9389 | * with different pointer types: |
| 9390 | * src_reg == ctx in one branch and |
| 9391 | * src_reg == stack|map in some other branch. |
| 9392 | * Reject it. |
| 9393 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9394 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9395 | return -EINVAL; |
| 9396 | } |
| 9397 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9398 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9399 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9400 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9401 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9402 | err = check_xadd(env, env->insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9403 | if (err) |
| 9404 | return err; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9405 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9406 | continue; |
| 9407 | } |
| 9408 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9409 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9410 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9411 | if (err) |
| 9412 | return err; |
| 9413 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9414 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9415 | if (err) |
| 9416 | return err; |
| 9417 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9418 | dst_reg_type = regs[insn->dst_reg].type; |
| 9419 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9420 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9421 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 9422 | insn->off, BPF_SIZE(insn->code), |
| 9423 | BPF_WRITE, insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9424 | if (err) |
| 9425 | return err; |
| 9426 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9427 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9428 | |
| 9429 | if (*prev_dst_type == NOT_INIT) { |
| 9430 | *prev_dst_type = dst_reg_type; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 9431 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9432 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9433 | return -EINVAL; |
| 9434 | } |
| 9435 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9436 | } else if (class == BPF_ST) { |
| 9437 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 9438 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9439 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9440 | return -EINVAL; |
| 9441 | } |
| 9442 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9443 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9444 | if (err) |
| 9445 | return err; |
| 9446 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 9447 | if (is_ctx_reg(env, insn->dst_reg)) { |
Joe Stringer | 9d2be44 | 2018-10-02 13:35:31 -0700 | [diff] [blame] | 9448 | 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] | 9449 | insn->dst_reg, |
| 9450 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 9451 | return -EACCES; |
| 9452 | } |
| 9453 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9454 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9455 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 9456 | insn->off, BPF_SIZE(insn->code), |
| 9457 | BPF_WRITE, -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9458 | if (err) |
| 9459 | return err; |
| 9460 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9461 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9462 | u8 opcode = BPF_OP(insn->code); |
| 9463 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9464 | env->jmps_processed++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9465 | if (opcode == BPF_CALL) { |
| 9466 | if (BPF_SRC(insn->code) != BPF_K || |
| 9467 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9468 | (insn->src_reg != BPF_REG_0 && |
| 9469 | insn->src_reg != BPF_PSEUDO_CALL) || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9470 | insn->dst_reg != BPF_REG_0 || |
| 9471 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9472 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9473 | return -EINVAL; |
| 9474 | } |
| 9475 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9476 | if (env->cur_state->active_spin_lock && |
| 9477 | (insn->src_reg == BPF_PSEUDO_CALL || |
| 9478 | insn->imm != BPF_FUNC_spin_unlock)) { |
| 9479 | verbose(env, "function calls are not allowed while holding a lock\n"); |
| 9480 | return -EINVAL; |
| 9481 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9482 | if (insn->src_reg == BPF_PSEUDO_CALL) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9483 | err = check_func_call(env, insn, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9484 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9485 | err = check_helper_call(env, insn->imm, env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9486 | if (err) |
| 9487 | return err; |
| 9488 | |
| 9489 | } else if (opcode == BPF_JA) { |
| 9490 | if (BPF_SRC(insn->code) != BPF_K || |
| 9491 | insn->imm != 0 || |
| 9492 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9493 | insn->dst_reg != BPF_REG_0 || |
| 9494 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9495 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9496 | return -EINVAL; |
| 9497 | } |
| 9498 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9499 | env->insn_idx += insn->off + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9500 | continue; |
| 9501 | |
| 9502 | } else if (opcode == BPF_EXIT) { |
| 9503 | if (BPF_SRC(insn->code) != BPF_K || |
| 9504 | insn->imm != 0 || |
| 9505 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9506 | insn->dst_reg != BPF_REG_0 || |
| 9507 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9508 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9509 | return -EINVAL; |
| 9510 | } |
| 9511 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9512 | if (env->cur_state->active_spin_lock) { |
| 9513 | verbose(env, "bpf_spin_unlock is missing\n"); |
| 9514 | return -EINVAL; |
| 9515 | } |
| 9516 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9517 | if (state->curframe) { |
| 9518 | /* exit from nested function */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9519 | err = prepare_func_exit(env, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9520 | if (err) |
| 9521 | return err; |
| 9522 | do_print_state = true; |
| 9523 | continue; |
| 9524 | } |
| 9525 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9526 | err = check_reference_leak(env); |
| 9527 | if (err) |
| 9528 | return err; |
| 9529 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9530 | err = check_return_code(env); |
| 9531 | if (err) |
| 9532 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9533 | process_bpf_exit: |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9534 | update_branch_counts(env, env->cur_state); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9535 | err = pop_stack(env, &prev_insn_idx, |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 9536 | &env->insn_idx, pop_log); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9537 | if (err < 0) { |
| 9538 | if (err != -ENOENT) |
| 9539 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9540 | break; |
| 9541 | } else { |
| 9542 | do_print_state = true; |
| 9543 | continue; |
| 9544 | } |
| 9545 | } else { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9546 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9547 | if (err) |
| 9548 | return err; |
| 9549 | } |
| 9550 | } else if (class == BPF_LD) { |
| 9551 | u8 mode = BPF_MODE(insn->code); |
| 9552 | |
| 9553 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9554 | err = check_ld_abs(env, insn); |
| 9555 | if (err) |
| 9556 | return err; |
| 9557 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9558 | } else if (mode == BPF_IMM) { |
| 9559 | err = check_ld_imm(env, insn); |
| 9560 | if (err) |
| 9561 | return err; |
| 9562 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9563 | env->insn_idx++; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 9564 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9565 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9566 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9567 | return -EINVAL; |
| 9568 | } |
| 9569 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9570 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9571 | return -EINVAL; |
| 9572 | } |
| 9573 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9574 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9575 | } |
| 9576 | |
| 9577 | return 0; |
| 9578 | } |
| 9579 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9580 | /* replace pseudo btf_id with kernel symbol address */ |
| 9581 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, |
| 9582 | struct bpf_insn *insn, |
| 9583 | struct bpf_insn_aux_data *aux) |
| 9584 | { |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 9585 | u32 datasec_id, type, id = insn->imm; |
| 9586 | const struct btf_var_secinfo *vsi; |
| 9587 | const struct btf_type *datasec; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9588 | const struct btf_type *t; |
| 9589 | const char *sym_name; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 9590 | bool percpu = false; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9591 | u64 addr; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 9592 | int i; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9593 | |
| 9594 | if (!btf_vmlinux) { |
| 9595 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); |
| 9596 | return -EINVAL; |
| 9597 | } |
| 9598 | |
| 9599 | if (insn[1].imm != 0) { |
| 9600 | verbose(env, "reserved field (insn[1].imm) is used in pseudo_btf_id ldimm64 insn.\n"); |
| 9601 | return -EINVAL; |
| 9602 | } |
| 9603 | |
| 9604 | t = btf_type_by_id(btf_vmlinux, id); |
| 9605 | if (!t) { |
| 9606 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); |
| 9607 | return -ENOENT; |
| 9608 | } |
| 9609 | |
| 9610 | if (!btf_type_is_var(t)) { |
| 9611 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", |
| 9612 | id); |
| 9613 | return -EINVAL; |
| 9614 | } |
| 9615 | |
| 9616 | sym_name = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 9617 | addr = kallsyms_lookup_name(sym_name); |
| 9618 | if (!addr) { |
| 9619 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", |
| 9620 | sym_name); |
| 9621 | return -ENOENT; |
| 9622 | } |
| 9623 | |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 9624 | datasec_id = btf_find_by_name_kind(btf_vmlinux, ".data..percpu", |
| 9625 | BTF_KIND_DATASEC); |
| 9626 | if (datasec_id > 0) { |
| 9627 | datasec = btf_type_by_id(btf_vmlinux, datasec_id); |
| 9628 | for_each_vsi(i, datasec, vsi) { |
| 9629 | if (vsi->type == id) { |
| 9630 | percpu = true; |
| 9631 | break; |
| 9632 | } |
| 9633 | } |
| 9634 | } |
| 9635 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9636 | insn[0].imm = (u32)addr; |
| 9637 | insn[1].imm = addr >> 32; |
| 9638 | |
| 9639 | type = t->type; |
| 9640 | t = btf_type_skip_modifiers(btf_vmlinux, type, NULL); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 9641 | if (percpu) { |
| 9642 | aux->btf_var.reg_type = PTR_TO_PERCPU_BTF_ID; |
| 9643 | aux->btf_var.btf_id = type; |
| 9644 | } else if (!btf_type_is_struct(t)) { |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9645 | const struct btf_type *ret; |
| 9646 | const char *tname; |
| 9647 | u32 tsize; |
| 9648 | |
| 9649 | /* resolve the type size of ksym. */ |
| 9650 | ret = btf_resolve_size(btf_vmlinux, t, &tsize); |
| 9651 | if (IS_ERR(ret)) { |
| 9652 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 9653 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
| 9654 | tname, PTR_ERR(ret)); |
| 9655 | return -EINVAL; |
| 9656 | } |
| 9657 | aux->btf_var.reg_type = PTR_TO_MEM; |
| 9658 | aux->btf_var.mem_size = tsize; |
| 9659 | } else { |
| 9660 | aux->btf_var.reg_type = PTR_TO_BTF_ID; |
| 9661 | aux->btf_var.btf_id = type; |
| 9662 | } |
| 9663 | return 0; |
| 9664 | } |
| 9665 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 9666 | static int check_map_prealloc(struct bpf_map *map) |
| 9667 | { |
| 9668 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 9669 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 9670 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 9671 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 9672 | } |
| 9673 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9674 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
| 9675 | { |
| 9676 | switch (type) { |
| 9677 | case BPF_PROG_TYPE_KPROBE: |
| 9678 | case BPF_PROG_TYPE_TRACEPOINT: |
| 9679 | case BPF_PROG_TYPE_PERF_EVENT: |
| 9680 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 9681 | return true; |
| 9682 | default: |
| 9683 | return false; |
| 9684 | } |
| 9685 | } |
| 9686 | |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 9687 | static bool is_preallocated_map(struct bpf_map *map) |
| 9688 | { |
| 9689 | if (!check_map_prealloc(map)) |
| 9690 | return false; |
| 9691 | if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) |
| 9692 | return false; |
| 9693 | return true; |
| 9694 | } |
| 9695 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9696 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 9697 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9698 | struct bpf_prog *prog) |
| 9699 | |
| 9700 | { |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9701 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 9702 | /* |
| 9703 | * Validate that trace type programs use preallocated hash maps. |
| 9704 | * |
| 9705 | * For programs attached to PERF events this is mandatory as the |
| 9706 | * perf NMI can hit any arbitrary code sequence. |
| 9707 | * |
| 9708 | * All other trace types using preallocated hash maps are unsafe as |
| 9709 | * well because tracepoint or kprobes can be inside locked regions |
| 9710 | * of the memory allocator or at a place where a recursion into the |
| 9711 | * memory allocator would see inconsistent state. |
| 9712 | * |
Thomas Gleixner | 2ed905c | 2020-02-24 15:01:33 +0100 | [diff] [blame] | 9713 | * On RT enabled kernels run-time allocation of all trace type |
| 9714 | * programs is strictly prohibited due to lock type constraints. On |
| 9715 | * !RT kernels it is allowed for backwards compatibility reasons for |
| 9716 | * now, but warnings are emitted so developers are made aware of |
| 9717 | * the unsafety and can fix their programs before this is enforced. |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 9718 | */ |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9719 | if (is_tracing_prog_type(prog_type) && !is_preallocated_map(map)) { |
| 9720 | if (prog_type == BPF_PROG_TYPE_PERF_EVENT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9721 | 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] | 9722 | return -EINVAL; |
| 9723 | } |
Thomas Gleixner | 2ed905c | 2020-02-24 15:01:33 +0100 | [diff] [blame] | 9724 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
| 9725 | verbose(env, "trace type programs can only use preallocated hash map\n"); |
| 9726 | return -EINVAL; |
| 9727 | } |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 9728 | WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); |
| 9729 | 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] | 9730 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 9731 | |
KP Singh | 9e7a4d9 | 2020-11-06 10:37:39 +0000 | [diff] [blame] | 9732 | if (map_value_has_spin_lock(map)) { |
| 9733 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { |
| 9734 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); |
| 9735 | return -EINVAL; |
| 9736 | } |
| 9737 | |
| 9738 | if (is_tracing_prog_type(prog_type)) { |
| 9739 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); |
| 9740 | return -EINVAL; |
| 9741 | } |
| 9742 | |
| 9743 | if (prog->aux->sleepable) { |
| 9744 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); |
| 9745 | return -EINVAL; |
| 9746 | } |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9747 | } |
| 9748 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 9749 | 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] | 9750 | !bpf_offload_prog_map_match(prog, map)) { |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 9751 | verbose(env, "offload device mismatch between prog and map\n"); |
| 9752 | return -EINVAL; |
| 9753 | } |
| 9754 | |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 9755 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
| 9756 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); |
| 9757 | return -EINVAL; |
| 9758 | } |
| 9759 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 9760 | if (prog->aux->sleepable) |
| 9761 | switch (map->map_type) { |
| 9762 | case BPF_MAP_TYPE_HASH: |
| 9763 | case BPF_MAP_TYPE_LRU_HASH: |
| 9764 | case BPF_MAP_TYPE_ARRAY: |
| 9765 | if (!is_preallocated_map(map)) { |
| 9766 | verbose(env, |
| 9767 | "Sleepable programs can only use preallocated hash maps\n"); |
| 9768 | return -EINVAL; |
| 9769 | } |
| 9770 | break; |
| 9771 | default: |
| 9772 | verbose(env, |
| 9773 | "Sleepable programs can only use array and hash maps\n"); |
| 9774 | return -EINVAL; |
| 9775 | } |
| 9776 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9777 | return 0; |
| 9778 | } |
| 9779 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 9780 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 9781 | { |
| 9782 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 9783 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 9784 | } |
| 9785 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9786 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
| 9787 | * |
| 9788 | * 1. if it accesses map FD, replace it with actual map pointer. |
| 9789 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. |
| 9790 | * |
| 9791 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9792 | */ |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9793 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9794 | { |
| 9795 | struct bpf_insn *insn = env->prog->insnsi; |
| 9796 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9797 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9798 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 9799 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 9800 | if (err) |
| 9801 | return err; |
| 9802 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9803 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9804 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9805 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9806 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9807 | return -EINVAL; |
| 9808 | } |
| 9809 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9810 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 9811 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 9812 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9813 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9814 | return -EINVAL; |
| 9815 | } |
| 9816 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9817 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9818 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9819 | struct bpf_map *map; |
| 9820 | struct fd f; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9821 | u64 addr; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9822 | |
| 9823 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 9824 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 9825 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9826 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9827 | return -EINVAL; |
| 9828 | } |
| 9829 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9830 | if (insn[0].src_reg == 0) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9831 | /* valid generic load 64-bit imm */ |
| 9832 | goto next_insn; |
| 9833 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9834 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
| 9835 | aux = &env->insn_aux_data[i]; |
| 9836 | err = check_pseudo_btf_id(env, insn, aux); |
| 9837 | if (err) |
| 9838 | return err; |
| 9839 | goto next_insn; |
| 9840 | } |
| 9841 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9842 | /* In final convert_pseudo_ld_imm64() step, this is |
| 9843 | * converted into regular 64-bit imm load insn. |
| 9844 | */ |
| 9845 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && |
| 9846 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || |
| 9847 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && |
| 9848 | insn[1].imm != 0)) { |
| 9849 | verbose(env, |
| 9850 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9851 | return -EINVAL; |
| 9852 | } |
| 9853 | |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 9854 | f = fdget(insn[0].imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 9855 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9856 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9857 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 9858 | insn[0].imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9859 | return PTR_ERR(map); |
| 9860 | } |
| 9861 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9862 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9863 | if (err) { |
| 9864 | fdput(f); |
| 9865 | return err; |
| 9866 | } |
| 9867 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9868 | aux = &env->insn_aux_data[i]; |
| 9869 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 9870 | addr = (unsigned long)map; |
| 9871 | } else { |
| 9872 | u32 off = insn[1].imm; |
| 9873 | |
| 9874 | if (off >= BPF_MAX_VAR_OFF) { |
| 9875 | verbose(env, "direct value offset of %u is not allowed\n", off); |
| 9876 | fdput(f); |
| 9877 | return -EINVAL; |
| 9878 | } |
| 9879 | |
| 9880 | if (!map->ops->map_direct_value_addr) { |
| 9881 | verbose(env, "no direct value access support for this map type\n"); |
| 9882 | fdput(f); |
| 9883 | return -EINVAL; |
| 9884 | } |
| 9885 | |
| 9886 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 9887 | if (err) { |
| 9888 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", |
| 9889 | map->value_size, off); |
| 9890 | fdput(f); |
| 9891 | return err; |
| 9892 | } |
| 9893 | |
| 9894 | aux->map_off = off; |
| 9895 | addr += off; |
| 9896 | } |
| 9897 | |
| 9898 | insn[0].imm = (u32)addr; |
| 9899 | insn[1].imm = addr >> 32; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9900 | |
| 9901 | /* check whether we recorded this map already */ |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9902 | for (j = 0; j < env->used_map_cnt; j++) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9903 | if (env->used_maps[j] == map) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9904 | aux->map_index = j; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9905 | fdput(f); |
| 9906 | goto next_insn; |
| 9907 | } |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9908 | } |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9909 | |
| 9910 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 9911 | fdput(f); |
| 9912 | return -E2BIG; |
| 9913 | } |
| 9914 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9915 | /* hold the map. If the program is rejected by verifier, |
| 9916 | * the map will be released by release_maps() or it |
| 9917 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 9918 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9919 | */ |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 9920 | bpf_map_inc(map); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9921 | |
| 9922 | aux->map_index = env->used_map_cnt; |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 9923 | env->used_maps[env->used_map_cnt++] = map; |
| 9924 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 9925 | if (bpf_map_is_cgroup_storage(map) && |
Daniel Borkmann | e473042 | 2019-12-17 13:28:16 +0100 | [diff] [blame] | 9926 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 9927 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 9928 | fdput(f); |
| 9929 | return -EBUSY; |
| 9930 | } |
| 9931 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9932 | fdput(f); |
| 9933 | next_insn: |
| 9934 | insn++; |
| 9935 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 9936 | continue; |
| 9937 | } |
| 9938 | |
| 9939 | /* Basic sanity check before we invest more work here. */ |
| 9940 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 9941 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 9942 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9943 | } |
| 9944 | } |
| 9945 | |
| 9946 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 9947 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 9948 | * These pointers will be used later by verifier to validate map access. |
| 9949 | */ |
| 9950 | return 0; |
| 9951 | } |
| 9952 | |
| 9953 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9954 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9955 | { |
Daniel Borkmann | a2ea074 | 2019-12-16 17:49:00 +0100 | [diff] [blame] | 9956 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
| 9957 | env->used_map_cnt); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9958 | } |
| 9959 | |
| 9960 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9961 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9962 | { |
| 9963 | struct bpf_insn *insn = env->prog->insnsi; |
| 9964 | int insn_cnt = env->prog->len; |
| 9965 | int i; |
| 9966 | |
| 9967 | for (i = 0; i < insn_cnt; i++, insn++) |
| 9968 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 9969 | insn->src_reg = 0; |
| 9970 | } |
| 9971 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9972 | /* single env->prog->insni[off] instruction was replaced with the range |
| 9973 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 9974 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 9975 | */ |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 9976 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, |
| 9977 | struct bpf_prog *new_prog, u32 off, u32 cnt) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9978 | { |
| 9979 | 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] | 9980 | struct bpf_insn *insn = new_prog->insnsi; |
| 9981 | u32 prog_len; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 9982 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9983 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 9984 | /* aux info at OFF always needs adjustment, no matter fast path |
| 9985 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the |
| 9986 | * original insn at old prog. |
| 9987 | */ |
| 9988 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); |
| 9989 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9990 | if (cnt == 1) |
| 9991 | return 0; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 9992 | prog_len = new_prog->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 9993 | new_data = vzalloc(array_size(prog_len, |
| 9994 | sizeof(struct bpf_insn_aux_data))); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9995 | if (!new_data) |
| 9996 | return -ENOMEM; |
| 9997 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 9998 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 9999 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 10000 | for (i = off; i < off + cnt - 1; i++) { |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 10001 | new_data[i].seen = env->pass_cnt; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 10002 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
| 10003 | } |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10004 | env->insn_aux_data = new_data; |
| 10005 | vfree(old_data); |
| 10006 | return 0; |
| 10007 | } |
| 10008 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 10009 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 10010 | { |
| 10011 | int i; |
| 10012 | |
| 10013 | if (len == 1) |
| 10014 | return; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 10015 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 10016 | for (i = 0; i <= env->subprog_cnt; i++) { |
Edward Cree | afd5942 | 2018-11-16 12:00:07 +0000 | [diff] [blame] | 10017 | if (env->subprog_info[i].start <= off) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 10018 | continue; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 10019 | env->subprog_info[i].start += len - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 10020 | } |
| 10021 | } |
| 10022 | |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10023 | static void adjust_poke_descs(struct bpf_prog *prog, u32 len) |
| 10024 | { |
| 10025 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; |
| 10026 | int i, sz = prog->aux->size_poke_tab; |
| 10027 | struct bpf_jit_poke_descriptor *desc; |
| 10028 | |
| 10029 | for (i = 0; i < sz; i++) { |
| 10030 | desc = &tab[i]; |
| 10031 | desc->insn_idx += len - 1; |
| 10032 | } |
| 10033 | } |
| 10034 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10035 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 10036 | const struct bpf_insn *patch, u32 len) |
| 10037 | { |
| 10038 | struct bpf_prog *new_prog; |
| 10039 | |
| 10040 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 10041 | if (IS_ERR(new_prog)) { |
| 10042 | if (PTR_ERR(new_prog) == -ERANGE) |
| 10043 | verbose(env, |
| 10044 | "insn %d cannot be patched due to 16-bit range\n", |
| 10045 | env->insn_aux_data[off].orig_idx); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10046 | return NULL; |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 10047 | } |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 10048 | if (adjust_insn_aux_data(env, new_prog, off, len)) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10049 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 10050 | adjust_subprog_starts(env, off, len); |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10051 | adjust_poke_descs(new_prog, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10052 | return new_prog; |
| 10053 | } |
| 10054 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 10055 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
| 10056 | u32 off, u32 cnt) |
| 10057 | { |
| 10058 | int i, j; |
| 10059 | |
| 10060 | /* find first prog starting at or after off (first to remove) */ |
| 10061 | for (i = 0; i < env->subprog_cnt; i++) |
| 10062 | if (env->subprog_info[i].start >= off) |
| 10063 | break; |
| 10064 | /* find first prog starting at or after off + cnt (first to stay) */ |
| 10065 | for (j = i; j < env->subprog_cnt; j++) |
| 10066 | if (env->subprog_info[j].start >= off + cnt) |
| 10067 | break; |
| 10068 | /* if j doesn't start exactly at off + cnt, we are just removing |
| 10069 | * the front of previous prog |
| 10070 | */ |
| 10071 | if (env->subprog_info[j].start != off + cnt) |
| 10072 | j--; |
| 10073 | |
| 10074 | if (j > i) { |
| 10075 | struct bpf_prog_aux *aux = env->prog->aux; |
| 10076 | int move; |
| 10077 | |
| 10078 | /* move fake 'exit' subprog as well */ |
| 10079 | move = env->subprog_cnt + 1 - j; |
| 10080 | |
| 10081 | memmove(env->subprog_info + i, |
| 10082 | env->subprog_info + j, |
| 10083 | sizeof(*env->subprog_info) * move); |
| 10084 | env->subprog_cnt -= j - i; |
| 10085 | |
| 10086 | /* remove func_info */ |
| 10087 | if (aux->func_info) { |
| 10088 | move = aux->func_info_cnt - j; |
| 10089 | |
| 10090 | memmove(aux->func_info + i, |
| 10091 | aux->func_info + j, |
| 10092 | sizeof(*aux->func_info) * move); |
| 10093 | aux->func_info_cnt -= j - i; |
| 10094 | /* func_info->insn_off is set after all code rewrites, |
| 10095 | * in adjust_btf_func() - no need to adjust |
| 10096 | */ |
| 10097 | } |
| 10098 | } else { |
| 10099 | /* convert i from "first prog to remove" to "first to adjust" */ |
| 10100 | if (env->subprog_info[i].start == off) |
| 10101 | i++; |
| 10102 | } |
| 10103 | |
| 10104 | /* update fake 'exit' subprog as well */ |
| 10105 | for (; i <= env->subprog_cnt; i++) |
| 10106 | env->subprog_info[i].start -= cnt; |
| 10107 | |
| 10108 | return 0; |
| 10109 | } |
| 10110 | |
| 10111 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, |
| 10112 | u32 cnt) |
| 10113 | { |
| 10114 | struct bpf_prog *prog = env->prog; |
| 10115 | u32 i, l_off, l_cnt, nr_linfo; |
| 10116 | struct bpf_line_info *linfo; |
| 10117 | |
| 10118 | nr_linfo = prog->aux->nr_linfo; |
| 10119 | if (!nr_linfo) |
| 10120 | return 0; |
| 10121 | |
| 10122 | linfo = prog->aux->linfo; |
| 10123 | |
| 10124 | /* find first line info to remove, count lines to be removed */ |
| 10125 | for (i = 0; i < nr_linfo; i++) |
| 10126 | if (linfo[i].insn_off >= off) |
| 10127 | break; |
| 10128 | |
| 10129 | l_off = i; |
| 10130 | l_cnt = 0; |
| 10131 | for (; i < nr_linfo; i++) |
| 10132 | if (linfo[i].insn_off < off + cnt) |
| 10133 | l_cnt++; |
| 10134 | else |
| 10135 | break; |
| 10136 | |
| 10137 | /* First live insn doesn't match first live linfo, it needs to "inherit" |
| 10138 | * last removed linfo. prog is already modified, so prog->len == off |
| 10139 | * means no live instructions after (tail of the program was removed). |
| 10140 | */ |
| 10141 | if (prog->len != off && l_cnt && |
| 10142 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { |
| 10143 | l_cnt--; |
| 10144 | linfo[--i].insn_off = off + cnt; |
| 10145 | } |
| 10146 | |
| 10147 | /* remove the line info which refer to the removed instructions */ |
| 10148 | if (l_cnt) { |
| 10149 | memmove(linfo + l_off, linfo + i, |
| 10150 | sizeof(*linfo) * (nr_linfo - i)); |
| 10151 | |
| 10152 | prog->aux->nr_linfo -= l_cnt; |
| 10153 | nr_linfo = prog->aux->nr_linfo; |
| 10154 | } |
| 10155 | |
| 10156 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ |
| 10157 | for (i = l_off; i < nr_linfo; i++) |
| 10158 | linfo[i].insn_off -= cnt; |
| 10159 | |
| 10160 | /* fix up all subprogs (incl. 'exit') which start >= off */ |
| 10161 | for (i = 0; i <= env->subprog_cnt; i++) |
| 10162 | if (env->subprog_info[i].linfo_idx > l_off) { |
| 10163 | /* program may have started in the removed region but |
| 10164 | * may not be fully removed |
| 10165 | */ |
| 10166 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) |
| 10167 | env->subprog_info[i].linfo_idx -= l_cnt; |
| 10168 | else |
| 10169 | env->subprog_info[i].linfo_idx = l_off; |
| 10170 | } |
| 10171 | |
| 10172 | return 0; |
| 10173 | } |
| 10174 | |
| 10175 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) |
| 10176 | { |
| 10177 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 10178 | unsigned int orig_prog_len = env->prog->len; |
| 10179 | int err; |
| 10180 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 10181 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 10182 | bpf_prog_offload_remove_insns(env, off, cnt); |
| 10183 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 10184 | err = bpf_remove_insns(env->prog, off, cnt); |
| 10185 | if (err) |
| 10186 | return err; |
| 10187 | |
| 10188 | err = adjust_subprog_starts_after_remove(env, off, cnt); |
| 10189 | if (err) |
| 10190 | return err; |
| 10191 | |
| 10192 | err = bpf_adj_linfo_after_remove(env, off, cnt); |
| 10193 | if (err) |
| 10194 | return err; |
| 10195 | |
| 10196 | memmove(aux_data + off, aux_data + off + cnt, |
| 10197 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); |
| 10198 | |
| 10199 | return 0; |
| 10200 | } |
| 10201 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 10202 | /* The verifier does more data flow analysis than llvm and will not |
| 10203 | * explore branches that are dead at run time. Malicious programs can |
| 10204 | * have dead code too. Therefore replace all dead at-run-time code |
| 10205 | * with 'ja -1'. |
| 10206 | * |
| 10207 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 10208 | * program and through another bug we would manage to jump there, then |
| 10209 | * we'd execute beyond program memory otherwise. Returning exception |
| 10210 | * code also wouldn't work since we can have subprogs where the dead |
| 10211 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 10212 | */ |
| 10213 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 10214 | { |
| 10215 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 10216 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 10217 | struct bpf_insn *insn = env->prog->insnsi; |
| 10218 | const int insn_cnt = env->prog->len; |
| 10219 | int i; |
| 10220 | |
| 10221 | for (i = 0; i < insn_cnt; i++) { |
| 10222 | if (aux_data[i].seen) |
| 10223 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 10224 | memcpy(insn + i, &trap, sizeof(trap)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 10225 | } |
| 10226 | } |
| 10227 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 10228 | static bool insn_is_cond_jump(u8 code) |
| 10229 | { |
| 10230 | u8 op; |
| 10231 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 10232 | if (BPF_CLASS(code) == BPF_JMP32) |
| 10233 | return true; |
| 10234 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 10235 | if (BPF_CLASS(code) != BPF_JMP) |
| 10236 | return false; |
| 10237 | |
| 10238 | op = BPF_OP(code); |
| 10239 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; |
| 10240 | } |
| 10241 | |
| 10242 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) |
| 10243 | { |
| 10244 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 10245 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 10246 | struct bpf_insn *insn = env->prog->insnsi; |
| 10247 | const int insn_cnt = env->prog->len; |
| 10248 | int i; |
| 10249 | |
| 10250 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 10251 | if (!insn_is_cond_jump(insn->code)) |
| 10252 | continue; |
| 10253 | |
| 10254 | if (!aux_data[i + 1].seen) |
| 10255 | ja.off = insn->off; |
| 10256 | else if (!aux_data[i + 1 + insn->off].seen) |
| 10257 | ja.off = 0; |
| 10258 | else |
| 10259 | continue; |
| 10260 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 10261 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 10262 | bpf_prog_offload_replace_insn(env, i, &ja); |
| 10263 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 10264 | memcpy(insn, &ja, sizeof(ja)); |
| 10265 | } |
| 10266 | } |
| 10267 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 10268 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
| 10269 | { |
| 10270 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 10271 | int insn_cnt = env->prog->len; |
| 10272 | int i, err; |
| 10273 | |
| 10274 | for (i = 0; i < insn_cnt; i++) { |
| 10275 | int j; |
| 10276 | |
| 10277 | j = 0; |
| 10278 | while (i + j < insn_cnt && !aux_data[i + j].seen) |
| 10279 | j++; |
| 10280 | if (!j) |
| 10281 | continue; |
| 10282 | |
| 10283 | err = verifier_remove_insns(env, i, j); |
| 10284 | if (err) |
| 10285 | return err; |
| 10286 | insn_cnt = env->prog->len; |
| 10287 | } |
| 10288 | |
| 10289 | return 0; |
| 10290 | } |
| 10291 | |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 10292 | static int opt_remove_nops(struct bpf_verifier_env *env) |
| 10293 | { |
| 10294 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 10295 | struct bpf_insn *insn = env->prog->insnsi; |
| 10296 | int insn_cnt = env->prog->len; |
| 10297 | int i, err; |
| 10298 | |
| 10299 | for (i = 0; i < insn_cnt; i++) { |
| 10300 | if (memcmp(&insn[i], &ja, sizeof(ja))) |
| 10301 | continue; |
| 10302 | |
| 10303 | err = verifier_remove_insns(env, i, 1); |
| 10304 | if (err) |
| 10305 | return err; |
| 10306 | insn_cnt--; |
| 10307 | i--; |
| 10308 | } |
| 10309 | |
| 10310 | return 0; |
| 10311 | } |
| 10312 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10313 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
| 10314 | const union bpf_attr *attr) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10315 | { |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10316 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10317 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10318 | int i, patch_len, delta = 0, len = env->prog->len; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10319 | struct bpf_insn *insns = env->prog->insnsi; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10320 | struct bpf_prog *new_prog; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10321 | bool rnd_hi32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10322 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10323 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10324 | zext_patch[1] = BPF_ZEXT_REG(0); |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10325 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
| 10326 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); |
| 10327 | 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] | 10328 | for (i = 0; i < len; i++) { |
| 10329 | int adj_idx = i + delta; |
| 10330 | struct bpf_insn insn; |
| 10331 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10332 | insn = insns[adj_idx]; |
| 10333 | if (!aux[adj_idx].zext_dst) { |
| 10334 | u8 code, class; |
| 10335 | u32 imm_rnd; |
| 10336 | |
| 10337 | if (!rnd_hi32) |
| 10338 | continue; |
| 10339 | |
| 10340 | code = insn.code; |
| 10341 | class = BPF_CLASS(code); |
| 10342 | if (insn_no_def(&insn)) |
| 10343 | continue; |
| 10344 | |
| 10345 | /* NOTE: arg "reg" (the fourth one) is only used for |
| 10346 | * BPF_STX which has been ruled out in above |
| 10347 | * check, it is safe to pass NULL here. |
| 10348 | */ |
| 10349 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { |
| 10350 | if (class == BPF_LD && |
| 10351 | BPF_MODE(code) == BPF_IMM) |
| 10352 | i++; |
| 10353 | continue; |
| 10354 | } |
| 10355 | |
| 10356 | /* ctx load could be transformed into wider load. */ |
| 10357 | if (class == BPF_LDX && |
| 10358 | aux[adj_idx].ptr_type == PTR_TO_CTX) |
| 10359 | continue; |
| 10360 | |
| 10361 | imm_rnd = get_random_int(); |
| 10362 | rnd_hi32_patch[0] = insn; |
| 10363 | rnd_hi32_patch[1].imm = imm_rnd; |
| 10364 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; |
| 10365 | patch = rnd_hi32_patch; |
| 10366 | patch_len = 4; |
| 10367 | goto apply_patch_buffer; |
| 10368 | } |
| 10369 | |
| 10370 | if (!bpf_jit_needs_zext()) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10371 | continue; |
| 10372 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10373 | zext_patch[0] = insn; |
| 10374 | zext_patch[1].dst_reg = insn.dst_reg; |
| 10375 | zext_patch[1].src_reg = insn.dst_reg; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10376 | patch = zext_patch; |
| 10377 | patch_len = 2; |
| 10378 | apply_patch_buffer: |
| 10379 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10380 | if (!new_prog) |
| 10381 | return -ENOMEM; |
| 10382 | env->prog = new_prog; |
| 10383 | insns = new_prog->insnsi; |
| 10384 | aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10385 | delta += patch_len - 1; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10386 | } |
| 10387 | |
| 10388 | return 0; |
| 10389 | } |
| 10390 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10391 | /* convert load instructions that access fields of a context type into a |
| 10392 | * sequence of instructions that access fields of the underlying structure: |
| 10393 | * struct __sk_buff -> struct sk_buff |
| 10394 | * struct bpf_sock_ops -> struct sock |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10395 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 10396 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10397 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 10398 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10399 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10400 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10401 | struct bpf_insn insn_buf[16], *insn; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10402 | u32 target_size, size_default, off; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10403 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 10404 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10405 | bool is_narrower_load; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10406 | |
Daniel Borkmann | b09928b | 2018-10-24 22:05:49 +0200 | [diff] [blame] | 10407 | if (ops->gen_prologue || env->seen_direct_write) { |
| 10408 | if (!ops->gen_prologue) { |
| 10409 | verbose(env, "bpf verifier is misconfigured\n"); |
| 10410 | return -EINVAL; |
| 10411 | } |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10412 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 10413 | env->prog); |
| 10414 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10415 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10416 | return -EINVAL; |
| 10417 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10418 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10419 | if (!new_prog) |
| 10420 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10421 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10422 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10423 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10424 | } |
| 10425 | } |
| 10426 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10427 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10428 | return 0; |
| 10429 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10430 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10431 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10432 | for (i = 0; i < insn_cnt; i++, insn++) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10433 | bpf_convert_ctx_access_t convert_ctx_access; |
| 10434 | |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 10435 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 10436 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 10437 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 10438 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 10439 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 10440 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 10441 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 10442 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 10443 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 10444 | type = BPF_WRITE; |
| 10445 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10446 | continue; |
| 10447 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 10448 | if (type == BPF_WRITE && |
| 10449 | env->insn_aux_data[i + delta].sanitize_stack_off) { |
| 10450 | struct bpf_insn patch[] = { |
| 10451 | /* Sanitize suspicious stack slot with zero. |
| 10452 | * There are no memory dependencies for this store, |
| 10453 | * since it's only using frame pointer and immediate |
| 10454 | * constant of zero |
| 10455 | */ |
| 10456 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, |
| 10457 | env->insn_aux_data[i + delta].sanitize_stack_off, |
| 10458 | 0), |
| 10459 | /* the original STX instruction will immediately |
| 10460 | * overwrite the same stack slot with appropriate value |
| 10461 | */ |
| 10462 | *insn, |
| 10463 | }; |
| 10464 | |
| 10465 | cnt = ARRAY_SIZE(patch); |
| 10466 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 10467 | if (!new_prog) |
| 10468 | return -ENOMEM; |
| 10469 | |
| 10470 | delta += cnt - 1; |
| 10471 | env->prog = new_prog; |
| 10472 | insn = new_prog->insnsi + i + delta; |
| 10473 | continue; |
| 10474 | } |
| 10475 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10476 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 10477 | case PTR_TO_CTX: |
| 10478 | if (!ops->convert_ctx_access) |
| 10479 | continue; |
| 10480 | convert_ctx_access = ops->convert_ctx_access; |
| 10481 | break; |
| 10482 | case PTR_TO_SOCKET: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 10483 | case PTR_TO_SOCK_COMMON: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10484 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 10485 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 10486 | case PTR_TO_TCP_SOCK: |
| 10487 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; |
| 10488 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 10489 | case PTR_TO_XDP_SOCK: |
| 10490 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; |
| 10491 | break; |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 10492 | case PTR_TO_BTF_ID: |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 10493 | if (type == BPF_READ) { |
| 10494 | insn->code = BPF_LDX | BPF_PROBE_MEM | |
| 10495 | BPF_SIZE((insn)->code); |
| 10496 | env->prog->aux->num_exentries++; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 10497 | } else if (resolve_prog_type(env->prog) != BPF_PROG_TYPE_STRUCT_OPS) { |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 10498 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
| 10499 | return -EINVAL; |
| 10500 | } |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 10501 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10502 | default: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10503 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10504 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10505 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10506 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10507 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10508 | |
| 10509 | /* If the read access is a narrower load of the field, |
| 10510 | * convert to a 4/8-byte load, to minimum program type specific |
| 10511 | * convert_ctx_access changes. If conversion is successful, |
| 10512 | * we will apply proper mask to the result. |
| 10513 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10514 | is_narrower_load = size < ctx_field_size; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10515 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
| 10516 | off = insn->off; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10517 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10518 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10519 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10520 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10521 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10522 | return -EINVAL; |
| 10523 | } |
| 10524 | |
| 10525 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10526 | if (ctx_field_size == 4) |
| 10527 | size_code = BPF_W; |
| 10528 | else if (ctx_field_size == 8) |
| 10529 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10530 | |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 10531 | insn->off = off & ~(size_default - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10532 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 10533 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10534 | |
| 10535 | target_size = 0; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10536 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 10537 | &target_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10538 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 10539 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10540 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10541 | return -EINVAL; |
| 10542 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10543 | |
| 10544 | if (is_narrower_load && size < target_size) { |
Ilya Leoshkevich | d895a0f | 2019-08-16 12:53:00 +0200 | [diff] [blame] | 10545 | u8 shift = bpf_ctx_narrow_access_offset( |
| 10546 | off, size, size_default) * 8; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10547 | if (ctx_field_size <= 4) { |
| 10548 | if (shift) |
| 10549 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, |
| 10550 | insn->dst_reg, |
| 10551 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10552 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10553 | (1 << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10554 | } else { |
| 10555 | if (shift) |
| 10556 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, |
| 10557 | insn->dst_reg, |
| 10558 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10559 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Krzesimir Nowak | e2f7fc0 | 2019-05-08 18:08:58 +0200 | [diff] [blame] | 10560 | (1ULL << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10561 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10562 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10563 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10564 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10565 | if (!new_prog) |
| 10566 | return -ENOMEM; |
| 10567 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10568 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10569 | |
| 10570 | /* keep walking new program and skip insns we just inserted */ |
| 10571 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10572 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10573 | } |
| 10574 | |
| 10575 | return 0; |
| 10576 | } |
| 10577 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10578 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 10579 | { |
| 10580 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 10581 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10582 | struct bpf_map *map_ptr; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10583 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10584 | void *old_bpf_func; |
Yonghong Song | c4c0bdc | 2020-06-23 17:10:54 -0700 | [diff] [blame] | 10585 | int err, num_exentries; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10586 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10587 | if (env->subprog_cnt <= 1) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10588 | return 0; |
| 10589 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10590 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10591 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10592 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10593 | continue; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10594 | /* Upon error here we cannot fall back to interpreter but |
| 10595 | * need a hard reject of the program. Thus -EFAULT is |
| 10596 | * propagated in any case. |
| 10597 | */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10598 | subprog = find_subprog(env, i + insn->imm + 1); |
| 10599 | if (subprog < 0) { |
| 10600 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 10601 | i + insn->imm + 1); |
| 10602 | return -EFAULT; |
| 10603 | } |
| 10604 | /* temporarily remember subprog id inside insn instead of |
| 10605 | * aux_data, since next loop will split up all insns into funcs |
| 10606 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10607 | insn->off = subprog; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10608 | /* remember original imm in case JIT fails and fallback |
| 10609 | * to interpreter will be needed |
| 10610 | */ |
| 10611 | env->insn_aux_data[i].call_imm = insn->imm; |
| 10612 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 10613 | insn->imm = 1; |
| 10614 | } |
| 10615 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10616 | err = bpf_prog_alloc_jited_linfo(prog); |
| 10617 | if (err) |
| 10618 | goto out_undo_insn; |
| 10619 | |
| 10620 | err = -ENOMEM; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 10621 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10622 | if (!func) |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10623 | goto out_undo_insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10624 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10625 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10626 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 10627 | subprog_end = env->subprog_info[i + 1].start; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10628 | |
| 10629 | len = subprog_end - subprog_start; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 10630 | /* BPF_PROG_RUN doesn't call subprogs directly, |
| 10631 | * hence main prog stats include the runtime of subprogs. |
| 10632 | * subprogs don't have IDs and not reachable via prog_get_next_id |
| 10633 | * func[i]->aux->stats will never be accessed and stays NULL |
| 10634 | */ |
| 10635 | 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] | 10636 | if (!func[i]) |
| 10637 | goto out_free; |
| 10638 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 10639 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 10640 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10641 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 10642 | if (bpf_prog_calc_tag(func[i])) |
| 10643 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10644 | func[i]->is_func = 1; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10645 | func[i]->aux->func_idx = i; |
| 10646 | /* the btf and func_info will be freed only at prog->aux */ |
| 10647 | func[i]->aux->btf = prog->aux->btf; |
| 10648 | func[i]->aux->func_info = prog->aux->func_info; |
| 10649 | |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10650 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
| 10651 | u32 insn_idx = prog->aux->poke_tab[j].insn_idx; |
| 10652 | int ret; |
| 10653 | |
| 10654 | if (!(insn_idx >= subprog_start && |
| 10655 | insn_idx <= subprog_end)) |
| 10656 | continue; |
| 10657 | |
| 10658 | ret = bpf_jit_add_poke_descriptor(func[i], |
| 10659 | &prog->aux->poke_tab[j]); |
| 10660 | if (ret < 0) { |
| 10661 | verbose(env, "adding tail call poke descriptor failed\n"); |
| 10662 | goto out_free; |
| 10663 | } |
| 10664 | |
| 10665 | func[i]->insnsi[insn_idx - subprog_start].imm = ret + 1; |
| 10666 | |
| 10667 | map_ptr = func[i]->aux->poke_tab[ret].tail_call.map; |
| 10668 | ret = map_ptr->ops->map_poke_track(map_ptr, func[i]->aux); |
| 10669 | if (ret < 0) { |
| 10670 | verbose(env, "tracking tail call prog failed\n"); |
| 10671 | goto out_free; |
| 10672 | } |
| 10673 | } |
| 10674 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10675 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 10676 | * Long term would need debug info to populate names |
| 10677 | */ |
| 10678 | func[i]->aux->name[0] = 'F'; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 10679 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10680 | func[i]->jit_requested = 1; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10681 | func[i]->aux->linfo = prog->aux->linfo; |
| 10682 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; |
| 10683 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; |
| 10684 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; |
Yonghong Song | c4c0bdc | 2020-06-23 17:10:54 -0700 | [diff] [blame] | 10685 | num_exentries = 0; |
| 10686 | insn = func[i]->insnsi; |
| 10687 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 10688 | if (BPF_CLASS(insn->code) == BPF_LDX && |
| 10689 | BPF_MODE(insn->code) == BPF_PROBE_MEM) |
| 10690 | num_exentries++; |
| 10691 | } |
| 10692 | func[i]->aux->num_exentries = num_exentries; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 10693 | 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] | 10694 | func[i] = bpf_int_jit_compile(func[i]); |
| 10695 | if (!func[i]->jited) { |
| 10696 | err = -ENOTSUPP; |
| 10697 | goto out_free; |
| 10698 | } |
| 10699 | cond_resched(); |
| 10700 | } |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10701 | |
| 10702 | /* Untrack main program's aux structs so that during map_poke_run() |
| 10703 | * we will not stumble upon the unfilled poke descriptors; each |
| 10704 | * of the main program's poke descs got distributed across subprogs |
| 10705 | * and got tracked onto map, so we are sure that none of them will |
| 10706 | * be missed after the operation below |
| 10707 | */ |
| 10708 | for (i = 0; i < prog->aux->size_poke_tab; i++) { |
| 10709 | map_ptr = prog->aux->poke_tab[i].tail_call.map; |
| 10710 | |
| 10711 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); |
| 10712 | } |
| 10713 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10714 | /* at this point all bpf functions were successfully JITed |
| 10715 | * now populate all bpf_calls with correct addresses and |
| 10716 | * run last pass of JIT |
| 10717 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10718 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10719 | insn = func[i]->insnsi; |
| 10720 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 10721 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10722 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10723 | continue; |
| 10724 | subprog = insn->off; |
Prashant Bhole | 0d306c3 | 2019-04-16 18:13:01 +0900 | [diff] [blame] | 10725 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
| 10726 | __bpf_call_base; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10727 | } |
Sandipan Das | 2162fed | 2018-05-24 12:26:45 +0530 | [diff] [blame] | 10728 | |
| 10729 | /* we use the aux data to keep a list of the start addresses |
| 10730 | * of the JITed images for each function in the program |
| 10731 | * |
| 10732 | * for some architectures, such as powerpc64, the imm field |
| 10733 | * might not be large enough to hold the offset of the start |
| 10734 | * address of the callee's JITed image from __bpf_call_base |
| 10735 | * |
| 10736 | * in such cases, we can lookup the start address of a callee |
| 10737 | * by using its subprog id, available from the off field of |
| 10738 | * the call instruction, as an index for this list |
| 10739 | */ |
| 10740 | func[i]->aux->func = func; |
| 10741 | func[i]->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10742 | } |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10743 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10744 | old_bpf_func = func[i]->bpf_func; |
| 10745 | tmp = bpf_int_jit_compile(func[i]); |
| 10746 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 10747 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10748 | err = -ENOTSUPP; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10749 | goto out_free; |
| 10750 | } |
| 10751 | cond_resched(); |
| 10752 | } |
| 10753 | |
| 10754 | /* finally lock prog and jit images for all functions and |
| 10755 | * populate kallsysm |
| 10756 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10757 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10758 | bpf_prog_lock_ro(func[i]); |
| 10759 | bpf_prog_kallsyms_add(func[i]); |
| 10760 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10761 | |
| 10762 | /* Last step: make now unused interpreter insns from main |
| 10763 | * prog consistent for later dump requests, so they can |
| 10764 | * later look the same as if they were interpreted only. |
| 10765 | */ |
| 10766 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10767 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10768 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10769 | continue; |
| 10770 | insn->off = env->insn_aux_data[i].call_imm; |
| 10771 | subprog = find_subprog(env, i + insn->off + 1); |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 10772 | insn->imm = subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10773 | } |
| 10774 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10775 | prog->jited = 1; |
| 10776 | prog->bpf_func = func[0]->bpf_func; |
| 10777 | prog->aux->func = func; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10778 | prog->aux->func_cnt = env->subprog_cnt; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10779 | bpf_prog_free_unused_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10780 | return 0; |
| 10781 | out_free: |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10782 | for (i = 0; i < env->subprog_cnt; i++) { |
| 10783 | if (!func[i]) |
| 10784 | continue; |
| 10785 | |
| 10786 | for (j = 0; j < func[i]->aux->size_poke_tab; j++) { |
| 10787 | map_ptr = func[i]->aux->poke_tab[j].tail_call.map; |
| 10788 | map_ptr->ops->map_poke_untrack(map_ptr, func[i]->aux); |
| 10789 | } |
| 10790 | bpf_jit_free(func[i]); |
| 10791 | } |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10792 | kfree(func); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10793 | out_undo_insn: |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10794 | /* cleanup main prog to be interpreted */ |
| 10795 | prog->jit_requested = 0; |
| 10796 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 10797 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10798 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10799 | continue; |
| 10800 | insn->off = 0; |
| 10801 | insn->imm = env->insn_aux_data[i].call_imm; |
| 10802 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10803 | bpf_prog_free_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10804 | return err; |
| 10805 | } |
| 10806 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10807 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 10808 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10809 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10810 | struct bpf_prog *prog = env->prog; |
| 10811 | struct bpf_insn *insn = prog->insnsi; |
| 10812 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10813 | #endif |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 10814 | int err = 0; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10815 | |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 10816 | if (env->prog->jit_requested && |
| 10817 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10818 | err = jit_subprogs(env); |
| 10819 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10820 | return 0; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10821 | if (err == -EFAULT) |
| 10822 | return err; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10823 | } |
| 10824 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 10825 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
| 10826 | /* When JIT fails the progs with bpf2bpf calls and tail_calls |
| 10827 | * have to be rejected, since interpreter doesn't support them yet. |
| 10828 | */ |
| 10829 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); |
| 10830 | return -EINVAL; |
| 10831 | } |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10832 | for (i = 0; i < prog->len; i++, insn++) { |
| 10833 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10834 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10835 | continue; |
| 10836 | depth = get_callee_stack_depth(env, insn, i); |
| 10837 | if (depth < 0) |
| 10838 | return depth; |
| 10839 | bpf_patch_call_args(insn, depth); |
| 10840 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10841 | err = 0; |
| 10842 | #endif |
| 10843 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10844 | } |
| 10845 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10846 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 10847 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10848 | * |
| 10849 | * this function is called after eBPF program passed verification |
| 10850 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10851 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10852 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10853 | struct bpf_prog *prog = env->prog; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 10854 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10855 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10856 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10857 | const int insn_cnt = prog->len; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 10858 | const struct bpf_map_ops *ops; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 10859 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 10860 | struct bpf_insn insn_buf[16]; |
| 10861 | struct bpf_prog *new_prog; |
| 10862 | struct bpf_map *map_ptr; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 10863 | int i, ret, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10864 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10865 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 10866 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 10867 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 10868 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 10869 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 10870 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
| 10871 | struct bpf_insn mask_and_div[] = { |
| 10872 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 10873 | /* Rx div 0 -> 0 */ |
| 10874 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), |
| 10875 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 10876 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 10877 | *insn, |
| 10878 | }; |
| 10879 | struct bpf_insn mask_and_mod[] = { |
| 10880 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 10881 | /* Rx mod 0 -> Rx */ |
| 10882 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), |
| 10883 | *insn, |
| 10884 | }; |
| 10885 | struct bpf_insn *patchlet; |
| 10886 | |
| 10887 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 10888 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 10889 | patchlet = mask_and_div + (is64 ? 1 : 0); |
| 10890 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); |
| 10891 | } else { |
| 10892 | patchlet = mask_and_mod + (is64 ? 1 : 0); |
| 10893 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); |
| 10894 | } |
| 10895 | |
| 10896 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 10897 | if (!new_prog) |
| 10898 | return -ENOMEM; |
| 10899 | |
| 10900 | delta += cnt - 1; |
| 10901 | env->prog = prog = new_prog; |
| 10902 | insn = new_prog->insnsi + i + delta; |
| 10903 | continue; |
| 10904 | } |
| 10905 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 10906 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 10907 | (BPF_MODE(insn->code) == BPF_ABS || |
| 10908 | BPF_MODE(insn->code) == BPF_IND)) { |
| 10909 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 10910 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 10911 | verbose(env, "bpf verifier is misconfigured\n"); |
| 10912 | return -EINVAL; |
| 10913 | } |
| 10914 | |
| 10915 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 10916 | if (!new_prog) |
| 10917 | return -ENOMEM; |
| 10918 | |
| 10919 | delta += cnt - 1; |
| 10920 | env->prog = prog = new_prog; |
| 10921 | insn = new_prog->insnsi + i + delta; |
| 10922 | continue; |
| 10923 | } |
| 10924 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 10925 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
| 10926 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { |
| 10927 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; |
| 10928 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; |
| 10929 | struct bpf_insn insn_buf[16]; |
| 10930 | struct bpf_insn *patch = &insn_buf[0]; |
| 10931 | bool issrc, isneg; |
| 10932 | u32 off_reg; |
| 10933 | |
| 10934 | aux = &env->insn_aux_data[i + delta]; |
Daniel Borkmann | 3612af7 | 2019-03-01 22:05:29 +0100 | [diff] [blame] | 10935 | if (!aux->alu_state || |
| 10936 | aux->alu_state == BPF_ALU_NON_POINTER) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 10937 | continue; |
| 10938 | |
| 10939 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; |
| 10940 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == |
| 10941 | BPF_ALU_SANITIZE_SRC; |
| 10942 | |
| 10943 | off_reg = issrc ? insn->src_reg : insn->dst_reg; |
| 10944 | if (isneg) |
| 10945 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 10946 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); |
| 10947 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); |
| 10948 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); |
| 10949 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); |
| 10950 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); |
| 10951 | if (issrc) { |
| 10952 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, |
| 10953 | off_reg); |
| 10954 | insn->src_reg = BPF_REG_AX; |
| 10955 | } else { |
| 10956 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, |
| 10957 | BPF_REG_AX); |
| 10958 | } |
| 10959 | if (isneg) |
| 10960 | insn->code = insn->code == code_add ? |
| 10961 | code_sub : code_add; |
| 10962 | *patch++ = *insn; |
| 10963 | if (issrc && isneg) |
| 10964 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 10965 | cnt = patch - insn_buf; |
| 10966 | |
| 10967 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 10968 | if (!new_prog) |
| 10969 | return -ENOMEM; |
| 10970 | |
| 10971 | delta += cnt - 1; |
| 10972 | env->prog = prog = new_prog; |
| 10973 | insn = new_prog->insnsi + i + delta; |
| 10974 | continue; |
| 10975 | } |
| 10976 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10977 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 10978 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 10979 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 10980 | continue; |
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 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 10983 | prog->dst_needed = 1; |
| 10984 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 10985 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 10986 | if (insn->imm == BPF_FUNC_override_return) |
| 10987 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10988 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 10989 | /* If we tail call into other programs, we |
| 10990 | * cannot make any assumptions since they can |
| 10991 | * be replaced dynamically during runtime in |
| 10992 | * the program array. |
| 10993 | */ |
| 10994 | prog->cb_access = 1; |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 10995 | if (!allow_tail_call_in_subprogs(env)) |
| 10996 | prog->aux->stack_depth = MAX_BPF_STACK; |
| 10997 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 10998 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10999 | /* mark bpf_tail_call as different opcode to avoid |
| 11000 | * conditional branch in the interpeter for every normal |
| 11001 | * call and to prevent accidental JITing by JIT compiler |
| 11002 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 11003 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11004 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 11005 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 11006 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 11007 | aux = &env->insn_aux_data[i + delta]; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 11008 | if (env->bpf_capable && !expect_blinding && |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 11009 | prog->jit_requested && |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 11010 | !bpf_map_key_poisoned(aux) && |
| 11011 | !bpf_map_ptr_poisoned(aux) && |
| 11012 | !bpf_map_ptr_unpriv(aux)) { |
| 11013 | struct bpf_jit_poke_descriptor desc = { |
| 11014 | .reason = BPF_POKE_REASON_TAIL_CALL, |
| 11015 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), |
| 11016 | .tail_call.key = bpf_map_key_immediate(aux), |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 11017 | .insn_idx = i + delta, |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 11018 | }; |
| 11019 | |
| 11020 | ret = bpf_jit_add_poke_descriptor(prog, &desc); |
| 11021 | if (ret < 0) { |
| 11022 | verbose(env, "adding tail call poke descriptor failed\n"); |
| 11023 | return ret; |
| 11024 | } |
| 11025 | |
| 11026 | insn->imm = ret + 1; |
| 11027 | continue; |
| 11028 | } |
| 11029 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 11030 | if (!bpf_map_ptr_unpriv(aux)) |
| 11031 | continue; |
| 11032 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 11033 | /* instead of changing every JIT dealing with tail_call |
| 11034 | * emit two extra insns: |
| 11035 | * if (index >= max_entries) goto out; |
| 11036 | * index &= array->index_mask; |
| 11037 | * to avoid out-of-bounds cpu speculation |
| 11038 | */ |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 11039 | if (bpf_map_ptr_poisoned(aux)) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 11040 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 11041 | return -EINVAL; |
| 11042 | } |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 11043 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 11044 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 11045 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 11046 | map_ptr->max_entries, 2); |
| 11047 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 11048 | container_of(map_ptr, |
| 11049 | struct bpf_array, |
| 11050 | map)->index_mask); |
| 11051 | insn_buf[2] = *insn; |
| 11052 | cnt = 3; |
| 11053 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 11054 | if (!new_prog) |
| 11055 | return -ENOMEM; |
| 11056 | |
| 11057 | delta += cnt - 1; |
| 11058 | env->prog = prog = new_prog; |
| 11059 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11060 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 11061 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 11062 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 11063 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11064 | * and other inlining handlers are currently limited to 64 bit |
| 11065 | * only. |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 11066 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 11067 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11068 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 11069 | insn->imm == BPF_FUNC_map_update_elem || |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 11070 | insn->imm == BPF_FUNC_map_delete_elem || |
| 11071 | insn->imm == BPF_FUNC_map_push_elem || |
| 11072 | insn->imm == BPF_FUNC_map_pop_elem || |
| 11073 | insn->imm == BPF_FUNC_map_peek_elem)) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 11074 | aux = &env->insn_aux_data[i + delta]; |
| 11075 | if (bpf_map_ptr_poisoned(aux)) |
| 11076 | goto patch_call_imm; |
| 11077 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 11078 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11079 | ops = map_ptr->ops; |
| 11080 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 11081 | ops->map_gen_lookup) { |
| 11082 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
Daniel Borkmann | 4a8f87e | 2020-10-11 01:40:03 +0200 | [diff] [blame] | 11083 | if (cnt == -EOPNOTSUPP) |
| 11084 | goto patch_map_ops_generic; |
| 11085 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11086 | verbose(env, "bpf verifier is misconfigured\n"); |
| 11087 | return -EINVAL; |
| 11088 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 11089 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11090 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 11091 | insn_buf, cnt); |
| 11092 | if (!new_prog) |
| 11093 | return -ENOMEM; |
| 11094 | |
| 11095 | delta += cnt - 1; |
| 11096 | env->prog = prog = new_prog; |
| 11097 | insn = new_prog->insnsi + i + delta; |
| 11098 | continue; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 11099 | } |
| 11100 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11101 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 11102 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 11103 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 11104 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 11105 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 11106 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 11107 | u64 flags))NULL)); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 11108 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 11109 | (int (*)(struct bpf_map *map, void *value, |
| 11110 | u64 flags))NULL)); |
| 11111 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 11112 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 11113 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 11114 | (int (*)(struct bpf_map *map, void *value))NULL)); |
Daniel Borkmann | 4a8f87e | 2020-10-11 01:40:03 +0200 | [diff] [blame] | 11115 | patch_map_ops_generic: |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11116 | switch (insn->imm) { |
| 11117 | case BPF_FUNC_map_lookup_elem: |
| 11118 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - |
| 11119 | __bpf_call_base; |
| 11120 | continue; |
| 11121 | case BPF_FUNC_map_update_elem: |
| 11122 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - |
| 11123 | __bpf_call_base; |
| 11124 | continue; |
| 11125 | case BPF_FUNC_map_delete_elem: |
| 11126 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - |
| 11127 | __bpf_call_base; |
| 11128 | continue; |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 11129 | case BPF_FUNC_map_push_elem: |
| 11130 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - |
| 11131 | __bpf_call_base; |
| 11132 | continue; |
| 11133 | case BPF_FUNC_map_pop_elem: |
| 11134 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - |
| 11135 | __bpf_call_base; |
| 11136 | continue; |
| 11137 | case BPF_FUNC_map_peek_elem: |
| 11138 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - |
| 11139 | __bpf_call_base; |
| 11140 | continue; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11141 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 11142 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 11143 | goto patch_call_imm; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 11144 | } |
| 11145 | |
Martin KaFai Lau | 5576b99 | 2020-01-22 15:36:46 -0800 | [diff] [blame] | 11146 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
| 11147 | insn->imm == BPF_FUNC_jiffies64) { |
| 11148 | struct bpf_insn ld_jiffies_addr[2] = { |
| 11149 | BPF_LD_IMM64(BPF_REG_0, |
| 11150 | (unsigned long)&jiffies), |
| 11151 | }; |
| 11152 | |
| 11153 | insn_buf[0] = ld_jiffies_addr[0]; |
| 11154 | insn_buf[1] = ld_jiffies_addr[1]; |
| 11155 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, |
| 11156 | BPF_REG_0, 0); |
| 11157 | cnt = 3; |
| 11158 | |
| 11159 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, |
| 11160 | cnt); |
| 11161 | if (!new_prog) |
| 11162 | return -ENOMEM; |
| 11163 | |
| 11164 | delta += cnt - 1; |
| 11165 | env->prog = prog = new_prog; |
| 11166 | insn = new_prog->insnsi + i + delta; |
| 11167 | continue; |
| 11168 | } |
| 11169 | |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 11170 | patch_call_imm: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 11171 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11172 | /* all functions that have prototype and verifier allowed |
| 11173 | * programs to call them, must be real in-kernel functions |
| 11174 | */ |
| 11175 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11176 | verbose(env, |
| 11177 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11178 | func_id_name(insn->imm), insn->imm); |
| 11179 | return -EFAULT; |
| 11180 | } |
| 11181 | insn->imm = fn->func - __bpf_call_base; |
| 11182 | } |
| 11183 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 11184 | /* Since poke tab is now finalized, publish aux to tracker. */ |
| 11185 | for (i = 0; i < prog->aux->size_poke_tab; i++) { |
| 11186 | map_ptr = prog->aux->poke_tab[i].tail_call.map; |
| 11187 | if (!map_ptr->ops->map_poke_track || |
| 11188 | !map_ptr->ops->map_poke_untrack || |
| 11189 | !map_ptr->ops->map_poke_run) { |
| 11190 | verbose(env, "bpf verifier is misconfigured\n"); |
| 11191 | return -EINVAL; |
| 11192 | } |
| 11193 | |
| 11194 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); |
| 11195 | if (ret < 0) { |
| 11196 | verbose(env, "tracking tail call prog failed\n"); |
| 11197 | return ret; |
| 11198 | } |
| 11199 | } |
| 11200 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11201 | return 0; |
| 11202 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 11203 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11204 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11205 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11206 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11207 | int i; |
| 11208 | |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11209 | sl = env->free_list; |
| 11210 | while (sl) { |
| 11211 | sln = sl->next; |
| 11212 | free_verifier_state(&sl->state, false); |
| 11213 | kfree(sl); |
| 11214 | sl = sln; |
| 11215 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11216 | env->free_list = NULL; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11217 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11218 | if (!env->explored_states) |
| 11219 | return; |
| 11220 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 11221 | for (i = 0; i < state_htab_size(env); i++) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11222 | sl = env->explored_states[i]; |
| 11223 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 11224 | while (sl) { |
| 11225 | sln = sl->next; |
| 11226 | free_verifier_state(&sl->state, false); |
| 11227 | kfree(sl); |
| 11228 | sl = sln; |
| 11229 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11230 | env->explored_states[i] = NULL; |
| 11231 | } |
| 11232 | } |
| 11233 | |
| 11234 | /* The verifier is using insn_aux_data[] to store temporary data during |
| 11235 | * verification and to store information for passes that run after the |
| 11236 | * verification like dead code sanitization. do_check_common() for subprogram N |
| 11237 | * may analyze many other subprograms. sanitize_insn_aux_data() clears all |
| 11238 | * temporary data after do_check_common() finds that subprogram N cannot be |
| 11239 | * verified independently. pass_cnt counts the number of times |
| 11240 | * do_check_common() was run and insn->aux->seen tells the pass number |
| 11241 | * insn_aux_data was touched. These variables are compared to clear temporary |
| 11242 | * data from failed pass. For testing and experiments do_check_common() can be |
| 11243 | * run multiple times even when prior attempt to verify is unsuccessful. |
| 11244 | */ |
| 11245 | static void sanitize_insn_aux_data(struct bpf_verifier_env *env) |
| 11246 | { |
| 11247 | struct bpf_insn *insn = env->prog->insnsi; |
| 11248 | struct bpf_insn_aux_data *aux; |
| 11249 | int i, class; |
| 11250 | |
| 11251 | for (i = 0; i < env->prog->len; i++) { |
| 11252 | class = BPF_CLASS(insn[i].code); |
| 11253 | if (class != BPF_LDX && class != BPF_STX) |
| 11254 | continue; |
| 11255 | aux = &env->insn_aux_data[i]; |
| 11256 | if (aux->seen != env->pass_cnt) |
| 11257 | continue; |
| 11258 | memset(aux, 0, offsetof(typeof(*aux), orig_idx)); |
| 11259 | } |
| 11260 | } |
| 11261 | |
| 11262 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
| 11263 | { |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 11264 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11265 | struct bpf_verifier_state *state; |
| 11266 | struct bpf_reg_state *regs; |
| 11267 | int ret, i; |
| 11268 | |
| 11269 | env->prev_linfo = NULL; |
| 11270 | env->pass_cnt++; |
| 11271 | |
| 11272 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 11273 | if (!state) |
| 11274 | return -ENOMEM; |
| 11275 | state->curframe = 0; |
| 11276 | state->speculative = false; |
| 11277 | state->branches = 1; |
| 11278 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 11279 | if (!state->frame[0]) { |
| 11280 | kfree(state); |
| 11281 | return -ENOMEM; |
| 11282 | } |
| 11283 | env->cur_state = state; |
| 11284 | init_func_state(env, state->frame[0], |
| 11285 | BPF_MAIN_FUNC /* callsite */, |
| 11286 | 0 /* frameno */, |
| 11287 | subprog); |
| 11288 | |
| 11289 | regs = state->frame[state->curframe]->regs; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11290 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11291 | ret = btf_prepare_func_args(env, subprog, regs); |
| 11292 | if (ret) |
| 11293 | goto out; |
| 11294 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { |
| 11295 | if (regs[i].type == PTR_TO_CTX) |
| 11296 | mark_reg_known_zero(env, regs, i); |
| 11297 | else if (regs[i].type == SCALAR_VALUE) |
| 11298 | mark_reg_unknown(env, regs, i); |
| 11299 | } |
| 11300 | } else { |
| 11301 | /* 1st arg to a function */ |
| 11302 | regs[BPF_REG_1].type = PTR_TO_CTX; |
| 11303 | mark_reg_known_zero(env, regs, BPF_REG_1); |
| 11304 | ret = btf_check_func_arg_match(env, subprog, regs); |
| 11305 | if (ret == -EFAULT) |
| 11306 | /* unlikely verifier bug. abort. |
| 11307 | * ret == 0 and ret < 0 are sadly acceptable for |
| 11308 | * main() function due to backward compatibility. |
| 11309 | * Like socket filter program may be written as: |
| 11310 | * int bpf_prog(struct pt_regs *ctx) |
| 11311 | * and never dereference that ctx in the program. |
| 11312 | * 'struct pt_regs' is a type mismatch for socket |
| 11313 | * filter that should be using 'struct __sk_buff'. |
| 11314 | */ |
| 11315 | goto out; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11316 | } |
| 11317 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11318 | ret = do_check(env); |
| 11319 | out: |
Alexei Starovoitov | f59bbfc | 2020-01-21 18:41:38 -0800 | [diff] [blame] | 11320 | /* check for NULL is necessary, since cur_state can be freed inside |
| 11321 | * do_check() under memory pressure. |
| 11322 | */ |
| 11323 | if (env->cur_state) { |
| 11324 | free_verifier_state(env->cur_state, true); |
| 11325 | env->cur_state = NULL; |
| 11326 | } |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 11327 | while (!pop_stack(env, NULL, NULL, false)); |
| 11328 | if (!ret && pop_log) |
| 11329 | bpf_vlog_reset(&env->log, 0); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11330 | free_states(env); |
| 11331 | if (ret) |
| 11332 | /* clean aux data in case subprog was rejected */ |
| 11333 | sanitize_insn_aux_data(env); |
| 11334 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11335 | } |
| 11336 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11337 | /* Verify all global functions in a BPF program one by one based on their BTF. |
| 11338 | * All global functions must pass verification. Otherwise the whole program is rejected. |
| 11339 | * Consider: |
| 11340 | * int bar(int); |
| 11341 | * int foo(int f) |
| 11342 | * { |
| 11343 | * return bar(f); |
| 11344 | * } |
| 11345 | * int bar(int b) |
| 11346 | * { |
| 11347 | * ... |
| 11348 | * } |
| 11349 | * foo() will be verified first for R1=any_scalar_value. During verification it |
| 11350 | * will be assumed that bar() already verified successfully and call to bar() |
| 11351 | * from foo() will be checked for type match only. Later bar() will be verified |
| 11352 | * independently to check that it's safe for R1=any_scalar_value. |
| 11353 | */ |
| 11354 | static int do_check_subprogs(struct bpf_verifier_env *env) |
| 11355 | { |
| 11356 | struct bpf_prog_aux *aux = env->prog->aux; |
| 11357 | int i, ret; |
| 11358 | |
| 11359 | if (!aux->func_info) |
| 11360 | return 0; |
| 11361 | |
| 11362 | for (i = 1; i < env->subprog_cnt; i++) { |
| 11363 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) |
| 11364 | continue; |
| 11365 | env->insn_idx = env->subprog_info[i].start; |
| 11366 | WARN_ON_ONCE(env->insn_idx == 0); |
| 11367 | ret = do_check_common(env, i); |
| 11368 | if (ret) { |
| 11369 | return ret; |
| 11370 | } else if (env->log.level & BPF_LOG_LEVEL) { |
| 11371 | verbose(env, |
| 11372 | "Func#%d is safe for any args that match its prototype\n", |
| 11373 | i); |
| 11374 | } |
| 11375 | } |
| 11376 | return 0; |
| 11377 | } |
| 11378 | |
| 11379 | static int do_check_main(struct bpf_verifier_env *env) |
| 11380 | { |
| 11381 | int ret; |
| 11382 | |
| 11383 | env->insn_idx = 0; |
| 11384 | ret = do_check_common(env, 0); |
| 11385 | if (!ret) |
| 11386 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
| 11387 | return ret; |
| 11388 | } |
| 11389 | |
| 11390 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11391 | static void print_verification_stats(struct bpf_verifier_env *env) |
| 11392 | { |
| 11393 | int i; |
| 11394 | |
| 11395 | if (env->log.level & BPF_LOG_STATS) { |
| 11396 | verbose(env, "verification time %lld usec\n", |
| 11397 | div_u64(env->verification_time, 1000)); |
| 11398 | verbose(env, "stack depth "); |
| 11399 | for (i = 0; i < env->subprog_cnt; i++) { |
| 11400 | u32 depth = env->subprog_info[i].stack_depth; |
| 11401 | |
| 11402 | verbose(env, "%d", depth); |
| 11403 | if (i + 1 < env->subprog_cnt) |
| 11404 | verbose(env, "+"); |
| 11405 | } |
| 11406 | verbose(env, "\n"); |
| 11407 | } |
| 11408 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " |
| 11409 | "total_states %d peak_states %d mark_read %d\n", |
| 11410 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, |
| 11411 | env->max_states_per_insn, env->total_states, |
| 11412 | env->peak_states, env->longest_mark_read_walk); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11413 | } |
| 11414 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 11415 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
| 11416 | { |
| 11417 | const struct btf_type *t, *func_proto; |
| 11418 | const struct bpf_struct_ops *st_ops; |
| 11419 | const struct btf_member *member; |
| 11420 | struct bpf_prog *prog = env->prog; |
| 11421 | u32 btf_id, member_idx; |
| 11422 | const char *mname; |
| 11423 | |
| 11424 | btf_id = prog->aux->attach_btf_id; |
| 11425 | st_ops = bpf_struct_ops_find(btf_id); |
| 11426 | if (!st_ops) { |
| 11427 | verbose(env, "attach_btf_id %u is not a supported struct\n", |
| 11428 | btf_id); |
| 11429 | return -ENOTSUPP; |
| 11430 | } |
| 11431 | |
| 11432 | t = st_ops->type; |
| 11433 | member_idx = prog->expected_attach_type; |
| 11434 | if (member_idx >= btf_type_vlen(t)) { |
| 11435 | verbose(env, "attach to invalid member idx %u of struct %s\n", |
| 11436 | member_idx, st_ops->name); |
| 11437 | return -EINVAL; |
| 11438 | } |
| 11439 | |
| 11440 | member = &btf_type_member(t)[member_idx]; |
| 11441 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); |
| 11442 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, |
| 11443 | NULL); |
| 11444 | if (!func_proto) { |
| 11445 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", |
| 11446 | mname, member_idx, st_ops->name); |
| 11447 | return -EINVAL; |
| 11448 | } |
| 11449 | |
| 11450 | if (st_ops->check_member) { |
| 11451 | int err = st_ops->check_member(t, member); |
| 11452 | |
| 11453 | if (err) { |
| 11454 | verbose(env, "attach to unsupported member %s of struct %s\n", |
| 11455 | mname, st_ops->name); |
| 11456 | return err; |
| 11457 | } |
| 11458 | } |
| 11459 | |
| 11460 | prog->aux->attach_func_proto = func_proto; |
| 11461 | prog->aux->attach_func_name = mname; |
| 11462 | env->ops = st_ops->verifier_ops; |
| 11463 | |
| 11464 | return 0; |
| 11465 | } |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11466 | #define SECURITY_PREFIX "security_" |
| 11467 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11468 | 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] | 11469 | { |
KP Singh | 6919175 | 2020-03-05 21:49:55 +0100 | [diff] [blame] | 11470 | if (within_error_injection_list(addr) || |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11471 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11472 | return 0; |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11473 | |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11474 | return -EINVAL; |
| 11475 | } |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 11476 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11477 | /* non exhaustive list of sleepable bpf_lsm_*() functions */ |
| 11478 | BTF_SET_START(btf_sleepable_lsm_hooks) |
| 11479 | #ifdef CONFIG_BPF_LSM |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11480 | BTF_ID(func, bpf_lsm_bprm_committed_creds) |
Alexei Starovoitov | 29523c5 | 2020-08-31 09:31:32 -0700 | [diff] [blame] | 11481 | #else |
| 11482 | BTF_ID_UNUSED |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11483 | #endif |
| 11484 | BTF_SET_END(btf_sleepable_lsm_hooks) |
| 11485 | |
| 11486 | static int check_sleepable_lsm_hook(u32 btf_id) |
| 11487 | { |
| 11488 | return btf_id_set_contains(&btf_sleepable_lsm_hooks, btf_id); |
| 11489 | } |
| 11490 | |
| 11491 | /* list of non-sleepable functions that are otherwise on |
| 11492 | * ALLOW_ERROR_INJECTION list |
| 11493 | */ |
| 11494 | BTF_SET_START(btf_non_sleepable_error_inject) |
| 11495 | /* Three functions below can be called from sleepable and non-sleepable context. |
| 11496 | * Assume non-sleepable from bpf safety point of view. |
| 11497 | */ |
| 11498 | BTF_ID(func, __add_to_page_cache_locked) |
| 11499 | BTF_ID(func, should_fail_alloc_page) |
| 11500 | BTF_ID(func, should_failslab) |
| 11501 | BTF_SET_END(btf_non_sleepable_error_inject) |
| 11502 | |
| 11503 | static int check_non_sleepable_error_inject(u32 btf_id) |
| 11504 | { |
| 11505 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); |
| 11506 | } |
| 11507 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11508 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
| 11509 | const struct bpf_prog *prog, |
| 11510 | const struct bpf_prog *tgt_prog, |
| 11511 | u32 btf_id, |
| 11512 | struct bpf_attach_target_info *tgt_info) |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11513 | { |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11514 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11515 | const char prefix[] = "btf_trace_"; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11516 | int ret = 0, subprog = -1, i; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11517 | const struct btf_type *t; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11518 | bool conservative = true; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11519 | const char *tname; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11520 | struct btf *btf; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11521 | long addr = 0; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11522 | |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11523 | if (!btf_id) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11524 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11525 | return -EINVAL; |
| 11526 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11527 | btf = tgt_prog ? tgt_prog->aux->btf : btf_vmlinux; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11528 | if (!btf) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11529 | bpf_log(log, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11530 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
| 11531 | return -EINVAL; |
| 11532 | } |
| 11533 | t = btf_type_by_id(btf, btf_id); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11534 | if (!t) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11535 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11536 | return -EINVAL; |
| 11537 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11538 | tname = btf_name_by_offset(btf, t->name_off); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11539 | if (!tname) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11540 | 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] | 11541 | return -EINVAL; |
| 11542 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11543 | if (tgt_prog) { |
| 11544 | struct bpf_prog_aux *aux = tgt_prog->aux; |
| 11545 | |
| 11546 | for (i = 0; i < aux->func_info_cnt; i++) |
| 11547 | if (aux->func_info[i].type_id == btf_id) { |
| 11548 | subprog = i; |
| 11549 | break; |
| 11550 | } |
| 11551 | if (subprog == -1) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11552 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11553 | return -EINVAL; |
| 11554 | } |
| 11555 | conservative = aux->func_info_aux[subprog].unreliable; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11556 | if (prog_extension) { |
| 11557 | if (conservative) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11558 | bpf_log(log, |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11559 | "Cannot replace static functions\n"); |
| 11560 | return -EINVAL; |
| 11561 | } |
| 11562 | if (!prog->jit_requested) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11563 | bpf_log(log, |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11564 | "Extension programs should be JITed\n"); |
| 11565 | return -EINVAL; |
| 11566 | } |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11567 | } |
| 11568 | if (!tgt_prog->jited) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11569 | bpf_log(log, "Can attach to only JITed progs\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11570 | return -EINVAL; |
| 11571 | } |
| 11572 | if (tgt_prog->type == prog->type) { |
| 11573 | /* Cannot fentry/fexit another fentry/fexit program. |
| 11574 | * Cannot attach program extension to another extension. |
| 11575 | * It's ok to attach fentry/fexit to extension program. |
| 11576 | */ |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11577 | bpf_log(log, "Cannot recursively attach\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11578 | return -EINVAL; |
| 11579 | } |
| 11580 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && |
| 11581 | prog_extension && |
| 11582 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || |
| 11583 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { |
| 11584 | /* Program extensions can extend all program types |
| 11585 | * except fentry/fexit. The reason is the following. |
| 11586 | * The fentry/fexit programs are used for performance |
| 11587 | * analysis, stats and can be attached to any program |
| 11588 | * type except themselves. When extension program is |
| 11589 | * replacing XDP function it is necessary to allow |
| 11590 | * performance analysis of all functions. Both original |
| 11591 | * XDP program and its program extension. Hence |
| 11592 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is |
| 11593 | * allowed. If extending of fentry/fexit was allowed it |
| 11594 | * would be possible to create long call chain |
| 11595 | * fentry->extension->fentry->extension beyond |
| 11596 | * reasonable stack size. Hence extending fentry is not |
| 11597 | * allowed. |
| 11598 | */ |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11599 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11600 | return -EINVAL; |
| 11601 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11602 | } else { |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11603 | if (prog_extension) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11604 | bpf_log(log, "Cannot replace kernel functions\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11605 | return -EINVAL; |
| 11606 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11607 | } |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11608 | |
| 11609 | switch (prog->expected_attach_type) { |
| 11610 | case BPF_TRACE_RAW_TP: |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11611 | if (tgt_prog) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11612 | bpf_log(log, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11613 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
| 11614 | return -EINVAL; |
| 11615 | } |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11616 | if (!btf_type_is_typedef(t)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11617 | 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] | 11618 | btf_id); |
| 11619 | return -EINVAL; |
| 11620 | } |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11621 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11622 | 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] | 11623 | btf_id, tname); |
| 11624 | return -EINVAL; |
| 11625 | } |
| 11626 | tname += sizeof(prefix) - 1; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11627 | t = btf_type_by_id(btf, t->type); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11628 | if (!btf_type_is_ptr(t)) |
| 11629 | /* should never happen in valid vmlinux build */ |
| 11630 | return -EINVAL; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11631 | t = btf_type_by_id(btf, t->type); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11632 | if (!btf_type_is_func_proto(t)) |
| 11633 | /* should never happen in valid vmlinux build */ |
| 11634 | return -EINVAL; |
| 11635 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11636 | break; |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 11637 | case BPF_TRACE_ITER: |
| 11638 | if (!btf_type_is_func(t)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11639 | bpf_log(log, "attach_btf_id %u is not a function\n", |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 11640 | btf_id); |
| 11641 | return -EINVAL; |
| 11642 | } |
| 11643 | t = btf_type_by_id(btf, t->type); |
| 11644 | if (!btf_type_is_func_proto(t)) |
| 11645 | return -EINVAL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11646 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
| 11647 | if (ret) |
| 11648 | return ret; |
| 11649 | break; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11650 | default: |
| 11651 | if (!prog_extension) |
| 11652 | return -EINVAL; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 11653 | fallthrough; |
KP Singh | ae24082 | 2020-03-04 20:18:49 +0100 | [diff] [blame] | 11654 | case BPF_MODIFY_RETURN: |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 11655 | case BPF_LSM_MAC: |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11656 | case BPF_TRACE_FENTRY: |
| 11657 | case BPF_TRACE_FEXIT: |
| 11658 | if (!btf_type_is_func(t)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11659 | bpf_log(log, "attach_btf_id %u is not a function\n", |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11660 | btf_id); |
| 11661 | return -EINVAL; |
| 11662 | } |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11663 | if (prog_extension && |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11664 | btf_check_type_match(log, prog, btf, t)) |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11665 | return -EINVAL; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11666 | t = btf_type_by_id(btf, t->type); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11667 | if (!btf_type_is_func_proto(t)) |
| 11668 | return -EINVAL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11669 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 11670 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
| 11671 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || |
| 11672 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) |
| 11673 | return -EINVAL; |
| 11674 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11675 | if (tgt_prog && conservative) |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11676 | t = NULL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11677 | |
| 11678 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11679 | if (ret < 0) |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11680 | return ret; |
| 11681 | |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11682 | if (tgt_prog) { |
Yonghong Song | e9eeec5 | 2019-12-04 17:06:06 -0800 | [diff] [blame] | 11683 | if (subprog == 0) |
| 11684 | addr = (long) tgt_prog->bpf_func; |
| 11685 | else |
| 11686 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11687 | } else { |
| 11688 | addr = kallsyms_lookup_name(tname); |
| 11689 | if (!addr) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11690 | bpf_log(log, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11691 | "The address of function %s cannot be found\n", |
| 11692 | tname); |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11693 | return -ENOENT; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11694 | } |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11695 | } |
Alexei Starovoitov | 18644ce | 2020-05-28 21:38:36 -0700 | [diff] [blame] | 11696 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11697 | if (prog->aux->sleepable) { |
| 11698 | ret = -EINVAL; |
| 11699 | switch (prog->type) { |
| 11700 | case BPF_PROG_TYPE_TRACING: |
| 11701 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are |
| 11702 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. |
| 11703 | */ |
| 11704 | if (!check_non_sleepable_error_inject(btf_id) && |
| 11705 | within_error_injection_list(addr)) |
| 11706 | ret = 0; |
| 11707 | break; |
| 11708 | case BPF_PROG_TYPE_LSM: |
| 11709 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. |
| 11710 | * Only some of them are sleepable. |
| 11711 | */ |
| 11712 | if (check_sleepable_lsm_hook(btf_id)) |
| 11713 | ret = 0; |
| 11714 | break; |
| 11715 | default: |
| 11716 | break; |
| 11717 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11718 | if (ret) { |
| 11719 | bpf_log(log, "%s is not sleepable\n", tname); |
| 11720 | return ret; |
| 11721 | } |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11722 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
Toke Høiland-Jørgensen | 1af9270 | 2020-09-25 23:25:00 +0200 | [diff] [blame] | 11723 | if (tgt_prog) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 11724 | 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] | 11725 | return -EINVAL; |
Toke Høiland-Jørgensen | 1af9270 | 2020-09-25 23:25:00 +0200 | [diff] [blame] | 11726 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11727 | ret = check_attach_modify_return(addr, tname); |
| 11728 | if (ret) { |
| 11729 | bpf_log(log, "%s() is not modifiable\n", tname); |
| 11730 | return ret; |
| 11731 | } |
Alexei Starovoitov | 18644ce | 2020-05-28 21:38:36 -0700 | [diff] [blame] | 11732 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11733 | |
| 11734 | break; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11735 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11736 | tgt_info->tgt_addr = addr; |
| 11737 | tgt_info->tgt_name = tname; |
| 11738 | tgt_info->tgt_type = t; |
| 11739 | return 0; |
| 11740 | } |
| 11741 | |
| 11742 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
| 11743 | { |
| 11744 | struct bpf_prog *prog = env->prog; |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 11745 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11746 | struct bpf_attach_target_info tgt_info = {}; |
| 11747 | u32 btf_id = prog->aux->attach_btf_id; |
| 11748 | struct bpf_trampoline *tr; |
| 11749 | int ret; |
| 11750 | u64 key; |
| 11751 | |
| 11752 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && |
| 11753 | prog->type != BPF_PROG_TYPE_LSM) { |
| 11754 | verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n"); |
| 11755 | return -EINVAL; |
| 11756 | } |
| 11757 | |
| 11758 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) |
| 11759 | return check_struct_ops_btf_id(env); |
| 11760 | |
| 11761 | if (prog->type != BPF_PROG_TYPE_TRACING && |
| 11762 | prog->type != BPF_PROG_TYPE_LSM && |
| 11763 | prog->type != BPF_PROG_TYPE_EXT) |
| 11764 | return 0; |
| 11765 | |
| 11766 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); |
| 11767 | if (ret) |
| 11768 | return ret; |
| 11769 | |
| 11770 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 11771 | /* to make freplace equivalent to their targets, they need to |
| 11772 | * inherit env->ops and expected_attach_type for the rest of the |
| 11773 | * verification |
| 11774 | */ |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11775 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
| 11776 | prog->expected_attach_type = tgt_prog->expected_attach_type; |
| 11777 | } |
| 11778 | |
| 11779 | /* store info about the attachment target that will be used later */ |
| 11780 | prog->aux->attach_func_proto = tgt_info.tgt_type; |
| 11781 | prog->aux->attach_func_name = tgt_info.tgt_name; |
| 11782 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 11783 | if (tgt_prog) { |
| 11784 | prog->aux->saved_dst_prog_type = tgt_prog->type; |
| 11785 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; |
| 11786 | } |
| 11787 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11788 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
| 11789 | prog->aux->attach_btf_trace = true; |
| 11790 | return 0; |
| 11791 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { |
| 11792 | if (!bpf_iter_prog_supported(prog)) |
| 11793 | return -EINVAL; |
| 11794 | return 0; |
| 11795 | } |
| 11796 | |
| 11797 | if (prog->type == BPF_PROG_TYPE_LSM) { |
| 11798 | ret = bpf_lsm_verify_prog(&env->log, prog); |
| 11799 | if (ret < 0) |
| 11800 | return ret; |
| 11801 | } |
| 11802 | |
| 11803 | key = bpf_trampoline_compute_key(tgt_prog, btf_id); |
| 11804 | tr = bpf_trampoline_get(key, &tgt_info); |
| 11805 | if (!tr) |
| 11806 | return -ENOMEM; |
| 11807 | |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 11808 | prog->aux->dst_trampoline = tr; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 11809 | return 0; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11810 | } |
| 11811 | |
Alan Maguire | 76654e6 | 2020-09-28 12:31:03 +0100 | [diff] [blame] | 11812 | struct btf *bpf_get_btf_vmlinux(void) |
| 11813 | { |
| 11814 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { |
| 11815 | mutex_lock(&bpf_verifier_lock); |
| 11816 | if (!btf_vmlinux) |
| 11817 | btf_vmlinux = btf_parse_vmlinux(); |
| 11818 | mutex_unlock(&bpf_verifier_lock); |
| 11819 | } |
| 11820 | return btf_vmlinux; |
| 11821 | } |
| 11822 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 11823 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
| 11824 | union bpf_attr __user *uattr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 11825 | { |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11826 | u64 start_time = ktime_get_ns(); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11827 | struct bpf_verifier_env *env; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 11828 | struct bpf_verifier_log *log; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 11829 | int i, len, ret = -EINVAL; |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 11830 | bool is_priv; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 11831 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 11832 | /* no program is valid */ |
| 11833 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 11834 | return -EINVAL; |
| 11835 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11836 | /* '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] | 11837 | * allocate/free it every time bpf_check() is called |
| 11838 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11839 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11840 | if (!env) |
| 11841 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11842 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11843 | |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 11844 | len = (*prog)->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 11845 | env->insn_aux_data = |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 11846 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11847 | ret = -ENOMEM; |
| 11848 | if (!env->insn_aux_data) |
| 11849 | goto err_free_env; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 11850 | for (i = 0; i < len; i++) |
| 11851 | env->insn_aux_data[i].orig_idx = i; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11852 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 11853 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 11854 | is_priv = bpf_capable(); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11855 | |
Alan Maguire | 76654e6 | 2020-09-28 12:31:03 +0100 | [diff] [blame] | 11856 | bpf_get_btf_vmlinux(); |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 11857 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11858 | /* grab the mutex to protect few globals used by verifier */ |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 11859 | if (!is_priv) |
| 11860 | mutex_lock(&bpf_verifier_lock); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11861 | |
| 11862 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 11863 | /* user requested verbose verifier output |
| 11864 | * and supplied buffer to store the verification trace |
| 11865 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 11866 | log->level = attr->log_level; |
| 11867 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 11868 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11869 | |
| 11870 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 11871 | /* log attributes have to be sane */ |
Alexei Starovoitov | 7a9f5c6 | 2019-04-01 21:27:46 -0700 | [diff] [blame] | 11872 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11873 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11874 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11875 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 11876 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 11877 | if (IS_ERR(btf_vmlinux)) { |
| 11878 | /* Either gcc or pahole or kernel are broken. */ |
| 11879 | verbose(env, "in-kernel BTF is malformed\n"); |
| 11880 | ret = PTR_ERR(btf_vmlinux); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11881 | goto skip_full_check; |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 11882 | } |
| 11883 | |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 11884 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 11885 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 11886 | env->strict_alignment = true; |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 11887 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
| 11888 | env->strict_alignment = false; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11889 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 11890 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 11891 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 11892 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
| 11893 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); |
| 11894 | env->bpf_capable = bpf_capable(); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 11895 | |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 11896 | if (is_priv) |
| 11897 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; |
| 11898 | |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 11899 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Quentin Monnet | a40a263 | 2018-11-09 13:03:31 +0000 | [diff] [blame] | 11900 | ret = bpf_prog_offload_verifier_prep(env->prog); |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 11901 | if (ret) |
| 11902 | goto skip_full_check; |
| 11903 | } |
| 11904 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 11905 | env->explored_states = kvcalloc(state_htab_size(env), |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11906 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11907 | GFP_USER); |
| 11908 | ret = -ENOMEM; |
| 11909 | if (!env->explored_states) |
| 11910 | goto skip_full_check; |
| 11911 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 11912 | ret = check_subprogs(env); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 11913 | if (ret < 0) |
| 11914 | goto skip_full_check; |
| 11915 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 11916 | ret = check_btf_info(env, attr, uattr); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 11917 | if (ret < 0) |
| 11918 | goto skip_full_check; |
| 11919 | |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11920 | ret = check_attach_btf_id(env); |
| 11921 | if (ret) |
| 11922 | goto skip_full_check; |
| 11923 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11924 | ret = resolve_pseudo_ldimm64(env); |
| 11925 | if (ret < 0) |
| 11926 | goto skip_full_check; |
| 11927 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 11928 | ret = check_cfg(env); |
| 11929 | if (ret < 0) |
| 11930 | goto skip_full_check; |
| 11931 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11932 | ret = do_check_subprogs(env); |
| 11933 | ret = ret ?: do_check_main(env); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11934 | |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 11935 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 11936 | ret = bpf_prog_offload_finalize(env); |
| 11937 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11938 | skip_full_check: |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11939 | kvfree(env->explored_states); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11940 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11941 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 11942 | ret = check_max_stack_depth(env); |
| 11943 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 11944 | /* instruction rewrites happen after this point */ |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 11945 | if (is_priv) { |
| 11946 | if (ret == 0) |
| 11947 | opt_hard_wire_dead_code_branches(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 11948 | if (ret == 0) |
| 11949 | ret = opt_remove_dead_code(env); |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 11950 | if (ret == 0) |
| 11951 | ret = opt_remove_nops(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 11952 | } else { |
| 11953 | if (ret == 0) |
| 11954 | sanitize_dead_code(env); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 11955 | } |
| 11956 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 11957 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11958 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 11959 | ret = convert_ctx_accesses(env); |
| 11960 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 11961 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11962 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 11963 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 11964 | /* do 32-bit optimization after insn patching has done so those patched |
| 11965 | * insns could be handled correctly. |
| 11966 | */ |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 11967 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
| 11968 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); |
| 11969 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret |
| 11970 | : false; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 11971 | } |
| 11972 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 11973 | if (ret == 0) |
| 11974 | ret = fixup_call_args(env); |
| 11975 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11976 | env->verification_time = ktime_get_ns() - start_time; |
| 11977 | print_verification_stats(env); |
| 11978 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 11979 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11980 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 11981 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11982 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 11983 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11984 | } |
| 11985 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11986 | if (ret == 0 && env->used_map_cnt) { |
| 11987 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11988 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 11989 | sizeof(env->used_maps[0]), |
| 11990 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11991 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11992 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11993 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 11994 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11995 | } |
| 11996 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11997 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11998 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11999 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12000 | |
| 12001 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 12002 | * bpf_ld_imm64 instructions |
| 12003 | */ |
| 12004 | convert_pseudo_ld_imm64(env); |
| 12005 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 12006 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 12007 | if (ret == 0) |
| 12008 | adjust_btf_func(env); |
| 12009 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 12010 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12011 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12012 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 12013 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12014 | */ |
| 12015 | release_maps(env); |
Toke Høiland-Jørgensen | 03f87c0 | 2020-04-24 15:34:27 +0200 | [diff] [blame] | 12016 | |
| 12017 | /* extension progs temporarily inherit the attach_type of their targets |
| 12018 | for verification purposes, so set it back to zero before returning |
| 12019 | */ |
| 12020 | if (env->prog->type == BPF_PROG_TYPE_EXT) |
| 12021 | env->prog->expected_attach_type = 0; |
| 12022 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12023 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12024 | err_unlock: |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 12025 | if (!is_priv) |
| 12026 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12027 | vfree(env->insn_aux_data); |
| 12028 | err_free_env: |
| 12029 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 12030 | return ret; |
| 12031 | } |