Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016 Facebook |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3 | * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of version 2 of the GNU General Public |
| 7 | * License as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 14 | #include <uapi/linux/btf.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/bpf.h> |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 19 | #include <linux/btf.h> |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 20 | #include <linux/bpf_verifier.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 21 | #include <linux/filter.h> |
| 22 | #include <net/netlink.h> |
| 23 | #include <linux/file.h> |
| 24 | #include <linux/vmalloc.h> |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 25 | #include <linux/stringify.h> |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 26 | #include <linux/bsearch.h> |
| 27 | #include <linux/sort.h> |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 28 | #include <linux/perf_event.h> |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 29 | #include <linux/ctype.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 30 | |
Jakub Kicinski | f4ac7e0 | 2017-10-09 10:30:12 -0700 | [diff] [blame] | 31 | #include "disasm.h" |
| 32 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 33 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
| 34 | #define BPF_PROG_TYPE(_id, _name) \ |
| 35 | [_id] = & _name ## _verifier_ops, |
| 36 | #define BPF_MAP_TYPE(_id, _ops) |
| 37 | #include <linux/bpf_types.h> |
| 38 | #undef BPF_PROG_TYPE |
| 39 | #undef BPF_MAP_TYPE |
| 40 | }; |
| 41 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 42 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 43 | * instruction by instruction and updates register/stack state. |
| 44 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 45 | * |
| 46 | * The first pass is depth-first-search to check that the program is a DAG. |
| 47 | * It rejects the following programs: |
| 48 | * - larger than BPF_MAXINSNS insns |
| 49 | * - if loop is present (detected via back-edge) |
| 50 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 51 | * - out of bounds or malformed jumps |
| 52 | * The second pass is all possible path descent from the 1st insn. |
| 53 | * 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] | 54 | * 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] | 55 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 56 | * Number of 'branches to be analyzed' is limited to 1k |
| 57 | * |
| 58 | * On entry to each instruction, each register has a type, and the instruction |
| 59 | * changes the types of the registers depending on instruction semantics. |
| 60 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 61 | * copied to R1. |
| 62 | * |
| 63 | * All registers are 64-bit. |
| 64 | * R0 - return register |
| 65 | * R1-R5 argument passing registers |
| 66 | * R6-R9 callee saved registers |
| 67 | * R10 - frame pointer read-only |
| 68 | * |
| 69 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 70 | * and has type PTR_TO_CTX. |
| 71 | * |
| 72 | * Verifier tracks arithmetic operations on pointers in case: |
| 73 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 74 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 75 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 76 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 77 | * that it wants to construct a pointer to some element within stack. |
| 78 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 79 | * (and -20 constant is saved for further stack bounds checking). |
| 80 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 81 | * |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 82 | * Most of the time the registers have SCALAR_VALUE type, which |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 83 | * 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] | 84 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 85 | * |
| 86 | * When verifier sees load or store instructions the type of base register |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 87 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
| 88 | * four pointer types recognized by check_mem_access() function. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 89 | * |
| 90 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 91 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 92 | * |
| 93 | * registers used to pass values to function calls are checked against |
| 94 | * function argument constraints. |
| 95 | * |
| 96 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 97 | * It means that the register type passed to this function must be |
| 98 | * PTR_TO_STACK and it will be used inside the function as |
| 99 | * 'pointer to map element key' |
| 100 | * |
| 101 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 102 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 103 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 104 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 105 | * |
| 106 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 107 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 108 | * 2nd argument should be a pointer to stack, which will be used inside |
| 109 | * the helper function as a pointer to map element key. |
| 110 | * |
| 111 | * On the kernel side the helper function looks like: |
| 112 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 113 | * { |
| 114 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 115 | * void *key = (void *) (unsigned long) r2; |
| 116 | * void *value; |
| 117 | * |
| 118 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 119 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 120 | * the stack of eBPF program. |
| 121 | * } |
| 122 | * |
| 123 | * Corresponding eBPF program may look like: |
| 124 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 125 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 126 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 127 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 128 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 129 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 130 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 131 | * |
| 132 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 133 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 134 | * and were initialized prior to this call. |
| 135 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 136 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 137 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 138 | * returns ether pointer to map value or NULL. |
| 139 | * |
| 140 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 141 | * insn, the register holding that pointer in the true branch changes state to |
| 142 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 143 | * branch. See check_cond_jmp_op(). |
| 144 | * |
| 145 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 146 | * 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] | 147 | * |
| 148 | * The following reference types represent a potential reference to a kernel |
| 149 | * resource which, after first being allocated, must be checked and freed by |
| 150 | * the BPF program: |
| 151 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET |
| 152 | * |
| 153 | * When the verifier sees a helper call return a reference type, it allocates a |
| 154 | * pointer id for the reference and stores it in the current function state. |
| 155 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into |
| 156 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type |
| 157 | * passes through a NULL-check conditional. For the branch wherein the state is |
| 158 | * changed to CONST_IMM, the verifier releases the reference. |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 159 | * |
| 160 | * For each helper function that allocates a reference, such as |
| 161 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as |
| 162 | * bpf_sk_release(). When a reference type passes into the release function, |
| 163 | * the verifier also releases the reference. If any unchecked or unreleased |
| 164 | * reference remains at the end of the program, the verifier rejects it. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 165 | */ |
| 166 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 167 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 168 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 169 | /* verifer state is 'st' |
| 170 | * before processing instruction 'insn_idx' |
| 171 | * and after processing instruction 'prev_insn_idx' |
| 172 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 173 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 174 | int insn_idx; |
| 175 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 176 | struct bpf_verifier_stack_elem *next; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 179 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 180 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 181 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 182 | #define BPF_MAP_PTR_UNPRIV 1UL |
| 183 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ |
| 184 | POISON_POINTER_DELTA)) |
| 185 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) |
| 186 | |
| 187 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
| 188 | { |
| 189 | return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON; |
| 190 | } |
| 191 | |
| 192 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) |
| 193 | { |
| 194 | return aux->map_state & BPF_MAP_PTR_UNPRIV; |
| 195 | } |
| 196 | |
| 197 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, |
| 198 | const struct bpf_map *map, bool unpriv) |
| 199 | { |
| 200 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); |
| 201 | unpriv |= bpf_map_ptr_unpriv(aux); |
| 202 | aux->map_state = (unsigned long)map | |
| 203 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); |
| 204 | } |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 205 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 206 | struct bpf_call_arg_meta { |
| 207 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 208 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 209 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 210 | int regno; |
| 211 | int access_size; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 212 | s64 msize_smax_value; |
| 213 | u64 msize_umax_value; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 214 | int ref_obj_id; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 215 | int func_id; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 216 | }; |
| 217 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 218 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 219 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 220 | static const struct bpf_line_info * |
| 221 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) |
| 222 | { |
| 223 | const struct bpf_line_info *linfo; |
| 224 | const struct bpf_prog *prog; |
| 225 | u32 i, nr_linfo; |
| 226 | |
| 227 | prog = env->prog; |
| 228 | nr_linfo = prog->aux->nr_linfo; |
| 229 | |
| 230 | if (!nr_linfo || insn_off >= prog->len) |
| 231 | return NULL; |
| 232 | |
| 233 | linfo = prog->aux->linfo; |
| 234 | for (i = 1; i < nr_linfo; i++) |
| 235 | if (insn_off < linfo[i].insn_off) |
| 236 | break; |
| 237 | |
| 238 | return &linfo[i - 1]; |
| 239 | } |
| 240 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 241 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
| 242 | va_list args) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 243 | { |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 244 | unsigned int n; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 245 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 246 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 247 | |
| 248 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 249 | "verifier log line truncated - local buffer too short\n"); |
| 250 | |
| 251 | n = min(log->len_total - log->len_used - 1, n); |
| 252 | log->kbuf[n] = '\0'; |
| 253 | |
| 254 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 255 | log->len_used += n; |
| 256 | else |
| 257 | log->ubuf = NULL; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 258 | } |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 259 | |
| 260 | /* log_level controls verbosity level of eBPF verifier. |
| 261 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 262 | * so the user can figure out what's wrong with the program |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 263 | */ |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 264 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 265 | const char *fmt, ...) |
| 266 | { |
| 267 | va_list args; |
| 268 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 269 | if (!bpf_verifier_log_needed(&env->log)) |
| 270 | return; |
| 271 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 272 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 273 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 274 | va_end(args); |
| 275 | } |
| 276 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 277 | |
| 278 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
| 279 | { |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 280 | struct bpf_verifier_env *env = private_data; |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 281 | va_list args; |
| 282 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 283 | if (!bpf_verifier_log_needed(&env->log)) |
| 284 | return; |
| 285 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 286 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 287 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 288 | va_end(args); |
| 289 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 290 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 291 | static const char *ltrim(const char *s) |
| 292 | { |
| 293 | while (isspace(*s)) |
| 294 | s++; |
| 295 | |
| 296 | return s; |
| 297 | } |
| 298 | |
| 299 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, |
| 300 | u32 insn_off, |
| 301 | const char *prefix_fmt, ...) |
| 302 | { |
| 303 | const struct bpf_line_info *linfo; |
| 304 | |
| 305 | if (!bpf_verifier_log_needed(&env->log)) |
| 306 | return; |
| 307 | |
| 308 | linfo = find_linfo(env, insn_off); |
| 309 | if (!linfo || linfo == env->prev_linfo) |
| 310 | return; |
| 311 | |
| 312 | if (prefix_fmt) { |
| 313 | va_list args; |
| 314 | |
| 315 | va_start(args, prefix_fmt); |
| 316 | bpf_verifier_vlog(&env->log, prefix_fmt, args); |
| 317 | va_end(args); |
| 318 | } |
| 319 | |
| 320 | verbose(env, "%s\n", |
| 321 | ltrim(btf_name_by_offset(env->prog->aux->btf, |
| 322 | linfo->line_off))); |
| 323 | |
| 324 | env->prev_linfo = linfo; |
| 325 | } |
| 326 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 327 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 328 | { |
| 329 | return type == PTR_TO_PACKET || |
| 330 | type == PTR_TO_PACKET_META; |
| 331 | } |
| 332 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 333 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
| 334 | { |
| 335 | return type == PTR_TO_SOCKET || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 336 | type == PTR_TO_SOCK_COMMON || |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 337 | type == PTR_TO_TCP_SOCK || |
| 338 | type == PTR_TO_XDP_SOCK; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 341 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
| 342 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 343 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 344 | type == PTR_TO_SOCKET_OR_NULL || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 345 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
| 346 | type == PTR_TO_TCP_SOCK_OR_NULL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 349 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
| 350 | { |
| 351 | return reg->type == PTR_TO_MAP_VALUE && |
| 352 | map_value_has_spin_lock(reg->map_ptr); |
| 353 | } |
| 354 | |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 355 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
| 356 | { |
| 357 | return type == PTR_TO_SOCKET || |
| 358 | type == PTR_TO_SOCKET_OR_NULL || |
| 359 | type == PTR_TO_TCP_SOCK || |
| 360 | type == PTR_TO_TCP_SOCK_OR_NULL; |
| 361 | } |
| 362 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 363 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 364 | { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 365 | return type == ARG_PTR_TO_SOCK_COMMON; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /* Determine whether the function releases some resources allocated by another |
| 369 | * function call. The first reference type argument will be assumed to be |
| 370 | * released by release_reference(). |
| 371 | */ |
| 372 | static bool is_release_function(enum bpf_func_id func_id) |
| 373 | { |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 374 | return func_id == BPF_FUNC_sk_release; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 377 | static bool is_acquire_function(enum bpf_func_id func_id) |
| 378 | { |
| 379 | return func_id == BPF_FUNC_sk_lookup_tcp || |
Lorenz Bauer | edbf8c0 | 2019-03-22 09:54:01 +0800 | [diff] [blame] | 380 | func_id == BPF_FUNC_sk_lookup_udp || |
| 381 | func_id == BPF_FUNC_skc_lookup_tcp; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 384 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
| 385 | { |
| 386 | return func_id == BPF_FUNC_tcp_sock || |
| 387 | func_id == BPF_FUNC_sk_fullsock; |
| 388 | } |
| 389 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 390 | /* string representation of 'enum bpf_reg_type' */ |
| 391 | static const char * const reg_type_str[] = { |
| 392 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 393 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 394 | [PTR_TO_CTX] = "ctx", |
| 395 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 396 | [PTR_TO_MAP_VALUE] = "map_value", |
| 397 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 398 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 399 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 400 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 401 | [PTR_TO_PACKET_END] = "pkt_end", |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 402 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 403 | [PTR_TO_SOCKET] = "sock", |
| 404 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 405 | [PTR_TO_SOCK_COMMON] = "sock_common", |
| 406 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 407 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
| 408 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 409 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 410 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 411 | }; |
| 412 | |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 413 | static char slot_type_char[] = { |
| 414 | [STACK_INVALID] = '?', |
| 415 | [STACK_SPILL] = 'r', |
| 416 | [STACK_MISC] = 'm', |
| 417 | [STACK_ZERO] = '0', |
| 418 | }; |
| 419 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 420 | static void print_liveness(struct bpf_verifier_env *env, |
| 421 | enum bpf_reg_liveness live) |
| 422 | { |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 423 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 424 | verbose(env, "_"); |
| 425 | if (live & REG_LIVE_READ) |
| 426 | verbose(env, "r"); |
| 427 | if (live & REG_LIVE_WRITTEN) |
| 428 | verbose(env, "w"); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 429 | if (live & REG_LIVE_DONE) |
| 430 | verbose(env, "D"); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 433 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 434 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 435 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 436 | struct bpf_verifier_state *cur = env->cur_state; |
| 437 | |
| 438 | return cur->frame[reg->frameno]; |
| 439 | } |
| 440 | |
| 441 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 442 | const struct bpf_func_state *state) |
| 443 | { |
| 444 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 445 | enum bpf_reg_type t; |
| 446 | int i; |
| 447 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 448 | if (state->frameno) |
| 449 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 450 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 451 | reg = &state->regs[i]; |
| 452 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 453 | if (t == NOT_INIT) |
| 454 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 455 | verbose(env, " R%d", i); |
| 456 | print_liveness(env, reg->live); |
| 457 | verbose(env, "=%s", reg_type_str[t]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 458 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 459 | tnum_is_const(reg->var_off)) { |
| 460 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 461 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 462 | if (t == PTR_TO_STACK) |
| 463 | verbose(env, ",call_%d", func(env, reg)->callsite); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 464 | } else { |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 465 | verbose(env, "(id=%d", reg->id); |
| 466 | if (reg_type_may_be_refcounted_or_null(t)) |
| 467 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 468 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 469 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 470 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 471 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 472 | else if (t == CONST_PTR_TO_MAP || |
| 473 | t == PTR_TO_MAP_VALUE || |
| 474 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 475 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 476 | reg->map_ptr->key_size, |
| 477 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 478 | if (tnum_is_const(reg->var_off)) { |
| 479 | /* Typically an immediate SCALAR_VALUE, but |
| 480 | * could be a pointer whose offset is too big |
| 481 | * for reg->off |
| 482 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 483 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 484 | } else { |
| 485 | if (reg->smin_value != reg->umin_value && |
| 486 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 487 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 488 | (long long)reg->smin_value); |
| 489 | if (reg->smax_value != reg->umax_value && |
| 490 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 491 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 492 | (long long)reg->smax_value); |
| 493 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 494 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 495 | (unsigned long long)reg->umin_value); |
| 496 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 497 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 498 | (unsigned long long)reg->umax_value); |
| 499 | if (!tnum_is_unknown(reg->var_off)) { |
| 500 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 501 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 502 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 503 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 504 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 505 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 506 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 507 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 508 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 509 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 510 | char types_buf[BPF_REG_SIZE + 1]; |
| 511 | bool valid = false; |
| 512 | int j; |
| 513 | |
| 514 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 515 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 516 | valid = true; |
| 517 | types_buf[j] = slot_type_char[ |
| 518 | state->stack[i].slot_type[j]]; |
| 519 | } |
| 520 | types_buf[BPF_REG_SIZE] = 0; |
| 521 | if (!valid) |
| 522 | continue; |
| 523 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 524 | print_liveness(env, state->stack[i].spilled_ptr.live); |
| 525 | if (state->stack[i].slot_type[0] == STACK_SPILL) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 526 | verbose(env, "=%s", |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 527 | reg_type_str[state->stack[i].spilled_ptr.type]); |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 528 | else |
| 529 | verbose(env, "=%s", types_buf); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 530 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 531 | if (state->acquired_refs && state->refs[0].id) { |
| 532 | verbose(env, " refs=%d", state->refs[0].id); |
| 533 | for (i = 1; i < state->acquired_refs; i++) |
| 534 | if (state->refs[i].id) |
| 535 | verbose(env, ",%d", state->refs[i].id); |
| 536 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 537 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 540 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 541 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ |
| 542 | const struct bpf_func_state *src) \ |
| 543 | { \ |
| 544 | if (!src->FIELD) \ |
| 545 | return 0; \ |
| 546 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ |
| 547 | /* internal bug, make state invalid to reject the program */ \ |
| 548 | memset(dst, 0, sizeof(*dst)); \ |
| 549 | return -EFAULT; \ |
| 550 | } \ |
| 551 | memcpy(dst->FIELD, src->FIELD, \ |
| 552 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ |
| 553 | return 0; \ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 554 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 555 | /* copy_reference_state() */ |
| 556 | COPY_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 557 | /* copy_stack_state() */ |
| 558 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 559 | #undef COPY_STATE_FN |
| 560 | |
| 561 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 562 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ |
| 563 | bool copy_old) \ |
| 564 | { \ |
| 565 | u32 old_size = state->COUNT; \ |
| 566 | struct bpf_##NAME##_state *new_##FIELD; \ |
| 567 | int slot = size / SIZE; \ |
| 568 | \ |
| 569 | if (size <= old_size || !size) { \ |
| 570 | if (copy_old) \ |
| 571 | return 0; \ |
| 572 | state->COUNT = slot * SIZE; \ |
| 573 | if (!size && old_size) { \ |
| 574 | kfree(state->FIELD); \ |
| 575 | state->FIELD = NULL; \ |
| 576 | } \ |
| 577 | return 0; \ |
| 578 | } \ |
| 579 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ |
| 580 | GFP_KERNEL); \ |
| 581 | if (!new_##FIELD) \ |
| 582 | return -ENOMEM; \ |
| 583 | if (copy_old) { \ |
| 584 | if (state->FIELD) \ |
| 585 | memcpy(new_##FIELD, state->FIELD, \ |
| 586 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ |
| 587 | memset(new_##FIELD + old_size / SIZE, 0, \ |
| 588 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ |
| 589 | } \ |
| 590 | state->COUNT = slot * SIZE; \ |
| 591 | kfree(state->FIELD); \ |
| 592 | state->FIELD = new_##FIELD; \ |
| 593 | return 0; \ |
| 594 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 595 | /* realloc_reference_state() */ |
| 596 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 597 | /* realloc_stack_state() */ |
| 598 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 599 | #undef REALLOC_STATE_FN |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 600 | |
| 601 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 602 | * make it consume minimal amount of memory. check_stack_write() access from |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 603 | * the program calls into realloc_func_state() to grow the stack size. |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 604 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 605 | * which realloc_stack_state() copies over. It points to previous |
| 606 | * bpf_verifier_state which is never reallocated. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 607 | */ |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 608 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
| 609 | int refs_size, bool copy_old) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 610 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 611 | int err = realloc_reference_state(state, refs_size, copy_old); |
| 612 | if (err) |
| 613 | return err; |
| 614 | return realloc_stack_state(state, stack_size, copy_old); |
| 615 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 616 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 617 | /* Acquire a pointer id from the env and update the state->refs to include |
| 618 | * this new pointer reference. |
| 619 | * On success, returns a valid pointer id to associate with the register |
| 620 | * On failure, returns a negative errno. |
| 621 | */ |
| 622 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 623 | { |
| 624 | struct bpf_func_state *state = cur_func(env); |
| 625 | int new_ofs = state->acquired_refs; |
| 626 | int id, err; |
| 627 | |
| 628 | err = realloc_reference_state(state, state->acquired_refs + 1, true); |
| 629 | if (err) |
| 630 | return err; |
| 631 | id = ++env->id_gen; |
| 632 | state->refs[new_ofs].id = id; |
| 633 | state->refs[new_ofs].insn_idx = insn_idx; |
| 634 | |
| 635 | return id; |
| 636 | } |
| 637 | |
| 638 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 639 | 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] | 640 | { |
| 641 | int i, last_idx; |
| 642 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 643 | last_idx = state->acquired_refs - 1; |
| 644 | for (i = 0; i < state->acquired_refs; i++) { |
| 645 | if (state->refs[i].id == ptr_id) { |
| 646 | if (last_idx && i != last_idx) |
| 647 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 648 | sizeof(*state->refs)); |
| 649 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 650 | state->acquired_refs--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 651 | return 0; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 652 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 653 | } |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 654 | return -EINVAL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static int transfer_reference_state(struct bpf_func_state *dst, |
| 658 | struct bpf_func_state *src) |
| 659 | { |
| 660 | int err = realloc_reference_state(dst, src->acquired_refs, false); |
| 661 | if (err) |
| 662 | return err; |
| 663 | err = copy_reference_state(dst, src); |
| 664 | if (err) |
| 665 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 666 | return 0; |
| 667 | } |
| 668 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 669 | static void free_func_state(struct bpf_func_state *state) |
| 670 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 671 | if (!state) |
| 672 | return; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 673 | kfree(state->refs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 674 | kfree(state->stack); |
| 675 | kfree(state); |
| 676 | } |
| 677 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 678 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 679 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 680 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 681 | int i; |
| 682 | |
| 683 | for (i = 0; i <= state->curframe; i++) { |
| 684 | free_func_state(state->frame[i]); |
| 685 | state->frame[i] = NULL; |
| 686 | } |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 687 | if (free_self) |
| 688 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* copy verifier state from src to dst growing dst stack space |
| 692 | * when necessary to accommodate larger src stack |
| 693 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 694 | static int copy_func_state(struct bpf_func_state *dst, |
| 695 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 696 | { |
| 697 | int err; |
| 698 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 699 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
| 700 | false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 701 | if (err) |
| 702 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 703 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 704 | err = copy_reference_state(dst, src); |
| 705 | if (err) |
| 706 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 707 | return copy_stack_state(dst, src); |
| 708 | } |
| 709 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 710 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 711 | const struct bpf_verifier_state *src) |
| 712 | { |
| 713 | struct bpf_func_state *dst; |
| 714 | int i, err; |
| 715 | |
| 716 | /* if dst has more stack frames then src frame, free them */ |
| 717 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 718 | free_func_state(dst_state->frame[i]); |
| 719 | dst_state->frame[i] = NULL; |
| 720 | } |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 721 | dst_state->speculative = src->speculative; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 722 | dst_state->curframe = src->curframe; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 723 | dst_state->active_spin_lock = src->active_spin_lock; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 724 | for (i = 0; i <= src->curframe; i++) { |
| 725 | dst = dst_state->frame[i]; |
| 726 | if (!dst) { |
| 727 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 728 | if (!dst) |
| 729 | return -ENOMEM; |
| 730 | dst_state->frame[i] = dst; |
| 731 | } |
| 732 | err = copy_func_state(dst, src->frame[i]); |
| 733 | if (err) |
| 734 | return err; |
| 735 | } |
| 736 | return 0; |
| 737 | } |
| 738 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 739 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
| 740 | int *insn_idx) |
| 741 | { |
| 742 | struct bpf_verifier_state *cur = env->cur_state; |
| 743 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 744 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 745 | |
| 746 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 747 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 748 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 749 | if (cur) { |
| 750 | err = copy_verifier_state(cur, &head->st); |
| 751 | if (err) |
| 752 | return err; |
| 753 | } |
| 754 | if (insn_idx) |
| 755 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 756 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 757 | *prev_insn_idx = head->prev_insn_idx; |
| 758 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 759 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 760 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 761 | env->head = elem; |
| 762 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 763 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 766 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 767 | int insn_idx, int prev_insn_idx, |
| 768 | bool speculative) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 769 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 770 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 771 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 772 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 773 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 774 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 775 | if (!elem) |
| 776 | goto err; |
| 777 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 778 | elem->insn_idx = insn_idx; |
| 779 | elem->prev_insn_idx = prev_insn_idx; |
| 780 | elem->next = env->head; |
| 781 | env->head = elem; |
| 782 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 783 | err = copy_verifier_state(&elem->st, cur); |
| 784 | if (err) |
| 785 | goto err; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 786 | elem->st.speculative |= speculative; |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 787 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
| 788 | verbose(env, "The sequence of %d jumps is too complex.\n", |
| 789 | env->stack_size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 790 | goto err; |
| 791 | } |
| 792 | return &elem->st; |
| 793 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 794 | free_verifier_state(env->cur_state, true); |
| 795 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 796 | /* pop all elements and return */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 797 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 798 | return NULL; |
| 799 | } |
| 800 | |
| 801 | #define CALLER_SAVED_REGS 6 |
| 802 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 803 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 804 | }; |
| 805 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 806 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
| 807 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 808 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 809 | * known to have the value @imm. |
| 810 | */ |
| 811 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 812 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 813 | /* Clear id, off, and union(map_ptr, range) */ |
| 814 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 815 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 816 | reg->var_off = tnum_const(imm); |
| 817 | reg->smin_value = (s64)imm; |
| 818 | reg->smax_value = (s64)imm; |
| 819 | reg->umin_value = imm; |
| 820 | reg->umax_value = imm; |
| 821 | } |
| 822 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 823 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 824 | * used only on registers holding a pointer type. |
| 825 | */ |
| 826 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 827 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 828 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 831 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 832 | { |
| 833 | __mark_reg_known(reg, 0); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 834 | reg->type = SCALAR_VALUE; |
| 835 | } |
| 836 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 837 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 838 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 839 | { |
| 840 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 841 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 842 | /* Something bad happened, let's kill all regs */ |
| 843 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 844 | __mark_reg_not_init(regs + regno); |
| 845 | return; |
| 846 | } |
| 847 | __mark_reg_known_zero(regs + regno); |
| 848 | } |
| 849 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 850 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 851 | { |
| 852 | return type_is_pkt_pointer(reg->type); |
| 853 | } |
| 854 | |
| 855 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 856 | { |
| 857 | return reg_is_pkt_pointer(reg) || |
| 858 | reg->type == PTR_TO_PACKET_END; |
| 859 | } |
| 860 | |
| 861 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 862 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 863 | enum bpf_reg_type which) |
| 864 | { |
| 865 | /* The register can already have a range from prior markings. |
| 866 | * This is fine as long as it hasn't been advanced from its |
| 867 | * origin. |
| 868 | */ |
| 869 | return reg->type == which && |
| 870 | reg->id == 0 && |
| 871 | reg->off == 0 && |
| 872 | tnum_equals_const(reg->var_off, 0); |
| 873 | } |
| 874 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 875 | /* Attempts to improve min/max values based on var_off information */ |
| 876 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 877 | { |
| 878 | /* min signed is max(sign bit) | min(other bits) */ |
| 879 | reg->smin_value = max_t(s64, reg->smin_value, |
| 880 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 881 | /* max signed is min(sign bit) | max(other bits) */ |
| 882 | reg->smax_value = min_t(s64, reg->smax_value, |
| 883 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 884 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 885 | reg->umax_value = min(reg->umax_value, |
| 886 | reg->var_off.value | reg->var_off.mask); |
| 887 | } |
| 888 | |
| 889 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
| 890 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 891 | { |
| 892 | /* Learn sign from signed bounds. |
| 893 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 894 | * are the same, so combine. This works even in the negative case, e.g. |
| 895 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 896 | */ |
| 897 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 898 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 899 | reg->umin_value); |
| 900 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 901 | reg->umax_value); |
| 902 | return; |
| 903 | } |
| 904 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 905 | * boundary, so we must be careful. |
| 906 | */ |
| 907 | if ((s64)reg->umax_value >= 0) { |
| 908 | /* Positive. We can't learn anything from the smin, but smax |
| 909 | * is positive, hence safe. |
| 910 | */ |
| 911 | reg->smin_value = reg->umin_value; |
| 912 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 913 | reg->umax_value); |
| 914 | } else if ((s64)reg->umin_value < 0) { |
| 915 | /* Negative. We can't learn anything from the smax, but smin |
| 916 | * is negative, hence safe. |
| 917 | */ |
| 918 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 919 | reg->umin_value); |
| 920 | reg->smax_value = reg->umax_value; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 925 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 926 | { |
| 927 | reg->var_off = tnum_intersect(reg->var_off, |
| 928 | tnum_range(reg->umin_value, |
| 929 | reg->umax_value)); |
| 930 | } |
| 931 | |
| 932 | /* Reset the min/max bounds of a register */ |
| 933 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 934 | { |
| 935 | reg->smin_value = S64_MIN; |
| 936 | reg->smax_value = S64_MAX; |
| 937 | reg->umin_value = 0; |
| 938 | reg->umax_value = U64_MAX; |
| 939 | } |
| 940 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 941 | /* Mark a register as having a completely unknown (scalar) value. */ |
| 942 | static void __mark_reg_unknown(struct bpf_reg_state *reg) |
| 943 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 944 | /* |
| 945 | * Clear type, id, off, and union(map_ptr, range) and |
| 946 | * padding between 'type' and union |
| 947 | */ |
| 948 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 949 | reg->type = SCALAR_VALUE; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 950 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 951 | reg->frameno = 0; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 952 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 953 | } |
| 954 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 955 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 956 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 957 | { |
| 958 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 959 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 960 | /* Something bad happened, let's kill all regs except FP */ |
| 961 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 962 | __mark_reg_not_init(regs + regno); |
| 963 | return; |
| 964 | } |
| 965 | __mark_reg_unknown(regs + regno); |
| 966 | } |
| 967 | |
| 968 | static void __mark_reg_not_init(struct bpf_reg_state *reg) |
| 969 | { |
| 970 | __mark_reg_unknown(reg); |
| 971 | reg->type = NOT_INIT; |
| 972 | } |
| 973 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 974 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 975 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 976 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 977 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 978 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 979 | /* Something bad happened, let's kill all regs except FP */ |
| 980 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 981 | __mark_reg_not_init(regs + regno); |
| 982 | return; |
| 983 | } |
| 984 | __mark_reg_not_init(regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 985 | } |
| 986 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 987 | #define DEF_NOT_SUBREG (0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 988 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 989 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 990 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 991 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 992 | int i; |
| 993 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 994 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 995 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 996 | regs[i].live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 997 | regs[i].parent = NULL; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 998 | regs[i].subreg_def = DEF_NOT_SUBREG; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 999 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1000 | |
| 1001 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1002 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1003 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1004 | regs[BPF_REG_FP].frameno = state->frameno; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1005 | |
| 1006 | /* 1st arg to a function */ |
| 1007 | regs[BPF_REG_1].type = PTR_TO_CTX; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1008 | mark_reg_known_zero(env, regs, BPF_REG_1); |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 1009 | } |
| 1010 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1011 | #define BPF_MAIN_FUNC (-1) |
| 1012 | static void init_func_state(struct bpf_verifier_env *env, |
| 1013 | struct bpf_func_state *state, |
| 1014 | int callsite, int frameno, int subprogno) |
| 1015 | { |
| 1016 | state->callsite = callsite; |
| 1017 | state->frameno = frameno; |
| 1018 | state->subprogno = subprogno; |
| 1019 | init_reg_state(env, state); |
| 1020 | } |
| 1021 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1022 | enum reg_arg_type { |
| 1023 | SRC_OP, /* register is used as source operand */ |
| 1024 | DST_OP, /* register is used as destination operand */ |
| 1025 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 1026 | }; |
| 1027 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1028 | static int cmp_subprogs(const void *a, const void *b) |
| 1029 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1030 | return ((struct bpf_subprog_info *)a)->start - |
| 1031 | ((struct bpf_subprog_info *)b)->start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 1035 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1036 | struct bpf_subprog_info *p; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1037 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1038 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 1039 | sizeof(env->subprog_info[0]), cmp_subprogs); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1040 | if (!p) |
| 1041 | return -ENOENT; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1042 | return p - env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1043 | |
| 1044 | } |
| 1045 | |
| 1046 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 1047 | { |
| 1048 | int insn_cnt = env->prog->len; |
| 1049 | int ret; |
| 1050 | |
| 1051 | if (off >= insn_cnt || off < 0) { |
| 1052 | verbose(env, "call to invalid destination\n"); |
| 1053 | return -EINVAL; |
| 1054 | } |
| 1055 | ret = find_subprog(env, off); |
| 1056 | if (ret >= 0) |
| 1057 | return 0; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1058 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1059 | verbose(env, "too many subprograms\n"); |
| 1060 | return -E2BIG; |
| 1061 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1062 | env->subprog_info[env->subprog_cnt++].start = off; |
| 1063 | sort(env->subprog_info, env->subprog_cnt, |
| 1064 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | static int check_subprogs(struct bpf_verifier_env *env) |
| 1069 | { |
| 1070 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1071 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1072 | struct bpf_insn *insn = env->prog->insnsi; |
| 1073 | int insn_cnt = env->prog->len; |
| 1074 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 1075 | /* Add entry function. */ |
| 1076 | ret = add_subprog(env, 0); |
| 1077 | if (ret < 0) |
| 1078 | return ret; |
| 1079 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1080 | /* determine subprog starts. The end is one before the next starts */ |
| 1081 | for (i = 0; i < insn_cnt; i++) { |
| 1082 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1083 | continue; |
| 1084 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1085 | continue; |
| 1086 | if (!env->allow_ptr_leaks) { |
| 1087 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); |
| 1088 | return -EPERM; |
| 1089 | } |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1090 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 1091 | if (ret < 0) |
| 1092 | return ret; |
| 1093 | } |
| 1094 | |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1095 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 1096 | * logic. 'subprog_cnt' should not be increased. |
| 1097 | */ |
| 1098 | subprog[env->subprog_cnt].start = insn_cnt; |
| 1099 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1100 | if (env->log.level & BPF_LOG_LEVEL2) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1101 | for (i = 0; i < env->subprog_cnt; i++) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1102 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1103 | |
| 1104 | /* now check that all jumps are within the same subprog */ |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1105 | subprog_start = subprog[cur_subprog].start; |
| 1106 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1107 | for (i = 0; i < insn_cnt; i++) { |
| 1108 | u8 code = insn[i].code; |
| 1109 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 1110 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1111 | goto next; |
| 1112 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 1113 | goto next; |
| 1114 | off = i + insn[i].off + 1; |
| 1115 | if (off < subprog_start || off >= subprog_end) { |
| 1116 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 1117 | return -EINVAL; |
| 1118 | } |
| 1119 | next: |
| 1120 | if (i == subprog_end - 1) { |
| 1121 | /* to avoid fall-through from one subprog into another |
| 1122 | * the last insn of the subprog should be either exit |
| 1123 | * or unconditional jump back |
| 1124 | */ |
| 1125 | if (code != (BPF_JMP | BPF_EXIT) && |
| 1126 | code != (BPF_JMP | BPF_JA)) { |
| 1127 | verbose(env, "last insn is not an exit or jmp\n"); |
| 1128 | return -EINVAL; |
| 1129 | } |
| 1130 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1131 | cur_subprog++; |
| 1132 | if (cur_subprog < env->subprog_cnt) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1133 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1139 | /* Parentage chain of this register (or stack slot) should take care of all |
| 1140 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 1141 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1142 | static int mark_reg_read(struct bpf_verifier_env *env, |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1143 | const struct bpf_reg_state *state, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1144 | struct bpf_reg_state *parent, u8 flag) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1145 | { |
| 1146 | bool writes = parent == state->parent; /* Observe write marks */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1147 | int cnt = 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1148 | |
| 1149 | while (parent) { |
| 1150 | /* if read wasn't screened by an earlier write ... */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1151 | if (writes && state->live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1152 | break; |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 1153 | if (parent->live & REG_LIVE_DONE) { |
| 1154 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", |
| 1155 | reg_type_str[parent->type], |
| 1156 | parent->var_off.value, parent->off); |
| 1157 | return -EFAULT; |
| 1158 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1159 | /* The first condition is more likely to be true than the |
| 1160 | * second, checked it first. |
| 1161 | */ |
| 1162 | if ((parent->live & REG_LIVE_READ) == flag || |
| 1163 | parent->live & REG_LIVE_READ64) |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 1164 | /* The parentage chain never changes and |
| 1165 | * this parent was already marked as LIVE_READ. |
| 1166 | * There is no need to keep walking the chain again and |
| 1167 | * keep re-marking all parents as LIVE_READ. |
| 1168 | * This case happens when the same register is read |
| 1169 | * multiple times without writes into it in-between. |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1170 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
| 1171 | * then no need to set the weak REG_LIVE_READ32. |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 1172 | */ |
| 1173 | break; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1174 | /* ... then we depend on parent's value */ |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1175 | parent->live |= flag; |
| 1176 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ |
| 1177 | if (flag == REG_LIVE_READ64) |
| 1178 | parent->live &= ~REG_LIVE_READ32; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1179 | state = parent; |
| 1180 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1181 | writes = true; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1182 | cnt++; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1183 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1184 | |
| 1185 | if (env->longest_mark_read_walk < cnt) |
| 1186 | env->longest_mark_read_walk = cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1187 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1188 | } |
| 1189 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1190 | /* This function is supposed to be used by the following 32-bit optimization |
| 1191 | * code only. It returns TRUE if the source or destination register operates |
| 1192 | * on 64-bit, otherwise return FALSE. |
| 1193 | */ |
| 1194 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 1195 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) |
| 1196 | { |
| 1197 | u8 code, class, op; |
| 1198 | |
| 1199 | code = insn->code; |
| 1200 | class = BPF_CLASS(code); |
| 1201 | op = BPF_OP(code); |
| 1202 | if (class == BPF_JMP) { |
| 1203 | /* BPF_EXIT for "main" will reach here. Return TRUE |
| 1204 | * conservatively. |
| 1205 | */ |
| 1206 | if (op == BPF_EXIT) |
| 1207 | return true; |
| 1208 | if (op == BPF_CALL) { |
| 1209 | /* BPF to BPF call will reach here because of marking |
| 1210 | * caller saved clobber with DST_OP_NO_MARK for which we |
| 1211 | * don't care the register def because they are anyway |
| 1212 | * marked as NOT_INIT already. |
| 1213 | */ |
| 1214 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1215 | return false; |
| 1216 | /* Helper call will reach here because of arg type |
| 1217 | * check, conservatively return TRUE. |
| 1218 | */ |
| 1219 | if (t == SRC_OP) |
| 1220 | return true; |
| 1221 | |
| 1222 | return false; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | if (class == BPF_ALU64 || class == BPF_JMP || |
| 1227 | /* BPF_END always use BPF_ALU class. */ |
| 1228 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) |
| 1229 | return true; |
| 1230 | |
| 1231 | if (class == BPF_ALU || class == BPF_JMP32) |
| 1232 | return false; |
| 1233 | |
| 1234 | if (class == BPF_LDX) { |
| 1235 | if (t != SRC_OP) |
| 1236 | return BPF_SIZE(code) == BPF_DW; |
| 1237 | /* LDX source must be ptr. */ |
| 1238 | return true; |
| 1239 | } |
| 1240 | |
| 1241 | if (class == BPF_STX) { |
| 1242 | if (reg->type != SCALAR_VALUE) |
| 1243 | return true; |
| 1244 | return BPF_SIZE(code) == BPF_DW; |
| 1245 | } |
| 1246 | |
| 1247 | if (class == BPF_LD) { |
| 1248 | u8 mode = BPF_MODE(code); |
| 1249 | |
| 1250 | /* LD_IMM64 */ |
| 1251 | if (mode == BPF_IMM) |
| 1252 | return true; |
| 1253 | |
| 1254 | /* Both LD_IND and LD_ABS return 32-bit data. */ |
| 1255 | if (t != SRC_OP) |
| 1256 | return false; |
| 1257 | |
| 1258 | /* Implicit ctx ptr. */ |
| 1259 | if (regno == BPF_REG_6) |
| 1260 | return true; |
| 1261 | |
| 1262 | /* Explicit source could be any width. */ |
| 1263 | return true; |
| 1264 | } |
| 1265 | |
| 1266 | if (class == BPF_ST) |
| 1267 | /* The only source register for BPF_ST is a ptr. */ |
| 1268 | return true; |
| 1269 | |
| 1270 | /* Conservatively return true at default. */ |
| 1271 | return true; |
| 1272 | } |
| 1273 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 1274 | /* Return TRUE if INSN doesn't have explicit value define. */ |
| 1275 | static bool insn_no_def(struct bpf_insn *insn) |
| 1276 | { |
| 1277 | u8 class = BPF_CLASS(insn->code); |
| 1278 | |
| 1279 | return (class == BPF_JMP || class == BPF_JMP32 || |
| 1280 | class == BPF_STX || class == BPF_ST); |
| 1281 | } |
| 1282 | |
| 1283 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ |
| 1284 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 1285 | { |
| 1286 | if (insn_no_def(insn)) |
| 1287 | return false; |
| 1288 | |
| 1289 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); |
| 1290 | } |
| 1291 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1292 | static void mark_insn_zext(struct bpf_verifier_env *env, |
| 1293 | struct bpf_reg_state *reg) |
| 1294 | { |
| 1295 | s32 def_idx = reg->subreg_def; |
| 1296 | |
| 1297 | if (def_idx == DEF_NOT_SUBREG) |
| 1298 | return; |
| 1299 | |
| 1300 | env->insn_aux_data[def_idx - 1].zext_dst = true; |
| 1301 | /* The dst will be zero extended, so won't be sub-register anymore. */ |
| 1302 | reg->subreg_def = DEF_NOT_SUBREG; |
| 1303 | } |
| 1304 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1305 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1306 | enum reg_arg_type t) |
| 1307 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1308 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1309 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1310 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1311 | struct bpf_reg_state *reg, *regs = state->regs; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1312 | bool rw64; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1313 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1314 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1315 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1316 | return -EINVAL; |
| 1317 | } |
| 1318 | |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1319 | reg = ®s[regno]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1320 | rw64 = is_reg64(env, insn, regno, reg, t); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1321 | if (t == SRC_OP) { |
| 1322 | /* check whether register used as source operand can be read */ |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1323 | if (reg->type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1324 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1325 | return -EACCES; |
| 1326 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1327 | /* 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] | 1328 | if (regno == BPF_REG_FP) |
| 1329 | return 0; |
| 1330 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1331 | if (rw64) |
| 1332 | mark_insn_zext(env, reg); |
| 1333 | |
| 1334 | return mark_reg_read(env, reg, reg->parent, |
| 1335 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1336 | } else { |
| 1337 | /* check whether register used as dest operand can be written to */ |
| 1338 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1339 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1340 | return -EACCES; |
| 1341 | } |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1342 | reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1343 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1344 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1345 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1346 | } |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1350 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 1351 | { |
| 1352 | switch (type) { |
| 1353 | case PTR_TO_MAP_VALUE: |
| 1354 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 1355 | case PTR_TO_STACK: |
| 1356 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1357 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1358 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1359 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1360 | case PTR_TO_FLOW_KEYS: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1361 | case CONST_PTR_TO_MAP: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1362 | case PTR_TO_SOCKET: |
| 1363 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1364 | case PTR_TO_SOCK_COMMON: |
| 1365 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 1366 | case PTR_TO_TCP_SOCK: |
| 1367 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 1368 | case PTR_TO_XDP_SOCK: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1369 | return true; |
| 1370 | default: |
| 1371 | return false; |
| 1372 | } |
| 1373 | } |
| 1374 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1375 | /* Does this register contain a constant zero? */ |
| 1376 | static bool register_is_null(struct bpf_reg_state *reg) |
| 1377 | { |
| 1378 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 1379 | } |
| 1380 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1381 | static bool register_is_const(struct bpf_reg_state *reg) |
| 1382 | { |
| 1383 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); |
| 1384 | } |
| 1385 | |
| 1386 | static void save_register_state(struct bpf_func_state *state, |
| 1387 | int spi, struct bpf_reg_state *reg) |
| 1388 | { |
| 1389 | int i; |
| 1390 | |
| 1391 | state->stack[spi].spilled_ptr = *reg; |
| 1392 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 1393 | |
| 1394 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 1395 | state->stack[spi].slot_type[i] = STACK_SPILL; |
| 1396 | } |
| 1397 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1398 | /* check_stack_read/write functions track spill/fill of registers, |
| 1399 | * stack boundary and alignment are checked in check_mem_access() |
| 1400 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1401 | static int check_stack_write(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1402 | struct bpf_func_state *state, /* func where register points to */ |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1403 | int off, int size, int value_regno, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1404 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1405 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1406 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1407 | struct bpf_reg_state *reg = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1408 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1409 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 1410 | state->acquired_refs, true); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1411 | if (err) |
| 1412 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1413 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 1414 | * so it's aligned access and [off, off + size) are within stack limits |
| 1415 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1416 | if (!env->allow_ptr_leaks && |
| 1417 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 1418 | size != BPF_REG_SIZE) { |
| 1419 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 1420 | return -EACCES; |
| 1421 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1422 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1423 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1424 | if (value_regno >= 0) |
| 1425 | reg = &cur->regs[value_regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1426 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1427 | if (reg && size == BPF_REG_SIZE && register_is_const(reg) && |
| 1428 | !register_is_null(reg) && env->allow_ptr_leaks) { |
| 1429 | save_register_state(state, spi, reg); |
| 1430 | } else if (reg && is_spillable_regtype(reg->type)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1431 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1432 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1433 | verbose_linfo(env, insn_idx, "; "); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1434 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1435 | return -EACCES; |
| 1436 | } |
| 1437 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1438 | if (state != cur && reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1439 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 1440 | return -EINVAL; |
| 1441 | } |
| 1442 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1443 | if (!env->allow_ptr_leaks) { |
| 1444 | bool sanitize = false; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1445 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1446 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 1447 | register_is_const(&state->stack[spi].spilled_ptr)) |
| 1448 | sanitize = true; |
| 1449 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 1450 | if (state->stack[spi].slot_type[i] == STACK_MISC) { |
| 1451 | sanitize = true; |
| 1452 | break; |
| 1453 | } |
| 1454 | if (sanitize) { |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1455 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
| 1456 | int soff = (-spi - 1) * BPF_REG_SIZE; |
| 1457 | |
| 1458 | /* detected reuse of integer stack slot with a pointer |
| 1459 | * which means either llvm is reusing stack slot or |
| 1460 | * an attacker is trying to exploit CVE-2018-3639 |
| 1461 | * (speculative store bypass) |
| 1462 | * Have to sanitize that slot with preemptive |
| 1463 | * store of zero. |
| 1464 | */ |
| 1465 | if (*poff && *poff != soff) { |
| 1466 | /* disallow programs where single insn stores |
| 1467 | * into two different stack slots, since verifier |
| 1468 | * cannot sanitize them |
| 1469 | */ |
| 1470 | verbose(env, |
| 1471 | "insn %d cannot access two stack slots fp%d and fp%d", |
| 1472 | insn_idx, *poff, soff); |
| 1473 | return -EINVAL; |
| 1474 | } |
| 1475 | *poff = soff; |
| 1476 | } |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1477 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1478 | save_register_state(state, spi, reg); |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1479 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1480 | u8 type = STACK_MISC; |
| 1481 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1482 | /* regular write of data into stack destroys any spilled ptr */ |
| 1483 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 1484 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
| 1485 | if (state->stack[spi].slot_type[0] == STACK_SPILL) |
| 1486 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 1487 | state->stack[spi].slot_type[i] = STACK_MISC; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1488 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1489 | /* only mark the slot as written if all 8 bytes were written |
| 1490 | * otherwise read propagation may incorrectly stop too soon |
| 1491 | * when stack slots are partially written. |
| 1492 | * This heuristic means that read propagation will be |
| 1493 | * conservative, since it will add reg_live_read marks |
| 1494 | * to stack slots all the way to first state when programs |
| 1495 | * writes+reads less than 8 bytes |
| 1496 | */ |
| 1497 | if (size == BPF_REG_SIZE) |
| 1498 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 1499 | |
| 1500 | /* when we zero initialize stack slots mark them as such */ |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1501 | if (reg && register_is_null(reg)) |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1502 | type = STACK_ZERO; |
| 1503 | |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 1504 | /* Mark slots affected by this stack write. */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1505 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1506 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1507 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1508 | } |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1512 | static int check_stack_read(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1513 | struct bpf_func_state *reg_state /* func where register points to */, |
| 1514 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1515 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1516 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1517 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1518 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1519 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1520 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1521 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1522 | if (reg_state->allocated_stack <= slot) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1523 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 1524 | off, size); |
| 1525 | return -EACCES; |
| 1526 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1527 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1528 | reg = ®_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1529 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1530 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1531 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1532 | if (reg->type != SCALAR_VALUE) { |
| 1533 | verbose_linfo(env, env->insn_idx, "; "); |
| 1534 | verbose(env, "invalid size of register fill\n"); |
| 1535 | return -EACCES; |
| 1536 | } |
| 1537 | if (value_regno >= 0) { |
| 1538 | mark_reg_unknown(env, state->regs, value_regno); |
| 1539 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 1540 | } |
| 1541 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
| 1542 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1543 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1544 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1545 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1546 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1547 | return -EACCES; |
| 1548 | } |
| 1549 | } |
| 1550 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1551 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1552 | /* restore register state from stack */ |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1553 | state->regs[value_regno] = *reg; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 1554 | /* mark reg as written since spilled pointer state likely |
| 1555 | * has its liveness marks cleared by is_state_visited() |
| 1556 | * which resets stack/reg liveness for state transitions |
| 1557 | */ |
| 1558 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1559 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1560 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1561 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1562 | int zeros = 0; |
| 1563 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1564 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1565 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 1566 | continue; |
| 1567 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 1568 | zeros++; |
| 1569 | continue; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1570 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1571 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 1572 | off, i, size); |
| 1573 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1574 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1575 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1576 | if (value_regno >= 0) { |
| 1577 | if (zeros == size) { |
| 1578 | /* any size read into register is zero extended, |
| 1579 | * so the whole register == const_zero |
| 1580 | */ |
| 1581 | __mark_reg_const_zero(&state->regs[value_regno]); |
| 1582 | } else { |
| 1583 | /* have read misc data from the stack */ |
| 1584 | mark_reg_unknown(env, state->regs, value_regno); |
| 1585 | } |
| 1586 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 1587 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1588 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 1589 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 1592 | static int check_stack_access(struct bpf_verifier_env *env, |
| 1593 | const struct bpf_reg_state *reg, |
| 1594 | int off, int size) |
| 1595 | { |
| 1596 | /* Stack accesses must be at a fixed offset, so that we |
| 1597 | * can determine what type of data were returned. See |
| 1598 | * check_stack_read(). |
| 1599 | */ |
| 1600 | if (!tnum_is_const(reg->var_off)) { |
| 1601 | char tn_buf[48]; |
| 1602 | |
| 1603 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Andrey Ignatov | 1fbd20f | 2019-04-03 23:22:43 -0700 | [diff] [blame] | 1604 | 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] | 1605 | tn_buf, off, size); |
| 1606 | return -EACCES; |
| 1607 | } |
| 1608 | |
| 1609 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 1610 | verbose(env, "invalid stack off=%d size=%d\n", off, size); |
| 1611 | return -EACCES; |
| 1612 | } |
| 1613 | |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 1617 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
| 1618 | int off, int size, enum bpf_access_type type) |
| 1619 | { |
| 1620 | struct bpf_reg_state *regs = cur_regs(env); |
| 1621 | struct bpf_map *map = regs[regno].map_ptr; |
| 1622 | u32 cap = bpf_map_flags_to_cap(map); |
| 1623 | |
| 1624 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { |
| 1625 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", |
| 1626 | map->value_size, off, size); |
| 1627 | return -EACCES; |
| 1628 | } |
| 1629 | |
| 1630 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { |
| 1631 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", |
| 1632 | map->value_size, off, size); |
| 1633 | return -EACCES; |
| 1634 | } |
| 1635 | |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1639 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1640 | 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] | 1641 | int size, bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1642 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1643 | struct bpf_reg_state *regs = cur_regs(env); |
| 1644 | struct bpf_map *map = regs[regno].map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1645 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1646 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1647 | off + size > map->value_size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1648 | 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] | 1649 | map->value_size, off, size); |
| 1650 | return -EACCES; |
| 1651 | } |
| 1652 | return 0; |
| 1653 | } |
| 1654 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1655 | /* check read/write into a map element with possible variable offset */ |
| 1656 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1657 | int off, int size, bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1658 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1659 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1660 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1661 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 1662 | int err; |
| 1663 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1664 | /* We may have adjusted the register to this map value, so we |
| 1665 | * need to try adding each of min_value and max_value to off |
| 1666 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1667 | */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1668 | if (env->log.level & BPF_LOG_LEVEL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1669 | print_verifier_state(env, state); |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 1670 | |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1671 | /* The minimum value is only important with signed |
| 1672 | * comparisons where we can't assume the floor of a |
| 1673 | * value is 0. If we are using signed variables for our |
| 1674 | * index'es we need to make sure that whatever we use |
| 1675 | * will have a set floor within our range. |
| 1676 | */ |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 1677 | if (reg->smin_value < 0 && |
| 1678 | (reg->smin_value == S64_MIN || |
| 1679 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || |
| 1680 | reg->smin_value + off < 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1681 | 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] | 1682 | regno); |
| 1683 | return -EACCES; |
| 1684 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1685 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
| 1686 | zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1687 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1688 | verbose(env, "R%d min value is outside of the array range\n", |
| 1689 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1690 | return err; |
| 1691 | } |
| 1692 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1693 | /* If we haven't set a max value then we need to bail since we can't be |
| 1694 | * sure we won't do bad things. |
| 1695 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1696 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1697 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1698 | 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] | 1699 | regno); |
| 1700 | return -EACCES; |
| 1701 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1702 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
| 1703 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1704 | if (err) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1705 | verbose(env, "R%d max value is outside of the array range\n", |
| 1706 | regno); |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 1707 | |
| 1708 | if (map_value_has_spin_lock(reg->map_ptr)) { |
| 1709 | u32 lock = reg->map_ptr->spin_lock_off; |
| 1710 | |
| 1711 | /* if any part of struct bpf_spin_lock can be touched by |
| 1712 | * load/store reject this program. |
| 1713 | * To check that [x1, x2) overlaps with [y1, y2) |
| 1714 | * it is sufficient to check x1 < y2 && y1 < x2. |
| 1715 | */ |
| 1716 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && |
| 1717 | lock < reg->umax_value + off + size) { |
| 1718 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); |
| 1719 | return -EACCES; |
| 1720 | } |
| 1721 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1722 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1723 | } |
| 1724 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1725 | #define MAX_PACKET_OFF 0xffff |
| 1726 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1727 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1728 | const struct bpf_call_arg_meta *meta, |
| 1729 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1730 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1731 | switch (env->prog->type) { |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1732 | /* Program types only with direct read access go here! */ |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1733 | case BPF_PROG_TYPE_LWT_IN: |
| 1734 | case BPF_PROG_TYPE_LWT_OUT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 1735 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 1736 | case BPF_PROG_TYPE_SK_REUSEPORT: |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1737 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Daniel Borkmann | d5563d3 | 2018-10-24 22:05:46 +0200 | [diff] [blame] | 1738 | case BPF_PROG_TYPE_CGROUP_SKB: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1739 | if (t == BPF_WRITE) |
| 1740 | return false; |
Alexander Alemayhu | 7e57fbb | 2017-02-14 00:02:35 +0100 | [diff] [blame] | 1741 | /* fallthrough */ |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1742 | |
| 1743 | /* Program types with direct read + write access go here! */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1744 | case BPF_PROG_TYPE_SCHED_CLS: |
| 1745 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1746 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1747 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 1748 | case BPF_PROG_TYPE_SK_SKB: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1749 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1750 | if (meta) |
| 1751 | return meta->pkt_access; |
| 1752 | |
| 1753 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1754 | return true; |
| 1755 | default: |
| 1756 | return false; |
| 1757 | } |
| 1758 | } |
| 1759 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1760 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1761 | int off, int size, bool zero_size_allowed) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1762 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1763 | struct bpf_reg_state *regs = cur_regs(env); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1764 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1765 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1766 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1767 | (u64)off + size > reg->range) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1768 | 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] | 1769 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1770 | return -EACCES; |
| 1771 | } |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1775 | 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] | 1776 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1777 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1778 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1779 | struct bpf_reg_state *reg = ®s[regno]; |
| 1780 | int err; |
| 1781 | |
| 1782 | /* We may have added a variable offset to the packet pointer; but any |
| 1783 | * reg->range we have comes after that. We are only checking the fixed |
| 1784 | * offset. |
| 1785 | */ |
| 1786 | |
| 1787 | /* We don't allow negative numbers, because we aren't tracking enough |
| 1788 | * detail to prove they're safe. |
| 1789 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1790 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1791 | 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] | 1792 | regno); |
| 1793 | return -EACCES; |
| 1794 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1795 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1796 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1797 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1798 | return err; |
| 1799 | } |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 1800 | |
| 1801 | /* __check_packet_access has made sure "off + size - 1" is within u16. |
| 1802 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 1803 | * otherwise find_good_pkt_pointers would have refused to set range info |
| 1804 | * that __check_packet_access would have rejected this pkt access. |
| 1805 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 1806 | */ |
| 1807 | env->prog->aux->max_pkt_offset = |
| 1808 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 1809 | off + reg->umax_value + size - 1); |
| 1810 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1811 | return err; |
| 1812 | } |
| 1813 | |
| 1814 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1815 | 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] | 1816 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1817 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1818 | struct bpf_insn_access_aux info = { |
| 1819 | .reg_type = *reg_type, |
| 1820 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1821 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1822 | if (env->ops->is_valid_access && |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1823 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1824 | /* A non zero info.ctx_field_size indicates that this field is a |
| 1825 | * candidate for later verifier transformation to load the whole |
| 1826 | * field and then apply a mask when accessed with a narrower |
| 1827 | * access than actual ctx access size. A zero info.ctx_field_size |
| 1828 | * will only allow for whole field access and rejects any other |
| 1829 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1830 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 1831 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1832 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1833 | 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] | 1834 | /* remember the offset of last byte accessed in ctx */ |
| 1835 | if (env->prog->aux->max_ctx_offset < off + size) |
| 1836 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1837 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 1838 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1839 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1840 | 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] | 1841 | return -EACCES; |
| 1842 | } |
| 1843 | |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1844 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 1845 | int size) |
| 1846 | { |
| 1847 | if (size < 0 || off < 0 || |
| 1848 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 1849 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 1850 | off, size); |
| 1851 | return -EACCES; |
| 1852 | } |
| 1853 | return 0; |
| 1854 | } |
| 1855 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 1856 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
| 1857 | u32 regno, int off, int size, |
| 1858 | enum bpf_access_type t) |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1859 | { |
| 1860 | struct bpf_reg_state *regs = cur_regs(env); |
| 1861 | struct bpf_reg_state *reg = ®s[regno]; |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 1862 | struct bpf_insn_access_aux info = {}; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1863 | bool valid; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1864 | |
| 1865 | if (reg->smin_value < 0) { |
| 1866 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 1867 | regno); |
| 1868 | return -EACCES; |
| 1869 | } |
| 1870 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1871 | switch (reg->type) { |
| 1872 | case PTR_TO_SOCK_COMMON: |
| 1873 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); |
| 1874 | break; |
| 1875 | case PTR_TO_SOCKET: |
| 1876 | valid = bpf_sock_is_valid_access(off, size, t, &info); |
| 1877 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 1878 | case PTR_TO_TCP_SOCK: |
| 1879 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); |
| 1880 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 1881 | case PTR_TO_XDP_SOCK: |
| 1882 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); |
| 1883 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1884 | default: |
| 1885 | valid = false; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 1888 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1889 | if (valid) { |
| 1890 | env->insn_aux_data[insn_idx].ctx_field_size = |
| 1891 | info.ctx_field_size; |
| 1892 | return 0; |
| 1893 | } |
| 1894 | |
| 1895 | verbose(env, "R%d invalid %s access off=%d size=%d\n", |
| 1896 | regno, reg_type_str[reg->type], off, size); |
| 1897 | |
| 1898 | return -EACCES; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1901 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 1902 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1903 | { |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1904 | if (allow_ptr_leaks) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1905 | return false; |
| 1906 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1907 | return reg->type != SCALAR_VALUE; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1910 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
| 1911 | { |
| 1912 | return cur_regs(env) + regno; |
| 1913 | } |
| 1914 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1915 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 1916 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1917 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1918 | } |
| 1919 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1920 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 1921 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1922 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1923 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1924 | return reg->type == PTR_TO_CTX; |
| 1925 | } |
| 1926 | |
| 1927 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) |
| 1928 | { |
| 1929 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 1930 | |
| 1931 | return type_is_sk_pointer(reg->type); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1932 | } |
| 1933 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1934 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 1935 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1936 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1937 | |
| 1938 | return type_is_pkt_pointer(reg->type); |
| 1939 | } |
| 1940 | |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 1941 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 1942 | { |
| 1943 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 1944 | |
| 1945 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 1946 | return reg->type == PTR_TO_FLOW_KEYS; |
| 1947 | } |
| 1948 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1949 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 1950 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1951 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1952 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1953 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1954 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1955 | |
| 1956 | /* Byte size accesses are always allowed. */ |
| 1957 | if (!strict || size == 1) |
| 1958 | return 0; |
| 1959 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1960 | /* For platforms that do not have a Kconfig enabling |
| 1961 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 1962 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 1963 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 1964 | * to this code only in strict mode where we want to emulate |
| 1965 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 1966 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1967 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1968 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1969 | |
| 1970 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 1971 | if (!tnum_is_aligned(reg_off, size)) { |
| 1972 | char tn_buf[48]; |
| 1973 | |
| 1974 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1975 | verbose(env, |
| 1976 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1977 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1978 | return -EACCES; |
| 1979 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1980 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1981 | return 0; |
| 1982 | } |
| 1983 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1984 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 1985 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1986 | const char *pointer_desc, |
| 1987 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1988 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1989 | struct tnum reg_off; |
| 1990 | |
| 1991 | /* Byte size accesses are always allowed. */ |
| 1992 | if (!strict || size == 1) |
| 1993 | return 0; |
| 1994 | |
| 1995 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 1996 | if (!tnum_is_aligned(reg_off, size)) { |
| 1997 | char tn_buf[48]; |
| 1998 | |
| 1999 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2000 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2001 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2002 | return -EACCES; |
| 2003 | } |
| 2004 | |
| 2005 | return 0; |
| 2006 | } |
| 2007 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 2008 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2009 | const struct bpf_reg_state *reg, int off, |
| 2010 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2011 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2012 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2013 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2014 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2015 | switch (reg->type) { |
| 2016 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2017 | case PTR_TO_PACKET_META: |
| 2018 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 2019 | * right in front, treat it the very same way. |
| 2020 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2021 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2022 | case PTR_TO_FLOW_KEYS: |
| 2023 | pointer_desc = "flow keys "; |
| 2024 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2025 | case PTR_TO_MAP_VALUE: |
| 2026 | pointer_desc = "value "; |
| 2027 | break; |
| 2028 | case PTR_TO_CTX: |
| 2029 | pointer_desc = "context "; |
| 2030 | break; |
| 2031 | case PTR_TO_STACK: |
| 2032 | pointer_desc = "stack "; |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 2033 | /* The stack spill tracking logic in check_stack_write() |
| 2034 | * and check_stack_read() relies on stack accesses being |
| 2035 | * aligned. |
| 2036 | */ |
| 2037 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2038 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2039 | case PTR_TO_SOCKET: |
| 2040 | pointer_desc = "sock "; |
| 2041 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2042 | case PTR_TO_SOCK_COMMON: |
| 2043 | pointer_desc = "sock_common "; |
| 2044 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2045 | case PTR_TO_TCP_SOCK: |
| 2046 | pointer_desc = "tcp_sock "; |
| 2047 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2048 | case PTR_TO_XDP_SOCK: |
| 2049 | pointer_desc = "xdp_sock "; |
| 2050 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2051 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2052 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2053 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2054 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 2055 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2056 | } |
| 2057 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2058 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 2059 | const struct bpf_func_state *func, |
| 2060 | int off) |
| 2061 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2062 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2063 | |
| 2064 | if (stack >= -off) |
| 2065 | return 0; |
| 2066 | |
| 2067 | /* update known max for given subprogram */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2068 | env->subprog_info[func->subprogno].stack_depth = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2069 | return 0; |
| 2070 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2071 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2072 | /* starting from main bpf function walk all instructions of the function |
| 2073 | * and recursively walk all callees that given function can call. |
| 2074 | * Ignore jump and exit insns. |
| 2075 | * Since recursion is prevented by check_cfg() this algorithm |
| 2076 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 2077 | */ |
| 2078 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 2079 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2080 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 2081 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2082 | struct bpf_insn *insn = env->prog->insnsi; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2083 | int ret_insn[MAX_CALL_FRAMES]; |
| 2084 | int ret_prog[MAX_CALL_FRAMES]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2085 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2086 | process_func: |
| 2087 | /* round up to 32-bytes, since this is granularity |
| 2088 | * of interpreter stack size |
| 2089 | */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2090 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2091 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2092 | 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] | 2093 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2094 | return -EACCES; |
| 2095 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2096 | continue_func: |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 2097 | subprog_end = subprog[idx + 1].start; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2098 | for (; i < subprog_end; i++) { |
| 2099 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 2100 | continue; |
| 2101 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 2102 | continue; |
| 2103 | /* remember insn and function to return to */ |
| 2104 | ret_insn[frame] = i + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2105 | ret_prog[frame] = idx; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2106 | |
| 2107 | /* find the callee */ |
| 2108 | i = i + insn[i].imm + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2109 | idx = find_subprog(env, i); |
| 2110 | if (idx < 0) { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2111 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 2112 | i); |
| 2113 | return -EFAULT; |
| 2114 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2115 | frame++; |
| 2116 | if (frame >= MAX_CALL_FRAMES) { |
Paul Chaignon | 927cb78 | 2019-03-20 13:58:27 +0100 | [diff] [blame] | 2117 | verbose(env, "the call stack of %d frames is too deep !\n", |
| 2118 | frame); |
| 2119 | return -E2BIG; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2120 | } |
| 2121 | goto process_func; |
| 2122 | } |
| 2123 | /* end of for() loop means the last insn of the 'subprog' |
| 2124 | * was reached. Doesn't matter whether it was JA or EXIT |
| 2125 | */ |
| 2126 | if (frame == 0) |
| 2127 | return 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2128 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2129 | frame--; |
| 2130 | i = ret_insn[frame]; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2131 | idx = ret_prog[frame]; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2132 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2133 | } |
| 2134 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 2135 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 2136 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 2137 | const struct bpf_insn *insn, int idx) |
| 2138 | { |
| 2139 | int start = idx + insn->imm + 1, subprog; |
| 2140 | |
| 2141 | subprog = find_subprog(env, start); |
| 2142 | if (subprog < 0) { |
| 2143 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 2144 | start); |
| 2145 | return -EFAULT; |
| 2146 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2147 | return env->subprog_info[subprog].stack_depth; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 2148 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 2149 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 2150 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 2151 | static int check_ctx_reg(struct bpf_verifier_env *env, |
| 2152 | const struct bpf_reg_state *reg, int regno) |
| 2153 | { |
| 2154 | /* Access to ctx or passing it to a helper is only allowed in |
| 2155 | * its original, unmodified form. |
| 2156 | */ |
| 2157 | |
| 2158 | if (reg->off) { |
| 2159 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 2160 | regno, reg->off); |
| 2161 | return -EACCES; |
| 2162 | } |
| 2163 | |
| 2164 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 2165 | char tn_buf[48]; |
| 2166 | |
| 2167 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2168 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 2169 | return -EACCES; |
| 2170 | } |
| 2171 | |
| 2172 | return 0; |
| 2173 | } |
| 2174 | |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 2175 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
| 2176 | const struct bpf_reg_state *reg, |
| 2177 | int regno, int off, int size) |
| 2178 | { |
| 2179 | if (off < 0) { |
| 2180 | verbose(env, |
| 2181 | "R%d invalid tracepoint buffer access: off=%d, size=%d", |
| 2182 | regno, off, size); |
| 2183 | return -EACCES; |
| 2184 | } |
| 2185 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 2186 | char tn_buf[48]; |
| 2187 | |
| 2188 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2189 | verbose(env, |
| 2190 | "R%d invalid variable buffer offset: off=%d, var_off=%s", |
| 2191 | regno, off, tn_buf); |
| 2192 | return -EACCES; |
| 2193 | } |
| 2194 | if (off + size > env->prog->aux->max_tp_access) |
| 2195 | env->prog->aux->max_tp_access = off + size; |
| 2196 | |
| 2197 | return 0; |
| 2198 | } |
| 2199 | |
| 2200 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 2201 | /* truncate register to smaller size (in bytes) |
| 2202 | * must be called with size < BPF_REG_SIZE |
| 2203 | */ |
| 2204 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 2205 | { |
| 2206 | u64 mask; |
| 2207 | |
| 2208 | /* clear high bits in bit representation */ |
| 2209 | reg->var_off = tnum_cast(reg->var_off, size); |
| 2210 | |
| 2211 | /* fix arithmetic bounds */ |
| 2212 | mask = ((u64)1 << (size * 8)) - 1; |
| 2213 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 2214 | reg->umin_value &= mask; |
| 2215 | reg->umax_value &= mask; |
| 2216 | } else { |
| 2217 | reg->umin_value = 0; |
| 2218 | reg->umax_value = mask; |
| 2219 | } |
| 2220 | reg->smin_value = reg->umin_value; |
| 2221 | reg->smax_value = reg->umax_value; |
| 2222 | } |
| 2223 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2224 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 2225 | * if t==write, value_regno is a register which value is stored into memory |
| 2226 | * if t==read, value_regno is a register which will receive the value from memory |
| 2227 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 2228 | * if t==read && value_regno==-1, don't care what we read from memory |
| 2229 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2230 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 2231 | int off, int bpf_size, enum bpf_access_type t, |
| 2232 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2233 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2234 | struct bpf_reg_state *regs = cur_regs(env); |
| 2235 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2236 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2237 | int size, err = 0; |
| 2238 | |
| 2239 | size = bpf_size_to_bytes(bpf_size); |
| 2240 | if (size < 0) |
| 2241 | return size; |
| 2242 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2243 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2244 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2245 | if (err) |
| 2246 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2247 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2248 | /* for access checks, reg->off is just part of off */ |
| 2249 | off += reg->off; |
| 2250 | |
| 2251 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2252 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2253 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2254 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2255 | return -EACCES; |
| 2256 | } |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 2257 | err = check_map_access_type(env, regno, off, size, t); |
| 2258 | if (err) |
| 2259 | return err; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2260 | err = check_map_access(env, regno, off, size, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2261 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2262 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2263 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2264 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2265 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 2266 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2267 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2268 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2269 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2270 | return -EACCES; |
| 2271 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2272 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 2273 | err = check_ctx_reg(env, reg, regno); |
| 2274 | if (err < 0) |
| 2275 | return err; |
| 2276 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2277 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2278 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2279 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2280 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 2281 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2282 | */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2283 | if (reg_type == SCALAR_VALUE) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2284 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2285 | } else { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2286 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2287 | value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2288 | if (reg_type_may_be_null(reg_type)) |
| 2289 | regs[value_regno].id = ++env->id_gen; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2290 | /* A load of ctx field could have different |
| 2291 | * actual load size with the one encoded in the |
| 2292 | * insn. When the dst is PTR, it is for sure not |
| 2293 | * a sub-register. |
| 2294 | */ |
| 2295 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2296 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2297 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2298 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2299 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2300 | } else if (reg->type == PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2301 | off += reg->var_off.value; |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 2302 | err = check_stack_access(env, reg, off, size); |
| 2303 | if (err) |
| 2304 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 2305 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2306 | state = func(env, reg); |
| 2307 | err = update_stack_depth(env, state, off); |
| 2308 | if (err) |
| 2309 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 2310 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2311 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2312 | err = check_stack_write(env, state, off, size, |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2313 | value_regno, insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2314 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2315 | err = check_stack_read(env, state, off, size, |
| 2316 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2317 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2318 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2319 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2320 | return -EACCES; |
| 2321 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2322 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2323 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2324 | verbose(env, "R%d leaks addr into packet\n", |
| 2325 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2326 | return -EACCES; |
| 2327 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2328 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2329 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2330 | mark_reg_unknown(env, regs, value_regno); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2331 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 2332 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2333 | is_pointer_value(env, value_regno)) { |
| 2334 | verbose(env, "R%d leaks addr into flow keys\n", |
| 2335 | value_regno); |
| 2336 | return -EACCES; |
| 2337 | } |
| 2338 | |
| 2339 | err = check_flow_keys_access(env, off, size); |
| 2340 | if (!err && t == BPF_READ && value_regno >= 0) |
| 2341 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2342 | } else if (type_is_sk_pointer(reg->type)) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2343 | if (t == BPF_WRITE) { |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2344 | verbose(env, "R%d cannot write into %s\n", |
| 2345 | regno, reg_type_str[reg->type]); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2346 | return -EACCES; |
| 2347 | } |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2348 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2349 | if (!err && value_regno >= 0) |
| 2350 | mark_reg_unknown(env, regs, value_regno); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 2351 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
| 2352 | err = check_tp_buffer_access(env, reg, regno, off, size); |
| 2353 | if (!err && t == BPF_READ && value_regno >= 0) |
| 2354 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2355 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2356 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 2357 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2358 | return -EACCES; |
| 2359 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2360 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2361 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2362 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2363 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 2364 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2365 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2366 | return err; |
| 2367 | } |
| 2368 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2369 | 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] | 2370 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2371 | int err; |
| 2372 | |
| 2373 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 2374 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2375 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2376 | return -EINVAL; |
| 2377 | } |
| 2378 | |
| 2379 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2380 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2381 | if (err) |
| 2382 | return err; |
| 2383 | |
| 2384 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2385 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2386 | if (err) |
| 2387 | return err; |
| 2388 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 2389 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2390 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 2391 | return -EACCES; |
| 2392 | } |
| 2393 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2394 | if (is_ctx_reg(env, insn->dst_reg) || |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 2395 | is_pkt_reg(env, insn->dst_reg) || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2396 | is_flow_key_reg(env, insn->dst_reg) || |
| 2397 | is_sk_reg(env, insn->dst_reg)) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2398 | 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] | 2399 | insn->dst_reg, |
| 2400 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2401 | return -EACCES; |
| 2402 | } |
| 2403 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2404 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2405 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2406 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2407 | if (err) |
| 2408 | return err; |
| 2409 | |
| 2410 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2411 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2412 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2413 | } |
| 2414 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2415 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
| 2416 | int off, int access_size, |
| 2417 | bool zero_size_allowed) |
| 2418 | { |
| 2419 | struct bpf_reg_state *reg = reg_state(env, regno); |
| 2420 | |
| 2421 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 2422 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
| 2423 | if (tnum_is_const(reg->var_off)) { |
| 2424 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", |
| 2425 | regno, off, access_size); |
| 2426 | } else { |
| 2427 | char tn_buf[48]; |
| 2428 | |
| 2429 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2430 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", |
| 2431 | regno, tn_buf, access_size); |
| 2432 | } |
| 2433 | return -EACCES; |
| 2434 | } |
| 2435 | return 0; |
| 2436 | } |
| 2437 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2438 | /* when register 'regno' is passed into function that will read 'access_size' |
| 2439 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2440 | * and all elements of stack are initialized. |
| 2441 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 2442 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2443 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2444 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2445 | int access_size, bool zero_size_allowed, |
| 2446 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2447 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2448 | struct bpf_reg_state *reg = reg_state(env, regno); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2449 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2450 | int err, min_off, max_off, i, j, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2451 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2452 | if (reg->type != PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2453 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2454 | if (zero_size_allowed && access_size == 0 && |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2455 | register_is_null(reg)) |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2456 | return 0; |
| 2457 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2458 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2459 | reg_type_str[reg->type], |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2460 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2461 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2462 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2463 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2464 | if (tnum_is_const(reg->var_off)) { |
| 2465 | min_off = max_off = reg->var_off.value + reg->off; |
| 2466 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 2467 | zero_size_allowed); |
| 2468 | if (err) |
| 2469 | return err; |
| 2470 | } else { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 2471 | /* Variable offset is prohibited for unprivileged mode for |
| 2472 | * simplicity since it requires corresponding support in |
| 2473 | * Spectre masking for stack ALU. |
| 2474 | * See also retrieve_ptr_limit(). |
| 2475 | */ |
| 2476 | if (!env->allow_ptr_leaks) { |
| 2477 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2478 | |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 2479 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2480 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", |
| 2481 | regno, tn_buf); |
| 2482 | return -EACCES; |
| 2483 | } |
Andrey Ignatov | f2bcd05 | 2019-04-03 23:22:37 -0700 | [diff] [blame] | 2484 | /* Only initialized buffer on stack is allowed to be accessed |
| 2485 | * with variable offset. With uninitialized buffer it's hard to |
| 2486 | * guarantee that whole memory is marked as initialized on |
| 2487 | * helper return since specific bounds are unknown what may |
| 2488 | * cause uninitialized stack leaking. |
| 2489 | */ |
| 2490 | if (meta && meta->raw_mode) |
| 2491 | meta = NULL; |
| 2492 | |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2493 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
| 2494 | reg->smax_value <= -BPF_MAX_VAR_OFF) { |
| 2495 | verbose(env, "R%d unbounded indirect variable offset stack access\n", |
| 2496 | regno); |
| 2497 | return -EACCES; |
| 2498 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2499 | min_off = reg->smin_value + reg->off; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2500 | max_off = reg->smax_value + reg->off; |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2501 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 2502 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2503 | if (err) { |
| 2504 | verbose(env, "R%d min value is outside of stack bound\n", |
| 2505 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2506 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2507 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2508 | err = __check_stack_boundary(env, regno, max_off, access_size, |
| 2509 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2510 | if (err) { |
| 2511 | verbose(env, "R%d max value is outside of stack bound\n", |
| 2512 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2513 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2514 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2515 | } |
| 2516 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2517 | if (meta && meta->raw_mode) { |
| 2518 | meta->access_size = access_size; |
| 2519 | meta->regno = regno; |
| 2520 | return 0; |
| 2521 | } |
| 2522 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2523 | for (i = min_off; i < max_off + access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2524 | u8 *stype; |
| 2525 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2526 | slot = -i - 1; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2527 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2528 | if (state->allocated_stack <= slot) |
| 2529 | goto err; |
| 2530 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 2531 | if (*stype == STACK_MISC) |
| 2532 | goto mark; |
| 2533 | if (*stype == STACK_ZERO) { |
| 2534 | /* helper can write anything into the stack */ |
| 2535 | *stype = STACK_MISC; |
| 2536 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2537 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2538 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 2539 | state->stack[spi].spilled_ptr.type == SCALAR_VALUE) { |
| 2540 | __mark_reg_unknown(&state->stack[spi].spilled_ptr); |
| 2541 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 2542 | state->stack[spi].slot_type[j] = STACK_MISC; |
| 2543 | goto mark; |
| 2544 | } |
| 2545 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2546 | err: |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2547 | if (tnum_is_const(reg->var_off)) { |
| 2548 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 2549 | min_off, i - min_off, access_size); |
| 2550 | } else { |
| 2551 | char tn_buf[48]; |
| 2552 | |
| 2553 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2554 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", |
| 2555 | tn_buf, i - min_off, access_size); |
| 2556 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2557 | return -EACCES; |
| 2558 | mark: |
| 2559 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 2560 | * the whole slot to be marked as 'read' |
| 2561 | */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2562 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2563 | state->stack[spi].spilled_ptr.parent, |
| 2564 | REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2565 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2566 | return update_stack_depth(env, state, min_off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2567 | } |
| 2568 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2569 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 2570 | int access_size, bool zero_size_allowed, |
| 2571 | struct bpf_call_arg_meta *meta) |
| 2572 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2573 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2574 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2575 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2576 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2577 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2578 | return check_packet_access(env, regno, reg->off, access_size, |
| 2579 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2580 | case PTR_TO_MAP_VALUE: |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 2581 | if (check_map_access_type(env, regno, reg->off, access_size, |
| 2582 | meta && meta->raw_mode ? BPF_WRITE : |
| 2583 | BPF_READ)) |
| 2584 | return -EACCES; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2585 | return check_map_access(env, regno, reg->off, access_size, |
| 2586 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2587 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2588 | return check_stack_boundary(env, regno, access_size, |
| 2589 | zero_size_allowed, meta); |
| 2590 | } |
| 2591 | } |
| 2592 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 2593 | /* Implementation details: |
| 2594 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL |
| 2595 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
| 2596 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after |
| 2597 | * value_or_null->value transition, since the verifier only cares about |
| 2598 | * the range of access to valid map value pointer and doesn't care about actual |
| 2599 | * address of the map element. |
| 2600 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
| 2601 | * reg->id > 0 after value_or_null->value transition. By doing so |
| 2602 | * two bpf_map_lookups will be considered two different pointers that |
| 2603 | * point to different bpf_spin_locks. |
| 2604 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
| 2605 | * dead-locks. |
| 2606 | * Since only one bpf_spin_lock is allowed the checks are simpler than |
| 2607 | * reg_is_refcounted() logic. The verifier needs to remember only |
| 2608 | * one spin_lock instead of array of acquired_refs. |
| 2609 | * cur_state->active_spin_lock remembers which map value element got locked |
| 2610 | * and clears it after bpf_spin_unlock. |
| 2611 | */ |
| 2612 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, |
| 2613 | bool is_lock) |
| 2614 | { |
| 2615 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 2616 | struct bpf_verifier_state *cur = env->cur_state; |
| 2617 | bool is_const = tnum_is_const(reg->var_off); |
| 2618 | struct bpf_map *map = reg->map_ptr; |
| 2619 | u64 val = reg->var_off.value; |
| 2620 | |
| 2621 | if (reg->type != PTR_TO_MAP_VALUE) { |
| 2622 | verbose(env, "R%d is not a pointer to map_value\n", regno); |
| 2623 | return -EINVAL; |
| 2624 | } |
| 2625 | if (!is_const) { |
| 2626 | verbose(env, |
| 2627 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", |
| 2628 | regno); |
| 2629 | return -EINVAL; |
| 2630 | } |
| 2631 | if (!map->btf) { |
| 2632 | verbose(env, |
| 2633 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", |
| 2634 | map->name); |
| 2635 | return -EINVAL; |
| 2636 | } |
| 2637 | if (!map_value_has_spin_lock(map)) { |
| 2638 | if (map->spin_lock_off == -E2BIG) |
| 2639 | verbose(env, |
| 2640 | "map '%s' has more than one 'struct bpf_spin_lock'\n", |
| 2641 | map->name); |
| 2642 | else if (map->spin_lock_off == -ENOENT) |
| 2643 | verbose(env, |
| 2644 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", |
| 2645 | map->name); |
| 2646 | else |
| 2647 | verbose(env, |
| 2648 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", |
| 2649 | map->name); |
| 2650 | return -EINVAL; |
| 2651 | } |
| 2652 | if (map->spin_lock_off != val + reg->off) { |
| 2653 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", |
| 2654 | val + reg->off); |
| 2655 | return -EINVAL; |
| 2656 | } |
| 2657 | if (is_lock) { |
| 2658 | if (cur->active_spin_lock) { |
| 2659 | verbose(env, |
| 2660 | "Locking two bpf_spin_locks are not allowed\n"); |
| 2661 | return -EINVAL; |
| 2662 | } |
| 2663 | cur->active_spin_lock = reg->id; |
| 2664 | } else { |
| 2665 | if (!cur->active_spin_lock) { |
| 2666 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
| 2667 | return -EINVAL; |
| 2668 | } |
| 2669 | if (cur->active_spin_lock != reg->id) { |
| 2670 | verbose(env, "bpf_spin_unlock of different lock\n"); |
| 2671 | return -EINVAL; |
| 2672 | } |
| 2673 | cur->active_spin_lock = 0; |
| 2674 | } |
| 2675 | return 0; |
| 2676 | } |
| 2677 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2678 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 2679 | { |
| 2680 | return type == ARG_PTR_TO_MEM || |
| 2681 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 2682 | type == ARG_PTR_TO_UNINIT_MEM; |
| 2683 | } |
| 2684 | |
| 2685 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 2686 | { |
| 2687 | return type == ARG_CONST_SIZE || |
| 2688 | type == ARG_CONST_SIZE_OR_ZERO; |
| 2689 | } |
| 2690 | |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 2691 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
| 2692 | { |
| 2693 | return type == ARG_PTR_TO_INT || |
| 2694 | type == ARG_PTR_TO_LONG; |
| 2695 | } |
| 2696 | |
| 2697 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
| 2698 | { |
| 2699 | if (type == ARG_PTR_TO_INT) |
| 2700 | return sizeof(u32); |
| 2701 | else if (type == ARG_PTR_TO_LONG) |
| 2702 | return sizeof(u64); |
| 2703 | |
| 2704 | return -EINVAL; |
| 2705 | } |
| 2706 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2707 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2708 | enum bpf_arg_type arg_type, |
| 2709 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2710 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2711 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2712 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2713 | int err = 0; |
| 2714 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2715 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2716 | return 0; |
| 2717 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2718 | err = check_reg_arg(env, regno, SRC_OP); |
| 2719 | if (err) |
| 2720 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2721 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2722 | if (arg_type == ARG_ANYTHING) { |
| 2723 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2724 | verbose(env, "R%d leaks addr into helper function\n", |
| 2725 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2726 | return -EACCES; |
| 2727 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2728 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2729 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2730 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2731 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2732 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2733 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2734 | return -EACCES; |
| 2735 | } |
| 2736 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2737 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2738 | arg_type == ARG_PTR_TO_MAP_VALUE || |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2739 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
| 2740 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2741 | expected_type = PTR_TO_STACK; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2742 | if (register_is_null(reg) && |
| 2743 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) |
| 2744 | /* final test in check_stack_boundary() */; |
| 2745 | else if (!type_is_pkt_pointer(type) && |
| 2746 | type != PTR_TO_MAP_VALUE && |
| 2747 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2748 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2749 | } else if (arg_type == ARG_CONST_SIZE || |
| 2750 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2751 | expected_type = SCALAR_VALUE; |
| 2752 | if (type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2753 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2754 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 2755 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2756 | if (type != expected_type) |
| 2757 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2758 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 2759 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2760 | if (type != expected_type) |
| 2761 | goto err_type; |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 2762 | err = check_ctx_reg(env, reg, regno); |
| 2763 | if (err < 0) |
| 2764 | return err; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2765 | } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) { |
| 2766 | expected_type = PTR_TO_SOCK_COMMON; |
| 2767 | /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */ |
| 2768 | if (!type_is_sk_pointer(type)) |
| 2769 | goto err_type; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2770 | if (reg->ref_obj_id) { |
| 2771 | if (meta->ref_obj_id) { |
| 2772 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", |
| 2773 | regno, reg->ref_obj_id, |
| 2774 | meta->ref_obj_id); |
| 2775 | return -EFAULT; |
| 2776 | } |
| 2777 | meta->ref_obj_id = reg->ref_obj_id; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2778 | } |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2779 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
| 2780 | expected_type = PTR_TO_SOCKET; |
| 2781 | if (type != expected_type) |
| 2782 | goto err_type; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 2783 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
| 2784 | if (meta->func_id == BPF_FUNC_spin_lock) { |
| 2785 | if (process_spin_lock(env, regno, true)) |
| 2786 | return -EACCES; |
| 2787 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
| 2788 | if (process_spin_lock(env, regno, false)) |
| 2789 | return -EACCES; |
| 2790 | } else { |
| 2791 | verbose(env, "verifier internal error\n"); |
| 2792 | return -EFAULT; |
| 2793 | } |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2794 | } else if (arg_type_is_mem_ptr(arg_type)) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2795 | expected_type = PTR_TO_STACK; |
| 2796 | /* One exception here. In case function allows for NULL to be |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2797 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2798 | * happens during stack boundary checking. |
| 2799 | */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2800 | if (register_is_null(reg) && |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 2801 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2802 | /* final test in check_stack_boundary() */; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2803 | else if (!type_is_pkt_pointer(type) && |
| 2804 | type != PTR_TO_MAP_VALUE && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2805 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2806 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2807 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 2808 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 2809 | expected_type = PTR_TO_STACK; |
| 2810 | if (!type_is_pkt_pointer(type) && |
| 2811 | type != PTR_TO_MAP_VALUE && |
| 2812 | type != expected_type) |
| 2813 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2814 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2815 | verbose(env, "unsupported arg_type %d\n", arg_type); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2816 | return -EFAULT; |
| 2817 | } |
| 2818 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2819 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 2820 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2821 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2822 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 2823 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 2824 | * check that [key, key + map->key_size) are within |
| 2825 | * stack limits and initialized |
| 2826 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2827 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2828 | /* in function declaration map_ptr must come before |
| 2829 | * map_key, so that it's verified and known before |
| 2830 | * we have to check map_key here. Otherwise it means |
| 2831 | * that kernel subsystem misconfigured verifier |
| 2832 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2833 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2834 | return -EACCES; |
| 2835 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2836 | err = check_helper_mem_access(env, regno, |
| 2837 | meta->map_ptr->key_size, false, |
| 2838 | NULL); |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2839 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2840 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
| 2841 | !register_is_null(reg)) || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2842 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2843 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 2844 | * check [value, value + map->value_size) validity |
| 2845 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2846 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2847 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2848 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2849 | return -EACCES; |
| 2850 | } |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2851 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2852 | err = check_helper_mem_access(env, regno, |
| 2853 | meta->map_ptr->value_size, false, |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2854 | meta); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2855 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2856 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2857 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2858 | /* remember the mem_size which may be used later |
| 2859 | * to refine return values. |
| 2860 | */ |
| 2861 | meta->msize_smax_value = reg->smax_value; |
| 2862 | meta->msize_umax_value = reg->umax_value; |
| 2863 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2864 | /* The register is SCALAR_VALUE; the access check |
| 2865 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2866 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2867 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2868 | /* For unprivileged variable accesses, disable raw |
| 2869 | * mode so that the program is required to |
| 2870 | * initialize all the memory that the helper could |
| 2871 | * just partially fill up. |
| 2872 | */ |
| 2873 | meta = NULL; |
| 2874 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2875 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2876 | 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] | 2877 | regno); |
| 2878 | return -EACCES; |
| 2879 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2880 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2881 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2882 | err = check_helper_mem_access(env, regno - 1, 0, |
| 2883 | zero_size_allowed, |
| 2884 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2885 | if (err) |
| 2886 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2887 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2888 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2889 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2890 | 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] | 2891 | regno); |
| 2892 | return -EACCES; |
| 2893 | } |
| 2894 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2895 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2896 | zero_size_allowed, meta); |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 2897 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 2898 | int size = int_ptr_type_to_size(arg_type); |
| 2899 | |
| 2900 | err = check_helper_mem_access(env, regno, size, false, meta); |
| 2901 | if (err) |
| 2902 | return err; |
| 2903 | err = check_ptr_alignment(env, reg, 0, size, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2907 | err_type: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2908 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2909 | reg_type_str[type], reg_type_str[expected_type]); |
| 2910 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2911 | } |
| 2912 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2913 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 2914 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2915 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2916 | if (!map) |
| 2917 | return 0; |
| 2918 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2919 | /* We need a two way check, first is from map perspective ... */ |
| 2920 | switch (map->map_type) { |
| 2921 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 2922 | if (func_id != BPF_FUNC_tail_call) |
| 2923 | goto error; |
| 2924 | break; |
| 2925 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 2926 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2927 | func_id != BPF_FUNC_perf_event_output && |
| 2928 | func_id != BPF_FUNC_perf_event_read_value) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2929 | goto error; |
| 2930 | break; |
| 2931 | case BPF_MAP_TYPE_STACK_TRACE: |
| 2932 | if (func_id != BPF_FUNC_get_stackid) |
| 2933 | goto error; |
| 2934 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 2935 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 2936 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2937 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2938 | goto error; |
| 2939 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2940 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 2941 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2942 | if (func_id != BPF_FUNC_get_local_storage) |
| 2943 | goto error; |
| 2944 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2945 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
| 2946 | * allow to be modified from bpf side. So do not allow lookup elements |
| 2947 | * for now. |
| 2948 | */ |
| 2949 | case BPF_MAP_TYPE_DEVMAP: |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 2950 | if (func_id != BPF_FUNC_redirect_map) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2951 | goto error; |
| 2952 | break; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2953 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 2954 | * appear. |
| 2955 | */ |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2956 | case BPF_MAP_TYPE_CPUMAP: |
| 2957 | if (func_id != BPF_FUNC_redirect_map) |
| 2958 | goto error; |
| 2959 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2960 | case BPF_MAP_TYPE_XSKMAP: |
| 2961 | if (func_id != BPF_FUNC_redirect_map && |
| 2962 | func_id != BPF_FUNC_map_lookup_elem) |
| 2963 | goto error; |
| 2964 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2965 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 2966 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2967 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 2968 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 2969 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2970 | case BPF_MAP_TYPE_SOCKMAP: |
| 2971 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 2972 | func_id != BPF_FUNC_sock_map_update && |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2973 | func_id != BPF_FUNC_map_delete_elem && |
| 2974 | func_id != BPF_FUNC_msg_redirect_map) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2975 | goto error; |
| 2976 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2977 | case BPF_MAP_TYPE_SOCKHASH: |
| 2978 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 2979 | func_id != BPF_FUNC_sock_hash_update && |
| 2980 | func_id != BPF_FUNC_map_delete_elem && |
| 2981 | func_id != BPF_FUNC_msg_redirect_hash) |
| 2982 | goto error; |
| 2983 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2984 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 2985 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 2986 | goto error; |
| 2987 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 2988 | case BPF_MAP_TYPE_QUEUE: |
| 2989 | case BPF_MAP_TYPE_STACK: |
| 2990 | if (func_id != BPF_FUNC_map_peek_elem && |
| 2991 | func_id != BPF_FUNC_map_pop_elem && |
| 2992 | func_id != BPF_FUNC_map_push_elem) |
| 2993 | goto error; |
| 2994 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2995 | case BPF_MAP_TYPE_SK_STORAGE: |
| 2996 | if (func_id != BPF_FUNC_sk_storage_get && |
| 2997 | func_id != BPF_FUNC_sk_storage_delete) |
| 2998 | goto error; |
| 2999 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3000 | default: |
| 3001 | break; |
| 3002 | } |
| 3003 | |
| 3004 | /* ... and second from the function itself. */ |
| 3005 | switch (func_id) { |
| 3006 | case BPF_FUNC_tail_call: |
| 3007 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 3008 | goto error; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 3009 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3010 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
| 3011 | return -EINVAL; |
| 3012 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3013 | break; |
| 3014 | case BPF_FUNC_perf_event_read: |
| 3015 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 3016 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3017 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 3018 | goto error; |
| 3019 | break; |
| 3020 | case BPF_FUNC_get_stackid: |
| 3021 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 3022 | goto error; |
| 3023 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 3024 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 3025 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 3026 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 3027 | goto error; |
| 3028 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 3029 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 3030 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 3031 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 3032 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 3033 | goto error; |
| 3034 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 3035 | case BPF_FUNC_sk_redirect_map: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 3036 | case BPF_FUNC_msg_redirect_map: |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 3037 | case BPF_FUNC_sock_map_update: |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 3038 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 3039 | goto error; |
| 3040 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 3041 | case BPF_FUNC_sk_redirect_hash: |
| 3042 | case BPF_FUNC_msg_redirect_hash: |
| 3043 | case BPF_FUNC_sock_hash_update: |
| 3044 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 3045 | goto error; |
| 3046 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 3047 | case BPF_FUNC_get_local_storage: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 3048 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 3049 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 3050 | goto error; |
| 3051 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 3052 | case BPF_FUNC_sk_select_reuseport: |
| 3053 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) |
| 3054 | goto error; |
| 3055 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 3056 | case BPF_FUNC_map_peek_elem: |
| 3057 | case BPF_FUNC_map_pop_elem: |
| 3058 | case BPF_FUNC_map_push_elem: |
| 3059 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 3060 | map->map_type != BPF_MAP_TYPE_STACK) |
| 3061 | goto error; |
| 3062 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 3063 | case BPF_FUNC_sk_storage_get: |
| 3064 | case BPF_FUNC_sk_storage_delete: |
| 3065 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
| 3066 | goto error; |
| 3067 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3068 | default: |
| 3069 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3073 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3074 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 3075 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3076 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 3079 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3080 | { |
| 3081 | int count = 0; |
| 3082 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3083 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3084 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3085 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3086 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3087 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3088 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3089 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3090 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3091 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3092 | count++; |
| 3093 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 3094 | /* We only support one arg being in raw mode at the moment, |
| 3095 | * which is sufficient for the helper functions we have |
| 3096 | * right now. |
| 3097 | */ |
| 3098 | return count <= 1; |
| 3099 | } |
| 3100 | |
| 3101 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 3102 | enum bpf_arg_type arg_next) |
| 3103 | { |
| 3104 | return (arg_type_is_mem_ptr(arg_curr) && |
| 3105 | !arg_type_is_mem_size(arg_next)) || |
| 3106 | (!arg_type_is_mem_ptr(arg_curr) && |
| 3107 | arg_type_is_mem_size(arg_next)); |
| 3108 | } |
| 3109 | |
| 3110 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 3111 | { |
| 3112 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 3113 | * bytes from memory 'buf'. Both arg types need |
| 3114 | * to be paired, so make sure there's no buggy |
| 3115 | * helper function specification. |
| 3116 | */ |
| 3117 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 3118 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 3119 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 3120 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 3121 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 3122 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 3123 | return false; |
| 3124 | |
| 3125 | return true; |
| 3126 | } |
| 3127 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3128 | 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] | 3129 | { |
| 3130 | int count = 0; |
| 3131 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3132 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3133 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3134 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3135 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3136 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3137 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3138 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3139 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3140 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3141 | count++; |
| 3142 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3143 | /* A reference acquiring function cannot acquire |
| 3144 | * another refcounted ptr. |
| 3145 | */ |
| 3146 | if (is_acquire_function(func_id) && count) |
| 3147 | return false; |
| 3148 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3149 | /* We only support one arg being unreferenced at the moment, |
| 3150 | * which is sufficient for the helper functions we have right now. |
| 3151 | */ |
| 3152 | return count <= 1; |
| 3153 | } |
| 3154 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3155 | 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] | 3156 | { |
| 3157 | return check_raw_mode_ok(fn) && |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3158 | check_arg_pair_ok(fn) && |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3159 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3160 | } |
| 3161 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3162 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 3163 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3164 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3165 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 3166 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3167 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3168 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3169 | int i; |
| 3170 | |
| 3171 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3172 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3173 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3174 | |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 3175 | bpf_for_each_spilled_reg(i, state, reg) { |
| 3176 | if (!reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3177 | continue; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3178 | if (reg_is_pkt_pointer_any(reg)) |
| 3179 | __mark_reg_unknown(reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3180 | } |
| 3181 | } |
| 3182 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3183 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 3184 | { |
| 3185 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3186 | int i; |
| 3187 | |
| 3188 | for (i = 0; i <= vstate->curframe; i++) |
| 3189 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 3190 | } |
| 3191 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3192 | static void release_reg_references(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3193 | struct bpf_func_state *state, |
| 3194 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3195 | { |
| 3196 | struct bpf_reg_state *regs = state->regs, *reg; |
| 3197 | int i; |
| 3198 | |
| 3199 | for (i = 0; i < MAX_BPF_REG; i++) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3200 | if (regs[i].ref_obj_id == ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3201 | mark_reg_unknown(env, regs, i); |
| 3202 | |
| 3203 | bpf_for_each_spilled_reg(i, state, reg) { |
| 3204 | if (!reg) |
| 3205 | continue; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3206 | if (reg->ref_obj_id == ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3207 | __mark_reg_unknown(reg); |
| 3208 | } |
| 3209 | } |
| 3210 | |
| 3211 | /* The pointer with the specified id has released its reference to kernel |
| 3212 | * resources. Identify all copies of the same pointer and clear the reference. |
| 3213 | */ |
| 3214 | static int release_reference(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3215 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3216 | { |
| 3217 | struct bpf_verifier_state *vstate = env->cur_state; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3218 | int err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3219 | int i; |
| 3220 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3221 | err = release_reference_state(cur_func(env), ref_obj_id); |
| 3222 | if (err) |
| 3223 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3224 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3225 | for (i = 0; i <= vstate->curframe; i++) |
| 3226 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
| 3227 | |
| 3228 | return 0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3229 | } |
| 3230 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3231 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 3232 | int *insn_idx) |
| 3233 | { |
| 3234 | struct bpf_verifier_state *state = env->cur_state; |
| 3235 | struct bpf_func_state *caller, *callee; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3236 | int i, err, subprog, target_insn; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3237 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 3238 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3239 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 3240 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3241 | return -E2BIG; |
| 3242 | } |
| 3243 | |
| 3244 | target_insn = *insn_idx + insn->imm; |
| 3245 | subprog = find_subprog(env, target_insn + 1); |
| 3246 | if (subprog < 0) { |
| 3247 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 3248 | target_insn + 1); |
| 3249 | return -EFAULT; |
| 3250 | } |
| 3251 | |
| 3252 | caller = state->frame[state->curframe]; |
| 3253 | if (state->frame[state->curframe + 1]) { |
| 3254 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 3255 | state->curframe + 1); |
| 3256 | return -EFAULT; |
| 3257 | } |
| 3258 | |
| 3259 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 3260 | if (!callee) |
| 3261 | return -ENOMEM; |
| 3262 | state->frame[state->curframe + 1] = callee; |
| 3263 | |
| 3264 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 3265 | * into its own stack before reading from it. |
| 3266 | * callee can read/write into caller's stack |
| 3267 | */ |
| 3268 | init_func_state(env, callee, |
| 3269 | /* remember the callsite, it will be used by bpf_exit */ |
| 3270 | *insn_idx /* callsite */, |
| 3271 | state->curframe + 1 /* frameno within this callchain */, |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 3272 | subprog /* subprog number within this prog */); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3273 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3274 | /* Transfer references to the callee */ |
| 3275 | err = transfer_reference_state(callee, caller); |
| 3276 | if (err) |
| 3277 | return err; |
| 3278 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 3279 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 3280 | * pointers, which connects us up to the liveness chain |
| 3281 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3282 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 3283 | callee->regs[i] = caller->regs[i]; |
| 3284 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 3285 | /* after the call registers r0 - r5 were scratched */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3286 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 3287 | mark_reg_not_init(env, caller->regs, caller_saved[i]); |
| 3288 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 3289 | } |
| 3290 | |
| 3291 | /* only increment it after check_reg_arg() finished */ |
| 3292 | state->curframe++; |
| 3293 | |
| 3294 | /* and go analyze first insn of the callee */ |
| 3295 | *insn_idx = target_insn; |
| 3296 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 3297 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3298 | verbose(env, "caller:\n"); |
| 3299 | print_verifier_state(env, caller); |
| 3300 | verbose(env, "callee:\n"); |
| 3301 | print_verifier_state(env, callee); |
| 3302 | } |
| 3303 | return 0; |
| 3304 | } |
| 3305 | |
| 3306 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 3307 | { |
| 3308 | struct bpf_verifier_state *state = env->cur_state; |
| 3309 | struct bpf_func_state *caller, *callee; |
| 3310 | struct bpf_reg_state *r0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3311 | int err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3312 | |
| 3313 | callee = state->frame[state->curframe]; |
| 3314 | r0 = &callee->regs[BPF_REG_0]; |
| 3315 | if (r0->type == PTR_TO_STACK) { |
| 3316 | /* technically it's ok to return caller's stack pointer |
| 3317 | * (or caller's caller's pointer) back to the caller, |
| 3318 | * since these pointers are valid. Only current stack |
| 3319 | * pointer will be invalid as soon as function exits, |
| 3320 | * but let's be conservative |
| 3321 | */ |
| 3322 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 3323 | return -EINVAL; |
| 3324 | } |
| 3325 | |
| 3326 | state->curframe--; |
| 3327 | caller = state->frame[state->curframe]; |
| 3328 | /* return to the caller whatever r0 had in the callee */ |
| 3329 | caller->regs[BPF_REG_0] = *r0; |
| 3330 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3331 | /* Transfer references to the caller */ |
| 3332 | err = transfer_reference_state(caller, callee); |
| 3333 | if (err) |
| 3334 | return err; |
| 3335 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3336 | *insn_idx = callee->callsite + 1; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 3337 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3338 | verbose(env, "returning from callee:\n"); |
| 3339 | print_verifier_state(env, callee); |
| 3340 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 3341 | print_verifier_state(env, caller); |
| 3342 | } |
| 3343 | /* clear everything in the callee */ |
| 3344 | free_func_state(callee); |
| 3345 | state->frame[state->curframe + 1] = NULL; |
| 3346 | return 0; |
| 3347 | } |
| 3348 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 3349 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 3350 | int func_id, |
| 3351 | struct bpf_call_arg_meta *meta) |
| 3352 | { |
| 3353 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 3354 | |
| 3355 | if (ret_type != RET_INTEGER || |
| 3356 | (func_id != BPF_FUNC_get_stack && |
| 3357 | func_id != BPF_FUNC_probe_read_str)) |
| 3358 | return; |
| 3359 | |
| 3360 | ret_reg->smax_value = meta->msize_smax_value; |
| 3361 | ret_reg->umax_value = meta->msize_umax_value; |
| 3362 | __reg_deduce_bounds(ret_reg); |
| 3363 | __reg_bound_offset(ret_reg); |
| 3364 | } |
| 3365 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3366 | static int |
| 3367 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 3368 | int func_id, int insn_idx) |
| 3369 | { |
| 3370 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3371 | struct bpf_map *map = meta->map_ptr; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3372 | |
| 3373 | if (func_id != BPF_FUNC_tail_call && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 3374 | func_id != BPF_FUNC_map_lookup_elem && |
| 3375 | func_id != BPF_FUNC_map_update_elem && |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 3376 | func_id != BPF_FUNC_map_delete_elem && |
| 3377 | func_id != BPF_FUNC_map_push_elem && |
| 3378 | func_id != BPF_FUNC_map_pop_elem && |
| 3379 | func_id != BPF_FUNC_map_peek_elem) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3380 | return 0; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 3381 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3382 | if (map == NULL) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3383 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 3384 | return -EINVAL; |
| 3385 | } |
| 3386 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3387 | /* In case of read-only, some additional restrictions |
| 3388 | * need to be applied in order to prevent altering the |
| 3389 | * state of the map from program side. |
| 3390 | */ |
| 3391 | if ((map->map_flags & BPF_F_RDONLY_PROG) && |
| 3392 | (func_id == BPF_FUNC_map_delete_elem || |
| 3393 | func_id == BPF_FUNC_map_update_elem || |
| 3394 | func_id == BPF_FUNC_map_push_elem || |
| 3395 | func_id == BPF_FUNC_map_pop_elem)) { |
| 3396 | verbose(env, "write into map forbidden\n"); |
| 3397 | return -EACCES; |
| 3398 | } |
| 3399 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3400 | if (!BPF_MAP_PTR(aux->map_state)) |
| 3401 | bpf_map_ptr_store(aux, meta->map_ptr, |
| 3402 | meta->map_ptr->unpriv_array); |
| 3403 | else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr) |
| 3404 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
| 3405 | meta->map_ptr->unpriv_array); |
| 3406 | return 0; |
| 3407 | } |
| 3408 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3409 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 3410 | { |
| 3411 | struct bpf_func_state *state = cur_func(env); |
| 3412 | int i; |
| 3413 | |
| 3414 | for (i = 0; i < state->acquired_refs; i++) { |
| 3415 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 3416 | state->refs[i].id, state->refs[i].insn_idx); |
| 3417 | } |
| 3418 | return state->acquired_refs ? -EINVAL : 0; |
| 3419 | } |
| 3420 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3421 | 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] | 3422 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3423 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3424 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3425 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3426 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3427 | int i, err; |
| 3428 | |
| 3429 | /* find function prototype */ |
| 3430 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3431 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 3432 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3433 | return -EINVAL; |
| 3434 | } |
| 3435 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 3436 | if (env->ops->get_func_proto) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 3437 | fn = env->ops->get_func_proto(func_id, env->prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3438 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3439 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 3440 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3441 | return -EINVAL; |
| 3442 | } |
| 3443 | |
| 3444 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 3445 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Daniel Borkmann | 3fe2867 | 2018-06-02 23:06:33 +0200 | [diff] [blame] | 3446 | 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] | 3447 | return -EINVAL; |
| 3448 | } |
| 3449 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 3450 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 3451 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 3452 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 3453 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 3454 | func_id_name(func_id), func_id); |
| 3455 | return -EINVAL; |
| 3456 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3457 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3458 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3459 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3460 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3461 | err = check_func_proto(fn, func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3462 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3463 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 3464 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3465 | return err; |
| 3466 | } |
| 3467 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3468 | meta.func_id = func_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3469 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3470 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3471 | if (err) |
| 3472 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3473 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3474 | if (err) |
| 3475 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3476 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3477 | if (err) |
| 3478 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3479 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3480 | if (err) |
| 3481 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3482 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3483 | if (err) |
| 3484 | return err; |
| 3485 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3486 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 3487 | if (err) |
| 3488 | return err; |
| 3489 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3490 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 3491 | * is inferred from register state. |
| 3492 | */ |
| 3493 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3494 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 3495 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3496 | if (err) |
| 3497 | return err; |
| 3498 | } |
| 3499 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3500 | if (func_id == BPF_FUNC_tail_call) { |
| 3501 | err = check_reference_leak(env); |
| 3502 | if (err) { |
| 3503 | verbose(env, "tail_call would lead to reference leak\n"); |
| 3504 | return err; |
| 3505 | } |
| 3506 | } else if (is_release_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3507 | err = release_reference(env, meta.ref_obj_id); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3508 | if (err) { |
| 3509 | verbose(env, "func %s#%d reference has not been acquired before\n", |
| 3510 | func_id_name(func_id), func_id); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3511 | return err; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3512 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3513 | } |
| 3514 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3515 | regs = cur_regs(env); |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 3516 | |
| 3517 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 3518 | * this is required because get_local_storage() can't return an error. |
| 3519 | */ |
| 3520 | if (func_id == BPF_FUNC_get_local_storage && |
| 3521 | !register_is_null(®s[BPF_REG_2])) { |
| 3522 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 3523 | return -EINVAL; |
| 3524 | } |
| 3525 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3526 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3527 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3528 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3529 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 3530 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3531 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 3532 | /* helper call returns 64-bit value. */ |
| 3533 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 3534 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3535 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3536 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3537 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3538 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3539 | } else if (fn->ret_type == RET_VOID) { |
| 3540 | regs[BPF_REG_0].type = NOT_INIT; |
Roman Gushchin | 3e6a4b3 | 2018-08-02 14:27:22 -0700 | [diff] [blame] | 3541 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 3542 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3543 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3544 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3545 | /* remember map_ptr, so that check_map_access() |
| 3546 | * can check 'value_size' boundary of memory access |
| 3547 | * to map element returned from bpf_map_lookup_elem() |
| 3548 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3549 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3550 | verbose(env, |
| 3551 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3552 | return -EINVAL; |
| 3553 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3554 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 3555 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 3556 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
Alexei Starovoitov | e16d2f1 | 2019-01-31 15:40:05 -0800 | [diff] [blame] | 3557 | if (map_value_has_spin_lock(meta.map_ptr)) |
| 3558 | regs[BPF_REG_0].id = ++env->id_gen; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 3559 | } else { |
| 3560 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
| 3561 | regs[BPF_REG_0].id = ++env->id_gen; |
| 3562 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3563 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
| 3564 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 3565 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 3566 | regs[BPF_REG_0].id = ++env->id_gen; |
Lorenz Bauer | 85a51f8 | 2019-03-22 09:54:00 +0800 | [diff] [blame] | 3567 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
| 3568 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 3569 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; |
| 3570 | regs[BPF_REG_0].id = ++env->id_gen; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 3571 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
| 3572 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 3573 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; |
| 3574 | regs[BPF_REG_0].id = ++env->id_gen; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3575 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3576 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 3577 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3578 | return -EINVAL; |
| 3579 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 3580 | |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 3581 | if (is_ptr_cast_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3582 | /* For release_reference() */ |
| 3583 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 3584 | } else if (is_acquire_function(func_id)) { |
| 3585 | int id = acquire_reference_state(env, insn_idx); |
| 3586 | |
| 3587 | if (id < 0) |
| 3588 | return id; |
| 3589 | /* For mark_ptr_or_null_reg() */ |
| 3590 | regs[BPF_REG_0].id = id; |
| 3591 | /* For release_reference() */ |
| 3592 | regs[BPF_REG_0].ref_obj_id = id; |
| 3593 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3594 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 3595 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
| 3596 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3597 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 3598 | if (err) |
| 3599 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 3600 | |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 3601 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
| 3602 | const char *err_str; |
| 3603 | |
| 3604 | #ifdef CONFIG_PERF_EVENTS |
| 3605 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 3606 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 3607 | #else |
| 3608 | err = -ENOTSUPP; |
| 3609 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 3610 | #endif |
| 3611 | if (err) { |
| 3612 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 3613 | return err; |
| 3614 | } |
| 3615 | |
| 3616 | env->prog->has_callchain_buf = true; |
| 3617 | } |
| 3618 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3619 | if (changes_data) |
| 3620 | clear_all_pkt_pointers(env); |
| 3621 | return 0; |
| 3622 | } |
| 3623 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3624 | static bool signed_add_overflows(s64 a, s64 b) |
| 3625 | { |
| 3626 | /* Do the add in u64, where overflow is well-defined */ |
| 3627 | s64 res = (s64)((u64)a + (u64)b); |
| 3628 | |
| 3629 | if (b < 0) |
| 3630 | return res > a; |
| 3631 | return res < a; |
| 3632 | } |
| 3633 | |
| 3634 | static bool signed_sub_overflows(s64 a, s64 b) |
| 3635 | { |
| 3636 | /* Do the sub in u64, where overflow is well-defined */ |
| 3637 | s64 res = (s64)((u64)a - (u64)b); |
| 3638 | |
| 3639 | if (b < 0) |
| 3640 | return res < a; |
| 3641 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3642 | } |
| 3643 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3644 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 3645 | const struct bpf_reg_state *reg, |
| 3646 | enum bpf_reg_type type) |
| 3647 | { |
| 3648 | bool known = tnum_is_const(reg->var_off); |
| 3649 | s64 val = reg->var_off.value; |
| 3650 | s64 smin = reg->smin_value; |
| 3651 | |
| 3652 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 3653 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 3654 | reg_type_str[type], val); |
| 3655 | return false; |
| 3656 | } |
| 3657 | |
| 3658 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 3659 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 3660 | reg_type_str[type], reg->off); |
| 3661 | return false; |
| 3662 | } |
| 3663 | |
| 3664 | if (smin == S64_MIN) { |
| 3665 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 3666 | reg_type_str[type]); |
| 3667 | return false; |
| 3668 | } |
| 3669 | |
| 3670 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 3671 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 3672 | smin, reg_type_str[type]); |
| 3673 | return false; |
| 3674 | } |
| 3675 | |
| 3676 | return true; |
| 3677 | } |
| 3678 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3679 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
| 3680 | { |
| 3681 | return &env->insn_aux_data[env->insn_idx]; |
| 3682 | } |
| 3683 | |
| 3684 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
| 3685 | u32 *ptr_limit, u8 opcode, bool off_is_neg) |
| 3686 | { |
| 3687 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || |
| 3688 | (opcode == BPF_SUB && !off_is_neg); |
| 3689 | u32 off; |
| 3690 | |
| 3691 | switch (ptr_reg->type) { |
| 3692 | case PTR_TO_STACK: |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3693 | /* Indirect variable offset stack access is prohibited in |
| 3694 | * unprivileged mode so it's not handled here. |
| 3695 | */ |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3696 | off = ptr_reg->off + ptr_reg->var_off.value; |
| 3697 | if (mask_to_left) |
| 3698 | *ptr_limit = MAX_BPF_STACK + off; |
| 3699 | else |
| 3700 | *ptr_limit = -off; |
| 3701 | return 0; |
| 3702 | case PTR_TO_MAP_VALUE: |
| 3703 | if (mask_to_left) { |
| 3704 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; |
| 3705 | } else { |
| 3706 | off = ptr_reg->smin_value + ptr_reg->off; |
| 3707 | *ptr_limit = ptr_reg->map_ptr->value_size - off; |
| 3708 | } |
| 3709 | return 0; |
| 3710 | default: |
| 3711 | return -EINVAL; |
| 3712 | } |
| 3713 | } |
| 3714 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 3715 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
| 3716 | const struct bpf_insn *insn) |
| 3717 | { |
| 3718 | return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K; |
| 3719 | } |
| 3720 | |
| 3721 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, |
| 3722 | u32 alu_state, u32 alu_limit) |
| 3723 | { |
| 3724 | /* If we arrived here from different branches with different |
| 3725 | * state or limits to sanitize, then this won't work. |
| 3726 | */ |
| 3727 | if (aux->alu_state && |
| 3728 | (aux->alu_state != alu_state || |
| 3729 | aux->alu_limit != alu_limit)) |
| 3730 | return -EACCES; |
| 3731 | |
| 3732 | /* Corresponding fixup done in fixup_bpf_calls(). */ |
| 3733 | aux->alu_state = alu_state; |
| 3734 | aux->alu_limit = alu_limit; |
| 3735 | return 0; |
| 3736 | } |
| 3737 | |
| 3738 | static int sanitize_val_alu(struct bpf_verifier_env *env, |
| 3739 | struct bpf_insn *insn) |
| 3740 | { |
| 3741 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 3742 | |
| 3743 | if (can_skip_alu_sanitation(env, insn)) |
| 3744 | return 0; |
| 3745 | |
| 3746 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); |
| 3747 | } |
| 3748 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3749 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
| 3750 | struct bpf_insn *insn, |
| 3751 | const struct bpf_reg_state *ptr_reg, |
| 3752 | struct bpf_reg_state *dst_reg, |
| 3753 | bool off_is_neg) |
| 3754 | { |
| 3755 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3756 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 3757 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
| 3758 | u8 opcode = BPF_OP(insn->code); |
| 3759 | u32 alu_state, alu_limit; |
| 3760 | struct bpf_reg_state tmp; |
| 3761 | bool ret; |
| 3762 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 3763 | if (can_skip_alu_sanitation(env, insn)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3764 | return 0; |
| 3765 | |
| 3766 | /* We already marked aux for masking from non-speculative |
| 3767 | * paths, thus we got here in the first place. We only care |
| 3768 | * to explore bad access from here. |
| 3769 | */ |
| 3770 | if (vstate->speculative) |
| 3771 | goto do_sim; |
| 3772 | |
| 3773 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; |
| 3774 | alu_state |= ptr_is_dst_reg ? |
| 3775 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; |
| 3776 | |
| 3777 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) |
| 3778 | return 0; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 3779 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3780 | return -EACCES; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3781 | do_sim: |
| 3782 | /* Simulate and find potential out-of-bounds access under |
| 3783 | * speculative execution from truncation as a result of |
| 3784 | * masking when off was not within expected range. If off |
| 3785 | * sits in dst, then we temporarily need to move ptr there |
| 3786 | * to simulate dst (== 0) +/-= ptr. Needed, for example, |
| 3787 | * for cases where we use K-based arithmetic in one direction |
| 3788 | * and truncated reg-based in the other in order to explore |
| 3789 | * bad access. |
| 3790 | */ |
| 3791 | if (!ptr_is_dst_reg) { |
| 3792 | tmp = *dst_reg; |
| 3793 | *dst_reg = *ptr_reg; |
| 3794 | } |
| 3795 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); |
Xu Yu | 0803278 | 2019-03-21 18:00:35 +0800 | [diff] [blame] | 3796 | if (!ptr_is_dst_reg && ret) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3797 | *dst_reg = tmp; |
| 3798 | return !ret ? -EFAULT : 0; |
| 3799 | } |
| 3800 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3801 | /* 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] | 3802 | * Caller should also handle BPF_MOV case separately. |
| 3803 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 3804 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 3805 | */ |
| 3806 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 3807 | struct bpf_insn *insn, |
| 3808 | const struct bpf_reg_state *ptr_reg, |
| 3809 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3810 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3811 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3812 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 3813 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3814 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3815 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 3816 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 3817 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 3818 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 3819 | u32 dst = insn->dst_reg, src = insn->src_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3820 | u8 opcode = BPF_OP(insn->code); |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3821 | int ret; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3822 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3823 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3824 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 3825 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 3826 | smin_val > smax_val || umin_val > umax_val) { |
| 3827 | /* Taint dst register if offset had invalid bounds derived from |
| 3828 | * e.g. dead branches. |
| 3829 | */ |
| 3830 | __mark_reg_unknown(dst_reg); |
| 3831 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3832 | } |
| 3833 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3834 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3835 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3836 | verbose(env, |
| 3837 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 3838 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3839 | return -EACCES; |
| 3840 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3841 | |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3842 | switch (ptr_reg->type) { |
| 3843 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 3844 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
| 3845 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3846 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3847 | case CONST_PTR_TO_MAP: |
| 3848 | case PTR_TO_PACKET_END: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3849 | case PTR_TO_SOCKET: |
| 3850 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3851 | case PTR_TO_SOCK_COMMON: |
| 3852 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 3853 | case PTR_TO_TCP_SOCK: |
| 3854 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 3855 | case PTR_TO_XDP_SOCK: |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3856 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
| 3857 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3858 | return -EACCES; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 3859 | case PTR_TO_MAP_VALUE: |
| 3860 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { |
| 3861 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", |
| 3862 | off_reg == dst_reg ? dst : src); |
| 3863 | return -EACCES; |
| 3864 | } |
| 3865 | /* fall-through */ |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3866 | default: |
| 3867 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 3871 | * 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] | 3872 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3873 | dst_reg->type = ptr_reg->type; |
| 3874 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 3875 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3876 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 3877 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 3878 | return -EINVAL; |
| 3879 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3880 | switch (opcode) { |
| 3881 | case BPF_ADD: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3882 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 3883 | if (ret < 0) { |
| 3884 | verbose(env, "R%d tried to add from different maps or paths\n", dst); |
| 3885 | return ret; |
| 3886 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3887 | /* We can take a fixed offset as long as it doesn't overflow |
| 3888 | * the s32 'off' field |
| 3889 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3890 | if (known && (ptr_reg->off + smin_val == |
| 3891 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3892 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3893 | dst_reg->smin_value = smin_ptr; |
| 3894 | dst_reg->smax_value = smax_ptr; |
| 3895 | dst_reg->umin_value = umin_ptr; |
| 3896 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3897 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3898 | dst_reg->off = ptr_reg->off + smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3899 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3900 | break; |
| 3901 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3902 | /* A new variable offset is created. Note that off_reg->off |
| 3903 | * == 0, since it's a scalar. |
| 3904 | * dst_reg gets the pointer type and since some positive |
| 3905 | * integer value was added to the pointer, give it a new 'id' |
| 3906 | * if it's a PTR_TO_PACKET. |
| 3907 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 3908 | * added into the variable offset, and we copy the fixed offset |
| 3909 | * from ptr_reg. |
| 3910 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3911 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 3912 | signed_add_overflows(smax_ptr, smax_val)) { |
| 3913 | dst_reg->smin_value = S64_MIN; |
| 3914 | dst_reg->smax_value = S64_MAX; |
| 3915 | } else { |
| 3916 | dst_reg->smin_value = smin_ptr + smin_val; |
| 3917 | dst_reg->smax_value = smax_ptr + smax_val; |
| 3918 | } |
| 3919 | if (umin_ptr + umin_val < umin_ptr || |
| 3920 | umax_ptr + umax_val < umax_ptr) { |
| 3921 | dst_reg->umin_value = 0; |
| 3922 | dst_reg->umax_value = U64_MAX; |
| 3923 | } else { |
| 3924 | dst_reg->umin_value = umin_ptr + umin_val; |
| 3925 | dst_reg->umax_value = umax_ptr + umax_val; |
| 3926 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3927 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 3928 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3929 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3930 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3931 | dst_reg->id = ++env->id_gen; |
| 3932 | /* something was added to pkt_ptr, set range to zero */ |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3933 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3934 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3935 | break; |
| 3936 | case BPF_SUB: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3937 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 3938 | if (ret < 0) { |
| 3939 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); |
| 3940 | return ret; |
| 3941 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3942 | if (dst_reg == off_reg) { |
| 3943 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3944 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 3945 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3946 | return -EACCES; |
| 3947 | } |
| 3948 | /* We don't allow subtraction from FP, because (according to |
| 3949 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 3950 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 3951 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3952 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3953 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 3954 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3955 | return -EACCES; |
| 3956 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3957 | if (known && (ptr_reg->off - smin_val == |
| 3958 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3959 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3960 | dst_reg->smin_value = smin_ptr; |
| 3961 | dst_reg->smax_value = smax_ptr; |
| 3962 | dst_reg->umin_value = umin_ptr; |
| 3963 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3964 | dst_reg->var_off = ptr_reg->var_off; |
| 3965 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3966 | dst_reg->off = ptr_reg->off - smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3967 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3968 | break; |
| 3969 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3970 | /* A new variable offset is created. If the subtrahend is known |
| 3971 | * nonnegative, then any reg->range we had before is still good. |
| 3972 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3973 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 3974 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 3975 | /* Overflow possible, we know nothing */ |
| 3976 | dst_reg->smin_value = S64_MIN; |
| 3977 | dst_reg->smax_value = S64_MAX; |
| 3978 | } else { |
| 3979 | dst_reg->smin_value = smin_ptr - smax_val; |
| 3980 | dst_reg->smax_value = smax_ptr - smin_val; |
| 3981 | } |
| 3982 | if (umin_ptr < umax_val) { |
| 3983 | /* Overflow possible, we know nothing */ |
| 3984 | dst_reg->umin_value = 0; |
| 3985 | dst_reg->umax_value = U64_MAX; |
| 3986 | } else { |
| 3987 | /* Cannot overflow (as long as bounds are consistent) */ |
| 3988 | dst_reg->umin_value = umin_ptr - umax_val; |
| 3989 | dst_reg->umax_value = umax_ptr - umin_val; |
| 3990 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3991 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 3992 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3993 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3994 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3995 | dst_reg->id = ++env->id_gen; |
| 3996 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3997 | if (smin_val < 0) |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3998 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3999 | } |
| 4000 | break; |
| 4001 | case BPF_AND: |
| 4002 | case BPF_OR: |
| 4003 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4004 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 4005 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 4006 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4007 | return -EACCES; |
| 4008 | default: |
| 4009 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4010 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 4011 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4012 | return -EACCES; |
| 4013 | } |
| 4014 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 4015 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 4016 | return -EINVAL; |
| 4017 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4018 | __update_reg_bounds(dst_reg); |
| 4019 | __reg_deduce_bounds(dst_reg); |
| 4020 | __reg_bound_offset(dst_reg); |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 4021 | |
| 4022 | /* For unprivileged we require that resulting offset must be in bounds |
| 4023 | * in order to be able to sanitize access later on. |
| 4024 | */ |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 4025 | if (!env->allow_ptr_leaks) { |
| 4026 | if (dst_reg->type == PTR_TO_MAP_VALUE && |
| 4027 | check_map_access(env, dst, dst_reg->off, 1, false)) { |
| 4028 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
| 4029 | "prohibited for !root\n", dst); |
| 4030 | return -EACCES; |
| 4031 | } else if (dst_reg->type == PTR_TO_STACK && |
| 4032 | check_stack_access(env, dst_reg, dst_reg->off + |
| 4033 | dst_reg->var_off.value, 1)) { |
| 4034 | verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 4035 | "prohibited for !root\n", dst); |
| 4036 | return -EACCES; |
| 4037 | } |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 4038 | } |
| 4039 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4040 | return 0; |
| 4041 | } |
| 4042 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4043 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 4044 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 4045 | * need extra checks in the 32-bit case. |
| 4046 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4047 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 4048 | struct bpf_insn *insn, |
| 4049 | struct bpf_reg_state *dst_reg, |
| 4050 | struct bpf_reg_state src_reg) |
| 4051 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4052 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4053 | u8 opcode = BPF_OP(insn->code); |
| 4054 | bool src_known, dst_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4055 | s64 smin_val, smax_val; |
| 4056 | u64 umin_val, umax_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4057 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 4058 | u32 dst = insn->dst_reg; |
| 4059 | int ret; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4060 | |
Jann Horn | b799207 | 2018-10-05 18:17:59 +0200 | [diff] [blame] | 4061 | if (insn_bitness == 32) { |
| 4062 | /* Relevant for 32-bit RSH: Information can propagate towards |
| 4063 | * LSB, so it isn't sufficient to only truncate the output to |
| 4064 | * 32 bits. |
| 4065 | */ |
| 4066 | coerce_reg_to_size(dst_reg, 4); |
| 4067 | coerce_reg_to_size(&src_reg, 4); |
| 4068 | } |
| 4069 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4070 | smin_val = src_reg.smin_value; |
| 4071 | smax_val = src_reg.smax_value; |
| 4072 | umin_val = src_reg.umin_value; |
| 4073 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4074 | src_known = tnum_is_const(src_reg.var_off); |
| 4075 | dst_known = tnum_is_const(dst_reg->var_off); |
| 4076 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 4077 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
| 4078 | smin_val > smax_val || umin_val > umax_val) { |
| 4079 | /* Taint dst register if offset had invalid bounds derived from |
| 4080 | * e.g. dead branches. |
| 4081 | */ |
| 4082 | __mark_reg_unknown(dst_reg); |
| 4083 | return 0; |
| 4084 | } |
| 4085 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 4086 | if (!src_known && |
| 4087 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
| 4088 | __mark_reg_unknown(dst_reg); |
| 4089 | return 0; |
| 4090 | } |
| 4091 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4092 | switch (opcode) { |
| 4093 | case BPF_ADD: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 4094 | ret = sanitize_val_alu(env, insn); |
| 4095 | if (ret < 0) { |
| 4096 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); |
| 4097 | return ret; |
| 4098 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4099 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 4100 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 4101 | dst_reg->smin_value = S64_MIN; |
| 4102 | dst_reg->smax_value = S64_MAX; |
| 4103 | } else { |
| 4104 | dst_reg->smin_value += smin_val; |
| 4105 | dst_reg->smax_value += smax_val; |
| 4106 | } |
| 4107 | if (dst_reg->umin_value + umin_val < umin_val || |
| 4108 | dst_reg->umax_value + umax_val < umax_val) { |
| 4109 | dst_reg->umin_value = 0; |
| 4110 | dst_reg->umax_value = U64_MAX; |
| 4111 | } else { |
| 4112 | dst_reg->umin_value += umin_val; |
| 4113 | dst_reg->umax_value += umax_val; |
| 4114 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4115 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 4116 | break; |
| 4117 | case BPF_SUB: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 4118 | ret = sanitize_val_alu(env, insn); |
| 4119 | if (ret < 0) { |
| 4120 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); |
| 4121 | return ret; |
| 4122 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4123 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 4124 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 4125 | /* Overflow possible, we know nothing */ |
| 4126 | dst_reg->smin_value = S64_MIN; |
| 4127 | dst_reg->smax_value = S64_MAX; |
| 4128 | } else { |
| 4129 | dst_reg->smin_value -= smax_val; |
| 4130 | dst_reg->smax_value -= smin_val; |
| 4131 | } |
| 4132 | if (dst_reg->umin_value < umax_val) { |
| 4133 | /* Overflow possible, we know nothing */ |
| 4134 | dst_reg->umin_value = 0; |
| 4135 | dst_reg->umax_value = U64_MAX; |
| 4136 | } else { |
| 4137 | /* Cannot overflow (as long as bounds are consistent) */ |
| 4138 | dst_reg->umin_value -= umax_val; |
| 4139 | dst_reg->umax_value -= umin_val; |
| 4140 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4141 | 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] | 4142 | break; |
| 4143 | case BPF_MUL: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4144 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 4145 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4146 | /* Ain't nobody got time to multiply that sign */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4147 | __mark_reg_unbounded(dst_reg); |
| 4148 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4149 | break; |
| 4150 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4151 | /* Both values are positive, so we can work with unsigned and |
| 4152 | * copy the result to signed (unless it exceeds S64_MAX). |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4153 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4154 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 4155 | /* Potential overflow, we know nothing */ |
| 4156 | __mark_reg_unbounded(dst_reg); |
| 4157 | /* (except what we can learn from the var_off) */ |
| 4158 | __update_reg_bounds(dst_reg); |
| 4159 | break; |
| 4160 | } |
| 4161 | dst_reg->umin_value *= umin_val; |
| 4162 | dst_reg->umax_value *= umax_val; |
| 4163 | if (dst_reg->umax_value > S64_MAX) { |
| 4164 | /* Overflow possible, we know nothing */ |
| 4165 | dst_reg->smin_value = S64_MIN; |
| 4166 | dst_reg->smax_value = S64_MAX; |
| 4167 | } else { |
| 4168 | dst_reg->smin_value = dst_reg->umin_value; |
| 4169 | dst_reg->smax_value = dst_reg->umax_value; |
| 4170 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4171 | break; |
| 4172 | case BPF_AND: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4173 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4174 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 4175 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4176 | break; |
| 4177 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4178 | /* We get our minimum from the var_off, since that's inherently |
| 4179 | * bitwise. Our maximum is the minimum of the operands' maxima. |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 4180 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4181 | 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] | 4182 | dst_reg->umin_value = dst_reg->var_off.value; |
| 4183 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 4184 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 4185 | /* Lose signed bounds when ANDing negative numbers, |
| 4186 | * ain't nobody got time for that. |
| 4187 | */ |
| 4188 | dst_reg->smin_value = S64_MIN; |
| 4189 | dst_reg->smax_value = S64_MAX; |
| 4190 | } else { |
| 4191 | /* ANDing two positives gives a positive, so safe to |
| 4192 | * cast result into s64. |
| 4193 | */ |
| 4194 | dst_reg->smin_value = dst_reg->umin_value; |
| 4195 | dst_reg->smax_value = dst_reg->umax_value; |
| 4196 | } |
| 4197 | /* We may learn something more from the var_off */ |
| 4198 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4199 | break; |
| 4200 | case BPF_OR: |
| 4201 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4202 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
| 4203 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4204 | break; |
| 4205 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4206 | /* We get our maximum from the var_off, and our minimum is the |
| 4207 | * maximum of the operands' minima |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4208 | */ |
| 4209 | 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] | 4210 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 4211 | dst_reg->umax_value = dst_reg->var_off.value | |
| 4212 | dst_reg->var_off.mask; |
| 4213 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 4214 | /* Lose signed bounds when ORing negative numbers, |
| 4215 | * ain't nobody got time for that. |
| 4216 | */ |
| 4217 | dst_reg->smin_value = S64_MIN; |
| 4218 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4219 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4220 | /* ORing two positives gives a positive, so safe to |
| 4221 | * cast result into s64. |
| 4222 | */ |
| 4223 | dst_reg->smin_value = dst_reg->umin_value; |
| 4224 | dst_reg->smax_value = dst_reg->umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4225 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4226 | /* We may learn something more from the var_off */ |
| 4227 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4228 | break; |
| 4229 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4230 | if (umax_val >= insn_bitness) { |
| 4231 | /* Shifts greater than 31 or 63 are undefined. |
| 4232 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4233 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4234 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4235 | break; |
| 4236 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4237 | /* We lose all sign bit information (except what we can pick |
| 4238 | * up from var_off) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4239 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4240 | dst_reg->smin_value = S64_MIN; |
| 4241 | dst_reg->smax_value = S64_MAX; |
| 4242 | /* If we might shift our top bit out, then we know nothing */ |
| 4243 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 4244 | dst_reg->umin_value = 0; |
| 4245 | dst_reg->umax_value = U64_MAX; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 4246 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4247 | dst_reg->umin_value <<= umin_val; |
| 4248 | dst_reg->umax_value <<= umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 4249 | } |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 4250 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4251 | /* We may learn something more from the var_off */ |
| 4252 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4253 | break; |
| 4254 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4255 | if (umax_val >= insn_bitness) { |
| 4256 | /* Shifts greater than 31 or 63 are undefined. |
| 4257 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4258 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4259 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4260 | break; |
| 4261 | } |
Edward Cree | 4374f25 | 2017-12-18 20:11:53 -0800 | [diff] [blame] | 4262 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 4263 | * be negative, then either: |
| 4264 | * 1) src_reg might be zero, so the sign bit of the result is |
| 4265 | * unknown, so we lose our signed bounds |
| 4266 | * 2) it's known negative, thus the unsigned bounds capture the |
| 4267 | * signed bounds |
| 4268 | * 3) the signed bounds cross zero, so they tell us nothing |
| 4269 | * about the result |
| 4270 | * If the value in dst_reg is known nonnegative, then again the |
| 4271 | * unsigned bounts capture the signed bounds. |
| 4272 | * Thus, in all cases it suffices to blow away our signed bounds |
| 4273 | * and rely on inferring new ones from the unsigned bounds and |
| 4274 | * var_off of the result. |
| 4275 | */ |
| 4276 | dst_reg->smin_value = S64_MIN; |
| 4277 | dst_reg->smax_value = S64_MAX; |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 4278 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4279 | dst_reg->umin_value >>= umax_val; |
| 4280 | dst_reg->umax_value >>= umin_val; |
| 4281 | /* We may learn something more from the var_off */ |
| 4282 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4283 | break; |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 4284 | case BPF_ARSH: |
| 4285 | if (umax_val >= insn_bitness) { |
| 4286 | /* Shifts greater than 31 or 63 are undefined. |
| 4287 | * This includes shifts by a negative number. |
| 4288 | */ |
| 4289 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 4290 | break; |
| 4291 | } |
| 4292 | |
| 4293 | /* Upon reaching here, src_known is true and |
| 4294 | * umax_val is equal to umin_val. |
| 4295 | */ |
| 4296 | dst_reg->smin_value >>= umin_val; |
| 4297 | dst_reg->smax_value >>= umin_val; |
| 4298 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val); |
| 4299 | |
| 4300 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 4301 | * dst_reg var_off to refine the result. |
| 4302 | */ |
| 4303 | dst_reg->umin_value = 0; |
| 4304 | dst_reg->umax_value = U64_MAX; |
| 4305 | __update_reg_bounds(dst_reg); |
| 4306 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4307 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4308 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4309 | break; |
| 4310 | } |
| 4311 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4312 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 4313 | /* 32-bit ALU ops are (32,32)->32 */ |
| 4314 | coerce_reg_to_size(dst_reg, 4); |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4315 | } |
| 4316 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4317 | __reg_deduce_bounds(dst_reg); |
| 4318 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4319 | return 0; |
| 4320 | } |
| 4321 | |
| 4322 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 4323 | * and var_off. |
| 4324 | */ |
| 4325 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 4326 | struct bpf_insn *insn) |
| 4327 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4328 | struct bpf_verifier_state *vstate = env->cur_state; |
| 4329 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 4330 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4331 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 4332 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4333 | |
| 4334 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4335 | src_reg = NULL; |
| 4336 | if (dst_reg->type != SCALAR_VALUE) |
| 4337 | ptr_reg = dst_reg; |
| 4338 | if (BPF_SRC(insn->code) == BPF_X) { |
| 4339 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4340 | if (src_reg->type != SCALAR_VALUE) { |
| 4341 | if (dst_reg->type != SCALAR_VALUE) { |
| 4342 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4343 | * an arbitrary scalar. Disallow all math except |
| 4344 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4345 | */ |
Alexei Starovoitov | dd06682 | 2018-09-12 14:06:10 -0700 | [diff] [blame] | 4346 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4347 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 4348 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4349 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4350 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 4351 | insn->dst_reg, |
| 4352 | bpf_alu_string[opcode >> 4]); |
| 4353 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4354 | } else { |
| 4355 | /* scalar += pointer |
| 4356 | * This is legal, but we have to reverse our |
| 4357 | * src/dest handling in computing the range |
| 4358 | */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4359 | return adjust_ptr_min_max_vals(env, insn, |
| 4360 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4361 | } |
| 4362 | } else if (ptr_reg) { |
| 4363 | /* pointer += scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4364 | return adjust_ptr_min_max_vals(env, insn, |
| 4365 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4366 | } |
| 4367 | } else { |
| 4368 | /* Pretend the src is a reg with a known value, since we only |
| 4369 | * need to be able to read from this state. |
| 4370 | */ |
| 4371 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4372 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4373 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4374 | if (ptr_reg) /* pointer += K */ |
| 4375 | return adjust_ptr_min_max_vals(env, insn, |
| 4376 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4377 | } |
| 4378 | |
| 4379 | /* Got here implies adding two SCALAR_VALUEs */ |
| 4380 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4381 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4382 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4383 | return -EINVAL; |
| 4384 | } |
| 4385 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4386 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4387 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4388 | return -EINVAL; |
| 4389 | } |
| 4390 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4391 | } |
| 4392 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4393 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4394 | 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] | 4395 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4396 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4397 | u8 opcode = BPF_OP(insn->code); |
| 4398 | int err; |
| 4399 | |
| 4400 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 4401 | if (opcode == BPF_NEG) { |
| 4402 | if (BPF_SRC(insn->code) != 0 || |
| 4403 | insn->src_reg != BPF_REG_0 || |
| 4404 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4405 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4406 | return -EINVAL; |
| 4407 | } |
| 4408 | } else { |
| 4409 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 4410 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 4411 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4412 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4413 | return -EINVAL; |
| 4414 | } |
| 4415 | } |
| 4416 | |
| 4417 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4418 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4419 | if (err) |
| 4420 | return err; |
| 4421 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4422 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4423 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4424 | insn->dst_reg); |
| 4425 | return -EACCES; |
| 4426 | } |
| 4427 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4428 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4429 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4430 | if (err) |
| 4431 | return err; |
| 4432 | |
| 4433 | } else if (opcode == BPF_MOV) { |
| 4434 | |
| 4435 | if (BPF_SRC(insn->code) == BPF_X) { |
| 4436 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4437 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4438 | return -EINVAL; |
| 4439 | } |
| 4440 | |
| 4441 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4442 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4443 | if (err) |
| 4444 | return err; |
| 4445 | } else { |
| 4446 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4447 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4448 | return -EINVAL; |
| 4449 | } |
| 4450 | } |
| 4451 | |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 4452 | /* check dest operand, mark as required later */ |
| 4453 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4454 | if (err) |
| 4455 | return err; |
| 4456 | |
| 4457 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4458 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
| 4459 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; |
| 4460 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4461 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 4462 | /* case: R1 = R2 |
| 4463 | * copy register state to dest reg |
| 4464 | */ |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4465 | *dst_reg = *src_reg; |
| 4466 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 4467 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4468 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4469 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4470 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4471 | verbose(env, |
| 4472 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4473 | insn->src_reg); |
| 4474 | return -EACCES; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4475 | } else if (src_reg->type == SCALAR_VALUE) { |
| 4476 | *dst_reg = *src_reg; |
| 4477 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 4478 | dst_reg->subreg_def = env->insn_idx + 1; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4479 | } else { |
| 4480 | mark_reg_unknown(env, regs, |
| 4481 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4482 | } |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4483 | coerce_reg_to_size(dst_reg, 4); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4484 | } |
| 4485 | } else { |
| 4486 | /* case: R = imm |
| 4487 | * remember the value we stored into this reg |
| 4488 | */ |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 4489 | /* clear any state __mark_reg_known doesn't set */ |
| 4490 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4491 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 4492 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 4493 | __mark_reg_known(regs + insn->dst_reg, |
| 4494 | insn->imm); |
| 4495 | } else { |
| 4496 | __mark_reg_known(regs + insn->dst_reg, |
| 4497 | (u32)insn->imm); |
| 4498 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4502 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4503 | return -EINVAL; |
| 4504 | |
| 4505 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 4506 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4507 | if (BPF_SRC(insn->code) == BPF_X) { |
| 4508 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4509 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4510 | return -EINVAL; |
| 4511 | } |
| 4512 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4513 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4514 | if (err) |
| 4515 | return err; |
| 4516 | } else { |
| 4517 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4518 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4519 | return -EINVAL; |
| 4520 | } |
| 4521 | } |
| 4522 | |
| 4523 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4524 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4525 | if (err) |
| 4526 | return err; |
| 4527 | |
| 4528 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 4529 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4530 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4531 | return -EINVAL; |
| 4532 | } |
| 4533 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 4534 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 4535 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 4536 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 4537 | |
| 4538 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4539 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 4540 | return -EINVAL; |
| 4541 | } |
| 4542 | } |
| 4543 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4544 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4545 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4546 | if (err) |
| 4547 | return err; |
| 4548 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4549 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4550 | } |
| 4551 | |
| 4552 | return 0; |
| 4553 | } |
| 4554 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 4555 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
| 4556 | struct bpf_reg_state *dst_reg, |
| 4557 | enum bpf_reg_type type, u16 new_range) |
| 4558 | { |
| 4559 | struct bpf_reg_state *reg; |
| 4560 | int i; |
| 4561 | |
| 4562 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 4563 | reg = &state->regs[i]; |
| 4564 | if (reg->type == type && reg->id == dst_reg->id) |
| 4565 | /* keep the maximum range already checked */ |
| 4566 | reg->range = max(reg->range, new_range); |
| 4567 | } |
| 4568 | |
| 4569 | bpf_for_each_spilled_reg(i, state, reg) { |
| 4570 | if (!reg) |
| 4571 | continue; |
| 4572 | if (reg->type == type && reg->id == dst_reg->id) |
| 4573 | reg->range = max(reg->range, new_range); |
| 4574 | } |
| 4575 | } |
| 4576 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4577 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4578 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 4579 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4580 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4581 | { |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4582 | u16 new_range; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 4583 | int i; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4584 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4585 | if (dst_reg->off < 0 || |
| 4586 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4587 | /* This doesn't give us any range */ |
| 4588 | return; |
| 4589 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4590 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 4591 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4592 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 4593 | * than pkt_end, but that's because it's also less than pkt. |
| 4594 | */ |
| 4595 | return; |
| 4596 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4597 | new_range = dst_reg->off; |
| 4598 | if (range_right_open) |
| 4599 | new_range--; |
| 4600 | |
| 4601 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4602 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4603 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4604 | * |
| 4605 | * r2 = r3; |
| 4606 | * r2 += 8; |
| 4607 | * if (r2 > pkt_end) goto <handle exception> |
| 4608 | * <access okay> |
| 4609 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4610 | * r2 = r3; |
| 4611 | * r2 += 8; |
| 4612 | * if (r2 < pkt_end) goto <access okay> |
| 4613 | * <handle exception> |
| 4614 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4615 | * Where: |
| 4616 | * r2 == dst_reg, pkt_end == src_reg |
| 4617 | * r2=pkt(id=n,off=8,r=0) |
| 4618 | * r3=pkt(id=n,off=0,r=0) |
| 4619 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4620 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4621 | * |
| 4622 | * r2 = r3; |
| 4623 | * r2 += 8; |
| 4624 | * if (pkt_end >= r2) goto <access okay> |
| 4625 | * <handle exception> |
| 4626 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4627 | * r2 = r3; |
| 4628 | * r2 += 8; |
| 4629 | * if (pkt_end <= r2) goto <handle exception> |
| 4630 | * <access okay> |
| 4631 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4632 | * Where: |
| 4633 | * pkt_end == dst_reg, r2 == src_reg |
| 4634 | * r2=pkt(id=n,off=8,r=0) |
| 4635 | * r3=pkt(id=n,off=0,r=0) |
| 4636 | * |
| 4637 | * 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] | 4638 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 4639 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 4640 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4641 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4642 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4643 | /* If our ids match, then we must have the same max_value. And we |
| 4644 | * don't care about the other reg's fixed offset, since if it's too big |
| 4645 | * the range won't allow anything. |
| 4646 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 4647 | */ |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 4648 | for (i = 0; i <= vstate->curframe; i++) |
| 4649 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, |
| 4650 | new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4651 | } |
| 4652 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4653 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
| 4654 | * and return: |
| 4655 | * 1 - branch will be taken and "goto target" will be executed |
| 4656 | * 0 - branch will not be taken and fall-through to next insn |
| 4657 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10] |
| 4658 | */ |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4659 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
| 4660 | bool is_jmp32) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4661 | { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4662 | struct bpf_reg_state reg_lo; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4663 | s64 sval; |
| 4664 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4665 | if (__is_pointer_value(false, reg)) |
| 4666 | return -1; |
| 4667 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4668 | if (is_jmp32) { |
| 4669 | reg_lo = *reg; |
| 4670 | reg = ®_lo; |
| 4671 | /* For JMP32, only low 32 bits are compared, coerce_reg_to_size |
| 4672 | * could truncate high bits and update umin/umax according to |
| 4673 | * information of low bits. |
| 4674 | */ |
| 4675 | coerce_reg_to_size(reg, 4); |
| 4676 | /* smin/smax need special handling. For example, after coerce, |
| 4677 | * if smin_value is 0x00000000ffffffffLL, the value is -1 when |
| 4678 | * used as operand to JMP32. It is a negative number from s32's |
| 4679 | * point of view, while it is a positive number when seen as |
| 4680 | * s64. The smin/smax are kept as s64, therefore, when used with |
| 4681 | * JMP32, they need to be transformed into s32, then sign |
| 4682 | * extended back to s64. |
| 4683 | * |
| 4684 | * Also, smin/smax were copied from umin/umax. If umin/umax has |
| 4685 | * different sign bit, then min/max relationship doesn't |
| 4686 | * maintain after casting into s32, for this case, set smin/smax |
| 4687 | * to safest range. |
| 4688 | */ |
| 4689 | if ((reg->umax_value ^ reg->umin_value) & |
| 4690 | (1ULL << 31)) { |
| 4691 | reg->smin_value = S32_MIN; |
| 4692 | reg->smax_value = S32_MAX; |
| 4693 | } |
| 4694 | reg->smin_value = (s64)(s32)reg->smin_value; |
| 4695 | reg->smax_value = (s64)(s32)reg->smax_value; |
| 4696 | |
| 4697 | val = (u32)val; |
| 4698 | sval = (s64)(s32)val; |
| 4699 | } else { |
| 4700 | sval = (s64)val; |
| 4701 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4702 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4703 | switch (opcode) { |
| 4704 | case BPF_JEQ: |
| 4705 | if (tnum_is_const(reg->var_off)) |
| 4706 | return !!tnum_equals_const(reg->var_off, val); |
| 4707 | break; |
| 4708 | case BPF_JNE: |
| 4709 | if (tnum_is_const(reg->var_off)) |
| 4710 | return !tnum_equals_const(reg->var_off, val); |
| 4711 | break; |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 4712 | case BPF_JSET: |
| 4713 | if ((~reg->var_off.mask & reg->var_off.value) & val) |
| 4714 | return 1; |
| 4715 | if (!((reg->var_off.mask | reg->var_off.value) & val)) |
| 4716 | return 0; |
| 4717 | break; |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4718 | case BPF_JGT: |
| 4719 | if (reg->umin_value > val) |
| 4720 | return 1; |
| 4721 | else if (reg->umax_value <= val) |
| 4722 | return 0; |
| 4723 | break; |
| 4724 | case BPF_JSGT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4725 | if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4726 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4727 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4728 | return 0; |
| 4729 | break; |
| 4730 | case BPF_JLT: |
| 4731 | if (reg->umax_value < val) |
| 4732 | return 1; |
| 4733 | else if (reg->umin_value >= val) |
| 4734 | return 0; |
| 4735 | break; |
| 4736 | case BPF_JSLT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4737 | if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4738 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4739 | else if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4740 | return 0; |
| 4741 | break; |
| 4742 | case BPF_JGE: |
| 4743 | if (reg->umin_value >= val) |
| 4744 | return 1; |
| 4745 | else if (reg->umax_value < val) |
| 4746 | return 0; |
| 4747 | break; |
| 4748 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4749 | if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4750 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4751 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4752 | return 0; |
| 4753 | break; |
| 4754 | case BPF_JLE: |
| 4755 | if (reg->umax_value <= val) |
| 4756 | return 1; |
| 4757 | else if (reg->umin_value > val) |
| 4758 | return 0; |
| 4759 | break; |
| 4760 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4761 | if (reg->smax_value <= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4762 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4763 | else if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4764 | return 0; |
| 4765 | break; |
| 4766 | } |
| 4767 | |
| 4768 | return -1; |
| 4769 | } |
| 4770 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4771 | /* Generate min value of the high 32-bit from TNUM info. */ |
| 4772 | static u64 gen_hi_min(struct tnum var) |
| 4773 | { |
| 4774 | return var.value & ~0xffffffffULL; |
| 4775 | } |
| 4776 | |
| 4777 | /* Generate max value of the high 32-bit from TNUM info. */ |
| 4778 | static u64 gen_hi_max(struct tnum var) |
| 4779 | { |
| 4780 | return (var.value | var.mask) & ~0xffffffffULL; |
| 4781 | } |
| 4782 | |
| 4783 | /* Return true if VAL is compared with a s64 sign extended from s32, and they |
| 4784 | * are with the same signedness. |
| 4785 | */ |
| 4786 | static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg) |
| 4787 | { |
| 4788 | return ((s32)sval >= 0 && |
| 4789 | reg->smin_value >= 0 && reg->smax_value <= S32_MAX) || |
| 4790 | ((s32)sval < 0 && |
| 4791 | reg->smax_value <= 0 && reg->smin_value >= S32_MIN); |
| 4792 | } |
| 4793 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4794 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 4795 | * variable register that we are working on, and src_reg is a constant or we're |
| 4796 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4797 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4798 | */ |
| 4799 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 4800 | struct bpf_reg_state *false_reg, u64 val, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4801 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4802 | { |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4803 | s64 sval; |
| 4804 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4805 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 4806 | * variable offset from the compare (unless src_reg were a pointer into |
| 4807 | * the same object, but we don't bother with that. |
| 4808 | * Since false_reg and true_reg have the same type by construction, we |
| 4809 | * only need to check one of them for pointerness. |
| 4810 | */ |
| 4811 | if (__is_pointer_value(false, false_reg)) |
| 4812 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 4813 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4814 | val = is_jmp32 ? (u32)val : val; |
| 4815 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4816 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4817 | switch (opcode) { |
| 4818 | case BPF_JEQ: |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4819 | case BPF_JNE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4820 | { |
| 4821 | struct bpf_reg_state *reg = |
| 4822 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 4823 | |
| 4824 | /* For BPF_JEQ, if this is false we know nothing Jon Snow, but |
| 4825 | * if it is true we know the value for sure. Likewise for |
| 4826 | * BPF_JNE. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4827 | */ |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4828 | if (is_jmp32) { |
| 4829 | u64 old_v = reg->var_off.value; |
| 4830 | u64 hi_mask = ~0xffffffffULL; |
| 4831 | |
| 4832 | reg->var_off.value = (old_v & hi_mask) | val; |
| 4833 | reg->var_off.mask &= hi_mask; |
| 4834 | } else { |
| 4835 | __mark_reg_known(reg, val); |
| 4836 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4837 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4838 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 4839 | case BPF_JSET: |
| 4840 | false_reg->var_off = tnum_and(false_reg->var_off, |
| 4841 | tnum_const(~val)); |
| 4842 | if (is_power_of_2(val)) |
| 4843 | true_reg->var_off = tnum_or(true_reg->var_off, |
| 4844 | tnum_const(val)); |
| 4845 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4846 | case BPF_JGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4847 | case BPF_JGT: |
| 4848 | { |
| 4849 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; |
| 4850 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; |
| 4851 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4852 | if (is_jmp32) { |
| 4853 | false_umax += gen_hi_max(false_reg->var_off); |
| 4854 | true_umin += gen_hi_min(true_reg->var_off); |
| 4855 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4856 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 4857 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4858 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4859 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4860 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4861 | case BPF_JSGT: |
| 4862 | { |
| 4863 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; |
| 4864 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; |
| 4865 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4866 | /* If the full s64 was not sign-extended from s32 then don't |
| 4867 | * deduct further info. |
| 4868 | */ |
| 4869 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 4870 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4871 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 4872 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4873 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4874 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4875 | case BPF_JLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4876 | case BPF_JLT: |
| 4877 | { |
| 4878 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; |
| 4879 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; |
| 4880 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4881 | if (is_jmp32) { |
| 4882 | false_umin += gen_hi_min(false_reg->var_off); |
| 4883 | true_umax += gen_hi_max(true_reg->var_off); |
| 4884 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4885 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 4886 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4887 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4888 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4889 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4890 | case BPF_JSLT: |
| 4891 | { |
| 4892 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; |
| 4893 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; |
| 4894 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4895 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 4896 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4897 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 4898 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4899 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4900 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4901 | default: |
| 4902 | break; |
| 4903 | } |
| 4904 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4905 | __reg_deduce_bounds(false_reg); |
| 4906 | __reg_deduce_bounds(true_reg); |
| 4907 | /* We might have learned some bits from the bounds. */ |
| 4908 | __reg_bound_offset(false_reg); |
| 4909 | __reg_bound_offset(true_reg); |
| 4910 | /* Intersecting with the old var_off might have improved our bounds |
| 4911 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 4912 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 4913 | */ |
| 4914 | __update_reg_bounds(false_reg); |
| 4915 | __update_reg_bounds(true_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4916 | } |
| 4917 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4918 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 4919 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4920 | */ |
| 4921 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 4922 | struct bpf_reg_state *false_reg, u64 val, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4923 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4924 | { |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4925 | s64 sval; |
| 4926 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4927 | if (__is_pointer_value(false, false_reg)) |
| 4928 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 4929 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4930 | val = is_jmp32 ? (u32)val : val; |
| 4931 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4932 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4933 | switch (opcode) { |
| 4934 | case BPF_JEQ: |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4935 | case BPF_JNE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4936 | { |
| 4937 | struct bpf_reg_state *reg = |
| 4938 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 4939 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4940 | if (is_jmp32) { |
| 4941 | u64 old_v = reg->var_off.value; |
| 4942 | u64 hi_mask = ~0xffffffffULL; |
| 4943 | |
| 4944 | reg->var_off.value = (old_v & hi_mask) | val; |
| 4945 | reg->var_off.mask &= hi_mask; |
| 4946 | } else { |
| 4947 | __mark_reg_known(reg, val); |
| 4948 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4949 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4950 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 4951 | case BPF_JSET: |
| 4952 | false_reg->var_off = tnum_and(false_reg->var_off, |
| 4953 | tnum_const(~val)); |
| 4954 | if (is_power_of_2(val)) |
| 4955 | true_reg->var_off = tnum_or(true_reg->var_off, |
| 4956 | tnum_const(val)); |
| 4957 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4958 | case BPF_JGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4959 | case BPF_JGT: |
| 4960 | { |
| 4961 | u64 false_umin = opcode == BPF_JGT ? val : val + 1; |
| 4962 | u64 true_umax = opcode == BPF_JGT ? val - 1 : val; |
| 4963 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4964 | if (is_jmp32) { |
| 4965 | false_umin += gen_hi_min(false_reg->var_off); |
| 4966 | true_umax += gen_hi_max(true_reg->var_off); |
| 4967 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4968 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 4969 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4970 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4971 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4972 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4973 | case BPF_JSGT: |
| 4974 | { |
| 4975 | s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1; |
| 4976 | s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval; |
| 4977 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4978 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 4979 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4980 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 4981 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4982 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4983 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4984 | case BPF_JLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4985 | case BPF_JLT: |
| 4986 | { |
| 4987 | u64 false_umax = opcode == BPF_JLT ? val : val - 1; |
| 4988 | u64 true_umin = opcode == BPF_JLT ? val + 1 : val; |
| 4989 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4990 | if (is_jmp32) { |
| 4991 | false_umax += gen_hi_max(false_reg->var_off); |
| 4992 | true_umin += gen_hi_min(true_reg->var_off); |
| 4993 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4994 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 4995 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4996 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4997 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4998 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4999 | case BPF_JSLT: |
| 5000 | { |
| 5001 | s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1; |
| 5002 | s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval; |
| 5003 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5004 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 5005 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5006 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 5007 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 5008 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 5009 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5010 | default: |
| 5011 | break; |
| 5012 | } |
| 5013 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5014 | __reg_deduce_bounds(false_reg); |
| 5015 | __reg_deduce_bounds(true_reg); |
| 5016 | /* We might have learned some bits from the bounds. */ |
| 5017 | __reg_bound_offset(false_reg); |
| 5018 | __reg_bound_offset(true_reg); |
| 5019 | /* Intersecting with the old var_off might have improved our bounds |
| 5020 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 5021 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 5022 | */ |
| 5023 | __update_reg_bounds(false_reg); |
| 5024 | __update_reg_bounds(true_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5025 | } |
| 5026 | |
| 5027 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 5028 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 5029 | struct bpf_reg_state *dst_reg) |
| 5030 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5031 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 5032 | dst_reg->umin_value); |
| 5033 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 5034 | dst_reg->umax_value); |
| 5035 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 5036 | dst_reg->smin_value); |
| 5037 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 5038 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5039 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 5040 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5041 | /* We might have learned new bounds from the var_off. */ |
| 5042 | __update_reg_bounds(src_reg); |
| 5043 | __update_reg_bounds(dst_reg); |
| 5044 | /* We might have learned something about the sign bit. */ |
| 5045 | __reg_deduce_bounds(src_reg); |
| 5046 | __reg_deduce_bounds(dst_reg); |
| 5047 | /* We might have learned some bits from the bounds. */ |
| 5048 | __reg_bound_offset(src_reg); |
| 5049 | __reg_bound_offset(dst_reg); |
| 5050 | /* Intersecting with the old var_off might have improved our bounds |
| 5051 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 5052 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 5053 | */ |
| 5054 | __update_reg_bounds(src_reg); |
| 5055 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 5059 | struct bpf_reg_state *true_dst, |
| 5060 | struct bpf_reg_state *false_src, |
| 5061 | struct bpf_reg_state *false_dst, |
| 5062 | u8 opcode) |
| 5063 | { |
| 5064 | switch (opcode) { |
| 5065 | case BPF_JEQ: |
| 5066 | __reg_combine_min_max(true_src, true_dst); |
| 5067 | break; |
| 5068 | case BPF_JNE: |
| 5069 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5070 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 5071 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5072 | } |
| 5073 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5074 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 5075 | struct bpf_reg_state *reg, u32 id, |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5076 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5077 | { |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5078 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5079 | /* Old offset (both fixed and variable parts) should |
| 5080 | * have been known-zero, because we don't allow pointer |
| 5081 | * arithmetic on pointers that might be NULL. |
| 5082 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5083 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 5084 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5085 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5086 | __mark_reg_known_zero(reg); |
| 5087 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5088 | } |
| 5089 | if (is_null) { |
| 5090 | reg->type = SCALAR_VALUE; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5091 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
| 5092 | if (reg->map_ptr->inner_map_meta) { |
| 5093 | reg->type = CONST_PTR_TO_MAP; |
| 5094 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 5095 | } else if (reg->map_ptr->map_type == |
| 5096 | BPF_MAP_TYPE_XSKMAP) { |
| 5097 | reg->type = PTR_TO_XDP_SOCK; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5098 | } else { |
| 5099 | reg->type = PTR_TO_MAP_VALUE; |
| 5100 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5101 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
| 5102 | reg->type = PTR_TO_SOCKET; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 5103 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
| 5104 | reg->type = PTR_TO_SOCK_COMMON; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 5105 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
| 5106 | reg->type = PTR_TO_TCP_SOCK; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5107 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5108 | if (is_null) { |
| 5109 | /* We don't need id and ref_obj_id from this point |
| 5110 | * onwards anymore, thus we should better reset it, |
| 5111 | * so that state pruning has chances to take effect. |
| 5112 | */ |
| 5113 | reg->id = 0; |
| 5114 | reg->ref_obj_id = 0; |
| 5115 | } else if (!reg_may_point_to_spin_lock(reg)) { |
| 5116 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
| 5117 | * in release_reg_references(). |
| 5118 | * |
| 5119 | * reg->id is still used by spin_lock ptr. Other |
| 5120 | * than spin_lock ptr type, reg->id can be reset. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5121 | */ |
| 5122 | reg->id = 0; |
| 5123 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5124 | } |
| 5125 | } |
| 5126 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5127 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
| 5128 | bool is_null) |
| 5129 | { |
| 5130 | struct bpf_reg_state *reg; |
| 5131 | int i; |
| 5132 | |
| 5133 | for (i = 0; i < MAX_BPF_REG; i++) |
| 5134 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); |
| 5135 | |
| 5136 | bpf_for_each_spilled_reg(i, state, reg) { |
| 5137 | if (!reg) |
| 5138 | continue; |
| 5139 | mark_ptr_or_null_reg(state, reg, id, is_null); |
| 5140 | } |
| 5141 | } |
| 5142 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5143 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 5144 | * be folded together at some point. |
| 5145 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5146 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 5147 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5148 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5149 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5150 | struct bpf_reg_state *regs = state->regs; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5151 | u32 ref_obj_id = regs[regno].ref_obj_id; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 5152 | u32 id = regs[regno].id; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5153 | int i; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5154 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5155 | if (ref_obj_id && ref_obj_id == id && is_null) |
| 5156 | /* regs[regno] is in the " == NULL" branch. |
| 5157 | * No one could have freed the reference state before |
| 5158 | * doing the NULL check. |
| 5159 | */ |
| 5160 | WARN_ON_ONCE(release_reference_state(state, id)); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5161 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5162 | for (i = 0; i <= vstate->curframe; i++) |
| 5163 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5164 | } |
| 5165 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 5166 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 5167 | struct bpf_reg_state *dst_reg, |
| 5168 | struct bpf_reg_state *src_reg, |
| 5169 | struct bpf_verifier_state *this_branch, |
| 5170 | struct bpf_verifier_state *other_branch) |
| 5171 | { |
| 5172 | if (BPF_SRC(insn->code) != BPF_X) |
| 5173 | return false; |
| 5174 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5175 | /* Pointers are always 64-bit. */ |
| 5176 | if (BPF_CLASS(insn->code) == BPF_JMP32) |
| 5177 | return false; |
| 5178 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 5179 | switch (BPF_OP(insn->code)) { |
| 5180 | case BPF_JGT: |
| 5181 | if ((dst_reg->type == PTR_TO_PACKET && |
| 5182 | src_reg->type == PTR_TO_PACKET_END) || |
| 5183 | (dst_reg->type == PTR_TO_PACKET_META && |
| 5184 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 5185 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 5186 | find_good_pkt_pointers(this_branch, dst_reg, |
| 5187 | dst_reg->type, false); |
| 5188 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 5189 | src_reg->type == PTR_TO_PACKET) || |
| 5190 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 5191 | src_reg->type == PTR_TO_PACKET_META)) { |
| 5192 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 5193 | find_good_pkt_pointers(other_branch, src_reg, |
| 5194 | src_reg->type, true); |
| 5195 | } else { |
| 5196 | return false; |
| 5197 | } |
| 5198 | break; |
| 5199 | case BPF_JLT: |
| 5200 | if ((dst_reg->type == PTR_TO_PACKET && |
| 5201 | src_reg->type == PTR_TO_PACKET_END) || |
| 5202 | (dst_reg->type == PTR_TO_PACKET_META && |
| 5203 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 5204 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 5205 | find_good_pkt_pointers(other_branch, dst_reg, |
| 5206 | dst_reg->type, true); |
| 5207 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 5208 | src_reg->type == PTR_TO_PACKET) || |
| 5209 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 5210 | src_reg->type == PTR_TO_PACKET_META)) { |
| 5211 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 5212 | find_good_pkt_pointers(this_branch, src_reg, |
| 5213 | src_reg->type, false); |
| 5214 | } else { |
| 5215 | return false; |
| 5216 | } |
| 5217 | break; |
| 5218 | case BPF_JGE: |
| 5219 | if ((dst_reg->type == PTR_TO_PACKET && |
| 5220 | src_reg->type == PTR_TO_PACKET_END) || |
| 5221 | (dst_reg->type == PTR_TO_PACKET_META && |
| 5222 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 5223 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 5224 | find_good_pkt_pointers(this_branch, dst_reg, |
| 5225 | dst_reg->type, true); |
| 5226 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 5227 | src_reg->type == PTR_TO_PACKET) || |
| 5228 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 5229 | src_reg->type == PTR_TO_PACKET_META)) { |
| 5230 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 5231 | find_good_pkt_pointers(other_branch, src_reg, |
| 5232 | src_reg->type, false); |
| 5233 | } else { |
| 5234 | return false; |
| 5235 | } |
| 5236 | break; |
| 5237 | case BPF_JLE: |
| 5238 | if ((dst_reg->type == PTR_TO_PACKET && |
| 5239 | src_reg->type == PTR_TO_PACKET_END) || |
| 5240 | (dst_reg->type == PTR_TO_PACKET_META && |
| 5241 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 5242 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 5243 | find_good_pkt_pointers(other_branch, dst_reg, |
| 5244 | dst_reg->type, false); |
| 5245 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 5246 | src_reg->type == PTR_TO_PACKET) || |
| 5247 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 5248 | src_reg->type == PTR_TO_PACKET_META)) { |
| 5249 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 5250 | find_good_pkt_pointers(this_branch, src_reg, |
| 5251 | src_reg->type, true); |
| 5252 | } else { |
| 5253 | return false; |
| 5254 | } |
| 5255 | break; |
| 5256 | default: |
| 5257 | return false; |
| 5258 | } |
| 5259 | |
| 5260 | return true; |
| 5261 | } |
| 5262 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5263 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5264 | struct bpf_insn *insn, int *insn_idx) |
| 5265 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5266 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 5267 | struct bpf_verifier_state *other_branch; |
| 5268 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame^] | 5269 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5270 | u8 opcode = BPF_OP(insn->code); |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5271 | bool is_jmp32; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame^] | 5272 | int pred = -1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5273 | int err; |
| 5274 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5275 | /* Only conditional jumps are expected to reach here. */ |
| 5276 | if (opcode == BPF_JA || opcode > BPF_JSLE) { |
| 5277 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5278 | return -EINVAL; |
| 5279 | } |
| 5280 | |
| 5281 | if (BPF_SRC(insn->code) == BPF_X) { |
| 5282 | if (insn->imm != 0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5283 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5284 | return -EINVAL; |
| 5285 | } |
| 5286 | |
| 5287 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5288 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5289 | if (err) |
| 5290 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5291 | |
| 5292 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5293 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5294 | insn->src_reg); |
| 5295 | return -EACCES; |
| 5296 | } |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame^] | 5297 | src_reg = ®s[insn->src_reg]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5298 | } else { |
| 5299 | if (insn->src_reg != BPF_REG_0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5300 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5301 | return -EINVAL; |
| 5302 | } |
| 5303 | } |
| 5304 | |
| 5305 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5306 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5307 | if (err) |
| 5308 | return err; |
| 5309 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 5310 | dst_reg = ®s[insn->dst_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5311 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 5312 | |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame^] | 5313 | if (BPF_SRC(insn->code) == BPF_K) |
| 5314 | pred = is_branch_taken(dst_reg, insn->imm, |
| 5315 | opcode, is_jmp32); |
| 5316 | else if (src_reg->type == SCALAR_VALUE && |
| 5317 | tnum_is_const(src_reg->var_off)) |
| 5318 | pred = is_branch_taken(dst_reg, src_reg->var_off.value, |
| 5319 | opcode, is_jmp32); |
| 5320 | if (pred == 1) { |
| 5321 | /* only follow the goto, ignore fall-through */ |
| 5322 | *insn_idx += insn->off; |
| 5323 | return 0; |
| 5324 | } else if (pred == 0) { |
| 5325 | /* only follow fall-through branch, since |
| 5326 | * that's where the program will go |
| 5327 | */ |
| 5328 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5329 | } |
| 5330 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5331 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
| 5332 | false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5333 | if (!other_branch) |
| 5334 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5335 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5336 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5337 | /* detect if we are comparing against a constant value so we can adjust |
| 5338 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5339 | * this is only legit if both are scalars (or pointers to the same |
| 5340 | * object, I suppose, but we don't support that right now), because |
| 5341 | * otherwise the different base pointers mean the offsets aren't |
| 5342 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5343 | */ |
| 5344 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5345 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
| 5346 | struct bpf_reg_state lo_reg0 = *dst_reg; |
| 5347 | struct bpf_reg_state lo_reg1 = *src_reg; |
| 5348 | struct bpf_reg_state *src_lo, *dst_lo; |
| 5349 | |
| 5350 | dst_lo = &lo_reg0; |
| 5351 | src_lo = &lo_reg1; |
| 5352 | coerce_reg_to_size(dst_lo, 4); |
| 5353 | coerce_reg_to_size(src_lo, 4); |
| 5354 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5355 | if (dst_reg->type == SCALAR_VALUE && |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5356 | src_reg->type == SCALAR_VALUE) { |
| 5357 | if (tnum_is_const(src_reg->var_off) || |
| 5358 | (is_jmp32 && tnum_is_const(src_lo->var_off))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5359 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5360 | dst_reg, |
| 5361 | is_jmp32 |
| 5362 | ? src_lo->var_off.value |
| 5363 | : src_reg->var_off.value, |
| 5364 | opcode, is_jmp32); |
| 5365 | else if (tnum_is_const(dst_reg->var_off) || |
| 5366 | (is_jmp32 && tnum_is_const(dst_lo->var_off))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5367 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5368 | src_reg, |
| 5369 | is_jmp32 |
| 5370 | ? dst_lo->var_off.value |
| 5371 | : dst_reg->var_off.value, |
| 5372 | opcode, is_jmp32); |
| 5373 | else if (!is_jmp32 && |
| 5374 | (opcode == BPF_JEQ || opcode == BPF_JNE)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5375 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5376 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 5377 | &other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5378 | src_reg, dst_reg, opcode); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5379 | } |
| 5380 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5381 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5382 | dst_reg, insn->imm, opcode, is_jmp32); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5383 | } |
| 5384 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5385 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
| 5386 | * NOTE: these optimizations below are related with pointer comparison |
| 5387 | * which will never be JMP32. |
| 5388 | */ |
| 5389 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 5390 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5391 | reg_type_may_be_null(dst_reg->type)) { |
| 5392 | /* Mark all identical registers in each branch as either |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5393 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 5394 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5395 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 5396 | opcode == BPF_JNE); |
| 5397 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 5398 | opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 5399 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 5400 | this_branch, other_branch) && |
| 5401 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5402 | verbose(env, "R%d pointer comparison prohibited\n", |
| 5403 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5404 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5405 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 5406 | if (env->log.level & BPF_LOG_LEVEL) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5407 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5408 | return 0; |
| 5409 | } |
| 5410 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5411 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5412 | 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] | 5413 | { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 5414 | struct bpf_insn_aux_data *aux = cur_aux(env); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5415 | struct bpf_reg_state *regs = cur_regs(env); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 5416 | struct bpf_map *map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5417 | int err; |
| 5418 | |
| 5419 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5420 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5421 | return -EINVAL; |
| 5422 | } |
| 5423 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5424 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5425 | return -EINVAL; |
| 5426 | } |
| 5427 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5428 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5429 | if (err) |
| 5430 | return err; |
| 5431 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 5432 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 5433 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 5434 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5435 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5436 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5437 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 5438 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5439 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 5440 | map = env->used_maps[aux->map_index]; |
| 5441 | mark_reg_known_zero(env, regs, insn->dst_reg); |
| 5442 | regs[insn->dst_reg].map_ptr = map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5443 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 5444 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { |
| 5445 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; |
| 5446 | regs[insn->dst_reg].off = aux->map_off; |
| 5447 | if (map_value_has_spin_lock(map)) |
| 5448 | regs[insn->dst_reg].id = ++env->id_gen; |
| 5449 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 5450 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 5451 | } else { |
| 5452 | verbose(env, "bpf verifier is misconfigured\n"); |
| 5453 | return -EINVAL; |
| 5454 | } |
| 5455 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5456 | return 0; |
| 5457 | } |
| 5458 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 5459 | static bool may_access_skb(enum bpf_prog_type type) |
| 5460 | { |
| 5461 | switch (type) { |
| 5462 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 5463 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 5464 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 5465 | return true; |
| 5466 | default: |
| 5467 | return false; |
| 5468 | } |
| 5469 | } |
| 5470 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5471 | /* verify safety of LD_ABS|LD_IND instructions: |
| 5472 | * - they can only appear in the programs where ctx == skb |
| 5473 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 5474 | * preserve R6-R9, and store return value into R0 |
| 5475 | * |
| 5476 | * Implicit input: |
| 5477 | * ctx == skb == R6 == CTX |
| 5478 | * |
| 5479 | * Explicit input: |
| 5480 | * SRC == any register |
| 5481 | * IMM == 32-bit immediate |
| 5482 | * |
| 5483 | * Output: |
| 5484 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 5485 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5486 | 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] | 5487 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5488 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5489 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5490 | int i, err; |
| 5491 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 5492 | if (!may_access_skb(env->prog->type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5493 | 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] | 5494 | return -EINVAL; |
| 5495 | } |
| 5496 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 5497 | if (!env->ops->gen_ld_abs) { |
| 5498 | verbose(env, "bpf verifier is misconfigured\n"); |
| 5499 | return -EINVAL; |
| 5500 | } |
| 5501 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5502 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5503 | /* when program has LD_ABS insn JITs and interpreter assume |
| 5504 | * that r1 == ctx == skb which is not the case for callees |
| 5505 | * that can have arbitrary arguments. It's problematic |
| 5506 | * for main prog as well since JITs would need to analyze |
| 5507 | * all functions in order to make proper register save/restore |
| 5508 | * decisions in the main prog. Hence disallow LD_ABS with calls |
| 5509 | */ |
| 5510 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); |
| 5511 | return -EINVAL; |
| 5512 | } |
| 5513 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5514 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 5515 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5516 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5517 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5518 | return -EINVAL; |
| 5519 | } |
| 5520 | |
| 5521 | /* check whether implicit source operand (register R6) is readable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5522 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5523 | if (err) |
| 5524 | return err; |
| 5525 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5526 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 5527 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 5528 | * reference leak. |
| 5529 | */ |
| 5530 | err = check_reference_leak(env); |
| 5531 | if (err) { |
| 5532 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 5533 | return err; |
| 5534 | } |
| 5535 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 5536 | if (env->cur_state->active_spin_lock) { |
| 5537 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
| 5538 | return -EINVAL; |
| 5539 | } |
| 5540 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5541 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5542 | verbose(env, |
| 5543 | "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] | 5544 | return -EINVAL; |
| 5545 | } |
| 5546 | |
| 5547 | if (mode == BPF_IND) { |
| 5548 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5549 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5550 | if (err) |
| 5551 | return err; |
| 5552 | } |
| 5553 | |
| 5554 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5555 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5556 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5557 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 5558 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5559 | |
| 5560 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5561 | * the value fetched from the packet. |
| 5562 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5563 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5564 | mark_reg_unknown(env, regs, BPF_REG_0); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 5565 | /* ld_abs load up to 32-bit skb data. */ |
| 5566 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5567 | return 0; |
| 5568 | } |
| 5569 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5570 | static int check_return_code(struct bpf_verifier_env *env) |
| 5571 | { |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 5572 | struct tnum enforce_attach_type_range = tnum_unknown; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5573 | struct bpf_reg_state *reg; |
| 5574 | struct tnum range = tnum_range(0, 1); |
| 5575 | |
| 5576 | switch (env->prog->type) { |
| 5577 | case BPF_PROG_TYPE_CGROUP_SKB: |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 5578 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
| 5579 | range = tnum_range(0, 3); |
| 5580 | enforce_attach_type_range = tnum_range(2, 3); |
| 5581 | } |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5582 | case BPF_PROG_TYPE_CGROUP_SOCK: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 5583 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5584 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 5585 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 5586 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5587 | break; |
| 5588 | default: |
| 5589 | return 0; |
| 5590 | } |
| 5591 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5592 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5593 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5594 | 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] | 5595 | reg_type_str[reg->type]); |
| 5596 | return -EINVAL; |
| 5597 | } |
| 5598 | |
| 5599 | if (!tnum_in(range, reg->var_off)) { |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 5600 | char tn_buf[48]; |
| 5601 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5602 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5603 | if (!tnum_is_unknown(reg->var_off)) { |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5604 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5605 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5606 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5607 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5608 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 5609 | tnum_strn(tn_buf, sizeof(tn_buf), range); |
| 5610 | verbose(env, " should have been %s\n", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5611 | return -EINVAL; |
| 5612 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 5613 | |
| 5614 | if (!tnum_is_unknown(enforce_attach_type_range) && |
| 5615 | tnum_in(enforce_attach_type_range, reg->var_off)) |
| 5616 | env->prog->enforce_expected_attach_type = 1; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5617 | return 0; |
| 5618 | } |
| 5619 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5620 | /* non-recursive DFS pseudo code |
| 5621 | * 1 procedure DFS-iterative(G,v): |
| 5622 | * 2 label v as discovered |
| 5623 | * 3 let S be a stack |
| 5624 | * 4 S.push(v) |
| 5625 | * 5 while S is not empty |
| 5626 | * 6 t <- S.pop() |
| 5627 | * 7 if t is what we're looking for: |
| 5628 | * 8 return t |
| 5629 | * 9 for all edges e in G.adjacentEdges(t) do |
| 5630 | * 10 if edge e is already labelled |
| 5631 | * 11 continue with the next edge |
| 5632 | * 12 w <- G.adjacentVertex(t,e) |
| 5633 | * 13 if vertex w is not discovered and not explored |
| 5634 | * 14 label e as tree-edge |
| 5635 | * 15 label w as discovered |
| 5636 | * 16 S.push(w) |
| 5637 | * 17 continue at 5 |
| 5638 | * 18 else if vertex w is discovered |
| 5639 | * 19 label e as back-edge |
| 5640 | * 20 else |
| 5641 | * 21 // vertex w is explored |
| 5642 | * 22 label e as forward- or cross-edge |
| 5643 | * 23 label t as explored |
| 5644 | * 24 S.pop() |
| 5645 | * |
| 5646 | * convention: |
| 5647 | * 0x10 - discovered |
| 5648 | * 0x11 - discovered and fall-through edge labelled |
| 5649 | * 0x12 - discovered and fall-through and branch edges labelled |
| 5650 | * 0x20 - explored |
| 5651 | */ |
| 5652 | |
| 5653 | enum { |
| 5654 | DISCOVERED = 0x10, |
| 5655 | EXPLORED = 0x20, |
| 5656 | FALLTHROUGH = 1, |
| 5657 | BRANCH = 2, |
| 5658 | }; |
| 5659 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 5660 | static u32 state_htab_size(struct bpf_verifier_env *env) |
| 5661 | { |
| 5662 | return env->prog->len; |
| 5663 | } |
| 5664 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5665 | static struct bpf_verifier_state_list **explored_state( |
| 5666 | struct bpf_verifier_env *env, |
| 5667 | int idx) |
| 5668 | { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 5669 | struct bpf_verifier_state *cur = env->cur_state; |
| 5670 | struct bpf_func_state *state = cur->frame[cur->curframe]; |
| 5671 | |
| 5672 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5673 | } |
| 5674 | |
| 5675 | static void init_explored_state(struct bpf_verifier_env *env, int idx) |
| 5676 | { |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 5677 | env->insn_aux_data[idx].prune_point = true; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5678 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5679 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5680 | /* t, w, e - match pseudo-code above: |
| 5681 | * t - index of current instruction |
| 5682 | * w - next instruction |
| 5683 | * e - edge |
| 5684 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5685 | 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] | 5686 | { |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5687 | int *insn_stack = env->cfg.insn_stack; |
| 5688 | int *insn_state = env->cfg.insn_state; |
| 5689 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5690 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 5691 | return 0; |
| 5692 | |
| 5693 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 5694 | return 0; |
| 5695 | |
| 5696 | if (w < 0 || w >= env->prog->len) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 5697 | verbose_linfo(env, t, "%d: ", t); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5698 | 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] | 5699 | return -EINVAL; |
| 5700 | } |
| 5701 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5702 | if (e == BRANCH) |
| 5703 | /* mark branch target for state pruning */ |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5704 | init_explored_state(env, w); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5705 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5706 | if (insn_state[w] == 0) { |
| 5707 | /* tree-edge */ |
| 5708 | insn_state[t] = DISCOVERED | e; |
| 5709 | insn_state[w] = DISCOVERED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5710 | if (env->cfg.cur_stack >= env->prog->len) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5711 | return -E2BIG; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5712 | insn_stack[env->cfg.cur_stack++] = w; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5713 | return 1; |
| 5714 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 5715 | verbose_linfo(env, t, "%d: ", t); |
| 5716 | verbose_linfo(env, w, "%d: ", w); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5717 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5718 | return -EINVAL; |
| 5719 | } else if (insn_state[w] == EXPLORED) { |
| 5720 | /* forward- or cross-edge */ |
| 5721 | insn_state[t] = DISCOVERED | e; |
| 5722 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5723 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5724 | return -EFAULT; |
| 5725 | } |
| 5726 | return 0; |
| 5727 | } |
| 5728 | |
| 5729 | /* non-recursive depth-first-search to detect loops in BPF program |
| 5730 | * loop == back-edge in directed graph |
| 5731 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5732 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5733 | { |
| 5734 | struct bpf_insn *insns = env->prog->insnsi; |
| 5735 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5736 | int *insn_stack, *insn_state; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5737 | int ret = 0; |
| 5738 | int i, t; |
| 5739 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5740 | 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] | 5741 | if (!insn_state) |
| 5742 | return -ENOMEM; |
| 5743 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5744 | 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] | 5745 | if (!insn_stack) { |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 5746 | kvfree(insn_state); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5747 | return -ENOMEM; |
| 5748 | } |
| 5749 | |
| 5750 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 5751 | insn_stack[0] = 0; /* 0 is the first instruction */ |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5752 | env->cfg.cur_stack = 1; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5753 | |
| 5754 | peek_stack: |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5755 | if (env->cfg.cur_stack == 0) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5756 | goto check_state; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5757 | t = insn_stack[env->cfg.cur_stack - 1]; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5758 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5759 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
| 5760 | BPF_CLASS(insns[t].code) == BPF_JMP32) { |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5761 | u8 opcode = BPF_OP(insns[t].code); |
| 5762 | |
| 5763 | if (opcode == BPF_EXIT) { |
| 5764 | goto mark_explored; |
| 5765 | } else if (opcode == BPF_CALL) { |
| 5766 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 5767 | if (ret == 1) |
| 5768 | goto peek_stack; |
| 5769 | else if (ret < 0) |
| 5770 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 5771 | if (t + 1 < insn_cnt) |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5772 | init_explored_state(env, t + 1); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5773 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5774 | init_explored_state(env, t); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5775 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); |
| 5776 | if (ret == 1) |
| 5777 | goto peek_stack; |
| 5778 | else if (ret < 0) |
| 5779 | goto err_free; |
| 5780 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5781 | } else if (opcode == BPF_JA) { |
| 5782 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 5783 | ret = -EINVAL; |
| 5784 | goto err_free; |
| 5785 | } |
| 5786 | /* unconditional jump with single edge */ |
| 5787 | ret = push_insn(t, t + insns[t].off + 1, |
| 5788 | FALLTHROUGH, env); |
| 5789 | if (ret == 1) |
| 5790 | goto peek_stack; |
| 5791 | else if (ret < 0) |
| 5792 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5793 | /* tell verifier to check for equivalent states |
| 5794 | * after every call and jump |
| 5795 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 5796 | if (t + 1 < insn_cnt) |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5797 | init_explored_state(env, t + 1); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5798 | } else { |
| 5799 | /* conditional jump with two edges */ |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5800 | init_explored_state(env, t); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5801 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 5802 | if (ret == 1) |
| 5803 | goto peek_stack; |
| 5804 | else if (ret < 0) |
| 5805 | goto err_free; |
| 5806 | |
| 5807 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 5808 | if (ret == 1) |
| 5809 | goto peek_stack; |
| 5810 | else if (ret < 0) |
| 5811 | goto err_free; |
| 5812 | } |
| 5813 | } else { |
| 5814 | /* all other non-branch instructions with single |
| 5815 | * fall-through edge |
| 5816 | */ |
| 5817 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 5818 | if (ret == 1) |
| 5819 | goto peek_stack; |
| 5820 | else if (ret < 0) |
| 5821 | goto err_free; |
| 5822 | } |
| 5823 | |
| 5824 | mark_explored: |
| 5825 | insn_state[t] = EXPLORED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5826 | if (env->cfg.cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5827 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5828 | ret = -EFAULT; |
| 5829 | goto err_free; |
| 5830 | } |
| 5831 | goto peek_stack; |
| 5832 | |
| 5833 | check_state: |
| 5834 | for (i = 0; i < insn_cnt; i++) { |
| 5835 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5836 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5837 | ret = -EINVAL; |
| 5838 | goto err_free; |
| 5839 | } |
| 5840 | } |
| 5841 | ret = 0; /* cfg looks good */ |
| 5842 | |
| 5843 | err_free: |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 5844 | kvfree(insn_state); |
| 5845 | kvfree(insn_stack); |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5846 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5847 | return ret; |
| 5848 | } |
| 5849 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5850 | /* The minimum supported BTF func info size */ |
| 5851 | #define MIN_BPF_FUNCINFO_SIZE 8 |
| 5852 | #define MAX_FUNCINFO_REC_SIZE 252 |
| 5853 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5854 | static int check_btf_func(struct bpf_verifier_env *env, |
| 5855 | const union bpf_attr *attr, |
| 5856 | union bpf_attr __user *uattr) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5857 | { |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 5858 | u32 i, nfuncs, urec_size, min_size; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5859 | u32 krec_size = sizeof(struct bpf_func_info); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5860 | struct bpf_func_info *krecord; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5861 | const struct btf_type *type; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5862 | struct bpf_prog *prog; |
| 5863 | const struct btf *btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5864 | void __user *urecord; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 5865 | u32 prev_offset = 0; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5866 | int ret = 0; |
| 5867 | |
| 5868 | nfuncs = attr->func_info_cnt; |
| 5869 | if (!nfuncs) |
| 5870 | return 0; |
| 5871 | |
| 5872 | if (nfuncs != env->subprog_cnt) { |
| 5873 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); |
| 5874 | return -EINVAL; |
| 5875 | } |
| 5876 | |
| 5877 | urec_size = attr->func_info_rec_size; |
| 5878 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || |
| 5879 | urec_size > MAX_FUNCINFO_REC_SIZE || |
| 5880 | urec_size % sizeof(u32)) { |
| 5881 | verbose(env, "invalid func info rec size %u\n", urec_size); |
| 5882 | return -EINVAL; |
| 5883 | } |
| 5884 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5885 | prog = env->prog; |
| 5886 | btf = prog->aux->btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5887 | |
| 5888 | urecord = u64_to_user_ptr(attr->func_info); |
| 5889 | min_size = min_t(u32, krec_size, urec_size); |
| 5890 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5891 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5892 | if (!krecord) |
| 5893 | return -ENOMEM; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5894 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5895 | for (i = 0; i < nfuncs; i++) { |
| 5896 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); |
| 5897 | if (ret) { |
| 5898 | if (ret == -E2BIG) { |
| 5899 | verbose(env, "nonzero tailing record in func info"); |
| 5900 | /* set the size kernel expects so loader can zero |
| 5901 | * out the rest of the record. |
| 5902 | */ |
| 5903 | if (put_user(min_size, &uattr->func_info_rec_size)) |
| 5904 | ret = -EFAULT; |
| 5905 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5906 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5907 | } |
| 5908 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5909 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5910 | ret = -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5911 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5912 | } |
| 5913 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5914 | /* check insn_off */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5915 | if (i == 0) { |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5916 | if (krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5917 | verbose(env, |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5918 | "nonzero insn_off %u for the first func info record", |
| 5919 | krecord[i].insn_off); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5920 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5921 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5922 | } |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5923 | } else if (krecord[i].insn_off <= prev_offset) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5924 | verbose(env, |
| 5925 | "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] | 5926 | krecord[i].insn_off, prev_offset); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5927 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5928 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5929 | } |
| 5930 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5931 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5932 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
| 5933 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5934 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5935 | } |
| 5936 | |
| 5937 | /* check type_id */ |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5938 | type = btf_type_by_id(btf, krecord[i].type_id); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5939 | if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) { |
| 5940 | verbose(env, "invalid type id %d in func info", |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5941 | krecord[i].type_id); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5942 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5943 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5944 | } |
| 5945 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5946 | prev_offset = krecord[i].insn_off; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5947 | urecord += urec_size; |
| 5948 | } |
| 5949 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5950 | prog->aux->func_info = krecord; |
| 5951 | prog->aux->func_info_cnt = nfuncs; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5952 | return 0; |
| 5953 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5954 | err_free: |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5955 | kvfree(krecord); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5956 | return ret; |
| 5957 | } |
| 5958 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5959 | static void adjust_btf_func(struct bpf_verifier_env *env) |
| 5960 | { |
| 5961 | int i; |
| 5962 | |
| 5963 | if (!env->prog->aux->func_info) |
| 5964 | return; |
| 5965 | |
| 5966 | for (i = 0; i < env->subprog_cnt; i++) |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5967 | 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] | 5968 | } |
| 5969 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5970 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
| 5971 | sizeof(((struct bpf_line_info *)(0))->line_col)) |
| 5972 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
| 5973 | |
| 5974 | static int check_btf_line(struct bpf_verifier_env *env, |
| 5975 | const union bpf_attr *attr, |
| 5976 | union bpf_attr __user *uattr) |
| 5977 | { |
| 5978 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; |
| 5979 | struct bpf_subprog_info *sub; |
| 5980 | struct bpf_line_info *linfo; |
| 5981 | struct bpf_prog *prog; |
| 5982 | const struct btf *btf; |
| 5983 | void __user *ulinfo; |
| 5984 | int err; |
| 5985 | |
| 5986 | nr_linfo = attr->line_info_cnt; |
| 5987 | if (!nr_linfo) |
| 5988 | return 0; |
| 5989 | |
| 5990 | rec_size = attr->line_info_rec_size; |
| 5991 | if (rec_size < MIN_BPF_LINEINFO_SIZE || |
| 5992 | rec_size > MAX_LINEINFO_REC_SIZE || |
| 5993 | rec_size & (sizeof(u32) - 1)) |
| 5994 | return -EINVAL; |
| 5995 | |
| 5996 | /* Need to zero it in case the userspace may |
| 5997 | * pass in a smaller bpf_line_info object. |
| 5998 | */ |
| 5999 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), |
| 6000 | GFP_KERNEL | __GFP_NOWARN); |
| 6001 | if (!linfo) |
| 6002 | return -ENOMEM; |
| 6003 | |
| 6004 | prog = env->prog; |
| 6005 | btf = prog->aux->btf; |
| 6006 | |
| 6007 | s = 0; |
| 6008 | sub = env->subprog_info; |
| 6009 | ulinfo = u64_to_user_ptr(attr->line_info); |
| 6010 | expected_size = sizeof(struct bpf_line_info); |
| 6011 | ncopy = min_t(u32, expected_size, rec_size); |
| 6012 | for (i = 0; i < nr_linfo; i++) { |
| 6013 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); |
| 6014 | if (err) { |
| 6015 | if (err == -E2BIG) { |
| 6016 | verbose(env, "nonzero tailing record in line_info"); |
| 6017 | if (put_user(expected_size, |
| 6018 | &uattr->line_info_rec_size)) |
| 6019 | err = -EFAULT; |
| 6020 | } |
| 6021 | goto err_free; |
| 6022 | } |
| 6023 | |
| 6024 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { |
| 6025 | err = -EFAULT; |
| 6026 | goto err_free; |
| 6027 | } |
| 6028 | |
| 6029 | /* |
| 6030 | * Check insn_off to ensure |
| 6031 | * 1) strictly increasing AND |
| 6032 | * 2) bounded by prog->len |
| 6033 | * |
| 6034 | * The linfo[0].insn_off == 0 check logically falls into |
| 6035 | * the later "missing bpf_line_info for func..." case |
| 6036 | * because the first linfo[0].insn_off must be the |
| 6037 | * first sub also and the first sub must have |
| 6038 | * subprog_info[0].start == 0. |
| 6039 | */ |
| 6040 | if ((i && linfo[i].insn_off <= prev_offset) || |
| 6041 | linfo[i].insn_off >= prog->len) { |
| 6042 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", |
| 6043 | i, linfo[i].insn_off, prev_offset, |
| 6044 | prog->len); |
| 6045 | err = -EINVAL; |
| 6046 | goto err_free; |
| 6047 | } |
| 6048 | |
Martin KaFai Lau | fdbaa0b | 2018-12-19 13:01:01 -0800 | [diff] [blame] | 6049 | if (!prog->insnsi[linfo[i].insn_off].code) { |
| 6050 | verbose(env, |
| 6051 | "Invalid insn code at line_info[%u].insn_off\n", |
| 6052 | i); |
| 6053 | err = -EINVAL; |
| 6054 | goto err_free; |
| 6055 | } |
| 6056 | |
Martin KaFai Lau | 23127b3 | 2018-12-13 10:41:46 -0800 | [diff] [blame] | 6057 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
| 6058 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6059 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
| 6060 | err = -EINVAL; |
| 6061 | goto err_free; |
| 6062 | } |
| 6063 | |
| 6064 | if (s != env->subprog_cnt) { |
| 6065 | if (linfo[i].insn_off == sub[s].start) { |
| 6066 | sub[s].linfo_idx = i; |
| 6067 | s++; |
| 6068 | } else if (sub[s].start < linfo[i].insn_off) { |
| 6069 | verbose(env, "missing bpf_line_info for func#%u\n", s); |
| 6070 | err = -EINVAL; |
| 6071 | goto err_free; |
| 6072 | } |
| 6073 | } |
| 6074 | |
| 6075 | prev_offset = linfo[i].insn_off; |
| 6076 | ulinfo += rec_size; |
| 6077 | } |
| 6078 | |
| 6079 | if (s != env->subprog_cnt) { |
| 6080 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", |
| 6081 | env->subprog_cnt - s, s); |
| 6082 | err = -EINVAL; |
| 6083 | goto err_free; |
| 6084 | } |
| 6085 | |
| 6086 | prog->aux->linfo = linfo; |
| 6087 | prog->aux->nr_linfo = nr_linfo; |
| 6088 | |
| 6089 | return 0; |
| 6090 | |
| 6091 | err_free: |
| 6092 | kvfree(linfo); |
| 6093 | return err; |
| 6094 | } |
| 6095 | |
| 6096 | static int check_btf_info(struct bpf_verifier_env *env, |
| 6097 | const union bpf_attr *attr, |
| 6098 | union bpf_attr __user *uattr) |
| 6099 | { |
| 6100 | struct btf *btf; |
| 6101 | int err; |
| 6102 | |
| 6103 | if (!attr->func_info_cnt && !attr->line_info_cnt) |
| 6104 | return 0; |
| 6105 | |
| 6106 | btf = btf_get_by_fd(attr->prog_btf_fd); |
| 6107 | if (IS_ERR(btf)) |
| 6108 | return PTR_ERR(btf); |
| 6109 | env->prog->aux->btf = btf; |
| 6110 | |
| 6111 | err = check_btf_func(env, attr, uattr); |
| 6112 | if (err) |
| 6113 | return err; |
| 6114 | |
| 6115 | err = check_btf_line(env, attr, uattr); |
| 6116 | if (err) |
| 6117 | return err; |
| 6118 | |
| 6119 | return 0; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6120 | } |
| 6121 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6122 | /* check %cur's range satisfies %old's */ |
| 6123 | static bool range_within(struct bpf_reg_state *old, |
| 6124 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6125 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6126 | return old->umin_value <= cur->umin_value && |
| 6127 | old->umax_value >= cur->umax_value && |
| 6128 | old->smin_value <= cur->smin_value && |
| 6129 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6130 | } |
| 6131 | |
| 6132 | /* Maximum number of register states that can exist at once */ |
| 6133 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 6134 | struct idpair { |
| 6135 | u32 old; |
| 6136 | u32 cur; |
| 6137 | }; |
| 6138 | |
| 6139 | /* If in the old state two registers had the same id, then they need to have |
| 6140 | * the same id in the new state as well. But that id could be different from |
| 6141 | * the old state, so we need to track the mapping from old to new ids. |
| 6142 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 6143 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 6144 | * regs with a different old id could still have new id 9, we don't care about |
| 6145 | * that. |
| 6146 | * So we look through our idmap to see if this old id has been seen before. If |
| 6147 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 6148 | */ |
| 6149 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 6150 | { |
| 6151 | unsigned int i; |
| 6152 | |
| 6153 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 6154 | if (!idmap[i].old) { |
| 6155 | /* Reached an empty slot; haven't seen this id before */ |
| 6156 | idmap[i].old = old_id; |
| 6157 | idmap[i].cur = cur_id; |
| 6158 | return true; |
| 6159 | } |
| 6160 | if (idmap[i].old == old_id) |
| 6161 | return idmap[i].cur == cur_id; |
| 6162 | } |
| 6163 | /* We ran out of idmap slots, which should be impossible */ |
| 6164 | WARN_ON_ONCE(1); |
| 6165 | return false; |
| 6166 | } |
| 6167 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 6168 | static void clean_func_state(struct bpf_verifier_env *env, |
| 6169 | struct bpf_func_state *st) |
| 6170 | { |
| 6171 | enum bpf_reg_liveness live; |
| 6172 | int i, j; |
| 6173 | |
| 6174 | for (i = 0; i < BPF_REG_FP; i++) { |
| 6175 | live = st->regs[i].live; |
| 6176 | /* liveness must not touch this register anymore */ |
| 6177 | st->regs[i].live |= REG_LIVE_DONE; |
| 6178 | if (!(live & REG_LIVE_READ)) |
| 6179 | /* since the register is unused, clear its state |
| 6180 | * to make further comparison simpler |
| 6181 | */ |
| 6182 | __mark_reg_not_init(&st->regs[i]); |
| 6183 | } |
| 6184 | |
| 6185 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { |
| 6186 | live = st->stack[i].spilled_ptr.live; |
| 6187 | /* liveness must not touch this stack slot anymore */ |
| 6188 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; |
| 6189 | if (!(live & REG_LIVE_READ)) { |
| 6190 | __mark_reg_not_init(&st->stack[i].spilled_ptr); |
| 6191 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 6192 | st->stack[i].slot_type[j] = STACK_INVALID; |
| 6193 | } |
| 6194 | } |
| 6195 | } |
| 6196 | |
| 6197 | static void clean_verifier_state(struct bpf_verifier_env *env, |
| 6198 | struct bpf_verifier_state *st) |
| 6199 | { |
| 6200 | int i; |
| 6201 | |
| 6202 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) |
| 6203 | /* all regs in this state in all frames were already marked */ |
| 6204 | return; |
| 6205 | |
| 6206 | for (i = 0; i <= st->curframe; i++) |
| 6207 | clean_func_state(env, st->frame[i]); |
| 6208 | } |
| 6209 | |
| 6210 | /* the parentage chains form a tree. |
| 6211 | * the verifier states are added to state lists at given insn and |
| 6212 | * pushed into state stack for future exploration. |
| 6213 | * when the verifier reaches bpf_exit insn some of the verifer states |
| 6214 | * stored in the state lists have their final liveness state already, |
| 6215 | * but a lot of states will get revised from liveness point of view when |
| 6216 | * the verifier explores other branches. |
| 6217 | * Example: |
| 6218 | * 1: r0 = 1 |
| 6219 | * 2: if r1 == 100 goto pc+1 |
| 6220 | * 3: r0 = 2 |
| 6221 | * 4: exit |
| 6222 | * when the verifier reaches exit insn the register r0 in the state list of |
| 6223 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch |
| 6224 | * of insn 2 and goes exploring further. At the insn 4 it will walk the |
| 6225 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. |
| 6226 | * |
| 6227 | * Since the verifier pushes the branch states as it sees them while exploring |
| 6228 | * the program the condition of walking the branch instruction for the second |
| 6229 | * time means that all states below this branch were already explored and |
| 6230 | * their final liveness markes are already propagated. |
| 6231 | * Hence when the verifier completes the search of state list in is_state_visited() |
| 6232 | * we can call this clean_live_states() function to mark all liveness states |
| 6233 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' |
| 6234 | * will not be used. |
| 6235 | * This function also clears the registers and stack for states that !READ |
| 6236 | * to simplify state merging. |
| 6237 | * |
| 6238 | * Important note here that walking the same branch instruction in the callee |
| 6239 | * doesn't meant that the states are DONE. The verifier has to compare |
| 6240 | * the callsites |
| 6241 | */ |
| 6242 | static void clean_live_states(struct bpf_verifier_env *env, int insn, |
| 6243 | struct bpf_verifier_state *cur) |
| 6244 | { |
| 6245 | struct bpf_verifier_state_list *sl; |
| 6246 | int i; |
| 6247 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 6248 | sl = *explored_state(env, insn); |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 6249 | while (sl) { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 6250 | if (sl->state.insn_idx != insn || |
| 6251 | sl->state.curframe != cur->curframe) |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 6252 | goto next; |
| 6253 | for (i = 0; i <= cur->curframe; i++) |
| 6254 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) |
| 6255 | goto next; |
| 6256 | clean_verifier_state(env, &sl->state); |
| 6257 | next: |
| 6258 | sl = sl->next; |
| 6259 | } |
| 6260 | } |
| 6261 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6262 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 6263 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 6264 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6265 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6266 | bool equal; |
| 6267 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6268 | if (!(rold->live & REG_LIVE_READ)) |
| 6269 | /* explored state didn't use this */ |
| 6270 | return true; |
| 6271 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6272 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6273 | |
| 6274 | if (rold->type == PTR_TO_STACK) |
| 6275 | /* two stack pointers are equal only if they're pointing to |
| 6276 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 6277 | */ |
| 6278 | return equal && rold->frameno == rcur->frameno; |
| 6279 | |
| 6280 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6281 | return true; |
| 6282 | |
| 6283 | if (rold->type == NOT_INIT) |
| 6284 | /* explored state can't have used this */ |
| 6285 | return true; |
| 6286 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6287 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6288 | switch (rold->type) { |
| 6289 | case SCALAR_VALUE: |
| 6290 | if (rcur->type == SCALAR_VALUE) { |
| 6291 | /* new val must satisfy old val knowledge */ |
| 6292 | return range_within(rold, rcur) && |
| 6293 | tnum_in(rold->var_off, rcur->var_off); |
| 6294 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 6295 | /* We're trying to use a pointer in place of a scalar. |
| 6296 | * Even if the scalar was unbounded, this could lead to |
| 6297 | * pointer leaks because scalars are allowed to leak |
| 6298 | * while pointers are not. We could make this safe in |
| 6299 | * special cases if root is calling us, but it's |
| 6300 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6301 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 6302 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6303 | } |
| 6304 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 6305 | /* If the new min/max/var_off satisfy the old ones and |
| 6306 | * everything else matches, we are OK. |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6307 | * 'id' is not compared, since it's only used for maps with |
| 6308 | * bpf_spin_lock inside map element and in such cases if |
| 6309 | * the rest of the prog is valid for one map element then |
| 6310 | * it's valid for all map elements regardless of the key |
| 6311 | * used in bpf_map_lookup() |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 6312 | */ |
| 6313 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 6314 | range_within(rold, rcur) && |
| 6315 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6316 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 6317 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 6318 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 6319 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 6320 | * checked, doing so could have affected others with the same |
| 6321 | * id, and we can't check for that because we lost the id when |
| 6322 | * we converted to a PTR_TO_MAP_VALUE. |
| 6323 | */ |
| 6324 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 6325 | return false; |
| 6326 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 6327 | return false; |
| 6328 | /* Check our ids match any regs they're supposed to */ |
| 6329 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 6330 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6331 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 6332 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6333 | return false; |
| 6334 | /* We must have at least as much range as the old ptr |
| 6335 | * did, so that any accesses which were safe before are |
| 6336 | * still safe. This is true even if old range < old off, |
| 6337 | * since someone could have accessed through (ptr - k), or |
| 6338 | * even done ptr -= k in a register, to get a safe access. |
| 6339 | */ |
| 6340 | if (rold->range > rcur->range) |
| 6341 | return false; |
| 6342 | /* If the offsets don't match, we can't trust our alignment; |
| 6343 | * nor can we be sure that we won't fall out of range. |
| 6344 | */ |
| 6345 | if (rold->off != rcur->off) |
| 6346 | return false; |
| 6347 | /* id relations must be preserved */ |
| 6348 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 6349 | return false; |
| 6350 | /* new val must satisfy old val knowledge */ |
| 6351 | return range_within(rold, rcur) && |
| 6352 | tnum_in(rold->var_off, rcur->var_off); |
| 6353 | case PTR_TO_CTX: |
| 6354 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6355 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 6356 | case PTR_TO_FLOW_KEYS: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6357 | case PTR_TO_SOCKET: |
| 6358 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 6359 | case PTR_TO_SOCK_COMMON: |
| 6360 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 6361 | case PTR_TO_TCP_SOCK: |
| 6362 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 6363 | case PTR_TO_XDP_SOCK: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6364 | /* Only valid matches are exact, which memcmp() above |
| 6365 | * would have accepted |
| 6366 | */ |
| 6367 | default: |
| 6368 | /* Don't know what's going on, just say it's not safe */ |
| 6369 | return false; |
| 6370 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6371 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6372 | /* Shouldn't get here; if we do, say it's not safe */ |
| 6373 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6374 | return false; |
| 6375 | } |
| 6376 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6377 | static bool stacksafe(struct bpf_func_state *old, |
| 6378 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6379 | struct idpair *idmap) |
| 6380 | { |
| 6381 | int i, spi; |
| 6382 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6383 | /* walk slots of the explored stack and ignore any additional |
| 6384 | * slots in the current stack, since explored(safe) state |
| 6385 | * didn't use them |
| 6386 | */ |
| 6387 | for (i = 0; i < old->allocated_stack; i++) { |
| 6388 | spi = i / BPF_REG_SIZE; |
| 6389 | |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 6390 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
| 6391 | i += BPF_REG_SIZE - 1; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 6392 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 6393 | continue; |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 6394 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 6395 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6396 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 6397 | continue; |
Alexei Starovoitov | 19e2dbb | 2018-12-13 11:42:33 -0800 | [diff] [blame] | 6398 | |
| 6399 | /* explored stack has more populated slots than current stack |
| 6400 | * and these slots were used |
| 6401 | */ |
| 6402 | if (i >= cur->allocated_stack) |
| 6403 | return false; |
| 6404 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 6405 | /* if old state was safe with misc data in the stack |
| 6406 | * it will be safe with zero-initialized stack. |
| 6407 | * The opposite is not true |
| 6408 | */ |
| 6409 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 6410 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 6411 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6412 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 6413 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 6414 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 6415 | * this stack slot, but current has has STACK_MISC -> |
| 6416 | * this verifier states are not equivalent, |
| 6417 | * return false to continue verification of this path |
| 6418 | */ |
| 6419 | return false; |
| 6420 | if (i % BPF_REG_SIZE) |
| 6421 | continue; |
| 6422 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 6423 | continue; |
| 6424 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 6425 | &cur->stack[spi].spilled_ptr, |
| 6426 | idmap)) |
| 6427 | /* when explored and current stack slot are both storing |
| 6428 | * spilled registers, check that stored pointers types |
| 6429 | * are the same as well. |
| 6430 | * Ex: explored safe path could have stored |
| 6431 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 6432 | * but current path has stored: |
| 6433 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 6434 | * such verifier states are not equivalent. |
| 6435 | * return false to continue verification of this path |
| 6436 | */ |
| 6437 | return false; |
| 6438 | } |
| 6439 | return true; |
| 6440 | } |
| 6441 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6442 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 6443 | { |
| 6444 | if (old->acquired_refs != cur->acquired_refs) |
| 6445 | return false; |
| 6446 | return !memcmp(old->refs, cur->refs, |
| 6447 | sizeof(*old->refs) * old->acquired_refs); |
| 6448 | } |
| 6449 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6450 | /* compare two verifier states |
| 6451 | * |
| 6452 | * all states stored in state_list are known to be valid, since |
| 6453 | * verifier reached 'bpf_exit' instruction through them |
| 6454 | * |
| 6455 | * this function is called when verifier exploring different branches of |
| 6456 | * execution popped from the state stack. If it sees an old state that has |
| 6457 | * more strict register state and more strict stack state then this execution |
| 6458 | * branch doesn't need to be explored further, since verifier already |
| 6459 | * concluded that more strict state leads to valid finish. |
| 6460 | * |
| 6461 | * Therefore two states are equivalent if register state is more conservative |
| 6462 | * and explored stack state is more conservative than the current one. |
| 6463 | * Example: |
| 6464 | * explored current |
| 6465 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 6466 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 6467 | * |
| 6468 | * In other words if current stack state (one being explored) has more |
| 6469 | * valid slots than old one that already passed validation, it means |
| 6470 | * the verifier can stop exploring and conclude that current state is valid too |
| 6471 | * |
| 6472 | * Similarly with registers. If explored state has register type as invalid |
| 6473 | * whereas register type in current state is meaningful, it means that |
| 6474 | * the current state will reach 'bpf_exit' instruction safely |
| 6475 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6476 | static bool func_states_equal(struct bpf_func_state *old, |
| 6477 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6478 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6479 | struct idpair *idmap; |
| 6480 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6481 | int i; |
| 6482 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6483 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 6484 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 6485 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 6486 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6487 | |
| 6488 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 6489 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6490 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6491 | } |
| 6492 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6493 | if (!stacksafe(old, cur, idmap)) |
| 6494 | goto out_free; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6495 | |
| 6496 | if (!refsafe(old, cur)) |
| 6497 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6498 | ret = true; |
| 6499 | out_free: |
| 6500 | kfree(idmap); |
| 6501 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6502 | } |
| 6503 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6504 | static bool states_equal(struct bpf_verifier_env *env, |
| 6505 | struct bpf_verifier_state *old, |
| 6506 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6507 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6508 | int i; |
| 6509 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6510 | if (old->curframe != cur->curframe) |
| 6511 | return false; |
| 6512 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6513 | /* Verification state from speculative execution simulation |
| 6514 | * must never prune a non-speculative execution one. |
| 6515 | */ |
| 6516 | if (old->speculative && !cur->speculative) |
| 6517 | return false; |
| 6518 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6519 | if (old->active_spin_lock != cur->active_spin_lock) |
| 6520 | return false; |
| 6521 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6522 | /* for states to be equal callsites have to be the same |
| 6523 | * and all frame states need to be equivalent |
| 6524 | */ |
| 6525 | for (i = 0; i <= old->curframe; i++) { |
| 6526 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 6527 | return false; |
| 6528 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 6529 | return false; |
| 6530 | } |
| 6531 | return true; |
| 6532 | } |
| 6533 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6534 | /* Return 0 if no propagation happened. Return negative error code if error |
| 6535 | * happened. Otherwise, return the propagated bit. |
| 6536 | */ |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6537 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
| 6538 | struct bpf_reg_state *reg, |
| 6539 | struct bpf_reg_state *parent_reg) |
| 6540 | { |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6541 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
| 6542 | u8 flag = reg->live & REG_LIVE_READ; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6543 | int err; |
| 6544 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6545 | /* When comes here, read flags of PARENT_REG or REG could be any of |
| 6546 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need |
| 6547 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. |
| 6548 | */ |
| 6549 | if (parent_flag == REG_LIVE_READ64 || |
| 6550 | /* Or if there is no read flag from REG. */ |
| 6551 | !flag || |
| 6552 | /* Or if the read flag from REG is the same as PARENT_REG. */ |
| 6553 | parent_flag == flag) |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6554 | return 0; |
| 6555 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6556 | err = mark_reg_read(env, reg, parent_reg, flag); |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6557 | if (err) |
| 6558 | return err; |
| 6559 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6560 | return flag; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6561 | } |
| 6562 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6563 | /* A write screens off any subsequent reads; but write marks come from the |
| 6564 | * straight-line code between a state and its parent. When we arrive at an |
| 6565 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 6566 | * code, so read marks in the state must propagate to the parent regardless |
| 6567 | * 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] | 6568 | * in mark_reg_read() is for. |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6569 | */ |
| 6570 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 6571 | const struct bpf_verifier_state *vstate, |
| 6572 | struct bpf_verifier_state *vparent) |
| 6573 | { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6574 | struct bpf_reg_state *state_reg, *parent_reg; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6575 | struct bpf_func_state *state, *parent; |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6576 | int i, frame, err = 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6577 | |
| 6578 | if (vparent->curframe != vstate->curframe) { |
| 6579 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 6580 | vparent->curframe, vstate->curframe); |
| 6581 | return -EFAULT; |
| 6582 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6583 | /* Propagate read liveness of registers... */ |
| 6584 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 6585 | for (frame = 0; frame <= vstate->curframe; frame++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6586 | parent = vparent->frame[frame]; |
| 6587 | state = vstate->frame[frame]; |
| 6588 | parent_reg = parent->regs; |
| 6589 | state_reg = state->regs; |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 6590 | /* We don't need to worry about FP liveness, it's read-only */ |
| 6591 | 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] | 6592 | err = propagate_liveness_reg(env, &state_reg[i], |
| 6593 | &parent_reg[i]); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6594 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6595 | return err; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6596 | if (err == REG_LIVE_READ64) |
| 6597 | mark_insn_zext(env, &parent_reg[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6598 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6599 | |
Jiong Wang | 1b04aee | 2019-04-12 22:59:34 +0100 | [diff] [blame] | 6600 | /* Propagate stack slots. */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6601 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 6602 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6603 | parent_reg = &parent->stack[i].spilled_ptr; |
| 6604 | state_reg = &state->stack[i].spilled_ptr; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6605 | err = propagate_liveness_reg(env, state_reg, |
| 6606 | parent_reg); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6607 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6608 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6609 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6610 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6611 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6612 | } |
| 6613 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6614 | 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] | 6615 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6616 | struct bpf_verifier_state_list *new_sl; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 6617 | struct bpf_verifier_state_list *sl, **pprev; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6618 | struct bpf_verifier_state *cur = env->cur_state, *new; |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 6619 | int i, j, err, states_cnt = 0; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6620 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 6621 | if (!env->insn_aux_data[insn_idx].prune_point) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6622 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 6623 | * be doing state search here |
| 6624 | */ |
| 6625 | return 0; |
| 6626 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 6627 | pprev = explored_state(env, insn_idx); |
| 6628 | sl = *pprev; |
| 6629 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 6630 | clean_live_states(env, insn_idx, cur); |
| 6631 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 6632 | while (sl) { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 6633 | states_cnt++; |
| 6634 | if (sl->state.insn_idx != insn_idx) |
| 6635 | goto next; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6636 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 6637 | sl->hit_cnt++; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6638 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6639 | * prune the search. |
| 6640 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 6641 | * If we have any write marks in env->cur_state, they |
| 6642 | * will prevent corresponding reads in the continuation |
| 6643 | * from reaching our parent (an explored_state). Our |
| 6644 | * own state will get the read marks recorded, but |
| 6645 | * they'll be immediately forgotten as we're pruning |
| 6646 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6647 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6648 | err = propagate_liveness(env, &sl->state, cur); |
| 6649 | if (err) |
| 6650 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6651 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6652 | } |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 6653 | sl->miss_cnt++; |
| 6654 | /* heuristic to determine whether this state is beneficial |
| 6655 | * to keep checking from state equivalence point of view. |
| 6656 | * Higher numbers increase max_states_per_insn and verification time, |
| 6657 | * but do not meaningfully decrease insn_processed. |
| 6658 | */ |
| 6659 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { |
| 6660 | /* the state is unlikely to be useful. Remove it to |
| 6661 | * speed up verification |
| 6662 | */ |
| 6663 | *pprev = sl->next; |
| 6664 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { |
| 6665 | free_verifier_state(&sl->state, false); |
| 6666 | kfree(sl); |
| 6667 | env->peak_states--; |
| 6668 | } else { |
| 6669 | /* cannot free this state, since parentage chain may |
| 6670 | * walk it later. Add it for free_list instead to |
| 6671 | * be freed at the end of verification |
| 6672 | */ |
| 6673 | sl->next = env->free_list; |
| 6674 | env->free_list = sl; |
| 6675 | } |
| 6676 | sl = *pprev; |
| 6677 | continue; |
| 6678 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 6679 | next: |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 6680 | pprev = &sl->next; |
| 6681 | sl = *pprev; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6682 | } |
| 6683 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6684 | if (env->max_states_per_insn < states_cnt) |
| 6685 | env->max_states_per_insn = states_cnt; |
| 6686 | |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 6687 | if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
| 6688 | return 0; |
| 6689 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6690 | /* there were no equivalent states, remember current one. |
| 6691 | * technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6692 | * but it will either reach outer most bpf_exit (which means it's safe) |
| 6693 | * or it will be rejected. Since there are no loops, we won't be |
| 6694 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
| 6695 | * again on the way to bpf_exit |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6696 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6697 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6698 | if (!new_sl) |
| 6699 | return -ENOMEM; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6700 | env->total_states++; |
| 6701 | env->peak_states++; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6702 | |
| 6703 | /* add new state to the head of linked list */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6704 | new = &new_sl->state; |
| 6705 | err = copy_verifier_state(new, cur); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 6706 | if (err) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6707 | free_verifier_state(new, false); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 6708 | kfree(new_sl); |
| 6709 | return err; |
| 6710 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 6711 | new->insn_idx = insn_idx; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 6712 | new_sl->next = *explored_state(env, insn_idx); |
| 6713 | *explored_state(env, insn_idx) = new_sl; |
Jakub Kicinski | 7640ead | 2018-12-12 16:29:07 -0800 | [diff] [blame] | 6714 | /* connect new state to parentage chain. Current frame needs all |
| 6715 | * registers connected. Only r6 - r9 of the callers are alive (pushed |
| 6716 | * to the stack implicitly by JITs) so in callers' frames connect just |
| 6717 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to |
| 6718 | * the state of the call instruction (with WRITTEN set), and r0 comes |
| 6719 | * from callee with its full parentage chain, anyway. |
| 6720 | */ |
| 6721 | for (j = 0; j <= cur->curframe; j++) |
| 6722 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) |
| 6723 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 6724 | /* clear write marks in current state: the writes we did are not writes |
| 6725 | * our child did, so they don't screen off its reads from us. |
| 6726 | * (There are no read marks in current state, because reads always mark |
| 6727 | * their parent and current state never has children yet. Only |
| 6728 | * explored_states can get read marks.) |
| 6729 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6730 | for (i = 0; i < BPF_REG_FP; i++) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6731 | cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE; |
| 6732 | |
| 6733 | /* all stack frames are accessible from callee, clear them all */ |
| 6734 | for (j = 0; j <= cur->curframe; j++) { |
| 6735 | struct bpf_func_state *frame = cur->frame[j]; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6736 | struct bpf_func_state *newframe = new->frame[j]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6737 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6738 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 6739 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6740 | frame->stack[i].spilled_ptr.parent = |
| 6741 | &newframe->stack[i].spilled_ptr; |
| 6742 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6743 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6744 | return 0; |
| 6745 | } |
| 6746 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6747 | /* Return true if it's OK to have the same insn return a different type. */ |
| 6748 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 6749 | { |
| 6750 | switch (type) { |
| 6751 | case PTR_TO_CTX: |
| 6752 | case PTR_TO_SOCKET: |
| 6753 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 6754 | case PTR_TO_SOCK_COMMON: |
| 6755 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 6756 | case PTR_TO_TCP_SOCK: |
| 6757 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 6758 | case PTR_TO_XDP_SOCK: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6759 | return false; |
| 6760 | default: |
| 6761 | return true; |
| 6762 | } |
| 6763 | } |
| 6764 | |
| 6765 | /* If an instruction was previously used with particular pointer types, then we |
| 6766 | * need to be careful to avoid cases such as the below, where it may be ok |
| 6767 | * for one branch accessing the pointer, but not ok for the other branch: |
| 6768 | * |
| 6769 | * R1 = sock_ptr |
| 6770 | * goto X; |
| 6771 | * ... |
| 6772 | * R1 = some_other_valid_ptr; |
| 6773 | * goto X; |
| 6774 | * ... |
| 6775 | * R2 = *(u32 *)(R1 + 0); |
| 6776 | */ |
| 6777 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 6778 | { |
| 6779 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 6780 | !reg_type_mismatch_ok(prev)); |
| 6781 | } |
| 6782 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6783 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6784 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6785 | struct bpf_verifier_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6786 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6787 | struct bpf_reg_state *regs; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6788 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6789 | bool do_print_state = false; |
| 6790 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 6791 | env->prev_linfo = NULL; |
| 6792 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6793 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 6794 | if (!state) |
| 6795 | return -ENOMEM; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6796 | state->curframe = 0; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6797 | state->speculative = false; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6798 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 6799 | if (!state->frame[0]) { |
| 6800 | kfree(state); |
| 6801 | return -ENOMEM; |
| 6802 | } |
| 6803 | env->cur_state = state; |
| 6804 | init_func_state(env, state->frame[0], |
| 6805 | BPF_MAIN_FUNC /* callsite */, |
| 6806 | 0 /* frameno */, |
| 6807 | 0 /* subprogno, zero == main subprog */); |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6808 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6809 | for (;;) { |
| 6810 | struct bpf_insn *insn; |
| 6811 | u8 class; |
| 6812 | int err; |
| 6813 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6814 | if (env->insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6815 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6816 | env->insn_idx, insn_cnt); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6817 | return -EFAULT; |
| 6818 | } |
| 6819 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6820 | insn = &insns[env->insn_idx]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6821 | class = BPF_CLASS(insn->code); |
| 6822 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6823 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6824 | verbose(env, |
| 6825 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6826 | env->insn_processed); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6827 | return -E2BIG; |
| 6828 | } |
| 6829 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6830 | err = is_state_visited(env, env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6831 | if (err < 0) |
| 6832 | return err; |
| 6833 | if (err == 1) { |
| 6834 | /* found equivalent state, can prune the search */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6835 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6836 | if (do_print_state) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6837 | verbose(env, "\nfrom %d to %d%s: safe\n", |
| 6838 | env->prev_insn_idx, env->insn_idx, |
| 6839 | env->cur_state->speculative ? |
| 6840 | " (speculative execution)" : ""); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6841 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6842 | verbose(env, "%d: safe\n", env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6843 | } |
| 6844 | goto process_bpf_exit; |
| 6845 | } |
| 6846 | |
Alexei Starovoitov | c349480 | 2018-12-03 22:46:04 -0800 | [diff] [blame] | 6847 | if (signal_pending(current)) |
| 6848 | return -EAGAIN; |
| 6849 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 6850 | if (need_resched()) |
| 6851 | cond_resched(); |
| 6852 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6853 | if (env->log.level & BPF_LOG_LEVEL2 || |
| 6854 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { |
| 6855 | if (env->log.level & BPF_LOG_LEVEL2) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6856 | verbose(env, "%d:", env->insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 6857 | else |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6858 | verbose(env, "\nfrom %d to %d%s:", |
| 6859 | env->prev_insn_idx, env->insn_idx, |
| 6860 | env->cur_state->speculative ? |
| 6861 | " (speculative execution)" : ""); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6862 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6863 | do_print_state = false; |
| 6864 | } |
| 6865 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6866 | if (env->log.level & BPF_LOG_LEVEL) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6867 | const struct bpf_insn_cbs cbs = { |
| 6868 | .cb_print = verbose, |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 6869 | .private_data = env, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6870 | }; |
| 6871 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6872 | verbose_linfo(env, env->insn_idx, "; "); |
| 6873 | verbose(env, "%d: ", env->insn_idx); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 6874 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6875 | } |
| 6876 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 6877 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6878 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
| 6879 | env->prev_insn_idx); |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 6880 | if (err) |
| 6881 | return err; |
| 6882 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 6883 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6884 | regs = cur_regs(env); |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6885 | env->insn_aux_data[env->insn_idx].seen = true; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6886 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6887 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6888 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6889 | if (err) |
| 6890 | return err; |
| 6891 | |
| 6892 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6893 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6894 | |
| 6895 | /* check for reserved fields is already done */ |
| 6896 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6897 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6898 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6899 | if (err) |
| 6900 | return err; |
| 6901 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6902 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6903 | if (err) |
| 6904 | return err; |
| 6905 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 6906 | src_reg_type = regs[insn->src_reg].type; |
| 6907 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6908 | /* check that memory (src_reg + off) is readable, |
| 6909 | * the state of dst_reg will be updated by this func |
| 6910 | */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6911 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
| 6912 | insn->off, BPF_SIZE(insn->code), |
| 6913 | BPF_READ, insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6914 | if (err) |
| 6915 | return err; |
| 6916 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6917 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6918 | |
| 6919 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6920 | /* saw a valid insn |
| 6921 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6922 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6923 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6924 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6925 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6926 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6927 | /* ABuser program is trying to use the same insn |
| 6928 | * dst_reg = *(u32*) (src_reg + off) |
| 6929 | * with different pointer types: |
| 6930 | * src_reg == ctx in one branch and |
| 6931 | * src_reg == stack|map in some other branch. |
| 6932 | * Reject it. |
| 6933 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6934 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6935 | return -EINVAL; |
| 6936 | } |
| 6937 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6938 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6939 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6940 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6941 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6942 | err = check_xadd(env, env->insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6943 | if (err) |
| 6944 | return err; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6945 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6946 | continue; |
| 6947 | } |
| 6948 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6949 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6950 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6951 | if (err) |
| 6952 | return err; |
| 6953 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6954 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6955 | if (err) |
| 6956 | return err; |
| 6957 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6958 | dst_reg_type = regs[insn->dst_reg].type; |
| 6959 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6960 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6961 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 6962 | insn->off, BPF_SIZE(insn->code), |
| 6963 | BPF_WRITE, insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6964 | if (err) |
| 6965 | return err; |
| 6966 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6967 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6968 | |
| 6969 | if (*prev_dst_type == NOT_INIT) { |
| 6970 | *prev_dst_type = dst_reg_type; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6971 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6972 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6973 | return -EINVAL; |
| 6974 | } |
| 6975 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6976 | } else if (class == BPF_ST) { |
| 6977 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 6978 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6979 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6980 | return -EINVAL; |
| 6981 | } |
| 6982 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6983 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6984 | if (err) |
| 6985 | return err; |
| 6986 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 6987 | if (is_ctx_reg(env, insn->dst_reg)) { |
Joe Stringer | 9d2be44 | 2018-10-02 13:35:31 -0700 | [diff] [blame] | 6988 | 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] | 6989 | insn->dst_reg, |
| 6990 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 6991 | return -EACCES; |
| 6992 | } |
| 6993 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6994 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6995 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 6996 | insn->off, BPF_SIZE(insn->code), |
| 6997 | BPF_WRITE, -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6998 | if (err) |
| 6999 | return err; |
| 7000 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7001 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7002 | u8 opcode = BPF_OP(insn->code); |
| 7003 | |
| 7004 | if (opcode == BPF_CALL) { |
| 7005 | if (BPF_SRC(insn->code) != BPF_K || |
| 7006 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7007 | (insn->src_reg != BPF_REG_0 && |
| 7008 | insn->src_reg != BPF_PSEUDO_CALL) || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7009 | insn->dst_reg != BPF_REG_0 || |
| 7010 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7011 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7012 | return -EINVAL; |
| 7013 | } |
| 7014 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 7015 | if (env->cur_state->active_spin_lock && |
| 7016 | (insn->src_reg == BPF_PSEUDO_CALL || |
| 7017 | insn->imm != BPF_FUNC_spin_unlock)) { |
| 7018 | verbose(env, "function calls are not allowed while holding a lock\n"); |
| 7019 | return -EINVAL; |
| 7020 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7021 | if (insn->src_reg == BPF_PSEUDO_CALL) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7022 | err = check_func_call(env, insn, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7023 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7024 | err = check_helper_call(env, insn->imm, env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7025 | if (err) |
| 7026 | return err; |
| 7027 | |
| 7028 | } else if (opcode == BPF_JA) { |
| 7029 | if (BPF_SRC(insn->code) != BPF_K || |
| 7030 | insn->imm != 0 || |
| 7031 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7032 | insn->dst_reg != BPF_REG_0 || |
| 7033 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7034 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7035 | return -EINVAL; |
| 7036 | } |
| 7037 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7038 | env->insn_idx += insn->off + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7039 | continue; |
| 7040 | |
| 7041 | } else if (opcode == BPF_EXIT) { |
| 7042 | if (BPF_SRC(insn->code) != BPF_K || |
| 7043 | insn->imm != 0 || |
| 7044 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7045 | insn->dst_reg != BPF_REG_0 || |
| 7046 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7047 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7048 | return -EINVAL; |
| 7049 | } |
| 7050 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 7051 | if (env->cur_state->active_spin_lock) { |
| 7052 | verbose(env, "bpf_spin_unlock is missing\n"); |
| 7053 | return -EINVAL; |
| 7054 | } |
| 7055 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7056 | if (state->curframe) { |
| 7057 | /* exit from nested function */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7058 | env->prev_insn_idx = env->insn_idx; |
| 7059 | err = prepare_func_exit(env, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7060 | if (err) |
| 7061 | return err; |
| 7062 | do_print_state = true; |
| 7063 | continue; |
| 7064 | } |
| 7065 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 7066 | err = check_reference_leak(env); |
| 7067 | if (err) |
| 7068 | return err; |
| 7069 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7070 | /* eBPF calling convetion is such that R0 is used |
| 7071 | * to return the value from eBPF program. |
| 7072 | * Make sure that it's readable at this time |
| 7073 | * of bpf_exit, which means that program wrote |
| 7074 | * something into it earlier |
| 7075 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7076 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7077 | if (err) |
| 7078 | return err; |
| 7079 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7080 | if (is_pointer_value(env, BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7081 | verbose(env, "R0 leaks addr as return value\n"); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7082 | return -EACCES; |
| 7083 | } |
| 7084 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7085 | err = check_return_code(env); |
| 7086 | if (err) |
| 7087 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7088 | process_bpf_exit: |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7089 | err = pop_stack(env, &env->prev_insn_idx, |
| 7090 | &env->insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7091 | if (err < 0) { |
| 7092 | if (err != -ENOENT) |
| 7093 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7094 | break; |
| 7095 | } else { |
| 7096 | do_print_state = true; |
| 7097 | continue; |
| 7098 | } |
| 7099 | } else { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7100 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7101 | if (err) |
| 7102 | return err; |
| 7103 | } |
| 7104 | } else if (class == BPF_LD) { |
| 7105 | u8 mode = BPF_MODE(insn->code); |
| 7106 | |
| 7107 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7108 | err = check_ld_abs(env, insn); |
| 7109 | if (err) |
| 7110 | return err; |
| 7111 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7112 | } else if (mode == BPF_IMM) { |
| 7113 | err = check_ld_imm(env, insn); |
| 7114 | if (err) |
| 7115 | return err; |
| 7116 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7117 | env->insn_idx++; |
| 7118 | env->insn_aux_data[env->insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7119 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7120 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7121 | return -EINVAL; |
| 7122 | } |
| 7123 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7124 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7125 | return -EINVAL; |
| 7126 | } |
| 7127 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7128 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7129 | } |
| 7130 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 7131 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7132 | return 0; |
| 7133 | } |
| 7134 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 7135 | static int check_map_prealloc(struct bpf_map *map) |
| 7136 | { |
| 7137 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 7138 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 7139 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 7140 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 7141 | } |
| 7142 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 7143 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
| 7144 | { |
| 7145 | switch (type) { |
| 7146 | case BPF_PROG_TYPE_KPROBE: |
| 7147 | case BPF_PROG_TYPE_TRACEPOINT: |
| 7148 | case BPF_PROG_TYPE_PERF_EVENT: |
| 7149 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 7150 | return true; |
| 7151 | default: |
| 7152 | return false; |
| 7153 | } |
| 7154 | } |
| 7155 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7156 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 7157 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 7158 | struct bpf_prog *prog) |
| 7159 | |
| 7160 | { |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 7161 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 7162 | * preallocated hash maps, since doing memory allocation |
| 7163 | * in overflow_handler can crash depending on where nmi got |
| 7164 | * triggered. |
| 7165 | */ |
| 7166 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 7167 | if (!check_map_prealloc(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7168 | 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] | 7169 | return -EINVAL; |
| 7170 | } |
| 7171 | if (map->inner_map_meta && |
| 7172 | !check_map_prealloc(map->inner_map_meta)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7173 | 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] | 7174 | return -EINVAL; |
| 7175 | } |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 7176 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 7177 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 7178 | if ((is_tracing_prog_type(prog->type) || |
| 7179 | prog->type == BPF_PROG_TYPE_SOCKET_FILTER) && |
| 7180 | map_value_has_spin_lock(map)) { |
| 7181 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); |
| 7182 | return -EINVAL; |
| 7183 | } |
| 7184 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 7185 | 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] | 7186 | !bpf_offload_prog_map_match(prog, map)) { |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 7187 | verbose(env, "offload device mismatch between prog and map\n"); |
| 7188 | return -EINVAL; |
| 7189 | } |
| 7190 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 7191 | return 0; |
| 7192 | } |
| 7193 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 7194 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 7195 | { |
| 7196 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 7197 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 7198 | } |
| 7199 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7200 | /* look for pseudo eBPF instructions that access map FDs and |
| 7201 | * replace them with actual map pointers |
| 7202 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7203 | 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] | 7204 | { |
| 7205 | struct bpf_insn *insn = env->prog->insnsi; |
| 7206 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 7207 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7208 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 7209 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 7210 | if (err) |
| 7211 | return err; |
| 7212 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7213 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7214 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7215 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7216 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7217 | return -EINVAL; |
| 7218 | } |
| 7219 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7220 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 7221 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 7222 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7223 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7224 | return -EINVAL; |
| 7225 | } |
| 7226 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7227 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7228 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7229 | struct bpf_map *map; |
| 7230 | struct fd f; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7231 | u64 addr; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7232 | |
| 7233 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 7234 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 7235 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7236 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7237 | return -EINVAL; |
| 7238 | } |
| 7239 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7240 | if (insn[0].src_reg == 0) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7241 | /* valid generic load 64-bit imm */ |
| 7242 | goto next_insn; |
| 7243 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7244 | /* In final convert_pseudo_ld_imm64() step, this is |
| 7245 | * converted into regular 64-bit imm load insn. |
| 7246 | */ |
| 7247 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && |
| 7248 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || |
| 7249 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && |
| 7250 | insn[1].imm != 0)) { |
| 7251 | verbose(env, |
| 7252 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7253 | return -EINVAL; |
| 7254 | } |
| 7255 | |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 7256 | f = fdget(insn[0].imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 7257 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7258 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7259 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 7260 | insn[0].imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7261 | return PTR_ERR(map); |
| 7262 | } |
| 7263 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7264 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 7265 | if (err) { |
| 7266 | fdput(f); |
| 7267 | return err; |
| 7268 | } |
| 7269 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7270 | aux = &env->insn_aux_data[i]; |
| 7271 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 7272 | addr = (unsigned long)map; |
| 7273 | } else { |
| 7274 | u32 off = insn[1].imm; |
| 7275 | |
| 7276 | if (off >= BPF_MAX_VAR_OFF) { |
| 7277 | verbose(env, "direct value offset of %u is not allowed\n", off); |
| 7278 | fdput(f); |
| 7279 | return -EINVAL; |
| 7280 | } |
| 7281 | |
| 7282 | if (!map->ops->map_direct_value_addr) { |
| 7283 | verbose(env, "no direct value access support for this map type\n"); |
| 7284 | fdput(f); |
| 7285 | return -EINVAL; |
| 7286 | } |
| 7287 | |
| 7288 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 7289 | if (err) { |
| 7290 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", |
| 7291 | map->value_size, off); |
| 7292 | fdput(f); |
| 7293 | return err; |
| 7294 | } |
| 7295 | |
| 7296 | aux->map_off = off; |
| 7297 | addr += off; |
| 7298 | } |
| 7299 | |
| 7300 | insn[0].imm = (u32)addr; |
| 7301 | insn[1].imm = addr >> 32; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7302 | |
| 7303 | /* check whether we recorded this map already */ |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7304 | for (j = 0; j < env->used_map_cnt; j++) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7305 | if (env->used_maps[j] == map) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7306 | aux->map_index = j; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7307 | fdput(f); |
| 7308 | goto next_insn; |
| 7309 | } |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7310 | } |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7311 | |
| 7312 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 7313 | fdput(f); |
| 7314 | return -E2BIG; |
| 7315 | } |
| 7316 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7317 | /* hold the map. If the program is rejected by verifier, |
| 7318 | * the map will be released by release_maps() or it |
| 7319 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 7320 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7321 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 7322 | map = bpf_map_inc(map, false); |
| 7323 | if (IS_ERR(map)) { |
| 7324 | fdput(f); |
| 7325 | return PTR_ERR(map); |
| 7326 | } |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7327 | |
| 7328 | aux->map_index = env->used_map_cnt; |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 7329 | env->used_maps[env->used_map_cnt++] = map; |
| 7330 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 7331 | if (bpf_map_is_cgroup_storage(map) && |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 7332 | bpf_cgroup_storage_assign(env->prog, map)) { |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 7333 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 7334 | fdput(f); |
| 7335 | return -EBUSY; |
| 7336 | } |
| 7337 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7338 | fdput(f); |
| 7339 | next_insn: |
| 7340 | insn++; |
| 7341 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 7342 | continue; |
| 7343 | } |
| 7344 | |
| 7345 | /* Basic sanity check before we invest more work here. */ |
| 7346 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 7347 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 7348 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7349 | } |
| 7350 | } |
| 7351 | |
| 7352 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 7353 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 7354 | * These pointers will be used later by verifier to validate map access. |
| 7355 | */ |
| 7356 | return 0; |
| 7357 | } |
| 7358 | |
| 7359 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7360 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7361 | { |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 7362 | enum bpf_cgroup_storage_type stype; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7363 | int i; |
| 7364 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 7365 | for_each_cgroup_storage_type(stype) { |
| 7366 | if (!env->prog->aux->cgroup_storage[stype]) |
| 7367 | continue; |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 7368 | bpf_cgroup_storage_release(env->prog, |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 7369 | env->prog->aux->cgroup_storage[stype]); |
| 7370 | } |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 7371 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7372 | for (i = 0; i < env->used_map_cnt; i++) |
| 7373 | bpf_map_put(env->used_maps[i]); |
| 7374 | } |
| 7375 | |
| 7376 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7377 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7378 | { |
| 7379 | struct bpf_insn *insn = env->prog->insnsi; |
| 7380 | int insn_cnt = env->prog->len; |
| 7381 | int i; |
| 7382 | |
| 7383 | for (i = 0; i < insn_cnt; i++, insn++) |
| 7384 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 7385 | insn->src_reg = 0; |
| 7386 | } |
| 7387 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7388 | /* single env->prog->insni[off] instruction was replaced with the range |
| 7389 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 7390 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 7391 | */ |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 7392 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, |
| 7393 | struct bpf_prog *new_prog, u32 off, u32 cnt) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7394 | { |
| 7395 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 7396 | struct bpf_insn *insn = new_prog->insnsi; |
| 7397 | u32 prog_len; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7398 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7399 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 7400 | /* aux info at OFF always needs adjustment, no matter fast path |
| 7401 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the |
| 7402 | * original insn at old prog. |
| 7403 | */ |
| 7404 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); |
| 7405 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7406 | if (cnt == 1) |
| 7407 | return 0; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 7408 | prog_len = new_prog->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 7409 | new_data = vzalloc(array_size(prog_len, |
| 7410 | sizeof(struct bpf_insn_aux_data))); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7411 | if (!new_data) |
| 7412 | return -ENOMEM; |
| 7413 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 7414 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 7415 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 7416 | for (i = off; i < off + cnt - 1; i++) { |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7417 | new_data[i].seen = true; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 7418 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
| 7419 | } |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7420 | env->insn_aux_data = new_data; |
| 7421 | vfree(old_data); |
| 7422 | return 0; |
| 7423 | } |
| 7424 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7425 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 7426 | { |
| 7427 | int i; |
| 7428 | |
| 7429 | if (len == 1) |
| 7430 | return; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 7431 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 7432 | for (i = 0; i <= env->subprog_cnt; i++) { |
Edward Cree | afd5942 | 2018-11-16 12:00:07 +0000 | [diff] [blame] | 7433 | if (env->subprog_info[i].start <= off) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7434 | continue; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 7435 | env->subprog_info[i].start += len - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7436 | } |
| 7437 | } |
| 7438 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7439 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 7440 | const struct bpf_insn *patch, u32 len) |
| 7441 | { |
| 7442 | struct bpf_prog *new_prog; |
| 7443 | |
| 7444 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 7445 | if (IS_ERR(new_prog)) { |
| 7446 | if (PTR_ERR(new_prog) == -ERANGE) |
| 7447 | verbose(env, |
| 7448 | "insn %d cannot be patched due to 16-bit range\n", |
| 7449 | env->insn_aux_data[off].orig_idx); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7450 | return NULL; |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 7451 | } |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 7452 | if (adjust_insn_aux_data(env, new_prog, off, len)) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7453 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7454 | adjust_subprog_starts(env, off, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7455 | return new_prog; |
| 7456 | } |
| 7457 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 7458 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
| 7459 | u32 off, u32 cnt) |
| 7460 | { |
| 7461 | int i, j; |
| 7462 | |
| 7463 | /* find first prog starting at or after off (first to remove) */ |
| 7464 | for (i = 0; i < env->subprog_cnt; i++) |
| 7465 | if (env->subprog_info[i].start >= off) |
| 7466 | break; |
| 7467 | /* find first prog starting at or after off + cnt (first to stay) */ |
| 7468 | for (j = i; j < env->subprog_cnt; j++) |
| 7469 | if (env->subprog_info[j].start >= off + cnt) |
| 7470 | break; |
| 7471 | /* if j doesn't start exactly at off + cnt, we are just removing |
| 7472 | * the front of previous prog |
| 7473 | */ |
| 7474 | if (env->subprog_info[j].start != off + cnt) |
| 7475 | j--; |
| 7476 | |
| 7477 | if (j > i) { |
| 7478 | struct bpf_prog_aux *aux = env->prog->aux; |
| 7479 | int move; |
| 7480 | |
| 7481 | /* move fake 'exit' subprog as well */ |
| 7482 | move = env->subprog_cnt + 1 - j; |
| 7483 | |
| 7484 | memmove(env->subprog_info + i, |
| 7485 | env->subprog_info + j, |
| 7486 | sizeof(*env->subprog_info) * move); |
| 7487 | env->subprog_cnt -= j - i; |
| 7488 | |
| 7489 | /* remove func_info */ |
| 7490 | if (aux->func_info) { |
| 7491 | move = aux->func_info_cnt - j; |
| 7492 | |
| 7493 | memmove(aux->func_info + i, |
| 7494 | aux->func_info + j, |
| 7495 | sizeof(*aux->func_info) * move); |
| 7496 | aux->func_info_cnt -= j - i; |
| 7497 | /* func_info->insn_off is set after all code rewrites, |
| 7498 | * in adjust_btf_func() - no need to adjust |
| 7499 | */ |
| 7500 | } |
| 7501 | } else { |
| 7502 | /* convert i from "first prog to remove" to "first to adjust" */ |
| 7503 | if (env->subprog_info[i].start == off) |
| 7504 | i++; |
| 7505 | } |
| 7506 | |
| 7507 | /* update fake 'exit' subprog as well */ |
| 7508 | for (; i <= env->subprog_cnt; i++) |
| 7509 | env->subprog_info[i].start -= cnt; |
| 7510 | |
| 7511 | return 0; |
| 7512 | } |
| 7513 | |
| 7514 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, |
| 7515 | u32 cnt) |
| 7516 | { |
| 7517 | struct bpf_prog *prog = env->prog; |
| 7518 | u32 i, l_off, l_cnt, nr_linfo; |
| 7519 | struct bpf_line_info *linfo; |
| 7520 | |
| 7521 | nr_linfo = prog->aux->nr_linfo; |
| 7522 | if (!nr_linfo) |
| 7523 | return 0; |
| 7524 | |
| 7525 | linfo = prog->aux->linfo; |
| 7526 | |
| 7527 | /* find first line info to remove, count lines to be removed */ |
| 7528 | for (i = 0; i < nr_linfo; i++) |
| 7529 | if (linfo[i].insn_off >= off) |
| 7530 | break; |
| 7531 | |
| 7532 | l_off = i; |
| 7533 | l_cnt = 0; |
| 7534 | for (; i < nr_linfo; i++) |
| 7535 | if (linfo[i].insn_off < off + cnt) |
| 7536 | l_cnt++; |
| 7537 | else |
| 7538 | break; |
| 7539 | |
| 7540 | /* First live insn doesn't match first live linfo, it needs to "inherit" |
| 7541 | * last removed linfo. prog is already modified, so prog->len == off |
| 7542 | * means no live instructions after (tail of the program was removed). |
| 7543 | */ |
| 7544 | if (prog->len != off && l_cnt && |
| 7545 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { |
| 7546 | l_cnt--; |
| 7547 | linfo[--i].insn_off = off + cnt; |
| 7548 | } |
| 7549 | |
| 7550 | /* remove the line info which refer to the removed instructions */ |
| 7551 | if (l_cnt) { |
| 7552 | memmove(linfo + l_off, linfo + i, |
| 7553 | sizeof(*linfo) * (nr_linfo - i)); |
| 7554 | |
| 7555 | prog->aux->nr_linfo -= l_cnt; |
| 7556 | nr_linfo = prog->aux->nr_linfo; |
| 7557 | } |
| 7558 | |
| 7559 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ |
| 7560 | for (i = l_off; i < nr_linfo; i++) |
| 7561 | linfo[i].insn_off -= cnt; |
| 7562 | |
| 7563 | /* fix up all subprogs (incl. 'exit') which start >= off */ |
| 7564 | for (i = 0; i <= env->subprog_cnt; i++) |
| 7565 | if (env->subprog_info[i].linfo_idx > l_off) { |
| 7566 | /* program may have started in the removed region but |
| 7567 | * may not be fully removed |
| 7568 | */ |
| 7569 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) |
| 7570 | env->subprog_info[i].linfo_idx -= l_cnt; |
| 7571 | else |
| 7572 | env->subprog_info[i].linfo_idx = l_off; |
| 7573 | } |
| 7574 | |
| 7575 | return 0; |
| 7576 | } |
| 7577 | |
| 7578 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) |
| 7579 | { |
| 7580 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 7581 | unsigned int orig_prog_len = env->prog->len; |
| 7582 | int err; |
| 7583 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 7584 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 7585 | bpf_prog_offload_remove_insns(env, off, cnt); |
| 7586 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 7587 | err = bpf_remove_insns(env->prog, off, cnt); |
| 7588 | if (err) |
| 7589 | return err; |
| 7590 | |
| 7591 | err = adjust_subprog_starts_after_remove(env, off, cnt); |
| 7592 | if (err) |
| 7593 | return err; |
| 7594 | |
| 7595 | err = bpf_adj_linfo_after_remove(env, off, cnt); |
| 7596 | if (err) |
| 7597 | return err; |
| 7598 | |
| 7599 | memmove(aux_data + off, aux_data + off + cnt, |
| 7600 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); |
| 7601 | |
| 7602 | return 0; |
| 7603 | } |
| 7604 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 7605 | /* The verifier does more data flow analysis than llvm and will not |
| 7606 | * explore branches that are dead at run time. Malicious programs can |
| 7607 | * have dead code too. Therefore replace all dead at-run-time code |
| 7608 | * with 'ja -1'. |
| 7609 | * |
| 7610 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 7611 | * program and through another bug we would manage to jump there, then |
| 7612 | * we'd execute beyond program memory otherwise. Returning exception |
| 7613 | * code also wouldn't work since we can have subprogs where the dead |
| 7614 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7615 | */ |
| 7616 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 7617 | { |
| 7618 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 7619 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7620 | struct bpf_insn *insn = env->prog->insnsi; |
| 7621 | const int insn_cnt = env->prog->len; |
| 7622 | int i; |
| 7623 | |
| 7624 | for (i = 0; i < insn_cnt; i++) { |
| 7625 | if (aux_data[i].seen) |
| 7626 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 7627 | memcpy(insn + i, &trap, sizeof(trap)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7628 | } |
| 7629 | } |
| 7630 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 7631 | static bool insn_is_cond_jump(u8 code) |
| 7632 | { |
| 7633 | u8 op; |
| 7634 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7635 | if (BPF_CLASS(code) == BPF_JMP32) |
| 7636 | return true; |
| 7637 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 7638 | if (BPF_CLASS(code) != BPF_JMP) |
| 7639 | return false; |
| 7640 | |
| 7641 | op = BPF_OP(code); |
| 7642 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; |
| 7643 | } |
| 7644 | |
| 7645 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) |
| 7646 | { |
| 7647 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 7648 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 7649 | struct bpf_insn *insn = env->prog->insnsi; |
| 7650 | const int insn_cnt = env->prog->len; |
| 7651 | int i; |
| 7652 | |
| 7653 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 7654 | if (!insn_is_cond_jump(insn->code)) |
| 7655 | continue; |
| 7656 | |
| 7657 | if (!aux_data[i + 1].seen) |
| 7658 | ja.off = insn->off; |
| 7659 | else if (!aux_data[i + 1 + insn->off].seen) |
| 7660 | ja.off = 0; |
| 7661 | else |
| 7662 | continue; |
| 7663 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 7664 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 7665 | bpf_prog_offload_replace_insn(env, i, &ja); |
| 7666 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 7667 | memcpy(insn, &ja, sizeof(ja)); |
| 7668 | } |
| 7669 | } |
| 7670 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 7671 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
| 7672 | { |
| 7673 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 7674 | int insn_cnt = env->prog->len; |
| 7675 | int i, err; |
| 7676 | |
| 7677 | for (i = 0; i < insn_cnt; i++) { |
| 7678 | int j; |
| 7679 | |
| 7680 | j = 0; |
| 7681 | while (i + j < insn_cnt && !aux_data[i + j].seen) |
| 7682 | j++; |
| 7683 | if (!j) |
| 7684 | continue; |
| 7685 | |
| 7686 | err = verifier_remove_insns(env, i, j); |
| 7687 | if (err) |
| 7688 | return err; |
| 7689 | insn_cnt = env->prog->len; |
| 7690 | } |
| 7691 | |
| 7692 | return 0; |
| 7693 | } |
| 7694 | |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 7695 | static int opt_remove_nops(struct bpf_verifier_env *env) |
| 7696 | { |
| 7697 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 7698 | struct bpf_insn *insn = env->prog->insnsi; |
| 7699 | int insn_cnt = env->prog->len; |
| 7700 | int i, err; |
| 7701 | |
| 7702 | for (i = 0; i < insn_cnt; i++) { |
| 7703 | if (memcmp(&insn[i], &ja, sizeof(ja))) |
| 7704 | continue; |
| 7705 | |
| 7706 | err = verifier_remove_insns(env, i, 1); |
| 7707 | if (err) |
| 7708 | return err; |
| 7709 | insn_cnt--; |
| 7710 | i--; |
| 7711 | } |
| 7712 | |
| 7713 | return 0; |
| 7714 | } |
| 7715 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 7716 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
| 7717 | const union bpf_attr *attr) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 7718 | { |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 7719 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 7720 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 7721 | int i, patch_len, delta = 0, len = env->prog->len; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 7722 | struct bpf_insn *insns = env->prog->insnsi; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 7723 | struct bpf_prog *new_prog; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 7724 | bool rnd_hi32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 7725 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 7726 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 7727 | zext_patch[1] = BPF_ZEXT_REG(0); |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 7728 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
| 7729 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); |
| 7730 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 7731 | for (i = 0; i < len; i++) { |
| 7732 | int adj_idx = i + delta; |
| 7733 | struct bpf_insn insn; |
| 7734 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 7735 | insn = insns[adj_idx]; |
| 7736 | if (!aux[adj_idx].zext_dst) { |
| 7737 | u8 code, class; |
| 7738 | u32 imm_rnd; |
| 7739 | |
| 7740 | if (!rnd_hi32) |
| 7741 | continue; |
| 7742 | |
| 7743 | code = insn.code; |
| 7744 | class = BPF_CLASS(code); |
| 7745 | if (insn_no_def(&insn)) |
| 7746 | continue; |
| 7747 | |
| 7748 | /* NOTE: arg "reg" (the fourth one) is only used for |
| 7749 | * BPF_STX which has been ruled out in above |
| 7750 | * check, it is safe to pass NULL here. |
| 7751 | */ |
| 7752 | if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) { |
| 7753 | if (class == BPF_LD && |
| 7754 | BPF_MODE(code) == BPF_IMM) |
| 7755 | i++; |
| 7756 | continue; |
| 7757 | } |
| 7758 | |
| 7759 | /* ctx load could be transformed into wider load. */ |
| 7760 | if (class == BPF_LDX && |
| 7761 | aux[adj_idx].ptr_type == PTR_TO_CTX) |
| 7762 | continue; |
| 7763 | |
| 7764 | imm_rnd = get_random_int(); |
| 7765 | rnd_hi32_patch[0] = insn; |
| 7766 | rnd_hi32_patch[1].imm = imm_rnd; |
| 7767 | rnd_hi32_patch[3].dst_reg = insn.dst_reg; |
| 7768 | patch = rnd_hi32_patch; |
| 7769 | patch_len = 4; |
| 7770 | goto apply_patch_buffer; |
| 7771 | } |
| 7772 | |
| 7773 | if (!bpf_jit_needs_zext()) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 7774 | continue; |
| 7775 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 7776 | zext_patch[0] = insn; |
| 7777 | zext_patch[1].dst_reg = insn.dst_reg; |
| 7778 | zext_patch[1].src_reg = insn.dst_reg; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 7779 | patch = zext_patch; |
| 7780 | patch_len = 2; |
| 7781 | apply_patch_buffer: |
| 7782 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 7783 | if (!new_prog) |
| 7784 | return -ENOMEM; |
| 7785 | env->prog = new_prog; |
| 7786 | insns = new_prog->insnsi; |
| 7787 | aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 7788 | delta += patch_len - 1; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 7789 | } |
| 7790 | |
| 7791 | return 0; |
| 7792 | } |
| 7793 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7794 | /* convert load instructions that access fields of a context type into a |
| 7795 | * sequence of instructions that access fields of the underlying structure: |
| 7796 | * struct __sk_buff -> struct sk_buff |
| 7797 | * struct bpf_sock_ops -> struct sock |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7798 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7799 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7800 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 7801 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7802 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7803 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7804 | struct bpf_insn insn_buf[16], *insn; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7805 | u32 target_size, size_default, off; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7806 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7807 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7808 | bool is_narrower_load; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7809 | |
Daniel Borkmann | b09928b | 2018-10-24 22:05:49 +0200 | [diff] [blame] | 7810 | if (ops->gen_prologue || env->seen_direct_write) { |
| 7811 | if (!ops->gen_prologue) { |
| 7812 | verbose(env, "bpf verifier is misconfigured\n"); |
| 7813 | return -EINVAL; |
| 7814 | } |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7815 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 7816 | env->prog); |
| 7817 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7818 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7819 | return -EINVAL; |
| 7820 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7821 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7822 | if (!new_prog) |
| 7823 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7824 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7825 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7826 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7827 | } |
| 7828 | } |
| 7829 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7830 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7831 | return 0; |
| 7832 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7833 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7834 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7835 | for (i = 0; i < insn_cnt; i++, insn++) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7836 | bpf_convert_ctx_access_t convert_ctx_access; |
| 7837 | |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 7838 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 7839 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 7840 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 7841 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7842 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 7843 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 7844 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 7845 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 7846 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7847 | type = BPF_WRITE; |
| 7848 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7849 | continue; |
| 7850 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 7851 | if (type == BPF_WRITE && |
| 7852 | env->insn_aux_data[i + delta].sanitize_stack_off) { |
| 7853 | struct bpf_insn patch[] = { |
| 7854 | /* Sanitize suspicious stack slot with zero. |
| 7855 | * There are no memory dependencies for this store, |
| 7856 | * since it's only using frame pointer and immediate |
| 7857 | * constant of zero |
| 7858 | */ |
| 7859 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, |
| 7860 | env->insn_aux_data[i + delta].sanitize_stack_off, |
| 7861 | 0), |
| 7862 | /* the original STX instruction will immediately |
| 7863 | * overwrite the same stack slot with appropriate value |
| 7864 | */ |
| 7865 | *insn, |
| 7866 | }; |
| 7867 | |
| 7868 | cnt = ARRAY_SIZE(patch); |
| 7869 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 7870 | if (!new_prog) |
| 7871 | return -ENOMEM; |
| 7872 | |
| 7873 | delta += cnt - 1; |
| 7874 | env->prog = new_prog; |
| 7875 | insn = new_prog->insnsi + i + delta; |
| 7876 | continue; |
| 7877 | } |
| 7878 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7879 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 7880 | case PTR_TO_CTX: |
| 7881 | if (!ops->convert_ctx_access) |
| 7882 | continue; |
| 7883 | convert_ctx_access = ops->convert_ctx_access; |
| 7884 | break; |
| 7885 | case PTR_TO_SOCKET: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 7886 | case PTR_TO_SOCK_COMMON: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7887 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 7888 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 7889 | case PTR_TO_TCP_SOCK: |
| 7890 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; |
| 7891 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 7892 | case PTR_TO_XDP_SOCK: |
| 7893 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; |
| 7894 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7895 | default: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7896 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7897 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7898 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7899 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7900 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7901 | |
| 7902 | /* If the read access is a narrower load of the field, |
| 7903 | * convert to a 4/8-byte load, to minimum program type specific |
| 7904 | * convert_ctx_access changes. If conversion is successful, |
| 7905 | * we will apply proper mask to the result. |
| 7906 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7907 | is_narrower_load = size < ctx_field_size; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7908 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
| 7909 | off = insn->off; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7910 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7911 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7912 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7913 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7914 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7915 | return -EINVAL; |
| 7916 | } |
| 7917 | |
| 7918 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7919 | if (ctx_field_size == 4) |
| 7920 | size_code = BPF_W; |
| 7921 | else if (ctx_field_size == 8) |
| 7922 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7923 | |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 7924 | insn->off = off & ~(size_default - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7925 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 7926 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7927 | |
| 7928 | target_size = 0; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7929 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 7930 | &target_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7931 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 7932 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7933 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7934 | return -EINVAL; |
| 7935 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7936 | |
| 7937 | if (is_narrower_load && size < target_size) { |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7938 | u8 shift = (off & (size_default - 1)) * 8; |
| 7939 | |
| 7940 | if (ctx_field_size <= 4) { |
| 7941 | if (shift) |
| 7942 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, |
| 7943 | insn->dst_reg, |
| 7944 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7945 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7946 | (1 << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7947 | } else { |
| 7948 | if (shift) |
| 7949 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, |
| 7950 | insn->dst_reg, |
| 7951 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7952 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Krzesimir Nowak | e2f7fc0 | 2019-05-08 18:08:58 +0200 | [diff] [blame] | 7953 | (1ULL << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7954 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7955 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7956 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7957 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7958 | if (!new_prog) |
| 7959 | return -ENOMEM; |
| 7960 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7961 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7962 | |
| 7963 | /* keep walking new program and skip insns we just inserted */ |
| 7964 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7965 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7966 | } |
| 7967 | |
| 7968 | return 0; |
| 7969 | } |
| 7970 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7971 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 7972 | { |
| 7973 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 7974 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7975 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7976 | void *old_bpf_func; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 7977 | int err; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7978 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7979 | if (env->subprog_cnt <= 1) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7980 | return 0; |
| 7981 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7982 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7983 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 7984 | insn->src_reg != BPF_PSEUDO_CALL) |
| 7985 | continue; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 7986 | /* Upon error here we cannot fall back to interpreter but |
| 7987 | * need a hard reject of the program. Thus -EFAULT is |
| 7988 | * propagated in any case. |
| 7989 | */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7990 | subprog = find_subprog(env, i + insn->imm + 1); |
| 7991 | if (subprog < 0) { |
| 7992 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 7993 | i + insn->imm + 1); |
| 7994 | return -EFAULT; |
| 7995 | } |
| 7996 | /* temporarily remember subprog id inside insn instead of |
| 7997 | * aux_data, since next loop will split up all insns into funcs |
| 7998 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7999 | insn->off = subprog; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8000 | /* remember original imm in case JIT fails and fallback |
| 8001 | * to interpreter will be needed |
| 8002 | */ |
| 8003 | env->insn_aux_data[i].call_imm = insn->imm; |
| 8004 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 8005 | insn->imm = 1; |
| 8006 | } |
| 8007 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8008 | err = bpf_prog_alloc_jited_linfo(prog); |
| 8009 | if (err) |
| 8010 | goto out_undo_insn; |
| 8011 | |
| 8012 | err = -ENOMEM; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 8013 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8014 | if (!func) |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 8015 | goto out_undo_insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8016 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 8017 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8018 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 8019 | subprog_end = env->subprog_info[i + 1].start; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8020 | |
| 8021 | len = subprog_end - subprog_start; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 8022 | /* BPF_PROG_RUN doesn't call subprogs directly, |
| 8023 | * hence main prog stats include the runtime of subprogs. |
| 8024 | * subprogs don't have IDs and not reachable via prog_get_next_id |
| 8025 | * func[i]->aux->stats will never be accessed and stays NULL |
| 8026 | */ |
| 8027 | 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] | 8028 | if (!func[i]) |
| 8029 | goto out_free; |
| 8030 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 8031 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 8032 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8033 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 8034 | if (bpf_prog_calc_tag(func[i])) |
| 8035 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8036 | func[i]->is_func = 1; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8037 | func[i]->aux->func_idx = i; |
| 8038 | /* the btf and func_info will be freed only at prog->aux */ |
| 8039 | func[i]->aux->btf = prog->aux->btf; |
| 8040 | func[i]->aux->func_info = prog->aux->func_info; |
| 8041 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8042 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 8043 | * Long term would need debug info to populate names |
| 8044 | */ |
| 8045 | func[i]->aux->name[0] = 'F'; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 8046 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8047 | func[i]->jit_requested = 1; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8048 | func[i]->aux->linfo = prog->aux->linfo; |
| 8049 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; |
| 8050 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; |
| 8051 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8052 | func[i] = bpf_int_jit_compile(func[i]); |
| 8053 | if (!func[i]->jited) { |
| 8054 | err = -ENOTSUPP; |
| 8055 | goto out_free; |
| 8056 | } |
| 8057 | cond_resched(); |
| 8058 | } |
| 8059 | /* at this point all bpf functions were successfully JITed |
| 8060 | * now populate all bpf_calls with correct addresses and |
| 8061 | * run last pass of JIT |
| 8062 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 8063 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8064 | insn = func[i]->insnsi; |
| 8065 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 8066 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 8067 | insn->src_reg != BPF_PSEUDO_CALL) |
| 8068 | continue; |
| 8069 | subprog = insn->off; |
Prashant Bhole | 0d306c3 | 2019-04-16 18:13:01 +0900 | [diff] [blame] | 8070 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
| 8071 | __bpf_call_base; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8072 | } |
Sandipan Das | 2162fed | 2018-05-24 12:26:45 +0530 | [diff] [blame] | 8073 | |
| 8074 | /* we use the aux data to keep a list of the start addresses |
| 8075 | * of the JITed images for each function in the program |
| 8076 | * |
| 8077 | * for some architectures, such as powerpc64, the imm field |
| 8078 | * might not be large enough to hold the offset of the start |
| 8079 | * address of the callee's JITed image from __bpf_call_base |
| 8080 | * |
| 8081 | * in such cases, we can lookup the start address of a callee |
| 8082 | * by using its subprog id, available from the off field of |
| 8083 | * the call instruction, as an index for this list |
| 8084 | */ |
| 8085 | func[i]->aux->func = func; |
| 8086 | func[i]->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8087 | } |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 8088 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8089 | old_bpf_func = func[i]->bpf_func; |
| 8090 | tmp = bpf_int_jit_compile(func[i]); |
| 8091 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 8092 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 8093 | err = -ENOTSUPP; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8094 | goto out_free; |
| 8095 | } |
| 8096 | cond_resched(); |
| 8097 | } |
| 8098 | |
| 8099 | /* finally lock prog and jit images for all functions and |
| 8100 | * populate kallsysm |
| 8101 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 8102 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8103 | bpf_prog_lock_ro(func[i]); |
| 8104 | bpf_prog_kallsyms_add(func[i]); |
| 8105 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 8106 | |
| 8107 | /* Last step: make now unused interpreter insns from main |
| 8108 | * prog consistent for later dump requests, so they can |
| 8109 | * later look the same as if they were interpreted only. |
| 8110 | */ |
| 8111 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 8112 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 8113 | insn->src_reg != BPF_PSEUDO_CALL) |
| 8114 | continue; |
| 8115 | insn->off = env->insn_aux_data[i].call_imm; |
| 8116 | subprog = find_subprog(env, i + insn->off + 1); |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 8117 | insn->imm = subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 8118 | } |
| 8119 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8120 | prog->jited = 1; |
| 8121 | prog->bpf_func = func[0]->bpf_func; |
| 8122 | prog->aux->func = func; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 8123 | prog->aux->func_cnt = env->subprog_cnt; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8124 | bpf_prog_free_unused_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8125 | return 0; |
| 8126 | out_free: |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 8127 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8128 | if (func[i]) |
| 8129 | bpf_jit_free(func[i]); |
| 8130 | kfree(func); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 8131 | out_undo_insn: |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8132 | /* cleanup main prog to be interpreted */ |
| 8133 | prog->jit_requested = 0; |
| 8134 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 8135 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 8136 | insn->src_reg != BPF_PSEUDO_CALL) |
| 8137 | continue; |
| 8138 | insn->off = 0; |
| 8139 | insn->imm = env->insn_aux_data[i].call_imm; |
| 8140 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8141 | bpf_prog_free_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8142 | return err; |
| 8143 | } |
| 8144 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 8145 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 8146 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 8147 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 8148 | struct bpf_prog *prog = env->prog; |
| 8149 | struct bpf_insn *insn = prog->insnsi; |
| 8150 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 8151 | #endif |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 8152 | int err = 0; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 8153 | |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 8154 | if (env->prog->jit_requested && |
| 8155 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 8156 | err = jit_subprogs(env); |
| 8157 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8158 | return 0; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 8159 | if (err == -EFAULT) |
| 8160 | return err; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 8161 | } |
| 8162 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 8163 | for (i = 0; i < prog->len; i++, insn++) { |
| 8164 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 8165 | insn->src_reg != BPF_PSEUDO_CALL) |
| 8166 | continue; |
| 8167 | depth = get_callee_stack_depth(env, insn, i); |
| 8168 | if (depth < 0) |
| 8169 | return depth; |
| 8170 | bpf_patch_call_args(insn, depth); |
| 8171 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 8172 | err = 0; |
| 8173 | #endif |
| 8174 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 8175 | } |
| 8176 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8177 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8178 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8179 | * |
| 8180 | * this function is called after eBPF program passed verification |
| 8181 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8182 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8183 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8184 | struct bpf_prog *prog = env->prog; |
| 8185 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8186 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8187 | const int insn_cnt = prog->len; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8188 | const struct bpf_map_ops *ops; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 8189 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8190 | struct bpf_insn insn_buf[16]; |
| 8191 | struct bpf_prog *new_prog; |
| 8192 | struct bpf_map *map_ptr; |
| 8193 | int i, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8194 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8195 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 8196 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 8197 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 8198 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 8199 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 8200 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
| 8201 | struct bpf_insn mask_and_div[] = { |
| 8202 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 8203 | /* Rx div 0 -> 0 */ |
| 8204 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), |
| 8205 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 8206 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 8207 | *insn, |
| 8208 | }; |
| 8209 | struct bpf_insn mask_and_mod[] = { |
| 8210 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 8211 | /* Rx mod 0 -> Rx */ |
| 8212 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), |
| 8213 | *insn, |
| 8214 | }; |
| 8215 | struct bpf_insn *patchlet; |
| 8216 | |
| 8217 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 8218 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 8219 | patchlet = mask_and_div + (is64 ? 1 : 0); |
| 8220 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); |
| 8221 | } else { |
| 8222 | patchlet = mask_and_mod + (is64 ? 1 : 0); |
| 8223 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); |
| 8224 | } |
| 8225 | |
| 8226 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 8227 | if (!new_prog) |
| 8228 | return -ENOMEM; |
| 8229 | |
| 8230 | delta += cnt - 1; |
| 8231 | env->prog = prog = new_prog; |
| 8232 | insn = new_prog->insnsi + i + delta; |
| 8233 | continue; |
| 8234 | } |
| 8235 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 8236 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 8237 | (BPF_MODE(insn->code) == BPF_ABS || |
| 8238 | BPF_MODE(insn->code) == BPF_IND)) { |
| 8239 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 8240 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 8241 | verbose(env, "bpf verifier is misconfigured\n"); |
| 8242 | return -EINVAL; |
| 8243 | } |
| 8244 | |
| 8245 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 8246 | if (!new_prog) |
| 8247 | return -ENOMEM; |
| 8248 | |
| 8249 | delta += cnt - 1; |
| 8250 | env->prog = prog = new_prog; |
| 8251 | insn = new_prog->insnsi + i + delta; |
| 8252 | continue; |
| 8253 | } |
| 8254 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 8255 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
| 8256 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { |
| 8257 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; |
| 8258 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; |
| 8259 | struct bpf_insn insn_buf[16]; |
| 8260 | struct bpf_insn *patch = &insn_buf[0]; |
| 8261 | bool issrc, isneg; |
| 8262 | u32 off_reg; |
| 8263 | |
| 8264 | aux = &env->insn_aux_data[i + delta]; |
Daniel Borkmann | 3612af7 | 2019-03-01 22:05:29 +0100 | [diff] [blame] | 8265 | if (!aux->alu_state || |
| 8266 | aux->alu_state == BPF_ALU_NON_POINTER) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 8267 | continue; |
| 8268 | |
| 8269 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; |
| 8270 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == |
| 8271 | BPF_ALU_SANITIZE_SRC; |
| 8272 | |
| 8273 | off_reg = issrc ? insn->src_reg : insn->dst_reg; |
| 8274 | if (isneg) |
| 8275 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 8276 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); |
| 8277 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); |
| 8278 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); |
| 8279 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); |
| 8280 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); |
| 8281 | if (issrc) { |
| 8282 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, |
| 8283 | off_reg); |
| 8284 | insn->src_reg = BPF_REG_AX; |
| 8285 | } else { |
| 8286 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, |
| 8287 | BPF_REG_AX); |
| 8288 | } |
| 8289 | if (isneg) |
| 8290 | insn->code = insn->code == code_add ? |
| 8291 | code_sub : code_add; |
| 8292 | *patch++ = *insn; |
| 8293 | if (issrc && isneg) |
| 8294 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 8295 | cnt = patch - insn_buf; |
| 8296 | |
| 8297 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 8298 | if (!new_prog) |
| 8299 | return -ENOMEM; |
| 8300 | |
| 8301 | delta += cnt - 1; |
| 8302 | env->prog = prog = new_prog; |
| 8303 | insn = new_prog->insnsi + i + delta; |
| 8304 | continue; |
| 8305 | } |
| 8306 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8307 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 8308 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 8309 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 8310 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8311 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8312 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 8313 | prog->dst_needed = 1; |
| 8314 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 8315 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 8316 | if (insn->imm == BPF_FUNC_override_return) |
| 8317 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8318 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 8319 | /* If we tail call into other programs, we |
| 8320 | * cannot make any assumptions since they can |
| 8321 | * be replaced dynamically during runtime in |
| 8322 | * the program array. |
| 8323 | */ |
| 8324 | prog->cb_access = 1; |
Alexei Starovoitov | 80a58d0 | 2017-05-30 13:31:30 -0700 | [diff] [blame] | 8325 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 8326 | env->prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 8327 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8328 | /* mark bpf_tail_call as different opcode to avoid |
| 8329 | * conditional branch in the interpeter for every normal |
| 8330 | * call and to prevent accidental JITing by JIT compiler |
| 8331 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8332 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8333 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 8334 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 8335 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 8336 | aux = &env->insn_aux_data[i + delta]; |
| 8337 | if (!bpf_map_ptr_unpriv(aux)) |
| 8338 | continue; |
| 8339 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 8340 | /* instead of changing every JIT dealing with tail_call |
| 8341 | * emit two extra insns: |
| 8342 | * if (index >= max_entries) goto out; |
| 8343 | * index &= array->index_mask; |
| 8344 | * to avoid out-of-bounds cpu speculation |
| 8345 | */ |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 8346 | if (bpf_map_ptr_poisoned(aux)) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 8347 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 8348 | return -EINVAL; |
| 8349 | } |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 8350 | |
| 8351 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 8352 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 8353 | map_ptr->max_entries, 2); |
| 8354 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 8355 | container_of(map_ptr, |
| 8356 | struct bpf_array, |
| 8357 | map)->index_mask); |
| 8358 | insn_buf[2] = *insn; |
| 8359 | cnt = 3; |
| 8360 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 8361 | if (!new_prog) |
| 8362 | return -ENOMEM; |
| 8363 | |
| 8364 | delta += cnt - 1; |
| 8365 | env->prog = prog = new_prog; |
| 8366 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8367 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8368 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8369 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 8370 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8371 | * and other inlining handlers are currently limited to 64 bit |
| 8372 | * only. |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 8373 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 8374 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8375 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 8376 | insn->imm == BPF_FUNC_map_update_elem || |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 8377 | insn->imm == BPF_FUNC_map_delete_elem || |
| 8378 | insn->imm == BPF_FUNC_map_push_elem || |
| 8379 | insn->imm == BPF_FUNC_map_pop_elem || |
| 8380 | insn->imm == BPF_FUNC_map_peek_elem)) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 8381 | aux = &env->insn_aux_data[i + delta]; |
| 8382 | if (bpf_map_ptr_poisoned(aux)) |
| 8383 | goto patch_call_imm; |
| 8384 | |
| 8385 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8386 | ops = map_ptr->ops; |
| 8387 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 8388 | ops->map_gen_lookup) { |
| 8389 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
| 8390 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 8391 | verbose(env, "bpf verifier is misconfigured\n"); |
| 8392 | return -EINVAL; |
| 8393 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8394 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8395 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 8396 | insn_buf, cnt); |
| 8397 | if (!new_prog) |
| 8398 | return -ENOMEM; |
| 8399 | |
| 8400 | delta += cnt - 1; |
| 8401 | env->prog = prog = new_prog; |
| 8402 | insn = new_prog->insnsi + i + delta; |
| 8403 | continue; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8404 | } |
| 8405 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8406 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 8407 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 8408 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 8409 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 8410 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 8411 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 8412 | u64 flags))NULL)); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 8413 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 8414 | (int (*)(struct bpf_map *map, void *value, |
| 8415 | u64 flags))NULL)); |
| 8416 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 8417 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 8418 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 8419 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 8420 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8421 | switch (insn->imm) { |
| 8422 | case BPF_FUNC_map_lookup_elem: |
| 8423 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - |
| 8424 | __bpf_call_base; |
| 8425 | continue; |
| 8426 | case BPF_FUNC_map_update_elem: |
| 8427 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - |
| 8428 | __bpf_call_base; |
| 8429 | continue; |
| 8430 | case BPF_FUNC_map_delete_elem: |
| 8431 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - |
| 8432 | __bpf_call_base; |
| 8433 | continue; |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 8434 | case BPF_FUNC_map_push_elem: |
| 8435 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - |
| 8436 | __bpf_call_base; |
| 8437 | continue; |
| 8438 | case BPF_FUNC_map_pop_elem: |
| 8439 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - |
| 8440 | __bpf_call_base; |
| 8441 | continue; |
| 8442 | case BPF_FUNC_map_peek_elem: |
| 8443 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - |
| 8444 | __bpf_call_base; |
| 8445 | continue; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8446 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8447 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8448 | goto patch_call_imm; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8449 | } |
| 8450 | |
| 8451 | patch_call_imm: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 8452 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8453 | /* all functions that have prototype and verifier allowed |
| 8454 | * programs to call them, must be real in-kernel functions |
| 8455 | */ |
| 8456 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8457 | verbose(env, |
| 8458 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8459 | func_id_name(insn->imm), insn->imm); |
| 8460 | return -EFAULT; |
| 8461 | } |
| 8462 | insn->imm = fn->func - __bpf_call_base; |
| 8463 | } |
| 8464 | |
| 8465 | return 0; |
| 8466 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8467 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8468 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8469 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8470 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8471 | int i; |
| 8472 | |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 8473 | sl = env->free_list; |
| 8474 | while (sl) { |
| 8475 | sln = sl->next; |
| 8476 | free_verifier_state(&sl->state, false); |
| 8477 | kfree(sl); |
| 8478 | sl = sln; |
| 8479 | } |
| 8480 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8481 | if (!env->explored_states) |
| 8482 | return; |
| 8483 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 8484 | for (i = 0; i < state_htab_size(env); i++) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8485 | sl = env->explored_states[i]; |
| 8486 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 8487 | while (sl) { |
| 8488 | sln = sl->next; |
| 8489 | free_verifier_state(&sl->state, false); |
| 8490 | kfree(sl); |
| 8491 | sl = sln; |
| 8492 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8493 | } |
| 8494 | |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 8495 | kvfree(env->explored_states); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8496 | } |
| 8497 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 8498 | static void print_verification_stats(struct bpf_verifier_env *env) |
| 8499 | { |
| 8500 | int i; |
| 8501 | |
| 8502 | if (env->log.level & BPF_LOG_STATS) { |
| 8503 | verbose(env, "verification time %lld usec\n", |
| 8504 | div_u64(env->verification_time, 1000)); |
| 8505 | verbose(env, "stack depth "); |
| 8506 | for (i = 0; i < env->subprog_cnt; i++) { |
| 8507 | u32 depth = env->subprog_info[i].stack_depth; |
| 8508 | |
| 8509 | verbose(env, "%d", depth); |
| 8510 | if (i + 1 < env->subprog_cnt) |
| 8511 | verbose(env, "+"); |
| 8512 | } |
| 8513 | verbose(env, "\n"); |
| 8514 | } |
| 8515 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " |
| 8516 | "total_states %d peak_states %d mark_read %d\n", |
| 8517 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, |
| 8518 | env->max_states_per_insn, env->total_states, |
| 8519 | env->peak_states, env->longest_mark_read_walk); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8520 | } |
| 8521 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8522 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
| 8523 | union bpf_attr __user *uattr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 8524 | { |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 8525 | u64 start_time = ktime_get_ns(); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8526 | struct bpf_verifier_env *env; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 8527 | struct bpf_verifier_log *log; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 8528 | int i, len, ret = -EINVAL; |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8529 | bool is_priv; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 8530 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 8531 | /* no program is valid */ |
| 8532 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 8533 | return -EINVAL; |
| 8534 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8535 | /* '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] | 8536 | * allocate/free it every time bpf_check() is called |
| 8537 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8538 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8539 | if (!env) |
| 8540 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8541 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8542 | |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 8543 | len = (*prog)->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 8544 | env->insn_aux_data = |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 8545 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8546 | ret = -ENOMEM; |
| 8547 | if (!env->insn_aux_data) |
| 8548 | goto err_free_env; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 8549 | for (i = 0; i < len; i++) |
| 8550 | env->insn_aux_data[i].orig_idx = i; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8551 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 8552 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 8553 | is_priv = capable(CAP_SYS_ADMIN); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8554 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8555 | /* grab the mutex to protect few globals used by verifier */ |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 8556 | if (!is_priv) |
| 8557 | mutex_lock(&bpf_verifier_lock); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8558 | |
| 8559 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 8560 | /* user requested verbose verifier output |
| 8561 | * and supplied buffer to store the verification trace |
| 8562 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 8563 | log->level = attr->log_level; |
| 8564 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 8565 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8566 | |
| 8567 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 8568 | /* log attributes have to be sane */ |
Alexei Starovoitov | 7a9f5c6 | 2019-04-01 21:27:46 -0700 | [diff] [blame] | 8569 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 8570 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8571 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8572 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 8573 | |
| 8574 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 8575 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 8576 | env->strict_alignment = true; |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 8577 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
| 8578 | env->strict_alignment = false; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8579 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8580 | env->allow_ptr_leaks = is_priv; |
| 8581 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8582 | ret = replace_map_fd_with_map_ptr(env); |
| 8583 | if (ret < 0) |
| 8584 | goto skip_full_check; |
| 8585 | |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 8586 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Quentin Monnet | a40a263 | 2018-11-09 13:03:31 +0000 | [diff] [blame] | 8587 | ret = bpf_prog_offload_verifier_prep(env->prog); |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 8588 | if (ret) |
| 8589 | goto skip_full_check; |
| 8590 | } |
| 8591 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 8592 | env->explored_states = kvcalloc(state_htab_size(env), |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8593 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8594 | GFP_USER); |
| 8595 | ret = -ENOMEM; |
| 8596 | if (!env->explored_states) |
| 8597 | goto skip_full_check; |
| 8598 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 8599 | ret = check_subprogs(env); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8600 | if (ret < 0) |
| 8601 | goto skip_full_check; |
| 8602 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8603 | ret = check_btf_info(env, attr, uattr); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8604 | if (ret < 0) |
| 8605 | goto skip_full_check; |
| 8606 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 8607 | ret = check_cfg(env); |
| 8608 | if (ret < 0) |
| 8609 | goto skip_full_check; |
| 8610 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8611 | ret = do_check(env); |
Craig Gallek | 8c01c4f | 2017-11-02 11:18:01 -0400 | [diff] [blame] | 8612 | if (env->cur_state) { |
| 8613 | free_verifier_state(env->cur_state, true); |
| 8614 | env->cur_state = NULL; |
| 8615 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8616 | |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 8617 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 8618 | ret = bpf_prog_offload_finalize(env); |
| 8619 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8620 | skip_full_check: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8621 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8622 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8623 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8624 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 8625 | ret = check_max_stack_depth(env); |
| 8626 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 8627 | /* instruction rewrites happen after this point */ |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8628 | if (is_priv) { |
| 8629 | if (ret == 0) |
| 8630 | opt_hard_wire_dead_code_branches(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 8631 | if (ret == 0) |
| 8632 | ret = opt_remove_dead_code(env); |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 8633 | if (ret == 0) |
| 8634 | ret = opt_remove_nops(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 8635 | } else { |
| 8636 | if (ret == 0) |
| 8637 | sanitize_dead_code(env); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8638 | } |
| 8639 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 8640 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8641 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 8642 | ret = convert_ctx_accesses(env); |
| 8643 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8644 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8645 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8646 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 8647 | /* do 32-bit optimization after insn patching has done so those patched |
| 8648 | * insns could be handled correctly. |
| 8649 | */ |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 8650 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
| 8651 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); |
| 8652 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret |
| 8653 | : false; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 8654 | } |
| 8655 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 8656 | if (ret == 0) |
| 8657 | ret = fixup_call_args(env); |
| 8658 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 8659 | env->verification_time = ktime_get_ns() - start_time; |
| 8660 | print_verification_stats(env); |
| 8661 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8662 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8663 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8664 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8665 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8666 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8667 | } |
| 8668 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8669 | if (ret == 0 && env->used_map_cnt) { |
| 8670 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8671 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 8672 | sizeof(env->used_maps[0]), |
| 8673 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8674 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8675 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8676 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8677 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8678 | } |
| 8679 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8680 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8681 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8682 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8683 | |
| 8684 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 8685 | * bpf_ld_imm64 instructions |
| 8686 | */ |
| 8687 | convert_pseudo_ld_imm64(env); |
| 8688 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8689 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8690 | if (ret == 0) |
| 8691 | adjust_btf_func(env); |
| 8692 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8693 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8694 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8695 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 8696 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8697 | */ |
| 8698 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8699 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8700 | err_unlock: |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 8701 | if (!is_priv) |
| 8702 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8703 | vfree(env->insn_aux_data); |
| 8704 | err_free_env: |
| 8705 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 8706 | return ret; |
| 8707 | } |