Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016 Facebook |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4 | * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5 | */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6 | #include <uapi/linux/btf.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/types.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/bpf.h> |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 11 | #include <linux/btf.h> |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 12 | #include <linux/bpf_verifier.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 13 | #include <linux/filter.h> |
| 14 | #include <net/netlink.h> |
| 15 | #include <linux/file.h> |
| 16 | #include <linux/vmalloc.h> |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 17 | #include <linux/stringify.h> |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 18 | #include <linux/bsearch.h> |
| 19 | #include <linux/sort.h> |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 20 | #include <linux/perf_event.h> |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 21 | #include <linux/ctype.h> |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 22 | #include <linux/error-injection.h> |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 23 | #include <linux/bpf_lsm.h> |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 24 | #include <linux/btf_ids.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 25 | |
Jakub Kicinski | f4ac7e0 | 2017-10-09 10:30:12 -0700 | [diff] [blame] | 26 | #include "disasm.h" |
| 27 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 28 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
Alexei Starovoitov | 91cc1a9 | 2019-11-14 10:57:15 -0800 | [diff] [blame] | 29 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 30 | [_id] = & _name ## _verifier_ops, |
| 31 | #define BPF_MAP_TYPE(_id, _ops) |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 32 | #define BPF_LINK_TYPE(_id, _name) |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 33 | #include <linux/bpf_types.h> |
| 34 | #undef BPF_PROG_TYPE |
| 35 | #undef BPF_MAP_TYPE |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 36 | #undef BPF_LINK_TYPE |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 39 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 40 | * instruction by instruction and updates register/stack state. |
| 41 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 42 | * |
| 43 | * The first pass is depth-first-search to check that the program is a DAG. |
| 44 | * It rejects the following programs: |
| 45 | * - larger than BPF_MAXINSNS insns |
| 46 | * - if loop is present (detected via back-edge) |
| 47 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 48 | * - out of bounds or malformed jumps |
| 49 | * The second pass is all possible path descent from the 1st insn. |
| 50 | * Since it's analyzing all pathes through the program, the length of the |
Gary Lin | eba38a9 | 2017-03-01 16:25:51 +0800 | [diff] [blame] | 51 | * analysis is limited to 64k insn, which may be hit even if total number of |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 52 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 53 | * Number of 'branches to be analyzed' is limited to 1k |
| 54 | * |
| 55 | * On entry to each instruction, each register has a type, and the instruction |
| 56 | * changes the types of the registers depending on instruction semantics. |
| 57 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 58 | * copied to R1. |
| 59 | * |
| 60 | * All registers are 64-bit. |
| 61 | * R0 - return register |
| 62 | * R1-R5 argument passing registers |
| 63 | * R6-R9 callee saved registers |
| 64 | * R10 - frame pointer read-only |
| 65 | * |
| 66 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 67 | * and has type PTR_TO_CTX. |
| 68 | * |
| 69 | * Verifier tracks arithmetic operations on pointers in case: |
| 70 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 71 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 72 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 73 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 74 | * that it wants to construct a pointer to some element within stack. |
| 75 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 76 | * (and -20 constant is saved for further stack bounds checking). |
| 77 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 78 | * |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 79 | * Most of the time the registers have SCALAR_VALUE type, which |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 80 | * means the register has some value, but it's not a valid pointer. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 81 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 82 | * |
| 83 | * When verifier sees load or store instructions the type of base register |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 84 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
| 85 | * four pointer types recognized by check_mem_access() function. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 86 | * |
| 87 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 88 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 89 | * |
| 90 | * registers used to pass values to function calls are checked against |
| 91 | * function argument constraints. |
| 92 | * |
| 93 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 94 | * It means that the register type passed to this function must be |
| 95 | * PTR_TO_STACK and it will be used inside the function as |
| 96 | * 'pointer to map element key' |
| 97 | * |
| 98 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 99 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 100 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 101 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 102 | * |
| 103 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 104 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 105 | * 2nd argument should be a pointer to stack, which will be used inside |
| 106 | * the helper function as a pointer to map element key. |
| 107 | * |
| 108 | * On the kernel side the helper function looks like: |
| 109 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 110 | * { |
| 111 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 112 | * void *key = (void *) (unsigned long) r2; |
| 113 | * void *value; |
| 114 | * |
| 115 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 116 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 117 | * the stack of eBPF program. |
| 118 | * } |
| 119 | * |
| 120 | * Corresponding eBPF program may look like: |
| 121 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 122 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 123 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 124 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 125 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 126 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 127 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 128 | * |
| 129 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 130 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 131 | * and were initialized prior to this call. |
| 132 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 133 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 134 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 135 | * returns ether pointer to map value or NULL. |
| 136 | * |
| 137 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 138 | * insn, the register holding that pointer in the true branch changes state to |
| 139 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 140 | * branch. See check_cond_jmp_op(). |
| 141 | * |
| 142 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 143 | * are set to NOT_INIT to indicate that they are no longer readable. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 144 | * |
| 145 | * The following reference types represent a potential reference to a kernel |
| 146 | * resource which, after first being allocated, must be checked and freed by |
| 147 | * the BPF program: |
| 148 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET |
| 149 | * |
| 150 | * When the verifier sees a helper call return a reference type, it allocates a |
| 151 | * pointer id for the reference and stores it in the current function state. |
| 152 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into |
| 153 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type |
| 154 | * passes through a NULL-check conditional. For the branch wherein the state is |
| 155 | * changed to CONST_IMM, the verifier releases the reference. |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 156 | * |
| 157 | * For each helper function that allocates a reference, such as |
| 158 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as |
| 159 | * bpf_sk_release(). When a reference type passes into the release function, |
| 160 | * the verifier also releases the reference. If any unchecked or unreleased |
| 161 | * reference remains at the end of the program, the verifier rejects it. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 162 | */ |
| 163 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 164 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 165 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 166 | /* verifer state is 'st' |
| 167 | * before processing instruction 'insn_idx' |
| 168 | * and after processing instruction 'prev_insn_idx' |
| 169 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 170 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 171 | int insn_idx; |
| 172 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 173 | struct bpf_verifier_stack_elem *next; |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 174 | /* length of verifier log at the time this state was pushed on stack */ |
| 175 | u32 log_pos; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 178 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 179 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 180 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 181 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
| 182 | #define BPF_MAP_KEY_SEEN (1ULL << 62) |
| 183 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 184 | #define BPF_MAP_PTR_UNPRIV 1UL |
| 185 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ |
| 186 | POISON_POINTER_DELTA)) |
| 187 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) |
| 188 | |
| 189 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
| 190 | { |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 191 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) |
| 195 | { |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 196 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, |
| 200 | const struct bpf_map *map, bool unpriv) |
| 201 | { |
| 202 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); |
| 203 | unpriv |= bpf_map_ptr_unpriv(aux); |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 204 | aux->map_ptr_state = (unsigned long)map | |
| 205 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); |
| 206 | } |
| 207 | |
| 208 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) |
| 209 | { |
| 210 | return aux->map_key_state & BPF_MAP_KEY_POISON; |
| 211 | } |
| 212 | |
| 213 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) |
| 214 | { |
| 215 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); |
| 216 | } |
| 217 | |
| 218 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) |
| 219 | { |
| 220 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); |
| 221 | } |
| 222 | |
| 223 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) |
| 224 | { |
| 225 | bool poisoned = bpf_map_key_poisoned(aux); |
| 226 | |
| 227 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | |
| 228 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 229 | } |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 230 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 231 | struct bpf_call_arg_meta { |
| 232 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 233 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 234 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 235 | int regno; |
| 236 | int access_size; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 237 | int mem_size; |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 238 | u64 msize_max_value; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 239 | int ref_obj_id; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 240 | int func_id; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 241 | }; |
| 242 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 243 | struct btf *btf_vmlinux; |
| 244 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 245 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 246 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 247 | static const struct bpf_line_info * |
| 248 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) |
| 249 | { |
| 250 | const struct bpf_line_info *linfo; |
| 251 | const struct bpf_prog *prog; |
| 252 | u32 i, nr_linfo; |
| 253 | |
| 254 | prog = env->prog; |
| 255 | nr_linfo = prog->aux->nr_linfo; |
| 256 | |
| 257 | if (!nr_linfo || insn_off >= prog->len) |
| 258 | return NULL; |
| 259 | |
| 260 | linfo = prog->aux->linfo; |
| 261 | for (i = 1; i < nr_linfo; i++) |
| 262 | if (insn_off < linfo[i].insn_off) |
| 263 | break; |
| 264 | |
| 265 | return &linfo[i - 1]; |
| 266 | } |
| 267 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 268 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
| 269 | va_list args) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 270 | { |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 271 | unsigned int n; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 272 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 273 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 274 | |
| 275 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 276 | "verifier log line truncated - local buffer too short\n"); |
| 277 | |
| 278 | n = min(log->len_total - log->len_used - 1, n); |
| 279 | log->kbuf[n] = '\0'; |
| 280 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 281 | if (log->level == BPF_LOG_KERNEL) { |
| 282 | pr_err("BPF:%s\n", log->kbuf); |
| 283 | return; |
| 284 | } |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 285 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 286 | log->len_used += n; |
| 287 | else |
| 288 | log->ubuf = NULL; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 289 | } |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 290 | |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 291 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
| 292 | { |
| 293 | char zero = 0; |
| 294 | |
| 295 | if (!bpf_verifier_log_needed(log)) |
| 296 | return; |
| 297 | |
| 298 | log->len_used = new_pos; |
| 299 | if (put_user(zero, log->ubuf + new_pos)) |
| 300 | log->ubuf = NULL; |
| 301 | } |
| 302 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 303 | /* log_level controls verbosity level of eBPF verifier. |
| 304 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 305 | * so the user can figure out what's wrong with the program |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 306 | */ |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 307 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 308 | const char *fmt, ...) |
| 309 | { |
| 310 | va_list args; |
| 311 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 312 | if (!bpf_verifier_log_needed(&env->log)) |
| 313 | return; |
| 314 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 315 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 316 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 317 | va_end(args); |
| 318 | } |
| 319 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 320 | |
| 321 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
| 322 | { |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 323 | struct bpf_verifier_env *env = private_data; |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 324 | va_list args; |
| 325 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 326 | if (!bpf_verifier_log_needed(&env->log)) |
| 327 | return; |
| 328 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 329 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 330 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 331 | va_end(args); |
| 332 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 333 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 334 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
| 335 | const char *fmt, ...) |
| 336 | { |
| 337 | va_list args; |
| 338 | |
| 339 | if (!bpf_verifier_log_needed(log)) |
| 340 | return; |
| 341 | |
| 342 | va_start(args, fmt); |
| 343 | bpf_verifier_vlog(log, fmt, args); |
| 344 | va_end(args); |
| 345 | } |
| 346 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 347 | static const char *ltrim(const char *s) |
| 348 | { |
| 349 | while (isspace(*s)) |
| 350 | s++; |
| 351 | |
| 352 | return s; |
| 353 | } |
| 354 | |
| 355 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, |
| 356 | u32 insn_off, |
| 357 | const char *prefix_fmt, ...) |
| 358 | { |
| 359 | const struct bpf_line_info *linfo; |
| 360 | |
| 361 | if (!bpf_verifier_log_needed(&env->log)) |
| 362 | return; |
| 363 | |
| 364 | linfo = find_linfo(env, insn_off); |
| 365 | if (!linfo || linfo == env->prev_linfo) |
| 366 | return; |
| 367 | |
| 368 | if (prefix_fmt) { |
| 369 | va_list args; |
| 370 | |
| 371 | va_start(args, prefix_fmt); |
| 372 | bpf_verifier_vlog(&env->log, prefix_fmt, args); |
| 373 | va_end(args); |
| 374 | } |
| 375 | |
| 376 | verbose(env, "%s\n", |
| 377 | ltrim(btf_name_by_offset(env->prog->aux->btf, |
| 378 | linfo->line_off))); |
| 379 | |
| 380 | env->prev_linfo = linfo; |
| 381 | } |
| 382 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 383 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 384 | { |
| 385 | return type == PTR_TO_PACKET || |
| 386 | type == PTR_TO_PACKET_META; |
| 387 | } |
| 388 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 389 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
| 390 | { |
| 391 | return type == PTR_TO_SOCKET || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 392 | type == PTR_TO_SOCK_COMMON || |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 393 | type == PTR_TO_TCP_SOCK || |
| 394 | type == PTR_TO_XDP_SOCK; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 395 | } |
| 396 | |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 397 | static bool reg_type_not_null(enum bpf_reg_type type) |
| 398 | { |
| 399 | return type == PTR_TO_SOCKET || |
| 400 | type == PTR_TO_TCP_SOCK || |
| 401 | type == PTR_TO_MAP_VALUE || |
Yonghong Song | 01c66c4 | 2020-06-30 10:12:40 -0700 | [diff] [blame] | 402 | type == PTR_TO_SOCK_COMMON; |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 405 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
| 406 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 407 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 408 | type == PTR_TO_SOCKET_OR_NULL || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 409 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 410 | type == PTR_TO_TCP_SOCK_OR_NULL || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 411 | type == PTR_TO_BTF_ID_OR_NULL || |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 412 | type == PTR_TO_MEM_OR_NULL || |
| 413 | type == PTR_TO_RDONLY_BUF_OR_NULL || |
| 414 | type == PTR_TO_RDWR_BUF_OR_NULL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 417 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
| 418 | { |
| 419 | return reg->type == PTR_TO_MAP_VALUE && |
| 420 | map_value_has_spin_lock(reg->map_ptr); |
| 421 | } |
| 422 | |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 423 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
| 424 | { |
| 425 | return type == PTR_TO_SOCKET || |
| 426 | type == PTR_TO_SOCKET_OR_NULL || |
| 427 | type == PTR_TO_TCP_SOCK || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 428 | type == PTR_TO_TCP_SOCK_OR_NULL || |
| 429 | type == PTR_TO_MEM || |
| 430 | type == PTR_TO_MEM_OR_NULL; |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 433 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 434 | { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 435 | return type == ARG_PTR_TO_SOCK_COMMON; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 438 | static bool arg_type_may_be_null(enum bpf_arg_type type) |
| 439 | { |
| 440 | return type == ARG_PTR_TO_MAP_VALUE_OR_NULL || |
| 441 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 442 | type == ARG_PTR_TO_CTX_OR_NULL || |
| 443 | type == ARG_PTR_TO_SOCKET_OR_NULL || |
| 444 | type == ARG_PTR_TO_ALLOC_MEM_OR_NULL; |
| 445 | } |
| 446 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 447 | /* Determine whether the function releases some resources allocated by another |
| 448 | * function call. The first reference type argument will be assumed to be |
| 449 | * released by release_reference(). |
| 450 | */ |
| 451 | static bool is_release_function(enum bpf_func_id func_id) |
| 452 | { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 453 | return func_id == BPF_FUNC_sk_release || |
| 454 | func_id == BPF_FUNC_ringbuf_submit || |
| 455 | func_id == BPF_FUNC_ringbuf_discard; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 458 | static bool may_be_acquire_function(enum bpf_func_id func_id) |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 459 | { |
| 460 | return func_id == BPF_FUNC_sk_lookup_tcp || |
Lorenz Bauer | edbf8c0 | 2019-03-22 09:54:01 +0800 | [diff] [blame] | 461 | func_id == BPF_FUNC_sk_lookup_udp || |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 462 | func_id == BPF_FUNC_skc_lookup_tcp || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 463 | func_id == BPF_FUNC_map_lookup_elem || |
| 464 | func_id == BPF_FUNC_ringbuf_reserve; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static bool is_acquire_function(enum bpf_func_id func_id, |
| 468 | const struct bpf_map *map) |
| 469 | { |
| 470 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; |
| 471 | |
| 472 | if (func_id == BPF_FUNC_sk_lookup_tcp || |
| 473 | func_id == BPF_FUNC_sk_lookup_udp || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 474 | func_id == BPF_FUNC_skc_lookup_tcp || |
| 475 | func_id == BPF_FUNC_ringbuf_reserve) |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 476 | return true; |
| 477 | |
| 478 | if (func_id == BPF_FUNC_map_lookup_elem && |
| 479 | (map_type == BPF_MAP_TYPE_SOCKMAP || |
| 480 | map_type == BPF_MAP_TYPE_SOCKHASH)) |
| 481 | return true; |
| 482 | |
| 483 | return false; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 486 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
| 487 | { |
| 488 | return func_id == BPF_FUNC_tcp_sock || |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 489 | func_id == BPF_FUNC_sk_fullsock || |
| 490 | func_id == BPF_FUNC_skc_to_tcp_sock || |
| 491 | func_id == BPF_FUNC_skc_to_tcp6_sock || |
| 492 | func_id == BPF_FUNC_skc_to_udp6_sock || |
| 493 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || |
| 494 | func_id == BPF_FUNC_skc_to_tcp_request_sock; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 497 | /* string representation of 'enum bpf_reg_type' */ |
| 498 | static const char * const reg_type_str[] = { |
| 499 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 500 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 501 | [PTR_TO_CTX] = "ctx", |
| 502 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 503 | [PTR_TO_MAP_VALUE] = "map_value", |
| 504 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 505 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 506 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 507 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 508 | [PTR_TO_PACKET_END] = "pkt_end", |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 509 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 510 | [PTR_TO_SOCKET] = "sock", |
| 511 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 512 | [PTR_TO_SOCK_COMMON] = "sock_common", |
| 513 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 514 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
| 515 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 516 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 517 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 518 | [PTR_TO_BTF_ID] = "ptr_", |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 519 | [PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_", |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 520 | [PTR_TO_MEM] = "mem", |
| 521 | [PTR_TO_MEM_OR_NULL] = "mem_or_null", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 522 | [PTR_TO_RDONLY_BUF] = "rdonly_buf", |
| 523 | [PTR_TO_RDONLY_BUF_OR_NULL] = "rdonly_buf_or_null", |
| 524 | [PTR_TO_RDWR_BUF] = "rdwr_buf", |
| 525 | [PTR_TO_RDWR_BUF_OR_NULL] = "rdwr_buf_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 526 | }; |
| 527 | |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 528 | static char slot_type_char[] = { |
| 529 | [STACK_INVALID] = '?', |
| 530 | [STACK_SPILL] = 'r', |
| 531 | [STACK_MISC] = 'm', |
| 532 | [STACK_ZERO] = '0', |
| 533 | }; |
| 534 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 535 | static void print_liveness(struct bpf_verifier_env *env, |
| 536 | enum bpf_reg_liveness live) |
| 537 | { |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 538 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 539 | verbose(env, "_"); |
| 540 | if (live & REG_LIVE_READ) |
| 541 | verbose(env, "r"); |
| 542 | if (live & REG_LIVE_WRITTEN) |
| 543 | verbose(env, "w"); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 544 | if (live & REG_LIVE_DONE) |
| 545 | verbose(env, "D"); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 548 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 549 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 550 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 551 | struct bpf_verifier_state *cur = env->cur_state; |
| 552 | |
| 553 | return cur->frame[reg->frameno]; |
| 554 | } |
| 555 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 556 | const char *kernel_type_name(u32 id) |
| 557 | { |
| 558 | return btf_name_by_offset(btf_vmlinux, |
| 559 | btf_type_by_id(btf_vmlinux, id)->name_off); |
| 560 | } |
| 561 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 562 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 563 | const struct bpf_func_state *state) |
| 564 | { |
| 565 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 566 | enum bpf_reg_type t; |
| 567 | int i; |
| 568 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 569 | if (state->frameno) |
| 570 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 571 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 572 | reg = &state->regs[i]; |
| 573 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 574 | if (t == NOT_INIT) |
| 575 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 576 | verbose(env, " R%d", i); |
| 577 | print_liveness(env, reg->live); |
| 578 | verbose(env, "=%s", reg_type_str[t]); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 579 | if (t == SCALAR_VALUE && reg->precise) |
| 580 | verbose(env, "P"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 581 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 582 | tnum_is_const(reg->var_off)) { |
| 583 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 584 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 585 | } else { |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 586 | if (t == PTR_TO_BTF_ID || t == PTR_TO_BTF_ID_OR_NULL) |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 587 | verbose(env, "%s", kernel_type_name(reg->btf_id)); |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 588 | verbose(env, "(id=%d", reg->id); |
| 589 | if (reg_type_may_be_refcounted_or_null(t)) |
| 590 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 591 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 592 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 593 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 594 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 595 | else if (t == CONST_PTR_TO_MAP || |
| 596 | t == PTR_TO_MAP_VALUE || |
| 597 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 598 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 599 | reg->map_ptr->key_size, |
| 600 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 601 | if (tnum_is_const(reg->var_off)) { |
| 602 | /* Typically an immediate SCALAR_VALUE, but |
| 603 | * could be a pointer whose offset is too big |
| 604 | * for reg->off |
| 605 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 606 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 607 | } else { |
| 608 | if (reg->smin_value != reg->umin_value && |
| 609 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 610 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 611 | (long long)reg->smin_value); |
| 612 | if (reg->smax_value != reg->umax_value && |
| 613 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 614 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 615 | (long long)reg->smax_value); |
| 616 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 617 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 618 | (unsigned long long)reg->umin_value); |
| 619 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 620 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 621 | (unsigned long long)reg->umax_value); |
| 622 | if (!tnum_is_unknown(reg->var_off)) { |
| 623 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 624 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 625 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 626 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 627 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 628 | if (reg->s32_min_value != reg->smin_value && |
| 629 | reg->s32_min_value != S32_MIN) |
| 630 | verbose(env, ",s32_min_value=%d", |
| 631 | (int)(reg->s32_min_value)); |
| 632 | if (reg->s32_max_value != reg->smax_value && |
| 633 | reg->s32_max_value != S32_MAX) |
| 634 | verbose(env, ",s32_max_value=%d", |
| 635 | (int)(reg->s32_max_value)); |
| 636 | if (reg->u32_min_value != reg->umin_value && |
| 637 | reg->u32_min_value != U32_MIN) |
| 638 | verbose(env, ",u32_min_value=%d", |
| 639 | (int)(reg->u32_min_value)); |
| 640 | if (reg->u32_max_value != reg->umax_value && |
| 641 | reg->u32_max_value != U32_MAX) |
| 642 | verbose(env, ",u32_max_value=%d", |
| 643 | (int)(reg->u32_max_value)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 644 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 645 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 646 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 647 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 648 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 649 | char types_buf[BPF_REG_SIZE + 1]; |
| 650 | bool valid = false; |
| 651 | int j; |
| 652 | |
| 653 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 654 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 655 | valid = true; |
| 656 | types_buf[j] = slot_type_char[ |
| 657 | state->stack[i].slot_type[j]]; |
| 658 | } |
| 659 | types_buf[BPF_REG_SIZE] = 0; |
| 660 | if (!valid) |
| 661 | continue; |
| 662 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 663 | print_liveness(env, state->stack[i].spilled_ptr.live); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 664 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
| 665 | reg = &state->stack[i].spilled_ptr; |
| 666 | t = reg->type; |
| 667 | verbose(env, "=%s", reg_type_str[t]); |
| 668 | if (t == SCALAR_VALUE && reg->precise) |
| 669 | verbose(env, "P"); |
| 670 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) |
| 671 | verbose(env, "%lld", reg->var_off.value + reg->off); |
| 672 | } else { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 673 | verbose(env, "=%s", types_buf); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 674 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 675 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 676 | if (state->acquired_refs && state->refs[0].id) { |
| 677 | verbose(env, " refs=%d", state->refs[0].id); |
| 678 | for (i = 1; i < state->acquired_refs; i++) |
| 679 | if (state->refs[i].id) |
| 680 | verbose(env, ",%d", state->refs[i].id); |
| 681 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 682 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 685 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 686 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ |
| 687 | const struct bpf_func_state *src) \ |
| 688 | { \ |
| 689 | if (!src->FIELD) \ |
| 690 | return 0; \ |
| 691 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ |
| 692 | /* internal bug, make state invalid to reject the program */ \ |
| 693 | memset(dst, 0, sizeof(*dst)); \ |
| 694 | return -EFAULT; \ |
| 695 | } \ |
| 696 | memcpy(dst->FIELD, src->FIELD, \ |
| 697 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ |
| 698 | return 0; \ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 699 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 700 | /* copy_reference_state() */ |
| 701 | COPY_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 702 | /* copy_stack_state() */ |
| 703 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 704 | #undef COPY_STATE_FN |
| 705 | |
| 706 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 707 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ |
| 708 | bool copy_old) \ |
| 709 | { \ |
| 710 | u32 old_size = state->COUNT; \ |
| 711 | struct bpf_##NAME##_state *new_##FIELD; \ |
| 712 | int slot = size / SIZE; \ |
| 713 | \ |
| 714 | if (size <= old_size || !size) { \ |
| 715 | if (copy_old) \ |
| 716 | return 0; \ |
| 717 | state->COUNT = slot * SIZE; \ |
| 718 | if (!size && old_size) { \ |
| 719 | kfree(state->FIELD); \ |
| 720 | state->FIELD = NULL; \ |
| 721 | } \ |
| 722 | return 0; \ |
| 723 | } \ |
| 724 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ |
| 725 | GFP_KERNEL); \ |
| 726 | if (!new_##FIELD) \ |
| 727 | return -ENOMEM; \ |
| 728 | if (copy_old) { \ |
| 729 | if (state->FIELD) \ |
| 730 | memcpy(new_##FIELD, state->FIELD, \ |
| 731 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ |
| 732 | memset(new_##FIELD + old_size / SIZE, 0, \ |
| 733 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ |
| 734 | } \ |
| 735 | state->COUNT = slot * SIZE; \ |
| 736 | kfree(state->FIELD); \ |
| 737 | state->FIELD = new_##FIELD; \ |
| 738 | return 0; \ |
| 739 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 740 | /* realloc_reference_state() */ |
| 741 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 742 | /* realloc_stack_state() */ |
| 743 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 744 | #undef REALLOC_STATE_FN |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 745 | |
| 746 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 747 | * make it consume minimal amount of memory. check_stack_write() access from |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 748 | * the program calls into realloc_func_state() to grow the stack size. |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 749 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 750 | * which realloc_stack_state() copies over. It points to previous |
| 751 | * bpf_verifier_state which is never reallocated. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 752 | */ |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 753 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
| 754 | int refs_size, bool copy_old) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 755 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 756 | int err = realloc_reference_state(state, refs_size, copy_old); |
| 757 | if (err) |
| 758 | return err; |
| 759 | return realloc_stack_state(state, stack_size, copy_old); |
| 760 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 761 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 762 | /* Acquire a pointer id from the env and update the state->refs to include |
| 763 | * this new pointer reference. |
| 764 | * On success, returns a valid pointer id to associate with the register |
| 765 | * On failure, returns a negative errno. |
| 766 | */ |
| 767 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 768 | { |
| 769 | struct bpf_func_state *state = cur_func(env); |
| 770 | int new_ofs = state->acquired_refs; |
| 771 | int id, err; |
| 772 | |
| 773 | err = realloc_reference_state(state, state->acquired_refs + 1, true); |
| 774 | if (err) |
| 775 | return err; |
| 776 | id = ++env->id_gen; |
| 777 | state->refs[new_ofs].id = id; |
| 778 | state->refs[new_ofs].insn_idx = insn_idx; |
| 779 | |
| 780 | return id; |
| 781 | } |
| 782 | |
| 783 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 784 | 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] | 785 | { |
| 786 | int i, last_idx; |
| 787 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 788 | last_idx = state->acquired_refs - 1; |
| 789 | for (i = 0; i < state->acquired_refs; i++) { |
| 790 | if (state->refs[i].id == ptr_id) { |
| 791 | if (last_idx && i != last_idx) |
| 792 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 793 | sizeof(*state->refs)); |
| 794 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 795 | state->acquired_refs--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 796 | return 0; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 797 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 798 | } |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 799 | return -EINVAL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | static int transfer_reference_state(struct bpf_func_state *dst, |
| 803 | struct bpf_func_state *src) |
| 804 | { |
| 805 | int err = realloc_reference_state(dst, src->acquired_refs, false); |
| 806 | if (err) |
| 807 | return err; |
| 808 | err = copy_reference_state(dst, src); |
| 809 | if (err) |
| 810 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 811 | return 0; |
| 812 | } |
| 813 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 814 | static void free_func_state(struct bpf_func_state *state) |
| 815 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 816 | if (!state) |
| 817 | return; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 818 | kfree(state->refs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 819 | kfree(state->stack); |
| 820 | kfree(state); |
| 821 | } |
| 822 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 823 | static void clear_jmp_history(struct bpf_verifier_state *state) |
| 824 | { |
| 825 | kfree(state->jmp_history); |
| 826 | state->jmp_history = NULL; |
| 827 | state->jmp_history_cnt = 0; |
| 828 | } |
| 829 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 830 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 831 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 832 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 833 | int i; |
| 834 | |
| 835 | for (i = 0; i <= state->curframe; i++) { |
| 836 | free_func_state(state->frame[i]); |
| 837 | state->frame[i] = NULL; |
| 838 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 839 | clear_jmp_history(state); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 840 | if (free_self) |
| 841 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | /* copy verifier state from src to dst growing dst stack space |
| 845 | * when necessary to accommodate larger src stack |
| 846 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 847 | static int copy_func_state(struct bpf_func_state *dst, |
| 848 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 849 | { |
| 850 | int err; |
| 851 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 852 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
| 853 | false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 854 | if (err) |
| 855 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 856 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 857 | err = copy_reference_state(dst, src); |
| 858 | if (err) |
| 859 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 860 | return copy_stack_state(dst, src); |
| 861 | } |
| 862 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 863 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 864 | const struct bpf_verifier_state *src) |
| 865 | { |
| 866 | struct bpf_func_state *dst; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 867 | u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 868 | int i, err; |
| 869 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 870 | if (dst_state->jmp_history_cnt < src->jmp_history_cnt) { |
| 871 | kfree(dst_state->jmp_history); |
| 872 | dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER); |
| 873 | if (!dst_state->jmp_history) |
| 874 | return -ENOMEM; |
| 875 | } |
| 876 | memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz); |
| 877 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
| 878 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 879 | /* if dst has more stack frames then src frame, free them */ |
| 880 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 881 | free_func_state(dst_state->frame[i]); |
| 882 | dst_state->frame[i] = NULL; |
| 883 | } |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 884 | dst_state->speculative = src->speculative; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 885 | dst_state->curframe = src->curframe; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 886 | dst_state->active_spin_lock = src->active_spin_lock; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 887 | dst_state->branches = src->branches; |
| 888 | dst_state->parent = src->parent; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 889 | dst_state->first_insn_idx = src->first_insn_idx; |
| 890 | dst_state->last_insn_idx = src->last_insn_idx; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 891 | for (i = 0; i <= src->curframe; i++) { |
| 892 | dst = dst_state->frame[i]; |
| 893 | if (!dst) { |
| 894 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 895 | if (!dst) |
| 896 | return -ENOMEM; |
| 897 | dst_state->frame[i] = dst; |
| 898 | } |
| 899 | err = copy_func_state(dst, src->frame[i]); |
| 900 | if (err) |
| 901 | return err; |
| 902 | } |
| 903 | return 0; |
| 904 | } |
| 905 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 906 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
| 907 | { |
| 908 | while (st) { |
| 909 | u32 br = --st->branches; |
| 910 | |
| 911 | /* WARN_ON(br > 1) technically makes sense here, |
| 912 | * but see comment in push_stack(), hence: |
| 913 | */ |
| 914 | WARN_ONCE((int)br < 0, |
| 915 | "BUG update_branch_counts:branches_to_explore=%d\n", |
| 916 | br); |
| 917 | if (br) |
| 918 | break; |
| 919 | st = st->parent; |
| 920 | } |
| 921 | } |
| 922 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 923 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 924 | int *insn_idx, bool pop_log) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 925 | { |
| 926 | struct bpf_verifier_state *cur = env->cur_state; |
| 927 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 928 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 929 | |
| 930 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 931 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 932 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 933 | if (cur) { |
| 934 | err = copy_verifier_state(cur, &head->st); |
| 935 | if (err) |
| 936 | return err; |
| 937 | } |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 938 | if (pop_log) |
| 939 | bpf_vlog_reset(&env->log, head->log_pos); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 940 | if (insn_idx) |
| 941 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 942 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 943 | *prev_insn_idx = head->prev_insn_idx; |
| 944 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 945 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 946 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 947 | env->head = elem; |
| 948 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 949 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 952 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 953 | int insn_idx, int prev_insn_idx, |
| 954 | bool speculative) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 955 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 956 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 957 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 958 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 959 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 960 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 961 | if (!elem) |
| 962 | goto err; |
| 963 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 964 | elem->insn_idx = insn_idx; |
| 965 | elem->prev_insn_idx = prev_insn_idx; |
| 966 | elem->next = env->head; |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 967 | elem->log_pos = env->log.len_used; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 968 | env->head = elem; |
| 969 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 970 | err = copy_verifier_state(&elem->st, cur); |
| 971 | if (err) |
| 972 | goto err; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 973 | elem->st.speculative |= speculative; |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 974 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
| 975 | verbose(env, "The sequence of %d jumps is too complex.\n", |
| 976 | env->stack_size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 977 | goto err; |
| 978 | } |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 979 | if (elem->st.parent) { |
| 980 | ++elem->st.parent->branches; |
| 981 | /* WARN_ON(branches > 2) technically makes sense here, |
| 982 | * but |
| 983 | * 1. speculative states will bump 'branches' for non-branch |
| 984 | * instructions |
| 985 | * 2. is_state_visited() heuristics may decide not to create |
| 986 | * a new state for a sequence of branches and all such current |
| 987 | * and cloned states will be pointing to a single parent state |
| 988 | * which might have large 'branches' count. |
| 989 | */ |
| 990 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 991 | return &elem->st; |
| 992 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 993 | free_verifier_state(env->cur_state, true); |
| 994 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 995 | /* pop all elements and return */ |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 996 | while (!pop_stack(env, NULL, NULL, false)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 997 | return NULL; |
| 998 | } |
| 999 | |
| 1000 | #define CALLER_SAVED_REGS 6 |
| 1001 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 1002 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 1003 | }; |
| 1004 | |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1005 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 1006 | struct bpf_reg_state *reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1007 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1008 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 1009 | * known to have the value @imm. |
| 1010 | */ |
| 1011 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 1012 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 1013 | /* Clear id, off, and union(map_ptr, range) */ |
| 1014 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 1015 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1016 | reg->var_off = tnum_const(imm); |
| 1017 | reg->smin_value = (s64)imm; |
| 1018 | reg->smax_value = (s64)imm; |
| 1019 | reg->umin_value = imm; |
| 1020 | reg->umax_value = imm; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1021 | |
| 1022 | reg->s32_min_value = (s32)imm; |
| 1023 | reg->s32_max_value = (s32)imm; |
| 1024 | reg->u32_min_value = (u32)imm; |
| 1025 | reg->u32_max_value = (u32)imm; |
| 1026 | } |
| 1027 | |
| 1028 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
| 1029 | { |
| 1030 | reg->var_off = tnum_const_subreg(reg->var_off, imm); |
| 1031 | reg->s32_min_value = (s32)imm; |
| 1032 | reg->s32_max_value = (s32)imm; |
| 1033 | reg->u32_min_value = (u32)imm; |
| 1034 | reg->u32_max_value = (u32)imm; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1035 | } |
| 1036 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1037 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 1038 | * used only on registers holding a pointer type. |
| 1039 | */ |
| 1040 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 1041 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1042 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1043 | } |
| 1044 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1045 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 1046 | { |
| 1047 | __mark_reg_known(reg, 0); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1048 | reg->type = SCALAR_VALUE; |
| 1049 | } |
| 1050 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1051 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 1052 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1053 | { |
| 1054 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1055 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1056 | /* Something bad happened, let's kill all regs */ |
| 1057 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1058 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1059 | return; |
| 1060 | } |
| 1061 | __mark_reg_known_zero(regs + regno); |
| 1062 | } |
| 1063 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1064 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 1065 | { |
| 1066 | return type_is_pkt_pointer(reg->type); |
| 1067 | } |
| 1068 | |
| 1069 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 1070 | { |
| 1071 | return reg_is_pkt_pointer(reg) || |
| 1072 | reg->type == PTR_TO_PACKET_END; |
| 1073 | } |
| 1074 | |
| 1075 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 1076 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 1077 | enum bpf_reg_type which) |
| 1078 | { |
| 1079 | /* The register can already have a range from prior markings. |
| 1080 | * This is fine as long as it hasn't been advanced from its |
| 1081 | * origin. |
| 1082 | */ |
| 1083 | return reg->type == which && |
| 1084 | reg->id == 0 && |
| 1085 | reg->off == 0 && |
| 1086 | tnum_equals_const(reg->var_off, 0); |
| 1087 | } |
| 1088 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1089 | /* Reset the min/max bounds of a register */ |
| 1090 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 1091 | { |
| 1092 | reg->smin_value = S64_MIN; |
| 1093 | reg->smax_value = S64_MAX; |
| 1094 | reg->umin_value = 0; |
| 1095 | reg->umax_value = U64_MAX; |
| 1096 | |
| 1097 | reg->s32_min_value = S32_MIN; |
| 1098 | reg->s32_max_value = S32_MAX; |
| 1099 | reg->u32_min_value = 0; |
| 1100 | reg->u32_max_value = U32_MAX; |
| 1101 | } |
| 1102 | |
| 1103 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) |
| 1104 | { |
| 1105 | reg->smin_value = S64_MIN; |
| 1106 | reg->smax_value = S64_MAX; |
| 1107 | reg->umin_value = 0; |
| 1108 | reg->umax_value = U64_MAX; |
| 1109 | } |
| 1110 | |
| 1111 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) |
| 1112 | { |
| 1113 | reg->s32_min_value = S32_MIN; |
| 1114 | reg->s32_max_value = S32_MAX; |
| 1115 | reg->u32_min_value = 0; |
| 1116 | reg->u32_max_value = U32_MAX; |
| 1117 | } |
| 1118 | |
| 1119 | static void __update_reg32_bounds(struct bpf_reg_state *reg) |
| 1120 | { |
| 1121 | struct tnum var32_off = tnum_subreg(reg->var_off); |
| 1122 | |
| 1123 | /* min signed is max(sign bit) | min(other bits) */ |
| 1124 | reg->s32_min_value = max_t(s32, reg->s32_min_value, |
| 1125 | var32_off.value | (var32_off.mask & S32_MIN)); |
| 1126 | /* max signed is min(sign bit) | max(other bits) */ |
| 1127 | reg->s32_max_value = min_t(s32, reg->s32_max_value, |
| 1128 | var32_off.value | (var32_off.mask & S32_MAX)); |
| 1129 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); |
| 1130 | reg->u32_max_value = min(reg->u32_max_value, |
| 1131 | (u32)(var32_off.value | var32_off.mask)); |
| 1132 | } |
| 1133 | |
| 1134 | static void __update_reg64_bounds(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1135 | { |
| 1136 | /* min signed is max(sign bit) | min(other bits) */ |
| 1137 | reg->smin_value = max_t(s64, reg->smin_value, |
| 1138 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 1139 | /* max signed is min(sign bit) | max(other bits) */ |
| 1140 | reg->smax_value = min_t(s64, reg->smax_value, |
| 1141 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 1142 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 1143 | reg->umax_value = min(reg->umax_value, |
| 1144 | reg->var_off.value | reg->var_off.mask); |
| 1145 | } |
| 1146 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1147 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 1148 | { |
| 1149 | __update_reg32_bounds(reg); |
| 1150 | __update_reg64_bounds(reg); |
| 1151 | } |
| 1152 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1153 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1154 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
| 1155 | { |
| 1156 | /* Learn sign from signed bounds. |
| 1157 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 1158 | * are the same, so combine. This works even in the negative case, e.g. |
| 1159 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 1160 | */ |
| 1161 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { |
| 1162 | reg->s32_min_value = reg->u32_min_value = |
| 1163 | max_t(u32, reg->s32_min_value, reg->u32_min_value); |
| 1164 | reg->s32_max_value = reg->u32_max_value = |
| 1165 | min_t(u32, reg->s32_max_value, reg->u32_max_value); |
| 1166 | return; |
| 1167 | } |
| 1168 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 1169 | * boundary, so we must be careful. |
| 1170 | */ |
| 1171 | if ((s32)reg->u32_max_value >= 0) { |
| 1172 | /* Positive. We can't learn anything from the smin, but smax |
| 1173 | * is positive, hence safe. |
| 1174 | */ |
| 1175 | reg->s32_min_value = reg->u32_min_value; |
| 1176 | reg->s32_max_value = reg->u32_max_value = |
| 1177 | min_t(u32, reg->s32_max_value, reg->u32_max_value); |
| 1178 | } else if ((s32)reg->u32_min_value < 0) { |
| 1179 | /* Negative. We can't learn anything from the smax, but smin |
| 1180 | * is negative, hence safe. |
| 1181 | */ |
| 1182 | reg->s32_min_value = reg->u32_min_value = |
| 1183 | max_t(u32, reg->s32_min_value, reg->u32_min_value); |
| 1184 | reg->s32_max_value = reg->u32_max_value; |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1189 | { |
| 1190 | /* Learn sign from signed bounds. |
| 1191 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 1192 | * are the same, so combine. This works even in the negative case, e.g. |
| 1193 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 1194 | */ |
| 1195 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 1196 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1197 | reg->umin_value); |
| 1198 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1199 | reg->umax_value); |
| 1200 | return; |
| 1201 | } |
| 1202 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 1203 | * boundary, so we must be careful. |
| 1204 | */ |
| 1205 | if ((s64)reg->umax_value >= 0) { |
| 1206 | /* Positive. We can't learn anything from the smin, but smax |
| 1207 | * is positive, hence safe. |
| 1208 | */ |
| 1209 | reg->smin_value = reg->umin_value; |
| 1210 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1211 | reg->umax_value); |
| 1212 | } else if ((s64)reg->umin_value < 0) { |
| 1213 | /* Negative. We can't learn anything from the smax, but smin |
| 1214 | * is negative, hence safe. |
| 1215 | */ |
| 1216 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1217 | reg->umin_value); |
| 1218 | reg->smax_value = reg->umax_value; |
| 1219 | } |
| 1220 | } |
| 1221 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1222 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 1223 | { |
| 1224 | __reg32_deduce_bounds(reg); |
| 1225 | __reg64_deduce_bounds(reg); |
| 1226 | } |
| 1227 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1228 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 1229 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 1230 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1231 | struct tnum var64_off = tnum_intersect(reg->var_off, |
| 1232 | tnum_range(reg->umin_value, |
| 1233 | reg->umax_value)); |
| 1234 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), |
| 1235 | tnum_range(reg->u32_min_value, |
| 1236 | reg->u32_max_value)); |
| 1237 | |
| 1238 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1239 | } |
| 1240 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1241 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1242 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1243 | reg->umin_value = reg->u32_min_value; |
| 1244 | reg->umax_value = reg->u32_max_value; |
| 1245 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds |
| 1246 | * but must be positive otherwise set to worse case bounds |
| 1247 | * and refine later from tnum. |
| 1248 | */ |
John Fastabend | 3a71dc3 | 2020-05-29 10:28:40 -0700 | [diff] [blame] | 1249 | if (reg->s32_min_value >= 0 && reg->s32_max_value >= 0) |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1250 | reg->smax_value = reg->s32_max_value; |
| 1251 | else |
| 1252 | reg->smax_value = U32_MAX; |
John Fastabend | 3a71dc3 | 2020-05-29 10:28:40 -0700 | [diff] [blame] | 1253 | if (reg->s32_min_value >= 0) |
| 1254 | reg->smin_value = reg->s32_min_value; |
| 1255 | else |
| 1256 | reg->smin_value = 0; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) |
| 1260 | { |
| 1261 | /* special case when 64-bit register has upper 32-bit register |
| 1262 | * zeroed. Typically happens after zext or <<32, >>32 sequence |
| 1263 | * allowing us to use 32-bit bounds directly, |
| 1264 | */ |
| 1265 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { |
| 1266 | __reg_assign_32_into_64(reg); |
| 1267 | } else { |
| 1268 | /* Otherwise the best we can do is push lower 32bit known and |
| 1269 | * unknown bits into register (var_off set from jmp logic) |
| 1270 | * then learn as much as possible from the 64-bit tnum |
| 1271 | * known and unknown bits. The previous smin/smax bounds are |
| 1272 | * invalid here because of jmp32 compare so mark them unknown |
| 1273 | * so they do not impact tnum bounds calculation. |
| 1274 | */ |
| 1275 | __mark_reg64_unbounded(reg); |
| 1276 | __update_reg_bounds(reg); |
| 1277 | } |
| 1278 | |
| 1279 | /* Intersecting with the old var_off might have improved our bounds |
| 1280 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 1281 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 1282 | */ |
| 1283 | __reg_deduce_bounds(reg); |
| 1284 | __reg_bound_offset(reg); |
| 1285 | __update_reg_bounds(reg); |
| 1286 | } |
| 1287 | |
| 1288 | static bool __reg64_bound_s32(s64 a) |
| 1289 | { |
| 1290 | if (a > S32_MIN && a < S32_MAX) |
| 1291 | return true; |
| 1292 | return false; |
| 1293 | } |
| 1294 | |
| 1295 | static bool __reg64_bound_u32(u64 a) |
| 1296 | { |
| 1297 | if (a > U32_MIN && a < U32_MAX) |
| 1298 | return true; |
| 1299 | return false; |
| 1300 | } |
| 1301 | |
| 1302 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) |
| 1303 | { |
| 1304 | __mark_reg32_unbounded(reg); |
| 1305 | |
| 1306 | if (__reg64_bound_s32(reg->smin_value)) |
| 1307 | reg->s32_min_value = (s32)reg->smin_value; |
| 1308 | if (__reg64_bound_s32(reg->smax_value)) |
| 1309 | reg->s32_max_value = (s32)reg->smax_value; |
| 1310 | if (__reg64_bound_u32(reg->umin_value)) |
| 1311 | reg->u32_min_value = (u32)reg->umin_value; |
| 1312 | if (__reg64_bound_u32(reg->umax_value)) |
| 1313 | reg->u32_max_value = (u32)reg->umax_value; |
| 1314 | |
| 1315 | /* Intersecting with the old var_off might have improved our bounds |
| 1316 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 1317 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 1318 | */ |
| 1319 | __reg_deduce_bounds(reg); |
| 1320 | __reg_bound_offset(reg); |
| 1321 | __update_reg_bounds(reg); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1322 | } |
| 1323 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1324 | /* Mark a register as having a completely unknown (scalar) value. */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1325 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
| 1326 | struct bpf_reg_state *reg) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1327 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 1328 | /* |
| 1329 | * Clear type, id, off, and union(map_ptr, range) and |
| 1330 | * padding between 'type' and union |
| 1331 | */ |
| 1332 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1333 | reg->type = SCALAR_VALUE; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1334 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1335 | reg->frameno = 0; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 1336 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1337 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1338 | } |
| 1339 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1340 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 1341 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1342 | { |
| 1343 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1344 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 1345 | /* Something bad happened, let's kill all regs except FP */ |
| 1346 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1347 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1348 | return; |
| 1349 | } |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1350 | __mark_reg_unknown(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1351 | } |
| 1352 | |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1353 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 1354 | struct bpf_reg_state *reg) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1355 | { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1356 | __mark_reg_unknown(env, reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1357 | reg->type = NOT_INIT; |
| 1358 | } |
| 1359 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1360 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 1361 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 1362 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1363 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1364 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 1365 | /* Something bad happened, let's kill all regs except FP */ |
| 1366 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1367 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1368 | return; |
| 1369 | } |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1370 | __mark_reg_not_init(env, regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 1371 | } |
| 1372 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 1373 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
| 1374 | struct bpf_reg_state *regs, u32 regno, |
| 1375 | enum bpf_reg_type reg_type, u32 btf_id) |
| 1376 | { |
| 1377 | if (reg_type == SCALAR_VALUE) { |
| 1378 | mark_reg_unknown(env, regs, regno); |
| 1379 | return; |
| 1380 | } |
| 1381 | mark_reg_known_zero(env, regs, regno); |
| 1382 | regs[regno].type = PTR_TO_BTF_ID; |
| 1383 | regs[regno].btf_id = btf_id; |
| 1384 | } |
| 1385 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1386 | #define DEF_NOT_SUBREG (0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1387 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1388 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1389 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1390 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1391 | int i; |
| 1392 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1393 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1394 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1395 | regs[i].live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1396 | regs[i].parent = NULL; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1397 | regs[i].subreg_def = DEF_NOT_SUBREG; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1398 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1399 | |
| 1400 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1401 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1402 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1403 | regs[BPF_REG_FP].frameno = state->frameno; |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 1404 | } |
| 1405 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1406 | #define BPF_MAIN_FUNC (-1) |
| 1407 | static void init_func_state(struct bpf_verifier_env *env, |
| 1408 | struct bpf_func_state *state, |
| 1409 | int callsite, int frameno, int subprogno) |
| 1410 | { |
| 1411 | state->callsite = callsite; |
| 1412 | state->frameno = frameno; |
| 1413 | state->subprogno = subprogno; |
| 1414 | init_reg_state(env, state); |
| 1415 | } |
| 1416 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1417 | enum reg_arg_type { |
| 1418 | SRC_OP, /* register is used as source operand */ |
| 1419 | DST_OP, /* register is used as destination operand */ |
| 1420 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 1421 | }; |
| 1422 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1423 | static int cmp_subprogs(const void *a, const void *b) |
| 1424 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1425 | return ((struct bpf_subprog_info *)a)->start - |
| 1426 | ((struct bpf_subprog_info *)b)->start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 1430 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1431 | struct bpf_subprog_info *p; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1432 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1433 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 1434 | sizeof(env->subprog_info[0]), cmp_subprogs); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1435 | if (!p) |
| 1436 | return -ENOENT; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1437 | return p - env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1438 | |
| 1439 | } |
| 1440 | |
| 1441 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 1442 | { |
| 1443 | int insn_cnt = env->prog->len; |
| 1444 | int ret; |
| 1445 | |
| 1446 | if (off >= insn_cnt || off < 0) { |
| 1447 | verbose(env, "call to invalid destination\n"); |
| 1448 | return -EINVAL; |
| 1449 | } |
| 1450 | ret = find_subprog(env, off); |
| 1451 | if (ret >= 0) |
| 1452 | return 0; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1453 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1454 | verbose(env, "too many subprograms\n"); |
| 1455 | return -E2BIG; |
| 1456 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1457 | env->subprog_info[env->subprog_cnt++].start = off; |
| 1458 | sort(env->subprog_info, env->subprog_cnt, |
| 1459 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1460 | return 0; |
| 1461 | } |
| 1462 | |
| 1463 | static int check_subprogs(struct bpf_verifier_env *env) |
| 1464 | { |
| 1465 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1466 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1467 | struct bpf_insn *insn = env->prog->insnsi; |
| 1468 | int insn_cnt = env->prog->len; |
| 1469 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 1470 | /* Add entry function. */ |
| 1471 | ret = add_subprog(env, 0); |
| 1472 | if (ret < 0) |
| 1473 | return ret; |
| 1474 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1475 | /* determine subprog starts. The end is one before the next starts */ |
| 1476 | for (i = 0; i < insn_cnt; i++) { |
| 1477 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1478 | continue; |
| 1479 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1480 | continue; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 1481 | if (!env->bpf_capable) { |
| 1482 | verbose(env, |
| 1483 | "function calls to other bpf functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1484 | return -EPERM; |
| 1485 | } |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1486 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 1487 | if (ret < 0) |
| 1488 | return ret; |
| 1489 | } |
| 1490 | |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1491 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 1492 | * logic. 'subprog_cnt' should not be increased. |
| 1493 | */ |
| 1494 | subprog[env->subprog_cnt].start = insn_cnt; |
| 1495 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1496 | if (env->log.level & BPF_LOG_LEVEL2) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1497 | for (i = 0; i < env->subprog_cnt; i++) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1498 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1499 | |
| 1500 | /* now check that all jumps are within the same subprog */ |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1501 | subprog_start = subprog[cur_subprog].start; |
| 1502 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1503 | for (i = 0; i < insn_cnt; i++) { |
| 1504 | u8 code = insn[i].code; |
| 1505 | |
Maciej Fijalkowski | 7f6e431 | 2020-09-16 23:10:07 +0200 | [diff] [blame] | 1506 | if (code == (BPF_JMP | BPF_CALL) && |
| 1507 | insn[i].imm == BPF_FUNC_tail_call && |
| 1508 | insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1509 | subprog[cur_subprog].has_tail_call = true; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 1510 | if (BPF_CLASS(code) == BPF_LD && |
| 1511 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) |
| 1512 | subprog[cur_subprog].has_ld_abs = true; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 1513 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1514 | goto next; |
| 1515 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 1516 | goto next; |
| 1517 | off = i + insn[i].off + 1; |
| 1518 | if (off < subprog_start || off >= subprog_end) { |
| 1519 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 1520 | return -EINVAL; |
| 1521 | } |
| 1522 | next: |
| 1523 | if (i == subprog_end - 1) { |
| 1524 | /* to avoid fall-through from one subprog into another |
| 1525 | * the last insn of the subprog should be either exit |
| 1526 | * or unconditional jump back |
| 1527 | */ |
| 1528 | if (code != (BPF_JMP | BPF_EXIT) && |
| 1529 | code != (BPF_JMP | BPF_JA)) { |
| 1530 | verbose(env, "last insn is not an exit or jmp\n"); |
| 1531 | return -EINVAL; |
| 1532 | } |
| 1533 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1534 | cur_subprog++; |
| 1535 | if (cur_subprog < env->subprog_cnt) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1536 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1537 | } |
| 1538 | } |
| 1539 | return 0; |
| 1540 | } |
| 1541 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1542 | /* Parentage chain of this register (or stack slot) should take care of all |
| 1543 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 1544 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1545 | static int mark_reg_read(struct bpf_verifier_env *env, |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1546 | const struct bpf_reg_state *state, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1547 | struct bpf_reg_state *parent, u8 flag) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1548 | { |
| 1549 | bool writes = parent == state->parent; /* Observe write marks */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1550 | int cnt = 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1551 | |
| 1552 | while (parent) { |
| 1553 | /* if read wasn't screened by an earlier write ... */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1554 | if (writes && state->live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1555 | break; |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 1556 | if (parent->live & REG_LIVE_DONE) { |
| 1557 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", |
| 1558 | reg_type_str[parent->type], |
| 1559 | parent->var_off.value, parent->off); |
| 1560 | return -EFAULT; |
| 1561 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1562 | /* The first condition is more likely to be true than the |
| 1563 | * second, checked it first. |
| 1564 | */ |
| 1565 | if ((parent->live & REG_LIVE_READ) == flag || |
| 1566 | parent->live & REG_LIVE_READ64) |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 1567 | /* The parentage chain never changes and |
| 1568 | * this parent was already marked as LIVE_READ. |
| 1569 | * There is no need to keep walking the chain again and |
| 1570 | * keep re-marking all parents as LIVE_READ. |
| 1571 | * This case happens when the same register is read |
| 1572 | * multiple times without writes into it in-between. |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1573 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
| 1574 | * then no need to set the weak REG_LIVE_READ32. |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 1575 | */ |
| 1576 | break; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1577 | /* ... then we depend on parent's value */ |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1578 | parent->live |= flag; |
| 1579 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ |
| 1580 | if (flag == REG_LIVE_READ64) |
| 1581 | parent->live &= ~REG_LIVE_READ32; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1582 | state = parent; |
| 1583 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1584 | writes = true; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1585 | cnt++; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1586 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1587 | |
| 1588 | if (env->longest_mark_read_walk < cnt) |
| 1589 | env->longest_mark_read_walk = cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1590 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1591 | } |
| 1592 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1593 | /* This function is supposed to be used by the following 32-bit optimization |
| 1594 | * code only. It returns TRUE if the source or destination register operates |
| 1595 | * on 64-bit, otherwise return FALSE. |
| 1596 | */ |
| 1597 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 1598 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) |
| 1599 | { |
| 1600 | u8 code, class, op; |
| 1601 | |
| 1602 | code = insn->code; |
| 1603 | class = BPF_CLASS(code); |
| 1604 | op = BPF_OP(code); |
| 1605 | if (class == BPF_JMP) { |
| 1606 | /* BPF_EXIT for "main" will reach here. Return TRUE |
| 1607 | * conservatively. |
| 1608 | */ |
| 1609 | if (op == BPF_EXIT) |
| 1610 | return true; |
| 1611 | if (op == BPF_CALL) { |
| 1612 | /* BPF to BPF call will reach here because of marking |
| 1613 | * caller saved clobber with DST_OP_NO_MARK for which we |
| 1614 | * don't care the register def because they are anyway |
| 1615 | * marked as NOT_INIT already. |
| 1616 | */ |
| 1617 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1618 | return false; |
| 1619 | /* Helper call will reach here because of arg type |
| 1620 | * check, conservatively return TRUE. |
| 1621 | */ |
| 1622 | if (t == SRC_OP) |
| 1623 | return true; |
| 1624 | |
| 1625 | return false; |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | if (class == BPF_ALU64 || class == BPF_JMP || |
| 1630 | /* BPF_END always use BPF_ALU class. */ |
| 1631 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) |
| 1632 | return true; |
| 1633 | |
| 1634 | if (class == BPF_ALU || class == BPF_JMP32) |
| 1635 | return false; |
| 1636 | |
| 1637 | if (class == BPF_LDX) { |
| 1638 | if (t != SRC_OP) |
| 1639 | return BPF_SIZE(code) == BPF_DW; |
| 1640 | /* LDX source must be ptr. */ |
| 1641 | return true; |
| 1642 | } |
| 1643 | |
| 1644 | if (class == BPF_STX) { |
| 1645 | if (reg->type != SCALAR_VALUE) |
| 1646 | return true; |
| 1647 | return BPF_SIZE(code) == BPF_DW; |
| 1648 | } |
| 1649 | |
| 1650 | if (class == BPF_LD) { |
| 1651 | u8 mode = BPF_MODE(code); |
| 1652 | |
| 1653 | /* LD_IMM64 */ |
| 1654 | if (mode == BPF_IMM) |
| 1655 | return true; |
| 1656 | |
| 1657 | /* Both LD_IND and LD_ABS return 32-bit data. */ |
| 1658 | if (t != SRC_OP) |
| 1659 | return false; |
| 1660 | |
| 1661 | /* Implicit ctx ptr. */ |
| 1662 | if (regno == BPF_REG_6) |
| 1663 | return true; |
| 1664 | |
| 1665 | /* Explicit source could be any width. */ |
| 1666 | return true; |
| 1667 | } |
| 1668 | |
| 1669 | if (class == BPF_ST) |
| 1670 | /* The only source register for BPF_ST is a ptr. */ |
| 1671 | return true; |
| 1672 | |
| 1673 | /* Conservatively return true at default. */ |
| 1674 | return true; |
| 1675 | } |
| 1676 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 1677 | /* Return TRUE if INSN doesn't have explicit value define. */ |
| 1678 | static bool insn_no_def(struct bpf_insn *insn) |
| 1679 | { |
| 1680 | u8 class = BPF_CLASS(insn->code); |
| 1681 | |
| 1682 | return (class == BPF_JMP || class == BPF_JMP32 || |
| 1683 | class == BPF_STX || class == BPF_ST); |
| 1684 | } |
| 1685 | |
| 1686 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ |
| 1687 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 1688 | { |
| 1689 | if (insn_no_def(insn)) |
| 1690 | return false; |
| 1691 | |
| 1692 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); |
| 1693 | } |
| 1694 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1695 | static void mark_insn_zext(struct bpf_verifier_env *env, |
| 1696 | struct bpf_reg_state *reg) |
| 1697 | { |
| 1698 | s32 def_idx = reg->subreg_def; |
| 1699 | |
| 1700 | if (def_idx == DEF_NOT_SUBREG) |
| 1701 | return; |
| 1702 | |
| 1703 | env->insn_aux_data[def_idx - 1].zext_dst = true; |
| 1704 | /* The dst will be zero extended, so won't be sub-register anymore. */ |
| 1705 | reg->subreg_def = DEF_NOT_SUBREG; |
| 1706 | } |
| 1707 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1708 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1709 | enum reg_arg_type t) |
| 1710 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1711 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1712 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1713 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1714 | struct bpf_reg_state *reg, *regs = state->regs; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1715 | bool rw64; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1716 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1717 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1718 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1719 | return -EINVAL; |
| 1720 | } |
| 1721 | |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1722 | reg = ®s[regno]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1723 | rw64 = is_reg64(env, insn, regno, reg, t); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1724 | if (t == SRC_OP) { |
| 1725 | /* check whether register used as source operand can be read */ |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1726 | if (reg->type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1727 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1728 | return -EACCES; |
| 1729 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1730 | /* 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] | 1731 | if (regno == BPF_REG_FP) |
| 1732 | return 0; |
| 1733 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1734 | if (rw64) |
| 1735 | mark_insn_zext(env, reg); |
| 1736 | |
| 1737 | return mark_reg_read(env, reg, reg->parent, |
| 1738 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1739 | } else { |
| 1740 | /* check whether register used as dest operand can be written to */ |
| 1741 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1742 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1743 | return -EACCES; |
| 1744 | } |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1745 | reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1746 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1747 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1748 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1749 | } |
| 1750 | return 0; |
| 1751 | } |
| 1752 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1753 | /* for any branch, call, exit record the history of jmps in the given state */ |
| 1754 | static int push_jmp_history(struct bpf_verifier_env *env, |
| 1755 | struct bpf_verifier_state *cur) |
| 1756 | { |
| 1757 | u32 cnt = cur->jmp_history_cnt; |
| 1758 | struct bpf_idx_pair *p; |
| 1759 | |
| 1760 | cnt++; |
| 1761 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); |
| 1762 | if (!p) |
| 1763 | return -ENOMEM; |
| 1764 | p[cnt - 1].idx = env->insn_idx; |
| 1765 | p[cnt - 1].prev_idx = env->prev_insn_idx; |
| 1766 | cur->jmp_history = p; |
| 1767 | cur->jmp_history_cnt = cnt; |
| 1768 | return 0; |
| 1769 | } |
| 1770 | |
| 1771 | /* Backtrack one insn at a time. If idx is not at the top of recorded |
| 1772 | * history then previous instruction came from straight line execution. |
| 1773 | */ |
| 1774 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, |
| 1775 | u32 *history) |
| 1776 | { |
| 1777 | u32 cnt = *history; |
| 1778 | |
| 1779 | if (cnt && st->jmp_history[cnt - 1].idx == i) { |
| 1780 | i = st->jmp_history[cnt - 1].prev_idx; |
| 1781 | (*history)--; |
| 1782 | } else { |
| 1783 | i--; |
| 1784 | } |
| 1785 | return i; |
| 1786 | } |
| 1787 | |
| 1788 | /* For given verifier state backtrack_insn() is called from the last insn to |
| 1789 | * the first insn. Its purpose is to compute a bitmask of registers and |
| 1790 | * stack slots that needs precision in the parent verifier state. |
| 1791 | */ |
| 1792 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, |
| 1793 | u32 *reg_mask, u64 *stack_mask) |
| 1794 | { |
| 1795 | const struct bpf_insn_cbs cbs = { |
| 1796 | .cb_print = verbose, |
| 1797 | .private_data = env, |
| 1798 | }; |
| 1799 | struct bpf_insn *insn = env->prog->insnsi + idx; |
| 1800 | u8 class = BPF_CLASS(insn->code); |
| 1801 | u8 opcode = BPF_OP(insn->code); |
| 1802 | u8 mode = BPF_MODE(insn->code); |
| 1803 | u32 dreg = 1u << insn->dst_reg; |
| 1804 | u32 sreg = 1u << insn->src_reg; |
| 1805 | u32 spi; |
| 1806 | |
| 1807 | if (insn->code == 0) |
| 1808 | return 0; |
| 1809 | if (env->log.level & BPF_LOG_LEVEL) { |
| 1810 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
| 1811 | verbose(env, "%d: ", idx); |
| 1812 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
| 1813 | } |
| 1814 | |
| 1815 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 1816 | if (!(*reg_mask & dreg)) |
| 1817 | return 0; |
| 1818 | if (opcode == BPF_MOV) { |
| 1819 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1820 | /* dreg = sreg |
| 1821 | * dreg needs precision after this insn |
| 1822 | * sreg needs precision before this insn |
| 1823 | */ |
| 1824 | *reg_mask &= ~dreg; |
| 1825 | *reg_mask |= sreg; |
| 1826 | } else { |
| 1827 | /* dreg = K |
| 1828 | * dreg needs precision after this insn. |
| 1829 | * Corresponding register is already marked |
| 1830 | * as precise=true in this verifier state. |
| 1831 | * No further markings in parent are necessary |
| 1832 | */ |
| 1833 | *reg_mask &= ~dreg; |
| 1834 | } |
| 1835 | } else { |
| 1836 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1837 | /* dreg += sreg |
| 1838 | * both dreg and sreg need precision |
| 1839 | * before this insn |
| 1840 | */ |
| 1841 | *reg_mask |= sreg; |
| 1842 | } /* else dreg += K |
| 1843 | * dreg still needs precision before this insn |
| 1844 | */ |
| 1845 | } |
| 1846 | } else if (class == BPF_LDX) { |
| 1847 | if (!(*reg_mask & dreg)) |
| 1848 | return 0; |
| 1849 | *reg_mask &= ~dreg; |
| 1850 | |
| 1851 | /* scalars can only be spilled into stack w/o losing precision. |
| 1852 | * Load from any other memory can be zero extended. |
| 1853 | * The desire to keep that precision is already indicated |
| 1854 | * by 'precise' mark in corresponding register of this state. |
| 1855 | * No further tracking necessary. |
| 1856 | */ |
| 1857 | if (insn->src_reg != BPF_REG_FP) |
| 1858 | return 0; |
| 1859 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 1860 | return 0; |
| 1861 | |
| 1862 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. |
| 1863 | * that [fp - off] slot contains scalar that needs to be |
| 1864 | * tracked with precision |
| 1865 | */ |
| 1866 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 1867 | if (spi >= 64) { |
| 1868 | verbose(env, "BUG spi %d\n", spi); |
| 1869 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1870 | return -EFAULT; |
| 1871 | } |
| 1872 | *stack_mask |= 1ull << spi; |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 1873 | } else if (class == BPF_STX || class == BPF_ST) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1874 | if (*reg_mask & dreg) |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 1875 | /* stx & st shouldn't be using _scalar_ dst_reg |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1876 | * to access memory. It means backtracking |
| 1877 | * encountered a case of pointer subtraction. |
| 1878 | */ |
| 1879 | return -ENOTSUPP; |
| 1880 | /* scalars can only be spilled into stack */ |
| 1881 | if (insn->dst_reg != BPF_REG_FP) |
| 1882 | return 0; |
| 1883 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 1884 | return 0; |
| 1885 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 1886 | if (spi >= 64) { |
| 1887 | verbose(env, "BUG spi %d\n", spi); |
| 1888 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1889 | return -EFAULT; |
| 1890 | } |
| 1891 | if (!(*stack_mask & (1ull << spi))) |
| 1892 | return 0; |
| 1893 | *stack_mask &= ~(1ull << spi); |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 1894 | if (class == BPF_STX) |
| 1895 | *reg_mask |= sreg; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1896 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
| 1897 | if (opcode == BPF_CALL) { |
| 1898 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1899 | return -ENOTSUPP; |
| 1900 | /* regular helper call sets R0 */ |
| 1901 | *reg_mask &= ~1; |
| 1902 | if (*reg_mask & 0x3f) { |
| 1903 | /* if backtracing was looking for registers R1-R5 |
| 1904 | * they should have been found already. |
| 1905 | */ |
| 1906 | verbose(env, "BUG regs %x\n", *reg_mask); |
| 1907 | WARN_ONCE(1, "verifier backtracking bug"); |
| 1908 | return -EFAULT; |
| 1909 | } |
| 1910 | } else if (opcode == BPF_EXIT) { |
| 1911 | return -ENOTSUPP; |
| 1912 | } |
| 1913 | } else if (class == BPF_LD) { |
| 1914 | if (!(*reg_mask & dreg)) |
| 1915 | return 0; |
| 1916 | *reg_mask &= ~dreg; |
| 1917 | /* It's ld_imm64 or ld_abs or ld_ind. |
| 1918 | * For ld_imm64 no further tracking of precision |
| 1919 | * into parent is necessary |
| 1920 | */ |
| 1921 | if (mode == BPF_IND || mode == BPF_ABS) |
| 1922 | /* to be analyzed */ |
| 1923 | return -ENOTSUPP; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1924 | } |
| 1925 | return 0; |
| 1926 | } |
| 1927 | |
| 1928 | /* the scalar precision tracking algorithm: |
| 1929 | * . at the start all registers have precise=false. |
| 1930 | * . scalar ranges are tracked as normal through alu and jmp insns. |
| 1931 | * . once precise value of the scalar register is used in: |
| 1932 | * . ptr + scalar alu |
| 1933 | * . if (scalar cond K|scalar) |
| 1934 | * . helper_call(.., scalar, ...) where ARG_CONST is expected |
| 1935 | * backtrack through the verifier states and mark all registers and |
| 1936 | * stack slots with spilled constants that these scalar regisers |
| 1937 | * should be precise. |
| 1938 | * . during state pruning two registers (or spilled stack slots) |
| 1939 | * are equivalent if both are not precise. |
| 1940 | * |
| 1941 | * Note the verifier cannot simply walk register parentage chain, |
| 1942 | * since many different registers and stack slots could have been |
| 1943 | * used to compute single precise scalar. |
| 1944 | * |
| 1945 | * The approach of starting with precise=true for all registers and then |
| 1946 | * backtrack to mark a register as not precise when the verifier detects |
| 1947 | * that program doesn't care about specific value (e.g., when helper |
| 1948 | * takes register as ARG_ANYTHING parameter) is not safe. |
| 1949 | * |
| 1950 | * It's ok to walk single parentage chain of the verifier states. |
| 1951 | * It's possible that this backtracking will go all the way till 1st insn. |
| 1952 | * All other branches will be explored for needing precision later. |
| 1953 | * |
| 1954 | * The backtracking needs to deal with cases like: |
| 1955 | * 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) |
| 1956 | * r9 -= r8 |
| 1957 | * r5 = r9 |
| 1958 | * if r5 > 0x79f goto pc+7 |
| 1959 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) |
| 1960 | * r5 += 1 |
| 1961 | * ... |
| 1962 | * call bpf_perf_event_output#25 |
| 1963 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO |
| 1964 | * |
| 1965 | * and this case: |
| 1966 | * r6 = 1 |
| 1967 | * call foo // uses callee's r6 inside to compute r0 |
| 1968 | * r0 += r6 |
| 1969 | * if r0 == 0 goto |
| 1970 | * |
| 1971 | * to track above reg_mask/stack_mask needs to be independent for each frame. |
| 1972 | * |
| 1973 | * Also if parent's curframe > frame where backtracking started, |
| 1974 | * the verifier need to mark registers in both frames, otherwise callees |
| 1975 | * may incorrectly prune callers. This is similar to |
| 1976 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") |
| 1977 | * |
| 1978 | * For now backtracking falls back into conservative marking. |
| 1979 | */ |
| 1980 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, |
| 1981 | struct bpf_verifier_state *st) |
| 1982 | { |
| 1983 | struct bpf_func_state *func; |
| 1984 | struct bpf_reg_state *reg; |
| 1985 | int i, j; |
| 1986 | |
| 1987 | /* big hammer: mark all scalars precise in this path. |
| 1988 | * pop_stack may still get !precise scalars. |
| 1989 | */ |
| 1990 | for (; st; st = st->parent) |
| 1991 | for (i = 0; i <= st->curframe; i++) { |
| 1992 | func = st->frame[i]; |
| 1993 | for (j = 0; j < BPF_REG_FP; j++) { |
| 1994 | reg = &func->regs[j]; |
| 1995 | if (reg->type != SCALAR_VALUE) |
| 1996 | continue; |
| 1997 | reg->precise = true; |
| 1998 | } |
| 1999 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { |
| 2000 | if (func->stack[j].slot_type[0] != STACK_SPILL) |
| 2001 | continue; |
| 2002 | reg = &func->stack[j].spilled_ptr; |
| 2003 | if (reg->type != SCALAR_VALUE) |
| 2004 | continue; |
| 2005 | reg->precise = true; |
| 2006 | } |
| 2007 | } |
| 2008 | } |
| 2009 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2010 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
| 2011 | int spi) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2012 | { |
| 2013 | struct bpf_verifier_state *st = env->cur_state; |
| 2014 | int first_idx = st->first_insn_idx; |
| 2015 | int last_idx = env->insn_idx; |
| 2016 | struct bpf_func_state *func; |
| 2017 | struct bpf_reg_state *reg; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2018 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
| 2019 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2020 | bool skip_first = true; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2021 | bool new_marks = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2022 | int i, err; |
| 2023 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2024 | if (!env->bpf_capable) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2025 | return 0; |
| 2026 | |
| 2027 | func = st->frame[st->curframe]; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2028 | if (regno >= 0) { |
| 2029 | reg = &func->regs[regno]; |
| 2030 | if (reg->type != SCALAR_VALUE) { |
| 2031 | WARN_ONCE(1, "backtracing misuse"); |
| 2032 | return -EFAULT; |
| 2033 | } |
| 2034 | if (!reg->precise) |
| 2035 | new_marks = true; |
| 2036 | else |
| 2037 | reg_mask = 0; |
| 2038 | reg->precise = true; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2039 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2040 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2041 | while (spi >= 0) { |
| 2042 | if (func->stack[spi].slot_type[0] != STACK_SPILL) { |
| 2043 | stack_mask = 0; |
| 2044 | break; |
| 2045 | } |
| 2046 | reg = &func->stack[spi].spilled_ptr; |
| 2047 | if (reg->type != SCALAR_VALUE) { |
| 2048 | stack_mask = 0; |
| 2049 | break; |
| 2050 | } |
| 2051 | if (!reg->precise) |
| 2052 | new_marks = true; |
| 2053 | else |
| 2054 | stack_mask = 0; |
| 2055 | reg->precise = true; |
| 2056 | break; |
| 2057 | } |
| 2058 | |
| 2059 | if (!new_marks) |
| 2060 | return 0; |
| 2061 | if (!reg_mask && !stack_mask) |
| 2062 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2063 | for (;;) { |
| 2064 | DECLARE_BITMAP(mask, 64); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2065 | u32 history = st->jmp_history_cnt; |
| 2066 | |
| 2067 | if (env->log.level & BPF_LOG_LEVEL) |
| 2068 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
| 2069 | for (i = last_idx;;) { |
| 2070 | if (skip_first) { |
| 2071 | err = 0; |
| 2072 | skip_first = false; |
| 2073 | } else { |
| 2074 | err = backtrack_insn(env, i, ®_mask, &stack_mask); |
| 2075 | } |
| 2076 | if (err == -ENOTSUPP) { |
| 2077 | mark_all_scalars_precise(env, st); |
| 2078 | return 0; |
| 2079 | } else if (err) { |
| 2080 | return err; |
| 2081 | } |
| 2082 | if (!reg_mask && !stack_mask) |
| 2083 | /* Found assignment(s) into tracked register in this state. |
| 2084 | * Since this state is already marked, just return. |
| 2085 | * Nothing to be tracked further in the parent state. |
| 2086 | */ |
| 2087 | return 0; |
| 2088 | if (i == first_idx) |
| 2089 | break; |
| 2090 | i = get_prev_insn_idx(st, i, &history); |
| 2091 | if (i >= env->prog->len) { |
| 2092 | /* This can happen if backtracking reached insn 0 |
| 2093 | * and there are still reg_mask or stack_mask |
| 2094 | * to backtrack. |
| 2095 | * It means the backtracking missed the spot where |
| 2096 | * particular register was initialized with a constant. |
| 2097 | */ |
| 2098 | verbose(env, "BUG backtracking idx %d\n", i); |
| 2099 | WARN_ONCE(1, "verifier backtracking bug"); |
| 2100 | return -EFAULT; |
| 2101 | } |
| 2102 | } |
| 2103 | st = st->parent; |
| 2104 | if (!st) |
| 2105 | break; |
| 2106 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2107 | new_marks = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2108 | func = st->frame[st->curframe]; |
| 2109 | bitmap_from_u64(mask, reg_mask); |
| 2110 | for_each_set_bit(i, mask, 32) { |
| 2111 | reg = &func->regs[i]; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2112 | if (reg->type != SCALAR_VALUE) { |
| 2113 | reg_mask &= ~(1u << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2114 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2115 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2116 | if (!reg->precise) |
| 2117 | new_marks = true; |
| 2118 | reg->precise = true; |
| 2119 | } |
| 2120 | |
| 2121 | bitmap_from_u64(mask, stack_mask); |
| 2122 | for_each_set_bit(i, mask, 64) { |
| 2123 | if (i >= func->allocated_stack / BPF_REG_SIZE) { |
Alexei Starovoitov | 2339cd6 | 2019-09-03 15:16:17 -0700 | [diff] [blame] | 2124 | /* the sequence of instructions: |
| 2125 | * 2: (bf) r3 = r10 |
| 2126 | * 3: (7b) *(u64 *)(r3 -8) = r0 |
| 2127 | * 4: (79) r4 = *(u64 *)(r10 -8) |
| 2128 | * doesn't contain jmps. It's backtracked |
| 2129 | * as a single block. |
| 2130 | * During backtracking insn 3 is not recognized as |
| 2131 | * stack access, so at the end of backtracking |
| 2132 | * stack slot fp-8 is still marked in stack_mask. |
| 2133 | * However the parent state may not have accessed |
| 2134 | * fp-8 and it's "unallocated" stack space. |
| 2135 | * In such case fallback to conservative. |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2136 | */ |
Alexei Starovoitov | 2339cd6 | 2019-09-03 15:16:17 -0700 | [diff] [blame] | 2137 | mark_all_scalars_precise(env, st); |
| 2138 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2139 | } |
| 2140 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2141 | if (func->stack[i].slot_type[0] != STACK_SPILL) { |
| 2142 | stack_mask &= ~(1ull << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2143 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2144 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2145 | reg = &func->stack[i].spilled_ptr; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2146 | if (reg->type != SCALAR_VALUE) { |
| 2147 | stack_mask &= ~(1ull << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2148 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2149 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2150 | if (!reg->precise) |
| 2151 | new_marks = true; |
| 2152 | reg->precise = true; |
| 2153 | } |
| 2154 | if (env->log.level & BPF_LOG_LEVEL) { |
| 2155 | print_verifier_state(env, func); |
| 2156 | verbose(env, "parent %s regs=%x stack=%llx marks\n", |
| 2157 | new_marks ? "didn't have" : "already had", |
| 2158 | reg_mask, stack_mask); |
| 2159 | } |
| 2160 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2161 | if (!reg_mask && !stack_mask) |
| 2162 | break; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2163 | if (!new_marks) |
| 2164 | break; |
| 2165 | |
| 2166 | last_idx = st->last_insn_idx; |
| 2167 | first_idx = st->first_insn_idx; |
| 2168 | } |
| 2169 | return 0; |
| 2170 | } |
| 2171 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2172 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
| 2173 | { |
| 2174 | return __mark_chain_precision(env, regno, -1); |
| 2175 | } |
| 2176 | |
| 2177 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) |
| 2178 | { |
| 2179 | return __mark_chain_precision(env, -1, spi); |
| 2180 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2181 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2182 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 2183 | { |
| 2184 | switch (type) { |
| 2185 | case PTR_TO_MAP_VALUE: |
| 2186 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 2187 | case PTR_TO_STACK: |
| 2188 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2189 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2190 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2191 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2192 | case PTR_TO_FLOW_KEYS: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2193 | case CONST_PTR_TO_MAP: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2194 | case PTR_TO_SOCKET: |
| 2195 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2196 | case PTR_TO_SOCK_COMMON: |
| 2197 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2198 | case PTR_TO_TCP_SOCK: |
| 2199 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2200 | case PTR_TO_XDP_SOCK: |
Martin KaFai Lau | 65726b5 | 2020-01-08 16:34:54 -0800 | [diff] [blame] | 2201 | case PTR_TO_BTF_ID: |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 2202 | case PTR_TO_BTF_ID_OR_NULL: |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 2203 | case PTR_TO_RDONLY_BUF: |
| 2204 | case PTR_TO_RDONLY_BUF_OR_NULL: |
| 2205 | case PTR_TO_RDWR_BUF: |
| 2206 | case PTR_TO_RDWR_BUF_OR_NULL: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2207 | return true; |
| 2208 | default: |
| 2209 | return false; |
| 2210 | } |
| 2211 | } |
| 2212 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2213 | /* Does this register contain a constant zero? */ |
| 2214 | static bool register_is_null(struct bpf_reg_state *reg) |
| 2215 | { |
| 2216 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 2217 | } |
| 2218 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2219 | static bool register_is_const(struct bpf_reg_state *reg) |
| 2220 | { |
| 2221 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); |
| 2222 | } |
| 2223 | |
Jann Horn | 6e7e63c | 2020-04-17 02:00:06 +0200 | [diff] [blame] | 2224 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 2225 | const struct bpf_reg_state *reg) |
| 2226 | { |
| 2227 | if (allow_ptr_leaks) |
| 2228 | return false; |
| 2229 | |
| 2230 | return reg->type != SCALAR_VALUE; |
| 2231 | } |
| 2232 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2233 | static void save_register_state(struct bpf_func_state *state, |
| 2234 | int spi, struct bpf_reg_state *reg) |
| 2235 | { |
| 2236 | int i; |
| 2237 | |
| 2238 | state->stack[spi].spilled_ptr = *reg; |
| 2239 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 2240 | |
| 2241 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 2242 | state->stack[spi].slot_type[i] = STACK_SPILL; |
| 2243 | } |
| 2244 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2245 | /* check_stack_read/write functions track spill/fill of registers, |
| 2246 | * stack boundary and alignment are checked in check_mem_access() |
| 2247 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2248 | static int check_stack_write(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2249 | struct bpf_func_state *state, /* func where register points to */ |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2250 | int off, int size, int value_regno, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2251 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2252 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2253 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2254 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2255 | struct bpf_reg_state *reg = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2256 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2257 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2258 | state->acquired_refs, true); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2259 | if (err) |
| 2260 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2261 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 2262 | * so it's aligned access and [off, off + size) are within stack limits |
| 2263 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2264 | if (!env->allow_ptr_leaks && |
| 2265 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 2266 | size != BPF_REG_SIZE) { |
| 2267 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 2268 | return -EACCES; |
| 2269 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2270 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2271 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2272 | if (value_regno >= 0) |
| 2273 | reg = &cur->regs[value_regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2274 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2275 | if (reg && size == BPF_REG_SIZE && register_is_const(reg) && |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2276 | !register_is_null(reg) && env->bpf_capable) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2277 | if (dst_reg != BPF_REG_FP) { |
| 2278 | /* The backtracking logic can only recognize explicit |
| 2279 | * stack slot address like [fp - 8]. Other spill of |
| 2280 | * scalar via different register has to be conervative. |
| 2281 | * Backtrack from here and mark all registers as precise |
| 2282 | * that contributed into 'reg' being a constant. |
| 2283 | */ |
| 2284 | err = mark_chain_precision(env, value_regno); |
| 2285 | if (err) |
| 2286 | return err; |
| 2287 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2288 | save_register_state(state, spi, reg); |
| 2289 | } else if (reg && is_spillable_regtype(reg->type)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2290 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2291 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2292 | verbose_linfo(env, insn_idx, "; "); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2293 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2294 | return -EACCES; |
| 2295 | } |
| 2296 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2297 | if (state != cur && reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2298 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 2299 | return -EINVAL; |
| 2300 | } |
| 2301 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2302 | if (!env->bypass_spec_v4) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2303 | bool sanitize = false; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2304 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2305 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 2306 | register_is_const(&state->stack[spi].spilled_ptr)) |
| 2307 | sanitize = true; |
| 2308 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 2309 | if (state->stack[spi].slot_type[i] == STACK_MISC) { |
| 2310 | sanitize = true; |
| 2311 | break; |
| 2312 | } |
| 2313 | if (sanitize) { |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2314 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
| 2315 | int soff = (-spi - 1) * BPF_REG_SIZE; |
| 2316 | |
| 2317 | /* detected reuse of integer stack slot with a pointer |
| 2318 | * which means either llvm is reusing stack slot or |
| 2319 | * an attacker is trying to exploit CVE-2018-3639 |
| 2320 | * (speculative store bypass) |
| 2321 | * Have to sanitize that slot with preemptive |
| 2322 | * store of zero. |
| 2323 | */ |
| 2324 | if (*poff && *poff != soff) { |
| 2325 | /* disallow programs where single insn stores |
| 2326 | * into two different stack slots, since verifier |
| 2327 | * cannot sanitize them |
| 2328 | */ |
| 2329 | verbose(env, |
| 2330 | "insn %d cannot access two stack slots fp%d and fp%d", |
| 2331 | insn_idx, *poff, soff); |
| 2332 | return -EINVAL; |
| 2333 | } |
| 2334 | *poff = soff; |
| 2335 | } |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2336 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2337 | save_register_state(state, spi, reg); |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2338 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2339 | u8 type = STACK_MISC; |
| 2340 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2341 | /* regular write of data into stack destroys any spilled ptr */ |
| 2342 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2343 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
| 2344 | if (state->stack[spi].slot_type[0] == STACK_SPILL) |
| 2345 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 2346 | state->stack[spi].slot_type[i] = STACK_MISC; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2347 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2348 | /* only mark the slot as written if all 8 bytes were written |
| 2349 | * otherwise read propagation may incorrectly stop too soon |
| 2350 | * when stack slots are partially written. |
| 2351 | * This heuristic means that read propagation will be |
| 2352 | * conservative, since it will add reg_live_read marks |
| 2353 | * to stack slots all the way to first state when programs |
| 2354 | * writes+reads less than 8 bytes |
| 2355 | */ |
| 2356 | if (size == BPF_REG_SIZE) |
| 2357 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 2358 | |
| 2359 | /* when we zero initialize stack slots mark them as such */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2360 | if (reg && register_is_null(reg)) { |
| 2361 | /* backtracking doesn't work for STACK_ZERO yet. */ |
| 2362 | err = mark_chain_precision(env, value_regno); |
| 2363 | if (err) |
| 2364 | return err; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2365 | type = STACK_ZERO; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2366 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2367 | |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2368 | /* Mark slots affected by this stack write. */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2369 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2370 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2371 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2372 | } |
| 2373 | return 0; |
| 2374 | } |
| 2375 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2376 | static int check_stack_read(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2377 | struct bpf_func_state *reg_state /* func where register points to */, |
| 2378 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2379 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2380 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2381 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2382 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2383 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2384 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2385 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2386 | if (reg_state->allocated_stack <= slot) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2387 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 2388 | off, size); |
| 2389 | return -EACCES; |
| 2390 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2391 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2392 | reg = ®_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2393 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2394 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2395 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2396 | if (reg->type != SCALAR_VALUE) { |
| 2397 | verbose_linfo(env, env->insn_idx, "; "); |
| 2398 | verbose(env, "invalid size of register fill\n"); |
| 2399 | return -EACCES; |
| 2400 | } |
| 2401 | if (value_regno >= 0) { |
| 2402 | mark_reg_unknown(env, state->regs, value_regno); |
| 2403 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 2404 | } |
| 2405 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
| 2406 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2407 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2408 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2409 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2410 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2411 | return -EACCES; |
| 2412 | } |
| 2413 | } |
| 2414 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2415 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2416 | /* restore register state from stack */ |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2417 | state->regs[value_regno] = *reg; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 2418 | /* mark reg as written since spilled pointer state likely |
| 2419 | * has its liveness marks cleared by is_state_visited() |
| 2420 | * which resets stack/reg liveness for state transitions |
| 2421 | */ |
| 2422 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Jann Horn | 6e7e63c | 2020-04-17 02:00:06 +0200 | [diff] [blame] | 2423 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
| 2424 | /* If value_regno==-1, the caller is asking us whether |
| 2425 | * it is acceptable to use this value as a SCALAR_VALUE |
| 2426 | * (e.g. for XADD). |
| 2427 | * We must not allow unprivileged callers to do that |
| 2428 | * with spilled pointers. |
| 2429 | */ |
| 2430 | verbose(env, "leaking pointer from stack off %d\n", |
| 2431 | off); |
| 2432 | return -EACCES; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2433 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2434 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2435 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2436 | int zeros = 0; |
| 2437 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2438 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2439 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 2440 | continue; |
| 2441 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 2442 | zeros++; |
| 2443 | continue; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2444 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2445 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 2446 | off, i, size); |
| 2447 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2448 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2449 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2450 | if (value_regno >= 0) { |
| 2451 | if (zeros == size) { |
| 2452 | /* any size read into register is zero extended, |
| 2453 | * so the whole register == const_zero |
| 2454 | */ |
| 2455 | __mark_reg_const_zero(&state->regs[value_regno]); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2456 | /* backtracking doesn't support STACK_ZERO yet, |
| 2457 | * so mark it precise here, so that later |
| 2458 | * backtracking can stop here. |
| 2459 | * Backtracking may not need this if this register |
| 2460 | * doesn't participate in pointer adjustment. |
| 2461 | * Forward propagation of precise flag is not |
| 2462 | * necessary either. This mark is only to stop |
| 2463 | * backtracking. Any register that contributed |
| 2464 | * to const 0 was marked precise before spill. |
| 2465 | */ |
| 2466 | state->regs[value_regno].precise = true; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2467 | } else { |
| 2468 | /* have read misc data from the stack */ |
| 2469 | mark_reg_unknown(env, state->regs, value_regno); |
| 2470 | } |
| 2471 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 2472 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2473 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2474 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 2477 | static int check_stack_access(struct bpf_verifier_env *env, |
| 2478 | const struct bpf_reg_state *reg, |
| 2479 | int off, int size) |
| 2480 | { |
| 2481 | /* Stack accesses must be at a fixed offset, so that we |
| 2482 | * can determine what type of data were returned. See |
| 2483 | * check_stack_read(). |
| 2484 | */ |
| 2485 | if (!tnum_is_const(reg->var_off)) { |
| 2486 | char tn_buf[48]; |
| 2487 | |
| 2488 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Andrey Ignatov | 1fbd20f | 2019-04-03 23:22:43 -0700 | [diff] [blame] | 2489 | 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] | 2490 | tn_buf, off, size); |
| 2491 | return -EACCES; |
| 2492 | } |
| 2493 | |
| 2494 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 2495 | verbose(env, "invalid stack off=%d size=%d\n", off, size); |
| 2496 | return -EACCES; |
| 2497 | } |
| 2498 | |
| 2499 | return 0; |
| 2500 | } |
| 2501 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 2502 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
| 2503 | int off, int size, enum bpf_access_type type) |
| 2504 | { |
| 2505 | struct bpf_reg_state *regs = cur_regs(env); |
| 2506 | struct bpf_map *map = regs[regno].map_ptr; |
| 2507 | u32 cap = bpf_map_flags_to_cap(map); |
| 2508 | |
| 2509 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { |
| 2510 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", |
| 2511 | map->value_size, off, size); |
| 2512 | return -EACCES; |
| 2513 | } |
| 2514 | |
| 2515 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { |
| 2516 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", |
| 2517 | map->value_size, off, size); |
| 2518 | return -EACCES; |
| 2519 | } |
| 2520 | |
| 2521 | return 0; |
| 2522 | } |
| 2523 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2524 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
| 2525 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, |
| 2526 | int off, int size, u32 mem_size, |
| 2527 | bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2528 | { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2529 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
| 2530 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2531 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2532 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) |
| 2533 | return 0; |
| 2534 | |
| 2535 | reg = &cur_regs(env)[regno]; |
| 2536 | switch (reg->type) { |
| 2537 | case PTR_TO_MAP_VALUE: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2538 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2539 | mem_size, off, size); |
| 2540 | break; |
| 2541 | case PTR_TO_PACKET: |
| 2542 | case PTR_TO_PACKET_META: |
| 2543 | case PTR_TO_PACKET_END: |
| 2544 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
| 2545 | off, size, regno, reg->id, off, mem_size); |
| 2546 | break; |
| 2547 | case PTR_TO_MEM: |
| 2548 | default: |
| 2549 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", |
| 2550 | mem_size, off, size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2551 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2552 | |
| 2553 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2554 | } |
| 2555 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2556 | /* check read/write into a memory region with possible variable offset */ |
| 2557 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, |
| 2558 | int off, int size, u32 mem_size, |
| 2559 | bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2560 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2561 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2562 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2563 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 2564 | int err; |
| 2565 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2566 | /* We may have adjusted the register pointing to memory region, so we |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2567 | * need to try adding each of min_value and max_value to off |
| 2568 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2569 | */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 2570 | if (env->log.level & BPF_LOG_LEVEL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2571 | print_verifier_state(env, state); |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 2572 | |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2573 | /* The minimum value is only important with signed |
| 2574 | * comparisons where we can't assume the floor of a |
| 2575 | * value is 0. If we are using signed variables for our |
| 2576 | * index'es we need to make sure that whatever we use |
| 2577 | * will have a set floor within our range. |
| 2578 | */ |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 2579 | if (reg->smin_value < 0 && |
| 2580 | (reg->smin_value == S64_MIN || |
| 2581 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || |
| 2582 | reg->smin_value + off < 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2583 | 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] | 2584 | regno); |
| 2585 | return -EACCES; |
| 2586 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2587 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
| 2588 | mem_size, zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2589 | if (err) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2590 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2591 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2592 | return err; |
| 2593 | } |
| 2594 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2595 | /* If we haven't set a max value then we need to bail since we can't be |
| 2596 | * sure we won't do bad things. |
| 2597 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2598 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2599 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2600 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2601 | regno); |
| 2602 | return -EACCES; |
| 2603 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2604 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
| 2605 | mem_size, zero_size_allowed); |
| 2606 | if (err) { |
| 2607 | verbose(env, "R%d max value is outside of the allowed memory range\n", |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2608 | regno); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2609 | return err; |
| 2610 | } |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 2611 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2612 | return 0; |
| 2613 | } |
| 2614 | |
| 2615 | /* check read/write into a map element with possible variable offset */ |
| 2616 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
| 2617 | int off, int size, bool zero_size_allowed) |
| 2618 | { |
| 2619 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2620 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2621 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 2622 | struct bpf_map *map = reg->map_ptr; |
| 2623 | int err; |
| 2624 | |
| 2625 | err = check_mem_region_access(env, regno, off, size, map->value_size, |
| 2626 | zero_size_allowed); |
| 2627 | if (err) |
| 2628 | return err; |
| 2629 | |
| 2630 | if (map_value_has_spin_lock(map)) { |
| 2631 | u32 lock = map->spin_lock_off; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 2632 | |
| 2633 | /* if any part of struct bpf_spin_lock can be touched by |
| 2634 | * load/store reject this program. |
| 2635 | * To check that [x1, x2) overlaps with [y1, y2) |
| 2636 | * it is sufficient to check x1 < y2 && y1 < x2. |
| 2637 | */ |
| 2638 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && |
| 2639 | lock < reg->umax_value + off + size) { |
| 2640 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); |
| 2641 | return -EACCES; |
| 2642 | } |
| 2643 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2644 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 2645 | } |
| 2646 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2647 | #define MAX_PACKET_OFF 0xffff |
| 2648 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 2649 | static enum bpf_prog_type resolve_prog_type(struct bpf_prog *prog) |
| 2650 | { |
| 2651 | return prog->aux->linked_prog ? prog->aux->linked_prog->type |
| 2652 | : prog->type; |
| 2653 | } |
| 2654 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2655 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2656 | const struct bpf_call_arg_meta *meta, |
| 2657 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2658 | { |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 2659 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
| 2660 | |
| 2661 | switch (prog_type) { |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 2662 | /* Program types only with direct read access go here! */ |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2663 | case BPF_PROG_TYPE_LWT_IN: |
| 2664 | case BPF_PROG_TYPE_LWT_OUT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 2665 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2666 | case BPF_PROG_TYPE_SK_REUSEPORT: |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 2667 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Daniel Borkmann | d5563d3 | 2018-10-24 22:05:46 +0200 | [diff] [blame] | 2668 | case BPF_PROG_TYPE_CGROUP_SKB: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2669 | if (t == BPF_WRITE) |
| 2670 | return false; |
Alexander Alemayhu | 7e57fbb | 2017-02-14 00:02:35 +0100 | [diff] [blame] | 2671 | /* fallthrough */ |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 2672 | |
| 2673 | /* Program types with direct read + write access go here! */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2674 | case BPF_PROG_TYPE_SCHED_CLS: |
| 2675 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2676 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2677 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 2678 | case BPF_PROG_TYPE_SK_SKB: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2679 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2680 | if (meta) |
| 2681 | return meta->pkt_access; |
| 2682 | |
| 2683 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2684 | return true; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2685 | |
| 2686 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 2687 | if (t == BPF_WRITE) |
| 2688 | env->seen_direct_write = true; |
| 2689 | |
| 2690 | return true; |
| 2691 | |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2692 | default: |
| 2693 | return false; |
| 2694 | } |
| 2695 | } |
| 2696 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2697 | 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] | 2698 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2699 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2700 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2701 | struct bpf_reg_state *reg = ®s[regno]; |
| 2702 | int err; |
| 2703 | |
| 2704 | /* We may have added a variable offset to the packet pointer; but any |
| 2705 | * reg->range we have comes after that. We are only checking the fixed |
| 2706 | * offset. |
| 2707 | */ |
| 2708 | |
| 2709 | /* We don't allow negative numbers, because we aren't tracking enough |
| 2710 | * detail to prove they're safe. |
| 2711 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2712 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2713 | 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] | 2714 | regno); |
| 2715 | return -EACCES; |
| 2716 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2717 | err = __check_mem_access(env, regno, off, size, reg->range, |
| 2718 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2719 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2720 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2721 | return err; |
| 2722 | } |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 2723 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2724 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 2725 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 2726 | * otherwise find_good_pkt_pointers would have refused to set range info |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 2727 | * that __check_mem_access would have rejected this pkt access. |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 2728 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 2729 | */ |
| 2730 | env->prog->aux->max_pkt_offset = |
| 2731 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 2732 | off + reg->umax_value + size - 1); |
| 2733 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2734 | return err; |
| 2735 | } |
| 2736 | |
| 2737 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2738 | 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] | 2739 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
| 2740 | u32 *btf_id) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2741 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 2742 | struct bpf_insn_access_aux info = { |
| 2743 | .reg_type = *reg_type, |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2744 | .log = &env->log, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 2745 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2746 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 2747 | if (env->ops->is_valid_access && |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2748 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 2749 | /* A non zero info.ctx_field_size indicates that this field is a |
| 2750 | * candidate for later verifier transformation to load the whole |
| 2751 | * field and then apply a mask when accessed with a narrower |
| 2752 | * access than actual ctx access size. A zero info.ctx_field_size |
| 2753 | * will only allow for whole field access and rejects any other |
| 2754 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2755 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 2756 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2757 | |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 2758 | if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL) |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 2759 | *btf_id = info.btf_id; |
| 2760 | else |
| 2761 | 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] | 2762 | /* remember the offset of last byte accessed in ctx */ |
| 2763 | if (env->prog->aux->max_ctx_offset < off + size) |
| 2764 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2765 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 2766 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2767 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2768 | 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] | 2769 | return -EACCES; |
| 2770 | } |
| 2771 | |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2772 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 2773 | int size) |
| 2774 | { |
| 2775 | if (size < 0 || off < 0 || |
| 2776 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 2777 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 2778 | off, size); |
| 2779 | return -EACCES; |
| 2780 | } |
| 2781 | return 0; |
| 2782 | } |
| 2783 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2784 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
| 2785 | u32 regno, int off, int size, |
| 2786 | enum bpf_access_type t) |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2787 | { |
| 2788 | struct bpf_reg_state *regs = cur_regs(env); |
| 2789 | struct bpf_reg_state *reg = ®s[regno]; |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2790 | struct bpf_insn_access_aux info = {}; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2791 | bool valid; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2792 | |
| 2793 | if (reg->smin_value < 0) { |
| 2794 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 2795 | regno); |
| 2796 | return -EACCES; |
| 2797 | } |
| 2798 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2799 | switch (reg->type) { |
| 2800 | case PTR_TO_SOCK_COMMON: |
| 2801 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); |
| 2802 | break; |
| 2803 | case PTR_TO_SOCKET: |
| 2804 | valid = bpf_sock_is_valid_access(off, size, t, &info); |
| 2805 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2806 | case PTR_TO_TCP_SOCK: |
| 2807 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); |
| 2808 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2809 | case PTR_TO_XDP_SOCK: |
| 2810 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); |
| 2811 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2812 | default: |
| 2813 | valid = false; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2814 | } |
| 2815 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2816 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2817 | if (valid) { |
| 2818 | env->insn_aux_data[insn_idx].ctx_field_size = |
| 2819 | info.ctx_field_size; |
| 2820 | return 0; |
| 2821 | } |
| 2822 | |
| 2823 | verbose(env, "R%d invalid %s access off=%d size=%d\n", |
| 2824 | regno, reg_type_str[reg->type], off, size); |
| 2825 | |
| 2826 | return -EACCES; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2827 | } |
| 2828 | |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2829 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
| 2830 | { |
| 2831 | return cur_regs(env) + regno; |
| 2832 | } |
| 2833 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2834 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 2835 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2836 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2837 | } |
| 2838 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2839 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 2840 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2841 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2842 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2843 | return reg->type == PTR_TO_CTX; |
| 2844 | } |
| 2845 | |
| 2846 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) |
| 2847 | { |
| 2848 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 2849 | |
| 2850 | return type_is_sk_pointer(reg->type); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2851 | } |
| 2852 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2853 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 2854 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2855 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2856 | |
| 2857 | return type_is_pkt_pointer(reg->type); |
| 2858 | } |
| 2859 | |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 2860 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 2861 | { |
| 2862 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 2863 | |
| 2864 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 2865 | return reg->type == PTR_TO_FLOW_KEYS; |
| 2866 | } |
| 2867 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2868 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 2869 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2870 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2871 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2872 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2873 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2874 | |
| 2875 | /* Byte size accesses are always allowed. */ |
| 2876 | if (!strict || size == 1) |
| 2877 | return 0; |
| 2878 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 2879 | /* For platforms that do not have a Kconfig enabling |
| 2880 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 2881 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 2882 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 2883 | * to this code only in strict mode where we want to emulate |
| 2884 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 2885 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2886 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 2887 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2888 | |
| 2889 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 2890 | if (!tnum_is_aligned(reg_off, size)) { |
| 2891 | char tn_buf[48]; |
| 2892 | |
| 2893 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2894 | verbose(env, |
| 2895 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2896 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2897 | return -EACCES; |
| 2898 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2899 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2900 | return 0; |
| 2901 | } |
| 2902 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2903 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 2904 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2905 | const char *pointer_desc, |
| 2906 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2907 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2908 | struct tnum reg_off; |
| 2909 | |
| 2910 | /* Byte size accesses are always allowed. */ |
| 2911 | if (!strict || size == 1) |
| 2912 | return 0; |
| 2913 | |
| 2914 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 2915 | if (!tnum_is_aligned(reg_off, size)) { |
| 2916 | char tn_buf[48]; |
| 2917 | |
| 2918 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2919 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2920 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2921 | return -EACCES; |
| 2922 | } |
| 2923 | |
| 2924 | return 0; |
| 2925 | } |
| 2926 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2927 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2928 | const struct bpf_reg_state *reg, int off, |
| 2929 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2930 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2931 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2932 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2933 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2934 | switch (reg->type) { |
| 2935 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2936 | case PTR_TO_PACKET_META: |
| 2937 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 2938 | * right in front, treat it the very same way. |
| 2939 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2940 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2941 | case PTR_TO_FLOW_KEYS: |
| 2942 | pointer_desc = "flow keys "; |
| 2943 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2944 | case PTR_TO_MAP_VALUE: |
| 2945 | pointer_desc = "value "; |
| 2946 | break; |
| 2947 | case PTR_TO_CTX: |
| 2948 | pointer_desc = "context "; |
| 2949 | break; |
| 2950 | case PTR_TO_STACK: |
| 2951 | pointer_desc = "stack "; |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 2952 | /* The stack spill tracking logic in check_stack_write() |
| 2953 | * and check_stack_read() relies on stack accesses being |
| 2954 | * aligned. |
| 2955 | */ |
| 2956 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2957 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2958 | case PTR_TO_SOCKET: |
| 2959 | pointer_desc = "sock "; |
| 2960 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2961 | case PTR_TO_SOCK_COMMON: |
| 2962 | pointer_desc = "sock_common "; |
| 2963 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2964 | case PTR_TO_TCP_SOCK: |
| 2965 | pointer_desc = "tcp_sock "; |
| 2966 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2967 | case PTR_TO_XDP_SOCK: |
| 2968 | pointer_desc = "xdp_sock "; |
| 2969 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2970 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2971 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2972 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2973 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 2974 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2975 | } |
| 2976 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2977 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 2978 | const struct bpf_func_state *func, |
| 2979 | int off) |
| 2980 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2981 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2982 | |
| 2983 | if (stack >= -off) |
| 2984 | return 0; |
| 2985 | |
| 2986 | /* update known max for given subprogram */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2987 | env->subprog_info[func->subprogno].stack_depth = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2988 | return 0; |
| 2989 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2990 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2991 | /* starting from main bpf function walk all instructions of the function |
| 2992 | * and recursively walk all callees that given function can call. |
| 2993 | * Ignore jump and exit insns. |
| 2994 | * Since recursion is prevented by check_cfg() this algorithm |
| 2995 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 2996 | */ |
| 2997 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 2998 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2999 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 3000 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3001 | struct bpf_insn *insn = env->prog->insnsi; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3002 | bool tail_call_reachable = false; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3003 | int ret_insn[MAX_CALL_FRAMES]; |
| 3004 | int ret_prog[MAX_CALL_FRAMES]; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3005 | int j; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3006 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3007 | process_func: |
Maciej Fijalkowski | 7f6e431 | 2020-09-16 23:10:07 +0200 | [diff] [blame] | 3008 | /* protect against potential stack overflow that might happen when |
| 3009 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack |
| 3010 | * depth for such case down to 256 so that the worst case scenario |
| 3011 | * would result in 8k stack size (32 which is tailcall limit * 256 = |
| 3012 | * 8k). |
| 3013 | * |
| 3014 | * To get the idea what might happen, see an example: |
| 3015 | * func1 -> sub rsp, 128 |
| 3016 | * subfunc1 -> sub rsp, 256 |
| 3017 | * tailcall1 -> add rsp, 256 |
| 3018 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) |
| 3019 | * subfunc2 -> sub rsp, 64 |
| 3020 | * subfunc22 -> sub rsp, 128 |
| 3021 | * tailcall2 -> add rsp, 128 |
| 3022 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) |
| 3023 | * |
| 3024 | * tailcall will unwind the current stack frame but it will not get rid |
| 3025 | * of caller's stack as shown on the example above. |
| 3026 | */ |
| 3027 | if (idx && subprog[idx].has_tail_call && depth >= 256) { |
| 3028 | verbose(env, |
| 3029 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", |
| 3030 | depth); |
| 3031 | return -EACCES; |
| 3032 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3033 | /* round up to 32-bytes, since this is granularity |
| 3034 | * of interpreter stack size |
| 3035 | */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3036 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3037 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3038 | 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] | 3039 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3040 | return -EACCES; |
| 3041 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3042 | continue_func: |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 3043 | subprog_end = subprog[idx + 1].start; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3044 | for (; i < subprog_end; i++) { |
| 3045 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 3046 | continue; |
| 3047 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 3048 | continue; |
| 3049 | /* remember insn and function to return to */ |
| 3050 | ret_insn[frame] = i + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3051 | ret_prog[frame] = idx; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3052 | |
| 3053 | /* find the callee */ |
| 3054 | i = i + insn[i].imm + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3055 | idx = find_subprog(env, i); |
| 3056 | if (idx < 0) { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3057 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 3058 | i); |
| 3059 | return -EFAULT; |
| 3060 | } |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3061 | |
| 3062 | if (subprog[idx].has_tail_call) |
| 3063 | tail_call_reachable = true; |
| 3064 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3065 | frame++; |
| 3066 | if (frame >= MAX_CALL_FRAMES) { |
Paul Chaignon | 927cb78 | 2019-03-20 13:58:27 +0100 | [diff] [blame] | 3067 | verbose(env, "the call stack of %d frames is too deep !\n", |
| 3068 | frame); |
| 3069 | return -E2BIG; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3070 | } |
| 3071 | goto process_func; |
| 3072 | } |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3073 | /* if tail call got detected across bpf2bpf calls then mark each of the |
| 3074 | * currently present subprog frames as tail call reachable subprogs; |
| 3075 | * this info will be utilized by JIT so that we will be preserving the |
| 3076 | * tail call counter throughout bpf2bpf calls combined with tailcalls |
| 3077 | */ |
| 3078 | if (tail_call_reachable) |
| 3079 | for (j = 0; j < frame; j++) |
| 3080 | subprog[ret_prog[j]].tail_call_reachable = true; |
| 3081 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3082 | /* end of for() loop means the last insn of the 'subprog' |
| 3083 | * was reached. Doesn't matter whether it was JA or EXIT |
| 3084 | */ |
| 3085 | if (frame == 0) |
| 3086 | return 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3087 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3088 | frame--; |
| 3089 | i = ret_insn[frame]; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3090 | idx = ret_prog[frame]; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3091 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3092 | } |
| 3093 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 3094 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 3095 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 3096 | const struct bpf_insn *insn, int idx) |
| 3097 | { |
| 3098 | int start = idx + insn->imm + 1, subprog; |
| 3099 | |
| 3100 | subprog = find_subprog(env, start); |
| 3101 | if (subprog < 0) { |
| 3102 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 3103 | start); |
| 3104 | return -EFAULT; |
| 3105 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3106 | return env->subprog_info[subprog].stack_depth; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 3107 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 3108 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 3109 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 3110 | int check_ctx_reg(struct bpf_verifier_env *env, |
| 3111 | const struct bpf_reg_state *reg, int regno) |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 3112 | { |
| 3113 | /* Access to ctx or passing it to a helper is only allowed in |
| 3114 | * its original, unmodified form. |
| 3115 | */ |
| 3116 | |
| 3117 | if (reg->off) { |
| 3118 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 3119 | regno, reg->off); |
| 3120 | return -EACCES; |
| 3121 | } |
| 3122 | |
| 3123 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 3124 | char tn_buf[48]; |
| 3125 | |
| 3126 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3127 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 3128 | return -EACCES; |
| 3129 | } |
| 3130 | |
| 3131 | return 0; |
| 3132 | } |
| 3133 | |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3134 | static int __check_buffer_access(struct bpf_verifier_env *env, |
| 3135 | const char *buf_info, |
| 3136 | const struct bpf_reg_state *reg, |
| 3137 | int regno, int off, int size) |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3138 | { |
| 3139 | if (off < 0) { |
| 3140 | verbose(env, |
Yonghong Song | 4fc00b7 | 2020-07-28 15:18:01 -0700 | [diff] [blame] | 3141 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3142 | regno, buf_info, off, size); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3143 | return -EACCES; |
| 3144 | } |
| 3145 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 3146 | char tn_buf[48]; |
| 3147 | |
| 3148 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3149 | verbose(env, |
Yonghong Song | 4fc00b7 | 2020-07-28 15:18:01 -0700 | [diff] [blame] | 3150 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3151 | regno, off, tn_buf); |
| 3152 | return -EACCES; |
| 3153 | } |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3154 | |
| 3155 | return 0; |
| 3156 | } |
| 3157 | |
| 3158 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
| 3159 | const struct bpf_reg_state *reg, |
| 3160 | int regno, int off, int size) |
| 3161 | { |
| 3162 | int err; |
| 3163 | |
| 3164 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); |
| 3165 | if (err) |
| 3166 | return err; |
| 3167 | |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3168 | if (off + size > env->prog->aux->max_tp_access) |
| 3169 | env->prog->aux->max_tp_access = off + size; |
| 3170 | |
| 3171 | return 0; |
| 3172 | } |
| 3173 | |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3174 | static int check_buffer_access(struct bpf_verifier_env *env, |
| 3175 | const struct bpf_reg_state *reg, |
| 3176 | int regno, int off, int size, |
| 3177 | bool zero_size_allowed, |
| 3178 | const char *buf_info, |
| 3179 | u32 *max_access) |
| 3180 | { |
| 3181 | int err; |
| 3182 | |
| 3183 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); |
| 3184 | if (err) |
| 3185 | return err; |
| 3186 | |
| 3187 | if (off + size > *max_access) |
| 3188 | *max_access = off + size; |
| 3189 | |
| 3190 | return 0; |
| 3191 | } |
| 3192 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 3193 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
| 3194 | static void zext_32_to_64(struct bpf_reg_state *reg) |
| 3195 | { |
| 3196 | reg->var_off = tnum_subreg(reg->var_off); |
| 3197 | __reg_assign_32_into_64(reg); |
| 3198 | } |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3199 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3200 | /* truncate register to smaller size (in bytes) |
| 3201 | * must be called with size < BPF_REG_SIZE |
| 3202 | */ |
| 3203 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 3204 | { |
| 3205 | u64 mask; |
| 3206 | |
| 3207 | /* clear high bits in bit representation */ |
| 3208 | reg->var_off = tnum_cast(reg->var_off, size); |
| 3209 | |
| 3210 | /* fix arithmetic bounds */ |
| 3211 | mask = ((u64)1 << (size * 8)) - 1; |
| 3212 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 3213 | reg->umin_value &= mask; |
| 3214 | reg->umax_value &= mask; |
| 3215 | } else { |
| 3216 | reg->umin_value = 0; |
| 3217 | reg->umax_value = mask; |
| 3218 | } |
| 3219 | reg->smin_value = reg->umin_value; |
| 3220 | reg->smax_value = reg->umax_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 3221 | |
| 3222 | /* If size is smaller than 32bit register the 32bit register |
| 3223 | * values are also truncated so we push 64-bit bounds into |
| 3224 | * 32-bit bounds. Above were truncated < 32-bits already. |
| 3225 | */ |
| 3226 | if (size >= 4) |
| 3227 | return; |
| 3228 | __reg_combine_64_into_32(reg); |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3229 | } |
| 3230 | |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 3231 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
| 3232 | { |
| 3233 | return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen; |
| 3234 | } |
| 3235 | |
| 3236 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) |
| 3237 | { |
| 3238 | void *ptr; |
| 3239 | u64 addr; |
| 3240 | int err; |
| 3241 | |
| 3242 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 3243 | if (err) |
| 3244 | return err; |
Andrii Nakryiko | 2dedd7d | 2019-10-11 10:20:53 -0700 | [diff] [blame] | 3245 | ptr = (void *)(long)addr + off; |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 3246 | |
| 3247 | switch (size) { |
| 3248 | case sizeof(u8): |
| 3249 | *val = (u64)*(u8 *)ptr; |
| 3250 | break; |
| 3251 | case sizeof(u16): |
| 3252 | *val = (u64)*(u16 *)ptr; |
| 3253 | break; |
| 3254 | case sizeof(u32): |
| 3255 | *val = (u64)*(u32 *)ptr; |
| 3256 | break; |
| 3257 | case sizeof(u64): |
| 3258 | *val = *(u64 *)ptr; |
| 3259 | break; |
| 3260 | default: |
| 3261 | return -EINVAL; |
| 3262 | } |
| 3263 | return 0; |
| 3264 | } |
| 3265 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3266 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
| 3267 | struct bpf_reg_state *regs, |
| 3268 | int regno, int off, int size, |
| 3269 | enum bpf_access_type atype, |
| 3270 | int value_regno) |
| 3271 | { |
| 3272 | struct bpf_reg_state *reg = regs + regno; |
| 3273 | const struct btf_type *t = btf_type_by_id(btf_vmlinux, reg->btf_id); |
| 3274 | const char *tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 3275 | u32 btf_id; |
| 3276 | int ret; |
| 3277 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3278 | if (off < 0) { |
| 3279 | verbose(env, |
| 3280 | "R%d is ptr_%s invalid negative access: off=%d\n", |
| 3281 | regno, tname, off); |
| 3282 | return -EACCES; |
| 3283 | } |
| 3284 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 3285 | char tn_buf[48]; |
| 3286 | |
| 3287 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3288 | verbose(env, |
| 3289 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", |
| 3290 | regno, tname, off, tn_buf); |
| 3291 | return -EACCES; |
| 3292 | } |
| 3293 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 3294 | if (env->ops->btf_struct_access) { |
| 3295 | ret = env->ops->btf_struct_access(&env->log, t, off, size, |
| 3296 | atype, &btf_id); |
| 3297 | } else { |
| 3298 | if (atype != BPF_READ) { |
| 3299 | verbose(env, "only read is supported\n"); |
| 3300 | return -EACCES; |
| 3301 | } |
| 3302 | |
| 3303 | ret = btf_struct_access(&env->log, t, off, size, atype, |
| 3304 | &btf_id); |
| 3305 | } |
| 3306 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3307 | if (ret < 0) |
| 3308 | return ret; |
| 3309 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 3310 | if (atype == BPF_READ && value_regno >= 0) |
| 3311 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_id); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 3312 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3313 | return 0; |
| 3314 | } |
| 3315 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 3316 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, |
| 3317 | struct bpf_reg_state *regs, |
| 3318 | int regno, int off, int size, |
| 3319 | enum bpf_access_type atype, |
| 3320 | int value_regno) |
| 3321 | { |
| 3322 | struct bpf_reg_state *reg = regs + regno; |
| 3323 | struct bpf_map *map = reg->map_ptr; |
| 3324 | const struct btf_type *t; |
| 3325 | const char *tname; |
| 3326 | u32 btf_id; |
| 3327 | int ret; |
| 3328 | |
| 3329 | if (!btf_vmlinux) { |
| 3330 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); |
| 3331 | return -ENOTSUPP; |
| 3332 | } |
| 3333 | |
| 3334 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { |
| 3335 | verbose(env, "map_ptr access not supported for map type %d\n", |
| 3336 | map->map_type); |
| 3337 | return -ENOTSUPP; |
| 3338 | } |
| 3339 | |
| 3340 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); |
| 3341 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 3342 | |
| 3343 | if (!env->allow_ptr_to_map_access) { |
| 3344 | verbose(env, |
| 3345 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", |
| 3346 | tname); |
| 3347 | return -EPERM; |
| 3348 | } |
| 3349 | |
| 3350 | if (off < 0) { |
| 3351 | verbose(env, "R%d is %s invalid negative access: off=%d\n", |
| 3352 | regno, tname, off); |
| 3353 | return -EACCES; |
| 3354 | } |
| 3355 | |
| 3356 | if (atype != BPF_READ) { |
| 3357 | verbose(env, "only read from %s is supported\n", tname); |
| 3358 | return -EACCES; |
| 3359 | } |
| 3360 | |
| 3361 | ret = btf_struct_access(&env->log, t, off, size, atype, &btf_id); |
| 3362 | if (ret < 0) |
| 3363 | return ret; |
| 3364 | |
| 3365 | if (value_regno >= 0) |
| 3366 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_id); |
| 3367 | |
| 3368 | return 0; |
| 3369 | } |
| 3370 | |
| 3371 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3372 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 3373 | * if t==write, value_regno is a register which value is stored into memory |
| 3374 | * if t==read, value_regno is a register which will receive the value from memory |
| 3375 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 3376 | * if t==read && value_regno==-1, don't care what we read from memory |
| 3377 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3378 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 3379 | int off, int bpf_size, enum bpf_access_type t, |
| 3380 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3381 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3382 | struct bpf_reg_state *regs = cur_regs(env); |
| 3383 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3384 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3385 | int size, err = 0; |
| 3386 | |
| 3387 | size = bpf_size_to_bytes(bpf_size); |
| 3388 | if (size < 0) |
| 3389 | return size; |
| 3390 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3391 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3392 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3393 | if (err) |
| 3394 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3395 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3396 | /* for access checks, reg->off is just part of off */ |
| 3397 | off += reg->off; |
| 3398 | |
| 3399 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3400 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3401 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3402 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3403 | return -EACCES; |
| 3404 | } |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3405 | err = check_map_access_type(env, regno, off, size, t); |
| 3406 | if (err) |
| 3407 | return err; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3408 | err = check_map_access(env, regno, off, size, false); |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 3409 | if (!err && t == BPF_READ && value_regno >= 0) { |
| 3410 | struct bpf_map *map = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3411 | |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 3412 | /* if map is read-only, track its contents as scalars */ |
| 3413 | if (tnum_is_const(reg->var_off) && |
| 3414 | bpf_map_is_rdonly(map) && |
| 3415 | map->ops->map_direct_value_addr) { |
| 3416 | int map_off = off + reg->var_off.value; |
| 3417 | u64 val = 0; |
| 3418 | |
| 3419 | err = bpf_map_direct_read(map, map_off, size, |
| 3420 | &val); |
| 3421 | if (err) |
| 3422 | return err; |
| 3423 | |
| 3424 | regs[value_regno].type = SCALAR_VALUE; |
| 3425 | __mark_reg_known(®s[value_regno], val); |
| 3426 | } else { |
| 3427 | mark_reg_unknown(env, regs, value_regno); |
| 3428 | } |
| 3429 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3430 | } else if (reg->type == PTR_TO_MEM) { |
| 3431 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3432 | is_pointer_value(env, value_regno)) { |
| 3433 | verbose(env, "R%d leaks addr into mem\n", value_regno); |
| 3434 | return -EACCES; |
| 3435 | } |
| 3436 | err = check_mem_region_access(env, regno, off, size, |
| 3437 | reg->mem_size, false); |
| 3438 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3439 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3440 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3441 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3442 | u32 btf_id = 0; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 3443 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3444 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3445 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3446 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3447 | return -EACCES; |
| 3448 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3449 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 3450 | err = check_ctx_reg(env, reg, regno); |
| 3451 | if (err < 0) |
| 3452 | return err; |
| 3453 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3454 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf_id); |
| 3455 | if (err) |
| 3456 | verbose_linfo(env, insn_idx, "; "); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3457 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3458 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3459 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 3460 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3461 | */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3462 | if (reg_type == SCALAR_VALUE) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3463 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3464 | } else { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3465 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3466 | value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3467 | if (reg_type_may_be_null(reg_type)) |
| 3468 | regs[value_regno].id = ++env->id_gen; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 3469 | /* A load of ctx field could have different |
| 3470 | * actual load size with the one encoded in the |
| 3471 | * insn. When the dst is PTR, it is for sure not |
| 3472 | * a sub-register. |
| 3473 | */ |
| 3474 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 3475 | if (reg_type == PTR_TO_BTF_ID || |
| 3476 | reg_type == PTR_TO_BTF_ID_OR_NULL) |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3477 | regs[value_regno].btf_id = btf_id; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3478 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3479 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3480 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3481 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3482 | } else if (reg->type == PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3483 | off += reg->var_off.value; |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3484 | err = check_stack_access(env, reg, off, size); |
| 3485 | if (err) |
| 3486 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 3487 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3488 | state = func(env, reg); |
| 3489 | err = update_stack_depth(env, state, off); |
| 3490 | if (err) |
| 3491 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 3492 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3493 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3494 | err = check_stack_write(env, state, off, size, |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 3495 | value_regno, insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3496 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3497 | err = check_stack_read(env, state, off, size, |
| 3498 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3499 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 3500 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3501 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3502 | return -EACCES; |
| 3503 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3504 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3505 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3506 | verbose(env, "R%d leaks addr into packet\n", |
| 3507 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3508 | return -EACCES; |
| 3509 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3510 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3511 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3512 | mark_reg_unknown(env, regs, value_regno); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 3513 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 3514 | if (t == BPF_WRITE && value_regno >= 0 && |
| 3515 | is_pointer_value(env, value_regno)) { |
| 3516 | verbose(env, "R%d leaks addr into flow keys\n", |
| 3517 | value_regno); |
| 3518 | return -EACCES; |
| 3519 | } |
| 3520 | |
| 3521 | err = check_flow_keys_access(env, off, size); |
| 3522 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3523 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3524 | } else if (type_is_sk_pointer(reg->type)) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3525 | if (t == BPF_WRITE) { |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3526 | verbose(env, "R%d cannot write into %s\n", |
| 3527 | regno, reg_type_str[reg->type]); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3528 | return -EACCES; |
| 3529 | } |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 3530 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3531 | if (!err && value_regno >= 0) |
| 3532 | mark_reg_unknown(env, regs, value_regno); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 3533 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
| 3534 | err = check_tp_buffer_access(env, reg, regno, off, size); |
| 3535 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3536 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3537 | } else if (reg->type == PTR_TO_BTF_ID) { |
| 3538 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
| 3539 | value_regno); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 3540 | } else if (reg->type == CONST_PTR_TO_MAP) { |
| 3541 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, |
| 3542 | value_regno); |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3543 | } else if (reg->type == PTR_TO_RDONLY_BUF) { |
| 3544 | if (t == BPF_WRITE) { |
| 3545 | verbose(env, "R%d cannot write into %s\n", |
| 3546 | regno, reg_type_str[reg->type]); |
| 3547 | return -EACCES; |
| 3548 | } |
Colin Ian King | f6dfbe31 | 2020-07-27 18:54:11 +0100 | [diff] [blame] | 3549 | err = check_buffer_access(env, reg, regno, off, size, false, |
| 3550 | "rdonly", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3551 | &env->prog->aux->max_rdonly_access); |
| 3552 | if (!err && value_regno >= 0) |
| 3553 | mark_reg_unknown(env, regs, value_regno); |
| 3554 | } else if (reg->type == PTR_TO_RDWR_BUF) { |
Colin Ian King | f6dfbe31 | 2020-07-27 18:54:11 +0100 | [diff] [blame] | 3555 | err = check_buffer_access(env, reg, regno, off, size, false, |
| 3556 | "rdwr", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3557 | &env->prog->aux->max_rdwr_access); |
| 3558 | if (!err && t == BPF_READ && value_regno >= 0) |
| 3559 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3560 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3561 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 3562 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3563 | return -EACCES; |
| 3564 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3565 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3566 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3567 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3568 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3569 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3570 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3571 | return err; |
| 3572 | } |
| 3573 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3574 | 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] | 3575 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3576 | int err; |
| 3577 | |
| 3578 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 3579 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3580 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3581 | return -EINVAL; |
| 3582 | } |
| 3583 | |
| 3584 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3585 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3586 | if (err) |
| 3587 | return err; |
| 3588 | |
| 3589 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3590 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3591 | if (err) |
| 3592 | return err; |
| 3593 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 3594 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3595 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 3596 | return -EACCES; |
| 3597 | } |
| 3598 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3599 | if (is_ctx_reg(env, insn->dst_reg) || |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 3600 | is_pkt_reg(env, insn->dst_reg) || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3601 | is_flow_key_reg(env, insn->dst_reg) || |
| 3602 | is_sk_reg(env, insn->dst_reg)) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3603 | 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] | 3604 | insn->dst_reg, |
| 3605 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 3606 | return -EACCES; |
| 3607 | } |
| 3608 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3609 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3610 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3611 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3612 | if (err) |
| 3613 | return err; |
| 3614 | |
| 3615 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3616 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3617 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3618 | } |
| 3619 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3620 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
| 3621 | int off, int access_size, |
| 3622 | bool zero_size_allowed) |
| 3623 | { |
| 3624 | struct bpf_reg_state *reg = reg_state(env, regno); |
| 3625 | |
| 3626 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 3627 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
| 3628 | if (tnum_is_const(reg->var_off)) { |
| 3629 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", |
| 3630 | regno, off, access_size); |
| 3631 | } else { |
| 3632 | char tn_buf[48]; |
| 3633 | |
| 3634 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3635 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", |
| 3636 | regno, tn_buf, access_size); |
| 3637 | } |
| 3638 | return -EACCES; |
| 3639 | } |
| 3640 | return 0; |
| 3641 | } |
| 3642 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3643 | /* when register 'regno' is passed into function that will read 'access_size' |
| 3644 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3645 | * and all elements of stack are initialized. |
| 3646 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 3647 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3648 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3649 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3650 | int access_size, bool zero_size_allowed, |
| 3651 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3652 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 3653 | struct bpf_reg_state *reg = reg_state(env, regno); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3654 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3655 | int err, min_off, max_off, i, j, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3656 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3657 | if (tnum_is_const(reg->var_off)) { |
| 3658 | min_off = max_off = reg->var_off.value + reg->off; |
| 3659 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 3660 | zero_size_allowed); |
| 3661 | if (err) |
| 3662 | return err; |
| 3663 | } else { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3664 | /* Variable offset is prohibited for unprivileged mode for |
| 3665 | * simplicity since it requires corresponding support in |
| 3666 | * Spectre masking for stack ALU. |
| 3667 | * See also retrieve_ptr_limit(). |
| 3668 | */ |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 3669 | if (!env->bypass_spec_v1) { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3670 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3671 | |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3672 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3673 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", |
| 3674 | regno, tn_buf); |
| 3675 | return -EACCES; |
| 3676 | } |
Andrey Ignatov | f2bcd05 | 2019-04-03 23:22:37 -0700 | [diff] [blame] | 3677 | /* Only initialized buffer on stack is allowed to be accessed |
| 3678 | * with variable offset. With uninitialized buffer it's hard to |
| 3679 | * guarantee that whole memory is marked as initialized on |
| 3680 | * helper return since specific bounds are unknown what may |
| 3681 | * cause uninitialized stack leaking. |
| 3682 | */ |
| 3683 | if (meta && meta->raw_mode) |
| 3684 | meta = NULL; |
| 3685 | |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3686 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
| 3687 | reg->smax_value <= -BPF_MAX_VAR_OFF) { |
| 3688 | verbose(env, "R%d unbounded indirect variable offset stack access\n", |
| 3689 | regno); |
| 3690 | return -EACCES; |
| 3691 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3692 | min_off = reg->smin_value + reg->off; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3693 | max_off = reg->smax_value + reg->off; |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3694 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 3695 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3696 | if (err) { |
| 3697 | verbose(env, "R%d min value is outside of stack bound\n", |
| 3698 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3699 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3700 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3701 | err = __check_stack_boundary(env, regno, max_off, access_size, |
| 3702 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3703 | if (err) { |
| 3704 | verbose(env, "R%d max value is outside of stack bound\n", |
| 3705 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3706 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 3707 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3708 | } |
| 3709 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3710 | if (meta && meta->raw_mode) { |
| 3711 | meta->access_size = access_size; |
| 3712 | meta->regno = regno; |
| 3713 | return 0; |
| 3714 | } |
| 3715 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3716 | for (i = min_off; i < max_off + access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3717 | u8 *stype; |
| 3718 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3719 | slot = -i - 1; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3720 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3721 | if (state->allocated_stack <= slot) |
| 3722 | goto err; |
| 3723 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 3724 | if (*stype == STACK_MISC) |
| 3725 | goto mark; |
| 3726 | if (*stype == STACK_ZERO) { |
| 3727 | /* helper can write anything into the stack */ |
| 3728 | *stype = STACK_MISC; |
| 3729 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3730 | } |
Yonghong Song | 1d68f22 | 2020-05-09 10:59:15 -0700 | [diff] [blame] | 3731 | |
| 3732 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 3733 | state->stack[spi].spilled_ptr.type == PTR_TO_BTF_ID) |
| 3734 | goto mark; |
| 3735 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3736 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 3737 | state->stack[spi].spilled_ptr.type == SCALAR_VALUE) { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 3738 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3739 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 3740 | state->stack[spi].slot_type[j] = STACK_MISC; |
| 3741 | goto mark; |
| 3742 | } |
| 3743 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3744 | err: |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3745 | if (tnum_is_const(reg->var_off)) { |
| 3746 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 3747 | min_off, i - min_off, access_size); |
| 3748 | } else { |
| 3749 | char tn_buf[48]; |
| 3750 | |
| 3751 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3752 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", |
| 3753 | tn_buf, i - min_off, access_size); |
| 3754 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3755 | return -EACCES; |
| 3756 | mark: |
| 3757 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 3758 | * the whole slot to be marked as 'read' |
| 3759 | */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 3760 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 3761 | state->stack[spi].spilled_ptr.parent, |
| 3762 | REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3763 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 3764 | return update_stack_depth(env, state, min_off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3765 | } |
| 3766 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3767 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 3768 | int access_size, bool zero_size_allowed, |
| 3769 | struct bpf_call_arg_meta *meta) |
| 3770 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3771 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3772 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3773 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3774 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3775 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3776 | return check_packet_access(env, regno, reg->off, access_size, |
| 3777 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3778 | case PTR_TO_MAP_VALUE: |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3779 | if (check_map_access_type(env, regno, reg->off, access_size, |
| 3780 | meta && meta->raw_mode ? BPF_WRITE : |
| 3781 | BPF_READ)) |
| 3782 | return -EACCES; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3783 | return check_map_access(env, regno, reg->off, access_size, |
| 3784 | zero_size_allowed); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3785 | case PTR_TO_MEM: |
| 3786 | return check_mem_region_access(env, regno, reg->off, |
| 3787 | access_size, reg->mem_size, |
| 3788 | zero_size_allowed); |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 3789 | case PTR_TO_RDONLY_BUF: |
| 3790 | if (meta && meta->raw_mode) |
| 3791 | return -EACCES; |
| 3792 | return check_buffer_access(env, reg, regno, reg->off, |
| 3793 | access_size, zero_size_allowed, |
| 3794 | "rdonly", |
| 3795 | &env->prog->aux->max_rdonly_access); |
| 3796 | case PTR_TO_RDWR_BUF: |
| 3797 | return check_buffer_access(env, reg, regno, reg->off, |
| 3798 | access_size, zero_size_allowed, |
| 3799 | "rdwr", |
| 3800 | &env->prog->aux->max_rdwr_access); |
Lorenz Bauer | 0d004c02 | 2020-09-21 13:12:18 +0100 | [diff] [blame] | 3801 | case PTR_TO_STACK: |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3802 | return check_stack_boundary(env, regno, access_size, |
| 3803 | zero_size_allowed, meta); |
Lorenz Bauer | 0d004c02 | 2020-09-21 13:12:18 +0100 | [diff] [blame] | 3804 | default: /* scalar_value or invalid ptr */ |
| 3805 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
| 3806 | if (zero_size_allowed && access_size == 0 && |
| 3807 | register_is_null(reg)) |
| 3808 | return 0; |
| 3809 | |
| 3810 | verbose(env, "R%d type=%s expected=%s\n", regno, |
| 3811 | reg_type_str[reg->type], |
| 3812 | reg_type_str[PTR_TO_STACK]); |
| 3813 | return -EACCES; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 3814 | } |
| 3815 | } |
| 3816 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3817 | /* Implementation details: |
| 3818 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL |
| 3819 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
| 3820 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after |
| 3821 | * value_or_null->value transition, since the verifier only cares about |
| 3822 | * the range of access to valid map value pointer and doesn't care about actual |
| 3823 | * address of the map element. |
| 3824 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
| 3825 | * reg->id > 0 after value_or_null->value transition. By doing so |
| 3826 | * two bpf_map_lookups will be considered two different pointers that |
| 3827 | * point to different bpf_spin_locks. |
| 3828 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
| 3829 | * dead-locks. |
| 3830 | * Since only one bpf_spin_lock is allowed the checks are simpler than |
| 3831 | * reg_is_refcounted() logic. The verifier needs to remember only |
| 3832 | * one spin_lock instead of array of acquired_refs. |
| 3833 | * cur_state->active_spin_lock remembers which map value element got locked |
| 3834 | * and clears it after bpf_spin_unlock. |
| 3835 | */ |
| 3836 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, |
| 3837 | bool is_lock) |
| 3838 | { |
| 3839 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 3840 | struct bpf_verifier_state *cur = env->cur_state; |
| 3841 | bool is_const = tnum_is_const(reg->var_off); |
| 3842 | struct bpf_map *map = reg->map_ptr; |
| 3843 | u64 val = reg->var_off.value; |
| 3844 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3845 | if (!is_const) { |
| 3846 | verbose(env, |
| 3847 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", |
| 3848 | regno); |
| 3849 | return -EINVAL; |
| 3850 | } |
| 3851 | if (!map->btf) { |
| 3852 | verbose(env, |
| 3853 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", |
| 3854 | map->name); |
| 3855 | return -EINVAL; |
| 3856 | } |
| 3857 | if (!map_value_has_spin_lock(map)) { |
| 3858 | if (map->spin_lock_off == -E2BIG) |
| 3859 | verbose(env, |
| 3860 | "map '%s' has more than one 'struct bpf_spin_lock'\n", |
| 3861 | map->name); |
| 3862 | else if (map->spin_lock_off == -ENOENT) |
| 3863 | verbose(env, |
| 3864 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", |
| 3865 | map->name); |
| 3866 | else |
| 3867 | verbose(env, |
| 3868 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", |
| 3869 | map->name); |
| 3870 | return -EINVAL; |
| 3871 | } |
| 3872 | if (map->spin_lock_off != val + reg->off) { |
| 3873 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", |
| 3874 | val + reg->off); |
| 3875 | return -EINVAL; |
| 3876 | } |
| 3877 | if (is_lock) { |
| 3878 | if (cur->active_spin_lock) { |
| 3879 | verbose(env, |
| 3880 | "Locking two bpf_spin_locks are not allowed\n"); |
| 3881 | return -EINVAL; |
| 3882 | } |
| 3883 | cur->active_spin_lock = reg->id; |
| 3884 | } else { |
| 3885 | if (!cur->active_spin_lock) { |
| 3886 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
| 3887 | return -EINVAL; |
| 3888 | } |
| 3889 | if (cur->active_spin_lock != reg->id) { |
| 3890 | verbose(env, "bpf_spin_unlock of different lock\n"); |
| 3891 | return -EINVAL; |
| 3892 | } |
| 3893 | cur->active_spin_lock = 0; |
| 3894 | } |
| 3895 | return 0; |
| 3896 | } |
| 3897 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 3898 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 3899 | { |
| 3900 | return type == ARG_PTR_TO_MEM || |
| 3901 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 3902 | type == ARG_PTR_TO_UNINIT_MEM; |
| 3903 | } |
| 3904 | |
| 3905 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 3906 | { |
| 3907 | return type == ARG_CONST_SIZE || |
| 3908 | type == ARG_CONST_SIZE_OR_ZERO; |
| 3909 | } |
| 3910 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3911 | static bool arg_type_is_alloc_size(enum bpf_arg_type type) |
| 3912 | { |
| 3913 | return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; |
| 3914 | } |
| 3915 | |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 3916 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
| 3917 | { |
| 3918 | return type == ARG_PTR_TO_INT || |
| 3919 | type == ARG_PTR_TO_LONG; |
| 3920 | } |
| 3921 | |
| 3922 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
| 3923 | { |
| 3924 | if (type == ARG_PTR_TO_INT) |
| 3925 | return sizeof(u32); |
| 3926 | else if (type == ARG_PTR_TO_LONG) |
| 3927 | return sizeof(u64); |
| 3928 | |
| 3929 | return -EINVAL; |
| 3930 | } |
| 3931 | |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 3932 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
| 3933 | const struct bpf_call_arg_meta *meta, |
| 3934 | enum bpf_arg_type *arg_type) |
| 3935 | { |
| 3936 | if (!meta->map_ptr) { |
| 3937 | /* kernel subsystem misconfigured verifier */ |
| 3938 | verbose(env, "invalid map_ptr to access map->type\n"); |
| 3939 | return -EACCES; |
| 3940 | } |
| 3941 | |
| 3942 | switch (meta->map_ptr->map_type) { |
| 3943 | case BPF_MAP_TYPE_SOCKMAP: |
| 3944 | case BPF_MAP_TYPE_SOCKHASH: |
| 3945 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 3946 | *arg_type = ARG_PTR_TO_SOCKET; |
| 3947 | } else { |
| 3948 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); |
| 3949 | return -EINVAL; |
| 3950 | } |
| 3951 | break; |
| 3952 | |
| 3953 | default: |
| 3954 | break; |
| 3955 | } |
| 3956 | return 0; |
| 3957 | } |
| 3958 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 3959 | struct bpf_reg_types { |
| 3960 | const enum bpf_reg_type types[10]; |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 3961 | u32 *btf_id; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 3962 | }; |
| 3963 | |
| 3964 | static const struct bpf_reg_types map_key_value_types = { |
| 3965 | .types = { |
| 3966 | PTR_TO_STACK, |
| 3967 | PTR_TO_PACKET, |
| 3968 | PTR_TO_PACKET_META, |
| 3969 | PTR_TO_MAP_VALUE, |
| 3970 | }, |
| 3971 | }; |
| 3972 | |
| 3973 | static const struct bpf_reg_types sock_types = { |
| 3974 | .types = { |
| 3975 | PTR_TO_SOCK_COMMON, |
| 3976 | PTR_TO_SOCKET, |
| 3977 | PTR_TO_TCP_SOCK, |
| 3978 | PTR_TO_XDP_SOCK, |
| 3979 | }, |
| 3980 | }; |
| 3981 | |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 3982 | static const struct bpf_reg_types btf_id_sock_common_types = { |
| 3983 | .types = { |
| 3984 | PTR_TO_SOCK_COMMON, |
| 3985 | PTR_TO_SOCKET, |
| 3986 | PTR_TO_TCP_SOCK, |
| 3987 | PTR_TO_XDP_SOCK, |
| 3988 | PTR_TO_BTF_ID, |
| 3989 | }, |
| 3990 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], |
| 3991 | }; |
| 3992 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 3993 | static const struct bpf_reg_types mem_types = { |
| 3994 | .types = { |
| 3995 | PTR_TO_STACK, |
| 3996 | PTR_TO_PACKET, |
| 3997 | PTR_TO_PACKET_META, |
| 3998 | PTR_TO_MAP_VALUE, |
| 3999 | PTR_TO_MEM, |
| 4000 | PTR_TO_RDONLY_BUF, |
| 4001 | PTR_TO_RDWR_BUF, |
| 4002 | }, |
| 4003 | }; |
| 4004 | |
| 4005 | static const struct bpf_reg_types int_ptr_types = { |
| 4006 | .types = { |
| 4007 | PTR_TO_STACK, |
| 4008 | PTR_TO_PACKET, |
| 4009 | PTR_TO_PACKET_META, |
| 4010 | PTR_TO_MAP_VALUE, |
| 4011 | }, |
| 4012 | }; |
| 4013 | |
| 4014 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; |
| 4015 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; |
| 4016 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; |
| 4017 | static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } }; |
| 4018 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; |
| 4019 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; |
| 4020 | static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; |
| 4021 | |
Lorenz Bauer | 0789e13b | 2020-09-23 17:01:55 +0100 | [diff] [blame] | 4022 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4023 | [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, |
| 4024 | [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, |
| 4025 | [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, |
| 4026 | [ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types, |
| 4027 | [ARG_CONST_SIZE] = &scalar_types, |
| 4028 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, |
| 4029 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, |
| 4030 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, |
| 4031 | [ARG_PTR_TO_CTX] = &context_types, |
| 4032 | [ARG_PTR_TO_CTX_OR_NULL] = &context_types, |
| 4033 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4034 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4035 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
| 4036 | [ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types, |
| 4037 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
| 4038 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, |
| 4039 | [ARG_PTR_TO_MEM] = &mem_types, |
| 4040 | [ARG_PTR_TO_MEM_OR_NULL] = &mem_types, |
| 4041 | [ARG_PTR_TO_UNINIT_MEM] = &mem_types, |
| 4042 | [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, |
| 4043 | [ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types, |
| 4044 | [ARG_PTR_TO_INT] = &int_ptr_types, |
| 4045 | [ARG_PTR_TO_LONG] = &int_ptr_types, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4046 | }; |
| 4047 | |
| 4048 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4049 | enum bpf_arg_type arg_type, |
| 4050 | const u32 *arg_btf_id) |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4051 | { |
| 4052 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 4053 | enum bpf_reg_type expected, type = reg->type; |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4054 | const struct bpf_reg_types *compatible; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4055 | int i, j; |
| 4056 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4057 | compatible = compatible_reg_types[arg_type]; |
| 4058 | if (!compatible) { |
| 4059 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); |
| 4060 | return -EFAULT; |
| 4061 | } |
| 4062 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4063 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
| 4064 | expected = compatible->types[i]; |
| 4065 | if (expected == NOT_INIT) |
| 4066 | break; |
| 4067 | |
| 4068 | if (type == expected) |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4069 | goto found; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | verbose(env, "R%d type=%s expected=", regno, reg_type_str[type]); |
| 4073 | for (j = 0; j + 1 < i; j++) |
| 4074 | verbose(env, "%s, ", reg_type_str[compatible->types[j]]); |
| 4075 | verbose(env, "%s\n", reg_type_str[compatible->types[j]]); |
| 4076 | return -EACCES; |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4077 | |
| 4078 | found: |
| 4079 | if (type == PTR_TO_BTF_ID) { |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4080 | if (!arg_btf_id) { |
| 4081 | if (!compatible->btf_id) { |
| 4082 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); |
| 4083 | return -EFAULT; |
| 4084 | } |
| 4085 | arg_btf_id = compatible->btf_id; |
| 4086 | } |
| 4087 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4088 | if (!btf_struct_ids_match(&env->log, reg->off, reg->btf_id, |
| 4089 | *arg_btf_id)) { |
| 4090 | verbose(env, "R%d is of type %s but %s is expected\n", |
| 4091 | regno, kernel_type_name(reg->btf_id), |
| 4092 | kernel_type_name(*arg_btf_id)); |
| 4093 | return -EACCES; |
| 4094 | } |
| 4095 | |
| 4096 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 4097 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", |
| 4098 | regno); |
| 4099 | return -EACCES; |
| 4100 | } |
| 4101 | } |
| 4102 | |
| 4103 | return 0; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4104 | } |
| 4105 | |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 4106 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
| 4107 | struct bpf_call_arg_meta *meta, |
| 4108 | const struct bpf_func_proto *fn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4109 | { |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 4110 | u32 regno = BPF_REG_1 + arg; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4111 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 4112 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4113 | enum bpf_reg_type type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4114 | int err = 0; |
| 4115 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 4116 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4117 | return 0; |
| 4118 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4119 | err = check_reg_arg(env, regno, SRC_OP); |
| 4120 | if (err) |
| 4121 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4122 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4123 | if (arg_type == ARG_ANYTHING) { |
| 4124 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4125 | verbose(env, "R%d leaks addr into helper function\n", |
| 4126 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4127 | return -EACCES; |
| 4128 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 4129 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4130 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 4131 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4132 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 4133 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4134 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 4135 | return -EACCES; |
| 4136 | } |
| 4137 | |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 4138 | if (arg_type == ARG_PTR_TO_MAP_VALUE || |
| 4139 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
| 4140 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { |
| 4141 | err = resolve_map_arg_type(env, meta, &arg_type); |
| 4142 | if (err) |
| 4143 | return err; |
| 4144 | } |
| 4145 | |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 4146 | if (register_is_null(reg) && arg_type_may_be_null(arg_type)) |
| 4147 | /* A NULL register has a SCALAR_VALUE type, so skip |
| 4148 | * type checking. |
| 4149 | */ |
| 4150 | goto skip_type_check; |
Jiri Olsa | faaf4a7 | 2020-08-25 21:21:18 +0200 | [diff] [blame] | 4151 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4152 | err = check_reg_type(env, regno, arg_type, fn->arg_btf_id[arg]); |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 4153 | if (err) |
| 4154 | return err; |
| 4155 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 4156 | if (type == PTR_TO_CTX) { |
Lorenz Bauer | feec704 | 2020-09-21 13:12:23 +0100 | [diff] [blame] | 4157 | err = check_ctx_reg(env, reg, regno); |
| 4158 | if (err < 0) |
| 4159 | return err; |
Lorenz Bauer | d7b9454 | 2020-09-21 13:12:21 +0100 | [diff] [blame] | 4160 | } |
| 4161 | |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 4162 | skip_type_check: |
Lorenz Bauer | 02f7c95 | 2020-09-21 13:12:22 +0100 | [diff] [blame] | 4163 | if (reg->ref_obj_id) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4164 | if (meta->ref_obj_id) { |
| 4165 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", |
| 4166 | regno, reg->ref_obj_id, |
| 4167 | meta->ref_obj_id); |
| 4168 | return -EFAULT; |
| 4169 | } |
| 4170 | meta->ref_obj_id = reg->ref_obj_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4171 | } |
| 4172 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4173 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 4174 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4175 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4176 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 4177 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 4178 | * check that [key, key + map->key_size) are within |
| 4179 | * stack limits and initialized |
| 4180 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4181 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4182 | /* in function declaration map_ptr must come before |
| 4183 | * map_key, so that it's verified and known before |
| 4184 | * we have to check map_key here. Otherwise it means |
| 4185 | * that kernel subsystem misconfigured verifier |
| 4186 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4187 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4188 | return -EACCES; |
| 4189 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 4190 | err = check_helper_mem_access(env, regno, |
| 4191 | meta->map_ptr->key_size, false, |
| 4192 | NULL); |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 4193 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 4194 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
| 4195 | !register_is_null(reg)) || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 4196 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4197 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 4198 | * check [value, value + map->value_size) validity |
| 4199 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4200 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4201 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4202 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4203 | return -EACCES; |
| 4204 | } |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 4205 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 4206 | err = check_helper_mem_access(env, regno, |
| 4207 | meta->map_ptr->value_size, false, |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 4208 | meta); |
Lorenz Bauer | c18f0b6 | 2020-09-21 13:12:25 +0100 | [diff] [blame] | 4209 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
| 4210 | if (meta->func_id == BPF_FUNC_spin_lock) { |
| 4211 | if (process_spin_lock(env, regno, true)) |
| 4212 | return -EACCES; |
| 4213 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
| 4214 | if (process_spin_lock(env, regno, false)) |
| 4215 | return -EACCES; |
| 4216 | } else { |
| 4217 | verbose(env, "verifier internal error\n"); |
| 4218 | return -EFAULT; |
| 4219 | } |
Lorenz Bauer | a2bbe7c | 2020-09-21 13:12:24 +0100 | [diff] [blame] | 4220 | } else if (arg_type_is_mem_ptr(arg_type)) { |
| 4221 | /* The access to this pointer is only checked when we hit the |
| 4222 | * next is_mem_size argument below. |
| 4223 | */ |
| 4224 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 4225 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4226 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4227 | |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 4228 | /* This is used to refine r0 return value bounds for helpers |
| 4229 | * that enforce this value as an upper bound on return values. |
| 4230 | * See do_refine_retval_range() for helpers that can refine |
| 4231 | * the return value. C type of helper is u32 so we pull register |
| 4232 | * bound from umax_value however, if negative verifier errors |
| 4233 | * out. Only upper bounds can be learned because retval is an |
| 4234 | * int type and negative retvals are allowed. |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4235 | */ |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 4236 | meta->msize_max_value = reg->umax_value; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4237 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4238 | /* The register is SCALAR_VALUE; the access check |
| 4239 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4240 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4241 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4242 | /* For unprivileged variable accesses, disable raw |
| 4243 | * mode so that the program is required to |
| 4244 | * initialize all the memory that the helper could |
| 4245 | * just partially fill up. |
| 4246 | */ |
| 4247 | meta = NULL; |
| 4248 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4249 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4250 | 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] | 4251 | regno); |
| 4252 | return -EACCES; |
| 4253 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4254 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4255 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4256 | err = check_helper_mem_access(env, regno - 1, 0, |
| 4257 | zero_size_allowed, |
| 4258 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4259 | if (err) |
| 4260 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4261 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4262 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4263 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4264 | 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] | 4265 | regno); |
| 4266 | return -EACCES; |
| 4267 | } |
| 4268 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4269 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4270 | zero_size_allowed, meta); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 4271 | if (!err) |
| 4272 | err = mark_chain_precision(env, regno); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4273 | } else if (arg_type_is_alloc_size(arg_type)) { |
| 4274 | if (!tnum_is_const(reg->var_off)) { |
| 4275 | verbose(env, "R%d unbounded size, use 'var &= const' or 'if (var < const)'\n", |
| 4276 | regno); |
| 4277 | return -EACCES; |
| 4278 | } |
| 4279 | meta->mem_size = reg->var_off.value; |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 4280 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 4281 | int size = int_ptr_type_to_size(arg_type); |
| 4282 | |
| 4283 | err = check_helper_mem_access(env, regno, size, false, meta); |
| 4284 | if (err) |
| 4285 | return err; |
| 4286 | err = check_ptr_alignment(env, reg, 0, size, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4287 | } |
| 4288 | |
| 4289 | return err; |
| 4290 | } |
| 4291 | |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 4292 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
| 4293 | { |
| 4294 | enum bpf_attach_type eatype = env->prog->expected_attach_type; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 4295 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 4296 | |
| 4297 | if (func_id != BPF_FUNC_map_update_elem) |
| 4298 | return false; |
| 4299 | |
| 4300 | /* It's not possible to get access to a locked struct sock in these |
| 4301 | * contexts, so updating is safe. |
| 4302 | */ |
| 4303 | switch (type) { |
| 4304 | case BPF_PROG_TYPE_TRACING: |
| 4305 | if (eatype == BPF_TRACE_ITER) |
| 4306 | return true; |
| 4307 | break; |
| 4308 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 4309 | case BPF_PROG_TYPE_SCHED_CLS: |
| 4310 | case BPF_PROG_TYPE_SCHED_ACT: |
| 4311 | case BPF_PROG_TYPE_XDP: |
| 4312 | case BPF_PROG_TYPE_SK_REUSEPORT: |
| 4313 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
| 4314 | case BPF_PROG_TYPE_SK_LOOKUP: |
| 4315 | return true; |
| 4316 | default: |
| 4317 | break; |
| 4318 | } |
| 4319 | |
| 4320 | verbose(env, "cannot update sockmap in this context\n"); |
| 4321 | return false; |
| 4322 | } |
| 4323 | |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 4324 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
| 4325 | { |
| 4326 | return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); |
| 4327 | } |
| 4328 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4329 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 4330 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 4331 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 4332 | if (!map) |
| 4333 | return 0; |
| 4334 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4335 | /* We need a two way check, first is from map perspective ... */ |
| 4336 | switch (map->map_type) { |
| 4337 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 4338 | if (func_id != BPF_FUNC_tail_call) |
| 4339 | goto error; |
| 4340 | break; |
| 4341 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 4342 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 4343 | func_id != BPF_FUNC_perf_event_output && |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 4344 | func_id != BPF_FUNC_skb_output && |
Eelco Chaudron | d831ee8 | 2020-03-06 08:59:23 +0000 | [diff] [blame] | 4345 | func_id != BPF_FUNC_perf_event_read_value && |
| 4346 | func_id != BPF_FUNC_xdp_output) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4347 | goto error; |
| 4348 | break; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4349 | case BPF_MAP_TYPE_RINGBUF: |
| 4350 | if (func_id != BPF_FUNC_ringbuf_output && |
| 4351 | func_id != BPF_FUNC_ringbuf_reserve && |
| 4352 | func_id != BPF_FUNC_ringbuf_submit && |
| 4353 | func_id != BPF_FUNC_ringbuf_discard && |
| 4354 | func_id != BPF_FUNC_ringbuf_query) |
| 4355 | goto error; |
| 4356 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4357 | case BPF_MAP_TYPE_STACK_TRACE: |
| 4358 | if (func_id != BPF_FUNC_get_stackid) |
| 4359 | goto error; |
| 4360 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 4361 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 4362 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 4363 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 4364 | goto error; |
| 4365 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 4366 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 4367 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 4368 | if (func_id != BPF_FUNC_get_local_storage) |
| 4369 | goto error; |
| 4370 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 4371 | case BPF_MAP_TYPE_DEVMAP: |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 4372 | case BPF_MAP_TYPE_DEVMAP_HASH: |
Toke Høiland-Jørgensen | 0cdbb4b | 2019-06-28 11:12:35 +0200 | [diff] [blame] | 4373 | if (func_id != BPF_FUNC_redirect_map && |
| 4374 | func_id != BPF_FUNC_map_lookup_elem) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 4375 | goto error; |
| 4376 | break; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 4377 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 4378 | * appear. |
| 4379 | */ |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 4380 | case BPF_MAP_TYPE_CPUMAP: |
| 4381 | if (func_id != BPF_FUNC_redirect_map) |
| 4382 | goto error; |
| 4383 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 4384 | case BPF_MAP_TYPE_XSKMAP: |
| 4385 | if (func_id != BPF_FUNC_redirect_map && |
| 4386 | func_id != BPF_FUNC_map_lookup_elem) |
| 4387 | goto error; |
| 4388 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4389 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 4390 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4391 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 4392 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 4393 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4394 | case BPF_MAP_TYPE_SOCKMAP: |
| 4395 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 4396 | func_id != BPF_FUNC_sock_map_update && |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 4397 | func_id != BPF_FUNC_map_delete_elem && |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 4398 | func_id != BPF_FUNC_msg_redirect_map && |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 4399 | func_id != BPF_FUNC_sk_select_reuseport && |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 4400 | func_id != BPF_FUNC_map_lookup_elem && |
| 4401 | !may_update_sockmap(env, func_id)) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4402 | goto error; |
| 4403 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 4404 | case BPF_MAP_TYPE_SOCKHASH: |
| 4405 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 4406 | func_id != BPF_FUNC_sock_hash_update && |
| 4407 | func_id != BPF_FUNC_map_delete_elem && |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 4408 | func_id != BPF_FUNC_msg_redirect_hash && |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 4409 | func_id != BPF_FUNC_sk_select_reuseport && |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 4410 | func_id != BPF_FUNC_map_lookup_elem && |
| 4411 | !may_update_sockmap(env, func_id)) |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 4412 | goto error; |
| 4413 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 4414 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 4415 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 4416 | goto error; |
| 4417 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 4418 | case BPF_MAP_TYPE_QUEUE: |
| 4419 | case BPF_MAP_TYPE_STACK: |
| 4420 | if (func_id != BPF_FUNC_map_peek_elem && |
| 4421 | func_id != BPF_FUNC_map_pop_elem && |
| 4422 | func_id != BPF_FUNC_map_push_elem) |
| 4423 | goto error; |
| 4424 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 4425 | case BPF_MAP_TYPE_SK_STORAGE: |
| 4426 | if (func_id != BPF_FUNC_sk_storage_get && |
| 4427 | func_id != BPF_FUNC_sk_storage_delete) |
| 4428 | goto error; |
| 4429 | break; |
KP Singh | 8ea6368 | 2020-08-25 20:29:17 +0200 | [diff] [blame] | 4430 | case BPF_MAP_TYPE_INODE_STORAGE: |
| 4431 | if (func_id != BPF_FUNC_inode_storage_get && |
| 4432 | func_id != BPF_FUNC_inode_storage_delete) |
| 4433 | goto error; |
| 4434 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4435 | default: |
| 4436 | break; |
| 4437 | } |
| 4438 | |
| 4439 | /* ... and second from the function itself. */ |
| 4440 | switch (func_id) { |
| 4441 | case BPF_FUNC_tail_call: |
| 4442 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 4443 | goto error; |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 4444 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
| 4445 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4446 | return -EINVAL; |
| 4447 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4448 | break; |
| 4449 | case BPF_FUNC_perf_event_read: |
| 4450 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 4451 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 4452 | case BPF_FUNC_skb_output: |
Eelco Chaudron | d831ee8 | 2020-03-06 08:59:23 +0000 | [diff] [blame] | 4453 | case BPF_FUNC_xdp_output: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4454 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 4455 | goto error; |
| 4456 | break; |
| 4457 | case BPF_FUNC_get_stackid: |
| 4458 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 4459 | goto error; |
| 4460 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 4461 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 4462 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 4463 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 4464 | goto error; |
| 4465 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 4466 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 4467 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 4468 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 4469 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 4470 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 4471 | goto error; |
| 4472 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4473 | case BPF_FUNC_sk_redirect_map: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 4474 | case BPF_FUNC_msg_redirect_map: |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 4475 | case BPF_FUNC_sock_map_update: |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4476 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 4477 | goto error; |
| 4478 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 4479 | case BPF_FUNC_sk_redirect_hash: |
| 4480 | case BPF_FUNC_msg_redirect_hash: |
| 4481 | case BPF_FUNC_sock_hash_update: |
| 4482 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 4483 | goto error; |
| 4484 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 4485 | case BPF_FUNC_get_local_storage: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 4486 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 4487 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 4488 | goto error; |
| 4489 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 4490 | case BPF_FUNC_sk_select_reuseport: |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 4491 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
| 4492 | map->map_type != BPF_MAP_TYPE_SOCKMAP && |
| 4493 | map->map_type != BPF_MAP_TYPE_SOCKHASH) |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 4494 | goto error; |
| 4495 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 4496 | case BPF_FUNC_map_peek_elem: |
| 4497 | case BPF_FUNC_map_pop_elem: |
| 4498 | case BPF_FUNC_map_push_elem: |
| 4499 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 4500 | map->map_type != BPF_MAP_TYPE_STACK) |
| 4501 | goto error; |
| 4502 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 4503 | case BPF_FUNC_sk_storage_get: |
| 4504 | case BPF_FUNC_sk_storage_delete: |
| 4505 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
| 4506 | goto error; |
| 4507 | break; |
KP Singh | 8ea6368 | 2020-08-25 20:29:17 +0200 | [diff] [blame] | 4508 | case BPF_FUNC_inode_storage_get: |
| 4509 | case BPF_FUNC_inode_storage_delete: |
| 4510 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) |
| 4511 | goto error; |
| 4512 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4513 | default: |
| 4514 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 4515 | } |
| 4516 | |
| 4517 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4518 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4519 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 4520 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 4521 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 4522 | } |
| 4523 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 4524 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4525 | { |
| 4526 | int count = 0; |
| 4527 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4528 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4529 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4530 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4531 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4532 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4533 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4534 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4535 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 4536 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4537 | count++; |
| 4538 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 4539 | /* We only support one arg being in raw mode at the moment, |
| 4540 | * which is sufficient for the helper functions we have |
| 4541 | * right now. |
| 4542 | */ |
| 4543 | return count <= 1; |
| 4544 | } |
| 4545 | |
| 4546 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 4547 | enum bpf_arg_type arg_next) |
| 4548 | { |
| 4549 | return (arg_type_is_mem_ptr(arg_curr) && |
| 4550 | !arg_type_is_mem_size(arg_next)) || |
| 4551 | (!arg_type_is_mem_ptr(arg_curr) && |
| 4552 | arg_type_is_mem_size(arg_next)); |
| 4553 | } |
| 4554 | |
| 4555 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 4556 | { |
| 4557 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 4558 | * bytes from memory 'buf'. Both arg types need |
| 4559 | * to be paired, so make sure there's no buggy |
| 4560 | * helper function specification. |
| 4561 | */ |
| 4562 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 4563 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 4564 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 4565 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 4566 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 4567 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 4568 | return false; |
| 4569 | |
| 4570 | return true; |
| 4571 | } |
| 4572 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4573 | 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] | 4574 | { |
| 4575 | int count = 0; |
| 4576 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4577 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4578 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4579 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4580 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4581 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4582 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4583 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4584 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4585 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4586 | count++; |
| 4587 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4588 | /* A reference acquiring function cannot acquire |
| 4589 | * another refcounted ptr. |
| 4590 | */ |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 4591 | if (may_be_acquire_function(func_id) && count) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4592 | return false; |
| 4593 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4594 | /* We only support one arg being unreferenced at the moment, |
| 4595 | * which is sufficient for the helper functions we have right now. |
| 4596 | */ |
| 4597 | return count <= 1; |
| 4598 | } |
| 4599 | |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 4600 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
| 4601 | { |
| 4602 | int i; |
| 4603 | |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4604 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 4605 | if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) |
| 4606 | return false; |
| 4607 | |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 4608 | if (fn->arg_type[i] != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i]) |
| 4609 | return false; |
| 4610 | } |
| 4611 | |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 4612 | return true; |
| 4613 | } |
| 4614 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4615 | 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] | 4616 | { |
| 4617 | return check_raw_mode_ok(fn) && |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4618 | check_arg_pair_ok(fn) && |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 4619 | check_btf_id_ok(fn) && |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4620 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4621 | } |
| 4622 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4623 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 4624 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4625 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4626 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 4627 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4628 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4629 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4630 | int i; |
| 4631 | |
| 4632 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4633 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4634 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4635 | |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 4636 | bpf_for_each_spilled_reg(i, state, reg) { |
| 4637 | if (!reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4638 | continue; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4639 | if (reg_is_pkt_pointer_any(reg)) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 4640 | __mark_reg_unknown(env, reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4641 | } |
| 4642 | } |
| 4643 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4644 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 4645 | { |
| 4646 | struct bpf_verifier_state *vstate = env->cur_state; |
| 4647 | int i; |
| 4648 | |
| 4649 | for (i = 0; i <= vstate->curframe; i++) |
| 4650 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 4651 | } |
| 4652 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4653 | static void release_reg_references(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4654 | struct bpf_func_state *state, |
| 4655 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4656 | { |
| 4657 | struct bpf_reg_state *regs = state->regs, *reg; |
| 4658 | int i; |
| 4659 | |
| 4660 | for (i = 0; i < MAX_BPF_REG; i++) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4661 | if (regs[i].ref_obj_id == ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4662 | mark_reg_unknown(env, regs, i); |
| 4663 | |
| 4664 | bpf_for_each_spilled_reg(i, state, reg) { |
| 4665 | if (!reg) |
| 4666 | continue; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4667 | if (reg->ref_obj_id == ref_obj_id) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 4668 | __mark_reg_unknown(env, reg); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4669 | } |
| 4670 | } |
| 4671 | |
| 4672 | /* The pointer with the specified id has released its reference to kernel |
| 4673 | * resources. Identify all copies of the same pointer and clear the reference. |
| 4674 | */ |
| 4675 | static int release_reference(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4676 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4677 | { |
| 4678 | struct bpf_verifier_state *vstate = env->cur_state; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4679 | int err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4680 | int i; |
| 4681 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4682 | err = release_reference_state(cur_func(env), ref_obj_id); |
| 4683 | if (err) |
| 4684 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4685 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4686 | for (i = 0; i <= vstate->curframe; i++) |
| 4687 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
| 4688 | |
| 4689 | return 0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4690 | } |
| 4691 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4692 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
| 4693 | struct bpf_reg_state *regs) |
| 4694 | { |
| 4695 | int i; |
| 4696 | |
| 4697 | /* after the call registers r0 - r5 were scratched */ |
| 4698 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 4699 | mark_reg_not_init(env, regs, caller_saved[i]); |
| 4700 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 4701 | } |
| 4702 | } |
| 4703 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4704 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 4705 | int *insn_idx) |
| 4706 | { |
| 4707 | struct bpf_verifier_state *state = env->cur_state; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4708 | struct bpf_func_info_aux *func_info_aux; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4709 | struct bpf_func_state *caller, *callee; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4710 | int i, err, subprog, target_insn; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4711 | bool is_global = false; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4712 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 4713 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4714 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 4715 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4716 | return -E2BIG; |
| 4717 | } |
| 4718 | |
| 4719 | target_insn = *insn_idx + insn->imm; |
| 4720 | subprog = find_subprog(env, target_insn + 1); |
| 4721 | if (subprog < 0) { |
| 4722 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 4723 | target_insn + 1); |
| 4724 | return -EFAULT; |
| 4725 | } |
| 4726 | |
| 4727 | caller = state->frame[state->curframe]; |
| 4728 | if (state->frame[state->curframe + 1]) { |
| 4729 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 4730 | state->curframe + 1); |
| 4731 | return -EFAULT; |
| 4732 | } |
| 4733 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4734 | func_info_aux = env->prog->aux->func_info_aux; |
| 4735 | if (func_info_aux) |
| 4736 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; |
| 4737 | err = btf_check_func_arg_match(env, subprog, caller->regs); |
| 4738 | if (err == -EFAULT) |
| 4739 | return err; |
| 4740 | if (is_global) { |
| 4741 | if (err) { |
| 4742 | verbose(env, "Caller passes invalid args into func#%d\n", |
| 4743 | subprog); |
| 4744 | return err; |
| 4745 | } else { |
| 4746 | if (env->log.level & BPF_LOG_LEVEL) |
| 4747 | verbose(env, |
| 4748 | "Func#%d is global and valid. Skipping.\n", |
| 4749 | subprog); |
| 4750 | clear_caller_saved_regs(env, caller->regs); |
| 4751 | |
| 4752 | /* All global functions return SCALAR_VALUE */ |
| 4753 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
| 4754 | |
| 4755 | /* continue with next insn after call */ |
| 4756 | return 0; |
| 4757 | } |
| 4758 | } |
| 4759 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4760 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 4761 | if (!callee) |
| 4762 | return -ENOMEM; |
| 4763 | state->frame[state->curframe + 1] = callee; |
| 4764 | |
| 4765 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 4766 | * into its own stack before reading from it. |
| 4767 | * callee can read/write into caller's stack |
| 4768 | */ |
| 4769 | init_func_state(env, callee, |
| 4770 | /* remember the callsite, it will be used by bpf_exit */ |
| 4771 | *insn_idx /* callsite */, |
| 4772 | state->curframe + 1 /* frameno within this callchain */, |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 4773 | subprog /* subprog number within this prog */); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4774 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4775 | /* Transfer references to the callee */ |
| 4776 | err = transfer_reference_state(callee, caller); |
| 4777 | if (err) |
| 4778 | return err; |
| 4779 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 4780 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 4781 | * pointers, which connects us up to the liveness chain |
| 4782 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4783 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 4784 | callee->regs[i] = caller->regs[i]; |
| 4785 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4786 | clear_caller_saved_regs(env, caller->regs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4787 | |
| 4788 | /* only increment it after check_reg_arg() finished */ |
| 4789 | state->curframe++; |
| 4790 | |
| 4791 | /* and go analyze first insn of the callee */ |
| 4792 | *insn_idx = target_insn; |
| 4793 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 4794 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4795 | verbose(env, "caller:\n"); |
| 4796 | print_verifier_state(env, caller); |
| 4797 | verbose(env, "callee:\n"); |
| 4798 | print_verifier_state(env, callee); |
| 4799 | } |
| 4800 | return 0; |
| 4801 | } |
| 4802 | |
| 4803 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 4804 | { |
| 4805 | struct bpf_verifier_state *state = env->cur_state; |
| 4806 | struct bpf_func_state *caller, *callee; |
| 4807 | struct bpf_reg_state *r0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4808 | int err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4809 | |
| 4810 | callee = state->frame[state->curframe]; |
| 4811 | r0 = &callee->regs[BPF_REG_0]; |
| 4812 | if (r0->type == PTR_TO_STACK) { |
| 4813 | /* technically it's ok to return caller's stack pointer |
| 4814 | * (or caller's caller's pointer) back to the caller, |
| 4815 | * since these pointers are valid. Only current stack |
| 4816 | * pointer will be invalid as soon as function exits, |
| 4817 | * but let's be conservative |
| 4818 | */ |
| 4819 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 4820 | return -EINVAL; |
| 4821 | } |
| 4822 | |
| 4823 | state->curframe--; |
| 4824 | caller = state->frame[state->curframe]; |
| 4825 | /* return to the caller whatever r0 had in the callee */ |
| 4826 | caller->regs[BPF_REG_0] = *r0; |
| 4827 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4828 | /* Transfer references to the caller */ |
| 4829 | err = transfer_reference_state(caller, callee); |
| 4830 | if (err) |
| 4831 | return err; |
| 4832 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4833 | *insn_idx = callee->callsite + 1; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 4834 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4835 | verbose(env, "returning from callee:\n"); |
| 4836 | print_verifier_state(env, callee); |
| 4837 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 4838 | print_verifier_state(env, caller); |
| 4839 | } |
| 4840 | /* clear everything in the callee */ |
| 4841 | free_func_state(callee); |
| 4842 | state->frame[state->curframe + 1] = NULL; |
| 4843 | return 0; |
| 4844 | } |
| 4845 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4846 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 4847 | int func_id, |
| 4848 | struct bpf_call_arg_meta *meta) |
| 4849 | { |
| 4850 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 4851 | |
| 4852 | if (ret_type != RET_INTEGER || |
| 4853 | (func_id != BPF_FUNC_get_stack && |
Daniel Borkmann | 47cc0ed | 2020-05-15 12:11:17 +0200 | [diff] [blame] | 4854 | func_id != BPF_FUNC_probe_read_str && |
| 4855 | func_id != BPF_FUNC_probe_read_kernel_str && |
| 4856 | func_id != BPF_FUNC_probe_read_user_str)) |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4857 | return; |
| 4858 | |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 4859 | ret_reg->smax_value = meta->msize_max_value; |
John Fastabend | fa123ac | 2020-03-30 14:36:59 -0700 | [diff] [blame] | 4860 | ret_reg->s32_max_value = meta->msize_max_value; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4861 | __reg_deduce_bounds(ret_reg); |
| 4862 | __reg_bound_offset(ret_reg); |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 4863 | __update_reg_bounds(ret_reg); |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 4864 | } |
| 4865 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4866 | static int |
| 4867 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 4868 | int func_id, int insn_idx) |
| 4869 | { |
| 4870 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4871 | struct bpf_map *map = meta->map_ptr; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4872 | |
| 4873 | if (func_id != BPF_FUNC_tail_call && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 4874 | func_id != BPF_FUNC_map_lookup_elem && |
| 4875 | func_id != BPF_FUNC_map_update_elem && |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 4876 | func_id != BPF_FUNC_map_delete_elem && |
| 4877 | func_id != BPF_FUNC_map_push_elem && |
| 4878 | func_id != BPF_FUNC_map_pop_elem && |
| 4879 | func_id != BPF_FUNC_map_peek_elem) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4880 | return 0; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 4881 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4882 | if (map == NULL) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4883 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 4884 | return -EINVAL; |
| 4885 | } |
| 4886 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4887 | /* In case of read-only, some additional restrictions |
| 4888 | * need to be applied in order to prevent altering the |
| 4889 | * state of the map from program side. |
| 4890 | */ |
| 4891 | if ((map->map_flags & BPF_F_RDONLY_PROG) && |
| 4892 | (func_id == BPF_FUNC_map_delete_elem || |
| 4893 | func_id == BPF_FUNC_map_update_elem || |
| 4894 | func_id == BPF_FUNC_map_push_elem || |
| 4895 | func_id == BPF_FUNC_map_pop_elem)) { |
| 4896 | verbose(env, "write into map forbidden\n"); |
| 4897 | return -EACCES; |
| 4898 | } |
| 4899 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4900 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4901 | bpf_map_ptr_store(aux, meta->map_ptr, |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 4902 | !meta->map_ptr->bypass_spec_v1); |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4903 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4904 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 4905 | !meta->map_ptr->bypass_spec_v1); |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 4906 | return 0; |
| 4907 | } |
| 4908 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4909 | static int |
| 4910 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 4911 | int func_id, int insn_idx) |
| 4912 | { |
| 4913 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
| 4914 | struct bpf_reg_state *regs = cur_regs(env), *reg; |
| 4915 | struct bpf_map *map = meta->map_ptr; |
| 4916 | struct tnum range; |
| 4917 | u64 val; |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 4918 | int err; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4919 | |
| 4920 | if (func_id != BPF_FUNC_tail_call) |
| 4921 | return 0; |
| 4922 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { |
| 4923 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 4924 | return -EINVAL; |
| 4925 | } |
| 4926 | |
| 4927 | range = tnum_range(0, map->max_entries - 1); |
| 4928 | reg = ®s[BPF_REG_3]; |
| 4929 | |
| 4930 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { |
| 4931 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
| 4932 | return 0; |
| 4933 | } |
| 4934 | |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 4935 | err = mark_chain_precision(env, BPF_REG_3); |
| 4936 | if (err) |
| 4937 | return err; |
| 4938 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 4939 | val = reg->var_off.value; |
| 4940 | if (bpf_map_key_unseen(aux)) |
| 4941 | bpf_map_key_store(aux, val); |
| 4942 | else if (!bpf_map_key_poisoned(aux) && |
| 4943 | bpf_map_key_immediate(aux) != val) |
| 4944 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
| 4945 | return 0; |
| 4946 | } |
| 4947 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4948 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 4949 | { |
| 4950 | struct bpf_func_state *state = cur_func(env); |
| 4951 | int i; |
| 4952 | |
| 4953 | for (i = 0; i < state->acquired_refs; i++) { |
| 4954 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 4955 | state->refs[i].id, state->refs[i].insn_idx); |
| 4956 | } |
| 4957 | return state->acquired_refs ? -EINVAL : 0; |
| 4958 | } |
| 4959 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4960 | 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] | 4961 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4962 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4963 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 4964 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4965 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4966 | int i, err; |
| 4967 | |
| 4968 | /* find function prototype */ |
| 4969 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4970 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 4971 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4972 | return -EINVAL; |
| 4973 | } |
| 4974 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 4975 | if (env->ops->get_func_proto) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 4976 | fn = env->ops->get_func_proto(func_id, env->prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4977 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4978 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 4979 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4980 | return -EINVAL; |
| 4981 | } |
| 4982 | |
| 4983 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 4984 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Daniel Borkmann | 3fe2867 | 2018-06-02 23:06:33 +0200 | [diff] [blame] | 4985 | 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] | 4986 | return -EINVAL; |
| 4987 | } |
| 4988 | |
Jiri Olsa | eae2e83 | 2020-08-25 21:21:19 +0200 | [diff] [blame] | 4989 | if (fn->allowed && !fn->allowed(env->prog)) { |
| 4990 | verbose(env, "helper call is not allowed in probe\n"); |
| 4991 | return -EINVAL; |
| 4992 | } |
| 4993 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 4994 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 4995 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 4996 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 4997 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 4998 | func_id_name(func_id), func_id); |
| 4999 | return -EINVAL; |
| 5000 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5001 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5002 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5003 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5004 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5005 | err = check_func_proto(fn, func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5006 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5007 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 5008 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5009 | return err; |
| 5010 | } |
| 5011 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 5012 | meta.func_id = func_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5013 | /* check args */ |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 5014 | for (i = 0; i < 5; i++) { |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5015 | err = check_func_arg(env, i, &meta, fn); |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 5016 | if (err) |
| 5017 | return err; |
| 5018 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5019 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 5020 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 5021 | if (err) |
| 5022 | return err; |
| 5023 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 5024 | err = record_func_key(env, &meta, func_id, insn_idx); |
| 5025 | if (err) |
| 5026 | return err; |
| 5027 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5028 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 5029 | * is inferred from register state. |
| 5030 | */ |
| 5031 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 5032 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 5033 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5034 | if (err) |
| 5035 | return err; |
| 5036 | } |
| 5037 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5038 | if (func_id == BPF_FUNC_tail_call) { |
| 5039 | err = check_reference_leak(env); |
| 5040 | if (err) { |
| 5041 | verbose(env, "tail_call would lead to reference leak\n"); |
| 5042 | return err; |
| 5043 | } |
| 5044 | } else if (is_release_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5045 | err = release_reference(env, meta.ref_obj_id); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 5046 | if (err) { |
| 5047 | verbose(env, "func %s#%d reference has not been acquired before\n", |
| 5048 | func_id_name(func_id), func_id); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5049 | return err; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 5050 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5051 | } |
| 5052 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5053 | regs = cur_regs(env); |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 5054 | |
| 5055 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 5056 | * this is required because get_local_storage() can't return an error. |
| 5057 | */ |
| 5058 | if (func_id == BPF_FUNC_get_local_storage && |
| 5059 | !register_is_null(®s[BPF_REG_2])) { |
| 5060 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 5061 | return -EINVAL; |
| 5062 | } |
| 5063 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5064 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5065 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5066 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5067 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 5068 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5069 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 5070 | /* helper call returns 64-bit value. */ |
| 5071 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 5072 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5073 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5074 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5075 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5076 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5077 | } else if (fn->ret_type == RET_VOID) { |
| 5078 | regs[BPF_REG_0].type = NOT_INIT; |
Roman Gushchin | 3e6a4b3 | 2018-08-02 14:27:22 -0700 | [diff] [blame] | 5079 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 5080 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5081 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5082 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5083 | /* remember map_ptr, so that check_map_access() |
| 5084 | * can check 'value_size' boundary of memory access |
| 5085 | * to map element returned from bpf_map_lookup_elem() |
| 5086 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5087 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5088 | verbose(env, |
| 5089 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5090 | return -EINVAL; |
| 5091 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5092 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 5093 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 5094 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
Alexei Starovoitov | e16d2f1 | 2019-01-31 15:40:05 -0800 | [diff] [blame] | 5095 | if (map_value_has_spin_lock(meta.map_ptr)) |
| 5096 | regs[BPF_REG_0].id = ++env->id_gen; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 5097 | } else { |
| 5098 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
| 5099 | regs[BPF_REG_0].id = ++env->id_gen; |
| 5100 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5101 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
| 5102 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5103 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 5104 | regs[BPF_REG_0].id = ++env->id_gen; |
Lorenz Bauer | 85a51f8 | 2019-03-22 09:54:00 +0800 | [diff] [blame] | 5105 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
| 5106 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5107 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; |
| 5108 | regs[BPF_REG_0].id = ++env->id_gen; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 5109 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
| 5110 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5111 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; |
| 5112 | regs[BPF_REG_0].id = ++env->id_gen; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5113 | } else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) { |
| 5114 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5115 | regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL; |
| 5116 | regs[BPF_REG_0].id = ++env->id_gen; |
| 5117 | regs[BPF_REG_0].mem_size = meta.mem_size; |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5118 | } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL) { |
| 5119 | int ret_btf_id; |
| 5120 | |
| 5121 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 5122 | regs[BPF_REG_0].type = PTR_TO_BTF_ID_OR_NULL; |
| 5123 | ret_btf_id = *fn->ret_btf_id; |
| 5124 | if (ret_btf_id == 0) { |
| 5125 | verbose(env, "invalid return type %d of func %s#%d\n", |
| 5126 | fn->ret_type, func_id_name(func_id), func_id); |
| 5127 | return -EINVAL; |
| 5128 | } |
| 5129 | regs[BPF_REG_0].btf_id = ret_btf_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5130 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5131 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 5132 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5133 | return -EINVAL; |
| 5134 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 5135 | |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 5136 | if (is_ptr_cast_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5137 | /* For release_reference() */ |
| 5138 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 5139 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 5140 | int id = acquire_reference_state(env, insn_idx); |
| 5141 | |
| 5142 | if (id < 0) |
| 5143 | return id; |
| 5144 | /* For mark_ptr_or_null_reg() */ |
| 5145 | regs[BPF_REG_0].id = id; |
| 5146 | /* For release_reference() */ |
| 5147 | regs[BPF_REG_0].ref_obj_id = id; |
| 5148 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5149 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 5150 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
| 5151 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5152 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 5153 | if (err) |
| 5154 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 5155 | |
Song Liu | fa28dcb | 2020-06-29 23:28:44 -0700 | [diff] [blame] | 5156 | if ((func_id == BPF_FUNC_get_stack || |
| 5157 | func_id == BPF_FUNC_get_task_stack) && |
| 5158 | !env->prog->has_callchain_buf) { |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 5159 | const char *err_str; |
| 5160 | |
| 5161 | #ifdef CONFIG_PERF_EVENTS |
| 5162 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 5163 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 5164 | #else |
| 5165 | err = -ENOTSUPP; |
| 5166 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 5167 | #endif |
| 5168 | if (err) { |
| 5169 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 5170 | return err; |
| 5171 | } |
| 5172 | |
| 5173 | env->prog->has_callchain_buf = true; |
| 5174 | } |
| 5175 | |
Song Liu | 5d99cb2c | 2020-07-23 11:06:45 -0700 | [diff] [blame] | 5176 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
| 5177 | env->prog->call_get_stack = true; |
| 5178 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5179 | if (changes_data) |
| 5180 | clear_all_pkt_pointers(env); |
| 5181 | return 0; |
| 5182 | } |
| 5183 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5184 | static bool signed_add_overflows(s64 a, s64 b) |
| 5185 | { |
| 5186 | /* Do the add in u64, where overflow is well-defined */ |
| 5187 | s64 res = (s64)((u64)a + (u64)b); |
| 5188 | |
| 5189 | if (b < 0) |
| 5190 | return res > a; |
| 5191 | return res < a; |
| 5192 | } |
| 5193 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5194 | static bool signed_add32_overflows(s64 a, s64 b) |
| 5195 | { |
| 5196 | /* Do the add in u32, where overflow is well-defined */ |
| 5197 | s32 res = (s32)((u32)a + (u32)b); |
| 5198 | |
| 5199 | if (b < 0) |
| 5200 | return res > a; |
| 5201 | return res < a; |
| 5202 | } |
| 5203 | |
| 5204 | static bool signed_sub_overflows(s32 a, s32 b) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5205 | { |
| 5206 | /* Do the sub in u64, where overflow is well-defined */ |
| 5207 | s64 res = (s64)((u64)a - (u64)b); |
| 5208 | |
| 5209 | if (b < 0) |
| 5210 | return res < a; |
| 5211 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 5212 | } |
| 5213 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5214 | static bool signed_sub32_overflows(s32 a, s32 b) |
| 5215 | { |
| 5216 | /* Do the sub in u64, where overflow is well-defined */ |
| 5217 | s32 res = (s32)((u32)a - (u32)b); |
| 5218 | |
| 5219 | if (b < 0) |
| 5220 | return res < a; |
| 5221 | return res > a; |
| 5222 | } |
| 5223 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 5224 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 5225 | const struct bpf_reg_state *reg, |
| 5226 | enum bpf_reg_type type) |
| 5227 | { |
| 5228 | bool known = tnum_is_const(reg->var_off); |
| 5229 | s64 val = reg->var_off.value; |
| 5230 | s64 smin = reg->smin_value; |
| 5231 | |
| 5232 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 5233 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 5234 | reg_type_str[type], val); |
| 5235 | return false; |
| 5236 | } |
| 5237 | |
| 5238 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 5239 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 5240 | reg_type_str[type], reg->off); |
| 5241 | return false; |
| 5242 | } |
| 5243 | |
| 5244 | if (smin == S64_MIN) { |
| 5245 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 5246 | reg_type_str[type]); |
| 5247 | return false; |
| 5248 | } |
| 5249 | |
| 5250 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 5251 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 5252 | smin, reg_type_str[type]); |
| 5253 | return false; |
| 5254 | } |
| 5255 | |
| 5256 | return true; |
| 5257 | } |
| 5258 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5259 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
| 5260 | { |
| 5261 | return &env->insn_aux_data[env->insn_idx]; |
| 5262 | } |
| 5263 | |
| 5264 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
| 5265 | u32 *ptr_limit, u8 opcode, bool off_is_neg) |
| 5266 | { |
| 5267 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || |
| 5268 | (opcode == BPF_SUB && !off_is_neg); |
| 5269 | u32 off; |
| 5270 | |
| 5271 | switch (ptr_reg->type) { |
| 5272 | case PTR_TO_STACK: |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 5273 | /* Indirect variable offset stack access is prohibited in |
| 5274 | * unprivileged mode so it's not handled here. |
| 5275 | */ |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5276 | off = ptr_reg->off + ptr_reg->var_off.value; |
| 5277 | if (mask_to_left) |
| 5278 | *ptr_limit = MAX_BPF_STACK + off; |
| 5279 | else |
| 5280 | *ptr_limit = -off; |
| 5281 | return 0; |
| 5282 | case PTR_TO_MAP_VALUE: |
| 5283 | if (mask_to_left) { |
| 5284 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; |
| 5285 | } else { |
| 5286 | off = ptr_reg->smin_value + ptr_reg->off; |
| 5287 | *ptr_limit = ptr_reg->map_ptr->value_size - off; |
| 5288 | } |
| 5289 | return 0; |
| 5290 | default: |
| 5291 | return -EINVAL; |
| 5292 | } |
| 5293 | } |
| 5294 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 5295 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
| 5296 | const struct bpf_insn *insn) |
| 5297 | { |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 5298 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 5299 | } |
| 5300 | |
| 5301 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, |
| 5302 | u32 alu_state, u32 alu_limit) |
| 5303 | { |
| 5304 | /* If we arrived here from different branches with different |
| 5305 | * state or limits to sanitize, then this won't work. |
| 5306 | */ |
| 5307 | if (aux->alu_state && |
| 5308 | (aux->alu_state != alu_state || |
| 5309 | aux->alu_limit != alu_limit)) |
| 5310 | return -EACCES; |
| 5311 | |
| 5312 | /* Corresponding fixup done in fixup_bpf_calls(). */ |
| 5313 | aux->alu_state = alu_state; |
| 5314 | aux->alu_limit = alu_limit; |
| 5315 | return 0; |
| 5316 | } |
| 5317 | |
| 5318 | static int sanitize_val_alu(struct bpf_verifier_env *env, |
| 5319 | struct bpf_insn *insn) |
| 5320 | { |
| 5321 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 5322 | |
| 5323 | if (can_skip_alu_sanitation(env, insn)) |
| 5324 | return 0; |
| 5325 | |
| 5326 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); |
| 5327 | } |
| 5328 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5329 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
| 5330 | struct bpf_insn *insn, |
| 5331 | const struct bpf_reg_state *ptr_reg, |
| 5332 | struct bpf_reg_state *dst_reg, |
| 5333 | bool off_is_neg) |
| 5334 | { |
| 5335 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5336 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 5337 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
| 5338 | u8 opcode = BPF_OP(insn->code); |
| 5339 | u32 alu_state, alu_limit; |
| 5340 | struct bpf_reg_state tmp; |
| 5341 | bool ret; |
| 5342 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 5343 | if (can_skip_alu_sanitation(env, insn)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5344 | return 0; |
| 5345 | |
| 5346 | /* We already marked aux for masking from non-speculative |
| 5347 | * paths, thus we got here in the first place. We only care |
| 5348 | * to explore bad access from here. |
| 5349 | */ |
| 5350 | if (vstate->speculative) |
| 5351 | goto do_sim; |
| 5352 | |
| 5353 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; |
| 5354 | alu_state |= ptr_is_dst_reg ? |
| 5355 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; |
| 5356 | |
| 5357 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) |
| 5358 | return 0; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 5359 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5360 | return -EACCES; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5361 | do_sim: |
| 5362 | /* Simulate and find potential out-of-bounds access under |
| 5363 | * speculative execution from truncation as a result of |
| 5364 | * masking when off was not within expected range. If off |
| 5365 | * sits in dst, then we temporarily need to move ptr there |
| 5366 | * to simulate dst (== 0) +/-= ptr. Needed, for example, |
| 5367 | * for cases where we use K-based arithmetic in one direction |
| 5368 | * and truncated reg-based in the other in order to explore |
| 5369 | * bad access. |
| 5370 | */ |
| 5371 | if (!ptr_is_dst_reg) { |
| 5372 | tmp = *dst_reg; |
| 5373 | *dst_reg = *ptr_reg; |
| 5374 | } |
| 5375 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); |
Xu Yu | 0803278 | 2019-03-21 18:00:35 +0800 | [diff] [blame] | 5376 | if (!ptr_is_dst_reg && ret) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5377 | *dst_reg = tmp; |
| 5378 | return !ret ? -EFAULT : 0; |
| 5379 | } |
| 5380 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5381 | /* 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] | 5382 | * Caller should also handle BPF_MOV case separately. |
| 5383 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 5384 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 5385 | */ |
| 5386 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 5387 | struct bpf_insn *insn, |
| 5388 | const struct bpf_reg_state *ptr_reg, |
| 5389 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5390 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5391 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5392 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 5393 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5394 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5395 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 5396 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 5397 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 5398 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 5399 | u32 dst = insn->dst_reg, src = insn->src_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5400 | u8 opcode = BPF_OP(insn->code); |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5401 | int ret; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5402 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5403 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5404 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 5405 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 5406 | smin_val > smax_val || umin_val > umax_val) { |
| 5407 | /* Taint dst register if offset had invalid bounds derived from |
| 5408 | * e.g. dead branches. |
| 5409 | */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 5410 | __mark_reg_unknown(env, dst_reg); |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 5411 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5412 | } |
| 5413 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5414 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 5415 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Yonghong Song | 6c69354 | 2020-06-18 16:46:31 -0700 | [diff] [blame] | 5416 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
| 5417 | __mark_reg_unknown(env, dst_reg); |
| 5418 | return 0; |
| 5419 | } |
| 5420 | |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5421 | verbose(env, |
| 5422 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 5423 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5424 | return -EACCES; |
| 5425 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 5426 | |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5427 | switch (ptr_reg->type) { |
| 5428 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 5429 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
| 5430 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5431 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5432 | case CONST_PTR_TO_MAP: |
Yonghong Song | 7c69673 | 2020-09-08 10:57:02 -0700 | [diff] [blame] | 5433 | /* smin_val represents the known value */ |
| 5434 | if (known && smin_val == 0 && opcode == BPF_ADD) |
| 5435 | break; |
| 5436 | /* fall-through */ |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5437 | case PTR_TO_PACKET_END: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5438 | case PTR_TO_SOCKET: |
| 5439 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 5440 | case PTR_TO_SOCK_COMMON: |
| 5441 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 5442 | case PTR_TO_TCP_SOCK: |
| 5443 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 5444 | case PTR_TO_XDP_SOCK: |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5445 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
| 5446 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5447 | return -EACCES; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 5448 | case PTR_TO_MAP_VALUE: |
| 5449 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { |
| 5450 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", |
| 5451 | off_reg == dst_reg ? dst : src); |
| 5452 | return -EACCES; |
| 5453 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 5454 | fallthrough; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 5455 | default: |
| 5456 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5457 | } |
| 5458 | |
| 5459 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 5460 | * 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] | 5461 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5462 | dst_reg->type = ptr_reg->type; |
| 5463 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 5464 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 5465 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 5466 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 5467 | return -EINVAL; |
| 5468 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5469 | /* pointer types do not carry 32-bit bounds at the moment. */ |
| 5470 | __mark_reg32_unbounded(dst_reg); |
| 5471 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5472 | switch (opcode) { |
| 5473 | case BPF_ADD: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5474 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 5475 | if (ret < 0) { |
| 5476 | verbose(env, "R%d tried to add from different maps or paths\n", dst); |
| 5477 | return ret; |
| 5478 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5479 | /* We can take a fixed offset as long as it doesn't overflow |
| 5480 | * the s32 'off' field |
| 5481 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5482 | if (known && (ptr_reg->off + smin_val == |
| 5483 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5484 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5485 | dst_reg->smin_value = smin_ptr; |
| 5486 | dst_reg->smax_value = smax_ptr; |
| 5487 | dst_reg->umin_value = umin_ptr; |
| 5488 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5489 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5490 | dst_reg->off = ptr_reg->off + smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5491 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5492 | break; |
| 5493 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5494 | /* A new variable offset is created. Note that off_reg->off |
| 5495 | * == 0, since it's a scalar. |
| 5496 | * dst_reg gets the pointer type and since some positive |
| 5497 | * integer value was added to the pointer, give it a new 'id' |
| 5498 | * if it's a PTR_TO_PACKET. |
| 5499 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 5500 | * added into the variable offset, and we copy the fixed offset |
| 5501 | * from ptr_reg. |
| 5502 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5503 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 5504 | signed_add_overflows(smax_ptr, smax_val)) { |
| 5505 | dst_reg->smin_value = S64_MIN; |
| 5506 | dst_reg->smax_value = S64_MAX; |
| 5507 | } else { |
| 5508 | dst_reg->smin_value = smin_ptr + smin_val; |
| 5509 | dst_reg->smax_value = smax_ptr + smax_val; |
| 5510 | } |
| 5511 | if (umin_ptr + umin_val < umin_ptr || |
| 5512 | umax_ptr + umax_val < umax_ptr) { |
| 5513 | dst_reg->umin_value = 0; |
| 5514 | dst_reg->umax_value = U64_MAX; |
| 5515 | } else { |
| 5516 | dst_reg->umin_value = umin_ptr + umin_val; |
| 5517 | dst_reg->umax_value = umax_ptr + umax_val; |
| 5518 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5519 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 5520 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5521 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5522 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5523 | dst_reg->id = ++env->id_gen; |
| 5524 | /* something was added to pkt_ptr, set range to zero */ |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5525 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5526 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5527 | break; |
| 5528 | case BPF_SUB: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5529 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 5530 | if (ret < 0) { |
| 5531 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); |
| 5532 | return ret; |
| 5533 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5534 | if (dst_reg == off_reg) { |
| 5535 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5536 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 5537 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5538 | return -EACCES; |
| 5539 | } |
| 5540 | /* We don't allow subtraction from FP, because (according to |
| 5541 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 5542 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 5543 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5544 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5545 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 5546 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5547 | return -EACCES; |
| 5548 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5549 | if (known && (ptr_reg->off - smin_val == |
| 5550 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5551 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5552 | dst_reg->smin_value = smin_ptr; |
| 5553 | dst_reg->smax_value = smax_ptr; |
| 5554 | dst_reg->umin_value = umin_ptr; |
| 5555 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5556 | dst_reg->var_off = ptr_reg->var_off; |
| 5557 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5558 | dst_reg->off = ptr_reg->off - smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5559 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5560 | break; |
| 5561 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5562 | /* A new variable offset is created. If the subtrahend is known |
| 5563 | * nonnegative, then any reg->range we had before is still good. |
| 5564 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5565 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 5566 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 5567 | /* Overflow possible, we know nothing */ |
| 5568 | dst_reg->smin_value = S64_MIN; |
| 5569 | dst_reg->smax_value = S64_MAX; |
| 5570 | } else { |
| 5571 | dst_reg->smin_value = smin_ptr - smax_val; |
| 5572 | dst_reg->smax_value = smax_ptr - smin_val; |
| 5573 | } |
| 5574 | if (umin_ptr < umax_val) { |
| 5575 | /* Overflow possible, we know nothing */ |
| 5576 | dst_reg->umin_value = 0; |
| 5577 | dst_reg->umax_value = U64_MAX; |
| 5578 | } else { |
| 5579 | /* Cannot overflow (as long as bounds are consistent) */ |
| 5580 | dst_reg->umin_value = umin_ptr - umax_val; |
| 5581 | dst_reg->umax_value = umax_ptr - umin_val; |
| 5582 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5583 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 5584 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5585 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5586 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5587 | dst_reg->id = ++env->id_gen; |
| 5588 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5589 | if (smin_val < 0) |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 5590 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5591 | } |
| 5592 | break; |
| 5593 | case BPF_AND: |
| 5594 | case BPF_OR: |
| 5595 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5596 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 5597 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 5598 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5599 | return -EACCES; |
| 5600 | default: |
| 5601 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 5602 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 5603 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5604 | return -EACCES; |
| 5605 | } |
| 5606 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 5607 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 5608 | return -EINVAL; |
| 5609 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5610 | __update_reg_bounds(dst_reg); |
| 5611 | __reg_deduce_bounds(dst_reg); |
| 5612 | __reg_bound_offset(dst_reg); |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 5613 | |
| 5614 | /* For unprivileged we require that resulting offset must be in bounds |
| 5615 | * in order to be able to sanitize access later on. |
| 5616 | */ |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 5617 | if (!env->bypass_spec_v1) { |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 5618 | if (dst_reg->type == PTR_TO_MAP_VALUE && |
| 5619 | check_map_access(env, dst, dst_reg->off, 1, false)) { |
| 5620 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
| 5621 | "prohibited for !root\n", dst); |
| 5622 | return -EACCES; |
| 5623 | } else if (dst_reg->type == PTR_TO_STACK && |
| 5624 | check_stack_access(env, dst_reg, dst_reg->off + |
| 5625 | dst_reg->var_off.value, 1)) { |
| 5626 | verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 5627 | "prohibited for !root\n", dst); |
| 5628 | return -EACCES; |
| 5629 | } |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 5630 | } |
| 5631 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5632 | return 0; |
| 5633 | } |
| 5634 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5635 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
| 5636 | struct bpf_reg_state *src_reg) |
| 5637 | { |
| 5638 | s32 smin_val = src_reg->s32_min_value; |
| 5639 | s32 smax_val = src_reg->s32_max_value; |
| 5640 | u32 umin_val = src_reg->u32_min_value; |
| 5641 | u32 umax_val = src_reg->u32_max_value; |
| 5642 | |
| 5643 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || |
| 5644 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { |
| 5645 | dst_reg->s32_min_value = S32_MIN; |
| 5646 | dst_reg->s32_max_value = S32_MAX; |
| 5647 | } else { |
| 5648 | dst_reg->s32_min_value += smin_val; |
| 5649 | dst_reg->s32_max_value += smax_val; |
| 5650 | } |
| 5651 | if (dst_reg->u32_min_value + umin_val < umin_val || |
| 5652 | dst_reg->u32_max_value + umax_val < umax_val) { |
| 5653 | dst_reg->u32_min_value = 0; |
| 5654 | dst_reg->u32_max_value = U32_MAX; |
| 5655 | } else { |
| 5656 | dst_reg->u32_min_value += umin_val; |
| 5657 | dst_reg->u32_max_value += umax_val; |
| 5658 | } |
| 5659 | } |
| 5660 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5661 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
| 5662 | struct bpf_reg_state *src_reg) |
| 5663 | { |
| 5664 | s64 smin_val = src_reg->smin_value; |
| 5665 | s64 smax_val = src_reg->smax_value; |
| 5666 | u64 umin_val = src_reg->umin_value; |
| 5667 | u64 umax_val = src_reg->umax_value; |
| 5668 | |
| 5669 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 5670 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 5671 | dst_reg->smin_value = S64_MIN; |
| 5672 | dst_reg->smax_value = S64_MAX; |
| 5673 | } else { |
| 5674 | dst_reg->smin_value += smin_val; |
| 5675 | dst_reg->smax_value += smax_val; |
| 5676 | } |
| 5677 | if (dst_reg->umin_value + umin_val < umin_val || |
| 5678 | dst_reg->umax_value + umax_val < umax_val) { |
| 5679 | dst_reg->umin_value = 0; |
| 5680 | dst_reg->umax_value = U64_MAX; |
| 5681 | } else { |
| 5682 | dst_reg->umin_value += umin_val; |
| 5683 | dst_reg->umax_value += umax_val; |
| 5684 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5685 | } |
| 5686 | |
| 5687 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, |
| 5688 | struct bpf_reg_state *src_reg) |
| 5689 | { |
| 5690 | s32 smin_val = src_reg->s32_min_value; |
| 5691 | s32 smax_val = src_reg->s32_max_value; |
| 5692 | u32 umin_val = src_reg->u32_min_value; |
| 5693 | u32 umax_val = src_reg->u32_max_value; |
| 5694 | |
| 5695 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || |
| 5696 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { |
| 5697 | /* Overflow possible, we know nothing */ |
| 5698 | dst_reg->s32_min_value = S32_MIN; |
| 5699 | dst_reg->s32_max_value = S32_MAX; |
| 5700 | } else { |
| 5701 | dst_reg->s32_min_value -= smax_val; |
| 5702 | dst_reg->s32_max_value -= smin_val; |
| 5703 | } |
| 5704 | if (dst_reg->u32_min_value < umax_val) { |
| 5705 | /* Overflow possible, we know nothing */ |
| 5706 | dst_reg->u32_min_value = 0; |
| 5707 | dst_reg->u32_max_value = U32_MAX; |
| 5708 | } else { |
| 5709 | /* Cannot overflow (as long as bounds are consistent) */ |
| 5710 | dst_reg->u32_min_value -= umax_val; |
| 5711 | dst_reg->u32_max_value -= umin_val; |
| 5712 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5713 | } |
| 5714 | |
| 5715 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, |
| 5716 | struct bpf_reg_state *src_reg) |
| 5717 | { |
| 5718 | s64 smin_val = src_reg->smin_value; |
| 5719 | s64 smax_val = src_reg->smax_value; |
| 5720 | u64 umin_val = src_reg->umin_value; |
| 5721 | u64 umax_val = src_reg->umax_value; |
| 5722 | |
| 5723 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 5724 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 5725 | /* Overflow possible, we know nothing */ |
| 5726 | dst_reg->smin_value = S64_MIN; |
| 5727 | dst_reg->smax_value = S64_MAX; |
| 5728 | } else { |
| 5729 | dst_reg->smin_value -= smax_val; |
| 5730 | dst_reg->smax_value -= smin_val; |
| 5731 | } |
| 5732 | if (dst_reg->umin_value < umax_val) { |
| 5733 | /* Overflow possible, we know nothing */ |
| 5734 | dst_reg->umin_value = 0; |
| 5735 | dst_reg->umax_value = U64_MAX; |
| 5736 | } else { |
| 5737 | /* Cannot overflow (as long as bounds are consistent) */ |
| 5738 | dst_reg->umin_value -= umax_val; |
| 5739 | dst_reg->umax_value -= umin_val; |
| 5740 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5741 | } |
| 5742 | |
| 5743 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, |
| 5744 | struct bpf_reg_state *src_reg) |
| 5745 | { |
| 5746 | s32 smin_val = src_reg->s32_min_value; |
| 5747 | u32 umin_val = src_reg->u32_min_value; |
| 5748 | u32 umax_val = src_reg->u32_max_value; |
| 5749 | |
| 5750 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { |
| 5751 | /* Ain't nobody got time to multiply that sign */ |
| 5752 | __mark_reg32_unbounded(dst_reg); |
| 5753 | return; |
| 5754 | } |
| 5755 | /* Both values are positive, so we can work with unsigned and |
| 5756 | * copy the result to signed (unless it exceeds S32_MAX). |
| 5757 | */ |
| 5758 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { |
| 5759 | /* Potential overflow, we know nothing */ |
| 5760 | __mark_reg32_unbounded(dst_reg); |
| 5761 | return; |
| 5762 | } |
| 5763 | dst_reg->u32_min_value *= umin_val; |
| 5764 | dst_reg->u32_max_value *= umax_val; |
| 5765 | if (dst_reg->u32_max_value > S32_MAX) { |
| 5766 | /* Overflow possible, we know nothing */ |
| 5767 | dst_reg->s32_min_value = S32_MIN; |
| 5768 | dst_reg->s32_max_value = S32_MAX; |
| 5769 | } else { |
| 5770 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 5771 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 5772 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5773 | } |
| 5774 | |
| 5775 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, |
| 5776 | struct bpf_reg_state *src_reg) |
| 5777 | { |
| 5778 | s64 smin_val = src_reg->smin_value; |
| 5779 | u64 umin_val = src_reg->umin_value; |
| 5780 | u64 umax_val = src_reg->umax_value; |
| 5781 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5782 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
| 5783 | /* Ain't nobody got time to multiply that sign */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5784 | __mark_reg64_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5785 | return; |
| 5786 | } |
| 5787 | /* Both values are positive, so we can work with unsigned and |
| 5788 | * copy the result to signed (unless it exceeds S64_MAX). |
| 5789 | */ |
| 5790 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 5791 | /* Potential overflow, we know nothing */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5792 | __mark_reg64_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5793 | return; |
| 5794 | } |
| 5795 | dst_reg->umin_value *= umin_val; |
| 5796 | dst_reg->umax_value *= umax_val; |
| 5797 | if (dst_reg->umax_value > S64_MAX) { |
| 5798 | /* Overflow possible, we know nothing */ |
| 5799 | dst_reg->smin_value = S64_MIN; |
| 5800 | dst_reg->smax_value = S64_MAX; |
| 5801 | } else { |
| 5802 | dst_reg->smin_value = dst_reg->umin_value; |
| 5803 | dst_reg->smax_value = dst_reg->umax_value; |
| 5804 | } |
| 5805 | } |
| 5806 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5807 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
| 5808 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5809 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5810 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 5811 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 5812 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 5813 | s32 smin_val = src_reg->s32_min_value; |
| 5814 | u32 umax_val = src_reg->u32_max_value; |
| 5815 | |
| 5816 | /* Assuming scalar64_min_max_and will be called so its safe |
| 5817 | * to skip updating register for known 32-bit case. |
| 5818 | */ |
| 5819 | if (src_known && dst_known) |
| 5820 | return; |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5821 | |
| 5822 | /* We get our minimum from the var_off, since that's inherently |
| 5823 | * bitwise. Our maximum is the minimum of the operands' maxima. |
| 5824 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5825 | dst_reg->u32_min_value = var32_off.value; |
| 5826 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); |
| 5827 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { |
| 5828 | /* Lose signed bounds when ANDing negative numbers, |
| 5829 | * ain't nobody got time for that. |
| 5830 | */ |
| 5831 | dst_reg->s32_min_value = S32_MIN; |
| 5832 | dst_reg->s32_max_value = S32_MAX; |
| 5833 | } else { |
| 5834 | /* ANDing two positives gives a positive, so safe to |
| 5835 | * cast result into s64. |
| 5836 | */ |
| 5837 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 5838 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 5839 | } |
| 5840 | |
| 5841 | } |
| 5842 | |
| 5843 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
| 5844 | struct bpf_reg_state *src_reg) |
| 5845 | { |
| 5846 | bool src_known = tnum_is_const(src_reg->var_off); |
| 5847 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 5848 | s64 smin_val = src_reg->smin_value; |
| 5849 | u64 umax_val = src_reg->umax_value; |
| 5850 | |
| 5851 | if (src_known && dst_known) { |
John Fastabend | 4fbb38a | 2020-09-24 11:45:06 -0700 | [diff] [blame^] | 5852 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5853 | return; |
| 5854 | } |
| 5855 | |
| 5856 | /* We get our minimum from the var_off, since that's inherently |
| 5857 | * bitwise. Our maximum is the minimum of the operands' maxima. |
| 5858 | */ |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5859 | dst_reg->umin_value = dst_reg->var_off.value; |
| 5860 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 5861 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 5862 | /* Lose signed bounds when ANDing negative numbers, |
| 5863 | * ain't nobody got time for that. |
| 5864 | */ |
| 5865 | dst_reg->smin_value = S64_MIN; |
| 5866 | dst_reg->smax_value = S64_MAX; |
| 5867 | } else { |
| 5868 | /* ANDing two positives gives a positive, so safe to |
| 5869 | * cast result into s64. |
| 5870 | */ |
| 5871 | dst_reg->smin_value = dst_reg->umin_value; |
| 5872 | dst_reg->smax_value = dst_reg->umax_value; |
| 5873 | } |
| 5874 | /* We may learn something more from the var_off */ |
| 5875 | __update_reg_bounds(dst_reg); |
| 5876 | } |
| 5877 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5878 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
| 5879 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5880 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5881 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 5882 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 5883 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 5884 | s32 smin_val = src_reg->smin_value; |
| 5885 | u32 umin_val = src_reg->umin_value; |
| 5886 | |
| 5887 | /* Assuming scalar64_min_max_or will be called so it is safe |
| 5888 | * to skip updating register for known case. |
| 5889 | */ |
| 5890 | if (src_known && dst_known) |
| 5891 | return; |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5892 | |
| 5893 | /* We get our maximum from the var_off, and our minimum is the |
| 5894 | * maximum of the operands' minima |
| 5895 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5896 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); |
| 5897 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; |
| 5898 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { |
| 5899 | /* Lose signed bounds when ORing negative numbers, |
| 5900 | * ain't nobody got time for that. |
| 5901 | */ |
| 5902 | dst_reg->s32_min_value = S32_MIN; |
| 5903 | dst_reg->s32_max_value = S32_MAX; |
| 5904 | } else { |
| 5905 | /* ORing two positives gives a positive, so safe to |
| 5906 | * cast result into s64. |
| 5907 | */ |
| 5908 | dst_reg->s32_min_value = dst_reg->umin_value; |
| 5909 | dst_reg->s32_max_value = dst_reg->umax_value; |
| 5910 | } |
| 5911 | } |
| 5912 | |
| 5913 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
| 5914 | struct bpf_reg_state *src_reg) |
| 5915 | { |
| 5916 | bool src_known = tnum_is_const(src_reg->var_off); |
| 5917 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 5918 | s64 smin_val = src_reg->smin_value; |
| 5919 | u64 umin_val = src_reg->umin_value; |
| 5920 | |
| 5921 | if (src_known && dst_known) { |
John Fastabend | 4fbb38a | 2020-09-24 11:45:06 -0700 | [diff] [blame^] | 5922 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 5923 | return; |
| 5924 | } |
| 5925 | |
| 5926 | /* We get our maximum from the var_off, and our minimum is the |
| 5927 | * maximum of the operands' minima |
| 5928 | */ |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 5929 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 5930 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; |
| 5931 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 5932 | /* Lose signed bounds when ORing negative numbers, |
| 5933 | * ain't nobody got time for that. |
| 5934 | */ |
| 5935 | dst_reg->smin_value = S64_MIN; |
| 5936 | dst_reg->smax_value = S64_MAX; |
| 5937 | } else { |
| 5938 | /* ORing two positives gives a positive, so safe to |
| 5939 | * cast result into s64. |
| 5940 | */ |
| 5941 | dst_reg->smin_value = dst_reg->umin_value; |
| 5942 | dst_reg->smax_value = dst_reg->umax_value; |
| 5943 | } |
| 5944 | /* We may learn something more from the var_off */ |
| 5945 | __update_reg_bounds(dst_reg); |
| 5946 | } |
| 5947 | |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 5948 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
| 5949 | struct bpf_reg_state *src_reg) |
| 5950 | { |
| 5951 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 5952 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 5953 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 5954 | s32 smin_val = src_reg->s32_min_value; |
| 5955 | |
| 5956 | /* Assuming scalar64_min_max_xor will be called so it is safe |
| 5957 | * to skip updating register for known case. |
| 5958 | */ |
| 5959 | if (src_known && dst_known) |
| 5960 | return; |
| 5961 | |
| 5962 | /* We get both minimum and maximum from the var32_off. */ |
| 5963 | dst_reg->u32_min_value = var32_off.value; |
| 5964 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; |
| 5965 | |
| 5966 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { |
| 5967 | /* XORing two positive sign numbers gives a positive, |
| 5968 | * so safe to cast u32 result into s32. |
| 5969 | */ |
| 5970 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 5971 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 5972 | } else { |
| 5973 | dst_reg->s32_min_value = S32_MIN; |
| 5974 | dst_reg->s32_max_value = S32_MAX; |
| 5975 | } |
| 5976 | } |
| 5977 | |
| 5978 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, |
| 5979 | struct bpf_reg_state *src_reg) |
| 5980 | { |
| 5981 | bool src_known = tnum_is_const(src_reg->var_off); |
| 5982 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 5983 | s64 smin_val = src_reg->smin_value; |
| 5984 | |
| 5985 | if (src_known && dst_known) { |
| 5986 | /* dst_reg->var_off.value has been updated earlier */ |
| 5987 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
| 5988 | return; |
| 5989 | } |
| 5990 | |
| 5991 | /* We get both minimum and maximum from the var_off. */ |
| 5992 | dst_reg->umin_value = dst_reg->var_off.value; |
| 5993 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; |
| 5994 | |
| 5995 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { |
| 5996 | /* XORing two positive sign numbers gives a positive, |
| 5997 | * so safe to cast u64 result into s64. |
| 5998 | */ |
| 5999 | dst_reg->smin_value = dst_reg->umin_value; |
| 6000 | dst_reg->smax_value = dst_reg->umax_value; |
| 6001 | } else { |
| 6002 | dst_reg->smin_value = S64_MIN; |
| 6003 | dst_reg->smax_value = S64_MAX; |
| 6004 | } |
| 6005 | |
| 6006 | __update_reg_bounds(dst_reg); |
| 6007 | } |
| 6008 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6009 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6010 | u64 umin_val, u64 umax_val) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6011 | { |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6012 | /* We lose all sign bit information (except what we can pick |
| 6013 | * up from var_off) |
| 6014 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6015 | dst_reg->s32_min_value = S32_MIN; |
| 6016 | dst_reg->s32_max_value = S32_MAX; |
| 6017 | /* If we might shift our top bit out, then we know nothing */ |
| 6018 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { |
| 6019 | dst_reg->u32_min_value = 0; |
| 6020 | dst_reg->u32_max_value = U32_MAX; |
| 6021 | } else { |
| 6022 | dst_reg->u32_min_value <<= umin_val; |
| 6023 | dst_reg->u32_max_value <<= umax_val; |
| 6024 | } |
| 6025 | } |
| 6026 | |
| 6027 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6028 | struct bpf_reg_state *src_reg) |
| 6029 | { |
| 6030 | u32 umax_val = src_reg->u32_max_value; |
| 6031 | u32 umin_val = src_reg->u32_min_value; |
| 6032 | /* u32 alu operation will zext upper bits */ |
| 6033 | struct tnum subreg = tnum_subreg(dst_reg->var_off); |
| 6034 | |
| 6035 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); |
| 6036 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); |
| 6037 | /* Not required but being careful mark reg64 bounds as unknown so |
| 6038 | * that we are forced to pick them up from tnum and zext later and |
| 6039 | * if some path skips this step we are still safe. |
| 6040 | */ |
| 6041 | __mark_reg64_unbounded(dst_reg); |
| 6042 | __update_reg32_bounds(dst_reg); |
| 6043 | } |
| 6044 | |
| 6045 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6046 | u64 umin_val, u64 umax_val) |
| 6047 | { |
| 6048 | /* Special case <<32 because it is a common compiler pattern to sign |
| 6049 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are |
| 6050 | * positive we know this shift will also be positive so we can track |
| 6051 | * bounds correctly. Otherwise we lose all sign bit information except |
| 6052 | * what we can pick up from var_off. Perhaps we can generalize this |
| 6053 | * later to shifts of any length. |
| 6054 | */ |
| 6055 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) |
| 6056 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; |
| 6057 | else |
| 6058 | dst_reg->smax_value = S64_MAX; |
| 6059 | |
| 6060 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) |
| 6061 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; |
| 6062 | else |
| 6063 | dst_reg->smin_value = S64_MIN; |
| 6064 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6065 | /* If we might shift our top bit out, then we know nothing */ |
| 6066 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 6067 | dst_reg->umin_value = 0; |
| 6068 | dst_reg->umax_value = U64_MAX; |
| 6069 | } else { |
| 6070 | dst_reg->umin_value <<= umin_val; |
| 6071 | dst_reg->umax_value <<= umax_val; |
| 6072 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6073 | } |
| 6074 | |
| 6075 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 6076 | struct bpf_reg_state *src_reg) |
| 6077 | { |
| 6078 | u64 umax_val = src_reg->umax_value; |
| 6079 | u64 umin_val = src_reg->umin_value; |
| 6080 | |
| 6081 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ |
| 6082 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); |
| 6083 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); |
| 6084 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6085 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
| 6086 | /* We may learn something more from the var_off */ |
| 6087 | __update_reg_bounds(dst_reg); |
| 6088 | } |
| 6089 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6090 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
| 6091 | struct bpf_reg_state *src_reg) |
| 6092 | { |
| 6093 | struct tnum subreg = tnum_subreg(dst_reg->var_off); |
| 6094 | u32 umax_val = src_reg->u32_max_value; |
| 6095 | u32 umin_val = src_reg->u32_min_value; |
| 6096 | |
| 6097 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 6098 | * be negative, then either: |
| 6099 | * 1) src_reg might be zero, so the sign bit of the result is |
| 6100 | * unknown, so we lose our signed bounds |
| 6101 | * 2) it's known negative, thus the unsigned bounds capture the |
| 6102 | * signed bounds |
| 6103 | * 3) the signed bounds cross zero, so they tell us nothing |
| 6104 | * about the result |
| 6105 | * If the value in dst_reg is known nonnegative, then again the |
| 6106 | * unsigned bounts capture the signed bounds. |
| 6107 | * Thus, in all cases it suffices to blow away our signed bounds |
| 6108 | * and rely on inferring new ones from the unsigned bounds and |
| 6109 | * var_off of the result. |
| 6110 | */ |
| 6111 | dst_reg->s32_min_value = S32_MIN; |
| 6112 | dst_reg->s32_max_value = S32_MAX; |
| 6113 | |
| 6114 | dst_reg->var_off = tnum_rshift(subreg, umin_val); |
| 6115 | dst_reg->u32_min_value >>= umax_val; |
| 6116 | dst_reg->u32_max_value >>= umin_val; |
| 6117 | |
| 6118 | __mark_reg64_unbounded(dst_reg); |
| 6119 | __update_reg32_bounds(dst_reg); |
| 6120 | } |
| 6121 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6122 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
| 6123 | struct bpf_reg_state *src_reg) |
| 6124 | { |
| 6125 | u64 umax_val = src_reg->umax_value; |
| 6126 | u64 umin_val = src_reg->umin_value; |
| 6127 | |
| 6128 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 6129 | * be negative, then either: |
| 6130 | * 1) src_reg might be zero, so the sign bit of the result is |
| 6131 | * unknown, so we lose our signed bounds |
| 6132 | * 2) it's known negative, thus the unsigned bounds capture the |
| 6133 | * signed bounds |
| 6134 | * 3) the signed bounds cross zero, so they tell us nothing |
| 6135 | * about the result |
| 6136 | * If the value in dst_reg is known nonnegative, then again the |
| 6137 | * unsigned bounts capture the signed bounds. |
| 6138 | * Thus, in all cases it suffices to blow away our signed bounds |
| 6139 | * and rely on inferring new ones from the unsigned bounds and |
| 6140 | * var_off of the result. |
| 6141 | */ |
| 6142 | dst_reg->smin_value = S64_MIN; |
| 6143 | dst_reg->smax_value = S64_MAX; |
| 6144 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
| 6145 | dst_reg->umin_value >>= umax_val; |
| 6146 | dst_reg->umax_value >>= umin_val; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6147 | |
| 6148 | /* Its not easy to operate on alu32 bounds here because it depends |
| 6149 | * on bits being shifted in. Take easy way out and mark unbounded |
| 6150 | * so we can recalculate later from tnum. |
| 6151 | */ |
| 6152 | __mark_reg32_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6153 | __update_reg_bounds(dst_reg); |
| 6154 | } |
| 6155 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6156 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
| 6157 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6158 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6159 | u64 umin_val = src_reg->u32_min_value; |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6160 | |
| 6161 | /* Upon reaching here, src_known is true and |
| 6162 | * umax_val is equal to umin_val. |
| 6163 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6164 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
| 6165 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6166 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6167 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
| 6168 | |
| 6169 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 6170 | * dst_reg var_off to refine the result. |
| 6171 | */ |
| 6172 | dst_reg->u32_min_value = 0; |
| 6173 | dst_reg->u32_max_value = U32_MAX; |
| 6174 | |
| 6175 | __mark_reg64_unbounded(dst_reg); |
| 6176 | __update_reg32_bounds(dst_reg); |
| 6177 | } |
| 6178 | |
| 6179 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, |
| 6180 | struct bpf_reg_state *src_reg) |
| 6181 | { |
| 6182 | u64 umin_val = src_reg->umin_value; |
| 6183 | |
| 6184 | /* Upon reaching here, src_known is true and umax_val is equal |
| 6185 | * to umin_val. |
| 6186 | */ |
| 6187 | dst_reg->smin_value >>= umin_val; |
| 6188 | dst_reg->smax_value >>= umin_val; |
| 6189 | |
| 6190 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6191 | |
| 6192 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 6193 | * dst_reg var_off to refine the result. |
| 6194 | */ |
| 6195 | dst_reg->umin_value = 0; |
| 6196 | dst_reg->umax_value = U64_MAX; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6197 | |
| 6198 | /* Its not easy to operate on alu32 bounds here because it depends |
| 6199 | * on bits being shifted in from upper 32-bits. Take easy way out |
| 6200 | * and mark unbounded so we can recalculate later from tnum. |
| 6201 | */ |
| 6202 | __mark_reg32_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6203 | __update_reg_bounds(dst_reg); |
| 6204 | } |
| 6205 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6206 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 6207 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 6208 | * need extra checks in the 32-bit case. |
| 6209 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6210 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 6211 | struct bpf_insn *insn, |
| 6212 | struct bpf_reg_state *dst_reg, |
| 6213 | struct bpf_reg_state src_reg) |
| 6214 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6215 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6216 | u8 opcode = BPF_OP(insn->code); |
Mao Wenan | b0b3fb6 | 2020-04-18 09:37:35 +0800 | [diff] [blame] | 6217 | bool src_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6218 | s64 smin_val, smax_val; |
| 6219 | u64 umin_val, umax_val; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6220 | s32 s32_min_val, s32_max_val; |
| 6221 | u32 u32_min_val, u32_max_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6222 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 6223 | u32 dst = insn->dst_reg; |
| 6224 | int ret; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6225 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
Jann Horn | b799207 | 2018-10-05 18:17:59 +0200 | [diff] [blame] | 6226 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6227 | smin_val = src_reg.smin_value; |
| 6228 | smax_val = src_reg.smax_value; |
| 6229 | umin_val = src_reg.umin_value; |
| 6230 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6231 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6232 | s32_min_val = src_reg.s32_min_value; |
| 6233 | s32_max_val = src_reg.s32_max_value; |
| 6234 | u32_min_val = src_reg.u32_min_value; |
| 6235 | u32_max_val = src_reg.u32_max_value; |
| 6236 | |
| 6237 | if (alu32) { |
| 6238 | src_known = tnum_subreg_is_const(src_reg.var_off); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6239 | if ((src_known && |
| 6240 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || |
| 6241 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { |
| 6242 | /* Taint dst register if offset had invalid bounds |
| 6243 | * derived from e.g. dead branches. |
| 6244 | */ |
| 6245 | __mark_reg_unknown(env, dst_reg); |
| 6246 | return 0; |
| 6247 | } |
| 6248 | } else { |
| 6249 | src_known = tnum_is_const(src_reg.var_off); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6250 | if ((src_known && |
| 6251 | (smin_val != smax_val || umin_val != umax_val)) || |
| 6252 | smin_val > smax_val || umin_val > umax_val) { |
| 6253 | /* Taint dst register if offset had invalid bounds |
| 6254 | * derived from e.g. dead branches. |
| 6255 | */ |
| 6256 | __mark_reg_unknown(env, dst_reg); |
| 6257 | return 0; |
| 6258 | } |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 6259 | } |
| 6260 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 6261 | if (!src_known && |
| 6262 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 6263 | __mark_reg_unknown(env, dst_reg); |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 6264 | return 0; |
| 6265 | } |
| 6266 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6267 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
| 6268 | * There are two classes of instructions: The first class we track both |
| 6269 | * alu32 and alu64 sign/unsigned bounds independently this provides the |
| 6270 | * greatest amount of precision when alu operations are mixed with jmp32 |
| 6271 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, |
| 6272 | * and BPF_OR. This is possible because these ops have fairly easy to |
| 6273 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. |
| 6274 | * See alu32 verifier tests for examples. The second class of |
| 6275 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy |
| 6276 | * with regards to tracking sign/unsigned bounds because the bits may |
| 6277 | * cross subreg boundaries in the alu64 case. When this happens we mark |
| 6278 | * the reg unbounded in the subreg bound space and use the resulting |
| 6279 | * tnum to calculate an approximation of the sign/unsigned bounds. |
| 6280 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6281 | switch (opcode) { |
| 6282 | case BPF_ADD: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 6283 | ret = sanitize_val_alu(env, insn); |
| 6284 | if (ret < 0) { |
| 6285 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); |
| 6286 | return ret; |
| 6287 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6288 | scalar32_min_max_add(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6289 | scalar_min_max_add(dst_reg, &src_reg); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6290 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6291 | break; |
| 6292 | case BPF_SUB: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 6293 | ret = sanitize_val_alu(env, insn); |
| 6294 | if (ret < 0) { |
| 6295 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); |
| 6296 | return ret; |
| 6297 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6298 | scalar32_min_max_sub(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6299 | scalar_min_max_sub(dst_reg, &src_reg); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6300 | 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] | 6301 | break; |
| 6302 | case BPF_MUL: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6303 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 6304 | scalar32_min_max_mul(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6305 | scalar_min_max_mul(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6306 | break; |
| 6307 | case BPF_AND: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6308 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
| 6309 | scalar32_min_max_and(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6310 | scalar_min_max_and(dst_reg, &src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6311 | break; |
| 6312 | case BPF_OR: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6313 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
| 6314 | scalar32_min_max_or(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 6315 | scalar_min_max_or(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6316 | break; |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 6317 | case BPF_XOR: |
| 6318 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); |
| 6319 | scalar32_min_max_xor(dst_reg, &src_reg); |
| 6320 | scalar_min_max_xor(dst_reg, &src_reg); |
| 6321 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6322 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6323 | if (umax_val >= insn_bitness) { |
| 6324 | /* Shifts greater than 31 or 63 are undefined. |
| 6325 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6326 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6327 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6328 | break; |
| 6329 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6330 | if (alu32) |
| 6331 | scalar32_min_max_lsh(dst_reg, &src_reg); |
| 6332 | else |
| 6333 | scalar_min_max_lsh(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6334 | break; |
| 6335 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6336 | if (umax_val >= insn_bitness) { |
| 6337 | /* Shifts greater than 31 or 63 are undefined. |
| 6338 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6339 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6340 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6341 | break; |
| 6342 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6343 | if (alu32) |
| 6344 | scalar32_min_max_rsh(dst_reg, &src_reg); |
| 6345 | else |
| 6346 | scalar_min_max_rsh(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6347 | break; |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 6348 | case BPF_ARSH: |
| 6349 | if (umax_val >= insn_bitness) { |
| 6350 | /* Shifts greater than 31 or 63 are undefined. |
| 6351 | * This includes shifts by a negative number. |
| 6352 | */ |
| 6353 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 6354 | break; |
| 6355 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6356 | if (alu32) |
| 6357 | scalar32_min_max_arsh(dst_reg, &src_reg); |
| 6358 | else |
| 6359 | scalar_min_max_arsh(dst_reg, &src_reg); |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 6360 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6361 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6362 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6363 | break; |
| 6364 | } |
| 6365 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6366 | /* ALU32 ops are zero extended into 64bit register */ |
| 6367 | if (alu32) |
| 6368 | zext_32_to_64(dst_reg); |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 6369 | |
John Fastabend | 294f2fc | 2020-03-24 10:38:37 -0700 | [diff] [blame] | 6370 | __update_reg_bounds(dst_reg); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6371 | __reg_deduce_bounds(dst_reg); |
| 6372 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6373 | return 0; |
| 6374 | } |
| 6375 | |
| 6376 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 6377 | * and var_off. |
| 6378 | */ |
| 6379 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 6380 | struct bpf_insn *insn) |
| 6381 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6382 | struct bpf_verifier_state *vstate = env->cur_state; |
| 6383 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 6384 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6385 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 6386 | u8 opcode = BPF_OP(insn->code); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 6387 | int err; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6388 | |
| 6389 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6390 | src_reg = NULL; |
| 6391 | if (dst_reg->type != SCALAR_VALUE) |
| 6392 | ptr_reg = dst_reg; |
| 6393 | if (BPF_SRC(insn->code) == BPF_X) { |
| 6394 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6395 | if (src_reg->type != SCALAR_VALUE) { |
| 6396 | if (dst_reg->type != SCALAR_VALUE) { |
| 6397 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6398 | * an arbitrary scalar. Disallow all math except |
| 6399 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6400 | */ |
Alexei Starovoitov | dd06682 | 2018-09-12 14:06:10 -0700 | [diff] [blame] | 6401 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6402 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 6403 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6404 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6405 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 6406 | insn->dst_reg, |
| 6407 | bpf_alu_string[opcode >> 4]); |
| 6408 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6409 | } else { |
| 6410 | /* scalar += pointer |
| 6411 | * This is legal, but we have to reverse our |
| 6412 | * src/dest handling in computing the range |
| 6413 | */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 6414 | err = mark_chain_precision(env, insn->dst_reg); |
| 6415 | if (err) |
| 6416 | return err; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6417 | return adjust_ptr_min_max_vals(env, insn, |
| 6418 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6419 | } |
| 6420 | } else if (ptr_reg) { |
| 6421 | /* pointer += scalar */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 6422 | err = mark_chain_precision(env, insn->src_reg); |
| 6423 | if (err) |
| 6424 | return err; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6425 | return adjust_ptr_min_max_vals(env, insn, |
| 6426 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6427 | } |
| 6428 | } else { |
| 6429 | /* Pretend the src is a reg with a known value, since we only |
| 6430 | * need to be able to read from this state. |
| 6431 | */ |
| 6432 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6433 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6434 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 6435 | if (ptr_reg) /* pointer += K */ |
| 6436 | return adjust_ptr_min_max_vals(env, insn, |
| 6437 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6438 | } |
| 6439 | |
| 6440 | /* Got here implies adding two SCALAR_VALUEs */ |
| 6441 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6442 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6443 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6444 | return -EINVAL; |
| 6445 | } |
| 6446 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6447 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6448 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6449 | return -EINVAL; |
| 6450 | } |
| 6451 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6452 | } |
| 6453 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6454 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6455 | 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] | 6456 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6457 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6458 | u8 opcode = BPF_OP(insn->code); |
| 6459 | int err; |
| 6460 | |
| 6461 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 6462 | if (opcode == BPF_NEG) { |
| 6463 | if (BPF_SRC(insn->code) != 0 || |
| 6464 | insn->src_reg != BPF_REG_0 || |
| 6465 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6466 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6467 | return -EINVAL; |
| 6468 | } |
| 6469 | } else { |
| 6470 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 6471 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 6472 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6473 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6474 | return -EINVAL; |
| 6475 | } |
| 6476 | } |
| 6477 | |
| 6478 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6479 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6480 | if (err) |
| 6481 | return err; |
| 6482 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6483 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6484 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6485 | insn->dst_reg); |
| 6486 | return -EACCES; |
| 6487 | } |
| 6488 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6489 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6490 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6491 | if (err) |
| 6492 | return err; |
| 6493 | |
| 6494 | } else if (opcode == BPF_MOV) { |
| 6495 | |
| 6496 | if (BPF_SRC(insn->code) == BPF_X) { |
| 6497 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6498 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6499 | return -EINVAL; |
| 6500 | } |
| 6501 | |
| 6502 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6503 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6504 | if (err) |
| 6505 | return err; |
| 6506 | } else { |
| 6507 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6508 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6509 | return -EINVAL; |
| 6510 | } |
| 6511 | } |
| 6512 | |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 6513 | /* check dest operand, mark as required later */ |
| 6514 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6515 | if (err) |
| 6516 | return err; |
| 6517 | |
| 6518 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6519 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
| 6520 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; |
| 6521 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6522 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 6523 | /* case: R1 = R2 |
| 6524 | * copy register state to dest reg |
| 6525 | */ |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6526 | *dst_reg = *src_reg; |
| 6527 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6528 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6529 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6530 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6531 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6532 | verbose(env, |
| 6533 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6534 | insn->src_reg); |
| 6535 | return -EACCES; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6536 | } else if (src_reg->type == SCALAR_VALUE) { |
| 6537 | *dst_reg = *src_reg; |
| 6538 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6539 | dst_reg->subreg_def = env->insn_idx + 1; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 6540 | } else { |
| 6541 | mark_reg_unknown(env, regs, |
| 6542 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6543 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6544 | zext_32_to_64(dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6545 | } |
| 6546 | } else { |
| 6547 | /* case: R = imm |
| 6548 | * remember the value we stored into this reg |
| 6549 | */ |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 6550 | /* clear any state __mark_reg_known doesn't set */ |
| 6551 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6552 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 6553 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 6554 | __mark_reg_known(regs + insn->dst_reg, |
| 6555 | insn->imm); |
| 6556 | } else { |
| 6557 | __mark_reg_known(regs + insn->dst_reg, |
| 6558 | (u32)insn->imm); |
| 6559 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6560 | } |
| 6561 | |
| 6562 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6563 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6564 | return -EINVAL; |
| 6565 | |
| 6566 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 6567 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6568 | if (BPF_SRC(insn->code) == BPF_X) { |
| 6569 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6570 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6571 | return -EINVAL; |
| 6572 | } |
| 6573 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6574 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6575 | if (err) |
| 6576 | return err; |
| 6577 | } else { |
| 6578 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6579 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6580 | return -EINVAL; |
| 6581 | } |
| 6582 | } |
| 6583 | |
| 6584 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6585 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6586 | if (err) |
| 6587 | return err; |
| 6588 | |
| 6589 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 6590 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6591 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6592 | return -EINVAL; |
| 6593 | } |
| 6594 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 6595 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 6596 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 6597 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 6598 | |
| 6599 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6600 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 6601 | return -EINVAL; |
| 6602 | } |
| 6603 | } |
| 6604 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 6605 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6606 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 6607 | if (err) |
| 6608 | return err; |
| 6609 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6610 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6611 | } |
| 6612 | |
| 6613 | return 0; |
| 6614 | } |
| 6615 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 6616 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
| 6617 | struct bpf_reg_state *dst_reg, |
| 6618 | enum bpf_reg_type type, u16 new_range) |
| 6619 | { |
| 6620 | struct bpf_reg_state *reg; |
| 6621 | int i; |
| 6622 | |
| 6623 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 6624 | reg = &state->regs[i]; |
| 6625 | if (reg->type == type && reg->id == dst_reg->id) |
| 6626 | /* keep the maximum range already checked */ |
| 6627 | reg->range = max(reg->range, new_range); |
| 6628 | } |
| 6629 | |
| 6630 | bpf_for_each_spilled_reg(i, state, reg) { |
| 6631 | if (!reg) |
| 6632 | continue; |
| 6633 | if (reg->type == type && reg->id == dst_reg->id) |
| 6634 | reg->range = max(reg->range, new_range); |
| 6635 | } |
| 6636 | } |
| 6637 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6638 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 6639 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 6640 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6641 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6642 | { |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6643 | u16 new_range; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 6644 | int i; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6645 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6646 | if (dst_reg->off < 0 || |
| 6647 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6648 | /* This doesn't give us any range */ |
| 6649 | return; |
| 6650 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6651 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 6652 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6653 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 6654 | * than pkt_end, but that's because it's also less than pkt. |
| 6655 | */ |
| 6656 | return; |
| 6657 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6658 | new_range = dst_reg->off; |
| 6659 | if (range_right_open) |
| 6660 | new_range--; |
| 6661 | |
| 6662 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6663 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6664 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6665 | * |
| 6666 | * r2 = r3; |
| 6667 | * r2 += 8; |
| 6668 | * if (r2 > pkt_end) goto <handle exception> |
| 6669 | * <access okay> |
| 6670 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 6671 | * r2 = r3; |
| 6672 | * r2 += 8; |
| 6673 | * if (r2 < pkt_end) goto <access okay> |
| 6674 | * <handle exception> |
| 6675 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6676 | * Where: |
| 6677 | * r2 == dst_reg, pkt_end == src_reg |
| 6678 | * r2=pkt(id=n,off=8,r=0) |
| 6679 | * r3=pkt(id=n,off=0,r=0) |
| 6680 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 6681 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6682 | * |
| 6683 | * r2 = r3; |
| 6684 | * r2 += 8; |
| 6685 | * if (pkt_end >= r2) goto <access okay> |
| 6686 | * <handle exception> |
| 6687 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 6688 | * r2 = r3; |
| 6689 | * r2 += 8; |
| 6690 | * if (pkt_end <= r2) goto <handle exception> |
| 6691 | * <access okay> |
| 6692 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6693 | * Where: |
| 6694 | * pkt_end == dst_reg, r2 == src_reg |
| 6695 | * r2=pkt(id=n,off=8,r=0) |
| 6696 | * r3=pkt(id=n,off=0,r=0) |
| 6697 | * |
| 6698 | * 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] | 6699 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 6700 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 6701 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6702 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 6703 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6704 | /* If our ids match, then we must have the same max_value. And we |
| 6705 | * don't care about the other reg's fixed offset, since if it's too big |
| 6706 | * the range won't allow anything. |
| 6707 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 6708 | */ |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 6709 | for (i = 0; i <= vstate->curframe; i++) |
| 6710 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, |
| 6711 | new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6712 | } |
| 6713 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6714 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6715 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6716 | struct tnum subreg = tnum_subreg(reg->var_off); |
| 6717 | s32 sval = (s32)val; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6718 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6719 | switch (opcode) { |
| 6720 | case BPF_JEQ: |
| 6721 | if (tnum_is_const(subreg)) |
| 6722 | return !!tnum_equals_const(subreg, val); |
| 6723 | break; |
| 6724 | case BPF_JNE: |
| 6725 | if (tnum_is_const(subreg)) |
| 6726 | return !tnum_equals_const(subreg, val); |
| 6727 | break; |
| 6728 | case BPF_JSET: |
| 6729 | if ((~subreg.mask & subreg.value) & val) |
| 6730 | return 1; |
| 6731 | if (!((subreg.mask | subreg.value) & val)) |
| 6732 | return 0; |
| 6733 | break; |
| 6734 | case BPF_JGT: |
| 6735 | if (reg->u32_min_value > val) |
| 6736 | return 1; |
| 6737 | else if (reg->u32_max_value <= val) |
| 6738 | return 0; |
| 6739 | break; |
| 6740 | case BPF_JSGT: |
| 6741 | if (reg->s32_min_value > sval) |
| 6742 | return 1; |
| 6743 | else if (reg->s32_max_value < sval) |
| 6744 | return 0; |
| 6745 | break; |
| 6746 | case BPF_JLT: |
| 6747 | if (reg->u32_max_value < val) |
| 6748 | return 1; |
| 6749 | else if (reg->u32_min_value >= val) |
| 6750 | return 0; |
| 6751 | break; |
| 6752 | case BPF_JSLT: |
| 6753 | if (reg->s32_max_value < sval) |
| 6754 | return 1; |
| 6755 | else if (reg->s32_min_value >= sval) |
| 6756 | return 0; |
| 6757 | break; |
| 6758 | case BPF_JGE: |
| 6759 | if (reg->u32_min_value >= val) |
| 6760 | return 1; |
| 6761 | else if (reg->u32_max_value < val) |
| 6762 | return 0; |
| 6763 | break; |
| 6764 | case BPF_JSGE: |
| 6765 | if (reg->s32_min_value >= sval) |
| 6766 | return 1; |
| 6767 | else if (reg->s32_max_value < sval) |
| 6768 | return 0; |
| 6769 | break; |
| 6770 | case BPF_JLE: |
| 6771 | if (reg->u32_max_value <= val) |
| 6772 | return 1; |
| 6773 | else if (reg->u32_min_value > val) |
| 6774 | return 0; |
| 6775 | break; |
| 6776 | case BPF_JSLE: |
| 6777 | if (reg->s32_max_value <= sval) |
| 6778 | return 1; |
| 6779 | else if (reg->s32_min_value > sval) |
| 6780 | return 0; |
| 6781 | break; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6782 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6783 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6784 | return -1; |
| 6785 | } |
| 6786 | |
| 6787 | |
| 6788 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) |
| 6789 | { |
| 6790 | s64 sval = (s64)val; |
| 6791 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6792 | switch (opcode) { |
| 6793 | case BPF_JEQ: |
| 6794 | if (tnum_is_const(reg->var_off)) |
| 6795 | return !!tnum_equals_const(reg->var_off, val); |
| 6796 | break; |
| 6797 | case BPF_JNE: |
| 6798 | if (tnum_is_const(reg->var_off)) |
| 6799 | return !tnum_equals_const(reg->var_off, val); |
| 6800 | break; |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 6801 | case BPF_JSET: |
| 6802 | if ((~reg->var_off.mask & reg->var_off.value) & val) |
| 6803 | return 1; |
| 6804 | if (!((reg->var_off.mask | reg->var_off.value) & val)) |
| 6805 | return 0; |
| 6806 | break; |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6807 | case BPF_JGT: |
| 6808 | if (reg->umin_value > val) |
| 6809 | return 1; |
| 6810 | else if (reg->umax_value <= val) |
| 6811 | return 0; |
| 6812 | break; |
| 6813 | case BPF_JSGT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6814 | if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6815 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6816 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6817 | return 0; |
| 6818 | break; |
| 6819 | case BPF_JLT: |
| 6820 | if (reg->umax_value < val) |
| 6821 | return 1; |
| 6822 | else if (reg->umin_value >= val) |
| 6823 | return 0; |
| 6824 | break; |
| 6825 | case BPF_JSLT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6826 | if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6827 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6828 | else if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6829 | return 0; |
| 6830 | break; |
| 6831 | case BPF_JGE: |
| 6832 | if (reg->umin_value >= val) |
| 6833 | return 1; |
| 6834 | else if (reg->umax_value < val) |
| 6835 | return 0; |
| 6836 | break; |
| 6837 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6838 | if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6839 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6840 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6841 | return 0; |
| 6842 | break; |
| 6843 | case BPF_JLE: |
| 6844 | if (reg->umax_value <= val) |
| 6845 | return 1; |
| 6846 | else if (reg->umin_value > val) |
| 6847 | return 0; |
| 6848 | break; |
| 6849 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6850 | if (reg->smax_value <= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6851 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6852 | else if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 6853 | return 0; |
| 6854 | break; |
| 6855 | } |
| 6856 | |
| 6857 | return -1; |
| 6858 | } |
| 6859 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6860 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
| 6861 | * and return: |
| 6862 | * 1 - branch will be taken and "goto target" will be executed |
| 6863 | * 0 - branch will not be taken and fall-through to next insn |
| 6864 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value |
| 6865 | * range [0,10] |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6866 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6867 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
| 6868 | bool is_jmp32) |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6869 | { |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 6870 | if (__is_pointer_value(false, reg)) { |
| 6871 | if (!reg_type_not_null(reg->type)) |
| 6872 | return -1; |
| 6873 | |
| 6874 | /* If pointer is valid tests against zero will fail so we can |
| 6875 | * use this to direct branch taken. |
| 6876 | */ |
| 6877 | if (val != 0) |
| 6878 | return -1; |
| 6879 | |
| 6880 | switch (opcode) { |
| 6881 | case BPF_JEQ: |
| 6882 | return 0; |
| 6883 | case BPF_JNE: |
| 6884 | return 1; |
| 6885 | default: |
| 6886 | return -1; |
| 6887 | } |
| 6888 | } |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6889 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6890 | if (is_jmp32) |
| 6891 | return is_branch32_taken(reg, val, opcode); |
| 6892 | return is_branch64_taken(reg, val, opcode); |
Jann Horn | 604dca5 | 2020-03-30 18:03:23 +0200 | [diff] [blame] | 6893 | } |
| 6894 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6895 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 6896 | * variable register that we are working on, and src_reg is a constant or we're |
| 6897 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6898 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6899 | */ |
| 6900 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6901 | struct bpf_reg_state *false_reg, |
| 6902 | u64 val, u32 val32, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6903 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6904 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6905 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
| 6906 | struct tnum false_64off = false_reg->var_off; |
| 6907 | struct tnum true_32off = tnum_subreg(true_reg->var_off); |
| 6908 | struct tnum true_64off = true_reg->var_off; |
| 6909 | s64 sval = (s64)val; |
| 6910 | s32 sval32 = (s32)val32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6911 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6912 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 6913 | * variable offset from the compare (unless src_reg were a pointer into |
| 6914 | * the same object, but we don't bother with that. |
| 6915 | * Since false_reg and true_reg have the same type by construction, we |
| 6916 | * only need to check one of them for pointerness. |
| 6917 | */ |
| 6918 | if (__is_pointer_value(false, false_reg)) |
| 6919 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 6920 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6921 | switch (opcode) { |
| 6922 | case BPF_JEQ: |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6923 | case BPF_JNE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6924 | { |
| 6925 | struct bpf_reg_state *reg = |
| 6926 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 6927 | |
| 6928 | /* For BPF_JEQ, if this is false we know nothing Jon Snow, but |
| 6929 | * if it is true we know the value for sure. Likewise for |
| 6930 | * BPF_JNE. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6931 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6932 | if (is_jmp32) |
| 6933 | __mark_reg32_known(reg, val32); |
| 6934 | else |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6935 | __mark_reg_known(reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6936 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6937 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 6938 | case BPF_JSET: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6939 | if (is_jmp32) { |
| 6940 | false_32off = tnum_and(false_32off, tnum_const(~val32)); |
| 6941 | if (is_power_of_2(val32)) |
| 6942 | true_32off = tnum_or(true_32off, |
| 6943 | tnum_const(val32)); |
| 6944 | } else { |
| 6945 | false_64off = tnum_and(false_64off, tnum_const(~val)); |
| 6946 | if (is_power_of_2(val)) |
| 6947 | true_64off = tnum_or(true_64off, |
| 6948 | tnum_const(val)); |
| 6949 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 6950 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6951 | case BPF_JGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6952 | case BPF_JGT: |
| 6953 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6954 | if (is_jmp32) { |
| 6955 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; |
| 6956 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; |
| 6957 | |
| 6958 | false_reg->u32_max_value = min(false_reg->u32_max_value, |
| 6959 | false_umax); |
| 6960 | true_reg->u32_min_value = max(true_reg->u32_min_value, |
| 6961 | true_umin); |
| 6962 | } else { |
| 6963 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; |
| 6964 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; |
| 6965 | |
| 6966 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 6967 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
| 6968 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6969 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6970 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6971 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6972 | case BPF_JSGT: |
| 6973 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6974 | if (is_jmp32) { |
| 6975 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; |
| 6976 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6977 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6978 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
| 6979 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); |
| 6980 | } else { |
| 6981 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; |
| 6982 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; |
| 6983 | |
| 6984 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 6985 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
| 6986 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 6987 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6988 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 6989 | case BPF_JLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 6990 | case BPF_JLT: |
| 6991 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6992 | if (is_jmp32) { |
| 6993 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; |
| 6994 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; |
| 6995 | |
| 6996 | false_reg->u32_min_value = max(false_reg->u32_min_value, |
| 6997 | false_umin); |
| 6998 | true_reg->u32_max_value = min(true_reg->u32_max_value, |
| 6999 | true_umax); |
| 7000 | } else { |
| 7001 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; |
| 7002 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; |
| 7003 | |
| 7004 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 7005 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
| 7006 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 7007 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7008 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 7009 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7010 | case BPF_JSLT: |
| 7011 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7012 | if (is_jmp32) { |
| 7013 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; |
| 7014 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7015 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7016 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
| 7017 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); |
| 7018 | } else { |
| 7019 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; |
| 7020 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; |
| 7021 | |
| 7022 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 7023 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
| 7024 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 7025 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 7026 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7027 | default: |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 7028 | return; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7029 | } |
| 7030 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7031 | if (is_jmp32) { |
| 7032 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), |
| 7033 | tnum_subreg(false_32off)); |
| 7034 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), |
| 7035 | tnum_subreg(true_32off)); |
| 7036 | __reg_combine_32_into_64(false_reg); |
| 7037 | __reg_combine_32_into_64(true_reg); |
| 7038 | } else { |
| 7039 | false_reg->var_off = false_64off; |
| 7040 | true_reg->var_off = true_64off; |
| 7041 | __reg_combine_64_into_32(false_reg); |
| 7042 | __reg_combine_64_into_32(true_reg); |
| 7043 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7044 | } |
| 7045 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7046 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 7047 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7048 | */ |
| 7049 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7050 | struct bpf_reg_state *false_reg, |
| 7051 | u64 val, u32 val32, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7052 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7053 | { |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 7054 | /* How can we transform "a <op> b" into "b <op> a"? */ |
| 7055 | static const u8 opcode_flip[16] = { |
| 7056 | /* these stay the same */ |
| 7057 | [BPF_JEQ >> 4] = BPF_JEQ, |
| 7058 | [BPF_JNE >> 4] = BPF_JNE, |
| 7059 | [BPF_JSET >> 4] = BPF_JSET, |
| 7060 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ |
| 7061 | [BPF_JGE >> 4] = BPF_JLE, |
| 7062 | [BPF_JGT >> 4] = BPF_JLT, |
| 7063 | [BPF_JLE >> 4] = BPF_JGE, |
| 7064 | [BPF_JLT >> 4] = BPF_JGT, |
| 7065 | [BPF_JSGE >> 4] = BPF_JSLE, |
| 7066 | [BPF_JSGT >> 4] = BPF_JSLT, |
| 7067 | [BPF_JSLE >> 4] = BPF_JSGE, |
| 7068 | [BPF_JSLT >> 4] = BPF_JSGT |
| 7069 | }; |
| 7070 | opcode = opcode_flip[opcode >> 4]; |
| 7071 | /* This uses zero as "not present in table"; luckily the zero opcode, |
| 7072 | * BPF_JA, can't get here. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7073 | */ |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 7074 | if (opcode) |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7075 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7076 | } |
| 7077 | |
| 7078 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 7079 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 7080 | struct bpf_reg_state *dst_reg) |
| 7081 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7082 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 7083 | dst_reg->umin_value); |
| 7084 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 7085 | dst_reg->umax_value); |
| 7086 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 7087 | dst_reg->smin_value); |
| 7088 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 7089 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7090 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 7091 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7092 | /* We might have learned new bounds from the var_off. */ |
| 7093 | __update_reg_bounds(src_reg); |
| 7094 | __update_reg_bounds(dst_reg); |
| 7095 | /* We might have learned something about the sign bit. */ |
| 7096 | __reg_deduce_bounds(src_reg); |
| 7097 | __reg_deduce_bounds(dst_reg); |
| 7098 | /* We might have learned some bits from the bounds. */ |
| 7099 | __reg_bound_offset(src_reg); |
| 7100 | __reg_bound_offset(dst_reg); |
| 7101 | /* Intersecting with the old var_off might have improved our bounds |
| 7102 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 7103 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 7104 | */ |
| 7105 | __update_reg_bounds(src_reg); |
| 7106 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7107 | } |
| 7108 | |
| 7109 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 7110 | struct bpf_reg_state *true_dst, |
| 7111 | struct bpf_reg_state *false_src, |
| 7112 | struct bpf_reg_state *false_dst, |
| 7113 | u8 opcode) |
| 7114 | { |
| 7115 | switch (opcode) { |
| 7116 | case BPF_JEQ: |
| 7117 | __reg_combine_min_max(true_src, true_dst); |
| 7118 | break; |
| 7119 | case BPF_JNE: |
| 7120 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7121 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 7122 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7123 | } |
| 7124 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7125 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 7126 | struct bpf_reg_state *reg, u32 id, |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7127 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7128 | { |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7129 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7130 | /* Old offset (both fixed and variable parts) should |
| 7131 | * have been known-zero, because we don't allow pointer |
| 7132 | * arithmetic on pointers that might be NULL. |
| 7133 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7134 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 7135 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7136 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7137 | __mark_reg_known_zero(reg); |
| 7138 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7139 | } |
| 7140 | if (is_null) { |
| 7141 | reg->type = SCALAR_VALUE; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7142 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 7143 | const struct bpf_map *map = reg->map_ptr; |
| 7144 | |
| 7145 | if (map->inner_map_meta) { |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7146 | reg->type = CONST_PTR_TO_MAP; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 7147 | reg->map_ptr = map->inner_map_meta; |
| 7148 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 7149 | reg->type = PTR_TO_XDP_SOCK; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 7150 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || |
| 7151 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { |
| 7152 | reg->type = PTR_TO_SOCKET; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7153 | } else { |
| 7154 | reg->type = PTR_TO_MAP_VALUE; |
| 7155 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7156 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
| 7157 | reg->type = PTR_TO_SOCKET; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 7158 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
| 7159 | reg->type = PTR_TO_SOCK_COMMON; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 7160 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
| 7161 | reg->type = PTR_TO_TCP_SOCK; |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 7162 | } else if (reg->type == PTR_TO_BTF_ID_OR_NULL) { |
| 7163 | reg->type = PTR_TO_BTF_ID; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 7164 | } else if (reg->type == PTR_TO_MEM_OR_NULL) { |
| 7165 | reg->type = PTR_TO_MEM; |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 7166 | } else if (reg->type == PTR_TO_RDONLY_BUF_OR_NULL) { |
| 7167 | reg->type = PTR_TO_RDONLY_BUF; |
| 7168 | } else if (reg->type == PTR_TO_RDWR_BUF_OR_NULL) { |
| 7169 | reg->type = PTR_TO_RDWR_BUF; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 7170 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 7171 | if (is_null) { |
| 7172 | /* We don't need id and ref_obj_id from this point |
| 7173 | * onwards anymore, thus we should better reset it, |
| 7174 | * so that state pruning has chances to take effect. |
| 7175 | */ |
| 7176 | reg->id = 0; |
| 7177 | reg->ref_obj_id = 0; |
| 7178 | } else if (!reg_may_point_to_spin_lock(reg)) { |
| 7179 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
| 7180 | * in release_reg_references(). |
| 7181 | * |
| 7182 | * reg->id is still used by spin_lock ptr. Other |
| 7183 | * than spin_lock ptr type, reg->id can be reset. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7184 | */ |
| 7185 | reg->id = 0; |
| 7186 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7187 | } |
| 7188 | } |
| 7189 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 7190 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
| 7191 | bool is_null) |
| 7192 | { |
| 7193 | struct bpf_reg_state *reg; |
| 7194 | int i; |
| 7195 | |
| 7196 | for (i = 0; i < MAX_BPF_REG; i++) |
| 7197 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); |
| 7198 | |
| 7199 | bpf_for_each_spilled_reg(i, state, reg) { |
| 7200 | if (!reg) |
| 7201 | continue; |
| 7202 | mark_ptr_or_null_reg(state, reg, id, is_null); |
| 7203 | } |
| 7204 | } |
| 7205 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7206 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 7207 | * be folded together at some point. |
| 7208 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7209 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 7210 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7211 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7212 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 7213 | struct bpf_reg_state *regs = state->regs; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 7214 | u32 ref_obj_id = regs[regno].ref_obj_id; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 7215 | u32 id = regs[regno].id; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 7216 | int i; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7217 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 7218 | if (ref_obj_id && ref_obj_id == id && is_null) |
| 7219 | /* regs[regno] is in the " == NULL" branch. |
| 7220 | * No one could have freed the reference state before |
| 7221 | * doing the NULL check. |
| 7222 | */ |
| 7223 | WARN_ON_ONCE(release_reference_state(state, id)); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7224 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 7225 | for (i = 0; i <= vstate->curframe; i++) |
| 7226 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7227 | } |
| 7228 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7229 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 7230 | struct bpf_reg_state *dst_reg, |
| 7231 | struct bpf_reg_state *src_reg, |
| 7232 | struct bpf_verifier_state *this_branch, |
| 7233 | struct bpf_verifier_state *other_branch) |
| 7234 | { |
| 7235 | if (BPF_SRC(insn->code) != BPF_X) |
| 7236 | return false; |
| 7237 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7238 | /* Pointers are always 64-bit. */ |
| 7239 | if (BPF_CLASS(insn->code) == BPF_JMP32) |
| 7240 | return false; |
| 7241 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7242 | switch (BPF_OP(insn->code)) { |
| 7243 | case BPF_JGT: |
| 7244 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7245 | src_reg->type == PTR_TO_PACKET_END) || |
| 7246 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7247 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7248 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 7249 | find_good_pkt_pointers(this_branch, dst_reg, |
| 7250 | dst_reg->type, false); |
| 7251 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7252 | src_reg->type == PTR_TO_PACKET) || |
| 7253 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7254 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7255 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 7256 | find_good_pkt_pointers(other_branch, src_reg, |
| 7257 | src_reg->type, true); |
| 7258 | } else { |
| 7259 | return false; |
| 7260 | } |
| 7261 | break; |
| 7262 | case BPF_JLT: |
| 7263 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7264 | src_reg->type == PTR_TO_PACKET_END) || |
| 7265 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7266 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7267 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 7268 | find_good_pkt_pointers(other_branch, dst_reg, |
| 7269 | dst_reg->type, true); |
| 7270 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7271 | src_reg->type == PTR_TO_PACKET) || |
| 7272 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7273 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7274 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 7275 | find_good_pkt_pointers(this_branch, src_reg, |
| 7276 | src_reg->type, false); |
| 7277 | } else { |
| 7278 | return false; |
| 7279 | } |
| 7280 | break; |
| 7281 | case BPF_JGE: |
| 7282 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7283 | src_reg->type == PTR_TO_PACKET_END) || |
| 7284 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7285 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7286 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 7287 | find_good_pkt_pointers(this_branch, dst_reg, |
| 7288 | dst_reg->type, true); |
| 7289 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7290 | src_reg->type == PTR_TO_PACKET) || |
| 7291 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7292 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7293 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 7294 | find_good_pkt_pointers(other_branch, src_reg, |
| 7295 | src_reg->type, false); |
| 7296 | } else { |
| 7297 | return false; |
| 7298 | } |
| 7299 | break; |
| 7300 | case BPF_JLE: |
| 7301 | if ((dst_reg->type == PTR_TO_PACKET && |
| 7302 | src_reg->type == PTR_TO_PACKET_END) || |
| 7303 | (dst_reg->type == PTR_TO_PACKET_META && |
| 7304 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 7305 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 7306 | find_good_pkt_pointers(other_branch, dst_reg, |
| 7307 | dst_reg->type, false); |
| 7308 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 7309 | src_reg->type == PTR_TO_PACKET) || |
| 7310 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 7311 | src_reg->type == PTR_TO_PACKET_META)) { |
| 7312 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 7313 | find_good_pkt_pointers(this_branch, src_reg, |
| 7314 | src_reg->type, true); |
| 7315 | } else { |
| 7316 | return false; |
| 7317 | } |
| 7318 | break; |
| 7319 | default: |
| 7320 | return false; |
| 7321 | } |
| 7322 | |
| 7323 | return true; |
| 7324 | } |
| 7325 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7326 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7327 | struct bpf_insn *insn, int *insn_idx) |
| 7328 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7329 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 7330 | struct bpf_verifier_state *other_branch; |
| 7331 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 7332 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7333 | u8 opcode = BPF_OP(insn->code); |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7334 | bool is_jmp32; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 7335 | int pred = -1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7336 | int err; |
| 7337 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7338 | /* Only conditional jumps are expected to reach here. */ |
| 7339 | if (opcode == BPF_JA || opcode > BPF_JSLE) { |
| 7340 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7341 | return -EINVAL; |
| 7342 | } |
| 7343 | |
| 7344 | if (BPF_SRC(insn->code) == BPF_X) { |
| 7345 | if (insn->imm != 0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7346 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7347 | return -EINVAL; |
| 7348 | } |
| 7349 | |
| 7350 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7351 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7352 | if (err) |
| 7353 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7354 | |
| 7355 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7356 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7357 | insn->src_reg); |
| 7358 | return -EACCES; |
| 7359 | } |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 7360 | src_reg = ®s[insn->src_reg]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7361 | } else { |
| 7362 | if (insn->src_reg != BPF_REG_0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7363 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7364 | return -EINVAL; |
| 7365 | } |
| 7366 | } |
| 7367 | |
| 7368 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7369 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7370 | if (err) |
| 7371 | return err; |
| 7372 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 7373 | dst_reg = ®s[insn->dst_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7374 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 7375 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7376 | if (BPF_SRC(insn->code) == BPF_K) { |
| 7377 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); |
| 7378 | } else if (src_reg->type == SCALAR_VALUE && |
| 7379 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { |
| 7380 | pred = is_branch_taken(dst_reg, |
| 7381 | tnum_subreg(src_reg->var_off).value, |
| 7382 | opcode, |
| 7383 | is_jmp32); |
| 7384 | } else if (src_reg->type == SCALAR_VALUE && |
| 7385 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { |
| 7386 | pred = is_branch_taken(dst_reg, |
| 7387 | src_reg->var_off.value, |
| 7388 | opcode, |
| 7389 | is_jmp32); |
| 7390 | } |
| 7391 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7392 | if (pred >= 0) { |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 7393 | /* If we get here with a dst_reg pointer type it is because |
| 7394 | * above is_branch_taken() special cased the 0 comparison. |
| 7395 | */ |
| 7396 | if (!__is_pointer_value(false, dst_reg)) |
| 7397 | err = mark_chain_precision(env, insn->dst_reg); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7398 | if (BPF_SRC(insn->code) == BPF_X && !err) |
| 7399 | err = mark_chain_precision(env, insn->src_reg); |
| 7400 | if (err) |
| 7401 | return err; |
| 7402 | } |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 7403 | if (pred == 1) { |
| 7404 | /* only follow the goto, ignore fall-through */ |
| 7405 | *insn_idx += insn->off; |
| 7406 | return 0; |
| 7407 | } else if (pred == 0) { |
| 7408 | /* only follow fall-through branch, since |
| 7409 | * that's where the program will go |
| 7410 | */ |
| 7411 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7412 | } |
| 7413 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7414 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
| 7415 | false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7416 | if (!other_branch) |
| 7417 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7418 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7419 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7420 | /* detect if we are comparing against a constant value so we can adjust |
| 7421 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7422 | * this is only legit if both are scalars (or pointers to the same |
| 7423 | * object, I suppose, but we don't support that right now), because |
| 7424 | * otherwise the different base pointers mean the offsets aren't |
| 7425 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7426 | */ |
| 7427 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7428 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7429 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7430 | if (dst_reg->type == SCALAR_VALUE && |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7431 | src_reg->type == SCALAR_VALUE) { |
| 7432 | if (tnum_is_const(src_reg->var_off) || |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7433 | (is_jmp32 && |
| 7434 | tnum_is_const(tnum_subreg(src_reg->var_off)))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7435 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7436 | dst_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7437 | src_reg->var_off.value, |
| 7438 | tnum_subreg(src_reg->var_off).value, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7439 | opcode, is_jmp32); |
| 7440 | else if (tnum_is_const(dst_reg->var_off) || |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7441 | (is_jmp32 && |
| 7442 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7443 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7444 | src_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7445 | dst_reg->var_off.value, |
| 7446 | tnum_subreg(dst_reg->var_off).value, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7447 | opcode, is_jmp32); |
| 7448 | else if (!is_jmp32 && |
| 7449 | (opcode == BPF_JEQ || opcode == BPF_JNE)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7450 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7451 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 7452 | &other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7453 | src_reg, dst_reg, opcode); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7454 | } |
| 7455 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7456 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7457 | dst_reg, insn->imm, (u32)insn->imm, |
| 7458 | opcode, is_jmp32); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7459 | } |
| 7460 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7461 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
| 7462 | * NOTE: these optimizations below are related with pointer comparison |
| 7463 | * which will never be JMP32. |
| 7464 | */ |
| 7465 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 7466 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7467 | reg_type_may_be_null(dst_reg->type)) { |
| 7468 | /* Mark all identical registers in each branch as either |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 7469 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 7470 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 7471 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 7472 | opcode == BPF_JNE); |
| 7473 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 7474 | opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 7475 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 7476 | this_branch, other_branch) && |
| 7477 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7478 | verbose(env, "R%d pointer comparison prohibited\n", |
| 7479 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7480 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7481 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 7482 | if (env->log.level & BPF_LOG_LEVEL) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7483 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7484 | return 0; |
| 7485 | } |
| 7486 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7487 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7488 | 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] | 7489 | { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7490 | struct bpf_insn_aux_data *aux = cur_aux(env); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7491 | struct bpf_reg_state *regs = cur_regs(env); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7492 | struct bpf_map *map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7493 | int err; |
| 7494 | |
| 7495 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7496 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7497 | return -EINVAL; |
| 7498 | } |
| 7499 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7500 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7501 | return -EINVAL; |
| 7502 | } |
| 7503 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7504 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7505 | if (err) |
| 7506 | return err; |
| 7507 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 7508 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 7509 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 7510 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7511 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7512 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7513 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 7514 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7515 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7516 | map = env->used_maps[aux->map_index]; |
| 7517 | mark_reg_known_zero(env, regs, insn->dst_reg); |
| 7518 | regs[insn->dst_reg].map_ptr = map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7519 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7520 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { |
| 7521 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; |
| 7522 | regs[insn->dst_reg].off = aux->map_off; |
| 7523 | if (map_value_has_spin_lock(map)) |
| 7524 | regs[insn->dst_reg].id = ++env->id_gen; |
| 7525 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 7526 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 7527 | } else { |
| 7528 | verbose(env, "bpf verifier is misconfigured\n"); |
| 7529 | return -EINVAL; |
| 7530 | } |
| 7531 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7532 | return 0; |
| 7533 | } |
| 7534 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 7535 | static bool may_access_skb(enum bpf_prog_type type) |
| 7536 | { |
| 7537 | switch (type) { |
| 7538 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 7539 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 7540 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 7541 | return true; |
| 7542 | default: |
| 7543 | return false; |
| 7544 | } |
| 7545 | } |
| 7546 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7547 | /* verify safety of LD_ABS|LD_IND instructions: |
| 7548 | * - they can only appear in the programs where ctx == skb |
| 7549 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 7550 | * preserve R6-R9, and store return value into R0 |
| 7551 | * |
| 7552 | * Implicit input: |
| 7553 | * ctx == skb == R6 == CTX |
| 7554 | * |
| 7555 | * Explicit input: |
| 7556 | * SRC == any register |
| 7557 | * IMM == 32-bit immediate |
| 7558 | * |
| 7559 | * Output: |
| 7560 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 7561 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7562 | 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] | 7563 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7564 | struct bpf_reg_state *regs = cur_regs(env); |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 7565 | static const int ctx_reg = BPF_REG_6; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7566 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7567 | int i, err; |
| 7568 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 7569 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7570 | 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] | 7571 | return -EINVAL; |
| 7572 | } |
| 7573 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 7574 | if (!env->ops->gen_ld_abs) { |
| 7575 | verbose(env, "bpf verifier is misconfigured\n"); |
| 7576 | return -EINVAL; |
| 7577 | } |
| 7578 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7579 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 7580 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7581 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7582 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7583 | return -EINVAL; |
| 7584 | } |
| 7585 | |
| 7586 | /* check whether implicit source operand (register R6) is readable */ |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 7587 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7588 | if (err) |
| 7589 | return err; |
| 7590 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7591 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 7592 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 7593 | * reference leak. |
| 7594 | */ |
| 7595 | err = check_reference_leak(env); |
| 7596 | if (err) { |
| 7597 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 7598 | return err; |
| 7599 | } |
| 7600 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 7601 | if (env->cur_state->active_spin_lock) { |
| 7602 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
| 7603 | return -EINVAL; |
| 7604 | } |
| 7605 | |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 7606 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7607 | verbose(env, |
| 7608 | "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] | 7609 | return -EINVAL; |
| 7610 | } |
| 7611 | |
| 7612 | if (mode == BPF_IND) { |
| 7613 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7614 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7615 | if (err) |
| 7616 | return err; |
| 7617 | } |
| 7618 | |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 7619 | err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg); |
| 7620 | if (err < 0) |
| 7621 | return err; |
| 7622 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7623 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7624 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7625 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7626 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 7627 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7628 | |
| 7629 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7630 | * the value fetched from the packet. |
| 7631 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7632 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7633 | mark_reg_unknown(env, regs, BPF_REG_0); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 7634 | /* ld_abs load up to 32-bit skb data. */ |
| 7635 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7636 | return 0; |
| 7637 | } |
| 7638 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7639 | static int check_return_code(struct bpf_verifier_env *env) |
| 7640 | { |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 7641 | struct tnum enforce_attach_type_range = tnum_unknown; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 7642 | const struct bpf_prog *prog = env->prog; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7643 | struct bpf_reg_state *reg; |
| 7644 | struct tnum range = tnum_range(0, 1); |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 7645 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 7646 | int err; |
| 7647 | |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 7648 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 7649 | if ((prog_type == BPF_PROG_TYPE_STRUCT_OPS || |
| 7650 | prog_type == BPF_PROG_TYPE_LSM) && |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 7651 | !prog->aux->attach_func_proto->type) |
| 7652 | return 0; |
| 7653 | |
| 7654 | /* eBPF calling convetion is such that R0 is used |
| 7655 | * to return the value from eBPF program. |
| 7656 | * Make sure that it's readable at this time |
| 7657 | * of bpf_exit, which means that program wrote |
| 7658 | * something into it earlier |
| 7659 | */ |
| 7660 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
| 7661 | if (err) |
| 7662 | return err; |
| 7663 | |
| 7664 | if (is_pointer_value(env, BPF_REG_0)) { |
| 7665 | verbose(env, "R0 leaks addr as return value\n"); |
| 7666 | return -EACCES; |
| 7667 | } |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7668 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 7669 | switch (prog_type) { |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 7670 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 7671 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || |
Daniel Borkmann | 1b66d25 | 2020-05-19 00:45:45 +0200 | [diff] [blame] | 7672 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
| 7673 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || |
| 7674 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || |
| 7675 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || |
| 7676 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 7677 | range = tnum_range(1, 1); |
Gustavo A. R. Silva | ed4ed40 | 2019-07-11 11:22:33 -0500 | [diff] [blame] | 7678 | break; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7679 | case BPF_PROG_TYPE_CGROUP_SKB: |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 7680 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
| 7681 | range = tnum_range(0, 3); |
| 7682 | enforce_attach_type_range = tnum_range(2, 3); |
| 7683 | } |
Gustavo A. R. Silva | ed4ed40 | 2019-07-11 11:22:33 -0500 | [diff] [blame] | 7684 | break; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7685 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 7686 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 7687 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 7688 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 7689 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7690 | break; |
Alexei Starovoitov | 15ab09b | 2019-10-28 20:24:26 -0700 | [diff] [blame] | 7691 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 7692 | if (!env->prog->aux->attach_btf_id) |
| 7693 | return 0; |
| 7694 | range = tnum_const(0); |
| 7695 | break; |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 7696 | case BPF_PROG_TYPE_TRACING: |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 7697 | switch (env->prog->expected_attach_type) { |
| 7698 | case BPF_TRACE_FENTRY: |
| 7699 | case BPF_TRACE_FEXIT: |
| 7700 | range = tnum_const(0); |
| 7701 | break; |
| 7702 | case BPF_TRACE_RAW_TP: |
| 7703 | case BPF_MODIFY_RETURN: |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 7704 | return 0; |
Daniel Borkmann | 2ec0616 | 2020-05-16 00:39:18 +0200 | [diff] [blame] | 7705 | case BPF_TRACE_ITER: |
| 7706 | break; |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 7707 | default: |
| 7708 | return -ENOTSUPP; |
| 7709 | } |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 7710 | break; |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 7711 | case BPF_PROG_TYPE_SK_LOOKUP: |
| 7712 | range = tnum_range(SK_DROP, SK_PASS); |
| 7713 | break; |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 7714 | case BPF_PROG_TYPE_EXT: |
| 7715 | /* freplace program can return anything as its return value |
| 7716 | * depends on the to-be-replaced kernel func or bpf program. |
| 7717 | */ |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7718 | default: |
| 7719 | return 0; |
| 7720 | } |
| 7721 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7722 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7723 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7724 | 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] | 7725 | reg_type_str[reg->type]); |
| 7726 | return -EINVAL; |
| 7727 | } |
| 7728 | |
| 7729 | if (!tnum_in(range, reg->var_off)) { |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 7730 | char tn_buf[48]; |
| 7731 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7732 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7733 | if (!tnum_is_unknown(reg->var_off)) { |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7734 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7735 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7736 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7737 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7738 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 7739 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 7740 | verbose(env, " should have been in %s\n", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7741 | return -EINVAL; |
| 7742 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 7743 | |
| 7744 | if (!tnum_is_unknown(enforce_attach_type_range) && |
| 7745 | tnum_in(enforce_attach_type_range, reg->var_off)) |
| 7746 | env->prog->enforce_expected_attach_type = 1; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7747 | return 0; |
| 7748 | } |
| 7749 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7750 | /* non-recursive DFS pseudo code |
| 7751 | * 1 procedure DFS-iterative(G,v): |
| 7752 | * 2 label v as discovered |
| 7753 | * 3 let S be a stack |
| 7754 | * 4 S.push(v) |
| 7755 | * 5 while S is not empty |
| 7756 | * 6 t <- S.pop() |
| 7757 | * 7 if t is what we're looking for: |
| 7758 | * 8 return t |
| 7759 | * 9 for all edges e in G.adjacentEdges(t) do |
| 7760 | * 10 if edge e is already labelled |
| 7761 | * 11 continue with the next edge |
| 7762 | * 12 w <- G.adjacentVertex(t,e) |
| 7763 | * 13 if vertex w is not discovered and not explored |
| 7764 | * 14 label e as tree-edge |
| 7765 | * 15 label w as discovered |
| 7766 | * 16 S.push(w) |
| 7767 | * 17 continue at 5 |
| 7768 | * 18 else if vertex w is discovered |
| 7769 | * 19 label e as back-edge |
| 7770 | * 20 else |
| 7771 | * 21 // vertex w is explored |
| 7772 | * 22 label e as forward- or cross-edge |
| 7773 | * 23 label t as explored |
| 7774 | * 24 S.pop() |
| 7775 | * |
| 7776 | * convention: |
| 7777 | * 0x10 - discovered |
| 7778 | * 0x11 - discovered and fall-through edge labelled |
| 7779 | * 0x12 - discovered and fall-through and branch edges labelled |
| 7780 | * 0x20 - explored |
| 7781 | */ |
| 7782 | |
| 7783 | enum { |
| 7784 | DISCOVERED = 0x10, |
| 7785 | EXPLORED = 0x20, |
| 7786 | FALLTHROUGH = 1, |
| 7787 | BRANCH = 2, |
| 7788 | }; |
| 7789 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 7790 | static u32 state_htab_size(struct bpf_verifier_env *env) |
| 7791 | { |
| 7792 | return env->prog->len; |
| 7793 | } |
| 7794 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7795 | static struct bpf_verifier_state_list **explored_state( |
| 7796 | struct bpf_verifier_env *env, |
| 7797 | int idx) |
| 7798 | { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 7799 | struct bpf_verifier_state *cur = env->cur_state; |
| 7800 | struct bpf_func_state *state = cur->frame[cur->curframe]; |
| 7801 | |
| 7802 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7803 | } |
| 7804 | |
| 7805 | static void init_explored_state(struct bpf_verifier_env *env, int idx) |
| 7806 | { |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 7807 | env->insn_aux_data[idx].prune_point = true; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7808 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7809 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7810 | /* t, w, e - match pseudo-code above: |
| 7811 | * t - index of current instruction |
| 7812 | * w - next instruction |
| 7813 | * e - edge |
| 7814 | */ |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7815 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
| 7816 | bool loop_ok) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7817 | { |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7818 | int *insn_stack = env->cfg.insn_stack; |
| 7819 | int *insn_state = env->cfg.insn_state; |
| 7820 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7821 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 7822 | return 0; |
| 7823 | |
| 7824 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 7825 | return 0; |
| 7826 | |
| 7827 | if (w < 0 || w >= env->prog->len) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 7828 | verbose_linfo(env, t, "%d: ", t); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7829 | 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] | 7830 | return -EINVAL; |
| 7831 | } |
| 7832 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7833 | if (e == BRANCH) |
| 7834 | /* mark branch target for state pruning */ |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7835 | init_explored_state(env, w); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7836 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7837 | if (insn_state[w] == 0) { |
| 7838 | /* tree-edge */ |
| 7839 | insn_state[t] = DISCOVERED | e; |
| 7840 | insn_state[w] = DISCOVERED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7841 | if (env->cfg.cur_stack >= env->prog->len) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7842 | return -E2BIG; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7843 | insn_stack[env->cfg.cur_stack++] = w; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7844 | return 1; |
| 7845 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 7846 | if (loop_ok && env->bpf_capable) |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7847 | return 0; |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 7848 | verbose_linfo(env, t, "%d: ", t); |
| 7849 | verbose_linfo(env, w, "%d: ", w); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7850 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7851 | return -EINVAL; |
| 7852 | } else if (insn_state[w] == EXPLORED) { |
| 7853 | /* forward- or cross-edge */ |
| 7854 | insn_state[t] = DISCOVERED | e; |
| 7855 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7856 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7857 | return -EFAULT; |
| 7858 | } |
| 7859 | return 0; |
| 7860 | } |
| 7861 | |
| 7862 | /* non-recursive depth-first-search to detect loops in BPF program |
| 7863 | * loop == back-edge in directed graph |
| 7864 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7865 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7866 | { |
| 7867 | struct bpf_insn *insns = env->prog->insnsi; |
| 7868 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7869 | int *insn_stack, *insn_state; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7870 | int ret = 0; |
| 7871 | int i, t; |
| 7872 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7873 | 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] | 7874 | if (!insn_state) |
| 7875 | return -ENOMEM; |
| 7876 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7877 | 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] | 7878 | if (!insn_stack) { |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 7879 | kvfree(insn_state); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7880 | return -ENOMEM; |
| 7881 | } |
| 7882 | |
| 7883 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 7884 | insn_stack[0] = 0; /* 0 is the first instruction */ |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7885 | env->cfg.cur_stack = 1; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7886 | |
| 7887 | peek_stack: |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7888 | if (env->cfg.cur_stack == 0) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7889 | goto check_state; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7890 | t = insn_stack[env->cfg.cur_stack - 1]; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7891 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7892 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
| 7893 | BPF_CLASS(insns[t].code) == BPF_JMP32) { |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7894 | u8 opcode = BPF_OP(insns[t].code); |
| 7895 | |
| 7896 | if (opcode == BPF_EXIT) { |
| 7897 | goto mark_explored; |
| 7898 | } else if (opcode == BPF_CALL) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7899 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7900 | if (ret == 1) |
| 7901 | goto peek_stack; |
| 7902 | else if (ret < 0) |
| 7903 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 7904 | if (t + 1 < insn_cnt) |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7905 | init_explored_state(env, t + 1); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7906 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7907 | init_explored_state(env, t); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7908 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, |
| 7909 | env, false); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7910 | if (ret == 1) |
| 7911 | goto peek_stack; |
| 7912 | else if (ret < 0) |
| 7913 | goto err_free; |
| 7914 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7915 | } else if (opcode == BPF_JA) { |
| 7916 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 7917 | ret = -EINVAL; |
| 7918 | goto err_free; |
| 7919 | } |
| 7920 | /* unconditional jump with single edge */ |
| 7921 | ret = push_insn(t, t + insns[t].off + 1, |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7922 | FALLTHROUGH, env, true); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7923 | if (ret == 1) |
| 7924 | goto peek_stack; |
| 7925 | else if (ret < 0) |
| 7926 | goto err_free; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 7927 | /* unconditional jmp is not a good pruning point, |
| 7928 | * but it's marked, since backtracking needs |
| 7929 | * to record jmp history in is_state_visited(). |
| 7930 | */ |
| 7931 | init_explored_state(env, t + insns[t].off + 1); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7932 | /* tell verifier to check for equivalent states |
| 7933 | * after every call and jump |
| 7934 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 7935 | if (t + 1 < insn_cnt) |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7936 | init_explored_state(env, t + 1); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7937 | } else { |
| 7938 | /* conditional jump with two edges */ |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 7939 | init_explored_state(env, t); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7940 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7941 | if (ret == 1) |
| 7942 | goto peek_stack; |
| 7943 | else if (ret < 0) |
| 7944 | goto err_free; |
| 7945 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7946 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7947 | if (ret == 1) |
| 7948 | goto peek_stack; |
| 7949 | else if (ret < 0) |
| 7950 | goto err_free; |
| 7951 | } |
| 7952 | } else { |
| 7953 | /* all other non-branch instructions with single |
| 7954 | * fall-through edge |
| 7955 | */ |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 7956 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7957 | if (ret == 1) |
| 7958 | goto peek_stack; |
| 7959 | else if (ret < 0) |
| 7960 | goto err_free; |
| 7961 | } |
| 7962 | |
| 7963 | mark_explored: |
| 7964 | insn_state[t] = EXPLORED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7965 | if (env->cfg.cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7966 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7967 | ret = -EFAULT; |
| 7968 | goto err_free; |
| 7969 | } |
| 7970 | goto peek_stack; |
| 7971 | |
| 7972 | check_state: |
| 7973 | for (i = 0; i < insn_cnt; i++) { |
| 7974 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7975 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7976 | ret = -EINVAL; |
| 7977 | goto err_free; |
| 7978 | } |
| 7979 | } |
| 7980 | ret = 0; /* cfg looks good */ |
| 7981 | |
| 7982 | err_free: |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 7983 | kvfree(insn_state); |
| 7984 | kvfree(insn_stack); |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 7985 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 7986 | return ret; |
| 7987 | } |
| 7988 | |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 7989 | static int check_abnormal_return(struct bpf_verifier_env *env) |
| 7990 | { |
| 7991 | int i; |
| 7992 | |
| 7993 | for (i = 1; i < env->subprog_cnt; i++) { |
| 7994 | if (env->subprog_info[i].has_ld_abs) { |
| 7995 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); |
| 7996 | return -EINVAL; |
| 7997 | } |
| 7998 | if (env->subprog_info[i].has_tail_call) { |
| 7999 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); |
| 8000 | return -EINVAL; |
| 8001 | } |
| 8002 | } |
| 8003 | return 0; |
| 8004 | } |
| 8005 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8006 | /* The minimum supported BTF func info size */ |
| 8007 | #define MIN_BPF_FUNCINFO_SIZE 8 |
| 8008 | #define MAX_FUNCINFO_REC_SIZE 252 |
| 8009 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8010 | static int check_btf_func(struct bpf_verifier_env *env, |
| 8011 | const union bpf_attr *attr, |
| 8012 | union bpf_attr __user *uattr) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8013 | { |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8014 | const struct btf_type *type, *func_proto, *ret_type; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 8015 | u32 i, nfuncs, urec_size, min_size; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8016 | u32 krec_size = sizeof(struct bpf_func_info); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8017 | struct bpf_func_info *krecord; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8018 | struct bpf_func_info_aux *info_aux = NULL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8019 | struct bpf_prog *prog; |
| 8020 | const struct btf *btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8021 | void __user *urecord; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 8022 | u32 prev_offset = 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8023 | bool scalar_return; |
Dan Carpenter | e7ed83d | 2020-06-04 11:54:36 +0300 | [diff] [blame] | 8024 | int ret = -ENOMEM; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8025 | |
| 8026 | nfuncs = attr->func_info_cnt; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8027 | if (!nfuncs) { |
| 8028 | if (check_abnormal_return(env)) |
| 8029 | return -EINVAL; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8030 | return 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8031 | } |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8032 | |
| 8033 | if (nfuncs != env->subprog_cnt) { |
| 8034 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); |
| 8035 | return -EINVAL; |
| 8036 | } |
| 8037 | |
| 8038 | urec_size = attr->func_info_rec_size; |
| 8039 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || |
| 8040 | urec_size > MAX_FUNCINFO_REC_SIZE || |
| 8041 | urec_size % sizeof(u32)) { |
| 8042 | verbose(env, "invalid func info rec size %u\n", urec_size); |
| 8043 | return -EINVAL; |
| 8044 | } |
| 8045 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8046 | prog = env->prog; |
| 8047 | btf = prog->aux->btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8048 | |
| 8049 | urecord = u64_to_user_ptr(attr->func_info); |
| 8050 | min_size = min_t(u32, krec_size, urec_size); |
| 8051 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8052 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8053 | if (!krecord) |
| 8054 | return -ENOMEM; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8055 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
| 8056 | if (!info_aux) |
| 8057 | goto err_free; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8058 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8059 | for (i = 0; i < nfuncs; i++) { |
| 8060 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); |
| 8061 | if (ret) { |
| 8062 | if (ret == -E2BIG) { |
| 8063 | verbose(env, "nonzero tailing record in func info"); |
| 8064 | /* set the size kernel expects so loader can zero |
| 8065 | * out the rest of the record. |
| 8066 | */ |
| 8067 | if (put_user(min_size, &uattr->func_info_rec_size)) |
| 8068 | ret = -EFAULT; |
| 8069 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8070 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8071 | } |
| 8072 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8073 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8074 | ret = -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8075 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8076 | } |
| 8077 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8078 | /* check insn_off */ |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8079 | ret = -EINVAL; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8080 | if (i == 0) { |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8081 | if (krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8082 | verbose(env, |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8083 | "nonzero insn_off %u for the first func info record", |
| 8084 | krecord[i].insn_off); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8085 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8086 | } |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8087 | } else if (krecord[i].insn_off <= prev_offset) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8088 | verbose(env, |
| 8089 | "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] | 8090 | krecord[i].insn_off, prev_offset); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8091 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8092 | } |
| 8093 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8094 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8095 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8096 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8097 | } |
| 8098 | |
| 8099 | /* check type_id */ |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8100 | type = btf_type_by_id(btf, krecord[i].type_id); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 8101 | if (!type || !btf_type_is_func(type)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8102 | verbose(env, "invalid type id %d in func info", |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8103 | krecord[i].type_id); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8104 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8105 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 8106 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8107 | |
| 8108 | func_proto = btf_type_by_id(btf, type->type); |
| 8109 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) |
| 8110 | /* btf_func_check() already verified it during BTF load */ |
| 8111 | goto err_free; |
| 8112 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); |
| 8113 | scalar_return = |
| 8114 | btf_type_is_small_int(ret_type) || btf_type_is_enum(ret_type); |
| 8115 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { |
| 8116 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); |
| 8117 | goto err_free; |
| 8118 | } |
| 8119 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { |
| 8120 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); |
| 8121 | goto err_free; |
| 8122 | } |
| 8123 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 8124 | prev_offset = krecord[i].insn_off; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8125 | urecord += urec_size; |
| 8126 | } |
| 8127 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8128 | prog->aux->func_info = krecord; |
| 8129 | prog->aux->func_info_cnt = nfuncs; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8130 | prog->aux->func_info_aux = info_aux; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8131 | return 0; |
| 8132 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8133 | err_free: |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8134 | kvfree(krecord); |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8135 | kfree(info_aux); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8136 | return ret; |
| 8137 | } |
| 8138 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8139 | static void adjust_btf_func(struct bpf_verifier_env *env) |
| 8140 | { |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8141 | struct bpf_prog_aux *aux = env->prog->aux; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8142 | int i; |
| 8143 | |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8144 | if (!aux->func_info) |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8145 | return; |
| 8146 | |
| 8147 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 8148 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8149 | } |
| 8150 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8151 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
| 8152 | sizeof(((struct bpf_line_info *)(0))->line_col)) |
| 8153 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
| 8154 | |
| 8155 | static int check_btf_line(struct bpf_verifier_env *env, |
| 8156 | const union bpf_attr *attr, |
| 8157 | union bpf_attr __user *uattr) |
| 8158 | { |
| 8159 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; |
| 8160 | struct bpf_subprog_info *sub; |
| 8161 | struct bpf_line_info *linfo; |
| 8162 | struct bpf_prog *prog; |
| 8163 | const struct btf *btf; |
| 8164 | void __user *ulinfo; |
| 8165 | int err; |
| 8166 | |
| 8167 | nr_linfo = attr->line_info_cnt; |
| 8168 | if (!nr_linfo) |
| 8169 | return 0; |
| 8170 | |
| 8171 | rec_size = attr->line_info_rec_size; |
| 8172 | if (rec_size < MIN_BPF_LINEINFO_SIZE || |
| 8173 | rec_size > MAX_LINEINFO_REC_SIZE || |
| 8174 | rec_size & (sizeof(u32) - 1)) |
| 8175 | return -EINVAL; |
| 8176 | |
| 8177 | /* Need to zero it in case the userspace may |
| 8178 | * pass in a smaller bpf_line_info object. |
| 8179 | */ |
| 8180 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), |
| 8181 | GFP_KERNEL | __GFP_NOWARN); |
| 8182 | if (!linfo) |
| 8183 | return -ENOMEM; |
| 8184 | |
| 8185 | prog = env->prog; |
| 8186 | btf = prog->aux->btf; |
| 8187 | |
| 8188 | s = 0; |
| 8189 | sub = env->subprog_info; |
| 8190 | ulinfo = u64_to_user_ptr(attr->line_info); |
| 8191 | expected_size = sizeof(struct bpf_line_info); |
| 8192 | ncopy = min_t(u32, expected_size, rec_size); |
| 8193 | for (i = 0; i < nr_linfo; i++) { |
| 8194 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); |
| 8195 | if (err) { |
| 8196 | if (err == -E2BIG) { |
| 8197 | verbose(env, "nonzero tailing record in line_info"); |
| 8198 | if (put_user(expected_size, |
| 8199 | &uattr->line_info_rec_size)) |
| 8200 | err = -EFAULT; |
| 8201 | } |
| 8202 | goto err_free; |
| 8203 | } |
| 8204 | |
| 8205 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { |
| 8206 | err = -EFAULT; |
| 8207 | goto err_free; |
| 8208 | } |
| 8209 | |
| 8210 | /* |
| 8211 | * Check insn_off to ensure |
| 8212 | * 1) strictly increasing AND |
| 8213 | * 2) bounded by prog->len |
| 8214 | * |
| 8215 | * The linfo[0].insn_off == 0 check logically falls into |
| 8216 | * the later "missing bpf_line_info for func..." case |
| 8217 | * because the first linfo[0].insn_off must be the |
| 8218 | * first sub also and the first sub must have |
| 8219 | * subprog_info[0].start == 0. |
| 8220 | */ |
| 8221 | if ((i && linfo[i].insn_off <= prev_offset) || |
| 8222 | linfo[i].insn_off >= prog->len) { |
| 8223 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", |
| 8224 | i, linfo[i].insn_off, prev_offset, |
| 8225 | prog->len); |
| 8226 | err = -EINVAL; |
| 8227 | goto err_free; |
| 8228 | } |
| 8229 | |
Martin KaFai Lau | fdbaa0b | 2018-12-19 13:01:01 -0800 | [diff] [blame] | 8230 | if (!prog->insnsi[linfo[i].insn_off].code) { |
| 8231 | verbose(env, |
| 8232 | "Invalid insn code at line_info[%u].insn_off\n", |
| 8233 | i); |
| 8234 | err = -EINVAL; |
| 8235 | goto err_free; |
| 8236 | } |
| 8237 | |
Martin KaFai Lau | 23127b3 | 2018-12-13 10:41:46 -0800 | [diff] [blame] | 8238 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
| 8239 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8240 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
| 8241 | err = -EINVAL; |
| 8242 | goto err_free; |
| 8243 | } |
| 8244 | |
| 8245 | if (s != env->subprog_cnt) { |
| 8246 | if (linfo[i].insn_off == sub[s].start) { |
| 8247 | sub[s].linfo_idx = i; |
| 8248 | s++; |
| 8249 | } else if (sub[s].start < linfo[i].insn_off) { |
| 8250 | verbose(env, "missing bpf_line_info for func#%u\n", s); |
| 8251 | err = -EINVAL; |
| 8252 | goto err_free; |
| 8253 | } |
| 8254 | } |
| 8255 | |
| 8256 | prev_offset = linfo[i].insn_off; |
| 8257 | ulinfo += rec_size; |
| 8258 | } |
| 8259 | |
| 8260 | if (s != env->subprog_cnt) { |
| 8261 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", |
| 8262 | env->subprog_cnt - s, s); |
| 8263 | err = -EINVAL; |
| 8264 | goto err_free; |
| 8265 | } |
| 8266 | |
| 8267 | prog->aux->linfo = linfo; |
| 8268 | prog->aux->nr_linfo = nr_linfo; |
| 8269 | |
| 8270 | return 0; |
| 8271 | |
| 8272 | err_free: |
| 8273 | kvfree(linfo); |
| 8274 | return err; |
| 8275 | } |
| 8276 | |
| 8277 | static int check_btf_info(struct bpf_verifier_env *env, |
| 8278 | const union bpf_attr *attr, |
| 8279 | union bpf_attr __user *uattr) |
| 8280 | { |
| 8281 | struct btf *btf; |
| 8282 | int err; |
| 8283 | |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8284 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
| 8285 | if (check_abnormal_return(env)) |
| 8286 | return -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8287 | return 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 8288 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8289 | |
| 8290 | btf = btf_get_by_fd(attr->prog_btf_fd); |
| 8291 | if (IS_ERR(btf)) |
| 8292 | return PTR_ERR(btf); |
| 8293 | env->prog->aux->btf = btf; |
| 8294 | |
| 8295 | err = check_btf_func(env, attr, uattr); |
| 8296 | if (err) |
| 8297 | return err; |
| 8298 | |
| 8299 | err = check_btf_line(env, attr, uattr); |
| 8300 | if (err) |
| 8301 | return err; |
| 8302 | |
| 8303 | return 0; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8304 | } |
| 8305 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8306 | /* check %cur's range satisfies %old's */ |
| 8307 | static bool range_within(struct bpf_reg_state *old, |
| 8308 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8309 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8310 | return old->umin_value <= cur->umin_value && |
| 8311 | old->umax_value >= cur->umax_value && |
| 8312 | old->smin_value <= cur->smin_value && |
| 8313 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8314 | } |
| 8315 | |
| 8316 | /* Maximum number of register states that can exist at once */ |
| 8317 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 8318 | struct idpair { |
| 8319 | u32 old; |
| 8320 | u32 cur; |
| 8321 | }; |
| 8322 | |
| 8323 | /* If in the old state two registers had the same id, then they need to have |
| 8324 | * the same id in the new state as well. But that id could be different from |
| 8325 | * the old state, so we need to track the mapping from old to new ids. |
| 8326 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 8327 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 8328 | * regs with a different old id could still have new id 9, we don't care about |
| 8329 | * that. |
| 8330 | * So we look through our idmap to see if this old id has been seen before. If |
| 8331 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 8332 | */ |
| 8333 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 8334 | { |
| 8335 | unsigned int i; |
| 8336 | |
| 8337 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 8338 | if (!idmap[i].old) { |
| 8339 | /* Reached an empty slot; haven't seen this id before */ |
| 8340 | idmap[i].old = old_id; |
| 8341 | idmap[i].cur = cur_id; |
| 8342 | return true; |
| 8343 | } |
| 8344 | if (idmap[i].old == old_id) |
| 8345 | return idmap[i].cur == cur_id; |
| 8346 | } |
| 8347 | /* We ran out of idmap slots, which should be impossible */ |
| 8348 | WARN_ON_ONCE(1); |
| 8349 | return false; |
| 8350 | } |
| 8351 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 8352 | static void clean_func_state(struct bpf_verifier_env *env, |
| 8353 | struct bpf_func_state *st) |
| 8354 | { |
| 8355 | enum bpf_reg_liveness live; |
| 8356 | int i, j; |
| 8357 | |
| 8358 | for (i = 0; i < BPF_REG_FP; i++) { |
| 8359 | live = st->regs[i].live; |
| 8360 | /* liveness must not touch this register anymore */ |
| 8361 | st->regs[i].live |= REG_LIVE_DONE; |
| 8362 | if (!(live & REG_LIVE_READ)) |
| 8363 | /* since the register is unused, clear its state |
| 8364 | * to make further comparison simpler |
| 8365 | */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 8366 | __mark_reg_not_init(env, &st->regs[i]); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 8367 | } |
| 8368 | |
| 8369 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { |
| 8370 | live = st->stack[i].spilled_ptr.live; |
| 8371 | /* liveness must not touch this stack slot anymore */ |
| 8372 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; |
| 8373 | if (!(live & REG_LIVE_READ)) { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 8374 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 8375 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 8376 | st->stack[i].slot_type[j] = STACK_INVALID; |
| 8377 | } |
| 8378 | } |
| 8379 | } |
| 8380 | |
| 8381 | static void clean_verifier_state(struct bpf_verifier_env *env, |
| 8382 | struct bpf_verifier_state *st) |
| 8383 | { |
| 8384 | int i; |
| 8385 | |
| 8386 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) |
| 8387 | /* all regs in this state in all frames were already marked */ |
| 8388 | return; |
| 8389 | |
| 8390 | for (i = 0; i <= st->curframe; i++) |
| 8391 | clean_func_state(env, st->frame[i]); |
| 8392 | } |
| 8393 | |
| 8394 | /* the parentage chains form a tree. |
| 8395 | * the verifier states are added to state lists at given insn and |
| 8396 | * pushed into state stack for future exploration. |
| 8397 | * when the verifier reaches bpf_exit insn some of the verifer states |
| 8398 | * stored in the state lists have their final liveness state already, |
| 8399 | * but a lot of states will get revised from liveness point of view when |
| 8400 | * the verifier explores other branches. |
| 8401 | * Example: |
| 8402 | * 1: r0 = 1 |
| 8403 | * 2: if r1 == 100 goto pc+1 |
| 8404 | * 3: r0 = 2 |
| 8405 | * 4: exit |
| 8406 | * when the verifier reaches exit insn the register r0 in the state list of |
| 8407 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch |
| 8408 | * of insn 2 and goes exploring further. At the insn 4 it will walk the |
| 8409 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. |
| 8410 | * |
| 8411 | * Since the verifier pushes the branch states as it sees them while exploring |
| 8412 | * the program the condition of walking the branch instruction for the second |
| 8413 | * time means that all states below this branch were already explored and |
| 8414 | * their final liveness markes are already propagated. |
| 8415 | * Hence when the verifier completes the search of state list in is_state_visited() |
| 8416 | * we can call this clean_live_states() function to mark all liveness states |
| 8417 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' |
| 8418 | * will not be used. |
| 8419 | * This function also clears the registers and stack for states that !READ |
| 8420 | * to simplify state merging. |
| 8421 | * |
| 8422 | * Important note here that walking the same branch instruction in the callee |
| 8423 | * doesn't meant that the states are DONE. The verifier has to compare |
| 8424 | * the callsites |
| 8425 | */ |
| 8426 | static void clean_live_states(struct bpf_verifier_env *env, int insn, |
| 8427 | struct bpf_verifier_state *cur) |
| 8428 | { |
| 8429 | struct bpf_verifier_state_list *sl; |
| 8430 | int i; |
| 8431 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 8432 | sl = *explored_state(env, insn); |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 8433 | while (sl) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8434 | if (sl->state.branches) |
| 8435 | goto next; |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 8436 | if (sl->state.insn_idx != insn || |
| 8437 | sl->state.curframe != cur->curframe) |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 8438 | goto next; |
| 8439 | for (i = 0; i <= cur->curframe; i++) |
| 8440 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) |
| 8441 | goto next; |
| 8442 | clean_verifier_state(env, &sl->state); |
| 8443 | next: |
| 8444 | sl = sl->next; |
| 8445 | } |
| 8446 | } |
| 8447 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8448 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 8449 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 8450 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8451 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8452 | bool equal; |
| 8453 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8454 | if (!(rold->live & REG_LIVE_READ)) |
| 8455 | /* explored state didn't use this */ |
| 8456 | return true; |
| 8457 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 8458 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8459 | |
| 8460 | if (rold->type == PTR_TO_STACK) |
| 8461 | /* two stack pointers are equal only if they're pointing to |
| 8462 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 8463 | */ |
| 8464 | return equal && rold->frameno == rcur->frameno; |
| 8465 | |
| 8466 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8467 | return true; |
| 8468 | |
| 8469 | if (rold->type == NOT_INIT) |
| 8470 | /* explored state can't have used this */ |
| 8471 | return true; |
| 8472 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8473 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8474 | switch (rold->type) { |
| 8475 | case SCALAR_VALUE: |
| 8476 | if (rcur->type == SCALAR_VALUE) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8477 | if (!rold->precise && !rcur->precise) |
| 8478 | return true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8479 | /* new val must satisfy old val knowledge */ |
| 8480 | return range_within(rold, rcur) && |
| 8481 | tnum_in(rold->var_off, rcur->var_off); |
| 8482 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 8483 | /* We're trying to use a pointer in place of a scalar. |
| 8484 | * Even if the scalar was unbounded, this could lead to |
| 8485 | * pointer leaks because scalars are allowed to leak |
| 8486 | * while pointers are not. We could make this safe in |
| 8487 | * special cases if root is calling us, but it's |
| 8488 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8489 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 8490 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8491 | } |
| 8492 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 8493 | /* If the new min/max/var_off satisfy the old ones and |
| 8494 | * everything else matches, we are OK. |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 8495 | * 'id' is not compared, since it's only used for maps with |
| 8496 | * bpf_spin_lock inside map element and in such cases if |
| 8497 | * the rest of the prog is valid for one map element then |
| 8498 | * it's valid for all map elements regardless of the key |
| 8499 | * used in bpf_map_lookup() |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 8500 | */ |
| 8501 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 8502 | range_within(rold, rcur) && |
| 8503 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8504 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 8505 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 8506 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 8507 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 8508 | * checked, doing so could have affected others with the same |
| 8509 | * id, and we can't check for that because we lost the id when |
| 8510 | * we converted to a PTR_TO_MAP_VALUE. |
| 8511 | */ |
| 8512 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 8513 | return false; |
| 8514 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 8515 | return false; |
| 8516 | /* Check our ids match any regs they're supposed to */ |
| 8517 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 8518 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8519 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 8520 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8521 | return false; |
| 8522 | /* We must have at least as much range as the old ptr |
| 8523 | * did, so that any accesses which were safe before are |
| 8524 | * still safe. This is true even if old range < old off, |
| 8525 | * since someone could have accessed through (ptr - k), or |
| 8526 | * even done ptr -= k in a register, to get a safe access. |
| 8527 | */ |
| 8528 | if (rold->range > rcur->range) |
| 8529 | return false; |
| 8530 | /* If the offsets don't match, we can't trust our alignment; |
| 8531 | * nor can we be sure that we won't fall out of range. |
| 8532 | */ |
| 8533 | if (rold->off != rcur->off) |
| 8534 | return false; |
| 8535 | /* id relations must be preserved */ |
| 8536 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 8537 | return false; |
| 8538 | /* new val must satisfy old val knowledge */ |
| 8539 | return range_within(rold, rcur) && |
| 8540 | tnum_in(rold->var_off, rcur->var_off); |
| 8541 | case PTR_TO_CTX: |
| 8542 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8543 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 8544 | case PTR_TO_FLOW_KEYS: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 8545 | case PTR_TO_SOCKET: |
| 8546 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 8547 | case PTR_TO_SOCK_COMMON: |
| 8548 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 8549 | case PTR_TO_TCP_SOCK: |
| 8550 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 8551 | case PTR_TO_XDP_SOCK: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8552 | /* Only valid matches are exact, which memcmp() above |
| 8553 | * would have accepted |
| 8554 | */ |
| 8555 | default: |
| 8556 | /* Don't know what's going on, just say it's not safe */ |
| 8557 | return false; |
| 8558 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8559 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8560 | /* Shouldn't get here; if we do, say it's not safe */ |
| 8561 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8562 | return false; |
| 8563 | } |
| 8564 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8565 | static bool stacksafe(struct bpf_func_state *old, |
| 8566 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8567 | struct idpair *idmap) |
| 8568 | { |
| 8569 | int i, spi; |
| 8570 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8571 | /* walk slots of the explored stack and ignore any additional |
| 8572 | * slots in the current stack, since explored(safe) state |
| 8573 | * didn't use them |
| 8574 | */ |
| 8575 | for (i = 0; i < old->allocated_stack; i++) { |
| 8576 | spi = i / BPF_REG_SIZE; |
| 8577 | |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 8578 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
| 8579 | i += BPF_REG_SIZE - 1; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 8580 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 8581 | continue; |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 8582 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 8583 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8584 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 8585 | continue; |
Alexei Starovoitov | 19e2dbb | 2018-12-13 11:42:33 -0800 | [diff] [blame] | 8586 | |
| 8587 | /* explored stack has more populated slots than current stack |
| 8588 | * and these slots were used |
| 8589 | */ |
| 8590 | if (i >= cur->allocated_stack) |
| 8591 | return false; |
| 8592 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 8593 | /* if old state was safe with misc data in the stack |
| 8594 | * it will be safe with zero-initialized stack. |
| 8595 | * The opposite is not true |
| 8596 | */ |
| 8597 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 8598 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 8599 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8600 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 8601 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 8602 | /* Ex: old explored (safe) state has STACK_SPILL in |
Randy Dunlap | b8c1a30 | 2020-08-06 20:31:41 -0700 | [diff] [blame] | 8603 | * this stack slot, but current has STACK_MISC -> |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8604 | * this verifier states are not equivalent, |
| 8605 | * return false to continue verification of this path |
| 8606 | */ |
| 8607 | return false; |
| 8608 | if (i % BPF_REG_SIZE) |
| 8609 | continue; |
| 8610 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 8611 | continue; |
| 8612 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 8613 | &cur->stack[spi].spilled_ptr, |
| 8614 | idmap)) |
| 8615 | /* when explored and current stack slot are both storing |
| 8616 | * spilled registers, check that stored pointers types |
| 8617 | * are the same as well. |
| 8618 | * Ex: explored safe path could have stored |
| 8619 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 8620 | * but current path has stored: |
| 8621 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 8622 | * such verifier states are not equivalent. |
| 8623 | * return false to continue verification of this path |
| 8624 | */ |
| 8625 | return false; |
| 8626 | } |
| 8627 | return true; |
| 8628 | } |
| 8629 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 8630 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 8631 | { |
| 8632 | if (old->acquired_refs != cur->acquired_refs) |
| 8633 | return false; |
| 8634 | return !memcmp(old->refs, cur->refs, |
| 8635 | sizeof(*old->refs) * old->acquired_refs); |
| 8636 | } |
| 8637 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8638 | /* compare two verifier states |
| 8639 | * |
| 8640 | * all states stored in state_list are known to be valid, since |
| 8641 | * verifier reached 'bpf_exit' instruction through them |
| 8642 | * |
| 8643 | * this function is called when verifier exploring different branches of |
| 8644 | * execution popped from the state stack. If it sees an old state that has |
| 8645 | * more strict register state and more strict stack state then this execution |
| 8646 | * branch doesn't need to be explored further, since verifier already |
| 8647 | * concluded that more strict state leads to valid finish. |
| 8648 | * |
| 8649 | * Therefore two states are equivalent if register state is more conservative |
| 8650 | * and explored stack state is more conservative than the current one. |
| 8651 | * Example: |
| 8652 | * explored current |
| 8653 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 8654 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 8655 | * |
| 8656 | * In other words if current stack state (one being explored) has more |
| 8657 | * valid slots than old one that already passed validation, it means |
| 8658 | * the verifier can stop exploring and conclude that current state is valid too |
| 8659 | * |
| 8660 | * Similarly with registers. If explored state has register type as invalid |
| 8661 | * whereas register type in current state is meaningful, it means that |
| 8662 | * the current state will reach 'bpf_exit' instruction safely |
| 8663 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8664 | static bool func_states_equal(struct bpf_func_state *old, |
| 8665 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8666 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8667 | struct idpair *idmap; |
| 8668 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8669 | int i; |
| 8670 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8671 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 8672 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 8673 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 8674 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8675 | |
| 8676 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 8677 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8678 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8679 | } |
| 8680 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8681 | if (!stacksafe(old, cur, idmap)) |
| 8682 | goto out_free; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 8683 | |
| 8684 | if (!refsafe(old, cur)) |
| 8685 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8686 | ret = true; |
| 8687 | out_free: |
| 8688 | kfree(idmap); |
| 8689 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8690 | } |
| 8691 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8692 | static bool states_equal(struct bpf_verifier_env *env, |
| 8693 | struct bpf_verifier_state *old, |
| 8694 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8695 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8696 | int i; |
| 8697 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8698 | if (old->curframe != cur->curframe) |
| 8699 | return false; |
| 8700 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 8701 | /* Verification state from speculative execution simulation |
| 8702 | * must never prune a non-speculative execution one. |
| 8703 | */ |
| 8704 | if (old->speculative && !cur->speculative) |
| 8705 | return false; |
| 8706 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 8707 | if (old->active_spin_lock != cur->active_spin_lock) |
| 8708 | return false; |
| 8709 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8710 | /* for states to be equal callsites have to be the same |
| 8711 | * and all frame states need to be equivalent |
| 8712 | */ |
| 8713 | for (i = 0; i <= old->curframe; i++) { |
| 8714 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 8715 | return false; |
| 8716 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 8717 | return false; |
| 8718 | } |
| 8719 | return true; |
| 8720 | } |
| 8721 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8722 | /* Return 0 if no propagation happened. Return negative error code if error |
| 8723 | * happened. Otherwise, return the propagated bit. |
| 8724 | */ |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 8725 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
| 8726 | struct bpf_reg_state *reg, |
| 8727 | struct bpf_reg_state *parent_reg) |
| 8728 | { |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8729 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
| 8730 | u8 flag = reg->live & REG_LIVE_READ; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 8731 | int err; |
| 8732 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8733 | /* When comes here, read flags of PARENT_REG or REG could be any of |
| 8734 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need |
| 8735 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. |
| 8736 | */ |
| 8737 | if (parent_flag == REG_LIVE_READ64 || |
| 8738 | /* Or if there is no read flag from REG. */ |
| 8739 | !flag || |
| 8740 | /* Or if the read flag from REG is the same as PARENT_REG. */ |
| 8741 | parent_flag == flag) |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 8742 | return 0; |
| 8743 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8744 | err = mark_reg_read(env, reg, parent_reg, flag); |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 8745 | if (err) |
| 8746 | return err; |
| 8747 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8748 | return flag; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 8749 | } |
| 8750 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8751 | /* A write screens off any subsequent reads; but write marks come from the |
| 8752 | * straight-line code between a state and its parent. When we arrive at an |
| 8753 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 8754 | * code, so read marks in the state must propagate to the parent regardless |
| 8755 | * 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] | 8756 | * in mark_reg_read() is for. |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8757 | */ |
| 8758 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 8759 | const struct bpf_verifier_state *vstate, |
| 8760 | struct bpf_verifier_state *vparent) |
| 8761 | { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 8762 | struct bpf_reg_state *state_reg, *parent_reg; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8763 | struct bpf_func_state *state, *parent; |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 8764 | int i, frame, err = 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8765 | |
| 8766 | if (vparent->curframe != vstate->curframe) { |
| 8767 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 8768 | vparent->curframe, vstate->curframe); |
| 8769 | return -EFAULT; |
| 8770 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8771 | /* Propagate read liveness of registers... */ |
| 8772 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 8773 | for (frame = 0; frame <= vstate->curframe; frame++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 8774 | parent = vparent->frame[frame]; |
| 8775 | state = vstate->frame[frame]; |
| 8776 | parent_reg = parent->regs; |
| 8777 | state_reg = state->regs; |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 8778 | /* We don't need to worry about FP liveness, it's read-only */ |
| 8779 | 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] | 8780 | err = propagate_liveness_reg(env, &state_reg[i], |
| 8781 | &parent_reg[i]); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8782 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 8783 | return err; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8784 | if (err == REG_LIVE_READ64) |
| 8785 | mark_insn_zext(env, &parent_reg[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8786 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8787 | |
Jiong Wang | 1b04aee | 2019-04-12 22:59:34 +0100 | [diff] [blame] | 8788 | /* Propagate stack slots. */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8789 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 8790 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 8791 | parent_reg = &parent->stack[i].spilled_ptr; |
| 8792 | state_reg = &state->stack[i].spilled_ptr; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 8793 | err = propagate_liveness_reg(env, state_reg, |
| 8794 | parent_reg); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8795 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 8796 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8797 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8798 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8799 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8800 | } |
| 8801 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 8802 | /* find precise scalars in the previous equivalent state and |
| 8803 | * propagate them into the current state |
| 8804 | */ |
| 8805 | static int propagate_precision(struct bpf_verifier_env *env, |
| 8806 | const struct bpf_verifier_state *old) |
| 8807 | { |
| 8808 | struct bpf_reg_state *state_reg; |
| 8809 | struct bpf_func_state *state; |
| 8810 | int i, err = 0; |
| 8811 | |
| 8812 | state = old->frame[old->curframe]; |
| 8813 | state_reg = state->regs; |
| 8814 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { |
| 8815 | if (state_reg->type != SCALAR_VALUE || |
| 8816 | !state_reg->precise) |
| 8817 | continue; |
| 8818 | if (env->log.level & BPF_LOG_LEVEL2) |
| 8819 | verbose(env, "propagating r%d\n", i); |
| 8820 | err = mark_chain_precision(env, i); |
| 8821 | if (err < 0) |
| 8822 | return err; |
| 8823 | } |
| 8824 | |
| 8825 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 8826 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 8827 | continue; |
| 8828 | state_reg = &state->stack[i].spilled_ptr; |
| 8829 | if (state_reg->type != SCALAR_VALUE || |
| 8830 | !state_reg->precise) |
| 8831 | continue; |
| 8832 | if (env->log.level & BPF_LOG_LEVEL2) |
| 8833 | verbose(env, "propagating fp%d\n", |
| 8834 | (-i - 1) * BPF_REG_SIZE); |
| 8835 | err = mark_chain_precision_stack(env, i); |
| 8836 | if (err < 0) |
| 8837 | return err; |
| 8838 | } |
| 8839 | return 0; |
| 8840 | } |
| 8841 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8842 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
| 8843 | struct bpf_verifier_state *cur) |
| 8844 | { |
| 8845 | struct bpf_func_state *fold, *fcur; |
| 8846 | int i, fr = cur->curframe; |
| 8847 | |
| 8848 | if (old->curframe != fr) |
| 8849 | return false; |
| 8850 | |
| 8851 | fold = old->frame[fr]; |
| 8852 | fcur = cur->frame[fr]; |
| 8853 | for (i = 0; i < MAX_BPF_REG; i++) |
| 8854 | if (memcmp(&fold->regs[i], &fcur->regs[i], |
| 8855 | offsetof(struct bpf_reg_state, parent))) |
| 8856 | return false; |
| 8857 | return true; |
| 8858 | } |
| 8859 | |
| 8860 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8861 | 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] | 8862 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8863 | struct bpf_verifier_state_list *new_sl; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 8864 | struct bpf_verifier_state_list *sl, **pprev; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 8865 | struct bpf_verifier_state *cur = env->cur_state, *new; |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 8866 | int i, j, err, states_cnt = 0; |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 8867 | bool add_new_state = env->test_state_freq ? true : false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8868 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8869 | cur->last_insn_idx = env->prev_insn_idx; |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 8870 | if (!env->insn_aux_data[insn_idx].prune_point) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8871 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 8872 | * be doing state search here |
| 8873 | */ |
| 8874 | return 0; |
| 8875 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8876 | /* bpf progs typically have pruning point every 4 instructions |
| 8877 | * http://vger.kernel.org/bpfconf2019.html#session-1 |
| 8878 | * Do not add new state for future pruning if the verifier hasn't seen |
| 8879 | * at least 2 jumps and at least 8 instructions. |
| 8880 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. |
| 8881 | * In tests that amounts to up to 50% reduction into total verifier |
| 8882 | * memory consumption and 20% verifier time speedup. |
| 8883 | */ |
| 8884 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && |
| 8885 | env->insn_processed - env->prev_insn_processed >= 8) |
| 8886 | add_new_state = true; |
| 8887 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 8888 | pprev = explored_state(env, insn_idx); |
| 8889 | sl = *pprev; |
| 8890 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 8891 | clean_live_states(env, insn_idx, cur); |
| 8892 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 8893 | while (sl) { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 8894 | states_cnt++; |
| 8895 | if (sl->state.insn_idx != insn_idx) |
| 8896 | goto next; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8897 | if (sl->state.branches) { |
| 8898 | if (states_maybe_looping(&sl->state, cur) && |
| 8899 | states_equal(env, &sl->state, cur)) { |
| 8900 | verbose_linfo(env, insn_idx, "; "); |
| 8901 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); |
| 8902 | return -EINVAL; |
| 8903 | } |
| 8904 | /* if the verifier is processing a loop, avoid adding new state |
| 8905 | * too often, since different loop iterations have distinct |
| 8906 | * states and may not help future pruning. |
| 8907 | * This threshold shouldn't be too low to make sure that |
| 8908 | * a loop with large bound will be rejected quickly. |
| 8909 | * The most abusive loop will be: |
| 8910 | * r1 += 1 |
| 8911 | * if r1 < 1000000 goto pc-2 |
| 8912 | * 1M insn_procssed limit / 100 == 10k peak states. |
| 8913 | * This threshold shouldn't be too high either, since states |
| 8914 | * at the end of the loop are likely to be useful in pruning. |
| 8915 | */ |
| 8916 | if (env->jmps_processed - env->prev_jmps_processed < 20 && |
| 8917 | env->insn_processed - env->prev_insn_processed < 100) |
| 8918 | add_new_state = false; |
| 8919 | goto miss; |
| 8920 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8921 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 8922 | sl->hit_cnt++; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8923 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8924 | * prune the search. |
| 8925 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 8926 | * If we have any write marks in env->cur_state, they |
| 8927 | * will prevent corresponding reads in the continuation |
| 8928 | * from reaching our parent (an explored_state). Our |
| 8929 | * own state will get the read marks recorded, but |
| 8930 | * they'll be immediately forgotten as we're pruning |
| 8931 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8932 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8933 | err = propagate_liveness(env, &sl->state, cur); |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 8934 | |
| 8935 | /* if previous state reached the exit with precision and |
| 8936 | * current state is equivalent to it (except precsion marks) |
| 8937 | * the precision needs to be propagated back in |
| 8938 | * the current state. |
| 8939 | */ |
| 8940 | err = err ? : push_jmp_history(env, cur); |
| 8941 | err = err ? : propagate_precision(env, &sl->state); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8942 | if (err) |
| 8943 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8944 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8945 | } |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8946 | miss: |
| 8947 | /* when new state is not going to be added do not increase miss count. |
| 8948 | * Otherwise several loop iterations will remove the state |
| 8949 | * recorded earlier. The goal of these heuristics is to have |
| 8950 | * states from some iterations of the loop (some in the beginning |
| 8951 | * and some at the end) to help pruning. |
| 8952 | */ |
| 8953 | if (add_new_state) |
| 8954 | sl->miss_cnt++; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 8955 | /* heuristic to determine whether this state is beneficial |
| 8956 | * to keep checking from state equivalence point of view. |
| 8957 | * Higher numbers increase max_states_per_insn and verification time, |
| 8958 | * but do not meaningfully decrease insn_processed. |
| 8959 | */ |
| 8960 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { |
| 8961 | /* the state is unlikely to be useful. Remove it to |
| 8962 | * speed up verification |
| 8963 | */ |
| 8964 | *pprev = sl->next; |
| 8965 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8966 | u32 br = sl->state.branches; |
| 8967 | |
| 8968 | WARN_ONCE(br, |
| 8969 | "BUG live_done but branches_to_explore %d\n", |
| 8970 | br); |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 8971 | free_verifier_state(&sl->state, false); |
| 8972 | kfree(sl); |
| 8973 | env->peak_states--; |
| 8974 | } else { |
| 8975 | /* cannot free this state, since parentage chain may |
| 8976 | * walk it later. Add it for free_list instead to |
| 8977 | * be freed at the end of verification |
| 8978 | */ |
| 8979 | sl->next = env->free_list; |
| 8980 | env->free_list = sl; |
| 8981 | } |
| 8982 | sl = *pprev; |
| 8983 | continue; |
| 8984 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 8985 | next: |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 8986 | pprev = &sl->next; |
| 8987 | sl = *pprev; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8988 | } |
| 8989 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 8990 | if (env->max_states_per_insn < states_cnt) |
| 8991 | env->max_states_per_insn = states_cnt; |
| 8992 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 8993 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8994 | return push_jmp_history(env, cur); |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 8995 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8996 | if (!add_new_state) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8997 | return push_jmp_history(env, cur); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 8998 | |
| 8999 | /* There were no equivalent states, remember the current one. |
| 9000 | * Technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9001 | * 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] | 9002 | * 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] | 9003 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9004 | * again on the way to bpf_exit. |
| 9005 | * When looping the sl->state.branches will be > 0 and this state |
| 9006 | * will not be considered for equivalence until branches == 0. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9007 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9008 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9009 | if (!new_sl) |
| 9010 | return -ENOMEM; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9011 | env->total_states++; |
| 9012 | env->peak_states++; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9013 | env->prev_jmps_processed = env->jmps_processed; |
| 9014 | env->prev_insn_processed = env->insn_processed; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9015 | |
| 9016 | /* add new state to the head of linked list */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9017 | new = &new_sl->state; |
| 9018 | err = copy_verifier_state(new, cur); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 9019 | if (err) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9020 | free_verifier_state(new, false); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 9021 | kfree(new_sl); |
| 9022 | return err; |
| 9023 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 9024 | new->insn_idx = insn_idx; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9025 | WARN_ONCE(new->branches != 1, |
| 9026 | "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] | 9027 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9028 | cur->parent = new; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9029 | cur->first_insn_idx = insn_idx; |
| 9030 | clear_jmp_history(cur); |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 9031 | new_sl->next = *explored_state(env, insn_idx); |
| 9032 | *explored_state(env, insn_idx) = new_sl; |
Jakub Kicinski | 7640ead | 2018-12-12 16:29:07 -0800 | [diff] [blame] | 9033 | /* connect new state to parentage chain. Current frame needs all |
| 9034 | * registers connected. Only r6 - r9 of the callers are alive (pushed |
| 9035 | * to the stack implicitly by JITs) so in callers' frames connect just |
| 9036 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to |
| 9037 | * the state of the call instruction (with WRITTEN set), and r0 comes |
| 9038 | * from callee with its full parentage chain, anyway. |
| 9039 | */ |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 9040 | /* clear write marks in current state: the writes we did are not writes |
| 9041 | * our child did, so they don't screen off its reads from us. |
| 9042 | * (There are no read marks in current state, because reads always mark |
| 9043 | * their parent and current state never has children yet. Only |
| 9044 | * explored_states can get read marks.) |
| 9045 | */ |
Alexei Starovoitov | eea1c22 | 2019-06-15 12:12:21 -0700 | [diff] [blame] | 9046 | for (j = 0; j <= cur->curframe; j++) { |
| 9047 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) |
| 9048 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; |
| 9049 | for (i = 0; i < BPF_REG_FP; i++) |
| 9050 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; |
| 9051 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9052 | |
| 9053 | /* all stack frames are accessible from callee, clear them all */ |
| 9054 | for (j = 0; j <= cur->curframe; j++) { |
| 9055 | struct bpf_func_state *frame = cur->frame[j]; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9056 | struct bpf_func_state *newframe = new->frame[j]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9057 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9058 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 9059 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 9060 | frame->stack[i].spilled_ptr.parent = |
| 9061 | &newframe->stack[i].spilled_ptr; |
| 9062 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9063 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9064 | return 0; |
| 9065 | } |
| 9066 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 9067 | /* Return true if it's OK to have the same insn return a different type. */ |
| 9068 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 9069 | { |
| 9070 | switch (type) { |
| 9071 | case PTR_TO_CTX: |
| 9072 | case PTR_TO_SOCKET: |
| 9073 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 9074 | case PTR_TO_SOCK_COMMON: |
| 9075 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 9076 | case PTR_TO_TCP_SOCK: |
| 9077 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 9078 | case PTR_TO_XDP_SOCK: |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 9079 | case PTR_TO_BTF_ID: |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 9080 | case PTR_TO_BTF_ID_OR_NULL: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 9081 | return false; |
| 9082 | default: |
| 9083 | return true; |
| 9084 | } |
| 9085 | } |
| 9086 | |
| 9087 | /* If an instruction was previously used with particular pointer types, then we |
| 9088 | * need to be careful to avoid cases such as the below, where it may be ok |
| 9089 | * for one branch accessing the pointer, but not ok for the other branch: |
| 9090 | * |
| 9091 | * R1 = sock_ptr |
| 9092 | * goto X; |
| 9093 | * ... |
| 9094 | * R1 = some_other_valid_ptr; |
| 9095 | * goto X; |
| 9096 | * ... |
| 9097 | * R2 = *(u32 *)(R1 + 0); |
| 9098 | */ |
| 9099 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 9100 | { |
| 9101 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 9102 | !reg_type_mismatch_ok(prev)); |
| 9103 | } |
| 9104 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9105 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9106 | { |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 9107 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 9108 | struct bpf_verifier_state *state = env->cur_state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9109 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9110 | struct bpf_reg_state *regs; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9111 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9112 | bool do_print_state = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9113 | int prev_insn_idx = -1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9114 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9115 | for (;;) { |
| 9116 | struct bpf_insn *insn; |
| 9117 | u8 class; |
| 9118 | int err; |
| 9119 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9120 | env->prev_insn_idx = prev_insn_idx; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9121 | if (env->insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9122 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9123 | env->insn_idx, insn_cnt); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9124 | return -EFAULT; |
| 9125 | } |
| 9126 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9127 | insn = &insns[env->insn_idx]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9128 | class = BPF_CLASS(insn->code); |
| 9129 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9130 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9131 | verbose(env, |
| 9132 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9133 | env->insn_processed); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9134 | return -E2BIG; |
| 9135 | } |
| 9136 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9137 | err = is_state_visited(env, env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9138 | if (err < 0) |
| 9139 | return err; |
| 9140 | if (err == 1) { |
| 9141 | /* found equivalent state, can prune the search */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9142 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9143 | if (do_print_state) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 9144 | verbose(env, "\nfrom %d to %d%s: safe\n", |
| 9145 | env->prev_insn_idx, env->insn_idx, |
| 9146 | env->cur_state->speculative ? |
| 9147 | " (speculative execution)" : ""); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9148 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9149 | verbose(env, "%d: safe\n", env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9150 | } |
| 9151 | goto process_bpf_exit; |
| 9152 | } |
| 9153 | |
Alexei Starovoitov | c349480 | 2018-12-03 22:46:04 -0800 | [diff] [blame] | 9154 | if (signal_pending(current)) |
| 9155 | return -EAGAIN; |
| 9156 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 9157 | if (need_resched()) |
| 9158 | cond_resched(); |
| 9159 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9160 | if (env->log.level & BPF_LOG_LEVEL2 || |
| 9161 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { |
| 9162 | if (env->log.level & BPF_LOG_LEVEL2) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9163 | verbose(env, "%d:", env->insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 9164 | else |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 9165 | verbose(env, "\nfrom %d to %d%s:", |
| 9166 | env->prev_insn_idx, env->insn_idx, |
| 9167 | env->cur_state->speculative ? |
| 9168 | " (speculative execution)" : ""); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9169 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9170 | do_print_state = false; |
| 9171 | } |
| 9172 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9173 | if (env->log.level & BPF_LOG_LEVEL) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 9174 | const struct bpf_insn_cbs cbs = { |
| 9175 | .cb_print = verbose, |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 9176 | .private_data = env, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 9177 | }; |
| 9178 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9179 | verbose_linfo(env, env->insn_idx, "; "); |
| 9180 | verbose(env, "%d: ", env->insn_idx); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 9181 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9182 | } |
| 9183 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 9184 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9185 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
| 9186 | env->prev_insn_idx); |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 9187 | if (err) |
| 9188 | return err; |
| 9189 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 9190 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9191 | regs = cur_regs(env); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 9192 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9193 | prev_insn_idx = env->insn_idx; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9194 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9195 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 9196 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9197 | if (err) |
| 9198 | return err; |
| 9199 | |
| 9200 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9201 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9202 | |
| 9203 | /* check for reserved fields is already done */ |
| 9204 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9205 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9206 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9207 | if (err) |
| 9208 | return err; |
| 9209 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9210 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9211 | if (err) |
| 9212 | return err; |
| 9213 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 9214 | src_reg_type = regs[insn->src_reg].type; |
| 9215 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9216 | /* check that memory (src_reg + off) is readable, |
| 9217 | * the state of dst_reg will be updated by this func |
| 9218 | */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9219 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
| 9220 | insn->off, BPF_SIZE(insn->code), |
| 9221 | BPF_READ, insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9222 | if (err) |
| 9223 | return err; |
| 9224 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9225 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9226 | |
| 9227 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9228 | /* saw a valid insn |
| 9229 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9230 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9231 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9232 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9233 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 9234 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9235 | /* ABuser program is trying to use the same insn |
| 9236 | * dst_reg = *(u32*) (src_reg + off) |
| 9237 | * with different pointer types: |
| 9238 | * src_reg == ctx in one branch and |
| 9239 | * src_reg == stack|map in some other branch. |
| 9240 | * Reject it. |
| 9241 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9242 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9243 | return -EINVAL; |
| 9244 | } |
| 9245 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9246 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9247 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9248 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9249 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9250 | err = check_xadd(env, env->insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9251 | if (err) |
| 9252 | return err; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9253 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9254 | continue; |
| 9255 | } |
| 9256 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9257 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9258 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9259 | if (err) |
| 9260 | return err; |
| 9261 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9262 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9263 | if (err) |
| 9264 | return err; |
| 9265 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9266 | dst_reg_type = regs[insn->dst_reg].type; |
| 9267 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9268 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9269 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 9270 | insn->off, BPF_SIZE(insn->code), |
| 9271 | BPF_WRITE, insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9272 | if (err) |
| 9273 | return err; |
| 9274 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9275 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 9276 | |
| 9277 | if (*prev_dst_type == NOT_INIT) { |
| 9278 | *prev_dst_type = dst_reg_type; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 9279 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9280 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9281 | return -EINVAL; |
| 9282 | } |
| 9283 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9284 | } else if (class == BPF_ST) { |
| 9285 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 9286 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9287 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9288 | return -EINVAL; |
| 9289 | } |
| 9290 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9291 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9292 | if (err) |
| 9293 | return err; |
| 9294 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 9295 | if (is_ctx_reg(env, insn->dst_reg)) { |
Joe Stringer | 9d2be44 | 2018-10-02 13:35:31 -0700 | [diff] [blame] | 9296 | 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] | 9297 | insn->dst_reg, |
| 9298 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 9299 | return -EACCES; |
| 9300 | } |
| 9301 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9302 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9303 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 9304 | insn->off, BPF_SIZE(insn->code), |
| 9305 | BPF_WRITE, -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9306 | if (err) |
| 9307 | return err; |
| 9308 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9309 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9310 | u8 opcode = BPF_OP(insn->code); |
| 9311 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9312 | env->jmps_processed++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9313 | if (opcode == BPF_CALL) { |
| 9314 | if (BPF_SRC(insn->code) != BPF_K || |
| 9315 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9316 | (insn->src_reg != BPF_REG_0 && |
| 9317 | insn->src_reg != BPF_PSEUDO_CALL) || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9318 | insn->dst_reg != BPF_REG_0 || |
| 9319 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9320 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9321 | return -EINVAL; |
| 9322 | } |
| 9323 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9324 | if (env->cur_state->active_spin_lock && |
| 9325 | (insn->src_reg == BPF_PSEUDO_CALL || |
| 9326 | insn->imm != BPF_FUNC_spin_unlock)) { |
| 9327 | verbose(env, "function calls are not allowed while holding a lock\n"); |
| 9328 | return -EINVAL; |
| 9329 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9330 | if (insn->src_reg == BPF_PSEUDO_CALL) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9331 | err = check_func_call(env, insn, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9332 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9333 | err = check_helper_call(env, insn->imm, env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9334 | if (err) |
| 9335 | return err; |
| 9336 | |
| 9337 | } else if (opcode == BPF_JA) { |
| 9338 | if (BPF_SRC(insn->code) != BPF_K || |
| 9339 | insn->imm != 0 || |
| 9340 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9341 | insn->dst_reg != BPF_REG_0 || |
| 9342 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9343 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9344 | return -EINVAL; |
| 9345 | } |
| 9346 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9347 | env->insn_idx += insn->off + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9348 | continue; |
| 9349 | |
| 9350 | } else if (opcode == BPF_EXIT) { |
| 9351 | if (BPF_SRC(insn->code) != BPF_K || |
| 9352 | insn->imm != 0 || |
| 9353 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9354 | insn->dst_reg != BPF_REG_0 || |
| 9355 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9356 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9357 | return -EINVAL; |
| 9358 | } |
| 9359 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9360 | if (env->cur_state->active_spin_lock) { |
| 9361 | verbose(env, "bpf_spin_unlock is missing\n"); |
| 9362 | return -EINVAL; |
| 9363 | } |
| 9364 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9365 | if (state->curframe) { |
| 9366 | /* exit from nested function */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9367 | err = prepare_func_exit(env, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9368 | if (err) |
| 9369 | return err; |
| 9370 | do_print_state = true; |
| 9371 | continue; |
| 9372 | } |
| 9373 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9374 | err = check_reference_leak(env); |
| 9375 | if (err) |
| 9376 | return err; |
| 9377 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9378 | err = check_return_code(env); |
| 9379 | if (err) |
| 9380 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9381 | process_bpf_exit: |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9382 | update_branch_counts(env, env->cur_state); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9383 | err = pop_stack(env, &prev_insn_idx, |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 9384 | &env->insn_idx, pop_log); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9385 | if (err < 0) { |
| 9386 | if (err != -ENOENT) |
| 9387 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9388 | break; |
| 9389 | } else { |
| 9390 | do_print_state = true; |
| 9391 | continue; |
| 9392 | } |
| 9393 | } else { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9394 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9395 | if (err) |
| 9396 | return err; |
| 9397 | } |
| 9398 | } else if (class == BPF_LD) { |
| 9399 | u8 mode = BPF_MODE(insn->code); |
| 9400 | |
| 9401 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9402 | err = check_ld_abs(env, insn); |
| 9403 | if (err) |
| 9404 | return err; |
| 9405 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9406 | } else if (mode == BPF_IMM) { |
| 9407 | err = check_ld_imm(env, insn); |
| 9408 | if (err) |
| 9409 | return err; |
| 9410 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9411 | env->insn_idx++; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 9412 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9413 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9414 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9415 | return -EINVAL; |
| 9416 | } |
| 9417 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9418 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9419 | return -EINVAL; |
| 9420 | } |
| 9421 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 9422 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9423 | } |
| 9424 | |
| 9425 | return 0; |
| 9426 | } |
| 9427 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 9428 | static int check_map_prealloc(struct bpf_map *map) |
| 9429 | { |
| 9430 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 9431 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 9432 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 9433 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 9434 | } |
| 9435 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9436 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
| 9437 | { |
| 9438 | switch (type) { |
| 9439 | case BPF_PROG_TYPE_KPROBE: |
| 9440 | case BPF_PROG_TYPE_TRACEPOINT: |
| 9441 | case BPF_PROG_TYPE_PERF_EVENT: |
| 9442 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 9443 | return true; |
| 9444 | default: |
| 9445 | return false; |
| 9446 | } |
| 9447 | } |
| 9448 | |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 9449 | static bool is_preallocated_map(struct bpf_map *map) |
| 9450 | { |
| 9451 | if (!check_map_prealloc(map)) |
| 9452 | return false; |
| 9453 | if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) |
| 9454 | return false; |
| 9455 | return true; |
| 9456 | } |
| 9457 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9458 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 9459 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9460 | struct bpf_prog *prog) |
| 9461 | |
| 9462 | { |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9463 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 9464 | /* |
| 9465 | * Validate that trace type programs use preallocated hash maps. |
| 9466 | * |
| 9467 | * For programs attached to PERF events this is mandatory as the |
| 9468 | * perf NMI can hit any arbitrary code sequence. |
| 9469 | * |
| 9470 | * All other trace types using preallocated hash maps are unsafe as |
| 9471 | * well because tracepoint or kprobes can be inside locked regions |
| 9472 | * of the memory allocator or at a place where a recursion into the |
| 9473 | * memory allocator would see inconsistent state. |
| 9474 | * |
Thomas Gleixner | 2ed905c | 2020-02-24 15:01:33 +0100 | [diff] [blame] | 9475 | * On RT enabled kernels run-time allocation of all trace type |
| 9476 | * programs is strictly prohibited due to lock type constraints. On |
| 9477 | * !RT kernels it is allowed for backwards compatibility reasons for |
| 9478 | * now, but warnings are emitted so developers are made aware of |
| 9479 | * the unsafety and can fix their programs before this is enforced. |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 9480 | */ |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9481 | if (is_tracing_prog_type(prog_type) && !is_preallocated_map(map)) { |
| 9482 | if (prog_type == BPF_PROG_TYPE_PERF_EVENT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9483 | 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] | 9484 | return -EINVAL; |
| 9485 | } |
Thomas Gleixner | 2ed905c | 2020-02-24 15:01:33 +0100 | [diff] [blame] | 9486 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
| 9487 | verbose(env, "trace type programs can only use preallocated hash map\n"); |
| 9488 | return -EINVAL; |
| 9489 | } |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 9490 | WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); |
| 9491 | verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9492 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 9493 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9494 | if ((is_tracing_prog_type(prog_type) || |
| 9495 | prog_type == BPF_PROG_TYPE_SOCKET_FILTER) && |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9496 | map_value_has_spin_lock(map)) { |
| 9497 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); |
| 9498 | return -EINVAL; |
| 9499 | } |
| 9500 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 9501 | 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] | 9502 | !bpf_offload_prog_map_match(prog, map)) { |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 9503 | verbose(env, "offload device mismatch between prog and map\n"); |
| 9504 | return -EINVAL; |
| 9505 | } |
| 9506 | |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 9507 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
| 9508 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); |
| 9509 | return -EINVAL; |
| 9510 | } |
| 9511 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 9512 | if (prog->aux->sleepable) |
| 9513 | switch (map->map_type) { |
| 9514 | case BPF_MAP_TYPE_HASH: |
| 9515 | case BPF_MAP_TYPE_LRU_HASH: |
| 9516 | case BPF_MAP_TYPE_ARRAY: |
| 9517 | if (!is_preallocated_map(map)) { |
| 9518 | verbose(env, |
| 9519 | "Sleepable programs can only use preallocated hash maps\n"); |
| 9520 | return -EINVAL; |
| 9521 | } |
| 9522 | break; |
| 9523 | default: |
| 9524 | verbose(env, |
| 9525 | "Sleepable programs can only use array and hash maps\n"); |
| 9526 | return -EINVAL; |
| 9527 | } |
| 9528 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9529 | return 0; |
| 9530 | } |
| 9531 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 9532 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 9533 | { |
| 9534 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 9535 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 9536 | } |
| 9537 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9538 | /* look for pseudo eBPF instructions that access map FDs and |
| 9539 | * replace them with actual map pointers |
| 9540 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9541 | 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] | 9542 | { |
| 9543 | struct bpf_insn *insn = env->prog->insnsi; |
| 9544 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9545 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9546 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 9547 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 9548 | if (err) |
| 9549 | return err; |
| 9550 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9551 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9552 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9553 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9554 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 9555 | return -EINVAL; |
| 9556 | } |
| 9557 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9558 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 9559 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 9560 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9561 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 9562 | return -EINVAL; |
| 9563 | } |
| 9564 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9565 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9566 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9567 | struct bpf_map *map; |
| 9568 | struct fd f; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9569 | u64 addr; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9570 | |
| 9571 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 9572 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 9573 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9574 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9575 | return -EINVAL; |
| 9576 | } |
| 9577 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9578 | if (insn[0].src_reg == 0) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9579 | /* valid generic load 64-bit imm */ |
| 9580 | goto next_insn; |
| 9581 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9582 | /* In final convert_pseudo_ld_imm64() step, this is |
| 9583 | * converted into regular 64-bit imm load insn. |
| 9584 | */ |
| 9585 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && |
| 9586 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || |
| 9587 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && |
| 9588 | insn[1].imm != 0)) { |
| 9589 | verbose(env, |
| 9590 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9591 | return -EINVAL; |
| 9592 | } |
| 9593 | |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 9594 | f = fdget(insn[0].imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 9595 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9596 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9597 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 9598 | insn[0].imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9599 | return PTR_ERR(map); |
| 9600 | } |
| 9601 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9602 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 9603 | if (err) { |
| 9604 | fdput(f); |
| 9605 | return err; |
| 9606 | } |
| 9607 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9608 | aux = &env->insn_aux_data[i]; |
| 9609 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 9610 | addr = (unsigned long)map; |
| 9611 | } else { |
| 9612 | u32 off = insn[1].imm; |
| 9613 | |
| 9614 | if (off >= BPF_MAX_VAR_OFF) { |
| 9615 | verbose(env, "direct value offset of %u is not allowed\n", off); |
| 9616 | fdput(f); |
| 9617 | return -EINVAL; |
| 9618 | } |
| 9619 | |
| 9620 | if (!map->ops->map_direct_value_addr) { |
| 9621 | verbose(env, "no direct value access support for this map type\n"); |
| 9622 | fdput(f); |
| 9623 | return -EINVAL; |
| 9624 | } |
| 9625 | |
| 9626 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 9627 | if (err) { |
| 9628 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", |
| 9629 | map->value_size, off); |
| 9630 | fdput(f); |
| 9631 | return err; |
| 9632 | } |
| 9633 | |
| 9634 | aux->map_off = off; |
| 9635 | addr += off; |
| 9636 | } |
| 9637 | |
| 9638 | insn[0].imm = (u32)addr; |
| 9639 | insn[1].imm = addr >> 32; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9640 | |
| 9641 | /* check whether we recorded this map already */ |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9642 | for (j = 0; j < env->used_map_cnt; j++) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9643 | if (env->used_maps[j] == map) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9644 | aux->map_index = j; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9645 | fdput(f); |
| 9646 | goto next_insn; |
| 9647 | } |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9648 | } |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9649 | |
| 9650 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 9651 | fdput(f); |
| 9652 | return -E2BIG; |
| 9653 | } |
| 9654 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9655 | /* hold the map. If the program is rejected by verifier, |
| 9656 | * the map will be released by release_maps() or it |
| 9657 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 9658 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9659 | */ |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 9660 | bpf_map_inc(map); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9661 | |
| 9662 | aux->map_index = env->used_map_cnt; |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 9663 | env->used_maps[env->used_map_cnt++] = map; |
| 9664 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 9665 | if (bpf_map_is_cgroup_storage(map) && |
Daniel Borkmann | e473042 | 2019-12-17 13:28:16 +0100 | [diff] [blame] | 9666 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 9667 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 9668 | fdput(f); |
| 9669 | return -EBUSY; |
| 9670 | } |
| 9671 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9672 | fdput(f); |
| 9673 | next_insn: |
| 9674 | insn++; |
| 9675 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 9676 | continue; |
| 9677 | } |
| 9678 | |
| 9679 | /* Basic sanity check before we invest more work here. */ |
| 9680 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 9681 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 9682 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9683 | } |
| 9684 | } |
| 9685 | |
| 9686 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 9687 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 9688 | * These pointers will be used later by verifier to validate map access. |
| 9689 | */ |
| 9690 | return 0; |
| 9691 | } |
| 9692 | |
| 9693 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9694 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9695 | { |
Daniel Borkmann | a2ea074 | 2019-12-16 17:49:00 +0100 | [diff] [blame] | 9696 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
| 9697 | env->used_map_cnt); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9698 | } |
| 9699 | |
| 9700 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9701 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 9702 | { |
| 9703 | struct bpf_insn *insn = env->prog->insnsi; |
| 9704 | int insn_cnt = env->prog->len; |
| 9705 | int i; |
| 9706 | |
| 9707 | for (i = 0; i < insn_cnt; i++, insn++) |
| 9708 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 9709 | insn->src_reg = 0; |
| 9710 | } |
| 9711 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9712 | /* single env->prog->insni[off] instruction was replaced with the range |
| 9713 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 9714 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 9715 | */ |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 9716 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, |
| 9717 | struct bpf_prog *new_prog, u32 off, u32 cnt) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9718 | { |
| 9719 | 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] | 9720 | struct bpf_insn *insn = new_prog->insnsi; |
| 9721 | u32 prog_len; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 9722 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9723 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 9724 | /* aux info at OFF always needs adjustment, no matter fast path |
| 9725 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the |
| 9726 | * original insn at old prog. |
| 9727 | */ |
| 9728 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); |
| 9729 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9730 | if (cnt == 1) |
| 9731 | return 0; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 9732 | prog_len = new_prog->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 9733 | new_data = vzalloc(array_size(prog_len, |
| 9734 | sizeof(struct bpf_insn_aux_data))); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9735 | if (!new_data) |
| 9736 | return -ENOMEM; |
| 9737 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 9738 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 9739 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 9740 | for (i = off; i < off + cnt - 1; i++) { |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 9741 | new_data[i].seen = env->pass_cnt; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 9742 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
| 9743 | } |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9744 | env->insn_aux_data = new_data; |
| 9745 | vfree(old_data); |
| 9746 | return 0; |
| 9747 | } |
| 9748 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 9749 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 9750 | { |
| 9751 | int i; |
| 9752 | |
| 9753 | if (len == 1) |
| 9754 | return; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 9755 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 9756 | for (i = 0; i <= env->subprog_cnt; i++) { |
Edward Cree | afd5942 | 2018-11-16 12:00:07 +0000 | [diff] [blame] | 9757 | if (env->subprog_info[i].start <= off) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 9758 | continue; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 9759 | env->subprog_info[i].start += len - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 9760 | } |
| 9761 | } |
| 9762 | |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 9763 | static void adjust_poke_descs(struct bpf_prog *prog, u32 len) |
| 9764 | { |
| 9765 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; |
| 9766 | int i, sz = prog->aux->size_poke_tab; |
| 9767 | struct bpf_jit_poke_descriptor *desc; |
| 9768 | |
| 9769 | for (i = 0; i < sz; i++) { |
| 9770 | desc = &tab[i]; |
| 9771 | desc->insn_idx += len - 1; |
| 9772 | } |
| 9773 | } |
| 9774 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9775 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 9776 | const struct bpf_insn *patch, u32 len) |
| 9777 | { |
| 9778 | struct bpf_prog *new_prog; |
| 9779 | |
| 9780 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 9781 | if (IS_ERR(new_prog)) { |
| 9782 | if (PTR_ERR(new_prog) == -ERANGE) |
| 9783 | verbose(env, |
| 9784 | "insn %d cannot be patched due to 16-bit range\n", |
| 9785 | env->insn_aux_data[off].orig_idx); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9786 | return NULL; |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 9787 | } |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 9788 | if (adjust_insn_aux_data(env, new_prog, off, len)) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9789 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 9790 | adjust_subprog_starts(env, off, len); |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 9791 | adjust_poke_descs(new_prog, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 9792 | return new_prog; |
| 9793 | } |
| 9794 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 9795 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
| 9796 | u32 off, u32 cnt) |
| 9797 | { |
| 9798 | int i, j; |
| 9799 | |
| 9800 | /* find first prog starting at or after off (first to remove) */ |
| 9801 | for (i = 0; i < env->subprog_cnt; i++) |
| 9802 | if (env->subprog_info[i].start >= off) |
| 9803 | break; |
| 9804 | /* find first prog starting at or after off + cnt (first to stay) */ |
| 9805 | for (j = i; j < env->subprog_cnt; j++) |
| 9806 | if (env->subprog_info[j].start >= off + cnt) |
| 9807 | break; |
| 9808 | /* if j doesn't start exactly at off + cnt, we are just removing |
| 9809 | * the front of previous prog |
| 9810 | */ |
| 9811 | if (env->subprog_info[j].start != off + cnt) |
| 9812 | j--; |
| 9813 | |
| 9814 | if (j > i) { |
| 9815 | struct bpf_prog_aux *aux = env->prog->aux; |
| 9816 | int move; |
| 9817 | |
| 9818 | /* move fake 'exit' subprog as well */ |
| 9819 | move = env->subprog_cnt + 1 - j; |
| 9820 | |
| 9821 | memmove(env->subprog_info + i, |
| 9822 | env->subprog_info + j, |
| 9823 | sizeof(*env->subprog_info) * move); |
| 9824 | env->subprog_cnt -= j - i; |
| 9825 | |
| 9826 | /* remove func_info */ |
| 9827 | if (aux->func_info) { |
| 9828 | move = aux->func_info_cnt - j; |
| 9829 | |
| 9830 | memmove(aux->func_info + i, |
| 9831 | aux->func_info + j, |
| 9832 | sizeof(*aux->func_info) * move); |
| 9833 | aux->func_info_cnt -= j - i; |
| 9834 | /* func_info->insn_off is set after all code rewrites, |
| 9835 | * in adjust_btf_func() - no need to adjust |
| 9836 | */ |
| 9837 | } |
| 9838 | } else { |
| 9839 | /* convert i from "first prog to remove" to "first to adjust" */ |
| 9840 | if (env->subprog_info[i].start == off) |
| 9841 | i++; |
| 9842 | } |
| 9843 | |
| 9844 | /* update fake 'exit' subprog as well */ |
| 9845 | for (; i <= env->subprog_cnt; i++) |
| 9846 | env->subprog_info[i].start -= cnt; |
| 9847 | |
| 9848 | return 0; |
| 9849 | } |
| 9850 | |
| 9851 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, |
| 9852 | u32 cnt) |
| 9853 | { |
| 9854 | struct bpf_prog *prog = env->prog; |
| 9855 | u32 i, l_off, l_cnt, nr_linfo; |
| 9856 | struct bpf_line_info *linfo; |
| 9857 | |
| 9858 | nr_linfo = prog->aux->nr_linfo; |
| 9859 | if (!nr_linfo) |
| 9860 | return 0; |
| 9861 | |
| 9862 | linfo = prog->aux->linfo; |
| 9863 | |
| 9864 | /* find first line info to remove, count lines to be removed */ |
| 9865 | for (i = 0; i < nr_linfo; i++) |
| 9866 | if (linfo[i].insn_off >= off) |
| 9867 | break; |
| 9868 | |
| 9869 | l_off = i; |
| 9870 | l_cnt = 0; |
| 9871 | for (; i < nr_linfo; i++) |
| 9872 | if (linfo[i].insn_off < off + cnt) |
| 9873 | l_cnt++; |
| 9874 | else |
| 9875 | break; |
| 9876 | |
| 9877 | /* First live insn doesn't match first live linfo, it needs to "inherit" |
| 9878 | * last removed linfo. prog is already modified, so prog->len == off |
| 9879 | * means no live instructions after (tail of the program was removed). |
| 9880 | */ |
| 9881 | if (prog->len != off && l_cnt && |
| 9882 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { |
| 9883 | l_cnt--; |
| 9884 | linfo[--i].insn_off = off + cnt; |
| 9885 | } |
| 9886 | |
| 9887 | /* remove the line info which refer to the removed instructions */ |
| 9888 | if (l_cnt) { |
| 9889 | memmove(linfo + l_off, linfo + i, |
| 9890 | sizeof(*linfo) * (nr_linfo - i)); |
| 9891 | |
| 9892 | prog->aux->nr_linfo -= l_cnt; |
| 9893 | nr_linfo = prog->aux->nr_linfo; |
| 9894 | } |
| 9895 | |
| 9896 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ |
| 9897 | for (i = l_off; i < nr_linfo; i++) |
| 9898 | linfo[i].insn_off -= cnt; |
| 9899 | |
| 9900 | /* fix up all subprogs (incl. 'exit') which start >= off */ |
| 9901 | for (i = 0; i <= env->subprog_cnt; i++) |
| 9902 | if (env->subprog_info[i].linfo_idx > l_off) { |
| 9903 | /* program may have started in the removed region but |
| 9904 | * may not be fully removed |
| 9905 | */ |
| 9906 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) |
| 9907 | env->subprog_info[i].linfo_idx -= l_cnt; |
| 9908 | else |
| 9909 | env->subprog_info[i].linfo_idx = l_off; |
| 9910 | } |
| 9911 | |
| 9912 | return 0; |
| 9913 | } |
| 9914 | |
| 9915 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) |
| 9916 | { |
| 9917 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 9918 | unsigned int orig_prog_len = env->prog->len; |
| 9919 | int err; |
| 9920 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 9921 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 9922 | bpf_prog_offload_remove_insns(env, off, cnt); |
| 9923 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 9924 | err = bpf_remove_insns(env->prog, off, cnt); |
| 9925 | if (err) |
| 9926 | return err; |
| 9927 | |
| 9928 | err = adjust_subprog_starts_after_remove(env, off, cnt); |
| 9929 | if (err) |
| 9930 | return err; |
| 9931 | |
| 9932 | err = bpf_adj_linfo_after_remove(env, off, cnt); |
| 9933 | if (err) |
| 9934 | return err; |
| 9935 | |
| 9936 | memmove(aux_data + off, aux_data + off + cnt, |
| 9937 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); |
| 9938 | |
| 9939 | return 0; |
| 9940 | } |
| 9941 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 9942 | /* The verifier does more data flow analysis than llvm and will not |
| 9943 | * explore branches that are dead at run time. Malicious programs can |
| 9944 | * have dead code too. Therefore replace all dead at-run-time code |
| 9945 | * with 'ja -1'. |
| 9946 | * |
| 9947 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 9948 | * program and through another bug we would manage to jump there, then |
| 9949 | * we'd execute beyond program memory otherwise. Returning exception |
| 9950 | * code also wouldn't work since we can have subprogs where the dead |
| 9951 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 9952 | */ |
| 9953 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 9954 | { |
| 9955 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 9956 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 9957 | struct bpf_insn *insn = env->prog->insnsi; |
| 9958 | const int insn_cnt = env->prog->len; |
| 9959 | int i; |
| 9960 | |
| 9961 | for (i = 0; i < insn_cnt; i++) { |
| 9962 | if (aux_data[i].seen) |
| 9963 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 9964 | memcpy(insn + i, &trap, sizeof(trap)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 9965 | } |
| 9966 | } |
| 9967 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 9968 | static bool insn_is_cond_jump(u8 code) |
| 9969 | { |
| 9970 | u8 op; |
| 9971 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9972 | if (BPF_CLASS(code) == BPF_JMP32) |
| 9973 | return true; |
| 9974 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 9975 | if (BPF_CLASS(code) != BPF_JMP) |
| 9976 | return false; |
| 9977 | |
| 9978 | op = BPF_OP(code); |
| 9979 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; |
| 9980 | } |
| 9981 | |
| 9982 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) |
| 9983 | { |
| 9984 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 9985 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 9986 | struct bpf_insn *insn = env->prog->insnsi; |
| 9987 | const int insn_cnt = env->prog->len; |
| 9988 | int i; |
| 9989 | |
| 9990 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 9991 | if (!insn_is_cond_jump(insn->code)) |
| 9992 | continue; |
| 9993 | |
| 9994 | if (!aux_data[i + 1].seen) |
| 9995 | ja.off = insn->off; |
| 9996 | else if (!aux_data[i + 1 + insn->off].seen) |
| 9997 | ja.off = 0; |
| 9998 | else |
| 9999 | continue; |
| 10000 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 10001 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 10002 | bpf_prog_offload_replace_insn(env, i, &ja); |
| 10003 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 10004 | memcpy(insn, &ja, sizeof(ja)); |
| 10005 | } |
| 10006 | } |
| 10007 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 10008 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
| 10009 | { |
| 10010 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 10011 | int insn_cnt = env->prog->len; |
| 10012 | int i, err; |
| 10013 | |
| 10014 | for (i = 0; i < insn_cnt; i++) { |
| 10015 | int j; |
| 10016 | |
| 10017 | j = 0; |
| 10018 | while (i + j < insn_cnt && !aux_data[i + j].seen) |
| 10019 | j++; |
| 10020 | if (!j) |
| 10021 | continue; |
| 10022 | |
| 10023 | err = verifier_remove_insns(env, i, j); |
| 10024 | if (err) |
| 10025 | return err; |
| 10026 | insn_cnt = env->prog->len; |
| 10027 | } |
| 10028 | |
| 10029 | return 0; |
| 10030 | } |
| 10031 | |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 10032 | static int opt_remove_nops(struct bpf_verifier_env *env) |
| 10033 | { |
| 10034 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 10035 | struct bpf_insn *insn = env->prog->insnsi; |
| 10036 | int insn_cnt = env->prog->len; |
| 10037 | int i, err; |
| 10038 | |
| 10039 | for (i = 0; i < insn_cnt; i++) { |
| 10040 | if (memcmp(&insn[i], &ja, sizeof(ja))) |
| 10041 | continue; |
| 10042 | |
| 10043 | err = verifier_remove_insns(env, i, 1); |
| 10044 | if (err) |
| 10045 | return err; |
| 10046 | insn_cnt--; |
| 10047 | i--; |
| 10048 | } |
| 10049 | |
| 10050 | return 0; |
| 10051 | } |
| 10052 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10053 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
| 10054 | const union bpf_attr *attr) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10055 | { |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10056 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10057 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10058 | int i, patch_len, delta = 0, len = env->prog->len; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10059 | struct bpf_insn *insns = env->prog->insnsi; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10060 | struct bpf_prog *new_prog; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10061 | bool rnd_hi32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10062 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10063 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10064 | zext_patch[1] = BPF_ZEXT_REG(0); |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10065 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
| 10066 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); |
| 10067 | 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] | 10068 | for (i = 0; i < len; i++) { |
| 10069 | int adj_idx = i + delta; |
| 10070 | struct bpf_insn insn; |
| 10071 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10072 | insn = insns[adj_idx]; |
| 10073 | if (!aux[adj_idx].zext_dst) { |
| 10074 | u8 code, class; |
| 10075 | u32 imm_rnd; |
| 10076 | |
| 10077 | if (!rnd_hi32) |
| 10078 | continue; |
| 10079 | |
| 10080 | code = insn.code; |
| 10081 | class = BPF_CLASS(code); |
| 10082 | if (insn_no_def(&insn)) |
| 10083 | continue; |
| 10084 | |
| 10085 | /* NOTE: arg "reg" (the fourth one) is only used for |
| 10086 | * BPF_STX which has been ruled out in above |
| 10087 | * check, it is safe to pass NULL here. |
| 10088 | */ |
| 10089 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { |
| 10090 | if (class == BPF_LD && |
| 10091 | BPF_MODE(code) == BPF_IMM) |
| 10092 | i++; |
| 10093 | continue; |
| 10094 | } |
| 10095 | |
| 10096 | /* ctx load could be transformed into wider load. */ |
| 10097 | if (class == BPF_LDX && |
| 10098 | aux[adj_idx].ptr_type == PTR_TO_CTX) |
| 10099 | continue; |
| 10100 | |
| 10101 | imm_rnd = get_random_int(); |
| 10102 | rnd_hi32_patch[0] = insn; |
| 10103 | rnd_hi32_patch[1].imm = imm_rnd; |
| 10104 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; |
| 10105 | patch = rnd_hi32_patch; |
| 10106 | patch_len = 4; |
| 10107 | goto apply_patch_buffer; |
| 10108 | } |
| 10109 | |
| 10110 | if (!bpf_jit_needs_zext()) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10111 | continue; |
| 10112 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10113 | zext_patch[0] = insn; |
| 10114 | zext_patch[1].dst_reg = insn.dst_reg; |
| 10115 | zext_patch[1].src_reg = insn.dst_reg; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10116 | patch = zext_patch; |
| 10117 | patch_len = 2; |
| 10118 | apply_patch_buffer: |
| 10119 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10120 | if (!new_prog) |
| 10121 | return -ENOMEM; |
| 10122 | env->prog = new_prog; |
| 10123 | insns = new_prog->insnsi; |
| 10124 | aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 10125 | delta += patch_len - 1; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 10126 | } |
| 10127 | |
| 10128 | return 0; |
| 10129 | } |
| 10130 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10131 | /* convert load instructions that access fields of a context type into a |
| 10132 | * sequence of instructions that access fields of the underlying structure: |
| 10133 | * struct __sk_buff -> struct sk_buff |
| 10134 | * struct bpf_sock_ops -> struct sock |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10135 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 10136 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10137 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 10138 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10139 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10140 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10141 | struct bpf_insn insn_buf[16], *insn; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10142 | u32 target_size, size_default, off; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10143 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 10144 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10145 | bool is_narrower_load; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10146 | |
Daniel Borkmann | b09928b | 2018-10-24 22:05:49 +0200 | [diff] [blame] | 10147 | if (ops->gen_prologue || env->seen_direct_write) { |
| 10148 | if (!ops->gen_prologue) { |
| 10149 | verbose(env, "bpf verifier is misconfigured\n"); |
| 10150 | return -EINVAL; |
| 10151 | } |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10152 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 10153 | env->prog); |
| 10154 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10155 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10156 | return -EINVAL; |
| 10157 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10158 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10159 | if (!new_prog) |
| 10160 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10161 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10162 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10163 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10164 | } |
| 10165 | } |
| 10166 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10167 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10168 | return 0; |
| 10169 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10170 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 10171 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10172 | for (i = 0; i < insn_cnt; i++, insn++) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10173 | bpf_convert_ctx_access_t convert_ctx_access; |
| 10174 | |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 10175 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 10176 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 10177 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 10178 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 10179 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 10180 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 10181 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 10182 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 10183 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 10184 | type = BPF_WRITE; |
| 10185 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10186 | continue; |
| 10187 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 10188 | if (type == BPF_WRITE && |
| 10189 | env->insn_aux_data[i + delta].sanitize_stack_off) { |
| 10190 | struct bpf_insn patch[] = { |
| 10191 | /* Sanitize suspicious stack slot with zero. |
| 10192 | * There are no memory dependencies for this store, |
| 10193 | * since it's only using frame pointer and immediate |
| 10194 | * constant of zero |
| 10195 | */ |
| 10196 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, |
| 10197 | env->insn_aux_data[i + delta].sanitize_stack_off, |
| 10198 | 0), |
| 10199 | /* the original STX instruction will immediately |
| 10200 | * overwrite the same stack slot with appropriate value |
| 10201 | */ |
| 10202 | *insn, |
| 10203 | }; |
| 10204 | |
| 10205 | cnt = ARRAY_SIZE(patch); |
| 10206 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 10207 | if (!new_prog) |
| 10208 | return -ENOMEM; |
| 10209 | |
| 10210 | delta += cnt - 1; |
| 10211 | env->prog = new_prog; |
| 10212 | insn = new_prog->insnsi + i + delta; |
| 10213 | continue; |
| 10214 | } |
| 10215 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10216 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 10217 | case PTR_TO_CTX: |
| 10218 | if (!ops->convert_ctx_access) |
| 10219 | continue; |
| 10220 | convert_ctx_access = ops->convert_ctx_access; |
| 10221 | break; |
| 10222 | case PTR_TO_SOCKET: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 10223 | case PTR_TO_SOCK_COMMON: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10224 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 10225 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 10226 | case PTR_TO_TCP_SOCK: |
| 10227 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; |
| 10228 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 10229 | case PTR_TO_XDP_SOCK: |
| 10230 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; |
| 10231 | break; |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 10232 | case PTR_TO_BTF_ID: |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 10233 | if (type == BPF_READ) { |
| 10234 | insn->code = BPF_LDX | BPF_PROBE_MEM | |
| 10235 | BPF_SIZE((insn)->code); |
| 10236 | env->prog->aux->num_exentries++; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 10237 | } else if (resolve_prog_type(env->prog) != BPF_PROG_TYPE_STRUCT_OPS) { |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 10238 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
| 10239 | return -EINVAL; |
| 10240 | } |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 10241 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10242 | default: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10243 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10244 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10245 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10246 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10247 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10248 | |
| 10249 | /* If the read access is a narrower load of the field, |
| 10250 | * convert to a 4/8-byte load, to minimum program type specific |
| 10251 | * convert_ctx_access changes. If conversion is successful, |
| 10252 | * we will apply proper mask to the result. |
| 10253 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10254 | is_narrower_load = size < ctx_field_size; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10255 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
| 10256 | off = insn->off; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10257 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10258 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10259 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10260 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10261 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10262 | return -EINVAL; |
| 10263 | } |
| 10264 | |
| 10265 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10266 | if (ctx_field_size == 4) |
| 10267 | size_code = BPF_W; |
| 10268 | else if (ctx_field_size == 8) |
| 10269 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10270 | |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 10271 | insn->off = off & ~(size_default - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10272 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 10273 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10274 | |
| 10275 | target_size = 0; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10276 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 10277 | &target_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10278 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 10279 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10280 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10281 | return -EINVAL; |
| 10282 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10283 | |
| 10284 | if (is_narrower_load && size < target_size) { |
Ilya Leoshkevich | d895a0f | 2019-08-16 12:53:00 +0200 | [diff] [blame] | 10285 | u8 shift = bpf_ctx_narrow_access_offset( |
| 10286 | off, size, size_default) * 8; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10287 | if (ctx_field_size <= 4) { |
| 10288 | if (shift) |
| 10289 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, |
| 10290 | insn->dst_reg, |
| 10291 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10292 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 10293 | (1 << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10294 | } else { |
| 10295 | if (shift) |
| 10296 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, |
| 10297 | insn->dst_reg, |
| 10298 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10299 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Krzesimir Nowak | e2f7fc0 | 2019-05-08 18:08:58 +0200 | [diff] [blame] | 10300 | (1ULL << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 10301 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 10302 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10303 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 10304 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10305 | if (!new_prog) |
| 10306 | return -ENOMEM; |
| 10307 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10308 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10309 | |
| 10310 | /* keep walking new program and skip insns we just inserted */ |
| 10311 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 10312 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 10313 | } |
| 10314 | |
| 10315 | return 0; |
| 10316 | } |
| 10317 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10318 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 10319 | { |
| 10320 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 10321 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10322 | struct bpf_map *map_ptr; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10323 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10324 | void *old_bpf_func; |
Yonghong Song | c4c0bdc | 2020-06-23 17:10:54 -0700 | [diff] [blame] | 10325 | int err, num_exentries; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10326 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10327 | if (env->subprog_cnt <= 1) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10328 | return 0; |
| 10329 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10330 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10331 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10332 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10333 | continue; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10334 | /* Upon error here we cannot fall back to interpreter but |
| 10335 | * need a hard reject of the program. Thus -EFAULT is |
| 10336 | * propagated in any case. |
| 10337 | */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10338 | subprog = find_subprog(env, i + insn->imm + 1); |
| 10339 | if (subprog < 0) { |
| 10340 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 10341 | i + insn->imm + 1); |
| 10342 | return -EFAULT; |
| 10343 | } |
| 10344 | /* temporarily remember subprog id inside insn instead of |
| 10345 | * aux_data, since next loop will split up all insns into funcs |
| 10346 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10347 | insn->off = subprog; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10348 | /* remember original imm in case JIT fails and fallback |
| 10349 | * to interpreter will be needed |
| 10350 | */ |
| 10351 | env->insn_aux_data[i].call_imm = insn->imm; |
| 10352 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 10353 | insn->imm = 1; |
| 10354 | } |
| 10355 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10356 | err = bpf_prog_alloc_jited_linfo(prog); |
| 10357 | if (err) |
| 10358 | goto out_undo_insn; |
| 10359 | |
| 10360 | err = -ENOMEM; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 10361 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10362 | if (!func) |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10363 | goto out_undo_insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10364 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10365 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10366 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 10367 | subprog_end = env->subprog_info[i + 1].start; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10368 | |
| 10369 | len = subprog_end - subprog_start; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 10370 | /* BPF_PROG_RUN doesn't call subprogs directly, |
| 10371 | * hence main prog stats include the runtime of subprogs. |
| 10372 | * subprogs don't have IDs and not reachable via prog_get_next_id |
| 10373 | * func[i]->aux->stats will never be accessed and stays NULL |
| 10374 | */ |
| 10375 | 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] | 10376 | if (!func[i]) |
| 10377 | goto out_free; |
| 10378 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 10379 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 10380 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10381 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 10382 | if (bpf_prog_calc_tag(func[i])) |
| 10383 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10384 | func[i]->is_func = 1; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10385 | func[i]->aux->func_idx = i; |
| 10386 | /* the btf and func_info will be freed only at prog->aux */ |
| 10387 | func[i]->aux->btf = prog->aux->btf; |
| 10388 | func[i]->aux->func_info = prog->aux->func_info; |
| 10389 | |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10390 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
| 10391 | u32 insn_idx = prog->aux->poke_tab[j].insn_idx; |
| 10392 | int ret; |
| 10393 | |
| 10394 | if (!(insn_idx >= subprog_start && |
| 10395 | insn_idx <= subprog_end)) |
| 10396 | continue; |
| 10397 | |
| 10398 | ret = bpf_jit_add_poke_descriptor(func[i], |
| 10399 | &prog->aux->poke_tab[j]); |
| 10400 | if (ret < 0) { |
| 10401 | verbose(env, "adding tail call poke descriptor failed\n"); |
| 10402 | goto out_free; |
| 10403 | } |
| 10404 | |
| 10405 | func[i]->insnsi[insn_idx - subprog_start].imm = ret + 1; |
| 10406 | |
| 10407 | map_ptr = func[i]->aux->poke_tab[ret].tail_call.map; |
| 10408 | ret = map_ptr->ops->map_poke_track(map_ptr, func[i]->aux); |
| 10409 | if (ret < 0) { |
| 10410 | verbose(env, "tracking tail call prog failed\n"); |
| 10411 | goto out_free; |
| 10412 | } |
| 10413 | } |
| 10414 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10415 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 10416 | * Long term would need debug info to populate names |
| 10417 | */ |
| 10418 | func[i]->aux->name[0] = 'F'; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 10419 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10420 | func[i]->jit_requested = 1; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10421 | func[i]->aux->linfo = prog->aux->linfo; |
| 10422 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; |
| 10423 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; |
| 10424 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; |
Yonghong Song | c4c0bdc | 2020-06-23 17:10:54 -0700 | [diff] [blame] | 10425 | num_exentries = 0; |
| 10426 | insn = func[i]->insnsi; |
| 10427 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 10428 | if (BPF_CLASS(insn->code) == BPF_LDX && |
| 10429 | BPF_MODE(insn->code) == BPF_PROBE_MEM) |
| 10430 | num_exentries++; |
| 10431 | } |
| 10432 | func[i]->aux->num_exentries = num_exentries; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 10433 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10434 | func[i] = bpf_int_jit_compile(func[i]); |
| 10435 | if (!func[i]->jited) { |
| 10436 | err = -ENOTSUPP; |
| 10437 | goto out_free; |
| 10438 | } |
| 10439 | cond_resched(); |
| 10440 | } |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10441 | |
| 10442 | /* Untrack main program's aux structs so that during map_poke_run() |
| 10443 | * we will not stumble upon the unfilled poke descriptors; each |
| 10444 | * of the main program's poke descs got distributed across subprogs |
| 10445 | * and got tracked onto map, so we are sure that none of them will |
| 10446 | * be missed after the operation below |
| 10447 | */ |
| 10448 | for (i = 0; i < prog->aux->size_poke_tab; i++) { |
| 10449 | map_ptr = prog->aux->poke_tab[i].tail_call.map; |
| 10450 | |
| 10451 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); |
| 10452 | } |
| 10453 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10454 | /* at this point all bpf functions were successfully JITed |
| 10455 | * now populate all bpf_calls with correct addresses and |
| 10456 | * run last pass of JIT |
| 10457 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10458 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10459 | insn = func[i]->insnsi; |
| 10460 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 10461 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10462 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10463 | continue; |
| 10464 | subprog = insn->off; |
Prashant Bhole | 0d306c3 | 2019-04-16 18:13:01 +0900 | [diff] [blame] | 10465 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
| 10466 | __bpf_call_base; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10467 | } |
Sandipan Das | 2162fed | 2018-05-24 12:26:45 +0530 | [diff] [blame] | 10468 | |
| 10469 | /* we use the aux data to keep a list of the start addresses |
| 10470 | * of the JITed images for each function in the program |
| 10471 | * |
| 10472 | * for some architectures, such as powerpc64, the imm field |
| 10473 | * might not be large enough to hold the offset of the start |
| 10474 | * address of the callee's JITed image from __bpf_call_base |
| 10475 | * |
| 10476 | * in such cases, we can lookup the start address of a callee |
| 10477 | * by using its subprog id, available from the off field of |
| 10478 | * the call instruction, as an index for this list |
| 10479 | */ |
| 10480 | func[i]->aux->func = func; |
| 10481 | func[i]->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10482 | } |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10483 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10484 | old_bpf_func = func[i]->bpf_func; |
| 10485 | tmp = bpf_int_jit_compile(func[i]); |
| 10486 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 10487 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10488 | err = -ENOTSUPP; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10489 | goto out_free; |
| 10490 | } |
| 10491 | cond_resched(); |
| 10492 | } |
| 10493 | |
| 10494 | /* finally lock prog and jit images for all functions and |
| 10495 | * populate kallsysm |
| 10496 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10497 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10498 | bpf_prog_lock_ro(func[i]); |
| 10499 | bpf_prog_kallsyms_add(func[i]); |
| 10500 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10501 | |
| 10502 | /* Last step: make now unused interpreter insns from main |
| 10503 | * prog consistent for later dump requests, so they can |
| 10504 | * later look the same as if they were interpreted only. |
| 10505 | */ |
| 10506 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10507 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10508 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10509 | continue; |
| 10510 | insn->off = env->insn_aux_data[i].call_imm; |
| 10511 | subprog = find_subprog(env, i + insn->off + 1); |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 10512 | insn->imm = subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 10513 | } |
| 10514 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10515 | prog->jited = 1; |
| 10516 | prog->bpf_func = func[0]->bpf_func; |
| 10517 | prog->aux->func = func; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 10518 | prog->aux->func_cnt = env->subprog_cnt; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10519 | bpf_prog_free_unused_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10520 | return 0; |
| 10521 | out_free: |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10522 | for (i = 0; i < env->subprog_cnt; i++) { |
| 10523 | if (!func[i]) |
| 10524 | continue; |
| 10525 | |
| 10526 | for (j = 0; j < func[i]->aux->size_poke_tab; j++) { |
| 10527 | map_ptr = func[i]->aux->poke_tab[j].tail_call.map; |
| 10528 | map_ptr->ops->map_poke_untrack(map_ptr, func[i]->aux); |
| 10529 | } |
| 10530 | bpf_jit_free(func[i]); |
| 10531 | } |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10532 | kfree(func); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10533 | out_undo_insn: |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10534 | /* cleanup main prog to be interpreted */ |
| 10535 | prog->jit_requested = 0; |
| 10536 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 10537 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10538 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10539 | continue; |
| 10540 | insn->off = 0; |
| 10541 | insn->imm = env->insn_aux_data[i].call_imm; |
| 10542 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10543 | bpf_prog_free_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10544 | return err; |
| 10545 | } |
| 10546 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10547 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 10548 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10549 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10550 | struct bpf_prog *prog = env->prog; |
| 10551 | struct bpf_insn *insn = prog->insnsi; |
| 10552 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10553 | #endif |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 10554 | int err = 0; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10555 | |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 10556 | if (env->prog->jit_requested && |
| 10557 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10558 | err = jit_subprogs(env); |
| 10559 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 10560 | return 0; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 10561 | if (err == -EFAULT) |
| 10562 | return err; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10563 | } |
| 10564 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 10565 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
| 10566 | /* When JIT fails the progs with bpf2bpf calls and tail_calls |
| 10567 | * have to be rejected, since interpreter doesn't support them yet. |
| 10568 | */ |
| 10569 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); |
| 10570 | return -EINVAL; |
| 10571 | } |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10572 | for (i = 0; i < prog->len; i++, insn++) { |
| 10573 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10574 | insn->src_reg != BPF_PSEUDO_CALL) |
| 10575 | continue; |
| 10576 | depth = get_callee_stack_depth(env, insn, i); |
| 10577 | if (depth < 0) |
| 10578 | return depth; |
| 10579 | bpf_patch_call_args(insn, depth); |
| 10580 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 10581 | err = 0; |
| 10582 | #endif |
| 10583 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 10584 | } |
| 10585 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10586 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 10587 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10588 | * |
| 10589 | * this function is called after eBPF program passed verification |
| 10590 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10591 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10592 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10593 | struct bpf_prog *prog = env->prog; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 10594 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10595 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10596 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10597 | const int insn_cnt = prog->len; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 10598 | const struct bpf_map_ops *ops; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 10599 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 10600 | struct bpf_insn insn_buf[16]; |
| 10601 | struct bpf_prog *new_prog; |
| 10602 | struct bpf_map *map_ptr; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 10603 | int i, ret, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10604 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10605 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 10606 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 10607 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 10608 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 10609 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 10610 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
| 10611 | struct bpf_insn mask_and_div[] = { |
| 10612 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 10613 | /* Rx div 0 -> 0 */ |
| 10614 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), |
| 10615 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 10616 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 10617 | *insn, |
| 10618 | }; |
| 10619 | struct bpf_insn mask_and_mod[] = { |
| 10620 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 10621 | /* Rx mod 0 -> Rx */ |
| 10622 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), |
| 10623 | *insn, |
| 10624 | }; |
| 10625 | struct bpf_insn *patchlet; |
| 10626 | |
| 10627 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 10628 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 10629 | patchlet = mask_and_div + (is64 ? 1 : 0); |
| 10630 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); |
| 10631 | } else { |
| 10632 | patchlet = mask_and_mod + (is64 ? 1 : 0); |
| 10633 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); |
| 10634 | } |
| 10635 | |
| 10636 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 10637 | if (!new_prog) |
| 10638 | return -ENOMEM; |
| 10639 | |
| 10640 | delta += cnt - 1; |
| 10641 | env->prog = prog = new_prog; |
| 10642 | insn = new_prog->insnsi + i + delta; |
| 10643 | continue; |
| 10644 | } |
| 10645 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 10646 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 10647 | (BPF_MODE(insn->code) == BPF_ABS || |
| 10648 | BPF_MODE(insn->code) == BPF_IND)) { |
| 10649 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 10650 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 10651 | verbose(env, "bpf verifier is misconfigured\n"); |
| 10652 | return -EINVAL; |
| 10653 | } |
| 10654 | |
| 10655 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 10656 | if (!new_prog) |
| 10657 | return -ENOMEM; |
| 10658 | |
| 10659 | delta += cnt - 1; |
| 10660 | env->prog = prog = new_prog; |
| 10661 | insn = new_prog->insnsi + i + delta; |
| 10662 | continue; |
| 10663 | } |
| 10664 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 10665 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
| 10666 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { |
| 10667 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; |
| 10668 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; |
| 10669 | struct bpf_insn insn_buf[16]; |
| 10670 | struct bpf_insn *patch = &insn_buf[0]; |
| 10671 | bool issrc, isneg; |
| 10672 | u32 off_reg; |
| 10673 | |
| 10674 | aux = &env->insn_aux_data[i + delta]; |
Daniel Borkmann | 3612af7 | 2019-03-01 22:05:29 +0100 | [diff] [blame] | 10675 | if (!aux->alu_state || |
| 10676 | aux->alu_state == BPF_ALU_NON_POINTER) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 10677 | continue; |
| 10678 | |
| 10679 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; |
| 10680 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == |
| 10681 | BPF_ALU_SANITIZE_SRC; |
| 10682 | |
| 10683 | off_reg = issrc ? insn->src_reg : insn->dst_reg; |
| 10684 | if (isneg) |
| 10685 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 10686 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); |
| 10687 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); |
| 10688 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); |
| 10689 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); |
| 10690 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); |
| 10691 | if (issrc) { |
| 10692 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, |
| 10693 | off_reg); |
| 10694 | insn->src_reg = BPF_REG_AX; |
| 10695 | } else { |
| 10696 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, |
| 10697 | BPF_REG_AX); |
| 10698 | } |
| 10699 | if (isneg) |
| 10700 | insn->code = insn->code == code_add ? |
| 10701 | code_sub : code_add; |
| 10702 | *patch++ = *insn; |
| 10703 | if (issrc && isneg) |
| 10704 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 10705 | cnt = patch - insn_buf; |
| 10706 | |
| 10707 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 10708 | if (!new_prog) |
| 10709 | return -ENOMEM; |
| 10710 | |
| 10711 | delta += cnt - 1; |
| 10712 | env->prog = prog = new_prog; |
| 10713 | insn = new_prog->insnsi + i + delta; |
| 10714 | continue; |
| 10715 | } |
| 10716 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10717 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 10718 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 10719 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 10720 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10721 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10722 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 10723 | prog->dst_needed = 1; |
| 10724 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 10725 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 10726 | if (insn->imm == BPF_FUNC_override_return) |
| 10727 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10728 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 10729 | /* If we tail call into other programs, we |
| 10730 | * cannot make any assumptions since they can |
| 10731 | * be replaced dynamically during runtime in |
| 10732 | * the program array. |
| 10733 | */ |
| 10734 | prog->cb_access = 1; |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 10735 | if (!allow_tail_call_in_subprogs(env)) |
| 10736 | prog->aux->stack_depth = MAX_BPF_STACK; |
| 10737 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 10738 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10739 | /* mark bpf_tail_call as different opcode to avoid |
| 10740 | * conditional branch in the interpeter for every normal |
| 10741 | * call and to prevent accidental JITing by JIT compiler |
| 10742 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10743 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10744 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 10745 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 10746 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 10747 | aux = &env->insn_aux_data[i + delta]; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 10748 | if (env->bpf_capable && !expect_blinding && |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 10749 | prog->jit_requested && |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 10750 | !bpf_map_key_poisoned(aux) && |
| 10751 | !bpf_map_ptr_poisoned(aux) && |
| 10752 | !bpf_map_ptr_unpriv(aux)) { |
| 10753 | struct bpf_jit_poke_descriptor desc = { |
| 10754 | .reason = BPF_POKE_REASON_TAIL_CALL, |
| 10755 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), |
| 10756 | .tail_call.key = bpf_map_key_immediate(aux), |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 10757 | .insn_idx = i + delta, |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 10758 | }; |
| 10759 | |
| 10760 | ret = bpf_jit_add_poke_descriptor(prog, &desc); |
| 10761 | if (ret < 0) { |
| 10762 | verbose(env, "adding tail call poke descriptor failed\n"); |
| 10763 | return ret; |
| 10764 | } |
| 10765 | |
| 10766 | insn->imm = ret + 1; |
| 10767 | continue; |
| 10768 | } |
| 10769 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 10770 | if (!bpf_map_ptr_unpriv(aux)) |
| 10771 | continue; |
| 10772 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 10773 | /* instead of changing every JIT dealing with tail_call |
| 10774 | * emit two extra insns: |
| 10775 | * if (index >= max_entries) goto out; |
| 10776 | * index &= array->index_mask; |
| 10777 | * to avoid out-of-bounds cpu speculation |
| 10778 | */ |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 10779 | if (bpf_map_ptr_poisoned(aux)) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 10780 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 10781 | return -EINVAL; |
| 10782 | } |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 10783 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 10784 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 10785 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 10786 | map_ptr->max_entries, 2); |
| 10787 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 10788 | container_of(map_ptr, |
| 10789 | struct bpf_array, |
| 10790 | map)->index_mask); |
| 10791 | insn_buf[2] = *insn; |
| 10792 | cnt = 3; |
| 10793 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 10794 | if (!new_prog) |
| 10795 | return -ENOMEM; |
| 10796 | |
| 10797 | delta += cnt - 1; |
| 10798 | env->prog = prog = new_prog; |
| 10799 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10800 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10801 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10802 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 10803 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 10804 | * and other inlining handlers are currently limited to 64 bit |
| 10805 | * only. |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 10806 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 10807 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 10808 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 10809 | insn->imm == BPF_FUNC_map_update_elem || |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 10810 | insn->imm == BPF_FUNC_map_delete_elem || |
| 10811 | insn->imm == BPF_FUNC_map_push_elem || |
| 10812 | insn->imm == BPF_FUNC_map_pop_elem || |
| 10813 | insn->imm == BPF_FUNC_map_peek_elem)) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 10814 | aux = &env->insn_aux_data[i + delta]; |
| 10815 | if (bpf_map_ptr_poisoned(aux)) |
| 10816 | goto patch_call_imm; |
| 10817 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 10818 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 10819 | ops = map_ptr->ops; |
| 10820 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 10821 | ops->map_gen_lookup) { |
| 10822 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
| 10823 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 10824 | verbose(env, "bpf verifier is misconfigured\n"); |
| 10825 | return -EINVAL; |
| 10826 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 10827 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 10828 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 10829 | insn_buf, cnt); |
| 10830 | if (!new_prog) |
| 10831 | return -ENOMEM; |
| 10832 | |
| 10833 | delta += cnt - 1; |
| 10834 | env->prog = prog = new_prog; |
| 10835 | insn = new_prog->insnsi + i + delta; |
| 10836 | continue; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 10837 | } |
| 10838 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 10839 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 10840 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 10841 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 10842 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 10843 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 10844 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 10845 | u64 flags))NULL)); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 10846 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 10847 | (int (*)(struct bpf_map *map, void *value, |
| 10848 | u64 flags))NULL)); |
| 10849 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 10850 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 10851 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 10852 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 10853 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 10854 | switch (insn->imm) { |
| 10855 | case BPF_FUNC_map_lookup_elem: |
| 10856 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - |
| 10857 | __bpf_call_base; |
| 10858 | continue; |
| 10859 | case BPF_FUNC_map_update_elem: |
| 10860 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - |
| 10861 | __bpf_call_base; |
| 10862 | continue; |
| 10863 | case BPF_FUNC_map_delete_elem: |
| 10864 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - |
| 10865 | __bpf_call_base; |
| 10866 | continue; |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 10867 | case BPF_FUNC_map_push_elem: |
| 10868 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - |
| 10869 | __bpf_call_base; |
| 10870 | continue; |
| 10871 | case BPF_FUNC_map_pop_elem: |
| 10872 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - |
| 10873 | __bpf_call_base; |
| 10874 | continue; |
| 10875 | case BPF_FUNC_map_peek_elem: |
| 10876 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - |
| 10877 | __bpf_call_base; |
| 10878 | continue; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 10879 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 10880 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 10881 | goto patch_call_imm; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 10882 | } |
| 10883 | |
Martin KaFai Lau | 5576b99 | 2020-01-22 15:36:46 -0800 | [diff] [blame] | 10884 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
| 10885 | insn->imm == BPF_FUNC_jiffies64) { |
| 10886 | struct bpf_insn ld_jiffies_addr[2] = { |
| 10887 | BPF_LD_IMM64(BPF_REG_0, |
| 10888 | (unsigned long)&jiffies), |
| 10889 | }; |
| 10890 | |
| 10891 | insn_buf[0] = ld_jiffies_addr[0]; |
| 10892 | insn_buf[1] = ld_jiffies_addr[1]; |
| 10893 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, |
| 10894 | BPF_REG_0, 0); |
| 10895 | cnt = 3; |
| 10896 | |
| 10897 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, |
| 10898 | cnt); |
| 10899 | if (!new_prog) |
| 10900 | return -ENOMEM; |
| 10901 | |
| 10902 | delta += cnt - 1; |
| 10903 | env->prog = prog = new_prog; |
| 10904 | insn = new_prog->insnsi + i + delta; |
| 10905 | continue; |
| 10906 | } |
| 10907 | |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 10908 | patch_call_imm: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 10909 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10910 | /* all functions that have prototype and verifier allowed |
| 10911 | * programs to call them, must be real in-kernel functions |
| 10912 | */ |
| 10913 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10914 | verbose(env, |
| 10915 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10916 | func_id_name(insn->imm), insn->imm); |
| 10917 | return -EFAULT; |
| 10918 | } |
| 10919 | insn->imm = fn->func - __bpf_call_base; |
| 10920 | } |
| 10921 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 10922 | /* Since poke tab is now finalized, publish aux to tracker. */ |
| 10923 | for (i = 0; i < prog->aux->size_poke_tab; i++) { |
| 10924 | map_ptr = prog->aux->poke_tab[i].tail_call.map; |
| 10925 | if (!map_ptr->ops->map_poke_track || |
| 10926 | !map_ptr->ops->map_poke_untrack || |
| 10927 | !map_ptr->ops->map_poke_run) { |
| 10928 | verbose(env, "bpf verifier is misconfigured\n"); |
| 10929 | return -EINVAL; |
| 10930 | } |
| 10931 | |
| 10932 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); |
| 10933 | if (ret < 0) { |
| 10934 | verbose(env, "tracking tail call prog failed\n"); |
| 10935 | return ret; |
| 10936 | } |
| 10937 | } |
| 10938 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 10939 | return 0; |
| 10940 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 10941 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 10942 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 10943 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 10944 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 10945 | int i; |
| 10946 | |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 10947 | sl = env->free_list; |
| 10948 | while (sl) { |
| 10949 | sln = sl->next; |
| 10950 | free_verifier_state(&sl->state, false); |
| 10951 | kfree(sl); |
| 10952 | sl = sln; |
| 10953 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 10954 | env->free_list = NULL; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 10955 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 10956 | if (!env->explored_states) |
| 10957 | return; |
| 10958 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 10959 | for (i = 0; i < state_htab_size(env); i++) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 10960 | sl = env->explored_states[i]; |
| 10961 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 10962 | while (sl) { |
| 10963 | sln = sl->next; |
| 10964 | free_verifier_state(&sl->state, false); |
| 10965 | kfree(sl); |
| 10966 | sl = sln; |
| 10967 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 10968 | env->explored_states[i] = NULL; |
| 10969 | } |
| 10970 | } |
| 10971 | |
| 10972 | /* The verifier is using insn_aux_data[] to store temporary data during |
| 10973 | * verification and to store information for passes that run after the |
| 10974 | * verification like dead code sanitization. do_check_common() for subprogram N |
| 10975 | * may analyze many other subprograms. sanitize_insn_aux_data() clears all |
| 10976 | * temporary data after do_check_common() finds that subprogram N cannot be |
| 10977 | * verified independently. pass_cnt counts the number of times |
| 10978 | * do_check_common() was run and insn->aux->seen tells the pass number |
| 10979 | * insn_aux_data was touched. These variables are compared to clear temporary |
| 10980 | * data from failed pass. For testing and experiments do_check_common() can be |
| 10981 | * run multiple times even when prior attempt to verify is unsuccessful. |
| 10982 | */ |
| 10983 | static void sanitize_insn_aux_data(struct bpf_verifier_env *env) |
| 10984 | { |
| 10985 | struct bpf_insn *insn = env->prog->insnsi; |
| 10986 | struct bpf_insn_aux_data *aux; |
| 10987 | int i, class; |
| 10988 | |
| 10989 | for (i = 0; i < env->prog->len; i++) { |
| 10990 | class = BPF_CLASS(insn[i].code); |
| 10991 | if (class != BPF_LDX && class != BPF_STX) |
| 10992 | continue; |
| 10993 | aux = &env->insn_aux_data[i]; |
| 10994 | if (aux->seen != env->pass_cnt) |
| 10995 | continue; |
| 10996 | memset(aux, 0, offsetof(typeof(*aux), orig_idx)); |
| 10997 | } |
| 10998 | } |
| 10999 | |
| 11000 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
| 11001 | { |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 11002 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11003 | struct bpf_verifier_state *state; |
| 11004 | struct bpf_reg_state *regs; |
| 11005 | int ret, i; |
| 11006 | |
| 11007 | env->prev_linfo = NULL; |
| 11008 | env->pass_cnt++; |
| 11009 | |
| 11010 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 11011 | if (!state) |
| 11012 | return -ENOMEM; |
| 11013 | state->curframe = 0; |
| 11014 | state->speculative = false; |
| 11015 | state->branches = 1; |
| 11016 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 11017 | if (!state->frame[0]) { |
| 11018 | kfree(state); |
| 11019 | return -ENOMEM; |
| 11020 | } |
| 11021 | env->cur_state = state; |
| 11022 | init_func_state(env, state->frame[0], |
| 11023 | BPF_MAIN_FUNC /* callsite */, |
| 11024 | 0 /* frameno */, |
| 11025 | subprog); |
| 11026 | |
| 11027 | regs = state->frame[state->curframe]->regs; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11028 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11029 | ret = btf_prepare_func_args(env, subprog, regs); |
| 11030 | if (ret) |
| 11031 | goto out; |
| 11032 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { |
| 11033 | if (regs[i].type == PTR_TO_CTX) |
| 11034 | mark_reg_known_zero(env, regs, i); |
| 11035 | else if (regs[i].type == SCALAR_VALUE) |
| 11036 | mark_reg_unknown(env, regs, i); |
| 11037 | } |
| 11038 | } else { |
| 11039 | /* 1st arg to a function */ |
| 11040 | regs[BPF_REG_1].type = PTR_TO_CTX; |
| 11041 | mark_reg_known_zero(env, regs, BPF_REG_1); |
| 11042 | ret = btf_check_func_arg_match(env, subprog, regs); |
| 11043 | if (ret == -EFAULT) |
| 11044 | /* unlikely verifier bug. abort. |
| 11045 | * ret == 0 and ret < 0 are sadly acceptable for |
| 11046 | * main() function due to backward compatibility. |
| 11047 | * Like socket filter program may be written as: |
| 11048 | * int bpf_prog(struct pt_regs *ctx) |
| 11049 | * and never dereference that ctx in the program. |
| 11050 | * 'struct pt_regs' is a type mismatch for socket |
| 11051 | * filter that should be using 'struct __sk_buff'. |
| 11052 | */ |
| 11053 | goto out; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11054 | } |
| 11055 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11056 | ret = do_check(env); |
| 11057 | out: |
Alexei Starovoitov | f59bbfc | 2020-01-21 18:41:38 -0800 | [diff] [blame] | 11058 | /* check for NULL is necessary, since cur_state can be freed inside |
| 11059 | * do_check() under memory pressure. |
| 11060 | */ |
| 11061 | if (env->cur_state) { |
| 11062 | free_verifier_state(env->cur_state, true); |
| 11063 | env->cur_state = NULL; |
| 11064 | } |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 11065 | while (!pop_stack(env, NULL, NULL, false)); |
| 11066 | if (!ret && pop_log) |
| 11067 | bpf_vlog_reset(&env->log, 0); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11068 | free_states(env); |
| 11069 | if (ret) |
| 11070 | /* clean aux data in case subprog was rejected */ |
| 11071 | sanitize_insn_aux_data(env); |
| 11072 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11073 | } |
| 11074 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11075 | /* Verify all global functions in a BPF program one by one based on their BTF. |
| 11076 | * All global functions must pass verification. Otherwise the whole program is rejected. |
| 11077 | * Consider: |
| 11078 | * int bar(int); |
| 11079 | * int foo(int f) |
| 11080 | * { |
| 11081 | * return bar(f); |
| 11082 | * } |
| 11083 | * int bar(int b) |
| 11084 | * { |
| 11085 | * ... |
| 11086 | * } |
| 11087 | * foo() will be verified first for R1=any_scalar_value. During verification it |
| 11088 | * will be assumed that bar() already verified successfully and call to bar() |
| 11089 | * from foo() will be checked for type match only. Later bar() will be verified |
| 11090 | * independently to check that it's safe for R1=any_scalar_value. |
| 11091 | */ |
| 11092 | static int do_check_subprogs(struct bpf_verifier_env *env) |
| 11093 | { |
| 11094 | struct bpf_prog_aux *aux = env->prog->aux; |
| 11095 | int i, ret; |
| 11096 | |
| 11097 | if (!aux->func_info) |
| 11098 | return 0; |
| 11099 | |
| 11100 | for (i = 1; i < env->subprog_cnt; i++) { |
| 11101 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) |
| 11102 | continue; |
| 11103 | env->insn_idx = env->subprog_info[i].start; |
| 11104 | WARN_ON_ONCE(env->insn_idx == 0); |
| 11105 | ret = do_check_common(env, i); |
| 11106 | if (ret) { |
| 11107 | return ret; |
| 11108 | } else if (env->log.level & BPF_LOG_LEVEL) { |
| 11109 | verbose(env, |
| 11110 | "Func#%d is safe for any args that match its prototype\n", |
| 11111 | i); |
| 11112 | } |
| 11113 | } |
| 11114 | return 0; |
| 11115 | } |
| 11116 | |
| 11117 | static int do_check_main(struct bpf_verifier_env *env) |
| 11118 | { |
| 11119 | int ret; |
| 11120 | |
| 11121 | env->insn_idx = 0; |
| 11122 | ret = do_check_common(env, 0); |
| 11123 | if (!ret) |
| 11124 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
| 11125 | return ret; |
| 11126 | } |
| 11127 | |
| 11128 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11129 | static void print_verification_stats(struct bpf_verifier_env *env) |
| 11130 | { |
| 11131 | int i; |
| 11132 | |
| 11133 | if (env->log.level & BPF_LOG_STATS) { |
| 11134 | verbose(env, "verification time %lld usec\n", |
| 11135 | div_u64(env->verification_time, 1000)); |
| 11136 | verbose(env, "stack depth "); |
| 11137 | for (i = 0; i < env->subprog_cnt; i++) { |
| 11138 | u32 depth = env->subprog_info[i].stack_depth; |
| 11139 | |
| 11140 | verbose(env, "%d", depth); |
| 11141 | if (i + 1 < env->subprog_cnt) |
| 11142 | verbose(env, "+"); |
| 11143 | } |
| 11144 | verbose(env, "\n"); |
| 11145 | } |
| 11146 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " |
| 11147 | "total_states %d peak_states %d mark_read %d\n", |
| 11148 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, |
| 11149 | env->max_states_per_insn, env->total_states, |
| 11150 | env->peak_states, env->longest_mark_read_walk); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11151 | } |
| 11152 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 11153 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
| 11154 | { |
| 11155 | const struct btf_type *t, *func_proto; |
| 11156 | const struct bpf_struct_ops *st_ops; |
| 11157 | const struct btf_member *member; |
| 11158 | struct bpf_prog *prog = env->prog; |
| 11159 | u32 btf_id, member_idx; |
| 11160 | const char *mname; |
| 11161 | |
| 11162 | btf_id = prog->aux->attach_btf_id; |
| 11163 | st_ops = bpf_struct_ops_find(btf_id); |
| 11164 | if (!st_ops) { |
| 11165 | verbose(env, "attach_btf_id %u is not a supported struct\n", |
| 11166 | btf_id); |
| 11167 | return -ENOTSUPP; |
| 11168 | } |
| 11169 | |
| 11170 | t = st_ops->type; |
| 11171 | member_idx = prog->expected_attach_type; |
| 11172 | if (member_idx >= btf_type_vlen(t)) { |
| 11173 | verbose(env, "attach to invalid member idx %u of struct %s\n", |
| 11174 | member_idx, st_ops->name); |
| 11175 | return -EINVAL; |
| 11176 | } |
| 11177 | |
| 11178 | member = &btf_type_member(t)[member_idx]; |
| 11179 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); |
| 11180 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, |
| 11181 | NULL); |
| 11182 | if (!func_proto) { |
| 11183 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", |
| 11184 | mname, member_idx, st_ops->name); |
| 11185 | return -EINVAL; |
| 11186 | } |
| 11187 | |
| 11188 | if (st_ops->check_member) { |
| 11189 | int err = st_ops->check_member(t, member); |
| 11190 | |
| 11191 | if (err) { |
| 11192 | verbose(env, "attach to unsupported member %s of struct %s\n", |
| 11193 | mname, st_ops->name); |
| 11194 | return err; |
| 11195 | } |
| 11196 | } |
| 11197 | |
| 11198 | prog->aux->attach_func_proto = func_proto; |
| 11199 | prog->aux->attach_func_name = mname; |
| 11200 | env->ops = st_ops->verifier_ops; |
| 11201 | |
| 11202 | return 0; |
| 11203 | } |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11204 | #define SECURITY_PREFIX "security_" |
| 11205 | |
Alexei Starovoitov | 18644ce | 2020-05-28 21:38:36 -0700 | [diff] [blame] | 11206 | static int check_attach_modify_return(struct bpf_prog *prog, unsigned long addr) |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11207 | { |
KP Singh | 6919175 | 2020-03-05 21:49:55 +0100 | [diff] [blame] | 11208 | if (within_error_injection_list(addr) || |
| 11209 | !strncmp(SECURITY_PREFIX, prog->aux->attach_func_name, |
| 11210 | sizeof(SECURITY_PREFIX) - 1)) |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11211 | return 0; |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11212 | |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 11213 | return -EINVAL; |
| 11214 | } |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 11215 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11216 | /* non exhaustive list of sleepable bpf_lsm_*() functions */ |
| 11217 | BTF_SET_START(btf_sleepable_lsm_hooks) |
| 11218 | #ifdef CONFIG_BPF_LSM |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11219 | BTF_ID(func, bpf_lsm_bprm_committed_creds) |
Alexei Starovoitov | 29523c5 | 2020-08-31 09:31:32 -0700 | [diff] [blame] | 11220 | #else |
| 11221 | BTF_ID_UNUSED |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11222 | #endif |
| 11223 | BTF_SET_END(btf_sleepable_lsm_hooks) |
| 11224 | |
| 11225 | static int check_sleepable_lsm_hook(u32 btf_id) |
| 11226 | { |
| 11227 | return btf_id_set_contains(&btf_sleepable_lsm_hooks, btf_id); |
| 11228 | } |
| 11229 | |
| 11230 | /* list of non-sleepable functions that are otherwise on |
| 11231 | * ALLOW_ERROR_INJECTION list |
| 11232 | */ |
| 11233 | BTF_SET_START(btf_non_sleepable_error_inject) |
| 11234 | /* Three functions below can be called from sleepable and non-sleepable context. |
| 11235 | * Assume non-sleepable from bpf safety point of view. |
| 11236 | */ |
| 11237 | BTF_ID(func, __add_to_page_cache_locked) |
| 11238 | BTF_ID(func, should_fail_alloc_page) |
| 11239 | BTF_ID(func, should_failslab) |
| 11240 | BTF_SET_END(btf_non_sleepable_error_inject) |
| 11241 | |
| 11242 | static int check_non_sleepable_error_inject(u32 btf_id) |
| 11243 | { |
| 11244 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); |
| 11245 | } |
| 11246 | |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11247 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
| 11248 | { |
| 11249 | struct bpf_prog *prog = env->prog; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11250 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11251 | struct bpf_prog *tgt_prog = prog->aux->linked_prog; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11252 | u32 btf_id = prog->aux->attach_btf_id; |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11253 | const char prefix[] = "btf_trace_"; |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 11254 | struct btf_func_model fmodel; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11255 | int ret = 0, subprog = -1, i; |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11256 | struct bpf_trampoline *tr; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11257 | const struct btf_type *t; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11258 | bool conservative = true; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11259 | const char *tname; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11260 | struct btf *btf; |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11261 | long addr; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11262 | u64 key; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11263 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11264 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && |
| 11265 | prog->type != BPF_PROG_TYPE_LSM) { |
| 11266 | verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n"); |
| 11267 | return -EINVAL; |
| 11268 | } |
| 11269 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 11270 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) |
| 11271 | return check_struct_ops_btf_id(env); |
| 11272 | |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 11273 | if (prog->type != BPF_PROG_TYPE_TRACING && |
| 11274 | prog->type != BPF_PROG_TYPE_LSM && |
| 11275 | !prog_extension) |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11276 | return 0; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11277 | |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11278 | if (!btf_id) { |
| 11279 | verbose(env, "Tracing programs must provide btf_id\n"); |
| 11280 | return -EINVAL; |
| 11281 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11282 | btf = bpf_prog_get_target_btf(prog); |
| 11283 | if (!btf) { |
| 11284 | verbose(env, |
| 11285 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
| 11286 | return -EINVAL; |
| 11287 | } |
| 11288 | t = btf_type_by_id(btf, btf_id); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11289 | if (!t) { |
| 11290 | verbose(env, "attach_btf_id %u is invalid\n", btf_id); |
| 11291 | return -EINVAL; |
| 11292 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11293 | tname = btf_name_by_offset(btf, t->name_off); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11294 | if (!tname) { |
| 11295 | verbose(env, "attach_btf_id %u doesn't have a name\n", btf_id); |
| 11296 | return -EINVAL; |
| 11297 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11298 | if (tgt_prog) { |
| 11299 | struct bpf_prog_aux *aux = tgt_prog->aux; |
| 11300 | |
| 11301 | for (i = 0; i < aux->func_info_cnt; i++) |
| 11302 | if (aux->func_info[i].type_id == btf_id) { |
| 11303 | subprog = i; |
| 11304 | break; |
| 11305 | } |
| 11306 | if (subprog == -1) { |
| 11307 | verbose(env, "Subprog %s doesn't exist\n", tname); |
| 11308 | return -EINVAL; |
| 11309 | } |
| 11310 | conservative = aux->func_info_aux[subprog].unreliable; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11311 | if (prog_extension) { |
| 11312 | if (conservative) { |
| 11313 | verbose(env, |
| 11314 | "Cannot replace static functions\n"); |
| 11315 | return -EINVAL; |
| 11316 | } |
| 11317 | if (!prog->jit_requested) { |
| 11318 | verbose(env, |
| 11319 | "Extension programs should be JITed\n"); |
| 11320 | return -EINVAL; |
| 11321 | } |
| 11322 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
Toke Høiland-Jørgensen | 03f87c0 | 2020-04-24 15:34:27 +0200 | [diff] [blame] | 11323 | prog->expected_attach_type = tgt_prog->expected_attach_type; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11324 | } |
| 11325 | if (!tgt_prog->jited) { |
| 11326 | verbose(env, "Can attach to only JITed progs\n"); |
| 11327 | return -EINVAL; |
| 11328 | } |
| 11329 | if (tgt_prog->type == prog->type) { |
| 11330 | /* Cannot fentry/fexit another fentry/fexit program. |
| 11331 | * Cannot attach program extension to another extension. |
| 11332 | * It's ok to attach fentry/fexit to extension program. |
| 11333 | */ |
| 11334 | verbose(env, "Cannot recursively attach\n"); |
| 11335 | return -EINVAL; |
| 11336 | } |
| 11337 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && |
| 11338 | prog_extension && |
| 11339 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || |
| 11340 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { |
| 11341 | /* Program extensions can extend all program types |
| 11342 | * except fentry/fexit. The reason is the following. |
| 11343 | * The fentry/fexit programs are used for performance |
| 11344 | * analysis, stats and can be attached to any program |
| 11345 | * type except themselves. When extension program is |
| 11346 | * replacing XDP function it is necessary to allow |
| 11347 | * performance analysis of all functions. Both original |
| 11348 | * XDP program and its program extension. Hence |
| 11349 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is |
| 11350 | * allowed. If extending of fentry/fexit was allowed it |
| 11351 | * would be possible to create long call chain |
| 11352 | * fentry->extension->fentry->extension beyond |
| 11353 | * reasonable stack size. Hence extending fentry is not |
| 11354 | * allowed. |
| 11355 | */ |
| 11356 | verbose(env, "Cannot extend fentry/fexit\n"); |
| 11357 | return -EINVAL; |
| 11358 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11359 | key = ((u64)aux->id) << 32 | btf_id; |
| 11360 | } else { |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11361 | if (prog_extension) { |
| 11362 | verbose(env, "Cannot replace kernel functions\n"); |
| 11363 | return -EINVAL; |
| 11364 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11365 | key = btf_id; |
| 11366 | } |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11367 | |
| 11368 | switch (prog->expected_attach_type) { |
| 11369 | case BPF_TRACE_RAW_TP: |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11370 | if (tgt_prog) { |
| 11371 | verbose(env, |
| 11372 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
| 11373 | return -EINVAL; |
| 11374 | } |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11375 | if (!btf_type_is_typedef(t)) { |
| 11376 | verbose(env, "attach_btf_id %u is not a typedef\n", |
| 11377 | btf_id); |
| 11378 | return -EINVAL; |
| 11379 | } |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11380 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11381 | verbose(env, "attach_btf_id %u points to wrong type name %s\n", |
| 11382 | btf_id, tname); |
| 11383 | return -EINVAL; |
| 11384 | } |
| 11385 | tname += sizeof(prefix) - 1; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11386 | t = btf_type_by_id(btf, t->type); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11387 | if (!btf_type_is_ptr(t)) |
| 11388 | /* should never happen in valid vmlinux build */ |
| 11389 | return -EINVAL; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11390 | t = btf_type_by_id(btf, t->type); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11391 | if (!btf_type_is_func_proto(t)) |
| 11392 | /* should never happen in valid vmlinux build */ |
| 11393 | return -EINVAL; |
| 11394 | |
| 11395 | /* remember two read only pointers that are valid for |
| 11396 | * the life time of the kernel |
| 11397 | */ |
| 11398 | prog->aux->attach_func_name = tname; |
| 11399 | prog->aux->attach_func_proto = t; |
| 11400 | prog->aux->attach_btf_trace = true; |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 11401 | return 0; |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 11402 | case BPF_TRACE_ITER: |
| 11403 | if (!btf_type_is_func(t)) { |
| 11404 | verbose(env, "attach_btf_id %u is not a function\n", |
| 11405 | btf_id); |
| 11406 | return -EINVAL; |
| 11407 | } |
| 11408 | t = btf_type_by_id(btf, t->type); |
| 11409 | if (!btf_type_is_func_proto(t)) |
| 11410 | return -EINVAL; |
| 11411 | prog->aux->attach_func_name = tname; |
| 11412 | prog->aux->attach_func_proto = t; |
| 11413 | if (!bpf_iter_prog_supported(prog)) |
| 11414 | return -EINVAL; |
| 11415 | ret = btf_distill_func_proto(&env->log, btf, t, |
| 11416 | tname, &fmodel); |
| 11417 | return ret; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11418 | default: |
| 11419 | if (!prog_extension) |
| 11420 | return -EINVAL; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 11421 | fallthrough; |
KP Singh | ae24082 | 2020-03-04 20:18:49 +0100 | [diff] [blame] | 11422 | case BPF_MODIFY_RETURN: |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 11423 | case BPF_LSM_MAC: |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11424 | case BPF_TRACE_FENTRY: |
| 11425 | case BPF_TRACE_FEXIT: |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 11426 | prog->aux->attach_func_name = tname; |
| 11427 | if (prog->type == BPF_PROG_TYPE_LSM) { |
| 11428 | ret = bpf_lsm_verify_prog(&env->log, prog); |
| 11429 | if (ret < 0) |
| 11430 | return ret; |
| 11431 | } |
| 11432 | |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11433 | if (!btf_type_is_func(t)) { |
| 11434 | verbose(env, "attach_btf_id %u is not a function\n", |
| 11435 | btf_id); |
| 11436 | return -EINVAL; |
| 11437 | } |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11438 | if (prog_extension && |
| 11439 | btf_check_type_match(env, prog, btf, t)) |
| 11440 | return -EINVAL; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11441 | t = btf_type_by_id(btf, t->type); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11442 | if (!btf_type_is_func_proto(t)) |
| 11443 | return -EINVAL; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11444 | tr = bpf_trampoline_lookup(key); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11445 | if (!tr) |
| 11446 | return -ENOMEM; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11447 | /* t is either vmlinux type or another program's type */ |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11448 | prog->aux->attach_func_proto = t; |
| 11449 | mutex_lock(&tr->mutex); |
| 11450 | if (tr->func.addr) { |
| 11451 | prog->aux->trampoline = tr; |
| 11452 | goto out; |
| 11453 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11454 | if (tgt_prog && conservative) { |
| 11455 | prog->aux->attach_func_proto = NULL; |
| 11456 | t = NULL; |
| 11457 | } |
| 11458 | ret = btf_distill_func_proto(&env->log, btf, t, |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11459 | tname, &tr->func.model); |
| 11460 | if (ret < 0) |
| 11461 | goto out; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11462 | if (tgt_prog) { |
Yonghong Song | e9eeec5 | 2019-12-04 17:06:06 -0800 | [diff] [blame] | 11463 | if (subprog == 0) |
| 11464 | addr = (long) tgt_prog->bpf_func; |
| 11465 | else |
| 11466 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 11467 | } else { |
| 11468 | addr = kallsyms_lookup_name(tname); |
| 11469 | if (!addr) { |
| 11470 | verbose(env, |
| 11471 | "The address of function %s cannot be found\n", |
| 11472 | tname); |
| 11473 | ret = -ENOENT; |
| 11474 | goto out; |
| 11475 | } |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11476 | } |
Alexei Starovoitov | 18644ce | 2020-05-28 21:38:36 -0700 | [diff] [blame] | 11477 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11478 | if (prog->aux->sleepable) { |
| 11479 | ret = -EINVAL; |
| 11480 | switch (prog->type) { |
| 11481 | case BPF_PROG_TYPE_TRACING: |
| 11482 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are |
| 11483 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. |
| 11484 | */ |
| 11485 | if (!check_non_sleepable_error_inject(btf_id) && |
| 11486 | within_error_injection_list(addr)) |
| 11487 | ret = 0; |
| 11488 | break; |
| 11489 | case BPF_PROG_TYPE_LSM: |
| 11490 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. |
| 11491 | * Only some of them are sleepable. |
| 11492 | */ |
| 11493 | if (check_sleepable_lsm_hook(btf_id)) |
| 11494 | ret = 0; |
| 11495 | break; |
| 11496 | default: |
| 11497 | break; |
| 11498 | } |
| 11499 | if (ret) |
| 11500 | verbose(env, "%s is not sleepable\n", |
| 11501 | prog->aux->attach_func_name); |
| 11502 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
Alexei Starovoitov | 18644ce | 2020-05-28 21:38:36 -0700 | [diff] [blame] | 11503 | ret = check_attach_modify_return(prog, addr); |
| 11504 | if (ret) |
| 11505 | verbose(env, "%s() is not modifiable\n", |
| 11506 | prog->aux->attach_func_name); |
| 11507 | } |
Alexei Starovoitov | 18644ce | 2020-05-28 21:38:36 -0700 | [diff] [blame] | 11508 | if (ret) |
| 11509 | goto out; |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 11510 | tr->func.addr = (void *)addr; |
| 11511 | prog->aux->trampoline = tr; |
| 11512 | out: |
| 11513 | mutex_unlock(&tr->mutex); |
| 11514 | if (ret) |
| 11515 | bpf_trampoline_put(tr); |
| 11516 | return ret; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11517 | } |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11518 | } |
| 11519 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 11520 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
| 11521 | union bpf_attr __user *uattr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 11522 | { |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11523 | u64 start_time = ktime_get_ns(); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11524 | struct bpf_verifier_env *env; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 11525 | struct bpf_verifier_log *log; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 11526 | int i, len, ret = -EINVAL; |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 11527 | bool is_priv; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 11528 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 11529 | /* no program is valid */ |
| 11530 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 11531 | return -EINVAL; |
| 11532 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11533 | /* '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] | 11534 | * allocate/free it every time bpf_check() is called |
| 11535 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11536 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11537 | if (!env) |
| 11538 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11539 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11540 | |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 11541 | len = (*prog)->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 11542 | env->insn_aux_data = |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 11543 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11544 | ret = -ENOMEM; |
| 11545 | if (!env->insn_aux_data) |
| 11546 | goto err_free_env; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 11547 | for (i = 0; i < len; i++) |
| 11548 | env->insn_aux_data[i].orig_idx = i; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11549 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 11550 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 11551 | is_priv = bpf_capable(); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11552 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 11553 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { |
| 11554 | mutex_lock(&bpf_verifier_lock); |
| 11555 | if (!btf_vmlinux) |
| 11556 | btf_vmlinux = btf_parse_vmlinux(); |
| 11557 | mutex_unlock(&bpf_verifier_lock); |
| 11558 | } |
| 11559 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11560 | /* grab the mutex to protect few globals used by verifier */ |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 11561 | if (!is_priv) |
| 11562 | mutex_lock(&bpf_verifier_lock); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11563 | |
| 11564 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 11565 | /* user requested verbose verifier output |
| 11566 | * and supplied buffer to store the verification trace |
| 11567 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 11568 | log->level = attr->log_level; |
| 11569 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 11570 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11571 | |
| 11572 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 11573 | /* log attributes have to be sane */ |
Alexei Starovoitov | 7a9f5c6 | 2019-04-01 21:27:46 -0700 | [diff] [blame] | 11574 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11575 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11576 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11577 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 11578 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 11579 | if (IS_ERR(btf_vmlinux)) { |
| 11580 | /* Either gcc or pahole or kernel are broken. */ |
| 11581 | verbose(env, "in-kernel BTF is malformed\n"); |
| 11582 | ret = PTR_ERR(btf_vmlinux); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 11583 | goto skip_full_check; |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 11584 | } |
| 11585 | |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 11586 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 11587 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 11588 | env->strict_alignment = true; |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 11589 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
| 11590 | env->strict_alignment = false; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11591 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 11592 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 11593 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 11594 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
| 11595 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); |
| 11596 | env->bpf_capable = bpf_capable(); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 11597 | |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 11598 | if (is_priv) |
| 11599 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; |
| 11600 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11601 | ret = replace_map_fd_with_map_ptr(env); |
| 11602 | if (ret < 0) |
| 11603 | goto skip_full_check; |
| 11604 | |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 11605 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Quentin Monnet | a40a263 | 2018-11-09 13:03:31 +0000 | [diff] [blame] | 11606 | ret = bpf_prog_offload_verifier_prep(env->prog); |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 11607 | if (ret) |
| 11608 | goto skip_full_check; |
| 11609 | } |
| 11610 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 11611 | env->explored_states = kvcalloc(state_htab_size(env), |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11612 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11613 | GFP_USER); |
| 11614 | ret = -ENOMEM; |
| 11615 | if (!env->explored_states) |
| 11616 | goto skip_full_check; |
| 11617 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 11618 | ret = check_subprogs(env); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 11619 | if (ret < 0) |
| 11620 | goto skip_full_check; |
| 11621 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 11622 | ret = check_btf_info(env, attr, uattr); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 11623 | if (ret < 0) |
| 11624 | goto skip_full_check; |
| 11625 | |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 11626 | ret = check_attach_btf_id(env); |
| 11627 | if (ret) |
| 11628 | goto skip_full_check; |
| 11629 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 11630 | ret = check_cfg(env); |
| 11631 | if (ret < 0) |
| 11632 | goto skip_full_check; |
| 11633 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11634 | ret = do_check_subprogs(env); |
| 11635 | ret = ret ?: do_check_main(env); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11636 | |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 11637 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 11638 | ret = bpf_prog_offload_finalize(env); |
| 11639 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11640 | skip_full_check: |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11641 | kvfree(env->explored_states); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11642 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11643 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 11644 | ret = check_max_stack_depth(env); |
| 11645 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 11646 | /* instruction rewrites happen after this point */ |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 11647 | if (is_priv) { |
| 11648 | if (ret == 0) |
| 11649 | opt_hard_wire_dead_code_branches(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 11650 | if (ret == 0) |
| 11651 | ret = opt_remove_dead_code(env); |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 11652 | if (ret == 0) |
| 11653 | ret = opt_remove_nops(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 11654 | } else { |
| 11655 | if (ret == 0) |
| 11656 | sanitize_dead_code(env); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 11657 | } |
| 11658 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 11659 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11660 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 11661 | ret = convert_ctx_accesses(env); |
| 11662 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 11663 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 11664 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 11665 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 11666 | /* do 32-bit optimization after insn patching has done so those patched |
| 11667 | * insns could be handled correctly. |
| 11668 | */ |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 11669 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
| 11670 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); |
| 11671 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret |
| 11672 | : false; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 11673 | } |
| 11674 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 11675 | if (ret == 0) |
| 11676 | ret = fixup_call_args(env); |
| 11677 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11678 | env->verification_time = ktime_get_ns() - start_time; |
| 11679 | print_verification_stats(env); |
| 11680 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 11681 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11682 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 11683 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11684 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 11685 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11686 | } |
| 11687 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11688 | if (ret == 0 && env->used_map_cnt) { |
| 11689 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11690 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 11691 | sizeof(env->used_maps[0]), |
| 11692 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11693 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11694 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11695 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 11696 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11697 | } |
| 11698 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11699 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11700 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11701 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11702 | |
| 11703 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 11704 | * bpf_ld_imm64 instructions |
| 11705 | */ |
| 11706 | convert_pseudo_ld_imm64(env); |
| 11707 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 11708 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 11709 | if (ret == 0) |
| 11710 | adjust_btf_func(env); |
| 11711 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 11712 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11713 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11714 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 11715 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11716 | */ |
| 11717 | release_maps(env); |
Toke Høiland-Jørgensen | 03f87c0 | 2020-04-24 15:34:27 +0200 | [diff] [blame] | 11718 | |
| 11719 | /* extension progs temporarily inherit the attach_type of their targets |
| 11720 | for verification purposes, so set it back to zero before returning |
| 11721 | */ |
| 11722 | if (env->prog->type == BPF_PROG_TYPE_EXT) |
| 11723 | env->prog->expected_attach_type = 0; |
| 11724 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11725 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11726 | err_unlock: |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 11727 | if (!is_priv) |
| 11728 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11729 | vfree(env->insn_aux_data); |
| 11730 | err_free_env: |
| 11731 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 11732 | return ret; |
| 11733 | } |