Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016 Facebook |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4 | * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5 | */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6 | #include <uapi/linux/btf.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/types.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/bpf.h> |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 11 | #include <linux/btf.h> |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 12 | #include <linux/bpf_verifier.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 13 | #include <linux/filter.h> |
| 14 | #include <net/netlink.h> |
| 15 | #include <linux/file.h> |
| 16 | #include <linux/vmalloc.h> |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 17 | #include <linux/stringify.h> |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 18 | #include <linux/bsearch.h> |
| 19 | #include <linux/sort.h> |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 20 | #include <linux/perf_event.h> |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 21 | #include <linux/ctype.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 22 | |
Jakub Kicinski | f4ac7e0 | 2017-10-09 10:30:12 -0700 | [diff] [blame] | 23 | #include "disasm.h" |
| 24 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 25 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
| 26 | #define BPF_PROG_TYPE(_id, _name) \ |
| 27 | [_id] = & _name ## _verifier_ops, |
| 28 | #define BPF_MAP_TYPE(_id, _ops) |
| 29 | #include <linux/bpf_types.h> |
| 30 | #undef BPF_PROG_TYPE |
| 31 | #undef BPF_MAP_TYPE |
| 32 | }; |
| 33 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 34 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 35 | * instruction by instruction and updates register/stack state. |
| 36 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 37 | * |
| 38 | * The first pass is depth-first-search to check that the program is a DAG. |
| 39 | * It rejects the following programs: |
| 40 | * - larger than BPF_MAXINSNS insns |
| 41 | * - if loop is present (detected via back-edge) |
| 42 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 43 | * - out of bounds or malformed jumps |
| 44 | * The second pass is all possible path descent from the 1st insn. |
| 45 | * Since it's analyzing all pathes through the program, the length of the |
Gary Lin | eba38a9 | 2017-03-01 16:25:51 +0800 | [diff] [blame] | 46 | * analysis is limited to 64k insn, which may be hit even if total number of |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 47 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 48 | * Number of 'branches to be analyzed' is limited to 1k |
| 49 | * |
| 50 | * On entry to each instruction, each register has a type, and the instruction |
| 51 | * changes the types of the registers depending on instruction semantics. |
| 52 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 53 | * copied to R1. |
| 54 | * |
| 55 | * All registers are 64-bit. |
| 56 | * R0 - return register |
| 57 | * R1-R5 argument passing registers |
| 58 | * R6-R9 callee saved registers |
| 59 | * R10 - frame pointer read-only |
| 60 | * |
| 61 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 62 | * and has type PTR_TO_CTX. |
| 63 | * |
| 64 | * Verifier tracks arithmetic operations on pointers in case: |
| 65 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 66 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 67 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 68 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 69 | * that it wants to construct a pointer to some element within stack. |
| 70 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 71 | * (and -20 constant is saved for further stack bounds checking). |
| 72 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 73 | * |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 74 | * Most of the time the registers have SCALAR_VALUE type, which |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 75 | * means the register has some value, but it's not a valid pointer. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 76 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 77 | * |
| 78 | * When verifier sees load or store instructions the type of base register |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 79 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
| 80 | * four pointer types recognized by check_mem_access() function. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 81 | * |
| 82 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 83 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 84 | * |
| 85 | * registers used to pass values to function calls are checked against |
| 86 | * function argument constraints. |
| 87 | * |
| 88 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 89 | * It means that the register type passed to this function must be |
| 90 | * PTR_TO_STACK and it will be used inside the function as |
| 91 | * 'pointer to map element key' |
| 92 | * |
| 93 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 94 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 95 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 96 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 97 | * |
| 98 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 99 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 100 | * 2nd argument should be a pointer to stack, which will be used inside |
| 101 | * the helper function as a pointer to map element key. |
| 102 | * |
| 103 | * On the kernel side the helper function looks like: |
| 104 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 105 | * { |
| 106 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 107 | * void *key = (void *) (unsigned long) r2; |
| 108 | * void *value; |
| 109 | * |
| 110 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 111 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 112 | * the stack of eBPF program. |
| 113 | * } |
| 114 | * |
| 115 | * Corresponding eBPF program may look like: |
| 116 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 117 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 118 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 119 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 120 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 121 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 122 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 123 | * |
| 124 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 125 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 126 | * and were initialized prior to this call. |
| 127 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 128 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 129 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 130 | * returns ether pointer to map value or NULL. |
| 131 | * |
| 132 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 133 | * insn, the register holding that pointer in the true branch changes state to |
| 134 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 135 | * branch. See check_cond_jmp_op(). |
| 136 | * |
| 137 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 138 | * are set to NOT_INIT to indicate that they are no longer readable. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 139 | * |
| 140 | * The following reference types represent a potential reference to a kernel |
| 141 | * resource which, after first being allocated, must be checked and freed by |
| 142 | * the BPF program: |
| 143 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET |
| 144 | * |
| 145 | * When the verifier sees a helper call return a reference type, it allocates a |
| 146 | * pointer id for the reference and stores it in the current function state. |
| 147 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into |
| 148 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type |
| 149 | * passes through a NULL-check conditional. For the branch wherein the state is |
| 150 | * changed to CONST_IMM, the verifier releases the reference. |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 151 | * |
| 152 | * For each helper function that allocates a reference, such as |
| 153 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as |
| 154 | * bpf_sk_release(). When a reference type passes into the release function, |
| 155 | * the verifier also releases the reference. If any unchecked or unreleased |
| 156 | * reference remains at the end of the program, the verifier rejects it. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 157 | */ |
| 158 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 159 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 160 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 161 | /* verifer state is 'st' |
| 162 | * before processing instruction 'insn_idx' |
| 163 | * and after processing instruction 'prev_insn_idx' |
| 164 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 165 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 166 | int insn_idx; |
| 167 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 168 | struct bpf_verifier_stack_elem *next; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 171 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 172 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 173 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 174 | #define BPF_MAP_PTR_UNPRIV 1UL |
| 175 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ |
| 176 | POISON_POINTER_DELTA)) |
| 177 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) |
| 178 | |
| 179 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
| 180 | { |
| 181 | return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON; |
| 182 | } |
| 183 | |
| 184 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) |
| 185 | { |
| 186 | return aux->map_state & BPF_MAP_PTR_UNPRIV; |
| 187 | } |
| 188 | |
| 189 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, |
| 190 | const struct bpf_map *map, bool unpriv) |
| 191 | { |
| 192 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); |
| 193 | unpriv |= bpf_map_ptr_unpriv(aux); |
| 194 | aux->map_state = (unsigned long)map | |
| 195 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); |
| 196 | } |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 197 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 198 | struct bpf_call_arg_meta { |
| 199 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 200 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 201 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 202 | int regno; |
| 203 | int access_size; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 204 | s64 msize_smax_value; |
| 205 | u64 msize_umax_value; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 206 | int ref_obj_id; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 207 | int func_id; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 208 | }; |
| 209 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 210 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 211 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 212 | static const struct bpf_line_info * |
| 213 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) |
| 214 | { |
| 215 | const struct bpf_line_info *linfo; |
| 216 | const struct bpf_prog *prog; |
| 217 | u32 i, nr_linfo; |
| 218 | |
| 219 | prog = env->prog; |
| 220 | nr_linfo = prog->aux->nr_linfo; |
| 221 | |
| 222 | if (!nr_linfo || insn_off >= prog->len) |
| 223 | return NULL; |
| 224 | |
| 225 | linfo = prog->aux->linfo; |
| 226 | for (i = 1; i < nr_linfo; i++) |
| 227 | if (insn_off < linfo[i].insn_off) |
| 228 | break; |
| 229 | |
| 230 | return &linfo[i - 1]; |
| 231 | } |
| 232 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 233 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
| 234 | va_list args) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 235 | { |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 236 | unsigned int n; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 237 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 238 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 239 | |
| 240 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 241 | "verifier log line truncated - local buffer too short\n"); |
| 242 | |
| 243 | n = min(log->len_total - log->len_used - 1, n); |
| 244 | log->kbuf[n] = '\0'; |
| 245 | |
| 246 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 247 | log->len_used += n; |
| 248 | else |
| 249 | log->ubuf = NULL; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 250 | } |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 251 | |
| 252 | /* log_level controls verbosity level of eBPF verifier. |
| 253 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 254 | * so the user can figure out what's wrong with the program |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 255 | */ |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 256 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 257 | const char *fmt, ...) |
| 258 | { |
| 259 | va_list args; |
| 260 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 261 | if (!bpf_verifier_log_needed(&env->log)) |
| 262 | return; |
| 263 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 264 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 265 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 266 | va_end(args); |
| 267 | } |
| 268 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 269 | |
| 270 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
| 271 | { |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 272 | struct bpf_verifier_env *env = private_data; |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 273 | va_list args; |
| 274 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 275 | if (!bpf_verifier_log_needed(&env->log)) |
| 276 | return; |
| 277 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 278 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 279 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 280 | va_end(args); |
| 281 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 282 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 283 | static const char *ltrim(const char *s) |
| 284 | { |
| 285 | while (isspace(*s)) |
| 286 | s++; |
| 287 | |
| 288 | return s; |
| 289 | } |
| 290 | |
| 291 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, |
| 292 | u32 insn_off, |
| 293 | const char *prefix_fmt, ...) |
| 294 | { |
| 295 | const struct bpf_line_info *linfo; |
| 296 | |
| 297 | if (!bpf_verifier_log_needed(&env->log)) |
| 298 | return; |
| 299 | |
| 300 | linfo = find_linfo(env, insn_off); |
| 301 | if (!linfo || linfo == env->prev_linfo) |
| 302 | return; |
| 303 | |
| 304 | if (prefix_fmt) { |
| 305 | va_list args; |
| 306 | |
| 307 | va_start(args, prefix_fmt); |
| 308 | bpf_verifier_vlog(&env->log, prefix_fmt, args); |
| 309 | va_end(args); |
| 310 | } |
| 311 | |
| 312 | verbose(env, "%s\n", |
| 313 | ltrim(btf_name_by_offset(env->prog->aux->btf, |
| 314 | linfo->line_off))); |
| 315 | |
| 316 | env->prev_linfo = linfo; |
| 317 | } |
| 318 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 319 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 320 | { |
| 321 | return type == PTR_TO_PACKET || |
| 322 | type == PTR_TO_PACKET_META; |
| 323 | } |
| 324 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 325 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
| 326 | { |
| 327 | return type == PTR_TO_SOCKET || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 328 | type == PTR_TO_SOCK_COMMON || |
| 329 | type == PTR_TO_TCP_SOCK; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 332 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
| 333 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 334 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 335 | type == PTR_TO_SOCKET_OR_NULL || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 336 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
| 337 | type == PTR_TO_TCP_SOCK_OR_NULL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 340 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
| 341 | { |
| 342 | return reg->type == PTR_TO_MAP_VALUE && |
| 343 | map_value_has_spin_lock(reg->map_ptr); |
| 344 | } |
| 345 | |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 346 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
| 347 | { |
| 348 | return type == PTR_TO_SOCKET || |
| 349 | type == PTR_TO_SOCKET_OR_NULL || |
| 350 | type == PTR_TO_TCP_SOCK || |
| 351 | type == PTR_TO_TCP_SOCK_OR_NULL; |
| 352 | } |
| 353 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 354 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 355 | { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 356 | return type == ARG_PTR_TO_SOCK_COMMON; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* Determine whether the function releases some resources allocated by another |
| 360 | * function call. The first reference type argument will be assumed to be |
| 361 | * released by release_reference(). |
| 362 | */ |
| 363 | static bool is_release_function(enum bpf_func_id func_id) |
| 364 | { |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 365 | return func_id == BPF_FUNC_sk_release; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 368 | static bool is_acquire_function(enum bpf_func_id func_id) |
| 369 | { |
| 370 | return func_id == BPF_FUNC_sk_lookup_tcp || |
Lorenz Bauer | edbf8c0 | 2019-03-22 09:54:01 +0800 | [diff] [blame] | 371 | func_id == BPF_FUNC_sk_lookup_udp || |
| 372 | func_id == BPF_FUNC_skc_lookup_tcp; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 375 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
| 376 | { |
| 377 | return func_id == BPF_FUNC_tcp_sock || |
| 378 | func_id == BPF_FUNC_sk_fullsock; |
| 379 | } |
| 380 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 381 | /* string representation of 'enum bpf_reg_type' */ |
| 382 | static const char * const reg_type_str[] = { |
| 383 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 384 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 385 | [PTR_TO_CTX] = "ctx", |
| 386 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 387 | [PTR_TO_MAP_VALUE] = "map_value", |
| 388 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 389 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 390 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 391 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 392 | [PTR_TO_PACKET_END] = "pkt_end", |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 393 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 394 | [PTR_TO_SOCKET] = "sock", |
| 395 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 396 | [PTR_TO_SOCK_COMMON] = "sock_common", |
| 397 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 398 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
| 399 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 400 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 401 | }; |
| 402 | |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 403 | static char slot_type_char[] = { |
| 404 | [STACK_INVALID] = '?', |
| 405 | [STACK_SPILL] = 'r', |
| 406 | [STACK_MISC] = 'm', |
| 407 | [STACK_ZERO] = '0', |
| 408 | }; |
| 409 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 410 | static void print_liveness(struct bpf_verifier_env *env, |
| 411 | enum bpf_reg_liveness live) |
| 412 | { |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 413 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 414 | verbose(env, "_"); |
| 415 | if (live & REG_LIVE_READ) |
| 416 | verbose(env, "r"); |
| 417 | if (live & REG_LIVE_WRITTEN) |
| 418 | verbose(env, "w"); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 419 | if (live & REG_LIVE_DONE) |
| 420 | verbose(env, "D"); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 423 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 424 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 425 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 426 | struct bpf_verifier_state *cur = env->cur_state; |
| 427 | |
| 428 | return cur->frame[reg->frameno]; |
| 429 | } |
| 430 | |
| 431 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 432 | const struct bpf_func_state *state) |
| 433 | { |
| 434 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 435 | enum bpf_reg_type t; |
| 436 | int i; |
| 437 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 438 | if (state->frameno) |
| 439 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 440 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 441 | reg = &state->regs[i]; |
| 442 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 443 | if (t == NOT_INIT) |
| 444 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 445 | verbose(env, " R%d", i); |
| 446 | print_liveness(env, reg->live); |
| 447 | verbose(env, "=%s", reg_type_str[t]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 448 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 449 | tnum_is_const(reg->var_off)) { |
| 450 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 451 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 452 | if (t == PTR_TO_STACK) |
| 453 | verbose(env, ",call_%d", func(env, reg)->callsite); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 454 | } else { |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 455 | verbose(env, "(id=%d", reg->id); |
| 456 | if (reg_type_may_be_refcounted_or_null(t)) |
| 457 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 458 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 459 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 460 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 461 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 462 | else if (t == CONST_PTR_TO_MAP || |
| 463 | t == PTR_TO_MAP_VALUE || |
| 464 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 465 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 466 | reg->map_ptr->key_size, |
| 467 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 468 | if (tnum_is_const(reg->var_off)) { |
| 469 | /* Typically an immediate SCALAR_VALUE, but |
| 470 | * could be a pointer whose offset is too big |
| 471 | * for reg->off |
| 472 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 473 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 474 | } else { |
| 475 | if (reg->smin_value != reg->umin_value && |
| 476 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 477 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 478 | (long long)reg->smin_value); |
| 479 | if (reg->smax_value != reg->umax_value && |
| 480 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 481 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 482 | (long long)reg->smax_value); |
| 483 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 484 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 485 | (unsigned long long)reg->umin_value); |
| 486 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 487 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 488 | (unsigned long long)reg->umax_value); |
| 489 | if (!tnum_is_unknown(reg->var_off)) { |
| 490 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 491 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 492 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 493 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 494 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 495 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 496 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 497 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 498 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 499 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 500 | char types_buf[BPF_REG_SIZE + 1]; |
| 501 | bool valid = false; |
| 502 | int j; |
| 503 | |
| 504 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 505 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 506 | valid = true; |
| 507 | types_buf[j] = slot_type_char[ |
| 508 | state->stack[i].slot_type[j]]; |
| 509 | } |
| 510 | types_buf[BPF_REG_SIZE] = 0; |
| 511 | if (!valid) |
| 512 | continue; |
| 513 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 514 | print_liveness(env, state->stack[i].spilled_ptr.live); |
| 515 | if (state->stack[i].slot_type[0] == STACK_SPILL) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 516 | verbose(env, "=%s", |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 517 | reg_type_str[state->stack[i].spilled_ptr.type]); |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 518 | else |
| 519 | verbose(env, "=%s", types_buf); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 520 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 521 | if (state->acquired_refs && state->refs[0].id) { |
| 522 | verbose(env, " refs=%d", state->refs[0].id); |
| 523 | for (i = 1; i < state->acquired_refs; i++) |
| 524 | if (state->refs[i].id) |
| 525 | verbose(env, ",%d", state->refs[i].id); |
| 526 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 527 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 530 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 531 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ |
| 532 | const struct bpf_func_state *src) \ |
| 533 | { \ |
| 534 | if (!src->FIELD) \ |
| 535 | return 0; \ |
| 536 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ |
| 537 | /* internal bug, make state invalid to reject the program */ \ |
| 538 | memset(dst, 0, sizeof(*dst)); \ |
| 539 | return -EFAULT; \ |
| 540 | } \ |
| 541 | memcpy(dst->FIELD, src->FIELD, \ |
| 542 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ |
| 543 | return 0; \ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 544 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 545 | /* copy_reference_state() */ |
| 546 | COPY_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 547 | /* copy_stack_state() */ |
| 548 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 549 | #undef COPY_STATE_FN |
| 550 | |
| 551 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 552 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ |
| 553 | bool copy_old) \ |
| 554 | { \ |
| 555 | u32 old_size = state->COUNT; \ |
| 556 | struct bpf_##NAME##_state *new_##FIELD; \ |
| 557 | int slot = size / SIZE; \ |
| 558 | \ |
| 559 | if (size <= old_size || !size) { \ |
| 560 | if (copy_old) \ |
| 561 | return 0; \ |
| 562 | state->COUNT = slot * SIZE; \ |
| 563 | if (!size && old_size) { \ |
| 564 | kfree(state->FIELD); \ |
| 565 | state->FIELD = NULL; \ |
| 566 | } \ |
| 567 | return 0; \ |
| 568 | } \ |
| 569 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ |
| 570 | GFP_KERNEL); \ |
| 571 | if (!new_##FIELD) \ |
| 572 | return -ENOMEM; \ |
| 573 | if (copy_old) { \ |
| 574 | if (state->FIELD) \ |
| 575 | memcpy(new_##FIELD, state->FIELD, \ |
| 576 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ |
| 577 | memset(new_##FIELD + old_size / SIZE, 0, \ |
| 578 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ |
| 579 | } \ |
| 580 | state->COUNT = slot * SIZE; \ |
| 581 | kfree(state->FIELD); \ |
| 582 | state->FIELD = new_##FIELD; \ |
| 583 | return 0; \ |
| 584 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 585 | /* realloc_reference_state() */ |
| 586 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 587 | /* realloc_stack_state() */ |
| 588 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 589 | #undef REALLOC_STATE_FN |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 590 | |
| 591 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 592 | * make it consume minimal amount of memory. check_stack_write() access from |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 593 | * the program calls into realloc_func_state() to grow the stack size. |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 594 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 595 | * which realloc_stack_state() copies over. It points to previous |
| 596 | * bpf_verifier_state which is never reallocated. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 597 | */ |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 598 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
| 599 | int refs_size, bool copy_old) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 600 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 601 | int err = realloc_reference_state(state, refs_size, copy_old); |
| 602 | if (err) |
| 603 | return err; |
| 604 | return realloc_stack_state(state, stack_size, copy_old); |
| 605 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 606 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 607 | /* Acquire a pointer id from the env and update the state->refs to include |
| 608 | * this new pointer reference. |
| 609 | * On success, returns a valid pointer id to associate with the register |
| 610 | * On failure, returns a negative errno. |
| 611 | */ |
| 612 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 613 | { |
| 614 | struct bpf_func_state *state = cur_func(env); |
| 615 | int new_ofs = state->acquired_refs; |
| 616 | int id, err; |
| 617 | |
| 618 | err = realloc_reference_state(state, state->acquired_refs + 1, true); |
| 619 | if (err) |
| 620 | return err; |
| 621 | id = ++env->id_gen; |
| 622 | state->refs[new_ofs].id = id; |
| 623 | state->refs[new_ofs].insn_idx = insn_idx; |
| 624 | |
| 625 | return id; |
| 626 | } |
| 627 | |
| 628 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 629 | 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] | 630 | { |
| 631 | int i, last_idx; |
| 632 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 633 | last_idx = state->acquired_refs - 1; |
| 634 | for (i = 0; i < state->acquired_refs; i++) { |
| 635 | if (state->refs[i].id == ptr_id) { |
| 636 | if (last_idx && i != last_idx) |
| 637 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 638 | sizeof(*state->refs)); |
| 639 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 640 | state->acquired_refs--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 641 | return 0; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 642 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 643 | } |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 644 | return -EINVAL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | static int transfer_reference_state(struct bpf_func_state *dst, |
| 648 | struct bpf_func_state *src) |
| 649 | { |
| 650 | int err = realloc_reference_state(dst, src->acquired_refs, false); |
| 651 | if (err) |
| 652 | return err; |
| 653 | err = copy_reference_state(dst, src); |
| 654 | if (err) |
| 655 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 656 | return 0; |
| 657 | } |
| 658 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 659 | static void free_func_state(struct bpf_func_state *state) |
| 660 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 661 | if (!state) |
| 662 | return; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 663 | kfree(state->refs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 664 | kfree(state->stack); |
| 665 | kfree(state); |
| 666 | } |
| 667 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 668 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 669 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 670 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 671 | int i; |
| 672 | |
| 673 | for (i = 0; i <= state->curframe; i++) { |
| 674 | free_func_state(state->frame[i]); |
| 675 | state->frame[i] = NULL; |
| 676 | } |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 677 | if (free_self) |
| 678 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /* copy verifier state from src to dst growing dst stack space |
| 682 | * when necessary to accommodate larger src stack |
| 683 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 684 | static int copy_func_state(struct bpf_func_state *dst, |
| 685 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 686 | { |
| 687 | int err; |
| 688 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 689 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
| 690 | false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 691 | if (err) |
| 692 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 693 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 694 | err = copy_reference_state(dst, src); |
| 695 | if (err) |
| 696 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 697 | return copy_stack_state(dst, src); |
| 698 | } |
| 699 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 700 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 701 | const struct bpf_verifier_state *src) |
| 702 | { |
| 703 | struct bpf_func_state *dst; |
| 704 | int i, err; |
| 705 | |
| 706 | /* if dst has more stack frames then src frame, free them */ |
| 707 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 708 | free_func_state(dst_state->frame[i]); |
| 709 | dst_state->frame[i] = NULL; |
| 710 | } |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 711 | dst_state->speculative = src->speculative; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 712 | dst_state->curframe = src->curframe; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 713 | dst_state->active_spin_lock = src->active_spin_lock; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 714 | for (i = 0; i <= src->curframe; i++) { |
| 715 | dst = dst_state->frame[i]; |
| 716 | if (!dst) { |
| 717 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 718 | if (!dst) |
| 719 | return -ENOMEM; |
| 720 | dst_state->frame[i] = dst; |
| 721 | } |
| 722 | err = copy_func_state(dst, src->frame[i]); |
| 723 | if (err) |
| 724 | return err; |
| 725 | } |
| 726 | return 0; |
| 727 | } |
| 728 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 729 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
| 730 | int *insn_idx) |
| 731 | { |
| 732 | struct bpf_verifier_state *cur = env->cur_state; |
| 733 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 734 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 735 | |
| 736 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 737 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 738 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 739 | if (cur) { |
| 740 | err = copy_verifier_state(cur, &head->st); |
| 741 | if (err) |
| 742 | return err; |
| 743 | } |
| 744 | if (insn_idx) |
| 745 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 746 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 747 | *prev_insn_idx = head->prev_insn_idx; |
| 748 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 749 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 750 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 751 | env->head = elem; |
| 752 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 753 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 756 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 757 | int insn_idx, int prev_insn_idx, |
| 758 | bool speculative) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 759 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 760 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 761 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 762 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 763 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 764 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 765 | if (!elem) |
| 766 | goto err; |
| 767 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 768 | elem->insn_idx = insn_idx; |
| 769 | elem->prev_insn_idx = prev_insn_idx; |
| 770 | elem->next = env->head; |
| 771 | env->head = elem; |
| 772 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 773 | err = copy_verifier_state(&elem->st, cur); |
| 774 | if (err) |
| 775 | goto err; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 776 | elem->st.speculative |= speculative; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 777 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 778 | verbose(env, "BPF program is too complex\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 779 | goto err; |
| 780 | } |
| 781 | return &elem->st; |
| 782 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 783 | free_verifier_state(env->cur_state, true); |
| 784 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 785 | /* pop all elements and return */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 786 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 787 | return NULL; |
| 788 | } |
| 789 | |
| 790 | #define CALLER_SAVED_REGS 6 |
| 791 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 792 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 793 | }; |
| 794 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 795 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
| 796 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 797 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 798 | * known to have the value @imm. |
| 799 | */ |
| 800 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 801 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 802 | /* Clear id, off, and union(map_ptr, range) */ |
| 803 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 804 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 805 | reg->var_off = tnum_const(imm); |
| 806 | reg->smin_value = (s64)imm; |
| 807 | reg->smax_value = (s64)imm; |
| 808 | reg->umin_value = imm; |
| 809 | reg->umax_value = imm; |
| 810 | } |
| 811 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 812 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 813 | * used only on registers holding a pointer type. |
| 814 | */ |
| 815 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 816 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 817 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 820 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 821 | { |
| 822 | __mark_reg_known(reg, 0); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 823 | reg->type = SCALAR_VALUE; |
| 824 | } |
| 825 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 826 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 827 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 828 | { |
| 829 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 830 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 831 | /* Something bad happened, let's kill all regs */ |
| 832 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 833 | __mark_reg_not_init(regs + regno); |
| 834 | return; |
| 835 | } |
| 836 | __mark_reg_known_zero(regs + regno); |
| 837 | } |
| 838 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 839 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 840 | { |
| 841 | return type_is_pkt_pointer(reg->type); |
| 842 | } |
| 843 | |
| 844 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 845 | { |
| 846 | return reg_is_pkt_pointer(reg) || |
| 847 | reg->type == PTR_TO_PACKET_END; |
| 848 | } |
| 849 | |
| 850 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 851 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 852 | enum bpf_reg_type which) |
| 853 | { |
| 854 | /* The register can already have a range from prior markings. |
| 855 | * This is fine as long as it hasn't been advanced from its |
| 856 | * origin. |
| 857 | */ |
| 858 | return reg->type == which && |
| 859 | reg->id == 0 && |
| 860 | reg->off == 0 && |
| 861 | tnum_equals_const(reg->var_off, 0); |
| 862 | } |
| 863 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 864 | /* Attempts to improve min/max values based on var_off information */ |
| 865 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 866 | { |
| 867 | /* min signed is max(sign bit) | min(other bits) */ |
| 868 | reg->smin_value = max_t(s64, reg->smin_value, |
| 869 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 870 | /* max signed is min(sign bit) | max(other bits) */ |
| 871 | reg->smax_value = min_t(s64, reg->smax_value, |
| 872 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 873 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 874 | reg->umax_value = min(reg->umax_value, |
| 875 | reg->var_off.value | reg->var_off.mask); |
| 876 | } |
| 877 | |
| 878 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
| 879 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 880 | { |
| 881 | /* Learn sign from signed bounds. |
| 882 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 883 | * are the same, so combine. This works even in the negative case, e.g. |
| 884 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 885 | */ |
| 886 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 887 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 888 | reg->umin_value); |
| 889 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 890 | reg->umax_value); |
| 891 | return; |
| 892 | } |
| 893 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 894 | * boundary, so we must be careful. |
| 895 | */ |
| 896 | if ((s64)reg->umax_value >= 0) { |
| 897 | /* Positive. We can't learn anything from the smin, but smax |
| 898 | * is positive, hence safe. |
| 899 | */ |
| 900 | reg->smin_value = reg->umin_value; |
| 901 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 902 | reg->umax_value); |
| 903 | } else if ((s64)reg->umin_value < 0) { |
| 904 | /* Negative. We can't learn anything from the smax, but smin |
| 905 | * is negative, hence safe. |
| 906 | */ |
| 907 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 908 | reg->umin_value); |
| 909 | reg->smax_value = reg->umax_value; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 914 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 915 | { |
| 916 | reg->var_off = tnum_intersect(reg->var_off, |
| 917 | tnum_range(reg->umin_value, |
| 918 | reg->umax_value)); |
| 919 | } |
| 920 | |
| 921 | /* Reset the min/max bounds of a register */ |
| 922 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 923 | { |
| 924 | reg->smin_value = S64_MIN; |
| 925 | reg->smax_value = S64_MAX; |
| 926 | reg->umin_value = 0; |
| 927 | reg->umax_value = U64_MAX; |
| 928 | } |
| 929 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 930 | /* Mark a register as having a completely unknown (scalar) value. */ |
| 931 | static void __mark_reg_unknown(struct bpf_reg_state *reg) |
| 932 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 933 | /* |
| 934 | * Clear type, id, off, and union(map_ptr, range) and |
| 935 | * padding between 'type' and union |
| 936 | */ |
| 937 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 938 | reg->type = SCALAR_VALUE; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 939 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 940 | reg->frameno = 0; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 941 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 942 | } |
| 943 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 944 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 945 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 946 | { |
| 947 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 948 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 949 | /* Something bad happened, let's kill all regs except FP */ |
| 950 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 951 | __mark_reg_not_init(regs + regno); |
| 952 | return; |
| 953 | } |
| 954 | __mark_reg_unknown(regs + regno); |
| 955 | } |
| 956 | |
| 957 | static void __mark_reg_not_init(struct bpf_reg_state *reg) |
| 958 | { |
| 959 | __mark_reg_unknown(reg); |
| 960 | reg->type = NOT_INIT; |
| 961 | } |
| 962 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 963 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 964 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 965 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 966 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 967 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 968 | /* Something bad happened, let's kill all regs except FP */ |
| 969 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 970 | __mark_reg_not_init(regs + regno); |
| 971 | return; |
| 972 | } |
| 973 | __mark_reg_not_init(regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 974 | } |
| 975 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 976 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 977 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 978 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 979 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 980 | int i; |
| 981 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 982 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 983 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 984 | regs[i].live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 985 | regs[i].parent = NULL; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 986 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 987 | |
| 988 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 989 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 990 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 991 | regs[BPF_REG_FP].frameno = state->frameno; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 992 | |
| 993 | /* 1st arg to a function */ |
| 994 | regs[BPF_REG_1].type = PTR_TO_CTX; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 995 | mark_reg_known_zero(env, regs, BPF_REG_1); |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 996 | } |
| 997 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 998 | #define BPF_MAIN_FUNC (-1) |
| 999 | static void init_func_state(struct bpf_verifier_env *env, |
| 1000 | struct bpf_func_state *state, |
| 1001 | int callsite, int frameno, int subprogno) |
| 1002 | { |
| 1003 | state->callsite = callsite; |
| 1004 | state->frameno = frameno; |
| 1005 | state->subprogno = subprogno; |
| 1006 | init_reg_state(env, state); |
| 1007 | } |
| 1008 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1009 | enum reg_arg_type { |
| 1010 | SRC_OP, /* register is used as source operand */ |
| 1011 | DST_OP, /* register is used as destination operand */ |
| 1012 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 1013 | }; |
| 1014 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1015 | static int cmp_subprogs(const void *a, const void *b) |
| 1016 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1017 | return ((struct bpf_subprog_info *)a)->start - |
| 1018 | ((struct bpf_subprog_info *)b)->start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 1022 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1023 | struct bpf_subprog_info *p; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1024 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1025 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 1026 | sizeof(env->subprog_info[0]), cmp_subprogs); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1027 | if (!p) |
| 1028 | return -ENOENT; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1029 | return p - env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1030 | |
| 1031 | } |
| 1032 | |
| 1033 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 1034 | { |
| 1035 | int insn_cnt = env->prog->len; |
| 1036 | int ret; |
| 1037 | |
| 1038 | if (off >= insn_cnt || off < 0) { |
| 1039 | verbose(env, "call to invalid destination\n"); |
| 1040 | return -EINVAL; |
| 1041 | } |
| 1042 | ret = find_subprog(env, off); |
| 1043 | if (ret >= 0) |
| 1044 | return 0; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1045 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1046 | verbose(env, "too many subprograms\n"); |
| 1047 | return -E2BIG; |
| 1048 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1049 | env->subprog_info[env->subprog_cnt++].start = off; |
| 1050 | sort(env->subprog_info, env->subprog_cnt, |
| 1051 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | static int check_subprogs(struct bpf_verifier_env *env) |
| 1056 | { |
| 1057 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1058 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1059 | struct bpf_insn *insn = env->prog->insnsi; |
| 1060 | int insn_cnt = env->prog->len; |
| 1061 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 1062 | /* Add entry function. */ |
| 1063 | ret = add_subprog(env, 0); |
| 1064 | if (ret < 0) |
| 1065 | return ret; |
| 1066 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1067 | /* determine subprog starts. The end is one before the next starts */ |
| 1068 | for (i = 0; i < insn_cnt; i++) { |
| 1069 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1070 | continue; |
| 1071 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1072 | continue; |
| 1073 | if (!env->allow_ptr_leaks) { |
| 1074 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); |
| 1075 | return -EPERM; |
| 1076 | } |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1077 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 1078 | if (ret < 0) |
| 1079 | return ret; |
| 1080 | } |
| 1081 | |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1082 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 1083 | * logic. 'subprog_cnt' should not be increased. |
| 1084 | */ |
| 1085 | subprog[env->subprog_cnt].start = insn_cnt; |
| 1086 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1087 | if (env->log.level & BPF_LOG_LEVEL2) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1088 | for (i = 0; i < env->subprog_cnt; i++) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1089 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1090 | |
| 1091 | /* now check that all jumps are within the same subprog */ |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1092 | subprog_start = subprog[cur_subprog].start; |
| 1093 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1094 | for (i = 0; i < insn_cnt; i++) { |
| 1095 | u8 code = insn[i].code; |
| 1096 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 1097 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1098 | goto next; |
| 1099 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 1100 | goto next; |
| 1101 | off = i + insn[i].off + 1; |
| 1102 | if (off < subprog_start || off >= subprog_end) { |
| 1103 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 1104 | return -EINVAL; |
| 1105 | } |
| 1106 | next: |
| 1107 | if (i == subprog_end - 1) { |
| 1108 | /* to avoid fall-through from one subprog into another |
| 1109 | * the last insn of the subprog should be either exit |
| 1110 | * or unconditional jump back |
| 1111 | */ |
| 1112 | if (code != (BPF_JMP | BPF_EXIT) && |
| 1113 | code != (BPF_JMP | BPF_JA)) { |
| 1114 | verbose(env, "last insn is not an exit or jmp\n"); |
| 1115 | return -EINVAL; |
| 1116 | } |
| 1117 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1118 | cur_subprog++; |
| 1119 | if (cur_subprog < env->subprog_cnt) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1120 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1126 | /* Parentage chain of this register (or stack slot) should take care of all |
| 1127 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 1128 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1129 | static int mark_reg_read(struct bpf_verifier_env *env, |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1130 | const struct bpf_reg_state *state, |
| 1131 | struct bpf_reg_state *parent) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1132 | { |
| 1133 | bool writes = parent == state->parent; /* Observe write marks */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1134 | int cnt = 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1135 | |
| 1136 | while (parent) { |
| 1137 | /* if read wasn't screened by an earlier write ... */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1138 | if (writes && state->live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1139 | break; |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 1140 | if (parent->live & REG_LIVE_DONE) { |
| 1141 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", |
| 1142 | reg_type_str[parent->type], |
| 1143 | parent->var_off.value, parent->off); |
| 1144 | return -EFAULT; |
| 1145 | } |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 1146 | if (parent->live & REG_LIVE_READ) |
| 1147 | /* The parentage chain never changes and |
| 1148 | * this parent was already marked as LIVE_READ. |
| 1149 | * There is no need to keep walking the chain again and |
| 1150 | * keep re-marking all parents as LIVE_READ. |
| 1151 | * This case happens when the same register is read |
| 1152 | * multiple times without writes into it in-between. |
| 1153 | */ |
| 1154 | break; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1155 | /* ... then we depend on parent's value */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1156 | parent->live |= REG_LIVE_READ; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1157 | state = parent; |
| 1158 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1159 | writes = true; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1160 | cnt++; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1161 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1162 | |
| 1163 | if (env->longest_mark_read_walk < cnt) |
| 1164 | env->longest_mark_read_walk = cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1165 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1169 | enum reg_arg_type t) |
| 1170 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1171 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1172 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1173 | struct bpf_reg_state *reg, *regs = state->regs; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1174 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1175 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1176 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1177 | return -EINVAL; |
| 1178 | } |
| 1179 | |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1180 | reg = ®s[regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1181 | if (t == SRC_OP) { |
| 1182 | /* check whether register used as source operand can be read */ |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1183 | if (reg->type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1184 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1185 | return -EACCES; |
| 1186 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1187 | /* 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] | 1188 | if (regno == BPF_REG_FP) |
| 1189 | return 0; |
| 1190 | |
| 1191 | return mark_reg_read(env, reg, reg->parent); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1192 | } else { |
| 1193 | /* check whether register used as dest operand can be written to */ |
| 1194 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1195 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1196 | return -EACCES; |
| 1197 | } |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1198 | reg->live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1199 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1200 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1201 | } |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1205 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 1206 | { |
| 1207 | switch (type) { |
| 1208 | case PTR_TO_MAP_VALUE: |
| 1209 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 1210 | case PTR_TO_STACK: |
| 1211 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1212 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1213 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1214 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1215 | case PTR_TO_FLOW_KEYS: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1216 | case CONST_PTR_TO_MAP: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1217 | case PTR_TO_SOCKET: |
| 1218 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1219 | case PTR_TO_SOCK_COMMON: |
| 1220 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 1221 | case PTR_TO_TCP_SOCK: |
| 1222 | case PTR_TO_TCP_SOCK_OR_NULL: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1223 | return true; |
| 1224 | default: |
| 1225 | return false; |
| 1226 | } |
| 1227 | } |
| 1228 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1229 | /* Does this register contain a constant zero? */ |
| 1230 | static bool register_is_null(struct bpf_reg_state *reg) |
| 1231 | { |
| 1232 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 1233 | } |
| 1234 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1235 | /* check_stack_read/write functions track spill/fill of registers, |
| 1236 | * stack boundary and alignment are checked in check_mem_access() |
| 1237 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1238 | static int check_stack_write(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1239 | struct bpf_func_state *state, /* func where register points to */ |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1240 | int off, int size, int value_regno, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1241 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1242 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1243 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1244 | enum bpf_reg_type type; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1245 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1246 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 1247 | state->acquired_refs, true); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1248 | if (err) |
| 1249 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1250 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 1251 | * so it's aligned access and [off, off + size) are within stack limits |
| 1252 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1253 | if (!env->allow_ptr_leaks && |
| 1254 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 1255 | size != BPF_REG_SIZE) { |
| 1256 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 1257 | return -EACCES; |
| 1258 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1259 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1260 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1261 | if (value_regno >= 0 && |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1262 | is_spillable_regtype((type = cur->regs[value_regno].type))) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1263 | |
| 1264 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1265 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1266 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1267 | return -EACCES; |
| 1268 | } |
| 1269 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1270 | if (state != cur && type == PTR_TO_STACK) { |
| 1271 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 1272 | return -EINVAL; |
| 1273 | } |
| 1274 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1275 | /* save register state */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1276 | state->stack[spi].spilled_ptr = cur->regs[value_regno]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1277 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1278 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1279 | for (i = 0; i < BPF_REG_SIZE; i++) { |
| 1280 | if (state->stack[spi].slot_type[i] == STACK_MISC && |
| 1281 | !env->allow_ptr_leaks) { |
| 1282 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
| 1283 | int soff = (-spi - 1) * BPF_REG_SIZE; |
| 1284 | |
| 1285 | /* detected reuse of integer stack slot with a pointer |
| 1286 | * which means either llvm is reusing stack slot or |
| 1287 | * an attacker is trying to exploit CVE-2018-3639 |
| 1288 | * (speculative store bypass) |
| 1289 | * Have to sanitize that slot with preemptive |
| 1290 | * store of zero. |
| 1291 | */ |
| 1292 | if (*poff && *poff != soff) { |
| 1293 | /* disallow programs where single insn stores |
| 1294 | * into two different stack slots, since verifier |
| 1295 | * cannot sanitize them |
| 1296 | */ |
| 1297 | verbose(env, |
| 1298 | "insn %d cannot access two stack slots fp%d and fp%d", |
| 1299 | insn_idx, *poff, soff); |
| 1300 | return -EINVAL; |
| 1301 | } |
| 1302 | *poff = soff; |
| 1303 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1304 | state->stack[spi].slot_type[i] = STACK_SPILL; |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1305 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1306 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1307 | u8 type = STACK_MISC; |
| 1308 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1309 | /* regular write of data into stack destroys any spilled ptr */ |
| 1310 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 1311 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
| 1312 | if (state->stack[spi].slot_type[0] == STACK_SPILL) |
| 1313 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 1314 | state->stack[spi].slot_type[i] = STACK_MISC; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1315 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1316 | /* only mark the slot as written if all 8 bytes were written |
| 1317 | * otherwise read propagation may incorrectly stop too soon |
| 1318 | * when stack slots are partially written. |
| 1319 | * This heuristic means that read propagation will be |
| 1320 | * conservative, since it will add reg_live_read marks |
| 1321 | * to stack slots all the way to first state when programs |
| 1322 | * writes+reads less than 8 bytes |
| 1323 | */ |
| 1324 | if (size == BPF_REG_SIZE) |
| 1325 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 1326 | |
| 1327 | /* when we zero initialize stack slots mark them as such */ |
| 1328 | if (value_regno >= 0 && |
| 1329 | register_is_null(&cur->regs[value_regno])) |
| 1330 | type = STACK_ZERO; |
| 1331 | |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 1332 | /* Mark slots affected by this stack write. */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1333 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1334 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1335 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1336 | } |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1340 | static int check_stack_read(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1341 | struct bpf_func_state *reg_state /* func where register points to */, |
| 1342 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1343 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1344 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1345 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1346 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
| 1347 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1348 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1349 | if (reg_state->allocated_stack <= slot) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1350 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 1351 | off, size); |
| 1352 | return -EACCES; |
| 1353 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1354 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1355 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1356 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1357 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1358 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1359 | return -EACCES; |
| 1360 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1361 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1362 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1363 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1364 | return -EACCES; |
| 1365 | } |
| 1366 | } |
| 1367 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1368 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1369 | /* restore register state from stack */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1370 | state->regs[value_regno] = reg_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 1371 | /* mark reg as written since spilled pointer state likely |
| 1372 | * has its liveness marks cleared by is_state_visited() |
| 1373 | * which resets stack/reg liveness for state transitions |
| 1374 | */ |
| 1375 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1376 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1377 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
| 1378 | reg_state->stack[spi].spilled_ptr.parent); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1379 | return 0; |
| 1380 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1381 | int zeros = 0; |
| 1382 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1383 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1384 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 1385 | continue; |
| 1386 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 1387 | zeros++; |
| 1388 | continue; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1389 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1390 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 1391 | off, i, size); |
| 1392 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1393 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1394 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
| 1395 | reg_state->stack[spi].spilled_ptr.parent); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1396 | if (value_regno >= 0) { |
| 1397 | if (zeros == size) { |
| 1398 | /* any size read into register is zero extended, |
| 1399 | * so the whole register == const_zero |
| 1400 | */ |
| 1401 | __mark_reg_const_zero(&state->regs[value_regno]); |
| 1402 | } else { |
| 1403 | /* have read misc data from the stack */ |
| 1404 | mark_reg_unknown(env, state->regs, value_regno); |
| 1405 | } |
| 1406 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 1407 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1408 | return 0; |
| 1409 | } |
| 1410 | } |
| 1411 | |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 1412 | static int check_stack_access(struct bpf_verifier_env *env, |
| 1413 | const struct bpf_reg_state *reg, |
| 1414 | int off, int size) |
| 1415 | { |
| 1416 | /* Stack accesses must be at a fixed offset, so that we |
| 1417 | * can determine what type of data were returned. See |
| 1418 | * check_stack_read(). |
| 1419 | */ |
| 1420 | if (!tnum_is_const(reg->var_off)) { |
| 1421 | char tn_buf[48]; |
| 1422 | |
| 1423 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Andrey Ignatov | 1fbd20f | 2019-04-03 23:22:43 -0700 | [diff] [blame] | 1424 | 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] | 1425 | tn_buf, off, size); |
| 1426 | return -EACCES; |
| 1427 | } |
| 1428 | |
| 1429 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 1430 | verbose(env, "invalid stack off=%d size=%d\n", off, size); |
| 1431 | return -EACCES; |
| 1432 | } |
| 1433 | |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 1437 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
| 1438 | int off, int size, enum bpf_access_type type) |
| 1439 | { |
| 1440 | struct bpf_reg_state *regs = cur_regs(env); |
| 1441 | struct bpf_map *map = regs[regno].map_ptr; |
| 1442 | u32 cap = bpf_map_flags_to_cap(map); |
| 1443 | |
| 1444 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { |
| 1445 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", |
| 1446 | map->value_size, off, size); |
| 1447 | return -EACCES; |
| 1448 | } |
| 1449 | |
| 1450 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { |
| 1451 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", |
| 1452 | map->value_size, off, size); |
| 1453 | return -EACCES; |
| 1454 | } |
| 1455 | |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1459 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1460 | static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1461 | int size, bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1462 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1463 | struct bpf_reg_state *regs = cur_regs(env); |
| 1464 | struct bpf_map *map = regs[regno].map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1465 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1466 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1467 | off + size > map->value_size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1468 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1469 | map->value_size, off, size); |
| 1470 | return -EACCES; |
| 1471 | } |
| 1472 | return 0; |
| 1473 | } |
| 1474 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1475 | /* check read/write into a map element with possible variable offset */ |
| 1476 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1477 | int off, int size, bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1478 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1479 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1480 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1481 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 1482 | int err; |
| 1483 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1484 | /* We may have adjusted the register to this map value, so we |
| 1485 | * need to try adding each of min_value and max_value to off |
| 1486 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1487 | */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1488 | if (env->log.level & BPF_LOG_LEVEL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1489 | print_verifier_state(env, state); |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 1490 | |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1491 | /* The minimum value is only important with signed |
| 1492 | * comparisons where we can't assume the floor of a |
| 1493 | * value is 0. If we are using signed variables for our |
| 1494 | * index'es we need to make sure that whatever we use |
| 1495 | * will have a set floor within our range. |
| 1496 | */ |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 1497 | if (reg->smin_value < 0 && |
| 1498 | (reg->smin_value == S64_MIN || |
| 1499 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || |
| 1500 | reg->smin_value + off < 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1501 | 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] | 1502 | regno); |
| 1503 | return -EACCES; |
| 1504 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1505 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
| 1506 | zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1507 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1508 | verbose(env, "R%d min value is outside of the array range\n", |
| 1509 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1510 | return err; |
| 1511 | } |
| 1512 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1513 | /* If we haven't set a max value then we need to bail since we can't be |
| 1514 | * sure we won't do bad things. |
| 1515 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1516 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1517 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1518 | verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n", |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1519 | regno); |
| 1520 | return -EACCES; |
| 1521 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1522 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
| 1523 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1524 | if (err) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1525 | verbose(env, "R%d max value is outside of the array range\n", |
| 1526 | regno); |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 1527 | |
| 1528 | if (map_value_has_spin_lock(reg->map_ptr)) { |
| 1529 | u32 lock = reg->map_ptr->spin_lock_off; |
| 1530 | |
| 1531 | /* if any part of struct bpf_spin_lock can be touched by |
| 1532 | * load/store reject this program. |
| 1533 | * To check that [x1, x2) overlaps with [y1, y2) |
| 1534 | * it is sufficient to check x1 < y2 && y1 < x2. |
| 1535 | */ |
| 1536 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && |
| 1537 | lock < reg->umax_value + off + size) { |
| 1538 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); |
| 1539 | return -EACCES; |
| 1540 | } |
| 1541 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1542 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1543 | } |
| 1544 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1545 | #define MAX_PACKET_OFF 0xffff |
| 1546 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1547 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1548 | const struct bpf_call_arg_meta *meta, |
| 1549 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1550 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1551 | switch (env->prog->type) { |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1552 | /* Program types only with direct read access go here! */ |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1553 | case BPF_PROG_TYPE_LWT_IN: |
| 1554 | case BPF_PROG_TYPE_LWT_OUT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 1555 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 1556 | case BPF_PROG_TYPE_SK_REUSEPORT: |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1557 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Daniel Borkmann | d5563d3 | 2018-10-24 22:05:46 +0200 | [diff] [blame] | 1558 | case BPF_PROG_TYPE_CGROUP_SKB: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1559 | if (t == BPF_WRITE) |
| 1560 | return false; |
Alexander Alemayhu | 7e57fbb | 2017-02-14 00:02:35 +0100 | [diff] [blame] | 1561 | /* fallthrough */ |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1562 | |
| 1563 | /* Program types with direct read + write access go here! */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1564 | case BPF_PROG_TYPE_SCHED_CLS: |
| 1565 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1566 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1567 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 1568 | case BPF_PROG_TYPE_SK_SKB: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1569 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1570 | if (meta) |
| 1571 | return meta->pkt_access; |
| 1572 | |
| 1573 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1574 | return true; |
| 1575 | default: |
| 1576 | return false; |
| 1577 | } |
| 1578 | } |
| 1579 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1580 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1581 | int off, int size, bool zero_size_allowed) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1582 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1583 | struct bpf_reg_state *regs = cur_regs(env); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1584 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1585 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1586 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1587 | (u64)off + size > reg->range) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1588 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
Alexei Starovoitov | d91b28e | 2016-05-19 18:17:13 -0700 | [diff] [blame] | 1589 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1590 | return -EACCES; |
| 1591 | } |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1595 | 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] | 1596 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1597 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1598 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1599 | struct bpf_reg_state *reg = ®s[regno]; |
| 1600 | int err; |
| 1601 | |
| 1602 | /* We may have added a variable offset to the packet pointer; but any |
| 1603 | * reg->range we have comes after that. We are only checking the fixed |
| 1604 | * offset. |
| 1605 | */ |
| 1606 | |
| 1607 | /* We don't allow negative numbers, because we aren't tracking enough |
| 1608 | * detail to prove they're safe. |
| 1609 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1610 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1611 | 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] | 1612 | regno); |
| 1613 | return -EACCES; |
| 1614 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1615 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1616 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1617 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1618 | return err; |
| 1619 | } |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 1620 | |
| 1621 | /* __check_packet_access has made sure "off + size - 1" is within u16. |
| 1622 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 1623 | * otherwise find_good_pkt_pointers would have refused to set range info |
| 1624 | * that __check_packet_access would have rejected this pkt access. |
| 1625 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 1626 | */ |
| 1627 | env->prog->aux->max_pkt_offset = |
| 1628 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 1629 | off + reg->umax_value + size - 1); |
| 1630 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1631 | return err; |
| 1632 | } |
| 1633 | |
| 1634 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1635 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 1636 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1637 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1638 | struct bpf_insn_access_aux info = { |
| 1639 | .reg_type = *reg_type, |
| 1640 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1641 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1642 | if (env->ops->is_valid_access && |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1643 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1644 | /* A non zero info.ctx_field_size indicates that this field is a |
| 1645 | * candidate for later verifier transformation to load the whole |
| 1646 | * field and then apply a mask when accessed with a narrower |
| 1647 | * access than actual ctx access size. A zero info.ctx_field_size |
| 1648 | * will only allow for whole field access and rejects any other |
| 1649 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1650 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 1651 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1652 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1653 | 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] | 1654 | /* remember the offset of last byte accessed in ctx */ |
| 1655 | if (env->prog->aux->max_ctx_offset < off + size) |
| 1656 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1657 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 1658 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1659 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1660 | 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] | 1661 | return -EACCES; |
| 1662 | } |
| 1663 | |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1664 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 1665 | int size) |
| 1666 | { |
| 1667 | if (size < 0 || off < 0 || |
| 1668 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 1669 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 1670 | off, size); |
| 1671 | return -EACCES; |
| 1672 | } |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 1676 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
| 1677 | u32 regno, int off, int size, |
| 1678 | enum bpf_access_type t) |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1679 | { |
| 1680 | struct bpf_reg_state *regs = cur_regs(env); |
| 1681 | struct bpf_reg_state *reg = ®s[regno]; |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 1682 | struct bpf_insn_access_aux info = {}; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1683 | bool valid; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1684 | |
| 1685 | if (reg->smin_value < 0) { |
| 1686 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 1687 | regno); |
| 1688 | return -EACCES; |
| 1689 | } |
| 1690 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1691 | switch (reg->type) { |
| 1692 | case PTR_TO_SOCK_COMMON: |
| 1693 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); |
| 1694 | break; |
| 1695 | case PTR_TO_SOCKET: |
| 1696 | valid = bpf_sock_is_valid_access(off, size, t, &info); |
| 1697 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 1698 | case PTR_TO_TCP_SOCK: |
| 1699 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); |
| 1700 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1701 | default: |
| 1702 | valid = false; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1703 | } |
| 1704 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 1705 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1706 | if (valid) { |
| 1707 | env->insn_aux_data[insn_idx].ctx_field_size = |
| 1708 | info.ctx_field_size; |
| 1709 | return 0; |
| 1710 | } |
| 1711 | |
| 1712 | verbose(env, "R%d invalid %s access off=%d size=%d\n", |
| 1713 | regno, reg_type_str[reg->type], off, size); |
| 1714 | |
| 1715 | return -EACCES; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1718 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 1719 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1720 | { |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1721 | if (allow_ptr_leaks) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1722 | return false; |
| 1723 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1724 | return reg->type != SCALAR_VALUE; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1727 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
| 1728 | { |
| 1729 | return cur_regs(env) + regno; |
| 1730 | } |
| 1731 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1732 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 1733 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1734 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1735 | } |
| 1736 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1737 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 1738 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1739 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1740 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1741 | return reg->type == PTR_TO_CTX; |
| 1742 | } |
| 1743 | |
| 1744 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) |
| 1745 | { |
| 1746 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 1747 | |
| 1748 | return type_is_sk_pointer(reg->type); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1749 | } |
| 1750 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1751 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 1752 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1753 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1754 | |
| 1755 | return type_is_pkt_pointer(reg->type); |
| 1756 | } |
| 1757 | |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 1758 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 1759 | { |
| 1760 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 1761 | |
| 1762 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 1763 | return reg->type == PTR_TO_FLOW_KEYS; |
| 1764 | } |
| 1765 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1766 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 1767 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1768 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1769 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1770 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1771 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1772 | |
| 1773 | /* Byte size accesses are always allowed. */ |
| 1774 | if (!strict || size == 1) |
| 1775 | return 0; |
| 1776 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1777 | /* For platforms that do not have a Kconfig enabling |
| 1778 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 1779 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 1780 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 1781 | * to this code only in strict mode where we want to emulate |
| 1782 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 1783 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1784 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1785 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1786 | |
| 1787 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 1788 | if (!tnum_is_aligned(reg_off, size)) { |
| 1789 | char tn_buf[48]; |
| 1790 | |
| 1791 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1792 | verbose(env, |
| 1793 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1794 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1795 | return -EACCES; |
| 1796 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1797 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1798 | return 0; |
| 1799 | } |
| 1800 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1801 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 1802 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1803 | const char *pointer_desc, |
| 1804 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1805 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1806 | struct tnum reg_off; |
| 1807 | |
| 1808 | /* Byte size accesses are always allowed. */ |
| 1809 | if (!strict || size == 1) |
| 1810 | return 0; |
| 1811 | |
| 1812 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 1813 | if (!tnum_is_aligned(reg_off, size)) { |
| 1814 | char tn_buf[48]; |
| 1815 | |
| 1816 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1817 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1818 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1819 | return -EACCES; |
| 1820 | } |
| 1821 | |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1825 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1826 | const struct bpf_reg_state *reg, int off, |
| 1827 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1828 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1829 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1830 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1831 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1832 | switch (reg->type) { |
| 1833 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1834 | case PTR_TO_PACKET_META: |
| 1835 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 1836 | * right in front, treat it the very same way. |
| 1837 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1838 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1839 | case PTR_TO_FLOW_KEYS: |
| 1840 | pointer_desc = "flow keys "; |
| 1841 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1842 | case PTR_TO_MAP_VALUE: |
| 1843 | pointer_desc = "value "; |
| 1844 | break; |
| 1845 | case PTR_TO_CTX: |
| 1846 | pointer_desc = "context "; |
| 1847 | break; |
| 1848 | case PTR_TO_STACK: |
| 1849 | pointer_desc = "stack "; |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 1850 | /* The stack spill tracking logic in check_stack_write() |
| 1851 | * and check_stack_read() relies on stack accesses being |
| 1852 | * aligned. |
| 1853 | */ |
| 1854 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1855 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1856 | case PTR_TO_SOCKET: |
| 1857 | pointer_desc = "sock "; |
| 1858 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1859 | case PTR_TO_SOCK_COMMON: |
| 1860 | pointer_desc = "sock_common "; |
| 1861 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 1862 | case PTR_TO_TCP_SOCK: |
| 1863 | pointer_desc = "tcp_sock "; |
| 1864 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1865 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1866 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1867 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1868 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 1869 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1870 | } |
| 1871 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1872 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 1873 | const struct bpf_func_state *func, |
| 1874 | int off) |
| 1875 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1876 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1877 | |
| 1878 | if (stack >= -off) |
| 1879 | return 0; |
| 1880 | |
| 1881 | /* update known max for given subprogram */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1882 | env->subprog_info[func->subprogno].stack_depth = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1883 | return 0; |
| 1884 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1885 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1886 | /* starting from main bpf function walk all instructions of the function |
| 1887 | * and recursively walk all callees that given function can call. |
| 1888 | * Ignore jump and exit insns. |
| 1889 | * Since recursion is prevented by check_cfg() this algorithm |
| 1890 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 1891 | */ |
| 1892 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 1893 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1894 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 1895 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1896 | struct bpf_insn *insn = env->prog->insnsi; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1897 | int ret_insn[MAX_CALL_FRAMES]; |
| 1898 | int ret_prog[MAX_CALL_FRAMES]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1899 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1900 | process_func: |
| 1901 | /* round up to 32-bytes, since this is granularity |
| 1902 | * of interpreter stack size |
| 1903 | */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1904 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1905 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1906 | 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] | 1907 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1908 | return -EACCES; |
| 1909 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1910 | continue_func: |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1911 | subprog_end = subprog[idx + 1].start; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1912 | for (; i < subprog_end; i++) { |
| 1913 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1914 | continue; |
| 1915 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1916 | continue; |
| 1917 | /* remember insn and function to return to */ |
| 1918 | ret_insn[frame] = i + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1919 | ret_prog[frame] = idx; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1920 | |
| 1921 | /* find the callee */ |
| 1922 | i = i + insn[i].imm + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1923 | idx = find_subprog(env, i); |
| 1924 | if (idx < 0) { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1925 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1926 | i); |
| 1927 | return -EFAULT; |
| 1928 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1929 | frame++; |
| 1930 | if (frame >= MAX_CALL_FRAMES) { |
Paul Chaignon | 927cb78 | 2019-03-20 13:58:27 +0100 | [diff] [blame] | 1931 | verbose(env, "the call stack of %d frames is too deep !\n", |
| 1932 | frame); |
| 1933 | return -E2BIG; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1934 | } |
| 1935 | goto process_func; |
| 1936 | } |
| 1937 | /* end of for() loop means the last insn of the 'subprog' |
| 1938 | * was reached. Doesn't matter whether it was JA or EXIT |
| 1939 | */ |
| 1940 | if (frame == 0) |
| 1941 | return 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1942 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1943 | frame--; |
| 1944 | i = ret_insn[frame]; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1945 | idx = ret_prog[frame]; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1946 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1947 | } |
| 1948 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1949 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1950 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 1951 | const struct bpf_insn *insn, int idx) |
| 1952 | { |
| 1953 | int start = idx + insn->imm + 1, subprog; |
| 1954 | |
| 1955 | subprog = find_subprog(env, start); |
| 1956 | if (subprog < 0) { |
| 1957 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1958 | start); |
| 1959 | return -EFAULT; |
| 1960 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1961 | return env->subprog_info[subprog].stack_depth; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1962 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1963 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1964 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 1965 | static int check_ctx_reg(struct bpf_verifier_env *env, |
| 1966 | const struct bpf_reg_state *reg, int regno) |
| 1967 | { |
| 1968 | /* Access to ctx or passing it to a helper is only allowed in |
| 1969 | * its original, unmodified form. |
| 1970 | */ |
| 1971 | |
| 1972 | if (reg->off) { |
| 1973 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 1974 | regno, reg->off); |
| 1975 | return -EACCES; |
| 1976 | } |
| 1977 | |
| 1978 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 1979 | char tn_buf[48]; |
| 1980 | |
| 1981 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 1982 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 1983 | return -EACCES; |
| 1984 | } |
| 1985 | |
| 1986 | return 0; |
| 1987 | } |
| 1988 | |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 1989 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
| 1990 | const struct bpf_reg_state *reg, |
| 1991 | int regno, int off, int size) |
| 1992 | { |
| 1993 | if (off < 0) { |
| 1994 | verbose(env, |
| 1995 | "R%d invalid tracepoint buffer access: off=%d, size=%d", |
| 1996 | regno, off, size); |
| 1997 | return -EACCES; |
| 1998 | } |
| 1999 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 2000 | char tn_buf[48]; |
| 2001 | |
| 2002 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2003 | verbose(env, |
| 2004 | "R%d invalid variable buffer offset: off=%d, var_off=%s", |
| 2005 | regno, off, tn_buf); |
| 2006 | return -EACCES; |
| 2007 | } |
| 2008 | if (off + size > env->prog->aux->max_tp_access) |
| 2009 | env->prog->aux->max_tp_access = off + size; |
| 2010 | |
| 2011 | return 0; |
| 2012 | } |
| 2013 | |
| 2014 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 2015 | /* truncate register to smaller size (in bytes) |
| 2016 | * must be called with size < BPF_REG_SIZE |
| 2017 | */ |
| 2018 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 2019 | { |
| 2020 | u64 mask; |
| 2021 | |
| 2022 | /* clear high bits in bit representation */ |
| 2023 | reg->var_off = tnum_cast(reg->var_off, size); |
| 2024 | |
| 2025 | /* fix arithmetic bounds */ |
| 2026 | mask = ((u64)1 << (size * 8)) - 1; |
| 2027 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 2028 | reg->umin_value &= mask; |
| 2029 | reg->umax_value &= mask; |
| 2030 | } else { |
| 2031 | reg->umin_value = 0; |
| 2032 | reg->umax_value = mask; |
| 2033 | } |
| 2034 | reg->smin_value = reg->umin_value; |
| 2035 | reg->smax_value = reg->umax_value; |
| 2036 | } |
| 2037 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2038 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 2039 | * if t==write, value_regno is a register which value is stored into memory |
| 2040 | * if t==read, value_regno is a register which will receive the value from memory |
| 2041 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 2042 | * if t==read && value_regno==-1, don't care what we read from memory |
| 2043 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2044 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 2045 | int off, int bpf_size, enum bpf_access_type t, |
| 2046 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2047 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2048 | struct bpf_reg_state *regs = cur_regs(env); |
| 2049 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2050 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2051 | int size, err = 0; |
| 2052 | |
| 2053 | size = bpf_size_to_bytes(bpf_size); |
| 2054 | if (size < 0) |
| 2055 | return size; |
| 2056 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2057 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2058 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2059 | if (err) |
| 2060 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2061 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2062 | /* for access checks, reg->off is just part of off */ |
| 2063 | off += reg->off; |
| 2064 | |
| 2065 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2066 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2067 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2068 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2069 | return -EACCES; |
| 2070 | } |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 2071 | err = check_map_access_type(env, regno, off, size, t); |
| 2072 | if (err) |
| 2073 | return err; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2074 | err = check_map_access(env, regno, off, size, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2075 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2076 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2077 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2078 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2079 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 2080 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2081 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2082 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2083 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2084 | return -EACCES; |
| 2085 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2086 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 2087 | err = check_ctx_reg(env, reg, regno); |
| 2088 | if (err < 0) |
| 2089 | return err; |
| 2090 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2091 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2092 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2093 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2094 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 2095 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2096 | */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2097 | if (reg_type == SCALAR_VALUE) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2098 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2099 | } else { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2100 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2101 | value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2102 | if (reg_type_may_be_null(reg_type)) |
| 2103 | regs[value_regno].id = ++env->id_gen; |
| 2104 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2105 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2106 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2107 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2108 | } else if (reg->type == PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2109 | off += reg->var_off.value; |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 2110 | err = check_stack_access(env, reg, off, size); |
| 2111 | if (err) |
| 2112 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 2113 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2114 | state = func(env, reg); |
| 2115 | err = update_stack_depth(env, state, off); |
| 2116 | if (err) |
| 2117 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 2118 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2119 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2120 | err = check_stack_write(env, state, off, size, |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2121 | value_regno, insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2122 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2123 | err = check_stack_read(env, state, off, size, |
| 2124 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2125 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2126 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2127 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2128 | return -EACCES; |
| 2129 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2130 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2131 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2132 | verbose(env, "R%d leaks addr into packet\n", |
| 2133 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2134 | return -EACCES; |
| 2135 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2136 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2137 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2138 | mark_reg_unknown(env, regs, value_regno); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2139 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 2140 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2141 | is_pointer_value(env, value_regno)) { |
| 2142 | verbose(env, "R%d leaks addr into flow keys\n", |
| 2143 | value_regno); |
| 2144 | return -EACCES; |
| 2145 | } |
| 2146 | |
| 2147 | err = check_flow_keys_access(env, off, size); |
| 2148 | if (!err && t == BPF_READ && value_regno >= 0) |
| 2149 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2150 | } else if (type_is_sk_pointer(reg->type)) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2151 | if (t == BPF_WRITE) { |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2152 | verbose(env, "R%d cannot write into %s\n", |
| 2153 | regno, reg_type_str[reg->type]); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2154 | return -EACCES; |
| 2155 | } |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2156 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2157 | if (!err && value_regno >= 0) |
| 2158 | mark_reg_unknown(env, regs, value_regno); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 2159 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
| 2160 | err = check_tp_buffer_access(env, reg, regno, off, size); |
| 2161 | if (!err && t == BPF_READ && value_regno >= 0) |
| 2162 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2163 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2164 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 2165 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2166 | return -EACCES; |
| 2167 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2168 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2169 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2170 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2171 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 2172 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2173 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2174 | return err; |
| 2175 | } |
| 2176 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2177 | 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] | 2178 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2179 | int err; |
| 2180 | |
| 2181 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 2182 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2183 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2184 | return -EINVAL; |
| 2185 | } |
| 2186 | |
| 2187 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2188 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2189 | if (err) |
| 2190 | return err; |
| 2191 | |
| 2192 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2193 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2194 | if (err) |
| 2195 | return err; |
| 2196 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 2197 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2198 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 2199 | return -EACCES; |
| 2200 | } |
| 2201 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2202 | if (is_ctx_reg(env, insn->dst_reg) || |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 2203 | is_pkt_reg(env, insn->dst_reg) || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2204 | is_flow_key_reg(env, insn->dst_reg) || |
| 2205 | is_sk_reg(env, insn->dst_reg)) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2206 | 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] | 2207 | insn->dst_reg, |
| 2208 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2209 | return -EACCES; |
| 2210 | } |
| 2211 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2212 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2213 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2214 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2215 | if (err) |
| 2216 | return err; |
| 2217 | |
| 2218 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2219 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2220 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2221 | } |
| 2222 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2223 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
| 2224 | int off, int access_size, |
| 2225 | bool zero_size_allowed) |
| 2226 | { |
| 2227 | struct bpf_reg_state *reg = reg_state(env, regno); |
| 2228 | |
| 2229 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 2230 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
| 2231 | if (tnum_is_const(reg->var_off)) { |
| 2232 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", |
| 2233 | regno, off, access_size); |
| 2234 | } else { |
| 2235 | char tn_buf[48]; |
| 2236 | |
| 2237 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2238 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", |
| 2239 | regno, tn_buf, access_size); |
| 2240 | } |
| 2241 | return -EACCES; |
| 2242 | } |
| 2243 | return 0; |
| 2244 | } |
| 2245 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2246 | /* when register 'regno' is passed into function that will read 'access_size' |
| 2247 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2248 | * and all elements of stack are initialized. |
| 2249 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 2250 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2251 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2252 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2253 | int access_size, bool zero_size_allowed, |
| 2254 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2255 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2256 | struct bpf_reg_state *reg = reg_state(env, regno); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2257 | struct bpf_func_state *state = func(env, reg); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2258 | int err, min_off, max_off, i, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2259 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2260 | if (reg->type != PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2261 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2262 | if (zero_size_allowed && access_size == 0 && |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2263 | register_is_null(reg)) |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2264 | return 0; |
| 2265 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2266 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2267 | reg_type_str[reg->type], |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2268 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2269 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2270 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2271 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2272 | if (tnum_is_const(reg->var_off)) { |
| 2273 | min_off = max_off = reg->var_off.value + reg->off; |
| 2274 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 2275 | zero_size_allowed); |
| 2276 | if (err) |
| 2277 | return err; |
| 2278 | } else { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 2279 | /* Variable offset is prohibited for unprivileged mode for |
| 2280 | * simplicity since it requires corresponding support in |
| 2281 | * Spectre masking for stack ALU. |
| 2282 | * See also retrieve_ptr_limit(). |
| 2283 | */ |
| 2284 | if (!env->allow_ptr_leaks) { |
| 2285 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2286 | |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 2287 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2288 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", |
| 2289 | regno, tn_buf); |
| 2290 | return -EACCES; |
| 2291 | } |
Andrey Ignatov | f2bcd05 | 2019-04-03 23:22:37 -0700 | [diff] [blame] | 2292 | /* Only initialized buffer on stack is allowed to be accessed |
| 2293 | * with variable offset. With uninitialized buffer it's hard to |
| 2294 | * guarantee that whole memory is marked as initialized on |
| 2295 | * helper return since specific bounds are unknown what may |
| 2296 | * cause uninitialized stack leaking. |
| 2297 | */ |
| 2298 | if (meta && meta->raw_mode) |
| 2299 | meta = NULL; |
| 2300 | |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2301 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
| 2302 | reg->smax_value <= -BPF_MAX_VAR_OFF) { |
| 2303 | verbose(env, "R%d unbounded indirect variable offset stack access\n", |
| 2304 | regno); |
| 2305 | return -EACCES; |
| 2306 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2307 | min_off = reg->smin_value + reg->off; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2308 | max_off = reg->smax_value + reg->off; |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2309 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 2310 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2311 | if (err) { |
| 2312 | verbose(env, "R%d min value is outside of stack bound\n", |
| 2313 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2314 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2315 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2316 | err = __check_stack_boundary(env, regno, max_off, access_size, |
| 2317 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2318 | if (err) { |
| 2319 | verbose(env, "R%d max value is outside of stack bound\n", |
| 2320 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2321 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2322 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2325 | if (meta && meta->raw_mode) { |
| 2326 | meta->access_size = access_size; |
| 2327 | meta->regno = regno; |
| 2328 | return 0; |
| 2329 | } |
| 2330 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2331 | for (i = min_off; i < max_off + access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2332 | u8 *stype; |
| 2333 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2334 | slot = -i - 1; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2335 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2336 | if (state->allocated_stack <= slot) |
| 2337 | goto err; |
| 2338 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 2339 | if (*stype == STACK_MISC) |
| 2340 | goto mark; |
| 2341 | if (*stype == STACK_ZERO) { |
| 2342 | /* helper can write anything into the stack */ |
| 2343 | *stype = STACK_MISC; |
| 2344 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2345 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2346 | err: |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2347 | if (tnum_is_const(reg->var_off)) { |
| 2348 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 2349 | min_off, i - min_off, access_size); |
| 2350 | } else { |
| 2351 | char tn_buf[48]; |
| 2352 | |
| 2353 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2354 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", |
| 2355 | tn_buf, i - min_off, access_size); |
| 2356 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2357 | return -EACCES; |
| 2358 | mark: |
| 2359 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 2360 | * the whole slot to be marked as 'read' |
| 2361 | */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2362 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
| 2363 | state->stack[spi].spilled_ptr.parent); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2364 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2365 | return update_stack_depth(env, state, min_off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2366 | } |
| 2367 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2368 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 2369 | int access_size, bool zero_size_allowed, |
| 2370 | struct bpf_call_arg_meta *meta) |
| 2371 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2372 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2373 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2374 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2375 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2376 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2377 | return check_packet_access(env, regno, reg->off, access_size, |
| 2378 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2379 | case PTR_TO_MAP_VALUE: |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 2380 | if (check_map_access_type(env, regno, reg->off, access_size, |
| 2381 | meta && meta->raw_mode ? BPF_WRITE : |
| 2382 | BPF_READ)) |
| 2383 | return -EACCES; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2384 | return check_map_access(env, regno, reg->off, access_size, |
| 2385 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2386 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2387 | return check_stack_boundary(env, regno, access_size, |
| 2388 | zero_size_allowed, meta); |
| 2389 | } |
| 2390 | } |
| 2391 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 2392 | /* Implementation details: |
| 2393 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL |
| 2394 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
| 2395 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after |
| 2396 | * value_or_null->value transition, since the verifier only cares about |
| 2397 | * the range of access to valid map value pointer and doesn't care about actual |
| 2398 | * address of the map element. |
| 2399 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
| 2400 | * reg->id > 0 after value_or_null->value transition. By doing so |
| 2401 | * two bpf_map_lookups will be considered two different pointers that |
| 2402 | * point to different bpf_spin_locks. |
| 2403 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
| 2404 | * dead-locks. |
| 2405 | * Since only one bpf_spin_lock is allowed the checks are simpler than |
| 2406 | * reg_is_refcounted() logic. The verifier needs to remember only |
| 2407 | * one spin_lock instead of array of acquired_refs. |
| 2408 | * cur_state->active_spin_lock remembers which map value element got locked |
| 2409 | * and clears it after bpf_spin_unlock. |
| 2410 | */ |
| 2411 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, |
| 2412 | bool is_lock) |
| 2413 | { |
| 2414 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 2415 | struct bpf_verifier_state *cur = env->cur_state; |
| 2416 | bool is_const = tnum_is_const(reg->var_off); |
| 2417 | struct bpf_map *map = reg->map_ptr; |
| 2418 | u64 val = reg->var_off.value; |
| 2419 | |
| 2420 | if (reg->type != PTR_TO_MAP_VALUE) { |
| 2421 | verbose(env, "R%d is not a pointer to map_value\n", regno); |
| 2422 | return -EINVAL; |
| 2423 | } |
| 2424 | if (!is_const) { |
| 2425 | verbose(env, |
| 2426 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", |
| 2427 | regno); |
| 2428 | return -EINVAL; |
| 2429 | } |
| 2430 | if (!map->btf) { |
| 2431 | verbose(env, |
| 2432 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", |
| 2433 | map->name); |
| 2434 | return -EINVAL; |
| 2435 | } |
| 2436 | if (!map_value_has_spin_lock(map)) { |
| 2437 | if (map->spin_lock_off == -E2BIG) |
| 2438 | verbose(env, |
| 2439 | "map '%s' has more than one 'struct bpf_spin_lock'\n", |
| 2440 | map->name); |
| 2441 | else if (map->spin_lock_off == -ENOENT) |
| 2442 | verbose(env, |
| 2443 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", |
| 2444 | map->name); |
| 2445 | else |
| 2446 | verbose(env, |
| 2447 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", |
| 2448 | map->name); |
| 2449 | return -EINVAL; |
| 2450 | } |
| 2451 | if (map->spin_lock_off != val + reg->off) { |
| 2452 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", |
| 2453 | val + reg->off); |
| 2454 | return -EINVAL; |
| 2455 | } |
| 2456 | if (is_lock) { |
| 2457 | if (cur->active_spin_lock) { |
| 2458 | verbose(env, |
| 2459 | "Locking two bpf_spin_locks are not allowed\n"); |
| 2460 | return -EINVAL; |
| 2461 | } |
| 2462 | cur->active_spin_lock = reg->id; |
| 2463 | } else { |
| 2464 | if (!cur->active_spin_lock) { |
| 2465 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
| 2466 | return -EINVAL; |
| 2467 | } |
| 2468 | if (cur->active_spin_lock != reg->id) { |
| 2469 | verbose(env, "bpf_spin_unlock of different lock\n"); |
| 2470 | return -EINVAL; |
| 2471 | } |
| 2472 | cur->active_spin_lock = 0; |
| 2473 | } |
| 2474 | return 0; |
| 2475 | } |
| 2476 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2477 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 2478 | { |
| 2479 | return type == ARG_PTR_TO_MEM || |
| 2480 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 2481 | type == ARG_PTR_TO_UNINIT_MEM; |
| 2482 | } |
| 2483 | |
| 2484 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 2485 | { |
| 2486 | return type == ARG_CONST_SIZE || |
| 2487 | type == ARG_CONST_SIZE_OR_ZERO; |
| 2488 | } |
| 2489 | |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 2490 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
| 2491 | { |
| 2492 | return type == ARG_PTR_TO_INT || |
| 2493 | type == ARG_PTR_TO_LONG; |
| 2494 | } |
| 2495 | |
| 2496 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
| 2497 | { |
| 2498 | if (type == ARG_PTR_TO_INT) |
| 2499 | return sizeof(u32); |
| 2500 | else if (type == ARG_PTR_TO_LONG) |
| 2501 | return sizeof(u64); |
| 2502 | |
| 2503 | return -EINVAL; |
| 2504 | } |
| 2505 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2506 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2507 | enum bpf_arg_type arg_type, |
| 2508 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2509 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2510 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2511 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2512 | int err = 0; |
| 2513 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2514 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2515 | return 0; |
| 2516 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2517 | err = check_reg_arg(env, regno, SRC_OP); |
| 2518 | if (err) |
| 2519 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2520 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2521 | if (arg_type == ARG_ANYTHING) { |
| 2522 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2523 | verbose(env, "R%d leaks addr into helper function\n", |
| 2524 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2525 | return -EACCES; |
| 2526 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2527 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2528 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2529 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2530 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2531 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2532 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2533 | return -EACCES; |
| 2534 | } |
| 2535 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2536 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2537 | arg_type == ARG_PTR_TO_MAP_VALUE || |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2538 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
| 2539 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2540 | expected_type = PTR_TO_STACK; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2541 | if (register_is_null(reg) && |
| 2542 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) |
| 2543 | /* final test in check_stack_boundary() */; |
| 2544 | else if (!type_is_pkt_pointer(type) && |
| 2545 | type != PTR_TO_MAP_VALUE && |
| 2546 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2547 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2548 | } else if (arg_type == ARG_CONST_SIZE || |
| 2549 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2550 | expected_type = SCALAR_VALUE; |
| 2551 | if (type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2552 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2553 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 2554 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2555 | if (type != expected_type) |
| 2556 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2557 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 2558 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2559 | if (type != expected_type) |
| 2560 | goto err_type; |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 2561 | err = check_ctx_reg(env, reg, regno); |
| 2562 | if (err < 0) |
| 2563 | return err; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2564 | } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) { |
| 2565 | expected_type = PTR_TO_SOCK_COMMON; |
| 2566 | /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */ |
| 2567 | if (!type_is_sk_pointer(type)) |
| 2568 | goto err_type; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2569 | if (reg->ref_obj_id) { |
| 2570 | if (meta->ref_obj_id) { |
| 2571 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", |
| 2572 | regno, reg->ref_obj_id, |
| 2573 | meta->ref_obj_id); |
| 2574 | return -EFAULT; |
| 2575 | } |
| 2576 | meta->ref_obj_id = reg->ref_obj_id; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2577 | } |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2578 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
| 2579 | expected_type = PTR_TO_SOCKET; |
| 2580 | if (type != expected_type) |
| 2581 | goto err_type; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 2582 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
| 2583 | if (meta->func_id == BPF_FUNC_spin_lock) { |
| 2584 | if (process_spin_lock(env, regno, true)) |
| 2585 | return -EACCES; |
| 2586 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
| 2587 | if (process_spin_lock(env, regno, false)) |
| 2588 | return -EACCES; |
| 2589 | } else { |
| 2590 | verbose(env, "verifier internal error\n"); |
| 2591 | return -EFAULT; |
| 2592 | } |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2593 | } else if (arg_type_is_mem_ptr(arg_type)) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2594 | expected_type = PTR_TO_STACK; |
| 2595 | /* One exception here. In case function allows for NULL to be |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2596 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2597 | * happens during stack boundary checking. |
| 2598 | */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2599 | if (register_is_null(reg) && |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 2600 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2601 | /* final test in check_stack_boundary() */; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2602 | else if (!type_is_pkt_pointer(type) && |
| 2603 | type != PTR_TO_MAP_VALUE && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2604 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2605 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2606 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 2607 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 2608 | expected_type = PTR_TO_STACK; |
| 2609 | if (!type_is_pkt_pointer(type) && |
| 2610 | type != PTR_TO_MAP_VALUE && |
| 2611 | type != expected_type) |
| 2612 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2613 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2614 | verbose(env, "unsupported arg_type %d\n", arg_type); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2615 | return -EFAULT; |
| 2616 | } |
| 2617 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2618 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 2619 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2620 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2621 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 2622 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 2623 | * check that [key, key + map->key_size) are within |
| 2624 | * stack limits and initialized |
| 2625 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2626 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2627 | /* in function declaration map_ptr must come before |
| 2628 | * map_key, so that it's verified and known before |
| 2629 | * we have to check map_key here. Otherwise it means |
| 2630 | * that kernel subsystem misconfigured verifier |
| 2631 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2632 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2633 | return -EACCES; |
| 2634 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2635 | err = check_helper_mem_access(env, regno, |
| 2636 | meta->map_ptr->key_size, false, |
| 2637 | NULL); |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2638 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2639 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
| 2640 | !register_is_null(reg)) || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2641 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2642 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 2643 | * check [value, value + map->value_size) validity |
| 2644 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2645 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2646 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2647 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2648 | return -EACCES; |
| 2649 | } |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2650 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2651 | err = check_helper_mem_access(env, regno, |
| 2652 | meta->map_ptr->value_size, false, |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2653 | meta); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2654 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2655 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2656 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2657 | /* remember the mem_size which may be used later |
| 2658 | * to refine return values. |
| 2659 | */ |
| 2660 | meta->msize_smax_value = reg->smax_value; |
| 2661 | meta->msize_umax_value = reg->umax_value; |
| 2662 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2663 | /* The register is SCALAR_VALUE; the access check |
| 2664 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2665 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2666 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2667 | /* For unprivileged variable accesses, disable raw |
| 2668 | * mode so that the program is required to |
| 2669 | * initialize all the memory that the helper could |
| 2670 | * just partially fill up. |
| 2671 | */ |
| 2672 | meta = NULL; |
| 2673 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2674 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2675 | 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] | 2676 | regno); |
| 2677 | return -EACCES; |
| 2678 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2679 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2680 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2681 | err = check_helper_mem_access(env, regno - 1, 0, |
| 2682 | zero_size_allowed, |
| 2683 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2684 | if (err) |
| 2685 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2686 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2687 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2688 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2689 | 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] | 2690 | regno); |
| 2691 | return -EACCES; |
| 2692 | } |
| 2693 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2694 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2695 | zero_size_allowed, meta); |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 2696 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 2697 | int size = int_ptr_type_to_size(arg_type); |
| 2698 | |
| 2699 | err = check_helper_mem_access(env, regno, size, false, meta); |
| 2700 | if (err) |
| 2701 | return err; |
| 2702 | err = check_ptr_alignment(env, reg, 0, size, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2706 | err_type: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2707 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2708 | reg_type_str[type], reg_type_str[expected_type]); |
| 2709 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2710 | } |
| 2711 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2712 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 2713 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2714 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2715 | if (!map) |
| 2716 | return 0; |
| 2717 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2718 | /* We need a two way check, first is from map perspective ... */ |
| 2719 | switch (map->map_type) { |
| 2720 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 2721 | if (func_id != BPF_FUNC_tail_call) |
| 2722 | goto error; |
| 2723 | break; |
| 2724 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 2725 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2726 | func_id != BPF_FUNC_perf_event_output && |
| 2727 | func_id != BPF_FUNC_perf_event_read_value) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2728 | goto error; |
| 2729 | break; |
| 2730 | case BPF_MAP_TYPE_STACK_TRACE: |
| 2731 | if (func_id != BPF_FUNC_get_stackid) |
| 2732 | goto error; |
| 2733 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 2734 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 2735 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2736 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2737 | goto error; |
| 2738 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2739 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 2740 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2741 | if (func_id != BPF_FUNC_get_local_storage) |
| 2742 | goto error; |
| 2743 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2744 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
| 2745 | * allow to be modified from bpf side. So do not allow lookup elements |
| 2746 | * for now. |
| 2747 | */ |
| 2748 | case BPF_MAP_TYPE_DEVMAP: |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 2749 | if (func_id != BPF_FUNC_redirect_map) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2750 | goto error; |
| 2751 | break; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2752 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 2753 | * appear. |
| 2754 | */ |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2755 | case BPF_MAP_TYPE_CPUMAP: |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2756 | case BPF_MAP_TYPE_XSKMAP: |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2757 | if (func_id != BPF_FUNC_redirect_map) |
| 2758 | goto error; |
| 2759 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2760 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 2761 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2762 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 2763 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 2764 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2765 | case BPF_MAP_TYPE_SOCKMAP: |
| 2766 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 2767 | func_id != BPF_FUNC_sock_map_update && |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2768 | func_id != BPF_FUNC_map_delete_elem && |
| 2769 | func_id != BPF_FUNC_msg_redirect_map) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2770 | goto error; |
| 2771 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2772 | case BPF_MAP_TYPE_SOCKHASH: |
| 2773 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 2774 | func_id != BPF_FUNC_sock_hash_update && |
| 2775 | func_id != BPF_FUNC_map_delete_elem && |
| 2776 | func_id != BPF_FUNC_msg_redirect_hash) |
| 2777 | goto error; |
| 2778 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2779 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 2780 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 2781 | goto error; |
| 2782 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 2783 | case BPF_MAP_TYPE_QUEUE: |
| 2784 | case BPF_MAP_TYPE_STACK: |
| 2785 | if (func_id != BPF_FUNC_map_peek_elem && |
| 2786 | func_id != BPF_FUNC_map_pop_elem && |
| 2787 | func_id != BPF_FUNC_map_push_elem) |
| 2788 | goto error; |
| 2789 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2790 | case BPF_MAP_TYPE_SK_STORAGE: |
| 2791 | if (func_id != BPF_FUNC_sk_storage_get && |
| 2792 | func_id != BPF_FUNC_sk_storage_delete) |
| 2793 | goto error; |
| 2794 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2795 | default: |
| 2796 | break; |
| 2797 | } |
| 2798 | |
| 2799 | /* ... and second from the function itself. */ |
| 2800 | switch (func_id) { |
| 2801 | case BPF_FUNC_tail_call: |
| 2802 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 2803 | goto error; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 2804 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2805 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
| 2806 | return -EINVAL; |
| 2807 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2808 | break; |
| 2809 | case BPF_FUNC_perf_event_read: |
| 2810 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2811 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2812 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 2813 | goto error; |
| 2814 | break; |
| 2815 | case BPF_FUNC_get_stackid: |
| 2816 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 2817 | goto error; |
| 2818 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2819 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 2820 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2821 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 2822 | goto error; |
| 2823 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2824 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 2825 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2826 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 2827 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2828 | goto error; |
| 2829 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2830 | case BPF_FUNC_sk_redirect_map: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2831 | case BPF_FUNC_msg_redirect_map: |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2832 | case BPF_FUNC_sock_map_update: |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2833 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 2834 | goto error; |
| 2835 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2836 | case BPF_FUNC_sk_redirect_hash: |
| 2837 | case BPF_FUNC_msg_redirect_hash: |
| 2838 | case BPF_FUNC_sock_hash_update: |
| 2839 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2840 | goto error; |
| 2841 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2842 | case BPF_FUNC_get_local_storage: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 2843 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 2844 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2845 | goto error; |
| 2846 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2847 | case BPF_FUNC_sk_select_reuseport: |
| 2848 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) |
| 2849 | goto error; |
| 2850 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 2851 | case BPF_FUNC_map_peek_elem: |
| 2852 | case BPF_FUNC_map_pop_elem: |
| 2853 | case BPF_FUNC_map_push_elem: |
| 2854 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 2855 | map->map_type != BPF_MAP_TYPE_STACK) |
| 2856 | goto error; |
| 2857 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2858 | case BPF_FUNC_sk_storage_get: |
| 2859 | case BPF_FUNC_sk_storage_delete: |
| 2860 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
| 2861 | goto error; |
| 2862 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2863 | default: |
| 2864 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2868 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2869 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2870 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2871 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2874 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2875 | { |
| 2876 | int count = 0; |
| 2877 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2878 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2879 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2880 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2881 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2882 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2883 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2884 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2885 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2886 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2887 | count++; |
| 2888 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2889 | /* We only support one arg being in raw mode at the moment, |
| 2890 | * which is sufficient for the helper functions we have |
| 2891 | * right now. |
| 2892 | */ |
| 2893 | return count <= 1; |
| 2894 | } |
| 2895 | |
| 2896 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 2897 | enum bpf_arg_type arg_next) |
| 2898 | { |
| 2899 | return (arg_type_is_mem_ptr(arg_curr) && |
| 2900 | !arg_type_is_mem_size(arg_next)) || |
| 2901 | (!arg_type_is_mem_ptr(arg_curr) && |
| 2902 | arg_type_is_mem_size(arg_next)); |
| 2903 | } |
| 2904 | |
| 2905 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 2906 | { |
| 2907 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 2908 | * bytes from memory 'buf'. Both arg types need |
| 2909 | * to be paired, so make sure there's no buggy |
| 2910 | * helper function specification. |
| 2911 | */ |
| 2912 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 2913 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 2914 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 2915 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 2916 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 2917 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 2918 | return false; |
| 2919 | |
| 2920 | return true; |
| 2921 | } |
| 2922 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2923 | 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] | 2924 | { |
| 2925 | int count = 0; |
| 2926 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2927 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2928 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2929 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2930 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2931 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2932 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2933 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2934 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2935 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2936 | count++; |
| 2937 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2938 | /* A reference acquiring function cannot acquire |
| 2939 | * another refcounted ptr. |
| 2940 | */ |
| 2941 | if (is_acquire_function(func_id) && count) |
| 2942 | return false; |
| 2943 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2944 | /* We only support one arg being unreferenced at the moment, |
| 2945 | * which is sufficient for the helper functions we have right now. |
| 2946 | */ |
| 2947 | return count <= 1; |
| 2948 | } |
| 2949 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2950 | 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] | 2951 | { |
| 2952 | return check_raw_mode_ok(fn) && |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2953 | check_arg_pair_ok(fn) && |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2954 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2955 | } |
| 2956 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2957 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 2958 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2959 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2960 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 2961 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2962 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2963 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2964 | int i; |
| 2965 | |
| 2966 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2967 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2968 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2969 | |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 2970 | bpf_for_each_spilled_reg(i, state, reg) { |
| 2971 | if (!reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2972 | continue; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2973 | if (reg_is_pkt_pointer_any(reg)) |
| 2974 | __mark_reg_unknown(reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2975 | } |
| 2976 | } |
| 2977 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2978 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 2979 | { |
| 2980 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2981 | int i; |
| 2982 | |
| 2983 | for (i = 0; i <= vstate->curframe; i++) |
| 2984 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 2985 | } |
| 2986 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2987 | static void release_reg_references(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2988 | struct bpf_func_state *state, |
| 2989 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2990 | { |
| 2991 | struct bpf_reg_state *regs = state->regs, *reg; |
| 2992 | int i; |
| 2993 | |
| 2994 | for (i = 0; i < MAX_BPF_REG; i++) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2995 | if (regs[i].ref_obj_id == ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2996 | mark_reg_unknown(env, regs, i); |
| 2997 | |
| 2998 | bpf_for_each_spilled_reg(i, state, reg) { |
| 2999 | if (!reg) |
| 3000 | continue; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3001 | if (reg->ref_obj_id == ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3002 | __mark_reg_unknown(reg); |
| 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | /* The pointer with the specified id has released its reference to kernel |
| 3007 | * resources. Identify all copies of the same pointer and clear the reference. |
| 3008 | */ |
| 3009 | static int release_reference(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3010 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3011 | { |
| 3012 | struct bpf_verifier_state *vstate = env->cur_state; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3013 | int err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3014 | int i; |
| 3015 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3016 | err = release_reference_state(cur_func(env), ref_obj_id); |
| 3017 | if (err) |
| 3018 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3019 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3020 | for (i = 0; i <= vstate->curframe; i++) |
| 3021 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
| 3022 | |
| 3023 | return 0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3024 | } |
| 3025 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3026 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 3027 | int *insn_idx) |
| 3028 | { |
| 3029 | struct bpf_verifier_state *state = env->cur_state; |
| 3030 | struct bpf_func_state *caller, *callee; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3031 | int i, err, subprog, target_insn; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3032 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 3033 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3034 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 3035 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3036 | return -E2BIG; |
| 3037 | } |
| 3038 | |
| 3039 | target_insn = *insn_idx + insn->imm; |
| 3040 | subprog = find_subprog(env, target_insn + 1); |
| 3041 | if (subprog < 0) { |
| 3042 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 3043 | target_insn + 1); |
| 3044 | return -EFAULT; |
| 3045 | } |
| 3046 | |
| 3047 | caller = state->frame[state->curframe]; |
| 3048 | if (state->frame[state->curframe + 1]) { |
| 3049 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 3050 | state->curframe + 1); |
| 3051 | return -EFAULT; |
| 3052 | } |
| 3053 | |
| 3054 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 3055 | if (!callee) |
| 3056 | return -ENOMEM; |
| 3057 | state->frame[state->curframe + 1] = callee; |
| 3058 | |
| 3059 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 3060 | * into its own stack before reading from it. |
| 3061 | * callee can read/write into caller's stack |
| 3062 | */ |
| 3063 | init_func_state(env, callee, |
| 3064 | /* remember the callsite, it will be used by bpf_exit */ |
| 3065 | *insn_idx /* callsite */, |
| 3066 | state->curframe + 1 /* frameno within this callchain */, |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 3067 | subprog /* subprog number within this prog */); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3068 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3069 | /* Transfer references to the callee */ |
| 3070 | err = transfer_reference_state(callee, caller); |
| 3071 | if (err) |
| 3072 | return err; |
| 3073 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 3074 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 3075 | * pointers, which connects us up to the liveness chain |
| 3076 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3077 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 3078 | callee->regs[i] = caller->regs[i]; |
| 3079 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 3080 | /* after the call registers r0 - r5 were scratched */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3081 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 3082 | mark_reg_not_init(env, caller->regs, caller_saved[i]); |
| 3083 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 3084 | } |
| 3085 | |
| 3086 | /* only increment it after check_reg_arg() finished */ |
| 3087 | state->curframe++; |
| 3088 | |
| 3089 | /* and go analyze first insn of the callee */ |
| 3090 | *insn_idx = target_insn; |
| 3091 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 3092 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3093 | verbose(env, "caller:\n"); |
| 3094 | print_verifier_state(env, caller); |
| 3095 | verbose(env, "callee:\n"); |
| 3096 | print_verifier_state(env, callee); |
| 3097 | } |
| 3098 | return 0; |
| 3099 | } |
| 3100 | |
| 3101 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 3102 | { |
| 3103 | struct bpf_verifier_state *state = env->cur_state; |
| 3104 | struct bpf_func_state *caller, *callee; |
| 3105 | struct bpf_reg_state *r0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3106 | int err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3107 | |
| 3108 | callee = state->frame[state->curframe]; |
| 3109 | r0 = &callee->regs[BPF_REG_0]; |
| 3110 | if (r0->type == PTR_TO_STACK) { |
| 3111 | /* technically it's ok to return caller's stack pointer |
| 3112 | * (or caller's caller's pointer) back to the caller, |
| 3113 | * since these pointers are valid. Only current stack |
| 3114 | * pointer will be invalid as soon as function exits, |
| 3115 | * but let's be conservative |
| 3116 | */ |
| 3117 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 3118 | return -EINVAL; |
| 3119 | } |
| 3120 | |
| 3121 | state->curframe--; |
| 3122 | caller = state->frame[state->curframe]; |
| 3123 | /* return to the caller whatever r0 had in the callee */ |
| 3124 | caller->regs[BPF_REG_0] = *r0; |
| 3125 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3126 | /* Transfer references to the caller */ |
| 3127 | err = transfer_reference_state(caller, callee); |
| 3128 | if (err) |
| 3129 | return err; |
| 3130 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3131 | *insn_idx = callee->callsite + 1; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 3132 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3133 | verbose(env, "returning from callee:\n"); |
| 3134 | print_verifier_state(env, callee); |
| 3135 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 3136 | print_verifier_state(env, caller); |
| 3137 | } |
| 3138 | /* clear everything in the callee */ |
| 3139 | free_func_state(callee); |
| 3140 | state->frame[state->curframe + 1] = NULL; |
| 3141 | return 0; |
| 3142 | } |
| 3143 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 3144 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 3145 | int func_id, |
| 3146 | struct bpf_call_arg_meta *meta) |
| 3147 | { |
| 3148 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 3149 | |
| 3150 | if (ret_type != RET_INTEGER || |
| 3151 | (func_id != BPF_FUNC_get_stack && |
| 3152 | func_id != BPF_FUNC_probe_read_str)) |
| 3153 | return; |
| 3154 | |
| 3155 | ret_reg->smax_value = meta->msize_smax_value; |
| 3156 | ret_reg->umax_value = meta->msize_umax_value; |
| 3157 | __reg_deduce_bounds(ret_reg); |
| 3158 | __reg_bound_offset(ret_reg); |
| 3159 | } |
| 3160 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3161 | static int |
| 3162 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 3163 | int func_id, int insn_idx) |
| 3164 | { |
| 3165 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3166 | struct bpf_map *map = meta->map_ptr; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3167 | |
| 3168 | if (func_id != BPF_FUNC_tail_call && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 3169 | func_id != BPF_FUNC_map_lookup_elem && |
| 3170 | func_id != BPF_FUNC_map_update_elem && |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 3171 | func_id != BPF_FUNC_map_delete_elem && |
| 3172 | func_id != BPF_FUNC_map_push_elem && |
| 3173 | func_id != BPF_FUNC_map_pop_elem && |
| 3174 | func_id != BPF_FUNC_map_peek_elem) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3175 | return 0; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 3176 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3177 | if (map == NULL) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3178 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 3179 | return -EINVAL; |
| 3180 | } |
| 3181 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3182 | /* In case of read-only, some additional restrictions |
| 3183 | * need to be applied in order to prevent altering the |
| 3184 | * state of the map from program side. |
| 3185 | */ |
| 3186 | if ((map->map_flags & BPF_F_RDONLY_PROG) && |
| 3187 | (func_id == BPF_FUNC_map_delete_elem || |
| 3188 | func_id == BPF_FUNC_map_update_elem || |
| 3189 | func_id == BPF_FUNC_map_push_elem || |
| 3190 | func_id == BPF_FUNC_map_pop_elem)) { |
| 3191 | verbose(env, "write into map forbidden\n"); |
| 3192 | return -EACCES; |
| 3193 | } |
| 3194 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3195 | if (!BPF_MAP_PTR(aux->map_state)) |
| 3196 | bpf_map_ptr_store(aux, meta->map_ptr, |
| 3197 | meta->map_ptr->unpriv_array); |
| 3198 | else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr) |
| 3199 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
| 3200 | meta->map_ptr->unpriv_array); |
| 3201 | return 0; |
| 3202 | } |
| 3203 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3204 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 3205 | { |
| 3206 | struct bpf_func_state *state = cur_func(env); |
| 3207 | int i; |
| 3208 | |
| 3209 | for (i = 0; i < state->acquired_refs; i++) { |
| 3210 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 3211 | state->refs[i].id, state->refs[i].insn_idx); |
| 3212 | } |
| 3213 | return state->acquired_refs ? -EINVAL : 0; |
| 3214 | } |
| 3215 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3216 | 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] | 3217 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3218 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3219 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3220 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3221 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3222 | int i, err; |
| 3223 | |
| 3224 | /* find function prototype */ |
| 3225 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3226 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 3227 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3228 | return -EINVAL; |
| 3229 | } |
| 3230 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 3231 | if (env->ops->get_func_proto) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 3232 | fn = env->ops->get_func_proto(func_id, env->prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3233 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3234 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 3235 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3236 | return -EINVAL; |
| 3237 | } |
| 3238 | |
| 3239 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 3240 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Daniel Borkmann | 3fe2867 | 2018-06-02 23:06:33 +0200 | [diff] [blame] | 3241 | 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] | 3242 | return -EINVAL; |
| 3243 | } |
| 3244 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 3245 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 3246 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 3247 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 3248 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 3249 | func_id_name(func_id), func_id); |
| 3250 | return -EINVAL; |
| 3251 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3252 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3253 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3254 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3255 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3256 | err = check_func_proto(fn, func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3257 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3258 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 3259 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3260 | return err; |
| 3261 | } |
| 3262 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3263 | meta.func_id = func_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3264 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3265 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3266 | if (err) |
| 3267 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3268 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3269 | if (err) |
| 3270 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3271 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3272 | if (err) |
| 3273 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3274 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3275 | if (err) |
| 3276 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3277 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3278 | if (err) |
| 3279 | return err; |
| 3280 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3281 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 3282 | if (err) |
| 3283 | return err; |
| 3284 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3285 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 3286 | * is inferred from register state. |
| 3287 | */ |
| 3288 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3289 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 3290 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3291 | if (err) |
| 3292 | return err; |
| 3293 | } |
| 3294 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3295 | if (func_id == BPF_FUNC_tail_call) { |
| 3296 | err = check_reference_leak(env); |
| 3297 | if (err) { |
| 3298 | verbose(env, "tail_call would lead to reference leak\n"); |
| 3299 | return err; |
| 3300 | } |
| 3301 | } else if (is_release_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3302 | err = release_reference(env, meta.ref_obj_id); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3303 | if (err) { |
| 3304 | verbose(env, "func %s#%d reference has not been acquired before\n", |
| 3305 | func_id_name(func_id), func_id); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3306 | return err; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3307 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3308 | } |
| 3309 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3310 | regs = cur_regs(env); |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 3311 | |
| 3312 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 3313 | * this is required because get_local_storage() can't return an error. |
| 3314 | */ |
| 3315 | if (func_id == BPF_FUNC_get_local_storage && |
| 3316 | !register_is_null(®s[BPF_REG_2])) { |
| 3317 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 3318 | return -EINVAL; |
| 3319 | } |
| 3320 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3321 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3322 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3323 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3324 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 3325 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3326 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3327 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3328 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3329 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3330 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3331 | } else if (fn->ret_type == RET_VOID) { |
| 3332 | regs[BPF_REG_0].type = NOT_INIT; |
Roman Gushchin | 3e6a4b3 | 2018-08-02 14:27:22 -0700 | [diff] [blame] | 3333 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 3334 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3335 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3336 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3337 | /* remember map_ptr, so that check_map_access() |
| 3338 | * can check 'value_size' boundary of memory access |
| 3339 | * to map element returned from bpf_map_lookup_elem() |
| 3340 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3341 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3342 | verbose(env, |
| 3343 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3344 | return -EINVAL; |
| 3345 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3346 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 3347 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 3348 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
Alexei Starovoitov | e16d2f1 | 2019-01-31 15:40:05 -0800 | [diff] [blame] | 3349 | if (map_value_has_spin_lock(meta.map_ptr)) |
| 3350 | regs[BPF_REG_0].id = ++env->id_gen; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 3351 | } else { |
| 3352 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
| 3353 | regs[BPF_REG_0].id = ++env->id_gen; |
| 3354 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3355 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
| 3356 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 3357 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 3358 | regs[BPF_REG_0].id = ++env->id_gen; |
Lorenz Bauer | 85a51f8 | 2019-03-22 09:54:00 +0800 | [diff] [blame] | 3359 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
| 3360 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 3361 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; |
| 3362 | regs[BPF_REG_0].id = ++env->id_gen; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 3363 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
| 3364 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 3365 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; |
| 3366 | regs[BPF_REG_0].id = ++env->id_gen; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3367 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3368 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 3369 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3370 | return -EINVAL; |
| 3371 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 3372 | |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 3373 | if (is_ptr_cast_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3374 | /* For release_reference() */ |
| 3375 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 3376 | } else if (is_acquire_function(func_id)) { |
| 3377 | int id = acquire_reference_state(env, insn_idx); |
| 3378 | |
| 3379 | if (id < 0) |
| 3380 | return id; |
| 3381 | /* For mark_ptr_or_null_reg() */ |
| 3382 | regs[BPF_REG_0].id = id; |
| 3383 | /* For release_reference() */ |
| 3384 | regs[BPF_REG_0].ref_obj_id = id; |
| 3385 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3386 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 3387 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
| 3388 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3389 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 3390 | if (err) |
| 3391 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 3392 | |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 3393 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
| 3394 | const char *err_str; |
| 3395 | |
| 3396 | #ifdef CONFIG_PERF_EVENTS |
| 3397 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 3398 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 3399 | #else |
| 3400 | err = -ENOTSUPP; |
| 3401 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 3402 | #endif |
| 3403 | if (err) { |
| 3404 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 3405 | return err; |
| 3406 | } |
| 3407 | |
| 3408 | env->prog->has_callchain_buf = true; |
| 3409 | } |
| 3410 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3411 | if (changes_data) |
| 3412 | clear_all_pkt_pointers(env); |
| 3413 | return 0; |
| 3414 | } |
| 3415 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3416 | static bool signed_add_overflows(s64 a, s64 b) |
| 3417 | { |
| 3418 | /* Do the add in u64, where overflow is well-defined */ |
| 3419 | s64 res = (s64)((u64)a + (u64)b); |
| 3420 | |
| 3421 | if (b < 0) |
| 3422 | return res > a; |
| 3423 | return res < a; |
| 3424 | } |
| 3425 | |
| 3426 | static bool signed_sub_overflows(s64 a, s64 b) |
| 3427 | { |
| 3428 | /* Do the sub in u64, where overflow is well-defined */ |
| 3429 | s64 res = (s64)((u64)a - (u64)b); |
| 3430 | |
| 3431 | if (b < 0) |
| 3432 | return res < a; |
| 3433 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3434 | } |
| 3435 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3436 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 3437 | const struct bpf_reg_state *reg, |
| 3438 | enum bpf_reg_type type) |
| 3439 | { |
| 3440 | bool known = tnum_is_const(reg->var_off); |
| 3441 | s64 val = reg->var_off.value; |
| 3442 | s64 smin = reg->smin_value; |
| 3443 | |
| 3444 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 3445 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 3446 | reg_type_str[type], val); |
| 3447 | return false; |
| 3448 | } |
| 3449 | |
| 3450 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 3451 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 3452 | reg_type_str[type], reg->off); |
| 3453 | return false; |
| 3454 | } |
| 3455 | |
| 3456 | if (smin == S64_MIN) { |
| 3457 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 3458 | reg_type_str[type]); |
| 3459 | return false; |
| 3460 | } |
| 3461 | |
| 3462 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 3463 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 3464 | smin, reg_type_str[type]); |
| 3465 | return false; |
| 3466 | } |
| 3467 | |
| 3468 | return true; |
| 3469 | } |
| 3470 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3471 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
| 3472 | { |
| 3473 | return &env->insn_aux_data[env->insn_idx]; |
| 3474 | } |
| 3475 | |
| 3476 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
| 3477 | u32 *ptr_limit, u8 opcode, bool off_is_neg) |
| 3478 | { |
| 3479 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || |
| 3480 | (opcode == BPF_SUB && !off_is_neg); |
| 3481 | u32 off; |
| 3482 | |
| 3483 | switch (ptr_reg->type) { |
| 3484 | case PTR_TO_STACK: |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3485 | /* Indirect variable offset stack access is prohibited in |
| 3486 | * unprivileged mode so it's not handled here. |
| 3487 | */ |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3488 | off = ptr_reg->off + ptr_reg->var_off.value; |
| 3489 | if (mask_to_left) |
| 3490 | *ptr_limit = MAX_BPF_STACK + off; |
| 3491 | else |
| 3492 | *ptr_limit = -off; |
| 3493 | return 0; |
| 3494 | case PTR_TO_MAP_VALUE: |
| 3495 | if (mask_to_left) { |
| 3496 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; |
| 3497 | } else { |
| 3498 | off = ptr_reg->smin_value + ptr_reg->off; |
| 3499 | *ptr_limit = ptr_reg->map_ptr->value_size - off; |
| 3500 | } |
| 3501 | return 0; |
| 3502 | default: |
| 3503 | return -EINVAL; |
| 3504 | } |
| 3505 | } |
| 3506 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 3507 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
| 3508 | const struct bpf_insn *insn) |
| 3509 | { |
| 3510 | return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K; |
| 3511 | } |
| 3512 | |
| 3513 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, |
| 3514 | u32 alu_state, u32 alu_limit) |
| 3515 | { |
| 3516 | /* If we arrived here from different branches with different |
| 3517 | * state or limits to sanitize, then this won't work. |
| 3518 | */ |
| 3519 | if (aux->alu_state && |
| 3520 | (aux->alu_state != alu_state || |
| 3521 | aux->alu_limit != alu_limit)) |
| 3522 | return -EACCES; |
| 3523 | |
| 3524 | /* Corresponding fixup done in fixup_bpf_calls(). */ |
| 3525 | aux->alu_state = alu_state; |
| 3526 | aux->alu_limit = alu_limit; |
| 3527 | return 0; |
| 3528 | } |
| 3529 | |
| 3530 | static int sanitize_val_alu(struct bpf_verifier_env *env, |
| 3531 | struct bpf_insn *insn) |
| 3532 | { |
| 3533 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 3534 | |
| 3535 | if (can_skip_alu_sanitation(env, insn)) |
| 3536 | return 0; |
| 3537 | |
| 3538 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); |
| 3539 | } |
| 3540 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3541 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
| 3542 | struct bpf_insn *insn, |
| 3543 | const struct bpf_reg_state *ptr_reg, |
| 3544 | struct bpf_reg_state *dst_reg, |
| 3545 | bool off_is_neg) |
| 3546 | { |
| 3547 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3548 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 3549 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
| 3550 | u8 opcode = BPF_OP(insn->code); |
| 3551 | u32 alu_state, alu_limit; |
| 3552 | struct bpf_reg_state tmp; |
| 3553 | bool ret; |
| 3554 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 3555 | if (can_skip_alu_sanitation(env, insn)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3556 | return 0; |
| 3557 | |
| 3558 | /* We already marked aux for masking from non-speculative |
| 3559 | * paths, thus we got here in the first place. We only care |
| 3560 | * to explore bad access from here. |
| 3561 | */ |
| 3562 | if (vstate->speculative) |
| 3563 | goto do_sim; |
| 3564 | |
| 3565 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; |
| 3566 | alu_state |= ptr_is_dst_reg ? |
| 3567 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; |
| 3568 | |
| 3569 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) |
| 3570 | return 0; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 3571 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3572 | return -EACCES; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3573 | do_sim: |
| 3574 | /* Simulate and find potential out-of-bounds access under |
| 3575 | * speculative execution from truncation as a result of |
| 3576 | * masking when off was not within expected range. If off |
| 3577 | * sits in dst, then we temporarily need to move ptr there |
| 3578 | * to simulate dst (== 0) +/-= ptr. Needed, for example, |
| 3579 | * for cases where we use K-based arithmetic in one direction |
| 3580 | * and truncated reg-based in the other in order to explore |
| 3581 | * bad access. |
| 3582 | */ |
| 3583 | if (!ptr_is_dst_reg) { |
| 3584 | tmp = *dst_reg; |
| 3585 | *dst_reg = *ptr_reg; |
| 3586 | } |
| 3587 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); |
Xu Yu | 0803278 | 2019-03-21 18:00:35 +0800 | [diff] [blame] | 3588 | if (!ptr_is_dst_reg && ret) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3589 | *dst_reg = tmp; |
| 3590 | return !ret ? -EFAULT : 0; |
| 3591 | } |
| 3592 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3593 | /* 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] | 3594 | * Caller should also handle BPF_MOV case separately. |
| 3595 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 3596 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 3597 | */ |
| 3598 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 3599 | struct bpf_insn *insn, |
| 3600 | const struct bpf_reg_state *ptr_reg, |
| 3601 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3602 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3603 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3604 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 3605 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3606 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3607 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 3608 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 3609 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 3610 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 3611 | u32 dst = insn->dst_reg, src = insn->src_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3612 | u8 opcode = BPF_OP(insn->code); |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3613 | int ret; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3614 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3615 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3616 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 3617 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 3618 | smin_val > smax_val || umin_val > umax_val) { |
| 3619 | /* Taint dst register if offset had invalid bounds derived from |
| 3620 | * e.g. dead branches. |
| 3621 | */ |
| 3622 | __mark_reg_unknown(dst_reg); |
| 3623 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3624 | } |
| 3625 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3626 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3627 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3628 | verbose(env, |
| 3629 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 3630 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3631 | return -EACCES; |
| 3632 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3633 | |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3634 | switch (ptr_reg->type) { |
| 3635 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 3636 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
| 3637 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3638 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3639 | case CONST_PTR_TO_MAP: |
| 3640 | case PTR_TO_PACKET_END: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3641 | case PTR_TO_SOCKET: |
| 3642 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3643 | case PTR_TO_SOCK_COMMON: |
| 3644 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 3645 | case PTR_TO_TCP_SOCK: |
| 3646 | case PTR_TO_TCP_SOCK_OR_NULL: |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3647 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
| 3648 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3649 | return -EACCES; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 3650 | case PTR_TO_MAP_VALUE: |
| 3651 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { |
| 3652 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", |
| 3653 | off_reg == dst_reg ? dst : src); |
| 3654 | return -EACCES; |
| 3655 | } |
| 3656 | /* fall-through */ |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3657 | default: |
| 3658 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 3662 | * 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] | 3663 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3664 | dst_reg->type = ptr_reg->type; |
| 3665 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 3666 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3667 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 3668 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 3669 | return -EINVAL; |
| 3670 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3671 | switch (opcode) { |
| 3672 | case BPF_ADD: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3673 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 3674 | if (ret < 0) { |
| 3675 | verbose(env, "R%d tried to add from different maps or paths\n", dst); |
| 3676 | return ret; |
| 3677 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3678 | /* We can take a fixed offset as long as it doesn't overflow |
| 3679 | * the s32 'off' field |
| 3680 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3681 | if (known && (ptr_reg->off + smin_val == |
| 3682 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3683 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3684 | dst_reg->smin_value = smin_ptr; |
| 3685 | dst_reg->smax_value = smax_ptr; |
| 3686 | dst_reg->umin_value = umin_ptr; |
| 3687 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3688 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3689 | dst_reg->off = ptr_reg->off + smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3690 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3691 | break; |
| 3692 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3693 | /* A new variable offset is created. Note that off_reg->off |
| 3694 | * == 0, since it's a scalar. |
| 3695 | * dst_reg gets the pointer type and since some positive |
| 3696 | * integer value was added to the pointer, give it a new 'id' |
| 3697 | * if it's a PTR_TO_PACKET. |
| 3698 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 3699 | * added into the variable offset, and we copy the fixed offset |
| 3700 | * from ptr_reg. |
| 3701 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3702 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 3703 | signed_add_overflows(smax_ptr, smax_val)) { |
| 3704 | dst_reg->smin_value = S64_MIN; |
| 3705 | dst_reg->smax_value = S64_MAX; |
| 3706 | } else { |
| 3707 | dst_reg->smin_value = smin_ptr + smin_val; |
| 3708 | dst_reg->smax_value = smax_ptr + smax_val; |
| 3709 | } |
| 3710 | if (umin_ptr + umin_val < umin_ptr || |
| 3711 | umax_ptr + umax_val < umax_ptr) { |
| 3712 | dst_reg->umin_value = 0; |
| 3713 | dst_reg->umax_value = U64_MAX; |
| 3714 | } else { |
| 3715 | dst_reg->umin_value = umin_ptr + umin_val; |
| 3716 | dst_reg->umax_value = umax_ptr + umax_val; |
| 3717 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3718 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 3719 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3720 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3721 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3722 | dst_reg->id = ++env->id_gen; |
| 3723 | /* something was added to pkt_ptr, set range to zero */ |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3724 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3725 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3726 | break; |
| 3727 | case BPF_SUB: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3728 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 3729 | if (ret < 0) { |
| 3730 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); |
| 3731 | return ret; |
| 3732 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3733 | if (dst_reg == off_reg) { |
| 3734 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3735 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 3736 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3737 | return -EACCES; |
| 3738 | } |
| 3739 | /* We don't allow subtraction from FP, because (according to |
| 3740 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 3741 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 3742 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3743 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3744 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 3745 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3746 | return -EACCES; |
| 3747 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3748 | if (known && (ptr_reg->off - smin_val == |
| 3749 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3750 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3751 | dst_reg->smin_value = smin_ptr; |
| 3752 | dst_reg->smax_value = smax_ptr; |
| 3753 | dst_reg->umin_value = umin_ptr; |
| 3754 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3755 | dst_reg->var_off = ptr_reg->var_off; |
| 3756 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3757 | dst_reg->off = ptr_reg->off - smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3758 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3759 | break; |
| 3760 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3761 | /* A new variable offset is created. If the subtrahend is known |
| 3762 | * nonnegative, then any reg->range we had before is still good. |
| 3763 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3764 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 3765 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 3766 | /* Overflow possible, we know nothing */ |
| 3767 | dst_reg->smin_value = S64_MIN; |
| 3768 | dst_reg->smax_value = S64_MAX; |
| 3769 | } else { |
| 3770 | dst_reg->smin_value = smin_ptr - smax_val; |
| 3771 | dst_reg->smax_value = smax_ptr - smin_val; |
| 3772 | } |
| 3773 | if (umin_ptr < umax_val) { |
| 3774 | /* Overflow possible, we know nothing */ |
| 3775 | dst_reg->umin_value = 0; |
| 3776 | dst_reg->umax_value = U64_MAX; |
| 3777 | } else { |
| 3778 | /* Cannot overflow (as long as bounds are consistent) */ |
| 3779 | dst_reg->umin_value = umin_ptr - umax_val; |
| 3780 | dst_reg->umax_value = umax_ptr - umin_val; |
| 3781 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3782 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 3783 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3784 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3785 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3786 | dst_reg->id = ++env->id_gen; |
| 3787 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3788 | if (smin_val < 0) |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3789 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3790 | } |
| 3791 | break; |
| 3792 | case BPF_AND: |
| 3793 | case BPF_OR: |
| 3794 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3795 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 3796 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 3797 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3798 | return -EACCES; |
| 3799 | default: |
| 3800 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3801 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 3802 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3803 | return -EACCES; |
| 3804 | } |
| 3805 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3806 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 3807 | return -EINVAL; |
| 3808 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3809 | __update_reg_bounds(dst_reg); |
| 3810 | __reg_deduce_bounds(dst_reg); |
| 3811 | __reg_bound_offset(dst_reg); |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 3812 | |
| 3813 | /* For unprivileged we require that resulting offset must be in bounds |
| 3814 | * in order to be able to sanitize access later on. |
| 3815 | */ |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3816 | if (!env->allow_ptr_leaks) { |
| 3817 | if (dst_reg->type == PTR_TO_MAP_VALUE && |
| 3818 | check_map_access(env, dst, dst_reg->off, 1, false)) { |
| 3819 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
| 3820 | "prohibited for !root\n", dst); |
| 3821 | return -EACCES; |
| 3822 | } else if (dst_reg->type == PTR_TO_STACK && |
| 3823 | check_stack_access(env, dst_reg, dst_reg->off + |
| 3824 | dst_reg->var_off.value, 1)) { |
| 3825 | verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 3826 | "prohibited for !root\n", dst); |
| 3827 | return -EACCES; |
| 3828 | } |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 3829 | } |
| 3830 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3831 | return 0; |
| 3832 | } |
| 3833 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3834 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 3835 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 3836 | * need extra checks in the 32-bit case. |
| 3837 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3838 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 3839 | struct bpf_insn *insn, |
| 3840 | struct bpf_reg_state *dst_reg, |
| 3841 | struct bpf_reg_state src_reg) |
| 3842 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3843 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3844 | u8 opcode = BPF_OP(insn->code); |
| 3845 | bool src_known, dst_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3846 | s64 smin_val, smax_val; |
| 3847 | u64 umin_val, umax_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3848 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 3849 | u32 dst = insn->dst_reg; |
| 3850 | int ret; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3851 | |
Jann Horn | b799207 | 2018-10-05 18:17:59 +0200 | [diff] [blame] | 3852 | if (insn_bitness == 32) { |
| 3853 | /* Relevant for 32-bit RSH: Information can propagate towards |
| 3854 | * LSB, so it isn't sufficient to only truncate the output to |
| 3855 | * 32 bits. |
| 3856 | */ |
| 3857 | coerce_reg_to_size(dst_reg, 4); |
| 3858 | coerce_reg_to_size(&src_reg, 4); |
| 3859 | } |
| 3860 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3861 | smin_val = src_reg.smin_value; |
| 3862 | smax_val = src_reg.smax_value; |
| 3863 | umin_val = src_reg.umin_value; |
| 3864 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3865 | src_known = tnum_is_const(src_reg.var_off); |
| 3866 | dst_known = tnum_is_const(dst_reg->var_off); |
| 3867 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 3868 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
| 3869 | smin_val > smax_val || umin_val > umax_val) { |
| 3870 | /* Taint dst register if offset had invalid bounds derived from |
| 3871 | * e.g. dead branches. |
| 3872 | */ |
| 3873 | __mark_reg_unknown(dst_reg); |
| 3874 | return 0; |
| 3875 | } |
| 3876 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3877 | if (!src_known && |
| 3878 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
| 3879 | __mark_reg_unknown(dst_reg); |
| 3880 | return 0; |
| 3881 | } |
| 3882 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3883 | switch (opcode) { |
| 3884 | case BPF_ADD: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 3885 | ret = sanitize_val_alu(env, insn); |
| 3886 | if (ret < 0) { |
| 3887 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); |
| 3888 | return ret; |
| 3889 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3890 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 3891 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 3892 | dst_reg->smin_value = S64_MIN; |
| 3893 | dst_reg->smax_value = S64_MAX; |
| 3894 | } else { |
| 3895 | dst_reg->smin_value += smin_val; |
| 3896 | dst_reg->smax_value += smax_val; |
| 3897 | } |
| 3898 | if (dst_reg->umin_value + umin_val < umin_val || |
| 3899 | dst_reg->umax_value + umax_val < umax_val) { |
| 3900 | dst_reg->umin_value = 0; |
| 3901 | dst_reg->umax_value = U64_MAX; |
| 3902 | } else { |
| 3903 | dst_reg->umin_value += umin_val; |
| 3904 | dst_reg->umax_value += umax_val; |
| 3905 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3906 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 3907 | break; |
| 3908 | case BPF_SUB: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 3909 | ret = sanitize_val_alu(env, insn); |
| 3910 | if (ret < 0) { |
| 3911 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); |
| 3912 | return ret; |
| 3913 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3914 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 3915 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 3916 | /* Overflow possible, we know nothing */ |
| 3917 | dst_reg->smin_value = S64_MIN; |
| 3918 | dst_reg->smax_value = S64_MAX; |
| 3919 | } else { |
| 3920 | dst_reg->smin_value -= smax_val; |
| 3921 | dst_reg->smax_value -= smin_val; |
| 3922 | } |
| 3923 | if (dst_reg->umin_value < umax_val) { |
| 3924 | /* Overflow possible, we know nothing */ |
| 3925 | dst_reg->umin_value = 0; |
| 3926 | dst_reg->umax_value = U64_MAX; |
| 3927 | } else { |
| 3928 | /* Cannot overflow (as long as bounds are consistent) */ |
| 3929 | dst_reg->umin_value -= umax_val; |
| 3930 | dst_reg->umax_value -= umin_val; |
| 3931 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3932 | 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] | 3933 | break; |
| 3934 | case BPF_MUL: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3935 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 3936 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3937 | /* Ain't nobody got time to multiply that sign */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3938 | __mark_reg_unbounded(dst_reg); |
| 3939 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3940 | break; |
| 3941 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3942 | /* Both values are positive, so we can work with unsigned and |
| 3943 | * copy the result to signed (unless it exceeds S64_MAX). |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3944 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3945 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 3946 | /* Potential overflow, we know nothing */ |
| 3947 | __mark_reg_unbounded(dst_reg); |
| 3948 | /* (except what we can learn from the var_off) */ |
| 3949 | __update_reg_bounds(dst_reg); |
| 3950 | break; |
| 3951 | } |
| 3952 | dst_reg->umin_value *= umin_val; |
| 3953 | dst_reg->umax_value *= umax_val; |
| 3954 | if (dst_reg->umax_value > S64_MAX) { |
| 3955 | /* Overflow possible, we know nothing */ |
| 3956 | dst_reg->smin_value = S64_MIN; |
| 3957 | dst_reg->smax_value = S64_MAX; |
| 3958 | } else { |
| 3959 | dst_reg->smin_value = dst_reg->umin_value; |
| 3960 | dst_reg->smax_value = dst_reg->umax_value; |
| 3961 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3962 | break; |
| 3963 | case BPF_AND: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3964 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3965 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 3966 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3967 | break; |
| 3968 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3969 | /* We get our minimum from the var_off, since that's inherently |
| 3970 | * bitwise. Our maximum is the minimum of the operands' maxima. |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 3971 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3972 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3973 | dst_reg->umin_value = dst_reg->var_off.value; |
| 3974 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 3975 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 3976 | /* Lose signed bounds when ANDing negative numbers, |
| 3977 | * ain't nobody got time for that. |
| 3978 | */ |
| 3979 | dst_reg->smin_value = S64_MIN; |
| 3980 | dst_reg->smax_value = S64_MAX; |
| 3981 | } else { |
| 3982 | /* ANDing two positives gives a positive, so safe to |
| 3983 | * cast result into s64. |
| 3984 | */ |
| 3985 | dst_reg->smin_value = dst_reg->umin_value; |
| 3986 | dst_reg->smax_value = dst_reg->umax_value; |
| 3987 | } |
| 3988 | /* We may learn something more from the var_off */ |
| 3989 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3990 | break; |
| 3991 | case BPF_OR: |
| 3992 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3993 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
| 3994 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3995 | break; |
| 3996 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3997 | /* We get our maximum from the var_off, and our minimum is the |
| 3998 | * maximum of the operands' minima |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3999 | */ |
| 4000 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4001 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 4002 | dst_reg->umax_value = dst_reg->var_off.value | |
| 4003 | dst_reg->var_off.mask; |
| 4004 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 4005 | /* Lose signed bounds when ORing negative numbers, |
| 4006 | * ain't nobody got time for that. |
| 4007 | */ |
| 4008 | dst_reg->smin_value = S64_MIN; |
| 4009 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4010 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4011 | /* ORing two positives gives a positive, so safe to |
| 4012 | * cast result into s64. |
| 4013 | */ |
| 4014 | dst_reg->smin_value = dst_reg->umin_value; |
| 4015 | dst_reg->smax_value = dst_reg->umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4016 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4017 | /* We may learn something more from the var_off */ |
| 4018 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4019 | break; |
| 4020 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4021 | if (umax_val >= insn_bitness) { |
| 4022 | /* Shifts greater than 31 or 63 are undefined. |
| 4023 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4024 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4025 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4026 | break; |
| 4027 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4028 | /* We lose all sign bit information (except what we can pick |
| 4029 | * up from var_off) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4030 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4031 | dst_reg->smin_value = S64_MIN; |
| 4032 | dst_reg->smax_value = S64_MAX; |
| 4033 | /* If we might shift our top bit out, then we know nothing */ |
| 4034 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 4035 | dst_reg->umin_value = 0; |
| 4036 | dst_reg->umax_value = U64_MAX; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 4037 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4038 | dst_reg->umin_value <<= umin_val; |
| 4039 | dst_reg->umax_value <<= umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 4040 | } |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 4041 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4042 | /* We may learn something more from the var_off */ |
| 4043 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4044 | break; |
| 4045 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4046 | if (umax_val >= insn_bitness) { |
| 4047 | /* Shifts greater than 31 or 63 are undefined. |
| 4048 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4049 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4050 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4051 | break; |
| 4052 | } |
Edward Cree | 4374f25 | 2017-12-18 20:11:53 -0800 | [diff] [blame] | 4053 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 4054 | * be negative, then either: |
| 4055 | * 1) src_reg might be zero, so the sign bit of the result is |
| 4056 | * unknown, so we lose our signed bounds |
| 4057 | * 2) it's known negative, thus the unsigned bounds capture the |
| 4058 | * signed bounds |
| 4059 | * 3) the signed bounds cross zero, so they tell us nothing |
| 4060 | * about the result |
| 4061 | * If the value in dst_reg is known nonnegative, then again the |
| 4062 | * unsigned bounts capture the signed bounds. |
| 4063 | * Thus, in all cases it suffices to blow away our signed bounds |
| 4064 | * and rely on inferring new ones from the unsigned bounds and |
| 4065 | * var_off of the result. |
| 4066 | */ |
| 4067 | dst_reg->smin_value = S64_MIN; |
| 4068 | dst_reg->smax_value = S64_MAX; |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 4069 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4070 | dst_reg->umin_value >>= umax_val; |
| 4071 | dst_reg->umax_value >>= umin_val; |
| 4072 | /* We may learn something more from the var_off */ |
| 4073 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4074 | break; |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 4075 | case BPF_ARSH: |
| 4076 | if (umax_val >= insn_bitness) { |
| 4077 | /* Shifts greater than 31 or 63 are undefined. |
| 4078 | * This includes shifts by a negative number. |
| 4079 | */ |
| 4080 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 4081 | break; |
| 4082 | } |
| 4083 | |
| 4084 | /* Upon reaching here, src_known is true and |
| 4085 | * umax_val is equal to umin_val. |
| 4086 | */ |
| 4087 | dst_reg->smin_value >>= umin_val; |
| 4088 | dst_reg->smax_value >>= umin_val; |
| 4089 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val); |
| 4090 | |
| 4091 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 4092 | * dst_reg var_off to refine the result. |
| 4093 | */ |
| 4094 | dst_reg->umin_value = 0; |
| 4095 | dst_reg->umax_value = U64_MAX; |
| 4096 | __update_reg_bounds(dst_reg); |
| 4097 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4098 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4099 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4100 | break; |
| 4101 | } |
| 4102 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4103 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 4104 | /* 32-bit ALU ops are (32,32)->32 */ |
| 4105 | coerce_reg_to_size(dst_reg, 4); |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4106 | } |
| 4107 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4108 | __reg_deduce_bounds(dst_reg); |
| 4109 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4110 | return 0; |
| 4111 | } |
| 4112 | |
| 4113 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 4114 | * and var_off. |
| 4115 | */ |
| 4116 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 4117 | struct bpf_insn *insn) |
| 4118 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4119 | struct bpf_verifier_state *vstate = env->cur_state; |
| 4120 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 4121 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4122 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 4123 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4124 | |
| 4125 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4126 | src_reg = NULL; |
| 4127 | if (dst_reg->type != SCALAR_VALUE) |
| 4128 | ptr_reg = dst_reg; |
| 4129 | if (BPF_SRC(insn->code) == BPF_X) { |
| 4130 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4131 | if (src_reg->type != SCALAR_VALUE) { |
| 4132 | if (dst_reg->type != SCALAR_VALUE) { |
| 4133 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4134 | * an arbitrary scalar. Disallow all math except |
| 4135 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4136 | */ |
Alexei Starovoitov | dd06682 | 2018-09-12 14:06:10 -0700 | [diff] [blame] | 4137 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4138 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 4139 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4140 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4141 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 4142 | insn->dst_reg, |
| 4143 | bpf_alu_string[opcode >> 4]); |
| 4144 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4145 | } else { |
| 4146 | /* scalar += pointer |
| 4147 | * This is legal, but we have to reverse our |
| 4148 | * src/dest handling in computing the range |
| 4149 | */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4150 | return adjust_ptr_min_max_vals(env, insn, |
| 4151 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4152 | } |
| 4153 | } else if (ptr_reg) { |
| 4154 | /* pointer += scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4155 | return adjust_ptr_min_max_vals(env, insn, |
| 4156 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4157 | } |
| 4158 | } else { |
| 4159 | /* Pretend the src is a reg with a known value, since we only |
| 4160 | * need to be able to read from this state. |
| 4161 | */ |
| 4162 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4163 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4164 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4165 | if (ptr_reg) /* pointer += K */ |
| 4166 | return adjust_ptr_min_max_vals(env, insn, |
| 4167 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | /* Got here implies adding two SCALAR_VALUEs */ |
| 4171 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4172 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4173 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4174 | return -EINVAL; |
| 4175 | } |
| 4176 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4177 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4178 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4179 | return -EINVAL; |
| 4180 | } |
| 4181 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4182 | } |
| 4183 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4184 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4185 | 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] | 4186 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4187 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4188 | u8 opcode = BPF_OP(insn->code); |
| 4189 | int err; |
| 4190 | |
| 4191 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 4192 | if (opcode == BPF_NEG) { |
| 4193 | if (BPF_SRC(insn->code) != 0 || |
| 4194 | insn->src_reg != BPF_REG_0 || |
| 4195 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4196 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4197 | return -EINVAL; |
| 4198 | } |
| 4199 | } else { |
| 4200 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 4201 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 4202 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4203 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4204 | return -EINVAL; |
| 4205 | } |
| 4206 | } |
| 4207 | |
| 4208 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4209 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4210 | if (err) |
| 4211 | return err; |
| 4212 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4213 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4214 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4215 | insn->dst_reg); |
| 4216 | return -EACCES; |
| 4217 | } |
| 4218 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4219 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4220 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4221 | if (err) |
| 4222 | return err; |
| 4223 | |
| 4224 | } else if (opcode == BPF_MOV) { |
| 4225 | |
| 4226 | if (BPF_SRC(insn->code) == BPF_X) { |
| 4227 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4228 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4229 | return -EINVAL; |
| 4230 | } |
| 4231 | |
| 4232 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4233 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4234 | if (err) |
| 4235 | return err; |
| 4236 | } else { |
| 4237 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4238 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4239 | return -EINVAL; |
| 4240 | } |
| 4241 | } |
| 4242 | |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 4243 | /* check dest operand, mark as required later */ |
| 4244 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4245 | if (err) |
| 4246 | return err; |
| 4247 | |
| 4248 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4249 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
| 4250 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; |
| 4251 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4252 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 4253 | /* case: R1 = R2 |
| 4254 | * copy register state to dest reg |
| 4255 | */ |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4256 | *dst_reg = *src_reg; |
| 4257 | dst_reg->live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4258 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4259 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4260 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4261 | verbose(env, |
| 4262 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4263 | insn->src_reg); |
| 4264 | return -EACCES; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4265 | } else if (src_reg->type == SCALAR_VALUE) { |
| 4266 | *dst_reg = *src_reg; |
| 4267 | dst_reg->live |= REG_LIVE_WRITTEN; |
| 4268 | } else { |
| 4269 | mark_reg_unknown(env, regs, |
| 4270 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4271 | } |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4272 | coerce_reg_to_size(dst_reg, 4); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4273 | } |
| 4274 | } else { |
| 4275 | /* case: R = imm |
| 4276 | * remember the value we stored into this reg |
| 4277 | */ |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 4278 | /* clear any state __mark_reg_known doesn't set */ |
| 4279 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4280 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 4281 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 4282 | __mark_reg_known(regs + insn->dst_reg, |
| 4283 | insn->imm); |
| 4284 | } else { |
| 4285 | __mark_reg_known(regs + insn->dst_reg, |
| 4286 | (u32)insn->imm); |
| 4287 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4291 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4292 | return -EINVAL; |
| 4293 | |
| 4294 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 4295 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4296 | if (BPF_SRC(insn->code) == BPF_X) { |
| 4297 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4298 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4299 | return -EINVAL; |
| 4300 | } |
| 4301 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4302 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4303 | if (err) |
| 4304 | return err; |
| 4305 | } else { |
| 4306 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4307 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4308 | return -EINVAL; |
| 4309 | } |
| 4310 | } |
| 4311 | |
| 4312 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4313 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4314 | if (err) |
| 4315 | return err; |
| 4316 | |
| 4317 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 4318 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4319 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4320 | return -EINVAL; |
| 4321 | } |
| 4322 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 4323 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 4324 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 4325 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 4326 | |
| 4327 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4328 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 4329 | return -EINVAL; |
| 4330 | } |
| 4331 | } |
| 4332 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4333 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4334 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4335 | if (err) |
| 4336 | return err; |
| 4337 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4338 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4339 | } |
| 4340 | |
| 4341 | return 0; |
| 4342 | } |
| 4343 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 4344 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
| 4345 | struct bpf_reg_state *dst_reg, |
| 4346 | enum bpf_reg_type type, u16 new_range) |
| 4347 | { |
| 4348 | struct bpf_reg_state *reg; |
| 4349 | int i; |
| 4350 | |
| 4351 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 4352 | reg = &state->regs[i]; |
| 4353 | if (reg->type == type && reg->id == dst_reg->id) |
| 4354 | /* keep the maximum range already checked */ |
| 4355 | reg->range = max(reg->range, new_range); |
| 4356 | } |
| 4357 | |
| 4358 | bpf_for_each_spilled_reg(i, state, reg) { |
| 4359 | if (!reg) |
| 4360 | continue; |
| 4361 | if (reg->type == type && reg->id == dst_reg->id) |
| 4362 | reg->range = max(reg->range, new_range); |
| 4363 | } |
| 4364 | } |
| 4365 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4366 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4367 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 4368 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4369 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4370 | { |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4371 | u16 new_range; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 4372 | int i; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4373 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4374 | if (dst_reg->off < 0 || |
| 4375 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4376 | /* This doesn't give us any range */ |
| 4377 | return; |
| 4378 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4379 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 4380 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4381 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 4382 | * than pkt_end, but that's because it's also less than pkt. |
| 4383 | */ |
| 4384 | return; |
| 4385 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4386 | new_range = dst_reg->off; |
| 4387 | if (range_right_open) |
| 4388 | new_range--; |
| 4389 | |
| 4390 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4391 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4392 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4393 | * |
| 4394 | * r2 = r3; |
| 4395 | * r2 += 8; |
| 4396 | * if (r2 > pkt_end) goto <handle exception> |
| 4397 | * <access okay> |
| 4398 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4399 | * r2 = r3; |
| 4400 | * r2 += 8; |
| 4401 | * if (r2 < pkt_end) goto <access okay> |
| 4402 | * <handle exception> |
| 4403 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4404 | * Where: |
| 4405 | * r2 == dst_reg, pkt_end == src_reg |
| 4406 | * r2=pkt(id=n,off=8,r=0) |
| 4407 | * r3=pkt(id=n,off=0,r=0) |
| 4408 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4409 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4410 | * |
| 4411 | * r2 = r3; |
| 4412 | * r2 += 8; |
| 4413 | * if (pkt_end >= r2) goto <access okay> |
| 4414 | * <handle exception> |
| 4415 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4416 | * r2 = r3; |
| 4417 | * r2 += 8; |
| 4418 | * if (pkt_end <= r2) goto <handle exception> |
| 4419 | * <access okay> |
| 4420 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4421 | * Where: |
| 4422 | * pkt_end == dst_reg, r2 == src_reg |
| 4423 | * r2=pkt(id=n,off=8,r=0) |
| 4424 | * r3=pkt(id=n,off=0,r=0) |
| 4425 | * |
| 4426 | * 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] | 4427 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 4428 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 4429 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4430 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4431 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4432 | /* If our ids match, then we must have the same max_value. And we |
| 4433 | * don't care about the other reg's fixed offset, since if it's too big |
| 4434 | * the range won't allow anything. |
| 4435 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 4436 | */ |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 4437 | for (i = 0; i <= vstate->curframe; i++) |
| 4438 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, |
| 4439 | new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4440 | } |
| 4441 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4442 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
| 4443 | * and return: |
| 4444 | * 1 - branch will be taken and "goto target" will be executed |
| 4445 | * 0 - branch will not be taken and fall-through to next insn |
| 4446 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10] |
| 4447 | */ |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4448 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
| 4449 | bool is_jmp32) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4450 | { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4451 | struct bpf_reg_state reg_lo; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4452 | s64 sval; |
| 4453 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4454 | if (__is_pointer_value(false, reg)) |
| 4455 | return -1; |
| 4456 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4457 | if (is_jmp32) { |
| 4458 | reg_lo = *reg; |
| 4459 | reg = ®_lo; |
| 4460 | /* For JMP32, only low 32 bits are compared, coerce_reg_to_size |
| 4461 | * could truncate high bits and update umin/umax according to |
| 4462 | * information of low bits. |
| 4463 | */ |
| 4464 | coerce_reg_to_size(reg, 4); |
| 4465 | /* smin/smax need special handling. For example, after coerce, |
| 4466 | * if smin_value is 0x00000000ffffffffLL, the value is -1 when |
| 4467 | * used as operand to JMP32. It is a negative number from s32's |
| 4468 | * point of view, while it is a positive number when seen as |
| 4469 | * s64. The smin/smax are kept as s64, therefore, when used with |
| 4470 | * JMP32, they need to be transformed into s32, then sign |
| 4471 | * extended back to s64. |
| 4472 | * |
| 4473 | * Also, smin/smax were copied from umin/umax. If umin/umax has |
| 4474 | * different sign bit, then min/max relationship doesn't |
| 4475 | * maintain after casting into s32, for this case, set smin/smax |
| 4476 | * to safest range. |
| 4477 | */ |
| 4478 | if ((reg->umax_value ^ reg->umin_value) & |
| 4479 | (1ULL << 31)) { |
| 4480 | reg->smin_value = S32_MIN; |
| 4481 | reg->smax_value = S32_MAX; |
| 4482 | } |
| 4483 | reg->smin_value = (s64)(s32)reg->smin_value; |
| 4484 | reg->smax_value = (s64)(s32)reg->smax_value; |
| 4485 | |
| 4486 | val = (u32)val; |
| 4487 | sval = (s64)(s32)val; |
| 4488 | } else { |
| 4489 | sval = (s64)val; |
| 4490 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4491 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4492 | switch (opcode) { |
| 4493 | case BPF_JEQ: |
| 4494 | if (tnum_is_const(reg->var_off)) |
| 4495 | return !!tnum_equals_const(reg->var_off, val); |
| 4496 | break; |
| 4497 | case BPF_JNE: |
| 4498 | if (tnum_is_const(reg->var_off)) |
| 4499 | return !tnum_equals_const(reg->var_off, val); |
| 4500 | break; |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 4501 | case BPF_JSET: |
| 4502 | if ((~reg->var_off.mask & reg->var_off.value) & val) |
| 4503 | return 1; |
| 4504 | if (!((reg->var_off.mask | reg->var_off.value) & val)) |
| 4505 | return 0; |
| 4506 | break; |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4507 | case BPF_JGT: |
| 4508 | if (reg->umin_value > val) |
| 4509 | return 1; |
| 4510 | else if (reg->umax_value <= val) |
| 4511 | return 0; |
| 4512 | break; |
| 4513 | case BPF_JSGT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4514 | if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4515 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4516 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4517 | return 0; |
| 4518 | break; |
| 4519 | case BPF_JLT: |
| 4520 | if (reg->umax_value < val) |
| 4521 | return 1; |
| 4522 | else if (reg->umin_value >= val) |
| 4523 | return 0; |
| 4524 | break; |
| 4525 | case BPF_JSLT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4526 | if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4527 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4528 | else if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4529 | return 0; |
| 4530 | break; |
| 4531 | case BPF_JGE: |
| 4532 | if (reg->umin_value >= val) |
| 4533 | return 1; |
| 4534 | else if (reg->umax_value < val) |
| 4535 | return 0; |
| 4536 | break; |
| 4537 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4538 | if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4539 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4540 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4541 | return 0; |
| 4542 | break; |
| 4543 | case BPF_JLE: |
| 4544 | if (reg->umax_value <= val) |
| 4545 | return 1; |
| 4546 | else if (reg->umin_value > val) |
| 4547 | return 0; |
| 4548 | break; |
| 4549 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4550 | if (reg->smax_value <= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4551 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4552 | else if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4553 | return 0; |
| 4554 | break; |
| 4555 | } |
| 4556 | |
| 4557 | return -1; |
| 4558 | } |
| 4559 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4560 | /* Generate min value of the high 32-bit from TNUM info. */ |
| 4561 | static u64 gen_hi_min(struct tnum var) |
| 4562 | { |
| 4563 | return var.value & ~0xffffffffULL; |
| 4564 | } |
| 4565 | |
| 4566 | /* Generate max value of the high 32-bit from TNUM info. */ |
| 4567 | static u64 gen_hi_max(struct tnum var) |
| 4568 | { |
| 4569 | return (var.value | var.mask) & ~0xffffffffULL; |
| 4570 | } |
| 4571 | |
| 4572 | /* Return true if VAL is compared with a s64 sign extended from s32, and they |
| 4573 | * are with the same signedness. |
| 4574 | */ |
| 4575 | static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg) |
| 4576 | { |
| 4577 | return ((s32)sval >= 0 && |
| 4578 | reg->smin_value >= 0 && reg->smax_value <= S32_MAX) || |
| 4579 | ((s32)sval < 0 && |
| 4580 | reg->smax_value <= 0 && reg->smin_value >= S32_MIN); |
| 4581 | } |
| 4582 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4583 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 4584 | * variable register that we are working on, and src_reg is a constant or we're |
| 4585 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4586 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4587 | */ |
| 4588 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 4589 | struct bpf_reg_state *false_reg, u64 val, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4590 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4591 | { |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4592 | s64 sval; |
| 4593 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4594 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 4595 | * variable offset from the compare (unless src_reg were a pointer into |
| 4596 | * the same object, but we don't bother with that. |
| 4597 | * Since false_reg and true_reg have the same type by construction, we |
| 4598 | * only need to check one of them for pointerness. |
| 4599 | */ |
| 4600 | if (__is_pointer_value(false, false_reg)) |
| 4601 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 4602 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4603 | val = is_jmp32 ? (u32)val : val; |
| 4604 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4605 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4606 | switch (opcode) { |
| 4607 | case BPF_JEQ: |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4608 | case BPF_JNE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4609 | { |
| 4610 | struct bpf_reg_state *reg = |
| 4611 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 4612 | |
| 4613 | /* For BPF_JEQ, if this is false we know nothing Jon Snow, but |
| 4614 | * if it is true we know the value for sure. Likewise for |
| 4615 | * BPF_JNE. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4616 | */ |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4617 | if (is_jmp32) { |
| 4618 | u64 old_v = reg->var_off.value; |
| 4619 | u64 hi_mask = ~0xffffffffULL; |
| 4620 | |
| 4621 | reg->var_off.value = (old_v & hi_mask) | val; |
| 4622 | reg->var_off.mask &= hi_mask; |
| 4623 | } else { |
| 4624 | __mark_reg_known(reg, val); |
| 4625 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4626 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4627 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 4628 | case BPF_JSET: |
| 4629 | false_reg->var_off = tnum_and(false_reg->var_off, |
| 4630 | tnum_const(~val)); |
| 4631 | if (is_power_of_2(val)) |
| 4632 | true_reg->var_off = tnum_or(true_reg->var_off, |
| 4633 | tnum_const(val)); |
| 4634 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4635 | case BPF_JGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4636 | case BPF_JGT: |
| 4637 | { |
| 4638 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; |
| 4639 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; |
| 4640 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4641 | if (is_jmp32) { |
| 4642 | false_umax += gen_hi_max(false_reg->var_off); |
| 4643 | true_umin += gen_hi_min(true_reg->var_off); |
| 4644 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4645 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 4646 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4647 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4648 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4649 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4650 | case BPF_JSGT: |
| 4651 | { |
| 4652 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; |
| 4653 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; |
| 4654 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4655 | /* If the full s64 was not sign-extended from s32 then don't |
| 4656 | * deduct further info. |
| 4657 | */ |
| 4658 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 4659 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4660 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 4661 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4662 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4663 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4664 | case BPF_JLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4665 | case BPF_JLT: |
| 4666 | { |
| 4667 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; |
| 4668 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; |
| 4669 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4670 | if (is_jmp32) { |
| 4671 | false_umin += gen_hi_min(false_reg->var_off); |
| 4672 | true_umax += gen_hi_max(true_reg->var_off); |
| 4673 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4674 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 4675 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4676 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4677 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4678 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4679 | case BPF_JSLT: |
| 4680 | { |
| 4681 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; |
| 4682 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; |
| 4683 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4684 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 4685 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4686 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 4687 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4688 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4689 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4690 | default: |
| 4691 | break; |
| 4692 | } |
| 4693 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4694 | __reg_deduce_bounds(false_reg); |
| 4695 | __reg_deduce_bounds(true_reg); |
| 4696 | /* We might have learned some bits from the bounds. */ |
| 4697 | __reg_bound_offset(false_reg); |
| 4698 | __reg_bound_offset(true_reg); |
| 4699 | /* Intersecting with the old var_off might have improved our bounds |
| 4700 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 4701 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 4702 | */ |
| 4703 | __update_reg_bounds(false_reg); |
| 4704 | __update_reg_bounds(true_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4705 | } |
| 4706 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4707 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 4708 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4709 | */ |
| 4710 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 4711 | struct bpf_reg_state *false_reg, u64 val, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4712 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4713 | { |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4714 | s64 sval; |
| 4715 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4716 | if (__is_pointer_value(false, false_reg)) |
| 4717 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 4718 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4719 | val = is_jmp32 ? (u32)val : val; |
| 4720 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4721 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4722 | switch (opcode) { |
| 4723 | case BPF_JEQ: |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4724 | case BPF_JNE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4725 | { |
| 4726 | struct bpf_reg_state *reg = |
| 4727 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 4728 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4729 | if (is_jmp32) { |
| 4730 | u64 old_v = reg->var_off.value; |
| 4731 | u64 hi_mask = ~0xffffffffULL; |
| 4732 | |
| 4733 | reg->var_off.value = (old_v & hi_mask) | val; |
| 4734 | reg->var_off.mask &= hi_mask; |
| 4735 | } else { |
| 4736 | __mark_reg_known(reg, val); |
| 4737 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4738 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4739 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 4740 | case BPF_JSET: |
| 4741 | false_reg->var_off = tnum_and(false_reg->var_off, |
| 4742 | tnum_const(~val)); |
| 4743 | if (is_power_of_2(val)) |
| 4744 | true_reg->var_off = tnum_or(true_reg->var_off, |
| 4745 | tnum_const(val)); |
| 4746 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4747 | case BPF_JGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4748 | case BPF_JGT: |
| 4749 | { |
| 4750 | u64 false_umin = opcode == BPF_JGT ? val : val + 1; |
| 4751 | u64 true_umax = opcode == BPF_JGT ? val - 1 : val; |
| 4752 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4753 | if (is_jmp32) { |
| 4754 | false_umin += gen_hi_min(false_reg->var_off); |
| 4755 | true_umax += gen_hi_max(true_reg->var_off); |
| 4756 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4757 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 4758 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4759 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4760 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4761 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4762 | case BPF_JSGT: |
| 4763 | { |
| 4764 | s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1; |
| 4765 | s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval; |
| 4766 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4767 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 4768 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4769 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 4770 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4771 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4772 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4773 | case BPF_JLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4774 | case BPF_JLT: |
| 4775 | { |
| 4776 | u64 false_umax = opcode == BPF_JLT ? val : val - 1; |
| 4777 | u64 true_umin = opcode == BPF_JLT ? val + 1 : val; |
| 4778 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4779 | if (is_jmp32) { |
| 4780 | false_umax += gen_hi_max(false_reg->var_off); |
| 4781 | true_umin += gen_hi_min(true_reg->var_off); |
| 4782 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4783 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 4784 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4785 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4786 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4787 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4788 | case BPF_JSLT: |
| 4789 | { |
| 4790 | s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1; |
| 4791 | s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval; |
| 4792 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4793 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 4794 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4795 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 4796 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4797 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4798 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4799 | default: |
| 4800 | break; |
| 4801 | } |
| 4802 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4803 | __reg_deduce_bounds(false_reg); |
| 4804 | __reg_deduce_bounds(true_reg); |
| 4805 | /* We might have learned some bits from the bounds. */ |
| 4806 | __reg_bound_offset(false_reg); |
| 4807 | __reg_bound_offset(true_reg); |
| 4808 | /* Intersecting with the old var_off might have improved our bounds |
| 4809 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 4810 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 4811 | */ |
| 4812 | __update_reg_bounds(false_reg); |
| 4813 | __update_reg_bounds(true_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4814 | } |
| 4815 | |
| 4816 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 4817 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 4818 | struct bpf_reg_state *dst_reg) |
| 4819 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4820 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 4821 | dst_reg->umin_value); |
| 4822 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 4823 | dst_reg->umax_value); |
| 4824 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 4825 | dst_reg->smin_value); |
| 4826 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 4827 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4828 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 4829 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4830 | /* We might have learned new bounds from the var_off. */ |
| 4831 | __update_reg_bounds(src_reg); |
| 4832 | __update_reg_bounds(dst_reg); |
| 4833 | /* We might have learned something about the sign bit. */ |
| 4834 | __reg_deduce_bounds(src_reg); |
| 4835 | __reg_deduce_bounds(dst_reg); |
| 4836 | /* We might have learned some bits from the bounds. */ |
| 4837 | __reg_bound_offset(src_reg); |
| 4838 | __reg_bound_offset(dst_reg); |
| 4839 | /* Intersecting with the old var_off might have improved our bounds |
| 4840 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 4841 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 4842 | */ |
| 4843 | __update_reg_bounds(src_reg); |
| 4844 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4845 | } |
| 4846 | |
| 4847 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 4848 | struct bpf_reg_state *true_dst, |
| 4849 | struct bpf_reg_state *false_src, |
| 4850 | struct bpf_reg_state *false_dst, |
| 4851 | u8 opcode) |
| 4852 | { |
| 4853 | switch (opcode) { |
| 4854 | case BPF_JEQ: |
| 4855 | __reg_combine_min_max(true_src, true_dst); |
| 4856 | break; |
| 4857 | case BPF_JNE: |
| 4858 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4859 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 4860 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4861 | } |
| 4862 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4863 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 4864 | struct bpf_reg_state *reg, u32 id, |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4865 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4866 | { |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4867 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4868 | /* Old offset (both fixed and variable parts) should |
| 4869 | * have been known-zero, because we don't allow pointer |
| 4870 | * arithmetic on pointers that might be NULL. |
| 4871 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4872 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 4873 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4874 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4875 | __mark_reg_known_zero(reg); |
| 4876 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4877 | } |
| 4878 | if (is_null) { |
| 4879 | reg->type = SCALAR_VALUE; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4880 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
| 4881 | if (reg->map_ptr->inner_map_meta) { |
| 4882 | reg->type = CONST_PTR_TO_MAP; |
| 4883 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
| 4884 | } else { |
| 4885 | reg->type = PTR_TO_MAP_VALUE; |
| 4886 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 4887 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
| 4888 | reg->type = PTR_TO_SOCKET; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4889 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
| 4890 | reg->type = PTR_TO_SOCK_COMMON; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 4891 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
| 4892 | reg->type = PTR_TO_TCP_SOCK; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4893 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4894 | if (is_null) { |
| 4895 | /* We don't need id and ref_obj_id from this point |
| 4896 | * onwards anymore, thus we should better reset it, |
| 4897 | * so that state pruning has chances to take effect. |
| 4898 | */ |
| 4899 | reg->id = 0; |
| 4900 | reg->ref_obj_id = 0; |
| 4901 | } else if (!reg_may_point_to_spin_lock(reg)) { |
| 4902 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
| 4903 | * in release_reg_references(). |
| 4904 | * |
| 4905 | * reg->id is still used by spin_lock ptr. Other |
| 4906 | * than spin_lock ptr type, reg->id can be reset. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4907 | */ |
| 4908 | reg->id = 0; |
| 4909 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4910 | } |
| 4911 | } |
| 4912 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 4913 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
| 4914 | bool is_null) |
| 4915 | { |
| 4916 | struct bpf_reg_state *reg; |
| 4917 | int i; |
| 4918 | |
| 4919 | for (i = 0; i < MAX_BPF_REG; i++) |
| 4920 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); |
| 4921 | |
| 4922 | bpf_for_each_spilled_reg(i, state, reg) { |
| 4923 | if (!reg) |
| 4924 | continue; |
| 4925 | mark_ptr_or_null_reg(state, reg, id, is_null); |
| 4926 | } |
| 4927 | } |
| 4928 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4929 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 4930 | * be folded together at some point. |
| 4931 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4932 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 4933 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4934 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4935 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 4936 | struct bpf_reg_state *regs = state->regs; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4937 | u32 ref_obj_id = regs[regno].ref_obj_id; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 4938 | u32 id = regs[regno].id; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 4939 | int i; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4940 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 4941 | if (ref_obj_id && ref_obj_id == id && is_null) |
| 4942 | /* regs[regno] is in the " == NULL" branch. |
| 4943 | * No one could have freed the reference state before |
| 4944 | * doing the NULL check. |
| 4945 | */ |
| 4946 | WARN_ON_ONCE(release_reference_state(state, id)); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4947 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 4948 | for (i = 0; i <= vstate->curframe; i++) |
| 4949 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4950 | } |
| 4951 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 4952 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 4953 | struct bpf_reg_state *dst_reg, |
| 4954 | struct bpf_reg_state *src_reg, |
| 4955 | struct bpf_verifier_state *this_branch, |
| 4956 | struct bpf_verifier_state *other_branch) |
| 4957 | { |
| 4958 | if (BPF_SRC(insn->code) != BPF_X) |
| 4959 | return false; |
| 4960 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4961 | /* Pointers are always 64-bit. */ |
| 4962 | if (BPF_CLASS(insn->code) == BPF_JMP32) |
| 4963 | return false; |
| 4964 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 4965 | switch (BPF_OP(insn->code)) { |
| 4966 | case BPF_JGT: |
| 4967 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4968 | src_reg->type == PTR_TO_PACKET_END) || |
| 4969 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4970 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4971 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 4972 | find_good_pkt_pointers(this_branch, dst_reg, |
| 4973 | dst_reg->type, false); |
| 4974 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4975 | src_reg->type == PTR_TO_PACKET) || |
| 4976 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4977 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4978 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 4979 | find_good_pkt_pointers(other_branch, src_reg, |
| 4980 | src_reg->type, true); |
| 4981 | } else { |
| 4982 | return false; |
| 4983 | } |
| 4984 | break; |
| 4985 | case BPF_JLT: |
| 4986 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4987 | src_reg->type == PTR_TO_PACKET_END) || |
| 4988 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4989 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4990 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 4991 | find_good_pkt_pointers(other_branch, dst_reg, |
| 4992 | dst_reg->type, true); |
| 4993 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4994 | src_reg->type == PTR_TO_PACKET) || |
| 4995 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4996 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4997 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 4998 | find_good_pkt_pointers(this_branch, src_reg, |
| 4999 | src_reg->type, false); |
| 5000 | } else { |
| 5001 | return false; |
| 5002 | } |
| 5003 | break; |
| 5004 | case BPF_JGE: |
| 5005 | if ((dst_reg->type == PTR_TO_PACKET && |
| 5006 | src_reg->type == PTR_TO_PACKET_END) || |
| 5007 | (dst_reg->type == PTR_TO_PACKET_META && |
| 5008 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 5009 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 5010 | find_good_pkt_pointers(this_branch, dst_reg, |
| 5011 | dst_reg->type, true); |
| 5012 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 5013 | src_reg->type == PTR_TO_PACKET) || |
| 5014 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 5015 | src_reg->type == PTR_TO_PACKET_META)) { |
| 5016 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 5017 | find_good_pkt_pointers(other_branch, src_reg, |
| 5018 | src_reg->type, false); |
| 5019 | } else { |
| 5020 | return false; |
| 5021 | } |
| 5022 | break; |
| 5023 | case BPF_JLE: |
| 5024 | if ((dst_reg->type == PTR_TO_PACKET && |
| 5025 | src_reg->type == PTR_TO_PACKET_END) || |
| 5026 | (dst_reg->type == PTR_TO_PACKET_META && |
| 5027 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 5028 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 5029 | find_good_pkt_pointers(other_branch, dst_reg, |
| 5030 | dst_reg->type, false); |
| 5031 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 5032 | src_reg->type == PTR_TO_PACKET) || |
| 5033 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 5034 | src_reg->type == PTR_TO_PACKET_META)) { |
| 5035 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 5036 | find_good_pkt_pointers(this_branch, src_reg, |
| 5037 | src_reg->type, true); |
| 5038 | } else { |
| 5039 | return false; |
| 5040 | } |
| 5041 | break; |
| 5042 | default: |
| 5043 | return false; |
| 5044 | } |
| 5045 | |
| 5046 | return true; |
| 5047 | } |
| 5048 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5049 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5050 | struct bpf_insn *insn, int *insn_idx) |
| 5051 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5052 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 5053 | struct bpf_verifier_state *other_branch; |
| 5054 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
| 5055 | struct bpf_reg_state *dst_reg, *other_branch_regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5056 | u8 opcode = BPF_OP(insn->code); |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5057 | bool is_jmp32; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5058 | int err; |
| 5059 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5060 | /* Only conditional jumps are expected to reach here. */ |
| 5061 | if (opcode == BPF_JA || opcode > BPF_JSLE) { |
| 5062 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5063 | return -EINVAL; |
| 5064 | } |
| 5065 | |
| 5066 | if (BPF_SRC(insn->code) == BPF_X) { |
| 5067 | if (insn->imm != 0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5068 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5069 | return -EINVAL; |
| 5070 | } |
| 5071 | |
| 5072 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5073 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5074 | if (err) |
| 5075 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5076 | |
| 5077 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5078 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5079 | insn->src_reg); |
| 5080 | return -EACCES; |
| 5081 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5082 | } else { |
| 5083 | if (insn->src_reg != BPF_REG_0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5084 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5085 | return -EINVAL; |
| 5086 | } |
| 5087 | } |
| 5088 | |
| 5089 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5090 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5091 | if (err) |
| 5092 | return err; |
| 5093 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 5094 | dst_reg = ®s[insn->dst_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5095 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 5096 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5097 | if (BPF_SRC(insn->code) == BPF_K) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5098 | int pred = is_branch_taken(dst_reg, insn->imm, opcode, |
| 5099 | is_jmp32); |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5100 | |
| 5101 | if (pred == 1) { |
| 5102 | /* only follow the goto, ignore fall-through */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5103 | *insn_idx += insn->off; |
| 5104 | return 0; |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5105 | } else if (pred == 0) { |
| 5106 | /* only follow fall-through branch, since |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5107 | * that's where the program will go |
| 5108 | */ |
| 5109 | return 0; |
| 5110 | } |
| 5111 | } |
| 5112 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5113 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
| 5114 | false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5115 | if (!other_branch) |
| 5116 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5117 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5118 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5119 | /* detect if we are comparing against a constant value so we can adjust |
| 5120 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5121 | * this is only legit if both are scalars (or pointers to the same |
| 5122 | * object, I suppose, but we don't support that right now), because |
| 5123 | * otherwise the different base pointers mean the offsets aren't |
| 5124 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5125 | */ |
| 5126 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5127 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
| 5128 | struct bpf_reg_state lo_reg0 = *dst_reg; |
| 5129 | struct bpf_reg_state lo_reg1 = *src_reg; |
| 5130 | struct bpf_reg_state *src_lo, *dst_lo; |
| 5131 | |
| 5132 | dst_lo = &lo_reg0; |
| 5133 | src_lo = &lo_reg1; |
| 5134 | coerce_reg_to_size(dst_lo, 4); |
| 5135 | coerce_reg_to_size(src_lo, 4); |
| 5136 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5137 | if (dst_reg->type == SCALAR_VALUE && |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5138 | src_reg->type == SCALAR_VALUE) { |
| 5139 | if (tnum_is_const(src_reg->var_off) || |
| 5140 | (is_jmp32 && tnum_is_const(src_lo->var_off))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5141 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5142 | dst_reg, |
| 5143 | is_jmp32 |
| 5144 | ? src_lo->var_off.value |
| 5145 | : src_reg->var_off.value, |
| 5146 | opcode, is_jmp32); |
| 5147 | else if (tnum_is_const(dst_reg->var_off) || |
| 5148 | (is_jmp32 && tnum_is_const(dst_lo->var_off))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5149 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5150 | src_reg, |
| 5151 | is_jmp32 |
| 5152 | ? dst_lo->var_off.value |
| 5153 | : dst_reg->var_off.value, |
| 5154 | opcode, is_jmp32); |
| 5155 | else if (!is_jmp32 && |
| 5156 | (opcode == BPF_JEQ || opcode == BPF_JNE)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5157 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5158 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 5159 | &other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5160 | src_reg, dst_reg, opcode); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5161 | } |
| 5162 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5163 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5164 | dst_reg, insn->imm, opcode, is_jmp32); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5165 | } |
| 5166 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5167 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
| 5168 | * NOTE: these optimizations below are related with pointer comparison |
| 5169 | * which will never be JMP32. |
| 5170 | */ |
| 5171 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 5172 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5173 | reg_type_may_be_null(dst_reg->type)) { |
| 5174 | /* Mark all identical registers in each branch as either |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5175 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 5176 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5177 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 5178 | opcode == BPF_JNE); |
| 5179 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 5180 | opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 5181 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 5182 | this_branch, other_branch) && |
| 5183 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5184 | verbose(env, "R%d pointer comparison prohibited\n", |
| 5185 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5186 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5187 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 5188 | if (env->log.level & BPF_LOG_LEVEL) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5189 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5190 | return 0; |
| 5191 | } |
| 5192 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5193 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5194 | 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] | 5195 | { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 5196 | struct bpf_insn_aux_data *aux = cur_aux(env); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5197 | struct bpf_reg_state *regs = cur_regs(env); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 5198 | struct bpf_map *map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5199 | int err; |
| 5200 | |
| 5201 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5202 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5203 | return -EINVAL; |
| 5204 | } |
| 5205 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5206 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5207 | return -EINVAL; |
| 5208 | } |
| 5209 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5210 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5211 | if (err) |
| 5212 | return err; |
| 5213 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 5214 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 5215 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 5216 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5217 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5218 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5219 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 5220 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5221 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 5222 | map = env->used_maps[aux->map_index]; |
| 5223 | mark_reg_known_zero(env, regs, insn->dst_reg); |
| 5224 | regs[insn->dst_reg].map_ptr = map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5225 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 5226 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { |
| 5227 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; |
| 5228 | regs[insn->dst_reg].off = aux->map_off; |
| 5229 | if (map_value_has_spin_lock(map)) |
| 5230 | regs[insn->dst_reg].id = ++env->id_gen; |
| 5231 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 5232 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 5233 | } else { |
| 5234 | verbose(env, "bpf verifier is misconfigured\n"); |
| 5235 | return -EINVAL; |
| 5236 | } |
| 5237 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5238 | return 0; |
| 5239 | } |
| 5240 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 5241 | static bool may_access_skb(enum bpf_prog_type type) |
| 5242 | { |
| 5243 | switch (type) { |
| 5244 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 5245 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 5246 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 5247 | return true; |
| 5248 | default: |
| 5249 | return false; |
| 5250 | } |
| 5251 | } |
| 5252 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5253 | /* verify safety of LD_ABS|LD_IND instructions: |
| 5254 | * - they can only appear in the programs where ctx == skb |
| 5255 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 5256 | * preserve R6-R9, and store return value into R0 |
| 5257 | * |
| 5258 | * Implicit input: |
| 5259 | * ctx == skb == R6 == CTX |
| 5260 | * |
| 5261 | * Explicit input: |
| 5262 | * SRC == any register |
| 5263 | * IMM == 32-bit immediate |
| 5264 | * |
| 5265 | * Output: |
| 5266 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 5267 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5268 | 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] | 5269 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5270 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5271 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5272 | int i, err; |
| 5273 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 5274 | if (!may_access_skb(env->prog->type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5275 | 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] | 5276 | return -EINVAL; |
| 5277 | } |
| 5278 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 5279 | if (!env->ops->gen_ld_abs) { |
| 5280 | verbose(env, "bpf verifier is misconfigured\n"); |
| 5281 | return -EINVAL; |
| 5282 | } |
| 5283 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5284 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5285 | /* when program has LD_ABS insn JITs and interpreter assume |
| 5286 | * that r1 == ctx == skb which is not the case for callees |
| 5287 | * that can have arbitrary arguments. It's problematic |
| 5288 | * for main prog as well since JITs would need to analyze |
| 5289 | * all functions in order to make proper register save/restore |
| 5290 | * decisions in the main prog. Hence disallow LD_ABS with calls |
| 5291 | */ |
| 5292 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); |
| 5293 | return -EINVAL; |
| 5294 | } |
| 5295 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5296 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 5297 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5298 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5299 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5300 | return -EINVAL; |
| 5301 | } |
| 5302 | |
| 5303 | /* check whether implicit source operand (register R6) is readable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5304 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5305 | if (err) |
| 5306 | return err; |
| 5307 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5308 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 5309 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 5310 | * reference leak. |
| 5311 | */ |
| 5312 | err = check_reference_leak(env); |
| 5313 | if (err) { |
| 5314 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 5315 | return err; |
| 5316 | } |
| 5317 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 5318 | if (env->cur_state->active_spin_lock) { |
| 5319 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
| 5320 | return -EINVAL; |
| 5321 | } |
| 5322 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5323 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5324 | verbose(env, |
| 5325 | "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] | 5326 | return -EINVAL; |
| 5327 | } |
| 5328 | |
| 5329 | if (mode == BPF_IND) { |
| 5330 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5331 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5332 | if (err) |
| 5333 | return err; |
| 5334 | } |
| 5335 | |
| 5336 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5337 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5338 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5339 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 5340 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5341 | |
| 5342 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5343 | * the value fetched from the packet. |
| 5344 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5345 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5346 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5347 | return 0; |
| 5348 | } |
| 5349 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5350 | static int check_return_code(struct bpf_verifier_env *env) |
| 5351 | { |
| 5352 | struct bpf_reg_state *reg; |
| 5353 | struct tnum range = tnum_range(0, 1); |
| 5354 | |
| 5355 | switch (env->prog->type) { |
| 5356 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 5357 | case BPF_PROG_TYPE_CGROUP_SOCK: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 5358 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5359 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 5360 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 5361 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5362 | break; |
| 5363 | default: |
| 5364 | return 0; |
| 5365 | } |
| 5366 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5367 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5368 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5369 | 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] | 5370 | reg_type_str[reg->type]); |
| 5371 | return -EINVAL; |
| 5372 | } |
| 5373 | |
| 5374 | if (!tnum_in(range, reg->var_off)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5375 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5376 | if (!tnum_is_unknown(reg->var_off)) { |
| 5377 | char tn_buf[48]; |
| 5378 | |
| 5379 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5380 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5381 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5382 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5383 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5384 | verbose(env, " should have been 0 or 1\n"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5385 | return -EINVAL; |
| 5386 | } |
| 5387 | return 0; |
| 5388 | } |
| 5389 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5390 | /* non-recursive DFS pseudo code |
| 5391 | * 1 procedure DFS-iterative(G,v): |
| 5392 | * 2 label v as discovered |
| 5393 | * 3 let S be a stack |
| 5394 | * 4 S.push(v) |
| 5395 | * 5 while S is not empty |
| 5396 | * 6 t <- S.pop() |
| 5397 | * 7 if t is what we're looking for: |
| 5398 | * 8 return t |
| 5399 | * 9 for all edges e in G.adjacentEdges(t) do |
| 5400 | * 10 if edge e is already labelled |
| 5401 | * 11 continue with the next edge |
| 5402 | * 12 w <- G.adjacentVertex(t,e) |
| 5403 | * 13 if vertex w is not discovered and not explored |
| 5404 | * 14 label e as tree-edge |
| 5405 | * 15 label w as discovered |
| 5406 | * 16 S.push(w) |
| 5407 | * 17 continue at 5 |
| 5408 | * 18 else if vertex w is discovered |
| 5409 | * 19 label e as back-edge |
| 5410 | * 20 else |
| 5411 | * 21 // vertex w is explored |
| 5412 | * 22 label e as forward- or cross-edge |
| 5413 | * 23 label t as explored |
| 5414 | * 24 S.pop() |
| 5415 | * |
| 5416 | * convention: |
| 5417 | * 0x10 - discovered |
| 5418 | * 0x11 - discovered and fall-through edge labelled |
| 5419 | * 0x12 - discovered and fall-through and branch edges labelled |
| 5420 | * 0x20 - explored |
| 5421 | */ |
| 5422 | |
| 5423 | enum { |
| 5424 | DISCOVERED = 0x10, |
| 5425 | EXPLORED = 0x20, |
| 5426 | FALLTHROUGH = 1, |
| 5427 | BRANCH = 2, |
| 5428 | }; |
| 5429 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5430 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5431 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5432 | /* t, w, e - match pseudo-code above: |
| 5433 | * t - index of current instruction |
| 5434 | * w - next instruction |
| 5435 | * e - edge |
| 5436 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5437 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5438 | { |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5439 | int *insn_stack = env->cfg.insn_stack; |
| 5440 | int *insn_state = env->cfg.insn_state; |
| 5441 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5442 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 5443 | return 0; |
| 5444 | |
| 5445 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 5446 | return 0; |
| 5447 | |
| 5448 | if (w < 0 || w >= env->prog->len) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 5449 | verbose_linfo(env, t, "%d: ", t); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5450 | 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] | 5451 | return -EINVAL; |
| 5452 | } |
| 5453 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5454 | if (e == BRANCH) |
| 5455 | /* mark branch target for state pruning */ |
| 5456 | env->explored_states[w] = STATE_LIST_MARK; |
| 5457 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5458 | if (insn_state[w] == 0) { |
| 5459 | /* tree-edge */ |
| 5460 | insn_state[t] = DISCOVERED | e; |
| 5461 | insn_state[w] = DISCOVERED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5462 | if (env->cfg.cur_stack >= env->prog->len) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5463 | return -E2BIG; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5464 | insn_stack[env->cfg.cur_stack++] = w; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5465 | return 1; |
| 5466 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 5467 | verbose_linfo(env, t, "%d: ", t); |
| 5468 | verbose_linfo(env, w, "%d: ", w); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5469 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5470 | return -EINVAL; |
| 5471 | } else if (insn_state[w] == EXPLORED) { |
| 5472 | /* forward- or cross-edge */ |
| 5473 | insn_state[t] = DISCOVERED | e; |
| 5474 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5475 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5476 | return -EFAULT; |
| 5477 | } |
| 5478 | return 0; |
| 5479 | } |
| 5480 | |
| 5481 | /* non-recursive depth-first-search to detect loops in BPF program |
| 5482 | * loop == back-edge in directed graph |
| 5483 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5484 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5485 | { |
| 5486 | struct bpf_insn *insns = env->prog->insnsi; |
| 5487 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5488 | int *insn_stack, *insn_state; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5489 | int ret = 0; |
| 5490 | int i, t; |
| 5491 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5492 | 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] | 5493 | if (!insn_state) |
| 5494 | return -ENOMEM; |
| 5495 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5496 | 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] | 5497 | if (!insn_stack) { |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 5498 | kvfree(insn_state); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5499 | return -ENOMEM; |
| 5500 | } |
| 5501 | |
| 5502 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 5503 | insn_stack[0] = 0; /* 0 is the first instruction */ |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5504 | env->cfg.cur_stack = 1; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5505 | |
| 5506 | peek_stack: |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5507 | if (env->cfg.cur_stack == 0) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5508 | goto check_state; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5509 | t = insn_stack[env->cfg.cur_stack - 1]; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5510 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5511 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
| 5512 | BPF_CLASS(insns[t].code) == BPF_JMP32) { |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5513 | u8 opcode = BPF_OP(insns[t].code); |
| 5514 | |
| 5515 | if (opcode == BPF_EXIT) { |
| 5516 | goto mark_explored; |
| 5517 | } else if (opcode == BPF_CALL) { |
| 5518 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 5519 | if (ret == 1) |
| 5520 | goto peek_stack; |
| 5521 | else if (ret < 0) |
| 5522 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 5523 | if (t + 1 < insn_cnt) |
| 5524 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5525 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
| 5526 | env->explored_states[t] = STATE_LIST_MARK; |
| 5527 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); |
| 5528 | if (ret == 1) |
| 5529 | goto peek_stack; |
| 5530 | else if (ret < 0) |
| 5531 | goto err_free; |
| 5532 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5533 | } else if (opcode == BPF_JA) { |
| 5534 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 5535 | ret = -EINVAL; |
| 5536 | goto err_free; |
| 5537 | } |
| 5538 | /* unconditional jump with single edge */ |
| 5539 | ret = push_insn(t, t + insns[t].off + 1, |
| 5540 | FALLTHROUGH, env); |
| 5541 | if (ret == 1) |
| 5542 | goto peek_stack; |
| 5543 | else if (ret < 0) |
| 5544 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5545 | /* tell verifier to check for equivalent states |
| 5546 | * after every call and jump |
| 5547 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 5548 | if (t + 1 < insn_cnt) |
| 5549 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5550 | } else { |
| 5551 | /* conditional jump with two edges */ |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 5552 | env->explored_states[t] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5553 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 5554 | if (ret == 1) |
| 5555 | goto peek_stack; |
| 5556 | else if (ret < 0) |
| 5557 | goto err_free; |
| 5558 | |
| 5559 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 5560 | if (ret == 1) |
| 5561 | goto peek_stack; |
| 5562 | else if (ret < 0) |
| 5563 | goto err_free; |
| 5564 | } |
| 5565 | } else { |
| 5566 | /* all other non-branch instructions with single |
| 5567 | * fall-through edge |
| 5568 | */ |
| 5569 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 5570 | if (ret == 1) |
| 5571 | goto peek_stack; |
| 5572 | else if (ret < 0) |
| 5573 | goto err_free; |
| 5574 | } |
| 5575 | |
| 5576 | mark_explored: |
| 5577 | insn_state[t] = EXPLORED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5578 | if (env->cfg.cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5579 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5580 | ret = -EFAULT; |
| 5581 | goto err_free; |
| 5582 | } |
| 5583 | goto peek_stack; |
| 5584 | |
| 5585 | check_state: |
| 5586 | for (i = 0; i < insn_cnt; i++) { |
| 5587 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5588 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5589 | ret = -EINVAL; |
| 5590 | goto err_free; |
| 5591 | } |
| 5592 | } |
| 5593 | ret = 0; /* cfg looks good */ |
| 5594 | |
| 5595 | err_free: |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 5596 | kvfree(insn_state); |
| 5597 | kvfree(insn_stack); |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5598 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5599 | return ret; |
| 5600 | } |
| 5601 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5602 | /* The minimum supported BTF func info size */ |
| 5603 | #define MIN_BPF_FUNCINFO_SIZE 8 |
| 5604 | #define MAX_FUNCINFO_REC_SIZE 252 |
| 5605 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5606 | static int check_btf_func(struct bpf_verifier_env *env, |
| 5607 | const union bpf_attr *attr, |
| 5608 | union bpf_attr __user *uattr) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5609 | { |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 5610 | u32 i, nfuncs, urec_size, min_size; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5611 | u32 krec_size = sizeof(struct bpf_func_info); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5612 | struct bpf_func_info *krecord; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5613 | const struct btf_type *type; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5614 | struct bpf_prog *prog; |
| 5615 | const struct btf *btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5616 | void __user *urecord; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 5617 | u32 prev_offset = 0; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5618 | int ret = 0; |
| 5619 | |
| 5620 | nfuncs = attr->func_info_cnt; |
| 5621 | if (!nfuncs) |
| 5622 | return 0; |
| 5623 | |
| 5624 | if (nfuncs != env->subprog_cnt) { |
| 5625 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); |
| 5626 | return -EINVAL; |
| 5627 | } |
| 5628 | |
| 5629 | urec_size = attr->func_info_rec_size; |
| 5630 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || |
| 5631 | urec_size > MAX_FUNCINFO_REC_SIZE || |
| 5632 | urec_size % sizeof(u32)) { |
| 5633 | verbose(env, "invalid func info rec size %u\n", urec_size); |
| 5634 | return -EINVAL; |
| 5635 | } |
| 5636 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5637 | prog = env->prog; |
| 5638 | btf = prog->aux->btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5639 | |
| 5640 | urecord = u64_to_user_ptr(attr->func_info); |
| 5641 | min_size = min_t(u32, krec_size, urec_size); |
| 5642 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5643 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5644 | if (!krecord) |
| 5645 | return -ENOMEM; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5646 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5647 | for (i = 0; i < nfuncs; i++) { |
| 5648 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); |
| 5649 | if (ret) { |
| 5650 | if (ret == -E2BIG) { |
| 5651 | verbose(env, "nonzero tailing record in func info"); |
| 5652 | /* set the size kernel expects so loader can zero |
| 5653 | * out the rest of the record. |
| 5654 | */ |
| 5655 | if (put_user(min_size, &uattr->func_info_rec_size)) |
| 5656 | ret = -EFAULT; |
| 5657 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5658 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5659 | } |
| 5660 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5661 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5662 | ret = -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5663 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5664 | } |
| 5665 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5666 | /* check insn_off */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5667 | if (i == 0) { |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5668 | if (krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5669 | verbose(env, |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5670 | "nonzero insn_off %u for the first func info record", |
| 5671 | krecord[i].insn_off); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5672 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5673 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5674 | } |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5675 | } else if (krecord[i].insn_off <= prev_offset) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5676 | verbose(env, |
| 5677 | "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] | 5678 | krecord[i].insn_off, prev_offset); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5679 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5680 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5681 | } |
| 5682 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5683 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5684 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
| 5685 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5686 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5687 | } |
| 5688 | |
| 5689 | /* check type_id */ |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5690 | type = btf_type_by_id(btf, krecord[i].type_id); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5691 | if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) { |
| 5692 | verbose(env, "invalid type id %d in func info", |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5693 | krecord[i].type_id); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5694 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5695 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5696 | } |
| 5697 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5698 | prev_offset = krecord[i].insn_off; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5699 | urecord += urec_size; |
| 5700 | } |
| 5701 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5702 | prog->aux->func_info = krecord; |
| 5703 | prog->aux->func_info_cnt = nfuncs; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5704 | return 0; |
| 5705 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5706 | err_free: |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5707 | kvfree(krecord); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5708 | return ret; |
| 5709 | } |
| 5710 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5711 | static void adjust_btf_func(struct bpf_verifier_env *env) |
| 5712 | { |
| 5713 | int i; |
| 5714 | |
| 5715 | if (!env->prog->aux->func_info) |
| 5716 | return; |
| 5717 | |
| 5718 | for (i = 0; i < env->subprog_cnt; i++) |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5719 | env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5720 | } |
| 5721 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5722 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
| 5723 | sizeof(((struct bpf_line_info *)(0))->line_col)) |
| 5724 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
| 5725 | |
| 5726 | static int check_btf_line(struct bpf_verifier_env *env, |
| 5727 | const union bpf_attr *attr, |
| 5728 | union bpf_attr __user *uattr) |
| 5729 | { |
| 5730 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; |
| 5731 | struct bpf_subprog_info *sub; |
| 5732 | struct bpf_line_info *linfo; |
| 5733 | struct bpf_prog *prog; |
| 5734 | const struct btf *btf; |
| 5735 | void __user *ulinfo; |
| 5736 | int err; |
| 5737 | |
| 5738 | nr_linfo = attr->line_info_cnt; |
| 5739 | if (!nr_linfo) |
| 5740 | return 0; |
| 5741 | |
| 5742 | rec_size = attr->line_info_rec_size; |
| 5743 | if (rec_size < MIN_BPF_LINEINFO_SIZE || |
| 5744 | rec_size > MAX_LINEINFO_REC_SIZE || |
| 5745 | rec_size & (sizeof(u32) - 1)) |
| 5746 | return -EINVAL; |
| 5747 | |
| 5748 | /* Need to zero it in case the userspace may |
| 5749 | * pass in a smaller bpf_line_info object. |
| 5750 | */ |
| 5751 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), |
| 5752 | GFP_KERNEL | __GFP_NOWARN); |
| 5753 | if (!linfo) |
| 5754 | return -ENOMEM; |
| 5755 | |
| 5756 | prog = env->prog; |
| 5757 | btf = prog->aux->btf; |
| 5758 | |
| 5759 | s = 0; |
| 5760 | sub = env->subprog_info; |
| 5761 | ulinfo = u64_to_user_ptr(attr->line_info); |
| 5762 | expected_size = sizeof(struct bpf_line_info); |
| 5763 | ncopy = min_t(u32, expected_size, rec_size); |
| 5764 | for (i = 0; i < nr_linfo; i++) { |
| 5765 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); |
| 5766 | if (err) { |
| 5767 | if (err == -E2BIG) { |
| 5768 | verbose(env, "nonzero tailing record in line_info"); |
| 5769 | if (put_user(expected_size, |
| 5770 | &uattr->line_info_rec_size)) |
| 5771 | err = -EFAULT; |
| 5772 | } |
| 5773 | goto err_free; |
| 5774 | } |
| 5775 | |
| 5776 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { |
| 5777 | err = -EFAULT; |
| 5778 | goto err_free; |
| 5779 | } |
| 5780 | |
| 5781 | /* |
| 5782 | * Check insn_off to ensure |
| 5783 | * 1) strictly increasing AND |
| 5784 | * 2) bounded by prog->len |
| 5785 | * |
| 5786 | * The linfo[0].insn_off == 0 check logically falls into |
| 5787 | * the later "missing bpf_line_info for func..." case |
| 5788 | * because the first linfo[0].insn_off must be the |
| 5789 | * first sub also and the first sub must have |
| 5790 | * subprog_info[0].start == 0. |
| 5791 | */ |
| 5792 | if ((i && linfo[i].insn_off <= prev_offset) || |
| 5793 | linfo[i].insn_off >= prog->len) { |
| 5794 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", |
| 5795 | i, linfo[i].insn_off, prev_offset, |
| 5796 | prog->len); |
| 5797 | err = -EINVAL; |
| 5798 | goto err_free; |
| 5799 | } |
| 5800 | |
Martin KaFai Lau | fdbaa0b | 2018-12-19 13:01:01 -0800 | [diff] [blame] | 5801 | if (!prog->insnsi[linfo[i].insn_off].code) { |
| 5802 | verbose(env, |
| 5803 | "Invalid insn code at line_info[%u].insn_off\n", |
| 5804 | i); |
| 5805 | err = -EINVAL; |
| 5806 | goto err_free; |
| 5807 | } |
| 5808 | |
Martin KaFai Lau | 23127b3 | 2018-12-13 10:41:46 -0800 | [diff] [blame] | 5809 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
| 5810 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5811 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
| 5812 | err = -EINVAL; |
| 5813 | goto err_free; |
| 5814 | } |
| 5815 | |
| 5816 | if (s != env->subprog_cnt) { |
| 5817 | if (linfo[i].insn_off == sub[s].start) { |
| 5818 | sub[s].linfo_idx = i; |
| 5819 | s++; |
| 5820 | } else if (sub[s].start < linfo[i].insn_off) { |
| 5821 | verbose(env, "missing bpf_line_info for func#%u\n", s); |
| 5822 | err = -EINVAL; |
| 5823 | goto err_free; |
| 5824 | } |
| 5825 | } |
| 5826 | |
| 5827 | prev_offset = linfo[i].insn_off; |
| 5828 | ulinfo += rec_size; |
| 5829 | } |
| 5830 | |
| 5831 | if (s != env->subprog_cnt) { |
| 5832 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", |
| 5833 | env->subprog_cnt - s, s); |
| 5834 | err = -EINVAL; |
| 5835 | goto err_free; |
| 5836 | } |
| 5837 | |
| 5838 | prog->aux->linfo = linfo; |
| 5839 | prog->aux->nr_linfo = nr_linfo; |
| 5840 | |
| 5841 | return 0; |
| 5842 | |
| 5843 | err_free: |
| 5844 | kvfree(linfo); |
| 5845 | return err; |
| 5846 | } |
| 5847 | |
| 5848 | static int check_btf_info(struct bpf_verifier_env *env, |
| 5849 | const union bpf_attr *attr, |
| 5850 | union bpf_attr __user *uattr) |
| 5851 | { |
| 5852 | struct btf *btf; |
| 5853 | int err; |
| 5854 | |
| 5855 | if (!attr->func_info_cnt && !attr->line_info_cnt) |
| 5856 | return 0; |
| 5857 | |
| 5858 | btf = btf_get_by_fd(attr->prog_btf_fd); |
| 5859 | if (IS_ERR(btf)) |
| 5860 | return PTR_ERR(btf); |
| 5861 | env->prog->aux->btf = btf; |
| 5862 | |
| 5863 | err = check_btf_func(env, attr, uattr); |
| 5864 | if (err) |
| 5865 | return err; |
| 5866 | |
| 5867 | err = check_btf_line(env, attr, uattr); |
| 5868 | if (err) |
| 5869 | return err; |
| 5870 | |
| 5871 | return 0; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5872 | } |
| 5873 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5874 | /* check %cur's range satisfies %old's */ |
| 5875 | static bool range_within(struct bpf_reg_state *old, |
| 5876 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5877 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5878 | return old->umin_value <= cur->umin_value && |
| 5879 | old->umax_value >= cur->umax_value && |
| 5880 | old->smin_value <= cur->smin_value && |
| 5881 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5882 | } |
| 5883 | |
| 5884 | /* Maximum number of register states that can exist at once */ |
| 5885 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 5886 | struct idpair { |
| 5887 | u32 old; |
| 5888 | u32 cur; |
| 5889 | }; |
| 5890 | |
| 5891 | /* If in the old state two registers had the same id, then they need to have |
| 5892 | * the same id in the new state as well. But that id could be different from |
| 5893 | * the old state, so we need to track the mapping from old to new ids. |
| 5894 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 5895 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 5896 | * regs with a different old id could still have new id 9, we don't care about |
| 5897 | * that. |
| 5898 | * So we look through our idmap to see if this old id has been seen before. If |
| 5899 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 5900 | */ |
| 5901 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 5902 | { |
| 5903 | unsigned int i; |
| 5904 | |
| 5905 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 5906 | if (!idmap[i].old) { |
| 5907 | /* Reached an empty slot; haven't seen this id before */ |
| 5908 | idmap[i].old = old_id; |
| 5909 | idmap[i].cur = cur_id; |
| 5910 | return true; |
| 5911 | } |
| 5912 | if (idmap[i].old == old_id) |
| 5913 | return idmap[i].cur == cur_id; |
| 5914 | } |
| 5915 | /* We ran out of idmap slots, which should be impossible */ |
| 5916 | WARN_ON_ONCE(1); |
| 5917 | return false; |
| 5918 | } |
| 5919 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 5920 | static void clean_func_state(struct bpf_verifier_env *env, |
| 5921 | struct bpf_func_state *st) |
| 5922 | { |
| 5923 | enum bpf_reg_liveness live; |
| 5924 | int i, j; |
| 5925 | |
| 5926 | for (i = 0; i < BPF_REG_FP; i++) { |
| 5927 | live = st->regs[i].live; |
| 5928 | /* liveness must not touch this register anymore */ |
| 5929 | st->regs[i].live |= REG_LIVE_DONE; |
| 5930 | if (!(live & REG_LIVE_READ)) |
| 5931 | /* since the register is unused, clear its state |
| 5932 | * to make further comparison simpler |
| 5933 | */ |
| 5934 | __mark_reg_not_init(&st->regs[i]); |
| 5935 | } |
| 5936 | |
| 5937 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { |
| 5938 | live = st->stack[i].spilled_ptr.live; |
| 5939 | /* liveness must not touch this stack slot anymore */ |
| 5940 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; |
| 5941 | if (!(live & REG_LIVE_READ)) { |
| 5942 | __mark_reg_not_init(&st->stack[i].spilled_ptr); |
| 5943 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 5944 | st->stack[i].slot_type[j] = STACK_INVALID; |
| 5945 | } |
| 5946 | } |
| 5947 | } |
| 5948 | |
| 5949 | static void clean_verifier_state(struct bpf_verifier_env *env, |
| 5950 | struct bpf_verifier_state *st) |
| 5951 | { |
| 5952 | int i; |
| 5953 | |
| 5954 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) |
| 5955 | /* all regs in this state in all frames were already marked */ |
| 5956 | return; |
| 5957 | |
| 5958 | for (i = 0; i <= st->curframe; i++) |
| 5959 | clean_func_state(env, st->frame[i]); |
| 5960 | } |
| 5961 | |
| 5962 | /* the parentage chains form a tree. |
| 5963 | * the verifier states are added to state lists at given insn and |
| 5964 | * pushed into state stack for future exploration. |
| 5965 | * when the verifier reaches bpf_exit insn some of the verifer states |
| 5966 | * stored in the state lists have their final liveness state already, |
| 5967 | * but a lot of states will get revised from liveness point of view when |
| 5968 | * the verifier explores other branches. |
| 5969 | * Example: |
| 5970 | * 1: r0 = 1 |
| 5971 | * 2: if r1 == 100 goto pc+1 |
| 5972 | * 3: r0 = 2 |
| 5973 | * 4: exit |
| 5974 | * when the verifier reaches exit insn the register r0 in the state list of |
| 5975 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch |
| 5976 | * of insn 2 and goes exploring further. At the insn 4 it will walk the |
| 5977 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. |
| 5978 | * |
| 5979 | * Since the verifier pushes the branch states as it sees them while exploring |
| 5980 | * the program the condition of walking the branch instruction for the second |
| 5981 | * time means that all states below this branch were already explored and |
| 5982 | * their final liveness markes are already propagated. |
| 5983 | * Hence when the verifier completes the search of state list in is_state_visited() |
| 5984 | * we can call this clean_live_states() function to mark all liveness states |
| 5985 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' |
| 5986 | * will not be used. |
| 5987 | * This function also clears the registers and stack for states that !READ |
| 5988 | * to simplify state merging. |
| 5989 | * |
| 5990 | * Important note here that walking the same branch instruction in the callee |
| 5991 | * doesn't meant that the states are DONE. The verifier has to compare |
| 5992 | * the callsites |
| 5993 | */ |
| 5994 | static void clean_live_states(struct bpf_verifier_env *env, int insn, |
| 5995 | struct bpf_verifier_state *cur) |
| 5996 | { |
| 5997 | struct bpf_verifier_state_list *sl; |
| 5998 | int i; |
| 5999 | |
| 6000 | sl = env->explored_states[insn]; |
| 6001 | if (!sl) |
| 6002 | return; |
| 6003 | |
| 6004 | while (sl != STATE_LIST_MARK) { |
| 6005 | if (sl->state.curframe != cur->curframe) |
| 6006 | goto next; |
| 6007 | for (i = 0; i <= cur->curframe; i++) |
| 6008 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) |
| 6009 | goto next; |
| 6010 | clean_verifier_state(env, &sl->state); |
| 6011 | next: |
| 6012 | sl = sl->next; |
| 6013 | } |
| 6014 | } |
| 6015 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6016 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 6017 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 6018 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6019 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6020 | bool equal; |
| 6021 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6022 | if (!(rold->live & REG_LIVE_READ)) |
| 6023 | /* explored state didn't use this */ |
| 6024 | return true; |
| 6025 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6026 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6027 | |
| 6028 | if (rold->type == PTR_TO_STACK) |
| 6029 | /* two stack pointers are equal only if they're pointing to |
| 6030 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 6031 | */ |
| 6032 | return equal && rold->frameno == rcur->frameno; |
| 6033 | |
| 6034 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6035 | return true; |
| 6036 | |
| 6037 | if (rold->type == NOT_INIT) |
| 6038 | /* explored state can't have used this */ |
| 6039 | return true; |
| 6040 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6041 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6042 | switch (rold->type) { |
| 6043 | case SCALAR_VALUE: |
| 6044 | if (rcur->type == SCALAR_VALUE) { |
| 6045 | /* new val must satisfy old val knowledge */ |
| 6046 | return range_within(rold, rcur) && |
| 6047 | tnum_in(rold->var_off, rcur->var_off); |
| 6048 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 6049 | /* We're trying to use a pointer in place of a scalar. |
| 6050 | * Even if the scalar was unbounded, this could lead to |
| 6051 | * pointer leaks because scalars are allowed to leak |
| 6052 | * while pointers are not. We could make this safe in |
| 6053 | * special cases if root is calling us, but it's |
| 6054 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6055 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 6056 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6057 | } |
| 6058 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 6059 | /* If the new min/max/var_off satisfy the old ones and |
| 6060 | * everything else matches, we are OK. |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6061 | * 'id' is not compared, since it's only used for maps with |
| 6062 | * bpf_spin_lock inside map element and in such cases if |
| 6063 | * the rest of the prog is valid for one map element then |
| 6064 | * it's valid for all map elements regardless of the key |
| 6065 | * used in bpf_map_lookup() |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 6066 | */ |
| 6067 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 6068 | range_within(rold, rcur) && |
| 6069 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6070 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 6071 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 6072 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 6073 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 6074 | * checked, doing so could have affected others with the same |
| 6075 | * id, and we can't check for that because we lost the id when |
| 6076 | * we converted to a PTR_TO_MAP_VALUE. |
| 6077 | */ |
| 6078 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 6079 | return false; |
| 6080 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 6081 | return false; |
| 6082 | /* Check our ids match any regs they're supposed to */ |
| 6083 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 6084 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6085 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 6086 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6087 | return false; |
| 6088 | /* We must have at least as much range as the old ptr |
| 6089 | * did, so that any accesses which were safe before are |
| 6090 | * still safe. This is true even if old range < old off, |
| 6091 | * since someone could have accessed through (ptr - k), or |
| 6092 | * even done ptr -= k in a register, to get a safe access. |
| 6093 | */ |
| 6094 | if (rold->range > rcur->range) |
| 6095 | return false; |
| 6096 | /* If the offsets don't match, we can't trust our alignment; |
| 6097 | * nor can we be sure that we won't fall out of range. |
| 6098 | */ |
| 6099 | if (rold->off != rcur->off) |
| 6100 | return false; |
| 6101 | /* id relations must be preserved */ |
| 6102 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 6103 | return false; |
| 6104 | /* new val must satisfy old val knowledge */ |
| 6105 | return range_within(rold, rcur) && |
| 6106 | tnum_in(rold->var_off, rcur->var_off); |
| 6107 | case PTR_TO_CTX: |
| 6108 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6109 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 6110 | case PTR_TO_FLOW_KEYS: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6111 | case PTR_TO_SOCKET: |
| 6112 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 6113 | case PTR_TO_SOCK_COMMON: |
| 6114 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 6115 | case PTR_TO_TCP_SOCK: |
| 6116 | case PTR_TO_TCP_SOCK_OR_NULL: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6117 | /* Only valid matches are exact, which memcmp() above |
| 6118 | * would have accepted |
| 6119 | */ |
| 6120 | default: |
| 6121 | /* Don't know what's going on, just say it's not safe */ |
| 6122 | return false; |
| 6123 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6124 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6125 | /* Shouldn't get here; if we do, say it's not safe */ |
| 6126 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6127 | return false; |
| 6128 | } |
| 6129 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6130 | static bool stacksafe(struct bpf_func_state *old, |
| 6131 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6132 | struct idpair *idmap) |
| 6133 | { |
| 6134 | int i, spi; |
| 6135 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6136 | /* walk slots of the explored stack and ignore any additional |
| 6137 | * slots in the current stack, since explored(safe) state |
| 6138 | * didn't use them |
| 6139 | */ |
| 6140 | for (i = 0; i < old->allocated_stack; i++) { |
| 6141 | spi = i / BPF_REG_SIZE; |
| 6142 | |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 6143 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
| 6144 | i += BPF_REG_SIZE - 1; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 6145 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 6146 | continue; |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 6147 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 6148 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6149 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 6150 | continue; |
Alexei Starovoitov | 19e2dbb | 2018-12-13 11:42:33 -0800 | [diff] [blame] | 6151 | |
| 6152 | /* explored stack has more populated slots than current stack |
| 6153 | * and these slots were used |
| 6154 | */ |
| 6155 | if (i >= cur->allocated_stack) |
| 6156 | return false; |
| 6157 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 6158 | /* if old state was safe with misc data in the stack |
| 6159 | * it will be safe with zero-initialized stack. |
| 6160 | * The opposite is not true |
| 6161 | */ |
| 6162 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 6163 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 6164 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6165 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 6166 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 6167 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 6168 | * this stack slot, but current has has STACK_MISC -> |
| 6169 | * this verifier states are not equivalent, |
| 6170 | * return false to continue verification of this path |
| 6171 | */ |
| 6172 | return false; |
| 6173 | if (i % BPF_REG_SIZE) |
| 6174 | continue; |
| 6175 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 6176 | continue; |
| 6177 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 6178 | &cur->stack[spi].spilled_ptr, |
| 6179 | idmap)) |
| 6180 | /* when explored and current stack slot are both storing |
| 6181 | * spilled registers, check that stored pointers types |
| 6182 | * are the same as well. |
| 6183 | * Ex: explored safe path could have stored |
| 6184 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 6185 | * but current path has stored: |
| 6186 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 6187 | * such verifier states are not equivalent. |
| 6188 | * return false to continue verification of this path |
| 6189 | */ |
| 6190 | return false; |
| 6191 | } |
| 6192 | return true; |
| 6193 | } |
| 6194 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6195 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 6196 | { |
| 6197 | if (old->acquired_refs != cur->acquired_refs) |
| 6198 | return false; |
| 6199 | return !memcmp(old->refs, cur->refs, |
| 6200 | sizeof(*old->refs) * old->acquired_refs); |
| 6201 | } |
| 6202 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6203 | /* compare two verifier states |
| 6204 | * |
| 6205 | * all states stored in state_list are known to be valid, since |
| 6206 | * verifier reached 'bpf_exit' instruction through them |
| 6207 | * |
| 6208 | * this function is called when verifier exploring different branches of |
| 6209 | * execution popped from the state stack. If it sees an old state that has |
| 6210 | * more strict register state and more strict stack state then this execution |
| 6211 | * branch doesn't need to be explored further, since verifier already |
| 6212 | * concluded that more strict state leads to valid finish. |
| 6213 | * |
| 6214 | * Therefore two states are equivalent if register state is more conservative |
| 6215 | * and explored stack state is more conservative than the current one. |
| 6216 | * Example: |
| 6217 | * explored current |
| 6218 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 6219 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 6220 | * |
| 6221 | * In other words if current stack state (one being explored) has more |
| 6222 | * valid slots than old one that already passed validation, it means |
| 6223 | * the verifier can stop exploring and conclude that current state is valid too |
| 6224 | * |
| 6225 | * Similarly with registers. If explored state has register type as invalid |
| 6226 | * whereas register type in current state is meaningful, it means that |
| 6227 | * the current state will reach 'bpf_exit' instruction safely |
| 6228 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6229 | static bool func_states_equal(struct bpf_func_state *old, |
| 6230 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6231 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6232 | struct idpair *idmap; |
| 6233 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6234 | int i; |
| 6235 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6236 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 6237 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 6238 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 6239 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6240 | |
| 6241 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 6242 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6243 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6244 | } |
| 6245 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6246 | if (!stacksafe(old, cur, idmap)) |
| 6247 | goto out_free; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6248 | |
| 6249 | if (!refsafe(old, cur)) |
| 6250 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6251 | ret = true; |
| 6252 | out_free: |
| 6253 | kfree(idmap); |
| 6254 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6255 | } |
| 6256 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6257 | static bool states_equal(struct bpf_verifier_env *env, |
| 6258 | struct bpf_verifier_state *old, |
| 6259 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6260 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6261 | int i; |
| 6262 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6263 | if (old->curframe != cur->curframe) |
| 6264 | return false; |
| 6265 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6266 | /* Verification state from speculative execution simulation |
| 6267 | * must never prune a non-speculative execution one. |
| 6268 | */ |
| 6269 | if (old->speculative && !cur->speculative) |
| 6270 | return false; |
| 6271 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6272 | if (old->active_spin_lock != cur->active_spin_lock) |
| 6273 | return false; |
| 6274 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6275 | /* for states to be equal callsites have to be the same |
| 6276 | * and all frame states need to be equivalent |
| 6277 | */ |
| 6278 | for (i = 0; i <= old->curframe; i++) { |
| 6279 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 6280 | return false; |
| 6281 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 6282 | return false; |
| 6283 | } |
| 6284 | return true; |
| 6285 | } |
| 6286 | |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6287 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
| 6288 | struct bpf_reg_state *reg, |
| 6289 | struct bpf_reg_state *parent_reg) |
| 6290 | { |
| 6291 | int err; |
| 6292 | |
| 6293 | if (parent_reg->live & REG_LIVE_READ || !(reg->live & REG_LIVE_READ)) |
| 6294 | return 0; |
| 6295 | |
| 6296 | err = mark_reg_read(env, reg, parent_reg); |
| 6297 | if (err) |
| 6298 | return err; |
| 6299 | |
| 6300 | return 0; |
| 6301 | } |
| 6302 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6303 | /* A write screens off any subsequent reads; but write marks come from the |
| 6304 | * straight-line code between a state and its parent. When we arrive at an |
| 6305 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 6306 | * code, so read marks in the state must propagate to the parent regardless |
| 6307 | * 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] | 6308 | * in mark_reg_read() is for. |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6309 | */ |
| 6310 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 6311 | const struct bpf_verifier_state *vstate, |
| 6312 | struct bpf_verifier_state *vparent) |
| 6313 | { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6314 | struct bpf_reg_state *state_reg, *parent_reg; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6315 | struct bpf_func_state *state, *parent; |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6316 | int i, frame, err = 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6317 | |
| 6318 | if (vparent->curframe != vstate->curframe) { |
| 6319 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 6320 | vparent->curframe, vstate->curframe); |
| 6321 | return -EFAULT; |
| 6322 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6323 | /* Propagate read liveness of registers... */ |
| 6324 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 6325 | for (frame = 0; frame <= vstate->curframe; frame++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6326 | parent = vparent->frame[frame]; |
| 6327 | state = vstate->frame[frame]; |
| 6328 | parent_reg = parent->regs; |
| 6329 | state_reg = state->regs; |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 6330 | /* We don't need to worry about FP liveness, it's read-only */ |
| 6331 | 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] | 6332 | err = propagate_liveness_reg(env, &state_reg[i], |
| 6333 | &parent_reg[i]); |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6334 | if (err) |
| 6335 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6336 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6337 | |
Jiong Wang | 1b04aee | 2019-04-12 22:59:34 +0100 | [diff] [blame] | 6338 | /* Propagate stack slots. */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6339 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 6340 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6341 | parent_reg = &parent->stack[i].spilled_ptr; |
| 6342 | state_reg = &state->stack[i].spilled_ptr; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6343 | err = propagate_liveness_reg(env, state_reg, |
| 6344 | parent_reg); |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6345 | if (err) |
| 6346 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6347 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6348 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6349 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6350 | } |
| 6351 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6352 | 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] | 6353 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6354 | struct bpf_verifier_state_list *new_sl; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 6355 | struct bpf_verifier_state_list *sl, **pprev; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6356 | struct bpf_verifier_state *cur = env->cur_state, *new; |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 6357 | int i, j, err, states_cnt = 0; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6358 | |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 6359 | pprev = &env->explored_states[insn_idx]; |
| 6360 | sl = *pprev; |
| 6361 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6362 | if (!sl) |
| 6363 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 6364 | * be doing state search here |
| 6365 | */ |
| 6366 | return 0; |
| 6367 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 6368 | clean_live_states(env, insn_idx, cur); |
| 6369 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6370 | while (sl != STATE_LIST_MARK) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6371 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 6372 | sl->hit_cnt++; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6373 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6374 | * prune the search. |
| 6375 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 6376 | * If we have any write marks in env->cur_state, they |
| 6377 | * will prevent corresponding reads in the continuation |
| 6378 | * from reaching our parent (an explored_state). Our |
| 6379 | * own state will get the read marks recorded, but |
| 6380 | * they'll be immediately forgotten as we're pruning |
| 6381 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6382 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6383 | err = propagate_liveness(env, &sl->state, cur); |
| 6384 | if (err) |
| 6385 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6386 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6387 | } |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 6388 | states_cnt++; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 6389 | sl->miss_cnt++; |
| 6390 | /* heuristic to determine whether this state is beneficial |
| 6391 | * to keep checking from state equivalence point of view. |
| 6392 | * Higher numbers increase max_states_per_insn and verification time, |
| 6393 | * but do not meaningfully decrease insn_processed. |
| 6394 | */ |
| 6395 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { |
| 6396 | /* the state is unlikely to be useful. Remove it to |
| 6397 | * speed up verification |
| 6398 | */ |
| 6399 | *pprev = sl->next; |
| 6400 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { |
| 6401 | free_verifier_state(&sl->state, false); |
| 6402 | kfree(sl); |
| 6403 | env->peak_states--; |
| 6404 | } else { |
| 6405 | /* cannot free this state, since parentage chain may |
| 6406 | * walk it later. Add it for free_list instead to |
| 6407 | * be freed at the end of verification |
| 6408 | */ |
| 6409 | sl->next = env->free_list; |
| 6410 | env->free_list = sl; |
| 6411 | } |
| 6412 | sl = *pprev; |
| 6413 | continue; |
| 6414 | } |
| 6415 | pprev = &sl->next; |
| 6416 | sl = *pprev; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6417 | } |
| 6418 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6419 | if (env->max_states_per_insn < states_cnt) |
| 6420 | env->max_states_per_insn = states_cnt; |
| 6421 | |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 6422 | if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
| 6423 | return 0; |
| 6424 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6425 | /* there were no equivalent states, remember current one. |
| 6426 | * technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6427 | * but it will either reach outer most bpf_exit (which means it's safe) |
| 6428 | * or it will be rejected. Since there are no loops, we won't be |
| 6429 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
| 6430 | * again on the way to bpf_exit |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6431 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6432 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6433 | if (!new_sl) |
| 6434 | return -ENOMEM; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6435 | env->total_states++; |
| 6436 | env->peak_states++; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6437 | |
| 6438 | /* add new state to the head of linked list */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6439 | new = &new_sl->state; |
| 6440 | err = copy_verifier_state(new, cur); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 6441 | if (err) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6442 | free_verifier_state(new, false); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 6443 | kfree(new_sl); |
| 6444 | return err; |
| 6445 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6446 | new_sl->next = env->explored_states[insn_idx]; |
| 6447 | env->explored_states[insn_idx] = new_sl; |
Jakub Kicinski | 7640ead | 2018-12-12 16:29:07 -0800 | [diff] [blame] | 6448 | /* connect new state to parentage chain. Current frame needs all |
| 6449 | * registers connected. Only r6 - r9 of the callers are alive (pushed |
| 6450 | * to the stack implicitly by JITs) so in callers' frames connect just |
| 6451 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to |
| 6452 | * the state of the call instruction (with WRITTEN set), and r0 comes |
| 6453 | * from callee with its full parentage chain, anyway. |
| 6454 | */ |
| 6455 | for (j = 0; j <= cur->curframe; j++) |
| 6456 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) |
| 6457 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 6458 | /* clear write marks in current state: the writes we did are not writes |
| 6459 | * our child did, so they don't screen off its reads from us. |
| 6460 | * (There are no read marks in current state, because reads always mark |
| 6461 | * their parent and current state never has children yet. Only |
| 6462 | * explored_states can get read marks.) |
| 6463 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6464 | for (i = 0; i < BPF_REG_FP; i++) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6465 | cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE; |
| 6466 | |
| 6467 | /* all stack frames are accessible from callee, clear them all */ |
| 6468 | for (j = 0; j <= cur->curframe; j++) { |
| 6469 | struct bpf_func_state *frame = cur->frame[j]; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6470 | struct bpf_func_state *newframe = new->frame[j]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6471 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6472 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 6473 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6474 | frame->stack[i].spilled_ptr.parent = |
| 6475 | &newframe->stack[i].spilled_ptr; |
| 6476 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6477 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6478 | return 0; |
| 6479 | } |
| 6480 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6481 | /* Return true if it's OK to have the same insn return a different type. */ |
| 6482 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 6483 | { |
| 6484 | switch (type) { |
| 6485 | case PTR_TO_CTX: |
| 6486 | case PTR_TO_SOCKET: |
| 6487 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 6488 | case PTR_TO_SOCK_COMMON: |
| 6489 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 6490 | case PTR_TO_TCP_SOCK: |
| 6491 | case PTR_TO_TCP_SOCK_OR_NULL: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6492 | return false; |
| 6493 | default: |
| 6494 | return true; |
| 6495 | } |
| 6496 | } |
| 6497 | |
| 6498 | /* If an instruction was previously used with particular pointer types, then we |
| 6499 | * need to be careful to avoid cases such as the below, where it may be ok |
| 6500 | * for one branch accessing the pointer, but not ok for the other branch: |
| 6501 | * |
| 6502 | * R1 = sock_ptr |
| 6503 | * goto X; |
| 6504 | * ... |
| 6505 | * R1 = some_other_valid_ptr; |
| 6506 | * goto X; |
| 6507 | * ... |
| 6508 | * R2 = *(u32 *)(R1 + 0); |
| 6509 | */ |
| 6510 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 6511 | { |
| 6512 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 6513 | !reg_type_mismatch_ok(prev)); |
| 6514 | } |
| 6515 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6516 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6517 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6518 | struct bpf_verifier_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6519 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6520 | struct bpf_reg_state *regs; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6521 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6522 | bool do_print_state = false; |
| 6523 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 6524 | env->prev_linfo = NULL; |
| 6525 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6526 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 6527 | if (!state) |
| 6528 | return -ENOMEM; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6529 | state->curframe = 0; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6530 | state->speculative = false; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6531 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 6532 | if (!state->frame[0]) { |
| 6533 | kfree(state); |
| 6534 | return -ENOMEM; |
| 6535 | } |
| 6536 | env->cur_state = state; |
| 6537 | init_func_state(env, state->frame[0], |
| 6538 | BPF_MAIN_FUNC /* callsite */, |
| 6539 | 0 /* frameno */, |
| 6540 | 0 /* subprogno, zero == main subprog */); |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6541 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6542 | for (;;) { |
| 6543 | struct bpf_insn *insn; |
| 6544 | u8 class; |
| 6545 | int err; |
| 6546 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6547 | if (env->insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6548 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6549 | env->insn_idx, insn_cnt); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6550 | return -EFAULT; |
| 6551 | } |
| 6552 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6553 | insn = &insns[env->insn_idx]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6554 | class = BPF_CLASS(insn->code); |
| 6555 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6556 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6557 | verbose(env, |
| 6558 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6559 | env->insn_processed); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6560 | return -E2BIG; |
| 6561 | } |
| 6562 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6563 | err = is_state_visited(env, env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6564 | if (err < 0) |
| 6565 | return err; |
| 6566 | if (err == 1) { |
| 6567 | /* found equivalent state, can prune the search */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6568 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6569 | if (do_print_state) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6570 | verbose(env, "\nfrom %d to %d%s: safe\n", |
| 6571 | env->prev_insn_idx, env->insn_idx, |
| 6572 | env->cur_state->speculative ? |
| 6573 | " (speculative execution)" : ""); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6574 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6575 | verbose(env, "%d: safe\n", env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6576 | } |
| 6577 | goto process_bpf_exit; |
| 6578 | } |
| 6579 | |
Alexei Starovoitov | c349480 | 2018-12-03 22:46:04 -0800 | [diff] [blame] | 6580 | if (signal_pending(current)) |
| 6581 | return -EAGAIN; |
| 6582 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 6583 | if (need_resched()) |
| 6584 | cond_resched(); |
| 6585 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6586 | if (env->log.level & BPF_LOG_LEVEL2 || |
| 6587 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { |
| 6588 | if (env->log.level & BPF_LOG_LEVEL2) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6589 | verbose(env, "%d:", env->insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 6590 | else |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6591 | verbose(env, "\nfrom %d to %d%s:", |
| 6592 | env->prev_insn_idx, env->insn_idx, |
| 6593 | env->cur_state->speculative ? |
| 6594 | " (speculative execution)" : ""); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6595 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6596 | do_print_state = false; |
| 6597 | } |
| 6598 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6599 | if (env->log.level & BPF_LOG_LEVEL) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6600 | const struct bpf_insn_cbs cbs = { |
| 6601 | .cb_print = verbose, |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 6602 | .private_data = env, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6603 | }; |
| 6604 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6605 | verbose_linfo(env, env->insn_idx, "; "); |
| 6606 | verbose(env, "%d: ", env->insn_idx); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 6607 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6608 | } |
| 6609 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 6610 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6611 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
| 6612 | env->prev_insn_idx); |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 6613 | if (err) |
| 6614 | return err; |
| 6615 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 6616 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6617 | regs = cur_regs(env); |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6618 | env->insn_aux_data[env->insn_idx].seen = true; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6619 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6620 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6621 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6622 | if (err) |
| 6623 | return err; |
| 6624 | |
| 6625 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6626 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6627 | |
| 6628 | /* check for reserved fields is already done */ |
| 6629 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6630 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6631 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6632 | if (err) |
| 6633 | return err; |
| 6634 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6635 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6636 | if (err) |
| 6637 | return err; |
| 6638 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 6639 | src_reg_type = regs[insn->src_reg].type; |
| 6640 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6641 | /* check that memory (src_reg + off) is readable, |
| 6642 | * the state of dst_reg will be updated by this func |
| 6643 | */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6644 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
| 6645 | insn->off, BPF_SIZE(insn->code), |
| 6646 | BPF_READ, insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6647 | if (err) |
| 6648 | return err; |
| 6649 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6650 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6651 | |
| 6652 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6653 | /* saw a valid insn |
| 6654 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6655 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6656 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6657 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6658 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6659 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6660 | /* ABuser program is trying to use the same insn |
| 6661 | * dst_reg = *(u32*) (src_reg + off) |
| 6662 | * with different pointer types: |
| 6663 | * src_reg == ctx in one branch and |
| 6664 | * src_reg == stack|map in some other branch. |
| 6665 | * Reject it. |
| 6666 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6667 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6668 | return -EINVAL; |
| 6669 | } |
| 6670 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6671 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6672 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6673 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6674 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6675 | err = check_xadd(env, env->insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6676 | if (err) |
| 6677 | return err; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6678 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6679 | continue; |
| 6680 | } |
| 6681 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6682 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6683 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6684 | if (err) |
| 6685 | return err; |
| 6686 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6687 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6688 | if (err) |
| 6689 | return err; |
| 6690 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6691 | dst_reg_type = regs[insn->dst_reg].type; |
| 6692 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6693 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6694 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 6695 | insn->off, BPF_SIZE(insn->code), |
| 6696 | BPF_WRITE, insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6697 | if (err) |
| 6698 | return err; |
| 6699 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6700 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6701 | |
| 6702 | if (*prev_dst_type == NOT_INIT) { |
| 6703 | *prev_dst_type = dst_reg_type; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6704 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6705 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6706 | return -EINVAL; |
| 6707 | } |
| 6708 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6709 | } else if (class == BPF_ST) { |
| 6710 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 6711 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6712 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6713 | return -EINVAL; |
| 6714 | } |
| 6715 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6716 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6717 | if (err) |
| 6718 | return err; |
| 6719 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 6720 | if (is_ctx_reg(env, insn->dst_reg)) { |
Joe Stringer | 9d2be44 | 2018-10-02 13:35:31 -0700 | [diff] [blame] | 6721 | 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] | 6722 | insn->dst_reg, |
| 6723 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 6724 | return -EACCES; |
| 6725 | } |
| 6726 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6727 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6728 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 6729 | insn->off, BPF_SIZE(insn->code), |
| 6730 | BPF_WRITE, -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6731 | if (err) |
| 6732 | return err; |
| 6733 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6734 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6735 | u8 opcode = BPF_OP(insn->code); |
| 6736 | |
| 6737 | if (opcode == BPF_CALL) { |
| 6738 | if (BPF_SRC(insn->code) != BPF_K || |
| 6739 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6740 | (insn->src_reg != BPF_REG_0 && |
| 6741 | insn->src_reg != BPF_PSEUDO_CALL) || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6742 | insn->dst_reg != BPF_REG_0 || |
| 6743 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6744 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6745 | return -EINVAL; |
| 6746 | } |
| 6747 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6748 | if (env->cur_state->active_spin_lock && |
| 6749 | (insn->src_reg == BPF_PSEUDO_CALL || |
| 6750 | insn->imm != BPF_FUNC_spin_unlock)) { |
| 6751 | verbose(env, "function calls are not allowed while holding a lock\n"); |
| 6752 | return -EINVAL; |
| 6753 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6754 | if (insn->src_reg == BPF_PSEUDO_CALL) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6755 | err = check_func_call(env, insn, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6756 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6757 | err = check_helper_call(env, insn->imm, env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6758 | if (err) |
| 6759 | return err; |
| 6760 | |
| 6761 | } else if (opcode == BPF_JA) { |
| 6762 | if (BPF_SRC(insn->code) != BPF_K || |
| 6763 | insn->imm != 0 || |
| 6764 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6765 | insn->dst_reg != BPF_REG_0 || |
| 6766 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6767 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6768 | return -EINVAL; |
| 6769 | } |
| 6770 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6771 | env->insn_idx += insn->off + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6772 | continue; |
| 6773 | |
| 6774 | } else if (opcode == BPF_EXIT) { |
| 6775 | if (BPF_SRC(insn->code) != BPF_K || |
| 6776 | insn->imm != 0 || |
| 6777 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6778 | insn->dst_reg != BPF_REG_0 || |
| 6779 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6780 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6781 | return -EINVAL; |
| 6782 | } |
| 6783 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6784 | if (env->cur_state->active_spin_lock) { |
| 6785 | verbose(env, "bpf_spin_unlock is missing\n"); |
| 6786 | return -EINVAL; |
| 6787 | } |
| 6788 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6789 | if (state->curframe) { |
| 6790 | /* exit from nested function */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6791 | env->prev_insn_idx = env->insn_idx; |
| 6792 | err = prepare_func_exit(env, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6793 | if (err) |
| 6794 | return err; |
| 6795 | do_print_state = true; |
| 6796 | continue; |
| 6797 | } |
| 6798 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6799 | err = check_reference_leak(env); |
| 6800 | if (err) |
| 6801 | return err; |
| 6802 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6803 | /* eBPF calling convetion is such that R0 is used |
| 6804 | * to return the value from eBPF program. |
| 6805 | * Make sure that it's readable at this time |
| 6806 | * of bpf_exit, which means that program wrote |
| 6807 | * something into it earlier |
| 6808 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6809 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6810 | if (err) |
| 6811 | return err; |
| 6812 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6813 | if (is_pointer_value(env, BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6814 | verbose(env, "R0 leaks addr as return value\n"); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6815 | return -EACCES; |
| 6816 | } |
| 6817 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 6818 | err = check_return_code(env); |
| 6819 | if (err) |
| 6820 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6821 | process_bpf_exit: |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6822 | err = pop_stack(env, &env->prev_insn_idx, |
| 6823 | &env->insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6824 | if (err < 0) { |
| 6825 | if (err != -ENOENT) |
| 6826 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6827 | break; |
| 6828 | } else { |
| 6829 | do_print_state = true; |
| 6830 | continue; |
| 6831 | } |
| 6832 | } else { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6833 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6834 | if (err) |
| 6835 | return err; |
| 6836 | } |
| 6837 | } else if (class == BPF_LD) { |
| 6838 | u8 mode = BPF_MODE(insn->code); |
| 6839 | |
| 6840 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 6841 | err = check_ld_abs(env, insn); |
| 6842 | if (err) |
| 6843 | return err; |
| 6844 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6845 | } else if (mode == BPF_IMM) { |
| 6846 | err = check_ld_imm(env, insn); |
| 6847 | if (err) |
| 6848 | return err; |
| 6849 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6850 | env->insn_idx++; |
| 6851 | env->insn_aux_data[env->insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6852 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6853 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6854 | return -EINVAL; |
| 6855 | } |
| 6856 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6857 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6858 | return -EINVAL; |
| 6859 | } |
| 6860 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6861 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6862 | } |
| 6863 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 6864 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6865 | return 0; |
| 6866 | } |
| 6867 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 6868 | static int check_map_prealloc(struct bpf_map *map) |
| 6869 | { |
| 6870 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 6871 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 6872 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 6873 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 6874 | } |
| 6875 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6876 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
| 6877 | { |
| 6878 | switch (type) { |
| 6879 | case BPF_PROG_TYPE_KPROBE: |
| 6880 | case BPF_PROG_TYPE_TRACEPOINT: |
| 6881 | case BPF_PROG_TYPE_PERF_EVENT: |
| 6882 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 6883 | return true; |
| 6884 | default: |
| 6885 | return false; |
| 6886 | } |
| 6887 | } |
| 6888 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6889 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 6890 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 6891 | struct bpf_prog *prog) |
| 6892 | |
| 6893 | { |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 6894 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 6895 | * preallocated hash maps, since doing memory allocation |
| 6896 | * in overflow_handler can crash depending on where nmi got |
| 6897 | * triggered. |
| 6898 | */ |
| 6899 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 6900 | if (!check_map_prealloc(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6901 | 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] | 6902 | return -EINVAL; |
| 6903 | } |
| 6904 | if (map->inner_map_meta && |
| 6905 | !check_map_prealloc(map->inner_map_meta)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6906 | verbose(env, "perf_event programs can only use preallocated inner hash map\n"); |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 6907 | return -EINVAL; |
| 6908 | } |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 6909 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 6910 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6911 | if ((is_tracing_prog_type(prog->type) || |
| 6912 | prog->type == BPF_PROG_TYPE_SOCKET_FILTER) && |
| 6913 | map_value_has_spin_lock(map)) { |
| 6914 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); |
| 6915 | return -EINVAL; |
| 6916 | } |
| 6917 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 6918 | 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] | 6919 | !bpf_offload_prog_map_match(prog, map)) { |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 6920 | verbose(env, "offload device mismatch between prog and map\n"); |
| 6921 | return -EINVAL; |
| 6922 | } |
| 6923 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 6924 | return 0; |
| 6925 | } |
| 6926 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 6927 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 6928 | { |
| 6929 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 6930 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 6931 | } |
| 6932 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6933 | /* look for pseudo eBPF instructions that access map FDs and |
| 6934 | * replace them with actual map pointers |
| 6935 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6936 | 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] | 6937 | { |
| 6938 | struct bpf_insn *insn = env->prog->insnsi; |
| 6939 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 6940 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6941 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 6942 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 6943 | if (err) |
| 6944 | return err; |
| 6945 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6946 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6947 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6948 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6949 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6950 | return -EINVAL; |
| 6951 | } |
| 6952 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6953 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 6954 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 6955 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6956 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6957 | return -EINVAL; |
| 6958 | } |
| 6959 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6960 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 6961 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6962 | struct bpf_map *map; |
| 6963 | struct fd f; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 6964 | u64 addr; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6965 | |
| 6966 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 6967 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 6968 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6969 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6970 | return -EINVAL; |
| 6971 | } |
| 6972 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 6973 | if (insn[0].src_reg == 0) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6974 | /* valid generic load 64-bit imm */ |
| 6975 | goto next_insn; |
| 6976 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 6977 | /* In final convert_pseudo_ld_imm64() step, this is |
| 6978 | * converted into regular 64-bit imm load insn. |
| 6979 | */ |
| 6980 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && |
| 6981 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || |
| 6982 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && |
| 6983 | insn[1].imm != 0)) { |
| 6984 | verbose(env, |
| 6985 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6986 | return -EINVAL; |
| 6987 | } |
| 6988 | |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 6989 | f = fdget(insn[0].imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 6990 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6991 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6992 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 6993 | insn[0].imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6994 | return PTR_ERR(map); |
| 6995 | } |
| 6996 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6997 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 6998 | if (err) { |
| 6999 | fdput(f); |
| 7000 | return err; |
| 7001 | } |
| 7002 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7003 | aux = &env->insn_aux_data[i]; |
| 7004 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 7005 | addr = (unsigned long)map; |
| 7006 | } else { |
| 7007 | u32 off = insn[1].imm; |
| 7008 | |
| 7009 | if (off >= BPF_MAX_VAR_OFF) { |
| 7010 | verbose(env, "direct value offset of %u is not allowed\n", off); |
| 7011 | fdput(f); |
| 7012 | return -EINVAL; |
| 7013 | } |
| 7014 | |
| 7015 | if (!map->ops->map_direct_value_addr) { |
| 7016 | verbose(env, "no direct value access support for this map type\n"); |
| 7017 | fdput(f); |
| 7018 | return -EINVAL; |
| 7019 | } |
| 7020 | |
| 7021 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 7022 | if (err) { |
| 7023 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", |
| 7024 | map->value_size, off); |
| 7025 | fdput(f); |
| 7026 | return err; |
| 7027 | } |
| 7028 | |
| 7029 | aux->map_off = off; |
| 7030 | addr += off; |
| 7031 | } |
| 7032 | |
| 7033 | insn[0].imm = (u32)addr; |
| 7034 | insn[1].imm = addr >> 32; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7035 | |
| 7036 | /* check whether we recorded this map already */ |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7037 | for (j = 0; j < env->used_map_cnt; j++) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7038 | if (env->used_maps[j] == map) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7039 | aux->map_index = j; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7040 | fdput(f); |
| 7041 | goto next_insn; |
| 7042 | } |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7043 | } |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7044 | |
| 7045 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 7046 | fdput(f); |
| 7047 | return -E2BIG; |
| 7048 | } |
| 7049 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7050 | /* hold the map. If the program is rejected by verifier, |
| 7051 | * the map will be released by release_maps() or it |
| 7052 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 7053 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7054 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 7055 | map = bpf_map_inc(map, false); |
| 7056 | if (IS_ERR(map)) { |
| 7057 | fdput(f); |
| 7058 | return PTR_ERR(map); |
| 7059 | } |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7060 | |
| 7061 | aux->map_index = env->used_map_cnt; |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 7062 | env->used_maps[env->used_map_cnt++] = map; |
| 7063 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 7064 | if (bpf_map_is_cgroup_storage(map) && |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 7065 | bpf_cgroup_storage_assign(env->prog, map)) { |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 7066 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 7067 | fdput(f); |
| 7068 | return -EBUSY; |
| 7069 | } |
| 7070 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7071 | fdput(f); |
| 7072 | next_insn: |
| 7073 | insn++; |
| 7074 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 7075 | continue; |
| 7076 | } |
| 7077 | |
| 7078 | /* Basic sanity check before we invest more work here. */ |
| 7079 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 7080 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 7081 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7082 | } |
| 7083 | } |
| 7084 | |
| 7085 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 7086 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 7087 | * These pointers will be used later by verifier to validate map access. |
| 7088 | */ |
| 7089 | return 0; |
| 7090 | } |
| 7091 | |
| 7092 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7093 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7094 | { |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 7095 | enum bpf_cgroup_storage_type stype; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7096 | int i; |
| 7097 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 7098 | for_each_cgroup_storage_type(stype) { |
| 7099 | if (!env->prog->aux->cgroup_storage[stype]) |
| 7100 | continue; |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 7101 | bpf_cgroup_storage_release(env->prog, |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 7102 | env->prog->aux->cgroup_storage[stype]); |
| 7103 | } |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 7104 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7105 | for (i = 0; i < env->used_map_cnt; i++) |
| 7106 | bpf_map_put(env->used_maps[i]); |
| 7107 | } |
| 7108 | |
| 7109 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7110 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7111 | { |
| 7112 | struct bpf_insn *insn = env->prog->insnsi; |
| 7113 | int insn_cnt = env->prog->len; |
| 7114 | int i; |
| 7115 | |
| 7116 | for (i = 0; i < insn_cnt; i++, insn++) |
| 7117 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 7118 | insn->src_reg = 0; |
| 7119 | } |
| 7120 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7121 | /* single env->prog->insni[off] instruction was replaced with the range |
| 7122 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 7123 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 7124 | */ |
| 7125 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, |
| 7126 | u32 off, u32 cnt) |
| 7127 | { |
| 7128 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7129 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7130 | |
| 7131 | if (cnt == 1) |
| 7132 | return 0; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 7133 | new_data = vzalloc(array_size(prog_len, |
| 7134 | sizeof(struct bpf_insn_aux_data))); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7135 | if (!new_data) |
| 7136 | return -ENOMEM; |
| 7137 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 7138 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 7139 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7140 | for (i = off; i < off + cnt - 1; i++) |
| 7141 | new_data[i].seen = true; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7142 | env->insn_aux_data = new_data; |
| 7143 | vfree(old_data); |
| 7144 | return 0; |
| 7145 | } |
| 7146 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7147 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 7148 | { |
| 7149 | int i; |
| 7150 | |
| 7151 | if (len == 1) |
| 7152 | return; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 7153 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 7154 | for (i = 0; i <= env->subprog_cnt; i++) { |
Edward Cree | afd5942 | 2018-11-16 12:00:07 +0000 | [diff] [blame] | 7155 | if (env->subprog_info[i].start <= off) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7156 | continue; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 7157 | env->subprog_info[i].start += len - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7158 | } |
| 7159 | } |
| 7160 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7161 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 7162 | const struct bpf_insn *patch, u32 len) |
| 7163 | { |
| 7164 | struct bpf_prog *new_prog; |
| 7165 | |
| 7166 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 7167 | if (IS_ERR(new_prog)) { |
| 7168 | if (PTR_ERR(new_prog) == -ERANGE) |
| 7169 | verbose(env, |
| 7170 | "insn %d cannot be patched due to 16-bit range\n", |
| 7171 | env->insn_aux_data[off].orig_idx); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7172 | return NULL; |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 7173 | } |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7174 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) |
| 7175 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7176 | adjust_subprog_starts(env, off, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7177 | return new_prog; |
| 7178 | } |
| 7179 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 7180 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
| 7181 | u32 off, u32 cnt) |
| 7182 | { |
| 7183 | int i, j; |
| 7184 | |
| 7185 | /* find first prog starting at or after off (first to remove) */ |
| 7186 | for (i = 0; i < env->subprog_cnt; i++) |
| 7187 | if (env->subprog_info[i].start >= off) |
| 7188 | break; |
| 7189 | /* find first prog starting at or after off + cnt (first to stay) */ |
| 7190 | for (j = i; j < env->subprog_cnt; j++) |
| 7191 | if (env->subprog_info[j].start >= off + cnt) |
| 7192 | break; |
| 7193 | /* if j doesn't start exactly at off + cnt, we are just removing |
| 7194 | * the front of previous prog |
| 7195 | */ |
| 7196 | if (env->subprog_info[j].start != off + cnt) |
| 7197 | j--; |
| 7198 | |
| 7199 | if (j > i) { |
| 7200 | struct bpf_prog_aux *aux = env->prog->aux; |
| 7201 | int move; |
| 7202 | |
| 7203 | /* move fake 'exit' subprog as well */ |
| 7204 | move = env->subprog_cnt + 1 - j; |
| 7205 | |
| 7206 | memmove(env->subprog_info + i, |
| 7207 | env->subprog_info + j, |
| 7208 | sizeof(*env->subprog_info) * move); |
| 7209 | env->subprog_cnt -= j - i; |
| 7210 | |
| 7211 | /* remove func_info */ |
| 7212 | if (aux->func_info) { |
| 7213 | move = aux->func_info_cnt - j; |
| 7214 | |
| 7215 | memmove(aux->func_info + i, |
| 7216 | aux->func_info + j, |
| 7217 | sizeof(*aux->func_info) * move); |
| 7218 | aux->func_info_cnt -= j - i; |
| 7219 | /* func_info->insn_off is set after all code rewrites, |
| 7220 | * in adjust_btf_func() - no need to adjust |
| 7221 | */ |
| 7222 | } |
| 7223 | } else { |
| 7224 | /* convert i from "first prog to remove" to "first to adjust" */ |
| 7225 | if (env->subprog_info[i].start == off) |
| 7226 | i++; |
| 7227 | } |
| 7228 | |
| 7229 | /* update fake 'exit' subprog as well */ |
| 7230 | for (; i <= env->subprog_cnt; i++) |
| 7231 | env->subprog_info[i].start -= cnt; |
| 7232 | |
| 7233 | return 0; |
| 7234 | } |
| 7235 | |
| 7236 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, |
| 7237 | u32 cnt) |
| 7238 | { |
| 7239 | struct bpf_prog *prog = env->prog; |
| 7240 | u32 i, l_off, l_cnt, nr_linfo; |
| 7241 | struct bpf_line_info *linfo; |
| 7242 | |
| 7243 | nr_linfo = prog->aux->nr_linfo; |
| 7244 | if (!nr_linfo) |
| 7245 | return 0; |
| 7246 | |
| 7247 | linfo = prog->aux->linfo; |
| 7248 | |
| 7249 | /* find first line info to remove, count lines to be removed */ |
| 7250 | for (i = 0; i < nr_linfo; i++) |
| 7251 | if (linfo[i].insn_off >= off) |
| 7252 | break; |
| 7253 | |
| 7254 | l_off = i; |
| 7255 | l_cnt = 0; |
| 7256 | for (; i < nr_linfo; i++) |
| 7257 | if (linfo[i].insn_off < off + cnt) |
| 7258 | l_cnt++; |
| 7259 | else |
| 7260 | break; |
| 7261 | |
| 7262 | /* First live insn doesn't match first live linfo, it needs to "inherit" |
| 7263 | * last removed linfo. prog is already modified, so prog->len == off |
| 7264 | * means no live instructions after (tail of the program was removed). |
| 7265 | */ |
| 7266 | if (prog->len != off && l_cnt && |
| 7267 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { |
| 7268 | l_cnt--; |
| 7269 | linfo[--i].insn_off = off + cnt; |
| 7270 | } |
| 7271 | |
| 7272 | /* remove the line info which refer to the removed instructions */ |
| 7273 | if (l_cnt) { |
| 7274 | memmove(linfo + l_off, linfo + i, |
| 7275 | sizeof(*linfo) * (nr_linfo - i)); |
| 7276 | |
| 7277 | prog->aux->nr_linfo -= l_cnt; |
| 7278 | nr_linfo = prog->aux->nr_linfo; |
| 7279 | } |
| 7280 | |
| 7281 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ |
| 7282 | for (i = l_off; i < nr_linfo; i++) |
| 7283 | linfo[i].insn_off -= cnt; |
| 7284 | |
| 7285 | /* fix up all subprogs (incl. 'exit') which start >= off */ |
| 7286 | for (i = 0; i <= env->subprog_cnt; i++) |
| 7287 | if (env->subprog_info[i].linfo_idx > l_off) { |
| 7288 | /* program may have started in the removed region but |
| 7289 | * may not be fully removed |
| 7290 | */ |
| 7291 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) |
| 7292 | env->subprog_info[i].linfo_idx -= l_cnt; |
| 7293 | else |
| 7294 | env->subprog_info[i].linfo_idx = l_off; |
| 7295 | } |
| 7296 | |
| 7297 | return 0; |
| 7298 | } |
| 7299 | |
| 7300 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) |
| 7301 | { |
| 7302 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 7303 | unsigned int orig_prog_len = env->prog->len; |
| 7304 | int err; |
| 7305 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 7306 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 7307 | bpf_prog_offload_remove_insns(env, off, cnt); |
| 7308 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 7309 | err = bpf_remove_insns(env->prog, off, cnt); |
| 7310 | if (err) |
| 7311 | return err; |
| 7312 | |
| 7313 | err = adjust_subprog_starts_after_remove(env, off, cnt); |
| 7314 | if (err) |
| 7315 | return err; |
| 7316 | |
| 7317 | err = bpf_adj_linfo_after_remove(env, off, cnt); |
| 7318 | if (err) |
| 7319 | return err; |
| 7320 | |
| 7321 | memmove(aux_data + off, aux_data + off + cnt, |
| 7322 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); |
| 7323 | |
| 7324 | return 0; |
| 7325 | } |
| 7326 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 7327 | /* The verifier does more data flow analysis than llvm and will not |
| 7328 | * explore branches that are dead at run time. Malicious programs can |
| 7329 | * have dead code too. Therefore replace all dead at-run-time code |
| 7330 | * with 'ja -1'. |
| 7331 | * |
| 7332 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 7333 | * program and through another bug we would manage to jump there, then |
| 7334 | * we'd execute beyond program memory otherwise. Returning exception |
| 7335 | * code also wouldn't work since we can have subprogs where the dead |
| 7336 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7337 | */ |
| 7338 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 7339 | { |
| 7340 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 7341 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7342 | struct bpf_insn *insn = env->prog->insnsi; |
| 7343 | const int insn_cnt = env->prog->len; |
| 7344 | int i; |
| 7345 | |
| 7346 | for (i = 0; i < insn_cnt; i++) { |
| 7347 | if (aux_data[i].seen) |
| 7348 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 7349 | memcpy(insn + i, &trap, sizeof(trap)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7350 | } |
| 7351 | } |
| 7352 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 7353 | static bool insn_is_cond_jump(u8 code) |
| 7354 | { |
| 7355 | u8 op; |
| 7356 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7357 | if (BPF_CLASS(code) == BPF_JMP32) |
| 7358 | return true; |
| 7359 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 7360 | if (BPF_CLASS(code) != BPF_JMP) |
| 7361 | return false; |
| 7362 | |
| 7363 | op = BPF_OP(code); |
| 7364 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; |
| 7365 | } |
| 7366 | |
| 7367 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) |
| 7368 | { |
| 7369 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 7370 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 7371 | struct bpf_insn *insn = env->prog->insnsi; |
| 7372 | const int insn_cnt = env->prog->len; |
| 7373 | int i; |
| 7374 | |
| 7375 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 7376 | if (!insn_is_cond_jump(insn->code)) |
| 7377 | continue; |
| 7378 | |
| 7379 | if (!aux_data[i + 1].seen) |
| 7380 | ja.off = insn->off; |
| 7381 | else if (!aux_data[i + 1 + insn->off].seen) |
| 7382 | ja.off = 0; |
| 7383 | else |
| 7384 | continue; |
| 7385 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 7386 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 7387 | bpf_prog_offload_replace_insn(env, i, &ja); |
| 7388 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 7389 | memcpy(insn, &ja, sizeof(ja)); |
| 7390 | } |
| 7391 | } |
| 7392 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 7393 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
| 7394 | { |
| 7395 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 7396 | int insn_cnt = env->prog->len; |
| 7397 | int i, err; |
| 7398 | |
| 7399 | for (i = 0; i < insn_cnt; i++) { |
| 7400 | int j; |
| 7401 | |
| 7402 | j = 0; |
| 7403 | while (i + j < insn_cnt && !aux_data[i + j].seen) |
| 7404 | j++; |
| 7405 | if (!j) |
| 7406 | continue; |
| 7407 | |
| 7408 | err = verifier_remove_insns(env, i, j); |
| 7409 | if (err) |
| 7410 | return err; |
| 7411 | insn_cnt = env->prog->len; |
| 7412 | } |
| 7413 | |
| 7414 | return 0; |
| 7415 | } |
| 7416 | |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 7417 | static int opt_remove_nops(struct bpf_verifier_env *env) |
| 7418 | { |
| 7419 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 7420 | struct bpf_insn *insn = env->prog->insnsi; |
| 7421 | int insn_cnt = env->prog->len; |
| 7422 | int i, err; |
| 7423 | |
| 7424 | for (i = 0; i < insn_cnt; i++) { |
| 7425 | if (memcmp(&insn[i], &ja, sizeof(ja))) |
| 7426 | continue; |
| 7427 | |
| 7428 | err = verifier_remove_insns(env, i, 1); |
| 7429 | if (err) |
| 7430 | return err; |
| 7431 | insn_cnt--; |
| 7432 | i--; |
| 7433 | } |
| 7434 | |
| 7435 | return 0; |
| 7436 | } |
| 7437 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7438 | /* convert load instructions that access fields of a context type into a |
| 7439 | * sequence of instructions that access fields of the underlying structure: |
| 7440 | * struct __sk_buff -> struct sk_buff |
| 7441 | * struct bpf_sock_ops -> struct sock |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7442 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7443 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7444 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 7445 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7446 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7447 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7448 | struct bpf_insn insn_buf[16], *insn; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7449 | u32 target_size, size_default, off; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7450 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7451 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7452 | bool is_narrower_load; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7453 | |
Daniel Borkmann | b09928b | 2018-10-24 22:05:49 +0200 | [diff] [blame] | 7454 | if (ops->gen_prologue || env->seen_direct_write) { |
| 7455 | if (!ops->gen_prologue) { |
| 7456 | verbose(env, "bpf verifier is misconfigured\n"); |
| 7457 | return -EINVAL; |
| 7458 | } |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7459 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 7460 | env->prog); |
| 7461 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7462 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7463 | return -EINVAL; |
| 7464 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7465 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7466 | if (!new_prog) |
| 7467 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7468 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7469 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7470 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7471 | } |
| 7472 | } |
| 7473 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7474 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7475 | return 0; |
| 7476 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7477 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7478 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7479 | for (i = 0; i < insn_cnt; i++, insn++) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7480 | bpf_convert_ctx_access_t convert_ctx_access; |
| 7481 | |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 7482 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 7483 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 7484 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 7485 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7486 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 7487 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 7488 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 7489 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 7490 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7491 | type = BPF_WRITE; |
| 7492 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7493 | continue; |
| 7494 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 7495 | if (type == BPF_WRITE && |
| 7496 | env->insn_aux_data[i + delta].sanitize_stack_off) { |
| 7497 | struct bpf_insn patch[] = { |
| 7498 | /* Sanitize suspicious stack slot with zero. |
| 7499 | * There are no memory dependencies for this store, |
| 7500 | * since it's only using frame pointer and immediate |
| 7501 | * constant of zero |
| 7502 | */ |
| 7503 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, |
| 7504 | env->insn_aux_data[i + delta].sanitize_stack_off, |
| 7505 | 0), |
| 7506 | /* the original STX instruction will immediately |
| 7507 | * overwrite the same stack slot with appropriate value |
| 7508 | */ |
| 7509 | *insn, |
| 7510 | }; |
| 7511 | |
| 7512 | cnt = ARRAY_SIZE(patch); |
| 7513 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 7514 | if (!new_prog) |
| 7515 | return -ENOMEM; |
| 7516 | |
| 7517 | delta += cnt - 1; |
| 7518 | env->prog = new_prog; |
| 7519 | insn = new_prog->insnsi + i + delta; |
| 7520 | continue; |
| 7521 | } |
| 7522 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7523 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 7524 | case PTR_TO_CTX: |
| 7525 | if (!ops->convert_ctx_access) |
| 7526 | continue; |
| 7527 | convert_ctx_access = ops->convert_ctx_access; |
| 7528 | break; |
| 7529 | case PTR_TO_SOCKET: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 7530 | case PTR_TO_SOCK_COMMON: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7531 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 7532 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 7533 | case PTR_TO_TCP_SOCK: |
| 7534 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; |
| 7535 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7536 | default: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7537 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7538 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7539 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7540 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7541 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7542 | |
| 7543 | /* If the read access is a narrower load of the field, |
| 7544 | * convert to a 4/8-byte load, to minimum program type specific |
| 7545 | * convert_ctx_access changes. If conversion is successful, |
| 7546 | * we will apply proper mask to the result. |
| 7547 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7548 | is_narrower_load = size < ctx_field_size; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7549 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
| 7550 | off = insn->off; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7551 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7552 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7553 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7554 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7555 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7556 | return -EINVAL; |
| 7557 | } |
| 7558 | |
| 7559 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7560 | if (ctx_field_size == 4) |
| 7561 | size_code = BPF_W; |
| 7562 | else if (ctx_field_size == 8) |
| 7563 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7564 | |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 7565 | insn->off = off & ~(size_default - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7566 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 7567 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7568 | |
| 7569 | target_size = 0; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7570 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 7571 | &target_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7572 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 7573 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7574 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7575 | return -EINVAL; |
| 7576 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7577 | |
| 7578 | if (is_narrower_load && size < target_size) { |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7579 | u8 shift = (off & (size_default - 1)) * 8; |
| 7580 | |
| 7581 | if (ctx_field_size <= 4) { |
| 7582 | if (shift) |
| 7583 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, |
| 7584 | insn->dst_reg, |
| 7585 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7586 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7587 | (1 << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7588 | } else { |
| 7589 | if (shift) |
| 7590 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, |
| 7591 | insn->dst_reg, |
| 7592 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7593 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Krzesimir Nowak | e2f7fc0 | 2019-05-08 18:08:58 +0200 | [diff] [blame] | 7594 | (1ULL << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7595 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7596 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7597 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7598 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7599 | if (!new_prog) |
| 7600 | return -ENOMEM; |
| 7601 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7602 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7603 | |
| 7604 | /* keep walking new program and skip insns we just inserted */ |
| 7605 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7606 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7607 | } |
| 7608 | |
| 7609 | return 0; |
| 7610 | } |
| 7611 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7612 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 7613 | { |
| 7614 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 7615 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7616 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7617 | void *old_bpf_func; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 7618 | int err; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7619 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7620 | if (env->subprog_cnt <= 1) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7621 | return 0; |
| 7622 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7623 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7624 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 7625 | insn->src_reg != BPF_PSEUDO_CALL) |
| 7626 | continue; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 7627 | /* Upon error here we cannot fall back to interpreter but |
| 7628 | * need a hard reject of the program. Thus -EFAULT is |
| 7629 | * propagated in any case. |
| 7630 | */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7631 | subprog = find_subprog(env, i + insn->imm + 1); |
| 7632 | if (subprog < 0) { |
| 7633 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 7634 | i + insn->imm + 1); |
| 7635 | return -EFAULT; |
| 7636 | } |
| 7637 | /* temporarily remember subprog id inside insn instead of |
| 7638 | * aux_data, since next loop will split up all insns into funcs |
| 7639 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7640 | insn->off = subprog; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7641 | /* remember original imm in case JIT fails and fallback |
| 7642 | * to interpreter will be needed |
| 7643 | */ |
| 7644 | env->insn_aux_data[i].call_imm = insn->imm; |
| 7645 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 7646 | insn->imm = 1; |
| 7647 | } |
| 7648 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 7649 | err = bpf_prog_alloc_jited_linfo(prog); |
| 7650 | if (err) |
| 7651 | goto out_undo_insn; |
| 7652 | |
| 7653 | err = -ENOMEM; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 7654 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7655 | if (!func) |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 7656 | goto out_undo_insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7657 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7658 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7659 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 7660 | subprog_end = env->subprog_info[i + 1].start; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7661 | |
| 7662 | len = subprog_end - subprog_start; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 7663 | /* BPF_PROG_RUN doesn't call subprogs directly, |
| 7664 | * hence main prog stats include the runtime of subprogs. |
| 7665 | * subprogs don't have IDs and not reachable via prog_get_next_id |
| 7666 | * func[i]->aux->stats will never be accessed and stays NULL |
| 7667 | */ |
| 7668 | 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] | 7669 | if (!func[i]) |
| 7670 | goto out_free; |
| 7671 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 7672 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 7673 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7674 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 7675 | if (bpf_prog_calc_tag(func[i])) |
| 7676 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7677 | func[i]->is_func = 1; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 7678 | func[i]->aux->func_idx = i; |
| 7679 | /* the btf and func_info will be freed only at prog->aux */ |
| 7680 | func[i]->aux->btf = prog->aux->btf; |
| 7681 | func[i]->aux->func_info = prog->aux->func_info; |
| 7682 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7683 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 7684 | * Long term would need debug info to populate names |
| 7685 | */ |
| 7686 | func[i]->aux->name[0] = 'F'; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 7687 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7688 | func[i]->jit_requested = 1; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 7689 | func[i]->aux->linfo = prog->aux->linfo; |
| 7690 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; |
| 7691 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; |
| 7692 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7693 | func[i] = bpf_int_jit_compile(func[i]); |
| 7694 | if (!func[i]->jited) { |
| 7695 | err = -ENOTSUPP; |
| 7696 | goto out_free; |
| 7697 | } |
| 7698 | cond_resched(); |
| 7699 | } |
| 7700 | /* at this point all bpf functions were successfully JITed |
| 7701 | * now populate all bpf_calls with correct addresses and |
| 7702 | * run last pass of JIT |
| 7703 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7704 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7705 | insn = func[i]->insnsi; |
| 7706 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 7707 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 7708 | insn->src_reg != BPF_PSEUDO_CALL) |
| 7709 | continue; |
| 7710 | subprog = insn->off; |
Prashant Bhole | 0d306c3 | 2019-04-16 18:13:01 +0900 | [diff] [blame] | 7711 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
| 7712 | __bpf_call_base; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7713 | } |
Sandipan Das | 2162fed | 2018-05-24 12:26:45 +0530 | [diff] [blame] | 7714 | |
| 7715 | /* we use the aux data to keep a list of the start addresses |
| 7716 | * of the JITed images for each function in the program |
| 7717 | * |
| 7718 | * for some architectures, such as powerpc64, the imm field |
| 7719 | * might not be large enough to hold the offset of the start |
| 7720 | * address of the callee's JITed image from __bpf_call_base |
| 7721 | * |
| 7722 | * in such cases, we can lookup the start address of a callee |
| 7723 | * by using its subprog id, available from the off field of |
| 7724 | * the call instruction, as an index for this list |
| 7725 | */ |
| 7726 | func[i]->aux->func = func; |
| 7727 | func[i]->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7728 | } |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7729 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7730 | old_bpf_func = func[i]->bpf_func; |
| 7731 | tmp = bpf_int_jit_compile(func[i]); |
| 7732 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 7733 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 7734 | err = -ENOTSUPP; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7735 | goto out_free; |
| 7736 | } |
| 7737 | cond_resched(); |
| 7738 | } |
| 7739 | |
| 7740 | /* finally lock prog and jit images for all functions and |
| 7741 | * populate kallsysm |
| 7742 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7743 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7744 | bpf_prog_lock_ro(func[i]); |
| 7745 | bpf_prog_kallsyms_add(func[i]); |
| 7746 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7747 | |
| 7748 | /* Last step: make now unused interpreter insns from main |
| 7749 | * prog consistent for later dump requests, so they can |
| 7750 | * later look the same as if they were interpreted only. |
| 7751 | */ |
| 7752 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7753 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 7754 | insn->src_reg != BPF_PSEUDO_CALL) |
| 7755 | continue; |
| 7756 | insn->off = env->insn_aux_data[i].call_imm; |
| 7757 | subprog = find_subprog(env, i + insn->off + 1); |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 7758 | insn->imm = subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7759 | } |
| 7760 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7761 | prog->jited = 1; |
| 7762 | prog->bpf_func = func[0]->bpf_func; |
| 7763 | prog->aux->func = func; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7764 | prog->aux->func_cnt = env->subprog_cnt; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 7765 | bpf_prog_free_unused_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7766 | return 0; |
| 7767 | out_free: |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7768 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7769 | if (func[i]) |
| 7770 | bpf_jit_free(func[i]); |
| 7771 | kfree(func); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 7772 | out_undo_insn: |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7773 | /* cleanup main prog to be interpreted */ |
| 7774 | prog->jit_requested = 0; |
| 7775 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 7776 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 7777 | insn->src_reg != BPF_PSEUDO_CALL) |
| 7778 | continue; |
| 7779 | insn->off = 0; |
| 7780 | insn->imm = env->insn_aux_data[i].call_imm; |
| 7781 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 7782 | bpf_prog_free_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7783 | return err; |
| 7784 | } |
| 7785 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 7786 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 7787 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 7788 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 7789 | struct bpf_prog *prog = env->prog; |
| 7790 | struct bpf_insn *insn = prog->insnsi; |
| 7791 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 7792 | #endif |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 7793 | int err = 0; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 7794 | |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 7795 | if (env->prog->jit_requested && |
| 7796 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 7797 | err = jit_subprogs(env); |
| 7798 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7799 | return 0; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 7800 | if (err == -EFAULT) |
| 7801 | return err; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 7802 | } |
| 7803 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 7804 | for (i = 0; i < prog->len; i++, insn++) { |
| 7805 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 7806 | insn->src_reg != BPF_PSEUDO_CALL) |
| 7807 | continue; |
| 7808 | depth = get_callee_stack_depth(env, insn, i); |
| 7809 | if (depth < 0) |
| 7810 | return depth; |
| 7811 | bpf_patch_call_args(insn, depth); |
| 7812 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 7813 | err = 0; |
| 7814 | #endif |
| 7815 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 7816 | } |
| 7817 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 7818 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 7819 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 7820 | * |
| 7821 | * this function is called after eBPF program passed verification |
| 7822 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 7823 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 7824 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 7825 | struct bpf_prog *prog = env->prog; |
| 7826 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 7827 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 7828 | const int insn_cnt = prog->len; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 7829 | const struct bpf_map_ops *ops; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 7830 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 7831 | struct bpf_insn insn_buf[16]; |
| 7832 | struct bpf_prog *new_prog; |
| 7833 | struct bpf_map *map_ptr; |
| 7834 | int i, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 7835 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 7836 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 7837 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 7838 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 7839 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 7840 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 7841 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
| 7842 | struct bpf_insn mask_and_div[] = { |
| 7843 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 7844 | /* Rx div 0 -> 0 */ |
| 7845 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), |
| 7846 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 7847 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 7848 | *insn, |
| 7849 | }; |
| 7850 | struct bpf_insn mask_and_mod[] = { |
| 7851 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 7852 | /* Rx mod 0 -> Rx */ |
| 7853 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), |
| 7854 | *insn, |
| 7855 | }; |
| 7856 | struct bpf_insn *patchlet; |
| 7857 | |
| 7858 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 7859 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 7860 | patchlet = mask_and_div + (is64 ? 1 : 0); |
| 7861 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); |
| 7862 | } else { |
| 7863 | patchlet = mask_and_mod + (is64 ? 1 : 0); |
| 7864 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); |
| 7865 | } |
| 7866 | |
| 7867 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 7868 | if (!new_prog) |
| 7869 | return -ENOMEM; |
| 7870 | |
| 7871 | delta += cnt - 1; |
| 7872 | env->prog = prog = new_prog; |
| 7873 | insn = new_prog->insnsi + i + delta; |
| 7874 | continue; |
| 7875 | } |
| 7876 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 7877 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 7878 | (BPF_MODE(insn->code) == BPF_ABS || |
| 7879 | BPF_MODE(insn->code) == BPF_IND)) { |
| 7880 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 7881 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 7882 | verbose(env, "bpf verifier is misconfigured\n"); |
| 7883 | return -EINVAL; |
| 7884 | } |
| 7885 | |
| 7886 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 7887 | if (!new_prog) |
| 7888 | return -ENOMEM; |
| 7889 | |
| 7890 | delta += cnt - 1; |
| 7891 | env->prog = prog = new_prog; |
| 7892 | insn = new_prog->insnsi + i + delta; |
| 7893 | continue; |
| 7894 | } |
| 7895 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7896 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
| 7897 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { |
| 7898 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; |
| 7899 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; |
| 7900 | struct bpf_insn insn_buf[16]; |
| 7901 | struct bpf_insn *patch = &insn_buf[0]; |
| 7902 | bool issrc, isneg; |
| 7903 | u32 off_reg; |
| 7904 | |
| 7905 | aux = &env->insn_aux_data[i + delta]; |
Daniel Borkmann | 3612af7 | 2019-03-01 22:05:29 +0100 | [diff] [blame] | 7906 | if (!aux->alu_state || |
| 7907 | aux->alu_state == BPF_ALU_NON_POINTER) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7908 | continue; |
| 7909 | |
| 7910 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; |
| 7911 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == |
| 7912 | BPF_ALU_SANITIZE_SRC; |
| 7913 | |
| 7914 | off_reg = issrc ? insn->src_reg : insn->dst_reg; |
| 7915 | if (isneg) |
| 7916 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 7917 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); |
| 7918 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); |
| 7919 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); |
| 7920 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); |
| 7921 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); |
| 7922 | if (issrc) { |
| 7923 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, |
| 7924 | off_reg); |
| 7925 | insn->src_reg = BPF_REG_AX; |
| 7926 | } else { |
| 7927 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, |
| 7928 | BPF_REG_AX); |
| 7929 | } |
| 7930 | if (isneg) |
| 7931 | insn->code = insn->code == code_add ? |
| 7932 | code_sub : code_add; |
| 7933 | *patch++ = *insn; |
| 7934 | if (issrc && isneg) |
| 7935 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 7936 | cnt = patch - insn_buf; |
| 7937 | |
| 7938 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 7939 | if (!new_prog) |
| 7940 | return -ENOMEM; |
| 7941 | |
| 7942 | delta += cnt - 1; |
| 7943 | env->prog = prog = new_prog; |
| 7944 | insn = new_prog->insnsi + i + delta; |
| 7945 | continue; |
| 7946 | } |
| 7947 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 7948 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 7949 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7950 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 7951 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 7952 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 7953 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 7954 | prog->dst_needed = 1; |
| 7955 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 7956 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 7957 | if (insn->imm == BPF_FUNC_override_return) |
| 7958 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 7959 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 7960 | /* If we tail call into other programs, we |
| 7961 | * cannot make any assumptions since they can |
| 7962 | * be replaced dynamically during runtime in |
| 7963 | * the program array. |
| 7964 | */ |
| 7965 | prog->cb_access = 1; |
Alexei Starovoitov | 80a58d0 | 2017-05-30 13:31:30 -0700 | [diff] [blame] | 7966 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 7967 | env->prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 7968 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 7969 | /* mark bpf_tail_call as different opcode to avoid |
| 7970 | * conditional branch in the interpeter for every normal |
| 7971 | * call and to prevent accidental JITing by JIT compiler |
| 7972 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 7973 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 7974 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 7975 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 7976 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 7977 | aux = &env->insn_aux_data[i + delta]; |
| 7978 | if (!bpf_map_ptr_unpriv(aux)) |
| 7979 | continue; |
| 7980 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 7981 | /* instead of changing every JIT dealing with tail_call |
| 7982 | * emit two extra insns: |
| 7983 | * if (index >= max_entries) goto out; |
| 7984 | * index &= array->index_mask; |
| 7985 | * to avoid out-of-bounds cpu speculation |
| 7986 | */ |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 7987 | if (bpf_map_ptr_poisoned(aux)) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 7988 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 7989 | return -EINVAL; |
| 7990 | } |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 7991 | |
| 7992 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 7993 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 7994 | map_ptr->max_entries, 2); |
| 7995 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 7996 | container_of(map_ptr, |
| 7997 | struct bpf_array, |
| 7998 | map)->index_mask); |
| 7999 | insn_buf[2] = *insn; |
| 8000 | cnt = 3; |
| 8001 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 8002 | if (!new_prog) |
| 8003 | return -ENOMEM; |
| 8004 | |
| 8005 | delta += cnt - 1; |
| 8006 | env->prog = prog = new_prog; |
| 8007 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8008 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8009 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8010 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 8011 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8012 | * and other inlining handlers are currently limited to 64 bit |
| 8013 | * only. |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 8014 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 8015 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8016 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 8017 | insn->imm == BPF_FUNC_map_update_elem || |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 8018 | insn->imm == BPF_FUNC_map_delete_elem || |
| 8019 | insn->imm == BPF_FUNC_map_push_elem || |
| 8020 | insn->imm == BPF_FUNC_map_pop_elem || |
| 8021 | insn->imm == BPF_FUNC_map_peek_elem)) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 8022 | aux = &env->insn_aux_data[i + delta]; |
| 8023 | if (bpf_map_ptr_poisoned(aux)) |
| 8024 | goto patch_call_imm; |
| 8025 | |
| 8026 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8027 | ops = map_ptr->ops; |
| 8028 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 8029 | ops->map_gen_lookup) { |
| 8030 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
| 8031 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 8032 | verbose(env, "bpf verifier is misconfigured\n"); |
| 8033 | return -EINVAL; |
| 8034 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8035 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8036 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 8037 | insn_buf, cnt); |
| 8038 | if (!new_prog) |
| 8039 | return -ENOMEM; |
| 8040 | |
| 8041 | delta += cnt - 1; |
| 8042 | env->prog = prog = new_prog; |
| 8043 | insn = new_prog->insnsi + i + delta; |
| 8044 | continue; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8045 | } |
| 8046 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8047 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 8048 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 8049 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 8050 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 8051 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 8052 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 8053 | u64 flags))NULL)); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 8054 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 8055 | (int (*)(struct bpf_map *map, void *value, |
| 8056 | u64 flags))NULL)); |
| 8057 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 8058 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 8059 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 8060 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 8061 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8062 | switch (insn->imm) { |
| 8063 | case BPF_FUNC_map_lookup_elem: |
| 8064 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - |
| 8065 | __bpf_call_base; |
| 8066 | continue; |
| 8067 | case BPF_FUNC_map_update_elem: |
| 8068 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - |
| 8069 | __bpf_call_base; |
| 8070 | continue; |
| 8071 | case BPF_FUNC_map_delete_elem: |
| 8072 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - |
| 8073 | __bpf_call_base; |
| 8074 | continue; |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 8075 | case BPF_FUNC_map_push_elem: |
| 8076 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - |
| 8077 | __bpf_call_base; |
| 8078 | continue; |
| 8079 | case BPF_FUNC_map_pop_elem: |
| 8080 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - |
| 8081 | __bpf_call_base; |
| 8082 | continue; |
| 8083 | case BPF_FUNC_map_peek_elem: |
| 8084 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - |
| 8085 | __bpf_call_base; |
| 8086 | continue; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8087 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8088 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8089 | goto patch_call_imm; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8090 | } |
| 8091 | |
| 8092 | patch_call_imm: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 8093 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8094 | /* all functions that have prototype and verifier allowed |
| 8095 | * programs to call them, must be real in-kernel functions |
| 8096 | */ |
| 8097 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8098 | verbose(env, |
| 8099 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8100 | func_id_name(insn->imm), insn->imm); |
| 8101 | return -EFAULT; |
| 8102 | } |
| 8103 | insn->imm = fn->func - __bpf_call_base; |
| 8104 | } |
| 8105 | |
| 8106 | return 0; |
| 8107 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8108 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8109 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8110 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8111 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8112 | int i; |
| 8113 | |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 8114 | sl = env->free_list; |
| 8115 | while (sl) { |
| 8116 | sln = sl->next; |
| 8117 | free_verifier_state(&sl->state, false); |
| 8118 | kfree(sl); |
| 8119 | sl = sln; |
| 8120 | } |
| 8121 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8122 | if (!env->explored_states) |
| 8123 | return; |
| 8124 | |
| 8125 | for (i = 0; i < env->prog->len; i++) { |
| 8126 | sl = env->explored_states[i]; |
| 8127 | |
| 8128 | if (sl) |
| 8129 | while (sl != STATE_LIST_MARK) { |
| 8130 | sln = sl->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 8131 | free_verifier_state(&sl->state, false); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8132 | kfree(sl); |
| 8133 | sl = sln; |
| 8134 | } |
| 8135 | } |
| 8136 | |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 8137 | kvfree(env->explored_states); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8138 | } |
| 8139 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 8140 | static void print_verification_stats(struct bpf_verifier_env *env) |
| 8141 | { |
| 8142 | int i; |
| 8143 | |
| 8144 | if (env->log.level & BPF_LOG_STATS) { |
| 8145 | verbose(env, "verification time %lld usec\n", |
| 8146 | div_u64(env->verification_time, 1000)); |
| 8147 | verbose(env, "stack depth "); |
| 8148 | for (i = 0; i < env->subprog_cnt; i++) { |
| 8149 | u32 depth = env->subprog_info[i].stack_depth; |
| 8150 | |
| 8151 | verbose(env, "%d", depth); |
| 8152 | if (i + 1 < env->subprog_cnt) |
| 8153 | verbose(env, "+"); |
| 8154 | } |
| 8155 | verbose(env, "\n"); |
| 8156 | } |
| 8157 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " |
| 8158 | "total_states %d peak_states %d mark_read %d\n", |
| 8159 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, |
| 8160 | env->max_states_per_insn, env->total_states, |
| 8161 | env->peak_states, env->longest_mark_read_walk); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8162 | } |
| 8163 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8164 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
| 8165 | union bpf_attr __user *uattr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 8166 | { |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 8167 | u64 start_time = ktime_get_ns(); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8168 | struct bpf_verifier_env *env; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 8169 | struct bpf_verifier_log *log; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 8170 | int i, len, ret = -EINVAL; |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8171 | bool is_priv; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 8172 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 8173 | /* no program is valid */ |
| 8174 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 8175 | return -EINVAL; |
| 8176 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8177 | /* '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] | 8178 | * allocate/free it every time bpf_check() is called |
| 8179 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8180 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8181 | if (!env) |
| 8182 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8183 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8184 | |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 8185 | len = (*prog)->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 8186 | env->insn_aux_data = |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 8187 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8188 | ret = -ENOMEM; |
| 8189 | if (!env->insn_aux_data) |
| 8190 | goto err_free_env; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 8191 | for (i = 0; i < len; i++) |
| 8192 | env->insn_aux_data[i].orig_idx = i; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8193 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 8194 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 8195 | is_priv = capable(CAP_SYS_ADMIN); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8196 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8197 | /* grab the mutex to protect few globals used by verifier */ |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 8198 | if (!is_priv) |
| 8199 | mutex_lock(&bpf_verifier_lock); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8200 | |
| 8201 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 8202 | /* user requested verbose verifier output |
| 8203 | * and supplied buffer to store the verification trace |
| 8204 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 8205 | log->level = attr->log_level; |
| 8206 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 8207 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8208 | |
| 8209 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 8210 | /* log attributes have to be sane */ |
Alexei Starovoitov | 7a9f5c6 | 2019-04-01 21:27:46 -0700 | [diff] [blame] | 8211 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 8212 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8213 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8214 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 8215 | |
| 8216 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 8217 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 8218 | env->strict_alignment = true; |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 8219 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
| 8220 | env->strict_alignment = false; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8221 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8222 | env->allow_ptr_leaks = is_priv; |
| 8223 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8224 | ret = replace_map_fd_with_map_ptr(env); |
| 8225 | if (ret < 0) |
| 8226 | goto skip_full_check; |
| 8227 | |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 8228 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Quentin Monnet | a40a263 | 2018-11-09 13:03:31 +0000 | [diff] [blame] | 8229 | ret = bpf_prog_offload_verifier_prep(env->prog); |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 8230 | if (ret) |
| 8231 | goto skip_full_check; |
| 8232 | } |
| 8233 | |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 8234 | env->explored_states = kvcalloc(env->prog->len, |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8235 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8236 | GFP_USER); |
| 8237 | ret = -ENOMEM; |
| 8238 | if (!env->explored_states) |
| 8239 | goto skip_full_check; |
| 8240 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 8241 | ret = check_subprogs(env); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8242 | if (ret < 0) |
| 8243 | goto skip_full_check; |
| 8244 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8245 | ret = check_btf_info(env, attr, uattr); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8246 | if (ret < 0) |
| 8247 | goto skip_full_check; |
| 8248 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 8249 | ret = check_cfg(env); |
| 8250 | if (ret < 0) |
| 8251 | goto skip_full_check; |
| 8252 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8253 | ret = do_check(env); |
Craig Gallek | 8c01c4f | 2017-11-02 11:18:01 -0400 | [diff] [blame] | 8254 | if (env->cur_state) { |
| 8255 | free_verifier_state(env->cur_state, true); |
| 8256 | env->cur_state = NULL; |
| 8257 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8258 | |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 8259 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 8260 | ret = bpf_prog_offload_finalize(env); |
| 8261 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8262 | skip_full_check: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8263 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8264 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8265 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8266 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 8267 | ret = check_max_stack_depth(env); |
| 8268 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 8269 | /* instruction rewrites happen after this point */ |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8270 | if (is_priv) { |
| 8271 | if (ret == 0) |
| 8272 | opt_hard_wire_dead_code_branches(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 8273 | if (ret == 0) |
| 8274 | ret = opt_remove_dead_code(env); |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 8275 | if (ret == 0) |
| 8276 | ret = opt_remove_nops(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 8277 | } else { |
| 8278 | if (ret == 0) |
| 8279 | sanitize_dead_code(env); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8280 | } |
| 8281 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 8282 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8283 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 8284 | ret = convert_ctx_accesses(env); |
| 8285 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8286 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8287 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8288 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 8289 | if (ret == 0) |
| 8290 | ret = fixup_call_args(env); |
| 8291 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 8292 | env->verification_time = ktime_get_ns() - start_time; |
| 8293 | print_verification_stats(env); |
| 8294 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8295 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8296 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8297 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8298 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8299 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8300 | } |
| 8301 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8302 | if (ret == 0 && env->used_map_cnt) { |
| 8303 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8304 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 8305 | sizeof(env->used_maps[0]), |
| 8306 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8307 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8308 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8309 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8310 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8311 | } |
| 8312 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8313 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8314 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8315 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8316 | |
| 8317 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 8318 | * bpf_ld_imm64 instructions |
| 8319 | */ |
| 8320 | convert_pseudo_ld_imm64(env); |
| 8321 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8322 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8323 | if (ret == 0) |
| 8324 | adjust_btf_func(env); |
| 8325 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8326 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8327 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8328 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 8329 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8330 | */ |
| 8331 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8332 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8333 | err_unlock: |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 8334 | if (!is_priv) |
| 8335 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8336 | vfree(env->insn_aux_data); |
| 8337 | err_free_env: |
| 8338 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 8339 | return ret; |
| 8340 | } |