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 || |
| 337 | type == PTR_TO_TCP_SOCK; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 340 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
| 341 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 342 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 343 | type == PTR_TO_SOCKET_OR_NULL || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 344 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
| 345 | type == PTR_TO_TCP_SOCK_OR_NULL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 348 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
| 349 | { |
| 350 | return reg->type == PTR_TO_MAP_VALUE && |
| 351 | map_value_has_spin_lock(reg->map_ptr); |
| 352 | } |
| 353 | |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 354 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
| 355 | { |
| 356 | return type == PTR_TO_SOCKET || |
| 357 | type == PTR_TO_SOCKET_OR_NULL || |
| 358 | type == PTR_TO_TCP_SOCK || |
| 359 | type == PTR_TO_TCP_SOCK_OR_NULL; |
| 360 | } |
| 361 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 362 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 363 | { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 364 | return type == ARG_PTR_TO_SOCK_COMMON; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /* Determine whether the function releases some resources allocated by another |
| 368 | * function call. The first reference type argument will be assumed to be |
| 369 | * released by release_reference(). |
| 370 | */ |
| 371 | static bool is_release_function(enum bpf_func_id func_id) |
| 372 | { |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 373 | return func_id == BPF_FUNC_sk_release; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 376 | static bool is_acquire_function(enum bpf_func_id func_id) |
| 377 | { |
| 378 | return func_id == BPF_FUNC_sk_lookup_tcp || |
Lorenz Bauer | edbf8c0 | 2019-03-22 09:54:01 +0800 | [diff] [blame] | 379 | func_id == BPF_FUNC_sk_lookup_udp || |
| 380 | func_id == BPF_FUNC_skc_lookup_tcp; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 383 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
| 384 | { |
| 385 | return func_id == BPF_FUNC_tcp_sock || |
| 386 | func_id == BPF_FUNC_sk_fullsock; |
| 387 | } |
| 388 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 389 | /* string representation of 'enum bpf_reg_type' */ |
| 390 | static const char * const reg_type_str[] = { |
| 391 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 392 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 393 | [PTR_TO_CTX] = "ctx", |
| 394 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 395 | [PTR_TO_MAP_VALUE] = "map_value", |
| 396 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 397 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 398 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 399 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 400 | [PTR_TO_PACKET_END] = "pkt_end", |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 401 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 402 | [PTR_TO_SOCKET] = "sock", |
| 403 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 404 | [PTR_TO_SOCK_COMMON] = "sock_common", |
| 405 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 406 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
| 407 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 408 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 409 | }; |
| 410 | |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 411 | static char slot_type_char[] = { |
| 412 | [STACK_INVALID] = '?', |
| 413 | [STACK_SPILL] = 'r', |
| 414 | [STACK_MISC] = 'm', |
| 415 | [STACK_ZERO] = '0', |
| 416 | }; |
| 417 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 418 | static void print_liveness(struct bpf_verifier_env *env, |
| 419 | enum bpf_reg_liveness live) |
| 420 | { |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 421 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 422 | verbose(env, "_"); |
| 423 | if (live & REG_LIVE_READ) |
| 424 | verbose(env, "r"); |
| 425 | if (live & REG_LIVE_WRITTEN) |
| 426 | verbose(env, "w"); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 427 | if (live & REG_LIVE_DONE) |
| 428 | verbose(env, "D"); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 431 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 432 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 433 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 434 | struct bpf_verifier_state *cur = env->cur_state; |
| 435 | |
| 436 | return cur->frame[reg->frameno]; |
| 437 | } |
| 438 | |
| 439 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 440 | const struct bpf_func_state *state) |
| 441 | { |
| 442 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 443 | enum bpf_reg_type t; |
| 444 | int i; |
| 445 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 446 | if (state->frameno) |
| 447 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 448 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 449 | reg = &state->regs[i]; |
| 450 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 451 | if (t == NOT_INIT) |
| 452 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 453 | verbose(env, " R%d", i); |
| 454 | print_liveness(env, reg->live); |
| 455 | verbose(env, "=%s", reg_type_str[t]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 456 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 457 | tnum_is_const(reg->var_off)) { |
| 458 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 459 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 460 | if (t == PTR_TO_STACK) |
| 461 | verbose(env, ",call_%d", func(env, reg)->callsite); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 462 | } else { |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 463 | verbose(env, "(id=%d", reg->id); |
| 464 | if (reg_type_may_be_refcounted_or_null(t)) |
| 465 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 466 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 467 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 468 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 469 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 470 | else if (t == CONST_PTR_TO_MAP || |
| 471 | t == PTR_TO_MAP_VALUE || |
| 472 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 473 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 474 | reg->map_ptr->key_size, |
| 475 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 476 | if (tnum_is_const(reg->var_off)) { |
| 477 | /* Typically an immediate SCALAR_VALUE, but |
| 478 | * could be a pointer whose offset is too big |
| 479 | * for reg->off |
| 480 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 481 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 482 | } else { |
| 483 | if (reg->smin_value != reg->umin_value && |
| 484 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 485 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 486 | (long long)reg->smin_value); |
| 487 | if (reg->smax_value != reg->umax_value && |
| 488 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 489 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 490 | (long long)reg->smax_value); |
| 491 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 492 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 493 | (unsigned long long)reg->umin_value); |
| 494 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 495 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 496 | (unsigned long long)reg->umax_value); |
| 497 | if (!tnum_is_unknown(reg->var_off)) { |
| 498 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 499 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 500 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 501 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 502 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 503 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 504 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 505 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 506 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 507 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 508 | char types_buf[BPF_REG_SIZE + 1]; |
| 509 | bool valid = false; |
| 510 | int j; |
| 511 | |
| 512 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 513 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 514 | valid = true; |
| 515 | types_buf[j] = slot_type_char[ |
| 516 | state->stack[i].slot_type[j]]; |
| 517 | } |
| 518 | types_buf[BPF_REG_SIZE] = 0; |
| 519 | if (!valid) |
| 520 | continue; |
| 521 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 522 | print_liveness(env, state->stack[i].spilled_ptr.live); |
| 523 | if (state->stack[i].slot_type[0] == STACK_SPILL) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 524 | verbose(env, "=%s", |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 525 | reg_type_str[state->stack[i].spilled_ptr.type]); |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 526 | else |
| 527 | verbose(env, "=%s", types_buf); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 528 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 529 | if (state->acquired_refs && state->refs[0].id) { |
| 530 | verbose(env, " refs=%d", state->refs[0].id); |
| 531 | for (i = 1; i < state->acquired_refs; i++) |
| 532 | if (state->refs[i].id) |
| 533 | verbose(env, ",%d", state->refs[i].id); |
| 534 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 535 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 538 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 539 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ |
| 540 | const struct bpf_func_state *src) \ |
| 541 | { \ |
| 542 | if (!src->FIELD) \ |
| 543 | return 0; \ |
| 544 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ |
| 545 | /* internal bug, make state invalid to reject the program */ \ |
| 546 | memset(dst, 0, sizeof(*dst)); \ |
| 547 | return -EFAULT; \ |
| 548 | } \ |
| 549 | memcpy(dst->FIELD, src->FIELD, \ |
| 550 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ |
| 551 | return 0; \ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 552 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 553 | /* copy_reference_state() */ |
| 554 | COPY_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 555 | /* copy_stack_state() */ |
| 556 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 557 | #undef COPY_STATE_FN |
| 558 | |
| 559 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 560 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ |
| 561 | bool copy_old) \ |
| 562 | { \ |
| 563 | u32 old_size = state->COUNT; \ |
| 564 | struct bpf_##NAME##_state *new_##FIELD; \ |
| 565 | int slot = size / SIZE; \ |
| 566 | \ |
| 567 | if (size <= old_size || !size) { \ |
| 568 | if (copy_old) \ |
| 569 | return 0; \ |
| 570 | state->COUNT = slot * SIZE; \ |
| 571 | if (!size && old_size) { \ |
| 572 | kfree(state->FIELD); \ |
| 573 | state->FIELD = NULL; \ |
| 574 | } \ |
| 575 | return 0; \ |
| 576 | } \ |
| 577 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ |
| 578 | GFP_KERNEL); \ |
| 579 | if (!new_##FIELD) \ |
| 580 | return -ENOMEM; \ |
| 581 | if (copy_old) { \ |
| 582 | if (state->FIELD) \ |
| 583 | memcpy(new_##FIELD, state->FIELD, \ |
| 584 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ |
| 585 | memset(new_##FIELD + old_size / SIZE, 0, \ |
| 586 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ |
| 587 | } \ |
| 588 | state->COUNT = slot * SIZE; \ |
| 589 | kfree(state->FIELD); \ |
| 590 | state->FIELD = new_##FIELD; \ |
| 591 | return 0; \ |
| 592 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 593 | /* realloc_reference_state() */ |
| 594 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 595 | /* realloc_stack_state() */ |
| 596 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 597 | #undef REALLOC_STATE_FN |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 598 | |
| 599 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 600 | * make it consume minimal amount of memory. check_stack_write() access from |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 601 | * the program calls into realloc_func_state() to grow the stack size. |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 602 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 603 | * which realloc_stack_state() copies over. It points to previous |
| 604 | * bpf_verifier_state which is never reallocated. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 605 | */ |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 606 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
| 607 | int refs_size, bool copy_old) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 608 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 609 | int err = realloc_reference_state(state, refs_size, copy_old); |
| 610 | if (err) |
| 611 | return err; |
| 612 | return realloc_stack_state(state, stack_size, copy_old); |
| 613 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 614 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 615 | /* Acquire a pointer id from the env and update the state->refs to include |
| 616 | * this new pointer reference. |
| 617 | * On success, returns a valid pointer id to associate with the register |
| 618 | * On failure, returns a negative errno. |
| 619 | */ |
| 620 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 621 | { |
| 622 | struct bpf_func_state *state = cur_func(env); |
| 623 | int new_ofs = state->acquired_refs; |
| 624 | int id, err; |
| 625 | |
| 626 | err = realloc_reference_state(state, state->acquired_refs + 1, true); |
| 627 | if (err) |
| 628 | return err; |
| 629 | id = ++env->id_gen; |
| 630 | state->refs[new_ofs].id = id; |
| 631 | state->refs[new_ofs].insn_idx = insn_idx; |
| 632 | |
| 633 | return id; |
| 634 | } |
| 635 | |
| 636 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 637 | 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] | 638 | { |
| 639 | int i, last_idx; |
| 640 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 641 | last_idx = state->acquired_refs - 1; |
| 642 | for (i = 0; i < state->acquired_refs; i++) { |
| 643 | if (state->refs[i].id == ptr_id) { |
| 644 | if (last_idx && i != last_idx) |
| 645 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 646 | sizeof(*state->refs)); |
| 647 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 648 | state->acquired_refs--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 649 | return 0; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 650 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 651 | } |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 652 | return -EINVAL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static int transfer_reference_state(struct bpf_func_state *dst, |
| 656 | struct bpf_func_state *src) |
| 657 | { |
| 658 | int err = realloc_reference_state(dst, src->acquired_refs, false); |
| 659 | if (err) |
| 660 | return err; |
| 661 | err = copy_reference_state(dst, src); |
| 662 | if (err) |
| 663 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 664 | return 0; |
| 665 | } |
| 666 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 667 | static void free_func_state(struct bpf_func_state *state) |
| 668 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 669 | if (!state) |
| 670 | return; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 671 | kfree(state->refs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 672 | kfree(state->stack); |
| 673 | kfree(state); |
| 674 | } |
| 675 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 676 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 677 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 678 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 679 | int i; |
| 680 | |
| 681 | for (i = 0; i <= state->curframe; i++) { |
| 682 | free_func_state(state->frame[i]); |
| 683 | state->frame[i] = NULL; |
| 684 | } |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 685 | if (free_self) |
| 686 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* copy verifier state from src to dst growing dst stack space |
| 690 | * when necessary to accommodate larger src stack |
| 691 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 692 | static int copy_func_state(struct bpf_func_state *dst, |
| 693 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 694 | { |
| 695 | int err; |
| 696 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 697 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
| 698 | false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 699 | if (err) |
| 700 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 701 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 702 | err = copy_reference_state(dst, src); |
| 703 | if (err) |
| 704 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 705 | return copy_stack_state(dst, src); |
| 706 | } |
| 707 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 708 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 709 | const struct bpf_verifier_state *src) |
| 710 | { |
| 711 | struct bpf_func_state *dst; |
| 712 | int i, err; |
| 713 | |
| 714 | /* if dst has more stack frames then src frame, free them */ |
| 715 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 716 | free_func_state(dst_state->frame[i]); |
| 717 | dst_state->frame[i] = NULL; |
| 718 | } |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 719 | dst_state->speculative = src->speculative; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 720 | dst_state->curframe = src->curframe; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 721 | dst_state->active_spin_lock = src->active_spin_lock; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 722 | for (i = 0; i <= src->curframe; i++) { |
| 723 | dst = dst_state->frame[i]; |
| 724 | if (!dst) { |
| 725 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 726 | if (!dst) |
| 727 | return -ENOMEM; |
| 728 | dst_state->frame[i] = dst; |
| 729 | } |
| 730 | err = copy_func_state(dst, src->frame[i]); |
| 731 | if (err) |
| 732 | return err; |
| 733 | } |
| 734 | return 0; |
| 735 | } |
| 736 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 737 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
| 738 | int *insn_idx) |
| 739 | { |
| 740 | struct bpf_verifier_state *cur = env->cur_state; |
| 741 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 742 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 743 | |
| 744 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 745 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 746 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 747 | if (cur) { |
| 748 | err = copy_verifier_state(cur, &head->st); |
| 749 | if (err) |
| 750 | return err; |
| 751 | } |
| 752 | if (insn_idx) |
| 753 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 754 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 755 | *prev_insn_idx = head->prev_insn_idx; |
| 756 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 757 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 758 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 759 | env->head = elem; |
| 760 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 761 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 764 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 765 | int insn_idx, int prev_insn_idx, |
| 766 | bool speculative) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 767 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 768 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 769 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 770 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 771 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 772 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 773 | if (!elem) |
| 774 | goto err; |
| 775 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 776 | elem->insn_idx = insn_idx; |
| 777 | elem->prev_insn_idx = prev_insn_idx; |
| 778 | elem->next = env->head; |
| 779 | env->head = elem; |
| 780 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 781 | err = copy_verifier_state(&elem->st, cur); |
| 782 | if (err) |
| 783 | goto err; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 784 | elem->st.speculative |= speculative; |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 785 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
| 786 | verbose(env, "The sequence of %d jumps is too complex.\n", |
| 787 | env->stack_size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 788 | goto err; |
| 789 | } |
| 790 | return &elem->st; |
| 791 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 792 | free_verifier_state(env->cur_state, true); |
| 793 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 794 | /* pop all elements and return */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 795 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 796 | return NULL; |
| 797 | } |
| 798 | |
| 799 | #define CALLER_SAVED_REGS 6 |
| 800 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 801 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 802 | }; |
| 803 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 804 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
| 805 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 806 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 807 | * known to have the value @imm. |
| 808 | */ |
| 809 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 810 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 811 | /* Clear id, off, and union(map_ptr, range) */ |
| 812 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 813 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 814 | reg->var_off = tnum_const(imm); |
| 815 | reg->smin_value = (s64)imm; |
| 816 | reg->smax_value = (s64)imm; |
| 817 | reg->umin_value = imm; |
| 818 | reg->umax_value = imm; |
| 819 | } |
| 820 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 821 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 822 | * used only on registers holding a pointer type. |
| 823 | */ |
| 824 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 825 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 826 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 827 | } |
| 828 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 829 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 830 | { |
| 831 | __mark_reg_known(reg, 0); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 832 | reg->type = SCALAR_VALUE; |
| 833 | } |
| 834 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 835 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 836 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 837 | { |
| 838 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 839 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 840 | /* Something bad happened, let's kill all regs */ |
| 841 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 842 | __mark_reg_not_init(regs + regno); |
| 843 | return; |
| 844 | } |
| 845 | __mark_reg_known_zero(regs + regno); |
| 846 | } |
| 847 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 848 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 849 | { |
| 850 | return type_is_pkt_pointer(reg->type); |
| 851 | } |
| 852 | |
| 853 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 854 | { |
| 855 | return reg_is_pkt_pointer(reg) || |
| 856 | reg->type == PTR_TO_PACKET_END; |
| 857 | } |
| 858 | |
| 859 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 860 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 861 | enum bpf_reg_type which) |
| 862 | { |
| 863 | /* The register can already have a range from prior markings. |
| 864 | * This is fine as long as it hasn't been advanced from its |
| 865 | * origin. |
| 866 | */ |
| 867 | return reg->type == which && |
| 868 | reg->id == 0 && |
| 869 | reg->off == 0 && |
| 870 | tnum_equals_const(reg->var_off, 0); |
| 871 | } |
| 872 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 873 | /* Attempts to improve min/max values based on var_off information */ |
| 874 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 875 | { |
| 876 | /* min signed is max(sign bit) | min(other bits) */ |
| 877 | reg->smin_value = max_t(s64, reg->smin_value, |
| 878 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 879 | /* max signed is min(sign bit) | max(other bits) */ |
| 880 | reg->smax_value = min_t(s64, reg->smax_value, |
| 881 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 882 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 883 | reg->umax_value = min(reg->umax_value, |
| 884 | reg->var_off.value | reg->var_off.mask); |
| 885 | } |
| 886 | |
| 887 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
| 888 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 889 | { |
| 890 | /* Learn sign from signed bounds. |
| 891 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 892 | * are the same, so combine. This works even in the negative case, e.g. |
| 893 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 894 | */ |
| 895 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 896 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 897 | reg->umin_value); |
| 898 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 899 | reg->umax_value); |
| 900 | return; |
| 901 | } |
| 902 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 903 | * boundary, so we must be careful. |
| 904 | */ |
| 905 | if ((s64)reg->umax_value >= 0) { |
| 906 | /* Positive. We can't learn anything from the smin, but smax |
| 907 | * is positive, hence safe. |
| 908 | */ |
| 909 | reg->smin_value = reg->umin_value; |
| 910 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 911 | reg->umax_value); |
| 912 | } else if ((s64)reg->umin_value < 0) { |
| 913 | /* Negative. We can't learn anything from the smax, but smin |
| 914 | * is negative, hence safe. |
| 915 | */ |
| 916 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 917 | reg->umin_value); |
| 918 | reg->smax_value = reg->umax_value; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 923 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 924 | { |
| 925 | reg->var_off = tnum_intersect(reg->var_off, |
| 926 | tnum_range(reg->umin_value, |
| 927 | reg->umax_value)); |
| 928 | } |
| 929 | |
| 930 | /* Reset the min/max bounds of a register */ |
| 931 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 932 | { |
| 933 | reg->smin_value = S64_MIN; |
| 934 | reg->smax_value = S64_MAX; |
| 935 | reg->umin_value = 0; |
| 936 | reg->umax_value = U64_MAX; |
| 937 | } |
| 938 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 939 | /* Mark a register as having a completely unknown (scalar) value. */ |
| 940 | static void __mark_reg_unknown(struct bpf_reg_state *reg) |
| 941 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 942 | /* |
| 943 | * Clear type, id, off, and union(map_ptr, range) and |
| 944 | * padding between 'type' and union |
| 945 | */ |
| 946 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 947 | reg->type = SCALAR_VALUE; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 948 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 949 | reg->frameno = 0; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 950 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 951 | } |
| 952 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 953 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 954 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 955 | { |
| 956 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 957 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 958 | /* Something bad happened, let's kill all regs except FP */ |
| 959 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 960 | __mark_reg_not_init(regs + regno); |
| 961 | return; |
| 962 | } |
| 963 | __mark_reg_unknown(regs + regno); |
| 964 | } |
| 965 | |
| 966 | static void __mark_reg_not_init(struct bpf_reg_state *reg) |
| 967 | { |
| 968 | __mark_reg_unknown(reg); |
| 969 | reg->type = NOT_INIT; |
| 970 | } |
| 971 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 972 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 973 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 974 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 975 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 976 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 977 | /* Something bad happened, let's kill all regs except FP */ |
| 978 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 979 | __mark_reg_not_init(regs + regno); |
| 980 | return; |
| 981 | } |
| 982 | __mark_reg_not_init(regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 983 | } |
| 984 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 985 | #define DEF_NOT_SUBREG (0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 986 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 987 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 988 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 989 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 990 | int i; |
| 991 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 992 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 993 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 994 | regs[i].live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 995 | regs[i].parent = NULL; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 996 | regs[i].subreg_def = DEF_NOT_SUBREG; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 997 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 998 | |
| 999 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1000 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1001 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1002 | regs[BPF_REG_FP].frameno = state->frameno; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1003 | |
| 1004 | /* 1st arg to a function */ |
| 1005 | regs[BPF_REG_1].type = PTR_TO_CTX; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1006 | mark_reg_known_zero(env, regs, BPF_REG_1); |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 1007 | } |
| 1008 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1009 | #define BPF_MAIN_FUNC (-1) |
| 1010 | static void init_func_state(struct bpf_verifier_env *env, |
| 1011 | struct bpf_func_state *state, |
| 1012 | int callsite, int frameno, int subprogno) |
| 1013 | { |
| 1014 | state->callsite = callsite; |
| 1015 | state->frameno = frameno; |
| 1016 | state->subprogno = subprogno; |
| 1017 | init_reg_state(env, state); |
| 1018 | } |
| 1019 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1020 | enum reg_arg_type { |
| 1021 | SRC_OP, /* register is used as source operand */ |
| 1022 | DST_OP, /* register is used as destination operand */ |
| 1023 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 1024 | }; |
| 1025 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1026 | static int cmp_subprogs(const void *a, const void *b) |
| 1027 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1028 | return ((struct bpf_subprog_info *)a)->start - |
| 1029 | ((struct bpf_subprog_info *)b)->start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 1033 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1034 | struct bpf_subprog_info *p; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1035 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1036 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 1037 | sizeof(env->subprog_info[0]), cmp_subprogs); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1038 | if (!p) |
| 1039 | return -ENOENT; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1040 | return p - env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1041 | |
| 1042 | } |
| 1043 | |
| 1044 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 1045 | { |
| 1046 | int insn_cnt = env->prog->len; |
| 1047 | int ret; |
| 1048 | |
| 1049 | if (off >= insn_cnt || off < 0) { |
| 1050 | verbose(env, "call to invalid destination\n"); |
| 1051 | return -EINVAL; |
| 1052 | } |
| 1053 | ret = find_subprog(env, off); |
| 1054 | if (ret >= 0) |
| 1055 | return 0; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1056 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1057 | verbose(env, "too many subprograms\n"); |
| 1058 | return -E2BIG; |
| 1059 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1060 | env->subprog_info[env->subprog_cnt++].start = off; |
| 1061 | sort(env->subprog_info, env->subprog_cnt, |
| 1062 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1063 | return 0; |
| 1064 | } |
| 1065 | |
| 1066 | static int check_subprogs(struct bpf_verifier_env *env) |
| 1067 | { |
| 1068 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1069 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1070 | struct bpf_insn *insn = env->prog->insnsi; |
| 1071 | int insn_cnt = env->prog->len; |
| 1072 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 1073 | /* Add entry function. */ |
| 1074 | ret = add_subprog(env, 0); |
| 1075 | if (ret < 0) |
| 1076 | return ret; |
| 1077 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1078 | /* determine subprog starts. The end is one before the next starts */ |
| 1079 | for (i = 0; i < insn_cnt; i++) { |
| 1080 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1081 | continue; |
| 1082 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1083 | continue; |
| 1084 | if (!env->allow_ptr_leaks) { |
| 1085 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); |
| 1086 | return -EPERM; |
| 1087 | } |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1088 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 1089 | if (ret < 0) |
| 1090 | return ret; |
| 1091 | } |
| 1092 | |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1093 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 1094 | * logic. 'subprog_cnt' should not be increased. |
| 1095 | */ |
| 1096 | subprog[env->subprog_cnt].start = insn_cnt; |
| 1097 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1098 | if (env->log.level & BPF_LOG_LEVEL2) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1099 | for (i = 0; i < env->subprog_cnt; i++) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1100 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1101 | |
| 1102 | /* now check that all jumps are within the same subprog */ |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1103 | subprog_start = subprog[cur_subprog].start; |
| 1104 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1105 | for (i = 0; i < insn_cnt; i++) { |
| 1106 | u8 code = insn[i].code; |
| 1107 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 1108 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1109 | goto next; |
| 1110 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 1111 | goto next; |
| 1112 | off = i + insn[i].off + 1; |
| 1113 | if (off < subprog_start || off >= subprog_end) { |
| 1114 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 1115 | return -EINVAL; |
| 1116 | } |
| 1117 | next: |
| 1118 | if (i == subprog_end - 1) { |
| 1119 | /* to avoid fall-through from one subprog into another |
| 1120 | * the last insn of the subprog should be either exit |
| 1121 | * or unconditional jump back |
| 1122 | */ |
| 1123 | if (code != (BPF_JMP | BPF_EXIT) && |
| 1124 | code != (BPF_JMP | BPF_JA)) { |
| 1125 | verbose(env, "last insn is not an exit or jmp\n"); |
| 1126 | return -EINVAL; |
| 1127 | } |
| 1128 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1129 | cur_subprog++; |
| 1130 | if (cur_subprog < env->subprog_cnt) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1131 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1137 | /* Parentage chain of this register (or stack slot) should take care of all |
| 1138 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 1139 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1140 | static int mark_reg_read(struct bpf_verifier_env *env, |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1141 | const struct bpf_reg_state *state, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1142 | struct bpf_reg_state *parent, u8 flag) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1143 | { |
| 1144 | bool writes = parent == state->parent; /* Observe write marks */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1145 | int cnt = 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1146 | |
| 1147 | while (parent) { |
| 1148 | /* if read wasn't screened by an earlier write ... */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1149 | if (writes && state->live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1150 | break; |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 1151 | if (parent->live & REG_LIVE_DONE) { |
| 1152 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", |
| 1153 | reg_type_str[parent->type], |
| 1154 | parent->var_off.value, parent->off); |
| 1155 | return -EFAULT; |
| 1156 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1157 | /* The first condition is more likely to be true than the |
| 1158 | * second, checked it first. |
| 1159 | */ |
| 1160 | if ((parent->live & REG_LIVE_READ) == flag || |
| 1161 | parent->live & REG_LIVE_READ64) |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 1162 | /* The parentage chain never changes and |
| 1163 | * this parent was already marked as LIVE_READ. |
| 1164 | * There is no need to keep walking the chain again and |
| 1165 | * keep re-marking all parents as LIVE_READ. |
| 1166 | * This case happens when the same register is read |
| 1167 | * multiple times without writes into it in-between. |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1168 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
| 1169 | * then no need to set the weak REG_LIVE_READ32. |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 1170 | */ |
| 1171 | break; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1172 | /* ... then we depend on parent's value */ |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1173 | parent->live |= flag; |
| 1174 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ |
| 1175 | if (flag == REG_LIVE_READ64) |
| 1176 | parent->live &= ~REG_LIVE_READ32; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1177 | state = parent; |
| 1178 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1179 | writes = true; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1180 | cnt++; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1181 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1182 | |
| 1183 | if (env->longest_mark_read_walk < cnt) |
| 1184 | env->longest_mark_read_walk = cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1185 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1186 | } |
| 1187 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1188 | /* This function is supposed to be used by the following 32-bit optimization |
| 1189 | * code only. It returns TRUE if the source or destination register operates |
| 1190 | * on 64-bit, otherwise return FALSE. |
| 1191 | */ |
| 1192 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 1193 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) |
| 1194 | { |
| 1195 | u8 code, class, op; |
| 1196 | |
| 1197 | code = insn->code; |
| 1198 | class = BPF_CLASS(code); |
| 1199 | op = BPF_OP(code); |
| 1200 | if (class == BPF_JMP) { |
| 1201 | /* BPF_EXIT for "main" will reach here. Return TRUE |
| 1202 | * conservatively. |
| 1203 | */ |
| 1204 | if (op == BPF_EXIT) |
| 1205 | return true; |
| 1206 | if (op == BPF_CALL) { |
| 1207 | /* BPF to BPF call will reach here because of marking |
| 1208 | * caller saved clobber with DST_OP_NO_MARK for which we |
| 1209 | * don't care the register def because they are anyway |
| 1210 | * marked as NOT_INIT already. |
| 1211 | */ |
| 1212 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 1213 | return false; |
| 1214 | /* Helper call will reach here because of arg type |
| 1215 | * check, conservatively return TRUE. |
| 1216 | */ |
| 1217 | if (t == SRC_OP) |
| 1218 | return true; |
| 1219 | |
| 1220 | return false; |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | if (class == BPF_ALU64 || class == BPF_JMP || |
| 1225 | /* BPF_END always use BPF_ALU class. */ |
| 1226 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) |
| 1227 | return true; |
| 1228 | |
| 1229 | if (class == BPF_ALU || class == BPF_JMP32) |
| 1230 | return false; |
| 1231 | |
| 1232 | if (class == BPF_LDX) { |
| 1233 | if (t != SRC_OP) |
| 1234 | return BPF_SIZE(code) == BPF_DW; |
| 1235 | /* LDX source must be ptr. */ |
| 1236 | return true; |
| 1237 | } |
| 1238 | |
| 1239 | if (class == BPF_STX) { |
| 1240 | if (reg->type != SCALAR_VALUE) |
| 1241 | return true; |
| 1242 | return BPF_SIZE(code) == BPF_DW; |
| 1243 | } |
| 1244 | |
| 1245 | if (class == BPF_LD) { |
| 1246 | u8 mode = BPF_MODE(code); |
| 1247 | |
| 1248 | /* LD_IMM64 */ |
| 1249 | if (mode == BPF_IMM) |
| 1250 | return true; |
| 1251 | |
| 1252 | /* Both LD_IND and LD_ABS return 32-bit data. */ |
| 1253 | if (t != SRC_OP) |
| 1254 | return false; |
| 1255 | |
| 1256 | /* Implicit ctx ptr. */ |
| 1257 | if (regno == BPF_REG_6) |
| 1258 | return true; |
| 1259 | |
| 1260 | /* Explicit source could be any width. */ |
| 1261 | return true; |
| 1262 | } |
| 1263 | |
| 1264 | if (class == BPF_ST) |
| 1265 | /* The only source register for BPF_ST is a ptr. */ |
| 1266 | return true; |
| 1267 | |
| 1268 | /* Conservatively return true at default. */ |
| 1269 | return true; |
| 1270 | } |
| 1271 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 1272 | /* Return TRUE if INSN doesn't have explicit value define. */ |
| 1273 | static bool insn_no_def(struct bpf_insn *insn) |
| 1274 | { |
| 1275 | u8 class = BPF_CLASS(insn->code); |
| 1276 | |
| 1277 | return (class == BPF_JMP || class == BPF_JMP32 || |
| 1278 | class == BPF_STX || class == BPF_ST); |
| 1279 | } |
| 1280 | |
| 1281 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ |
| 1282 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 1283 | { |
| 1284 | if (insn_no_def(insn)) |
| 1285 | return false; |
| 1286 | |
| 1287 | return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP); |
| 1288 | } |
| 1289 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1290 | static void mark_insn_zext(struct bpf_verifier_env *env, |
| 1291 | struct bpf_reg_state *reg) |
| 1292 | { |
| 1293 | s32 def_idx = reg->subreg_def; |
| 1294 | |
| 1295 | if (def_idx == DEF_NOT_SUBREG) |
| 1296 | return; |
| 1297 | |
| 1298 | env->insn_aux_data[def_idx - 1].zext_dst = true; |
| 1299 | /* The dst will be zero extended, so won't be sub-register anymore. */ |
| 1300 | reg->subreg_def = DEF_NOT_SUBREG; |
| 1301 | } |
| 1302 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1303 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1304 | enum reg_arg_type t) |
| 1305 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1306 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1307 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1308 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1309 | struct bpf_reg_state *reg, *regs = state->regs; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1310 | bool rw64; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1311 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1312 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1313 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1314 | return -EINVAL; |
| 1315 | } |
| 1316 | |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1317 | reg = ®s[regno]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1318 | rw64 = is_reg64(env, insn, regno, reg, t); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1319 | if (t == SRC_OP) { |
| 1320 | /* check whether register used as source operand can be read */ |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1321 | if (reg->type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1322 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1323 | return -EACCES; |
| 1324 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1325 | /* 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] | 1326 | if (regno == BPF_REG_FP) |
| 1327 | return 0; |
| 1328 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1329 | if (rw64) |
| 1330 | mark_insn_zext(env, reg); |
| 1331 | |
| 1332 | return mark_reg_read(env, reg, reg->parent, |
| 1333 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1334 | } else { |
| 1335 | /* check whether register used as dest operand can be written to */ |
| 1336 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1337 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1338 | return -EACCES; |
| 1339 | } |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 1340 | reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1341 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1342 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1343 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1344 | } |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1348 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 1349 | { |
| 1350 | switch (type) { |
| 1351 | case PTR_TO_MAP_VALUE: |
| 1352 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 1353 | case PTR_TO_STACK: |
| 1354 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1355 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1356 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1357 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1358 | case PTR_TO_FLOW_KEYS: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1359 | case CONST_PTR_TO_MAP: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1360 | case PTR_TO_SOCKET: |
| 1361 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1362 | case PTR_TO_SOCK_COMMON: |
| 1363 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 1364 | case PTR_TO_TCP_SOCK: |
| 1365 | case PTR_TO_TCP_SOCK_OR_NULL: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1366 | return true; |
| 1367 | default: |
| 1368 | return false; |
| 1369 | } |
| 1370 | } |
| 1371 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1372 | /* Does this register contain a constant zero? */ |
| 1373 | static bool register_is_null(struct bpf_reg_state *reg) |
| 1374 | { |
| 1375 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 1376 | } |
| 1377 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1378 | /* check_stack_read/write functions track spill/fill of registers, |
| 1379 | * stack boundary and alignment are checked in check_mem_access() |
| 1380 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1381 | static int check_stack_write(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1382 | struct bpf_func_state *state, /* func where register points to */ |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1383 | int off, int size, int value_regno, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1384 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1385 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1386 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1387 | enum bpf_reg_type type; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1388 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1389 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 1390 | state->acquired_refs, true); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1391 | if (err) |
| 1392 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1393 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 1394 | * so it's aligned access and [off, off + size) are within stack limits |
| 1395 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1396 | if (!env->allow_ptr_leaks && |
| 1397 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 1398 | size != BPF_REG_SIZE) { |
| 1399 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 1400 | return -EACCES; |
| 1401 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1402 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1403 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1404 | if (value_regno >= 0 && |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1405 | is_spillable_regtype((type = cur->regs[value_regno].type))) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1406 | |
| 1407 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1408 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1409 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1410 | return -EACCES; |
| 1411 | } |
| 1412 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1413 | if (state != cur && type == PTR_TO_STACK) { |
| 1414 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 1415 | return -EINVAL; |
| 1416 | } |
| 1417 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1418 | /* save register state */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1419 | state->stack[spi].spilled_ptr = cur->regs[value_regno]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1420 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1421 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1422 | for (i = 0; i < BPF_REG_SIZE; i++) { |
| 1423 | if (state->stack[spi].slot_type[i] == STACK_MISC && |
| 1424 | !env->allow_ptr_leaks) { |
| 1425 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
| 1426 | int soff = (-spi - 1) * BPF_REG_SIZE; |
| 1427 | |
| 1428 | /* detected reuse of integer stack slot with a pointer |
| 1429 | * which means either llvm is reusing stack slot or |
| 1430 | * an attacker is trying to exploit CVE-2018-3639 |
| 1431 | * (speculative store bypass) |
| 1432 | * Have to sanitize that slot with preemptive |
| 1433 | * store of zero. |
| 1434 | */ |
| 1435 | if (*poff && *poff != soff) { |
| 1436 | /* disallow programs where single insn stores |
| 1437 | * into two different stack slots, since verifier |
| 1438 | * cannot sanitize them |
| 1439 | */ |
| 1440 | verbose(env, |
| 1441 | "insn %d cannot access two stack slots fp%d and fp%d", |
| 1442 | insn_idx, *poff, soff); |
| 1443 | return -EINVAL; |
| 1444 | } |
| 1445 | *poff = soff; |
| 1446 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1447 | state->stack[spi].slot_type[i] = STACK_SPILL; |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1448 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1449 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1450 | u8 type = STACK_MISC; |
| 1451 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1452 | /* regular write of data into stack destroys any spilled ptr */ |
| 1453 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 1454 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
| 1455 | if (state->stack[spi].slot_type[0] == STACK_SPILL) |
| 1456 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 1457 | state->stack[spi].slot_type[i] = STACK_MISC; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1458 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1459 | /* only mark the slot as written if all 8 bytes were written |
| 1460 | * otherwise read propagation may incorrectly stop too soon |
| 1461 | * when stack slots are partially written. |
| 1462 | * This heuristic means that read propagation will be |
| 1463 | * conservative, since it will add reg_live_read marks |
| 1464 | * to stack slots all the way to first state when programs |
| 1465 | * writes+reads less than 8 bytes |
| 1466 | */ |
| 1467 | if (size == BPF_REG_SIZE) |
| 1468 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 1469 | |
| 1470 | /* when we zero initialize stack slots mark them as such */ |
| 1471 | if (value_regno >= 0 && |
| 1472 | register_is_null(&cur->regs[value_regno])) |
| 1473 | type = STACK_ZERO; |
| 1474 | |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 1475 | /* Mark slots affected by this stack write. */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1476 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1477 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1478 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1479 | } |
| 1480 | return 0; |
| 1481 | } |
| 1482 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1483 | static int check_stack_read(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1484 | struct bpf_func_state *reg_state /* func where register points to */, |
| 1485 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1486 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1487 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1488 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1489 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
| 1490 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1491 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1492 | if (reg_state->allocated_stack <= slot) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1493 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 1494 | off, size); |
| 1495 | return -EACCES; |
| 1496 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1497 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1498 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1499 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1500 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1501 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1502 | return -EACCES; |
| 1503 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1504 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1505 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1506 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1507 | return -EACCES; |
| 1508 | } |
| 1509 | } |
| 1510 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1511 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1512 | /* restore register state from stack */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1513 | state->regs[value_regno] = reg_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 1514 | /* mark reg as written since spilled pointer state likely |
| 1515 | * has its liveness marks cleared by is_state_visited() |
| 1516 | * which resets stack/reg liveness for state transitions |
| 1517 | */ |
| 1518 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1519 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1520 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1521 | reg_state->stack[spi].spilled_ptr.parent, |
| 1522 | REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1523 | return 0; |
| 1524 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1525 | int zeros = 0; |
| 1526 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1527 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1528 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 1529 | continue; |
| 1530 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 1531 | zeros++; |
| 1532 | continue; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1533 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1534 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 1535 | off, i, size); |
| 1536 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1537 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1538 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1539 | reg_state->stack[spi].spilled_ptr.parent, |
| 1540 | REG_LIVE_READ64); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1541 | if (value_regno >= 0) { |
| 1542 | if (zeros == size) { |
| 1543 | /* any size read into register is zero extended, |
| 1544 | * so the whole register == const_zero |
| 1545 | */ |
| 1546 | __mark_reg_const_zero(&state->regs[value_regno]); |
| 1547 | } else { |
| 1548 | /* have read misc data from the stack */ |
| 1549 | mark_reg_unknown(env, state->regs, value_regno); |
| 1550 | } |
| 1551 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 1552 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1553 | return 0; |
| 1554 | } |
| 1555 | } |
| 1556 | |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 1557 | static int check_stack_access(struct bpf_verifier_env *env, |
| 1558 | const struct bpf_reg_state *reg, |
| 1559 | int off, int size) |
| 1560 | { |
| 1561 | /* Stack accesses must be at a fixed offset, so that we |
| 1562 | * can determine what type of data were returned. See |
| 1563 | * check_stack_read(). |
| 1564 | */ |
| 1565 | if (!tnum_is_const(reg->var_off)) { |
| 1566 | char tn_buf[48]; |
| 1567 | |
| 1568 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Andrey Ignatov | 1fbd20f | 2019-04-03 23:22:43 -0700 | [diff] [blame] | 1569 | 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] | 1570 | tn_buf, off, size); |
| 1571 | return -EACCES; |
| 1572 | } |
| 1573 | |
| 1574 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 1575 | verbose(env, "invalid stack off=%d size=%d\n", off, size); |
| 1576 | return -EACCES; |
| 1577 | } |
| 1578 | |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 1582 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
| 1583 | int off, int size, enum bpf_access_type type) |
| 1584 | { |
| 1585 | struct bpf_reg_state *regs = cur_regs(env); |
| 1586 | struct bpf_map *map = regs[regno].map_ptr; |
| 1587 | u32 cap = bpf_map_flags_to_cap(map); |
| 1588 | |
| 1589 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { |
| 1590 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", |
| 1591 | map->value_size, off, size); |
| 1592 | return -EACCES; |
| 1593 | } |
| 1594 | |
| 1595 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { |
| 1596 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", |
| 1597 | map->value_size, off, size); |
| 1598 | return -EACCES; |
| 1599 | } |
| 1600 | |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1604 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1605 | 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] | 1606 | int size, bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1607 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1608 | struct bpf_reg_state *regs = cur_regs(env); |
| 1609 | struct bpf_map *map = regs[regno].map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1610 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1611 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1612 | off + size > map->value_size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1613 | 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] | 1614 | map->value_size, off, size); |
| 1615 | return -EACCES; |
| 1616 | } |
| 1617 | return 0; |
| 1618 | } |
| 1619 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1620 | /* check read/write into a map element with possible variable offset */ |
| 1621 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1622 | int off, int size, bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1623 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1624 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1625 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1626 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 1627 | int err; |
| 1628 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1629 | /* We may have adjusted the register to this map value, so we |
| 1630 | * need to try adding each of min_value and max_value to off |
| 1631 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1632 | */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 1633 | if (env->log.level & BPF_LOG_LEVEL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1634 | print_verifier_state(env, state); |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 1635 | |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1636 | /* The minimum value is only important with signed |
| 1637 | * comparisons where we can't assume the floor of a |
| 1638 | * value is 0. If we are using signed variables for our |
| 1639 | * index'es we need to make sure that whatever we use |
| 1640 | * will have a set floor within our range. |
| 1641 | */ |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 1642 | if (reg->smin_value < 0 && |
| 1643 | (reg->smin_value == S64_MIN || |
| 1644 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || |
| 1645 | reg->smin_value + off < 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1646 | 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] | 1647 | regno); |
| 1648 | return -EACCES; |
| 1649 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1650 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
| 1651 | zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1652 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1653 | verbose(env, "R%d min value is outside of the array range\n", |
| 1654 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1655 | return err; |
| 1656 | } |
| 1657 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1658 | /* If we haven't set a max value then we need to bail since we can't be |
| 1659 | * sure we won't do bad things. |
| 1660 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1661 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1662 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1663 | 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] | 1664 | regno); |
| 1665 | return -EACCES; |
| 1666 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1667 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
| 1668 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1669 | if (err) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1670 | verbose(env, "R%d max value is outside of the array range\n", |
| 1671 | regno); |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 1672 | |
| 1673 | if (map_value_has_spin_lock(reg->map_ptr)) { |
| 1674 | u32 lock = reg->map_ptr->spin_lock_off; |
| 1675 | |
| 1676 | /* if any part of struct bpf_spin_lock can be touched by |
| 1677 | * load/store reject this program. |
| 1678 | * To check that [x1, x2) overlaps with [y1, y2) |
| 1679 | * it is sufficient to check x1 < y2 && y1 < x2. |
| 1680 | */ |
| 1681 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && |
| 1682 | lock < reg->umax_value + off + size) { |
| 1683 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); |
| 1684 | return -EACCES; |
| 1685 | } |
| 1686 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1687 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1688 | } |
| 1689 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1690 | #define MAX_PACKET_OFF 0xffff |
| 1691 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1692 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1693 | const struct bpf_call_arg_meta *meta, |
| 1694 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1695 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1696 | switch (env->prog->type) { |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1697 | /* Program types only with direct read access go here! */ |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1698 | case BPF_PROG_TYPE_LWT_IN: |
| 1699 | case BPF_PROG_TYPE_LWT_OUT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 1700 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 1701 | case BPF_PROG_TYPE_SK_REUSEPORT: |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1702 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Daniel Borkmann | d5563d3 | 2018-10-24 22:05:46 +0200 | [diff] [blame] | 1703 | case BPF_PROG_TYPE_CGROUP_SKB: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1704 | if (t == BPF_WRITE) |
| 1705 | return false; |
Alexander Alemayhu | 7e57fbb | 2017-02-14 00:02:35 +0100 | [diff] [blame] | 1706 | /* fallthrough */ |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1707 | |
| 1708 | /* Program types with direct read + write access go here! */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1709 | case BPF_PROG_TYPE_SCHED_CLS: |
| 1710 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1711 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1712 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 1713 | case BPF_PROG_TYPE_SK_SKB: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1714 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1715 | if (meta) |
| 1716 | return meta->pkt_access; |
| 1717 | |
| 1718 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1719 | return true; |
| 1720 | default: |
| 1721 | return false; |
| 1722 | } |
| 1723 | } |
| 1724 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1725 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1726 | int off, int size, bool zero_size_allowed) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1727 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1728 | struct bpf_reg_state *regs = cur_regs(env); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1729 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1730 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1731 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1732 | (u64)off + size > reg->range) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1733 | 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] | 1734 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1735 | return -EACCES; |
| 1736 | } |
| 1737 | return 0; |
| 1738 | } |
| 1739 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1740 | 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] | 1741 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1742 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1743 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1744 | struct bpf_reg_state *reg = ®s[regno]; |
| 1745 | int err; |
| 1746 | |
| 1747 | /* We may have added a variable offset to the packet pointer; but any |
| 1748 | * reg->range we have comes after that. We are only checking the fixed |
| 1749 | * offset. |
| 1750 | */ |
| 1751 | |
| 1752 | /* We don't allow negative numbers, because we aren't tracking enough |
| 1753 | * detail to prove they're safe. |
| 1754 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1755 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1756 | 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] | 1757 | regno); |
| 1758 | return -EACCES; |
| 1759 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1760 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1761 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1762 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1763 | return err; |
| 1764 | } |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 1765 | |
| 1766 | /* __check_packet_access has made sure "off + size - 1" is within u16. |
| 1767 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 1768 | * otherwise find_good_pkt_pointers would have refused to set range info |
| 1769 | * that __check_packet_access would have rejected this pkt access. |
| 1770 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 1771 | */ |
| 1772 | env->prog->aux->max_pkt_offset = |
| 1773 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 1774 | off + reg->umax_value + size - 1); |
| 1775 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1776 | return err; |
| 1777 | } |
| 1778 | |
| 1779 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1780 | 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] | 1781 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1782 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1783 | struct bpf_insn_access_aux info = { |
| 1784 | .reg_type = *reg_type, |
| 1785 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1786 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1787 | if (env->ops->is_valid_access && |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1788 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1789 | /* A non zero info.ctx_field_size indicates that this field is a |
| 1790 | * candidate for later verifier transformation to load the whole |
| 1791 | * field and then apply a mask when accessed with a narrower |
| 1792 | * access than actual ctx access size. A zero info.ctx_field_size |
| 1793 | * will only allow for whole field access and rejects any other |
| 1794 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1795 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 1796 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1797 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1798 | 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] | 1799 | /* remember the offset of last byte accessed in ctx */ |
| 1800 | if (env->prog->aux->max_ctx_offset < off + size) |
| 1801 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1802 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 1803 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1804 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1805 | 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] | 1806 | return -EACCES; |
| 1807 | } |
| 1808 | |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1809 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 1810 | int size) |
| 1811 | { |
| 1812 | if (size < 0 || off < 0 || |
| 1813 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 1814 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 1815 | off, size); |
| 1816 | return -EACCES; |
| 1817 | } |
| 1818 | return 0; |
| 1819 | } |
| 1820 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 1821 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
| 1822 | u32 regno, int off, int size, |
| 1823 | enum bpf_access_type t) |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1824 | { |
| 1825 | struct bpf_reg_state *regs = cur_regs(env); |
| 1826 | struct bpf_reg_state *reg = ®s[regno]; |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 1827 | struct bpf_insn_access_aux info = {}; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1828 | bool valid; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1829 | |
| 1830 | if (reg->smin_value < 0) { |
| 1831 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 1832 | regno); |
| 1833 | return -EACCES; |
| 1834 | } |
| 1835 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1836 | switch (reg->type) { |
| 1837 | case PTR_TO_SOCK_COMMON: |
| 1838 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); |
| 1839 | break; |
| 1840 | case PTR_TO_SOCKET: |
| 1841 | valid = bpf_sock_is_valid_access(off, size, t, &info); |
| 1842 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 1843 | case PTR_TO_TCP_SOCK: |
| 1844 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); |
| 1845 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1846 | default: |
| 1847 | valid = false; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 1850 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1851 | if (valid) { |
| 1852 | env->insn_aux_data[insn_idx].ctx_field_size = |
| 1853 | info.ctx_field_size; |
| 1854 | return 0; |
| 1855 | } |
| 1856 | |
| 1857 | verbose(env, "R%d invalid %s access off=%d size=%d\n", |
| 1858 | regno, reg_type_str[reg->type], off, size); |
| 1859 | |
| 1860 | return -EACCES; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1863 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 1864 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1865 | { |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1866 | if (allow_ptr_leaks) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1867 | return false; |
| 1868 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1869 | return reg->type != SCALAR_VALUE; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1872 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
| 1873 | { |
| 1874 | return cur_regs(env) + regno; |
| 1875 | } |
| 1876 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1877 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 1878 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1879 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1880 | } |
| 1881 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1882 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 1883 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1884 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1885 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 1886 | return reg->type == PTR_TO_CTX; |
| 1887 | } |
| 1888 | |
| 1889 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) |
| 1890 | { |
| 1891 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 1892 | |
| 1893 | return type_is_sk_pointer(reg->type); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1894 | } |
| 1895 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1896 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 1897 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1898 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1899 | |
| 1900 | return type_is_pkt_pointer(reg->type); |
| 1901 | } |
| 1902 | |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 1903 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 1904 | { |
| 1905 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 1906 | |
| 1907 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 1908 | return reg->type == PTR_TO_FLOW_KEYS; |
| 1909 | } |
| 1910 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1911 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 1912 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1913 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1914 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1915 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1916 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1917 | |
| 1918 | /* Byte size accesses are always allowed. */ |
| 1919 | if (!strict || size == 1) |
| 1920 | return 0; |
| 1921 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1922 | /* For platforms that do not have a Kconfig enabling |
| 1923 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 1924 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 1925 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 1926 | * to this code only in strict mode where we want to emulate |
| 1927 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 1928 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1929 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1930 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1931 | |
| 1932 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 1933 | if (!tnum_is_aligned(reg_off, size)) { |
| 1934 | char tn_buf[48]; |
| 1935 | |
| 1936 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1937 | verbose(env, |
| 1938 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1939 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1940 | return -EACCES; |
| 1941 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1942 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1943 | return 0; |
| 1944 | } |
| 1945 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1946 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 1947 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1948 | const char *pointer_desc, |
| 1949 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1950 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1951 | struct tnum reg_off; |
| 1952 | |
| 1953 | /* Byte size accesses are always allowed. */ |
| 1954 | if (!strict || size == 1) |
| 1955 | return 0; |
| 1956 | |
| 1957 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 1958 | if (!tnum_is_aligned(reg_off, size)) { |
| 1959 | char tn_buf[48]; |
| 1960 | |
| 1961 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1962 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1963 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1964 | return -EACCES; |
| 1965 | } |
| 1966 | |
| 1967 | return 0; |
| 1968 | } |
| 1969 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1970 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1971 | const struct bpf_reg_state *reg, int off, |
| 1972 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1973 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1974 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1975 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1976 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1977 | switch (reg->type) { |
| 1978 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1979 | case PTR_TO_PACKET_META: |
| 1980 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 1981 | * right in front, treat it the very same way. |
| 1982 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1983 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1984 | case PTR_TO_FLOW_KEYS: |
| 1985 | pointer_desc = "flow keys "; |
| 1986 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1987 | case PTR_TO_MAP_VALUE: |
| 1988 | pointer_desc = "value "; |
| 1989 | break; |
| 1990 | case PTR_TO_CTX: |
| 1991 | pointer_desc = "context "; |
| 1992 | break; |
| 1993 | case PTR_TO_STACK: |
| 1994 | pointer_desc = "stack "; |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 1995 | /* The stack spill tracking logic in check_stack_write() |
| 1996 | * and check_stack_read() relies on stack accesses being |
| 1997 | * aligned. |
| 1998 | */ |
| 1999 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2000 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2001 | case PTR_TO_SOCKET: |
| 2002 | pointer_desc = "sock "; |
| 2003 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2004 | case PTR_TO_SOCK_COMMON: |
| 2005 | pointer_desc = "sock_common "; |
| 2006 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2007 | case PTR_TO_TCP_SOCK: |
| 2008 | pointer_desc = "tcp_sock "; |
| 2009 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2010 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2011 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2012 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2013 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 2014 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 2015 | } |
| 2016 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2017 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 2018 | const struct bpf_func_state *func, |
| 2019 | int off) |
| 2020 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2021 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2022 | |
| 2023 | if (stack >= -off) |
| 2024 | return 0; |
| 2025 | |
| 2026 | /* update known max for given subprogram */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2027 | env->subprog_info[func->subprogno].stack_depth = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2028 | return 0; |
| 2029 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2030 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2031 | /* starting from main bpf function walk all instructions of the function |
| 2032 | * and recursively walk all callees that given function can call. |
| 2033 | * Ignore jump and exit insns. |
| 2034 | * Since recursion is prevented by check_cfg() this algorithm |
| 2035 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 2036 | */ |
| 2037 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 2038 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2039 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 2040 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2041 | struct bpf_insn *insn = env->prog->insnsi; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2042 | int ret_insn[MAX_CALL_FRAMES]; |
| 2043 | int ret_prog[MAX_CALL_FRAMES]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2044 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2045 | process_func: |
| 2046 | /* round up to 32-bytes, since this is granularity |
| 2047 | * of interpreter stack size |
| 2048 | */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2049 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2050 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2051 | 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] | 2052 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2053 | return -EACCES; |
| 2054 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2055 | continue_func: |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 2056 | subprog_end = subprog[idx + 1].start; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2057 | for (; i < subprog_end; i++) { |
| 2058 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 2059 | continue; |
| 2060 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 2061 | continue; |
| 2062 | /* remember insn and function to return to */ |
| 2063 | ret_insn[frame] = i + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2064 | ret_prog[frame] = idx; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2065 | |
| 2066 | /* find the callee */ |
| 2067 | i = i + insn[i].imm + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2068 | idx = find_subprog(env, i); |
| 2069 | if (idx < 0) { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2070 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 2071 | i); |
| 2072 | return -EFAULT; |
| 2073 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2074 | frame++; |
| 2075 | if (frame >= MAX_CALL_FRAMES) { |
Paul Chaignon | 927cb78 | 2019-03-20 13:58:27 +0100 | [diff] [blame] | 2076 | verbose(env, "the call stack of %d frames is too deep !\n", |
| 2077 | frame); |
| 2078 | return -E2BIG; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2079 | } |
| 2080 | goto process_func; |
| 2081 | } |
| 2082 | /* end of for() loop means the last insn of the 'subprog' |
| 2083 | * was reached. Doesn't matter whether it was JA or EXIT |
| 2084 | */ |
| 2085 | if (frame == 0) |
| 2086 | return 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2087 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2088 | frame--; |
| 2089 | i = ret_insn[frame]; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2090 | idx = ret_prog[frame]; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 2091 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2092 | } |
| 2093 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 2094 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 2095 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 2096 | const struct bpf_insn *insn, int idx) |
| 2097 | { |
| 2098 | int start = idx + insn->imm + 1, subprog; |
| 2099 | |
| 2100 | subprog = find_subprog(env, start); |
| 2101 | if (subprog < 0) { |
| 2102 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 2103 | start); |
| 2104 | return -EFAULT; |
| 2105 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2106 | return env->subprog_info[subprog].stack_depth; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 2107 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 2108 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 2109 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 2110 | static int check_ctx_reg(struct bpf_verifier_env *env, |
| 2111 | const struct bpf_reg_state *reg, int regno) |
| 2112 | { |
| 2113 | /* Access to ctx or passing it to a helper is only allowed in |
| 2114 | * its original, unmodified form. |
| 2115 | */ |
| 2116 | |
| 2117 | if (reg->off) { |
| 2118 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 2119 | regno, reg->off); |
| 2120 | return -EACCES; |
| 2121 | } |
| 2122 | |
| 2123 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 2124 | char tn_buf[48]; |
| 2125 | |
| 2126 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2127 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 2128 | return -EACCES; |
| 2129 | } |
| 2130 | |
| 2131 | return 0; |
| 2132 | } |
| 2133 | |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 2134 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
| 2135 | const struct bpf_reg_state *reg, |
| 2136 | int regno, int off, int size) |
| 2137 | { |
| 2138 | if (off < 0) { |
| 2139 | verbose(env, |
| 2140 | "R%d invalid tracepoint buffer access: off=%d, size=%d", |
| 2141 | regno, off, size); |
| 2142 | return -EACCES; |
| 2143 | } |
| 2144 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 2145 | char tn_buf[48]; |
| 2146 | |
| 2147 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2148 | verbose(env, |
| 2149 | "R%d invalid variable buffer offset: off=%d, var_off=%s", |
| 2150 | regno, off, tn_buf); |
| 2151 | return -EACCES; |
| 2152 | } |
| 2153 | if (off + size > env->prog->aux->max_tp_access) |
| 2154 | env->prog->aux->max_tp_access = off + size; |
| 2155 | |
| 2156 | return 0; |
| 2157 | } |
| 2158 | |
| 2159 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 2160 | /* truncate register to smaller size (in bytes) |
| 2161 | * must be called with size < BPF_REG_SIZE |
| 2162 | */ |
| 2163 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 2164 | { |
| 2165 | u64 mask; |
| 2166 | |
| 2167 | /* clear high bits in bit representation */ |
| 2168 | reg->var_off = tnum_cast(reg->var_off, size); |
| 2169 | |
| 2170 | /* fix arithmetic bounds */ |
| 2171 | mask = ((u64)1 << (size * 8)) - 1; |
| 2172 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 2173 | reg->umin_value &= mask; |
| 2174 | reg->umax_value &= mask; |
| 2175 | } else { |
| 2176 | reg->umin_value = 0; |
| 2177 | reg->umax_value = mask; |
| 2178 | } |
| 2179 | reg->smin_value = reg->umin_value; |
| 2180 | reg->smax_value = reg->umax_value; |
| 2181 | } |
| 2182 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2183 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 2184 | * if t==write, value_regno is a register which value is stored into memory |
| 2185 | * if t==read, value_regno is a register which will receive the value from memory |
| 2186 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 2187 | * if t==read && value_regno==-1, don't care what we read from memory |
| 2188 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2189 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 2190 | int off, int bpf_size, enum bpf_access_type t, |
| 2191 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2192 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2193 | struct bpf_reg_state *regs = cur_regs(env); |
| 2194 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2195 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2196 | int size, err = 0; |
| 2197 | |
| 2198 | size = bpf_size_to_bytes(bpf_size); |
| 2199 | if (size < 0) |
| 2200 | return size; |
| 2201 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2202 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2203 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2204 | if (err) |
| 2205 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2206 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2207 | /* for access checks, reg->off is just part of off */ |
| 2208 | off += reg->off; |
| 2209 | |
| 2210 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2211 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2212 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2213 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2214 | return -EACCES; |
| 2215 | } |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 2216 | err = check_map_access_type(env, regno, off, size, t); |
| 2217 | if (err) |
| 2218 | return err; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2219 | err = check_map_access(env, regno, off, size, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2220 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2221 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2222 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2223 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2224 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 2225 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2226 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2227 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2228 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2229 | return -EACCES; |
| 2230 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2231 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 2232 | err = check_ctx_reg(env, reg, regno); |
| 2233 | if (err < 0) |
| 2234 | return err; |
| 2235 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2236 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2237 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2238 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2239 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 2240 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2241 | */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2242 | if (reg_type == SCALAR_VALUE) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2243 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2244 | } else { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2245 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2246 | value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2247 | if (reg_type_may_be_null(reg_type)) |
| 2248 | regs[value_regno].id = ++env->id_gen; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2249 | /* A load of ctx field could have different |
| 2250 | * actual load size with the one encoded in the |
| 2251 | * insn. When the dst is PTR, it is for sure not |
| 2252 | * a sub-register. |
| 2253 | */ |
| 2254 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2255 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2256 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2257 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2258 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2259 | } else if (reg->type == PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2260 | off += reg->var_off.value; |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 2261 | err = check_stack_access(env, reg, off, size); |
| 2262 | if (err) |
| 2263 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 2264 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2265 | state = func(env, reg); |
| 2266 | err = update_stack_depth(env, state, off); |
| 2267 | if (err) |
| 2268 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 2269 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2270 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2271 | err = check_stack_write(env, state, off, size, |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 2272 | value_regno, insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2273 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2274 | err = check_stack_read(env, state, off, size, |
| 2275 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2276 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2277 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2278 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2279 | return -EACCES; |
| 2280 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2281 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2282 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2283 | verbose(env, "R%d leaks addr into packet\n", |
| 2284 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2285 | return -EACCES; |
| 2286 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2287 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2288 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2289 | mark_reg_unknown(env, regs, value_regno); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2290 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 2291 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2292 | is_pointer_value(env, value_regno)) { |
| 2293 | verbose(env, "R%d leaks addr into flow keys\n", |
| 2294 | value_regno); |
| 2295 | return -EACCES; |
| 2296 | } |
| 2297 | |
| 2298 | err = check_flow_keys_access(env, off, size); |
| 2299 | if (!err && t == BPF_READ && value_regno >= 0) |
| 2300 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2301 | } else if (type_is_sk_pointer(reg->type)) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2302 | if (t == BPF_WRITE) { |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2303 | verbose(env, "R%d cannot write into %s\n", |
| 2304 | regno, reg_type_str[reg->type]); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2305 | return -EACCES; |
| 2306 | } |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 2307 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2308 | if (!err && value_regno >= 0) |
| 2309 | mark_reg_unknown(env, regs, value_regno); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 2310 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
| 2311 | err = check_tp_buffer_access(env, reg, regno, off, size); |
| 2312 | if (!err && t == BPF_READ && value_regno >= 0) |
| 2313 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2314 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2315 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 2316 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2317 | return -EACCES; |
| 2318 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2319 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2320 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2321 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2322 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 2323 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2324 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2325 | return err; |
| 2326 | } |
| 2327 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2328 | 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] | 2329 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2330 | int err; |
| 2331 | |
| 2332 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 2333 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2334 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2335 | return -EINVAL; |
| 2336 | } |
| 2337 | |
| 2338 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2339 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2340 | if (err) |
| 2341 | return err; |
| 2342 | |
| 2343 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2344 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2345 | if (err) |
| 2346 | return err; |
| 2347 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 2348 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2349 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 2350 | return -EACCES; |
| 2351 | } |
| 2352 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2353 | if (is_ctx_reg(env, insn->dst_reg) || |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 2354 | is_pkt_reg(env, insn->dst_reg) || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2355 | is_flow_key_reg(env, insn->dst_reg) || |
| 2356 | is_sk_reg(env, insn->dst_reg)) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2357 | 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] | 2358 | insn->dst_reg, |
| 2359 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2360 | return -EACCES; |
| 2361 | } |
| 2362 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2363 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2364 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2365 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2366 | if (err) |
| 2367 | return err; |
| 2368 | |
| 2369 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2370 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2371 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2372 | } |
| 2373 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2374 | static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
| 2375 | int off, int access_size, |
| 2376 | bool zero_size_allowed) |
| 2377 | { |
| 2378 | struct bpf_reg_state *reg = reg_state(env, regno); |
| 2379 | |
| 2380 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 2381 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
| 2382 | if (tnum_is_const(reg->var_off)) { |
| 2383 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", |
| 2384 | regno, off, access_size); |
| 2385 | } else { |
| 2386 | char tn_buf[48]; |
| 2387 | |
| 2388 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2389 | verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", |
| 2390 | regno, tn_buf, access_size); |
| 2391 | } |
| 2392 | return -EACCES; |
| 2393 | } |
| 2394 | return 0; |
| 2395 | } |
| 2396 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2397 | /* when register 'regno' is passed into function that will read 'access_size' |
| 2398 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2399 | * and all elements of stack are initialized. |
| 2400 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 2401 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2402 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2403 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2404 | int access_size, bool zero_size_allowed, |
| 2405 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2406 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2407 | struct bpf_reg_state *reg = reg_state(env, regno); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2408 | struct bpf_func_state *state = func(env, reg); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2409 | int err, min_off, max_off, i, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2410 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2411 | if (reg->type != PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2412 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2413 | if (zero_size_allowed && access_size == 0 && |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2414 | register_is_null(reg)) |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2415 | return 0; |
| 2416 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2417 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2418 | reg_type_str[reg->type], |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2419 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2420 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2421 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2422 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2423 | if (tnum_is_const(reg->var_off)) { |
| 2424 | min_off = max_off = reg->var_off.value + reg->off; |
| 2425 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 2426 | zero_size_allowed); |
| 2427 | if (err) |
| 2428 | return err; |
| 2429 | } else { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 2430 | /* Variable offset is prohibited for unprivileged mode for |
| 2431 | * simplicity since it requires corresponding support in |
| 2432 | * Spectre masking for stack ALU. |
| 2433 | * See also retrieve_ptr_limit(). |
| 2434 | */ |
| 2435 | if (!env->allow_ptr_leaks) { |
| 2436 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2437 | |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 2438 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2439 | verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", |
| 2440 | regno, tn_buf); |
| 2441 | return -EACCES; |
| 2442 | } |
Andrey Ignatov | f2bcd05 | 2019-04-03 23:22:37 -0700 | [diff] [blame] | 2443 | /* Only initialized buffer on stack is allowed to be accessed |
| 2444 | * with variable offset. With uninitialized buffer it's hard to |
| 2445 | * guarantee that whole memory is marked as initialized on |
| 2446 | * helper return since specific bounds are unknown what may |
| 2447 | * cause uninitialized stack leaking. |
| 2448 | */ |
| 2449 | if (meta && meta->raw_mode) |
| 2450 | meta = NULL; |
| 2451 | |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2452 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
| 2453 | reg->smax_value <= -BPF_MAX_VAR_OFF) { |
| 2454 | verbose(env, "R%d unbounded indirect variable offset stack access\n", |
| 2455 | regno); |
| 2456 | return -EACCES; |
| 2457 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2458 | min_off = reg->smin_value + reg->off; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2459 | max_off = reg->smax_value + reg->off; |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2460 | err = __check_stack_boundary(env, regno, min_off, access_size, |
| 2461 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2462 | if (err) { |
| 2463 | verbose(env, "R%d min value is outside of stack bound\n", |
| 2464 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2465 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2466 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2467 | err = __check_stack_boundary(env, regno, max_off, access_size, |
| 2468 | zero_size_allowed); |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2469 | if (err) { |
| 2470 | verbose(env, "R%d max value is outside of stack bound\n", |
| 2471 | regno); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2472 | return err; |
Andrey Ignatov | 107c26a7 | 2019-04-03 23:22:41 -0700 | [diff] [blame] | 2473 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2474 | } |
| 2475 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2476 | if (meta && meta->raw_mode) { |
| 2477 | meta->access_size = access_size; |
| 2478 | meta->regno = regno; |
| 2479 | return 0; |
| 2480 | } |
| 2481 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2482 | for (i = min_off; i < max_off + access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2483 | u8 *stype; |
| 2484 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2485 | slot = -i - 1; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2486 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2487 | if (state->allocated_stack <= slot) |
| 2488 | goto err; |
| 2489 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 2490 | if (*stype == STACK_MISC) |
| 2491 | goto mark; |
| 2492 | if (*stype == STACK_ZERO) { |
| 2493 | /* helper can write anything into the stack */ |
| 2494 | *stype = STACK_MISC; |
| 2495 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2496 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2497 | err: |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2498 | if (tnum_is_const(reg->var_off)) { |
| 2499 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 2500 | min_off, i - min_off, access_size); |
| 2501 | } else { |
| 2502 | char tn_buf[48]; |
| 2503 | |
| 2504 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2505 | verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", |
| 2506 | tn_buf, i - min_off, access_size); |
| 2507 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2508 | return -EACCES; |
| 2509 | mark: |
| 2510 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 2511 | * the whole slot to be marked as 'read' |
| 2512 | */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2513 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2514 | state->stack[spi].spilled_ptr.parent, |
| 2515 | REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2516 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 2517 | return update_stack_depth(env, state, min_off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2518 | } |
| 2519 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2520 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 2521 | int access_size, bool zero_size_allowed, |
| 2522 | struct bpf_call_arg_meta *meta) |
| 2523 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2524 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2525 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2526 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2527 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2528 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2529 | return check_packet_access(env, regno, reg->off, access_size, |
| 2530 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2531 | case PTR_TO_MAP_VALUE: |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 2532 | if (check_map_access_type(env, regno, reg->off, access_size, |
| 2533 | meta && meta->raw_mode ? BPF_WRITE : |
| 2534 | BPF_READ)) |
| 2535 | return -EACCES; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2536 | return check_map_access(env, regno, reg->off, access_size, |
| 2537 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2538 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2539 | return check_stack_boundary(env, regno, access_size, |
| 2540 | zero_size_allowed, meta); |
| 2541 | } |
| 2542 | } |
| 2543 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 2544 | /* Implementation details: |
| 2545 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL |
| 2546 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
| 2547 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after |
| 2548 | * value_or_null->value transition, since the verifier only cares about |
| 2549 | * the range of access to valid map value pointer and doesn't care about actual |
| 2550 | * address of the map element. |
| 2551 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
| 2552 | * reg->id > 0 after value_or_null->value transition. By doing so |
| 2553 | * two bpf_map_lookups will be considered two different pointers that |
| 2554 | * point to different bpf_spin_locks. |
| 2555 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
| 2556 | * dead-locks. |
| 2557 | * Since only one bpf_spin_lock is allowed the checks are simpler than |
| 2558 | * reg_is_refcounted() logic. The verifier needs to remember only |
| 2559 | * one spin_lock instead of array of acquired_refs. |
| 2560 | * cur_state->active_spin_lock remembers which map value element got locked |
| 2561 | * and clears it after bpf_spin_unlock. |
| 2562 | */ |
| 2563 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, |
| 2564 | bool is_lock) |
| 2565 | { |
| 2566 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 2567 | struct bpf_verifier_state *cur = env->cur_state; |
| 2568 | bool is_const = tnum_is_const(reg->var_off); |
| 2569 | struct bpf_map *map = reg->map_ptr; |
| 2570 | u64 val = reg->var_off.value; |
| 2571 | |
| 2572 | if (reg->type != PTR_TO_MAP_VALUE) { |
| 2573 | verbose(env, "R%d is not a pointer to map_value\n", regno); |
| 2574 | return -EINVAL; |
| 2575 | } |
| 2576 | if (!is_const) { |
| 2577 | verbose(env, |
| 2578 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", |
| 2579 | regno); |
| 2580 | return -EINVAL; |
| 2581 | } |
| 2582 | if (!map->btf) { |
| 2583 | verbose(env, |
| 2584 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", |
| 2585 | map->name); |
| 2586 | return -EINVAL; |
| 2587 | } |
| 2588 | if (!map_value_has_spin_lock(map)) { |
| 2589 | if (map->spin_lock_off == -E2BIG) |
| 2590 | verbose(env, |
| 2591 | "map '%s' has more than one 'struct bpf_spin_lock'\n", |
| 2592 | map->name); |
| 2593 | else if (map->spin_lock_off == -ENOENT) |
| 2594 | verbose(env, |
| 2595 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", |
| 2596 | map->name); |
| 2597 | else |
| 2598 | verbose(env, |
| 2599 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", |
| 2600 | map->name); |
| 2601 | return -EINVAL; |
| 2602 | } |
| 2603 | if (map->spin_lock_off != val + reg->off) { |
| 2604 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", |
| 2605 | val + reg->off); |
| 2606 | return -EINVAL; |
| 2607 | } |
| 2608 | if (is_lock) { |
| 2609 | if (cur->active_spin_lock) { |
| 2610 | verbose(env, |
| 2611 | "Locking two bpf_spin_locks are not allowed\n"); |
| 2612 | return -EINVAL; |
| 2613 | } |
| 2614 | cur->active_spin_lock = reg->id; |
| 2615 | } else { |
| 2616 | if (!cur->active_spin_lock) { |
| 2617 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
| 2618 | return -EINVAL; |
| 2619 | } |
| 2620 | if (cur->active_spin_lock != reg->id) { |
| 2621 | verbose(env, "bpf_spin_unlock of different lock\n"); |
| 2622 | return -EINVAL; |
| 2623 | } |
| 2624 | cur->active_spin_lock = 0; |
| 2625 | } |
| 2626 | return 0; |
| 2627 | } |
| 2628 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2629 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 2630 | { |
| 2631 | return type == ARG_PTR_TO_MEM || |
| 2632 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 2633 | type == ARG_PTR_TO_UNINIT_MEM; |
| 2634 | } |
| 2635 | |
| 2636 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 2637 | { |
| 2638 | return type == ARG_CONST_SIZE || |
| 2639 | type == ARG_CONST_SIZE_OR_ZERO; |
| 2640 | } |
| 2641 | |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 2642 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
| 2643 | { |
| 2644 | return type == ARG_PTR_TO_INT || |
| 2645 | type == ARG_PTR_TO_LONG; |
| 2646 | } |
| 2647 | |
| 2648 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
| 2649 | { |
| 2650 | if (type == ARG_PTR_TO_INT) |
| 2651 | return sizeof(u32); |
| 2652 | else if (type == ARG_PTR_TO_LONG) |
| 2653 | return sizeof(u64); |
| 2654 | |
| 2655 | return -EINVAL; |
| 2656 | } |
| 2657 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2658 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2659 | enum bpf_arg_type arg_type, |
| 2660 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2661 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2662 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2663 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2664 | int err = 0; |
| 2665 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2666 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2667 | return 0; |
| 2668 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2669 | err = check_reg_arg(env, regno, SRC_OP); |
| 2670 | if (err) |
| 2671 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2672 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2673 | if (arg_type == ARG_ANYTHING) { |
| 2674 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2675 | verbose(env, "R%d leaks addr into helper function\n", |
| 2676 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2677 | return -EACCES; |
| 2678 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2679 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2680 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2681 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2682 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2683 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2684 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2685 | return -EACCES; |
| 2686 | } |
| 2687 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2688 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2689 | arg_type == ARG_PTR_TO_MAP_VALUE || |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2690 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
| 2691 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2692 | expected_type = PTR_TO_STACK; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2693 | if (register_is_null(reg) && |
| 2694 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) |
| 2695 | /* final test in check_stack_boundary() */; |
| 2696 | else if (!type_is_pkt_pointer(type) && |
| 2697 | type != PTR_TO_MAP_VALUE && |
| 2698 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2699 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2700 | } else if (arg_type == ARG_CONST_SIZE || |
| 2701 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2702 | expected_type = SCALAR_VALUE; |
| 2703 | if (type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2704 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2705 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 2706 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2707 | if (type != expected_type) |
| 2708 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2709 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 2710 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2711 | if (type != expected_type) |
| 2712 | goto err_type; |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 2713 | err = check_ctx_reg(env, reg, regno); |
| 2714 | if (err < 0) |
| 2715 | return err; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2716 | } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) { |
| 2717 | expected_type = PTR_TO_SOCK_COMMON; |
| 2718 | /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */ |
| 2719 | if (!type_is_sk_pointer(type)) |
| 2720 | goto err_type; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 2721 | if (reg->ref_obj_id) { |
| 2722 | if (meta->ref_obj_id) { |
| 2723 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", |
| 2724 | regno, reg->ref_obj_id, |
| 2725 | meta->ref_obj_id); |
| 2726 | return -EFAULT; |
| 2727 | } |
| 2728 | meta->ref_obj_id = reg->ref_obj_id; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2729 | } |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2730 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
| 2731 | expected_type = PTR_TO_SOCKET; |
| 2732 | if (type != expected_type) |
| 2733 | goto err_type; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 2734 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
| 2735 | if (meta->func_id == BPF_FUNC_spin_lock) { |
| 2736 | if (process_spin_lock(env, regno, true)) |
| 2737 | return -EACCES; |
| 2738 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
| 2739 | if (process_spin_lock(env, regno, false)) |
| 2740 | return -EACCES; |
| 2741 | } else { |
| 2742 | verbose(env, "verifier internal error\n"); |
| 2743 | return -EFAULT; |
| 2744 | } |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2745 | } else if (arg_type_is_mem_ptr(arg_type)) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2746 | expected_type = PTR_TO_STACK; |
| 2747 | /* One exception here. In case function allows for NULL to be |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2748 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2749 | * happens during stack boundary checking. |
| 2750 | */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2751 | if (register_is_null(reg) && |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 2752 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2753 | /* final test in check_stack_boundary() */; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2754 | else if (!type_is_pkt_pointer(type) && |
| 2755 | type != PTR_TO_MAP_VALUE && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2756 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2757 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2758 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 2759 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 2760 | expected_type = PTR_TO_STACK; |
| 2761 | if (!type_is_pkt_pointer(type) && |
| 2762 | type != PTR_TO_MAP_VALUE && |
| 2763 | type != expected_type) |
| 2764 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2765 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2766 | verbose(env, "unsupported arg_type %d\n", arg_type); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2767 | return -EFAULT; |
| 2768 | } |
| 2769 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2770 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 2771 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2772 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2773 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 2774 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 2775 | * check that [key, key + map->key_size) are within |
| 2776 | * stack limits and initialized |
| 2777 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2778 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2779 | /* in function declaration map_ptr must come before |
| 2780 | * map_key, so that it's verified and known before |
| 2781 | * we have to check map_key here. Otherwise it means |
| 2782 | * that kernel subsystem misconfigured verifier |
| 2783 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2784 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2785 | return -EACCES; |
| 2786 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2787 | err = check_helper_mem_access(env, regno, |
| 2788 | meta->map_ptr->key_size, false, |
| 2789 | NULL); |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2790 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2791 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
| 2792 | !register_is_null(reg)) || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2793 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2794 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 2795 | * check [value, value + map->value_size) validity |
| 2796 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2797 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2798 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2799 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2800 | return -EACCES; |
| 2801 | } |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2802 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2803 | err = check_helper_mem_access(env, regno, |
| 2804 | meta->map_ptr->value_size, false, |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2805 | meta); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2806 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2807 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2808 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2809 | /* remember the mem_size which may be used later |
| 2810 | * to refine return values. |
| 2811 | */ |
| 2812 | meta->msize_smax_value = reg->smax_value; |
| 2813 | meta->msize_umax_value = reg->umax_value; |
| 2814 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2815 | /* The register is SCALAR_VALUE; the access check |
| 2816 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2817 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2818 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2819 | /* For unprivileged variable accesses, disable raw |
| 2820 | * mode so that the program is required to |
| 2821 | * initialize all the memory that the helper could |
| 2822 | * just partially fill up. |
| 2823 | */ |
| 2824 | meta = NULL; |
| 2825 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2826 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2827 | 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] | 2828 | regno); |
| 2829 | return -EACCES; |
| 2830 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2831 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2832 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2833 | err = check_helper_mem_access(env, regno - 1, 0, |
| 2834 | zero_size_allowed, |
| 2835 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2836 | if (err) |
| 2837 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2838 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2839 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2840 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2841 | 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] | 2842 | regno); |
| 2843 | return -EACCES; |
| 2844 | } |
| 2845 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2846 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2847 | zero_size_allowed, meta); |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 2848 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 2849 | int size = int_ptr_type_to_size(arg_type); |
| 2850 | |
| 2851 | err = check_helper_mem_access(env, regno, size, false, meta); |
| 2852 | if (err) |
| 2853 | return err; |
| 2854 | err = check_ptr_alignment(env, reg, 0, size, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2858 | err_type: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2859 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2860 | reg_type_str[type], reg_type_str[expected_type]); |
| 2861 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2862 | } |
| 2863 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2864 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 2865 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2866 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2867 | if (!map) |
| 2868 | return 0; |
| 2869 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2870 | /* We need a two way check, first is from map perspective ... */ |
| 2871 | switch (map->map_type) { |
| 2872 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 2873 | if (func_id != BPF_FUNC_tail_call) |
| 2874 | goto error; |
| 2875 | break; |
| 2876 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 2877 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2878 | func_id != BPF_FUNC_perf_event_output && |
| 2879 | func_id != BPF_FUNC_perf_event_read_value) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2880 | goto error; |
| 2881 | break; |
| 2882 | case BPF_MAP_TYPE_STACK_TRACE: |
| 2883 | if (func_id != BPF_FUNC_get_stackid) |
| 2884 | goto error; |
| 2885 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 2886 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 2887 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2888 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2889 | goto error; |
| 2890 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2891 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 2892 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2893 | if (func_id != BPF_FUNC_get_local_storage) |
| 2894 | goto error; |
| 2895 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2896 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
| 2897 | * allow to be modified from bpf side. So do not allow lookup elements |
| 2898 | * for now. |
| 2899 | */ |
| 2900 | case BPF_MAP_TYPE_DEVMAP: |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 2901 | if (func_id != BPF_FUNC_redirect_map) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2902 | goto error; |
| 2903 | break; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2904 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 2905 | * appear. |
| 2906 | */ |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2907 | case BPF_MAP_TYPE_CPUMAP: |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2908 | case BPF_MAP_TYPE_XSKMAP: |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2909 | if (func_id != BPF_FUNC_redirect_map) |
| 2910 | goto error; |
| 2911 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2912 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 2913 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2914 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 2915 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 2916 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2917 | case BPF_MAP_TYPE_SOCKMAP: |
| 2918 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 2919 | func_id != BPF_FUNC_sock_map_update && |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2920 | func_id != BPF_FUNC_map_delete_elem && |
| 2921 | func_id != BPF_FUNC_msg_redirect_map) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2922 | goto error; |
| 2923 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2924 | case BPF_MAP_TYPE_SOCKHASH: |
| 2925 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 2926 | func_id != BPF_FUNC_sock_hash_update && |
| 2927 | func_id != BPF_FUNC_map_delete_elem && |
| 2928 | func_id != BPF_FUNC_msg_redirect_hash) |
| 2929 | goto error; |
| 2930 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2931 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 2932 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 2933 | goto error; |
| 2934 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 2935 | case BPF_MAP_TYPE_QUEUE: |
| 2936 | case BPF_MAP_TYPE_STACK: |
| 2937 | if (func_id != BPF_FUNC_map_peek_elem && |
| 2938 | func_id != BPF_FUNC_map_pop_elem && |
| 2939 | func_id != BPF_FUNC_map_push_elem) |
| 2940 | goto error; |
| 2941 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 2942 | case BPF_MAP_TYPE_SK_STORAGE: |
| 2943 | if (func_id != BPF_FUNC_sk_storage_get && |
| 2944 | func_id != BPF_FUNC_sk_storage_delete) |
| 2945 | goto error; |
| 2946 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2947 | default: |
| 2948 | break; |
| 2949 | } |
| 2950 | |
| 2951 | /* ... and second from the function itself. */ |
| 2952 | switch (func_id) { |
| 2953 | case BPF_FUNC_tail_call: |
| 2954 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 2955 | goto error; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 2956 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2957 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
| 2958 | return -EINVAL; |
| 2959 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2960 | break; |
| 2961 | case BPF_FUNC_perf_event_read: |
| 2962 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2963 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2964 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 2965 | goto error; |
| 2966 | break; |
| 2967 | case BPF_FUNC_get_stackid: |
| 2968 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 2969 | goto error; |
| 2970 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2971 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 2972 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2973 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 2974 | goto error; |
| 2975 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2976 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 2977 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2978 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 2979 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2980 | goto error; |
| 2981 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2982 | case BPF_FUNC_sk_redirect_map: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2983 | case BPF_FUNC_msg_redirect_map: |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2984 | case BPF_FUNC_sock_map_update: |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2985 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 2986 | goto error; |
| 2987 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2988 | case BPF_FUNC_sk_redirect_hash: |
| 2989 | case BPF_FUNC_msg_redirect_hash: |
| 2990 | case BPF_FUNC_sock_hash_update: |
| 2991 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2992 | goto error; |
| 2993 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2994 | case BPF_FUNC_get_local_storage: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 2995 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 2996 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2997 | goto error; |
| 2998 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2999 | case BPF_FUNC_sk_select_reuseport: |
| 3000 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) |
| 3001 | goto error; |
| 3002 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 3003 | case BPF_FUNC_map_peek_elem: |
| 3004 | case BPF_FUNC_map_pop_elem: |
| 3005 | case BPF_FUNC_map_push_elem: |
| 3006 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 3007 | map->map_type != BPF_MAP_TYPE_STACK) |
| 3008 | goto error; |
| 3009 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 3010 | case BPF_FUNC_sk_storage_get: |
| 3011 | case BPF_FUNC_sk_storage_delete: |
| 3012 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
| 3013 | goto error; |
| 3014 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3015 | default: |
| 3016 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3020 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3021 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 3022 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 3023 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 3024 | } |
| 3025 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 3026 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3027 | { |
| 3028 | int count = 0; |
| 3029 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3030 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3031 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3032 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3033 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3034 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3035 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3036 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3037 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 3038 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3039 | count++; |
| 3040 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 3041 | /* We only support one arg being in raw mode at the moment, |
| 3042 | * which is sufficient for the helper functions we have |
| 3043 | * right now. |
| 3044 | */ |
| 3045 | return count <= 1; |
| 3046 | } |
| 3047 | |
| 3048 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 3049 | enum bpf_arg_type arg_next) |
| 3050 | { |
| 3051 | return (arg_type_is_mem_ptr(arg_curr) && |
| 3052 | !arg_type_is_mem_size(arg_next)) || |
| 3053 | (!arg_type_is_mem_ptr(arg_curr) && |
| 3054 | arg_type_is_mem_size(arg_next)); |
| 3055 | } |
| 3056 | |
| 3057 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 3058 | { |
| 3059 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 3060 | * bytes from memory 'buf'. Both arg types need |
| 3061 | * to be paired, so make sure there's no buggy |
| 3062 | * helper function specification. |
| 3063 | */ |
| 3064 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 3065 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 3066 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 3067 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 3068 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 3069 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 3070 | return false; |
| 3071 | |
| 3072 | return true; |
| 3073 | } |
| 3074 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3075 | 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] | 3076 | { |
| 3077 | int count = 0; |
| 3078 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3079 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3080 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3081 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3082 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3083 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3084 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3085 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3086 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3087 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3088 | count++; |
| 3089 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3090 | /* A reference acquiring function cannot acquire |
| 3091 | * another refcounted ptr. |
| 3092 | */ |
| 3093 | if (is_acquire_function(func_id) && count) |
| 3094 | return false; |
| 3095 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3096 | /* We only support one arg being unreferenced at the moment, |
| 3097 | * which is sufficient for the helper functions we have right now. |
| 3098 | */ |
| 3099 | return count <= 1; |
| 3100 | } |
| 3101 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3102 | 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] | 3103 | { |
| 3104 | return check_raw_mode_ok(fn) && |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3105 | check_arg_pair_ok(fn) && |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3106 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3107 | } |
| 3108 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3109 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 3110 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3111 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3112 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 3113 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3114 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3115 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3116 | int i; |
| 3117 | |
| 3118 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3119 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3120 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3121 | |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 3122 | bpf_for_each_spilled_reg(i, state, reg) { |
| 3123 | if (!reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3124 | continue; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3125 | if (reg_is_pkt_pointer_any(reg)) |
| 3126 | __mark_reg_unknown(reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3127 | } |
| 3128 | } |
| 3129 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3130 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 3131 | { |
| 3132 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3133 | int i; |
| 3134 | |
| 3135 | for (i = 0; i <= vstate->curframe; i++) |
| 3136 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 3137 | } |
| 3138 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3139 | static void release_reg_references(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3140 | struct bpf_func_state *state, |
| 3141 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3142 | { |
| 3143 | struct bpf_reg_state *regs = state->regs, *reg; |
| 3144 | int i; |
| 3145 | |
| 3146 | for (i = 0; i < MAX_BPF_REG; i++) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3147 | if (regs[i].ref_obj_id == ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3148 | mark_reg_unknown(env, regs, i); |
| 3149 | |
| 3150 | bpf_for_each_spilled_reg(i, state, reg) { |
| 3151 | if (!reg) |
| 3152 | continue; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3153 | if (reg->ref_obj_id == ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3154 | __mark_reg_unknown(reg); |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | /* The pointer with the specified id has released its reference to kernel |
| 3159 | * resources. Identify all copies of the same pointer and clear the reference. |
| 3160 | */ |
| 3161 | static int release_reference(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3162 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3163 | { |
| 3164 | struct bpf_verifier_state *vstate = env->cur_state; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3165 | int err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3166 | int i; |
| 3167 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3168 | err = release_reference_state(cur_func(env), ref_obj_id); |
| 3169 | if (err) |
| 3170 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3171 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3172 | for (i = 0; i <= vstate->curframe; i++) |
| 3173 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
| 3174 | |
| 3175 | return 0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3176 | } |
| 3177 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3178 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 3179 | int *insn_idx) |
| 3180 | { |
| 3181 | struct bpf_verifier_state *state = env->cur_state; |
| 3182 | struct bpf_func_state *caller, *callee; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3183 | int i, err, subprog, target_insn; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3184 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 3185 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3186 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 3187 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3188 | return -E2BIG; |
| 3189 | } |
| 3190 | |
| 3191 | target_insn = *insn_idx + insn->imm; |
| 3192 | subprog = find_subprog(env, target_insn + 1); |
| 3193 | if (subprog < 0) { |
| 3194 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 3195 | target_insn + 1); |
| 3196 | return -EFAULT; |
| 3197 | } |
| 3198 | |
| 3199 | caller = state->frame[state->curframe]; |
| 3200 | if (state->frame[state->curframe + 1]) { |
| 3201 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 3202 | state->curframe + 1); |
| 3203 | return -EFAULT; |
| 3204 | } |
| 3205 | |
| 3206 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 3207 | if (!callee) |
| 3208 | return -ENOMEM; |
| 3209 | state->frame[state->curframe + 1] = callee; |
| 3210 | |
| 3211 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 3212 | * into its own stack before reading from it. |
| 3213 | * callee can read/write into caller's stack |
| 3214 | */ |
| 3215 | init_func_state(env, callee, |
| 3216 | /* remember the callsite, it will be used by bpf_exit */ |
| 3217 | *insn_idx /* callsite */, |
| 3218 | state->curframe + 1 /* frameno within this callchain */, |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 3219 | subprog /* subprog number within this prog */); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3220 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3221 | /* Transfer references to the callee */ |
| 3222 | err = transfer_reference_state(callee, caller); |
| 3223 | if (err) |
| 3224 | return err; |
| 3225 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 3226 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 3227 | * pointers, which connects us up to the liveness chain |
| 3228 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3229 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 3230 | callee->regs[i] = caller->regs[i]; |
| 3231 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 3232 | /* after the call registers r0 - r5 were scratched */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3233 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 3234 | mark_reg_not_init(env, caller->regs, caller_saved[i]); |
| 3235 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 3236 | } |
| 3237 | |
| 3238 | /* only increment it after check_reg_arg() finished */ |
| 3239 | state->curframe++; |
| 3240 | |
| 3241 | /* and go analyze first insn of the callee */ |
| 3242 | *insn_idx = target_insn; |
| 3243 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 3244 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3245 | verbose(env, "caller:\n"); |
| 3246 | print_verifier_state(env, caller); |
| 3247 | verbose(env, "callee:\n"); |
| 3248 | print_verifier_state(env, callee); |
| 3249 | } |
| 3250 | return 0; |
| 3251 | } |
| 3252 | |
| 3253 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 3254 | { |
| 3255 | struct bpf_verifier_state *state = env->cur_state; |
| 3256 | struct bpf_func_state *caller, *callee; |
| 3257 | struct bpf_reg_state *r0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3258 | int err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3259 | |
| 3260 | callee = state->frame[state->curframe]; |
| 3261 | r0 = &callee->regs[BPF_REG_0]; |
| 3262 | if (r0->type == PTR_TO_STACK) { |
| 3263 | /* technically it's ok to return caller's stack pointer |
| 3264 | * (or caller's caller's pointer) back to the caller, |
| 3265 | * since these pointers are valid. Only current stack |
| 3266 | * pointer will be invalid as soon as function exits, |
| 3267 | * but let's be conservative |
| 3268 | */ |
| 3269 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 3270 | return -EINVAL; |
| 3271 | } |
| 3272 | |
| 3273 | state->curframe--; |
| 3274 | caller = state->frame[state->curframe]; |
| 3275 | /* return to the caller whatever r0 had in the callee */ |
| 3276 | caller->regs[BPF_REG_0] = *r0; |
| 3277 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3278 | /* Transfer references to the caller */ |
| 3279 | err = transfer_reference_state(caller, callee); |
| 3280 | if (err) |
| 3281 | return err; |
| 3282 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3283 | *insn_idx = callee->callsite + 1; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 3284 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3285 | verbose(env, "returning from callee:\n"); |
| 3286 | print_verifier_state(env, callee); |
| 3287 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 3288 | print_verifier_state(env, caller); |
| 3289 | } |
| 3290 | /* clear everything in the callee */ |
| 3291 | free_func_state(callee); |
| 3292 | state->frame[state->curframe + 1] = NULL; |
| 3293 | return 0; |
| 3294 | } |
| 3295 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 3296 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 3297 | int func_id, |
| 3298 | struct bpf_call_arg_meta *meta) |
| 3299 | { |
| 3300 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 3301 | |
| 3302 | if (ret_type != RET_INTEGER || |
| 3303 | (func_id != BPF_FUNC_get_stack && |
| 3304 | func_id != BPF_FUNC_probe_read_str)) |
| 3305 | return; |
| 3306 | |
| 3307 | ret_reg->smax_value = meta->msize_smax_value; |
| 3308 | ret_reg->umax_value = meta->msize_umax_value; |
| 3309 | __reg_deduce_bounds(ret_reg); |
| 3310 | __reg_bound_offset(ret_reg); |
| 3311 | } |
| 3312 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3313 | static int |
| 3314 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 3315 | int func_id, int insn_idx) |
| 3316 | { |
| 3317 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3318 | struct bpf_map *map = meta->map_ptr; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3319 | |
| 3320 | if (func_id != BPF_FUNC_tail_call && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 3321 | func_id != BPF_FUNC_map_lookup_elem && |
| 3322 | func_id != BPF_FUNC_map_update_elem && |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 3323 | func_id != BPF_FUNC_map_delete_elem && |
| 3324 | func_id != BPF_FUNC_map_push_elem && |
| 3325 | func_id != BPF_FUNC_map_pop_elem && |
| 3326 | func_id != BPF_FUNC_map_peek_elem) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3327 | return 0; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 3328 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3329 | if (map == NULL) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3330 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 3331 | return -EINVAL; |
| 3332 | } |
| 3333 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3334 | /* In case of read-only, some additional restrictions |
| 3335 | * need to be applied in order to prevent altering the |
| 3336 | * state of the map from program side. |
| 3337 | */ |
| 3338 | if ((map->map_flags & BPF_F_RDONLY_PROG) && |
| 3339 | (func_id == BPF_FUNC_map_delete_elem || |
| 3340 | func_id == BPF_FUNC_map_update_elem || |
| 3341 | func_id == BPF_FUNC_map_push_elem || |
| 3342 | func_id == BPF_FUNC_map_pop_elem)) { |
| 3343 | verbose(env, "write into map forbidden\n"); |
| 3344 | return -EACCES; |
| 3345 | } |
| 3346 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3347 | if (!BPF_MAP_PTR(aux->map_state)) |
| 3348 | bpf_map_ptr_store(aux, meta->map_ptr, |
| 3349 | meta->map_ptr->unpriv_array); |
| 3350 | else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr) |
| 3351 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
| 3352 | meta->map_ptr->unpriv_array); |
| 3353 | return 0; |
| 3354 | } |
| 3355 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3356 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 3357 | { |
| 3358 | struct bpf_func_state *state = cur_func(env); |
| 3359 | int i; |
| 3360 | |
| 3361 | for (i = 0; i < state->acquired_refs; i++) { |
| 3362 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 3363 | state->refs[i].id, state->refs[i].insn_idx); |
| 3364 | } |
| 3365 | return state->acquired_refs ? -EINVAL : 0; |
| 3366 | } |
| 3367 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3368 | 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] | 3369 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3370 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3371 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3372 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3373 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3374 | int i, err; |
| 3375 | |
| 3376 | /* find function prototype */ |
| 3377 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3378 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 3379 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3380 | return -EINVAL; |
| 3381 | } |
| 3382 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 3383 | if (env->ops->get_func_proto) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 3384 | fn = env->ops->get_func_proto(func_id, env->prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3385 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3386 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 3387 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3388 | return -EINVAL; |
| 3389 | } |
| 3390 | |
| 3391 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 3392 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Daniel Borkmann | 3fe2867 | 2018-06-02 23:06:33 +0200 | [diff] [blame] | 3393 | 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] | 3394 | return -EINVAL; |
| 3395 | } |
| 3396 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 3397 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 3398 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 3399 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 3400 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 3401 | func_id_name(func_id), func_id); |
| 3402 | return -EINVAL; |
| 3403 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3404 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3405 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3406 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3407 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3408 | err = check_func_proto(fn, func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3409 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3410 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 3411 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3412 | return err; |
| 3413 | } |
| 3414 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3415 | meta.func_id = func_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3416 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3417 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3418 | if (err) |
| 3419 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3420 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3421 | if (err) |
| 3422 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3423 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3424 | if (err) |
| 3425 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3426 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3427 | if (err) |
| 3428 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3429 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3430 | if (err) |
| 3431 | return err; |
| 3432 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 3433 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 3434 | if (err) |
| 3435 | return err; |
| 3436 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3437 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 3438 | * is inferred from register state. |
| 3439 | */ |
| 3440 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3441 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 3442 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 3443 | if (err) |
| 3444 | return err; |
| 3445 | } |
| 3446 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3447 | if (func_id == BPF_FUNC_tail_call) { |
| 3448 | err = check_reference_leak(env); |
| 3449 | if (err) { |
| 3450 | verbose(env, "tail_call would lead to reference leak\n"); |
| 3451 | return err; |
| 3452 | } |
| 3453 | } else if (is_release_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3454 | err = release_reference(env, meta.ref_obj_id); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3455 | if (err) { |
| 3456 | verbose(env, "func %s#%d reference has not been acquired before\n", |
| 3457 | func_id_name(func_id), func_id); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3458 | return err; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3459 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3460 | } |
| 3461 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3462 | regs = cur_regs(env); |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 3463 | |
| 3464 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 3465 | * this is required because get_local_storage() can't return an error. |
| 3466 | */ |
| 3467 | if (func_id == BPF_FUNC_get_local_storage && |
| 3468 | !register_is_null(®s[BPF_REG_2])) { |
| 3469 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 3470 | return -EINVAL; |
| 3471 | } |
| 3472 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3473 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3474 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3475 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3476 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 3477 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3478 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 3479 | /* helper call returns 64-bit value. */ |
| 3480 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 3481 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3482 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3483 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3484 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3485 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3486 | } else if (fn->ret_type == RET_VOID) { |
| 3487 | regs[BPF_REG_0].type = NOT_INIT; |
Roman Gushchin | 3e6a4b3 | 2018-08-02 14:27:22 -0700 | [diff] [blame] | 3488 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 3489 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3490 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3491 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3492 | /* remember map_ptr, so that check_map_access() |
| 3493 | * can check 'value_size' boundary of memory access |
| 3494 | * to map element returned from bpf_map_lookup_elem() |
| 3495 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3496 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3497 | verbose(env, |
| 3498 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3499 | return -EINVAL; |
| 3500 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 3501 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 3502 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 3503 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
Alexei Starovoitov | e16d2f1 | 2019-01-31 15:40:05 -0800 | [diff] [blame] | 3504 | if (map_value_has_spin_lock(meta.map_ptr)) |
| 3505 | regs[BPF_REG_0].id = ++env->id_gen; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 3506 | } else { |
| 3507 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
| 3508 | regs[BPF_REG_0].id = ++env->id_gen; |
| 3509 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3510 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
| 3511 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 3512 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 3513 | regs[BPF_REG_0].id = ++env->id_gen; |
Lorenz Bauer | 85a51f8 | 2019-03-22 09:54:00 +0800 | [diff] [blame] | 3514 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
| 3515 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 3516 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; |
| 3517 | regs[BPF_REG_0].id = ++env->id_gen; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 3518 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
| 3519 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 3520 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; |
| 3521 | regs[BPF_REG_0].id = ++env->id_gen; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3522 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3523 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 3524 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3525 | return -EINVAL; |
| 3526 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 3527 | |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 3528 | if (is_ptr_cast_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3529 | /* For release_reference() */ |
| 3530 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 3531 | } else if (is_acquire_function(func_id)) { |
| 3532 | int id = acquire_reference_state(env, insn_idx); |
| 3533 | |
| 3534 | if (id < 0) |
| 3535 | return id; |
| 3536 | /* For mark_ptr_or_null_reg() */ |
| 3537 | regs[BPF_REG_0].id = id; |
| 3538 | /* For release_reference() */ |
| 3539 | regs[BPF_REG_0].ref_obj_id = id; |
| 3540 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 3541 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 3542 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
| 3543 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3544 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 3545 | if (err) |
| 3546 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 3547 | |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 3548 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
| 3549 | const char *err_str; |
| 3550 | |
| 3551 | #ifdef CONFIG_PERF_EVENTS |
| 3552 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 3553 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 3554 | #else |
| 3555 | err = -ENOTSUPP; |
| 3556 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 3557 | #endif |
| 3558 | if (err) { |
| 3559 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 3560 | return err; |
| 3561 | } |
| 3562 | |
| 3563 | env->prog->has_callchain_buf = true; |
| 3564 | } |
| 3565 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3566 | if (changes_data) |
| 3567 | clear_all_pkt_pointers(env); |
| 3568 | return 0; |
| 3569 | } |
| 3570 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3571 | static bool signed_add_overflows(s64 a, s64 b) |
| 3572 | { |
| 3573 | /* Do the add in u64, where overflow is well-defined */ |
| 3574 | s64 res = (s64)((u64)a + (u64)b); |
| 3575 | |
| 3576 | if (b < 0) |
| 3577 | return res > a; |
| 3578 | return res < a; |
| 3579 | } |
| 3580 | |
| 3581 | static bool signed_sub_overflows(s64 a, s64 b) |
| 3582 | { |
| 3583 | /* Do the sub in u64, where overflow is well-defined */ |
| 3584 | s64 res = (s64)((u64)a - (u64)b); |
| 3585 | |
| 3586 | if (b < 0) |
| 3587 | return res < a; |
| 3588 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3589 | } |
| 3590 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3591 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 3592 | const struct bpf_reg_state *reg, |
| 3593 | enum bpf_reg_type type) |
| 3594 | { |
| 3595 | bool known = tnum_is_const(reg->var_off); |
| 3596 | s64 val = reg->var_off.value; |
| 3597 | s64 smin = reg->smin_value; |
| 3598 | |
| 3599 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 3600 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 3601 | reg_type_str[type], val); |
| 3602 | return false; |
| 3603 | } |
| 3604 | |
| 3605 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 3606 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 3607 | reg_type_str[type], reg->off); |
| 3608 | return false; |
| 3609 | } |
| 3610 | |
| 3611 | if (smin == S64_MIN) { |
| 3612 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 3613 | reg_type_str[type]); |
| 3614 | return false; |
| 3615 | } |
| 3616 | |
| 3617 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 3618 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 3619 | smin, reg_type_str[type]); |
| 3620 | return false; |
| 3621 | } |
| 3622 | |
| 3623 | return true; |
| 3624 | } |
| 3625 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3626 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
| 3627 | { |
| 3628 | return &env->insn_aux_data[env->insn_idx]; |
| 3629 | } |
| 3630 | |
| 3631 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
| 3632 | u32 *ptr_limit, u8 opcode, bool off_is_neg) |
| 3633 | { |
| 3634 | bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || |
| 3635 | (opcode == BPF_SUB && !off_is_neg); |
| 3636 | u32 off; |
| 3637 | |
| 3638 | switch (ptr_reg->type) { |
| 3639 | case PTR_TO_STACK: |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 3640 | /* Indirect variable offset stack access is prohibited in |
| 3641 | * unprivileged mode so it's not handled here. |
| 3642 | */ |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3643 | off = ptr_reg->off + ptr_reg->var_off.value; |
| 3644 | if (mask_to_left) |
| 3645 | *ptr_limit = MAX_BPF_STACK + off; |
| 3646 | else |
| 3647 | *ptr_limit = -off; |
| 3648 | return 0; |
| 3649 | case PTR_TO_MAP_VALUE: |
| 3650 | if (mask_to_left) { |
| 3651 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; |
| 3652 | } else { |
| 3653 | off = ptr_reg->smin_value + ptr_reg->off; |
| 3654 | *ptr_limit = ptr_reg->map_ptr->value_size - off; |
| 3655 | } |
| 3656 | return 0; |
| 3657 | default: |
| 3658 | return -EINVAL; |
| 3659 | } |
| 3660 | } |
| 3661 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 3662 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
| 3663 | const struct bpf_insn *insn) |
| 3664 | { |
| 3665 | return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K; |
| 3666 | } |
| 3667 | |
| 3668 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, |
| 3669 | u32 alu_state, u32 alu_limit) |
| 3670 | { |
| 3671 | /* If we arrived here from different branches with different |
| 3672 | * state or limits to sanitize, then this won't work. |
| 3673 | */ |
| 3674 | if (aux->alu_state && |
| 3675 | (aux->alu_state != alu_state || |
| 3676 | aux->alu_limit != alu_limit)) |
| 3677 | return -EACCES; |
| 3678 | |
| 3679 | /* Corresponding fixup done in fixup_bpf_calls(). */ |
| 3680 | aux->alu_state = alu_state; |
| 3681 | aux->alu_limit = alu_limit; |
| 3682 | return 0; |
| 3683 | } |
| 3684 | |
| 3685 | static int sanitize_val_alu(struct bpf_verifier_env *env, |
| 3686 | struct bpf_insn *insn) |
| 3687 | { |
| 3688 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 3689 | |
| 3690 | if (can_skip_alu_sanitation(env, insn)) |
| 3691 | return 0; |
| 3692 | |
| 3693 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); |
| 3694 | } |
| 3695 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3696 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
| 3697 | struct bpf_insn *insn, |
| 3698 | const struct bpf_reg_state *ptr_reg, |
| 3699 | struct bpf_reg_state *dst_reg, |
| 3700 | bool off_is_neg) |
| 3701 | { |
| 3702 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3703 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 3704 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
| 3705 | u8 opcode = BPF_OP(insn->code); |
| 3706 | u32 alu_state, alu_limit; |
| 3707 | struct bpf_reg_state tmp; |
| 3708 | bool ret; |
| 3709 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 3710 | if (can_skip_alu_sanitation(env, insn)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3711 | return 0; |
| 3712 | |
| 3713 | /* We already marked aux for masking from non-speculative |
| 3714 | * paths, thus we got here in the first place. We only care |
| 3715 | * to explore bad access from here. |
| 3716 | */ |
| 3717 | if (vstate->speculative) |
| 3718 | goto do_sim; |
| 3719 | |
| 3720 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; |
| 3721 | alu_state |= ptr_is_dst_reg ? |
| 3722 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; |
| 3723 | |
| 3724 | if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg)) |
| 3725 | return 0; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 3726 | if (update_alu_sanitation_state(aux, alu_state, alu_limit)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3727 | return -EACCES; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3728 | do_sim: |
| 3729 | /* Simulate and find potential out-of-bounds access under |
| 3730 | * speculative execution from truncation as a result of |
| 3731 | * masking when off was not within expected range. If off |
| 3732 | * sits in dst, then we temporarily need to move ptr there |
| 3733 | * to simulate dst (== 0) +/-= ptr. Needed, for example, |
| 3734 | * for cases where we use K-based arithmetic in one direction |
| 3735 | * and truncated reg-based in the other in order to explore |
| 3736 | * bad access. |
| 3737 | */ |
| 3738 | if (!ptr_is_dst_reg) { |
| 3739 | tmp = *dst_reg; |
| 3740 | *dst_reg = *ptr_reg; |
| 3741 | } |
| 3742 | ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); |
Xu Yu | 0803278 | 2019-03-21 18:00:35 +0800 | [diff] [blame] | 3743 | if (!ptr_is_dst_reg && ret) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3744 | *dst_reg = tmp; |
| 3745 | return !ret ? -EFAULT : 0; |
| 3746 | } |
| 3747 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3748 | /* 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] | 3749 | * Caller should also handle BPF_MOV case separately. |
| 3750 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 3751 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 3752 | */ |
| 3753 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 3754 | struct bpf_insn *insn, |
| 3755 | const struct bpf_reg_state *ptr_reg, |
| 3756 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3757 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3758 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3759 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 3760 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3761 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3762 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 3763 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 3764 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 3765 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 3766 | u32 dst = insn->dst_reg, src = insn->src_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3767 | u8 opcode = BPF_OP(insn->code); |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3768 | int ret; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3769 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3770 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3771 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 3772 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 3773 | smin_val > smax_val || umin_val > umax_val) { |
| 3774 | /* Taint dst register if offset had invalid bounds derived from |
| 3775 | * e.g. dead branches. |
| 3776 | */ |
| 3777 | __mark_reg_unknown(dst_reg); |
| 3778 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3779 | } |
| 3780 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3781 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3782 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3783 | verbose(env, |
| 3784 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 3785 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3786 | return -EACCES; |
| 3787 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3788 | |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3789 | switch (ptr_reg->type) { |
| 3790 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 3791 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
| 3792 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3793 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3794 | case CONST_PTR_TO_MAP: |
| 3795 | case PTR_TO_PACKET_END: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3796 | case PTR_TO_SOCKET: |
| 3797 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3798 | case PTR_TO_SOCK_COMMON: |
| 3799 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 3800 | case PTR_TO_TCP_SOCK: |
| 3801 | case PTR_TO_TCP_SOCK_OR_NULL: |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3802 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
| 3803 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3804 | return -EACCES; |
Daniel Borkmann | 9d7ecee | 2019-01-03 00:58:32 +0100 | [diff] [blame] | 3805 | case PTR_TO_MAP_VALUE: |
| 3806 | if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { |
| 3807 | verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", |
| 3808 | off_reg == dst_reg ? dst : src); |
| 3809 | return -EACCES; |
| 3810 | } |
| 3811 | /* fall-through */ |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3812 | default: |
| 3813 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 3817 | * 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] | 3818 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3819 | dst_reg->type = ptr_reg->type; |
| 3820 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 3821 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3822 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 3823 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 3824 | return -EINVAL; |
| 3825 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3826 | switch (opcode) { |
| 3827 | case BPF_ADD: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3828 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 3829 | if (ret < 0) { |
| 3830 | verbose(env, "R%d tried to add from different maps or paths\n", dst); |
| 3831 | return ret; |
| 3832 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3833 | /* We can take a fixed offset as long as it doesn't overflow |
| 3834 | * the s32 'off' field |
| 3835 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3836 | if (known && (ptr_reg->off + smin_val == |
| 3837 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3838 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3839 | dst_reg->smin_value = smin_ptr; |
| 3840 | dst_reg->smax_value = smax_ptr; |
| 3841 | dst_reg->umin_value = umin_ptr; |
| 3842 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3843 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3844 | dst_reg->off = ptr_reg->off + smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3845 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3846 | break; |
| 3847 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3848 | /* A new variable offset is created. Note that off_reg->off |
| 3849 | * == 0, since it's a scalar. |
| 3850 | * dst_reg gets the pointer type and since some positive |
| 3851 | * integer value was added to the pointer, give it a new 'id' |
| 3852 | * if it's a PTR_TO_PACKET. |
| 3853 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 3854 | * added into the variable offset, and we copy the fixed offset |
| 3855 | * from ptr_reg. |
| 3856 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3857 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 3858 | signed_add_overflows(smax_ptr, smax_val)) { |
| 3859 | dst_reg->smin_value = S64_MIN; |
| 3860 | dst_reg->smax_value = S64_MAX; |
| 3861 | } else { |
| 3862 | dst_reg->smin_value = smin_ptr + smin_val; |
| 3863 | dst_reg->smax_value = smax_ptr + smax_val; |
| 3864 | } |
| 3865 | if (umin_ptr + umin_val < umin_ptr || |
| 3866 | umax_ptr + umax_val < umax_ptr) { |
| 3867 | dst_reg->umin_value = 0; |
| 3868 | dst_reg->umax_value = U64_MAX; |
| 3869 | } else { |
| 3870 | dst_reg->umin_value = umin_ptr + umin_val; |
| 3871 | dst_reg->umax_value = umax_ptr + umax_val; |
| 3872 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3873 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 3874 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3875 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3876 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3877 | dst_reg->id = ++env->id_gen; |
| 3878 | /* something was added to pkt_ptr, set range to zero */ |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3879 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3880 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3881 | break; |
| 3882 | case BPF_SUB: |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 3883 | ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0); |
| 3884 | if (ret < 0) { |
| 3885 | verbose(env, "R%d tried to sub from different maps or paths\n", dst); |
| 3886 | return ret; |
| 3887 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3888 | if (dst_reg == off_reg) { |
| 3889 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3890 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 3891 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3892 | return -EACCES; |
| 3893 | } |
| 3894 | /* We don't allow subtraction from FP, because (according to |
| 3895 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 3896 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 3897 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3898 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3899 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 3900 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3901 | return -EACCES; |
| 3902 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3903 | if (known && (ptr_reg->off - smin_val == |
| 3904 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3905 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3906 | dst_reg->smin_value = smin_ptr; |
| 3907 | dst_reg->smax_value = smax_ptr; |
| 3908 | dst_reg->umin_value = umin_ptr; |
| 3909 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3910 | dst_reg->var_off = ptr_reg->var_off; |
| 3911 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3912 | dst_reg->off = ptr_reg->off - smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3913 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3914 | break; |
| 3915 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3916 | /* A new variable offset is created. If the subtrahend is known |
| 3917 | * nonnegative, then any reg->range we had before is still good. |
| 3918 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3919 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 3920 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 3921 | /* Overflow possible, we know nothing */ |
| 3922 | dst_reg->smin_value = S64_MIN; |
| 3923 | dst_reg->smax_value = S64_MAX; |
| 3924 | } else { |
| 3925 | dst_reg->smin_value = smin_ptr - smax_val; |
| 3926 | dst_reg->smax_value = smax_ptr - smin_val; |
| 3927 | } |
| 3928 | if (umin_ptr < umax_val) { |
| 3929 | /* Overflow possible, we know nothing */ |
| 3930 | dst_reg->umin_value = 0; |
| 3931 | dst_reg->umax_value = U64_MAX; |
| 3932 | } else { |
| 3933 | /* Cannot overflow (as long as bounds are consistent) */ |
| 3934 | dst_reg->umin_value = umin_ptr - umax_val; |
| 3935 | dst_reg->umax_value = umax_ptr - umin_val; |
| 3936 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3937 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 3938 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3939 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3940 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3941 | dst_reg->id = ++env->id_gen; |
| 3942 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3943 | if (smin_val < 0) |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3944 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3945 | } |
| 3946 | break; |
| 3947 | case BPF_AND: |
| 3948 | case BPF_OR: |
| 3949 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3950 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 3951 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 3952 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3953 | return -EACCES; |
| 3954 | default: |
| 3955 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3956 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 3957 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3958 | return -EACCES; |
| 3959 | } |
| 3960 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3961 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 3962 | return -EINVAL; |
| 3963 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3964 | __update_reg_bounds(dst_reg); |
| 3965 | __reg_deduce_bounds(dst_reg); |
| 3966 | __reg_bound_offset(dst_reg); |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 3967 | |
| 3968 | /* For unprivileged we require that resulting offset must be in bounds |
| 3969 | * in order to be able to sanitize access later on. |
| 3970 | */ |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3971 | if (!env->allow_ptr_leaks) { |
| 3972 | if (dst_reg->type == PTR_TO_MAP_VALUE && |
| 3973 | check_map_access(env, dst, dst_reg->off, 1, false)) { |
| 3974 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
| 3975 | "prohibited for !root\n", dst); |
| 3976 | return -EACCES; |
| 3977 | } else if (dst_reg->type == PTR_TO_STACK && |
| 3978 | check_stack_access(env, dst_reg, dst_reg->off + |
| 3979 | dst_reg->var_off.value, 1)) { |
| 3980 | verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 3981 | "prohibited for !root\n", dst); |
| 3982 | return -EACCES; |
| 3983 | } |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 3984 | } |
| 3985 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3986 | return 0; |
| 3987 | } |
| 3988 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3989 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 3990 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 3991 | * need extra checks in the 32-bit case. |
| 3992 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3993 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 3994 | struct bpf_insn *insn, |
| 3995 | struct bpf_reg_state *dst_reg, |
| 3996 | struct bpf_reg_state src_reg) |
| 3997 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3998 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3999 | u8 opcode = BPF_OP(insn->code); |
| 4000 | bool src_known, dst_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4001 | s64 smin_val, smax_val; |
| 4002 | u64 umin_val, umax_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4003 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 4004 | u32 dst = insn->dst_reg; |
| 4005 | int ret; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4006 | |
Jann Horn | b799207 | 2018-10-05 18:17:59 +0200 | [diff] [blame] | 4007 | if (insn_bitness == 32) { |
| 4008 | /* Relevant for 32-bit RSH: Information can propagate towards |
| 4009 | * LSB, so it isn't sufficient to only truncate the output to |
| 4010 | * 32 bits. |
| 4011 | */ |
| 4012 | coerce_reg_to_size(dst_reg, 4); |
| 4013 | coerce_reg_to_size(&src_reg, 4); |
| 4014 | } |
| 4015 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4016 | smin_val = src_reg.smin_value; |
| 4017 | smax_val = src_reg.smax_value; |
| 4018 | umin_val = src_reg.umin_value; |
| 4019 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4020 | src_known = tnum_is_const(src_reg.var_off); |
| 4021 | dst_known = tnum_is_const(dst_reg->var_off); |
| 4022 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 4023 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
| 4024 | smin_val > smax_val || umin_val > umax_val) { |
| 4025 | /* Taint dst register if offset had invalid bounds derived from |
| 4026 | * e.g. dead branches. |
| 4027 | */ |
| 4028 | __mark_reg_unknown(dst_reg); |
| 4029 | return 0; |
| 4030 | } |
| 4031 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 4032 | if (!src_known && |
| 4033 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
| 4034 | __mark_reg_unknown(dst_reg); |
| 4035 | return 0; |
| 4036 | } |
| 4037 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4038 | switch (opcode) { |
| 4039 | case BPF_ADD: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 4040 | ret = sanitize_val_alu(env, insn); |
| 4041 | if (ret < 0) { |
| 4042 | verbose(env, "R%d tried to add from different pointers or scalars\n", dst); |
| 4043 | return ret; |
| 4044 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4045 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 4046 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 4047 | dst_reg->smin_value = S64_MIN; |
| 4048 | dst_reg->smax_value = S64_MAX; |
| 4049 | } else { |
| 4050 | dst_reg->smin_value += smin_val; |
| 4051 | dst_reg->smax_value += smax_val; |
| 4052 | } |
| 4053 | if (dst_reg->umin_value + umin_val < umin_val || |
| 4054 | dst_reg->umax_value + umax_val < umax_val) { |
| 4055 | dst_reg->umin_value = 0; |
| 4056 | dst_reg->umax_value = U64_MAX; |
| 4057 | } else { |
| 4058 | dst_reg->umin_value += umin_val; |
| 4059 | dst_reg->umax_value += umax_val; |
| 4060 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4061 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 4062 | break; |
| 4063 | case BPF_SUB: |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 4064 | ret = sanitize_val_alu(env, insn); |
| 4065 | if (ret < 0) { |
| 4066 | verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); |
| 4067 | return ret; |
| 4068 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4069 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 4070 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 4071 | /* Overflow possible, we know nothing */ |
| 4072 | dst_reg->smin_value = S64_MIN; |
| 4073 | dst_reg->smax_value = S64_MAX; |
| 4074 | } else { |
| 4075 | dst_reg->smin_value -= smax_val; |
| 4076 | dst_reg->smax_value -= smin_val; |
| 4077 | } |
| 4078 | if (dst_reg->umin_value < umax_val) { |
| 4079 | /* Overflow possible, we know nothing */ |
| 4080 | dst_reg->umin_value = 0; |
| 4081 | dst_reg->umax_value = U64_MAX; |
| 4082 | } else { |
| 4083 | /* Cannot overflow (as long as bounds are consistent) */ |
| 4084 | dst_reg->umin_value -= umax_val; |
| 4085 | dst_reg->umax_value -= umin_val; |
| 4086 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4087 | 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] | 4088 | break; |
| 4089 | case BPF_MUL: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4090 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 4091 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4092 | /* Ain't nobody got time to multiply that sign */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4093 | __mark_reg_unbounded(dst_reg); |
| 4094 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4095 | break; |
| 4096 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4097 | /* Both values are positive, so we can work with unsigned and |
| 4098 | * copy the result to signed (unless it exceeds S64_MAX). |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4099 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4100 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 4101 | /* Potential overflow, we know nothing */ |
| 4102 | __mark_reg_unbounded(dst_reg); |
| 4103 | /* (except what we can learn from the var_off) */ |
| 4104 | __update_reg_bounds(dst_reg); |
| 4105 | break; |
| 4106 | } |
| 4107 | dst_reg->umin_value *= umin_val; |
| 4108 | dst_reg->umax_value *= umax_val; |
| 4109 | if (dst_reg->umax_value > S64_MAX) { |
| 4110 | /* Overflow possible, we know nothing */ |
| 4111 | dst_reg->smin_value = S64_MIN; |
| 4112 | dst_reg->smax_value = S64_MAX; |
| 4113 | } else { |
| 4114 | dst_reg->smin_value = dst_reg->umin_value; |
| 4115 | dst_reg->smax_value = dst_reg->umax_value; |
| 4116 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4117 | break; |
| 4118 | case BPF_AND: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4119 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4120 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 4121 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4122 | break; |
| 4123 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4124 | /* We get our minimum from the var_off, since that's inherently |
| 4125 | * bitwise. Our maximum is the minimum of the operands' maxima. |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 4126 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4127 | 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] | 4128 | dst_reg->umin_value = dst_reg->var_off.value; |
| 4129 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 4130 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 4131 | /* Lose signed bounds when ANDing negative numbers, |
| 4132 | * ain't nobody got time for that. |
| 4133 | */ |
| 4134 | dst_reg->smin_value = S64_MIN; |
| 4135 | dst_reg->smax_value = S64_MAX; |
| 4136 | } else { |
| 4137 | /* ANDing two positives gives a positive, so safe to |
| 4138 | * cast result into s64. |
| 4139 | */ |
| 4140 | dst_reg->smin_value = dst_reg->umin_value; |
| 4141 | dst_reg->smax_value = dst_reg->umax_value; |
| 4142 | } |
| 4143 | /* We may learn something more from the var_off */ |
| 4144 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4145 | break; |
| 4146 | case BPF_OR: |
| 4147 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4148 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
| 4149 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4150 | break; |
| 4151 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4152 | /* We get our maximum from the var_off, and our minimum is the |
| 4153 | * maximum of the operands' minima |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4154 | */ |
| 4155 | 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] | 4156 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 4157 | dst_reg->umax_value = dst_reg->var_off.value | |
| 4158 | dst_reg->var_off.mask; |
| 4159 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 4160 | /* Lose signed bounds when ORing negative numbers, |
| 4161 | * ain't nobody got time for that. |
| 4162 | */ |
| 4163 | dst_reg->smin_value = S64_MIN; |
| 4164 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4165 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4166 | /* ORing two positives gives a positive, so safe to |
| 4167 | * cast result into s64. |
| 4168 | */ |
| 4169 | dst_reg->smin_value = dst_reg->umin_value; |
| 4170 | dst_reg->smax_value = dst_reg->umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4171 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4172 | /* We may learn something more from the var_off */ |
| 4173 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4174 | break; |
| 4175 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4176 | if (umax_val >= insn_bitness) { |
| 4177 | /* Shifts greater than 31 or 63 are undefined. |
| 4178 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4179 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4180 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4181 | break; |
| 4182 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4183 | /* We lose all sign bit information (except what we can pick |
| 4184 | * up from var_off) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4185 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4186 | dst_reg->smin_value = S64_MIN; |
| 4187 | dst_reg->smax_value = S64_MAX; |
| 4188 | /* If we might shift our top bit out, then we know nothing */ |
| 4189 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 4190 | dst_reg->umin_value = 0; |
| 4191 | dst_reg->umax_value = U64_MAX; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 4192 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4193 | dst_reg->umin_value <<= umin_val; |
| 4194 | dst_reg->umax_value <<= umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 4195 | } |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 4196 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4197 | /* We may learn something more from the var_off */ |
| 4198 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4199 | break; |
| 4200 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4201 | if (umax_val >= insn_bitness) { |
| 4202 | /* Shifts greater than 31 or 63 are undefined. |
| 4203 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4204 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4205 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4206 | break; |
| 4207 | } |
Edward Cree | 4374f25 | 2017-12-18 20:11:53 -0800 | [diff] [blame] | 4208 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 4209 | * be negative, then either: |
| 4210 | * 1) src_reg might be zero, so the sign bit of the result is |
| 4211 | * unknown, so we lose our signed bounds |
| 4212 | * 2) it's known negative, thus the unsigned bounds capture the |
| 4213 | * signed bounds |
| 4214 | * 3) the signed bounds cross zero, so they tell us nothing |
| 4215 | * about the result |
| 4216 | * If the value in dst_reg is known nonnegative, then again the |
| 4217 | * unsigned bounts capture the signed bounds. |
| 4218 | * Thus, in all cases it suffices to blow away our signed bounds |
| 4219 | * and rely on inferring new ones from the unsigned bounds and |
| 4220 | * var_off of the result. |
| 4221 | */ |
| 4222 | dst_reg->smin_value = S64_MIN; |
| 4223 | dst_reg->smax_value = S64_MAX; |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 4224 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4225 | dst_reg->umin_value >>= umax_val; |
| 4226 | dst_reg->umax_value >>= umin_val; |
| 4227 | /* We may learn something more from the var_off */ |
| 4228 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4229 | break; |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 4230 | case BPF_ARSH: |
| 4231 | if (umax_val >= insn_bitness) { |
| 4232 | /* Shifts greater than 31 or 63 are undefined. |
| 4233 | * This includes shifts by a negative number. |
| 4234 | */ |
| 4235 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 4236 | break; |
| 4237 | } |
| 4238 | |
| 4239 | /* Upon reaching here, src_known is true and |
| 4240 | * umax_val is equal to umin_val. |
| 4241 | */ |
| 4242 | dst_reg->smin_value >>= umin_val; |
| 4243 | dst_reg->smax_value >>= umin_val; |
| 4244 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val); |
| 4245 | |
| 4246 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 4247 | * dst_reg var_off to refine the result. |
| 4248 | */ |
| 4249 | dst_reg->umin_value = 0; |
| 4250 | dst_reg->umax_value = U64_MAX; |
| 4251 | __update_reg_bounds(dst_reg); |
| 4252 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4253 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4254 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4255 | break; |
| 4256 | } |
| 4257 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4258 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 4259 | /* 32-bit ALU ops are (32,32)->32 */ |
| 4260 | coerce_reg_to_size(dst_reg, 4); |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 4261 | } |
| 4262 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4263 | __reg_deduce_bounds(dst_reg); |
| 4264 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4265 | return 0; |
| 4266 | } |
| 4267 | |
| 4268 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 4269 | * and var_off. |
| 4270 | */ |
| 4271 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 4272 | struct bpf_insn *insn) |
| 4273 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4274 | struct bpf_verifier_state *vstate = env->cur_state; |
| 4275 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 4276 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4277 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 4278 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4279 | |
| 4280 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4281 | src_reg = NULL; |
| 4282 | if (dst_reg->type != SCALAR_VALUE) |
| 4283 | ptr_reg = dst_reg; |
| 4284 | if (BPF_SRC(insn->code) == BPF_X) { |
| 4285 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4286 | if (src_reg->type != SCALAR_VALUE) { |
| 4287 | if (dst_reg->type != SCALAR_VALUE) { |
| 4288 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4289 | * an arbitrary scalar. Disallow all math except |
| 4290 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4291 | */ |
Alexei Starovoitov | dd06682 | 2018-09-12 14:06:10 -0700 | [diff] [blame] | 4292 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4293 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 4294 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4295 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4296 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 4297 | insn->dst_reg, |
| 4298 | bpf_alu_string[opcode >> 4]); |
| 4299 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4300 | } else { |
| 4301 | /* scalar += pointer |
| 4302 | * This is legal, but we have to reverse our |
| 4303 | * src/dest handling in computing the range |
| 4304 | */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4305 | return adjust_ptr_min_max_vals(env, insn, |
| 4306 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4307 | } |
| 4308 | } else if (ptr_reg) { |
| 4309 | /* pointer += scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4310 | return adjust_ptr_min_max_vals(env, insn, |
| 4311 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4312 | } |
| 4313 | } else { |
| 4314 | /* Pretend the src is a reg with a known value, since we only |
| 4315 | * need to be able to read from this state. |
| 4316 | */ |
| 4317 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4318 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4319 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 4320 | if (ptr_reg) /* pointer += K */ |
| 4321 | return adjust_ptr_min_max_vals(env, insn, |
| 4322 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4323 | } |
| 4324 | |
| 4325 | /* Got here implies adding two SCALAR_VALUEs */ |
| 4326 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4327 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4328 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4329 | return -EINVAL; |
| 4330 | } |
| 4331 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4332 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4333 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4334 | return -EINVAL; |
| 4335 | } |
| 4336 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4337 | } |
| 4338 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4339 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4340 | 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] | 4341 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4342 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4343 | u8 opcode = BPF_OP(insn->code); |
| 4344 | int err; |
| 4345 | |
| 4346 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 4347 | if (opcode == BPF_NEG) { |
| 4348 | if (BPF_SRC(insn->code) != 0 || |
| 4349 | insn->src_reg != BPF_REG_0 || |
| 4350 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4351 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4352 | return -EINVAL; |
| 4353 | } |
| 4354 | } else { |
| 4355 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 4356 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 4357 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4358 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4359 | return -EINVAL; |
| 4360 | } |
| 4361 | } |
| 4362 | |
| 4363 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4364 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4365 | if (err) |
| 4366 | return err; |
| 4367 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4368 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4369 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4370 | insn->dst_reg); |
| 4371 | return -EACCES; |
| 4372 | } |
| 4373 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4374 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4375 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4376 | if (err) |
| 4377 | return err; |
| 4378 | |
| 4379 | } else if (opcode == BPF_MOV) { |
| 4380 | |
| 4381 | if (BPF_SRC(insn->code) == BPF_X) { |
| 4382 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4383 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4384 | return -EINVAL; |
| 4385 | } |
| 4386 | |
| 4387 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4388 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4389 | if (err) |
| 4390 | return err; |
| 4391 | } else { |
| 4392 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4393 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4394 | return -EINVAL; |
| 4395 | } |
| 4396 | } |
| 4397 | |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 4398 | /* check dest operand, mark as required later */ |
| 4399 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4400 | if (err) |
| 4401 | return err; |
| 4402 | |
| 4403 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4404 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
| 4405 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; |
| 4406 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4407 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 4408 | /* case: R1 = R2 |
| 4409 | * copy register state to dest reg |
| 4410 | */ |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4411 | *dst_reg = *src_reg; |
| 4412 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 4413 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4414 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4415 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4416 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4417 | verbose(env, |
| 4418 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4419 | insn->src_reg); |
| 4420 | return -EACCES; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4421 | } else if (src_reg->type == SCALAR_VALUE) { |
| 4422 | *dst_reg = *src_reg; |
| 4423 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 4424 | dst_reg->subreg_def = env->insn_idx + 1; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4425 | } else { |
| 4426 | mark_reg_unknown(env, regs, |
| 4427 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4428 | } |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 4429 | coerce_reg_to_size(dst_reg, 4); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4430 | } |
| 4431 | } else { |
| 4432 | /* case: R = imm |
| 4433 | * remember the value we stored into this reg |
| 4434 | */ |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 4435 | /* clear any state __mark_reg_known doesn't set */ |
| 4436 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4437 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 4438 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 4439 | __mark_reg_known(regs + insn->dst_reg, |
| 4440 | insn->imm); |
| 4441 | } else { |
| 4442 | __mark_reg_known(regs + insn->dst_reg, |
| 4443 | (u32)insn->imm); |
| 4444 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4448 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4449 | return -EINVAL; |
| 4450 | |
| 4451 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 4452 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4453 | if (BPF_SRC(insn->code) == BPF_X) { |
| 4454 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4455 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4456 | return -EINVAL; |
| 4457 | } |
| 4458 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4459 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4460 | if (err) |
| 4461 | return err; |
| 4462 | } else { |
| 4463 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4464 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4465 | return -EINVAL; |
| 4466 | } |
| 4467 | } |
| 4468 | |
| 4469 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4470 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4471 | if (err) |
| 4472 | return err; |
| 4473 | |
| 4474 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 4475 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4476 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4477 | return -EINVAL; |
| 4478 | } |
| 4479 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 4480 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 4481 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 4482 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 4483 | |
| 4484 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4485 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 4486 | return -EINVAL; |
| 4487 | } |
| 4488 | } |
| 4489 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4490 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4491 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4492 | if (err) |
| 4493 | return err; |
| 4494 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4495 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4496 | } |
| 4497 | |
| 4498 | return 0; |
| 4499 | } |
| 4500 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 4501 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
| 4502 | struct bpf_reg_state *dst_reg, |
| 4503 | enum bpf_reg_type type, u16 new_range) |
| 4504 | { |
| 4505 | struct bpf_reg_state *reg; |
| 4506 | int i; |
| 4507 | |
| 4508 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 4509 | reg = &state->regs[i]; |
| 4510 | if (reg->type == type && reg->id == dst_reg->id) |
| 4511 | /* keep the maximum range already checked */ |
| 4512 | reg->range = max(reg->range, new_range); |
| 4513 | } |
| 4514 | |
| 4515 | bpf_for_each_spilled_reg(i, state, reg) { |
| 4516 | if (!reg) |
| 4517 | continue; |
| 4518 | if (reg->type == type && reg->id == dst_reg->id) |
| 4519 | reg->range = max(reg->range, new_range); |
| 4520 | } |
| 4521 | } |
| 4522 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4523 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4524 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 4525 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4526 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4527 | { |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4528 | u16 new_range; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 4529 | int i; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4530 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4531 | if (dst_reg->off < 0 || |
| 4532 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4533 | /* This doesn't give us any range */ |
| 4534 | return; |
| 4535 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4536 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 4537 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4538 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 4539 | * than pkt_end, but that's because it's also less than pkt. |
| 4540 | */ |
| 4541 | return; |
| 4542 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4543 | new_range = dst_reg->off; |
| 4544 | if (range_right_open) |
| 4545 | new_range--; |
| 4546 | |
| 4547 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4548 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4549 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4550 | * |
| 4551 | * r2 = r3; |
| 4552 | * r2 += 8; |
| 4553 | * if (r2 > pkt_end) goto <handle exception> |
| 4554 | * <access okay> |
| 4555 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4556 | * r2 = r3; |
| 4557 | * r2 += 8; |
| 4558 | * if (r2 < pkt_end) goto <access okay> |
| 4559 | * <handle exception> |
| 4560 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4561 | * Where: |
| 4562 | * r2 == dst_reg, pkt_end == src_reg |
| 4563 | * r2=pkt(id=n,off=8,r=0) |
| 4564 | * r3=pkt(id=n,off=0,r=0) |
| 4565 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 4566 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4567 | * |
| 4568 | * r2 = r3; |
| 4569 | * r2 += 8; |
| 4570 | * if (pkt_end >= r2) goto <access okay> |
| 4571 | * <handle exception> |
| 4572 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4573 | * r2 = r3; |
| 4574 | * r2 += 8; |
| 4575 | * if (pkt_end <= r2) goto <handle exception> |
| 4576 | * <access okay> |
| 4577 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4578 | * Where: |
| 4579 | * pkt_end == dst_reg, r2 == src_reg |
| 4580 | * r2=pkt(id=n,off=8,r=0) |
| 4581 | * r3=pkt(id=n,off=0,r=0) |
| 4582 | * |
| 4583 | * 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] | 4584 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 4585 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 4586 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4587 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 4588 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4589 | /* If our ids match, then we must have the same max_value. And we |
| 4590 | * don't care about the other reg's fixed offset, since if it's too big |
| 4591 | * the range won't allow anything. |
| 4592 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 4593 | */ |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 4594 | for (i = 0; i <= vstate->curframe; i++) |
| 4595 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, |
| 4596 | new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4597 | } |
| 4598 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4599 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
| 4600 | * and return: |
| 4601 | * 1 - branch will be taken and "goto target" will be executed |
| 4602 | * 0 - branch will not be taken and fall-through to next insn |
| 4603 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10] |
| 4604 | */ |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4605 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
| 4606 | bool is_jmp32) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4607 | { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4608 | struct bpf_reg_state reg_lo; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4609 | s64 sval; |
| 4610 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4611 | if (__is_pointer_value(false, reg)) |
| 4612 | return -1; |
| 4613 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4614 | if (is_jmp32) { |
| 4615 | reg_lo = *reg; |
| 4616 | reg = ®_lo; |
| 4617 | /* For JMP32, only low 32 bits are compared, coerce_reg_to_size |
| 4618 | * could truncate high bits and update umin/umax according to |
| 4619 | * information of low bits. |
| 4620 | */ |
| 4621 | coerce_reg_to_size(reg, 4); |
| 4622 | /* smin/smax need special handling. For example, after coerce, |
| 4623 | * if smin_value is 0x00000000ffffffffLL, the value is -1 when |
| 4624 | * used as operand to JMP32. It is a negative number from s32's |
| 4625 | * point of view, while it is a positive number when seen as |
| 4626 | * s64. The smin/smax are kept as s64, therefore, when used with |
| 4627 | * JMP32, they need to be transformed into s32, then sign |
| 4628 | * extended back to s64. |
| 4629 | * |
| 4630 | * Also, smin/smax were copied from umin/umax. If umin/umax has |
| 4631 | * different sign bit, then min/max relationship doesn't |
| 4632 | * maintain after casting into s32, for this case, set smin/smax |
| 4633 | * to safest range. |
| 4634 | */ |
| 4635 | if ((reg->umax_value ^ reg->umin_value) & |
| 4636 | (1ULL << 31)) { |
| 4637 | reg->smin_value = S32_MIN; |
| 4638 | reg->smax_value = S32_MAX; |
| 4639 | } |
| 4640 | reg->smin_value = (s64)(s32)reg->smin_value; |
| 4641 | reg->smax_value = (s64)(s32)reg->smax_value; |
| 4642 | |
| 4643 | val = (u32)val; |
| 4644 | sval = (s64)(s32)val; |
| 4645 | } else { |
| 4646 | sval = (s64)val; |
| 4647 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4648 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4649 | switch (opcode) { |
| 4650 | case BPF_JEQ: |
| 4651 | if (tnum_is_const(reg->var_off)) |
| 4652 | return !!tnum_equals_const(reg->var_off, val); |
| 4653 | break; |
| 4654 | case BPF_JNE: |
| 4655 | if (tnum_is_const(reg->var_off)) |
| 4656 | return !tnum_equals_const(reg->var_off, val); |
| 4657 | break; |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 4658 | case BPF_JSET: |
| 4659 | if ((~reg->var_off.mask & reg->var_off.value) & val) |
| 4660 | return 1; |
| 4661 | if (!((reg->var_off.mask | reg->var_off.value) & val)) |
| 4662 | return 0; |
| 4663 | break; |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4664 | case BPF_JGT: |
| 4665 | if (reg->umin_value > val) |
| 4666 | return 1; |
| 4667 | else if (reg->umax_value <= val) |
| 4668 | return 0; |
| 4669 | break; |
| 4670 | case BPF_JSGT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4671 | if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4672 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4673 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4674 | return 0; |
| 4675 | break; |
| 4676 | case BPF_JLT: |
| 4677 | if (reg->umax_value < val) |
| 4678 | return 1; |
| 4679 | else if (reg->umin_value >= val) |
| 4680 | return 0; |
| 4681 | break; |
| 4682 | case BPF_JSLT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4683 | if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4684 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4685 | else if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4686 | return 0; |
| 4687 | break; |
| 4688 | case BPF_JGE: |
| 4689 | if (reg->umin_value >= val) |
| 4690 | return 1; |
| 4691 | else if (reg->umax_value < val) |
| 4692 | return 0; |
| 4693 | break; |
| 4694 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4695 | if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4696 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4697 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4698 | return 0; |
| 4699 | break; |
| 4700 | case BPF_JLE: |
| 4701 | if (reg->umax_value <= val) |
| 4702 | return 1; |
| 4703 | else if (reg->umin_value > val) |
| 4704 | return 0; |
| 4705 | break; |
| 4706 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4707 | if (reg->smax_value <= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4708 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4709 | else if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4710 | return 0; |
| 4711 | break; |
| 4712 | } |
| 4713 | |
| 4714 | return -1; |
| 4715 | } |
| 4716 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4717 | /* Generate min value of the high 32-bit from TNUM info. */ |
| 4718 | static u64 gen_hi_min(struct tnum var) |
| 4719 | { |
| 4720 | return var.value & ~0xffffffffULL; |
| 4721 | } |
| 4722 | |
| 4723 | /* Generate max value of the high 32-bit from TNUM info. */ |
| 4724 | static u64 gen_hi_max(struct tnum var) |
| 4725 | { |
| 4726 | return (var.value | var.mask) & ~0xffffffffULL; |
| 4727 | } |
| 4728 | |
| 4729 | /* Return true if VAL is compared with a s64 sign extended from s32, and they |
| 4730 | * are with the same signedness. |
| 4731 | */ |
| 4732 | static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg) |
| 4733 | { |
| 4734 | return ((s32)sval >= 0 && |
| 4735 | reg->smin_value >= 0 && reg->smax_value <= S32_MAX) || |
| 4736 | ((s32)sval < 0 && |
| 4737 | reg->smax_value <= 0 && reg->smin_value >= S32_MIN); |
| 4738 | } |
| 4739 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4740 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 4741 | * variable register that we are working on, and src_reg is a constant or we're |
| 4742 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4743 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4744 | */ |
| 4745 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 4746 | struct bpf_reg_state *false_reg, u64 val, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4747 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4748 | { |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4749 | s64 sval; |
| 4750 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4751 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 4752 | * variable offset from the compare (unless src_reg were a pointer into |
| 4753 | * the same object, but we don't bother with that. |
| 4754 | * Since false_reg and true_reg have the same type by construction, we |
| 4755 | * only need to check one of them for pointerness. |
| 4756 | */ |
| 4757 | if (__is_pointer_value(false, false_reg)) |
| 4758 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 4759 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4760 | val = is_jmp32 ? (u32)val : val; |
| 4761 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4762 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4763 | switch (opcode) { |
| 4764 | case BPF_JEQ: |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4765 | case BPF_JNE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4766 | { |
| 4767 | struct bpf_reg_state *reg = |
| 4768 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 4769 | |
| 4770 | /* For BPF_JEQ, if this is false we know nothing Jon Snow, but |
| 4771 | * if it is true we know the value for sure. Likewise for |
| 4772 | * BPF_JNE. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4773 | */ |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4774 | if (is_jmp32) { |
| 4775 | u64 old_v = reg->var_off.value; |
| 4776 | u64 hi_mask = ~0xffffffffULL; |
| 4777 | |
| 4778 | reg->var_off.value = (old_v & hi_mask) | val; |
| 4779 | reg->var_off.mask &= hi_mask; |
| 4780 | } else { |
| 4781 | __mark_reg_known(reg, val); |
| 4782 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4783 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4784 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 4785 | case BPF_JSET: |
| 4786 | false_reg->var_off = tnum_and(false_reg->var_off, |
| 4787 | tnum_const(~val)); |
| 4788 | if (is_power_of_2(val)) |
| 4789 | true_reg->var_off = tnum_or(true_reg->var_off, |
| 4790 | tnum_const(val)); |
| 4791 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4792 | case BPF_JGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4793 | case BPF_JGT: |
| 4794 | { |
| 4795 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; |
| 4796 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; |
| 4797 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4798 | if (is_jmp32) { |
| 4799 | false_umax += gen_hi_max(false_reg->var_off); |
| 4800 | true_umin += gen_hi_min(true_reg->var_off); |
| 4801 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4802 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 4803 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4804 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4805 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4806 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4807 | case BPF_JSGT: |
| 4808 | { |
| 4809 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; |
| 4810 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; |
| 4811 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4812 | /* If the full s64 was not sign-extended from s32 then don't |
| 4813 | * deduct further info. |
| 4814 | */ |
| 4815 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 4816 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4817 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 4818 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4819 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4820 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4821 | case BPF_JLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4822 | case BPF_JLT: |
| 4823 | { |
| 4824 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; |
| 4825 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; |
| 4826 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4827 | if (is_jmp32) { |
| 4828 | false_umin += gen_hi_min(false_reg->var_off); |
| 4829 | true_umax += gen_hi_max(true_reg->var_off); |
| 4830 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4831 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 4832 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4833 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4834 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4835 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4836 | case BPF_JSLT: |
| 4837 | { |
| 4838 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; |
| 4839 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; |
| 4840 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4841 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 4842 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4843 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 4844 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4845 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4846 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4847 | default: |
| 4848 | break; |
| 4849 | } |
| 4850 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4851 | __reg_deduce_bounds(false_reg); |
| 4852 | __reg_deduce_bounds(true_reg); |
| 4853 | /* We might have learned some bits from the bounds. */ |
| 4854 | __reg_bound_offset(false_reg); |
| 4855 | __reg_bound_offset(true_reg); |
| 4856 | /* Intersecting with the old var_off might have improved our bounds |
| 4857 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 4858 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 4859 | */ |
| 4860 | __update_reg_bounds(false_reg); |
| 4861 | __update_reg_bounds(true_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4862 | } |
| 4863 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4864 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 4865 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4866 | */ |
| 4867 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 4868 | struct bpf_reg_state *false_reg, u64 val, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4869 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4870 | { |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4871 | s64 sval; |
| 4872 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4873 | if (__is_pointer_value(false, false_reg)) |
| 4874 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 4875 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4876 | val = is_jmp32 ? (u32)val : val; |
| 4877 | sval = is_jmp32 ? (s64)(s32)val : (s64)val; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4878 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4879 | switch (opcode) { |
| 4880 | case BPF_JEQ: |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4881 | case BPF_JNE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4882 | { |
| 4883 | struct bpf_reg_state *reg = |
| 4884 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 4885 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4886 | if (is_jmp32) { |
| 4887 | u64 old_v = reg->var_off.value; |
| 4888 | u64 hi_mask = ~0xffffffffULL; |
| 4889 | |
| 4890 | reg->var_off.value = (old_v & hi_mask) | val; |
| 4891 | reg->var_off.mask &= hi_mask; |
| 4892 | } else { |
| 4893 | __mark_reg_known(reg, val); |
| 4894 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4895 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4896 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 4897 | case BPF_JSET: |
| 4898 | false_reg->var_off = tnum_and(false_reg->var_off, |
| 4899 | tnum_const(~val)); |
| 4900 | if (is_power_of_2(val)) |
| 4901 | true_reg->var_off = tnum_or(true_reg->var_off, |
| 4902 | tnum_const(val)); |
| 4903 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4904 | case BPF_JGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4905 | case BPF_JGT: |
| 4906 | { |
| 4907 | u64 false_umin = opcode == BPF_JGT ? val : val + 1; |
| 4908 | u64 true_umax = opcode == BPF_JGT ? val - 1 : val; |
| 4909 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4910 | if (is_jmp32) { |
| 4911 | false_umin += gen_hi_min(false_reg->var_off); |
| 4912 | true_umax += gen_hi_max(true_reg->var_off); |
| 4913 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4914 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 4915 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4916 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4917 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4918 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4919 | case BPF_JSGT: |
| 4920 | { |
| 4921 | s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1; |
| 4922 | s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval; |
| 4923 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4924 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 4925 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4926 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 4927 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4928 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4929 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4930 | case BPF_JLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4931 | case BPF_JLT: |
| 4932 | { |
| 4933 | u64 false_umax = opcode == BPF_JLT ? val : val - 1; |
| 4934 | u64 true_umin = opcode == BPF_JLT ? val + 1 : val; |
| 4935 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4936 | if (is_jmp32) { |
| 4937 | false_umax += gen_hi_max(false_reg->var_off); |
| 4938 | true_umin += gen_hi_min(true_reg->var_off); |
| 4939 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4940 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 4941 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4942 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4943 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4944 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4945 | case BPF_JSLT: |
| 4946 | { |
| 4947 | s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1; |
| 4948 | s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval; |
| 4949 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 4950 | if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg)) |
| 4951 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4952 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 4953 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4954 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 4955 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4956 | default: |
| 4957 | break; |
| 4958 | } |
| 4959 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4960 | __reg_deduce_bounds(false_reg); |
| 4961 | __reg_deduce_bounds(true_reg); |
| 4962 | /* We might have learned some bits from the bounds. */ |
| 4963 | __reg_bound_offset(false_reg); |
| 4964 | __reg_bound_offset(true_reg); |
| 4965 | /* Intersecting with the old var_off might have improved our bounds |
| 4966 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 4967 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 4968 | */ |
| 4969 | __update_reg_bounds(false_reg); |
| 4970 | __update_reg_bounds(true_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4971 | } |
| 4972 | |
| 4973 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 4974 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 4975 | struct bpf_reg_state *dst_reg) |
| 4976 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4977 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 4978 | dst_reg->umin_value); |
| 4979 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 4980 | dst_reg->umax_value); |
| 4981 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 4982 | dst_reg->smin_value); |
| 4983 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 4984 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4985 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 4986 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4987 | /* We might have learned new bounds from the var_off. */ |
| 4988 | __update_reg_bounds(src_reg); |
| 4989 | __update_reg_bounds(dst_reg); |
| 4990 | /* We might have learned something about the sign bit. */ |
| 4991 | __reg_deduce_bounds(src_reg); |
| 4992 | __reg_deduce_bounds(dst_reg); |
| 4993 | /* We might have learned some bits from the bounds. */ |
| 4994 | __reg_bound_offset(src_reg); |
| 4995 | __reg_bound_offset(dst_reg); |
| 4996 | /* Intersecting with the old var_off might have improved our bounds |
| 4997 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 4998 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 4999 | */ |
| 5000 | __update_reg_bounds(src_reg); |
| 5001 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5002 | } |
| 5003 | |
| 5004 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 5005 | struct bpf_reg_state *true_dst, |
| 5006 | struct bpf_reg_state *false_src, |
| 5007 | struct bpf_reg_state *false_dst, |
| 5008 | u8 opcode) |
| 5009 | { |
| 5010 | switch (opcode) { |
| 5011 | case BPF_JEQ: |
| 5012 | __reg_combine_min_max(true_src, true_dst); |
| 5013 | break; |
| 5014 | case BPF_JNE: |
| 5015 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5016 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 5017 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5018 | } |
| 5019 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5020 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 5021 | struct bpf_reg_state *reg, u32 id, |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5022 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5023 | { |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5024 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5025 | /* Old offset (both fixed and variable parts) should |
| 5026 | * have been known-zero, because we don't allow pointer |
| 5027 | * arithmetic on pointers that might be NULL. |
| 5028 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5029 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 5030 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5031 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5032 | __mark_reg_known_zero(reg); |
| 5033 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5034 | } |
| 5035 | if (is_null) { |
| 5036 | reg->type = SCALAR_VALUE; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5037 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
| 5038 | if (reg->map_ptr->inner_map_meta) { |
| 5039 | reg->type = CONST_PTR_TO_MAP; |
| 5040 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
| 5041 | } else { |
| 5042 | reg->type = PTR_TO_MAP_VALUE; |
| 5043 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5044 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
| 5045 | reg->type = PTR_TO_SOCKET; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 5046 | } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
| 5047 | reg->type = PTR_TO_SOCK_COMMON; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 5048 | } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
| 5049 | reg->type = PTR_TO_TCP_SOCK; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5050 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5051 | if (is_null) { |
| 5052 | /* We don't need id and ref_obj_id from this point |
| 5053 | * onwards anymore, thus we should better reset it, |
| 5054 | * so that state pruning has chances to take effect. |
| 5055 | */ |
| 5056 | reg->id = 0; |
| 5057 | reg->ref_obj_id = 0; |
| 5058 | } else if (!reg_may_point_to_spin_lock(reg)) { |
| 5059 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
| 5060 | * in release_reg_references(). |
| 5061 | * |
| 5062 | * reg->id is still used by spin_lock ptr. Other |
| 5063 | * than spin_lock ptr type, reg->id can be reset. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5064 | */ |
| 5065 | reg->id = 0; |
| 5066 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5067 | } |
| 5068 | } |
| 5069 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5070 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
| 5071 | bool is_null) |
| 5072 | { |
| 5073 | struct bpf_reg_state *reg; |
| 5074 | int i; |
| 5075 | |
| 5076 | for (i = 0; i < MAX_BPF_REG; i++) |
| 5077 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); |
| 5078 | |
| 5079 | bpf_for_each_spilled_reg(i, state, reg) { |
| 5080 | if (!reg) |
| 5081 | continue; |
| 5082 | mark_ptr_or_null_reg(state, reg, id, is_null); |
| 5083 | } |
| 5084 | } |
| 5085 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5086 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 5087 | * be folded together at some point. |
| 5088 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5089 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 5090 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5091 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5092 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5093 | struct bpf_reg_state *regs = state->regs; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5094 | u32 ref_obj_id = regs[regno].ref_obj_id; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 5095 | u32 id = regs[regno].id; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5096 | int i; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5097 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5098 | if (ref_obj_id && ref_obj_id == id && is_null) |
| 5099 | /* regs[regno] is in the " == NULL" branch. |
| 5100 | * No one could have freed the reference state before |
| 5101 | * doing the NULL check. |
| 5102 | */ |
| 5103 | WARN_ON_ONCE(release_reference_state(state, id)); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5104 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 5105 | for (i = 0; i <= vstate->curframe; i++) |
| 5106 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5107 | } |
| 5108 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 5109 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 5110 | struct bpf_reg_state *dst_reg, |
| 5111 | struct bpf_reg_state *src_reg, |
| 5112 | struct bpf_verifier_state *this_branch, |
| 5113 | struct bpf_verifier_state *other_branch) |
| 5114 | { |
| 5115 | if (BPF_SRC(insn->code) != BPF_X) |
| 5116 | return false; |
| 5117 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5118 | /* Pointers are always 64-bit. */ |
| 5119 | if (BPF_CLASS(insn->code) == BPF_JMP32) |
| 5120 | return false; |
| 5121 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 5122 | switch (BPF_OP(insn->code)) { |
| 5123 | case BPF_JGT: |
| 5124 | if ((dst_reg->type == PTR_TO_PACKET && |
| 5125 | src_reg->type == PTR_TO_PACKET_END) || |
| 5126 | (dst_reg->type == PTR_TO_PACKET_META && |
| 5127 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 5128 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 5129 | find_good_pkt_pointers(this_branch, dst_reg, |
| 5130 | dst_reg->type, false); |
| 5131 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 5132 | src_reg->type == PTR_TO_PACKET) || |
| 5133 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 5134 | src_reg->type == PTR_TO_PACKET_META)) { |
| 5135 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 5136 | find_good_pkt_pointers(other_branch, src_reg, |
| 5137 | src_reg->type, true); |
| 5138 | } else { |
| 5139 | return false; |
| 5140 | } |
| 5141 | break; |
| 5142 | case BPF_JLT: |
| 5143 | if ((dst_reg->type == PTR_TO_PACKET && |
| 5144 | src_reg->type == PTR_TO_PACKET_END) || |
| 5145 | (dst_reg->type == PTR_TO_PACKET_META && |
| 5146 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 5147 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 5148 | find_good_pkt_pointers(other_branch, dst_reg, |
| 5149 | dst_reg->type, true); |
| 5150 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 5151 | src_reg->type == PTR_TO_PACKET) || |
| 5152 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 5153 | src_reg->type == PTR_TO_PACKET_META)) { |
| 5154 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 5155 | find_good_pkt_pointers(this_branch, src_reg, |
| 5156 | src_reg->type, false); |
| 5157 | } else { |
| 5158 | return false; |
| 5159 | } |
| 5160 | break; |
| 5161 | case BPF_JGE: |
| 5162 | if ((dst_reg->type == PTR_TO_PACKET && |
| 5163 | src_reg->type == PTR_TO_PACKET_END) || |
| 5164 | (dst_reg->type == PTR_TO_PACKET_META && |
| 5165 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 5166 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 5167 | find_good_pkt_pointers(this_branch, dst_reg, |
| 5168 | dst_reg->type, true); |
| 5169 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 5170 | src_reg->type == PTR_TO_PACKET) || |
| 5171 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 5172 | src_reg->type == PTR_TO_PACKET_META)) { |
| 5173 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 5174 | find_good_pkt_pointers(other_branch, src_reg, |
| 5175 | src_reg->type, false); |
| 5176 | } else { |
| 5177 | return false; |
| 5178 | } |
| 5179 | break; |
| 5180 | case BPF_JLE: |
| 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(other_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(this_branch, src_reg, |
| 5194 | src_reg->type, true); |
| 5195 | } else { |
| 5196 | return false; |
| 5197 | } |
| 5198 | break; |
| 5199 | default: |
| 5200 | return false; |
| 5201 | } |
| 5202 | |
| 5203 | return true; |
| 5204 | } |
| 5205 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5206 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5207 | struct bpf_insn *insn, int *insn_idx) |
| 5208 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5209 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 5210 | struct bpf_verifier_state *other_branch; |
| 5211 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
| 5212 | struct bpf_reg_state *dst_reg, *other_branch_regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5213 | u8 opcode = BPF_OP(insn->code); |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5214 | bool is_jmp32; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5215 | int err; |
| 5216 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5217 | /* Only conditional jumps are expected to reach here. */ |
| 5218 | if (opcode == BPF_JA || opcode > BPF_JSLE) { |
| 5219 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5220 | return -EINVAL; |
| 5221 | } |
| 5222 | |
| 5223 | if (BPF_SRC(insn->code) == BPF_X) { |
| 5224 | if (insn->imm != 0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5225 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5226 | return -EINVAL; |
| 5227 | } |
| 5228 | |
| 5229 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5230 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5231 | if (err) |
| 5232 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5233 | |
| 5234 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5235 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5236 | insn->src_reg); |
| 5237 | return -EACCES; |
| 5238 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5239 | } else { |
| 5240 | if (insn->src_reg != BPF_REG_0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5241 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5242 | return -EINVAL; |
| 5243 | } |
| 5244 | } |
| 5245 | |
| 5246 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5247 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5248 | if (err) |
| 5249 | return err; |
| 5250 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 5251 | dst_reg = ®s[insn->dst_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5252 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 5253 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5254 | if (BPF_SRC(insn->code) == BPF_K) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5255 | int pred = is_branch_taken(dst_reg, insn->imm, opcode, |
| 5256 | is_jmp32); |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5257 | |
| 5258 | if (pred == 1) { |
| 5259 | /* only follow the goto, ignore fall-through */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5260 | *insn_idx += insn->off; |
| 5261 | return 0; |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 5262 | } else if (pred == 0) { |
| 5263 | /* only follow fall-through branch, since |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5264 | * that's where the program will go |
| 5265 | */ |
| 5266 | return 0; |
| 5267 | } |
| 5268 | } |
| 5269 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 5270 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
| 5271 | false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5272 | if (!other_branch) |
| 5273 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5274 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5275 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5276 | /* detect if we are comparing against a constant value so we can adjust |
| 5277 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5278 | * this is only legit if both are scalars (or pointers to the same |
| 5279 | * object, I suppose, but we don't support that right now), because |
| 5280 | * otherwise the different base pointers mean the offsets aren't |
| 5281 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5282 | */ |
| 5283 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5284 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
| 5285 | struct bpf_reg_state lo_reg0 = *dst_reg; |
| 5286 | struct bpf_reg_state lo_reg1 = *src_reg; |
| 5287 | struct bpf_reg_state *src_lo, *dst_lo; |
| 5288 | |
| 5289 | dst_lo = &lo_reg0; |
| 5290 | src_lo = &lo_reg1; |
| 5291 | coerce_reg_to_size(dst_lo, 4); |
| 5292 | coerce_reg_to_size(src_lo, 4); |
| 5293 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5294 | if (dst_reg->type == SCALAR_VALUE && |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5295 | src_reg->type == SCALAR_VALUE) { |
| 5296 | if (tnum_is_const(src_reg->var_off) || |
| 5297 | (is_jmp32 && tnum_is_const(src_lo->var_off))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5298 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5299 | dst_reg, |
| 5300 | is_jmp32 |
| 5301 | ? src_lo->var_off.value |
| 5302 | : src_reg->var_off.value, |
| 5303 | opcode, is_jmp32); |
| 5304 | else if (tnum_is_const(dst_reg->var_off) || |
| 5305 | (is_jmp32 && tnum_is_const(dst_lo->var_off))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5306 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5307 | src_reg, |
| 5308 | is_jmp32 |
| 5309 | ? dst_lo->var_off.value |
| 5310 | : dst_reg->var_off.value, |
| 5311 | opcode, is_jmp32); |
| 5312 | else if (!is_jmp32 && |
| 5313 | (opcode == BPF_JEQ || opcode == BPF_JNE)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5314 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5315 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 5316 | &other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5317 | src_reg, dst_reg, opcode); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5318 | } |
| 5319 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5320 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5321 | dst_reg, insn->imm, opcode, is_jmp32); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 5322 | } |
| 5323 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5324 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
| 5325 | * NOTE: these optimizations below are related with pointer comparison |
| 5326 | * which will never be JMP32. |
| 5327 | */ |
| 5328 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 5329 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5330 | reg_type_may_be_null(dst_reg->type)) { |
| 5331 | /* Mark all identical registers in each branch as either |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 5332 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 5333 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 5334 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 5335 | opcode == BPF_JNE); |
| 5336 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 5337 | opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 5338 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 5339 | this_branch, other_branch) && |
| 5340 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5341 | verbose(env, "R%d pointer comparison prohibited\n", |
| 5342 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5343 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5344 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 5345 | if (env->log.level & BPF_LOG_LEVEL) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5346 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5347 | return 0; |
| 5348 | } |
| 5349 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5350 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5351 | 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] | 5352 | { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 5353 | struct bpf_insn_aux_data *aux = cur_aux(env); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5354 | struct bpf_reg_state *regs = cur_regs(env); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 5355 | struct bpf_map *map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5356 | int err; |
| 5357 | |
| 5358 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5359 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5360 | return -EINVAL; |
| 5361 | } |
| 5362 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5363 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5364 | return -EINVAL; |
| 5365 | } |
| 5366 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5367 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5368 | if (err) |
| 5369 | return err; |
| 5370 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 5371 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 5372 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 5373 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5374 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5375 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5376 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 5377 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5378 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 5379 | map = env->used_maps[aux->map_index]; |
| 5380 | mark_reg_known_zero(env, regs, insn->dst_reg); |
| 5381 | regs[insn->dst_reg].map_ptr = map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5382 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 5383 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) { |
| 5384 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; |
| 5385 | regs[insn->dst_reg].off = aux->map_off; |
| 5386 | if (map_value_has_spin_lock(map)) |
| 5387 | regs[insn->dst_reg].id = ++env->id_gen; |
| 5388 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 5389 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 5390 | } else { |
| 5391 | verbose(env, "bpf verifier is misconfigured\n"); |
| 5392 | return -EINVAL; |
| 5393 | } |
| 5394 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5395 | return 0; |
| 5396 | } |
| 5397 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 5398 | static bool may_access_skb(enum bpf_prog_type type) |
| 5399 | { |
| 5400 | switch (type) { |
| 5401 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 5402 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 5403 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 5404 | return true; |
| 5405 | default: |
| 5406 | return false; |
| 5407 | } |
| 5408 | } |
| 5409 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5410 | /* verify safety of LD_ABS|LD_IND instructions: |
| 5411 | * - they can only appear in the programs where ctx == skb |
| 5412 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 5413 | * preserve R6-R9, and store return value into R0 |
| 5414 | * |
| 5415 | * Implicit input: |
| 5416 | * ctx == skb == R6 == CTX |
| 5417 | * |
| 5418 | * Explicit input: |
| 5419 | * SRC == any register |
| 5420 | * IMM == 32-bit immediate |
| 5421 | * |
| 5422 | * Output: |
| 5423 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 5424 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5425 | 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] | 5426 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5427 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5428 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5429 | int i, err; |
| 5430 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 5431 | if (!may_access_skb(env->prog->type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5432 | 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] | 5433 | return -EINVAL; |
| 5434 | } |
| 5435 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 5436 | if (!env->ops->gen_ld_abs) { |
| 5437 | verbose(env, "bpf verifier is misconfigured\n"); |
| 5438 | return -EINVAL; |
| 5439 | } |
| 5440 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5441 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5442 | /* when program has LD_ABS insn JITs and interpreter assume |
| 5443 | * that r1 == ctx == skb which is not the case for callees |
| 5444 | * that can have arbitrary arguments. It's problematic |
| 5445 | * for main prog as well since JITs would need to analyze |
| 5446 | * all functions in order to make proper register save/restore |
| 5447 | * decisions in the main prog. Hence disallow LD_ABS with calls |
| 5448 | */ |
| 5449 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); |
| 5450 | return -EINVAL; |
| 5451 | } |
| 5452 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5453 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 5454 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5455 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5456 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5457 | return -EINVAL; |
| 5458 | } |
| 5459 | |
| 5460 | /* check whether implicit source operand (register R6) is readable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5461 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5462 | if (err) |
| 5463 | return err; |
| 5464 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5465 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 5466 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 5467 | * reference leak. |
| 5468 | */ |
| 5469 | err = check_reference_leak(env); |
| 5470 | if (err) { |
| 5471 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 5472 | return err; |
| 5473 | } |
| 5474 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 5475 | if (env->cur_state->active_spin_lock) { |
| 5476 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
| 5477 | return -EINVAL; |
| 5478 | } |
| 5479 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5480 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5481 | verbose(env, |
| 5482 | "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] | 5483 | return -EINVAL; |
| 5484 | } |
| 5485 | |
| 5486 | if (mode == BPF_IND) { |
| 5487 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5488 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5489 | if (err) |
| 5490 | return err; |
| 5491 | } |
| 5492 | |
| 5493 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5494 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5495 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5496 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 5497 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5498 | |
| 5499 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5500 | * the value fetched from the packet. |
| 5501 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5502 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5503 | mark_reg_unknown(env, regs, BPF_REG_0); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 5504 | /* ld_abs load up to 32-bit skb data. */ |
| 5505 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5506 | return 0; |
| 5507 | } |
| 5508 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5509 | static int check_return_code(struct bpf_verifier_env *env) |
| 5510 | { |
| 5511 | struct bpf_reg_state *reg; |
| 5512 | struct tnum range = tnum_range(0, 1); |
| 5513 | |
| 5514 | switch (env->prog->type) { |
| 5515 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 5516 | case BPF_PROG_TYPE_CGROUP_SOCK: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 5517 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5518 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 5519 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 5520 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5521 | break; |
| 5522 | default: |
| 5523 | return 0; |
| 5524 | } |
| 5525 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5526 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5527 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5528 | 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] | 5529 | reg_type_str[reg->type]); |
| 5530 | return -EINVAL; |
| 5531 | } |
| 5532 | |
| 5533 | if (!tnum_in(range, reg->var_off)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5534 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5535 | if (!tnum_is_unknown(reg->var_off)) { |
| 5536 | char tn_buf[48]; |
| 5537 | |
| 5538 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5539 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5540 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5541 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5542 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5543 | verbose(env, " should have been 0 or 1\n"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5544 | return -EINVAL; |
| 5545 | } |
| 5546 | return 0; |
| 5547 | } |
| 5548 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5549 | /* non-recursive DFS pseudo code |
| 5550 | * 1 procedure DFS-iterative(G,v): |
| 5551 | * 2 label v as discovered |
| 5552 | * 3 let S be a stack |
| 5553 | * 4 S.push(v) |
| 5554 | * 5 while S is not empty |
| 5555 | * 6 t <- S.pop() |
| 5556 | * 7 if t is what we're looking for: |
| 5557 | * 8 return t |
| 5558 | * 9 for all edges e in G.adjacentEdges(t) do |
| 5559 | * 10 if edge e is already labelled |
| 5560 | * 11 continue with the next edge |
| 5561 | * 12 w <- G.adjacentVertex(t,e) |
| 5562 | * 13 if vertex w is not discovered and not explored |
| 5563 | * 14 label e as tree-edge |
| 5564 | * 15 label w as discovered |
| 5565 | * 16 S.push(w) |
| 5566 | * 17 continue at 5 |
| 5567 | * 18 else if vertex w is discovered |
| 5568 | * 19 label e as back-edge |
| 5569 | * 20 else |
| 5570 | * 21 // vertex w is explored |
| 5571 | * 22 label e as forward- or cross-edge |
| 5572 | * 23 label t as explored |
| 5573 | * 24 S.pop() |
| 5574 | * |
| 5575 | * convention: |
| 5576 | * 0x10 - discovered |
| 5577 | * 0x11 - discovered and fall-through edge labelled |
| 5578 | * 0x12 - discovered and fall-through and branch edges labelled |
| 5579 | * 0x20 - explored |
| 5580 | */ |
| 5581 | |
| 5582 | enum { |
| 5583 | DISCOVERED = 0x10, |
| 5584 | EXPLORED = 0x20, |
| 5585 | FALLTHROUGH = 1, |
| 5586 | BRANCH = 2, |
| 5587 | }; |
| 5588 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 5589 | static u32 state_htab_size(struct bpf_verifier_env *env) |
| 5590 | { |
| 5591 | return env->prog->len; |
| 5592 | } |
| 5593 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5594 | static struct bpf_verifier_state_list **explored_state( |
| 5595 | struct bpf_verifier_env *env, |
| 5596 | int idx) |
| 5597 | { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 5598 | struct bpf_verifier_state *cur = env->cur_state; |
| 5599 | struct bpf_func_state *state = cur->frame[cur->curframe]; |
| 5600 | |
| 5601 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5602 | } |
| 5603 | |
| 5604 | static void init_explored_state(struct bpf_verifier_env *env, int idx) |
| 5605 | { |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 5606 | env->insn_aux_data[idx].prune_point = true; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5607 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5608 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5609 | /* t, w, e - match pseudo-code above: |
| 5610 | * t - index of current instruction |
| 5611 | * w - next instruction |
| 5612 | * e - edge |
| 5613 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5614 | 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] | 5615 | { |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5616 | int *insn_stack = env->cfg.insn_stack; |
| 5617 | int *insn_state = env->cfg.insn_state; |
| 5618 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5619 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 5620 | return 0; |
| 5621 | |
| 5622 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 5623 | return 0; |
| 5624 | |
| 5625 | if (w < 0 || w >= env->prog->len) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 5626 | verbose_linfo(env, t, "%d: ", t); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5627 | 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] | 5628 | return -EINVAL; |
| 5629 | } |
| 5630 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5631 | if (e == BRANCH) |
| 5632 | /* mark branch target for state pruning */ |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5633 | init_explored_state(env, w); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5634 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5635 | if (insn_state[w] == 0) { |
| 5636 | /* tree-edge */ |
| 5637 | insn_state[t] = DISCOVERED | e; |
| 5638 | insn_state[w] = DISCOVERED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5639 | if (env->cfg.cur_stack >= env->prog->len) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5640 | return -E2BIG; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5641 | insn_stack[env->cfg.cur_stack++] = w; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5642 | return 1; |
| 5643 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 5644 | verbose_linfo(env, t, "%d: ", t); |
| 5645 | verbose_linfo(env, w, "%d: ", w); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5646 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5647 | return -EINVAL; |
| 5648 | } else if (insn_state[w] == EXPLORED) { |
| 5649 | /* forward- or cross-edge */ |
| 5650 | insn_state[t] = DISCOVERED | e; |
| 5651 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5652 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5653 | return -EFAULT; |
| 5654 | } |
| 5655 | return 0; |
| 5656 | } |
| 5657 | |
| 5658 | /* non-recursive depth-first-search to detect loops in BPF program |
| 5659 | * loop == back-edge in directed graph |
| 5660 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5661 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5662 | { |
| 5663 | struct bpf_insn *insns = env->prog->insnsi; |
| 5664 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5665 | int *insn_stack, *insn_state; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5666 | int ret = 0; |
| 5667 | int i, t; |
| 5668 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5669 | 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] | 5670 | if (!insn_state) |
| 5671 | return -ENOMEM; |
| 5672 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5673 | 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] | 5674 | if (!insn_stack) { |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 5675 | kvfree(insn_state); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5676 | return -ENOMEM; |
| 5677 | } |
| 5678 | |
| 5679 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 5680 | insn_stack[0] = 0; /* 0 is the first instruction */ |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5681 | env->cfg.cur_stack = 1; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5682 | |
| 5683 | peek_stack: |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5684 | if (env->cfg.cur_stack == 0) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5685 | goto check_state; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5686 | t = insn_stack[env->cfg.cur_stack - 1]; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5687 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 5688 | if (BPF_CLASS(insns[t].code) == BPF_JMP || |
| 5689 | BPF_CLASS(insns[t].code) == BPF_JMP32) { |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5690 | u8 opcode = BPF_OP(insns[t].code); |
| 5691 | |
| 5692 | if (opcode == BPF_EXIT) { |
| 5693 | goto mark_explored; |
| 5694 | } else if (opcode == BPF_CALL) { |
| 5695 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 5696 | if (ret == 1) |
| 5697 | goto peek_stack; |
| 5698 | else if (ret < 0) |
| 5699 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 5700 | if (t + 1 < insn_cnt) |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5701 | init_explored_state(env, t + 1); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5702 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5703 | init_explored_state(env, t); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5704 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); |
| 5705 | if (ret == 1) |
| 5706 | goto peek_stack; |
| 5707 | else if (ret < 0) |
| 5708 | goto err_free; |
| 5709 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5710 | } else if (opcode == BPF_JA) { |
| 5711 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 5712 | ret = -EINVAL; |
| 5713 | goto err_free; |
| 5714 | } |
| 5715 | /* unconditional jump with single edge */ |
| 5716 | ret = push_insn(t, t + insns[t].off + 1, |
| 5717 | FALLTHROUGH, env); |
| 5718 | if (ret == 1) |
| 5719 | goto peek_stack; |
| 5720 | else if (ret < 0) |
| 5721 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5722 | /* tell verifier to check for equivalent states |
| 5723 | * after every call and jump |
| 5724 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 5725 | if (t + 1 < insn_cnt) |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5726 | init_explored_state(env, t + 1); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5727 | } else { |
| 5728 | /* conditional jump with two edges */ |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 5729 | init_explored_state(env, t); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5730 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 5731 | if (ret == 1) |
| 5732 | goto peek_stack; |
| 5733 | else if (ret < 0) |
| 5734 | goto err_free; |
| 5735 | |
| 5736 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 5737 | if (ret == 1) |
| 5738 | goto peek_stack; |
| 5739 | else if (ret < 0) |
| 5740 | goto err_free; |
| 5741 | } |
| 5742 | } else { |
| 5743 | /* all other non-branch instructions with single |
| 5744 | * fall-through edge |
| 5745 | */ |
| 5746 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 5747 | if (ret == 1) |
| 5748 | goto peek_stack; |
| 5749 | else if (ret < 0) |
| 5750 | goto err_free; |
| 5751 | } |
| 5752 | |
| 5753 | mark_explored: |
| 5754 | insn_state[t] = EXPLORED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5755 | if (env->cfg.cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5756 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5757 | ret = -EFAULT; |
| 5758 | goto err_free; |
| 5759 | } |
| 5760 | goto peek_stack; |
| 5761 | |
| 5762 | check_state: |
| 5763 | for (i = 0; i < insn_cnt; i++) { |
| 5764 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5765 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5766 | ret = -EINVAL; |
| 5767 | goto err_free; |
| 5768 | } |
| 5769 | } |
| 5770 | ret = 0; /* cfg looks good */ |
| 5771 | |
| 5772 | err_free: |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 5773 | kvfree(insn_state); |
| 5774 | kvfree(insn_stack); |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 5775 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5776 | return ret; |
| 5777 | } |
| 5778 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5779 | /* The minimum supported BTF func info size */ |
| 5780 | #define MIN_BPF_FUNCINFO_SIZE 8 |
| 5781 | #define MAX_FUNCINFO_REC_SIZE 252 |
| 5782 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5783 | static int check_btf_func(struct bpf_verifier_env *env, |
| 5784 | const union bpf_attr *attr, |
| 5785 | union bpf_attr __user *uattr) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5786 | { |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 5787 | u32 i, nfuncs, urec_size, min_size; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5788 | u32 krec_size = sizeof(struct bpf_func_info); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5789 | struct bpf_func_info *krecord; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5790 | const struct btf_type *type; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5791 | struct bpf_prog *prog; |
| 5792 | const struct btf *btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5793 | void __user *urecord; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 5794 | u32 prev_offset = 0; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5795 | int ret = 0; |
| 5796 | |
| 5797 | nfuncs = attr->func_info_cnt; |
| 5798 | if (!nfuncs) |
| 5799 | return 0; |
| 5800 | |
| 5801 | if (nfuncs != env->subprog_cnt) { |
| 5802 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); |
| 5803 | return -EINVAL; |
| 5804 | } |
| 5805 | |
| 5806 | urec_size = attr->func_info_rec_size; |
| 5807 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || |
| 5808 | urec_size > MAX_FUNCINFO_REC_SIZE || |
| 5809 | urec_size % sizeof(u32)) { |
| 5810 | verbose(env, "invalid func info rec size %u\n", urec_size); |
| 5811 | return -EINVAL; |
| 5812 | } |
| 5813 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5814 | prog = env->prog; |
| 5815 | btf = prog->aux->btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5816 | |
| 5817 | urecord = u64_to_user_ptr(attr->func_info); |
| 5818 | min_size = min_t(u32, krec_size, urec_size); |
| 5819 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5820 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5821 | if (!krecord) |
| 5822 | return -ENOMEM; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5823 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5824 | for (i = 0; i < nfuncs; i++) { |
| 5825 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); |
| 5826 | if (ret) { |
| 5827 | if (ret == -E2BIG) { |
| 5828 | verbose(env, "nonzero tailing record in func info"); |
| 5829 | /* set the size kernel expects so loader can zero |
| 5830 | * out the rest of the record. |
| 5831 | */ |
| 5832 | if (put_user(min_size, &uattr->func_info_rec_size)) |
| 5833 | ret = -EFAULT; |
| 5834 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5835 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5836 | } |
| 5837 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5838 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5839 | ret = -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5840 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5841 | } |
| 5842 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5843 | /* check insn_off */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5844 | if (i == 0) { |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5845 | if (krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5846 | verbose(env, |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5847 | "nonzero insn_off %u for the first func info record", |
| 5848 | krecord[i].insn_off); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5849 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5850 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5851 | } |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5852 | } else if (krecord[i].insn_off <= prev_offset) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5853 | verbose(env, |
| 5854 | "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] | 5855 | krecord[i].insn_off, prev_offset); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5856 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5857 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5858 | } |
| 5859 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5860 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5861 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
| 5862 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5863 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5864 | } |
| 5865 | |
| 5866 | /* check type_id */ |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5867 | type = btf_type_by_id(btf, krecord[i].type_id); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5868 | if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) { |
| 5869 | verbose(env, "invalid type id %d in func info", |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5870 | krecord[i].type_id); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5871 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5872 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5873 | } |
| 5874 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5875 | prev_offset = krecord[i].insn_off; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5876 | urecord += urec_size; |
| 5877 | } |
| 5878 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5879 | prog->aux->func_info = krecord; |
| 5880 | prog->aux->func_info_cnt = nfuncs; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5881 | return 0; |
| 5882 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5883 | err_free: |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5884 | kvfree(krecord); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 5885 | return ret; |
| 5886 | } |
| 5887 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 5888 | static void adjust_btf_func(struct bpf_verifier_env *env) |
| 5889 | { |
| 5890 | int i; |
| 5891 | |
| 5892 | if (!env->prog->aux->func_info) |
| 5893 | return; |
| 5894 | |
| 5895 | for (i = 0; i < env->subprog_cnt; i++) |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 5896 | 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] | 5897 | } |
| 5898 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5899 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
| 5900 | sizeof(((struct bpf_line_info *)(0))->line_col)) |
| 5901 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
| 5902 | |
| 5903 | static int check_btf_line(struct bpf_verifier_env *env, |
| 5904 | const union bpf_attr *attr, |
| 5905 | union bpf_attr __user *uattr) |
| 5906 | { |
| 5907 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; |
| 5908 | struct bpf_subprog_info *sub; |
| 5909 | struct bpf_line_info *linfo; |
| 5910 | struct bpf_prog *prog; |
| 5911 | const struct btf *btf; |
| 5912 | void __user *ulinfo; |
| 5913 | int err; |
| 5914 | |
| 5915 | nr_linfo = attr->line_info_cnt; |
| 5916 | if (!nr_linfo) |
| 5917 | return 0; |
| 5918 | |
| 5919 | rec_size = attr->line_info_rec_size; |
| 5920 | if (rec_size < MIN_BPF_LINEINFO_SIZE || |
| 5921 | rec_size > MAX_LINEINFO_REC_SIZE || |
| 5922 | rec_size & (sizeof(u32) - 1)) |
| 5923 | return -EINVAL; |
| 5924 | |
| 5925 | /* Need to zero it in case the userspace may |
| 5926 | * pass in a smaller bpf_line_info object. |
| 5927 | */ |
| 5928 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), |
| 5929 | GFP_KERNEL | __GFP_NOWARN); |
| 5930 | if (!linfo) |
| 5931 | return -ENOMEM; |
| 5932 | |
| 5933 | prog = env->prog; |
| 5934 | btf = prog->aux->btf; |
| 5935 | |
| 5936 | s = 0; |
| 5937 | sub = env->subprog_info; |
| 5938 | ulinfo = u64_to_user_ptr(attr->line_info); |
| 5939 | expected_size = sizeof(struct bpf_line_info); |
| 5940 | ncopy = min_t(u32, expected_size, rec_size); |
| 5941 | for (i = 0; i < nr_linfo; i++) { |
| 5942 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); |
| 5943 | if (err) { |
| 5944 | if (err == -E2BIG) { |
| 5945 | verbose(env, "nonzero tailing record in line_info"); |
| 5946 | if (put_user(expected_size, |
| 5947 | &uattr->line_info_rec_size)) |
| 5948 | err = -EFAULT; |
| 5949 | } |
| 5950 | goto err_free; |
| 5951 | } |
| 5952 | |
| 5953 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { |
| 5954 | err = -EFAULT; |
| 5955 | goto err_free; |
| 5956 | } |
| 5957 | |
| 5958 | /* |
| 5959 | * Check insn_off to ensure |
| 5960 | * 1) strictly increasing AND |
| 5961 | * 2) bounded by prog->len |
| 5962 | * |
| 5963 | * The linfo[0].insn_off == 0 check logically falls into |
| 5964 | * the later "missing bpf_line_info for func..." case |
| 5965 | * because the first linfo[0].insn_off must be the |
| 5966 | * first sub also and the first sub must have |
| 5967 | * subprog_info[0].start == 0. |
| 5968 | */ |
| 5969 | if ((i && linfo[i].insn_off <= prev_offset) || |
| 5970 | linfo[i].insn_off >= prog->len) { |
| 5971 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", |
| 5972 | i, linfo[i].insn_off, prev_offset, |
| 5973 | prog->len); |
| 5974 | err = -EINVAL; |
| 5975 | goto err_free; |
| 5976 | } |
| 5977 | |
Martin KaFai Lau | fdbaa0b | 2018-12-19 13:01:01 -0800 | [diff] [blame] | 5978 | if (!prog->insnsi[linfo[i].insn_off].code) { |
| 5979 | verbose(env, |
| 5980 | "Invalid insn code at line_info[%u].insn_off\n", |
| 5981 | i); |
| 5982 | err = -EINVAL; |
| 5983 | goto err_free; |
| 5984 | } |
| 5985 | |
Martin KaFai Lau | 23127b3 | 2018-12-13 10:41:46 -0800 | [diff] [blame] | 5986 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
| 5987 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5988 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
| 5989 | err = -EINVAL; |
| 5990 | goto err_free; |
| 5991 | } |
| 5992 | |
| 5993 | if (s != env->subprog_cnt) { |
| 5994 | if (linfo[i].insn_off == sub[s].start) { |
| 5995 | sub[s].linfo_idx = i; |
| 5996 | s++; |
| 5997 | } else if (sub[s].start < linfo[i].insn_off) { |
| 5998 | verbose(env, "missing bpf_line_info for func#%u\n", s); |
| 5999 | err = -EINVAL; |
| 6000 | goto err_free; |
| 6001 | } |
| 6002 | } |
| 6003 | |
| 6004 | prev_offset = linfo[i].insn_off; |
| 6005 | ulinfo += rec_size; |
| 6006 | } |
| 6007 | |
| 6008 | if (s != env->subprog_cnt) { |
| 6009 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", |
| 6010 | env->subprog_cnt - s, s); |
| 6011 | err = -EINVAL; |
| 6012 | goto err_free; |
| 6013 | } |
| 6014 | |
| 6015 | prog->aux->linfo = linfo; |
| 6016 | prog->aux->nr_linfo = nr_linfo; |
| 6017 | |
| 6018 | return 0; |
| 6019 | |
| 6020 | err_free: |
| 6021 | kvfree(linfo); |
| 6022 | return err; |
| 6023 | } |
| 6024 | |
| 6025 | static int check_btf_info(struct bpf_verifier_env *env, |
| 6026 | const union bpf_attr *attr, |
| 6027 | union bpf_attr __user *uattr) |
| 6028 | { |
| 6029 | struct btf *btf; |
| 6030 | int err; |
| 6031 | |
| 6032 | if (!attr->func_info_cnt && !attr->line_info_cnt) |
| 6033 | return 0; |
| 6034 | |
| 6035 | btf = btf_get_by_fd(attr->prog_btf_fd); |
| 6036 | if (IS_ERR(btf)) |
| 6037 | return PTR_ERR(btf); |
| 6038 | env->prog->aux->btf = btf; |
| 6039 | |
| 6040 | err = check_btf_func(env, attr, uattr); |
| 6041 | if (err) |
| 6042 | return err; |
| 6043 | |
| 6044 | err = check_btf_line(env, attr, uattr); |
| 6045 | if (err) |
| 6046 | return err; |
| 6047 | |
| 6048 | return 0; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6049 | } |
| 6050 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6051 | /* check %cur's range satisfies %old's */ |
| 6052 | static bool range_within(struct bpf_reg_state *old, |
| 6053 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6054 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6055 | return old->umin_value <= cur->umin_value && |
| 6056 | old->umax_value >= cur->umax_value && |
| 6057 | old->smin_value <= cur->smin_value && |
| 6058 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | /* Maximum number of register states that can exist at once */ |
| 6062 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 6063 | struct idpair { |
| 6064 | u32 old; |
| 6065 | u32 cur; |
| 6066 | }; |
| 6067 | |
| 6068 | /* If in the old state two registers had the same id, then they need to have |
| 6069 | * the same id in the new state as well. But that id could be different from |
| 6070 | * the old state, so we need to track the mapping from old to new ids. |
| 6071 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 6072 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 6073 | * regs with a different old id could still have new id 9, we don't care about |
| 6074 | * that. |
| 6075 | * So we look through our idmap to see if this old id has been seen before. If |
| 6076 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 6077 | */ |
| 6078 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 6079 | { |
| 6080 | unsigned int i; |
| 6081 | |
| 6082 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 6083 | if (!idmap[i].old) { |
| 6084 | /* Reached an empty slot; haven't seen this id before */ |
| 6085 | idmap[i].old = old_id; |
| 6086 | idmap[i].cur = cur_id; |
| 6087 | return true; |
| 6088 | } |
| 6089 | if (idmap[i].old == old_id) |
| 6090 | return idmap[i].cur == cur_id; |
| 6091 | } |
| 6092 | /* We ran out of idmap slots, which should be impossible */ |
| 6093 | WARN_ON_ONCE(1); |
| 6094 | return false; |
| 6095 | } |
| 6096 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 6097 | static void clean_func_state(struct bpf_verifier_env *env, |
| 6098 | struct bpf_func_state *st) |
| 6099 | { |
| 6100 | enum bpf_reg_liveness live; |
| 6101 | int i, j; |
| 6102 | |
| 6103 | for (i = 0; i < BPF_REG_FP; i++) { |
| 6104 | live = st->regs[i].live; |
| 6105 | /* liveness must not touch this register anymore */ |
| 6106 | st->regs[i].live |= REG_LIVE_DONE; |
| 6107 | if (!(live & REG_LIVE_READ)) |
| 6108 | /* since the register is unused, clear its state |
| 6109 | * to make further comparison simpler |
| 6110 | */ |
| 6111 | __mark_reg_not_init(&st->regs[i]); |
| 6112 | } |
| 6113 | |
| 6114 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { |
| 6115 | live = st->stack[i].spilled_ptr.live; |
| 6116 | /* liveness must not touch this stack slot anymore */ |
| 6117 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; |
| 6118 | if (!(live & REG_LIVE_READ)) { |
| 6119 | __mark_reg_not_init(&st->stack[i].spilled_ptr); |
| 6120 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 6121 | st->stack[i].slot_type[j] = STACK_INVALID; |
| 6122 | } |
| 6123 | } |
| 6124 | } |
| 6125 | |
| 6126 | static void clean_verifier_state(struct bpf_verifier_env *env, |
| 6127 | struct bpf_verifier_state *st) |
| 6128 | { |
| 6129 | int i; |
| 6130 | |
| 6131 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) |
| 6132 | /* all regs in this state in all frames were already marked */ |
| 6133 | return; |
| 6134 | |
| 6135 | for (i = 0; i <= st->curframe; i++) |
| 6136 | clean_func_state(env, st->frame[i]); |
| 6137 | } |
| 6138 | |
| 6139 | /* the parentage chains form a tree. |
| 6140 | * the verifier states are added to state lists at given insn and |
| 6141 | * pushed into state stack for future exploration. |
| 6142 | * when the verifier reaches bpf_exit insn some of the verifer states |
| 6143 | * stored in the state lists have their final liveness state already, |
| 6144 | * but a lot of states will get revised from liveness point of view when |
| 6145 | * the verifier explores other branches. |
| 6146 | * Example: |
| 6147 | * 1: r0 = 1 |
| 6148 | * 2: if r1 == 100 goto pc+1 |
| 6149 | * 3: r0 = 2 |
| 6150 | * 4: exit |
| 6151 | * when the verifier reaches exit insn the register r0 in the state list of |
| 6152 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch |
| 6153 | * of insn 2 and goes exploring further. At the insn 4 it will walk the |
| 6154 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. |
| 6155 | * |
| 6156 | * Since the verifier pushes the branch states as it sees them while exploring |
| 6157 | * the program the condition of walking the branch instruction for the second |
| 6158 | * time means that all states below this branch were already explored and |
| 6159 | * their final liveness markes are already propagated. |
| 6160 | * Hence when the verifier completes the search of state list in is_state_visited() |
| 6161 | * we can call this clean_live_states() function to mark all liveness states |
| 6162 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' |
| 6163 | * will not be used. |
| 6164 | * This function also clears the registers and stack for states that !READ |
| 6165 | * to simplify state merging. |
| 6166 | * |
| 6167 | * Important note here that walking the same branch instruction in the callee |
| 6168 | * doesn't meant that the states are DONE. The verifier has to compare |
| 6169 | * the callsites |
| 6170 | */ |
| 6171 | static void clean_live_states(struct bpf_verifier_env *env, int insn, |
| 6172 | struct bpf_verifier_state *cur) |
| 6173 | { |
| 6174 | struct bpf_verifier_state_list *sl; |
| 6175 | int i; |
| 6176 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 6177 | sl = *explored_state(env, insn); |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 6178 | while (sl) { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 6179 | if (sl->state.insn_idx != insn || |
| 6180 | sl->state.curframe != cur->curframe) |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 6181 | goto next; |
| 6182 | for (i = 0; i <= cur->curframe; i++) |
| 6183 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) |
| 6184 | goto next; |
| 6185 | clean_verifier_state(env, &sl->state); |
| 6186 | next: |
| 6187 | sl = sl->next; |
| 6188 | } |
| 6189 | } |
| 6190 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6191 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 6192 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 6193 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6194 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6195 | bool equal; |
| 6196 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6197 | if (!(rold->live & REG_LIVE_READ)) |
| 6198 | /* explored state didn't use this */ |
| 6199 | return true; |
| 6200 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6201 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6202 | |
| 6203 | if (rold->type == PTR_TO_STACK) |
| 6204 | /* two stack pointers are equal only if they're pointing to |
| 6205 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 6206 | */ |
| 6207 | return equal && rold->frameno == rcur->frameno; |
| 6208 | |
| 6209 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6210 | return true; |
| 6211 | |
| 6212 | if (rold->type == NOT_INIT) |
| 6213 | /* explored state can't have used this */ |
| 6214 | return true; |
| 6215 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6216 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6217 | switch (rold->type) { |
| 6218 | case SCALAR_VALUE: |
| 6219 | if (rcur->type == SCALAR_VALUE) { |
| 6220 | /* new val must satisfy old val knowledge */ |
| 6221 | return range_within(rold, rcur) && |
| 6222 | tnum_in(rold->var_off, rcur->var_off); |
| 6223 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 6224 | /* We're trying to use a pointer in place of a scalar. |
| 6225 | * Even if the scalar was unbounded, this could lead to |
| 6226 | * pointer leaks because scalars are allowed to leak |
| 6227 | * while pointers are not. We could make this safe in |
| 6228 | * special cases if root is calling us, but it's |
| 6229 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6230 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 6231 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6232 | } |
| 6233 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 6234 | /* If the new min/max/var_off satisfy the old ones and |
| 6235 | * everything else matches, we are OK. |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6236 | * 'id' is not compared, since it's only used for maps with |
| 6237 | * bpf_spin_lock inside map element and in such cases if |
| 6238 | * the rest of the prog is valid for one map element then |
| 6239 | * it's valid for all map elements regardless of the key |
| 6240 | * used in bpf_map_lookup() |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 6241 | */ |
| 6242 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 6243 | range_within(rold, rcur) && |
| 6244 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6245 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 6246 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 6247 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 6248 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 6249 | * checked, doing so could have affected others with the same |
| 6250 | * id, and we can't check for that because we lost the id when |
| 6251 | * we converted to a PTR_TO_MAP_VALUE. |
| 6252 | */ |
| 6253 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 6254 | return false; |
| 6255 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 6256 | return false; |
| 6257 | /* Check our ids match any regs they're supposed to */ |
| 6258 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 6259 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6260 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 6261 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6262 | return false; |
| 6263 | /* We must have at least as much range as the old ptr |
| 6264 | * did, so that any accesses which were safe before are |
| 6265 | * still safe. This is true even if old range < old off, |
| 6266 | * since someone could have accessed through (ptr - k), or |
| 6267 | * even done ptr -= k in a register, to get a safe access. |
| 6268 | */ |
| 6269 | if (rold->range > rcur->range) |
| 6270 | return false; |
| 6271 | /* If the offsets don't match, we can't trust our alignment; |
| 6272 | * nor can we be sure that we won't fall out of range. |
| 6273 | */ |
| 6274 | if (rold->off != rcur->off) |
| 6275 | return false; |
| 6276 | /* id relations must be preserved */ |
| 6277 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 6278 | return false; |
| 6279 | /* new val must satisfy old val knowledge */ |
| 6280 | return range_within(rold, rcur) && |
| 6281 | tnum_in(rold->var_off, rcur->var_off); |
| 6282 | case PTR_TO_CTX: |
| 6283 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6284 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 6285 | case PTR_TO_FLOW_KEYS: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6286 | case PTR_TO_SOCKET: |
| 6287 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 6288 | case PTR_TO_SOCK_COMMON: |
| 6289 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 6290 | case PTR_TO_TCP_SOCK: |
| 6291 | case PTR_TO_TCP_SOCK_OR_NULL: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6292 | /* Only valid matches are exact, which memcmp() above |
| 6293 | * would have accepted |
| 6294 | */ |
| 6295 | default: |
| 6296 | /* Don't know what's going on, just say it's not safe */ |
| 6297 | return false; |
| 6298 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6299 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6300 | /* Shouldn't get here; if we do, say it's not safe */ |
| 6301 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6302 | return false; |
| 6303 | } |
| 6304 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6305 | static bool stacksafe(struct bpf_func_state *old, |
| 6306 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6307 | struct idpair *idmap) |
| 6308 | { |
| 6309 | int i, spi; |
| 6310 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6311 | /* walk slots of the explored stack and ignore any additional |
| 6312 | * slots in the current stack, since explored(safe) state |
| 6313 | * didn't use them |
| 6314 | */ |
| 6315 | for (i = 0; i < old->allocated_stack; i++) { |
| 6316 | spi = i / BPF_REG_SIZE; |
| 6317 | |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 6318 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
| 6319 | i += BPF_REG_SIZE - 1; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 6320 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 6321 | continue; |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 6322 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 6323 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6324 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 6325 | continue; |
Alexei Starovoitov | 19e2dbb | 2018-12-13 11:42:33 -0800 | [diff] [blame] | 6326 | |
| 6327 | /* explored stack has more populated slots than current stack |
| 6328 | * and these slots were used |
| 6329 | */ |
| 6330 | if (i >= cur->allocated_stack) |
| 6331 | return false; |
| 6332 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 6333 | /* if old state was safe with misc data in the stack |
| 6334 | * it will be safe with zero-initialized stack. |
| 6335 | * The opposite is not true |
| 6336 | */ |
| 6337 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 6338 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 6339 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6340 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 6341 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 6342 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 6343 | * this stack slot, but current has has STACK_MISC -> |
| 6344 | * this verifier states are not equivalent, |
| 6345 | * return false to continue verification of this path |
| 6346 | */ |
| 6347 | return false; |
| 6348 | if (i % BPF_REG_SIZE) |
| 6349 | continue; |
| 6350 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 6351 | continue; |
| 6352 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 6353 | &cur->stack[spi].spilled_ptr, |
| 6354 | idmap)) |
| 6355 | /* when explored and current stack slot are both storing |
| 6356 | * spilled registers, check that stored pointers types |
| 6357 | * are the same as well. |
| 6358 | * Ex: explored safe path could have stored |
| 6359 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 6360 | * but current path has stored: |
| 6361 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 6362 | * such verifier states are not equivalent. |
| 6363 | * return false to continue verification of this path |
| 6364 | */ |
| 6365 | return false; |
| 6366 | } |
| 6367 | return true; |
| 6368 | } |
| 6369 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6370 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 6371 | { |
| 6372 | if (old->acquired_refs != cur->acquired_refs) |
| 6373 | return false; |
| 6374 | return !memcmp(old->refs, cur->refs, |
| 6375 | sizeof(*old->refs) * old->acquired_refs); |
| 6376 | } |
| 6377 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6378 | /* compare two verifier states |
| 6379 | * |
| 6380 | * all states stored in state_list are known to be valid, since |
| 6381 | * verifier reached 'bpf_exit' instruction through them |
| 6382 | * |
| 6383 | * this function is called when verifier exploring different branches of |
| 6384 | * execution popped from the state stack. If it sees an old state that has |
| 6385 | * more strict register state and more strict stack state then this execution |
| 6386 | * branch doesn't need to be explored further, since verifier already |
| 6387 | * concluded that more strict state leads to valid finish. |
| 6388 | * |
| 6389 | * Therefore two states are equivalent if register state is more conservative |
| 6390 | * and explored stack state is more conservative than the current one. |
| 6391 | * Example: |
| 6392 | * explored current |
| 6393 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 6394 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 6395 | * |
| 6396 | * In other words if current stack state (one being explored) has more |
| 6397 | * valid slots than old one that already passed validation, it means |
| 6398 | * the verifier can stop exploring and conclude that current state is valid too |
| 6399 | * |
| 6400 | * Similarly with registers. If explored state has register type as invalid |
| 6401 | * whereas register type in current state is meaningful, it means that |
| 6402 | * the current state will reach 'bpf_exit' instruction safely |
| 6403 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6404 | static bool func_states_equal(struct bpf_func_state *old, |
| 6405 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6406 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6407 | struct idpair *idmap; |
| 6408 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6409 | int i; |
| 6410 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6411 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 6412 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 6413 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 6414 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6415 | |
| 6416 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 6417 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6418 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6419 | } |
| 6420 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6421 | if (!stacksafe(old, cur, idmap)) |
| 6422 | goto out_free; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6423 | |
| 6424 | if (!refsafe(old, cur)) |
| 6425 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6426 | ret = true; |
| 6427 | out_free: |
| 6428 | kfree(idmap); |
| 6429 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6430 | } |
| 6431 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6432 | static bool states_equal(struct bpf_verifier_env *env, |
| 6433 | struct bpf_verifier_state *old, |
| 6434 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6435 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6436 | int i; |
| 6437 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6438 | if (old->curframe != cur->curframe) |
| 6439 | return false; |
| 6440 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6441 | /* Verification state from speculative execution simulation |
| 6442 | * must never prune a non-speculative execution one. |
| 6443 | */ |
| 6444 | if (old->speculative && !cur->speculative) |
| 6445 | return false; |
| 6446 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6447 | if (old->active_spin_lock != cur->active_spin_lock) |
| 6448 | return false; |
| 6449 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6450 | /* for states to be equal callsites have to be the same |
| 6451 | * and all frame states need to be equivalent |
| 6452 | */ |
| 6453 | for (i = 0; i <= old->curframe; i++) { |
| 6454 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 6455 | return false; |
| 6456 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 6457 | return false; |
| 6458 | } |
| 6459 | return true; |
| 6460 | } |
| 6461 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6462 | /* Return 0 if no propagation happened. Return negative error code if error |
| 6463 | * happened. Otherwise, return the propagated bit. |
| 6464 | */ |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6465 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
| 6466 | struct bpf_reg_state *reg, |
| 6467 | struct bpf_reg_state *parent_reg) |
| 6468 | { |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6469 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
| 6470 | u8 flag = reg->live & REG_LIVE_READ; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6471 | int err; |
| 6472 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6473 | /* When comes here, read flags of PARENT_REG or REG could be any of |
| 6474 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need |
| 6475 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. |
| 6476 | */ |
| 6477 | if (parent_flag == REG_LIVE_READ64 || |
| 6478 | /* Or if there is no read flag from REG. */ |
| 6479 | !flag || |
| 6480 | /* Or if the read flag from REG is the same as PARENT_REG. */ |
| 6481 | parent_flag == flag) |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6482 | return 0; |
| 6483 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6484 | err = mark_reg_read(env, reg, parent_reg, flag); |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6485 | if (err) |
| 6486 | return err; |
| 6487 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6488 | return flag; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6489 | } |
| 6490 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6491 | /* A write screens off any subsequent reads; but write marks come from the |
| 6492 | * straight-line code between a state and its parent. When we arrive at an |
| 6493 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 6494 | * code, so read marks in the state must propagate to the parent regardless |
| 6495 | * 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] | 6496 | * in mark_reg_read() is for. |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6497 | */ |
| 6498 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 6499 | const struct bpf_verifier_state *vstate, |
| 6500 | struct bpf_verifier_state *vparent) |
| 6501 | { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6502 | struct bpf_reg_state *state_reg, *parent_reg; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6503 | struct bpf_func_state *state, *parent; |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6504 | int i, frame, err = 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6505 | |
| 6506 | if (vparent->curframe != vstate->curframe) { |
| 6507 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 6508 | vparent->curframe, vstate->curframe); |
| 6509 | return -EFAULT; |
| 6510 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6511 | /* Propagate read liveness of registers... */ |
| 6512 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 6513 | for (frame = 0; frame <= vstate->curframe; frame++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6514 | parent = vparent->frame[frame]; |
| 6515 | state = vstate->frame[frame]; |
| 6516 | parent_reg = parent->regs; |
| 6517 | state_reg = state->regs; |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 6518 | /* We don't need to worry about FP liveness, it's read-only */ |
| 6519 | 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] | 6520 | err = propagate_liveness_reg(env, &state_reg[i], |
| 6521 | &parent_reg[i]); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6522 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6523 | return err; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6524 | if (err == REG_LIVE_READ64) |
| 6525 | mark_insn_zext(env, &parent_reg[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6526 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6527 | |
Jiong Wang | 1b04aee | 2019-04-12 22:59:34 +0100 | [diff] [blame] | 6528 | /* Propagate stack slots. */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6529 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 6530 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6531 | parent_reg = &parent->stack[i].spilled_ptr; |
| 6532 | state_reg = &state->stack[i].spilled_ptr; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 6533 | err = propagate_liveness_reg(env, state_reg, |
| 6534 | parent_reg); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6535 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 6536 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6537 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6538 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6539 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6540 | } |
| 6541 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6542 | 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] | 6543 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6544 | struct bpf_verifier_state_list *new_sl; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 6545 | struct bpf_verifier_state_list *sl, **pprev; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6546 | struct bpf_verifier_state *cur = env->cur_state, *new; |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 6547 | int i, j, err, states_cnt = 0; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6548 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 6549 | if (!env->insn_aux_data[insn_idx].prune_point) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6550 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 6551 | * be doing state search here |
| 6552 | */ |
| 6553 | return 0; |
| 6554 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 6555 | pprev = explored_state(env, insn_idx); |
| 6556 | sl = *pprev; |
| 6557 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 6558 | clean_live_states(env, insn_idx, cur); |
| 6559 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 6560 | while (sl) { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 6561 | states_cnt++; |
| 6562 | if (sl->state.insn_idx != insn_idx) |
| 6563 | goto next; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6564 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 6565 | sl->hit_cnt++; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6566 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6567 | * prune the search. |
| 6568 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 6569 | * If we have any write marks in env->cur_state, they |
| 6570 | * will prevent corresponding reads in the continuation |
| 6571 | * from reaching our parent (an explored_state). Our |
| 6572 | * own state will get the read marks recorded, but |
| 6573 | * they'll be immediately forgotten as we're pruning |
| 6574 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6575 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6576 | err = propagate_liveness(env, &sl->state, cur); |
| 6577 | if (err) |
| 6578 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6579 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6580 | } |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 6581 | sl->miss_cnt++; |
| 6582 | /* heuristic to determine whether this state is beneficial |
| 6583 | * to keep checking from state equivalence point of view. |
| 6584 | * Higher numbers increase max_states_per_insn and verification time, |
| 6585 | * but do not meaningfully decrease insn_processed. |
| 6586 | */ |
| 6587 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { |
| 6588 | /* the state is unlikely to be useful. Remove it to |
| 6589 | * speed up verification |
| 6590 | */ |
| 6591 | *pprev = sl->next; |
| 6592 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { |
| 6593 | free_verifier_state(&sl->state, false); |
| 6594 | kfree(sl); |
| 6595 | env->peak_states--; |
| 6596 | } else { |
| 6597 | /* cannot free this state, since parentage chain may |
| 6598 | * walk it later. Add it for free_list instead to |
| 6599 | * be freed at the end of verification |
| 6600 | */ |
| 6601 | sl->next = env->free_list; |
| 6602 | env->free_list = sl; |
| 6603 | } |
| 6604 | sl = *pprev; |
| 6605 | continue; |
| 6606 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 6607 | next: |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 6608 | pprev = &sl->next; |
| 6609 | sl = *pprev; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6610 | } |
| 6611 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6612 | if (env->max_states_per_insn < states_cnt) |
| 6613 | env->max_states_per_insn = states_cnt; |
| 6614 | |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 6615 | if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
| 6616 | return 0; |
| 6617 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6618 | /* there were no equivalent states, remember current one. |
| 6619 | * technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6620 | * but it will either reach outer most bpf_exit (which means it's safe) |
| 6621 | * or it will be rejected. Since there are no loops, we won't be |
| 6622 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
| 6623 | * again on the way to bpf_exit |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6624 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6625 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6626 | if (!new_sl) |
| 6627 | return -ENOMEM; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6628 | env->total_states++; |
| 6629 | env->peak_states++; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6630 | |
| 6631 | /* add new state to the head of linked list */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6632 | new = &new_sl->state; |
| 6633 | err = copy_verifier_state(new, cur); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 6634 | if (err) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6635 | free_verifier_state(new, false); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 6636 | kfree(new_sl); |
| 6637 | return err; |
| 6638 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 6639 | new->insn_idx = insn_idx; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 6640 | new_sl->next = *explored_state(env, insn_idx); |
| 6641 | *explored_state(env, insn_idx) = new_sl; |
Jakub Kicinski | 7640ead | 2018-12-12 16:29:07 -0800 | [diff] [blame] | 6642 | /* connect new state to parentage chain. Current frame needs all |
| 6643 | * registers connected. Only r6 - r9 of the callers are alive (pushed |
| 6644 | * to the stack implicitly by JITs) so in callers' frames connect just |
| 6645 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to |
| 6646 | * the state of the call instruction (with WRITTEN set), and r0 comes |
| 6647 | * from callee with its full parentage chain, anyway. |
| 6648 | */ |
| 6649 | for (j = 0; j <= cur->curframe; j++) |
| 6650 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) |
| 6651 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 6652 | /* clear write marks in current state: the writes we did are not writes |
| 6653 | * our child did, so they don't screen off its reads from us. |
| 6654 | * (There are no read marks in current state, because reads always mark |
| 6655 | * their parent and current state never has children yet. Only |
| 6656 | * explored_states can get read marks.) |
| 6657 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6658 | for (i = 0; i < BPF_REG_FP; i++) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6659 | cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE; |
| 6660 | |
| 6661 | /* all stack frames are accessible from callee, clear them all */ |
| 6662 | for (j = 0; j <= cur->curframe; j++) { |
| 6663 | struct bpf_func_state *frame = cur->frame[j]; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6664 | struct bpf_func_state *newframe = new->frame[j]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6665 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6666 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 6667 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 6668 | frame->stack[i].spilled_ptr.parent = |
| 6669 | &newframe->stack[i].spilled_ptr; |
| 6670 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6671 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6672 | return 0; |
| 6673 | } |
| 6674 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6675 | /* Return true if it's OK to have the same insn return a different type. */ |
| 6676 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 6677 | { |
| 6678 | switch (type) { |
| 6679 | case PTR_TO_CTX: |
| 6680 | case PTR_TO_SOCKET: |
| 6681 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 6682 | case PTR_TO_SOCK_COMMON: |
| 6683 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 6684 | case PTR_TO_TCP_SOCK: |
| 6685 | case PTR_TO_TCP_SOCK_OR_NULL: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6686 | return false; |
| 6687 | default: |
| 6688 | return true; |
| 6689 | } |
| 6690 | } |
| 6691 | |
| 6692 | /* If an instruction was previously used with particular pointer types, then we |
| 6693 | * need to be careful to avoid cases such as the below, where it may be ok |
| 6694 | * for one branch accessing the pointer, but not ok for the other branch: |
| 6695 | * |
| 6696 | * R1 = sock_ptr |
| 6697 | * goto X; |
| 6698 | * ... |
| 6699 | * R1 = some_other_valid_ptr; |
| 6700 | * goto X; |
| 6701 | * ... |
| 6702 | * R2 = *(u32 *)(R1 + 0); |
| 6703 | */ |
| 6704 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 6705 | { |
| 6706 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 6707 | !reg_type_mismatch_ok(prev)); |
| 6708 | } |
| 6709 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6710 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6711 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6712 | struct bpf_verifier_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6713 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6714 | struct bpf_reg_state *regs; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6715 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6716 | bool do_print_state = false; |
| 6717 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 6718 | env->prev_linfo = NULL; |
| 6719 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6720 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 6721 | if (!state) |
| 6722 | return -ENOMEM; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6723 | state->curframe = 0; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6724 | state->speculative = false; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6725 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 6726 | if (!state->frame[0]) { |
| 6727 | kfree(state); |
| 6728 | return -ENOMEM; |
| 6729 | } |
| 6730 | env->cur_state = state; |
| 6731 | init_func_state(env, state->frame[0], |
| 6732 | BPF_MAIN_FUNC /* callsite */, |
| 6733 | 0 /* frameno */, |
| 6734 | 0 /* subprogno, zero == main subprog */); |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6735 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6736 | for (;;) { |
| 6737 | struct bpf_insn *insn; |
| 6738 | u8 class; |
| 6739 | int err; |
| 6740 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6741 | if (env->insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6742 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6743 | env->insn_idx, insn_cnt); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6744 | return -EFAULT; |
| 6745 | } |
| 6746 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6747 | insn = &insns[env->insn_idx]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6748 | class = BPF_CLASS(insn->code); |
| 6749 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6750 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6751 | verbose(env, |
| 6752 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6753 | env->insn_processed); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6754 | return -E2BIG; |
| 6755 | } |
| 6756 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6757 | err = is_state_visited(env, env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6758 | if (err < 0) |
| 6759 | return err; |
| 6760 | if (err == 1) { |
| 6761 | /* found equivalent state, can prune the search */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6762 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6763 | if (do_print_state) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6764 | verbose(env, "\nfrom %d to %d%s: safe\n", |
| 6765 | env->prev_insn_idx, env->insn_idx, |
| 6766 | env->cur_state->speculative ? |
| 6767 | " (speculative execution)" : ""); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6768 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6769 | verbose(env, "%d: safe\n", env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6770 | } |
| 6771 | goto process_bpf_exit; |
| 6772 | } |
| 6773 | |
Alexei Starovoitov | c349480 | 2018-12-03 22:46:04 -0800 | [diff] [blame] | 6774 | if (signal_pending(current)) |
| 6775 | return -EAGAIN; |
| 6776 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 6777 | if (need_resched()) |
| 6778 | cond_resched(); |
| 6779 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6780 | if (env->log.level & BPF_LOG_LEVEL2 || |
| 6781 | (env->log.level & BPF_LOG_LEVEL && do_print_state)) { |
| 6782 | if (env->log.level & BPF_LOG_LEVEL2) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6783 | verbose(env, "%d:", env->insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 6784 | else |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6785 | verbose(env, "\nfrom %d to %d%s:", |
| 6786 | env->prev_insn_idx, env->insn_idx, |
| 6787 | env->cur_state->speculative ? |
| 6788 | " (speculative execution)" : ""); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6789 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6790 | do_print_state = false; |
| 6791 | } |
| 6792 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6793 | if (env->log.level & BPF_LOG_LEVEL) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6794 | const struct bpf_insn_cbs cbs = { |
| 6795 | .cb_print = verbose, |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 6796 | .private_data = env, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6797 | }; |
| 6798 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6799 | verbose_linfo(env, env->insn_idx, "; "); |
| 6800 | verbose(env, "%d: ", env->insn_idx); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 6801 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6802 | } |
| 6803 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 6804 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6805 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
| 6806 | env->prev_insn_idx); |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 6807 | if (err) |
| 6808 | return err; |
| 6809 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 6810 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6811 | regs = cur_regs(env); |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6812 | env->insn_aux_data[env->insn_idx].seen = true; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6813 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6814 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 6815 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6816 | if (err) |
| 6817 | return err; |
| 6818 | |
| 6819 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6820 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6821 | |
| 6822 | /* check for reserved fields is already done */ |
| 6823 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6824 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6825 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6826 | if (err) |
| 6827 | return err; |
| 6828 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6829 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6830 | if (err) |
| 6831 | return err; |
| 6832 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 6833 | src_reg_type = regs[insn->src_reg].type; |
| 6834 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6835 | /* check that memory (src_reg + off) is readable, |
| 6836 | * the state of dst_reg will be updated by this func |
| 6837 | */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6838 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
| 6839 | insn->off, BPF_SIZE(insn->code), |
| 6840 | BPF_READ, insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6841 | if (err) |
| 6842 | return err; |
| 6843 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6844 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6845 | |
| 6846 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6847 | /* saw a valid insn |
| 6848 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6849 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6850 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6851 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6852 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6853 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6854 | /* ABuser program is trying to use the same insn |
| 6855 | * dst_reg = *(u32*) (src_reg + off) |
| 6856 | * with different pointer types: |
| 6857 | * src_reg == ctx in one branch and |
| 6858 | * src_reg == stack|map in some other branch. |
| 6859 | * Reject it. |
| 6860 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6861 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6862 | return -EINVAL; |
| 6863 | } |
| 6864 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6865 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6866 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6867 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6868 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6869 | err = check_xadd(env, env->insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6870 | if (err) |
| 6871 | return err; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6872 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6873 | continue; |
| 6874 | } |
| 6875 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6876 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6877 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6878 | if (err) |
| 6879 | return err; |
| 6880 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6881 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6882 | if (err) |
| 6883 | return err; |
| 6884 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6885 | dst_reg_type = regs[insn->dst_reg].type; |
| 6886 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6887 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6888 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 6889 | insn->off, BPF_SIZE(insn->code), |
| 6890 | BPF_WRITE, insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6891 | if (err) |
| 6892 | return err; |
| 6893 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6894 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6895 | |
| 6896 | if (*prev_dst_type == NOT_INIT) { |
| 6897 | *prev_dst_type = dst_reg_type; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6898 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6899 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6900 | return -EINVAL; |
| 6901 | } |
| 6902 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6903 | } else if (class == BPF_ST) { |
| 6904 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 6905 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6906 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6907 | return -EINVAL; |
| 6908 | } |
| 6909 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6910 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6911 | if (err) |
| 6912 | return err; |
| 6913 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 6914 | if (is_ctx_reg(env, insn->dst_reg)) { |
Joe Stringer | 9d2be44 | 2018-10-02 13:35:31 -0700 | [diff] [blame] | 6915 | 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] | 6916 | insn->dst_reg, |
| 6917 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 6918 | return -EACCES; |
| 6919 | } |
| 6920 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6921 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6922 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 6923 | insn->off, BPF_SIZE(insn->code), |
| 6924 | BPF_WRITE, -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6925 | if (err) |
| 6926 | return err; |
| 6927 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6928 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6929 | u8 opcode = BPF_OP(insn->code); |
| 6930 | |
| 6931 | if (opcode == BPF_CALL) { |
| 6932 | if (BPF_SRC(insn->code) != BPF_K || |
| 6933 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6934 | (insn->src_reg != BPF_REG_0 && |
| 6935 | insn->src_reg != BPF_PSEUDO_CALL) || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6936 | insn->dst_reg != BPF_REG_0 || |
| 6937 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6938 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6939 | return -EINVAL; |
| 6940 | } |
| 6941 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6942 | if (env->cur_state->active_spin_lock && |
| 6943 | (insn->src_reg == BPF_PSEUDO_CALL || |
| 6944 | insn->imm != BPF_FUNC_spin_unlock)) { |
| 6945 | verbose(env, "function calls are not allowed while holding a lock\n"); |
| 6946 | return -EINVAL; |
| 6947 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6948 | if (insn->src_reg == BPF_PSEUDO_CALL) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6949 | err = check_func_call(env, insn, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6950 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6951 | err = check_helper_call(env, insn->imm, env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6952 | if (err) |
| 6953 | return err; |
| 6954 | |
| 6955 | } else if (opcode == BPF_JA) { |
| 6956 | if (BPF_SRC(insn->code) != BPF_K || |
| 6957 | insn->imm != 0 || |
| 6958 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6959 | insn->dst_reg != BPF_REG_0 || |
| 6960 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6961 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6962 | return -EINVAL; |
| 6963 | } |
| 6964 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6965 | env->insn_idx += insn->off + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6966 | continue; |
| 6967 | |
| 6968 | } else if (opcode == BPF_EXIT) { |
| 6969 | if (BPF_SRC(insn->code) != BPF_K || |
| 6970 | insn->imm != 0 || |
| 6971 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 6972 | insn->dst_reg != BPF_REG_0 || |
| 6973 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6974 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6975 | return -EINVAL; |
| 6976 | } |
| 6977 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6978 | if (env->cur_state->active_spin_lock) { |
| 6979 | verbose(env, "bpf_spin_unlock is missing\n"); |
| 6980 | return -EINVAL; |
| 6981 | } |
| 6982 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6983 | if (state->curframe) { |
| 6984 | /* exit from nested function */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6985 | env->prev_insn_idx = env->insn_idx; |
| 6986 | err = prepare_func_exit(env, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6987 | if (err) |
| 6988 | return err; |
| 6989 | do_print_state = true; |
| 6990 | continue; |
| 6991 | } |
| 6992 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6993 | err = check_reference_leak(env); |
| 6994 | if (err) |
| 6995 | return err; |
| 6996 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6997 | /* eBPF calling convetion is such that R0 is used |
| 6998 | * to return the value from eBPF program. |
| 6999 | * Make sure that it's readable at this time |
| 7000 | * of bpf_exit, which means that program wrote |
| 7001 | * something into it earlier |
| 7002 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 7003 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7004 | if (err) |
| 7005 | return err; |
| 7006 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7007 | if (is_pointer_value(env, BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7008 | verbose(env, "R0 leaks addr as return value\n"); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 7009 | return -EACCES; |
| 7010 | } |
| 7011 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 7012 | err = check_return_code(env); |
| 7013 | if (err) |
| 7014 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7015 | process_bpf_exit: |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7016 | err = pop_stack(env, &env->prev_insn_idx, |
| 7017 | &env->insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7018 | if (err < 0) { |
| 7019 | if (err != -ENOENT) |
| 7020 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7021 | break; |
| 7022 | } else { |
| 7023 | do_print_state = true; |
| 7024 | continue; |
| 7025 | } |
| 7026 | } else { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7027 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7028 | if (err) |
| 7029 | return err; |
| 7030 | } |
| 7031 | } else if (class == BPF_LD) { |
| 7032 | u8 mode = BPF_MODE(insn->code); |
| 7033 | |
| 7034 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 7035 | err = check_ld_abs(env, insn); |
| 7036 | if (err) |
| 7037 | return err; |
| 7038 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7039 | } else if (mode == BPF_IMM) { |
| 7040 | err = check_ld_imm(env, insn); |
| 7041 | if (err) |
| 7042 | return err; |
| 7043 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7044 | env->insn_idx++; |
| 7045 | env->insn_aux_data[env->insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7046 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7047 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7048 | return -EINVAL; |
| 7049 | } |
| 7050 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7051 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7052 | return -EINVAL; |
| 7053 | } |
| 7054 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 7055 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7056 | } |
| 7057 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 7058 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7059 | return 0; |
| 7060 | } |
| 7061 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 7062 | static int check_map_prealloc(struct bpf_map *map) |
| 7063 | { |
| 7064 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 7065 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 7066 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 7067 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 7068 | } |
| 7069 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 7070 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
| 7071 | { |
| 7072 | switch (type) { |
| 7073 | case BPF_PROG_TYPE_KPROBE: |
| 7074 | case BPF_PROG_TYPE_TRACEPOINT: |
| 7075 | case BPF_PROG_TYPE_PERF_EVENT: |
| 7076 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 7077 | return true; |
| 7078 | default: |
| 7079 | return false; |
| 7080 | } |
| 7081 | } |
| 7082 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7083 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 7084 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 7085 | struct bpf_prog *prog) |
| 7086 | |
| 7087 | { |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 7088 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 7089 | * preallocated hash maps, since doing memory allocation |
| 7090 | * in overflow_handler can crash depending on where nmi got |
| 7091 | * triggered. |
| 7092 | */ |
| 7093 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 7094 | if (!check_map_prealloc(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7095 | 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] | 7096 | return -EINVAL; |
| 7097 | } |
| 7098 | if (map->inner_map_meta && |
| 7099 | !check_map_prealloc(map->inner_map_meta)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7100 | 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] | 7101 | return -EINVAL; |
| 7102 | } |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 7103 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 7104 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 7105 | if ((is_tracing_prog_type(prog->type) || |
| 7106 | prog->type == BPF_PROG_TYPE_SOCKET_FILTER) && |
| 7107 | map_value_has_spin_lock(map)) { |
| 7108 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); |
| 7109 | return -EINVAL; |
| 7110 | } |
| 7111 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 7112 | 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] | 7113 | !bpf_offload_prog_map_match(prog, map)) { |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 7114 | verbose(env, "offload device mismatch between prog and map\n"); |
| 7115 | return -EINVAL; |
| 7116 | } |
| 7117 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 7118 | return 0; |
| 7119 | } |
| 7120 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 7121 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 7122 | { |
| 7123 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 7124 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 7125 | } |
| 7126 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7127 | /* look for pseudo eBPF instructions that access map FDs and |
| 7128 | * replace them with actual map pointers |
| 7129 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7130 | 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] | 7131 | { |
| 7132 | struct bpf_insn *insn = env->prog->insnsi; |
| 7133 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 7134 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7135 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 7136 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 7137 | if (err) |
| 7138 | return err; |
| 7139 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7140 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7141 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7142 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7143 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7144 | return -EINVAL; |
| 7145 | } |
| 7146 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7147 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 7148 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 7149 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7150 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7151 | return -EINVAL; |
| 7152 | } |
| 7153 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7154 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7155 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7156 | struct bpf_map *map; |
| 7157 | struct fd f; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7158 | u64 addr; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7159 | |
| 7160 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 7161 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 7162 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7163 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7164 | return -EINVAL; |
| 7165 | } |
| 7166 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7167 | if (insn[0].src_reg == 0) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7168 | /* valid generic load 64-bit imm */ |
| 7169 | goto next_insn; |
| 7170 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7171 | /* In final convert_pseudo_ld_imm64() step, this is |
| 7172 | * converted into regular 64-bit imm load insn. |
| 7173 | */ |
| 7174 | if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD && |
| 7175 | insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) || |
| 7176 | (insn[0].src_reg == BPF_PSEUDO_MAP_FD && |
| 7177 | insn[1].imm != 0)) { |
| 7178 | verbose(env, |
| 7179 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7180 | return -EINVAL; |
| 7181 | } |
| 7182 | |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 7183 | f = fdget(insn[0].imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 7184 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7185 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7186 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 7187 | insn[0].imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7188 | return PTR_ERR(map); |
| 7189 | } |
| 7190 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7191 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 7192 | if (err) { |
| 7193 | fdput(f); |
| 7194 | return err; |
| 7195 | } |
| 7196 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7197 | aux = &env->insn_aux_data[i]; |
| 7198 | if (insn->src_reg == BPF_PSEUDO_MAP_FD) { |
| 7199 | addr = (unsigned long)map; |
| 7200 | } else { |
| 7201 | u32 off = insn[1].imm; |
| 7202 | |
| 7203 | if (off >= BPF_MAX_VAR_OFF) { |
| 7204 | verbose(env, "direct value offset of %u is not allowed\n", off); |
| 7205 | fdput(f); |
| 7206 | return -EINVAL; |
| 7207 | } |
| 7208 | |
| 7209 | if (!map->ops->map_direct_value_addr) { |
| 7210 | verbose(env, "no direct value access support for this map type\n"); |
| 7211 | fdput(f); |
| 7212 | return -EINVAL; |
| 7213 | } |
| 7214 | |
| 7215 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 7216 | if (err) { |
| 7217 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", |
| 7218 | map->value_size, off); |
| 7219 | fdput(f); |
| 7220 | return err; |
| 7221 | } |
| 7222 | |
| 7223 | aux->map_off = off; |
| 7224 | addr += off; |
| 7225 | } |
| 7226 | |
| 7227 | insn[0].imm = (u32)addr; |
| 7228 | insn[1].imm = addr >> 32; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7229 | |
| 7230 | /* check whether we recorded this map already */ |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7231 | for (j = 0; j < env->used_map_cnt; j++) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7232 | if (env->used_maps[j] == map) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7233 | aux->map_index = j; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7234 | fdput(f); |
| 7235 | goto next_insn; |
| 7236 | } |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7237 | } |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7238 | |
| 7239 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 7240 | fdput(f); |
| 7241 | return -E2BIG; |
| 7242 | } |
| 7243 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7244 | /* hold the map. If the program is rejected by verifier, |
| 7245 | * the map will be released by release_maps() or it |
| 7246 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 7247 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7248 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 7249 | map = bpf_map_inc(map, false); |
| 7250 | if (IS_ERR(map)) { |
| 7251 | fdput(f); |
| 7252 | return PTR_ERR(map); |
| 7253 | } |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 7254 | |
| 7255 | aux->map_index = env->used_map_cnt; |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 7256 | env->used_maps[env->used_map_cnt++] = map; |
| 7257 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 7258 | if (bpf_map_is_cgroup_storage(map) && |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 7259 | bpf_cgroup_storage_assign(env->prog, map)) { |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 7260 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 7261 | fdput(f); |
| 7262 | return -EBUSY; |
| 7263 | } |
| 7264 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7265 | fdput(f); |
| 7266 | next_insn: |
| 7267 | insn++; |
| 7268 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 7269 | continue; |
| 7270 | } |
| 7271 | |
| 7272 | /* Basic sanity check before we invest more work here. */ |
| 7273 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 7274 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 7275 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7276 | } |
| 7277 | } |
| 7278 | |
| 7279 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 7280 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 7281 | * These pointers will be used later by verifier to validate map access. |
| 7282 | */ |
| 7283 | return 0; |
| 7284 | } |
| 7285 | |
| 7286 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7287 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7288 | { |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 7289 | enum bpf_cgroup_storage_type stype; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7290 | int i; |
| 7291 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 7292 | for_each_cgroup_storage_type(stype) { |
| 7293 | if (!env->prog->aux->cgroup_storage[stype]) |
| 7294 | continue; |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 7295 | bpf_cgroup_storage_release(env->prog, |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 7296 | env->prog->aux->cgroup_storage[stype]); |
| 7297 | } |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 7298 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7299 | for (i = 0; i < env->used_map_cnt; i++) |
| 7300 | bpf_map_put(env->used_maps[i]); |
| 7301 | } |
| 7302 | |
| 7303 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7304 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7305 | { |
| 7306 | struct bpf_insn *insn = env->prog->insnsi; |
| 7307 | int insn_cnt = env->prog->len; |
| 7308 | int i; |
| 7309 | |
| 7310 | for (i = 0; i < insn_cnt; i++, insn++) |
| 7311 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 7312 | insn->src_reg = 0; |
| 7313 | } |
| 7314 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7315 | /* single env->prog->insni[off] instruction was replaced with the range |
| 7316 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 7317 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 7318 | */ |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 7319 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, |
| 7320 | struct bpf_prog *new_prog, u32 off, u32 cnt) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7321 | { |
| 7322 | 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] | 7323 | struct bpf_insn *insn = new_prog->insnsi; |
| 7324 | u32 prog_len; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7325 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7326 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 7327 | /* aux info at OFF always needs adjustment, no matter fast path |
| 7328 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the |
| 7329 | * original insn at old prog. |
| 7330 | */ |
| 7331 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); |
| 7332 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7333 | if (cnt == 1) |
| 7334 | return 0; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 7335 | prog_len = new_prog->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 7336 | new_data = vzalloc(array_size(prog_len, |
| 7337 | sizeof(struct bpf_insn_aux_data))); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7338 | if (!new_data) |
| 7339 | return -ENOMEM; |
| 7340 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 7341 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 7342 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 7343 | for (i = off; i < off + cnt - 1; i++) { |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7344 | new_data[i].seen = true; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 7345 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
| 7346 | } |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7347 | env->insn_aux_data = new_data; |
| 7348 | vfree(old_data); |
| 7349 | return 0; |
| 7350 | } |
| 7351 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7352 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 7353 | { |
| 7354 | int i; |
| 7355 | |
| 7356 | if (len == 1) |
| 7357 | return; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 7358 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 7359 | for (i = 0; i <= env->subprog_cnt; i++) { |
Edward Cree | afd5942 | 2018-11-16 12:00:07 +0000 | [diff] [blame] | 7360 | if (env->subprog_info[i].start <= off) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7361 | continue; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 7362 | env->subprog_info[i].start += len - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7363 | } |
| 7364 | } |
| 7365 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7366 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 7367 | const struct bpf_insn *patch, u32 len) |
| 7368 | { |
| 7369 | struct bpf_prog *new_prog; |
| 7370 | |
| 7371 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 7372 | if (IS_ERR(new_prog)) { |
| 7373 | if (PTR_ERR(new_prog) == -ERANGE) |
| 7374 | verbose(env, |
| 7375 | "insn %d cannot be patched due to 16-bit range\n", |
| 7376 | env->insn_aux_data[off].orig_idx); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7377 | return NULL; |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 7378 | } |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 7379 | if (adjust_insn_aux_data(env, new_prog, off, len)) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7380 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 7381 | adjust_subprog_starts(env, off, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7382 | return new_prog; |
| 7383 | } |
| 7384 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 7385 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
| 7386 | u32 off, u32 cnt) |
| 7387 | { |
| 7388 | int i, j; |
| 7389 | |
| 7390 | /* find first prog starting at or after off (first to remove) */ |
| 7391 | for (i = 0; i < env->subprog_cnt; i++) |
| 7392 | if (env->subprog_info[i].start >= off) |
| 7393 | break; |
| 7394 | /* find first prog starting at or after off + cnt (first to stay) */ |
| 7395 | for (j = i; j < env->subprog_cnt; j++) |
| 7396 | if (env->subprog_info[j].start >= off + cnt) |
| 7397 | break; |
| 7398 | /* if j doesn't start exactly at off + cnt, we are just removing |
| 7399 | * the front of previous prog |
| 7400 | */ |
| 7401 | if (env->subprog_info[j].start != off + cnt) |
| 7402 | j--; |
| 7403 | |
| 7404 | if (j > i) { |
| 7405 | struct bpf_prog_aux *aux = env->prog->aux; |
| 7406 | int move; |
| 7407 | |
| 7408 | /* move fake 'exit' subprog as well */ |
| 7409 | move = env->subprog_cnt + 1 - j; |
| 7410 | |
| 7411 | memmove(env->subprog_info + i, |
| 7412 | env->subprog_info + j, |
| 7413 | sizeof(*env->subprog_info) * move); |
| 7414 | env->subprog_cnt -= j - i; |
| 7415 | |
| 7416 | /* remove func_info */ |
| 7417 | if (aux->func_info) { |
| 7418 | move = aux->func_info_cnt - j; |
| 7419 | |
| 7420 | memmove(aux->func_info + i, |
| 7421 | aux->func_info + j, |
| 7422 | sizeof(*aux->func_info) * move); |
| 7423 | aux->func_info_cnt -= j - i; |
| 7424 | /* func_info->insn_off is set after all code rewrites, |
| 7425 | * in adjust_btf_func() - no need to adjust |
| 7426 | */ |
| 7427 | } |
| 7428 | } else { |
| 7429 | /* convert i from "first prog to remove" to "first to adjust" */ |
| 7430 | if (env->subprog_info[i].start == off) |
| 7431 | i++; |
| 7432 | } |
| 7433 | |
| 7434 | /* update fake 'exit' subprog as well */ |
| 7435 | for (; i <= env->subprog_cnt; i++) |
| 7436 | env->subprog_info[i].start -= cnt; |
| 7437 | |
| 7438 | return 0; |
| 7439 | } |
| 7440 | |
| 7441 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, |
| 7442 | u32 cnt) |
| 7443 | { |
| 7444 | struct bpf_prog *prog = env->prog; |
| 7445 | u32 i, l_off, l_cnt, nr_linfo; |
| 7446 | struct bpf_line_info *linfo; |
| 7447 | |
| 7448 | nr_linfo = prog->aux->nr_linfo; |
| 7449 | if (!nr_linfo) |
| 7450 | return 0; |
| 7451 | |
| 7452 | linfo = prog->aux->linfo; |
| 7453 | |
| 7454 | /* find first line info to remove, count lines to be removed */ |
| 7455 | for (i = 0; i < nr_linfo; i++) |
| 7456 | if (linfo[i].insn_off >= off) |
| 7457 | break; |
| 7458 | |
| 7459 | l_off = i; |
| 7460 | l_cnt = 0; |
| 7461 | for (; i < nr_linfo; i++) |
| 7462 | if (linfo[i].insn_off < off + cnt) |
| 7463 | l_cnt++; |
| 7464 | else |
| 7465 | break; |
| 7466 | |
| 7467 | /* First live insn doesn't match first live linfo, it needs to "inherit" |
| 7468 | * last removed linfo. prog is already modified, so prog->len == off |
| 7469 | * means no live instructions after (tail of the program was removed). |
| 7470 | */ |
| 7471 | if (prog->len != off && l_cnt && |
| 7472 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { |
| 7473 | l_cnt--; |
| 7474 | linfo[--i].insn_off = off + cnt; |
| 7475 | } |
| 7476 | |
| 7477 | /* remove the line info which refer to the removed instructions */ |
| 7478 | if (l_cnt) { |
| 7479 | memmove(linfo + l_off, linfo + i, |
| 7480 | sizeof(*linfo) * (nr_linfo - i)); |
| 7481 | |
| 7482 | prog->aux->nr_linfo -= l_cnt; |
| 7483 | nr_linfo = prog->aux->nr_linfo; |
| 7484 | } |
| 7485 | |
| 7486 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ |
| 7487 | for (i = l_off; i < nr_linfo; i++) |
| 7488 | linfo[i].insn_off -= cnt; |
| 7489 | |
| 7490 | /* fix up all subprogs (incl. 'exit') which start >= off */ |
| 7491 | for (i = 0; i <= env->subprog_cnt; i++) |
| 7492 | if (env->subprog_info[i].linfo_idx > l_off) { |
| 7493 | /* program may have started in the removed region but |
| 7494 | * may not be fully removed |
| 7495 | */ |
| 7496 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) |
| 7497 | env->subprog_info[i].linfo_idx -= l_cnt; |
| 7498 | else |
| 7499 | env->subprog_info[i].linfo_idx = l_off; |
| 7500 | } |
| 7501 | |
| 7502 | return 0; |
| 7503 | } |
| 7504 | |
| 7505 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) |
| 7506 | { |
| 7507 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 7508 | unsigned int orig_prog_len = env->prog->len; |
| 7509 | int err; |
| 7510 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 7511 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 7512 | bpf_prog_offload_remove_insns(env, off, cnt); |
| 7513 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 7514 | err = bpf_remove_insns(env->prog, off, cnt); |
| 7515 | if (err) |
| 7516 | return err; |
| 7517 | |
| 7518 | err = adjust_subprog_starts_after_remove(env, off, cnt); |
| 7519 | if (err) |
| 7520 | return err; |
| 7521 | |
| 7522 | err = bpf_adj_linfo_after_remove(env, off, cnt); |
| 7523 | if (err) |
| 7524 | return err; |
| 7525 | |
| 7526 | memmove(aux_data + off, aux_data + off + cnt, |
| 7527 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); |
| 7528 | |
| 7529 | return 0; |
| 7530 | } |
| 7531 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 7532 | /* The verifier does more data flow analysis than llvm and will not |
| 7533 | * explore branches that are dead at run time. Malicious programs can |
| 7534 | * have dead code too. Therefore replace all dead at-run-time code |
| 7535 | * with 'ja -1'. |
| 7536 | * |
| 7537 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 7538 | * program and through another bug we would manage to jump there, then |
| 7539 | * we'd execute beyond program memory otherwise. Returning exception |
| 7540 | * code also wouldn't work since we can have subprogs where the dead |
| 7541 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7542 | */ |
| 7543 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 7544 | { |
| 7545 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 7546 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7547 | struct bpf_insn *insn = env->prog->insnsi; |
| 7548 | const int insn_cnt = env->prog->len; |
| 7549 | int i; |
| 7550 | |
| 7551 | for (i = 0; i < insn_cnt; i++) { |
| 7552 | if (aux_data[i].seen) |
| 7553 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 7554 | memcpy(insn + i, &trap, sizeof(trap)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 7555 | } |
| 7556 | } |
| 7557 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 7558 | static bool insn_is_cond_jump(u8 code) |
| 7559 | { |
| 7560 | u8 op; |
| 7561 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 7562 | if (BPF_CLASS(code) == BPF_JMP32) |
| 7563 | return true; |
| 7564 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 7565 | if (BPF_CLASS(code) != BPF_JMP) |
| 7566 | return false; |
| 7567 | |
| 7568 | op = BPF_OP(code); |
| 7569 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; |
| 7570 | } |
| 7571 | |
| 7572 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) |
| 7573 | { |
| 7574 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 7575 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 7576 | struct bpf_insn *insn = env->prog->insnsi; |
| 7577 | const int insn_cnt = env->prog->len; |
| 7578 | int i; |
| 7579 | |
| 7580 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 7581 | if (!insn_is_cond_jump(insn->code)) |
| 7582 | continue; |
| 7583 | |
| 7584 | if (!aux_data[i + 1].seen) |
| 7585 | ja.off = insn->off; |
| 7586 | else if (!aux_data[i + 1 + insn->off].seen) |
| 7587 | ja.off = 0; |
| 7588 | else |
| 7589 | continue; |
| 7590 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 7591 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 7592 | bpf_prog_offload_replace_insn(env, i, &ja); |
| 7593 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 7594 | memcpy(insn, &ja, sizeof(ja)); |
| 7595 | } |
| 7596 | } |
| 7597 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 7598 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
| 7599 | { |
| 7600 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 7601 | int insn_cnt = env->prog->len; |
| 7602 | int i, err; |
| 7603 | |
| 7604 | for (i = 0; i < insn_cnt; i++) { |
| 7605 | int j; |
| 7606 | |
| 7607 | j = 0; |
| 7608 | while (i + j < insn_cnt && !aux_data[i + j].seen) |
| 7609 | j++; |
| 7610 | if (!j) |
| 7611 | continue; |
| 7612 | |
| 7613 | err = verifier_remove_insns(env, i, j); |
| 7614 | if (err) |
| 7615 | return err; |
| 7616 | insn_cnt = env->prog->len; |
| 7617 | } |
| 7618 | |
| 7619 | return 0; |
| 7620 | } |
| 7621 | |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 7622 | static int opt_remove_nops(struct bpf_verifier_env *env) |
| 7623 | { |
| 7624 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 7625 | struct bpf_insn *insn = env->prog->insnsi; |
| 7626 | int insn_cnt = env->prog->len; |
| 7627 | int i, err; |
| 7628 | |
| 7629 | for (i = 0; i < insn_cnt; i++) { |
| 7630 | if (memcmp(&insn[i], &ja, sizeof(ja))) |
| 7631 | continue; |
| 7632 | |
| 7633 | err = verifier_remove_insns(env, i, 1); |
| 7634 | if (err) |
| 7635 | return err; |
| 7636 | insn_cnt--; |
| 7637 | i--; |
| 7638 | } |
| 7639 | |
| 7640 | return 0; |
| 7641 | } |
| 7642 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame^] | 7643 | static int opt_subreg_zext_lo32(struct bpf_verifier_env *env) |
| 7644 | { |
| 7645 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
| 7646 | struct bpf_insn *insns = env->prog->insnsi; |
| 7647 | int i, delta = 0, len = env->prog->len; |
| 7648 | struct bpf_insn zext_patch[2]; |
| 7649 | struct bpf_prog *new_prog; |
| 7650 | |
| 7651 | zext_patch[1] = BPF_ZEXT_REG(0); |
| 7652 | for (i = 0; i < len; i++) { |
| 7653 | int adj_idx = i + delta; |
| 7654 | struct bpf_insn insn; |
| 7655 | |
| 7656 | if (!aux[adj_idx].zext_dst) |
| 7657 | continue; |
| 7658 | |
| 7659 | insn = insns[adj_idx]; |
| 7660 | zext_patch[0] = insn; |
| 7661 | zext_patch[1].dst_reg = insn.dst_reg; |
| 7662 | zext_patch[1].src_reg = insn.dst_reg; |
| 7663 | new_prog = bpf_patch_insn_data(env, adj_idx, zext_patch, 2); |
| 7664 | if (!new_prog) |
| 7665 | return -ENOMEM; |
| 7666 | env->prog = new_prog; |
| 7667 | insns = new_prog->insnsi; |
| 7668 | aux = env->insn_aux_data; |
| 7669 | delta += 2; |
| 7670 | } |
| 7671 | |
| 7672 | return 0; |
| 7673 | } |
| 7674 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7675 | /* convert load instructions that access fields of a context type into a |
| 7676 | * sequence of instructions that access fields of the underlying structure: |
| 7677 | * struct __sk_buff -> struct sk_buff |
| 7678 | * struct bpf_sock_ops -> struct sock |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7679 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 7680 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7681 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 7682 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7683 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7684 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7685 | struct bpf_insn insn_buf[16], *insn; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7686 | u32 target_size, size_default, off; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7687 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7688 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7689 | bool is_narrower_load; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7690 | |
Daniel Borkmann | b09928b | 2018-10-24 22:05:49 +0200 | [diff] [blame] | 7691 | if (ops->gen_prologue || env->seen_direct_write) { |
| 7692 | if (!ops->gen_prologue) { |
| 7693 | verbose(env, "bpf verifier is misconfigured\n"); |
| 7694 | return -EINVAL; |
| 7695 | } |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7696 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 7697 | env->prog); |
| 7698 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7699 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7700 | return -EINVAL; |
| 7701 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7702 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7703 | if (!new_prog) |
| 7704 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7705 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7706 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7707 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7708 | } |
| 7709 | } |
| 7710 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7711 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7712 | return 0; |
| 7713 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7714 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 7715 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7716 | for (i = 0; i < insn_cnt; i++, insn++) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7717 | bpf_convert_ctx_access_t convert_ctx_access; |
| 7718 | |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 7719 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 7720 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 7721 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 7722 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7723 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 7724 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 7725 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 7726 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 7727 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 7728 | type = BPF_WRITE; |
| 7729 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7730 | continue; |
| 7731 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 7732 | if (type == BPF_WRITE && |
| 7733 | env->insn_aux_data[i + delta].sanitize_stack_off) { |
| 7734 | struct bpf_insn patch[] = { |
| 7735 | /* Sanitize suspicious stack slot with zero. |
| 7736 | * There are no memory dependencies for this store, |
| 7737 | * since it's only using frame pointer and immediate |
| 7738 | * constant of zero |
| 7739 | */ |
| 7740 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, |
| 7741 | env->insn_aux_data[i + delta].sanitize_stack_off, |
| 7742 | 0), |
| 7743 | /* the original STX instruction will immediately |
| 7744 | * overwrite the same stack slot with appropriate value |
| 7745 | */ |
| 7746 | *insn, |
| 7747 | }; |
| 7748 | |
| 7749 | cnt = ARRAY_SIZE(patch); |
| 7750 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 7751 | if (!new_prog) |
| 7752 | return -ENOMEM; |
| 7753 | |
| 7754 | delta += cnt - 1; |
| 7755 | env->prog = new_prog; |
| 7756 | insn = new_prog->insnsi + i + delta; |
| 7757 | continue; |
| 7758 | } |
| 7759 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7760 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 7761 | case PTR_TO_CTX: |
| 7762 | if (!ops->convert_ctx_access) |
| 7763 | continue; |
| 7764 | convert_ctx_access = ops->convert_ctx_access; |
| 7765 | break; |
| 7766 | case PTR_TO_SOCKET: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 7767 | case PTR_TO_SOCK_COMMON: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7768 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 7769 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 7770 | case PTR_TO_TCP_SOCK: |
| 7771 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; |
| 7772 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7773 | default: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7774 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7775 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7776 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7777 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7778 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7779 | |
| 7780 | /* If the read access is a narrower load of the field, |
| 7781 | * convert to a 4/8-byte load, to minimum program type specific |
| 7782 | * convert_ctx_access changes. If conversion is successful, |
| 7783 | * we will apply proper mask to the result. |
| 7784 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7785 | is_narrower_load = size < ctx_field_size; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7786 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
| 7787 | off = insn->off; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7788 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7789 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7790 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7791 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7792 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7793 | return -EINVAL; |
| 7794 | } |
| 7795 | |
| 7796 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7797 | if (ctx_field_size == 4) |
| 7798 | size_code = BPF_W; |
| 7799 | else if (ctx_field_size == 8) |
| 7800 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7801 | |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 7802 | insn->off = off & ~(size_default - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7803 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 7804 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7805 | |
| 7806 | target_size = 0; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7807 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 7808 | &target_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7809 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 7810 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 7811 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7812 | return -EINVAL; |
| 7813 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7814 | |
| 7815 | if (is_narrower_load && size < target_size) { |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7816 | u8 shift = (off & (size_default - 1)) * 8; |
| 7817 | |
| 7818 | if (ctx_field_size <= 4) { |
| 7819 | if (shift) |
| 7820 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, |
| 7821 | insn->dst_reg, |
| 7822 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7823 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 7824 | (1 << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7825 | } else { |
| 7826 | if (shift) |
| 7827 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, |
| 7828 | insn->dst_reg, |
| 7829 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7830 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Krzesimir Nowak | e2f7fc0 | 2019-05-08 18:08:58 +0200 | [diff] [blame] | 7831 | (1ULL << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 7832 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 7833 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7834 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 7835 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7836 | if (!new_prog) |
| 7837 | return -ENOMEM; |
| 7838 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7839 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7840 | |
| 7841 | /* keep walking new program and skip insns we just inserted */ |
| 7842 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7843 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7844 | } |
| 7845 | |
| 7846 | return 0; |
| 7847 | } |
| 7848 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7849 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 7850 | { |
| 7851 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 7852 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7853 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7854 | void *old_bpf_func; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 7855 | int err; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7856 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7857 | if (env->subprog_cnt <= 1) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7858 | return 0; |
| 7859 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7860 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7861 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 7862 | insn->src_reg != BPF_PSEUDO_CALL) |
| 7863 | continue; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 7864 | /* Upon error here we cannot fall back to interpreter but |
| 7865 | * need a hard reject of the program. Thus -EFAULT is |
| 7866 | * propagated in any case. |
| 7867 | */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7868 | subprog = find_subprog(env, i + insn->imm + 1); |
| 7869 | if (subprog < 0) { |
| 7870 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 7871 | i + insn->imm + 1); |
| 7872 | return -EFAULT; |
| 7873 | } |
| 7874 | /* temporarily remember subprog id inside insn instead of |
| 7875 | * aux_data, since next loop will split up all insns into funcs |
| 7876 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7877 | insn->off = subprog; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7878 | /* remember original imm in case JIT fails and fallback |
| 7879 | * to interpreter will be needed |
| 7880 | */ |
| 7881 | env->insn_aux_data[i].call_imm = insn->imm; |
| 7882 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 7883 | insn->imm = 1; |
| 7884 | } |
| 7885 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 7886 | err = bpf_prog_alloc_jited_linfo(prog); |
| 7887 | if (err) |
| 7888 | goto out_undo_insn; |
| 7889 | |
| 7890 | err = -ENOMEM; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 7891 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7892 | if (!func) |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 7893 | goto out_undo_insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7894 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7895 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7896 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 7897 | subprog_end = env->subprog_info[i + 1].start; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7898 | |
| 7899 | len = subprog_end - subprog_start; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 7900 | /* BPF_PROG_RUN doesn't call subprogs directly, |
| 7901 | * hence main prog stats include the runtime of subprogs. |
| 7902 | * subprogs don't have IDs and not reachable via prog_get_next_id |
| 7903 | * func[i]->aux->stats will never be accessed and stays NULL |
| 7904 | */ |
| 7905 | 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] | 7906 | if (!func[i]) |
| 7907 | goto out_free; |
| 7908 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 7909 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 7910 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7911 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 7912 | if (bpf_prog_calc_tag(func[i])) |
| 7913 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7914 | func[i]->is_func = 1; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 7915 | func[i]->aux->func_idx = i; |
| 7916 | /* the btf and func_info will be freed only at prog->aux */ |
| 7917 | func[i]->aux->btf = prog->aux->btf; |
| 7918 | func[i]->aux->func_info = prog->aux->func_info; |
| 7919 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7920 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 7921 | * Long term would need debug info to populate names |
| 7922 | */ |
| 7923 | func[i]->aux->name[0] = 'F'; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 7924 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7925 | func[i]->jit_requested = 1; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 7926 | func[i]->aux->linfo = prog->aux->linfo; |
| 7927 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; |
| 7928 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; |
| 7929 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7930 | func[i] = bpf_int_jit_compile(func[i]); |
| 7931 | if (!func[i]->jited) { |
| 7932 | err = -ENOTSUPP; |
| 7933 | goto out_free; |
| 7934 | } |
| 7935 | cond_resched(); |
| 7936 | } |
| 7937 | /* at this point all bpf functions were successfully JITed |
| 7938 | * now populate all bpf_calls with correct addresses and |
| 7939 | * run last pass of JIT |
| 7940 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7941 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7942 | insn = func[i]->insnsi; |
| 7943 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 7944 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 7945 | insn->src_reg != BPF_PSEUDO_CALL) |
| 7946 | continue; |
| 7947 | subprog = insn->off; |
Prashant Bhole | 0d306c3 | 2019-04-16 18:13:01 +0900 | [diff] [blame] | 7948 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
| 7949 | __bpf_call_base; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7950 | } |
Sandipan Das | 2162fed | 2018-05-24 12:26:45 +0530 | [diff] [blame] | 7951 | |
| 7952 | /* we use the aux data to keep a list of the start addresses |
| 7953 | * of the JITed images for each function in the program |
| 7954 | * |
| 7955 | * for some architectures, such as powerpc64, the imm field |
| 7956 | * might not be large enough to hold the offset of the start |
| 7957 | * address of the callee's JITed image from __bpf_call_base |
| 7958 | * |
| 7959 | * in such cases, we can lookup the start address of a callee |
| 7960 | * by using its subprog id, available from the off field of |
| 7961 | * the call instruction, as an index for this list |
| 7962 | */ |
| 7963 | func[i]->aux->func = func; |
| 7964 | func[i]->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7965 | } |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7966 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7967 | old_bpf_func = func[i]->bpf_func; |
| 7968 | tmp = bpf_int_jit_compile(func[i]); |
| 7969 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 7970 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 7971 | err = -ENOTSUPP; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7972 | goto out_free; |
| 7973 | } |
| 7974 | cond_resched(); |
| 7975 | } |
| 7976 | |
| 7977 | /* finally lock prog and jit images for all functions and |
| 7978 | * populate kallsysm |
| 7979 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 7980 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7981 | bpf_prog_lock_ro(func[i]); |
| 7982 | bpf_prog_kallsyms_add(func[i]); |
| 7983 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7984 | |
| 7985 | /* Last step: make now unused interpreter insns from main |
| 7986 | * prog consistent for later dump requests, so they can |
| 7987 | * later look the same as if they were interpreted only. |
| 7988 | */ |
| 7989 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7990 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 7991 | insn->src_reg != BPF_PSEUDO_CALL) |
| 7992 | continue; |
| 7993 | insn->off = env->insn_aux_data[i].call_imm; |
| 7994 | subprog = find_subprog(env, i + insn->off + 1); |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 7995 | insn->imm = subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 7996 | } |
| 7997 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 7998 | prog->jited = 1; |
| 7999 | prog->bpf_func = func[0]->bpf_func; |
| 8000 | prog->aux->func = func; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 8001 | prog->aux->func_cnt = env->subprog_cnt; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8002 | bpf_prog_free_unused_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8003 | return 0; |
| 8004 | out_free: |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 8005 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8006 | if (func[i]) |
| 8007 | bpf_jit_free(func[i]); |
| 8008 | kfree(func); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 8009 | out_undo_insn: |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8010 | /* cleanup main prog to be interpreted */ |
| 8011 | prog->jit_requested = 0; |
| 8012 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 8013 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 8014 | insn->src_reg != BPF_PSEUDO_CALL) |
| 8015 | continue; |
| 8016 | insn->off = 0; |
| 8017 | insn->imm = env->insn_aux_data[i].call_imm; |
| 8018 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8019 | bpf_prog_free_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8020 | return err; |
| 8021 | } |
| 8022 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 8023 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 8024 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 8025 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 8026 | struct bpf_prog *prog = env->prog; |
| 8027 | struct bpf_insn *insn = prog->insnsi; |
| 8028 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 8029 | #endif |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 8030 | int err = 0; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 8031 | |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 8032 | if (env->prog->jit_requested && |
| 8033 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 8034 | err = jit_subprogs(env); |
| 8035 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 8036 | return 0; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 8037 | if (err == -EFAULT) |
| 8038 | return err; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 8039 | } |
| 8040 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 8041 | for (i = 0; i < prog->len; i++, insn++) { |
| 8042 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 8043 | insn->src_reg != BPF_PSEUDO_CALL) |
| 8044 | continue; |
| 8045 | depth = get_callee_stack_depth(env, insn, i); |
| 8046 | if (depth < 0) |
| 8047 | return depth; |
| 8048 | bpf_patch_call_args(insn, depth); |
| 8049 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 8050 | err = 0; |
| 8051 | #endif |
| 8052 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 8053 | } |
| 8054 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8055 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8056 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8057 | * |
| 8058 | * this function is called after eBPF program passed verification |
| 8059 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8060 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8061 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8062 | struct bpf_prog *prog = env->prog; |
| 8063 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8064 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8065 | const int insn_cnt = prog->len; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8066 | const struct bpf_map_ops *ops; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 8067 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8068 | struct bpf_insn insn_buf[16]; |
| 8069 | struct bpf_prog *new_prog; |
| 8070 | struct bpf_map *map_ptr; |
| 8071 | int i, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8072 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8073 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 8074 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 8075 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 8076 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 8077 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 8078 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
| 8079 | struct bpf_insn mask_and_div[] = { |
| 8080 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 8081 | /* Rx div 0 -> 0 */ |
| 8082 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), |
| 8083 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 8084 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 8085 | *insn, |
| 8086 | }; |
| 8087 | struct bpf_insn mask_and_mod[] = { |
| 8088 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 8089 | /* Rx mod 0 -> Rx */ |
| 8090 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), |
| 8091 | *insn, |
| 8092 | }; |
| 8093 | struct bpf_insn *patchlet; |
| 8094 | |
| 8095 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 8096 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 8097 | patchlet = mask_and_div + (is64 ? 1 : 0); |
| 8098 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); |
| 8099 | } else { |
| 8100 | patchlet = mask_and_mod + (is64 ? 1 : 0); |
| 8101 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); |
| 8102 | } |
| 8103 | |
| 8104 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 8105 | if (!new_prog) |
| 8106 | return -ENOMEM; |
| 8107 | |
| 8108 | delta += cnt - 1; |
| 8109 | env->prog = prog = new_prog; |
| 8110 | insn = new_prog->insnsi + i + delta; |
| 8111 | continue; |
| 8112 | } |
| 8113 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 8114 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 8115 | (BPF_MODE(insn->code) == BPF_ABS || |
| 8116 | BPF_MODE(insn->code) == BPF_IND)) { |
| 8117 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 8118 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 8119 | verbose(env, "bpf verifier is misconfigured\n"); |
| 8120 | return -EINVAL; |
| 8121 | } |
| 8122 | |
| 8123 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 8124 | if (!new_prog) |
| 8125 | return -ENOMEM; |
| 8126 | |
| 8127 | delta += cnt - 1; |
| 8128 | env->prog = prog = new_prog; |
| 8129 | insn = new_prog->insnsi + i + delta; |
| 8130 | continue; |
| 8131 | } |
| 8132 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 8133 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
| 8134 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { |
| 8135 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; |
| 8136 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; |
| 8137 | struct bpf_insn insn_buf[16]; |
| 8138 | struct bpf_insn *patch = &insn_buf[0]; |
| 8139 | bool issrc, isneg; |
| 8140 | u32 off_reg; |
| 8141 | |
| 8142 | aux = &env->insn_aux_data[i + delta]; |
Daniel Borkmann | 3612af7 | 2019-03-01 22:05:29 +0100 | [diff] [blame] | 8143 | if (!aux->alu_state || |
| 8144 | aux->alu_state == BPF_ALU_NON_POINTER) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 8145 | continue; |
| 8146 | |
| 8147 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; |
| 8148 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == |
| 8149 | BPF_ALU_SANITIZE_SRC; |
| 8150 | |
| 8151 | off_reg = issrc ? insn->src_reg : insn->dst_reg; |
| 8152 | if (isneg) |
| 8153 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 8154 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); |
| 8155 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); |
| 8156 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); |
| 8157 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); |
| 8158 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); |
| 8159 | if (issrc) { |
| 8160 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, |
| 8161 | off_reg); |
| 8162 | insn->src_reg = BPF_REG_AX; |
| 8163 | } else { |
| 8164 | *patch++ = BPF_ALU64_REG(BPF_AND, off_reg, |
| 8165 | BPF_REG_AX); |
| 8166 | } |
| 8167 | if (isneg) |
| 8168 | insn->code = insn->code == code_add ? |
| 8169 | code_sub : code_add; |
| 8170 | *patch++ = *insn; |
| 8171 | if (issrc && isneg) |
| 8172 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 8173 | cnt = patch - insn_buf; |
| 8174 | |
| 8175 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 8176 | if (!new_prog) |
| 8177 | return -ENOMEM; |
| 8178 | |
| 8179 | delta += cnt - 1; |
| 8180 | env->prog = prog = new_prog; |
| 8181 | insn = new_prog->insnsi + i + delta; |
| 8182 | continue; |
| 8183 | } |
| 8184 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8185 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 8186 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 8187 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 8188 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8189 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8190 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 8191 | prog->dst_needed = 1; |
| 8192 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 8193 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 8194 | if (insn->imm == BPF_FUNC_override_return) |
| 8195 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8196 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 8197 | /* If we tail call into other programs, we |
| 8198 | * cannot make any assumptions since they can |
| 8199 | * be replaced dynamically during runtime in |
| 8200 | * the program array. |
| 8201 | */ |
| 8202 | prog->cb_access = 1; |
Alexei Starovoitov | 80a58d0 | 2017-05-30 13:31:30 -0700 | [diff] [blame] | 8203 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 8204 | env->prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 8205 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8206 | /* mark bpf_tail_call as different opcode to avoid |
| 8207 | * conditional branch in the interpeter for every normal |
| 8208 | * call and to prevent accidental JITing by JIT compiler |
| 8209 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8210 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8211 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 8212 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 8213 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 8214 | aux = &env->insn_aux_data[i + delta]; |
| 8215 | if (!bpf_map_ptr_unpriv(aux)) |
| 8216 | continue; |
| 8217 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 8218 | /* instead of changing every JIT dealing with tail_call |
| 8219 | * emit two extra insns: |
| 8220 | * if (index >= max_entries) goto out; |
| 8221 | * index &= array->index_mask; |
| 8222 | * to avoid out-of-bounds cpu speculation |
| 8223 | */ |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 8224 | if (bpf_map_ptr_poisoned(aux)) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 8225 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 8226 | return -EINVAL; |
| 8227 | } |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 8228 | |
| 8229 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 8230 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 8231 | map_ptr->max_entries, 2); |
| 8232 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 8233 | container_of(map_ptr, |
| 8234 | struct bpf_array, |
| 8235 | map)->index_mask); |
| 8236 | insn_buf[2] = *insn; |
| 8237 | cnt = 3; |
| 8238 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 8239 | if (!new_prog) |
| 8240 | return -ENOMEM; |
| 8241 | |
| 8242 | delta += cnt - 1; |
| 8243 | env->prog = prog = new_prog; |
| 8244 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8245 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8246 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8247 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 8248 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8249 | * and other inlining handlers are currently limited to 64 bit |
| 8250 | * only. |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 8251 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 8252 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8253 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 8254 | insn->imm == BPF_FUNC_map_update_elem || |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 8255 | insn->imm == BPF_FUNC_map_delete_elem || |
| 8256 | insn->imm == BPF_FUNC_map_push_elem || |
| 8257 | insn->imm == BPF_FUNC_map_pop_elem || |
| 8258 | insn->imm == BPF_FUNC_map_peek_elem)) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 8259 | aux = &env->insn_aux_data[i + delta]; |
| 8260 | if (bpf_map_ptr_poisoned(aux)) |
| 8261 | goto patch_call_imm; |
| 8262 | |
| 8263 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8264 | ops = map_ptr->ops; |
| 8265 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 8266 | ops->map_gen_lookup) { |
| 8267 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
| 8268 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 8269 | verbose(env, "bpf verifier is misconfigured\n"); |
| 8270 | return -EINVAL; |
| 8271 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8272 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8273 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 8274 | insn_buf, cnt); |
| 8275 | if (!new_prog) |
| 8276 | return -ENOMEM; |
| 8277 | |
| 8278 | delta += cnt - 1; |
| 8279 | env->prog = prog = new_prog; |
| 8280 | insn = new_prog->insnsi + i + delta; |
| 8281 | continue; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8282 | } |
| 8283 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8284 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 8285 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 8286 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 8287 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 8288 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 8289 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 8290 | u64 flags))NULL)); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 8291 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 8292 | (int (*)(struct bpf_map *map, void *value, |
| 8293 | u64 flags))NULL)); |
| 8294 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 8295 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 8296 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 8297 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 8298 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8299 | switch (insn->imm) { |
| 8300 | case BPF_FUNC_map_lookup_elem: |
| 8301 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - |
| 8302 | __bpf_call_base; |
| 8303 | continue; |
| 8304 | case BPF_FUNC_map_update_elem: |
| 8305 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - |
| 8306 | __bpf_call_base; |
| 8307 | continue; |
| 8308 | case BPF_FUNC_map_delete_elem: |
| 8309 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - |
| 8310 | __bpf_call_base; |
| 8311 | continue; |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 8312 | case BPF_FUNC_map_push_elem: |
| 8313 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - |
| 8314 | __bpf_call_base; |
| 8315 | continue; |
| 8316 | case BPF_FUNC_map_pop_elem: |
| 8317 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - |
| 8318 | __bpf_call_base; |
| 8319 | continue; |
| 8320 | case BPF_FUNC_map_peek_elem: |
| 8321 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - |
| 8322 | __bpf_call_base; |
| 8323 | continue; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8324 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8325 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 8326 | goto patch_call_imm; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 8327 | } |
| 8328 | |
| 8329 | patch_call_imm: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 8330 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8331 | /* all functions that have prototype and verifier allowed |
| 8332 | * programs to call them, must be real in-kernel functions |
| 8333 | */ |
| 8334 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8335 | verbose(env, |
| 8336 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8337 | func_id_name(insn->imm), insn->imm); |
| 8338 | return -EFAULT; |
| 8339 | } |
| 8340 | insn->imm = fn->func - __bpf_call_base; |
| 8341 | } |
| 8342 | |
| 8343 | return 0; |
| 8344 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8345 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8346 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8347 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8348 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8349 | int i; |
| 8350 | |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 8351 | sl = env->free_list; |
| 8352 | while (sl) { |
| 8353 | sln = sl->next; |
| 8354 | free_verifier_state(&sl->state, false); |
| 8355 | kfree(sl); |
| 8356 | sl = sln; |
| 8357 | } |
| 8358 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8359 | if (!env->explored_states) |
| 8360 | return; |
| 8361 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 8362 | for (i = 0; i < state_htab_size(env); i++) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8363 | sl = env->explored_states[i]; |
| 8364 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 8365 | while (sl) { |
| 8366 | sln = sl->next; |
| 8367 | free_verifier_state(&sl->state, false); |
| 8368 | kfree(sl); |
| 8369 | sl = sln; |
| 8370 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8371 | } |
| 8372 | |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 8373 | kvfree(env->explored_states); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8374 | } |
| 8375 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 8376 | static void print_verification_stats(struct bpf_verifier_env *env) |
| 8377 | { |
| 8378 | int i; |
| 8379 | |
| 8380 | if (env->log.level & BPF_LOG_STATS) { |
| 8381 | verbose(env, "verification time %lld usec\n", |
| 8382 | div_u64(env->verification_time, 1000)); |
| 8383 | verbose(env, "stack depth "); |
| 8384 | for (i = 0; i < env->subprog_cnt; i++) { |
| 8385 | u32 depth = env->subprog_info[i].stack_depth; |
| 8386 | |
| 8387 | verbose(env, "%d", depth); |
| 8388 | if (i + 1 < env->subprog_cnt) |
| 8389 | verbose(env, "+"); |
| 8390 | } |
| 8391 | verbose(env, "\n"); |
| 8392 | } |
| 8393 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " |
| 8394 | "total_states %d peak_states %d mark_read %d\n", |
| 8395 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, |
| 8396 | env->max_states_per_insn, env->total_states, |
| 8397 | env->peak_states, env->longest_mark_read_walk); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8398 | } |
| 8399 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8400 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
| 8401 | union bpf_attr __user *uattr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 8402 | { |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 8403 | u64 start_time = ktime_get_ns(); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8404 | struct bpf_verifier_env *env; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 8405 | struct bpf_verifier_log *log; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 8406 | int i, len, ret = -EINVAL; |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8407 | bool is_priv; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 8408 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 8409 | /* no program is valid */ |
| 8410 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 8411 | return -EINVAL; |
| 8412 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8413 | /* '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] | 8414 | * allocate/free it every time bpf_check() is called |
| 8415 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8416 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8417 | if (!env) |
| 8418 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8419 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8420 | |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 8421 | len = (*prog)->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 8422 | env->insn_aux_data = |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 8423 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8424 | ret = -ENOMEM; |
| 8425 | if (!env->insn_aux_data) |
| 8426 | goto err_free_env; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 8427 | for (i = 0; i < len; i++) |
| 8428 | env->insn_aux_data[i].orig_idx = i; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8429 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 8430 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 8431 | is_priv = capable(CAP_SYS_ADMIN); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8432 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8433 | /* grab the mutex to protect few globals used by verifier */ |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 8434 | if (!is_priv) |
| 8435 | mutex_lock(&bpf_verifier_lock); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8436 | |
| 8437 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 8438 | /* user requested verbose verifier output |
| 8439 | * and supplied buffer to store the verification trace |
| 8440 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 8441 | log->level = attr->log_level; |
| 8442 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 8443 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8444 | |
| 8445 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 8446 | /* log attributes have to be sane */ |
Alexei Starovoitov | 7a9f5c6 | 2019-04-01 21:27:46 -0700 | [diff] [blame] | 8447 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 || |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 8448 | !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8449 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8450 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 8451 | |
| 8452 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 8453 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 8454 | env->strict_alignment = true; |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 8455 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
| 8456 | env->strict_alignment = false; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8457 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8458 | env->allow_ptr_leaks = is_priv; |
| 8459 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8460 | ret = replace_map_fd_with_map_ptr(env); |
| 8461 | if (ret < 0) |
| 8462 | goto skip_full_check; |
| 8463 | |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 8464 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Quentin Monnet | a40a263 | 2018-11-09 13:03:31 +0000 | [diff] [blame] | 8465 | ret = bpf_prog_offload_verifier_prep(env->prog); |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 8466 | if (ret) |
| 8467 | goto skip_full_check; |
| 8468 | } |
| 8469 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 8470 | env->explored_states = kvcalloc(state_htab_size(env), |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8471 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8472 | GFP_USER); |
| 8473 | ret = -ENOMEM; |
| 8474 | if (!env->explored_states) |
| 8475 | goto skip_full_check; |
| 8476 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 8477 | ret = check_subprogs(env); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 8478 | if (ret < 0) |
| 8479 | goto skip_full_check; |
| 8480 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 8481 | ret = check_btf_info(env, attr, uattr); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 8482 | if (ret < 0) |
| 8483 | goto skip_full_check; |
| 8484 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 8485 | ret = check_cfg(env); |
| 8486 | if (ret < 0) |
| 8487 | goto skip_full_check; |
| 8488 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8489 | ret = do_check(env); |
Craig Gallek | 8c01c4f | 2017-11-02 11:18:01 -0400 | [diff] [blame] | 8490 | if (env->cur_state) { |
| 8491 | free_verifier_state(env->cur_state, true); |
| 8492 | env->cur_state = NULL; |
| 8493 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8494 | |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 8495 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 8496 | ret = bpf_prog_offload_finalize(env); |
| 8497 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8498 | skip_full_check: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8499 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 8500 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8501 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8502 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 8503 | ret = check_max_stack_depth(env); |
| 8504 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 8505 | /* instruction rewrites happen after this point */ |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8506 | if (is_priv) { |
| 8507 | if (ret == 0) |
| 8508 | opt_hard_wire_dead_code_branches(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 8509 | if (ret == 0) |
| 8510 | ret = opt_remove_dead_code(env); |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 8511 | if (ret == 0) |
| 8512 | ret = opt_remove_nops(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 8513 | } else { |
| 8514 | if (ret == 0) |
| 8515 | sanitize_dead_code(env); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 8516 | } |
| 8517 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 8518 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8519 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 8520 | ret = convert_ctx_accesses(env); |
| 8521 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8522 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 8523 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 8524 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame^] | 8525 | /* do 32-bit optimization after insn patching has done so those patched |
| 8526 | * insns could be handled correctly. |
| 8527 | */ |
| 8528 | if (ret == 0 && bpf_jit_needs_zext() && |
| 8529 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
| 8530 | ret = opt_subreg_zext_lo32(env); |
| 8531 | env->prog->aux->verifier_zext = !ret; |
| 8532 | } |
| 8533 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 8534 | if (ret == 0) |
| 8535 | ret = fixup_call_args(env); |
| 8536 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 8537 | env->verification_time = ktime_get_ns() - start_time; |
| 8538 | print_verification_stats(env); |
| 8539 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8540 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8541 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8542 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8543 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8544 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8545 | } |
| 8546 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8547 | if (ret == 0 && env->used_map_cnt) { |
| 8548 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8549 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 8550 | sizeof(env->used_maps[0]), |
| 8551 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8552 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8553 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8554 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8555 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8556 | } |
| 8557 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8558 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8559 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8560 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8561 | |
| 8562 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 8563 | * bpf_ld_imm64 instructions |
| 8564 | */ |
| 8565 | convert_pseudo_ld_imm64(env); |
| 8566 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 8567 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 8568 | if (ret == 0) |
| 8569 | adjust_btf_func(env); |
| 8570 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 8571 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8572 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8573 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 8574 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 8575 | */ |
| 8576 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 8577 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8578 | err_unlock: |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 8579 | if (!is_priv) |
| 8580 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 8581 | vfree(env->insn_aux_data); |
| 8582 | err_free_env: |
| 8583 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 8584 | return ret; |
| 8585 | } |