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> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 22 | |
Jakub Kicinski | f4ac7e0 | 2017-10-09 10:30:12 -0700 | [diff] [blame] | 23 | #include "disasm.h" |
| 24 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 25 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
Alexei Starovoitov | 91cc1a9 | 2019-11-14 10:57:15 -0800 | [diff] [blame] | 26 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 27 | [_id] = & _name ## _verifier_ops, |
| 28 | #define BPF_MAP_TYPE(_id, _ops) |
| 29 | #include <linux/bpf_types.h> |
| 30 | #undef BPF_PROG_TYPE |
| 31 | #undef BPF_MAP_TYPE |
| 32 | }; |
| 33 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 34 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 35 | * instruction by instruction and updates register/stack state. |
| 36 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 37 | * |
| 38 | * The first pass is depth-first-search to check that the program is a DAG. |
| 39 | * It rejects the following programs: |
| 40 | * - larger than BPF_MAXINSNS insns |
| 41 | * - if loop is present (detected via back-edge) |
| 42 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 43 | * - out of bounds or malformed jumps |
| 44 | * The second pass is all possible path descent from the 1st insn. |
| 45 | * Since it's analyzing all pathes through the program, the length of the |
Gary Lin | eba38a9 | 2017-03-01 16:25:51 +0800 | [diff] [blame] | 46 | * 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] | 47 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 48 | * Number of 'branches to be analyzed' is limited to 1k |
| 49 | * |
| 50 | * On entry to each instruction, each register has a type, and the instruction |
| 51 | * changes the types of the registers depending on instruction semantics. |
| 52 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 53 | * copied to R1. |
| 54 | * |
| 55 | * All registers are 64-bit. |
| 56 | * R0 - return register |
| 57 | * R1-R5 argument passing registers |
| 58 | * R6-R9 callee saved registers |
| 59 | * R10 - frame pointer read-only |
| 60 | * |
| 61 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 62 | * and has type PTR_TO_CTX. |
| 63 | * |
| 64 | * Verifier tracks arithmetic operations on pointers in case: |
| 65 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 66 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 67 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 68 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 69 | * that it wants to construct a pointer to some element within stack. |
| 70 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 71 | * (and -20 constant is saved for further stack bounds checking). |
| 72 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 73 | * |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 74 | * Most of the time the registers have SCALAR_VALUE type, which |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 75 | * 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] | 76 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 77 | * |
| 78 | * When verifier sees load or store instructions the type of base register |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 79 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
| 80 | * four pointer types recognized by check_mem_access() function. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 81 | * |
| 82 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 83 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 84 | * |
| 85 | * registers used to pass values to function calls are checked against |
| 86 | * function argument constraints. |
| 87 | * |
| 88 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 89 | * It means that the register type passed to this function must be |
| 90 | * PTR_TO_STACK and it will be used inside the function as |
| 91 | * 'pointer to map element key' |
| 92 | * |
| 93 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 94 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 95 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 96 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 97 | * |
| 98 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 99 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 100 | * 2nd argument should be a pointer to stack, which will be used inside |
| 101 | * the helper function as a pointer to map element key. |
| 102 | * |
| 103 | * On the kernel side the helper function looks like: |
| 104 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 105 | * { |
| 106 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 107 | * void *key = (void *) (unsigned long) r2; |
| 108 | * void *value; |
| 109 | * |
| 110 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 111 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 112 | * the stack of eBPF program. |
| 113 | * } |
| 114 | * |
| 115 | * Corresponding eBPF program may look like: |
| 116 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 117 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 118 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 119 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 120 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 121 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 122 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 123 | * |
| 124 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 125 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 126 | * and were initialized prior to this call. |
| 127 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 128 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 129 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 130 | * returns ether pointer to map value or NULL. |
| 131 | * |
| 132 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 133 | * insn, the register holding that pointer in the true branch changes state to |
| 134 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 135 | * branch. See check_cond_jmp_op(). |
| 136 | * |
| 137 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 138 | * are set to NOT_INIT to indicate that they are no longer readable. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 139 | * |
| 140 | * The following reference types represent a potential reference to a kernel |
| 141 | * resource which, after first being allocated, must be checked and freed by |
| 142 | * the BPF program: |
| 143 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET |
| 144 | * |
| 145 | * When the verifier sees a helper call return a reference type, it allocates a |
| 146 | * pointer id for the reference and stores it in the current function state. |
| 147 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into |
| 148 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type |
| 149 | * passes through a NULL-check conditional. For the branch wherein the state is |
| 150 | * changed to CONST_IMM, the verifier releases the reference. |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 151 | * |
| 152 | * For each helper function that allocates a reference, such as |
| 153 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as |
| 154 | * bpf_sk_release(). When a reference type passes into the release function, |
| 155 | * the verifier also releases the reference. If any unchecked or unreleased |
| 156 | * reference remains at the end of the program, the verifier rejects it. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 157 | */ |
| 158 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 159 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 160 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 161 | /* verifer state is 'st' |
| 162 | * before processing instruction 'insn_idx' |
| 163 | * and after processing instruction 'prev_insn_idx' |
| 164 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 165 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 166 | int insn_idx; |
| 167 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 168 | struct bpf_verifier_stack_elem *next; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 171 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 172 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 173 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 174 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
| 175 | #define BPF_MAP_KEY_SEEN (1ULL << 62) |
| 176 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 177 | #define BPF_MAP_PTR_UNPRIV 1UL |
| 178 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ |
| 179 | POISON_POINTER_DELTA)) |
| 180 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) |
| 181 | |
| 182 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
| 183 | { |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 184 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) |
| 188 | { |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 189 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, |
| 193 | const struct bpf_map *map, bool unpriv) |
| 194 | { |
| 195 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); |
| 196 | unpriv |= bpf_map_ptr_unpriv(aux); |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 197 | aux->map_ptr_state = (unsigned long)map | |
| 198 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); |
| 199 | } |
| 200 | |
| 201 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) |
| 202 | { |
| 203 | return aux->map_key_state & BPF_MAP_KEY_POISON; |
| 204 | } |
| 205 | |
| 206 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) |
| 207 | { |
| 208 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); |
| 209 | } |
| 210 | |
| 211 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) |
| 212 | { |
| 213 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); |
| 214 | } |
| 215 | |
| 216 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) |
| 217 | { |
| 218 | bool poisoned = bpf_map_key_poisoned(aux); |
| 219 | |
| 220 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | |
| 221 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 222 | } |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 223 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 224 | struct bpf_call_arg_meta { |
| 225 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 226 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 227 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 228 | int regno; |
| 229 | int access_size; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 230 | s64 msize_smax_value; |
| 231 | u64 msize_umax_value; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 232 | int ref_obj_id; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 233 | int func_id; |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 234 | u32 btf_id; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 235 | }; |
| 236 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 237 | struct btf *btf_vmlinux; |
| 238 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 239 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 240 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 241 | static const struct bpf_line_info * |
| 242 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) |
| 243 | { |
| 244 | const struct bpf_line_info *linfo; |
| 245 | const struct bpf_prog *prog; |
| 246 | u32 i, nr_linfo; |
| 247 | |
| 248 | prog = env->prog; |
| 249 | nr_linfo = prog->aux->nr_linfo; |
| 250 | |
| 251 | if (!nr_linfo || insn_off >= prog->len) |
| 252 | return NULL; |
| 253 | |
| 254 | linfo = prog->aux->linfo; |
| 255 | for (i = 1; i < nr_linfo; i++) |
| 256 | if (insn_off < linfo[i].insn_off) |
| 257 | break; |
| 258 | |
| 259 | return &linfo[i - 1]; |
| 260 | } |
| 261 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 262 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
| 263 | va_list args) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 264 | { |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 265 | unsigned int n; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 266 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 267 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 268 | |
| 269 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 270 | "verifier log line truncated - local buffer too short\n"); |
| 271 | |
| 272 | n = min(log->len_total - log->len_used - 1, n); |
| 273 | log->kbuf[n] = '\0'; |
| 274 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 275 | if (log->level == BPF_LOG_KERNEL) { |
| 276 | pr_err("BPF:%s\n", log->kbuf); |
| 277 | return; |
| 278 | } |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 279 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 280 | log->len_used += n; |
| 281 | else |
| 282 | log->ubuf = NULL; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 283 | } |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 284 | |
| 285 | /* log_level controls verbosity level of eBPF verifier. |
| 286 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 287 | * so the user can figure out what's wrong with the program |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 288 | */ |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 289 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 290 | const char *fmt, ...) |
| 291 | { |
| 292 | va_list args; |
| 293 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 294 | if (!bpf_verifier_log_needed(&env->log)) |
| 295 | return; |
| 296 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 297 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 298 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 299 | va_end(args); |
| 300 | } |
| 301 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 302 | |
| 303 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
| 304 | { |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 305 | struct bpf_verifier_env *env = private_data; |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 306 | va_list args; |
| 307 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 308 | if (!bpf_verifier_log_needed(&env->log)) |
| 309 | return; |
| 310 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 311 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 312 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 313 | va_end(args); |
| 314 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 315 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 316 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
| 317 | const char *fmt, ...) |
| 318 | { |
| 319 | va_list args; |
| 320 | |
| 321 | if (!bpf_verifier_log_needed(log)) |
| 322 | return; |
| 323 | |
| 324 | va_start(args, fmt); |
| 325 | bpf_verifier_vlog(log, fmt, args); |
| 326 | va_end(args); |
| 327 | } |
| 328 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 329 | static const char *ltrim(const char *s) |
| 330 | { |
| 331 | while (isspace(*s)) |
| 332 | s++; |
| 333 | |
| 334 | return s; |
| 335 | } |
| 336 | |
| 337 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, |
| 338 | u32 insn_off, |
| 339 | const char *prefix_fmt, ...) |
| 340 | { |
| 341 | const struct bpf_line_info *linfo; |
| 342 | |
| 343 | if (!bpf_verifier_log_needed(&env->log)) |
| 344 | return; |
| 345 | |
| 346 | linfo = find_linfo(env, insn_off); |
| 347 | if (!linfo || linfo == env->prev_linfo) |
| 348 | return; |
| 349 | |
| 350 | if (prefix_fmt) { |
| 351 | va_list args; |
| 352 | |
| 353 | va_start(args, prefix_fmt); |
| 354 | bpf_verifier_vlog(&env->log, prefix_fmt, args); |
| 355 | va_end(args); |
| 356 | } |
| 357 | |
| 358 | verbose(env, "%s\n", |
| 359 | ltrim(btf_name_by_offset(env->prog->aux->btf, |
| 360 | linfo->line_off))); |
| 361 | |
| 362 | env->prev_linfo = linfo; |
| 363 | } |
| 364 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 365 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 366 | { |
| 367 | return type == PTR_TO_PACKET || |
| 368 | type == PTR_TO_PACKET_META; |
| 369 | } |
| 370 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 371 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
| 372 | { |
| 373 | return type == PTR_TO_SOCKET || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 374 | type == PTR_TO_SOCK_COMMON || |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 375 | type == PTR_TO_TCP_SOCK || |
| 376 | type == PTR_TO_XDP_SOCK; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 379 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
| 380 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 381 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 382 | type == PTR_TO_SOCKET_OR_NULL || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 383 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
| 384 | type == PTR_TO_TCP_SOCK_OR_NULL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 387 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
| 388 | { |
| 389 | return reg->type == PTR_TO_MAP_VALUE && |
| 390 | map_value_has_spin_lock(reg->map_ptr); |
| 391 | } |
| 392 | |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 393 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
| 394 | { |
| 395 | return type == PTR_TO_SOCKET || |
| 396 | type == PTR_TO_SOCKET_OR_NULL || |
| 397 | type == PTR_TO_TCP_SOCK || |
| 398 | type == PTR_TO_TCP_SOCK_OR_NULL; |
| 399 | } |
| 400 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 401 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 402 | { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 403 | return type == ARG_PTR_TO_SOCK_COMMON; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /* Determine whether the function releases some resources allocated by another |
| 407 | * function call. The first reference type argument will be assumed to be |
| 408 | * released by release_reference(). |
| 409 | */ |
| 410 | static bool is_release_function(enum bpf_func_id func_id) |
| 411 | { |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 412 | return func_id == BPF_FUNC_sk_release; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 415 | static bool is_acquire_function(enum bpf_func_id func_id) |
| 416 | { |
| 417 | return func_id == BPF_FUNC_sk_lookup_tcp || |
Lorenz Bauer | edbf8c0 | 2019-03-22 09:54:01 +0800 | [diff] [blame] | 418 | func_id == BPF_FUNC_sk_lookup_udp || |
| 419 | func_id == BPF_FUNC_skc_lookup_tcp; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 422 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
| 423 | { |
| 424 | return func_id == BPF_FUNC_tcp_sock || |
| 425 | func_id == BPF_FUNC_sk_fullsock; |
| 426 | } |
| 427 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 428 | /* string representation of 'enum bpf_reg_type' */ |
| 429 | static const char * const reg_type_str[] = { |
| 430 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 431 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 432 | [PTR_TO_CTX] = "ctx", |
| 433 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 434 | [PTR_TO_MAP_VALUE] = "map_value", |
| 435 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 436 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 437 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 438 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 439 | [PTR_TO_PACKET_END] = "pkt_end", |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 440 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 441 | [PTR_TO_SOCKET] = "sock", |
| 442 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 443 | [PTR_TO_SOCK_COMMON] = "sock_common", |
| 444 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 445 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
| 446 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 447 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 448 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 449 | [PTR_TO_BTF_ID] = "ptr_", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 450 | }; |
| 451 | |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 452 | static char slot_type_char[] = { |
| 453 | [STACK_INVALID] = '?', |
| 454 | [STACK_SPILL] = 'r', |
| 455 | [STACK_MISC] = 'm', |
| 456 | [STACK_ZERO] = '0', |
| 457 | }; |
| 458 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 459 | static void print_liveness(struct bpf_verifier_env *env, |
| 460 | enum bpf_reg_liveness live) |
| 461 | { |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 462 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 463 | verbose(env, "_"); |
| 464 | if (live & REG_LIVE_READ) |
| 465 | verbose(env, "r"); |
| 466 | if (live & REG_LIVE_WRITTEN) |
| 467 | verbose(env, "w"); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 468 | if (live & REG_LIVE_DONE) |
| 469 | verbose(env, "D"); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 472 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 473 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 474 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 475 | struct bpf_verifier_state *cur = env->cur_state; |
| 476 | |
| 477 | return cur->frame[reg->frameno]; |
| 478 | } |
| 479 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 480 | const char *kernel_type_name(u32 id) |
| 481 | { |
| 482 | return btf_name_by_offset(btf_vmlinux, |
| 483 | btf_type_by_id(btf_vmlinux, id)->name_off); |
| 484 | } |
| 485 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 486 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 487 | const struct bpf_func_state *state) |
| 488 | { |
| 489 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 490 | enum bpf_reg_type t; |
| 491 | int i; |
| 492 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 493 | if (state->frameno) |
| 494 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 495 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 496 | reg = &state->regs[i]; |
| 497 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 498 | if (t == NOT_INIT) |
| 499 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 500 | verbose(env, " R%d", i); |
| 501 | print_liveness(env, reg->live); |
| 502 | verbose(env, "=%s", reg_type_str[t]); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 503 | if (t == SCALAR_VALUE && reg->precise) |
| 504 | verbose(env, "P"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 505 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 506 | tnum_is_const(reg->var_off)) { |
| 507 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 508 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 509 | } else { |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 510 | if (t == PTR_TO_BTF_ID) |
| 511 | verbose(env, "%s", kernel_type_name(reg->btf_id)); |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 512 | verbose(env, "(id=%d", reg->id); |
| 513 | if (reg_type_may_be_refcounted_or_null(t)) |
| 514 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 515 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 516 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 517 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 518 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 519 | else if (t == CONST_PTR_TO_MAP || |
| 520 | t == PTR_TO_MAP_VALUE || |
| 521 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 522 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 523 | reg->map_ptr->key_size, |
| 524 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 525 | if (tnum_is_const(reg->var_off)) { |
| 526 | /* Typically an immediate SCALAR_VALUE, but |
| 527 | * could be a pointer whose offset is too big |
| 528 | * for reg->off |
| 529 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 530 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 531 | } else { |
| 532 | if (reg->smin_value != reg->umin_value && |
| 533 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 534 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 535 | (long long)reg->smin_value); |
| 536 | if (reg->smax_value != reg->umax_value && |
| 537 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 538 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 539 | (long long)reg->smax_value); |
| 540 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 541 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 542 | (unsigned long long)reg->umin_value); |
| 543 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 544 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 545 | (unsigned long long)reg->umax_value); |
| 546 | if (!tnum_is_unknown(reg->var_off)) { |
| 547 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 548 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 549 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 550 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 551 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 552 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 553 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 554 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 555 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 556 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 557 | char types_buf[BPF_REG_SIZE + 1]; |
| 558 | bool valid = false; |
| 559 | int j; |
| 560 | |
| 561 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 562 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 563 | valid = true; |
| 564 | types_buf[j] = slot_type_char[ |
| 565 | state->stack[i].slot_type[j]]; |
| 566 | } |
| 567 | types_buf[BPF_REG_SIZE] = 0; |
| 568 | if (!valid) |
| 569 | continue; |
| 570 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 571 | print_liveness(env, state->stack[i].spilled_ptr.live); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 572 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
| 573 | reg = &state->stack[i].spilled_ptr; |
| 574 | t = reg->type; |
| 575 | verbose(env, "=%s", reg_type_str[t]); |
| 576 | if (t == SCALAR_VALUE && reg->precise) |
| 577 | verbose(env, "P"); |
| 578 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) |
| 579 | verbose(env, "%lld", reg->var_off.value + reg->off); |
| 580 | } else { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 581 | verbose(env, "=%s", types_buf); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 582 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 583 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 584 | if (state->acquired_refs && state->refs[0].id) { |
| 585 | verbose(env, " refs=%d", state->refs[0].id); |
| 586 | for (i = 1; i < state->acquired_refs; i++) |
| 587 | if (state->refs[i].id) |
| 588 | verbose(env, ",%d", state->refs[i].id); |
| 589 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 590 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 593 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 594 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ |
| 595 | const struct bpf_func_state *src) \ |
| 596 | { \ |
| 597 | if (!src->FIELD) \ |
| 598 | return 0; \ |
| 599 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ |
| 600 | /* internal bug, make state invalid to reject the program */ \ |
| 601 | memset(dst, 0, sizeof(*dst)); \ |
| 602 | return -EFAULT; \ |
| 603 | } \ |
| 604 | memcpy(dst->FIELD, src->FIELD, \ |
| 605 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ |
| 606 | return 0; \ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 607 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 608 | /* copy_reference_state() */ |
| 609 | COPY_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 610 | /* copy_stack_state() */ |
| 611 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 612 | #undef COPY_STATE_FN |
| 613 | |
| 614 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 615 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ |
| 616 | bool copy_old) \ |
| 617 | { \ |
| 618 | u32 old_size = state->COUNT; \ |
| 619 | struct bpf_##NAME##_state *new_##FIELD; \ |
| 620 | int slot = size / SIZE; \ |
| 621 | \ |
| 622 | if (size <= old_size || !size) { \ |
| 623 | if (copy_old) \ |
| 624 | return 0; \ |
| 625 | state->COUNT = slot * SIZE; \ |
| 626 | if (!size && old_size) { \ |
| 627 | kfree(state->FIELD); \ |
| 628 | state->FIELD = NULL; \ |
| 629 | } \ |
| 630 | return 0; \ |
| 631 | } \ |
| 632 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ |
| 633 | GFP_KERNEL); \ |
| 634 | if (!new_##FIELD) \ |
| 635 | return -ENOMEM; \ |
| 636 | if (copy_old) { \ |
| 637 | if (state->FIELD) \ |
| 638 | memcpy(new_##FIELD, state->FIELD, \ |
| 639 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ |
| 640 | memset(new_##FIELD + old_size / SIZE, 0, \ |
| 641 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ |
| 642 | } \ |
| 643 | state->COUNT = slot * SIZE; \ |
| 644 | kfree(state->FIELD); \ |
| 645 | state->FIELD = new_##FIELD; \ |
| 646 | return 0; \ |
| 647 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 648 | /* realloc_reference_state() */ |
| 649 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 650 | /* realloc_stack_state() */ |
| 651 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 652 | #undef REALLOC_STATE_FN |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 653 | |
| 654 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 655 | * make it consume minimal amount of memory. check_stack_write() access from |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 656 | * the program calls into realloc_func_state() to grow the stack size. |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 657 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 658 | * which realloc_stack_state() copies over. It points to previous |
| 659 | * bpf_verifier_state which is never reallocated. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 660 | */ |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 661 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
| 662 | int refs_size, bool copy_old) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 663 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 664 | int err = realloc_reference_state(state, refs_size, copy_old); |
| 665 | if (err) |
| 666 | return err; |
| 667 | return realloc_stack_state(state, stack_size, copy_old); |
| 668 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 669 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 670 | /* Acquire a pointer id from the env and update the state->refs to include |
| 671 | * this new pointer reference. |
| 672 | * On success, returns a valid pointer id to associate with the register |
| 673 | * On failure, returns a negative errno. |
| 674 | */ |
| 675 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 676 | { |
| 677 | struct bpf_func_state *state = cur_func(env); |
| 678 | int new_ofs = state->acquired_refs; |
| 679 | int id, err; |
| 680 | |
| 681 | err = realloc_reference_state(state, state->acquired_refs + 1, true); |
| 682 | if (err) |
| 683 | return err; |
| 684 | id = ++env->id_gen; |
| 685 | state->refs[new_ofs].id = id; |
| 686 | state->refs[new_ofs].insn_idx = insn_idx; |
| 687 | |
| 688 | return id; |
| 689 | } |
| 690 | |
| 691 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 692 | 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] | 693 | { |
| 694 | int i, last_idx; |
| 695 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 696 | last_idx = state->acquired_refs - 1; |
| 697 | for (i = 0; i < state->acquired_refs; i++) { |
| 698 | if (state->refs[i].id == ptr_id) { |
| 699 | if (last_idx && i != last_idx) |
| 700 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 701 | sizeof(*state->refs)); |
| 702 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 703 | state->acquired_refs--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 704 | return 0; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 705 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 706 | } |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 707 | return -EINVAL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | static int transfer_reference_state(struct bpf_func_state *dst, |
| 711 | struct bpf_func_state *src) |
| 712 | { |
| 713 | int err = realloc_reference_state(dst, src->acquired_refs, false); |
| 714 | if (err) |
| 715 | return err; |
| 716 | err = copy_reference_state(dst, src); |
| 717 | if (err) |
| 718 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 719 | return 0; |
| 720 | } |
| 721 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 722 | static void free_func_state(struct bpf_func_state *state) |
| 723 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 724 | if (!state) |
| 725 | return; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 726 | kfree(state->refs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 727 | kfree(state->stack); |
| 728 | kfree(state); |
| 729 | } |
| 730 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 731 | static void clear_jmp_history(struct bpf_verifier_state *state) |
| 732 | { |
| 733 | kfree(state->jmp_history); |
| 734 | state->jmp_history = NULL; |
| 735 | state->jmp_history_cnt = 0; |
| 736 | } |
| 737 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 738 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 739 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 740 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 741 | int i; |
| 742 | |
| 743 | for (i = 0; i <= state->curframe; i++) { |
| 744 | free_func_state(state->frame[i]); |
| 745 | state->frame[i] = NULL; |
| 746 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 747 | clear_jmp_history(state); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 748 | if (free_self) |
| 749 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* copy verifier state from src to dst growing dst stack space |
| 753 | * when necessary to accommodate larger src stack |
| 754 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 755 | static int copy_func_state(struct bpf_func_state *dst, |
| 756 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 757 | { |
| 758 | int err; |
| 759 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 760 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
| 761 | false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 762 | if (err) |
| 763 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 764 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 765 | err = copy_reference_state(dst, src); |
| 766 | if (err) |
| 767 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 768 | return copy_stack_state(dst, src); |
| 769 | } |
| 770 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 771 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 772 | const struct bpf_verifier_state *src) |
| 773 | { |
| 774 | struct bpf_func_state *dst; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 775 | u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 776 | int i, err; |
| 777 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 778 | if (dst_state->jmp_history_cnt < src->jmp_history_cnt) { |
| 779 | kfree(dst_state->jmp_history); |
| 780 | dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER); |
| 781 | if (!dst_state->jmp_history) |
| 782 | return -ENOMEM; |
| 783 | } |
| 784 | memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz); |
| 785 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
| 786 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 787 | /* if dst has more stack frames then src frame, free them */ |
| 788 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 789 | free_func_state(dst_state->frame[i]); |
| 790 | dst_state->frame[i] = NULL; |
| 791 | } |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 792 | dst_state->speculative = src->speculative; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 793 | dst_state->curframe = src->curframe; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 794 | dst_state->active_spin_lock = src->active_spin_lock; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 795 | dst_state->branches = src->branches; |
| 796 | dst_state->parent = src->parent; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 797 | dst_state->first_insn_idx = src->first_insn_idx; |
| 798 | dst_state->last_insn_idx = src->last_insn_idx; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 799 | for (i = 0; i <= src->curframe; i++) { |
| 800 | dst = dst_state->frame[i]; |
| 801 | if (!dst) { |
| 802 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 803 | if (!dst) |
| 804 | return -ENOMEM; |
| 805 | dst_state->frame[i] = dst; |
| 806 | } |
| 807 | err = copy_func_state(dst, src->frame[i]); |
| 808 | if (err) |
| 809 | return err; |
| 810 | } |
| 811 | return 0; |
| 812 | } |
| 813 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 814 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
| 815 | { |
| 816 | while (st) { |
| 817 | u32 br = --st->branches; |
| 818 | |
| 819 | /* WARN_ON(br > 1) technically makes sense here, |
| 820 | * but see comment in push_stack(), hence: |
| 821 | */ |
| 822 | WARN_ONCE((int)br < 0, |
| 823 | "BUG update_branch_counts:branches_to_explore=%d\n", |
| 824 | br); |
| 825 | if (br) |
| 826 | break; |
| 827 | st = st->parent; |
| 828 | } |
| 829 | } |
| 830 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 831 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
| 832 | int *insn_idx) |
| 833 | { |
| 834 | struct bpf_verifier_state *cur = env->cur_state; |
| 835 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 836 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 837 | |
| 838 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 839 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 840 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 841 | if (cur) { |
| 842 | err = copy_verifier_state(cur, &head->st); |
| 843 | if (err) |
| 844 | return err; |
| 845 | } |
| 846 | if (insn_idx) |
| 847 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 848 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 849 | *prev_insn_idx = head->prev_insn_idx; |
| 850 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 851 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 852 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 853 | env->head = elem; |
| 854 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 855 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 858 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 859 | int insn_idx, int prev_insn_idx, |
| 860 | bool speculative) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 861 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 862 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 863 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 864 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 865 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 866 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 867 | if (!elem) |
| 868 | goto err; |
| 869 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 870 | elem->insn_idx = insn_idx; |
| 871 | elem->prev_insn_idx = prev_insn_idx; |
| 872 | elem->next = env->head; |
| 873 | env->head = elem; |
| 874 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 875 | err = copy_verifier_state(&elem->st, cur); |
| 876 | if (err) |
| 877 | goto err; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 878 | elem->st.speculative |= speculative; |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 879 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
| 880 | verbose(env, "The sequence of %d jumps is too complex.\n", |
| 881 | env->stack_size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 882 | goto err; |
| 883 | } |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 884 | if (elem->st.parent) { |
| 885 | ++elem->st.parent->branches; |
| 886 | /* WARN_ON(branches > 2) technically makes sense here, |
| 887 | * but |
| 888 | * 1. speculative states will bump 'branches' for non-branch |
| 889 | * instructions |
| 890 | * 2. is_state_visited() heuristics may decide not to create |
| 891 | * a new state for a sequence of branches and all such current |
| 892 | * and cloned states will be pointing to a single parent state |
| 893 | * which might have large 'branches' count. |
| 894 | */ |
| 895 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 896 | return &elem->st; |
| 897 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 898 | free_verifier_state(env->cur_state, true); |
| 899 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 900 | /* pop all elements and return */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 901 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 902 | return NULL; |
| 903 | } |
| 904 | |
| 905 | #define CALLER_SAVED_REGS 6 |
| 906 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 907 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 908 | }; |
| 909 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 910 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
| 911 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 912 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 913 | * known to have the value @imm. |
| 914 | */ |
| 915 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 916 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 917 | /* Clear id, off, and union(map_ptr, range) */ |
| 918 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 919 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 920 | reg->var_off = tnum_const(imm); |
| 921 | reg->smin_value = (s64)imm; |
| 922 | reg->smax_value = (s64)imm; |
| 923 | reg->umin_value = imm; |
| 924 | reg->umax_value = imm; |
| 925 | } |
| 926 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 927 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 928 | * used only on registers holding a pointer type. |
| 929 | */ |
| 930 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 931 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 932 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 933 | } |
| 934 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 935 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 936 | { |
| 937 | __mark_reg_known(reg, 0); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 938 | reg->type = SCALAR_VALUE; |
| 939 | } |
| 940 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 941 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 942 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 943 | { |
| 944 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 945 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 946 | /* Something bad happened, let's kill all regs */ |
| 947 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 948 | __mark_reg_not_init(regs + regno); |
| 949 | return; |
| 950 | } |
| 951 | __mark_reg_known_zero(regs + regno); |
| 952 | } |
| 953 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 954 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 955 | { |
| 956 | return type_is_pkt_pointer(reg->type); |
| 957 | } |
| 958 | |
| 959 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 960 | { |
| 961 | return reg_is_pkt_pointer(reg) || |
| 962 | reg->type == PTR_TO_PACKET_END; |
| 963 | } |
| 964 | |
| 965 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 966 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 967 | enum bpf_reg_type which) |
| 968 | { |
| 969 | /* The register can already have a range from prior markings. |
| 970 | * This is fine as long as it hasn't been advanced from its |
| 971 | * origin. |
| 972 | */ |
| 973 | return reg->type == which && |
| 974 | reg->id == 0 && |
| 975 | reg->off == 0 && |
| 976 | tnum_equals_const(reg->var_off, 0); |
| 977 | } |
| 978 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 979 | /* Attempts to improve min/max values based on var_off information */ |
| 980 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 981 | { |
| 982 | /* min signed is max(sign bit) | min(other bits) */ |
| 983 | reg->smin_value = max_t(s64, reg->smin_value, |
| 984 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 985 | /* max signed is min(sign bit) | max(other bits) */ |
| 986 | reg->smax_value = min_t(s64, reg->smax_value, |
| 987 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 988 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 989 | reg->umax_value = min(reg->umax_value, |
| 990 | reg->var_off.value | reg->var_off.mask); |
| 991 | } |
| 992 | |
| 993 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
| 994 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 995 | { |
| 996 | /* Learn sign from signed bounds. |
| 997 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 998 | * are the same, so combine. This works even in the negative case, e.g. |
| 999 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 1000 | */ |
| 1001 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 1002 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1003 | reg->umin_value); |
| 1004 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1005 | reg->umax_value); |
| 1006 | return; |
| 1007 | } |
| 1008 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 1009 | * boundary, so we must be careful. |
| 1010 | */ |
| 1011 | if ((s64)reg->umax_value >= 0) { |
| 1012 | /* Positive. We can't learn anything from the smin, but smax |
| 1013 | * is positive, hence safe. |
| 1014 | */ |
| 1015 | reg->smin_value = reg->umin_value; |
| 1016 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1017 | reg->umax_value); |
| 1018 | } else if ((s64)reg->umin_value < 0) { |
| 1019 | /* Negative. We can't learn anything from the smax, but smin |
| 1020 | * is negative, hence safe. |
| 1021 | */ |
| 1022 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1023 | reg->umin_value); |
| 1024 | reg->smax_value = reg->umax_value; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 1029 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 1030 | { |
| 1031 | reg->var_off = tnum_intersect(reg->var_off, |
| 1032 | tnum_range(reg->umin_value, |
| 1033 | reg->umax_value)); |
| 1034 | } |
| 1035 | |
Yonghong Song | 581738a | 2019-11-21 09:06:50 -0800 | [diff] [blame] | 1036 | static void __reg_bound_offset32(struct bpf_reg_state *reg) |
| 1037 | { |
| 1038 | u64 mask = 0xffffFFFF; |
| 1039 | struct tnum range = tnum_range(reg->umin_value & mask, |
| 1040 | reg->umax_value & mask); |
| 1041 | struct tnum lo32 = tnum_cast(reg->var_off, 4); |
| 1042 | struct tnum hi32 = tnum_lshift(tnum_rshift(reg->var_off, 32), 32); |
| 1043 | |
| 1044 | reg->var_off = tnum_or(hi32, tnum_intersect(lo32, range)); |
| 1045 | } |
| 1046 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1047 | /* Reset the min/max bounds of a register */ |
| 1048 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 1049 | { |
| 1050 | reg->smin_value = S64_MIN; |
| 1051 | reg->smax_value = S64_MAX; |
| 1052 | reg->umin_value = 0; |
| 1053 | reg->umax_value = U64_MAX; |
| 1054 | } |
| 1055 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1056 | /* Mark a register as having a completely unknown (scalar) value. */ |
| 1057 | static void __mark_reg_unknown(struct bpf_reg_state *reg) |
| 1058 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 1059 | /* |
| 1060 | * Clear type, id, off, and union(map_ptr, range) and |
| 1061 | * padding between 'type' and union |
| 1062 | */ |
| 1063 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1064 | reg->type = SCALAR_VALUE; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1065 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1066 | reg->frameno = 0; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1067 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1070 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 1071 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1072 | { |
| 1073 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1074 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 1075 | /* Something bad happened, let's kill all regs except FP */ |
| 1076 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1077 | __mark_reg_not_init(regs + regno); |
| 1078 | return; |
| 1079 | } |
Alexei Starovoitov | 6754172 | 2019-08-21 14:07:10 -0700 | [diff] [blame] | 1080 | regs += regno; |
| 1081 | __mark_reg_unknown(regs); |
| 1082 | /* constant backtracking is enabled for root without bpf2bpf calls */ |
| 1083 | regs->precise = env->subprog_cnt > 1 || !env->allow_ptr_leaks ? |
| 1084 | true : false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | static void __mark_reg_not_init(struct bpf_reg_state *reg) |
| 1088 | { |
| 1089 | __mark_reg_unknown(reg); |
| 1090 | reg->type = NOT_INIT; |
| 1091 | } |
| 1092 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1093 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 1094 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 1095 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1096 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1097 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 1098 | /* Something bad happened, let's kill all regs except FP */ |
| 1099 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1100 | __mark_reg_not_init(regs + regno); |
| 1101 | return; |
| 1102 | } |
| 1103 | __mark_reg_not_init(regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 1104 | } |
| 1105 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1106 | #define DEF_NOT_SUBREG (0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1107 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1108 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1109 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1110 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1111 | int i; |
| 1112 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1113 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1114 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1115 | regs[i].live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1116 | regs[i].parent = NULL; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1117 | regs[i].subreg_def = DEF_NOT_SUBREG; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1118 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1119 | |
| 1120 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1121 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1122 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1123 | regs[BPF_REG_FP].frameno = state->frameno; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1124 | |
| 1125 | /* 1st arg to a function */ |
| 1126 | regs[BPF_REG_1].type = PTR_TO_CTX; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1127 | mark_reg_known_zero(env, regs, BPF_REG_1); |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 1128 | } |
| 1129 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1130 | #define BPF_MAIN_FUNC (-1) |
| 1131 | static void init_func_state(struct bpf_verifier_env *env, |
| 1132 | struct bpf_func_state *state, |
| 1133 | int callsite, int frameno, int subprogno) |
| 1134 | { |
| 1135 | state->callsite = callsite; |
| 1136 | state->frameno = frameno; |
| 1137 | state->subprogno = subprogno; |
| 1138 | init_reg_state(env, state); |
| 1139 | } |
| 1140 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1141 | enum reg_arg_type { |
| 1142 | SRC_OP, /* register is used as source operand */ |
| 1143 | DST_OP, /* register is used as destination operand */ |
| 1144 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 1145 | }; |
| 1146 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1147 | static int cmp_subprogs(const void *a, const void *b) |
| 1148 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1149 | return ((struct bpf_subprog_info *)a)->start - |
| 1150 | ((struct bpf_subprog_info *)b)->start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 1154 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1155 | struct bpf_subprog_info *p; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1156 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1157 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 1158 | sizeof(env->subprog_info[0]), cmp_subprogs); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1159 | if (!p) |
| 1160 | return -ENOENT; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1161 | return p - env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1162 | |
| 1163 | } |
| 1164 | |
| 1165 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 1166 | { |
| 1167 | int insn_cnt = env->prog->len; |
| 1168 | int ret; |
| 1169 | |
| 1170 | if (off >= insn_cnt || off < 0) { |
| 1171 | verbose(env, "call to invalid destination\n"); |
| 1172 | return -EINVAL; |
| 1173 | } |
| 1174 | ret = find_subprog(env, off); |
| 1175 | if (ret >= 0) |
| 1176 | return 0; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1177 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1178 | verbose(env, "too many subprograms\n"); |
| 1179 | return -E2BIG; |
| 1180 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1181 | env->subprog_info[env->subprog_cnt++].start = off; |
| 1182 | sort(env->subprog_info, env->subprog_cnt, |
| 1183 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | static int check_subprogs(struct bpf_verifier_env *env) |
| 1188 | { |
| 1189 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1190 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1191 | struct bpf_insn *insn = env->prog->insnsi; |
| 1192 | int insn_cnt = env->prog->len; |
| 1193 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 1194 | /* Add entry function. */ |
| 1195 | ret = add_subprog(env, 0); |
| 1196 | if (ret < 0) |
| 1197 | return ret; |
| 1198 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1199 | /* determine subprog starts. The end is one before the next starts */ |
| 1200 | for (i = 0; i < insn_cnt; i++) { |
| 1201 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1202 | continue; |
| 1203 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1204 | continue; |
| 1205 | if (!env->allow_ptr_leaks) { |
| 1206 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); |
| 1207 | return -EPERM; |
| 1208 | } |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1209 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 1210 | if (ret < 0) |
| 1211 | return ret; |
| 1212 | } |
| 1213 | |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1214 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 1215 | * logic. 'subprog_cnt' should not be increased. |
| 1216 | */ |
| 1217 | subprog[env->subprog_cnt].start = insn_cnt; |
| 1218 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1219 | if (env->log.level & BPF_LOG_LEVEL2) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1220 | for (i = 0; i < env->subprog_cnt; i++) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1221 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1222 | |
| 1223 | /* now check that all jumps are within the same subprog */ |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1224 | subprog_start = subprog[cur_subprog].start; |
| 1225 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1226 | for (i = 0; i < insn_cnt; i++) { |
| 1227 | u8 code = insn[i].code; |
| 1228 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 1229 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1230 | goto next; |
| 1231 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 1232 | goto next; |
| 1233 | off = i + insn[i].off + 1; |
| 1234 | if (off < subprog_start || off >= subprog_end) { |
| 1235 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 1236 | return -EINVAL; |
| 1237 | } |
| 1238 | next: |
| 1239 | if (i == subprog_end - 1) { |
| 1240 | /* to avoid fall-through from one subprog into another |
| 1241 | * the last insn of the subprog should be either exit |
| 1242 | * or unconditional jump back |
| 1243 | */ |
| 1244 | if (code != (BPF_JMP | BPF_EXIT) && |
| 1245 | code != (BPF_JMP | BPF_JA)) { |
| 1246 | verbose(env, "last insn is not an exit or jmp\n"); |
| 1247 | return -EINVAL; |
| 1248 | } |
| 1249 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1250 | cur_subprog++; |
| 1251 | if (cur_subprog < env->subprog_cnt) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1252 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1258 | /* Parentage chain of this register (or stack slot) should take care of all |
| 1259 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 1260 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1261 | static int mark_reg_read(struct bpf_verifier_env *env, |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1262 | const struct bpf_reg_state *state, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1263 | struct bpf_reg_state *parent, u8 flag) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1264 | { |
| 1265 | bool writes = parent == state->parent; /* Observe write marks */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1266 | int cnt = 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1267 | |
| 1268 | while (parent) { |
| 1269 | /* if read wasn't screened by an earlier write ... */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1270 | if (writes && state->live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1271 | break; |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 1272 | if (parent->live & REG_LIVE_DONE) { |
| 1273 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", |
| 1274 | reg_type_str[parent->type], |
| 1275 | parent->var_off.value, parent->off); |
| 1276 | return -EFAULT; |
| 1277 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1278 | /* The first condition is more likely to be true than the |
| 1279 | * second, checked it first. |
| 1280 | */ |
| 1281 | if ((parent->live & REG_LIVE_READ) == flag || |
| 1282 | parent->live & REG_LIVE_READ64) |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 1283 | /* The parentage chain never changes and |
| 1284 | * this parent was already marked as LIVE_READ. |
| 1285 | * There is no need to keep walking the chain again and |
| 1286 | * keep re-marking all parents as LIVE_READ. |
| 1287 | * This case happens when the same register is read |
| 1288 | * multiple times without writes into it in-between. |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1289 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
| 1290 | * then no need to set the weak REG_LIVE_READ32. |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 1291 | */ |
| 1292 | break; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1293 | /* ... then we depend on parent's value */ |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1294 | parent->live |= flag; |
| 1295 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ |
| 1296 | if (flag == REG_LIVE_READ64) |
| 1297 | parent->live &= ~REG_LIVE_READ32; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1298 | state = parent; |
| 1299 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1300 | writes = true; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1301 | cnt++; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1302 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1303 | |
| 1304 | if (env->longest_mark_read_walk < cnt) |
| 1305 | env->longest_mark_read_walk = cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1306 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1307 | } |
| 1308 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1309 | /* This function is supposed to be used by the following 32-bit optimization |
| 1310 | * code only. It returns TRUE if the source or destination register operates |
| 1311 | * on 64-bit, otherwise return FALSE. |
| 1312 | */ |
| 1313 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 1314 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) |
| 1315 | { |
| 1316 | u8 code, class, op; |
| 1317 | |
| 1318 | code = insn->code; |
| 1319 | class = BPF_CLASS(code); |
| 1320 | op = BPF_OP(code); |
| 1321 | if (class == BPF_JMP) { |
| 1322 | /* BPF_EXIT for "main" will reach here. Return TRUE |
| 1323 | * conservatively. |
| 1324 | */ |
| 1325 | if (op == BPF_EXIT) |
| 1326 | return true; |
| 1327 | if (op == BPF_CALL) { |
| 1328 | /* BPF to BPF call will reach here because of marking |
| 1329 | * caller saved clobber with DST_OP_NO_MARK for which we |
| 1330 | * don't care the register def because they are anyway |
| 1331 | * marked as NOT_INIT already. |
| 1332 | */ |
| 1333 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1334 | return false; |
| 1335 | /* Helper call will reach here because of arg type |
| 1336 | * check, conservatively return TRUE. |
| 1337 | */ |
| 1338 | if (t == SRC_OP) |
| 1339 | return true; |
| 1340 | |
| 1341 | return false; |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | if (class == BPF_ALU64 || class == BPF_JMP || |
| 1346 | /* BPF_END always use BPF_ALU class. */ |
| 1347 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) |
| 1348 | return true; |
| 1349 | |
| 1350 | if (class == BPF_ALU || class == BPF_JMP32) |
| 1351 | return false; |
| 1352 | |
| 1353 | if (class == BPF_LDX) { |
| 1354 | if (t != SRC_OP) |
| 1355 | return BPF_SIZE(code) == BPF_DW; |
| 1356 | /* LDX source must be ptr. */ |
| 1357 | return true; |
| 1358 | } |
| 1359 | |
| 1360 | if (class == BPF_STX) { |
| 1361 | if (reg->type != SCALAR_VALUE) |
| 1362 | return true; |
| 1363 | return BPF_SIZE(code) == BPF_DW; |
| 1364 | } |
| 1365 | |
| 1366 | if (class == BPF_LD) { |
| 1367 | u8 mode = BPF_MODE(code); |
| 1368 | |
| 1369 | /* LD_IMM64 */ |
| 1370 | if (mode == BPF_IMM) |
| 1371 | return true; |
| 1372 | |
| 1373 | /* Both LD_IND and LD_ABS return 32-bit data. */ |
| 1374 | if (t != SRC_OP) |
| 1375 | return false; |
| 1376 | |
| 1377 | /* Implicit ctx ptr. */ |
| 1378 | if (regno == BPF_REG_6) |
| 1379 | return true; |
| 1380 | |
| 1381 | /* Explicit source could be any width. */ |
| 1382 | return true; |
| 1383 | } |
| 1384 | |
| 1385 | if (class == BPF_ST) |
| 1386 | /* The only source register for BPF_ST is a ptr. */ |
| 1387 | return true; |
| 1388 | |
| 1389 | /* Conservatively return true at default. */ |
| 1390 | return true; |
| 1391 | } |
| 1392 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 1393 | /* Return TRUE if INSN doesn't have explicit value define. */ |
| 1394 | static bool insn_no_def(struct bpf_insn *insn) |
| 1395 | { |
| 1396 | u8 class = BPF_CLASS(insn->code); |
| 1397 | |
| 1398 | return (class == BPF_JMP || class == BPF_JMP32 || |
| 1399 | class == BPF_STX || class == BPF_ST); |
| 1400 | } |
| 1401 | |
| 1402 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ |
| 1403 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 1404 | { |
| 1405 | if (insn_no_def(insn)) |
| 1406 | return false; |
| 1407 | |
| 1408 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); |
| 1409 | } |
| 1410 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1411 | static void mark_insn_zext(struct bpf_verifier_env *env, |
| 1412 | struct bpf_reg_state *reg) |
| 1413 | { |
| 1414 | s32 def_idx = reg->subreg_def; |
| 1415 | |
| 1416 | if (def_idx == DEF_NOT_SUBREG) |
| 1417 | return; |
| 1418 | |
| 1419 | env->insn_aux_data[def_idx - 1].zext_dst = true; |
| 1420 | /* The dst will be zero extended, so won't be sub-register anymore. */ |
| 1421 | reg->subreg_def = DEF_NOT_SUBREG; |
| 1422 | } |
| 1423 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1424 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1425 | enum reg_arg_type t) |
| 1426 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1427 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1428 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1429 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1430 | struct bpf_reg_state *reg, *regs = state->regs; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1431 | bool rw64; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1432 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1433 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1434 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1435 | return -EINVAL; |
| 1436 | } |
| 1437 | |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1438 | reg = ®s[regno]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1439 | rw64 = is_reg64(env, insn, regno, reg, t); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1440 | if (t == SRC_OP) { |
| 1441 | /* check whether register used as source operand can be read */ |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1442 | if (reg->type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1443 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1444 | return -EACCES; |
| 1445 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1446 | /* 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] | 1447 | if (regno == BPF_REG_FP) |
| 1448 | return 0; |
| 1449 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1450 | if (rw64) |
| 1451 | mark_insn_zext(env, reg); |
| 1452 | |
| 1453 | return mark_reg_read(env, reg, reg->parent, |
| 1454 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1455 | } else { |
| 1456 | /* check whether register used as dest operand can be written to */ |
| 1457 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1458 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1459 | return -EACCES; |
| 1460 | } |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1461 | reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1462 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1463 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1464 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1465 | } |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1469 | /* for any branch, call, exit record the history of jmps in the given state */ |
| 1470 | static int push_jmp_history(struct bpf_verifier_env *env, |
| 1471 | struct bpf_verifier_state *cur) |
| 1472 | { |
| 1473 | u32 cnt = cur->jmp_history_cnt; |
| 1474 | struct bpf_idx_pair *p; |
| 1475 | |
| 1476 | cnt++; |
| 1477 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); |
| 1478 | if (!p) |
| 1479 | return -ENOMEM; |
| 1480 | p[cnt - 1].idx = env->insn_idx; |
| 1481 | p[cnt - 1].prev_idx = env->prev_insn_idx; |
| 1482 | cur->jmp_history = p; |
| 1483 | cur->jmp_history_cnt = cnt; |
| 1484 | return 0; |
| 1485 | } |
| 1486 | |
| 1487 | /* Backtrack one insn at a time. If idx is not at the top of recorded |
| 1488 | * history then previous instruction came from straight line execution. |
| 1489 | */ |
| 1490 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, |
| 1491 | u32 *history) |
| 1492 | { |
| 1493 | u32 cnt = *history; |
| 1494 | |
| 1495 | if (cnt && st->jmp_history[cnt - 1].idx == i) { |
| 1496 | i = st->jmp_history[cnt - 1].prev_idx; |
| 1497 | (*history)--; |
| 1498 | } else { |
| 1499 | i--; |
| 1500 | } |
| 1501 | return i; |
| 1502 | } |
| 1503 | |
| 1504 | /* For given verifier state backtrack_insn() is called from the last insn to |
| 1505 | * the first insn. Its purpose is to compute a bitmask of registers and |
| 1506 | * stack slots that needs precision in the parent verifier state. |
| 1507 | */ |
| 1508 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, |
| 1509 | u32 *reg_mask, u64 *stack_mask) |
| 1510 | { |
| 1511 | const struct bpf_insn_cbs cbs = { |
| 1512 | .cb_print = verbose, |
| 1513 | .private_data = env, |
| 1514 | }; |
| 1515 | struct bpf_insn *insn = env->prog->insnsi + idx; |
| 1516 | u8 class = BPF_CLASS(insn->code); |
| 1517 | u8 opcode = BPF_OP(insn->code); |
| 1518 | u8 mode = BPF_MODE(insn->code); |
| 1519 | u32 dreg = 1u << insn->dst_reg; |
| 1520 | u32 sreg = 1u << insn->src_reg; |
| 1521 | u32 spi; |
| 1522 | |
| 1523 | if (insn->code == 0) |
| 1524 | return 0; |
| 1525 | if (env->log.level & BPF_LOG_LEVEL) { |
| 1526 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
| 1527 | verbose(env, "%d: ", idx); |
| 1528 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
| 1529 | } |
| 1530 | |
| 1531 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 1532 | if (!(*reg_mask & dreg)) |
| 1533 | return 0; |
| 1534 | if (opcode == BPF_MOV) { |
| 1535 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1536 | /* dreg = sreg |
| 1537 | * dreg needs precision after this insn |
| 1538 | * sreg needs precision before this insn |
| 1539 | */ |
| 1540 | *reg_mask &= ~dreg; |
| 1541 | *reg_mask |= sreg; |
| 1542 | } else { |
| 1543 | /* dreg = K |
| 1544 | * dreg needs precision after this insn. |
| 1545 | * Corresponding register is already marked |
| 1546 | * as precise=true in this verifier state. |
| 1547 | * No further markings in parent are necessary |
| 1548 | */ |
| 1549 | *reg_mask &= ~dreg; |
| 1550 | } |
| 1551 | } else { |
| 1552 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1553 | /* dreg += sreg |
| 1554 | * both dreg and sreg need precision |
| 1555 | * before this insn |
| 1556 | */ |
| 1557 | *reg_mask |= sreg; |
| 1558 | } /* else dreg += K |
| 1559 | * dreg still needs precision before this insn |
| 1560 | */ |
| 1561 | } |
| 1562 | } else if (class == BPF_LDX) { |
| 1563 | if (!(*reg_mask & dreg)) |
| 1564 | return 0; |
| 1565 | *reg_mask &= ~dreg; |
| 1566 | |
| 1567 | /* scalars can only be spilled into stack w/o losing precision. |
| 1568 | * Load from any other memory can be zero extended. |
| 1569 | * The desire to keep that precision is already indicated |
| 1570 | * by 'precise' mark in corresponding register of this state. |
| 1571 | * No further tracking necessary. |
| 1572 | */ |
| 1573 | if (insn->src_reg != BPF_REG_FP) |
| 1574 | return 0; |
| 1575 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 1576 | return 0; |
| 1577 | |
| 1578 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. |
| 1579 | * that [fp - off] slot contains scalar that needs to be |
| 1580 | * tracked with precision |
| 1581 | */ |
| 1582 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 1583 | if (spi >= 64) { |
| 1584 | verbose(env, "BUG spi %d\n", spi); |
| 1585 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1586 | return -EFAULT; |
| 1587 | } |
| 1588 | *stack_mask |= 1ull << spi; |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 1589 | } else if (class == BPF_STX || class == BPF_ST) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1590 | if (*reg_mask & dreg) |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 1591 | /* stx & st shouldn't be using _scalar_ dst_reg |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1592 | * to access memory. It means backtracking |
| 1593 | * encountered a case of pointer subtraction. |
| 1594 | */ |
| 1595 | return -ENOTSUPP; |
| 1596 | /* scalars can only be spilled into stack */ |
| 1597 | if (insn->dst_reg != BPF_REG_FP) |
| 1598 | return 0; |
| 1599 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 1600 | return 0; |
| 1601 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 1602 | if (spi >= 64) { |
| 1603 | verbose(env, "BUG spi %d\n", spi); |
| 1604 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1605 | return -EFAULT; |
| 1606 | } |
| 1607 | if (!(*stack_mask & (1ull << spi))) |
| 1608 | return 0; |
| 1609 | *stack_mask &= ~(1ull << spi); |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 1610 | if (class == BPF_STX) |
| 1611 | *reg_mask |= sreg; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1612 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
| 1613 | if (opcode == BPF_CALL) { |
| 1614 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1615 | return -ENOTSUPP; |
| 1616 | /* regular helper call sets R0 */ |
| 1617 | *reg_mask &= ~1; |
| 1618 | if (*reg_mask & 0x3f) { |
| 1619 | /* if backtracing was looking for registers R1-R5 |
| 1620 | * they should have been found already. |
| 1621 | */ |
| 1622 | verbose(env, "BUG regs %x\n", *reg_mask); |
| 1623 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1624 | return -EFAULT; |
| 1625 | } |
| 1626 | } else if (opcode == BPF_EXIT) { |
| 1627 | return -ENOTSUPP; |
| 1628 | } |
| 1629 | } else if (class == BPF_LD) { |
| 1630 | if (!(*reg_mask & dreg)) |
| 1631 | return 0; |
| 1632 | *reg_mask &= ~dreg; |
| 1633 | /* It's ld_imm64 or ld_abs or ld_ind. |
| 1634 | * For ld_imm64 no further tracking of precision |
| 1635 | * into parent is necessary |
| 1636 | */ |
| 1637 | if (mode == BPF_IND || mode == BPF_ABS) |
| 1638 | /* to be analyzed */ |
| 1639 | return -ENOTSUPP; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1640 | } |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
| 1644 | /* the scalar precision tracking algorithm: |
| 1645 | * . at the start all registers have precise=false. |
| 1646 | * . scalar ranges are tracked as normal through alu and jmp insns. |
| 1647 | * . once precise value of the scalar register is used in: |
| 1648 | * . ptr + scalar alu |
| 1649 | * . if (scalar cond K|scalar) |
| 1650 | * . helper_call(.., scalar, ...) where ARG_CONST is expected |
| 1651 | * backtrack through the verifier states and mark all registers and |
| 1652 | * stack slots with spilled constants that these scalar regisers |
| 1653 | * should be precise. |
| 1654 | * . during state pruning two registers (or spilled stack slots) |
| 1655 | * are equivalent if both are not precise. |
| 1656 | * |
| 1657 | * Note the verifier cannot simply walk register parentage chain, |
| 1658 | * since many different registers and stack slots could have been |
| 1659 | * used to compute single precise scalar. |
| 1660 | * |
| 1661 | * The approach of starting with precise=true for all registers and then |
| 1662 | * backtrack to mark a register as not precise when the verifier detects |
| 1663 | * that program doesn't care about specific value (e.g., when helper |
| 1664 | * takes register as ARG_ANYTHING parameter) is not safe. |
| 1665 | * |
| 1666 | * It's ok to walk single parentage chain of the verifier states. |
| 1667 | * It's possible that this backtracking will go all the way till 1st insn. |
| 1668 | * All other branches will be explored for needing precision later. |
| 1669 | * |
| 1670 | * The backtracking needs to deal with cases like: |
| 1671 | * 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) |
| 1672 | * r9 -= r8 |
| 1673 | * r5 = r9 |
| 1674 | * if r5 > 0x79f goto pc+7 |
| 1675 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) |
| 1676 | * r5 += 1 |
| 1677 | * ... |
| 1678 | * call bpf_perf_event_output#25 |
| 1679 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO |
| 1680 | * |
| 1681 | * and this case: |
| 1682 | * r6 = 1 |
| 1683 | * call foo // uses callee's r6 inside to compute r0 |
| 1684 | * r0 += r6 |
| 1685 | * if r0 == 0 goto |
| 1686 | * |
| 1687 | * to track above reg_mask/stack_mask needs to be independent for each frame. |
| 1688 | * |
| 1689 | * Also if parent's curframe > frame where backtracking started, |
| 1690 | * the verifier need to mark registers in both frames, otherwise callees |
| 1691 | * may incorrectly prune callers. This is similar to |
| 1692 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") |
| 1693 | * |
| 1694 | * For now backtracking falls back into conservative marking. |
| 1695 | */ |
| 1696 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, |
| 1697 | struct bpf_verifier_state *st) |
| 1698 | { |
| 1699 | struct bpf_func_state *func; |
| 1700 | struct bpf_reg_state *reg; |
| 1701 | int i, j; |
| 1702 | |
| 1703 | /* big hammer: mark all scalars precise in this path. |
| 1704 | * pop_stack may still get !precise scalars. |
| 1705 | */ |
| 1706 | for (; st; st = st->parent) |
| 1707 | for (i = 0; i <= st->curframe; i++) { |
| 1708 | func = st->frame[i]; |
| 1709 | for (j = 0; j < BPF_REG_FP; j++) { |
| 1710 | reg = &func->regs[j]; |
| 1711 | if (reg->type != SCALAR_VALUE) |
| 1712 | continue; |
| 1713 | reg->precise = true; |
| 1714 | } |
| 1715 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { |
| 1716 | if (func->stack[j].slot_type[0] != STACK_SPILL) |
| 1717 | continue; |
| 1718 | reg = &func->stack[j].spilled_ptr; |
| 1719 | if (reg->type != SCALAR_VALUE) |
| 1720 | continue; |
| 1721 | reg->precise = true; |
| 1722 | } |
| 1723 | } |
| 1724 | } |
| 1725 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1726 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
| 1727 | int spi) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1728 | { |
| 1729 | struct bpf_verifier_state *st = env->cur_state; |
| 1730 | int first_idx = st->first_insn_idx; |
| 1731 | int last_idx = env->insn_idx; |
| 1732 | struct bpf_func_state *func; |
| 1733 | struct bpf_reg_state *reg; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1734 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
| 1735 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1736 | bool skip_first = true; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1737 | bool new_marks = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1738 | int i, err; |
| 1739 | |
| 1740 | if (!env->allow_ptr_leaks) |
| 1741 | /* backtracking is root only for now */ |
| 1742 | return 0; |
| 1743 | |
| 1744 | func = st->frame[st->curframe]; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1745 | if (regno >= 0) { |
| 1746 | reg = &func->regs[regno]; |
| 1747 | if (reg->type != SCALAR_VALUE) { |
| 1748 | WARN_ONCE(1, "backtracing misuse"); |
| 1749 | return -EFAULT; |
| 1750 | } |
| 1751 | if (!reg->precise) |
| 1752 | new_marks = true; |
| 1753 | else |
| 1754 | reg_mask = 0; |
| 1755 | reg->precise = true; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1756 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1757 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1758 | while (spi >= 0) { |
| 1759 | if (func->stack[spi].slot_type[0] != STACK_SPILL) { |
| 1760 | stack_mask = 0; |
| 1761 | break; |
| 1762 | } |
| 1763 | reg = &func->stack[spi].spilled_ptr; |
| 1764 | if (reg->type != SCALAR_VALUE) { |
| 1765 | stack_mask = 0; |
| 1766 | break; |
| 1767 | } |
| 1768 | if (!reg->precise) |
| 1769 | new_marks = true; |
| 1770 | else |
| 1771 | stack_mask = 0; |
| 1772 | reg->precise = true; |
| 1773 | break; |
| 1774 | } |
| 1775 | |
| 1776 | if (!new_marks) |
| 1777 | return 0; |
| 1778 | if (!reg_mask && !stack_mask) |
| 1779 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1780 | for (;;) { |
| 1781 | DECLARE_BITMAP(mask, 64); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1782 | u32 history = st->jmp_history_cnt; |
| 1783 | |
| 1784 | if (env->log.level & BPF_LOG_LEVEL) |
| 1785 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
| 1786 | for (i = last_idx;;) { |
| 1787 | if (skip_first) { |
| 1788 | err = 0; |
| 1789 | skip_first = false; |
| 1790 | } else { |
| 1791 | err = backtrack_insn(env, i, ®_mask, &stack_mask); |
| 1792 | } |
| 1793 | if (err == -ENOTSUPP) { |
| 1794 | mark_all_scalars_precise(env, st); |
| 1795 | return 0; |
| 1796 | } else if (err) { |
| 1797 | return err; |
| 1798 | } |
| 1799 | if (!reg_mask && !stack_mask) |
| 1800 | /* Found assignment(s) into tracked register in this state. |
| 1801 | * Since this state is already marked, just return. |
| 1802 | * Nothing to be tracked further in the parent state. |
| 1803 | */ |
| 1804 | return 0; |
| 1805 | if (i == first_idx) |
| 1806 | break; |
| 1807 | i = get_prev_insn_idx(st, i, &history); |
| 1808 | if (i >= env->prog->len) { |
| 1809 | /* This can happen if backtracking reached insn 0 |
| 1810 | * and there are still reg_mask or stack_mask |
| 1811 | * to backtrack. |
| 1812 | * It means the backtracking missed the spot where |
| 1813 | * particular register was initialized with a constant. |
| 1814 | */ |
| 1815 | verbose(env, "BUG backtracking idx %d\n", i); |
| 1816 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1817 | return -EFAULT; |
| 1818 | } |
| 1819 | } |
| 1820 | st = st->parent; |
| 1821 | if (!st) |
| 1822 | break; |
| 1823 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1824 | new_marks = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1825 | func = st->frame[st->curframe]; |
| 1826 | bitmap_from_u64(mask, reg_mask); |
| 1827 | for_each_set_bit(i, mask, 32) { |
| 1828 | reg = &func->regs[i]; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1829 | if (reg->type != SCALAR_VALUE) { |
| 1830 | reg_mask &= ~(1u << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1831 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1832 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1833 | if (!reg->precise) |
| 1834 | new_marks = true; |
| 1835 | reg->precise = true; |
| 1836 | } |
| 1837 | |
| 1838 | bitmap_from_u64(mask, stack_mask); |
| 1839 | for_each_set_bit(i, mask, 64) { |
| 1840 | if (i >= func->allocated_stack / BPF_REG_SIZE) { |
Alexei Starovoitov | 2339cd6 | 2019-09-03 15:16:17 -0700 | [diff] [blame] | 1841 | /* the sequence of instructions: |
| 1842 | * 2: (bf) r3 = r10 |
| 1843 | * 3: (7b) *(u64 *)(r3 -8) = r0 |
| 1844 | * 4: (79) r4 = *(u64 *)(r10 -8) |
| 1845 | * doesn't contain jmps. It's backtracked |
| 1846 | * as a single block. |
| 1847 | * During backtracking insn 3 is not recognized as |
| 1848 | * stack access, so at the end of backtracking |
| 1849 | * stack slot fp-8 is still marked in stack_mask. |
| 1850 | * However the parent state may not have accessed |
| 1851 | * fp-8 and it's "unallocated" stack space. |
| 1852 | * In such case fallback to conservative. |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1853 | */ |
Alexei Starovoitov | 2339cd6 | 2019-09-03 15:16:17 -0700 | [diff] [blame] | 1854 | mark_all_scalars_precise(env, st); |
| 1855 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1858 | if (func->stack[i].slot_type[0] != STACK_SPILL) { |
| 1859 | stack_mask &= ~(1ull << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1860 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1861 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1862 | reg = &func->stack[i].spilled_ptr; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1863 | if (reg->type != SCALAR_VALUE) { |
| 1864 | stack_mask &= ~(1ull << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1865 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1866 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1867 | if (!reg->precise) |
| 1868 | new_marks = true; |
| 1869 | reg->precise = true; |
| 1870 | } |
| 1871 | if (env->log.level & BPF_LOG_LEVEL) { |
| 1872 | print_verifier_state(env, func); |
| 1873 | verbose(env, "parent %s regs=%x stack=%llx marks\n", |
| 1874 | new_marks ? "didn't have" : "already had", |
| 1875 | reg_mask, stack_mask); |
| 1876 | } |
| 1877 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1878 | if (!reg_mask && !stack_mask) |
| 1879 | break; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1880 | if (!new_marks) |
| 1881 | break; |
| 1882 | |
| 1883 | last_idx = st->last_insn_idx; |
| 1884 | first_idx = st->first_insn_idx; |
| 1885 | } |
| 1886 | return 0; |
| 1887 | } |
| 1888 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 1889 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
| 1890 | { |
| 1891 | return __mark_chain_precision(env, regno, -1); |
| 1892 | } |
| 1893 | |
| 1894 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) |
| 1895 | { |
| 1896 | return __mark_chain_precision(env, -1, spi); |
| 1897 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1898 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1899 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 1900 | { |
| 1901 | switch (type) { |
| 1902 | case PTR_TO_MAP_VALUE: |
| 1903 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 1904 | case PTR_TO_STACK: |
| 1905 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1906 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1907 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1908 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1909 | case PTR_TO_FLOW_KEYS: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1910 | case CONST_PTR_TO_MAP: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1911 | case PTR_TO_SOCKET: |
| 1912 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1913 | case PTR_TO_SOCK_COMMON: |
| 1914 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 1915 | case PTR_TO_TCP_SOCK: |
| 1916 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 1917 | case PTR_TO_XDP_SOCK: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1918 | return true; |
| 1919 | default: |
| 1920 | return false; |
| 1921 | } |
| 1922 | } |
| 1923 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1924 | /* Does this register contain a constant zero? */ |
| 1925 | static bool register_is_null(struct bpf_reg_state *reg) |
| 1926 | { |
| 1927 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 1928 | } |
| 1929 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1930 | static bool register_is_const(struct bpf_reg_state *reg) |
| 1931 | { |
| 1932 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); |
| 1933 | } |
| 1934 | |
| 1935 | static void save_register_state(struct bpf_func_state *state, |
| 1936 | int spi, struct bpf_reg_state *reg) |
| 1937 | { |
| 1938 | int i; |
| 1939 | |
| 1940 | state->stack[spi].spilled_ptr = *reg; |
| 1941 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 1942 | |
| 1943 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 1944 | state->stack[spi].slot_type[i] = STACK_SPILL; |
| 1945 | } |
| 1946 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1947 | /* check_stack_read/write functions track spill/fill of registers, |
| 1948 | * stack boundary and alignment are checked in check_mem_access() |
| 1949 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1950 | static int check_stack_write(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1951 | struct bpf_func_state *state, /* func where register points to */ |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1952 | int off, int size, int value_regno, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1953 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1954 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1955 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1956 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1957 | struct bpf_reg_state *reg = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1958 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1959 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 1960 | state->acquired_refs, true); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1961 | if (err) |
| 1962 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1963 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 1964 | * so it's aligned access and [off, off + size) are within stack limits |
| 1965 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1966 | if (!env->allow_ptr_leaks && |
| 1967 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 1968 | size != BPF_REG_SIZE) { |
| 1969 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 1970 | return -EACCES; |
| 1971 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1972 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1973 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1974 | if (value_regno >= 0) |
| 1975 | reg = &cur->regs[value_regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1976 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1977 | if (reg && size == BPF_REG_SIZE && register_is_const(reg) && |
| 1978 | !register_is_null(reg) && env->allow_ptr_leaks) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1979 | if (dst_reg != BPF_REG_FP) { |
| 1980 | /* The backtracking logic can only recognize explicit |
| 1981 | * stack slot address like [fp - 8]. Other spill of |
| 1982 | * scalar via different register has to be conervative. |
| 1983 | * Backtrack from here and mark all registers as precise |
| 1984 | * that contributed into 'reg' being a constant. |
| 1985 | */ |
| 1986 | err = mark_chain_precision(env, value_regno); |
| 1987 | if (err) |
| 1988 | return err; |
| 1989 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1990 | save_register_state(state, spi, reg); |
| 1991 | } else if (reg && is_spillable_regtype(reg->type)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1992 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1993 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1994 | verbose_linfo(env, insn_idx, "; "); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1995 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1996 | return -EACCES; |
| 1997 | } |
| 1998 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1999 | if (state != cur && reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2000 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 2001 | return -EINVAL; |
| 2002 | } |
| 2003 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2004 | if (!env->allow_ptr_leaks) { |
| 2005 | bool sanitize = false; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2006 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2007 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 2008 | register_is_const(&state->stack[spi].spilled_ptr)) |
| 2009 | sanitize = true; |
| 2010 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 2011 | if (state->stack[spi].slot_type[i] == STACK_MISC) { |
| 2012 | sanitize = true; |
| 2013 | break; |
| 2014 | } |
| 2015 | if (sanitize) { |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2016 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
| 2017 | int soff = (-spi - 1) * BPF_REG_SIZE; |
| 2018 | |
| 2019 | /* detected reuse of integer stack slot with a pointer |
| 2020 | * which means either llvm is reusing stack slot or |
| 2021 | * an attacker is trying to exploit CVE-2018-3639 |
| 2022 | * (speculative store bypass) |
| 2023 | * Have to sanitize that slot with preemptive |
| 2024 | * store of zero. |
| 2025 | */ |
| 2026 | if (*poff && *poff != soff) { |
| 2027 | /* disallow programs where single insn stores |
| 2028 | * into two different stack slots, since verifier |
| 2029 | * cannot sanitize them |
| 2030 | */ |
| 2031 | verbose(env, |
| 2032 | "insn %d cannot access two stack slots fp%d and fp%d", |
| 2033 | insn_idx, *poff, soff); |
| 2034 | return -EINVAL; |
| 2035 | } |
| 2036 | *poff = soff; |
| 2037 | } |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2038 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2039 | save_register_state(state, spi, reg); |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2040 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2041 | u8 type = STACK_MISC; |
| 2042 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2043 | /* regular write of data into stack destroys any spilled ptr */ |
| 2044 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2045 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
| 2046 | if (state->stack[spi].slot_type[0] == STACK_SPILL) |
| 2047 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 2048 | state->stack[spi].slot_type[i] = STACK_MISC; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2049 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2050 | /* only mark the slot as written if all 8 bytes were written |
| 2051 | * otherwise read propagation may incorrectly stop too soon |
| 2052 | * when stack slots are partially written. |
| 2053 | * This heuristic means that read propagation will be |
| 2054 | * conservative, since it will add reg_live_read marks |
| 2055 | * to stack slots all the way to first state when programs |
| 2056 | * writes+reads less than 8 bytes |
| 2057 | */ |
| 2058 | if (size == BPF_REG_SIZE) |
| 2059 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 2060 | |
| 2061 | /* when we zero initialize stack slots mark them as such */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2062 | if (reg && register_is_null(reg)) { |
| 2063 | /* backtracking doesn't work for STACK_ZERO yet. */ |
| 2064 | err = mark_chain_precision(env, value_regno); |
| 2065 | if (err) |
| 2066 | return err; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2067 | type = STACK_ZERO; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2068 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2069 | |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2070 | /* Mark slots affected by this stack write. */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2071 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2072 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2073 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2074 | } |
| 2075 | return 0; |
| 2076 | } |
| 2077 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2078 | static int check_stack_read(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2079 | struct bpf_func_state *reg_state /* func where register points to */, |
| 2080 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2081 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2082 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2083 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2084 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2085 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2086 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2087 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2088 | if (reg_state->allocated_stack <= slot) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2089 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 2090 | off, size); |
| 2091 | return -EACCES; |
| 2092 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2093 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2094 | reg = ®_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2095 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2096 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2097 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2098 | if (reg->type != SCALAR_VALUE) { |
| 2099 | verbose_linfo(env, env->insn_idx, "; "); |
| 2100 | verbose(env, "invalid size of register fill\n"); |
| 2101 | return -EACCES; |
| 2102 | } |
| 2103 | if (value_regno >= 0) { |
| 2104 | mark_reg_unknown(env, state->regs, value_regno); |
| 2105 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 2106 | } |
| 2107 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
| 2108 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2109 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2110 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2111 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2112 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2113 | return -EACCES; |
| 2114 | } |
| 2115 | } |
| 2116 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2117 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2118 | /* restore register state from stack */ |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2119 | state->regs[value_regno] = *reg; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 2120 | /* mark reg as written since spilled pointer state likely |
| 2121 | * has its liveness marks cleared by is_state_visited() |
| 2122 | * which resets stack/reg liveness for state transitions |
| 2123 | */ |
| 2124 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2125 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2126 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2127 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2128 | int zeros = 0; |
| 2129 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2130 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2131 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 2132 | continue; |
| 2133 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 2134 | zeros++; |
| 2135 | continue; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2136 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2137 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 2138 | off, i, size); |
| 2139 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2140 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2141 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2142 | if (value_regno >= 0) { |
| 2143 | if (zeros == size) { |
| 2144 | /* any size read into register is zero extended, |
| 2145 | * so the whole register == const_zero |
| 2146 | */ |
| 2147 | __mark_reg_const_zero(&state->regs[value_regno]); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2148 | /* backtracking doesn't support STACK_ZERO yet, |
| 2149 | * so mark it precise here, so that later |
| 2150 | * backtracking can stop here. |
| 2151 | * Backtracking may not need this if this register |
| 2152 | * doesn't participate in pointer adjustment. |
| 2153 | * Forward propagation of precise flag is not |
| 2154 | * necessary either. This mark is only to stop |
| 2155 | * backtracking. Any register that contributed |
| 2156 | * to const 0 was marked precise before spill. |
| 2157 | */ |
| 2158 | state->regs[value_regno].precise = true; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2159 | } else { |
| 2160 | /* have read misc data from the stack */ |
| 2161 | mark_reg_unknown(env, state->regs, value_regno); |
| 2162 | } |
| 2163 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 2164 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2165 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2166 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2167 | } |
| 2168 | |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 2169 | static int check_stack_access(struct bpf_verifier_env *env, |
| 2170 | const struct bpf_reg_state *reg, |
| 2171 | int off, int size) |
| 2172 | { |
| 2173 | /* Stack accesses must be at a fixed offset, so that we |
| 2174 | * can determine what type of data were returned. See |
| 2175 | * check_stack_read(). |
| 2176 | */ |
| 2177 | if (!tnum_is_const(reg->var_off)) { |
| 2178 | char tn_buf[48]; |
| 2179 | |
| 2180 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Andrey Ignatov | 1fbd20f | 2019-04-03 23:22:43 -0700 | [diff] [blame] | 2181 | 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] | 2182 | tn_buf, off, size); |
| 2183 | return -EACCES; |
| 2184 | } |
| 2185 | |
| 2186 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 2187 | verbose(env, "invalid stack off=%d size=%d\n", off, size); |
| 2188 | return -EACCES; |
| 2189 | } |
| 2190 | |
| 2191 | return 0; |
| 2192 | } |
| 2193 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 2194 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
| 2195 | int off, int size, enum bpf_access_type type) |
| 2196 | { |
| 2197 | struct bpf_reg_state *regs = cur_regs(env); |
| 2198 | struct bpf_map *map = regs[regno].map_ptr; |
| 2199 | u32 cap = bpf_map_flags_to_cap(map); |
| 2200 | |
| 2201 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { |
| 2202 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", |
| 2203 | map->value_size, off, size); |
| 2204 | return -EACCES; |
| 2205 | } |
| 2206 | |
| 2207 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { |
| 2208 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", |
| 2209 | map->value_size, off, size); |
| 2210 | return -EACCES; |
| 2211 | } |
| 2212 | |
| 2213 | return 0; |
| 2214 | } |
| 2215 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2216 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2217 | static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2218 | int size, bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2219 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2220 | struct bpf_reg_state *regs = cur_regs(env); |
| 2221 | struct bpf_map *map = regs[regno].map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2222 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2223 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 2224 | off + size > map->value_size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2225 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2226 | map->value_size, off, size); |
| 2227 | return -EACCES; |
| 2228 | } |
| 2229 | return 0; |
| 2230 | } |
| 2231 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2232 | /* check read/write into a map element with possible variable offset */ |
| 2233 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2234 | int off, int size, bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2235 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2236 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2237 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2238 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 2239 | int err; |
| 2240 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2241 | /* We may have adjusted the register to this map value, so we |
| 2242 | * need to try adding each of min_value and max_value to off |
| 2243 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2244 | */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 2245 | if (env->log.level & BPF_LOG_LEVEL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2246 | print_verifier_state(env, state); |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 2247 | |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2248 | /* The minimum value is only important with signed |
| 2249 | * comparisons where we can't assume the floor of a |
| 2250 | * value is 0. If we are using signed variables for our |
| 2251 | * index'es we need to make sure that whatever we use |
| 2252 | * will have a set floor within our range. |
| 2253 | */ |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 2254 | if (reg->smin_value < 0 && |
| 2255 | (reg->smin_value == S64_MIN || |
| 2256 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || |
| 2257 | reg->smin_value + off < 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2258 | 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] | 2259 | regno); |
| 2260 | return -EACCES; |
| 2261 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2262 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
| 2263 | zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2264 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2265 | verbose(env, "R%d min value is outside of the array range\n", |
| 2266 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2267 | return err; |
| 2268 | } |
| 2269 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2270 | /* If we haven't set a max value then we need to bail since we can't be |
| 2271 | * sure we won't do bad things. |
| 2272 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2273 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2274 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2275 | verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n", |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2276 | regno); |
| 2277 | return -EACCES; |
| 2278 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2279 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
| 2280 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2281 | if (err) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2282 | verbose(env, "R%d max value is outside of the array range\n", |
| 2283 | regno); |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 2284 | |
| 2285 | if (map_value_has_spin_lock(reg->map_ptr)) { |
| 2286 | u32 lock = reg->map_ptr->spin_lock_off; |
| 2287 | |
| 2288 | /* if any part of struct bpf_spin_lock can be touched by |
| 2289 | * load/store reject this program. |
| 2290 | * To check that [x1, x2) overlaps with [y1, y2) |
| 2291 | * it is sufficient to check x1 < y2 && y1 < x2. |
| 2292 | */ |
| 2293 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && |
| 2294 | lock < reg->umax_value + off + size) { |
| 2295 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); |
| 2296 | return -EACCES; |
| 2297 | } |
| 2298 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2299 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2300 | } |
| 2301 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2302 | #define MAX_PACKET_OFF 0xffff |
| 2303 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2304 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2305 | const struct bpf_call_arg_meta *meta, |
| 2306 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2307 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2308 | switch (env->prog->type) { |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 2309 | /* Program types only with direct read access go here! */ |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2310 | case BPF_PROG_TYPE_LWT_IN: |
| 2311 | case BPF_PROG_TYPE_LWT_OUT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 2312 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2313 | case BPF_PROG_TYPE_SK_REUSEPORT: |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 2314 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Daniel Borkmann | d5563d3 | 2018-10-24 22:05:46 +0200 | [diff] [blame] | 2315 | case BPF_PROG_TYPE_CGROUP_SKB: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2316 | if (t == BPF_WRITE) |
| 2317 | return false; |
Alexander Alemayhu | 7e57fbb | 2017-02-14 00:02:35 +0100 | [diff] [blame] | 2318 | /* fallthrough */ |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 2319 | |
| 2320 | /* Program types with direct read + write access go here! */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2321 | case BPF_PROG_TYPE_SCHED_CLS: |
| 2322 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2323 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2324 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 2325 | case BPF_PROG_TYPE_SK_SKB: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2326 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2327 | if (meta) |
| 2328 | return meta->pkt_access; |
| 2329 | |
| 2330 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2331 | return true; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2332 | |
| 2333 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 2334 | if (t == BPF_WRITE) |
| 2335 | env->seen_direct_write = true; |
| 2336 | |
| 2337 | return true; |
| 2338 | |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2339 | default: |
| 2340 | return false; |
| 2341 | } |
| 2342 | } |
| 2343 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2344 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2345 | int off, int size, bool zero_size_allowed) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2346 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2347 | struct bpf_reg_state *regs = cur_regs(env); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2348 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2349 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2350 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 2351 | (u64)off + size > reg->range) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2352 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
Alexei Starovoitov | d91b28e | 2016-05-19 18:17:13 -0700 | [diff] [blame] | 2353 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2354 | return -EACCES; |
| 2355 | } |
| 2356 | return 0; |
| 2357 | } |
| 2358 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2359 | 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] | 2360 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2361 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2362 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2363 | struct bpf_reg_state *reg = ®s[regno]; |
| 2364 | int err; |
| 2365 | |
| 2366 | /* We may have added a variable offset to the packet pointer; but any |
| 2367 | * reg->range we have comes after that. We are only checking the fixed |
| 2368 | * offset. |
| 2369 | */ |
| 2370 | |
| 2371 | /* We don't allow negative numbers, because we aren't tracking enough |
| 2372 | * detail to prove they're safe. |
| 2373 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2374 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2375 | 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] | 2376 | regno); |
| 2377 | return -EACCES; |
| 2378 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2379 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2380 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2381 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2382 | return err; |
| 2383 | } |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 2384 | |
| 2385 | /* __check_packet_access has made sure "off + size - 1" is within u16. |
| 2386 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 2387 | * otherwise find_good_pkt_pointers would have refused to set range info |
| 2388 | * that __check_packet_access would have rejected this pkt access. |
| 2389 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 2390 | */ |
| 2391 | env->prog->aux->max_pkt_offset = |
| 2392 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 2393 | off + reg->umax_value + size - 1); |
| 2394 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2395 | return err; |
| 2396 | } |
| 2397 | |
| 2398 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2399 | 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] | 2400 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
| 2401 | u32 *btf_id) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2402 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 2403 | struct bpf_insn_access_aux info = { |
| 2404 | .reg_type = *reg_type, |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2405 | .log = &env->log, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 2406 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2407 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 2408 | if (env->ops->is_valid_access && |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2409 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 2410 | /* A non zero info.ctx_field_size indicates that this field is a |
| 2411 | * candidate for later verifier transformation to load the whole |
| 2412 | * field and then apply a mask when accessed with a narrower |
| 2413 | * access than actual ctx access size. A zero info.ctx_field_size |
| 2414 | * will only allow for whole field access and rejects any other |
| 2415 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2416 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 2417 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2418 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2419 | if (*reg_type == PTR_TO_BTF_ID) |
| 2420 | *btf_id = info.btf_id; |
| 2421 | else |
| 2422 | 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] | 2423 | /* remember the offset of last byte accessed in ctx */ |
| 2424 | if (env->prog->aux->max_ctx_offset < off + size) |
| 2425 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2426 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 2427 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2428 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2429 | 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] | 2430 | return -EACCES; |
| 2431 | } |
| 2432 | |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2433 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 2434 | int size) |
| 2435 | { |
| 2436 | if (size < 0 || off < 0 || |
| 2437 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 2438 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 2439 | off, size); |
| 2440 | return -EACCES; |
| 2441 | } |
| 2442 | return 0; |
| 2443 | } |
| 2444 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2445 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
| 2446 | u32 regno, int off, int size, |
| 2447 | enum bpf_access_type t) |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2448 | { |
| 2449 | struct bpf_reg_state *regs = cur_regs(env); |
| 2450 | struct bpf_reg_state *reg = ®s[regno]; |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2451 | struct bpf_insn_access_aux info = {}; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2452 | bool valid; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2453 | |
| 2454 | if (reg->smin_value < 0) { |
| 2455 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 2456 | regno); |
| 2457 | return -EACCES; |
| 2458 | } |
| 2459 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2460 | switch (reg->type) { |
| 2461 | case PTR_TO_SOCK_COMMON: |
| 2462 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); |
| 2463 | break; |
| 2464 | case PTR_TO_SOCKET: |
| 2465 | valid = bpf_sock_is_valid_access(off, size, t, &info); |
| 2466 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2467 | case PTR_TO_TCP_SOCK: |
| 2468 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); |
| 2469 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2470 | case PTR_TO_XDP_SOCK: |
| 2471 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); |
| 2472 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2473 | default: |
| 2474 | valid = false; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2477 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2478 | if (valid) { |
| 2479 | env->insn_aux_data[insn_idx].ctx_field_size = |
| 2480 | info.ctx_field_size; |
| 2481 | return 0; |
| 2482 | } |
| 2483 | |
| 2484 | verbose(env, "R%d invalid %s access off=%d size=%d\n", |
| 2485 | regno, reg_type_str[reg->type], off, size); |
| 2486 | |
| 2487 | return -EACCES; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2488 | } |
| 2489 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2490 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 2491 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2492 | { |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2493 | if (allow_ptr_leaks) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2494 | return false; |
| 2495 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2496 | return reg->type != SCALAR_VALUE; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2497 | } |
| 2498 | |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2499 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
| 2500 | { |
| 2501 | return cur_regs(env) + regno; |
| 2502 | } |
| 2503 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2504 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 2505 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2506 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2507 | } |
| 2508 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2509 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 2510 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2511 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2512 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2513 | return reg->type == PTR_TO_CTX; |
| 2514 | } |
| 2515 | |
| 2516 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) |
| 2517 | { |
| 2518 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 2519 | |
| 2520 | return type_is_sk_pointer(reg->type); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2521 | } |
| 2522 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2523 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 2524 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2525 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2526 | |
| 2527 | return type_is_pkt_pointer(reg->type); |
| 2528 | } |
| 2529 | |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 2530 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 2531 | { |
| 2532 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 2533 | |
| 2534 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 2535 | return reg->type == PTR_TO_FLOW_KEYS; |
| 2536 | } |
| 2537 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2538 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 2539 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2540 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2541 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2542 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2543 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2544 | |
| 2545 | /* Byte size accesses are always allowed. */ |
| 2546 | if (!strict || size == 1) |
| 2547 | return 0; |
| 2548 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 2549 | /* For platforms that do not have a Kconfig enabling |
| 2550 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 2551 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 2552 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 2553 | * to this code only in strict mode where we want to emulate |
| 2554 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 2555 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2556 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 2557 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2558 | |
| 2559 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 2560 | if (!tnum_is_aligned(reg_off, size)) { |
| 2561 | char tn_buf[48]; |
| 2562 | |
| 2563 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2564 | verbose(env, |
| 2565 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2566 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2567 | return -EACCES; |
| 2568 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2569 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2570 | return 0; |
| 2571 | } |
| 2572 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2573 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 2574 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2575 | const char *pointer_desc, |
| 2576 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2577 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2578 | struct tnum reg_off; |
| 2579 | |
| 2580 | /* Byte size accesses are always allowed. */ |
| 2581 | if (!strict || size == 1) |
| 2582 | return 0; |
| 2583 | |
| 2584 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 2585 | if (!tnum_is_aligned(reg_off, size)) { |
| 2586 | char tn_buf[48]; |
| 2587 | |
| 2588 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2589 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2590 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2591 | return -EACCES; |
| 2592 | } |
| 2593 | |
| 2594 | return 0; |
| 2595 | } |
| 2596 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2597 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2598 | const struct bpf_reg_state *reg, int off, |
| 2599 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2600 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2601 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2602 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2603 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2604 | switch (reg->type) { |
| 2605 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2606 | case PTR_TO_PACKET_META: |
| 2607 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 2608 | * right in front, treat it the very same way. |
| 2609 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2610 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2611 | case PTR_TO_FLOW_KEYS: |
| 2612 | pointer_desc = "flow keys "; |
| 2613 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2614 | case PTR_TO_MAP_VALUE: |
| 2615 | pointer_desc = "value "; |
| 2616 | break; |
| 2617 | case PTR_TO_CTX: |
| 2618 | pointer_desc = "context "; |
| 2619 | break; |
| 2620 | case PTR_TO_STACK: |
| 2621 | pointer_desc = "stack "; |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 2622 | /* The stack spill tracking logic in check_stack_write() |
| 2623 | * and check_stack_read() relies on stack accesses being |
| 2624 | * aligned. |
| 2625 | */ |
| 2626 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2627 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2628 | case PTR_TO_SOCKET: |
| 2629 | pointer_desc = "sock "; |
| 2630 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2631 | case PTR_TO_SOCK_COMMON: |
| 2632 | pointer_desc = "sock_common "; |
| 2633 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2634 | case PTR_TO_TCP_SOCK: |
| 2635 | pointer_desc = "tcp_sock "; |
| 2636 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2637 | case PTR_TO_XDP_SOCK: |
| 2638 | pointer_desc = "xdp_sock "; |
| 2639 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2640 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2641 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2642 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2643 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 2644 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2645 | } |
| 2646 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2647 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 2648 | const struct bpf_func_state *func, |
| 2649 | int off) |
| 2650 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2651 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2652 | |
| 2653 | if (stack >= -off) |
| 2654 | return 0; |
| 2655 | |
| 2656 | /* update known max for given subprogram */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2657 | env->subprog_info[func->subprogno].stack_depth = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2658 | return 0; |
| 2659 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2660 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2661 | /* starting from main bpf function walk all instructions of the function |
| 2662 | * and recursively walk all callees that given function can call. |
| 2663 | * Ignore jump and exit insns. |
| 2664 | * Since recursion is prevented by check_cfg() this algorithm |
| 2665 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 2666 | */ |
| 2667 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 2668 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2669 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 2670 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2671 | struct bpf_insn *insn = env->prog->insnsi; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2672 | int ret_insn[MAX_CALL_FRAMES]; |
| 2673 | int ret_prog[MAX_CALL_FRAMES]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2674 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2675 | process_func: |
| 2676 | /* round up to 32-bytes, since this is granularity |
| 2677 | * of interpreter stack size |
| 2678 | */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2679 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2680 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2681 | 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] | 2682 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2683 | return -EACCES; |
| 2684 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2685 | continue_func: |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 2686 | subprog_end = subprog[idx + 1].start; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2687 | for (; i < subprog_end; i++) { |
| 2688 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 2689 | continue; |
| 2690 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 2691 | continue; |
| 2692 | /* remember insn and function to return to */ |
| 2693 | ret_insn[frame] = i + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2694 | ret_prog[frame] = idx; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2695 | |
| 2696 | /* find the callee */ |
| 2697 | i = i + insn[i].imm + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2698 | idx = find_subprog(env, i); |
| 2699 | if (idx < 0) { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2700 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 2701 | i); |
| 2702 | return -EFAULT; |
| 2703 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2704 | frame++; |
| 2705 | if (frame >= MAX_CALL_FRAMES) { |
Paul Chaignon | 927cb78 | 2019-03-20 13:58:27 +0100 | [diff] [blame] | 2706 | verbose(env, "the call stack of %d frames is too deep !\n", |
| 2707 | frame); |
| 2708 | return -E2BIG; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2709 | } |
| 2710 | goto process_func; |
| 2711 | } |
| 2712 | /* end of for() loop means the last insn of the 'subprog' |
| 2713 | * was reached. Doesn't matter whether it was JA or EXIT |
| 2714 | */ |
| 2715 | if (frame == 0) |
| 2716 | return 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2717 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2718 | frame--; |
| 2719 | i = ret_insn[frame]; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2720 | idx = ret_prog[frame]; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2721 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2722 | } |
| 2723 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 2724 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 2725 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 2726 | const struct bpf_insn *insn, int idx) |
| 2727 | { |
| 2728 | int start = idx + insn->imm + 1, subprog; |
| 2729 | |
| 2730 | subprog = find_subprog(env, start); |
| 2731 | if (subprog < 0) { |
| 2732 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 2733 | start); |
| 2734 | return -EFAULT; |
| 2735 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2736 | return env->subprog_info[subprog].stack_depth; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 2737 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 2738 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 2739 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 2740 | static int check_ctx_reg(struct bpf_verifier_env *env, |
| 2741 | const struct bpf_reg_state *reg, int regno) |
| 2742 | { |
| 2743 | /* Access to ctx or passing it to a helper is only allowed in |
| 2744 | * its original, unmodified form. |
| 2745 | */ |
| 2746 | |
| 2747 | if (reg->off) { |
| 2748 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 2749 | regno, reg->off); |
| 2750 | return -EACCES; |
| 2751 | } |
| 2752 | |
| 2753 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 2754 | char tn_buf[48]; |
| 2755 | |
| 2756 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2757 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 2758 | return -EACCES; |
| 2759 | } |
| 2760 | |
| 2761 | return 0; |
| 2762 | } |
| 2763 | |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 2764 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
| 2765 | const struct bpf_reg_state *reg, |
| 2766 | int regno, int off, int size) |
| 2767 | { |
| 2768 | if (off < 0) { |
| 2769 | verbose(env, |
| 2770 | "R%d invalid tracepoint buffer access: off=%d, size=%d", |
| 2771 | regno, off, size); |
| 2772 | return -EACCES; |
| 2773 | } |
| 2774 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 2775 | char tn_buf[48]; |
| 2776 | |
| 2777 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2778 | verbose(env, |
| 2779 | "R%d invalid variable buffer offset: off=%d, var_off=%s", |
| 2780 | regno, off, tn_buf); |
| 2781 | return -EACCES; |
| 2782 | } |
| 2783 | if (off + size > env->prog->aux->max_tp_access) |
| 2784 | env->prog->aux->max_tp_access = off + size; |
| 2785 | |
| 2786 | return 0; |
| 2787 | } |
| 2788 | |
| 2789 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 2790 | /* truncate register to smaller size (in bytes) |
| 2791 | * must be called with size < BPF_REG_SIZE |
| 2792 | */ |
| 2793 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 2794 | { |
| 2795 | u64 mask; |
| 2796 | |
| 2797 | /* clear high bits in bit representation */ |
| 2798 | reg->var_off = tnum_cast(reg->var_off, size); |
| 2799 | |
| 2800 | /* fix arithmetic bounds */ |
| 2801 | mask = ((u64)1 << (size * 8)) - 1; |
| 2802 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 2803 | reg->umin_value &= mask; |
| 2804 | reg->umax_value &= mask; |
| 2805 | } else { |
| 2806 | reg->umin_value = 0; |
| 2807 | reg->umax_value = mask; |
| 2808 | } |
| 2809 | reg->smin_value = reg->umin_value; |
| 2810 | reg->smax_value = reg->umax_value; |
| 2811 | } |
| 2812 | |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 2813 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
| 2814 | { |
| 2815 | return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen; |
| 2816 | } |
| 2817 | |
| 2818 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) |
| 2819 | { |
| 2820 | void *ptr; |
| 2821 | u64 addr; |
| 2822 | int err; |
| 2823 | |
| 2824 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 2825 | if (err) |
| 2826 | return err; |
Andrii Nakryiko | 2dedd7d | 2019-10-11 10:20:53 -0700 | [diff] [blame] | 2827 | ptr = (void *)(long)addr + off; |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 2828 | |
| 2829 | switch (size) { |
| 2830 | case sizeof(u8): |
| 2831 | *val = (u64)*(u8 *)ptr; |
| 2832 | break; |
| 2833 | case sizeof(u16): |
| 2834 | *val = (u64)*(u16 *)ptr; |
| 2835 | break; |
| 2836 | case sizeof(u32): |
| 2837 | *val = (u64)*(u32 *)ptr; |
| 2838 | break; |
| 2839 | case sizeof(u64): |
| 2840 | *val = *(u64 *)ptr; |
| 2841 | break; |
| 2842 | default: |
| 2843 | return -EINVAL; |
| 2844 | } |
| 2845 | return 0; |
| 2846 | } |
| 2847 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2848 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
| 2849 | struct bpf_reg_state *regs, |
| 2850 | int regno, int off, int size, |
| 2851 | enum bpf_access_type atype, |
| 2852 | int value_regno) |
| 2853 | { |
| 2854 | struct bpf_reg_state *reg = regs + regno; |
| 2855 | const struct btf_type *t = btf_type_by_id(btf_vmlinux, reg->btf_id); |
| 2856 | const char *tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 2857 | u32 btf_id; |
| 2858 | int ret; |
| 2859 | |
| 2860 | if (atype != BPF_READ) { |
| 2861 | verbose(env, "only read is supported\n"); |
| 2862 | return -EACCES; |
| 2863 | } |
| 2864 | |
| 2865 | if (off < 0) { |
| 2866 | verbose(env, |
| 2867 | "R%d is ptr_%s invalid negative access: off=%d\n", |
| 2868 | regno, tname, off); |
| 2869 | return -EACCES; |
| 2870 | } |
| 2871 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 2872 | char tn_buf[48]; |
| 2873 | |
| 2874 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2875 | verbose(env, |
| 2876 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", |
| 2877 | regno, tname, off, tn_buf); |
| 2878 | return -EACCES; |
| 2879 | } |
| 2880 | |
| 2881 | ret = btf_struct_access(&env->log, t, off, size, atype, &btf_id); |
| 2882 | if (ret < 0) |
| 2883 | return ret; |
| 2884 | |
| 2885 | if (ret == SCALAR_VALUE) { |
| 2886 | mark_reg_unknown(env, regs, value_regno); |
| 2887 | return 0; |
| 2888 | } |
| 2889 | mark_reg_known_zero(env, regs, value_regno); |
| 2890 | regs[value_regno].type = PTR_TO_BTF_ID; |
| 2891 | regs[value_regno].btf_id = btf_id; |
| 2892 | return 0; |
| 2893 | } |
| 2894 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2895 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 2896 | * if t==write, value_regno is a register which value is stored into memory |
| 2897 | * if t==read, value_regno is a register which will receive the value from memory |
| 2898 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 2899 | * if t==read && value_regno==-1, don't care what we read from memory |
| 2900 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2901 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 2902 | int off, int bpf_size, enum bpf_access_type t, |
| 2903 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2904 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2905 | struct bpf_reg_state *regs = cur_regs(env); |
| 2906 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2907 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2908 | int size, err = 0; |
| 2909 | |
| 2910 | size = bpf_size_to_bytes(bpf_size); |
| 2911 | if (size < 0) |
| 2912 | return size; |
| 2913 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2914 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2915 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2916 | if (err) |
| 2917 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2918 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2919 | /* for access checks, reg->off is just part of off */ |
| 2920 | off += reg->off; |
| 2921 | |
| 2922 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2923 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2924 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2925 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2926 | return -EACCES; |
| 2927 | } |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 2928 | err = check_map_access_type(env, regno, off, size, t); |
| 2929 | if (err) |
| 2930 | return err; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2931 | err = check_map_access(env, regno, off, size, false); |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 2932 | if (!err && t == BPF_READ && value_regno >= 0) { |
| 2933 | struct bpf_map *map = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2934 | |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 2935 | /* if map is read-only, track its contents as scalars */ |
| 2936 | if (tnum_is_const(reg->var_off) && |
| 2937 | bpf_map_is_rdonly(map) && |
| 2938 | map->ops->map_direct_value_addr) { |
| 2939 | int map_off = off + reg->var_off.value; |
| 2940 | u64 val = 0; |
| 2941 | |
| 2942 | err = bpf_map_direct_read(map, map_off, size, |
| 2943 | &val); |
| 2944 | if (err) |
| 2945 | return err; |
| 2946 | |
| 2947 | regs[value_regno].type = SCALAR_VALUE; |
| 2948 | __mark_reg_known(®s[value_regno], val); |
| 2949 | } else { |
| 2950 | mark_reg_unknown(env, regs, value_regno); |
| 2951 | } |
| 2952 | } |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2953 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2954 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2955 | u32 btf_id = 0; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 2956 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2957 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2958 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2959 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2960 | return -EACCES; |
| 2961 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2962 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 2963 | err = check_ctx_reg(env, reg, regno); |
| 2964 | if (err < 0) |
| 2965 | return err; |
| 2966 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2967 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf_id); |
| 2968 | if (err) |
| 2969 | verbose_linfo(env, insn_idx, "; "); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2970 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2971 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2972 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 2973 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2974 | */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2975 | if (reg_type == SCALAR_VALUE) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2976 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2977 | } else { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2978 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2979 | value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2980 | if (reg_type_may_be_null(reg_type)) |
| 2981 | regs[value_regno].id = ++env->id_gen; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2982 | /* A load of ctx field could have different |
| 2983 | * actual load size with the one encoded in the |
| 2984 | * insn. When the dst is PTR, it is for sure not |
| 2985 | * a sub-register. |
| 2986 | */ |
| 2987 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2988 | if (reg_type == PTR_TO_BTF_ID) |
| 2989 | regs[value_regno].btf_id = btf_id; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2990 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2991 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2992 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2993 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2994 | } else if (reg->type == PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2995 | off += reg->var_off.value; |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 2996 | err = check_stack_access(env, reg, off, size); |
| 2997 | if (err) |
| 2998 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 2999 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3000 | state = func(env, reg); |
| 3001 | err = update_stack_depth(env, state, off); |
| 3002 | if (err) |
| 3003 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 3004 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3005 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3006 | err = check_stack_write(env, state, off, size, |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 3007 | value_regno, insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3008 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3009 | err = check_stack_read(env, state, off, size, |
| 3010 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3011 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 3012 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3013 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3014 | return -EACCES; |
| 3015 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3016 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3017 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3018 | verbose(env, "R%d leaks addr into packet\n", |
| 3019 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3020 | return -EACCES; |
| 3021 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3022 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3023 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3024 | mark_reg_unknown(env, regs, value_regno); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 3025 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 3026 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3027 | is_pointer_value(env, value_regno)) { |
| 3028 | verbose(env, "R%d leaks addr into flow keys\n", |
| 3029 | value_regno); |
| 3030 | return -EACCES; |
| 3031 | } |
| 3032 | |
| 3033 | err = check_flow_keys_access(env, off, size); |
| 3034 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3035 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3036 | } else if (type_is_sk_pointer(reg->type)) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3037 | if (t == BPF_WRITE) { |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3038 | verbose(env, "R%d cannot write into %s\n", |
| 3039 | regno, reg_type_str[reg->type]); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3040 | return -EACCES; |
| 3041 | } |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 3042 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3043 | if (!err && value_regno >= 0) |
| 3044 | mark_reg_unknown(env, regs, value_regno); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3045 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
| 3046 | err = check_tp_buffer_access(env, reg, regno, off, size); |
| 3047 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3048 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3049 | } else if (reg->type == PTR_TO_BTF_ID) { |
| 3050 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
| 3051 | value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3052 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3053 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 3054 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3055 | return -EACCES; |
| 3056 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3057 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3058 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3059 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3060 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3061 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3062 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3063 | return err; |
| 3064 | } |
| 3065 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3066 | 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] | 3067 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3068 | int err; |
| 3069 | |
| 3070 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 3071 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3072 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3073 | return -EINVAL; |
| 3074 | } |
| 3075 | |
| 3076 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3077 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3078 | if (err) |
| 3079 | return err; |
| 3080 | |
| 3081 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3082 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3083 | if (err) |
| 3084 | return err; |
| 3085 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 3086 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3087 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 3088 | return -EACCES; |
| 3089 | } |
| 3090 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3091 | if (is_ctx_reg(env, insn->dst_reg) || |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 3092 | is_pkt_reg(env, insn->dst_reg) || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3093 | is_flow_key_reg(env, insn->dst_reg) || |
| 3094 | is_sk_reg(env, insn->dst_reg)) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3095 | 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] | 3096 | insn->dst_reg, |
| 3097 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 3098 | return -EACCES; |
| 3099 | } |
| 3100 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3101 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3102 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3103 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3104 | if (err) |
| 3105 | return err; |
| 3106 | |
| 3107 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3108 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3109 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3110 | } |
| 3111 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3112 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
| 3113 | int off, int access_size, |
| 3114 | bool zero_size_allowed) |
| 3115 | { |
| 3116 | struct bpf_reg_state *reg = reg_state(env, regno); |
| 3117 | |
| 3118 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 3119 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
| 3120 | if (tnum_is_const(reg->var_off)) { |
| 3121 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", |
| 3122 | regno, off, access_size); |
| 3123 | } else { |
| 3124 | char tn_buf[48]; |
| 3125 | |
| 3126 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3127 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", |
| 3128 | regno, tn_buf, access_size); |
| 3129 | } |
| 3130 | return -EACCES; |
| 3131 | } |
| 3132 | return 0; |
| 3133 | } |
| 3134 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3135 | /* when register 'regno' is passed into function that will read 'access_size' |
| 3136 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3137 | * and all elements of stack are initialized. |
| 3138 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 3139 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3140 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3141 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3142 | int access_size, bool zero_size_allowed, |
| 3143 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3144 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 3145 | struct bpf_reg_state *reg = reg_state(env, regno); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3146 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3147 | int err, min_off, max_off, i, j, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3148 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 3149 | if (reg->type != PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3150 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 3151 | if (zero_size_allowed && access_size == 0 && |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 3152 | register_is_null(reg)) |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 3153 | return 0; |
| 3154 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3155 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 3156 | reg_type_str[reg->type], |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 3157 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3158 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 3159 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3160 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3161 | if (tnum_is_const(reg->var_off)) { |
| 3162 | min_off = max_off = reg->var_off.value + reg->off; |
| 3163 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 3164 | zero_size_allowed); |
| 3165 | if (err) |
| 3166 | return err; |
| 3167 | } else { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3168 | /* Variable offset is prohibited for unprivileged mode for |
| 3169 | * simplicity since it requires corresponding support in |
| 3170 | * Spectre masking for stack ALU. |
| 3171 | * See also retrieve_ptr_limit(). |
| 3172 | */ |
| 3173 | if (!env->allow_ptr_leaks) { |
| 3174 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3175 | |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3176 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3177 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", |
| 3178 | regno, tn_buf); |
| 3179 | return -EACCES; |
| 3180 | } |
Andrey Ignatov | f2bcd05 | 2019-04-03 23:22:37 -0700 | [diff] [blame] | 3181 | /* Only initialized buffer on stack is allowed to be accessed |
| 3182 | * with variable offset. With uninitialized buffer it's hard to |
| 3183 | * guarantee that whole memory is marked as initialized on |
| 3184 | * helper return since specific bounds are unknown what may |
| 3185 | * cause uninitialized stack leaking. |
| 3186 | */ |
| 3187 | if (meta && meta->raw_mode) |
| 3188 | meta = NULL; |
| 3189 | |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3190 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
| 3191 | reg->smax_value <= -BPF_MAX_VAR_OFF) { |
| 3192 | verbose(env, "R%d unbounded indirect variable offset stack access\n", |
| 3193 | regno); |
| 3194 | return -EACCES; |
| 3195 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3196 | min_off = reg->smin_value + reg->off; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3197 | max_off = reg->smax_value + reg->off; |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3198 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 3199 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3200 | if (err) { |
| 3201 | verbose(env, "R%d min value is outside of stack bound\n", |
| 3202 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3203 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3204 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3205 | err = __check_stack_boundary(env, regno, max_off, access_size, |
| 3206 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3207 | if (err) { |
| 3208 | verbose(env, "R%d max value is outside of stack bound\n", |
| 3209 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3210 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3211 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3212 | } |
| 3213 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3214 | if (meta && meta->raw_mode) { |
| 3215 | meta->access_size = access_size; |
| 3216 | meta->regno = regno; |
| 3217 | return 0; |
| 3218 | } |
| 3219 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3220 | for (i = min_off; i < max_off + access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3221 | u8 *stype; |
| 3222 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3223 | slot = -i - 1; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3224 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3225 | if (state->allocated_stack <= slot) |
| 3226 | goto err; |
| 3227 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 3228 | if (*stype == STACK_MISC) |
| 3229 | goto mark; |
| 3230 | if (*stype == STACK_ZERO) { |
| 3231 | /* helper can write anything into the stack */ |
| 3232 | *stype = STACK_MISC; |
| 3233 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3234 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3235 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 3236 | state->stack[spi].spilled_ptr.type == SCALAR_VALUE) { |
| 3237 | __mark_reg_unknown(&state->stack[spi].spilled_ptr); |
| 3238 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 3239 | state->stack[spi].slot_type[j] = STACK_MISC; |
| 3240 | goto mark; |
| 3241 | } |
| 3242 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3243 | err: |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3244 | if (tnum_is_const(reg->var_off)) { |
| 3245 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 3246 | min_off, i - min_off, access_size); |
| 3247 | } else { |
| 3248 | char tn_buf[48]; |
| 3249 | |
| 3250 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3251 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", |
| 3252 | tn_buf, i - min_off, access_size); |
| 3253 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3254 | return -EACCES; |
| 3255 | mark: |
| 3256 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 3257 | * the whole slot to be marked as 'read' |
| 3258 | */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 3259 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 3260 | state->stack[spi].spilled_ptr.parent, |
| 3261 | REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3262 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3263 | return update_stack_depth(env, state, min_off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3264 | } |
| 3265 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3266 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 3267 | int access_size, bool zero_size_allowed, |
| 3268 | struct bpf_call_arg_meta *meta) |
| 3269 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3270 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3271 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3272 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3273 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3274 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3275 | return check_packet_access(env, regno, reg->off, access_size, |
| 3276 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3277 | case PTR_TO_MAP_VALUE: |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3278 | if (check_map_access_type(env, regno, reg->off, access_size, |
| 3279 | meta && meta->raw_mode ? BPF_WRITE : |
| 3280 | BPF_READ)) |
| 3281 | return -EACCES; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3282 | return check_map_access(env, regno, reg->off, access_size, |
| 3283 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3284 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3285 | return check_stack_boundary(env, regno, access_size, |
| 3286 | zero_size_allowed, meta); |
| 3287 | } |
| 3288 | } |
| 3289 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3290 | /* Implementation details: |
| 3291 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL |
| 3292 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
| 3293 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after |
| 3294 | * value_or_null->value transition, since the verifier only cares about |
| 3295 | * the range of access to valid map value pointer and doesn't care about actual |
| 3296 | * address of the map element. |
| 3297 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
| 3298 | * reg->id > 0 after value_or_null->value transition. By doing so |
| 3299 | * two bpf_map_lookups will be considered two different pointers that |
| 3300 | * point to different bpf_spin_locks. |
| 3301 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
| 3302 | * dead-locks. |
| 3303 | * Since only one bpf_spin_lock is allowed the checks are simpler than |
| 3304 | * reg_is_refcounted() logic. The verifier needs to remember only |
| 3305 | * one spin_lock instead of array of acquired_refs. |
| 3306 | * cur_state->active_spin_lock remembers which map value element got locked |
| 3307 | * and clears it after bpf_spin_unlock. |
| 3308 | */ |
| 3309 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, |
| 3310 | bool is_lock) |
| 3311 | { |
| 3312 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 3313 | struct bpf_verifier_state *cur = env->cur_state; |
| 3314 | bool is_const = tnum_is_const(reg->var_off); |
| 3315 | struct bpf_map *map = reg->map_ptr; |
| 3316 | u64 val = reg->var_off.value; |
| 3317 | |
| 3318 | if (reg->type != PTR_TO_MAP_VALUE) { |
| 3319 | verbose(env, "R%d is not a pointer to map_value\n", regno); |
| 3320 | return -EINVAL; |
| 3321 | } |
| 3322 | if (!is_const) { |
| 3323 | verbose(env, |
| 3324 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", |
| 3325 | regno); |
| 3326 | return -EINVAL; |
| 3327 | } |
| 3328 | if (!map->btf) { |
| 3329 | verbose(env, |
| 3330 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", |
| 3331 | map->name); |
| 3332 | return -EINVAL; |
| 3333 | } |
| 3334 | if (!map_value_has_spin_lock(map)) { |
| 3335 | if (map->spin_lock_off == -E2BIG) |
| 3336 | verbose(env, |
| 3337 | "map '%s' has more than one 'struct bpf_spin_lock'\n", |
| 3338 | map->name); |
| 3339 | else if (map->spin_lock_off == -ENOENT) |
| 3340 | verbose(env, |
| 3341 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", |
| 3342 | map->name); |
| 3343 | else |
| 3344 | verbose(env, |
| 3345 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", |
| 3346 | map->name); |
| 3347 | return -EINVAL; |
| 3348 | } |
| 3349 | if (map->spin_lock_off != val + reg->off) { |
| 3350 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", |
| 3351 | val + reg->off); |
| 3352 | return -EINVAL; |
| 3353 | } |
| 3354 | if (is_lock) { |
| 3355 | if (cur->active_spin_lock) { |
| 3356 | verbose(env, |
| 3357 | "Locking two bpf_spin_locks are not allowed\n"); |
| 3358 | return -EINVAL; |
| 3359 | } |
| 3360 | cur->active_spin_lock = reg->id; |
| 3361 | } else { |
| 3362 | if (!cur->active_spin_lock) { |
| 3363 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
| 3364 | return -EINVAL; |
| 3365 | } |
| 3366 | if (cur->active_spin_lock != reg->id) { |
| 3367 | verbose(env, "bpf_spin_unlock of different lock\n"); |
| 3368 | return -EINVAL; |
| 3369 | } |
| 3370 | cur->active_spin_lock = 0; |
| 3371 | } |
| 3372 | return 0; |
| 3373 | } |
| 3374 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 3375 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 3376 | { |
| 3377 | return type == ARG_PTR_TO_MEM || |
| 3378 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 3379 | type == ARG_PTR_TO_UNINIT_MEM; |
| 3380 | } |
| 3381 | |
| 3382 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 3383 | { |
| 3384 | return type == ARG_CONST_SIZE || |
| 3385 | type == ARG_CONST_SIZE_OR_ZERO; |
| 3386 | } |
| 3387 | |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 3388 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
| 3389 | { |
| 3390 | return type == ARG_PTR_TO_INT || |
| 3391 | type == ARG_PTR_TO_LONG; |
| 3392 | } |
| 3393 | |
| 3394 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
| 3395 | { |
| 3396 | if (type == ARG_PTR_TO_INT) |
| 3397 | return sizeof(u32); |
| 3398 | else if (type == ARG_PTR_TO_LONG) |
| 3399 | return sizeof(u64); |
| 3400 | |
| 3401 | return -EINVAL; |
| 3402 | } |
| 3403 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3404 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3405 | enum bpf_arg_type arg_type, |
| 3406 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3407 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3408 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 3409 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3410 | int err = 0; |
| 3411 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 3412 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3413 | return 0; |
| 3414 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3415 | err = check_reg_arg(env, regno, SRC_OP); |
| 3416 | if (err) |
| 3417 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3418 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3419 | if (arg_type == ARG_ANYTHING) { |
| 3420 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3421 | verbose(env, "R%d leaks addr into helper function\n", |
| 3422 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3423 | return -EACCES; |
| 3424 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 3425 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3426 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 3427 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3428 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 3429 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3430 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 3431 | return -EACCES; |
| 3432 | } |
| 3433 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 3434 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 3435 | arg_type == ARG_PTR_TO_MAP_VALUE || |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 3436 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
| 3437 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3438 | expected_type = PTR_TO_STACK; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 3439 | if (register_is_null(reg) && |
| 3440 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) |
| 3441 | /* final test in check_stack_boundary() */; |
| 3442 | else if (!type_is_pkt_pointer(type) && |
| 3443 | type != PTR_TO_MAP_VALUE && |
| 3444 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 3445 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3446 | } else if (arg_type == ARG_CONST_SIZE || |
| 3447 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3448 | expected_type = SCALAR_VALUE; |
| 3449 | if (type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 3450 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3451 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 3452 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 3453 | if (type != expected_type) |
| 3454 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 3455 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 3456 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 3457 | if (type != expected_type) |
| 3458 | goto err_type; |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 3459 | err = check_ctx_reg(env, reg, regno); |
| 3460 | if (err < 0) |
| 3461 | return err; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3462 | } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) { |
| 3463 | expected_type = PTR_TO_SOCK_COMMON; |
| 3464 | /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */ |
| 3465 | if (!type_is_sk_pointer(type)) |
| 3466 | goto err_type; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3467 | if (reg->ref_obj_id) { |
| 3468 | if (meta->ref_obj_id) { |
| 3469 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", |
| 3470 | regno, reg->ref_obj_id, |
| 3471 | meta->ref_obj_id); |
| 3472 | return -EFAULT; |
| 3473 | } |
| 3474 | meta->ref_obj_id = reg->ref_obj_id; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3475 | } |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 3476 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
| 3477 | expected_type = PTR_TO_SOCKET; |
| 3478 | if (type != expected_type) |
| 3479 | goto err_type; |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 3480 | } else if (arg_type == ARG_PTR_TO_BTF_ID) { |
| 3481 | expected_type = PTR_TO_BTF_ID; |
| 3482 | if (type != expected_type) |
| 3483 | goto err_type; |
| 3484 | if (reg->btf_id != meta->btf_id) { |
| 3485 | verbose(env, "Helper has type %s got %s in R%d\n", |
| 3486 | kernel_type_name(meta->btf_id), |
| 3487 | kernel_type_name(reg->btf_id), regno); |
| 3488 | |
| 3489 | return -EACCES; |
| 3490 | } |
| 3491 | if (!tnum_is_const(reg->var_off) || reg->var_off.value || reg->off) { |
| 3492 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", |
| 3493 | regno); |
| 3494 | return -EACCES; |
| 3495 | } |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3496 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
| 3497 | if (meta->func_id == BPF_FUNC_spin_lock) { |
| 3498 | if (process_spin_lock(env, regno, true)) |
| 3499 | return -EACCES; |
| 3500 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
| 3501 | if (process_spin_lock(env, regno, false)) |
| 3502 | return -EACCES; |
| 3503 | } else { |
| 3504 | verbose(env, "verifier internal error\n"); |
| 3505 | return -EFAULT; |
| 3506 | } |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 3507 | } else if (arg_type_is_mem_ptr(arg_type)) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 3508 | expected_type = PTR_TO_STACK; |
| 3509 | /* One exception here. In case function allows for NULL to be |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3510 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 3511 | * happens during stack boundary checking. |
| 3512 | */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 3513 | if (register_is_null(reg) && |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 3514 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 3515 | /* final test in check_stack_boundary() */; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3516 | else if (!type_is_pkt_pointer(type) && |
| 3517 | type != PTR_TO_MAP_VALUE && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3518 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 3519 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3520 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 3521 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 3522 | expected_type = PTR_TO_STACK; |
| 3523 | if (!type_is_pkt_pointer(type) && |
| 3524 | type != PTR_TO_MAP_VALUE && |
| 3525 | type != expected_type) |
| 3526 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3527 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3528 | verbose(env, "unsupported arg_type %d\n", arg_type); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3529 | return -EFAULT; |
| 3530 | } |
| 3531 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3532 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 3533 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3534 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3535 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 3536 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 3537 | * check that [key, key + map->key_size) are within |
| 3538 | * stack limits and initialized |
| 3539 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3540 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3541 | /* in function declaration map_ptr must come before |
| 3542 | * map_key, so that it's verified and known before |
| 3543 | * we have to check map_key here. Otherwise it means |
| 3544 | * that kernel subsystem misconfigured verifier |
| 3545 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3546 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3547 | return -EACCES; |
| 3548 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 3549 | err = check_helper_mem_access(env, regno, |
| 3550 | meta->map_ptr->key_size, false, |
| 3551 | NULL); |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 3552 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 3553 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
| 3554 | !register_is_null(reg)) || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 3555 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3556 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 3557 | * check [value, value + map->value_size) validity |
| 3558 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3559 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3560 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3561 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3562 | return -EACCES; |
| 3563 | } |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 3564 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 3565 | err = check_helper_mem_access(env, regno, |
| 3566 | meta->map_ptr->value_size, false, |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 3567 | meta); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 3568 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3569 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3570 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 3571 | /* remember the mem_size which may be used later |
| 3572 | * to refine return values. |
| 3573 | */ |
| 3574 | meta->msize_smax_value = reg->smax_value; |
| 3575 | meta->msize_umax_value = reg->umax_value; |
| 3576 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3577 | /* The register is SCALAR_VALUE; the access check |
| 3578 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3579 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3580 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3581 | /* For unprivileged variable accesses, disable raw |
| 3582 | * mode so that the program is required to |
| 3583 | * initialize all the memory that the helper could |
| 3584 | * just partially fill up. |
| 3585 | */ |
| 3586 | meta = NULL; |
| 3587 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3588 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3589 | 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] | 3590 | regno); |
| 3591 | return -EACCES; |
| 3592 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3593 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3594 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3595 | err = check_helper_mem_access(env, regno - 1, 0, |
| 3596 | zero_size_allowed, |
| 3597 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3598 | if (err) |
| 3599 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3600 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3601 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3602 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3603 | 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] | 3604 | regno); |
| 3605 | return -EACCES; |
| 3606 | } |
| 3607 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3608 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3609 | zero_size_allowed, meta); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 3610 | if (!err) |
| 3611 | err = mark_chain_precision(env, regno); |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 3612 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 3613 | int size = int_ptr_type_to_size(arg_type); |
| 3614 | |
| 3615 | err = check_helper_mem_access(env, regno, size, false, meta); |
| 3616 | if (err) |
| 3617 | return err; |
| 3618 | err = check_ptr_alignment(env, reg, 0, size, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 3622 | err_type: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3623 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 3624 | reg_type_str[type], reg_type_str[expected_type]); |
| 3625 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3626 | } |
| 3627 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3628 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 3629 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 3630 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 3631 | if (!map) |
| 3632 | return 0; |
| 3633 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3634 | /* We need a two way check, first is from map perspective ... */ |
| 3635 | switch (map->map_type) { |
| 3636 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 3637 | if (func_id != BPF_FUNC_tail_call) |
| 3638 | goto error; |
| 3639 | break; |
| 3640 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 3641 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 3642 | func_id != BPF_FUNC_perf_event_output && |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 3643 | func_id != BPF_FUNC_skb_output && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 3644 | func_id != BPF_FUNC_perf_event_read_value) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3645 | goto error; |
| 3646 | break; |
| 3647 | case BPF_MAP_TYPE_STACK_TRACE: |
| 3648 | if (func_id != BPF_FUNC_get_stackid) |
| 3649 | goto error; |
| 3650 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 3651 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 3652 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 3653 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 3654 | goto error; |
| 3655 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 3656 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 3657 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 3658 | if (func_id != BPF_FUNC_get_local_storage) |
| 3659 | goto error; |
| 3660 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 3661 | case BPF_MAP_TYPE_DEVMAP: |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 3662 | case BPF_MAP_TYPE_DEVMAP_HASH: |
Toke Høiland-Jørgensen | 0cdbb4b | 2019-06-28 11:12:35 +0200 | [diff] [blame] | 3663 | if (func_id != BPF_FUNC_redirect_map && |
| 3664 | func_id != BPF_FUNC_map_lookup_elem) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 3665 | goto error; |
| 3666 | break; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 3667 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 3668 | * appear. |
| 3669 | */ |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 3670 | case BPF_MAP_TYPE_CPUMAP: |
| 3671 | if (func_id != BPF_FUNC_redirect_map) |
| 3672 | goto error; |
| 3673 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 3674 | case BPF_MAP_TYPE_XSKMAP: |
| 3675 | if (func_id != BPF_FUNC_redirect_map && |
| 3676 | func_id != BPF_FUNC_map_lookup_elem) |
| 3677 | goto error; |
| 3678 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3679 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 3680 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3681 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 3682 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 3683 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 3684 | case BPF_MAP_TYPE_SOCKMAP: |
| 3685 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 3686 | func_id != BPF_FUNC_sock_map_update && |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 3687 | func_id != BPF_FUNC_map_delete_elem && |
| 3688 | func_id != BPF_FUNC_msg_redirect_map) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 3689 | goto error; |
| 3690 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 3691 | case BPF_MAP_TYPE_SOCKHASH: |
| 3692 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 3693 | func_id != BPF_FUNC_sock_hash_update && |
| 3694 | func_id != BPF_FUNC_map_delete_elem && |
| 3695 | func_id != BPF_FUNC_msg_redirect_hash) |
| 3696 | goto error; |
| 3697 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 3698 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 3699 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 3700 | goto error; |
| 3701 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 3702 | case BPF_MAP_TYPE_QUEUE: |
| 3703 | case BPF_MAP_TYPE_STACK: |
| 3704 | if (func_id != BPF_FUNC_map_peek_elem && |
| 3705 | func_id != BPF_FUNC_map_pop_elem && |
| 3706 | func_id != BPF_FUNC_map_push_elem) |
| 3707 | goto error; |
| 3708 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 3709 | case BPF_MAP_TYPE_SK_STORAGE: |
| 3710 | if (func_id != BPF_FUNC_sk_storage_get && |
| 3711 | func_id != BPF_FUNC_sk_storage_delete) |
| 3712 | goto error; |
| 3713 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3714 | default: |
| 3715 | break; |
| 3716 | } |
| 3717 | |
| 3718 | /* ... and second from the function itself. */ |
| 3719 | switch (func_id) { |
| 3720 | case BPF_FUNC_tail_call: |
| 3721 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 3722 | goto error; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 3723 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3724 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
| 3725 | return -EINVAL; |
| 3726 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3727 | break; |
| 3728 | case BPF_FUNC_perf_event_read: |
| 3729 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 3730 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 3731 | case BPF_FUNC_skb_output: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3732 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 3733 | goto error; |
| 3734 | break; |
| 3735 | case BPF_FUNC_get_stackid: |
| 3736 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 3737 | goto error; |
| 3738 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 3739 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 3740 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 3741 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 3742 | goto error; |
| 3743 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 3744 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 3745 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 3746 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 3747 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 3748 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 3749 | goto error; |
| 3750 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 3751 | case BPF_FUNC_sk_redirect_map: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 3752 | case BPF_FUNC_msg_redirect_map: |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 3753 | case BPF_FUNC_sock_map_update: |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 3754 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 3755 | goto error; |
| 3756 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 3757 | case BPF_FUNC_sk_redirect_hash: |
| 3758 | case BPF_FUNC_msg_redirect_hash: |
| 3759 | case BPF_FUNC_sock_hash_update: |
| 3760 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 3761 | goto error; |
| 3762 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 3763 | case BPF_FUNC_get_local_storage: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 3764 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 3765 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 3766 | goto error; |
| 3767 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 3768 | case BPF_FUNC_sk_select_reuseport: |
| 3769 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) |
| 3770 | goto error; |
| 3771 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 3772 | case BPF_FUNC_map_peek_elem: |
| 3773 | case BPF_FUNC_map_pop_elem: |
| 3774 | case BPF_FUNC_map_push_elem: |
| 3775 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 3776 | map->map_type != BPF_MAP_TYPE_STACK) |
| 3777 | goto error; |
| 3778 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 3779 | case BPF_FUNC_sk_storage_get: |
| 3780 | case BPF_FUNC_sk_storage_delete: |
| 3781 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
| 3782 | goto error; |
| 3783 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3784 | default: |
| 3785 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
| 3788 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3789 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3790 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 3791 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3792 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 3795 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3796 | { |
| 3797 | int count = 0; |
| 3798 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3799 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3800 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3801 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3802 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3803 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3804 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3805 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3806 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3807 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3808 | count++; |
| 3809 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 3810 | /* We only support one arg being in raw mode at the moment, |
| 3811 | * which is sufficient for the helper functions we have |
| 3812 | * right now. |
| 3813 | */ |
| 3814 | return count <= 1; |
| 3815 | } |
| 3816 | |
| 3817 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 3818 | enum bpf_arg_type arg_next) |
| 3819 | { |
| 3820 | return (arg_type_is_mem_ptr(arg_curr) && |
| 3821 | !arg_type_is_mem_size(arg_next)) || |
| 3822 | (!arg_type_is_mem_ptr(arg_curr) && |
| 3823 | arg_type_is_mem_size(arg_next)); |
| 3824 | } |
| 3825 | |
| 3826 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 3827 | { |
| 3828 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 3829 | * bytes from memory 'buf'. Both arg types need |
| 3830 | * to be paired, so make sure there's no buggy |
| 3831 | * helper function specification. |
| 3832 | */ |
| 3833 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 3834 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 3835 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 3836 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 3837 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 3838 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 3839 | return false; |
| 3840 | |
| 3841 | return true; |
| 3842 | } |
| 3843 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3844 | 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] | 3845 | { |
| 3846 | int count = 0; |
| 3847 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3848 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3849 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3850 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3851 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3852 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3853 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3854 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3855 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3856 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3857 | count++; |
| 3858 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3859 | /* A reference acquiring function cannot acquire |
| 3860 | * another refcounted ptr. |
| 3861 | */ |
| 3862 | if (is_acquire_function(func_id) && count) |
| 3863 | return false; |
| 3864 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3865 | /* We only support one arg being unreferenced at the moment, |
| 3866 | * which is sufficient for the helper functions we have right now. |
| 3867 | */ |
| 3868 | return count <= 1; |
| 3869 | } |
| 3870 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3871 | 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] | 3872 | { |
| 3873 | return check_raw_mode_ok(fn) && |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3874 | check_arg_pair_ok(fn) && |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3875 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3876 | } |
| 3877 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3878 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 3879 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3880 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3881 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 3882 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3883 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3884 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3885 | int i; |
| 3886 | |
| 3887 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3888 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3889 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3890 | |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 3891 | bpf_for_each_spilled_reg(i, state, reg) { |
| 3892 | if (!reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3893 | continue; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3894 | if (reg_is_pkt_pointer_any(reg)) |
| 3895 | __mark_reg_unknown(reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3896 | } |
| 3897 | } |
| 3898 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3899 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 3900 | { |
| 3901 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3902 | int i; |
| 3903 | |
| 3904 | for (i = 0; i <= vstate->curframe; i++) |
| 3905 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 3906 | } |
| 3907 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3908 | static void release_reg_references(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3909 | struct bpf_func_state *state, |
| 3910 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3911 | { |
| 3912 | struct bpf_reg_state *regs = state->regs, *reg; |
| 3913 | int i; |
| 3914 | |
| 3915 | for (i = 0; i < MAX_BPF_REG; i++) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3916 | if (regs[i].ref_obj_id == ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3917 | mark_reg_unknown(env, regs, i); |
| 3918 | |
| 3919 | bpf_for_each_spilled_reg(i, state, reg) { |
| 3920 | if (!reg) |
| 3921 | continue; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3922 | if (reg->ref_obj_id == ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3923 | __mark_reg_unknown(reg); |
| 3924 | } |
| 3925 | } |
| 3926 | |
| 3927 | /* The pointer with the specified id has released its reference to kernel |
| 3928 | * resources. Identify all copies of the same pointer and clear the reference. |
| 3929 | */ |
| 3930 | static int release_reference(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3931 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3932 | { |
| 3933 | struct bpf_verifier_state *vstate = env->cur_state; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3934 | int err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3935 | int i; |
| 3936 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3937 | err = release_reference_state(cur_func(env), ref_obj_id); |
| 3938 | if (err) |
| 3939 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3940 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3941 | for (i = 0; i <= vstate->curframe; i++) |
| 3942 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
| 3943 | |
| 3944 | return 0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3945 | } |
| 3946 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3947 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 3948 | int *insn_idx) |
| 3949 | { |
| 3950 | struct bpf_verifier_state *state = env->cur_state; |
| 3951 | struct bpf_func_state *caller, *callee; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3952 | int i, err, subprog, target_insn; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3953 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 3954 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3955 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 3956 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3957 | return -E2BIG; |
| 3958 | } |
| 3959 | |
| 3960 | target_insn = *insn_idx + insn->imm; |
| 3961 | subprog = find_subprog(env, target_insn + 1); |
| 3962 | if (subprog < 0) { |
| 3963 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 3964 | target_insn + 1); |
| 3965 | return -EFAULT; |
| 3966 | } |
| 3967 | |
| 3968 | caller = state->frame[state->curframe]; |
| 3969 | if (state->frame[state->curframe + 1]) { |
| 3970 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 3971 | state->curframe + 1); |
| 3972 | return -EFAULT; |
| 3973 | } |
| 3974 | |
| 3975 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 3976 | if (!callee) |
| 3977 | return -ENOMEM; |
| 3978 | state->frame[state->curframe + 1] = callee; |
| 3979 | |
| 3980 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 3981 | * into its own stack before reading from it. |
| 3982 | * callee can read/write into caller's stack |
| 3983 | */ |
| 3984 | init_func_state(env, callee, |
| 3985 | /* remember the callsite, it will be used by bpf_exit */ |
| 3986 | *insn_idx /* callsite */, |
| 3987 | state->curframe + 1 /* frameno within this callchain */, |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 3988 | subprog /* subprog number within this prog */); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3989 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3990 | /* Transfer references to the callee */ |
| 3991 | err = transfer_reference_state(callee, caller); |
| 3992 | if (err) |
| 3993 | return err; |
| 3994 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 3995 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 3996 | * pointers, which connects us up to the liveness chain |
| 3997 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3998 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 3999 | callee->regs[i] = caller->regs[i]; |
| 4000 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 4001 | /* after the call registers r0 - r5 were scratched */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4002 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 4003 | mark_reg_not_init(env, caller->regs, caller_saved[i]); |
| 4004 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 4005 | } |
| 4006 | |
| 4007 | /* only increment it after check_reg_arg() finished */ |
| 4008 | state->curframe++; |
| 4009 | |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 4010 | if (btf_check_func_arg_match(env, subprog)) |
| 4011 | return -EINVAL; |
| 4012 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4013 | /* and go analyze first insn of the callee */ |
| 4014 | *insn_idx = target_insn; |
| 4015 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 4016 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4017 | verbose(env, "caller:\n"); |
| 4018 | print_verifier_state(env, caller); |
| 4019 | verbose(env, "callee:\n"); |
| 4020 | print_verifier_state(env, callee); |
| 4021 | } |
| 4022 | return 0; |
| 4023 | } |
| 4024 | |
| 4025 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 4026 | { |
| 4027 | struct bpf_verifier_state *state = env->cur_state; |
| 4028 | struct bpf_func_state *caller, *callee; |
| 4029 | struct bpf_reg_state *r0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4030 | int err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4031 | |
| 4032 | callee = state->frame[state->curframe]; |
| 4033 | r0 = &callee->regs[BPF_REG_0]; |
| 4034 | if (r0->type == PTR_TO_STACK) { |
| 4035 | /* technically it's ok to return caller's stack pointer |
| 4036 | * (or caller's caller's pointer) back to the caller, |
| 4037 | * since these pointers are valid. Only current stack |
| 4038 | * pointer will be invalid as soon as function exits, |
| 4039 | * but let's be conservative |
| 4040 | */ |
| 4041 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 4042 | return -EINVAL; |
| 4043 | } |
| 4044 | |
| 4045 | state->curframe--; |
| 4046 | caller = state->frame[state->curframe]; |
| 4047 | /* return to the caller whatever r0 had in the callee */ |
| 4048 | caller->regs[BPF_REG_0] = *r0; |
| 4049 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4050 | /* Transfer references to the caller */ |
| 4051 | err = transfer_reference_state(caller, callee); |
| 4052 | if (err) |
| 4053 | return err; |
| 4054 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4055 | *insn_idx = callee->callsite + 1; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 4056 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4057 | verbose(env, "returning from callee:\n"); |
| 4058 | print_verifier_state(env, callee); |
| 4059 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 4060 | print_verifier_state(env, caller); |
| 4061 | } |
| 4062 | /* clear everything in the callee */ |
| 4063 | free_func_state(callee); |
| 4064 | state->frame[state->curframe + 1] = NULL; |
| 4065 | return 0; |
| 4066 | } |
| 4067 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4068 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 4069 | int func_id, |
| 4070 | struct bpf_call_arg_meta *meta) |
| 4071 | { |
| 4072 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 4073 | |
| 4074 | if (ret_type != RET_INTEGER || |
| 4075 | (func_id != BPF_FUNC_get_stack && |
| 4076 | func_id != BPF_FUNC_probe_read_str)) |
| 4077 | return; |
| 4078 | |
| 4079 | ret_reg->smax_value = meta->msize_smax_value; |
| 4080 | ret_reg->umax_value = meta->msize_umax_value; |
| 4081 | __reg_deduce_bounds(ret_reg); |
| 4082 | __reg_bound_offset(ret_reg); |
| 4083 | } |
| 4084 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4085 | static int |
| 4086 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 4087 | int func_id, int insn_idx) |
| 4088 | { |
| 4089 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4090 | struct bpf_map *map = meta->map_ptr; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4091 | |
| 4092 | if (func_id != BPF_FUNC_tail_call && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 4093 | func_id != BPF_FUNC_map_lookup_elem && |
| 4094 | func_id != BPF_FUNC_map_update_elem && |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 4095 | func_id != BPF_FUNC_map_delete_elem && |
| 4096 | func_id != BPF_FUNC_map_push_elem && |
| 4097 | func_id != BPF_FUNC_map_pop_elem && |
| 4098 | func_id != BPF_FUNC_map_peek_elem) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4099 | return 0; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 4100 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4101 | if (map == NULL) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4102 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 4103 | return -EINVAL; |
| 4104 | } |
| 4105 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4106 | /* In case of read-only, some additional restrictions |
| 4107 | * need to be applied in order to prevent altering the |
| 4108 | * state of the map from program side. |
| 4109 | */ |
| 4110 | if ((map->map_flags & BPF_F_RDONLY_PROG) && |
| 4111 | (func_id == BPF_FUNC_map_delete_elem || |
| 4112 | func_id == BPF_FUNC_map_update_elem || |
| 4113 | func_id == BPF_FUNC_map_push_elem || |
| 4114 | func_id == BPF_FUNC_map_pop_elem)) { |
| 4115 | verbose(env, "write into map forbidden\n"); |
| 4116 | return -EACCES; |
| 4117 | } |
| 4118 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4119 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4120 | bpf_map_ptr_store(aux, meta->map_ptr, |
| 4121 | meta->map_ptr->unpriv_array); |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4122 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4123 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
| 4124 | meta->map_ptr->unpriv_array); |
| 4125 | return 0; |
| 4126 | } |
| 4127 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4128 | static int |
| 4129 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 4130 | int func_id, int insn_idx) |
| 4131 | { |
| 4132 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
| 4133 | struct bpf_reg_state *regs = cur_regs(env), *reg; |
| 4134 | struct bpf_map *map = meta->map_ptr; |
| 4135 | struct tnum range; |
| 4136 | u64 val; |
| 4137 | |
| 4138 | if (func_id != BPF_FUNC_tail_call) |
| 4139 | return 0; |
| 4140 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { |
| 4141 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 4142 | return -EINVAL; |
| 4143 | } |
| 4144 | |
| 4145 | range = tnum_range(0, map->max_entries - 1); |
| 4146 | reg = ®s[BPF_REG_3]; |
| 4147 | |
| 4148 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { |
| 4149 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
| 4150 | return 0; |
| 4151 | } |
| 4152 | |
| 4153 | val = reg->var_off.value; |
| 4154 | if (bpf_map_key_unseen(aux)) |
| 4155 | bpf_map_key_store(aux, val); |
| 4156 | else if (!bpf_map_key_poisoned(aux) && |
| 4157 | bpf_map_key_immediate(aux) != val) |
| 4158 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
| 4159 | return 0; |
| 4160 | } |
| 4161 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4162 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 4163 | { |
| 4164 | struct bpf_func_state *state = cur_func(env); |
| 4165 | int i; |
| 4166 | |
| 4167 | for (i = 0; i < state->acquired_refs; i++) { |
| 4168 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 4169 | state->refs[i].id, state->refs[i].insn_idx); |
| 4170 | } |
| 4171 | return state->acquired_refs ? -EINVAL : 0; |
| 4172 | } |
| 4173 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4174 | 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] | 4175 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4176 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4177 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4178 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4179 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4180 | int i, err; |
| 4181 | |
| 4182 | /* find function prototype */ |
| 4183 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4184 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 4185 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4186 | return -EINVAL; |
| 4187 | } |
| 4188 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 4189 | if (env->ops->get_func_proto) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 4190 | fn = env->ops->get_func_proto(func_id, env->prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4191 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4192 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 4193 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4194 | return -EINVAL; |
| 4195 | } |
| 4196 | |
| 4197 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 4198 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Daniel Borkmann | 3fe2867 | 2018-06-02 23:06:33 +0200 | [diff] [blame] | 4199 | 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] | 4200 | return -EINVAL; |
| 4201 | } |
| 4202 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 4203 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 4204 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 4205 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 4206 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 4207 | func_id_name(func_id), func_id); |
| 4208 | return -EINVAL; |
| 4209 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4210 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4211 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4212 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4213 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4214 | err = check_func_proto(fn, func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4215 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4216 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 4217 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4218 | return err; |
| 4219 | } |
| 4220 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 4221 | meta.func_id = func_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4222 | /* check args */ |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 4223 | for (i = 0; i < 5; i++) { |
Alexei Starovoitov | 9cc31b3 | 2019-11-14 10:57:14 -0800 | [diff] [blame] | 4224 | err = btf_resolve_helper_id(&env->log, fn, i); |
| 4225 | if (err > 0) |
| 4226 | meta.btf_id = err; |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 4227 | err = check_func_arg(env, BPF_REG_1 + i, fn->arg_type[i], &meta); |
| 4228 | if (err) |
| 4229 | return err; |
| 4230 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4231 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4232 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 4233 | if (err) |
| 4234 | return err; |
| 4235 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4236 | err = record_func_key(env, &meta, func_id, insn_idx); |
| 4237 | if (err) |
| 4238 | return err; |
| 4239 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4240 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 4241 | * is inferred from register state. |
| 4242 | */ |
| 4243 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 4244 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 4245 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4246 | if (err) |
| 4247 | return err; |
| 4248 | } |
| 4249 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4250 | if (func_id == BPF_FUNC_tail_call) { |
| 4251 | err = check_reference_leak(env); |
| 4252 | if (err) { |
| 4253 | verbose(env, "tail_call would lead to reference leak\n"); |
| 4254 | return err; |
| 4255 | } |
| 4256 | } else if (is_release_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4257 | err = release_reference(env, meta.ref_obj_id); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4258 | if (err) { |
| 4259 | verbose(env, "func %s#%d reference has not been acquired before\n", |
| 4260 | func_id_name(func_id), func_id); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4261 | return err; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4262 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4263 | } |
| 4264 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4265 | regs = cur_regs(env); |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 4266 | |
| 4267 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 4268 | * this is required because get_local_storage() can't return an error. |
| 4269 | */ |
| 4270 | if (func_id == BPF_FUNC_get_local_storage && |
| 4271 | !register_is_null(®s[BPF_REG_2])) { |
| 4272 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 4273 | return -EINVAL; |
| 4274 | } |
| 4275 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4276 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4277 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4278 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4279 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 4280 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4281 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 4282 | /* helper call returns 64-bit value. */ |
| 4283 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 4284 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4285 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4286 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4287 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4288 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4289 | } else if (fn->ret_type == RET_VOID) { |
| 4290 | regs[BPF_REG_0].type = NOT_INIT; |
Roman Gushchin | 3e6a4b3 | 2018-08-02 14:27:22 -0700 | [diff] [blame] | 4291 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 4292 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4293 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4294 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4295 | /* remember map_ptr, so that check_map_access() |
| 4296 | * can check 'value_size' boundary of memory access |
| 4297 | * to map element returned from bpf_map_lookup_elem() |
| 4298 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4299 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4300 | verbose(env, |
| 4301 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4302 | return -EINVAL; |
| 4303 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4304 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 4305 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 4306 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
Alexei Starovoitov | e16d2f1 | 2019-01-31 15:40:05 -0800 | [diff] [blame] | 4307 | if (map_value_has_spin_lock(meta.map_ptr)) |
| 4308 | regs[BPF_REG_0].id = ++env->id_gen; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 4309 | } else { |
| 4310 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
| 4311 | regs[BPF_REG_0].id = ++env->id_gen; |
| 4312 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 4313 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
| 4314 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 4315 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 4316 | regs[BPF_REG_0].id = ++env->id_gen; |
Lorenz Bauer | 85a51f8 | 2019-03-22 09:54:00 +0800 | [diff] [blame] | 4317 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
| 4318 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 4319 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; |
| 4320 | regs[BPF_REG_0].id = ++env->id_gen; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 4321 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
| 4322 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 4323 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; |
| 4324 | regs[BPF_REG_0].id = ++env->id_gen; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4325 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4326 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 4327 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4328 | return -EINVAL; |
| 4329 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 4330 | |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 4331 | if (is_ptr_cast_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4332 | /* For release_reference() */ |
| 4333 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 4334 | } else if (is_acquire_function(func_id)) { |
| 4335 | int id = acquire_reference_state(env, insn_idx); |
| 4336 | |
| 4337 | if (id < 0) |
| 4338 | return id; |
| 4339 | /* For mark_ptr_or_null_reg() */ |
| 4340 | regs[BPF_REG_0].id = id; |
| 4341 | /* For release_reference() */ |
| 4342 | regs[BPF_REG_0].ref_obj_id = id; |
| 4343 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4344 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4345 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
| 4346 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4347 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 4348 | if (err) |
| 4349 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 4350 | |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 4351 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
| 4352 | const char *err_str; |
| 4353 | |
| 4354 | #ifdef CONFIG_PERF_EVENTS |
| 4355 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 4356 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 4357 | #else |
| 4358 | err = -ENOTSUPP; |
| 4359 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 4360 | #endif |
| 4361 | if (err) { |
| 4362 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 4363 | return err; |
| 4364 | } |
| 4365 | |
| 4366 | env->prog->has_callchain_buf = true; |
| 4367 | } |
| 4368 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4369 | if (changes_data) |
| 4370 | clear_all_pkt_pointers(env); |
| 4371 | return 0; |
| 4372 | } |
| 4373 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4374 | static bool signed_add_overflows(s64 a, s64 b) |
| 4375 | { |
| 4376 | /* Do the add in u64, where overflow is well-defined */ |
| 4377 | s64 res = (s64)((u64)a + (u64)b); |
| 4378 | |
| 4379 | if (b < 0) |
| 4380 | return res > a; |
| 4381 | return res < a; |
| 4382 | } |
| 4383 | |
| 4384 | static bool signed_sub_overflows(s64 a, s64 b) |
| 4385 | { |
| 4386 | /* Do the sub in u64, where overflow is well-defined */ |
| 4387 | s64 res = (s64)((u64)a - (u64)b); |
| 4388 | |
| 4389 | if (b < 0) |
| 4390 | return res < a; |
| 4391 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 4392 | } |
| 4393 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 4394 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 4395 | const struct bpf_reg_state *reg, |
| 4396 | enum bpf_reg_type type) |
| 4397 | { |
| 4398 | bool known = tnum_is_const(reg->var_off); |
| 4399 | s64 val = reg->var_off.value; |
| 4400 | s64 smin = reg->smin_value; |
| 4401 | |
| 4402 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 4403 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 4404 | reg_type_str[type], val); |
| 4405 | return false; |
| 4406 | } |
| 4407 | |
| 4408 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 4409 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 4410 | reg_type_str[type], reg->off); |
| 4411 | return false; |
| 4412 | } |
| 4413 | |
| 4414 | if (smin == S64_MIN) { |
| 4415 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 4416 | reg_type_str[type]); |
| 4417 | return false; |
| 4418 | } |
| 4419 | |
| 4420 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 4421 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 4422 | smin, reg_type_str[type]); |
| 4423 | return false; |
| 4424 | } |
| 4425 | |
| 4426 | return true; |
| 4427 | } |
| 4428 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 4429 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
| 4430 | { |
| 4431 | return &env->insn_aux_data[env->insn_idx]; |
| 4432 | } |
| 4433 | |
| 4434 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
| 4435 | u32 *ptr_limit, u8 opcode, bool off_is_neg) |
| 4436 | { |
| 4437 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || |
| 4438 | (opcode == BPF_SUB && !off_is_neg); |
| 4439 | u32 off; |
| 4440 | |
| 4441 | switch (ptr_reg->type) { |
| 4442 | case PTR_TO_STACK: |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 4443 | /* Indirect variable offset stack access is prohibited in |
| 4444 | * unprivileged mode so it's not handled here. |
| 4445 | */ |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 4446 | off = ptr_reg->off + ptr_reg->var_off.value; |
| 4447 | if (mask_to_left) |
| 4448 | *ptr_limit = MAX_BPF_STACK + off; |
| 4449 | else |
| 4450 | *ptr_limit = -off; |
| 4451 | return 0; |
| 4452 | case PTR_TO_MAP_VALUE: |
| 4453 | if (mask_to_left) { |
| 4454 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; |
| 4455 | } else { |
| 4456 | off = ptr_reg->smin_value + ptr_reg->off; |
| 4457 | *ptr_limit = ptr_reg->map_ptr->value_size - off; |
| 4458 | } |
| 4459 | return 0; |
| 4460 | default: |
| 4461 | return -EINVAL; |
| 4462 | } |
| 4463 | } |
| 4464 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 4465 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
| 4466 | const struct bpf_insn *insn) |
| 4467 | { |
| 4468 | return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K; |
| 4469 | } |
| 4470 | |
| 4471 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, |
| 4472 | u32 alu_state, u32 alu_limit) |
| 4473 | { |
| 4474 | /* If we arrived here from different branches with different |
| 4475 | * state or limits to sanitize, then this won't work. |
| 4476 | */ |
| 4477 | if (aux->alu_state && |
| 4478 | (aux->alu_state != alu_state || |
| 4479 | aux->alu_limit != alu_limit)) |
| 4480 | return -EACCES; |
| 4481 | |
| 4482 | /* Corresponding fixup done in fixup_bpf_calls(). */ |
| 4483 | aux->alu_state = alu_state; |
| 4484 | aux->alu_limit = alu_limit; |
| 4485 | return 0; |
| 4486 | } |
| 4487 | |
| 4488 | static int sanitize_val_alu(struct bpf_verifier_env *env, |
| 4489 | struct bpf_insn *insn) |
| 4490 | { |
| 4491 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 4492 | |
| 4493 | if (can_skip_alu_sanitation(env, insn)) |
| 4494 | return 0; |
| 4495 | |
| 4496 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); |
| 4497 | } |
| 4498 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 4499 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
| 4500 | struct bpf_insn *insn, |
| 4501 | const struct bpf_reg_state *ptr_reg, |
| 4502 | struct bpf_reg_state *dst_reg, |
| 4503 | bool off_is_neg) |
| 4504 | { |
| 4505 | struct bpf_verifier_state *vstate = env->cur_state; |
| 4506 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 4507 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
| 4508 | u8 opcode = BPF_OP(insn->code); |
| 4509 | u32 alu_state, alu_limit; |
| 4510 | struct bpf_reg_state tmp; |
| 4511 | bool ret; |
| 4512 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 4513 | if (can_skip_alu_sanitation(env, insn)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 4514 | return 0; |
| 4515 | |
| 4516 | /* We already marked aux for masking from non-speculative |
| 4517 | * paths, thus we got here in the first place. We only care |
| 4518 | * to explore bad access from here. |
| 4519 | */ |
| 4520 | if (vstate->speculative) |
| 4521 | goto do_sim; |
| 4522 | |
| 4523 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; |
| 4524 | alu_state |= ptr_is_dst_reg ? |
| 4525 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; |
| 4526 | |
| 4527 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) |
| 4528 | return 0; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 4529 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 4530 | return -EACCES; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 4531 | do_sim: |
| 4532 | /* Simulate and find potential out-of-bounds access under |
| 4533 | * speculative execution from truncation as a result of |
| 4534 | * masking when off was not within expected range. If off |
| 4535 | * sits in dst, then we temporarily need to move ptr there |
| 4536 | * to simulate dst (== 0) +/-= ptr. Needed, for example, |
| 4537 | * for cases where we use K-based arithmetic in one direction |
| 4538 | * and truncated reg-based in the other in order to explore |
| 4539 | * bad access. |
| 4540 | */ |
| 4541 | if (!ptr_is_dst_reg) { |
| 4542 | tmp = *dst_reg; |
| 4543 | *dst_reg = *ptr_reg; |
| 4544 | } |
| 4545 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); |
Xu Yu | 0803278 | 2019-03-21 18:00:35 +0800 | [diff] [blame] | 4546 | if (!ptr_is_dst_reg && ret) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 4547 | *dst_reg = tmp; |
| 4548 | return !ret ? -EFAULT : 0; |
| 4549 | } |
| 4550 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4551 | /* 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] | 4552 | * Caller should also handle BPF_MOV case separately. |
| 4553 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 4554 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 4555 | */ |
| 4556 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 4557 | struct bpf_insn *insn, |
| 4558 | const struct bpf_reg_state *ptr_reg, |
| 4559 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4560 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4561 | struct bpf_verifier_state *vstate = env->cur_state; |
| 4562 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 4563 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4564 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4565 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 4566 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 4567 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 4568 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 4569 | u32 dst = insn->dst_reg, src = insn->src_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4570 | u8 opcode = BPF_OP(insn->code); |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 4571 | int ret; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4572 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4573 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4574 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 4575 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 4576 | smin_val > smax_val || umin_val > umax_val) { |
| 4577 | /* Taint dst register if offset had invalid bounds derived from |
| 4578 | * e.g. dead branches. |
| 4579 | */ |
| 4580 | __mark_reg_unknown(dst_reg); |
| 4581 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4582 | } |
| 4583 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4584 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 4585 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4586 | verbose(env, |
| 4587 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 4588 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4589 | return -EACCES; |
| 4590 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 4591 | |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 4592 | switch (ptr_reg->type) { |
| 4593 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 4594 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
| 4595 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4596 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 4597 | case CONST_PTR_TO_MAP: |
| 4598 | case PTR_TO_PACKET_END: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 4599 | case PTR_TO_SOCKET: |
| 4600 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4601 | case PTR_TO_SOCK_COMMON: |
| 4602 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 4603 | case PTR_TO_TCP_SOCK: |
| 4604 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 4605 | case PTR_TO_XDP_SOCK: |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 4606 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
| 4607 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4608 | return -EACCES; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 4609 | case PTR_TO_MAP_VALUE: |
| 4610 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { |
| 4611 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", |
| 4612 | off_reg == dst_reg ? dst : src); |
| 4613 | return -EACCES; |
| 4614 | } |
| 4615 | /* fall-through */ |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 4616 | default: |
| 4617 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 4621 | * 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] | 4622 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4623 | dst_reg->type = ptr_reg->type; |
| 4624 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 4625 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 4626 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 4627 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 4628 | return -EINVAL; |
| 4629 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4630 | switch (opcode) { |
| 4631 | case BPF_ADD: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 4632 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 4633 | if (ret < 0) { |
| 4634 | verbose(env, "R%d tried to add from different maps or paths\n", dst); |
| 4635 | return ret; |
| 4636 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4637 | /* We can take a fixed offset as long as it doesn't overflow |
| 4638 | * the s32 'off' field |
| 4639 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4640 | if (known && (ptr_reg->off + smin_val == |
| 4641 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4642 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4643 | dst_reg->smin_value = smin_ptr; |
| 4644 | dst_reg->smax_value = smax_ptr; |
| 4645 | dst_reg->umin_value = umin_ptr; |
| 4646 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4647 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4648 | dst_reg->off = ptr_reg->off + smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 4649 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4650 | break; |
| 4651 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4652 | /* A new variable offset is created. Note that off_reg->off |
| 4653 | * == 0, since it's a scalar. |
| 4654 | * dst_reg gets the pointer type and since some positive |
| 4655 | * integer value was added to the pointer, give it a new 'id' |
| 4656 | * if it's a PTR_TO_PACKET. |
| 4657 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 4658 | * added into the variable offset, and we copy the fixed offset |
| 4659 | * from ptr_reg. |
| 4660 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4661 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 4662 | signed_add_overflows(smax_ptr, smax_val)) { |
| 4663 | dst_reg->smin_value = S64_MIN; |
| 4664 | dst_reg->smax_value = S64_MAX; |
| 4665 | } else { |
| 4666 | dst_reg->smin_value = smin_ptr + smin_val; |
| 4667 | dst_reg->smax_value = smax_ptr + smax_val; |
| 4668 | } |
| 4669 | if (umin_ptr + umin_val < umin_ptr || |
| 4670 | umax_ptr + umax_val < umax_ptr) { |
| 4671 | dst_reg->umin_value = 0; |
| 4672 | dst_reg->umax_value = U64_MAX; |
| 4673 | } else { |
| 4674 | dst_reg->umin_value = umin_ptr + umin_val; |
| 4675 | dst_reg->umax_value = umax_ptr + umax_val; |
| 4676 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4677 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 4678 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 4679 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4680 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4681 | dst_reg->id = ++env->id_gen; |
| 4682 | /* something was added to pkt_ptr, set range to zero */ |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 4683 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4684 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4685 | break; |
| 4686 | case BPF_SUB: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 4687 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 4688 | if (ret < 0) { |
| 4689 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); |
| 4690 | return ret; |
| 4691 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4692 | if (dst_reg == off_reg) { |
| 4693 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4694 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 4695 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4696 | return -EACCES; |
| 4697 | } |
| 4698 | /* We don't allow subtraction from FP, because (according to |
| 4699 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 4700 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 4701 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4702 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4703 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 4704 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4705 | return -EACCES; |
| 4706 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4707 | if (known && (ptr_reg->off - smin_val == |
| 4708 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4709 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4710 | dst_reg->smin_value = smin_ptr; |
| 4711 | dst_reg->smax_value = smax_ptr; |
| 4712 | dst_reg->umin_value = umin_ptr; |
| 4713 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4714 | dst_reg->var_off = ptr_reg->var_off; |
| 4715 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4716 | dst_reg->off = ptr_reg->off - smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 4717 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4718 | break; |
| 4719 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4720 | /* A new variable offset is created. If the subtrahend is known |
| 4721 | * nonnegative, then any reg->range we had before is still good. |
| 4722 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4723 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 4724 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 4725 | /* Overflow possible, we know nothing */ |
| 4726 | dst_reg->smin_value = S64_MIN; |
| 4727 | dst_reg->smax_value = S64_MAX; |
| 4728 | } else { |
| 4729 | dst_reg->smin_value = smin_ptr - smax_val; |
| 4730 | dst_reg->smax_value = smax_ptr - smin_val; |
| 4731 | } |
| 4732 | if (umin_ptr < umax_val) { |
| 4733 | /* Overflow possible, we know nothing */ |
| 4734 | dst_reg->umin_value = 0; |
| 4735 | dst_reg->umax_value = U64_MAX; |
| 4736 | } else { |
| 4737 | /* Cannot overflow (as long as bounds are consistent) */ |
| 4738 | dst_reg->umin_value = umin_ptr - umax_val; |
| 4739 | dst_reg->umax_value = umax_ptr - umin_val; |
| 4740 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4741 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 4742 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 4743 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4744 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4745 | dst_reg->id = ++env->id_gen; |
| 4746 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4747 | if (smin_val < 0) |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 4748 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4749 | } |
| 4750 | break; |
| 4751 | case BPF_AND: |
| 4752 | case BPF_OR: |
| 4753 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4754 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 4755 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 4756 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4757 | return -EACCES; |
| 4758 | default: |
| 4759 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4760 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 4761 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4762 | return -EACCES; |
| 4763 | } |
| 4764 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 4765 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 4766 | return -EINVAL; |
| 4767 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4768 | __update_reg_bounds(dst_reg); |
| 4769 | __reg_deduce_bounds(dst_reg); |
| 4770 | __reg_bound_offset(dst_reg); |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 4771 | |
| 4772 | /* For unprivileged we require that resulting offset must be in bounds |
| 4773 | * in order to be able to sanitize access later on. |
| 4774 | */ |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 4775 | if (!env->allow_ptr_leaks) { |
| 4776 | if (dst_reg->type == PTR_TO_MAP_VALUE && |
| 4777 | check_map_access(env, dst, dst_reg->off, 1, false)) { |
| 4778 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
| 4779 | "prohibited for !root\n", dst); |
| 4780 | return -EACCES; |
| 4781 | } else if (dst_reg->type == PTR_TO_STACK && |
| 4782 | check_stack_access(env, dst_reg, dst_reg->off + |
| 4783 | dst_reg->var_off.value, 1)) { |
| 4784 | verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 4785 | "prohibited for !root\n", dst); |
| 4786 | return -EACCES; |
| 4787 | } |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 4788 | } |
| 4789 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4790 | return 0; |
| 4791 | } |
| 4792 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4793 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 4794 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 4795 | * need extra checks in the 32-bit case. |
| 4796 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4797 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 4798 | struct bpf_insn *insn, |
| 4799 | struct bpf_reg_state *dst_reg, |
| 4800 | struct bpf_reg_state src_reg) |
| 4801 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4802 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4803 | u8 opcode = BPF_OP(insn->code); |
| 4804 | bool src_known, dst_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4805 | s64 smin_val, smax_val; |
| 4806 | u64 umin_val, umax_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4807 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 4808 | u32 dst = insn->dst_reg; |
| 4809 | int ret; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4810 | |
Jann Horn | b799207 | 2018-10-05 18:17:59 +0200 | [diff] [blame] | 4811 | if (insn_bitness == 32) { |
| 4812 | /* Relevant for 32-bit RSH: Information can propagate towards |
| 4813 | * LSB, so it isn't sufficient to only truncate the output to |
| 4814 | * 32 bits. |
| 4815 | */ |
| 4816 | coerce_reg_to_size(dst_reg, 4); |
| 4817 | coerce_reg_to_size(&src_reg, 4); |
| 4818 | } |
| 4819 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4820 | smin_val = src_reg.smin_value; |
| 4821 | smax_val = src_reg.smax_value; |
| 4822 | umin_val = src_reg.umin_value; |
| 4823 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4824 | src_known = tnum_is_const(src_reg.var_off); |
| 4825 | dst_known = tnum_is_const(dst_reg->var_off); |
| 4826 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 4827 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
| 4828 | smin_val > smax_val || umin_val > umax_val) { |
| 4829 | /* Taint dst register if offset had invalid bounds derived from |
| 4830 | * e.g. dead branches. |
| 4831 | */ |
| 4832 | __mark_reg_unknown(dst_reg); |
| 4833 | return 0; |
| 4834 | } |
| 4835 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 4836 | if (!src_known && |
| 4837 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
| 4838 | __mark_reg_unknown(dst_reg); |
| 4839 | return 0; |
| 4840 | } |
| 4841 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4842 | switch (opcode) { |
| 4843 | case BPF_ADD: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 4844 | ret = sanitize_val_alu(env, insn); |
| 4845 | if (ret < 0) { |
| 4846 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); |
| 4847 | return ret; |
| 4848 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4849 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 4850 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 4851 | dst_reg->smin_value = S64_MIN; |
| 4852 | dst_reg->smax_value = S64_MAX; |
| 4853 | } else { |
| 4854 | dst_reg->smin_value += smin_val; |
| 4855 | dst_reg->smax_value += smax_val; |
| 4856 | } |
| 4857 | if (dst_reg->umin_value + umin_val < umin_val || |
| 4858 | dst_reg->umax_value + umax_val < umax_val) { |
| 4859 | dst_reg->umin_value = 0; |
| 4860 | dst_reg->umax_value = U64_MAX; |
| 4861 | } else { |
| 4862 | dst_reg->umin_value += umin_val; |
| 4863 | dst_reg->umax_value += umax_val; |
| 4864 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4865 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 4866 | break; |
| 4867 | case BPF_SUB: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 4868 | ret = sanitize_val_alu(env, insn); |
| 4869 | if (ret < 0) { |
| 4870 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); |
| 4871 | return ret; |
| 4872 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4873 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 4874 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 4875 | /* Overflow possible, we know nothing */ |
| 4876 | dst_reg->smin_value = S64_MIN; |
| 4877 | dst_reg->smax_value = S64_MAX; |
| 4878 | } else { |
| 4879 | dst_reg->smin_value -= smax_val; |
| 4880 | dst_reg->smax_value -= smin_val; |
| 4881 | } |
| 4882 | if (dst_reg->umin_value < umax_val) { |
| 4883 | /* Overflow possible, we know nothing */ |
| 4884 | dst_reg->umin_value = 0; |
| 4885 | dst_reg->umax_value = U64_MAX; |
| 4886 | } else { |
| 4887 | /* Cannot overflow (as long as bounds are consistent) */ |
| 4888 | dst_reg->umin_value -= umax_val; |
| 4889 | dst_reg->umax_value -= umin_val; |
| 4890 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4891 | 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] | 4892 | break; |
| 4893 | case BPF_MUL: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4894 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 4895 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4896 | /* Ain't nobody got time to multiply that sign */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4897 | __mark_reg_unbounded(dst_reg); |
| 4898 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4899 | break; |
| 4900 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4901 | /* Both values are positive, so we can work with unsigned and |
| 4902 | * copy the result to signed (unless it exceeds S64_MAX). |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4903 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4904 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 4905 | /* Potential overflow, we know nothing */ |
| 4906 | __mark_reg_unbounded(dst_reg); |
| 4907 | /* (except what we can learn from the var_off) */ |
| 4908 | __update_reg_bounds(dst_reg); |
| 4909 | break; |
| 4910 | } |
| 4911 | dst_reg->umin_value *= umin_val; |
| 4912 | dst_reg->umax_value *= umax_val; |
| 4913 | if (dst_reg->umax_value > S64_MAX) { |
| 4914 | /* Overflow possible, we know nothing */ |
| 4915 | dst_reg->smin_value = S64_MIN; |
| 4916 | dst_reg->smax_value = S64_MAX; |
| 4917 | } else { |
| 4918 | dst_reg->smin_value = dst_reg->umin_value; |
| 4919 | dst_reg->smax_value = dst_reg->umax_value; |
| 4920 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4921 | break; |
| 4922 | case BPF_AND: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4923 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4924 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 4925 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4926 | break; |
| 4927 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4928 | /* We get our minimum from the var_off, since that's inherently |
| 4929 | * bitwise. Our maximum is the minimum of the operands' maxima. |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 4930 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4931 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4932 | dst_reg->umin_value = dst_reg->var_off.value; |
| 4933 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 4934 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 4935 | /* Lose signed bounds when ANDing negative numbers, |
| 4936 | * ain't nobody got time for that. |
| 4937 | */ |
| 4938 | dst_reg->smin_value = S64_MIN; |
| 4939 | dst_reg->smax_value = S64_MAX; |
| 4940 | } else { |
| 4941 | /* ANDing two positives gives a positive, so safe to |
| 4942 | * cast result into s64. |
| 4943 | */ |
| 4944 | dst_reg->smin_value = dst_reg->umin_value; |
| 4945 | dst_reg->smax_value = dst_reg->umax_value; |
| 4946 | } |
| 4947 | /* We may learn something more from the var_off */ |
| 4948 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4949 | break; |
| 4950 | case BPF_OR: |
| 4951 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4952 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
| 4953 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4954 | break; |
| 4955 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4956 | /* We get our maximum from the var_off, and our minimum is the |
| 4957 | * maximum of the operands' minima |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4958 | */ |
| 4959 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4960 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 4961 | dst_reg->umax_value = dst_reg->var_off.value | |
| 4962 | dst_reg->var_off.mask; |
| 4963 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 4964 | /* Lose signed bounds when ORing negative numbers, |
| 4965 | * ain't nobody got time for that. |
| 4966 | */ |
| 4967 | dst_reg->smin_value = S64_MIN; |
| 4968 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4969 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4970 | /* ORing two positives gives a positive, so safe to |
| 4971 | * cast result into s64. |
| 4972 | */ |
| 4973 | dst_reg->smin_value = dst_reg->umin_value; |
| 4974 | dst_reg->smax_value = dst_reg->umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4975 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4976 | /* We may learn something more from the var_off */ |
| 4977 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4978 | break; |
| 4979 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4980 | if (umax_val >= insn_bitness) { |
| 4981 | /* Shifts greater than 31 or 63 are undefined. |
| 4982 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4983 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4984 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4985 | break; |
| 4986 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4987 | /* We lose all sign bit information (except what we can pick |
| 4988 | * up from var_off) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4989 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4990 | dst_reg->smin_value = S64_MIN; |
| 4991 | dst_reg->smax_value = S64_MAX; |
| 4992 | /* If we might shift our top bit out, then we know nothing */ |
| 4993 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 4994 | dst_reg->umin_value = 0; |
| 4995 | dst_reg->umax_value = U64_MAX; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 4996 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4997 | dst_reg->umin_value <<= umin_val; |
| 4998 | dst_reg->umax_value <<= umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 4999 | } |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 5000 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5001 | /* We may learn something more from the var_off */ |
| 5002 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5003 | break; |
| 5004 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 5005 | if (umax_val >= insn_bitness) { |
| 5006 | /* Shifts greater than 31 or 63 are undefined. |
| 5007 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5008 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5009 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5010 | break; |
| 5011 | } |
Edward Cree | 4374f25 | 2017-12-18 20:11:53 -0800 | [diff] [blame] | 5012 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 5013 | * be negative, then either: |
| 5014 | * 1) src_reg might be zero, so the sign bit of the result is |
| 5015 | * unknown, so we lose our signed bounds |
| 5016 | * 2) it's known negative, thus the unsigned bounds capture the |
| 5017 | * signed bounds |
| 5018 | * 3) the signed bounds cross zero, so they tell us nothing |
| 5019 | * about the result |
| 5020 | * If the value in dst_reg is known nonnegative, then again the |
| 5021 | * unsigned bounts capture the signed bounds. |
| 5022 | * Thus, in all cases it suffices to blow away our signed bounds |
| 5023 | * and rely on inferring new ones from the unsigned bounds and |
| 5024 | * var_off of the result. |
| 5025 | */ |
| 5026 | dst_reg->smin_value = S64_MIN; |
| 5027 | dst_reg->smax_value = S64_MAX; |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 5028 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5029 | dst_reg->umin_value >>= umax_val; |
| 5030 | dst_reg->umax_value >>= umin_val; |
| 5031 | /* We may learn something more from the var_off */ |
| 5032 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5033 | break; |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 5034 | case BPF_ARSH: |
| 5035 | if (umax_val >= insn_bitness) { |
| 5036 | /* Shifts greater than 31 or 63 are undefined. |
| 5037 | * This includes shifts by a negative number. |
| 5038 | */ |
| 5039 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 5040 | break; |
| 5041 | } |
| 5042 | |
| 5043 | /* Upon reaching here, src_known is true and |
| 5044 | * umax_val is equal to umin_val. |
| 5045 | */ |
| 5046 | dst_reg->smin_value >>= umin_val; |
| 5047 | dst_reg->smax_value >>= umin_val; |
| 5048 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val); |
| 5049 | |
| 5050 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 5051 | * dst_reg var_off to refine the result. |
| 5052 | */ |
| 5053 | dst_reg->umin_value = 0; |
| 5054 | dst_reg->umax_value = U64_MAX; |
| 5055 | __update_reg_bounds(dst_reg); |
| 5056 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5057 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5058 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5059 | break; |
| 5060 | } |
| 5061 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 5062 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 5063 | /* 32-bit ALU ops are (32,32)->32 */ |
| 5064 | coerce_reg_to_size(dst_reg, 4); |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 5065 | } |
| 5066 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5067 | __reg_deduce_bounds(dst_reg); |
| 5068 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5069 | return 0; |
| 5070 | } |
| 5071 | |
| 5072 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 5073 | * and var_off. |
| 5074 | */ |
| 5075 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 5076 | struct bpf_insn *insn) |
| 5077 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5078 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5079 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 5080 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5081 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 5082 | u8 opcode = BPF_OP(insn->code); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 5083 | int err; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5084 | |
| 5085 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5086 | src_reg = NULL; |
| 5087 | if (dst_reg->type != SCALAR_VALUE) |
| 5088 | ptr_reg = dst_reg; |
| 5089 | if (BPF_SRC(insn->code) == BPF_X) { |
| 5090 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5091 | if (src_reg->type != SCALAR_VALUE) { |
| 5092 | if (dst_reg->type != SCALAR_VALUE) { |
| 5093 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5094 | * an arbitrary scalar. Disallow all math except |
| 5095 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5096 | */ |
Alexei Starovoitov | dd06682 | 2018-09-12 14:06:10 -0700 | [diff] [blame] | 5097 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5098 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 5099 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5100 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5101 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 5102 | insn->dst_reg, |
| 5103 | bpf_alu_string[opcode >> 4]); |
| 5104 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5105 | } else { |
| 5106 | /* scalar += pointer |
| 5107 | * This is legal, but we have to reverse our |
| 5108 | * src/dest handling in computing the range |
| 5109 | */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 5110 | err = mark_chain_precision(env, insn->dst_reg); |
| 5111 | if (err) |
| 5112 | return err; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5113 | return adjust_ptr_min_max_vals(env, insn, |
| 5114 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5115 | } |
| 5116 | } else if (ptr_reg) { |
| 5117 | /* pointer += scalar */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 5118 | err = mark_chain_precision(env, insn->src_reg); |
| 5119 | if (err) |
| 5120 | return err; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5121 | return adjust_ptr_min_max_vals(env, insn, |
| 5122 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5123 | } |
| 5124 | } else { |
| 5125 | /* Pretend the src is a reg with a known value, since we only |
| 5126 | * need to be able to read from this state. |
| 5127 | */ |
| 5128 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5129 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5130 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5131 | if (ptr_reg) /* pointer += K */ |
| 5132 | return adjust_ptr_min_max_vals(env, insn, |
| 5133 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5134 | } |
| 5135 | |
| 5136 | /* Got here implies adding two SCALAR_VALUEs */ |
| 5137 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5138 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5139 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5140 | return -EINVAL; |
| 5141 | } |
| 5142 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5143 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5144 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5145 | return -EINVAL; |
| 5146 | } |
| 5147 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5148 | } |
| 5149 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5150 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5151 | 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] | 5152 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5153 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5154 | u8 opcode = BPF_OP(insn->code); |
| 5155 | int err; |
| 5156 | |
| 5157 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 5158 | if (opcode == BPF_NEG) { |
| 5159 | if (BPF_SRC(insn->code) != 0 || |
| 5160 | insn->src_reg != BPF_REG_0 || |
| 5161 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5162 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5163 | return -EINVAL; |
| 5164 | } |
| 5165 | } else { |
| 5166 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 5167 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 5168 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5169 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5170 | return -EINVAL; |
| 5171 | } |
| 5172 | } |
| 5173 | |
| 5174 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5175 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5176 | if (err) |
| 5177 | return err; |
| 5178 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5179 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5180 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5181 | insn->dst_reg); |
| 5182 | return -EACCES; |
| 5183 | } |
| 5184 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5185 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5186 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5187 | if (err) |
| 5188 | return err; |
| 5189 | |
| 5190 | } else if (opcode == BPF_MOV) { |
| 5191 | |
| 5192 | if (BPF_SRC(insn->code) == BPF_X) { |
| 5193 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5194 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5195 | return -EINVAL; |
| 5196 | } |
| 5197 | |
| 5198 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5199 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5200 | if (err) |
| 5201 | return err; |
| 5202 | } else { |
| 5203 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5204 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5205 | return -EINVAL; |
| 5206 | } |
| 5207 | } |
| 5208 | |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 5209 | /* check dest operand, mark as required later */ |
| 5210 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5211 | if (err) |
| 5212 | return err; |
| 5213 | |
| 5214 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 5215 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
| 5216 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; |
| 5217 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5218 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 5219 | /* case: R1 = R2 |
| 5220 | * copy register state to dest reg |
| 5221 | */ |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 5222 | *dst_reg = *src_reg; |
| 5223 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 5224 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5225 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5226 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5227 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5228 | verbose(env, |
| 5229 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5230 | insn->src_reg); |
| 5231 | return -EACCES; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 5232 | } else if (src_reg->type == SCALAR_VALUE) { |
| 5233 | *dst_reg = *src_reg; |
| 5234 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 5235 | dst_reg->subreg_def = env->insn_idx + 1; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 5236 | } else { |
| 5237 | mark_reg_unknown(env, regs, |
| 5238 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5239 | } |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 5240 | coerce_reg_to_size(dst_reg, 4); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5241 | } |
| 5242 | } else { |
| 5243 | /* case: R = imm |
| 5244 | * remember the value we stored into this reg |
| 5245 | */ |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 5246 | /* clear any state __mark_reg_known doesn't set */ |
| 5247 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5248 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 5249 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 5250 | __mark_reg_known(regs + insn->dst_reg, |
| 5251 | insn->imm); |
| 5252 | } else { |
| 5253 | __mark_reg_known(regs + insn->dst_reg, |
| 5254 | (u32)insn->imm); |
| 5255 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5256 | } |
| 5257 | |
| 5258 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5259 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5260 | return -EINVAL; |
| 5261 | |
| 5262 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 5263 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5264 | if (BPF_SRC(insn->code) == BPF_X) { |
| 5265 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5266 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5267 | return -EINVAL; |
| 5268 | } |
| 5269 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5270 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5271 | if (err) |
| 5272 | return err; |
| 5273 | } else { |
| 5274 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5275 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5276 | return -EINVAL; |
| 5277 | } |
| 5278 | } |
| 5279 | |
| 5280 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5281 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5282 | if (err) |
| 5283 | return err; |
| 5284 | |
| 5285 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 5286 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5287 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5288 | return -EINVAL; |
| 5289 | } |
| 5290 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 5291 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 5292 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 5293 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 5294 | |
| 5295 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5296 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 5297 | return -EINVAL; |
| 5298 | } |
| 5299 | } |
| 5300 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 5301 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5302 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 5303 | if (err) |
| 5304 | return err; |
| 5305 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5306 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5307 | } |
| 5308 | |
| 5309 | return 0; |
| 5310 | } |
| 5311 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5312 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
| 5313 | struct bpf_reg_state *dst_reg, |
| 5314 | enum bpf_reg_type type, u16 new_range) |
| 5315 | { |
| 5316 | struct bpf_reg_state *reg; |
| 5317 | int i; |
| 5318 | |
| 5319 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 5320 | reg = &state->regs[i]; |
| 5321 | if (reg->type == type && reg->id == dst_reg->id) |
| 5322 | /* keep the maximum range already checked */ |
| 5323 | reg->range = max(reg->range, new_range); |
| 5324 | } |
| 5325 | |
| 5326 | bpf_for_each_spilled_reg(i, state, reg) { |
| 5327 | if (!reg) |
| 5328 | continue; |
| 5329 | if (reg->type == type && reg->id == dst_reg->id) |
| 5330 | reg->range = max(reg->range, new_range); |
| 5331 | } |
| 5332 | } |
| 5333 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5334 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5335 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 5336 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 5337 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5338 | { |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 5339 | u16 new_range; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5340 | int i; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 5341 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 5342 | if (dst_reg->off < 0 || |
| 5343 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5344 | /* This doesn't give us any range */ |
| 5345 | return; |
| 5346 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5347 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 5348 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5349 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 5350 | * than pkt_end, but that's because it's also less than pkt. |
| 5351 | */ |
| 5352 | return; |
| 5353 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 5354 | new_range = dst_reg->off; |
| 5355 | if (range_right_open) |
| 5356 | new_range--; |
| 5357 | |
| 5358 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 5359 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 5360 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 5361 | * |
| 5362 | * r2 = r3; |
| 5363 | * r2 += 8; |
| 5364 | * if (r2 > pkt_end) goto <handle exception> |
| 5365 | * <access okay> |
| 5366 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 5367 | * r2 = r3; |
| 5368 | * r2 += 8; |
| 5369 | * if (r2 < pkt_end) goto <access okay> |
| 5370 | * <handle exception> |
| 5371 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 5372 | * Where: |
| 5373 | * r2 == dst_reg, pkt_end == src_reg |
| 5374 | * r2=pkt(id=n,off=8,r=0) |
| 5375 | * r3=pkt(id=n,off=0,r=0) |
| 5376 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 5377 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 5378 | * |
| 5379 | * r2 = r3; |
| 5380 | * r2 += 8; |
| 5381 | * if (pkt_end >= r2) goto <access okay> |
| 5382 | * <handle exception> |
| 5383 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 5384 | * r2 = r3; |
| 5385 | * r2 += 8; |
| 5386 | * if (pkt_end <= r2) goto <handle exception> |
| 5387 | * <access okay> |
| 5388 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 5389 | * Where: |
| 5390 | * pkt_end == dst_reg, r2 == src_reg |
| 5391 | * r2=pkt(id=n,off=8,r=0) |
| 5392 | * r3=pkt(id=n,off=0,r=0) |
| 5393 | * |
| 5394 | * 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] | 5395 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 5396 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 5397 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5398 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 5399 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5400 | /* If our ids match, then we must have the same max_value. And we |
| 5401 | * don't care about the other reg's fixed offset, since if it's too big |
| 5402 | * the range won't allow anything. |
| 5403 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 5404 | */ |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5405 | for (i = 0; i <= vstate->curframe; i++) |
| 5406 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, |
| 5407 | new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5408 | } |
| 5409 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5410 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
| 5411 | * and return: |
| 5412 | * 1 - branch will be taken and "goto target" will be executed |
| 5413 | * 0 - branch will not be taken and fall-through to next insn |
| 5414 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10] |
| 5415 | */ |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5416 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
| 5417 | bool is_jmp32) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5418 | { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5419 | struct bpf_reg_state reg_lo; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5420 | s64 sval; |
| 5421 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5422 | if (__is_pointer_value(false, reg)) |
| 5423 | return -1; |
| 5424 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5425 | if (is_jmp32) { |
| 5426 | reg_lo = *reg; |
| 5427 | reg = ®_lo; |
| 5428 | /* For JMP32, only low 32 bits are compared, coerce_reg_to_size |
| 5429 | * could truncate high bits and update umin/umax according to |
| 5430 | * information of low bits. |
| 5431 | */ |
| 5432 | coerce_reg_to_size(reg, 4); |
| 5433 | /* smin/smax need special handling. For example, after coerce, |
| 5434 | * if smin_value is 0x00000000ffffffffLL, the value is -1 when |
| 5435 | * used as operand to JMP32. It is a negative number from s32's |
| 5436 | * point of view, while it is a positive number when seen as |
| 5437 | * s64. The smin/smax are kept as s64, therefore, when used with |
| 5438 | * JMP32, they need to be transformed into s32, then sign |
| 5439 | * extended back to s64. |
| 5440 | * |
| 5441 | * Also, smin/smax were copied from umin/umax. If umin/umax has |
| 5442 | * different sign bit, then min/max relationship doesn't |
| 5443 | * maintain after casting into s32, for this case, set smin/smax |
| 5444 | * to safest range. |
| 5445 | */ |
| 5446 | if ((reg->umax_value ^ reg->umin_value) & |
| 5447 | (1ULL << 31)) { |
| 5448 | reg->smin_value = S32_MIN; |
| 5449 | reg->smax_value = S32_MAX; |
| 5450 | } |
| 5451 | reg->smin_value = (s64)(s32)reg->smin_value; |
| 5452 | reg->smax_value = (s64)(s32)reg->smax_value; |
| 5453 | |
| 5454 | val = (u32)val; |
| 5455 | sval = (s64)(s32)val; |
| 5456 | } else { |
| 5457 | sval = (s64)val; |
| 5458 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5459 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5460 | switch (opcode) { |
| 5461 | case BPF_JEQ: |
| 5462 | if (tnum_is_const(reg->var_off)) |
| 5463 | return !!tnum_equals_const(reg->var_off, val); |
| 5464 | break; |
| 5465 | case BPF_JNE: |
| 5466 | if (tnum_is_const(reg->var_off)) |
| 5467 | return !tnum_equals_const(reg->var_off, val); |
| 5468 | break; |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 5469 | case BPF_JSET: |
| 5470 | if ((~reg->var_off.mask & reg->var_off.value) & val) |
| 5471 | return 1; |
| 5472 | if (!((reg->var_off.mask | reg->var_off.value) & val)) |
| 5473 | return 0; |
| 5474 | break; |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5475 | case BPF_JGT: |
| 5476 | if (reg->umin_value > val) |
| 5477 | return 1; |
| 5478 | else if (reg->umax_value <= val) |
| 5479 | return 0; |
| 5480 | break; |
| 5481 | case BPF_JSGT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5482 | if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5483 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5484 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5485 | return 0; |
| 5486 | break; |
| 5487 | case BPF_JLT: |
| 5488 | if (reg->umax_value < val) |
| 5489 | return 1; |
| 5490 | else if (reg->umin_value >= val) |
| 5491 | return 0; |
| 5492 | break; |
| 5493 | case BPF_JSLT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5494 | if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5495 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5496 | else if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5497 | return 0; |
| 5498 | break; |
| 5499 | case BPF_JGE: |
| 5500 | if (reg->umin_value >= val) |
| 5501 | return 1; |
| 5502 | else if (reg->umax_value < val) |
| 5503 | return 0; |
| 5504 | break; |
| 5505 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5506 | if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5507 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5508 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5509 | return 0; |
| 5510 | break; |
| 5511 | case BPF_JLE: |
| 5512 | if (reg->umax_value <= val) |
| 5513 | return 1; |
| 5514 | else if (reg->umin_value > val) |
| 5515 | return 0; |
| 5516 | break; |
| 5517 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5518 | if (reg->smax_value <= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5519 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5520 | else if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5521 | return 0; |
| 5522 | break; |
| 5523 | } |
| 5524 | |
| 5525 | return -1; |
| 5526 | } |
| 5527 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5528 | /* Generate min value of the high 32-bit from TNUM info. */ |
| 5529 | static u64 gen_hi_min(struct tnum var) |
| 5530 | { |
| 5531 | return var.value & ~0xffffffffULL; |
| 5532 | } |
| 5533 | |
| 5534 | /* Generate max value of the high 32-bit from TNUM info. */ |
| 5535 | static u64 gen_hi_max(struct tnum var) |
| 5536 | { |
| 5537 | return (var.value | var.mask) & ~0xffffffffULL; |
| 5538 | } |
| 5539 | |
| 5540 | /* Return true if VAL is compared with a s64 sign extended from s32, and they |
| 5541 | * are with the same signedness. |
| 5542 | */ |
| 5543 | static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg) |
| 5544 | { |
| 5545 | return ((s32)sval >= 0 && |
| 5546 | reg->smin_value >= 0 && reg->smax_value <= S32_MAX) || |
| 5547 | ((s32)sval < 0 && |
| 5548 | reg->smax_value <= 0 && reg->smin_value >= S32_MIN); |
| 5549 | } |
| 5550 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5551 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 5552 | * variable register that we are working on, and src_reg is a constant or we're |
| 5553 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5554 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5555 | */ |
| 5556 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 5557 | struct bpf_reg_state *false_reg, u64 val, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5558 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5559 | { |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5560 | s64 sval; |
| 5561 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5562 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 5563 | * variable offset from the compare (unless src_reg were a pointer into |
| 5564 | * the same object, but we don't bother with that. |
| 5565 | * Since false_reg and true_reg have the same type by construction, we |
| 5566 | * only need to check one of them for pointerness. |
| 5567 | */ |
| 5568 | if (__is_pointer_value(false, false_reg)) |
| 5569 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 5570 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5571 | val = is_jmp32 ? (u32)val : val; |
| 5572 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5573 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5574 | switch (opcode) { |
| 5575 | case BPF_JEQ: |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5576 | case BPF_JNE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5577 | { |
| 5578 | struct bpf_reg_state *reg = |
| 5579 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 5580 | |
| 5581 | /* For BPF_JEQ, if this is false we know nothing Jon Snow, but |
| 5582 | * if it is true we know the value for sure. Likewise for |
| 5583 | * BPF_JNE. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5584 | */ |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5585 | if (is_jmp32) { |
| 5586 | u64 old_v = reg->var_off.value; |
| 5587 | u64 hi_mask = ~0xffffffffULL; |
| 5588 | |
| 5589 | reg->var_off.value = (old_v & hi_mask) | val; |
| 5590 | reg->var_off.mask &= hi_mask; |
| 5591 | } else { |
| 5592 | __mark_reg_known(reg, val); |
| 5593 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5594 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5595 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 5596 | case BPF_JSET: |
| 5597 | false_reg->var_off = tnum_and(false_reg->var_off, |
| 5598 | tnum_const(~val)); |
| 5599 | if (is_power_of_2(val)) |
| 5600 | true_reg->var_off = tnum_or(true_reg->var_off, |
| 5601 | tnum_const(val)); |
| 5602 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5603 | case BPF_JGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5604 | case BPF_JGT: |
| 5605 | { |
| 5606 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; |
| 5607 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; |
| 5608 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5609 | if (is_jmp32) { |
| 5610 | false_umax += gen_hi_max(false_reg->var_off); |
| 5611 | true_umin += gen_hi_min(true_reg->var_off); |
| 5612 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5613 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 5614 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5615 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5616 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5617 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5618 | case BPF_JSGT: |
| 5619 | { |
| 5620 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; |
| 5621 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; |
| 5622 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5623 | /* If the full s64 was not sign-extended from s32 then don't |
| 5624 | * deduct further info. |
| 5625 | */ |
| 5626 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 5627 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5628 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 5629 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5630 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5631 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 5632 | case BPF_JLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5633 | case BPF_JLT: |
| 5634 | { |
| 5635 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; |
| 5636 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; |
| 5637 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5638 | if (is_jmp32) { |
| 5639 | false_umin += gen_hi_min(false_reg->var_off); |
| 5640 | true_umax += gen_hi_max(true_reg->var_off); |
| 5641 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5642 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 5643 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 5644 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5645 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 5646 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5647 | case BPF_JSLT: |
| 5648 | { |
| 5649 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; |
| 5650 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; |
| 5651 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5652 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 5653 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5654 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 5655 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 5656 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5657 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5658 | default: |
| 5659 | break; |
| 5660 | } |
| 5661 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5662 | __reg_deduce_bounds(false_reg); |
| 5663 | __reg_deduce_bounds(true_reg); |
| 5664 | /* We might have learned some bits from the bounds. */ |
| 5665 | __reg_bound_offset(false_reg); |
| 5666 | __reg_bound_offset(true_reg); |
Yonghong Song | 581738a | 2019-11-21 09:06:50 -0800 | [diff] [blame] | 5667 | if (is_jmp32) { |
| 5668 | __reg_bound_offset32(false_reg); |
| 5669 | __reg_bound_offset32(true_reg); |
| 5670 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5671 | /* Intersecting with the old var_off might have improved our bounds |
| 5672 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 5673 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 5674 | */ |
| 5675 | __update_reg_bounds(false_reg); |
| 5676 | __update_reg_bounds(true_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5677 | } |
| 5678 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5679 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 5680 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5681 | */ |
| 5682 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 5683 | struct bpf_reg_state *false_reg, u64 val, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5684 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5685 | { |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5686 | s64 sval; |
| 5687 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5688 | if (__is_pointer_value(false, false_reg)) |
| 5689 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 5690 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5691 | val = is_jmp32 ? (u32)val : val; |
| 5692 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5693 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5694 | switch (opcode) { |
| 5695 | case BPF_JEQ: |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5696 | case BPF_JNE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5697 | { |
| 5698 | struct bpf_reg_state *reg = |
| 5699 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 5700 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5701 | if (is_jmp32) { |
| 5702 | u64 old_v = reg->var_off.value; |
| 5703 | u64 hi_mask = ~0xffffffffULL; |
| 5704 | |
| 5705 | reg->var_off.value = (old_v & hi_mask) | val; |
| 5706 | reg->var_off.mask &= hi_mask; |
| 5707 | } else { |
| 5708 | __mark_reg_known(reg, val); |
| 5709 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5710 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5711 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 5712 | case BPF_JSET: |
| 5713 | false_reg->var_off = tnum_and(false_reg->var_off, |
| 5714 | tnum_const(~val)); |
| 5715 | if (is_power_of_2(val)) |
| 5716 | true_reg->var_off = tnum_or(true_reg->var_off, |
| 5717 | tnum_const(val)); |
| 5718 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5719 | case BPF_JGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5720 | case BPF_JGT: |
| 5721 | { |
| 5722 | u64 false_umin = opcode == BPF_JGT ? val : val + 1; |
| 5723 | u64 true_umax = opcode == BPF_JGT ? val - 1 : val; |
| 5724 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5725 | if (is_jmp32) { |
| 5726 | false_umin += gen_hi_min(false_reg->var_off); |
| 5727 | true_umax += gen_hi_max(true_reg->var_off); |
| 5728 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5729 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 5730 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5731 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5732 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5733 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5734 | case BPF_JSGT: |
| 5735 | { |
| 5736 | s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1; |
| 5737 | s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval; |
| 5738 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5739 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 5740 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5741 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 5742 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5743 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5744 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 5745 | case BPF_JLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5746 | case BPF_JLT: |
| 5747 | { |
| 5748 | u64 false_umax = opcode == BPF_JLT ? val : val - 1; |
| 5749 | u64 true_umin = opcode == BPF_JLT ? val + 1 : val; |
| 5750 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5751 | if (is_jmp32) { |
| 5752 | false_umax += gen_hi_max(false_reg->var_off); |
| 5753 | true_umin += gen_hi_min(true_reg->var_off); |
| 5754 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5755 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 5756 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 5757 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5758 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 5759 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5760 | case BPF_JSLT: |
| 5761 | { |
| 5762 | s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1; |
| 5763 | s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval; |
| 5764 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5765 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 5766 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5767 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 5768 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 5769 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5770 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5771 | default: |
| 5772 | break; |
| 5773 | } |
| 5774 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5775 | __reg_deduce_bounds(false_reg); |
| 5776 | __reg_deduce_bounds(true_reg); |
| 5777 | /* We might have learned some bits from the bounds. */ |
| 5778 | __reg_bound_offset(false_reg); |
| 5779 | __reg_bound_offset(true_reg); |
Yonghong Song | 581738a | 2019-11-21 09:06:50 -0800 | [diff] [blame] | 5780 | if (is_jmp32) { |
| 5781 | __reg_bound_offset32(false_reg); |
| 5782 | __reg_bound_offset32(true_reg); |
| 5783 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5784 | /* Intersecting with the old var_off might have improved our bounds |
| 5785 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 5786 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 5787 | */ |
| 5788 | __update_reg_bounds(false_reg); |
| 5789 | __update_reg_bounds(true_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5790 | } |
| 5791 | |
| 5792 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 5793 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 5794 | struct bpf_reg_state *dst_reg) |
| 5795 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5796 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 5797 | dst_reg->umin_value); |
| 5798 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 5799 | dst_reg->umax_value); |
| 5800 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 5801 | dst_reg->smin_value); |
| 5802 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 5803 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5804 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 5805 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5806 | /* We might have learned new bounds from the var_off. */ |
| 5807 | __update_reg_bounds(src_reg); |
| 5808 | __update_reg_bounds(dst_reg); |
| 5809 | /* We might have learned something about the sign bit. */ |
| 5810 | __reg_deduce_bounds(src_reg); |
| 5811 | __reg_deduce_bounds(dst_reg); |
| 5812 | /* We might have learned some bits from the bounds. */ |
| 5813 | __reg_bound_offset(src_reg); |
| 5814 | __reg_bound_offset(dst_reg); |
| 5815 | /* Intersecting with the old var_off might have improved our bounds |
| 5816 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 5817 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 5818 | */ |
| 5819 | __update_reg_bounds(src_reg); |
| 5820 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5821 | } |
| 5822 | |
| 5823 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 5824 | struct bpf_reg_state *true_dst, |
| 5825 | struct bpf_reg_state *false_src, |
| 5826 | struct bpf_reg_state *false_dst, |
| 5827 | u8 opcode) |
| 5828 | { |
| 5829 | switch (opcode) { |
| 5830 | case BPF_JEQ: |
| 5831 | __reg_combine_min_max(true_src, true_dst); |
| 5832 | break; |
| 5833 | case BPF_JNE: |
| 5834 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5835 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 5836 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5837 | } |
| 5838 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5839 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 5840 | struct bpf_reg_state *reg, u32 id, |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5841 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5842 | { |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5843 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5844 | /* Old offset (both fixed and variable parts) should |
| 5845 | * have been known-zero, because we don't allow pointer |
| 5846 | * arithmetic on pointers that might be NULL. |
| 5847 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5848 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 5849 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5850 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5851 | __mark_reg_known_zero(reg); |
| 5852 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5853 | } |
| 5854 | if (is_null) { |
| 5855 | reg->type = SCALAR_VALUE; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5856 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
| 5857 | if (reg->map_ptr->inner_map_meta) { |
| 5858 | reg->type = CONST_PTR_TO_MAP; |
| 5859 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 5860 | } else if (reg->map_ptr->map_type == |
| 5861 | BPF_MAP_TYPE_XSKMAP) { |
| 5862 | reg->type = PTR_TO_XDP_SOCK; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5863 | } else { |
| 5864 | reg->type = PTR_TO_MAP_VALUE; |
| 5865 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5866 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
| 5867 | reg->type = PTR_TO_SOCKET; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 5868 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
| 5869 | reg->type = PTR_TO_SOCK_COMMON; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 5870 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
| 5871 | reg->type = PTR_TO_TCP_SOCK; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5872 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5873 | if (is_null) { |
| 5874 | /* We don't need id and ref_obj_id from this point |
| 5875 | * onwards anymore, thus we should better reset it, |
| 5876 | * so that state pruning has chances to take effect. |
| 5877 | */ |
| 5878 | reg->id = 0; |
| 5879 | reg->ref_obj_id = 0; |
| 5880 | } else if (!reg_may_point_to_spin_lock(reg)) { |
| 5881 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
| 5882 | * in release_reg_references(). |
| 5883 | * |
| 5884 | * reg->id is still used by spin_lock ptr. Other |
| 5885 | * than spin_lock ptr type, reg->id can be reset. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5886 | */ |
| 5887 | reg->id = 0; |
| 5888 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5889 | } |
| 5890 | } |
| 5891 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5892 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
| 5893 | bool is_null) |
| 5894 | { |
| 5895 | struct bpf_reg_state *reg; |
| 5896 | int i; |
| 5897 | |
| 5898 | for (i = 0; i < MAX_BPF_REG; i++) |
| 5899 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); |
| 5900 | |
| 5901 | bpf_for_each_spilled_reg(i, state, reg) { |
| 5902 | if (!reg) |
| 5903 | continue; |
| 5904 | mark_ptr_or_null_reg(state, reg, id, is_null); |
| 5905 | } |
| 5906 | } |
| 5907 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5908 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 5909 | * be folded together at some point. |
| 5910 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5911 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 5912 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5913 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5914 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5915 | struct bpf_reg_state *regs = state->regs; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5916 | u32 ref_obj_id = regs[regno].ref_obj_id; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 5917 | u32 id = regs[regno].id; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5918 | int i; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5919 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5920 | if (ref_obj_id && ref_obj_id == id && is_null) |
| 5921 | /* regs[regno] is in the " == NULL" branch. |
| 5922 | * No one could have freed the reference state before |
| 5923 | * doing the NULL check. |
| 5924 | */ |
| 5925 | WARN_ON_ONCE(release_reference_state(state, id)); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5926 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5927 | for (i = 0; i <= vstate->curframe; i++) |
| 5928 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5929 | } |
| 5930 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 5931 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 5932 | struct bpf_reg_state *dst_reg, |
| 5933 | struct bpf_reg_state *src_reg, |
| 5934 | struct bpf_verifier_state *this_branch, |
| 5935 | struct bpf_verifier_state *other_branch) |
| 5936 | { |
| 5937 | if (BPF_SRC(insn->code) != BPF_X) |
| 5938 | return false; |
| 5939 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5940 | /* Pointers are always 64-bit. */ |
| 5941 | if (BPF_CLASS(insn->code) == BPF_JMP32) |
| 5942 | return false; |
| 5943 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 5944 | switch (BPF_OP(insn->code)) { |
| 5945 | case BPF_JGT: |
| 5946 | if ((dst_reg->type == PTR_TO_PACKET && |
| 5947 | src_reg->type == PTR_TO_PACKET_END) || |
| 5948 | (dst_reg->type == PTR_TO_PACKET_META && |
| 5949 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 5950 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 5951 | find_good_pkt_pointers(this_branch, dst_reg, |
| 5952 | dst_reg->type, false); |
| 5953 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 5954 | src_reg->type == PTR_TO_PACKET) || |
| 5955 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 5956 | src_reg->type == PTR_TO_PACKET_META)) { |
| 5957 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 5958 | find_good_pkt_pointers(other_branch, src_reg, |
| 5959 | src_reg->type, true); |
| 5960 | } else { |
| 5961 | return false; |
| 5962 | } |
| 5963 | break; |
| 5964 | case BPF_JLT: |
| 5965 | if ((dst_reg->type == PTR_TO_PACKET && |
| 5966 | src_reg->type == PTR_TO_PACKET_END) || |
| 5967 | (dst_reg->type == PTR_TO_PACKET_META && |
| 5968 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 5969 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 5970 | find_good_pkt_pointers(other_branch, dst_reg, |
| 5971 | dst_reg->type, true); |
| 5972 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 5973 | src_reg->type == PTR_TO_PACKET) || |
| 5974 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 5975 | src_reg->type == PTR_TO_PACKET_META)) { |
| 5976 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 5977 | find_good_pkt_pointers(this_branch, src_reg, |
| 5978 | src_reg->type, false); |
| 5979 | } else { |
| 5980 | return false; |
| 5981 | } |
| 5982 | break; |
| 5983 | case BPF_JGE: |
| 5984 | if ((dst_reg->type == PTR_TO_PACKET && |
| 5985 | src_reg->type == PTR_TO_PACKET_END) || |
| 5986 | (dst_reg->type == PTR_TO_PACKET_META && |
| 5987 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 5988 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 5989 | find_good_pkt_pointers(this_branch, dst_reg, |
| 5990 | dst_reg->type, true); |
| 5991 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 5992 | src_reg->type == PTR_TO_PACKET) || |
| 5993 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 5994 | src_reg->type == PTR_TO_PACKET_META)) { |
| 5995 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 5996 | find_good_pkt_pointers(other_branch, src_reg, |
| 5997 | src_reg->type, false); |
| 5998 | } else { |
| 5999 | return false; |
| 6000 | } |
| 6001 | break; |
| 6002 | case BPF_JLE: |
| 6003 | if ((dst_reg->type == PTR_TO_PACKET && |
| 6004 | src_reg->type == PTR_TO_PACKET_END) || |
| 6005 | (dst_reg->type == PTR_TO_PACKET_META && |
| 6006 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 6007 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 6008 | find_good_pkt_pointers(other_branch, dst_reg, |
| 6009 | dst_reg->type, false); |
| 6010 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 6011 | src_reg->type == PTR_TO_PACKET) || |
| 6012 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 6013 | src_reg->type == PTR_TO_PACKET_META)) { |
| 6014 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 6015 | find_good_pkt_pointers(this_branch, src_reg, |
| 6016 | src_reg->type, true); |
| 6017 | } else { |
| 6018 | return false; |
| 6019 | } |
| 6020 | break; |
| 6021 | default: |
| 6022 | return false; |
| 6023 | } |
| 6024 | |
| 6025 | return true; |
| 6026 | } |
| 6027 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6028 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6029 | struct bpf_insn *insn, int *insn_idx) |
| 6030 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6031 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 6032 | struct bpf_verifier_state *other_branch; |
| 6033 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 6034 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6035 | u8 opcode = BPF_OP(insn->code); |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6036 | bool is_jmp32; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 6037 | int pred = -1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6038 | int err; |
| 6039 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6040 | /* Only conditional jumps are expected to reach here. */ |
| 6041 | if (opcode == BPF_JA || opcode > BPF_JSLE) { |
| 6042 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6043 | return -EINVAL; |
| 6044 | } |
| 6045 | |
| 6046 | if (BPF_SRC(insn->code) == BPF_X) { |
| 6047 | if (insn->imm != 0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6048 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6049 | return -EINVAL; |
| 6050 | } |
| 6051 | |
| 6052 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6053 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6054 | if (err) |
| 6055 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6056 | |
| 6057 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6058 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6059 | insn->src_reg); |
| 6060 | return -EACCES; |
| 6061 | } |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 6062 | src_reg = ®s[insn->src_reg]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6063 | } else { |
| 6064 | if (insn->src_reg != BPF_REG_0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6065 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6066 | return -EINVAL; |
| 6067 | } |
| 6068 | } |
| 6069 | |
| 6070 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6071 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6072 | if (err) |
| 6073 | return err; |
| 6074 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 6075 | dst_reg = ®s[insn->dst_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6076 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 6077 | |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 6078 | if (BPF_SRC(insn->code) == BPF_K) |
| 6079 | pred = is_branch_taken(dst_reg, insn->imm, |
| 6080 | opcode, is_jmp32); |
| 6081 | else if (src_reg->type == SCALAR_VALUE && |
| 6082 | tnum_is_const(src_reg->var_off)) |
| 6083 | pred = is_branch_taken(dst_reg, src_reg->var_off.value, |
| 6084 | opcode, is_jmp32); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 6085 | if (pred >= 0) { |
| 6086 | err = mark_chain_precision(env, insn->dst_reg); |
| 6087 | if (BPF_SRC(insn->code) == BPF_X && !err) |
| 6088 | err = mark_chain_precision(env, insn->src_reg); |
| 6089 | if (err) |
| 6090 | return err; |
| 6091 | } |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 6092 | if (pred == 1) { |
| 6093 | /* only follow the goto, ignore fall-through */ |
| 6094 | *insn_idx += insn->off; |
| 6095 | return 0; |
| 6096 | } else if (pred == 0) { |
| 6097 | /* only follow fall-through branch, since |
| 6098 | * that's where the program will go |
| 6099 | */ |
| 6100 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6101 | } |
| 6102 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6103 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
| 6104 | false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6105 | if (!other_branch) |
| 6106 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6107 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6108 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6109 | /* detect if we are comparing against a constant value so we can adjust |
| 6110 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6111 | * this is only legit if both are scalars (or pointers to the same |
| 6112 | * object, I suppose, but we don't support that right now), because |
| 6113 | * otherwise the different base pointers mean the offsets aren't |
| 6114 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6115 | */ |
| 6116 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6117 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
| 6118 | struct bpf_reg_state lo_reg0 = *dst_reg; |
| 6119 | struct bpf_reg_state lo_reg1 = *src_reg; |
| 6120 | struct bpf_reg_state *src_lo, *dst_lo; |
| 6121 | |
| 6122 | dst_lo = &lo_reg0; |
| 6123 | src_lo = &lo_reg1; |
| 6124 | coerce_reg_to_size(dst_lo, 4); |
| 6125 | coerce_reg_to_size(src_lo, 4); |
| 6126 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6127 | if (dst_reg->type == SCALAR_VALUE && |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6128 | src_reg->type == SCALAR_VALUE) { |
| 6129 | if (tnum_is_const(src_reg->var_off) || |
| 6130 | (is_jmp32 && tnum_is_const(src_lo->var_off))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6131 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6132 | dst_reg, |
| 6133 | is_jmp32 |
| 6134 | ? src_lo->var_off.value |
| 6135 | : src_reg->var_off.value, |
| 6136 | opcode, is_jmp32); |
| 6137 | else if (tnum_is_const(dst_reg->var_off) || |
| 6138 | (is_jmp32 && tnum_is_const(dst_lo->var_off))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6139 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6140 | src_reg, |
| 6141 | is_jmp32 |
| 6142 | ? dst_lo->var_off.value |
| 6143 | : dst_reg->var_off.value, |
| 6144 | opcode, is_jmp32); |
| 6145 | else if (!is_jmp32 && |
| 6146 | (opcode == BPF_JEQ || opcode == BPF_JNE)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6147 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6148 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 6149 | &other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6150 | src_reg, dst_reg, opcode); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6151 | } |
| 6152 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6153 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6154 | dst_reg, insn->imm, opcode, is_jmp32); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6155 | } |
| 6156 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6157 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
| 6158 | * NOTE: these optimizations below are related with pointer comparison |
| 6159 | * which will never be JMP32. |
| 6160 | */ |
| 6161 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 6162 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 6163 | reg_type_may_be_null(dst_reg->type)) { |
| 6164 | /* Mark all identical registers in each branch as either |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 6165 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 6166 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 6167 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 6168 | opcode == BPF_JNE); |
| 6169 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 6170 | opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 6171 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 6172 | this_branch, other_branch) && |
| 6173 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6174 | verbose(env, "R%d pointer comparison prohibited\n", |
| 6175 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6176 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6177 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6178 | if (env->log.level & BPF_LOG_LEVEL) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6179 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6180 | return 0; |
| 6181 | } |
| 6182 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6183 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6184 | 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] | 6185 | { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 6186 | struct bpf_insn_aux_data *aux = cur_aux(env); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6187 | struct bpf_reg_state *regs = cur_regs(env); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 6188 | struct bpf_map *map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6189 | int err; |
| 6190 | |
| 6191 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6192 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6193 | return -EINVAL; |
| 6194 | } |
| 6195 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6196 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6197 | return -EINVAL; |
| 6198 | } |
| 6199 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6200 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6201 | if (err) |
| 6202 | return err; |
| 6203 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 6204 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 6205 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 6206 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6207 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6208 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6209 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 6210 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6211 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 6212 | map = env->used_maps[aux->map_index]; |
| 6213 | mark_reg_known_zero(env, regs, insn->dst_reg); |
| 6214 | regs[insn->dst_reg].map_ptr = map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6215 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 6216 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { |
| 6217 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; |
| 6218 | regs[insn->dst_reg].off = aux->map_off; |
| 6219 | if (map_value_has_spin_lock(map)) |
| 6220 | regs[insn->dst_reg].id = ++env->id_gen; |
| 6221 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 6222 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 6223 | } else { |
| 6224 | verbose(env, "bpf verifier is misconfigured\n"); |
| 6225 | return -EINVAL; |
| 6226 | } |
| 6227 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6228 | return 0; |
| 6229 | } |
| 6230 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 6231 | static bool may_access_skb(enum bpf_prog_type type) |
| 6232 | { |
| 6233 | switch (type) { |
| 6234 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 6235 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 6236 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 6237 | return true; |
| 6238 | default: |
| 6239 | return false; |
| 6240 | } |
| 6241 | } |
| 6242 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 6243 | /* verify safety of LD_ABS|LD_IND instructions: |
| 6244 | * - they can only appear in the programs where ctx == skb |
| 6245 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 6246 | * preserve R6-R9, and store return value into R0 |
| 6247 | * |
| 6248 | * Implicit input: |
| 6249 | * ctx == skb == R6 == CTX |
| 6250 | * |
| 6251 | * Explicit input: |
| 6252 | * SRC == any register |
| 6253 | * IMM == 32-bit immediate |
| 6254 | * |
| 6255 | * Output: |
| 6256 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 6257 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6258 | 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] | 6259 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6260 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 6261 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 6262 | int i, err; |
| 6263 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 6264 | if (!may_access_skb(env->prog->type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6265 | 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] | 6266 | return -EINVAL; |
| 6267 | } |
| 6268 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 6269 | if (!env->ops->gen_ld_abs) { |
| 6270 | verbose(env, "bpf verifier is misconfigured\n"); |
| 6271 | return -EINVAL; |
| 6272 | } |
| 6273 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6274 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6275 | /* when program has LD_ABS insn JITs and interpreter assume |
| 6276 | * that r1 == ctx == skb which is not the case for callees |
| 6277 | * that can have arbitrary arguments. It's problematic |
| 6278 | * for main prog as well since JITs would need to analyze |
| 6279 | * all functions in order to make proper register save/restore |
| 6280 | * decisions in the main prog. Hence disallow LD_ABS with calls |
| 6281 | */ |
| 6282 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); |
| 6283 | return -EINVAL; |
| 6284 | } |
| 6285 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 6286 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 6287 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 6288 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6289 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 6290 | return -EINVAL; |
| 6291 | } |
| 6292 | |
| 6293 | /* check whether implicit source operand (register R6) is readable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6294 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 6295 | if (err) |
| 6296 | return err; |
| 6297 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6298 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 6299 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 6300 | * reference leak. |
| 6301 | */ |
| 6302 | err = check_reference_leak(env); |
| 6303 | if (err) { |
| 6304 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 6305 | return err; |
| 6306 | } |
| 6307 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6308 | if (env->cur_state->active_spin_lock) { |
| 6309 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
| 6310 | return -EINVAL; |
| 6311 | } |
| 6312 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 6313 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6314 | verbose(env, |
| 6315 | "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] | 6316 | return -EINVAL; |
| 6317 | } |
| 6318 | |
| 6319 | if (mode == BPF_IND) { |
| 6320 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6321 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 6322 | if (err) |
| 6323 | return err; |
| 6324 | } |
| 6325 | |
| 6326 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6327 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6328 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6329 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 6330 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 6331 | |
| 6332 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6333 | * the value fetched from the packet. |
| 6334 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 6335 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6336 | mark_reg_unknown(env, regs, BPF_REG_0); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6337 | /* ld_abs load up to 32-bit skb data. */ |
| 6338 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 6339 | return 0; |
| 6340 | } |
| 6341 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6342 | static int check_return_code(struct bpf_verifier_env *env) |
| 6343 | { |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 6344 | struct tnum enforce_attach_type_range = tnum_unknown; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6345 | struct bpf_reg_state *reg; |
| 6346 | struct tnum range = tnum_range(0, 1); |
| 6347 | |
| 6348 | switch (env->prog->type) { |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 6349 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 6350 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || |
| 6351 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG) |
| 6352 | range = tnum_range(1, 1); |
Gustavo A. R. Silva | ed4ed40 | 2019-07-11 11:22:33 -0500 | [diff] [blame] | 6353 | break; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6354 | case BPF_PROG_TYPE_CGROUP_SKB: |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 6355 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
| 6356 | range = tnum_range(0, 3); |
| 6357 | enforce_attach_type_range = tnum_range(2, 3); |
| 6358 | } |
Gustavo A. R. Silva | ed4ed40 | 2019-07-11 11:22:33 -0500 | [diff] [blame] | 6359 | break; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6360 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 6361 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 6362 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 6363 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 6364 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6365 | break; |
Alexei Starovoitov | 15ab09b | 2019-10-28 20:24:26 -0700 | [diff] [blame] | 6366 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 6367 | if (!env->prog->aux->attach_btf_id) |
| 6368 | return 0; |
| 6369 | range = tnum_const(0); |
| 6370 | break; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6371 | default: |
| 6372 | return 0; |
| 6373 | } |
| 6374 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6375 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6376 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6377 | 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] | 6378 | reg_type_str[reg->type]); |
| 6379 | return -EINVAL; |
| 6380 | } |
| 6381 | |
| 6382 | if (!tnum_in(range, reg->var_off)) { |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 6383 | char tn_buf[48]; |
| 6384 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6385 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6386 | if (!tnum_is_unknown(reg->var_off)) { |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6387 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6388 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6389 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6390 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6391 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 6392 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 6393 | verbose(env, " should have been in %s\n", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6394 | return -EINVAL; |
| 6395 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 6396 | |
| 6397 | if (!tnum_is_unknown(enforce_attach_type_range) && |
| 6398 | tnum_in(enforce_attach_type_range, reg->var_off)) |
| 6399 | env->prog->enforce_expected_attach_type = 1; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6400 | return 0; |
| 6401 | } |
| 6402 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6403 | /* non-recursive DFS pseudo code |
| 6404 | * 1 procedure DFS-iterative(G,v): |
| 6405 | * 2 label v as discovered |
| 6406 | * 3 let S be a stack |
| 6407 | * 4 S.push(v) |
| 6408 | * 5 while S is not empty |
| 6409 | * 6 t <- S.pop() |
| 6410 | * 7 if t is what we're looking for: |
| 6411 | * 8 return t |
| 6412 | * 9 for all edges e in G.adjacentEdges(t) do |
| 6413 | * 10 if edge e is already labelled |
| 6414 | * 11 continue with the next edge |
| 6415 | * 12 w <- G.adjacentVertex(t,e) |
| 6416 | * 13 if vertex w is not discovered and not explored |
| 6417 | * 14 label e as tree-edge |
| 6418 | * 15 label w as discovered |
| 6419 | * 16 S.push(w) |
| 6420 | * 17 continue at 5 |
| 6421 | * 18 else if vertex w is discovered |
| 6422 | * 19 label e as back-edge |
| 6423 | * 20 else |
| 6424 | * 21 // vertex w is explored |
| 6425 | * 22 label e as forward- or cross-edge |
| 6426 | * 23 label t as explored |
| 6427 | * 24 S.pop() |
| 6428 | * |
| 6429 | * convention: |
| 6430 | * 0x10 - discovered |
| 6431 | * 0x11 - discovered and fall-through edge labelled |
| 6432 | * 0x12 - discovered and fall-through and branch edges labelled |
| 6433 | * 0x20 - explored |
| 6434 | */ |
| 6435 | |
| 6436 | enum { |
| 6437 | DISCOVERED = 0x10, |
| 6438 | EXPLORED = 0x20, |
| 6439 | FALLTHROUGH = 1, |
| 6440 | BRANCH = 2, |
| 6441 | }; |
| 6442 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 6443 | static u32 state_htab_size(struct bpf_verifier_env *env) |
| 6444 | { |
| 6445 | return env->prog->len; |
| 6446 | } |
| 6447 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 6448 | static struct bpf_verifier_state_list **explored_state( |
| 6449 | struct bpf_verifier_env *env, |
| 6450 | int idx) |
| 6451 | { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 6452 | struct bpf_verifier_state *cur = env->cur_state; |
| 6453 | struct bpf_func_state *state = cur->frame[cur->curframe]; |
| 6454 | |
| 6455 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 6456 | } |
| 6457 | |
| 6458 | static void init_explored_state(struct bpf_verifier_env *env, int idx) |
| 6459 | { |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 6460 | env->insn_aux_data[idx].prune_point = true; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 6461 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6462 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6463 | /* t, w, e - match pseudo-code above: |
| 6464 | * t - index of current instruction |
| 6465 | * w - next instruction |
| 6466 | * e - edge |
| 6467 | */ |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 6468 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
| 6469 | bool loop_ok) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6470 | { |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 6471 | int *insn_stack = env->cfg.insn_stack; |
| 6472 | int *insn_state = env->cfg.insn_state; |
| 6473 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6474 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 6475 | return 0; |
| 6476 | |
| 6477 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 6478 | return 0; |
| 6479 | |
| 6480 | if (w < 0 || w >= env->prog->len) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 6481 | verbose_linfo(env, t, "%d: ", t); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6482 | 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] | 6483 | return -EINVAL; |
| 6484 | } |
| 6485 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6486 | if (e == BRANCH) |
| 6487 | /* mark branch target for state pruning */ |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 6488 | init_explored_state(env, w); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6489 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6490 | if (insn_state[w] == 0) { |
| 6491 | /* tree-edge */ |
| 6492 | insn_state[t] = DISCOVERED | e; |
| 6493 | insn_state[w] = DISCOVERED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 6494 | if (env->cfg.cur_stack >= env->prog->len) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6495 | return -E2BIG; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 6496 | insn_stack[env->cfg.cur_stack++] = w; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6497 | return 1; |
| 6498 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 6499 | if (loop_ok && env->allow_ptr_leaks) |
| 6500 | return 0; |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 6501 | verbose_linfo(env, t, "%d: ", t); |
| 6502 | verbose_linfo(env, w, "%d: ", w); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6503 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6504 | return -EINVAL; |
| 6505 | } else if (insn_state[w] == EXPLORED) { |
| 6506 | /* forward- or cross-edge */ |
| 6507 | insn_state[t] = DISCOVERED | e; |
| 6508 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6509 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6510 | return -EFAULT; |
| 6511 | } |
| 6512 | return 0; |
| 6513 | } |
| 6514 | |
| 6515 | /* non-recursive depth-first-search to detect loops in BPF program |
| 6516 | * loop == back-edge in directed graph |
| 6517 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6518 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6519 | { |
| 6520 | struct bpf_insn *insns = env->prog->insnsi; |
| 6521 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 6522 | int *insn_stack, *insn_state; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6523 | int ret = 0; |
| 6524 | int i, t; |
| 6525 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 6526 | 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] | 6527 | if (!insn_state) |
| 6528 | return -ENOMEM; |
| 6529 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 6530 | 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] | 6531 | if (!insn_stack) { |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 6532 | kvfree(insn_state); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6533 | return -ENOMEM; |
| 6534 | } |
| 6535 | |
| 6536 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 6537 | insn_stack[0] = 0; /* 0 is the first instruction */ |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 6538 | env->cfg.cur_stack = 1; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6539 | |
| 6540 | peek_stack: |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 6541 | if (env->cfg.cur_stack == 0) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6542 | goto check_state; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 6543 | t = insn_stack[env->cfg.cur_stack - 1]; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6544 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6545 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
| 6546 | BPF_CLASS(insns[t].code) == BPF_JMP32) { |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6547 | u8 opcode = BPF_OP(insns[t].code); |
| 6548 | |
| 6549 | if (opcode == BPF_EXIT) { |
| 6550 | goto mark_explored; |
| 6551 | } else if (opcode == BPF_CALL) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 6552 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6553 | if (ret == 1) |
| 6554 | goto peek_stack; |
| 6555 | else if (ret < 0) |
| 6556 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 6557 | if (t + 1 < insn_cnt) |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 6558 | init_explored_state(env, t + 1); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 6559 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 6560 | init_explored_state(env, t); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 6561 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, |
| 6562 | env, false); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 6563 | if (ret == 1) |
| 6564 | goto peek_stack; |
| 6565 | else if (ret < 0) |
| 6566 | goto err_free; |
| 6567 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6568 | } else if (opcode == BPF_JA) { |
| 6569 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 6570 | ret = -EINVAL; |
| 6571 | goto err_free; |
| 6572 | } |
| 6573 | /* unconditional jump with single edge */ |
| 6574 | ret = push_insn(t, t + insns[t].off + 1, |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 6575 | FALLTHROUGH, env, true); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6576 | if (ret == 1) |
| 6577 | goto peek_stack; |
| 6578 | else if (ret < 0) |
| 6579 | goto err_free; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 6580 | /* unconditional jmp is not a good pruning point, |
| 6581 | * but it's marked, since backtracking needs |
| 6582 | * to record jmp history in is_state_visited(). |
| 6583 | */ |
| 6584 | init_explored_state(env, t + insns[t].off + 1); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6585 | /* tell verifier to check for equivalent states |
| 6586 | * after every call and jump |
| 6587 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 6588 | if (t + 1 < insn_cnt) |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 6589 | init_explored_state(env, t + 1); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6590 | } else { |
| 6591 | /* conditional jump with two edges */ |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 6592 | init_explored_state(env, t); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 6593 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6594 | if (ret == 1) |
| 6595 | goto peek_stack; |
| 6596 | else if (ret < 0) |
| 6597 | goto err_free; |
| 6598 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 6599 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6600 | if (ret == 1) |
| 6601 | goto peek_stack; |
| 6602 | else if (ret < 0) |
| 6603 | goto err_free; |
| 6604 | } |
| 6605 | } else { |
| 6606 | /* all other non-branch instructions with single |
| 6607 | * fall-through edge |
| 6608 | */ |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 6609 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6610 | if (ret == 1) |
| 6611 | goto peek_stack; |
| 6612 | else if (ret < 0) |
| 6613 | goto err_free; |
| 6614 | } |
| 6615 | |
| 6616 | mark_explored: |
| 6617 | insn_state[t] = EXPLORED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 6618 | if (env->cfg.cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6619 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6620 | ret = -EFAULT; |
| 6621 | goto err_free; |
| 6622 | } |
| 6623 | goto peek_stack; |
| 6624 | |
| 6625 | check_state: |
| 6626 | for (i = 0; i < insn_cnt; i++) { |
| 6627 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6628 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6629 | ret = -EINVAL; |
| 6630 | goto err_free; |
| 6631 | } |
| 6632 | } |
| 6633 | ret = 0; /* cfg looks good */ |
| 6634 | |
| 6635 | err_free: |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 6636 | kvfree(insn_state); |
| 6637 | kvfree(insn_stack); |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 6638 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6639 | return ret; |
| 6640 | } |
| 6641 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6642 | /* The minimum supported BTF func info size */ |
| 6643 | #define MIN_BPF_FUNCINFO_SIZE 8 |
| 6644 | #define MAX_FUNCINFO_REC_SIZE 252 |
| 6645 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6646 | static int check_btf_func(struct bpf_verifier_env *env, |
| 6647 | const union bpf_attr *attr, |
| 6648 | union bpf_attr __user *uattr) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6649 | { |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 6650 | u32 i, nfuncs, urec_size, min_size; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6651 | u32 krec_size = sizeof(struct bpf_func_info); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6652 | struct bpf_func_info *krecord; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 6653 | struct bpf_func_info_aux *info_aux = NULL; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6654 | const struct btf_type *type; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6655 | struct bpf_prog *prog; |
| 6656 | const struct btf *btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6657 | void __user *urecord; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 6658 | u32 prev_offset = 0; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6659 | int ret = 0; |
| 6660 | |
| 6661 | nfuncs = attr->func_info_cnt; |
| 6662 | if (!nfuncs) |
| 6663 | return 0; |
| 6664 | |
| 6665 | if (nfuncs != env->subprog_cnt) { |
| 6666 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); |
| 6667 | return -EINVAL; |
| 6668 | } |
| 6669 | |
| 6670 | urec_size = attr->func_info_rec_size; |
| 6671 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || |
| 6672 | urec_size > MAX_FUNCINFO_REC_SIZE || |
| 6673 | urec_size % sizeof(u32)) { |
| 6674 | verbose(env, "invalid func info rec size %u\n", urec_size); |
| 6675 | return -EINVAL; |
| 6676 | } |
| 6677 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6678 | prog = env->prog; |
| 6679 | btf = prog->aux->btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6680 | |
| 6681 | urecord = u64_to_user_ptr(attr->func_info); |
| 6682 | min_size = min_t(u32, krec_size, urec_size); |
| 6683 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6684 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6685 | if (!krecord) |
| 6686 | return -ENOMEM; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 6687 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
| 6688 | if (!info_aux) |
| 6689 | goto err_free; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6690 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6691 | for (i = 0; i < nfuncs; i++) { |
| 6692 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); |
| 6693 | if (ret) { |
| 6694 | if (ret == -E2BIG) { |
| 6695 | verbose(env, "nonzero tailing record in func info"); |
| 6696 | /* set the size kernel expects so loader can zero |
| 6697 | * out the rest of the record. |
| 6698 | */ |
| 6699 | if (put_user(min_size, &uattr->func_info_rec_size)) |
| 6700 | ret = -EFAULT; |
| 6701 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6702 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6703 | } |
| 6704 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6705 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6706 | ret = -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6707 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6708 | } |
| 6709 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 6710 | /* check insn_off */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6711 | if (i == 0) { |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 6712 | if (krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6713 | verbose(env, |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 6714 | "nonzero insn_off %u for the first func info record", |
| 6715 | krecord[i].insn_off); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6716 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6717 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6718 | } |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 6719 | } else if (krecord[i].insn_off <= prev_offset) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6720 | verbose(env, |
| 6721 | "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] | 6722 | krecord[i].insn_off, prev_offset); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6723 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6724 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6725 | } |
| 6726 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 6727 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6728 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
| 6729 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6730 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6731 | } |
| 6732 | |
| 6733 | /* check type_id */ |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6734 | type = btf_type_by_id(btf, krecord[i].type_id); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6735 | if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) { |
| 6736 | verbose(env, "invalid type id %d in func info", |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6737 | krecord[i].type_id); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6738 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6739 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6740 | } |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 6741 | prev_offset = krecord[i].insn_off; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6742 | urecord += urec_size; |
| 6743 | } |
| 6744 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6745 | prog->aux->func_info = krecord; |
| 6746 | prog->aux->func_info_cnt = nfuncs; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 6747 | prog->aux->func_info_aux = info_aux; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6748 | return 0; |
| 6749 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6750 | err_free: |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6751 | kvfree(krecord); |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 6752 | kfree(info_aux); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6753 | return ret; |
| 6754 | } |
| 6755 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6756 | static void adjust_btf_func(struct bpf_verifier_env *env) |
| 6757 | { |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 6758 | struct bpf_prog_aux *aux = env->prog->aux; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6759 | int i; |
| 6760 | |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 6761 | if (!aux->func_info) |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6762 | return; |
| 6763 | |
| 6764 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 6765 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6766 | } |
| 6767 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6768 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
| 6769 | sizeof(((struct bpf_line_info *)(0))->line_col)) |
| 6770 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
| 6771 | |
| 6772 | static int check_btf_line(struct bpf_verifier_env *env, |
| 6773 | const union bpf_attr *attr, |
| 6774 | union bpf_attr __user *uattr) |
| 6775 | { |
| 6776 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; |
| 6777 | struct bpf_subprog_info *sub; |
| 6778 | struct bpf_line_info *linfo; |
| 6779 | struct bpf_prog *prog; |
| 6780 | const struct btf *btf; |
| 6781 | void __user *ulinfo; |
| 6782 | int err; |
| 6783 | |
| 6784 | nr_linfo = attr->line_info_cnt; |
| 6785 | if (!nr_linfo) |
| 6786 | return 0; |
| 6787 | |
| 6788 | rec_size = attr->line_info_rec_size; |
| 6789 | if (rec_size < MIN_BPF_LINEINFO_SIZE || |
| 6790 | rec_size > MAX_LINEINFO_REC_SIZE || |
| 6791 | rec_size & (sizeof(u32) - 1)) |
| 6792 | return -EINVAL; |
| 6793 | |
| 6794 | /* Need to zero it in case the userspace may |
| 6795 | * pass in a smaller bpf_line_info object. |
| 6796 | */ |
| 6797 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), |
| 6798 | GFP_KERNEL | __GFP_NOWARN); |
| 6799 | if (!linfo) |
| 6800 | return -ENOMEM; |
| 6801 | |
| 6802 | prog = env->prog; |
| 6803 | btf = prog->aux->btf; |
| 6804 | |
| 6805 | s = 0; |
| 6806 | sub = env->subprog_info; |
| 6807 | ulinfo = u64_to_user_ptr(attr->line_info); |
| 6808 | expected_size = sizeof(struct bpf_line_info); |
| 6809 | ncopy = min_t(u32, expected_size, rec_size); |
| 6810 | for (i = 0; i < nr_linfo; i++) { |
| 6811 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); |
| 6812 | if (err) { |
| 6813 | if (err == -E2BIG) { |
| 6814 | verbose(env, "nonzero tailing record in line_info"); |
| 6815 | if (put_user(expected_size, |
| 6816 | &uattr->line_info_rec_size)) |
| 6817 | err = -EFAULT; |
| 6818 | } |
| 6819 | goto err_free; |
| 6820 | } |
| 6821 | |
| 6822 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { |
| 6823 | err = -EFAULT; |
| 6824 | goto err_free; |
| 6825 | } |
| 6826 | |
| 6827 | /* |
| 6828 | * Check insn_off to ensure |
| 6829 | * 1) strictly increasing AND |
| 6830 | * 2) bounded by prog->len |
| 6831 | * |
| 6832 | * The linfo[0].insn_off == 0 check logically falls into |
| 6833 | * the later "missing bpf_line_info for func..." case |
| 6834 | * because the first linfo[0].insn_off must be the |
| 6835 | * first sub also and the first sub must have |
| 6836 | * subprog_info[0].start == 0. |
| 6837 | */ |
| 6838 | if ((i && linfo[i].insn_off <= prev_offset) || |
| 6839 | linfo[i].insn_off >= prog->len) { |
| 6840 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", |
| 6841 | i, linfo[i].insn_off, prev_offset, |
| 6842 | prog->len); |
| 6843 | err = -EINVAL; |
| 6844 | goto err_free; |
| 6845 | } |
| 6846 | |
Martin KaFai Lau | fdbaa0b | 2018-12-19 13:01:01 -0800 | [diff] [blame] | 6847 | if (!prog->insnsi[linfo[i].insn_off].code) { |
| 6848 | verbose(env, |
| 6849 | "Invalid insn code at line_info[%u].insn_off\n", |
| 6850 | i); |
| 6851 | err = -EINVAL; |
| 6852 | goto err_free; |
| 6853 | } |
| 6854 | |
Martin KaFai Lau | 23127b3 | 2018-12-13 10:41:46 -0800 | [diff] [blame] | 6855 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
| 6856 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6857 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
| 6858 | err = -EINVAL; |
| 6859 | goto err_free; |
| 6860 | } |
| 6861 | |
| 6862 | if (s != env->subprog_cnt) { |
| 6863 | if (linfo[i].insn_off == sub[s].start) { |
| 6864 | sub[s].linfo_idx = i; |
| 6865 | s++; |
| 6866 | } else if (sub[s].start < linfo[i].insn_off) { |
| 6867 | verbose(env, "missing bpf_line_info for func#%u\n", s); |
| 6868 | err = -EINVAL; |
| 6869 | goto err_free; |
| 6870 | } |
| 6871 | } |
| 6872 | |
| 6873 | prev_offset = linfo[i].insn_off; |
| 6874 | ulinfo += rec_size; |
| 6875 | } |
| 6876 | |
| 6877 | if (s != env->subprog_cnt) { |
| 6878 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", |
| 6879 | env->subprog_cnt - s, s); |
| 6880 | err = -EINVAL; |
| 6881 | goto err_free; |
| 6882 | } |
| 6883 | |
| 6884 | prog->aux->linfo = linfo; |
| 6885 | prog->aux->nr_linfo = nr_linfo; |
| 6886 | |
| 6887 | return 0; |
| 6888 | |
| 6889 | err_free: |
| 6890 | kvfree(linfo); |
| 6891 | return err; |
| 6892 | } |
| 6893 | |
| 6894 | static int check_btf_info(struct bpf_verifier_env *env, |
| 6895 | const union bpf_attr *attr, |
| 6896 | union bpf_attr __user *uattr) |
| 6897 | { |
| 6898 | struct btf *btf; |
| 6899 | int err; |
| 6900 | |
| 6901 | if (!attr->func_info_cnt && !attr->line_info_cnt) |
| 6902 | return 0; |
| 6903 | |
| 6904 | btf = btf_get_by_fd(attr->prog_btf_fd); |
| 6905 | if (IS_ERR(btf)) |
| 6906 | return PTR_ERR(btf); |
| 6907 | env->prog->aux->btf = btf; |
| 6908 | |
| 6909 | err = check_btf_func(env, attr, uattr); |
| 6910 | if (err) |
| 6911 | return err; |
| 6912 | |
| 6913 | err = check_btf_line(env, attr, uattr); |
| 6914 | if (err) |
| 6915 | return err; |
| 6916 | |
| 6917 | return 0; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6918 | } |
| 6919 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6920 | /* check %cur's range satisfies %old's */ |
| 6921 | static bool range_within(struct bpf_reg_state *old, |
| 6922 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6923 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6924 | return old->umin_value <= cur->umin_value && |
| 6925 | old->umax_value >= cur->umax_value && |
| 6926 | old->smin_value <= cur->smin_value && |
| 6927 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6928 | } |
| 6929 | |
| 6930 | /* Maximum number of register states that can exist at once */ |
| 6931 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 6932 | struct idpair { |
| 6933 | u32 old; |
| 6934 | u32 cur; |
| 6935 | }; |
| 6936 | |
| 6937 | /* If in the old state two registers had the same id, then they need to have |
| 6938 | * the same id in the new state as well. But that id could be different from |
| 6939 | * the old state, so we need to track the mapping from old to new ids. |
| 6940 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 6941 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 6942 | * regs with a different old id could still have new id 9, we don't care about |
| 6943 | * that. |
| 6944 | * So we look through our idmap to see if this old id has been seen before. If |
| 6945 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 6946 | */ |
| 6947 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 6948 | { |
| 6949 | unsigned int i; |
| 6950 | |
| 6951 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 6952 | if (!idmap[i].old) { |
| 6953 | /* Reached an empty slot; haven't seen this id before */ |
| 6954 | idmap[i].old = old_id; |
| 6955 | idmap[i].cur = cur_id; |
| 6956 | return true; |
| 6957 | } |
| 6958 | if (idmap[i].old == old_id) |
| 6959 | return idmap[i].cur == cur_id; |
| 6960 | } |
| 6961 | /* We ran out of idmap slots, which should be impossible */ |
| 6962 | WARN_ON_ONCE(1); |
| 6963 | return false; |
| 6964 | } |
| 6965 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 6966 | static void clean_func_state(struct bpf_verifier_env *env, |
| 6967 | struct bpf_func_state *st) |
| 6968 | { |
| 6969 | enum bpf_reg_liveness live; |
| 6970 | int i, j; |
| 6971 | |
| 6972 | for (i = 0; i < BPF_REG_FP; i++) { |
| 6973 | live = st->regs[i].live; |
| 6974 | /* liveness must not touch this register anymore */ |
| 6975 | st->regs[i].live |= REG_LIVE_DONE; |
| 6976 | if (!(live & REG_LIVE_READ)) |
| 6977 | /* since the register is unused, clear its state |
| 6978 | * to make further comparison simpler |
| 6979 | */ |
| 6980 | __mark_reg_not_init(&st->regs[i]); |
| 6981 | } |
| 6982 | |
| 6983 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { |
| 6984 | live = st->stack[i].spilled_ptr.live; |
| 6985 | /* liveness must not touch this stack slot anymore */ |
| 6986 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; |
| 6987 | if (!(live & REG_LIVE_READ)) { |
| 6988 | __mark_reg_not_init(&st->stack[i].spilled_ptr); |
| 6989 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 6990 | st->stack[i].slot_type[j] = STACK_INVALID; |
| 6991 | } |
| 6992 | } |
| 6993 | } |
| 6994 | |
| 6995 | static void clean_verifier_state(struct bpf_verifier_env *env, |
| 6996 | struct bpf_verifier_state *st) |
| 6997 | { |
| 6998 | int i; |
| 6999 | |
| 7000 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) |
| 7001 | /* all regs in this state in all frames were already marked */ |
| 7002 | return; |
| 7003 | |
| 7004 | for (i = 0; i <= st->curframe; i++) |
| 7005 | clean_func_state(env, st->frame[i]); |
| 7006 | } |
| 7007 | |
| 7008 | /* the parentage chains form a tree. |
| 7009 | * the verifier states are added to state lists at given insn and |
| 7010 | * pushed into state stack for future exploration. |
| 7011 | * when the verifier reaches bpf_exit insn some of the verifer states |
| 7012 | * stored in the state lists have their final liveness state already, |
| 7013 | * but a lot of states will get revised from liveness point of view when |
| 7014 | * the verifier explores other branches. |
| 7015 | * Example: |
| 7016 | * 1: r0 = 1 |
| 7017 | * 2: if r1 == 100 goto pc+1 |
| 7018 | * 3: r0 = 2 |
| 7019 | * 4: exit |
| 7020 | * when the verifier reaches exit insn the register r0 in the state list of |
| 7021 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch |
| 7022 | * of insn 2 and goes exploring further. At the insn 4 it will walk the |
| 7023 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. |
| 7024 | * |
| 7025 | * Since the verifier pushes the branch states as it sees them while exploring |
| 7026 | * the program the condition of walking the branch instruction for the second |
| 7027 | * time means that all states below this branch were already explored and |
| 7028 | * their final liveness markes are already propagated. |
| 7029 | * Hence when the verifier completes the search of state list in is_state_visited() |
| 7030 | * we can call this clean_live_states() function to mark all liveness states |
| 7031 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' |
| 7032 | * will not be used. |
| 7033 | * This function also clears the registers and stack for states that !READ |
| 7034 | * to simplify state merging. |
| 7035 | * |
| 7036 | * Important note here that walking the same branch instruction in the callee |
| 7037 | * doesn't meant that the states are DONE. The verifier has to compare |
| 7038 | * the callsites |
| 7039 | */ |
| 7040 | static void clean_live_states(struct bpf_verifier_env *env, int insn, |
| 7041 | struct bpf_verifier_state *cur) |
| 7042 | { |
| 7043 | struct bpf_verifier_state_list *sl; |
| 7044 | int i; |
| 7045 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7046 | sl = *explored_state(env, insn); |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 7047 | while (sl) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7048 | if (sl->state.branches) |
| 7049 | goto next; |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 7050 | if (sl->state.insn_idx != insn || |
| 7051 | sl->state.curframe != cur->curframe) |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 7052 | goto next; |
| 7053 | for (i = 0; i <= cur->curframe; i++) |
| 7054 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) |
| 7055 | goto next; |
| 7056 | clean_verifier_state(env, &sl->state); |
| 7057 | next: |
| 7058 | sl = sl->next; |
| 7059 | } |
| 7060 | } |
| 7061 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7062 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 7063 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 7064 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7065 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7066 | bool equal; |
| 7067 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7068 | if (!(rold->live & REG_LIVE_READ)) |
| 7069 | /* explored state didn't use this */ |
| 7070 | return true; |
| 7071 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 7072 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7073 | |
| 7074 | if (rold->type == PTR_TO_STACK) |
| 7075 | /* two stack pointers are equal only if they're pointing to |
| 7076 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 7077 | */ |
| 7078 | return equal && rold->frameno == rcur->frameno; |
| 7079 | |
| 7080 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7081 | return true; |
| 7082 | |
| 7083 | if (rold->type == NOT_INIT) |
| 7084 | /* explored state can't have used this */ |
| 7085 | return true; |
| 7086 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 7087 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7088 | switch (rold->type) { |
| 7089 | case SCALAR_VALUE: |
| 7090 | if (rcur->type == SCALAR_VALUE) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7091 | if (!rold->precise && !rcur->precise) |
| 7092 | return true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7093 | /* new val must satisfy old val knowledge */ |
| 7094 | return range_within(rold, rcur) && |
| 7095 | tnum_in(rold->var_off, rcur->var_off); |
| 7096 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 7097 | /* We're trying to use a pointer in place of a scalar. |
| 7098 | * Even if the scalar was unbounded, this could lead to |
| 7099 | * pointer leaks because scalars are allowed to leak |
| 7100 | * while pointers are not. We could make this safe in |
| 7101 | * special cases if root is calling us, but it's |
| 7102 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7103 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 7104 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7105 | } |
| 7106 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 7107 | /* If the new min/max/var_off satisfy the old ones and |
| 7108 | * everything else matches, we are OK. |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 7109 | * 'id' is not compared, since it's only used for maps with |
| 7110 | * bpf_spin_lock inside map element and in such cases if |
| 7111 | * the rest of the prog is valid for one map element then |
| 7112 | * it's valid for all map elements regardless of the key |
| 7113 | * used in bpf_map_lookup() |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 7114 | */ |
| 7115 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 7116 | range_within(rold, rcur) && |
| 7117 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7118 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 7119 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 7120 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 7121 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 7122 | * checked, doing so could have affected others with the same |
| 7123 | * id, and we can't check for that because we lost the id when |
| 7124 | * we converted to a PTR_TO_MAP_VALUE. |
| 7125 | */ |
| 7126 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 7127 | return false; |
| 7128 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 7129 | return false; |
| 7130 | /* Check our ids match any regs they're supposed to */ |
| 7131 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 7132 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7133 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 7134 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7135 | return false; |
| 7136 | /* We must have at least as much range as the old ptr |
| 7137 | * did, so that any accesses which were safe before are |
| 7138 | * still safe. This is true even if old range < old off, |
| 7139 | * since someone could have accessed through (ptr - k), or |
| 7140 | * even done ptr -= k in a register, to get a safe access. |
| 7141 | */ |
| 7142 | if (rold->range > rcur->range) |
| 7143 | return false; |
| 7144 | /* If the offsets don't match, we can't trust our alignment; |
| 7145 | * nor can we be sure that we won't fall out of range. |
| 7146 | */ |
| 7147 | if (rold->off != rcur->off) |
| 7148 | return false; |
| 7149 | /* id relations must be preserved */ |
| 7150 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 7151 | return false; |
| 7152 | /* new val must satisfy old val knowledge */ |
| 7153 | return range_within(rold, rcur) && |
| 7154 | tnum_in(rold->var_off, rcur->var_off); |
| 7155 | case PTR_TO_CTX: |
| 7156 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7157 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 7158 | case PTR_TO_FLOW_KEYS: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7159 | case PTR_TO_SOCKET: |
| 7160 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 7161 | case PTR_TO_SOCK_COMMON: |
| 7162 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 7163 | case PTR_TO_TCP_SOCK: |
| 7164 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 7165 | case PTR_TO_XDP_SOCK: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7166 | /* Only valid matches are exact, which memcmp() above |
| 7167 | * would have accepted |
| 7168 | */ |
| 7169 | default: |
| 7170 | /* Don't know what's going on, just say it's not safe */ |
| 7171 | return false; |
| 7172 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 7173 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7174 | /* Shouldn't get here; if we do, say it's not safe */ |
| 7175 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 7176 | return false; |
| 7177 | } |
| 7178 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7179 | static bool stacksafe(struct bpf_func_state *old, |
| 7180 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7181 | struct idpair *idmap) |
| 7182 | { |
| 7183 | int i, spi; |
| 7184 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7185 | /* walk slots of the explored stack and ignore any additional |
| 7186 | * slots in the current stack, since explored(safe) state |
| 7187 | * didn't use them |
| 7188 | */ |
| 7189 | for (i = 0; i < old->allocated_stack; i++) { |
| 7190 | spi = i / BPF_REG_SIZE; |
| 7191 | |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 7192 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
| 7193 | i += BPF_REG_SIZE - 1; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 7194 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 7195 | continue; |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 7196 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 7197 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7198 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 7199 | continue; |
Alexei Starovoitov | 19e2dbb | 2018-12-13 11:42:33 -0800 | [diff] [blame] | 7200 | |
| 7201 | /* explored stack has more populated slots than current stack |
| 7202 | * and these slots were used |
| 7203 | */ |
| 7204 | if (i >= cur->allocated_stack) |
| 7205 | return false; |
| 7206 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 7207 | /* if old state was safe with misc data in the stack |
| 7208 | * it will be safe with zero-initialized stack. |
| 7209 | * The opposite is not true |
| 7210 | */ |
| 7211 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 7212 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 7213 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7214 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 7215 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 7216 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 7217 | * this stack slot, but current has has STACK_MISC -> |
| 7218 | * this verifier states are not equivalent, |
| 7219 | * return false to continue verification of this path |
| 7220 | */ |
| 7221 | return false; |
| 7222 | if (i % BPF_REG_SIZE) |
| 7223 | continue; |
| 7224 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 7225 | continue; |
| 7226 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 7227 | &cur->stack[spi].spilled_ptr, |
| 7228 | idmap)) |
| 7229 | /* when explored and current stack slot are both storing |
| 7230 | * spilled registers, check that stored pointers types |
| 7231 | * are the same as well. |
| 7232 | * Ex: explored safe path could have stored |
| 7233 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 7234 | * but current path has stored: |
| 7235 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 7236 | * such verifier states are not equivalent. |
| 7237 | * return false to continue verification of this path |
| 7238 | */ |
| 7239 | return false; |
| 7240 | } |
| 7241 | return true; |
| 7242 | } |
| 7243 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7244 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 7245 | { |
| 7246 | if (old->acquired_refs != cur->acquired_refs) |
| 7247 | return false; |
| 7248 | return !memcmp(old->refs, cur->refs, |
| 7249 | sizeof(*old->refs) * old->acquired_refs); |
| 7250 | } |
| 7251 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7252 | /* compare two verifier states |
| 7253 | * |
| 7254 | * all states stored in state_list are known to be valid, since |
| 7255 | * verifier reached 'bpf_exit' instruction through them |
| 7256 | * |
| 7257 | * this function is called when verifier exploring different branches of |
| 7258 | * execution popped from the state stack. If it sees an old state that has |
| 7259 | * more strict register state and more strict stack state then this execution |
| 7260 | * branch doesn't need to be explored further, since verifier already |
| 7261 | * concluded that more strict state leads to valid finish. |
| 7262 | * |
| 7263 | * Therefore two states are equivalent if register state is more conservative |
| 7264 | * and explored stack state is more conservative than the current one. |
| 7265 | * Example: |
| 7266 | * explored current |
| 7267 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 7268 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 7269 | * |
| 7270 | * In other words if current stack state (one being explored) has more |
| 7271 | * valid slots than old one that already passed validation, it means |
| 7272 | * the verifier can stop exploring and conclude that current state is valid too |
| 7273 | * |
| 7274 | * Similarly with registers. If explored state has register type as invalid |
| 7275 | * whereas register type in current state is meaningful, it means that |
| 7276 | * the current state will reach 'bpf_exit' instruction safely |
| 7277 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7278 | static bool func_states_equal(struct bpf_func_state *old, |
| 7279 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7280 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7281 | struct idpair *idmap; |
| 7282 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7283 | int i; |
| 7284 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7285 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 7286 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 7287 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 7288 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7289 | |
| 7290 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 7291 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7292 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7293 | } |
| 7294 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7295 | if (!stacksafe(old, cur, idmap)) |
| 7296 | goto out_free; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7297 | |
| 7298 | if (!refsafe(old, cur)) |
| 7299 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7300 | ret = true; |
| 7301 | out_free: |
| 7302 | kfree(idmap); |
| 7303 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7304 | } |
| 7305 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7306 | static bool states_equal(struct bpf_verifier_env *env, |
| 7307 | struct bpf_verifier_state *old, |
| 7308 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7309 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7310 | int i; |
| 7311 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7312 | if (old->curframe != cur->curframe) |
| 7313 | return false; |
| 7314 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7315 | /* Verification state from speculative execution simulation |
| 7316 | * must never prune a non-speculative execution one. |
| 7317 | */ |
| 7318 | if (old->speculative && !cur->speculative) |
| 7319 | return false; |
| 7320 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 7321 | if (old->active_spin_lock != cur->active_spin_lock) |
| 7322 | return false; |
| 7323 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7324 | /* for states to be equal callsites have to be the same |
| 7325 | * and all frame states need to be equivalent |
| 7326 | */ |
| 7327 | for (i = 0; i <= old->curframe; i++) { |
| 7328 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 7329 | return false; |
| 7330 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 7331 | return false; |
| 7332 | } |
| 7333 | return true; |
| 7334 | } |
| 7335 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 7336 | /* Return 0 if no propagation happened. Return negative error code if error |
| 7337 | * happened. Otherwise, return the propagated bit. |
| 7338 | */ |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 7339 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
| 7340 | struct bpf_reg_state *reg, |
| 7341 | struct bpf_reg_state *parent_reg) |
| 7342 | { |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 7343 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
| 7344 | u8 flag = reg->live & REG_LIVE_READ; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 7345 | int err; |
| 7346 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 7347 | /* When comes here, read flags of PARENT_REG or REG could be any of |
| 7348 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need |
| 7349 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. |
| 7350 | */ |
| 7351 | if (parent_flag == REG_LIVE_READ64 || |
| 7352 | /* Or if there is no read flag from REG. */ |
| 7353 | !flag || |
| 7354 | /* Or if the read flag from REG is the same as PARENT_REG. */ |
| 7355 | parent_flag == flag) |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 7356 | return 0; |
| 7357 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 7358 | err = mark_reg_read(env, reg, parent_reg, flag); |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 7359 | if (err) |
| 7360 | return err; |
| 7361 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 7362 | return flag; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 7363 | } |
| 7364 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7365 | /* A write screens off any subsequent reads; but write marks come from the |
| 7366 | * straight-line code between a state and its parent. When we arrive at an |
| 7367 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 7368 | * code, so read marks in the state must propagate to the parent regardless |
| 7369 | * 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] | 7370 | * in mark_reg_read() is for. |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7371 | */ |
| 7372 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 7373 | const struct bpf_verifier_state *vstate, |
| 7374 | struct bpf_verifier_state *vparent) |
| 7375 | { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 7376 | struct bpf_reg_state *state_reg, *parent_reg; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7377 | struct bpf_func_state *state, *parent; |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 7378 | int i, frame, err = 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7379 | |
| 7380 | if (vparent->curframe != vstate->curframe) { |
| 7381 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 7382 | vparent->curframe, vstate->curframe); |
| 7383 | return -EFAULT; |
| 7384 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7385 | /* Propagate read liveness of registers... */ |
| 7386 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 7387 | for (frame = 0; frame <= vstate->curframe; frame++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 7388 | parent = vparent->frame[frame]; |
| 7389 | state = vstate->frame[frame]; |
| 7390 | parent_reg = parent->regs; |
| 7391 | state_reg = state->regs; |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 7392 | /* We don't need to worry about FP liveness, it's read-only */ |
| 7393 | 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] | 7394 | err = propagate_liveness_reg(env, &state_reg[i], |
| 7395 | &parent_reg[i]); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 7396 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 7397 | return err; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 7398 | if (err == REG_LIVE_READ64) |
| 7399 | mark_insn_zext(env, &parent_reg[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7400 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7401 | |
Jiong Wang | 1b04aee | 2019-04-12 22:59:34 +0100 | [diff] [blame] | 7402 | /* Propagate stack slots. */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7403 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 7404 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 7405 | parent_reg = &parent->stack[i].spilled_ptr; |
| 7406 | state_reg = &state->stack[i].spilled_ptr; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 7407 | err = propagate_liveness_reg(env, state_reg, |
| 7408 | parent_reg); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 7409 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 7410 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7411 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7412 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 7413 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7414 | } |
| 7415 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 7416 | /* find precise scalars in the previous equivalent state and |
| 7417 | * propagate them into the current state |
| 7418 | */ |
| 7419 | static int propagate_precision(struct bpf_verifier_env *env, |
| 7420 | const struct bpf_verifier_state *old) |
| 7421 | { |
| 7422 | struct bpf_reg_state *state_reg; |
| 7423 | struct bpf_func_state *state; |
| 7424 | int i, err = 0; |
| 7425 | |
| 7426 | state = old->frame[old->curframe]; |
| 7427 | state_reg = state->regs; |
| 7428 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { |
| 7429 | if (state_reg->type != SCALAR_VALUE || |
| 7430 | !state_reg->precise) |
| 7431 | continue; |
| 7432 | if (env->log.level & BPF_LOG_LEVEL2) |
| 7433 | verbose(env, "propagating r%d\n", i); |
| 7434 | err = mark_chain_precision(env, i); |
| 7435 | if (err < 0) |
| 7436 | return err; |
| 7437 | } |
| 7438 | |
| 7439 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 7440 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 7441 | continue; |
| 7442 | state_reg = &state->stack[i].spilled_ptr; |
| 7443 | if (state_reg->type != SCALAR_VALUE || |
| 7444 | !state_reg->precise) |
| 7445 | continue; |
| 7446 | if (env->log.level & BPF_LOG_LEVEL2) |
| 7447 | verbose(env, "propagating fp%d\n", |
| 7448 | (-i - 1) * BPF_REG_SIZE); |
| 7449 | err = mark_chain_precision_stack(env, i); |
| 7450 | if (err < 0) |
| 7451 | return err; |
| 7452 | } |
| 7453 | return 0; |
| 7454 | } |
| 7455 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7456 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
| 7457 | struct bpf_verifier_state *cur) |
| 7458 | { |
| 7459 | struct bpf_func_state *fold, *fcur; |
| 7460 | int i, fr = cur->curframe; |
| 7461 | |
| 7462 | if (old->curframe != fr) |
| 7463 | return false; |
| 7464 | |
| 7465 | fold = old->frame[fr]; |
| 7466 | fcur = cur->frame[fr]; |
| 7467 | for (i = 0; i < MAX_BPF_REG; i++) |
| 7468 | if (memcmp(&fold->regs[i], &fcur->regs[i], |
| 7469 | offsetof(struct bpf_reg_state, parent))) |
| 7470 | return false; |
| 7471 | return true; |
| 7472 | } |
| 7473 | |
| 7474 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7475 | 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] | 7476 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7477 | struct bpf_verifier_state_list *new_sl; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 7478 | struct bpf_verifier_state_list *sl, **pprev; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 7479 | struct bpf_verifier_state *cur = env->cur_state, *new; |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 7480 | int i, j, err, states_cnt = 0; |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 7481 | bool add_new_state = env->test_state_freq ? true : false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7482 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7483 | cur->last_insn_idx = env->prev_insn_idx; |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 7484 | if (!env->insn_aux_data[insn_idx].prune_point) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7485 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 7486 | * be doing state search here |
| 7487 | */ |
| 7488 | return 0; |
| 7489 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7490 | /* bpf progs typically have pruning point every 4 instructions |
| 7491 | * http://vger.kernel.org/bpfconf2019.html#session-1 |
| 7492 | * Do not add new state for future pruning if the verifier hasn't seen |
| 7493 | * at least 2 jumps and at least 8 instructions. |
| 7494 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. |
| 7495 | * In tests that amounts to up to 50% reduction into total verifier |
| 7496 | * memory consumption and 20% verifier time speedup. |
| 7497 | */ |
| 7498 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && |
| 7499 | env->insn_processed - env->prev_insn_processed >= 8) |
| 7500 | add_new_state = true; |
| 7501 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 7502 | pprev = explored_state(env, insn_idx); |
| 7503 | sl = *pprev; |
| 7504 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 7505 | clean_live_states(env, insn_idx, cur); |
| 7506 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 7507 | while (sl) { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 7508 | states_cnt++; |
| 7509 | if (sl->state.insn_idx != insn_idx) |
| 7510 | goto next; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7511 | if (sl->state.branches) { |
| 7512 | if (states_maybe_looping(&sl->state, cur) && |
| 7513 | states_equal(env, &sl->state, cur)) { |
| 7514 | verbose_linfo(env, insn_idx, "; "); |
| 7515 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); |
| 7516 | return -EINVAL; |
| 7517 | } |
| 7518 | /* if the verifier is processing a loop, avoid adding new state |
| 7519 | * too often, since different loop iterations have distinct |
| 7520 | * states and may not help future pruning. |
| 7521 | * This threshold shouldn't be too low to make sure that |
| 7522 | * a loop with large bound will be rejected quickly. |
| 7523 | * The most abusive loop will be: |
| 7524 | * r1 += 1 |
| 7525 | * if r1 < 1000000 goto pc-2 |
| 7526 | * 1M insn_procssed limit / 100 == 10k peak states. |
| 7527 | * This threshold shouldn't be too high either, since states |
| 7528 | * at the end of the loop are likely to be useful in pruning. |
| 7529 | */ |
| 7530 | if (env->jmps_processed - env->prev_jmps_processed < 20 && |
| 7531 | env->insn_processed - env->prev_insn_processed < 100) |
| 7532 | add_new_state = false; |
| 7533 | goto miss; |
| 7534 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7535 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 7536 | sl->hit_cnt++; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7537 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7538 | * prune the search. |
| 7539 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 7540 | * If we have any write marks in env->cur_state, they |
| 7541 | * will prevent corresponding reads in the continuation |
| 7542 | * from reaching our parent (an explored_state). Our |
| 7543 | * own state will get the read marks recorded, but |
| 7544 | * they'll be immediately forgotten as we're pruning |
| 7545 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7546 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7547 | err = propagate_liveness(env, &sl->state, cur); |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 7548 | |
| 7549 | /* if previous state reached the exit with precision and |
| 7550 | * current state is equivalent to it (except precsion marks) |
| 7551 | * the precision needs to be propagated back in |
| 7552 | * the current state. |
| 7553 | */ |
| 7554 | err = err ? : push_jmp_history(env, cur); |
| 7555 | err = err ? : propagate_precision(env, &sl->state); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7556 | if (err) |
| 7557 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7558 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7559 | } |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7560 | miss: |
| 7561 | /* when new state is not going to be added do not increase miss count. |
| 7562 | * Otherwise several loop iterations will remove the state |
| 7563 | * recorded earlier. The goal of these heuristics is to have |
| 7564 | * states from some iterations of the loop (some in the beginning |
| 7565 | * and some at the end) to help pruning. |
| 7566 | */ |
| 7567 | if (add_new_state) |
| 7568 | sl->miss_cnt++; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 7569 | /* heuristic to determine whether this state is beneficial |
| 7570 | * to keep checking from state equivalence point of view. |
| 7571 | * Higher numbers increase max_states_per_insn and verification time, |
| 7572 | * but do not meaningfully decrease insn_processed. |
| 7573 | */ |
| 7574 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { |
| 7575 | /* the state is unlikely to be useful. Remove it to |
| 7576 | * speed up verification |
| 7577 | */ |
| 7578 | *pprev = sl->next; |
| 7579 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7580 | u32 br = sl->state.branches; |
| 7581 | |
| 7582 | WARN_ONCE(br, |
| 7583 | "BUG live_done but branches_to_explore %d\n", |
| 7584 | br); |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 7585 | free_verifier_state(&sl->state, false); |
| 7586 | kfree(sl); |
| 7587 | env->peak_states--; |
| 7588 | } else { |
| 7589 | /* cannot free this state, since parentage chain may |
| 7590 | * walk it later. Add it for free_list instead to |
| 7591 | * be freed at the end of verification |
| 7592 | */ |
| 7593 | sl->next = env->free_list; |
| 7594 | env->free_list = sl; |
| 7595 | } |
| 7596 | sl = *pprev; |
| 7597 | continue; |
| 7598 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 7599 | next: |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 7600 | pprev = &sl->next; |
| 7601 | sl = *pprev; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7602 | } |
| 7603 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 7604 | if (env->max_states_per_insn < states_cnt) |
| 7605 | env->max_states_per_insn = states_cnt; |
| 7606 | |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 7607 | if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7608 | return push_jmp_history(env, cur); |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 7609 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7610 | if (!add_new_state) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7611 | return push_jmp_history(env, cur); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7612 | |
| 7613 | /* There were no equivalent states, remember the current one. |
| 7614 | * Technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7615 | * 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] | 7616 | * 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] | 7617 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7618 | * again on the way to bpf_exit. |
| 7619 | * When looping the sl->state.branches will be > 0 and this state |
| 7620 | * will not be considered for equivalence until branches == 0. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7621 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7622 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7623 | if (!new_sl) |
| 7624 | return -ENOMEM; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 7625 | env->total_states++; |
| 7626 | env->peak_states++; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7627 | env->prev_jmps_processed = env->jmps_processed; |
| 7628 | env->prev_insn_processed = env->insn_processed; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7629 | |
| 7630 | /* add new state to the head of linked list */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 7631 | new = &new_sl->state; |
| 7632 | err = copy_verifier_state(new, cur); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 7633 | if (err) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 7634 | free_verifier_state(new, false); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 7635 | kfree(new_sl); |
| 7636 | return err; |
| 7637 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 7638 | new->insn_idx = insn_idx; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7639 | WARN_ONCE(new->branches != 1, |
| 7640 | "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] | 7641 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7642 | cur->parent = new; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7643 | cur->first_insn_idx = insn_idx; |
| 7644 | clear_jmp_history(cur); |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7645 | new_sl->next = *explored_state(env, insn_idx); |
| 7646 | *explored_state(env, insn_idx) = new_sl; |
Jakub Kicinski | 7640ead | 2018-12-12 16:29:07 -0800 | [diff] [blame] | 7647 | /* connect new state to parentage chain. Current frame needs all |
| 7648 | * registers connected. Only r6 - r9 of the callers are alive (pushed |
| 7649 | * to the stack implicitly by JITs) so in callers' frames connect just |
| 7650 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to |
| 7651 | * the state of the call instruction (with WRITTEN set), and r0 comes |
| 7652 | * from callee with its full parentage chain, anyway. |
| 7653 | */ |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 7654 | /* clear write marks in current state: the writes we did are not writes |
| 7655 | * our child did, so they don't screen off its reads from us. |
| 7656 | * (There are no read marks in current state, because reads always mark |
| 7657 | * their parent and current state never has children yet. Only |
| 7658 | * explored_states can get read marks.) |
| 7659 | */ |
Alexei Starovoitov | eea1c22 | 2019-06-15 12:12:21 -0700 | [diff] [blame] | 7660 | for (j = 0; j <= cur->curframe; j++) { |
| 7661 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) |
| 7662 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; |
| 7663 | for (i = 0; i < BPF_REG_FP; i++) |
| 7664 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; |
| 7665 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7666 | |
| 7667 | /* all stack frames are accessible from callee, clear them all */ |
| 7668 | for (j = 0; j <= cur->curframe; j++) { |
| 7669 | struct bpf_func_state *frame = cur->frame[j]; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 7670 | struct bpf_func_state *newframe = new->frame[j]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7671 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 7672 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 7673 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 7674 | frame->stack[i].spilled_ptr.parent = |
| 7675 | &newframe->stack[i].spilled_ptr; |
| 7676 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7677 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7678 | return 0; |
| 7679 | } |
| 7680 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7681 | /* Return true if it's OK to have the same insn return a different type. */ |
| 7682 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 7683 | { |
| 7684 | switch (type) { |
| 7685 | case PTR_TO_CTX: |
| 7686 | case PTR_TO_SOCKET: |
| 7687 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 7688 | case PTR_TO_SOCK_COMMON: |
| 7689 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 7690 | case PTR_TO_TCP_SOCK: |
| 7691 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 7692 | case PTR_TO_XDP_SOCK: |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 7693 | case PTR_TO_BTF_ID: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7694 | return false; |
| 7695 | default: |
| 7696 | return true; |
| 7697 | } |
| 7698 | } |
| 7699 | |
| 7700 | /* If an instruction was previously used with particular pointer types, then we |
| 7701 | * need to be careful to avoid cases such as the below, where it may be ok |
| 7702 | * for one branch accessing the pointer, but not ok for the other branch: |
| 7703 | * |
| 7704 | * R1 = sock_ptr |
| 7705 | * goto X; |
| 7706 | * ... |
| 7707 | * R1 = some_other_valid_ptr; |
| 7708 | * goto X; |
| 7709 | * ... |
| 7710 | * R2 = *(u32 *)(R1 + 0); |
| 7711 | */ |
| 7712 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 7713 | { |
| 7714 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 7715 | !reg_type_mismatch_ok(prev)); |
| 7716 | } |
| 7717 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7718 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7719 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7720 | struct bpf_verifier_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7721 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7722 | struct bpf_reg_state *regs; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 7723 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7724 | bool do_print_state = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7725 | int prev_insn_idx = -1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7726 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 7727 | env->prev_linfo = NULL; |
| 7728 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7729 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 7730 | if (!state) |
| 7731 | return -ENOMEM; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7732 | state->curframe = 0; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7733 | state->speculative = false; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7734 | state->branches = 1; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7735 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 7736 | if (!state->frame[0]) { |
| 7737 | kfree(state); |
| 7738 | return -ENOMEM; |
| 7739 | } |
| 7740 | env->cur_state = state; |
| 7741 | init_func_state(env, state->frame[0], |
| 7742 | BPF_MAIN_FUNC /* callsite */, |
| 7743 | 0 /* frameno */, |
| 7744 | 0 /* subprogno, zero == main subprog */); |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7745 | |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 7746 | if (btf_check_func_arg_match(env, 0)) |
| 7747 | return -EINVAL; |
| 7748 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7749 | for (;;) { |
| 7750 | struct bpf_insn *insn; |
| 7751 | u8 class; |
| 7752 | int err; |
| 7753 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7754 | env->prev_insn_idx = prev_insn_idx; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7755 | if (env->insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7756 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7757 | env->insn_idx, insn_cnt); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7758 | return -EFAULT; |
| 7759 | } |
| 7760 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7761 | insn = &insns[env->insn_idx]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7762 | class = BPF_CLASS(insn->code); |
| 7763 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 7764 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7765 | verbose(env, |
| 7766 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 7767 | env->insn_processed); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7768 | return -E2BIG; |
| 7769 | } |
| 7770 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7771 | err = is_state_visited(env, env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7772 | if (err < 0) |
| 7773 | return err; |
| 7774 | if (err == 1) { |
| 7775 | /* found equivalent state, can prune the search */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 7776 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7777 | if (do_print_state) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7778 | verbose(env, "\nfrom %d to %d%s: safe\n", |
| 7779 | env->prev_insn_idx, env->insn_idx, |
| 7780 | env->cur_state->speculative ? |
| 7781 | " (speculative execution)" : ""); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7782 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7783 | verbose(env, "%d: safe\n", env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7784 | } |
| 7785 | goto process_bpf_exit; |
| 7786 | } |
| 7787 | |
Alexei Starovoitov | c349480 | 2018-12-03 22:46:04 -0800 | [diff] [blame] | 7788 | if (signal_pending(current)) |
| 7789 | return -EAGAIN; |
| 7790 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 7791 | if (need_resched()) |
| 7792 | cond_resched(); |
| 7793 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 7794 | if (env->log.level & BPF_LOG_LEVEL2 || |
| 7795 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { |
| 7796 | if (env->log.level & BPF_LOG_LEVEL2) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7797 | verbose(env, "%d:", env->insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 7798 | else |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7799 | verbose(env, "\nfrom %d to %d%s:", |
| 7800 | env->prev_insn_idx, env->insn_idx, |
| 7801 | env->cur_state->speculative ? |
| 7802 | " (speculative execution)" : ""); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7803 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7804 | do_print_state = false; |
| 7805 | } |
| 7806 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 7807 | if (env->log.level & BPF_LOG_LEVEL) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7808 | const struct bpf_insn_cbs cbs = { |
| 7809 | .cb_print = verbose, |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 7810 | .private_data = env, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7811 | }; |
| 7812 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7813 | verbose_linfo(env, env->insn_idx, "; "); |
| 7814 | verbose(env, "%d: ", env->insn_idx); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 7815 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7816 | } |
| 7817 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 7818 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7819 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
| 7820 | env->prev_insn_idx); |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 7821 | if (err) |
| 7822 | return err; |
| 7823 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 7824 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7825 | regs = cur_regs(env); |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7826 | env->insn_aux_data[env->insn_idx].seen = true; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7827 | prev_insn_idx = env->insn_idx; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7828 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7829 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7830 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7831 | if (err) |
| 7832 | return err; |
| 7833 | |
| 7834 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7835 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7836 | |
| 7837 | /* check for reserved fields is already done */ |
| 7838 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7839 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7840 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7841 | if (err) |
| 7842 | return err; |
| 7843 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7844 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7845 | if (err) |
| 7846 | return err; |
| 7847 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 7848 | src_reg_type = regs[insn->src_reg].type; |
| 7849 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7850 | /* check that memory (src_reg + off) is readable, |
| 7851 | * the state of dst_reg will be updated by this func |
| 7852 | */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7853 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
| 7854 | insn->off, BPF_SIZE(insn->code), |
| 7855 | BPF_READ, insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7856 | if (err) |
| 7857 | return err; |
| 7858 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7859 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7860 | |
| 7861 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7862 | /* saw a valid insn |
| 7863 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7864 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7865 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7866 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7867 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7868 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7869 | /* ABuser program is trying to use the same insn |
| 7870 | * dst_reg = *(u32*) (src_reg + off) |
| 7871 | * with different pointer types: |
| 7872 | * src_reg == ctx in one branch and |
| 7873 | * src_reg == stack|map in some other branch. |
| 7874 | * Reject it. |
| 7875 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7876 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7877 | return -EINVAL; |
| 7878 | } |
| 7879 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7880 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7881 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7882 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7883 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7884 | err = check_xadd(env, env->insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7885 | if (err) |
| 7886 | return err; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7887 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7888 | continue; |
| 7889 | } |
| 7890 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7891 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7892 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7893 | if (err) |
| 7894 | return err; |
| 7895 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7896 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7897 | if (err) |
| 7898 | return err; |
| 7899 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7900 | dst_reg_type = regs[insn->dst_reg].type; |
| 7901 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7902 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7903 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 7904 | insn->off, BPF_SIZE(insn->code), |
| 7905 | BPF_WRITE, insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7906 | if (err) |
| 7907 | return err; |
| 7908 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7909 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7910 | |
| 7911 | if (*prev_dst_type == NOT_INIT) { |
| 7912 | *prev_dst_type = dst_reg_type; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7913 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7914 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7915 | return -EINVAL; |
| 7916 | } |
| 7917 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7918 | } else if (class == BPF_ST) { |
| 7919 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 7920 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7921 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7922 | return -EINVAL; |
| 7923 | } |
| 7924 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7925 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7926 | if (err) |
| 7927 | return err; |
| 7928 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 7929 | if (is_ctx_reg(env, insn->dst_reg)) { |
Joe Stringer | 9d2be44 | 2018-10-02 13:35:31 -0700 | [diff] [blame] | 7930 | 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] | 7931 | insn->dst_reg, |
| 7932 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 7933 | return -EACCES; |
| 7934 | } |
| 7935 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7936 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7937 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 7938 | insn->off, BPF_SIZE(insn->code), |
| 7939 | BPF_WRITE, -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7940 | if (err) |
| 7941 | return err; |
| 7942 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7943 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7944 | u8 opcode = BPF_OP(insn->code); |
| 7945 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7946 | env->jmps_processed++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7947 | if (opcode == BPF_CALL) { |
| 7948 | if (BPF_SRC(insn->code) != BPF_K || |
| 7949 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7950 | (insn->src_reg != BPF_REG_0 && |
| 7951 | insn->src_reg != BPF_PSEUDO_CALL) || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7952 | insn->dst_reg != BPF_REG_0 || |
| 7953 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7954 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7955 | return -EINVAL; |
| 7956 | } |
| 7957 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 7958 | if (env->cur_state->active_spin_lock && |
| 7959 | (insn->src_reg == BPF_PSEUDO_CALL || |
| 7960 | insn->imm != BPF_FUNC_spin_unlock)) { |
| 7961 | verbose(env, "function calls are not allowed while holding a lock\n"); |
| 7962 | return -EINVAL; |
| 7963 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7964 | if (insn->src_reg == BPF_PSEUDO_CALL) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7965 | err = check_func_call(env, insn, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7966 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7967 | err = check_helper_call(env, insn->imm, env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7968 | if (err) |
| 7969 | return err; |
| 7970 | |
| 7971 | } else if (opcode == BPF_JA) { |
| 7972 | if (BPF_SRC(insn->code) != BPF_K || |
| 7973 | insn->imm != 0 || |
| 7974 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7975 | insn->dst_reg != BPF_REG_0 || |
| 7976 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7977 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7978 | return -EINVAL; |
| 7979 | } |
| 7980 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7981 | env->insn_idx += insn->off + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7982 | continue; |
| 7983 | |
| 7984 | } else if (opcode == BPF_EXIT) { |
| 7985 | if (BPF_SRC(insn->code) != BPF_K || |
| 7986 | insn->imm != 0 || |
| 7987 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7988 | insn->dst_reg != BPF_REG_0 || |
| 7989 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7990 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7991 | return -EINVAL; |
| 7992 | } |
| 7993 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 7994 | if (env->cur_state->active_spin_lock) { |
| 7995 | verbose(env, "bpf_spin_unlock is missing\n"); |
| 7996 | return -EINVAL; |
| 7997 | } |
| 7998 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7999 | if (state->curframe) { |
| 8000 | /* exit from nested function */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 8001 | err = prepare_func_exit(env, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8002 | if (err) |
| 8003 | return err; |
| 8004 | do_print_state = true; |
| 8005 | continue; |
| 8006 | } |
| 8007 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 8008 | err = check_reference_leak(env); |
| 8009 | if (err) |
| 8010 | return err; |
| 8011 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8012 | /* eBPF calling convetion is such that R0 is used |
| 8013 | * to return the value from eBPF program. |
| 8014 | * Make sure that it's readable at this time |
| 8015 | * of bpf_exit, which means that program wrote |
| 8016 | * something into it earlier |
| 8017 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8018 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8019 | if (err) |
| 8020 | return err; |
| 8021 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 8022 | if (is_pointer_value(env, BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8023 | verbose(env, "R0 leaks addr as return value\n"); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 8024 | return -EACCES; |
| 8025 | } |
| 8026 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 8027 | err = check_return_code(env); |
| 8028 | if (err) |
| 8029 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8030 | process_bpf_exit: |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8031 | update_branch_counts(env, env->cur_state); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8032 | err = pop_stack(env, &prev_insn_idx, |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 8033 | &env->insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8034 | if (err < 0) { |
| 8035 | if (err != -ENOENT) |
| 8036 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8037 | break; |
| 8038 | } else { |
| 8039 | do_print_state = true; |
| 8040 | continue; |
| 8041 | } |
| 8042 | } else { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 8043 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8044 | if (err) |
| 8045 | return err; |
| 8046 | } |
| 8047 | } else if (class == BPF_LD) { |
| 8048 | u8 mode = BPF_MODE(insn->code); |
| 8049 | |
| 8050 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 8051 | err = check_ld_abs(env, insn); |
| 8052 | if (err) |
| 8053 | return err; |
| 8054 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8055 | } else if (mode == BPF_IMM) { |
| 8056 | err = check_ld_imm(env, insn); |
| 8057 | if (err) |
| 8058 | return err; |
| 8059 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 8060 | env->insn_idx++; |
| 8061 | env->insn_aux_data[env->insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8062 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8063 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8064 | return -EINVAL; |
| 8065 | } |
| 8066 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8067 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8068 | return -EINVAL; |
| 8069 | } |
| 8070 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 8071 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8072 | } |
| 8073 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 8074 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8075 | return 0; |
| 8076 | } |
| 8077 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 8078 | static int check_map_prealloc(struct bpf_map *map) |
| 8079 | { |
| 8080 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 8081 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 8082 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 8083 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 8084 | } |
| 8085 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 8086 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
| 8087 | { |
| 8088 | switch (type) { |
| 8089 | case BPF_PROG_TYPE_KPROBE: |
| 8090 | case BPF_PROG_TYPE_TRACEPOINT: |
| 8091 | case BPF_PROG_TYPE_PERF_EVENT: |
| 8092 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 8093 | return true; |
| 8094 | default: |
| 8095 | return false; |
| 8096 | } |
| 8097 | } |
| 8098 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8099 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 8100 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 8101 | struct bpf_prog *prog) |
| 8102 | |
| 8103 | { |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 8104 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 8105 | * preallocated hash maps, since doing memory allocation |
| 8106 | * in overflow_handler can crash depending on where nmi got |
| 8107 | * triggered. |
| 8108 | */ |
| 8109 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 8110 | if (!check_map_prealloc(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8111 | 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] | 8112 | return -EINVAL; |
| 8113 | } |
| 8114 | if (map->inner_map_meta && |
| 8115 | !check_map_prealloc(map->inner_map_meta)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8116 | verbose(env, "perf_event programs can only use preallocated inner hash map\n"); |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 8117 | return -EINVAL; |
| 8118 | } |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 8119 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 8120 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 8121 | if ((is_tracing_prog_type(prog->type) || |
| 8122 | prog->type == BPF_PROG_TYPE_SOCKET_FILTER) && |
| 8123 | map_value_has_spin_lock(map)) { |
| 8124 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); |
| 8125 | return -EINVAL; |
| 8126 | } |
| 8127 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 8128 | 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] | 8129 | !bpf_offload_prog_map_match(prog, map)) { |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 8130 | verbose(env, "offload device mismatch between prog and map\n"); |
| 8131 | return -EINVAL; |
| 8132 | } |
| 8133 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 8134 | return 0; |
| 8135 | } |
| 8136 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 8137 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 8138 | { |
| 8139 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 8140 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 8141 | } |
| 8142 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8143 | /* look for pseudo eBPF instructions that access map FDs and |
| 8144 | * replace them with actual map pointers |
| 8145 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8146 | static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8147 | { |
| 8148 | struct bpf_insn *insn = env->prog->insnsi; |
| 8149 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 8150 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8151 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 8152 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 8153 | if (err) |
| 8154 | return err; |
| 8155 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8156 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8157 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 8158 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8159 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8160 | return -EINVAL; |
| 8161 | } |
| 8162 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 8163 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 8164 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 8165 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8166 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 8167 | return -EINVAL; |
| 8168 | } |
| 8169 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8170 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 8171 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8172 | struct bpf_map *map; |
| 8173 | struct fd f; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 8174 | u64 addr; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8175 | |
| 8176 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 8177 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 8178 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8179 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8180 | return -EINVAL; |
| 8181 | } |
| 8182 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 8183 | if (insn[0].src_reg == 0) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8184 | /* valid generic load 64-bit imm */ |
| 8185 | goto next_insn; |
| 8186 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 8187 | /* In final convert_pseudo_ld_imm64() step, this is |
| 8188 | * converted into regular 64-bit imm load insn. |
| 8189 | */ |
| 8190 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && |
| 8191 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || |
| 8192 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && |
| 8193 | insn[1].imm != 0)) { |
| 8194 | verbose(env, |
| 8195 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8196 | return -EINVAL; |
| 8197 | } |
| 8198 | |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 8199 | f = fdget(insn[0].imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 8200 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8201 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8202 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 8203 | insn[0].imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8204 | return PTR_ERR(map); |
| 8205 | } |
| 8206 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8207 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 8208 | if (err) { |
| 8209 | fdput(f); |
| 8210 | return err; |
| 8211 | } |
| 8212 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 8213 | aux = &env->insn_aux_data[i]; |
| 8214 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 8215 | addr = (unsigned long)map; |
| 8216 | } else { |
| 8217 | u32 off = insn[1].imm; |
| 8218 | |
| 8219 | if (off >= BPF_MAX_VAR_OFF) { |
| 8220 | verbose(env, "direct value offset of %u is not allowed\n", off); |
| 8221 | fdput(f); |
| 8222 | return -EINVAL; |
| 8223 | } |
| 8224 | |
| 8225 | if (!map->ops->map_direct_value_addr) { |
| 8226 | verbose(env, "no direct value access support for this map type\n"); |
| 8227 | fdput(f); |
| 8228 | return -EINVAL; |
| 8229 | } |
| 8230 | |
| 8231 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 8232 | if (err) { |
| 8233 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", |
| 8234 | map->value_size, off); |
| 8235 | fdput(f); |
| 8236 | return err; |
| 8237 | } |
| 8238 | |
| 8239 | aux->map_off = off; |
| 8240 | addr += off; |
| 8241 | } |
| 8242 | |
| 8243 | insn[0].imm = (u32)addr; |
| 8244 | insn[1].imm = addr >> 32; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8245 | |
| 8246 | /* check whether we recorded this map already */ |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 8247 | for (j = 0; j < env->used_map_cnt; j++) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8248 | if (env->used_maps[j] == map) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 8249 | aux->map_index = j; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8250 | fdput(f); |
| 8251 | goto next_insn; |
| 8252 | } |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 8253 | } |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8254 | |
| 8255 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 8256 | fdput(f); |
| 8257 | return -E2BIG; |
| 8258 | } |
| 8259 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8260 | /* hold the map. If the program is rejected by verifier, |
| 8261 | * the map will be released by release_maps() or it |
| 8262 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 8263 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8264 | */ |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 8265 | bpf_map_inc(map); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 8266 | |
| 8267 | aux->map_index = env->used_map_cnt; |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 8268 | env->used_maps[env->used_map_cnt++] = map; |
| 8269 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 8270 | if (bpf_map_is_cgroup_storage(map) && |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 8271 | bpf_cgroup_storage_assign(env->prog, map)) { |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 8272 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 8273 | fdput(f); |
| 8274 | return -EBUSY; |
| 8275 | } |
| 8276 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8277 | fdput(f); |
| 8278 | next_insn: |
| 8279 | insn++; |
| 8280 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 8281 | continue; |
| 8282 | } |
| 8283 | |
| 8284 | /* Basic sanity check before we invest more work here. */ |
| 8285 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 8286 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 8287 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8288 | } |
| 8289 | } |
| 8290 | |
| 8291 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 8292 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 8293 | * These pointers will be used later by verifier to validate map access. |
| 8294 | */ |
| 8295 | return 0; |
| 8296 | } |
| 8297 | |
| 8298 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8299 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8300 | { |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 8301 | enum bpf_cgroup_storage_type stype; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8302 | int i; |
| 8303 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 8304 | for_each_cgroup_storage_type(stype) { |
| 8305 | if (!env->prog->aux->cgroup_storage[stype]) |
| 8306 | continue; |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 8307 | bpf_cgroup_storage_release(env->prog, |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 8308 | env->prog->aux->cgroup_storage[stype]); |
| 8309 | } |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 8310 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8311 | for (i = 0; i < env->used_map_cnt; i++) |
| 8312 | bpf_map_put(env->used_maps[i]); |
| 8313 | } |
| 8314 | |
| 8315 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8316 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8317 | { |
| 8318 | struct bpf_insn *insn = env->prog->insnsi; |
| 8319 | int insn_cnt = env->prog->len; |
| 8320 | int i; |
| 8321 | |
| 8322 | for (i = 0; i < insn_cnt; i++, insn++) |
| 8323 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 8324 | insn->src_reg = 0; |
| 8325 | } |
| 8326 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 8327 | /* single env->prog->insni[off] instruction was replaced with the range |
| 8328 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 8329 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 8330 | */ |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 8331 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, |
| 8332 | struct bpf_prog *new_prog, u32 off, u32 cnt) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 8333 | { |
| 8334 | 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] | 8335 | struct bpf_insn *insn = new_prog->insnsi; |
| 8336 | u32 prog_len; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 8337 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 8338 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 8339 | /* aux info at OFF always needs adjustment, no matter fast path |
| 8340 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the |
| 8341 | * original insn at old prog. |
| 8342 | */ |
| 8343 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); |
| 8344 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 8345 | if (cnt == 1) |
| 8346 | return 0; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 8347 | prog_len = new_prog->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 8348 | new_data = vzalloc(array_size(prog_len, |
| 8349 | sizeof(struct bpf_insn_aux_data))); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 8350 | if (!new_data) |
| 8351 | return -ENOMEM; |
| 8352 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 8353 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 8354 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 8355 | for (i = off; i < off + cnt - 1; i++) { |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 8356 | new_data[i].seen = true; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 8357 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
| 8358 | } |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 8359 | env->insn_aux_data = new_data; |
| 8360 | vfree(old_data); |
| 8361 | return 0; |
| 8362 | } |
| 8363 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 8364 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 8365 | { |
| 8366 | int i; |
| 8367 | |
| 8368 | if (len == 1) |
| 8369 | return; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 8370 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 8371 | for (i = 0; i <= env->subprog_cnt; i++) { |
Edward Cree | afd5942 | 2018-11-16 12:00:07 +0000 | [diff] [blame] | 8372 | if (env->subprog_info[i].start <= off) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 8373 | continue; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 8374 | env->subprog_info[i].start += len - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 8375 | } |
| 8376 | } |
| 8377 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 8378 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 8379 | const struct bpf_insn *patch, u32 len) |
| 8380 | { |
| 8381 | struct bpf_prog *new_prog; |
| 8382 | |
| 8383 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 8384 | if (IS_ERR(new_prog)) { |
| 8385 | if (PTR_ERR(new_prog) == -ERANGE) |
| 8386 | verbose(env, |
| 8387 | "insn %d cannot be patched due to 16-bit range\n", |
| 8388 | env->insn_aux_data[off].orig_idx); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 8389 | return NULL; |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 8390 | } |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 8391 | if (adjust_insn_aux_data(env, new_prog, off, len)) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 8392 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 8393 | adjust_subprog_starts(env, off, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 8394 | return new_prog; |
| 8395 | } |
| 8396 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 8397 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
| 8398 | u32 off, u32 cnt) |
| 8399 | { |
| 8400 | int i, j; |
| 8401 | |
| 8402 | /* find first prog starting at or after off (first to remove) */ |
| 8403 | for (i = 0; i < env->subprog_cnt; i++) |
| 8404 | if (env->subprog_info[i].start >= off) |
| 8405 | break; |
| 8406 | /* find first prog starting at or after off + cnt (first to stay) */ |
| 8407 | for (j = i; j < env->subprog_cnt; j++) |
| 8408 | if (env->subprog_info[j].start >= off + cnt) |
| 8409 | break; |
| 8410 | /* if j doesn't start exactly at off + cnt, we are just removing |
| 8411 | * the front of previous prog |
| 8412 | */ |
| 8413 | if (env->subprog_info[j].start != off + cnt) |
| 8414 | j--; |
| 8415 | |
| 8416 | if (j > i) { |
| 8417 | struct bpf_prog_aux *aux = env->prog->aux; |
| 8418 | int move; |
| 8419 | |
| 8420 | /* move fake 'exit' subprog as well */ |
| 8421 | move = env->subprog_cnt + 1 - j; |
| 8422 | |
| 8423 | memmove(env->subprog_info + i, |
| 8424 | env->subprog_info + j, |
| 8425 | sizeof(*env->subprog_info) * move); |
| 8426 | env->subprog_cnt -= j - i; |
| 8427 | |
| 8428 | /* remove func_info */ |
| 8429 | if (aux->func_info) { |
| 8430 | move = aux->func_info_cnt - j; |
| 8431 | |
| 8432 | memmove(aux->func_info + i, |
| 8433 | aux->func_info + j, |
| 8434 | sizeof(*aux->func_info) * move); |
| 8435 | aux->func_info_cnt -= j - i; |
| 8436 | /* func_info->insn_off is set after all code rewrites, |
| 8437 | * in adjust_btf_func() - no need to adjust |
| 8438 | */ |
| 8439 | } |
| 8440 | } else { |
| 8441 | /* convert i from "first prog to remove" to "first to adjust" */ |
| 8442 | if (env->subprog_info[i].start == off) |
| 8443 | i++; |
| 8444 | } |
| 8445 | |
| 8446 | /* update fake 'exit' subprog as well */ |
| 8447 | for (; i <= env->subprog_cnt; i++) |
| 8448 | env->subprog_info[i].start -= cnt; |
| 8449 | |
| 8450 | return 0; |
| 8451 | } |
| 8452 | |
| 8453 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, |
| 8454 | u32 cnt) |
| 8455 | { |
| 8456 | struct bpf_prog *prog = env->prog; |
| 8457 | u32 i, l_off, l_cnt, nr_linfo; |
| 8458 | struct bpf_line_info *linfo; |
| 8459 | |
| 8460 | nr_linfo = prog->aux->nr_linfo; |
| 8461 | if (!nr_linfo) |
| 8462 | return 0; |
| 8463 | |
| 8464 | linfo = prog->aux->linfo; |
| 8465 | |
| 8466 | /* find first line info to remove, count lines to be removed */ |
| 8467 | for (i = 0; i < nr_linfo; i++) |
| 8468 | if (linfo[i].insn_off >= off) |
| 8469 | break; |
| 8470 | |
| 8471 | l_off = i; |
| 8472 | l_cnt = 0; |
| 8473 | for (; i < nr_linfo; i++) |
| 8474 | if (linfo[i].insn_off < off + cnt) |
| 8475 | l_cnt++; |
| 8476 | else |
| 8477 | break; |
| 8478 | |
| 8479 | /* First live insn doesn't match first live linfo, it needs to "inherit" |
| 8480 | * last removed linfo. prog is already modified, so prog->len == off |
| 8481 | * means no live instructions after (tail of the program was removed). |
| 8482 | */ |
| 8483 | if (prog->len != off && l_cnt && |
| 8484 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { |
| 8485 | l_cnt--; |
| 8486 | linfo[--i].insn_off = off + cnt; |
| 8487 | } |
| 8488 | |
| 8489 | /* remove the line info which refer to the removed instructions */ |
| 8490 | if (l_cnt) { |
| 8491 | memmove(linfo + l_off, linfo + i, |
| 8492 | sizeof(*linfo) * (nr_linfo - i)); |
| 8493 | |
| 8494 | prog->aux->nr_linfo -= l_cnt; |
| 8495 | nr_linfo = prog->aux->nr_linfo; |
| 8496 | } |
| 8497 | |
| 8498 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ |
| 8499 | for (i = l_off; i < nr_linfo; i++) |
| 8500 | linfo[i].insn_off -= cnt; |
| 8501 | |
| 8502 | /* fix up all subprogs (incl. 'exit') which start >= off */ |
| 8503 | for (i = 0; i <= env->subprog_cnt; i++) |
| 8504 | if (env->subprog_info[i].linfo_idx > l_off) { |
| 8505 | /* program may have started in the removed region but |
| 8506 | * may not be fully removed |
| 8507 | */ |
| 8508 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) |
| 8509 | env->subprog_info[i].linfo_idx -= l_cnt; |
| 8510 | else |
| 8511 | env->subprog_info[i].linfo_idx = l_off; |
| 8512 | } |
| 8513 | |
| 8514 | return 0; |
| 8515 | } |
| 8516 | |
| 8517 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) |
| 8518 | { |
| 8519 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 8520 | unsigned int orig_prog_len = env->prog->len; |
| 8521 | int err; |
| 8522 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 8523 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 8524 | bpf_prog_offload_remove_insns(env, off, cnt); |
| 8525 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 8526 | err = bpf_remove_insns(env->prog, off, cnt); |
| 8527 | if (err) |
| 8528 | return err; |
| 8529 | |
| 8530 | err = adjust_subprog_starts_after_remove(env, off, cnt); |
| 8531 | if (err) |
| 8532 | return err; |
| 8533 | |
| 8534 | err = bpf_adj_linfo_after_remove(env, off, cnt); |
| 8535 | if (err) |
| 8536 | return err; |
| 8537 | |
| 8538 | memmove(aux_data + off, aux_data + off + cnt, |
| 8539 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); |
| 8540 | |
| 8541 | return 0; |
| 8542 | } |
| 8543 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 8544 | /* The verifier does more data flow analysis than llvm and will not |
| 8545 | * explore branches that are dead at run time. Malicious programs can |
| 8546 | * have dead code too. Therefore replace all dead at-run-time code |
| 8547 | * with 'ja -1'. |
| 8548 | * |
| 8549 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 8550 | * program and through another bug we would manage to jump there, then |
| 8551 | * we'd execute beyond program memory otherwise. Returning exception |
| 8552 | * code also wouldn't work since we can have subprogs where the dead |
| 8553 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 8554 | */ |
| 8555 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 8556 | { |
| 8557 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 8558 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 8559 | struct bpf_insn *insn = env->prog->insnsi; |
| 8560 | const int insn_cnt = env->prog->len; |
| 8561 | int i; |
| 8562 | |
| 8563 | for (i = 0; i < insn_cnt; i++) { |
| 8564 | if (aux_data[i].seen) |
| 8565 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 8566 | memcpy(insn + i, &trap, sizeof(trap)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 8567 | } |
| 8568 | } |
| 8569 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8570 | static bool insn_is_cond_jump(u8 code) |
| 8571 | { |
| 8572 | u8 op; |
| 8573 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 8574 | if (BPF_CLASS(code) == BPF_JMP32) |
| 8575 | return true; |
| 8576 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8577 | if (BPF_CLASS(code) != BPF_JMP) |
| 8578 | return false; |
| 8579 | |
| 8580 | op = BPF_OP(code); |
| 8581 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; |
| 8582 | } |
| 8583 | |
| 8584 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) |
| 8585 | { |
| 8586 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 8587 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 8588 | struct bpf_insn *insn = env->prog->insnsi; |
| 8589 | const int insn_cnt = env->prog->len; |
| 8590 | int i; |
| 8591 | |
| 8592 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 8593 | if (!insn_is_cond_jump(insn->code)) |
| 8594 | continue; |
| 8595 | |
| 8596 | if (!aux_data[i + 1].seen) |
| 8597 | ja.off = insn->off; |
| 8598 | else if (!aux_data[i + 1 + insn->off].seen) |
| 8599 | ja.off = 0; |
| 8600 | else |
| 8601 | continue; |
| 8602 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 8603 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 8604 | bpf_prog_offload_replace_insn(env, i, &ja); |
| 8605 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8606 | memcpy(insn, &ja, sizeof(ja)); |
| 8607 | } |
| 8608 | } |
| 8609 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 8610 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
| 8611 | { |
| 8612 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 8613 | int insn_cnt = env->prog->len; |
| 8614 | int i, err; |
| 8615 | |
| 8616 | for (i = 0; i < insn_cnt; i++) { |
| 8617 | int j; |
| 8618 | |
| 8619 | j = 0; |
| 8620 | while (i + j < insn_cnt && !aux_data[i + j].seen) |
| 8621 | j++; |
| 8622 | if (!j) |
| 8623 | continue; |
| 8624 | |
| 8625 | err = verifier_remove_insns(env, i, j); |
| 8626 | if (err) |
| 8627 | return err; |
| 8628 | insn_cnt = env->prog->len; |
| 8629 | } |
| 8630 | |
| 8631 | return 0; |
| 8632 | } |
| 8633 | |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 8634 | static int opt_remove_nops(struct bpf_verifier_env *env) |
| 8635 | { |
| 8636 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 8637 | struct bpf_insn *insn = env->prog->insnsi; |
| 8638 | int insn_cnt = env->prog->len; |
| 8639 | int i, err; |
| 8640 | |
| 8641 | for (i = 0; i < insn_cnt; i++) { |
| 8642 | if (memcmp(&insn[i], &ja, sizeof(ja))) |
| 8643 | continue; |
| 8644 | |
| 8645 | err = verifier_remove_insns(env, i, 1); |
| 8646 | if (err) |
| 8647 | return err; |
| 8648 | insn_cnt--; |
| 8649 | i--; |
| 8650 | } |
| 8651 | |
| 8652 | return 0; |
| 8653 | } |
| 8654 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 8655 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
| 8656 | const union bpf_attr *attr) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 8657 | { |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 8658 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 8659 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 8660 | int i, patch_len, delta = 0, len = env->prog->len; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 8661 | struct bpf_insn *insns = env->prog->insnsi; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 8662 | struct bpf_prog *new_prog; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 8663 | bool rnd_hi32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 8664 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 8665 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 8666 | zext_patch[1] = BPF_ZEXT_REG(0); |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 8667 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
| 8668 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); |
| 8669 | 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] | 8670 | for (i = 0; i < len; i++) { |
| 8671 | int adj_idx = i + delta; |
| 8672 | struct bpf_insn insn; |
| 8673 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 8674 | insn = insns[adj_idx]; |
| 8675 | if (!aux[adj_idx].zext_dst) { |
| 8676 | u8 code, class; |
| 8677 | u32 imm_rnd; |
| 8678 | |
| 8679 | if (!rnd_hi32) |
| 8680 | continue; |
| 8681 | |
| 8682 | code = insn.code; |
| 8683 | class = BPF_CLASS(code); |
| 8684 | if (insn_no_def(&insn)) |
| 8685 | continue; |
| 8686 | |
| 8687 | /* NOTE: arg "reg" (the fourth one) is only used for |
| 8688 | * BPF_STX which has been ruled out in above |
| 8689 | * check, it is safe to pass NULL here. |
| 8690 | */ |
| 8691 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { |
| 8692 | if (class == BPF_LD && |
| 8693 | BPF_MODE(code) == BPF_IMM) |
| 8694 | i++; |
| 8695 | continue; |
| 8696 | } |
| 8697 | |
| 8698 | /* ctx load could be transformed into wider load. */ |
| 8699 | if (class == BPF_LDX && |
| 8700 | aux[adj_idx].ptr_type == PTR_TO_CTX) |
| 8701 | continue; |
| 8702 | |
| 8703 | imm_rnd = get_random_int(); |
| 8704 | rnd_hi32_patch[0] = insn; |
| 8705 | rnd_hi32_patch[1].imm = imm_rnd; |
| 8706 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; |
| 8707 | patch = rnd_hi32_patch; |
| 8708 | patch_len = 4; |
| 8709 | goto apply_patch_buffer; |
| 8710 | } |
| 8711 | |
| 8712 | if (!bpf_jit_needs_zext()) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 8713 | continue; |
| 8714 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 8715 | zext_patch[0] = insn; |
| 8716 | zext_patch[1].dst_reg = insn.dst_reg; |
| 8717 | zext_patch[1].src_reg = insn.dst_reg; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 8718 | patch = zext_patch; |
| 8719 | patch_len = 2; |
| 8720 | apply_patch_buffer: |
| 8721 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 8722 | if (!new_prog) |
| 8723 | return -ENOMEM; |
| 8724 | env->prog = new_prog; |
| 8725 | insns = new_prog->insnsi; |
| 8726 | aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 8727 | delta += patch_len - 1; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 8728 | } |
| 8729 | |
| 8730 | return 0; |
| 8731 | } |
| 8732 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 8733 | /* convert load instructions that access fields of a context type into a |
| 8734 | * sequence of instructions that access fields of the underlying structure: |
| 8735 | * struct __sk_buff -> struct sk_buff |
| 8736 | * struct bpf_sock_ops -> struct sock |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8737 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8738 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8739 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 8740 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 8741 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8742 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 8743 | struct bpf_insn insn_buf[16], *insn; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 8744 | u32 target_size, size_default, off; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8745 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 8746 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 8747 | bool is_narrower_load; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8748 | |
Daniel Borkmann | b09928b | 2018-10-24 22:05:49 +0200 | [diff] [blame] | 8749 | if (ops->gen_prologue || env->seen_direct_write) { |
| 8750 | if (!ops->gen_prologue) { |
| 8751 | verbose(env, "bpf verifier is misconfigured\n"); |
| 8752 | return -EINVAL; |
| 8753 | } |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 8754 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 8755 | env->prog); |
| 8756 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8757 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 8758 | return -EINVAL; |
| 8759 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 8760 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 8761 | if (!new_prog) |
| 8762 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 8763 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 8764 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8765 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 8766 | } |
| 8767 | } |
| 8768 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 8769 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8770 | return 0; |
| 8771 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8772 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 8773 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8774 | for (i = 0; i < insn_cnt; i++, insn++) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 8775 | bpf_convert_ctx_access_t convert_ctx_access; |
| 8776 | |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 8777 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 8778 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 8779 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 8780 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 8781 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 8782 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 8783 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 8784 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 8785 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 8786 | type = BPF_WRITE; |
| 8787 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8788 | continue; |
| 8789 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 8790 | if (type == BPF_WRITE && |
| 8791 | env->insn_aux_data[i + delta].sanitize_stack_off) { |
| 8792 | struct bpf_insn patch[] = { |
| 8793 | /* Sanitize suspicious stack slot with zero. |
| 8794 | * There are no memory dependencies for this store, |
| 8795 | * since it's only using frame pointer and immediate |
| 8796 | * constant of zero |
| 8797 | */ |
| 8798 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, |
| 8799 | env->insn_aux_data[i + delta].sanitize_stack_off, |
| 8800 | 0), |
| 8801 | /* the original STX instruction will immediately |
| 8802 | * overwrite the same stack slot with appropriate value |
| 8803 | */ |
| 8804 | *insn, |
| 8805 | }; |
| 8806 | |
| 8807 | cnt = ARRAY_SIZE(patch); |
| 8808 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 8809 | if (!new_prog) |
| 8810 | return -ENOMEM; |
| 8811 | |
| 8812 | delta += cnt - 1; |
| 8813 | env->prog = new_prog; |
| 8814 | insn = new_prog->insnsi + i + delta; |
| 8815 | continue; |
| 8816 | } |
| 8817 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 8818 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 8819 | case PTR_TO_CTX: |
| 8820 | if (!ops->convert_ctx_access) |
| 8821 | continue; |
| 8822 | convert_ctx_access = ops->convert_ctx_access; |
| 8823 | break; |
| 8824 | case PTR_TO_SOCKET: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 8825 | case PTR_TO_SOCK_COMMON: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 8826 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 8827 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 8828 | case PTR_TO_TCP_SOCK: |
| 8829 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; |
| 8830 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 8831 | case PTR_TO_XDP_SOCK: |
| 8832 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; |
| 8833 | break; |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 8834 | case PTR_TO_BTF_ID: |
| 8835 | if (type == BPF_WRITE) { |
| 8836 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
| 8837 | return -EINVAL; |
| 8838 | } |
| 8839 | insn->code = BPF_LDX | BPF_PROBE_MEM | BPF_SIZE((insn)->code); |
Alexei Starovoitov | 3dec541 | 2019-10-15 20:25:03 -0700 | [diff] [blame] | 8840 | env->prog->aux->num_exentries++; |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 8841 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 8842 | default: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8843 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 8844 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8845 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 8846 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 8847 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 8848 | |
| 8849 | /* If the read access is a narrower load of the field, |
| 8850 | * convert to a 4/8-byte load, to minimum program type specific |
| 8851 | * convert_ctx_access changes. If conversion is successful, |
| 8852 | * we will apply proper mask to the result. |
| 8853 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 8854 | is_narrower_load = size < ctx_field_size; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 8855 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
| 8856 | off = insn->off; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 8857 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 8858 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 8859 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 8860 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8861 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 8862 | return -EINVAL; |
| 8863 | } |
| 8864 | |
| 8865 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 8866 | if (ctx_field_size == 4) |
| 8867 | size_code = BPF_W; |
| 8868 | else if (ctx_field_size == 8) |
| 8869 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 8870 | |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 8871 | insn->off = off & ~(size_default - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 8872 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 8873 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 8874 | |
| 8875 | target_size = 0; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 8876 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 8877 | &target_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 8878 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 8879 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8880 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8881 | return -EINVAL; |
| 8882 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 8883 | |
| 8884 | if (is_narrower_load && size < target_size) { |
Ilya Leoshkevich | d895a0f | 2019-08-16 12:53:00 +0200 | [diff] [blame] | 8885 | u8 shift = bpf_ctx_narrow_access_offset( |
| 8886 | off, size, size_default) * 8; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 8887 | if (ctx_field_size <= 4) { |
| 8888 | if (shift) |
| 8889 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, |
| 8890 | insn->dst_reg, |
| 8891 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 8892 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 8893 | (1 << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 8894 | } else { |
| 8895 | if (shift) |
| 8896 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, |
| 8897 | insn->dst_reg, |
| 8898 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 8899 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Krzesimir Nowak | e2f7fc0 | 2019-05-08 18:08:58 +0200 | [diff] [blame] | 8900 | (1ULL << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 8901 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 8902 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8903 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 8904 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8905 | if (!new_prog) |
| 8906 | return -ENOMEM; |
| 8907 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8908 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8909 | |
| 8910 | /* keep walking new program and skip insns we just inserted */ |
| 8911 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8912 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8913 | } |
| 8914 | |
| 8915 | return 0; |
| 8916 | } |
| 8917 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8918 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 8919 | { |
| 8920 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 8921 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 8922 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8923 | void *old_bpf_func; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8924 | int err; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8925 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 8926 | if (env->subprog_cnt <= 1) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8927 | return 0; |
| 8928 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 8929 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8930 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 8931 | insn->src_reg != BPF_PSEUDO_CALL) |
| 8932 | continue; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 8933 | /* Upon error here we cannot fall back to interpreter but |
| 8934 | * need a hard reject of the program. Thus -EFAULT is |
| 8935 | * propagated in any case. |
| 8936 | */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8937 | subprog = find_subprog(env, i + insn->imm + 1); |
| 8938 | if (subprog < 0) { |
| 8939 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 8940 | i + insn->imm + 1); |
| 8941 | return -EFAULT; |
| 8942 | } |
| 8943 | /* temporarily remember subprog id inside insn instead of |
| 8944 | * aux_data, since next loop will split up all insns into funcs |
| 8945 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 8946 | insn->off = subprog; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8947 | /* remember original imm in case JIT fails and fallback |
| 8948 | * to interpreter will be needed |
| 8949 | */ |
| 8950 | env->insn_aux_data[i].call_imm = insn->imm; |
| 8951 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 8952 | insn->imm = 1; |
| 8953 | } |
| 8954 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8955 | err = bpf_prog_alloc_jited_linfo(prog); |
| 8956 | if (err) |
| 8957 | goto out_undo_insn; |
| 8958 | |
| 8959 | err = -ENOMEM; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 8960 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8961 | if (!func) |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 8962 | goto out_undo_insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8963 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 8964 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8965 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 8966 | subprog_end = env->subprog_info[i + 1].start; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8967 | |
| 8968 | len = subprog_end - subprog_start; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 8969 | /* BPF_PROG_RUN doesn't call subprogs directly, |
| 8970 | * hence main prog stats include the runtime of subprogs. |
| 8971 | * subprogs don't have IDs and not reachable via prog_get_next_id |
| 8972 | * func[i]->aux->stats will never be accessed and stays NULL |
| 8973 | */ |
| 8974 | 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] | 8975 | if (!func[i]) |
| 8976 | goto out_free; |
| 8977 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 8978 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 8979 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8980 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 8981 | if (bpf_prog_calc_tag(func[i])) |
| 8982 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8983 | func[i]->is_func = 1; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8984 | func[i]->aux->func_idx = i; |
| 8985 | /* the btf and func_info will be freed only at prog->aux */ |
| 8986 | func[i]->aux->btf = prog->aux->btf; |
| 8987 | func[i]->aux->func_info = prog->aux->func_info; |
| 8988 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8989 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 8990 | * Long term would need debug info to populate names |
| 8991 | */ |
| 8992 | func[i]->aux->name[0] = 'F'; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 8993 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8994 | func[i]->jit_requested = 1; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8995 | func[i]->aux->linfo = prog->aux->linfo; |
| 8996 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; |
| 8997 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; |
| 8998 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8999 | func[i] = bpf_int_jit_compile(func[i]); |
| 9000 | if (!func[i]->jited) { |
| 9001 | err = -ENOTSUPP; |
| 9002 | goto out_free; |
| 9003 | } |
| 9004 | cond_resched(); |
| 9005 | } |
| 9006 | /* at this point all bpf functions were successfully JITed |
| 9007 | * now populate all bpf_calls with correct addresses and |
| 9008 | * run last pass of JIT |
| 9009 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 9010 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 9011 | insn = func[i]->insnsi; |
| 9012 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 9013 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 9014 | insn->src_reg != BPF_PSEUDO_CALL) |
| 9015 | continue; |
| 9016 | subprog = insn->off; |
Prashant Bhole | 0d306c3 | 2019-04-16 18:13:01 +0900 | [diff] [blame] | 9017 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
| 9018 | __bpf_call_base; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 9019 | } |
Sandipan Das | 2162fed | 2018-05-24 12:26:45 +0530 | [diff] [blame] | 9020 | |
| 9021 | /* we use the aux data to keep a list of the start addresses |
| 9022 | * of the JITed images for each function in the program |
| 9023 | * |
| 9024 | * for some architectures, such as powerpc64, the imm field |
| 9025 | * might not be large enough to hold the offset of the start |
| 9026 | * address of the callee's JITed image from __bpf_call_base |
| 9027 | * |
| 9028 | * in such cases, we can lookup the start address of a callee |
| 9029 | * by using its subprog id, available from the off field of |
| 9030 | * the call instruction, as an index for this list |
| 9031 | */ |
| 9032 | func[i]->aux->func = func; |
| 9033 | func[i]->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 9034 | } |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 9035 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 9036 | old_bpf_func = func[i]->bpf_func; |
| 9037 | tmp = bpf_int_jit_compile(func[i]); |
| 9038 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 9039 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 9040 | err = -ENOTSUPP; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 9041 | goto out_free; |
| 9042 | } |
| 9043 | cond_resched(); |
| 9044 | } |
| 9045 | |
| 9046 | /* finally lock prog and jit images for all functions and |
| 9047 | * populate kallsysm |
| 9048 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 9049 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 9050 | bpf_prog_lock_ro(func[i]); |
| 9051 | bpf_prog_kallsyms_add(func[i]); |
| 9052 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 9053 | |
| 9054 | /* Last step: make now unused interpreter insns from main |
| 9055 | * prog consistent for later dump requests, so they can |
| 9056 | * later look the same as if they were interpreted only. |
| 9057 | */ |
| 9058 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 9059 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 9060 | insn->src_reg != BPF_PSEUDO_CALL) |
| 9061 | continue; |
| 9062 | insn->off = env->insn_aux_data[i].call_imm; |
| 9063 | subprog = find_subprog(env, i + insn->off + 1); |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 9064 | insn->imm = subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 9065 | } |
| 9066 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 9067 | prog->jited = 1; |
| 9068 | prog->bpf_func = func[0]->bpf_func; |
| 9069 | prog->aux->func = func; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 9070 | prog->aux->func_cnt = env->subprog_cnt; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 9071 | bpf_prog_free_unused_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 9072 | return 0; |
| 9073 | out_free: |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 9074 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 9075 | if (func[i]) |
| 9076 | bpf_jit_free(func[i]); |
| 9077 | kfree(func); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 9078 | out_undo_insn: |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 9079 | /* cleanup main prog to be interpreted */ |
| 9080 | prog->jit_requested = 0; |
| 9081 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 9082 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 9083 | insn->src_reg != BPF_PSEUDO_CALL) |
| 9084 | continue; |
| 9085 | insn->off = 0; |
| 9086 | insn->imm = env->insn_aux_data[i].call_imm; |
| 9087 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 9088 | bpf_prog_free_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 9089 | return err; |
| 9090 | } |
| 9091 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 9092 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 9093 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 9094 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 9095 | struct bpf_prog *prog = env->prog; |
| 9096 | struct bpf_insn *insn = prog->insnsi; |
| 9097 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 9098 | #endif |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 9099 | int err = 0; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 9100 | |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 9101 | if (env->prog->jit_requested && |
| 9102 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 9103 | err = jit_subprogs(env); |
| 9104 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 9105 | return 0; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 9106 | if (err == -EFAULT) |
| 9107 | return err; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 9108 | } |
| 9109 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 9110 | for (i = 0; i < prog->len; i++, insn++) { |
| 9111 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 9112 | insn->src_reg != BPF_PSEUDO_CALL) |
| 9113 | continue; |
| 9114 | depth = get_callee_stack_depth(env, insn, i); |
| 9115 | if (depth < 0) |
| 9116 | return depth; |
| 9117 | bpf_patch_call_args(insn, depth); |
| 9118 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 9119 | err = 0; |
| 9120 | #endif |
| 9121 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 9122 | } |
| 9123 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9124 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 9125 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 9126 | * |
| 9127 | * this function is called after eBPF program passed verification |
| 9128 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9129 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 9130 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9131 | struct bpf_prog *prog = env->prog; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 9132 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9133 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 9134 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9135 | const int insn_cnt = prog->len; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 9136 | const struct bpf_map_ops *ops; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 9137 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 9138 | struct bpf_insn insn_buf[16]; |
| 9139 | struct bpf_prog *new_prog; |
| 9140 | struct bpf_map *map_ptr; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 9141 | int i, ret, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 9142 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9143 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 9144 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 9145 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 9146 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 9147 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 9148 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
| 9149 | struct bpf_insn mask_and_div[] = { |
| 9150 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 9151 | /* Rx div 0 -> 0 */ |
| 9152 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), |
| 9153 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 9154 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 9155 | *insn, |
| 9156 | }; |
| 9157 | struct bpf_insn mask_and_mod[] = { |
| 9158 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 9159 | /* Rx mod 0 -> Rx */ |
| 9160 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), |
| 9161 | *insn, |
| 9162 | }; |
| 9163 | struct bpf_insn *patchlet; |
| 9164 | |
| 9165 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 9166 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 9167 | patchlet = mask_and_div + (is64 ? 1 : 0); |
| 9168 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); |
| 9169 | } else { |
| 9170 | patchlet = mask_and_mod + (is64 ? 1 : 0); |
| 9171 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); |
| 9172 | } |
| 9173 | |
| 9174 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 9175 | if (!new_prog) |
| 9176 | return -ENOMEM; |
| 9177 | |
| 9178 | delta += cnt - 1; |
| 9179 | env->prog = prog = new_prog; |
| 9180 | insn = new_prog->insnsi + i + delta; |
| 9181 | continue; |
| 9182 | } |
| 9183 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 9184 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 9185 | (BPF_MODE(insn->code) == BPF_ABS || |
| 9186 | BPF_MODE(insn->code) == BPF_IND)) { |
| 9187 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 9188 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 9189 | verbose(env, "bpf verifier is misconfigured\n"); |
| 9190 | return -EINVAL; |
| 9191 | } |
| 9192 | |
| 9193 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 9194 | if (!new_prog) |
| 9195 | return -ENOMEM; |
| 9196 | |
| 9197 | delta += cnt - 1; |
| 9198 | env->prog = prog = new_prog; |
| 9199 | insn = new_prog->insnsi + i + delta; |
| 9200 | continue; |
| 9201 | } |
| 9202 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 9203 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
| 9204 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { |
| 9205 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; |
| 9206 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; |
| 9207 | struct bpf_insn insn_buf[16]; |
| 9208 | struct bpf_insn *patch = &insn_buf[0]; |
| 9209 | bool issrc, isneg; |
| 9210 | u32 off_reg; |
| 9211 | |
| 9212 | aux = &env->insn_aux_data[i + delta]; |
Daniel Borkmann | 3612af7 | 2019-03-01 22:05:29 +0100 | [diff] [blame] | 9213 | if (!aux->alu_state || |
| 9214 | aux->alu_state == BPF_ALU_NON_POINTER) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 9215 | continue; |
| 9216 | |
| 9217 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; |
| 9218 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == |
| 9219 | BPF_ALU_SANITIZE_SRC; |
| 9220 | |
| 9221 | off_reg = issrc ? insn->src_reg : insn->dst_reg; |
| 9222 | if (isneg) |
| 9223 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 9224 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); |
| 9225 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); |
| 9226 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); |
| 9227 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); |
| 9228 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); |
| 9229 | if (issrc) { |
| 9230 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, |
| 9231 | off_reg); |
| 9232 | insn->src_reg = BPF_REG_AX; |
| 9233 | } else { |
| 9234 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, |
| 9235 | BPF_REG_AX); |
| 9236 | } |
| 9237 | if (isneg) |
| 9238 | insn->code = insn->code == code_add ? |
| 9239 | code_sub : code_add; |
| 9240 | *patch++ = *insn; |
| 9241 | if (issrc && isneg) |
| 9242 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 9243 | cnt = patch - insn_buf; |
| 9244 | |
| 9245 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 9246 | if (!new_prog) |
| 9247 | return -ENOMEM; |
| 9248 | |
| 9249 | delta += cnt - 1; |
| 9250 | env->prog = prog = new_prog; |
| 9251 | insn = new_prog->insnsi + i + delta; |
| 9252 | continue; |
| 9253 | } |
| 9254 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9255 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 9256 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 9257 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 9258 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 9259 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9260 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 9261 | prog->dst_needed = 1; |
| 9262 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 9263 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 9264 | if (insn->imm == BPF_FUNC_override_return) |
| 9265 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9266 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 9267 | /* If we tail call into other programs, we |
| 9268 | * cannot make any assumptions since they can |
| 9269 | * be replaced dynamically during runtime in |
| 9270 | * the program array. |
| 9271 | */ |
| 9272 | prog->cb_access = 1; |
Alexei Starovoitov | 80a58d0 | 2017-05-30 13:31:30 -0700 | [diff] [blame] | 9273 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 9274 | env->prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 9275 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9276 | /* mark bpf_tail_call as different opcode to avoid |
| 9277 | * conditional branch in the interpeter for every normal |
| 9278 | * call and to prevent accidental JITing by JIT compiler |
| 9279 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 9280 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9281 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 9282 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 9283 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 9284 | aux = &env->insn_aux_data[i + delta]; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 9285 | if (prog->jit_requested && !expect_blinding && |
| 9286 | !bpf_map_key_poisoned(aux) && |
| 9287 | !bpf_map_ptr_poisoned(aux) && |
| 9288 | !bpf_map_ptr_unpriv(aux)) { |
| 9289 | struct bpf_jit_poke_descriptor desc = { |
| 9290 | .reason = BPF_POKE_REASON_TAIL_CALL, |
| 9291 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), |
| 9292 | .tail_call.key = bpf_map_key_immediate(aux), |
| 9293 | }; |
| 9294 | |
| 9295 | ret = bpf_jit_add_poke_descriptor(prog, &desc); |
| 9296 | if (ret < 0) { |
| 9297 | verbose(env, "adding tail call poke descriptor failed\n"); |
| 9298 | return ret; |
| 9299 | } |
| 9300 | |
| 9301 | insn->imm = ret + 1; |
| 9302 | continue; |
| 9303 | } |
| 9304 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 9305 | if (!bpf_map_ptr_unpriv(aux)) |
| 9306 | continue; |
| 9307 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 9308 | /* instead of changing every JIT dealing with tail_call |
| 9309 | * emit two extra insns: |
| 9310 | * if (index >= max_entries) goto out; |
| 9311 | * index &= array->index_mask; |
| 9312 | * to avoid out-of-bounds cpu speculation |
| 9313 | */ |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 9314 | if (bpf_map_ptr_poisoned(aux)) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 9315 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 9316 | return -EINVAL; |
| 9317 | } |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 9318 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 9319 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 9320 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 9321 | map_ptr->max_entries, 2); |
| 9322 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 9323 | container_of(map_ptr, |
| 9324 | struct bpf_array, |
| 9325 | map)->index_mask); |
| 9326 | insn_buf[2] = *insn; |
| 9327 | cnt = 3; |
| 9328 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 9329 | if (!new_prog) |
| 9330 | return -ENOMEM; |
| 9331 | |
| 9332 | delta += cnt - 1; |
| 9333 | env->prog = prog = new_prog; |
| 9334 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9335 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 9336 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 9337 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 9338 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 9339 | * and other inlining handlers are currently limited to 64 bit |
| 9340 | * only. |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 9341 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 9342 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 9343 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 9344 | insn->imm == BPF_FUNC_map_update_elem || |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 9345 | insn->imm == BPF_FUNC_map_delete_elem || |
| 9346 | insn->imm == BPF_FUNC_map_push_elem || |
| 9347 | insn->imm == BPF_FUNC_map_pop_elem || |
| 9348 | insn->imm == BPF_FUNC_map_peek_elem)) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 9349 | aux = &env->insn_aux_data[i + delta]; |
| 9350 | if (bpf_map_ptr_poisoned(aux)) |
| 9351 | goto patch_call_imm; |
| 9352 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 9353 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 9354 | ops = map_ptr->ops; |
| 9355 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 9356 | ops->map_gen_lookup) { |
| 9357 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
| 9358 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 9359 | verbose(env, "bpf verifier is misconfigured\n"); |
| 9360 | return -EINVAL; |
| 9361 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 9362 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 9363 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 9364 | insn_buf, cnt); |
| 9365 | if (!new_prog) |
| 9366 | return -ENOMEM; |
| 9367 | |
| 9368 | delta += cnt - 1; |
| 9369 | env->prog = prog = new_prog; |
| 9370 | insn = new_prog->insnsi + i + delta; |
| 9371 | continue; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 9372 | } |
| 9373 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 9374 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 9375 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 9376 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 9377 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 9378 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 9379 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 9380 | u64 flags))NULL)); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 9381 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 9382 | (int (*)(struct bpf_map *map, void *value, |
| 9383 | u64 flags))NULL)); |
| 9384 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 9385 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 9386 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 9387 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 9388 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 9389 | switch (insn->imm) { |
| 9390 | case BPF_FUNC_map_lookup_elem: |
| 9391 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - |
| 9392 | __bpf_call_base; |
| 9393 | continue; |
| 9394 | case BPF_FUNC_map_update_elem: |
| 9395 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - |
| 9396 | __bpf_call_base; |
| 9397 | continue; |
| 9398 | case BPF_FUNC_map_delete_elem: |
| 9399 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - |
| 9400 | __bpf_call_base; |
| 9401 | continue; |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 9402 | case BPF_FUNC_map_push_elem: |
| 9403 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - |
| 9404 | __bpf_call_base; |
| 9405 | continue; |
| 9406 | case BPF_FUNC_map_pop_elem: |
| 9407 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - |
| 9408 | __bpf_call_base; |
| 9409 | continue; |
| 9410 | case BPF_FUNC_map_peek_elem: |
| 9411 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - |
| 9412 | __bpf_call_base; |
| 9413 | continue; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 9414 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 9415 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 9416 | goto patch_call_imm; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 9417 | } |
| 9418 | |
| 9419 | patch_call_imm: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 9420 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9421 | /* all functions that have prototype and verifier allowed |
| 9422 | * programs to call them, must be real in-kernel functions |
| 9423 | */ |
| 9424 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9425 | verbose(env, |
| 9426 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9427 | func_id_name(insn->imm), insn->imm); |
| 9428 | return -EFAULT; |
| 9429 | } |
| 9430 | insn->imm = fn->func - __bpf_call_base; |
| 9431 | } |
| 9432 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 9433 | /* Since poke tab is now finalized, publish aux to tracker. */ |
| 9434 | for (i = 0; i < prog->aux->size_poke_tab; i++) { |
| 9435 | map_ptr = prog->aux->poke_tab[i].tail_call.map; |
| 9436 | if (!map_ptr->ops->map_poke_track || |
| 9437 | !map_ptr->ops->map_poke_untrack || |
| 9438 | !map_ptr->ops->map_poke_run) { |
| 9439 | verbose(env, "bpf verifier is misconfigured\n"); |
| 9440 | return -EINVAL; |
| 9441 | } |
| 9442 | |
| 9443 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); |
| 9444 | if (ret < 0) { |
| 9445 | verbose(env, "tracking tail call prog failed\n"); |
| 9446 | return ret; |
| 9447 | } |
| 9448 | } |
| 9449 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9450 | return 0; |
| 9451 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 9452 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9453 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9454 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9455 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9456 | int i; |
| 9457 | |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 9458 | sl = env->free_list; |
| 9459 | while (sl) { |
| 9460 | sln = sl->next; |
| 9461 | free_verifier_state(&sl->state, false); |
| 9462 | kfree(sl); |
| 9463 | sl = sln; |
| 9464 | } |
| 9465 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9466 | if (!env->explored_states) |
| 9467 | return; |
| 9468 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 9469 | for (i = 0; i < state_htab_size(env); i++) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9470 | sl = env->explored_states[i]; |
| 9471 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 9472 | while (sl) { |
| 9473 | sln = sl->next; |
| 9474 | free_verifier_state(&sl->state, false); |
| 9475 | kfree(sl); |
| 9476 | sl = sln; |
| 9477 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9478 | } |
| 9479 | |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 9480 | kvfree(env->explored_states); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9481 | } |
| 9482 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9483 | static void print_verification_stats(struct bpf_verifier_env *env) |
| 9484 | { |
| 9485 | int i; |
| 9486 | |
| 9487 | if (env->log.level & BPF_LOG_STATS) { |
| 9488 | verbose(env, "verification time %lld usec\n", |
| 9489 | div_u64(env->verification_time, 1000)); |
| 9490 | verbose(env, "stack depth "); |
| 9491 | for (i = 0; i < env->subprog_cnt; i++) { |
| 9492 | u32 depth = env->subprog_info[i].stack_depth; |
| 9493 | |
| 9494 | verbose(env, "%d", depth); |
| 9495 | if (i + 1 < env->subprog_cnt) |
| 9496 | verbose(env, "+"); |
| 9497 | } |
| 9498 | verbose(env, "\n"); |
| 9499 | } |
| 9500 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " |
| 9501 | "total_states %d peak_states %d mark_read %d\n", |
| 9502 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, |
| 9503 | env->max_states_per_insn, env->total_states, |
| 9504 | env->peak_states, env->longest_mark_read_walk); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 9505 | } |
| 9506 | |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9507 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
| 9508 | { |
| 9509 | struct bpf_prog *prog = env->prog; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9510 | struct bpf_prog *tgt_prog = prog->aux->linked_prog; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9511 | u32 btf_id = prog->aux->attach_btf_id; |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 9512 | const char prefix[] = "btf_trace_"; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9513 | int ret = 0, subprog = -1, i; |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 9514 | struct bpf_trampoline *tr; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9515 | const struct btf_type *t; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9516 | bool conservative = true; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9517 | const char *tname; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9518 | struct btf *btf; |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 9519 | long addr; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9520 | u64 key; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9521 | |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 9522 | if (prog->type != BPF_PROG_TYPE_TRACING) |
| 9523 | return 0; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9524 | |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 9525 | if (!btf_id) { |
| 9526 | verbose(env, "Tracing programs must provide btf_id\n"); |
| 9527 | return -EINVAL; |
| 9528 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9529 | btf = bpf_prog_get_target_btf(prog); |
| 9530 | if (!btf) { |
| 9531 | verbose(env, |
| 9532 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
| 9533 | return -EINVAL; |
| 9534 | } |
| 9535 | t = btf_type_by_id(btf, btf_id); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 9536 | if (!t) { |
| 9537 | verbose(env, "attach_btf_id %u is invalid\n", btf_id); |
| 9538 | return -EINVAL; |
| 9539 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9540 | tname = btf_name_by_offset(btf, t->name_off); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 9541 | if (!tname) { |
| 9542 | verbose(env, "attach_btf_id %u doesn't have a name\n", btf_id); |
| 9543 | return -EINVAL; |
| 9544 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9545 | if (tgt_prog) { |
| 9546 | struct bpf_prog_aux *aux = tgt_prog->aux; |
| 9547 | |
| 9548 | for (i = 0; i < aux->func_info_cnt; i++) |
| 9549 | if (aux->func_info[i].type_id == btf_id) { |
| 9550 | subprog = i; |
| 9551 | break; |
| 9552 | } |
| 9553 | if (subprog == -1) { |
| 9554 | verbose(env, "Subprog %s doesn't exist\n", tname); |
| 9555 | return -EINVAL; |
| 9556 | } |
| 9557 | conservative = aux->func_info_aux[subprog].unreliable; |
| 9558 | key = ((u64)aux->id) << 32 | btf_id; |
| 9559 | } else { |
| 9560 | key = btf_id; |
| 9561 | } |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 9562 | |
| 9563 | switch (prog->expected_attach_type) { |
| 9564 | case BPF_TRACE_RAW_TP: |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9565 | if (tgt_prog) { |
| 9566 | verbose(env, |
| 9567 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
| 9568 | return -EINVAL; |
| 9569 | } |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9570 | if (!btf_type_is_typedef(t)) { |
| 9571 | verbose(env, "attach_btf_id %u is not a typedef\n", |
| 9572 | btf_id); |
| 9573 | return -EINVAL; |
| 9574 | } |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 9575 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9576 | verbose(env, "attach_btf_id %u points to wrong type name %s\n", |
| 9577 | btf_id, tname); |
| 9578 | return -EINVAL; |
| 9579 | } |
| 9580 | tname += sizeof(prefix) - 1; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9581 | t = btf_type_by_id(btf, t->type); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9582 | if (!btf_type_is_ptr(t)) |
| 9583 | /* should never happen in valid vmlinux build */ |
| 9584 | return -EINVAL; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9585 | t = btf_type_by_id(btf, t->type); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9586 | if (!btf_type_is_func_proto(t)) |
| 9587 | /* should never happen in valid vmlinux build */ |
| 9588 | return -EINVAL; |
| 9589 | |
| 9590 | /* remember two read only pointers that are valid for |
| 9591 | * the life time of the kernel |
| 9592 | */ |
| 9593 | prog->aux->attach_func_name = tname; |
| 9594 | prog->aux->attach_func_proto = t; |
| 9595 | prog->aux->attach_btf_trace = true; |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 9596 | return 0; |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 9597 | case BPF_TRACE_FENTRY: |
| 9598 | case BPF_TRACE_FEXIT: |
| 9599 | if (!btf_type_is_func(t)) { |
| 9600 | verbose(env, "attach_btf_id %u is not a function\n", |
| 9601 | btf_id); |
| 9602 | return -EINVAL; |
| 9603 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9604 | t = btf_type_by_id(btf, t->type); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 9605 | if (!btf_type_is_func_proto(t)) |
| 9606 | return -EINVAL; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9607 | tr = bpf_trampoline_lookup(key); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 9608 | if (!tr) |
| 9609 | return -ENOMEM; |
| 9610 | prog->aux->attach_func_name = tname; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9611 | /* t is either vmlinux type or another program's type */ |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 9612 | prog->aux->attach_func_proto = t; |
| 9613 | mutex_lock(&tr->mutex); |
| 9614 | if (tr->func.addr) { |
| 9615 | prog->aux->trampoline = tr; |
| 9616 | goto out; |
| 9617 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9618 | if (tgt_prog && conservative) { |
| 9619 | prog->aux->attach_func_proto = NULL; |
| 9620 | t = NULL; |
| 9621 | } |
| 9622 | ret = btf_distill_func_proto(&env->log, btf, t, |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 9623 | tname, &tr->func.model); |
| 9624 | if (ret < 0) |
| 9625 | goto out; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9626 | if (tgt_prog) { |
| 9627 | if (!tgt_prog->jited) { |
| 9628 | /* for now */ |
| 9629 | verbose(env, "Can trace only JITed BPF progs\n"); |
| 9630 | ret = -EINVAL; |
| 9631 | goto out; |
| 9632 | } |
| 9633 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING) { |
| 9634 | /* prevent cycles */ |
| 9635 | verbose(env, "Cannot recursively attach\n"); |
| 9636 | ret = -EINVAL; |
| 9637 | goto out; |
| 9638 | } |
Yonghong Song | e9eeec5 | 2019-12-04 17:06:06 -0800 | [diff] [blame^] | 9639 | if (subprog == 0) |
| 9640 | addr = (long) tgt_prog->bpf_func; |
| 9641 | else |
| 9642 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 9643 | } else { |
| 9644 | addr = kallsyms_lookup_name(tname); |
| 9645 | if (!addr) { |
| 9646 | verbose(env, |
| 9647 | "The address of function %s cannot be found\n", |
| 9648 | tname); |
| 9649 | ret = -ENOENT; |
| 9650 | goto out; |
| 9651 | } |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 9652 | } |
| 9653 | tr->func.addr = (void *)addr; |
| 9654 | prog->aux->trampoline = tr; |
| 9655 | out: |
| 9656 | mutex_unlock(&tr->mutex); |
| 9657 | if (ret) |
| 9658 | bpf_trampoline_put(tr); |
| 9659 | return ret; |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 9660 | default: |
| 9661 | return -EINVAL; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9662 | } |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9663 | } |
| 9664 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 9665 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
| 9666 | union bpf_attr __user *uattr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 9667 | { |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9668 | u64 start_time = ktime_get_ns(); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9669 | struct bpf_verifier_env *env; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 9670 | struct bpf_verifier_log *log; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 9671 | int i, len, ret = -EINVAL; |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 9672 | bool is_priv; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 9673 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 9674 | /* no program is valid */ |
| 9675 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 9676 | return -EINVAL; |
| 9677 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9678 | /* '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] | 9679 | * allocate/free it every time bpf_check() is called |
| 9680 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9681 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 9682 | if (!env) |
| 9683 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9684 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 9685 | |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 9686 | len = (*prog)->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 9687 | env->insn_aux_data = |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 9688 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9689 | ret = -ENOMEM; |
| 9690 | if (!env->insn_aux_data) |
| 9691 | goto err_free_env; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 9692 | for (i = 0; i < len; i++) |
| 9693 | env->insn_aux_data[i].orig_idx = i; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9694 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 9695 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 9696 | is_priv = capable(CAP_SYS_ADMIN); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9697 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 9698 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { |
| 9699 | mutex_lock(&bpf_verifier_lock); |
| 9700 | if (!btf_vmlinux) |
| 9701 | btf_vmlinux = btf_parse_vmlinux(); |
| 9702 | mutex_unlock(&bpf_verifier_lock); |
| 9703 | } |
| 9704 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 9705 | /* grab the mutex to protect few globals used by verifier */ |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 9706 | if (!is_priv) |
| 9707 | mutex_lock(&bpf_verifier_lock); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 9708 | |
| 9709 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 9710 | /* user requested verbose verifier output |
| 9711 | * and supplied buffer to store the verification trace |
| 9712 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 9713 | log->level = attr->log_level; |
| 9714 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 9715 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 9716 | |
| 9717 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 9718 | /* log attributes have to be sane */ |
Alexei Starovoitov | 7a9f5c6 | 2019-04-01 21:27:46 -0700 | [diff] [blame] | 9719 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9720 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9721 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 9722 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 9723 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 9724 | if (IS_ERR(btf_vmlinux)) { |
| 9725 | /* Either gcc or pahole or kernel are broken. */ |
| 9726 | verbose(env, "in-kernel BTF is malformed\n"); |
| 9727 | ret = PTR_ERR(btf_vmlinux); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9728 | goto skip_full_check; |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 9729 | } |
| 9730 | |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 9731 | ret = check_attach_btf_id(env); |
| 9732 | if (ret) |
| 9733 | goto skip_full_check; |
| 9734 | |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 9735 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 9736 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 9737 | env->strict_alignment = true; |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 9738 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
| 9739 | env->strict_alignment = false; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 9740 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 9741 | env->allow_ptr_leaks = is_priv; |
| 9742 | |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 9743 | if (is_priv) |
| 9744 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; |
| 9745 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9746 | ret = replace_map_fd_with_map_ptr(env); |
| 9747 | if (ret < 0) |
| 9748 | goto skip_full_check; |
| 9749 | |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 9750 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Quentin Monnet | a40a263 | 2018-11-09 13:03:31 +0000 | [diff] [blame] | 9751 | ret = bpf_prog_offload_verifier_prep(env->prog); |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 9752 | if (ret) |
| 9753 | goto skip_full_check; |
| 9754 | } |
| 9755 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 9756 | env->explored_states = kvcalloc(state_htab_size(env), |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9757 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9758 | GFP_USER); |
| 9759 | ret = -ENOMEM; |
| 9760 | if (!env->explored_states) |
| 9761 | goto skip_full_check; |
| 9762 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 9763 | ret = check_subprogs(env); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9764 | if (ret < 0) |
| 9765 | goto skip_full_check; |
| 9766 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 9767 | ret = check_btf_info(env, attr, uattr); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 9768 | if (ret < 0) |
| 9769 | goto skip_full_check; |
| 9770 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 9771 | ret = check_cfg(env); |
| 9772 | if (ret < 0) |
| 9773 | goto skip_full_check; |
| 9774 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9775 | ret = do_check(env); |
Craig Gallek | 8c01c4f | 2017-11-02 11:18:01 -0400 | [diff] [blame] | 9776 | if (env->cur_state) { |
| 9777 | free_verifier_state(env->cur_state, true); |
| 9778 | env->cur_state = NULL; |
| 9779 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 9780 | |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 9781 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 9782 | ret = bpf_prog_offload_finalize(env); |
| 9783 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9784 | skip_full_check: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9785 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9786 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9787 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9788 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 9789 | ret = check_max_stack_depth(env); |
| 9790 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 9791 | /* instruction rewrites happen after this point */ |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 9792 | if (is_priv) { |
| 9793 | if (ret == 0) |
| 9794 | opt_hard_wire_dead_code_branches(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 9795 | if (ret == 0) |
| 9796 | ret = opt_remove_dead_code(env); |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 9797 | if (ret == 0) |
| 9798 | ret = opt_remove_nops(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 9799 | } else { |
| 9800 | if (ret == 0) |
| 9801 | sanitize_dead_code(env); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 9802 | } |
| 9803 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 9804 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9805 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 9806 | ret = convert_ctx_accesses(env); |
| 9807 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 9808 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 9809 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 9810 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 9811 | /* do 32-bit optimization after insn patching has done so those patched |
| 9812 | * insns could be handled correctly. |
| 9813 | */ |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 9814 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
| 9815 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); |
| 9816 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret |
| 9817 | : false; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 9818 | } |
| 9819 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 9820 | if (ret == 0) |
| 9821 | ret = fixup_call_args(env); |
| 9822 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9823 | env->verification_time = ktime_get_ns() - start_time; |
| 9824 | print_verification_stats(env); |
| 9825 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 9826 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 9827 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 9828 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 9829 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 9830 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 9831 | } |
| 9832 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9833 | if (ret == 0 && env->used_map_cnt) { |
| 9834 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9835 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 9836 | sizeof(env->used_maps[0]), |
| 9837 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9838 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9839 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9840 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 9841 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9842 | } |
| 9843 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9844 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9845 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9846 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9847 | |
| 9848 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 9849 | * bpf_ld_imm64 instructions |
| 9850 | */ |
| 9851 | convert_pseudo_ld_imm64(env); |
| 9852 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 9853 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 9854 | if (ret == 0) |
| 9855 | adjust_btf_func(env); |
| 9856 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 9857 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9858 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9859 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 9860 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9861 | */ |
| 9862 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9863 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9864 | err_unlock: |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 9865 | if (!is_priv) |
| 9866 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9867 | vfree(env->insn_aux_data); |
| 9868 | err_free_env: |
| 9869 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 9870 | return ret; |
| 9871 | } |