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 | |
Edward Cree | 8e17c1b | 2017-08-07 15:30:30 +0100 | [diff] [blame] | 179 | #define BPF_COMPLEXITY_LIMIT_INSNS 131072 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 180 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 181 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 182 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 183 | #define BPF_MAP_PTR_UNPRIV 1UL |
| 184 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ |
| 185 | POISON_POINTER_DELTA)) |
| 186 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) |
| 187 | |
| 188 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
| 189 | { |
| 190 | return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON; |
| 191 | } |
| 192 | |
| 193 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) |
| 194 | { |
| 195 | return aux->map_state & BPF_MAP_PTR_UNPRIV; |
| 196 | } |
| 197 | |
| 198 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, |
| 199 | const struct bpf_map *map, bool unpriv) |
| 200 | { |
| 201 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); |
| 202 | unpriv |= bpf_map_ptr_unpriv(aux); |
| 203 | aux->map_state = (unsigned long)map | |
| 204 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); |
| 205 | } |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 206 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 207 | struct bpf_call_arg_meta { |
| 208 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 209 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 210 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 211 | int regno; |
| 212 | int access_size; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 213 | s64 msize_smax_value; |
| 214 | u64 msize_umax_value; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 215 | int ptr_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 | |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 333 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
| 334 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 335 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
| 336 | type == PTR_TO_SOCKET_OR_NULL; |
| 337 | } |
| 338 | |
| 339 | static bool type_is_refcounted(enum bpf_reg_type type) |
| 340 | { |
| 341 | return type == PTR_TO_SOCKET; |
| 342 | } |
| 343 | |
| 344 | static bool type_is_refcounted_or_null(enum bpf_reg_type type) |
| 345 | { |
| 346 | return type == PTR_TO_SOCKET || type == PTR_TO_SOCKET_OR_NULL; |
| 347 | } |
| 348 | |
| 349 | static bool reg_is_refcounted(const struct bpf_reg_state *reg) |
| 350 | { |
| 351 | return type_is_refcounted(reg->type); |
| 352 | } |
| 353 | |
| 354 | static bool reg_is_refcounted_or_null(const struct bpf_reg_state *reg) |
| 355 | { |
| 356 | return type_is_refcounted_or_null(reg->type); |
| 357 | } |
| 358 | |
| 359 | static bool arg_type_is_refcounted(enum bpf_arg_type type) |
| 360 | { |
| 361 | return type == ARG_PTR_TO_SOCKET; |
| 362 | } |
| 363 | |
| 364 | /* Determine whether the function releases some resources allocated by another |
| 365 | * function call. The first reference type argument will be assumed to be |
| 366 | * released by release_reference(). |
| 367 | */ |
| 368 | static bool is_release_function(enum bpf_func_id func_id) |
| 369 | { |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 370 | return func_id == BPF_FUNC_sk_release; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 373 | /* string representation of 'enum bpf_reg_type' */ |
| 374 | static const char * const reg_type_str[] = { |
| 375 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 376 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 377 | [PTR_TO_CTX] = "ctx", |
| 378 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 379 | [PTR_TO_MAP_VALUE] = "map_value", |
| 380 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 381 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 382 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 383 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 384 | [PTR_TO_PACKET_END] = "pkt_end", |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 385 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 386 | [PTR_TO_SOCKET] = "sock", |
| 387 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 388 | }; |
| 389 | |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 390 | static char slot_type_char[] = { |
| 391 | [STACK_INVALID] = '?', |
| 392 | [STACK_SPILL] = 'r', |
| 393 | [STACK_MISC] = 'm', |
| 394 | [STACK_ZERO] = '0', |
| 395 | }; |
| 396 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 397 | static void print_liveness(struct bpf_verifier_env *env, |
| 398 | enum bpf_reg_liveness live) |
| 399 | { |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 400 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 401 | verbose(env, "_"); |
| 402 | if (live & REG_LIVE_READ) |
| 403 | verbose(env, "r"); |
| 404 | if (live & REG_LIVE_WRITTEN) |
| 405 | verbose(env, "w"); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 406 | if (live & REG_LIVE_DONE) |
| 407 | verbose(env, "D"); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 410 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 411 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 412 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 413 | struct bpf_verifier_state *cur = env->cur_state; |
| 414 | |
| 415 | return cur->frame[reg->frameno]; |
| 416 | } |
| 417 | |
| 418 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 419 | const struct bpf_func_state *state) |
| 420 | { |
| 421 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 422 | enum bpf_reg_type t; |
| 423 | int i; |
| 424 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 425 | if (state->frameno) |
| 426 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 427 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 428 | reg = &state->regs[i]; |
| 429 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 430 | if (t == NOT_INIT) |
| 431 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 432 | verbose(env, " R%d", i); |
| 433 | print_liveness(env, reg->live); |
| 434 | verbose(env, "=%s", reg_type_str[t]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 435 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 436 | tnum_is_const(reg->var_off)) { |
| 437 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 438 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 439 | if (t == PTR_TO_STACK) |
| 440 | verbose(env, ",call_%d", func(env, reg)->callsite); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 441 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 442 | verbose(env, "(id=%d", reg->id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 443 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 444 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 445 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 446 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 447 | else if (t == CONST_PTR_TO_MAP || |
| 448 | t == PTR_TO_MAP_VALUE || |
| 449 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 450 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 451 | reg->map_ptr->key_size, |
| 452 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 453 | if (tnum_is_const(reg->var_off)) { |
| 454 | /* Typically an immediate SCALAR_VALUE, but |
| 455 | * could be a pointer whose offset is too big |
| 456 | * for reg->off |
| 457 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 458 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 459 | } else { |
| 460 | if (reg->smin_value != reg->umin_value && |
| 461 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 462 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 463 | (long long)reg->smin_value); |
| 464 | if (reg->smax_value != reg->umax_value && |
| 465 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 466 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 467 | (long long)reg->smax_value); |
| 468 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 469 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 470 | (unsigned long long)reg->umin_value); |
| 471 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 472 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 473 | (unsigned long long)reg->umax_value); |
| 474 | if (!tnum_is_unknown(reg->var_off)) { |
| 475 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 476 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 477 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 478 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 479 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 480 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 481 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 482 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 483 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 484 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 485 | char types_buf[BPF_REG_SIZE + 1]; |
| 486 | bool valid = false; |
| 487 | int j; |
| 488 | |
| 489 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 490 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 491 | valid = true; |
| 492 | types_buf[j] = slot_type_char[ |
| 493 | state->stack[i].slot_type[j]]; |
| 494 | } |
| 495 | types_buf[BPF_REG_SIZE] = 0; |
| 496 | if (!valid) |
| 497 | continue; |
| 498 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 499 | print_liveness(env, state->stack[i].spilled_ptr.live); |
| 500 | if (state->stack[i].slot_type[0] == STACK_SPILL) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 501 | verbose(env, "=%s", |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 502 | reg_type_str[state->stack[i].spilled_ptr.type]); |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 503 | else |
| 504 | verbose(env, "=%s", types_buf); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 505 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 506 | if (state->acquired_refs && state->refs[0].id) { |
| 507 | verbose(env, " refs=%d", state->refs[0].id); |
| 508 | for (i = 1; i < state->acquired_refs; i++) |
| 509 | if (state->refs[i].id) |
| 510 | verbose(env, ",%d", state->refs[i].id); |
| 511 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 512 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 515 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 516 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ |
| 517 | const struct bpf_func_state *src) \ |
| 518 | { \ |
| 519 | if (!src->FIELD) \ |
| 520 | return 0; \ |
| 521 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ |
| 522 | /* internal bug, make state invalid to reject the program */ \ |
| 523 | memset(dst, 0, sizeof(*dst)); \ |
| 524 | return -EFAULT; \ |
| 525 | } \ |
| 526 | memcpy(dst->FIELD, src->FIELD, \ |
| 527 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ |
| 528 | return 0; \ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 529 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 530 | /* copy_reference_state() */ |
| 531 | COPY_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 532 | /* copy_stack_state() */ |
| 533 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 534 | #undef COPY_STATE_FN |
| 535 | |
| 536 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 537 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ |
| 538 | bool copy_old) \ |
| 539 | { \ |
| 540 | u32 old_size = state->COUNT; \ |
| 541 | struct bpf_##NAME##_state *new_##FIELD; \ |
| 542 | int slot = size / SIZE; \ |
| 543 | \ |
| 544 | if (size <= old_size || !size) { \ |
| 545 | if (copy_old) \ |
| 546 | return 0; \ |
| 547 | state->COUNT = slot * SIZE; \ |
| 548 | if (!size && old_size) { \ |
| 549 | kfree(state->FIELD); \ |
| 550 | state->FIELD = NULL; \ |
| 551 | } \ |
| 552 | return 0; \ |
| 553 | } \ |
| 554 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ |
| 555 | GFP_KERNEL); \ |
| 556 | if (!new_##FIELD) \ |
| 557 | return -ENOMEM; \ |
| 558 | if (copy_old) { \ |
| 559 | if (state->FIELD) \ |
| 560 | memcpy(new_##FIELD, state->FIELD, \ |
| 561 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ |
| 562 | memset(new_##FIELD + old_size / SIZE, 0, \ |
| 563 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ |
| 564 | } \ |
| 565 | state->COUNT = slot * SIZE; \ |
| 566 | kfree(state->FIELD); \ |
| 567 | state->FIELD = new_##FIELD; \ |
| 568 | return 0; \ |
| 569 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 570 | /* realloc_reference_state() */ |
| 571 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 572 | /* realloc_stack_state() */ |
| 573 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 574 | #undef REALLOC_STATE_FN |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 575 | |
| 576 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 577 | * make it consume minimal amount of memory. check_stack_write() access from |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 578 | * the program calls into realloc_func_state() to grow the stack size. |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 579 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 580 | * which realloc_stack_state() copies over. It points to previous |
| 581 | * bpf_verifier_state which is never reallocated. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 582 | */ |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 583 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
| 584 | int refs_size, bool copy_old) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 585 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 586 | int err = realloc_reference_state(state, refs_size, copy_old); |
| 587 | if (err) |
| 588 | return err; |
| 589 | return realloc_stack_state(state, stack_size, copy_old); |
| 590 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 591 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 592 | /* Acquire a pointer id from the env and update the state->refs to include |
| 593 | * this new pointer reference. |
| 594 | * On success, returns a valid pointer id to associate with the register |
| 595 | * On failure, returns a negative errno. |
| 596 | */ |
| 597 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 598 | { |
| 599 | struct bpf_func_state *state = cur_func(env); |
| 600 | int new_ofs = state->acquired_refs; |
| 601 | int id, err; |
| 602 | |
| 603 | err = realloc_reference_state(state, state->acquired_refs + 1, true); |
| 604 | if (err) |
| 605 | return err; |
| 606 | id = ++env->id_gen; |
| 607 | state->refs[new_ofs].id = id; |
| 608 | state->refs[new_ofs].insn_idx = insn_idx; |
| 609 | |
| 610 | return id; |
| 611 | } |
| 612 | |
| 613 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
| 614 | static int __release_reference_state(struct bpf_func_state *state, int ptr_id) |
| 615 | { |
| 616 | int i, last_idx; |
| 617 | |
| 618 | if (!ptr_id) |
| 619 | return -EFAULT; |
| 620 | |
| 621 | last_idx = state->acquired_refs - 1; |
| 622 | for (i = 0; i < state->acquired_refs; i++) { |
| 623 | if (state->refs[i].id == ptr_id) { |
| 624 | if (last_idx && i != last_idx) |
| 625 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 626 | sizeof(*state->refs)); |
| 627 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 628 | state->acquired_refs--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 629 | return 0; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 630 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 631 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 632 | return -EFAULT; |
| 633 | } |
| 634 | |
| 635 | /* variation on the above for cases where we expect that there must be an |
| 636 | * outstanding reference for the specified ptr_id. |
| 637 | */ |
| 638 | static int release_reference_state(struct bpf_verifier_env *env, int ptr_id) |
| 639 | { |
| 640 | struct bpf_func_state *state = cur_func(env); |
| 641 | int err; |
| 642 | |
| 643 | err = __release_reference_state(state, ptr_id); |
| 644 | if (WARN_ON_ONCE(err != 0)) |
| 645 | verbose(env, "verifier internal error: can't release reference\n"); |
| 646 | return err; |
| 647 | } |
| 648 | |
| 649 | static int transfer_reference_state(struct bpf_func_state *dst, |
| 650 | struct bpf_func_state *src) |
| 651 | { |
| 652 | int err = realloc_reference_state(dst, src->acquired_refs, false); |
| 653 | if (err) |
| 654 | return err; |
| 655 | err = copy_reference_state(dst, src); |
| 656 | if (err) |
| 657 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 661 | static void free_func_state(struct bpf_func_state *state) |
| 662 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 663 | if (!state) |
| 664 | return; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 665 | kfree(state->refs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 666 | kfree(state->stack); |
| 667 | kfree(state); |
| 668 | } |
| 669 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 670 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 671 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 672 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 673 | int i; |
| 674 | |
| 675 | for (i = 0; i <= state->curframe; i++) { |
| 676 | free_func_state(state->frame[i]); |
| 677 | state->frame[i] = NULL; |
| 678 | } |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 679 | if (free_self) |
| 680 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* copy verifier state from src to dst growing dst stack space |
| 684 | * when necessary to accommodate larger src stack |
| 685 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 686 | static int copy_func_state(struct bpf_func_state *dst, |
| 687 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 688 | { |
| 689 | int err; |
| 690 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 691 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
| 692 | false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 693 | if (err) |
| 694 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 695 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 696 | err = copy_reference_state(dst, src); |
| 697 | if (err) |
| 698 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 699 | return copy_stack_state(dst, src); |
| 700 | } |
| 701 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 702 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 703 | const struct bpf_verifier_state *src) |
| 704 | { |
| 705 | struct bpf_func_state *dst; |
| 706 | int i, err; |
| 707 | |
| 708 | /* if dst has more stack frames then src frame, free them */ |
| 709 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 710 | free_func_state(dst_state->frame[i]); |
| 711 | dst_state->frame[i] = NULL; |
| 712 | } |
| 713 | dst_state->curframe = src->curframe; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 714 | for (i = 0; i <= src->curframe; i++) { |
| 715 | dst = dst_state->frame[i]; |
| 716 | if (!dst) { |
| 717 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 718 | if (!dst) |
| 719 | return -ENOMEM; |
| 720 | dst_state->frame[i] = dst; |
| 721 | } |
| 722 | err = copy_func_state(dst, src->frame[i]); |
| 723 | if (err) |
| 724 | return err; |
| 725 | } |
| 726 | return 0; |
| 727 | } |
| 728 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 729 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
| 730 | int *insn_idx) |
| 731 | { |
| 732 | struct bpf_verifier_state *cur = env->cur_state; |
| 733 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 734 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 735 | |
| 736 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 737 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 738 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 739 | if (cur) { |
| 740 | err = copy_verifier_state(cur, &head->st); |
| 741 | if (err) |
| 742 | return err; |
| 743 | } |
| 744 | if (insn_idx) |
| 745 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 746 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 747 | *prev_insn_idx = head->prev_insn_idx; |
| 748 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 749 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 750 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 751 | env->head = elem; |
| 752 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 753 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 756 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
| 757 | int insn_idx, int prev_insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 758 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 759 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 760 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 761 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 762 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 763 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 764 | if (!elem) |
| 765 | goto err; |
| 766 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 767 | elem->insn_idx = insn_idx; |
| 768 | elem->prev_insn_idx = prev_insn_idx; |
| 769 | elem->next = env->head; |
| 770 | env->head = elem; |
| 771 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 772 | err = copy_verifier_state(&elem->st, cur); |
| 773 | if (err) |
| 774 | goto err; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 775 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 776 | verbose(env, "BPF program is too complex\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 777 | goto err; |
| 778 | } |
| 779 | return &elem->st; |
| 780 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 781 | free_verifier_state(env->cur_state, true); |
| 782 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 783 | /* pop all elements and return */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 784 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 785 | return NULL; |
| 786 | } |
| 787 | |
| 788 | #define CALLER_SAVED_REGS 6 |
| 789 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 790 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 791 | }; |
| 792 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 793 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
| 794 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 795 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 796 | * known to have the value @imm. |
| 797 | */ |
| 798 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 799 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 800 | /* Clear id, off, and union(map_ptr, range) */ |
| 801 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 802 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 803 | reg->var_off = tnum_const(imm); |
| 804 | reg->smin_value = (s64)imm; |
| 805 | reg->smax_value = (s64)imm; |
| 806 | reg->umin_value = imm; |
| 807 | reg->umax_value = imm; |
| 808 | } |
| 809 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 810 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 811 | * used only on registers holding a pointer type. |
| 812 | */ |
| 813 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 814 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 815 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 816 | } |
| 817 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 818 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 819 | { |
| 820 | __mark_reg_known(reg, 0); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 821 | reg->type = SCALAR_VALUE; |
| 822 | } |
| 823 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 824 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 825 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 826 | { |
| 827 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 828 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 829 | /* Something bad happened, let's kill all regs */ |
| 830 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 831 | __mark_reg_not_init(regs + regno); |
| 832 | return; |
| 833 | } |
| 834 | __mark_reg_known_zero(regs + regno); |
| 835 | } |
| 836 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 837 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 838 | { |
| 839 | return type_is_pkt_pointer(reg->type); |
| 840 | } |
| 841 | |
| 842 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 843 | { |
| 844 | return reg_is_pkt_pointer(reg) || |
| 845 | reg->type == PTR_TO_PACKET_END; |
| 846 | } |
| 847 | |
| 848 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 849 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 850 | enum bpf_reg_type which) |
| 851 | { |
| 852 | /* The register can already have a range from prior markings. |
| 853 | * This is fine as long as it hasn't been advanced from its |
| 854 | * origin. |
| 855 | */ |
| 856 | return reg->type == which && |
| 857 | reg->id == 0 && |
| 858 | reg->off == 0 && |
| 859 | tnum_equals_const(reg->var_off, 0); |
| 860 | } |
| 861 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 862 | /* Attempts to improve min/max values based on var_off information */ |
| 863 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 864 | { |
| 865 | /* min signed is max(sign bit) | min(other bits) */ |
| 866 | reg->smin_value = max_t(s64, reg->smin_value, |
| 867 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 868 | /* max signed is min(sign bit) | max(other bits) */ |
| 869 | reg->smax_value = min_t(s64, reg->smax_value, |
| 870 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 871 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 872 | reg->umax_value = min(reg->umax_value, |
| 873 | reg->var_off.value | reg->var_off.mask); |
| 874 | } |
| 875 | |
| 876 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
| 877 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 878 | { |
| 879 | /* Learn sign from signed bounds. |
| 880 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 881 | * are the same, so combine. This works even in the negative case, e.g. |
| 882 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 883 | */ |
| 884 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 885 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 886 | reg->umin_value); |
| 887 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 888 | reg->umax_value); |
| 889 | return; |
| 890 | } |
| 891 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 892 | * boundary, so we must be careful. |
| 893 | */ |
| 894 | if ((s64)reg->umax_value >= 0) { |
| 895 | /* Positive. We can't learn anything from the smin, but smax |
| 896 | * is positive, hence safe. |
| 897 | */ |
| 898 | reg->smin_value = reg->umin_value; |
| 899 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 900 | reg->umax_value); |
| 901 | } else if ((s64)reg->umin_value < 0) { |
| 902 | /* Negative. We can't learn anything from the smax, but smin |
| 903 | * is negative, hence safe. |
| 904 | */ |
| 905 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 906 | reg->umin_value); |
| 907 | reg->smax_value = reg->umax_value; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 912 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 913 | { |
| 914 | reg->var_off = tnum_intersect(reg->var_off, |
| 915 | tnum_range(reg->umin_value, |
| 916 | reg->umax_value)); |
| 917 | } |
| 918 | |
| 919 | /* Reset the min/max bounds of a register */ |
| 920 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 921 | { |
| 922 | reg->smin_value = S64_MIN; |
| 923 | reg->smax_value = S64_MAX; |
| 924 | reg->umin_value = 0; |
| 925 | reg->umax_value = U64_MAX; |
| 926 | } |
| 927 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 928 | /* Mark a register as having a completely unknown (scalar) value. */ |
| 929 | static void __mark_reg_unknown(struct bpf_reg_state *reg) |
| 930 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 931 | /* |
| 932 | * Clear type, id, off, and union(map_ptr, range) and |
| 933 | * padding between 'type' and union |
| 934 | */ |
| 935 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 936 | reg->type = SCALAR_VALUE; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 937 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 938 | reg->frameno = 0; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 939 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 940 | } |
| 941 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 942 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 943 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 944 | { |
| 945 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 946 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 947 | /* Something bad happened, let's kill all regs except FP */ |
| 948 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 949 | __mark_reg_not_init(regs + regno); |
| 950 | return; |
| 951 | } |
| 952 | __mark_reg_unknown(regs + regno); |
| 953 | } |
| 954 | |
| 955 | static void __mark_reg_not_init(struct bpf_reg_state *reg) |
| 956 | { |
| 957 | __mark_reg_unknown(reg); |
| 958 | reg->type = NOT_INIT; |
| 959 | } |
| 960 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 961 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 962 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 963 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 964 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 965 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 966 | /* Something bad happened, let's kill all regs except FP */ |
| 967 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 968 | __mark_reg_not_init(regs + regno); |
| 969 | return; |
| 970 | } |
| 971 | __mark_reg_not_init(regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 972 | } |
| 973 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 974 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 975 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 976 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 977 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 978 | int i; |
| 979 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 980 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 981 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 982 | regs[i].live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 983 | regs[i].parent = NULL; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 984 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 985 | |
| 986 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 987 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 988 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 989 | regs[BPF_REG_FP].frameno = state->frameno; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 990 | |
| 991 | /* 1st arg to a function */ |
| 992 | regs[BPF_REG_1].type = PTR_TO_CTX; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 993 | mark_reg_known_zero(env, regs, BPF_REG_1); |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 994 | } |
| 995 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 996 | #define BPF_MAIN_FUNC (-1) |
| 997 | static void init_func_state(struct bpf_verifier_env *env, |
| 998 | struct bpf_func_state *state, |
| 999 | int callsite, int frameno, int subprogno) |
| 1000 | { |
| 1001 | state->callsite = callsite; |
| 1002 | state->frameno = frameno; |
| 1003 | state->subprogno = subprogno; |
| 1004 | init_reg_state(env, state); |
| 1005 | } |
| 1006 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1007 | enum reg_arg_type { |
| 1008 | SRC_OP, /* register is used as source operand */ |
| 1009 | DST_OP, /* register is used as destination operand */ |
| 1010 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 1011 | }; |
| 1012 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1013 | static int cmp_subprogs(const void *a, const void *b) |
| 1014 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1015 | return ((struct bpf_subprog_info *)a)->start - |
| 1016 | ((struct bpf_subprog_info *)b)->start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 1020 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1021 | struct bpf_subprog_info *p; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1022 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1023 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 1024 | sizeof(env->subprog_info[0]), cmp_subprogs); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1025 | if (!p) |
| 1026 | return -ENOENT; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1027 | return p - env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1028 | |
| 1029 | } |
| 1030 | |
| 1031 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 1032 | { |
| 1033 | int insn_cnt = env->prog->len; |
| 1034 | int ret; |
| 1035 | |
| 1036 | if (off >= insn_cnt || off < 0) { |
| 1037 | verbose(env, "call to invalid destination\n"); |
| 1038 | return -EINVAL; |
| 1039 | } |
| 1040 | ret = find_subprog(env, off); |
| 1041 | if (ret >= 0) |
| 1042 | return 0; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1043 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1044 | verbose(env, "too many subprograms\n"); |
| 1045 | return -E2BIG; |
| 1046 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1047 | env->subprog_info[env->subprog_cnt++].start = off; |
| 1048 | sort(env->subprog_info, env->subprog_cnt, |
| 1049 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | static int check_subprogs(struct bpf_verifier_env *env) |
| 1054 | { |
| 1055 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1056 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1057 | struct bpf_insn *insn = env->prog->insnsi; |
| 1058 | int insn_cnt = env->prog->len; |
| 1059 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 1060 | /* Add entry function. */ |
| 1061 | ret = add_subprog(env, 0); |
| 1062 | if (ret < 0) |
| 1063 | return ret; |
| 1064 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1065 | /* determine subprog starts. The end is one before the next starts */ |
| 1066 | for (i = 0; i < insn_cnt; i++) { |
| 1067 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1068 | continue; |
| 1069 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1070 | continue; |
| 1071 | if (!env->allow_ptr_leaks) { |
| 1072 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); |
| 1073 | return -EPERM; |
| 1074 | } |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1075 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 1076 | if (ret < 0) |
| 1077 | return ret; |
| 1078 | } |
| 1079 | |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1080 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 1081 | * logic. 'subprog_cnt' should not be increased. |
| 1082 | */ |
| 1083 | subprog[env->subprog_cnt].start = insn_cnt; |
| 1084 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1085 | if (env->log.level > 1) |
| 1086 | for (i = 0; i < env->subprog_cnt; i++) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1087 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1088 | |
| 1089 | /* now check that all jumps are within the same subprog */ |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1090 | subprog_start = subprog[cur_subprog].start; |
| 1091 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1092 | for (i = 0; i < insn_cnt; i++) { |
| 1093 | u8 code = insn[i].code; |
| 1094 | |
| 1095 | if (BPF_CLASS(code) != BPF_JMP) |
| 1096 | goto next; |
| 1097 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 1098 | goto next; |
| 1099 | off = i + insn[i].off + 1; |
| 1100 | if (off < subprog_start || off >= subprog_end) { |
| 1101 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 1102 | return -EINVAL; |
| 1103 | } |
| 1104 | next: |
| 1105 | if (i == subprog_end - 1) { |
| 1106 | /* to avoid fall-through from one subprog into another |
| 1107 | * the last insn of the subprog should be either exit |
| 1108 | * or unconditional jump back |
| 1109 | */ |
| 1110 | if (code != (BPF_JMP | BPF_EXIT) && |
| 1111 | code != (BPF_JMP | BPF_JA)) { |
| 1112 | verbose(env, "last insn is not an exit or jmp\n"); |
| 1113 | return -EINVAL; |
| 1114 | } |
| 1115 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1116 | cur_subprog++; |
| 1117 | if (cur_subprog < env->subprog_cnt) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1118 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1124 | /* Parentage chain of this register (or stack slot) should take care of all |
| 1125 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 1126 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1127 | static int mark_reg_read(struct bpf_verifier_env *env, |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1128 | const struct bpf_reg_state *state, |
| 1129 | struct bpf_reg_state *parent) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1130 | { |
| 1131 | bool writes = parent == state->parent; /* Observe write marks */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1132 | |
| 1133 | while (parent) { |
| 1134 | /* if read wasn't screened by an earlier write ... */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1135 | if (writes && state->live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1136 | break; |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 1137 | if (parent->live & REG_LIVE_DONE) { |
| 1138 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", |
| 1139 | reg_type_str[parent->type], |
| 1140 | parent->var_off.value, parent->off); |
| 1141 | return -EFAULT; |
| 1142 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1143 | /* ... then we depend on parent's value */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1144 | parent->live |= REG_LIVE_READ; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1145 | state = parent; |
| 1146 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1147 | writes = true; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1148 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1149 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1153 | enum reg_arg_type t) |
| 1154 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1155 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1156 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 1157 | struct bpf_reg_state *regs = state->regs; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1158 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1159 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1160 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1161 | return -EINVAL; |
| 1162 | } |
| 1163 | |
| 1164 | if (t == SRC_OP) { |
| 1165 | /* check whether register used as source operand can be read */ |
| 1166 | if (regs[regno].type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1167 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1168 | return -EACCES; |
| 1169 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1170 | /* We don't need to worry about FP liveness because it's read-only */ |
| 1171 | if (regno != BPF_REG_FP) |
| 1172 | return mark_reg_read(env, ®s[regno], |
| 1173 | regs[regno].parent); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1174 | } else { |
| 1175 | /* check whether register used as dest operand can be written to */ |
| 1176 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1177 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1178 | return -EACCES; |
| 1179 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1180 | regs[regno].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1181 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1182 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1183 | } |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1187 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 1188 | { |
| 1189 | switch (type) { |
| 1190 | case PTR_TO_MAP_VALUE: |
| 1191 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 1192 | case PTR_TO_STACK: |
| 1193 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1194 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1195 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1196 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1197 | case PTR_TO_FLOW_KEYS: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1198 | case CONST_PTR_TO_MAP: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1199 | case PTR_TO_SOCKET: |
| 1200 | case PTR_TO_SOCKET_OR_NULL: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1201 | return true; |
| 1202 | default: |
| 1203 | return false; |
| 1204 | } |
| 1205 | } |
| 1206 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1207 | /* Does this register contain a constant zero? */ |
| 1208 | static bool register_is_null(struct bpf_reg_state *reg) |
| 1209 | { |
| 1210 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 1211 | } |
| 1212 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1213 | /* check_stack_read/write functions track spill/fill of registers, |
| 1214 | * stack boundary and alignment are checked in check_mem_access() |
| 1215 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1216 | static int check_stack_write(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1217 | struct bpf_func_state *state, /* func where register points to */ |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1218 | int off, int size, int value_regno, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1219 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1220 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1221 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1222 | enum bpf_reg_type type; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1223 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1224 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 1225 | state->acquired_refs, true); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1226 | if (err) |
| 1227 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1228 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 1229 | * so it's aligned access and [off, off + size) are within stack limits |
| 1230 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1231 | if (!env->allow_ptr_leaks && |
| 1232 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 1233 | size != BPF_REG_SIZE) { |
| 1234 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 1235 | return -EACCES; |
| 1236 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1237 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1238 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1239 | if (value_regno >= 0 && |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1240 | is_spillable_regtype((type = cur->regs[value_regno].type))) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1241 | |
| 1242 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1243 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1244 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1245 | return -EACCES; |
| 1246 | } |
| 1247 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1248 | if (state != cur && type == PTR_TO_STACK) { |
| 1249 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 1250 | return -EINVAL; |
| 1251 | } |
| 1252 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1253 | /* save register state */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1254 | state->stack[spi].spilled_ptr = cur->regs[value_regno]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1255 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1256 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1257 | for (i = 0; i < BPF_REG_SIZE; i++) { |
| 1258 | if (state->stack[spi].slot_type[i] == STACK_MISC && |
| 1259 | !env->allow_ptr_leaks) { |
| 1260 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
| 1261 | int soff = (-spi - 1) * BPF_REG_SIZE; |
| 1262 | |
| 1263 | /* detected reuse of integer stack slot with a pointer |
| 1264 | * which means either llvm is reusing stack slot or |
| 1265 | * an attacker is trying to exploit CVE-2018-3639 |
| 1266 | * (speculative store bypass) |
| 1267 | * Have to sanitize that slot with preemptive |
| 1268 | * store of zero. |
| 1269 | */ |
| 1270 | if (*poff && *poff != soff) { |
| 1271 | /* disallow programs where single insn stores |
| 1272 | * into two different stack slots, since verifier |
| 1273 | * cannot sanitize them |
| 1274 | */ |
| 1275 | verbose(env, |
| 1276 | "insn %d cannot access two stack slots fp%d and fp%d", |
| 1277 | insn_idx, *poff, soff); |
| 1278 | return -EINVAL; |
| 1279 | } |
| 1280 | *poff = soff; |
| 1281 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1282 | state->stack[spi].slot_type[i] = STACK_SPILL; |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1283 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1284 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1285 | u8 type = STACK_MISC; |
| 1286 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1287 | /* regular write of data into stack destroys any spilled ptr */ |
| 1288 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 1289 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
| 1290 | if (state->stack[spi].slot_type[0] == STACK_SPILL) |
| 1291 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 1292 | state->stack[spi].slot_type[i] = STACK_MISC; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1293 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1294 | /* only mark the slot as written if all 8 bytes were written |
| 1295 | * otherwise read propagation may incorrectly stop too soon |
| 1296 | * when stack slots are partially written. |
| 1297 | * This heuristic means that read propagation will be |
| 1298 | * conservative, since it will add reg_live_read marks |
| 1299 | * to stack slots all the way to first state when programs |
| 1300 | * writes+reads less than 8 bytes |
| 1301 | */ |
| 1302 | if (size == BPF_REG_SIZE) |
| 1303 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 1304 | |
| 1305 | /* when we zero initialize stack slots mark them as such */ |
| 1306 | if (value_regno >= 0 && |
| 1307 | register_is_null(&cur->regs[value_regno])) |
| 1308 | type = STACK_ZERO; |
| 1309 | |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 1310 | /* Mark slots affected by this stack write. */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1311 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1312 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1313 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1314 | } |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1318 | static int check_stack_read(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1319 | struct bpf_func_state *reg_state /* func where register points to */, |
| 1320 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1321 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1322 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1323 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1324 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
| 1325 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1326 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1327 | if (reg_state->allocated_stack <= slot) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1328 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 1329 | off, size); |
| 1330 | return -EACCES; |
| 1331 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1332 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1333 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1334 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1335 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1336 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1337 | return -EACCES; |
| 1338 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1339 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1340 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1341 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1342 | return -EACCES; |
| 1343 | } |
| 1344 | } |
| 1345 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1346 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1347 | /* restore register state from stack */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1348 | state->regs[value_regno] = reg_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 1349 | /* mark reg as written since spilled pointer state likely |
| 1350 | * has its liveness marks cleared by is_state_visited() |
| 1351 | * which resets stack/reg liveness for state transitions |
| 1352 | */ |
| 1353 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1354 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1355 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
| 1356 | reg_state->stack[spi].spilled_ptr.parent); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1357 | return 0; |
| 1358 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1359 | int zeros = 0; |
| 1360 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1361 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1362 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 1363 | continue; |
| 1364 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 1365 | zeros++; |
| 1366 | continue; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1367 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1368 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 1369 | off, i, size); |
| 1370 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1371 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1372 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
| 1373 | reg_state->stack[spi].spilled_ptr.parent); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1374 | if (value_regno >= 0) { |
| 1375 | if (zeros == size) { |
| 1376 | /* any size read into register is zero extended, |
| 1377 | * so the whole register == const_zero |
| 1378 | */ |
| 1379 | __mark_reg_const_zero(&state->regs[value_regno]); |
| 1380 | } else { |
| 1381 | /* have read misc data from the stack */ |
| 1382 | mark_reg_unknown(env, state->regs, value_regno); |
| 1383 | } |
| 1384 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 1385 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1386 | return 0; |
| 1387 | } |
| 1388 | } |
| 1389 | |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame^] | 1390 | static int check_stack_access(struct bpf_verifier_env *env, |
| 1391 | const struct bpf_reg_state *reg, |
| 1392 | int off, int size) |
| 1393 | { |
| 1394 | /* Stack accesses must be at a fixed offset, so that we |
| 1395 | * can determine what type of data were returned. See |
| 1396 | * check_stack_read(). |
| 1397 | */ |
| 1398 | if (!tnum_is_const(reg->var_off)) { |
| 1399 | char tn_buf[48]; |
| 1400 | |
| 1401 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 1402 | verbose(env, "variable stack access var_off=%s off=%d size=%d", |
| 1403 | tn_buf, off, size); |
| 1404 | return -EACCES; |
| 1405 | } |
| 1406 | |
| 1407 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 1408 | verbose(env, "invalid stack off=%d size=%d\n", off, size); |
| 1409 | return -EACCES; |
| 1410 | } |
| 1411 | |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1415 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1416 | 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] | 1417 | int size, bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1418 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1419 | struct bpf_reg_state *regs = cur_regs(env); |
| 1420 | struct bpf_map *map = regs[regno].map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1421 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1422 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1423 | off + size > map->value_size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1424 | 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] | 1425 | map->value_size, off, size); |
| 1426 | return -EACCES; |
| 1427 | } |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1431 | /* check read/write into a map element with possible variable offset */ |
| 1432 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1433 | int off, int size, bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1434 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1435 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1436 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1437 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 1438 | int err; |
| 1439 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1440 | /* We may have adjusted the register to this map value, so we |
| 1441 | * need to try adding each of min_value and max_value to off |
| 1442 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1443 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1444 | if (env->log.level) |
| 1445 | print_verifier_state(env, state); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1446 | /* The minimum value is only important with signed |
| 1447 | * comparisons where we can't assume the floor of a |
| 1448 | * value is 0. If we are using signed variables for our |
| 1449 | * index'es we need to make sure that whatever we use |
| 1450 | * will have a set floor within our range. |
| 1451 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1452 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1453 | 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] | 1454 | regno); |
| 1455 | return -EACCES; |
| 1456 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1457 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
| 1458 | zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1459 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1460 | verbose(env, "R%d min value is outside of the array range\n", |
| 1461 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1462 | return err; |
| 1463 | } |
| 1464 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1465 | /* If we haven't set a max value then we need to bail since we can't be |
| 1466 | * sure we won't do bad things. |
| 1467 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1468 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1469 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1470 | 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] | 1471 | regno); |
| 1472 | return -EACCES; |
| 1473 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1474 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
| 1475 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1476 | if (err) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1477 | verbose(env, "R%d max value is outside of the array range\n", |
| 1478 | regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1479 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1480 | } |
| 1481 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1482 | #define MAX_PACKET_OFF 0xffff |
| 1483 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1484 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1485 | const struct bpf_call_arg_meta *meta, |
| 1486 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1487 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1488 | switch (env->prog->type) { |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1489 | /* Program types only with direct read access go here! */ |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1490 | case BPF_PROG_TYPE_LWT_IN: |
| 1491 | case BPF_PROG_TYPE_LWT_OUT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 1492 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 1493 | case BPF_PROG_TYPE_SK_REUSEPORT: |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1494 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Daniel Borkmann | d5563d3 | 2018-10-24 22:05:46 +0200 | [diff] [blame] | 1495 | case BPF_PROG_TYPE_CGROUP_SKB: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1496 | if (t == BPF_WRITE) |
| 1497 | return false; |
Alexander Alemayhu | 7e57fbb | 2017-02-14 00:02:35 +0100 | [diff] [blame] | 1498 | /* fallthrough */ |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1499 | |
| 1500 | /* Program types with direct read + write access go here! */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1501 | case BPF_PROG_TYPE_SCHED_CLS: |
| 1502 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1503 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1504 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 1505 | case BPF_PROG_TYPE_SK_SKB: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1506 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1507 | if (meta) |
| 1508 | return meta->pkt_access; |
| 1509 | |
| 1510 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1511 | return true; |
| 1512 | default: |
| 1513 | return false; |
| 1514 | } |
| 1515 | } |
| 1516 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1517 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1518 | int off, int size, bool zero_size_allowed) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1519 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1520 | struct bpf_reg_state *regs = cur_regs(env); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1521 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1522 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1523 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1524 | (u64)off + size > reg->range) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1525 | 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] | 1526 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1527 | return -EACCES; |
| 1528 | } |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1532 | 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] | 1533 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1534 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1535 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1536 | struct bpf_reg_state *reg = ®s[regno]; |
| 1537 | int err; |
| 1538 | |
| 1539 | /* We may have added a variable offset to the packet pointer; but any |
| 1540 | * reg->range we have comes after that. We are only checking the fixed |
| 1541 | * offset. |
| 1542 | */ |
| 1543 | |
| 1544 | /* We don't allow negative numbers, because we aren't tracking enough |
| 1545 | * detail to prove they're safe. |
| 1546 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1547 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1548 | 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] | 1549 | regno); |
| 1550 | return -EACCES; |
| 1551 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1552 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1553 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1554 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1555 | return err; |
| 1556 | } |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 1557 | |
| 1558 | /* __check_packet_access has made sure "off + size - 1" is within u16. |
| 1559 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 1560 | * otherwise find_good_pkt_pointers would have refused to set range info |
| 1561 | * that __check_packet_access would have rejected this pkt access. |
| 1562 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 1563 | */ |
| 1564 | env->prog->aux->max_pkt_offset = |
| 1565 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 1566 | off + reg->umax_value + size - 1); |
| 1567 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1568 | return err; |
| 1569 | } |
| 1570 | |
| 1571 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1572 | 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] | 1573 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1574 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1575 | struct bpf_insn_access_aux info = { |
| 1576 | .reg_type = *reg_type, |
| 1577 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1578 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1579 | if (env->ops->is_valid_access && |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1580 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1581 | /* A non zero info.ctx_field_size indicates that this field is a |
| 1582 | * candidate for later verifier transformation to load the whole |
| 1583 | * field and then apply a mask when accessed with a narrower |
| 1584 | * access than actual ctx access size. A zero info.ctx_field_size |
| 1585 | * will only allow for whole field access and rejects any other |
| 1586 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1587 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 1588 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1589 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1590 | 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] | 1591 | /* remember the offset of last byte accessed in ctx */ |
| 1592 | if (env->prog->aux->max_ctx_offset < off + size) |
| 1593 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1594 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 1595 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1596 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1597 | 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] | 1598 | return -EACCES; |
| 1599 | } |
| 1600 | |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1601 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 1602 | int size) |
| 1603 | { |
| 1604 | if (size < 0 || off < 0 || |
| 1605 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 1606 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 1607 | off, size); |
| 1608 | return -EACCES; |
| 1609 | } |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1613 | static int check_sock_access(struct bpf_verifier_env *env, u32 regno, int off, |
| 1614 | int size, enum bpf_access_type t) |
| 1615 | { |
| 1616 | struct bpf_reg_state *regs = cur_regs(env); |
| 1617 | struct bpf_reg_state *reg = ®s[regno]; |
| 1618 | struct bpf_insn_access_aux info; |
| 1619 | |
| 1620 | if (reg->smin_value < 0) { |
| 1621 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 1622 | regno); |
| 1623 | return -EACCES; |
| 1624 | } |
| 1625 | |
| 1626 | if (!bpf_sock_is_valid_access(off, size, t, &info)) { |
| 1627 | verbose(env, "invalid bpf_sock access off=%d size=%d\n", |
| 1628 | off, size); |
| 1629 | return -EACCES; |
| 1630 | } |
| 1631 | |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1635 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 1636 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1637 | { |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1638 | if (allow_ptr_leaks) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1639 | return false; |
| 1640 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1641 | return reg->type != SCALAR_VALUE; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1644 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
| 1645 | { |
| 1646 | return cur_regs(env) + regno; |
| 1647 | } |
| 1648 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1649 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 1650 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1651 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1652 | } |
| 1653 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1654 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 1655 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1656 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1657 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 1658 | return reg->type == PTR_TO_CTX || |
| 1659 | reg->type == PTR_TO_SOCKET; |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1660 | } |
| 1661 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1662 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 1663 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1664 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1665 | |
| 1666 | return type_is_pkt_pointer(reg->type); |
| 1667 | } |
| 1668 | |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 1669 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 1670 | { |
| 1671 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 1672 | |
| 1673 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 1674 | return reg->type == PTR_TO_FLOW_KEYS; |
| 1675 | } |
| 1676 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1677 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 1678 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1679 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1680 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1681 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1682 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1683 | |
| 1684 | /* Byte size accesses are always allowed. */ |
| 1685 | if (!strict || size == 1) |
| 1686 | return 0; |
| 1687 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1688 | /* For platforms that do not have a Kconfig enabling |
| 1689 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 1690 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 1691 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 1692 | * to this code only in strict mode where we want to emulate |
| 1693 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 1694 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1695 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1696 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1697 | |
| 1698 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 1699 | if (!tnum_is_aligned(reg_off, size)) { |
| 1700 | char tn_buf[48]; |
| 1701 | |
| 1702 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1703 | verbose(env, |
| 1704 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1705 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1706 | return -EACCES; |
| 1707 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1708 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1709 | return 0; |
| 1710 | } |
| 1711 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1712 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 1713 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1714 | const char *pointer_desc, |
| 1715 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1716 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1717 | struct tnum reg_off; |
| 1718 | |
| 1719 | /* Byte size accesses are always allowed. */ |
| 1720 | if (!strict || size == 1) |
| 1721 | return 0; |
| 1722 | |
| 1723 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 1724 | if (!tnum_is_aligned(reg_off, size)) { |
| 1725 | char tn_buf[48]; |
| 1726 | |
| 1727 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1728 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1729 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1730 | return -EACCES; |
| 1731 | } |
| 1732 | |
| 1733 | return 0; |
| 1734 | } |
| 1735 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1736 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1737 | const struct bpf_reg_state *reg, int off, |
| 1738 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1739 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1740 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1741 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1742 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1743 | switch (reg->type) { |
| 1744 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1745 | case PTR_TO_PACKET_META: |
| 1746 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 1747 | * right in front, treat it the very same way. |
| 1748 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1749 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1750 | case PTR_TO_FLOW_KEYS: |
| 1751 | pointer_desc = "flow keys "; |
| 1752 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1753 | case PTR_TO_MAP_VALUE: |
| 1754 | pointer_desc = "value "; |
| 1755 | break; |
| 1756 | case PTR_TO_CTX: |
| 1757 | pointer_desc = "context "; |
| 1758 | break; |
| 1759 | case PTR_TO_STACK: |
| 1760 | pointer_desc = "stack "; |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 1761 | /* The stack spill tracking logic in check_stack_write() |
| 1762 | * and check_stack_read() relies on stack accesses being |
| 1763 | * aligned. |
| 1764 | */ |
| 1765 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1766 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1767 | case PTR_TO_SOCKET: |
| 1768 | pointer_desc = "sock "; |
| 1769 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1770 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1771 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1772 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1773 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 1774 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1775 | } |
| 1776 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1777 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 1778 | const struct bpf_func_state *func, |
| 1779 | int off) |
| 1780 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1781 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1782 | |
| 1783 | if (stack >= -off) |
| 1784 | return 0; |
| 1785 | |
| 1786 | /* update known max for given subprogram */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1787 | env->subprog_info[func->subprogno].stack_depth = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1788 | return 0; |
| 1789 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1790 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1791 | /* starting from main bpf function walk all instructions of the function |
| 1792 | * and recursively walk all callees that given function can call. |
| 1793 | * Ignore jump and exit insns. |
| 1794 | * Since recursion is prevented by check_cfg() this algorithm |
| 1795 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 1796 | */ |
| 1797 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 1798 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1799 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 1800 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1801 | struct bpf_insn *insn = env->prog->insnsi; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1802 | int ret_insn[MAX_CALL_FRAMES]; |
| 1803 | int ret_prog[MAX_CALL_FRAMES]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1804 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1805 | process_func: |
| 1806 | /* round up to 32-bytes, since this is granularity |
| 1807 | * of interpreter stack size |
| 1808 | */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1809 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1810 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1811 | 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] | 1812 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1813 | return -EACCES; |
| 1814 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1815 | continue_func: |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1816 | subprog_end = subprog[idx + 1].start; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1817 | for (; i < subprog_end; i++) { |
| 1818 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1819 | continue; |
| 1820 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1821 | continue; |
| 1822 | /* remember insn and function to return to */ |
| 1823 | ret_insn[frame] = i + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1824 | ret_prog[frame] = idx; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1825 | |
| 1826 | /* find the callee */ |
| 1827 | i = i + insn[i].imm + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1828 | idx = find_subprog(env, i); |
| 1829 | if (idx < 0) { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1830 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1831 | i); |
| 1832 | return -EFAULT; |
| 1833 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1834 | frame++; |
| 1835 | if (frame >= MAX_CALL_FRAMES) { |
| 1836 | WARN_ONCE(1, "verifier bug. Call stack is too deep\n"); |
| 1837 | return -EFAULT; |
| 1838 | } |
| 1839 | goto process_func; |
| 1840 | } |
| 1841 | /* end of for() loop means the last insn of the 'subprog' |
| 1842 | * was reached. Doesn't matter whether it was JA or EXIT |
| 1843 | */ |
| 1844 | if (frame == 0) |
| 1845 | return 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1846 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1847 | frame--; |
| 1848 | i = ret_insn[frame]; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1849 | idx = ret_prog[frame]; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1850 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1851 | } |
| 1852 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1853 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1854 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 1855 | const struct bpf_insn *insn, int idx) |
| 1856 | { |
| 1857 | int start = idx + insn->imm + 1, subprog; |
| 1858 | |
| 1859 | subprog = find_subprog(env, start); |
| 1860 | if (subprog < 0) { |
| 1861 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1862 | start); |
| 1863 | return -EFAULT; |
| 1864 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1865 | return env->subprog_info[subprog].stack_depth; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1866 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1867 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1868 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 1869 | static int check_ctx_reg(struct bpf_verifier_env *env, |
| 1870 | const struct bpf_reg_state *reg, int regno) |
| 1871 | { |
| 1872 | /* Access to ctx or passing it to a helper is only allowed in |
| 1873 | * its original, unmodified form. |
| 1874 | */ |
| 1875 | |
| 1876 | if (reg->off) { |
| 1877 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 1878 | regno, reg->off); |
| 1879 | return -EACCES; |
| 1880 | } |
| 1881 | |
| 1882 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 1883 | char tn_buf[48]; |
| 1884 | |
| 1885 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 1886 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 1887 | return -EACCES; |
| 1888 | } |
| 1889 | |
| 1890 | return 0; |
| 1891 | } |
| 1892 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 1893 | /* truncate register to smaller size (in bytes) |
| 1894 | * must be called with size < BPF_REG_SIZE |
| 1895 | */ |
| 1896 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 1897 | { |
| 1898 | u64 mask; |
| 1899 | |
| 1900 | /* clear high bits in bit representation */ |
| 1901 | reg->var_off = tnum_cast(reg->var_off, size); |
| 1902 | |
| 1903 | /* fix arithmetic bounds */ |
| 1904 | mask = ((u64)1 << (size * 8)) - 1; |
| 1905 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 1906 | reg->umin_value &= mask; |
| 1907 | reg->umax_value &= mask; |
| 1908 | } else { |
| 1909 | reg->umin_value = 0; |
| 1910 | reg->umax_value = mask; |
| 1911 | } |
| 1912 | reg->smin_value = reg->umin_value; |
| 1913 | reg->smax_value = reg->umax_value; |
| 1914 | } |
| 1915 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1916 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 1917 | * if t==write, value_regno is a register which value is stored into memory |
| 1918 | * if t==read, value_regno is a register which will receive the value from memory |
| 1919 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 1920 | * if t==read && value_regno==-1, don't care what we read from memory |
| 1921 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1922 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 1923 | int off, int bpf_size, enum bpf_access_type t, |
| 1924 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1925 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1926 | struct bpf_reg_state *regs = cur_regs(env); |
| 1927 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1928 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1929 | int size, err = 0; |
| 1930 | |
| 1931 | size = bpf_size_to_bytes(bpf_size); |
| 1932 | if (size < 0) |
| 1933 | return size; |
| 1934 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1935 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1936 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1937 | if (err) |
| 1938 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1939 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1940 | /* for access checks, reg->off is just part of off */ |
| 1941 | off += reg->off; |
| 1942 | |
| 1943 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1944 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1945 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1946 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1947 | return -EACCES; |
| 1948 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1949 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1950 | err = check_map_access(env, regno, off, size, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1951 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1952 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1953 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1954 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1955 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 1956 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1957 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1958 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1959 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1960 | return -EACCES; |
| 1961 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1962 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 1963 | err = check_ctx_reg(env, reg, regno); |
| 1964 | if (err < 0) |
| 1965 | return err; |
| 1966 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1967 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1968 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1969 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1970 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 1971 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1972 | */ |
| 1973 | if (reg_type == SCALAR_VALUE) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1974 | mark_reg_unknown(env, regs, value_regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1975 | else |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1976 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1977 | value_regno); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1978 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1979 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1980 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1981 | } else if (reg->type == PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1982 | off += reg->var_off.value; |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame^] | 1983 | err = check_stack_access(env, reg, off, size); |
| 1984 | if (err) |
| 1985 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1986 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1987 | state = func(env, reg); |
| 1988 | err = update_stack_depth(env, state, off); |
| 1989 | if (err) |
| 1990 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1991 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1992 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1993 | err = check_stack_write(env, state, off, size, |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1994 | value_regno, insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1995 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1996 | err = check_stack_read(env, state, off, size, |
| 1997 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1998 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1999 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2000 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2001 | return -EACCES; |
| 2002 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2003 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2004 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2005 | verbose(env, "R%d leaks addr into packet\n", |
| 2006 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 2007 | return -EACCES; |
| 2008 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2009 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2010 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2011 | mark_reg_unknown(env, regs, value_regno); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2012 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 2013 | if (t == BPF_WRITE && value_regno >= 0 && |
| 2014 | is_pointer_value(env, value_regno)) { |
| 2015 | verbose(env, "R%d leaks addr into flow keys\n", |
| 2016 | value_regno); |
| 2017 | return -EACCES; |
| 2018 | } |
| 2019 | |
| 2020 | err = check_flow_keys_access(env, off, size); |
| 2021 | if (!err && t == BPF_READ && value_regno >= 0) |
| 2022 | mark_reg_unknown(env, regs, value_regno); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2023 | } else if (reg->type == PTR_TO_SOCKET) { |
| 2024 | if (t == BPF_WRITE) { |
| 2025 | verbose(env, "cannot write into socket\n"); |
| 2026 | return -EACCES; |
| 2027 | } |
| 2028 | err = check_sock_access(env, regno, off, size, t); |
| 2029 | if (!err && value_regno >= 0) |
| 2030 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2031 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2032 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 2033 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2034 | return -EACCES; |
| 2035 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2036 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2037 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2038 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2039 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 2040 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2041 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2042 | return err; |
| 2043 | } |
| 2044 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2045 | 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] | 2046 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2047 | int err; |
| 2048 | |
| 2049 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 2050 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2051 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2052 | return -EINVAL; |
| 2053 | } |
| 2054 | |
| 2055 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2056 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2057 | if (err) |
| 2058 | return err; |
| 2059 | |
| 2060 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2061 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2062 | if (err) |
| 2063 | return err; |
| 2064 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 2065 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2066 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 2067 | return -EACCES; |
| 2068 | } |
| 2069 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2070 | if (is_ctx_reg(env, insn->dst_reg) || |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 2071 | is_pkt_reg(env, insn->dst_reg) || |
| 2072 | is_flow_key_reg(env, insn->dst_reg)) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2073 | 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] | 2074 | insn->dst_reg, |
| 2075 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 2076 | return -EACCES; |
| 2077 | } |
| 2078 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2079 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2080 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2081 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2082 | if (err) |
| 2083 | return err; |
| 2084 | |
| 2085 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2086 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2087 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | /* when register 'regno' is passed into function that will read 'access_size' |
| 2091 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2092 | * and all elements of stack are initialized. |
| 2093 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 2094 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2095 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2096 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2097 | int access_size, bool zero_size_allowed, |
| 2098 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2099 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2100 | struct bpf_reg_state *reg = reg_state(env, regno); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2101 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2102 | int off, i, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2103 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2104 | if (reg->type != PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2105 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2106 | if (zero_size_allowed && access_size == 0 && |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2107 | register_is_null(reg)) |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2108 | return 0; |
| 2109 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2110 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2111 | reg_type_str[reg->type], |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2112 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2113 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2114 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2115 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2116 | /* Only allow fixed-offset stack reads */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2117 | if (!tnum_is_const(reg->var_off)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2118 | char tn_buf[48]; |
| 2119 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2120 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2121 | verbose(env, "invalid variable stack read R%d var_off=%s\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2122 | regno, tn_buf); |
Jann Horn | ea25f91 | 2017-12-18 20:11:57 -0800 | [diff] [blame] | 2123 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2124 | } |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2125 | off = reg->off + reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2126 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2127 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2128 | verbose(env, "invalid stack type R%d off=%d access_size=%d\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2129 | regno, off, access_size); |
| 2130 | return -EACCES; |
| 2131 | } |
| 2132 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2133 | if (meta && meta->raw_mode) { |
| 2134 | meta->access_size = access_size; |
| 2135 | meta->regno = regno; |
| 2136 | return 0; |
| 2137 | } |
| 2138 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2139 | for (i = 0; i < access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2140 | u8 *stype; |
| 2141 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2142 | slot = -(off + i) - 1; |
| 2143 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2144 | if (state->allocated_stack <= slot) |
| 2145 | goto err; |
| 2146 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 2147 | if (*stype == STACK_MISC) |
| 2148 | goto mark; |
| 2149 | if (*stype == STACK_ZERO) { |
| 2150 | /* helper can write anything into the stack */ |
| 2151 | *stype = STACK_MISC; |
| 2152 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2153 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2154 | err: |
| 2155 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 2156 | off, i, access_size); |
| 2157 | return -EACCES; |
| 2158 | mark: |
| 2159 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 2160 | * the whole slot to be marked as 'read' |
| 2161 | */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2162 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
| 2163 | state->stack[spi].spilled_ptr.parent); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2164 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2165 | return update_stack_depth(env, state, off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2168 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 2169 | int access_size, bool zero_size_allowed, |
| 2170 | struct bpf_call_arg_meta *meta) |
| 2171 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2172 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2173 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2174 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2175 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2176 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2177 | return check_packet_access(env, regno, reg->off, access_size, |
| 2178 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2179 | case PTR_TO_MAP_VALUE: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2180 | return check_map_access(env, regno, reg->off, access_size, |
| 2181 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2182 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2183 | return check_stack_boundary(env, regno, access_size, |
| 2184 | zero_size_allowed, meta); |
| 2185 | } |
| 2186 | } |
| 2187 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2188 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 2189 | { |
| 2190 | return type == ARG_PTR_TO_MEM || |
| 2191 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 2192 | type == ARG_PTR_TO_UNINIT_MEM; |
| 2193 | } |
| 2194 | |
| 2195 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 2196 | { |
| 2197 | return type == ARG_CONST_SIZE || |
| 2198 | type == ARG_CONST_SIZE_OR_ZERO; |
| 2199 | } |
| 2200 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2201 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2202 | enum bpf_arg_type arg_type, |
| 2203 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2204 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2205 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2206 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2207 | int err = 0; |
| 2208 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2209 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2210 | return 0; |
| 2211 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2212 | err = check_reg_arg(env, regno, SRC_OP); |
| 2213 | if (err) |
| 2214 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2215 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2216 | if (arg_type == ARG_ANYTHING) { |
| 2217 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2218 | verbose(env, "R%d leaks addr into helper function\n", |
| 2219 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2220 | return -EACCES; |
| 2221 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2222 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2223 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2224 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2225 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2226 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2227 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2228 | return -EACCES; |
| 2229 | } |
| 2230 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2231 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2232 | arg_type == ARG_PTR_TO_MAP_VALUE || |
| 2233 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2234 | expected_type = PTR_TO_STACK; |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2235 | if (!type_is_pkt_pointer(type) && type != PTR_TO_MAP_VALUE && |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2236 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2237 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2238 | } else if (arg_type == ARG_CONST_SIZE || |
| 2239 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2240 | expected_type = SCALAR_VALUE; |
| 2241 | if (type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2242 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2243 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 2244 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2245 | if (type != expected_type) |
| 2246 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2247 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 2248 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2249 | if (type != expected_type) |
| 2250 | goto err_type; |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 2251 | err = check_ctx_reg(env, reg, regno); |
| 2252 | if (err < 0) |
| 2253 | return err; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2254 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
| 2255 | expected_type = PTR_TO_SOCKET; |
| 2256 | if (type != expected_type) |
| 2257 | goto err_type; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2258 | if (meta->ptr_id || !reg->id) { |
| 2259 | verbose(env, "verifier internal error: mismatched references meta=%d, reg=%d\n", |
| 2260 | meta->ptr_id, reg->id); |
| 2261 | return -EFAULT; |
| 2262 | } |
| 2263 | meta->ptr_id = reg->id; |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2264 | } else if (arg_type_is_mem_ptr(arg_type)) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2265 | expected_type = PTR_TO_STACK; |
| 2266 | /* One exception here. In case function allows for NULL to be |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2267 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2268 | * happens during stack boundary checking. |
| 2269 | */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2270 | if (register_is_null(reg) && |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 2271 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2272 | /* final test in check_stack_boundary() */; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2273 | else if (!type_is_pkt_pointer(type) && |
| 2274 | type != PTR_TO_MAP_VALUE && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2275 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2276 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2277 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2278 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2279 | verbose(env, "unsupported arg_type %d\n", arg_type); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2280 | return -EFAULT; |
| 2281 | } |
| 2282 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2283 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 2284 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2285 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2286 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 2287 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 2288 | * check that [key, key + map->key_size) are within |
| 2289 | * stack limits and initialized |
| 2290 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2291 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2292 | /* in function declaration map_ptr must come before |
| 2293 | * map_key, so that it's verified and known before |
| 2294 | * we have to check map_key here. Otherwise it means |
| 2295 | * that kernel subsystem misconfigured verifier |
| 2296 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2297 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2298 | return -EACCES; |
| 2299 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2300 | err = check_helper_mem_access(env, regno, |
| 2301 | meta->map_ptr->key_size, false, |
| 2302 | NULL); |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2303 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
| 2304 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2305 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 2306 | * check [value, value + map->value_size) validity |
| 2307 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2308 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2309 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2310 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2311 | return -EACCES; |
| 2312 | } |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2313 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2314 | err = check_helper_mem_access(env, regno, |
| 2315 | meta->map_ptr->value_size, false, |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2316 | meta); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2317 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2318 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2319 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2320 | /* remember the mem_size which may be used later |
| 2321 | * to refine return values. |
| 2322 | */ |
| 2323 | meta->msize_smax_value = reg->smax_value; |
| 2324 | meta->msize_umax_value = reg->umax_value; |
| 2325 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2326 | /* The register is SCALAR_VALUE; the access check |
| 2327 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2328 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2329 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2330 | /* For unprivileged variable accesses, disable raw |
| 2331 | * mode so that the program is required to |
| 2332 | * initialize all the memory that the helper could |
| 2333 | * just partially fill up. |
| 2334 | */ |
| 2335 | meta = NULL; |
| 2336 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2337 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2338 | 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] | 2339 | regno); |
| 2340 | return -EACCES; |
| 2341 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2342 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2343 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2344 | err = check_helper_mem_access(env, regno - 1, 0, |
| 2345 | zero_size_allowed, |
| 2346 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2347 | if (err) |
| 2348 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2349 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2350 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2351 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2352 | 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] | 2353 | regno); |
| 2354 | return -EACCES; |
| 2355 | } |
| 2356 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2357 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2358 | zero_size_allowed, meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2362 | err_type: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2363 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2364 | reg_type_str[type], reg_type_str[expected_type]); |
| 2365 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2366 | } |
| 2367 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2368 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 2369 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2370 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2371 | if (!map) |
| 2372 | return 0; |
| 2373 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2374 | /* We need a two way check, first is from map perspective ... */ |
| 2375 | switch (map->map_type) { |
| 2376 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 2377 | if (func_id != BPF_FUNC_tail_call) |
| 2378 | goto error; |
| 2379 | break; |
| 2380 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 2381 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2382 | func_id != BPF_FUNC_perf_event_output && |
| 2383 | func_id != BPF_FUNC_perf_event_read_value) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2384 | goto error; |
| 2385 | break; |
| 2386 | case BPF_MAP_TYPE_STACK_TRACE: |
| 2387 | if (func_id != BPF_FUNC_get_stackid) |
| 2388 | goto error; |
| 2389 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 2390 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 2391 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2392 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2393 | goto error; |
| 2394 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2395 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 2396 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2397 | if (func_id != BPF_FUNC_get_local_storage) |
| 2398 | goto error; |
| 2399 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2400 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
| 2401 | * allow to be modified from bpf side. So do not allow lookup elements |
| 2402 | * for now. |
| 2403 | */ |
| 2404 | case BPF_MAP_TYPE_DEVMAP: |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 2405 | if (func_id != BPF_FUNC_redirect_map) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2406 | goto error; |
| 2407 | break; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2408 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 2409 | * appear. |
| 2410 | */ |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2411 | case BPF_MAP_TYPE_CPUMAP: |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2412 | case BPF_MAP_TYPE_XSKMAP: |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2413 | if (func_id != BPF_FUNC_redirect_map) |
| 2414 | goto error; |
| 2415 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2416 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 2417 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2418 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 2419 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 2420 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2421 | case BPF_MAP_TYPE_SOCKMAP: |
| 2422 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 2423 | func_id != BPF_FUNC_sock_map_update && |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2424 | func_id != BPF_FUNC_map_delete_elem && |
| 2425 | func_id != BPF_FUNC_msg_redirect_map) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2426 | goto error; |
| 2427 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2428 | case BPF_MAP_TYPE_SOCKHASH: |
| 2429 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 2430 | func_id != BPF_FUNC_sock_hash_update && |
| 2431 | func_id != BPF_FUNC_map_delete_elem && |
| 2432 | func_id != BPF_FUNC_msg_redirect_hash) |
| 2433 | goto error; |
| 2434 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2435 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 2436 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 2437 | goto error; |
| 2438 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 2439 | case BPF_MAP_TYPE_QUEUE: |
| 2440 | case BPF_MAP_TYPE_STACK: |
| 2441 | if (func_id != BPF_FUNC_map_peek_elem && |
| 2442 | func_id != BPF_FUNC_map_pop_elem && |
| 2443 | func_id != BPF_FUNC_map_push_elem) |
| 2444 | goto error; |
| 2445 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2446 | default: |
| 2447 | break; |
| 2448 | } |
| 2449 | |
| 2450 | /* ... and second from the function itself. */ |
| 2451 | switch (func_id) { |
| 2452 | case BPF_FUNC_tail_call: |
| 2453 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 2454 | goto error; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 2455 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2456 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
| 2457 | return -EINVAL; |
| 2458 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2459 | break; |
| 2460 | case BPF_FUNC_perf_event_read: |
| 2461 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2462 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2463 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 2464 | goto error; |
| 2465 | break; |
| 2466 | case BPF_FUNC_get_stackid: |
| 2467 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 2468 | goto error; |
| 2469 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2470 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 2471 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2472 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 2473 | goto error; |
| 2474 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2475 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 2476 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2477 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 2478 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2479 | goto error; |
| 2480 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2481 | case BPF_FUNC_sk_redirect_map: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2482 | case BPF_FUNC_msg_redirect_map: |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2483 | case BPF_FUNC_sock_map_update: |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2484 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 2485 | goto error; |
| 2486 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2487 | case BPF_FUNC_sk_redirect_hash: |
| 2488 | case BPF_FUNC_msg_redirect_hash: |
| 2489 | case BPF_FUNC_sock_hash_update: |
| 2490 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2491 | goto error; |
| 2492 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2493 | case BPF_FUNC_get_local_storage: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 2494 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 2495 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2496 | goto error; |
| 2497 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2498 | case BPF_FUNC_sk_select_reuseport: |
| 2499 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) |
| 2500 | goto error; |
| 2501 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 2502 | case BPF_FUNC_map_peek_elem: |
| 2503 | case BPF_FUNC_map_pop_elem: |
| 2504 | case BPF_FUNC_map_push_elem: |
| 2505 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 2506 | map->map_type != BPF_MAP_TYPE_STACK) |
| 2507 | goto error; |
| 2508 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2509 | default: |
| 2510 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
| 2513 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2514 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2515 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2516 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2517 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2520 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2521 | { |
| 2522 | int count = 0; |
| 2523 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2524 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2525 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2526 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2527 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2528 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2529 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2530 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2531 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2532 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2533 | count++; |
| 2534 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2535 | /* We only support one arg being in raw mode at the moment, |
| 2536 | * which is sufficient for the helper functions we have |
| 2537 | * right now. |
| 2538 | */ |
| 2539 | return count <= 1; |
| 2540 | } |
| 2541 | |
| 2542 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 2543 | enum bpf_arg_type arg_next) |
| 2544 | { |
| 2545 | return (arg_type_is_mem_ptr(arg_curr) && |
| 2546 | !arg_type_is_mem_size(arg_next)) || |
| 2547 | (!arg_type_is_mem_ptr(arg_curr) && |
| 2548 | arg_type_is_mem_size(arg_next)); |
| 2549 | } |
| 2550 | |
| 2551 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 2552 | { |
| 2553 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 2554 | * bytes from memory 'buf'. Both arg types need |
| 2555 | * to be paired, so make sure there's no buggy |
| 2556 | * helper function specification. |
| 2557 | */ |
| 2558 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 2559 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 2560 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 2561 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 2562 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 2563 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 2564 | return false; |
| 2565 | |
| 2566 | return true; |
| 2567 | } |
| 2568 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2569 | static bool check_refcount_ok(const struct bpf_func_proto *fn) |
| 2570 | { |
| 2571 | int count = 0; |
| 2572 | |
| 2573 | if (arg_type_is_refcounted(fn->arg1_type)) |
| 2574 | count++; |
| 2575 | if (arg_type_is_refcounted(fn->arg2_type)) |
| 2576 | count++; |
| 2577 | if (arg_type_is_refcounted(fn->arg3_type)) |
| 2578 | count++; |
| 2579 | if (arg_type_is_refcounted(fn->arg4_type)) |
| 2580 | count++; |
| 2581 | if (arg_type_is_refcounted(fn->arg5_type)) |
| 2582 | count++; |
| 2583 | |
| 2584 | /* We only support one arg being unreferenced at the moment, |
| 2585 | * which is sufficient for the helper functions we have right now. |
| 2586 | */ |
| 2587 | return count <= 1; |
| 2588 | } |
| 2589 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2590 | static int check_func_proto(const struct bpf_func_proto *fn) |
| 2591 | { |
| 2592 | return check_raw_mode_ok(fn) && |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2593 | check_arg_pair_ok(fn) && |
| 2594 | check_refcount_ok(fn) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2595 | } |
| 2596 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2597 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 2598 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2599 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2600 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 2601 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2602 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2603 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2604 | int i; |
| 2605 | |
| 2606 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2607 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2608 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2609 | |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 2610 | bpf_for_each_spilled_reg(i, state, reg) { |
| 2611 | if (!reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2612 | continue; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2613 | if (reg_is_pkt_pointer_any(reg)) |
| 2614 | __mark_reg_unknown(reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2615 | } |
| 2616 | } |
| 2617 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2618 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 2619 | { |
| 2620 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2621 | int i; |
| 2622 | |
| 2623 | for (i = 0; i <= vstate->curframe; i++) |
| 2624 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 2625 | } |
| 2626 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2627 | static void release_reg_references(struct bpf_verifier_env *env, |
| 2628 | struct bpf_func_state *state, int id) |
| 2629 | { |
| 2630 | struct bpf_reg_state *regs = state->regs, *reg; |
| 2631 | int i; |
| 2632 | |
| 2633 | for (i = 0; i < MAX_BPF_REG; i++) |
| 2634 | if (regs[i].id == id) |
| 2635 | mark_reg_unknown(env, regs, i); |
| 2636 | |
| 2637 | bpf_for_each_spilled_reg(i, state, reg) { |
| 2638 | if (!reg) |
| 2639 | continue; |
| 2640 | if (reg_is_refcounted(reg) && reg->id == id) |
| 2641 | __mark_reg_unknown(reg); |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | /* The pointer with the specified id has released its reference to kernel |
| 2646 | * resources. Identify all copies of the same pointer and clear the reference. |
| 2647 | */ |
| 2648 | static int release_reference(struct bpf_verifier_env *env, |
| 2649 | struct bpf_call_arg_meta *meta) |
| 2650 | { |
| 2651 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2652 | int i; |
| 2653 | |
| 2654 | for (i = 0; i <= vstate->curframe; i++) |
| 2655 | release_reg_references(env, vstate->frame[i], meta->ptr_id); |
| 2656 | |
| 2657 | return release_reference_state(env, meta->ptr_id); |
| 2658 | } |
| 2659 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2660 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 2661 | int *insn_idx) |
| 2662 | { |
| 2663 | struct bpf_verifier_state *state = env->cur_state; |
| 2664 | struct bpf_func_state *caller, *callee; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2665 | int i, err, subprog, target_insn; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2666 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 2667 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2668 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 2669 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2670 | return -E2BIG; |
| 2671 | } |
| 2672 | |
| 2673 | target_insn = *insn_idx + insn->imm; |
| 2674 | subprog = find_subprog(env, target_insn + 1); |
| 2675 | if (subprog < 0) { |
| 2676 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 2677 | target_insn + 1); |
| 2678 | return -EFAULT; |
| 2679 | } |
| 2680 | |
| 2681 | caller = state->frame[state->curframe]; |
| 2682 | if (state->frame[state->curframe + 1]) { |
| 2683 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 2684 | state->curframe + 1); |
| 2685 | return -EFAULT; |
| 2686 | } |
| 2687 | |
| 2688 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 2689 | if (!callee) |
| 2690 | return -ENOMEM; |
| 2691 | state->frame[state->curframe + 1] = callee; |
| 2692 | |
| 2693 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 2694 | * into its own stack before reading from it. |
| 2695 | * callee can read/write into caller's stack |
| 2696 | */ |
| 2697 | init_func_state(env, callee, |
| 2698 | /* remember the callsite, it will be used by bpf_exit */ |
| 2699 | *insn_idx /* callsite */, |
| 2700 | state->curframe + 1 /* frameno within this callchain */, |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 2701 | subprog /* subprog number within this prog */); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2702 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2703 | /* Transfer references to the callee */ |
| 2704 | err = transfer_reference_state(callee, caller); |
| 2705 | if (err) |
| 2706 | return err; |
| 2707 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2708 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 2709 | * pointers, which connects us up to the liveness chain |
| 2710 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2711 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 2712 | callee->regs[i] = caller->regs[i]; |
| 2713 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2714 | /* after the call registers r0 - r5 were scratched */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2715 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 2716 | mark_reg_not_init(env, caller->regs, caller_saved[i]); |
| 2717 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2718 | } |
| 2719 | |
| 2720 | /* only increment it after check_reg_arg() finished */ |
| 2721 | state->curframe++; |
| 2722 | |
| 2723 | /* and go analyze first insn of the callee */ |
| 2724 | *insn_idx = target_insn; |
| 2725 | |
| 2726 | if (env->log.level) { |
| 2727 | verbose(env, "caller:\n"); |
| 2728 | print_verifier_state(env, caller); |
| 2729 | verbose(env, "callee:\n"); |
| 2730 | print_verifier_state(env, callee); |
| 2731 | } |
| 2732 | return 0; |
| 2733 | } |
| 2734 | |
| 2735 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 2736 | { |
| 2737 | struct bpf_verifier_state *state = env->cur_state; |
| 2738 | struct bpf_func_state *caller, *callee; |
| 2739 | struct bpf_reg_state *r0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2740 | int err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2741 | |
| 2742 | callee = state->frame[state->curframe]; |
| 2743 | r0 = &callee->regs[BPF_REG_0]; |
| 2744 | if (r0->type == PTR_TO_STACK) { |
| 2745 | /* technically it's ok to return caller's stack pointer |
| 2746 | * (or caller's caller's pointer) back to the caller, |
| 2747 | * since these pointers are valid. Only current stack |
| 2748 | * pointer will be invalid as soon as function exits, |
| 2749 | * but let's be conservative |
| 2750 | */ |
| 2751 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 2752 | return -EINVAL; |
| 2753 | } |
| 2754 | |
| 2755 | state->curframe--; |
| 2756 | caller = state->frame[state->curframe]; |
| 2757 | /* return to the caller whatever r0 had in the callee */ |
| 2758 | caller->regs[BPF_REG_0] = *r0; |
| 2759 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2760 | /* Transfer references to the caller */ |
| 2761 | err = transfer_reference_state(caller, callee); |
| 2762 | if (err) |
| 2763 | return err; |
| 2764 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2765 | *insn_idx = callee->callsite + 1; |
| 2766 | if (env->log.level) { |
| 2767 | verbose(env, "returning from callee:\n"); |
| 2768 | print_verifier_state(env, callee); |
| 2769 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 2770 | print_verifier_state(env, caller); |
| 2771 | } |
| 2772 | /* clear everything in the callee */ |
| 2773 | free_func_state(callee); |
| 2774 | state->frame[state->curframe + 1] = NULL; |
| 2775 | return 0; |
| 2776 | } |
| 2777 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2778 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 2779 | int func_id, |
| 2780 | struct bpf_call_arg_meta *meta) |
| 2781 | { |
| 2782 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 2783 | |
| 2784 | if (ret_type != RET_INTEGER || |
| 2785 | (func_id != BPF_FUNC_get_stack && |
| 2786 | func_id != BPF_FUNC_probe_read_str)) |
| 2787 | return; |
| 2788 | |
| 2789 | ret_reg->smax_value = meta->msize_smax_value; |
| 2790 | ret_reg->umax_value = meta->msize_umax_value; |
| 2791 | __reg_deduce_bounds(ret_reg); |
| 2792 | __reg_bound_offset(ret_reg); |
| 2793 | } |
| 2794 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2795 | static int |
| 2796 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 2797 | int func_id, int insn_idx) |
| 2798 | { |
| 2799 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
| 2800 | |
| 2801 | if (func_id != BPF_FUNC_tail_call && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 2802 | func_id != BPF_FUNC_map_lookup_elem && |
| 2803 | func_id != BPF_FUNC_map_update_elem && |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 2804 | func_id != BPF_FUNC_map_delete_elem && |
| 2805 | func_id != BPF_FUNC_map_push_elem && |
| 2806 | func_id != BPF_FUNC_map_pop_elem && |
| 2807 | func_id != BPF_FUNC_map_peek_elem) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2808 | return 0; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 2809 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2810 | if (meta->map_ptr == NULL) { |
| 2811 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 2812 | return -EINVAL; |
| 2813 | } |
| 2814 | |
| 2815 | if (!BPF_MAP_PTR(aux->map_state)) |
| 2816 | bpf_map_ptr_store(aux, meta->map_ptr, |
| 2817 | meta->map_ptr->unpriv_array); |
| 2818 | else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr) |
| 2819 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
| 2820 | meta->map_ptr->unpriv_array); |
| 2821 | return 0; |
| 2822 | } |
| 2823 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2824 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 2825 | { |
| 2826 | struct bpf_func_state *state = cur_func(env); |
| 2827 | int i; |
| 2828 | |
| 2829 | for (i = 0; i < state->acquired_refs; i++) { |
| 2830 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 2831 | state->refs[i].id, state->refs[i].insn_idx); |
| 2832 | } |
| 2833 | return state->acquired_refs ? -EINVAL : 0; |
| 2834 | } |
| 2835 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2836 | 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] | 2837 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2838 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2839 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2840 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2841 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2842 | int i, err; |
| 2843 | |
| 2844 | /* find function prototype */ |
| 2845 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2846 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 2847 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2848 | return -EINVAL; |
| 2849 | } |
| 2850 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 2851 | if (env->ops->get_func_proto) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2852 | fn = env->ops->get_func_proto(func_id, env->prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2853 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2854 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 2855 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2856 | return -EINVAL; |
| 2857 | } |
| 2858 | |
| 2859 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 2860 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Daniel Borkmann | 3fe2867 | 2018-06-02 23:06:33 +0200 | [diff] [blame] | 2861 | 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] | 2862 | return -EINVAL; |
| 2863 | } |
| 2864 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 2865 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 2866 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 2867 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 2868 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 2869 | func_id_name(func_id), func_id); |
| 2870 | return -EINVAL; |
| 2871 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2872 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2873 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2874 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2875 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2876 | err = check_func_proto(fn); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2877 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2878 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2879 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2880 | return err; |
| 2881 | } |
| 2882 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2883 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2884 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2885 | if (err) |
| 2886 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2887 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2888 | if (err) |
| 2889 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2890 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2891 | if (err) |
| 2892 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2893 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2894 | if (err) |
| 2895 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2896 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2897 | if (err) |
| 2898 | return err; |
| 2899 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2900 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 2901 | if (err) |
| 2902 | return err; |
| 2903 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2904 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 2905 | * is inferred from register state. |
| 2906 | */ |
| 2907 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2908 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 2909 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2910 | if (err) |
| 2911 | return err; |
| 2912 | } |
| 2913 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2914 | if (func_id == BPF_FUNC_tail_call) { |
| 2915 | err = check_reference_leak(env); |
| 2916 | if (err) { |
| 2917 | verbose(env, "tail_call would lead to reference leak\n"); |
| 2918 | return err; |
| 2919 | } |
| 2920 | } else if (is_release_function(func_id)) { |
| 2921 | err = release_reference(env, &meta); |
| 2922 | if (err) |
| 2923 | return err; |
| 2924 | } |
| 2925 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2926 | regs = cur_regs(env); |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2927 | |
| 2928 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 2929 | * this is required because get_local_storage() can't return an error. |
| 2930 | */ |
| 2931 | if (func_id == BPF_FUNC_get_local_storage && |
| 2932 | !register_is_null(®s[BPF_REG_2])) { |
| 2933 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 2934 | return -EINVAL; |
| 2935 | } |
| 2936 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2937 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2938 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2939 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2940 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2941 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2942 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2943 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2944 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2945 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2946 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2947 | } else if (fn->ret_type == RET_VOID) { |
| 2948 | regs[BPF_REG_0].type = NOT_INIT; |
Roman Gushchin | 3e6a4b3 | 2018-08-02 14:27:22 -0700 | [diff] [blame] | 2949 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 2950 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2951 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2952 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2953 | /* remember map_ptr, so that check_map_access() |
| 2954 | * can check 'value_size' boundary of memory access |
| 2955 | * to map element returned from bpf_map_lookup_elem() |
| 2956 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2957 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2958 | verbose(env, |
| 2959 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2960 | return -EINVAL; |
| 2961 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2962 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 2963 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 2964 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
| 2965 | } else { |
| 2966 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
| 2967 | regs[BPF_REG_0].id = ++env->id_gen; |
| 2968 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2969 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2970 | int id = acquire_reference_state(env, insn_idx); |
| 2971 | if (id < 0) |
| 2972 | return id; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2973 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 2974 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2975 | regs[BPF_REG_0].id = id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2976 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2977 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2978 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2979 | return -EINVAL; |
| 2980 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2981 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2982 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
| 2983 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2984 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2985 | if (err) |
| 2986 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2987 | |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 2988 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
| 2989 | const char *err_str; |
| 2990 | |
| 2991 | #ifdef CONFIG_PERF_EVENTS |
| 2992 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 2993 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 2994 | #else |
| 2995 | err = -ENOTSUPP; |
| 2996 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 2997 | #endif |
| 2998 | if (err) { |
| 2999 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 3000 | return err; |
| 3001 | } |
| 3002 | |
| 3003 | env->prog->has_callchain_buf = true; |
| 3004 | } |
| 3005 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3006 | if (changes_data) |
| 3007 | clear_all_pkt_pointers(env); |
| 3008 | return 0; |
| 3009 | } |
| 3010 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3011 | static bool signed_add_overflows(s64 a, s64 b) |
| 3012 | { |
| 3013 | /* Do the add in u64, where overflow is well-defined */ |
| 3014 | s64 res = (s64)((u64)a + (u64)b); |
| 3015 | |
| 3016 | if (b < 0) |
| 3017 | return res > a; |
| 3018 | return res < a; |
| 3019 | } |
| 3020 | |
| 3021 | static bool signed_sub_overflows(s64 a, s64 b) |
| 3022 | { |
| 3023 | /* Do the sub in u64, where overflow is well-defined */ |
| 3024 | s64 res = (s64)((u64)a - (u64)b); |
| 3025 | |
| 3026 | if (b < 0) |
| 3027 | return res < a; |
| 3028 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3029 | } |
| 3030 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3031 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 3032 | const struct bpf_reg_state *reg, |
| 3033 | enum bpf_reg_type type) |
| 3034 | { |
| 3035 | bool known = tnum_is_const(reg->var_off); |
| 3036 | s64 val = reg->var_off.value; |
| 3037 | s64 smin = reg->smin_value; |
| 3038 | |
| 3039 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 3040 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 3041 | reg_type_str[type], val); |
| 3042 | return false; |
| 3043 | } |
| 3044 | |
| 3045 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 3046 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 3047 | reg_type_str[type], reg->off); |
| 3048 | return false; |
| 3049 | } |
| 3050 | |
| 3051 | if (smin == S64_MIN) { |
| 3052 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 3053 | reg_type_str[type]); |
| 3054 | return false; |
| 3055 | } |
| 3056 | |
| 3057 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 3058 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 3059 | smin, reg_type_str[type]); |
| 3060 | return false; |
| 3061 | } |
| 3062 | |
| 3063 | return true; |
| 3064 | } |
| 3065 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3066 | /* 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] | 3067 | * Caller should also handle BPF_MOV case separately. |
| 3068 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 3069 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 3070 | */ |
| 3071 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 3072 | struct bpf_insn *insn, |
| 3073 | const struct bpf_reg_state *ptr_reg, |
| 3074 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3075 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3076 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3077 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 3078 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3079 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3080 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 3081 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 3082 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 3083 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3084 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3085 | u32 dst = insn->dst_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3086 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3087 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3088 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 3089 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 3090 | smin_val > smax_val || umin_val > umax_val) { |
| 3091 | /* Taint dst register if offset had invalid bounds derived from |
| 3092 | * e.g. dead branches. |
| 3093 | */ |
| 3094 | __mark_reg_unknown(dst_reg); |
| 3095 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3096 | } |
| 3097 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3098 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3099 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3100 | verbose(env, |
| 3101 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 3102 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3103 | return -EACCES; |
| 3104 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3105 | |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3106 | switch (ptr_reg->type) { |
| 3107 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 3108 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
| 3109 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3110 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3111 | case CONST_PTR_TO_MAP: |
| 3112 | case PTR_TO_PACKET_END: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3113 | case PTR_TO_SOCKET: |
| 3114 | case PTR_TO_SOCKET_OR_NULL: |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3115 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
| 3116 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3117 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3118 | default: |
| 3119 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 3123 | * 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] | 3124 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3125 | dst_reg->type = ptr_reg->type; |
| 3126 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 3127 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3128 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 3129 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 3130 | return -EINVAL; |
| 3131 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3132 | switch (opcode) { |
| 3133 | case BPF_ADD: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3134 | /* We can take a fixed offset as long as it doesn't overflow |
| 3135 | * the s32 'off' field |
| 3136 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3137 | if (known && (ptr_reg->off + smin_val == |
| 3138 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3139 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3140 | dst_reg->smin_value = smin_ptr; |
| 3141 | dst_reg->smax_value = smax_ptr; |
| 3142 | dst_reg->umin_value = umin_ptr; |
| 3143 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3144 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3145 | dst_reg->off = ptr_reg->off + smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3146 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3147 | break; |
| 3148 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3149 | /* A new variable offset is created. Note that off_reg->off |
| 3150 | * == 0, since it's a scalar. |
| 3151 | * dst_reg gets the pointer type and since some positive |
| 3152 | * integer value was added to the pointer, give it a new 'id' |
| 3153 | * if it's a PTR_TO_PACKET. |
| 3154 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 3155 | * added into the variable offset, and we copy the fixed offset |
| 3156 | * from ptr_reg. |
| 3157 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3158 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 3159 | signed_add_overflows(smax_ptr, smax_val)) { |
| 3160 | dst_reg->smin_value = S64_MIN; |
| 3161 | dst_reg->smax_value = S64_MAX; |
| 3162 | } else { |
| 3163 | dst_reg->smin_value = smin_ptr + smin_val; |
| 3164 | dst_reg->smax_value = smax_ptr + smax_val; |
| 3165 | } |
| 3166 | if (umin_ptr + umin_val < umin_ptr || |
| 3167 | umax_ptr + umax_val < umax_ptr) { |
| 3168 | dst_reg->umin_value = 0; |
| 3169 | dst_reg->umax_value = U64_MAX; |
| 3170 | } else { |
| 3171 | dst_reg->umin_value = umin_ptr + umin_val; |
| 3172 | dst_reg->umax_value = umax_ptr + umax_val; |
| 3173 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3174 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 3175 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3176 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3177 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3178 | dst_reg->id = ++env->id_gen; |
| 3179 | /* something was added to pkt_ptr, set range to zero */ |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3180 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3181 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3182 | break; |
| 3183 | case BPF_SUB: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3184 | if (dst_reg == off_reg) { |
| 3185 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3186 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 3187 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3188 | return -EACCES; |
| 3189 | } |
| 3190 | /* We don't allow subtraction from FP, because (according to |
| 3191 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 3192 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 3193 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3194 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3195 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 3196 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3197 | return -EACCES; |
| 3198 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3199 | if (known && (ptr_reg->off - smin_val == |
| 3200 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3201 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3202 | dst_reg->smin_value = smin_ptr; |
| 3203 | dst_reg->smax_value = smax_ptr; |
| 3204 | dst_reg->umin_value = umin_ptr; |
| 3205 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3206 | dst_reg->var_off = ptr_reg->var_off; |
| 3207 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3208 | dst_reg->off = ptr_reg->off - smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3209 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3210 | break; |
| 3211 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3212 | /* A new variable offset is created. If the subtrahend is known |
| 3213 | * nonnegative, then any reg->range we had before is still good. |
| 3214 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3215 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 3216 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 3217 | /* Overflow possible, we know nothing */ |
| 3218 | dst_reg->smin_value = S64_MIN; |
| 3219 | dst_reg->smax_value = S64_MAX; |
| 3220 | } else { |
| 3221 | dst_reg->smin_value = smin_ptr - smax_val; |
| 3222 | dst_reg->smax_value = smax_ptr - smin_val; |
| 3223 | } |
| 3224 | if (umin_ptr < umax_val) { |
| 3225 | /* Overflow possible, we know nothing */ |
| 3226 | dst_reg->umin_value = 0; |
| 3227 | dst_reg->umax_value = U64_MAX; |
| 3228 | } else { |
| 3229 | /* Cannot overflow (as long as bounds are consistent) */ |
| 3230 | dst_reg->umin_value = umin_ptr - umax_val; |
| 3231 | dst_reg->umax_value = umax_ptr - umin_val; |
| 3232 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3233 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 3234 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3235 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3236 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3237 | dst_reg->id = ++env->id_gen; |
| 3238 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3239 | if (smin_val < 0) |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3240 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3241 | } |
| 3242 | break; |
| 3243 | case BPF_AND: |
| 3244 | case BPF_OR: |
| 3245 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3246 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 3247 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 3248 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3249 | return -EACCES; |
| 3250 | default: |
| 3251 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3252 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 3253 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3254 | return -EACCES; |
| 3255 | } |
| 3256 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3257 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 3258 | return -EINVAL; |
| 3259 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3260 | __update_reg_bounds(dst_reg); |
| 3261 | __reg_deduce_bounds(dst_reg); |
| 3262 | __reg_bound_offset(dst_reg); |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 3263 | |
| 3264 | /* For unprivileged we require that resulting offset must be in bounds |
| 3265 | * in order to be able to sanitize access later on. |
| 3266 | */ |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame^] | 3267 | if (!env->allow_ptr_leaks) { |
| 3268 | if (dst_reg->type == PTR_TO_MAP_VALUE && |
| 3269 | check_map_access(env, dst, dst_reg->off, 1, false)) { |
| 3270 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
| 3271 | "prohibited for !root\n", dst); |
| 3272 | return -EACCES; |
| 3273 | } else if (dst_reg->type == PTR_TO_STACK && |
| 3274 | check_stack_access(env, dst_reg, dst_reg->off + |
| 3275 | dst_reg->var_off.value, 1)) { |
| 3276 | verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 3277 | "prohibited for !root\n", dst); |
| 3278 | return -EACCES; |
| 3279 | } |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 3280 | } |
| 3281 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3282 | return 0; |
| 3283 | } |
| 3284 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3285 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 3286 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 3287 | * need extra checks in the 32-bit case. |
| 3288 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3289 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 3290 | struct bpf_insn *insn, |
| 3291 | struct bpf_reg_state *dst_reg, |
| 3292 | struct bpf_reg_state src_reg) |
| 3293 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3294 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3295 | u8 opcode = BPF_OP(insn->code); |
| 3296 | bool src_known, dst_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3297 | s64 smin_val, smax_val; |
| 3298 | u64 umin_val, umax_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3299 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3300 | |
Jann Horn | b799207 | 2018-10-05 18:17:59 +0200 | [diff] [blame] | 3301 | if (insn_bitness == 32) { |
| 3302 | /* Relevant for 32-bit RSH: Information can propagate towards |
| 3303 | * LSB, so it isn't sufficient to only truncate the output to |
| 3304 | * 32 bits. |
| 3305 | */ |
| 3306 | coerce_reg_to_size(dst_reg, 4); |
| 3307 | coerce_reg_to_size(&src_reg, 4); |
| 3308 | } |
| 3309 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3310 | smin_val = src_reg.smin_value; |
| 3311 | smax_val = src_reg.smax_value; |
| 3312 | umin_val = src_reg.umin_value; |
| 3313 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3314 | src_known = tnum_is_const(src_reg.var_off); |
| 3315 | dst_known = tnum_is_const(dst_reg->var_off); |
| 3316 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 3317 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
| 3318 | smin_val > smax_val || umin_val > umax_val) { |
| 3319 | /* Taint dst register if offset had invalid bounds derived from |
| 3320 | * e.g. dead branches. |
| 3321 | */ |
| 3322 | __mark_reg_unknown(dst_reg); |
| 3323 | return 0; |
| 3324 | } |
| 3325 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3326 | if (!src_known && |
| 3327 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
| 3328 | __mark_reg_unknown(dst_reg); |
| 3329 | return 0; |
| 3330 | } |
| 3331 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3332 | switch (opcode) { |
| 3333 | case BPF_ADD: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3334 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 3335 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 3336 | dst_reg->smin_value = S64_MIN; |
| 3337 | dst_reg->smax_value = S64_MAX; |
| 3338 | } else { |
| 3339 | dst_reg->smin_value += smin_val; |
| 3340 | dst_reg->smax_value += smax_val; |
| 3341 | } |
| 3342 | if (dst_reg->umin_value + umin_val < umin_val || |
| 3343 | dst_reg->umax_value + umax_val < umax_val) { |
| 3344 | dst_reg->umin_value = 0; |
| 3345 | dst_reg->umax_value = U64_MAX; |
| 3346 | } else { |
| 3347 | dst_reg->umin_value += umin_val; |
| 3348 | dst_reg->umax_value += umax_val; |
| 3349 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3350 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 3351 | break; |
| 3352 | case BPF_SUB: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3353 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 3354 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 3355 | /* Overflow possible, we know nothing */ |
| 3356 | dst_reg->smin_value = S64_MIN; |
| 3357 | dst_reg->smax_value = S64_MAX; |
| 3358 | } else { |
| 3359 | dst_reg->smin_value -= smax_val; |
| 3360 | dst_reg->smax_value -= smin_val; |
| 3361 | } |
| 3362 | if (dst_reg->umin_value < umax_val) { |
| 3363 | /* Overflow possible, we know nothing */ |
| 3364 | dst_reg->umin_value = 0; |
| 3365 | dst_reg->umax_value = U64_MAX; |
| 3366 | } else { |
| 3367 | /* Cannot overflow (as long as bounds are consistent) */ |
| 3368 | dst_reg->umin_value -= umax_val; |
| 3369 | dst_reg->umax_value -= umin_val; |
| 3370 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3371 | 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] | 3372 | break; |
| 3373 | case BPF_MUL: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3374 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 3375 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3376 | /* Ain't nobody got time to multiply that sign */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3377 | __mark_reg_unbounded(dst_reg); |
| 3378 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3379 | break; |
| 3380 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3381 | /* Both values are positive, so we can work with unsigned and |
| 3382 | * copy the result to signed (unless it exceeds S64_MAX). |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3383 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3384 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 3385 | /* Potential overflow, we know nothing */ |
| 3386 | __mark_reg_unbounded(dst_reg); |
| 3387 | /* (except what we can learn from the var_off) */ |
| 3388 | __update_reg_bounds(dst_reg); |
| 3389 | break; |
| 3390 | } |
| 3391 | dst_reg->umin_value *= umin_val; |
| 3392 | dst_reg->umax_value *= umax_val; |
| 3393 | if (dst_reg->umax_value > S64_MAX) { |
| 3394 | /* Overflow possible, we know nothing */ |
| 3395 | dst_reg->smin_value = S64_MIN; |
| 3396 | dst_reg->smax_value = S64_MAX; |
| 3397 | } else { |
| 3398 | dst_reg->smin_value = dst_reg->umin_value; |
| 3399 | dst_reg->smax_value = dst_reg->umax_value; |
| 3400 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3401 | break; |
| 3402 | case BPF_AND: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3403 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3404 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 3405 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3406 | break; |
| 3407 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3408 | /* We get our minimum from the var_off, since that's inherently |
| 3409 | * bitwise. Our maximum is the minimum of the operands' maxima. |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 3410 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3411 | 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] | 3412 | dst_reg->umin_value = dst_reg->var_off.value; |
| 3413 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 3414 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 3415 | /* Lose signed bounds when ANDing negative numbers, |
| 3416 | * ain't nobody got time for that. |
| 3417 | */ |
| 3418 | dst_reg->smin_value = S64_MIN; |
| 3419 | dst_reg->smax_value = S64_MAX; |
| 3420 | } else { |
| 3421 | /* ANDing two positives gives a positive, so safe to |
| 3422 | * cast result into s64. |
| 3423 | */ |
| 3424 | dst_reg->smin_value = dst_reg->umin_value; |
| 3425 | dst_reg->smax_value = dst_reg->umax_value; |
| 3426 | } |
| 3427 | /* We may learn something more from the var_off */ |
| 3428 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3429 | break; |
| 3430 | case BPF_OR: |
| 3431 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3432 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
| 3433 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3434 | break; |
| 3435 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3436 | /* We get our maximum from the var_off, and our minimum is the |
| 3437 | * maximum of the operands' minima |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3438 | */ |
| 3439 | 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] | 3440 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 3441 | dst_reg->umax_value = dst_reg->var_off.value | |
| 3442 | dst_reg->var_off.mask; |
| 3443 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 3444 | /* Lose signed bounds when ORing negative numbers, |
| 3445 | * ain't nobody got time for that. |
| 3446 | */ |
| 3447 | dst_reg->smin_value = S64_MIN; |
| 3448 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3449 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3450 | /* ORing two positives gives a positive, so safe to |
| 3451 | * cast result into s64. |
| 3452 | */ |
| 3453 | dst_reg->smin_value = dst_reg->umin_value; |
| 3454 | dst_reg->smax_value = dst_reg->umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3455 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3456 | /* We may learn something more from the var_off */ |
| 3457 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3458 | break; |
| 3459 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3460 | if (umax_val >= insn_bitness) { |
| 3461 | /* Shifts greater than 31 or 63 are undefined. |
| 3462 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3463 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3464 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3465 | break; |
| 3466 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3467 | /* We lose all sign bit information (except what we can pick |
| 3468 | * up from var_off) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3469 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3470 | dst_reg->smin_value = S64_MIN; |
| 3471 | dst_reg->smax_value = S64_MAX; |
| 3472 | /* If we might shift our top bit out, then we know nothing */ |
| 3473 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 3474 | dst_reg->umin_value = 0; |
| 3475 | dst_reg->umax_value = U64_MAX; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3476 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3477 | dst_reg->umin_value <<= umin_val; |
| 3478 | dst_reg->umax_value <<= umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3479 | } |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 3480 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3481 | /* We may learn something more from the var_off */ |
| 3482 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3483 | break; |
| 3484 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3485 | if (umax_val >= insn_bitness) { |
| 3486 | /* Shifts greater than 31 or 63 are undefined. |
| 3487 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3488 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3489 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3490 | break; |
| 3491 | } |
Edward Cree | 4374f25 | 2017-12-18 20:11:53 -0800 | [diff] [blame] | 3492 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 3493 | * be negative, then either: |
| 3494 | * 1) src_reg might be zero, so the sign bit of the result is |
| 3495 | * unknown, so we lose our signed bounds |
| 3496 | * 2) it's known negative, thus the unsigned bounds capture the |
| 3497 | * signed bounds |
| 3498 | * 3) the signed bounds cross zero, so they tell us nothing |
| 3499 | * about the result |
| 3500 | * If the value in dst_reg is known nonnegative, then again the |
| 3501 | * unsigned bounts capture the signed bounds. |
| 3502 | * Thus, in all cases it suffices to blow away our signed bounds |
| 3503 | * and rely on inferring new ones from the unsigned bounds and |
| 3504 | * var_off of the result. |
| 3505 | */ |
| 3506 | dst_reg->smin_value = S64_MIN; |
| 3507 | dst_reg->smax_value = S64_MAX; |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 3508 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3509 | dst_reg->umin_value >>= umax_val; |
| 3510 | dst_reg->umax_value >>= umin_val; |
| 3511 | /* We may learn something more from the var_off */ |
| 3512 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3513 | break; |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 3514 | case BPF_ARSH: |
| 3515 | if (umax_val >= insn_bitness) { |
| 3516 | /* Shifts greater than 31 or 63 are undefined. |
| 3517 | * This includes shifts by a negative number. |
| 3518 | */ |
| 3519 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 3520 | break; |
| 3521 | } |
| 3522 | |
| 3523 | /* Upon reaching here, src_known is true and |
| 3524 | * umax_val is equal to umin_val. |
| 3525 | */ |
| 3526 | dst_reg->smin_value >>= umin_val; |
| 3527 | dst_reg->smax_value >>= umin_val; |
| 3528 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val); |
| 3529 | |
| 3530 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 3531 | * dst_reg var_off to refine the result. |
| 3532 | */ |
| 3533 | dst_reg->umin_value = 0; |
| 3534 | dst_reg->umax_value = U64_MAX; |
| 3535 | __update_reg_bounds(dst_reg); |
| 3536 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3537 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3538 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3539 | break; |
| 3540 | } |
| 3541 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3542 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3543 | /* 32-bit ALU ops are (32,32)->32 */ |
| 3544 | coerce_reg_to_size(dst_reg, 4); |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3545 | } |
| 3546 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3547 | __reg_deduce_bounds(dst_reg); |
| 3548 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3549 | return 0; |
| 3550 | } |
| 3551 | |
| 3552 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 3553 | * and var_off. |
| 3554 | */ |
| 3555 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 3556 | struct bpf_insn *insn) |
| 3557 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3558 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3559 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 3560 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3561 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 3562 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3563 | |
| 3564 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3565 | src_reg = NULL; |
| 3566 | if (dst_reg->type != SCALAR_VALUE) |
| 3567 | ptr_reg = dst_reg; |
| 3568 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3569 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3570 | if (src_reg->type != SCALAR_VALUE) { |
| 3571 | if (dst_reg->type != SCALAR_VALUE) { |
| 3572 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3573 | * an arbitrary scalar. Disallow all math except |
| 3574 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3575 | */ |
Alexei Starovoitov | dd06682 | 2018-09-12 14:06:10 -0700 | [diff] [blame] | 3576 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3577 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 3578 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3579 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3580 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 3581 | insn->dst_reg, |
| 3582 | bpf_alu_string[opcode >> 4]); |
| 3583 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3584 | } else { |
| 3585 | /* scalar += pointer |
| 3586 | * This is legal, but we have to reverse our |
| 3587 | * src/dest handling in computing the range |
| 3588 | */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3589 | return adjust_ptr_min_max_vals(env, insn, |
| 3590 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3591 | } |
| 3592 | } else if (ptr_reg) { |
| 3593 | /* pointer += scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3594 | return adjust_ptr_min_max_vals(env, insn, |
| 3595 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3596 | } |
| 3597 | } else { |
| 3598 | /* Pretend the src is a reg with a known value, since we only |
| 3599 | * need to be able to read from this state. |
| 3600 | */ |
| 3601 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3602 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3603 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3604 | if (ptr_reg) /* pointer += K */ |
| 3605 | return adjust_ptr_min_max_vals(env, insn, |
| 3606 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | /* Got here implies adding two SCALAR_VALUEs */ |
| 3610 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3611 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3612 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3613 | return -EINVAL; |
| 3614 | } |
| 3615 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3616 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3617 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3618 | return -EINVAL; |
| 3619 | } |
| 3620 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3621 | } |
| 3622 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3623 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3624 | 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] | 3625 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3626 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3627 | u8 opcode = BPF_OP(insn->code); |
| 3628 | int err; |
| 3629 | |
| 3630 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 3631 | if (opcode == BPF_NEG) { |
| 3632 | if (BPF_SRC(insn->code) != 0 || |
| 3633 | insn->src_reg != BPF_REG_0 || |
| 3634 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3635 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3636 | return -EINVAL; |
| 3637 | } |
| 3638 | } else { |
| 3639 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 3640 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 3641 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3642 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3643 | return -EINVAL; |
| 3644 | } |
| 3645 | } |
| 3646 | |
| 3647 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3648 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3649 | if (err) |
| 3650 | return err; |
| 3651 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3652 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3653 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3654 | insn->dst_reg); |
| 3655 | return -EACCES; |
| 3656 | } |
| 3657 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3658 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3659 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3660 | if (err) |
| 3661 | return err; |
| 3662 | |
| 3663 | } else if (opcode == BPF_MOV) { |
| 3664 | |
| 3665 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3666 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3667 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3668 | return -EINVAL; |
| 3669 | } |
| 3670 | |
| 3671 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3672 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3673 | if (err) |
| 3674 | return err; |
| 3675 | } else { |
| 3676 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3677 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3678 | return -EINVAL; |
| 3679 | } |
| 3680 | } |
| 3681 | |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 3682 | /* check dest operand, mark as required later */ |
| 3683 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3684 | if (err) |
| 3685 | return err; |
| 3686 | |
| 3687 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 3688 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
| 3689 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; |
| 3690 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3691 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 3692 | /* case: R1 = R2 |
| 3693 | * copy register state to dest reg |
| 3694 | */ |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 3695 | *dst_reg = *src_reg; |
| 3696 | dst_reg->live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3697 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3698 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3699 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3700 | verbose(env, |
| 3701 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3702 | insn->src_reg); |
| 3703 | return -EACCES; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 3704 | } else if (src_reg->type == SCALAR_VALUE) { |
| 3705 | *dst_reg = *src_reg; |
| 3706 | dst_reg->live |= REG_LIVE_WRITTEN; |
| 3707 | } else { |
| 3708 | mark_reg_unknown(env, regs, |
| 3709 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3710 | } |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 3711 | coerce_reg_to_size(dst_reg, 4); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3712 | } |
| 3713 | } else { |
| 3714 | /* case: R = imm |
| 3715 | * remember the value we stored into this reg |
| 3716 | */ |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 3717 | /* clear any state __mark_reg_known doesn't set */ |
| 3718 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3719 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 3720 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 3721 | __mark_reg_known(regs + insn->dst_reg, |
| 3722 | insn->imm); |
| 3723 | } else { |
| 3724 | __mark_reg_known(regs + insn->dst_reg, |
| 3725 | (u32)insn->imm); |
| 3726 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3730 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3731 | return -EINVAL; |
| 3732 | |
| 3733 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 3734 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3735 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3736 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3737 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3738 | return -EINVAL; |
| 3739 | } |
| 3740 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3741 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3742 | if (err) |
| 3743 | return err; |
| 3744 | } else { |
| 3745 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3746 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3747 | return -EINVAL; |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3752 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3753 | if (err) |
| 3754 | return err; |
| 3755 | |
| 3756 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 3757 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3758 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3759 | return -EINVAL; |
| 3760 | } |
| 3761 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3762 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 3763 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 3764 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 3765 | |
| 3766 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3767 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3768 | return -EINVAL; |
| 3769 | } |
| 3770 | } |
| 3771 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3772 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3773 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3774 | if (err) |
| 3775 | return err; |
| 3776 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3777 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3778 | } |
| 3779 | |
| 3780 | return 0; |
| 3781 | } |
| 3782 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3783 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3784 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 3785 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3786 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3787 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3788 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3789 | struct bpf_reg_state *regs = state->regs, *reg; |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3790 | u16 new_range; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3791 | int i, j; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3792 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3793 | if (dst_reg->off < 0 || |
| 3794 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3795 | /* This doesn't give us any range */ |
| 3796 | return; |
| 3797 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3798 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 3799 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3800 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 3801 | * than pkt_end, but that's because it's also less than pkt. |
| 3802 | */ |
| 3803 | return; |
| 3804 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3805 | new_range = dst_reg->off; |
| 3806 | if (range_right_open) |
| 3807 | new_range--; |
| 3808 | |
| 3809 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3810 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3811 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3812 | * |
| 3813 | * r2 = r3; |
| 3814 | * r2 += 8; |
| 3815 | * if (r2 > pkt_end) goto <handle exception> |
| 3816 | * <access okay> |
| 3817 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3818 | * r2 = r3; |
| 3819 | * r2 += 8; |
| 3820 | * if (r2 < pkt_end) goto <access okay> |
| 3821 | * <handle exception> |
| 3822 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3823 | * Where: |
| 3824 | * r2 == dst_reg, pkt_end == src_reg |
| 3825 | * r2=pkt(id=n,off=8,r=0) |
| 3826 | * r3=pkt(id=n,off=0,r=0) |
| 3827 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3828 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3829 | * |
| 3830 | * r2 = r3; |
| 3831 | * r2 += 8; |
| 3832 | * if (pkt_end >= r2) goto <access okay> |
| 3833 | * <handle exception> |
| 3834 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3835 | * r2 = r3; |
| 3836 | * r2 += 8; |
| 3837 | * if (pkt_end <= r2) goto <handle exception> |
| 3838 | * <access okay> |
| 3839 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3840 | * Where: |
| 3841 | * pkt_end == dst_reg, r2 == src_reg |
| 3842 | * r2=pkt(id=n,off=8,r=0) |
| 3843 | * r3=pkt(id=n,off=0,r=0) |
| 3844 | * |
| 3845 | * 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] | 3846 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 3847 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 3848 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3849 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3850 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3851 | /* If our ids match, then we must have the same max_value. And we |
| 3852 | * don't care about the other reg's fixed offset, since if it's too big |
| 3853 | * the range won't allow anything. |
| 3854 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 3855 | */ |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3856 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3857 | if (regs[i].type == type && regs[i].id == dst_reg->id) |
Alexei Starovoitov | b197768 | 2017-03-24 15:57:33 -0700 | [diff] [blame] | 3858 | /* keep the maximum range already checked */ |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3859 | regs[i].range = max(regs[i].range, new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3860 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3861 | for (j = 0; j <= vstate->curframe; j++) { |
| 3862 | state = vstate->frame[j]; |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 3863 | bpf_for_each_spilled_reg(i, state, reg) { |
| 3864 | if (!reg) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3865 | continue; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3866 | if (reg->type == type && reg->id == dst_reg->id) |
| 3867 | reg->range = max(reg->range, new_range); |
| 3868 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3869 | } |
| 3870 | } |
| 3871 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 3872 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
| 3873 | * and return: |
| 3874 | * 1 - branch will be taken and "goto target" will be executed |
| 3875 | * 0 - branch will not be taken and fall-through to next insn |
| 3876 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10] |
| 3877 | */ |
| 3878 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) |
| 3879 | { |
| 3880 | if (__is_pointer_value(false, reg)) |
| 3881 | return -1; |
| 3882 | |
| 3883 | switch (opcode) { |
| 3884 | case BPF_JEQ: |
| 3885 | if (tnum_is_const(reg->var_off)) |
| 3886 | return !!tnum_equals_const(reg->var_off, val); |
| 3887 | break; |
| 3888 | case BPF_JNE: |
| 3889 | if (tnum_is_const(reg->var_off)) |
| 3890 | return !tnum_equals_const(reg->var_off, val); |
| 3891 | break; |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 3892 | case BPF_JSET: |
| 3893 | if ((~reg->var_off.mask & reg->var_off.value) & val) |
| 3894 | return 1; |
| 3895 | if (!((reg->var_off.mask | reg->var_off.value) & val)) |
| 3896 | return 0; |
| 3897 | break; |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 3898 | case BPF_JGT: |
| 3899 | if (reg->umin_value > val) |
| 3900 | return 1; |
| 3901 | else if (reg->umax_value <= val) |
| 3902 | return 0; |
| 3903 | break; |
| 3904 | case BPF_JSGT: |
| 3905 | if (reg->smin_value > (s64)val) |
| 3906 | return 1; |
| 3907 | else if (reg->smax_value < (s64)val) |
| 3908 | return 0; |
| 3909 | break; |
| 3910 | case BPF_JLT: |
| 3911 | if (reg->umax_value < val) |
| 3912 | return 1; |
| 3913 | else if (reg->umin_value >= val) |
| 3914 | return 0; |
| 3915 | break; |
| 3916 | case BPF_JSLT: |
| 3917 | if (reg->smax_value < (s64)val) |
| 3918 | return 1; |
| 3919 | else if (reg->smin_value >= (s64)val) |
| 3920 | return 0; |
| 3921 | break; |
| 3922 | case BPF_JGE: |
| 3923 | if (reg->umin_value >= val) |
| 3924 | return 1; |
| 3925 | else if (reg->umax_value < val) |
| 3926 | return 0; |
| 3927 | break; |
| 3928 | case BPF_JSGE: |
| 3929 | if (reg->smin_value >= (s64)val) |
| 3930 | return 1; |
| 3931 | else if (reg->smax_value < (s64)val) |
| 3932 | return 0; |
| 3933 | break; |
| 3934 | case BPF_JLE: |
| 3935 | if (reg->umax_value <= val) |
| 3936 | return 1; |
| 3937 | else if (reg->umin_value > val) |
| 3938 | return 0; |
| 3939 | break; |
| 3940 | case BPF_JSLE: |
| 3941 | if (reg->smax_value <= (s64)val) |
| 3942 | return 1; |
| 3943 | else if (reg->smin_value > (s64)val) |
| 3944 | return 0; |
| 3945 | break; |
| 3946 | } |
| 3947 | |
| 3948 | return -1; |
| 3949 | } |
| 3950 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3951 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 3952 | * variable register that we are working on, and src_reg is a constant or we're |
| 3953 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3954 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3955 | */ |
| 3956 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 3957 | struct bpf_reg_state *false_reg, u64 val, |
| 3958 | u8 opcode) |
| 3959 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3960 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 3961 | * variable offset from the compare (unless src_reg were a pointer into |
| 3962 | * the same object, but we don't bother with that. |
| 3963 | * Since false_reg and true_reg have the same type by construction, we |
| 3964 | * only need to check one of them for pointerness. |
| 3965 | */ |
| 3966 | if (__is_pointer_value(false, false_reg)) |
| 3967 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3968 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3969 | switch (opcode) { |
| 3970 | case BPF_JEQ: |
| 3971 | /* If this is false then we know nothing Jon Snow, but if it is |
| 3972 | * true then we know for sure. |
| 3973 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3974 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3975 | break; |
| 3976 | case BPF_JNE: |
| 3977 | /* If this is true we know nothing Jon Snow, but if it is false |
| 3978 | * we know the value for sure; |
| 3979 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3980 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3981 | break; |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 3982 | case BPF_JSET: |
| 3983 | false_reg->var_off = tnum_and(false_reg->var_off, |
| 3984 | tnum_const(~val)); |
| 3985 | if (is_power_of_2(val)) |
| 3986 | true_reg->var_off = tnum_or(true_reg->var_off, |
| 3987 | tnum_const(val)); |
| 3988 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3989 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3990 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 3991 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 3992 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3993 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3994 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 3995 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3996 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3997 | case BPF_JLT: |
| 3998 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 3999 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 4000 | break; |
| 4001 | case BPF_JSLT: |
| 4002 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
| 4003 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 4004 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4005 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4006 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 4007 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 4008 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4009 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4010 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 4011 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4012 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4013 | case BPF_JLE: |
| 4014 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 4015 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 4016 | break; |
| 4017 | case BPF_JSLE: |
| 4018 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); |
| 4019 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 4020 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4021 | default: |
| 4022 | break; |
| 4023 | } |
| 4024 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4025 | __reg_deduce_bounds(false_reg); |
| 4026 | __reg_deduce_bounds(true_reg); |
| 4027 | /* We might have learned some bits from the bounds. */ |
| 4028 | __reg_bound_offset(false_reg); |
| 4029 | __reg_bound_offset(true_reg); |
| 4030 | /* Intersecting with the old var_off might have improved our bounds |
| 4031 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 4032 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 4033 | */ |
| 4034 | __update_reg_bounds(false_reg); |
| 4035 | __update_reg_bounds(true_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4036 | } |
| 4037 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4038 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 4039 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4040 | */ |
| 4041 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 4042 | struct bpf_reg_state *false_reg, u64 val, |
| 4043 | u8 opcode) |
| 4044 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4045 | if (__is_pointer_value(false, false_reg)) |
| 4046 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 4047 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4048 | switch (opcode) { |
| 4049 | case BPF_JEQ: |
| 4050 | /* If this is false then we know nothing Jon Snow, but if it is |
| 4051 | * true then we know for sure. |
| 4052 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4053 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4054 | break; |
| 4055 | case BPF_JNE: |
| 4056 | /* If this is true we know nothing Jon Snow, but if it is false |
| 4057 | * we know the value for sure; |
| 4058 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4059 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4060 | break; |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 4061 | case BPF_JSET: |
| 4062 | false_reg->var_off = tnum_and(false_reg->var_off, |
| 4063 | tnum_const(~val)); |
| 4064 | if (is_power_of_2(val)) |
| 4065 | true_reg->var_off = tnum_or(true_reg->var_off, |
| 4066 | tnum_const(val)); |
| 4067 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4068 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4069 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 4070 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 4071 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4072 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4073 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 4074 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4075 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4076 | case BPF_JLT: |
| 4077 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 4078 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 4079 | break; |
| 4080 | case BPF_JSLT: |
| 4081 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); |
| 4082 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 4083 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4084 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4085 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 4086 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 4087 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4088 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4089 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 4090 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4091 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4092 | case BPF_JLE: |
| 4093 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 4094 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 4095 | break; |
| 4096 | case BPF_JSLE: |
| 4097 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
| 4098 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 4099 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4100 | default: |
| 4101 | break; |
| 4102 | } |
| 4103 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4104 | __reg_deduce_bounds(false_reg); |
| 4105 | __reg_deduce_bounds(true_reg); |
| 4106 | /* We might have learned some bits from the bounds. */ |
| 4107 | __reg_bound_offset(false_reg); |
| 4108 | __reg_bound_offset(true_reg); |
| 4109 | /* Intersecting with the old var_off might have improved our bounds |
| 4110 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 4111 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 4112 | */ |
| 4113 | __update_reg_bounds(false_reg); |
| 4114 | __update_reg_bounds(true_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 4118 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 4119 | struct bpf_reg_state *dst_reg) |
| 4120 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4121 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 4122 | dst_reg->umin_value); |
| 4123 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 4124 | dst_reg->umax_value); |
| 4125 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 4126 | dst_reg->smin_value); |
| 4127 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 4128 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4129 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 4130 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4131 | /* We might have learned new bounds from the var_off. */ |
| 4132 | __update_reg_bounds(src_reg); |
| 4133 | __update_reg_bounds(dst_reg); |
| 4134 | /* We might have learned something about the sign bit. */ |
| 4135 | __reg_deduce_bounds(src_reg); |
| 4136 | __reg_deduce_bounds(dst_reg); |
| 4137 | /* We might have learned some bits from the bounds. */ |
| 4138 | __reg_bound_offset(src_reg); |
| 4139 | __reg_bound_offset(dst_reg); |
| 4140 | /* Intersecting with the old var_off might have improved our bounds |
| 4141 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 4142 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 4143 | */ |
| 4144 | __update_reg_bounds(src_reg); |
| 4145 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4146 | } |
| 4147 | |
| 4148 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 4149 | struct bpf_reg_state *true_dst, |
| 4150 | struct bpf_reg_state *false_src, |
| 4151 | struct bpf_reg_state *false_dst, |
| 4152 | u8 opcode) |
| 4153 | { |
| 4154 | switch (opcode) { |
| 4155 | case BPF_JEQ: |
| 4156 | __reg_combine_min_max(true_src, true_dst); |
| 4157 | break; |
| 4158 | case BPF_JNE: |
| 4159 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4160 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 4161 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4162 | } |
| 4163 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4164 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 4165 | struct bpf_reg_state *reg, u32 id, |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4166 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4167 | { |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4168 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4169 | /* Old offset (both fixed and variable parts) should |
| 4170 | * have been known-zero, because we don't allow pointer |
| 4171 | * arithmetic on pointers that might be NULL. |
| 4172 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4173 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 4174 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4175 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4176 | __mark_reg_known_zero(reg); |
| 4177 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4178 | } |
| 4179 | if (is_null) { |
| 4180 | reg->type = SCALAR_VALUE; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4181 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
| 4182 | if (reg->map_ptr->inner_map_meta) { |
| 4183 | reg->type = CONST_PTR_TO_MAP; |
| 4184 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
| 4185 | } else { |
| 4186 | reg->type = PTR_TO_MAP_VALUE; |
| 4187 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 4188 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
| 4189 | reg->type = PTR_TO_SOCKET; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4190 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4191 | if (is_null || !reg_is_refcounted(reg)) { |
| 4192 | /* We don't need id from this point onwards anymore, |
| 4193 | * thus we should better reset it, so that state |
| 4194 | * pruning has chances to take effect. |
| 4195 | */ |
| 4196 | reg->id = 0; |
| 4197 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4198 | } |
| 4199 | } |
| 4200 | |
| 4201 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 4202 | * be folded together at some point. |
| 4203 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4204 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 4205 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4206 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4207 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 4208 | struct bpf_reg_state *reg, *regs = state->regs; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 4209 | u32 id = regs[regno].id; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4210 | int i, j; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4211 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4212 | if (reg_is_refcounted_or_null(®s[regno]) && is_null) |
| 4213 | __release_reference_state(state, id); |
| 4214 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4215 | for (i = 0; i < MAX_BPF_REG; i++) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4216 | mark_ptr_or_null_reg(state, ®s[i], id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4217 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4218 | for (j = 0; j <= vstate->curframe; j++) { |
| 4219 | state = vstate->frame[j]; |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 4220 | bpf_for_each_spilled_reg(i, state, reg) { |
| 4221 | if (!reg) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4222 | continue; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4223 | mark_ptr_or_null_reg(state, reg, id, is_null); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4224 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4225 | } |
| 4226 | } |
| 4227 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 4228 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 4229 | struct bpf_reg_state *dst_reg, |
| 4230 | struct bpf_reg_state *src_reg, |
| 4231 | struct bpf_verifier_state *this_branch, |
| 4232 | struct bpf_verifier_state *other_branch) |
| 4233 | { |
| 4234 | if (BPF_SRC(insn->code) != BPF_X) |
| 4235 | return false; |
| 4236 | |
| 4237 | switch (BPF_OP(insn->code)) { |
| 4238 | case BPF_JGT: |
| 4239 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4240 | src_reg->type == PTR_TO_PACKET_END) || |
| 4241 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4242 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4243 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 4244 | find_good_pkt_pointers(this_branch, dst_reg, |
| 4245 | dst_reg->type, false); |
| 4246 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4247 | src_reg->type == PTR_TO_PACKET) || |
| 4248 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4249 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4250 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 4251 | find_good_pkt_pointers(other_branch, src_reg, |
| 4252 | src_reg->type, true); |
| 4253 | } else { |
| 4254 | return false; |
| 4255 | } |
| 4256 | break; |
| 4257 | case BPF_JLT: |
| 4258 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4259 | src_reg->type == PTR_TO_PACKET_END) || |
| 4260 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4261 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4262 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 4263 | find_good_pkt_pointers(other_branch, dst_reg, |
| 4264 | dst_reg->type, true); |
| 4265 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4266 | src_reg->type == PTR_TO_PACKET) || |
| 4267 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4268 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4269 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 4270 | find_good_pkt_pointers(this_branch, src_reg, |
| 4271 | src_reg->type, false); |
| 4272 | } else { |
| 4273 | return false; |
| 4274 | } |
| 4275 | break; |
| 4276 | case BPF_JGE: |
| 4277 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4278 | src_reg->type == PTR_TO_PACKET_END) || |
| 4279 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4280 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4281 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 4282 | find_good_pkt_pointers(this_branch, dst_reg, |
| 4283 | dst_reg->type, true); |
| 4284 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4285 | src_reg->type == PTR_TO_PACKET) || |
| 4286 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4287 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4288 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 4289 | find_good_pkt_pointers(other_branch, src_reg, |
| 4290 | src_reg->type, false); |
| 4291 | } else { |
| 4292 | return false; |
| 4293 | } |
| 4294 | break; |
| 4295 | case BPF_JLE: |
| 4296 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4297 | src_reg->type == PTR_TO_PACKET_END) || |
| 4298 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4299 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4300 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 4301 | find_good_pkt_pointers(other_branch, dst_reg, |
| 4302 | dst_reg->type, false); |
| 4303 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4304 | src_reg->type == PTR_TO_PACKET) || |
| 4305 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4306 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4307 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 4308 | find_good_pkt_pointers(this_branch, src_reg, |
| 4309 | src_reg->type, true); |
| 4310 | } else { |
| 4311 | return false; |
| 4312 | } |
| 4313 | break; |
| 4314 | default: |
| 4315 | return false; |
| 4316 | } |
| 4317 | |
| 4318 | return true; |
| 4319 | } |
| 4320 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4321 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4322 | struct bpf_insn *insn, int *insn_idx) |
| 4323 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4324 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 4325 | struct bpf_verifier_state *other_branch; |
| 4326 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
| 4327 | struct bpf_reg_state *dst_reg, *other_branch_regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4328 | u8 opcode = BPF_OP(insn->code); |
| 4329 | int err; |
| 4330 | |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4331 | if (opcode > BPF_JSLE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4332 | verbose(env, "invalid BPF_JMP opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4333 | return -EINVAL; |
| 4334 | } |
| 4335 | |
| 4336 | if (BPF_SRC(insn->code) == BPF_X) { |
| 4337 | if (insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4338 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4339 | return -EINVAL; |
| 4340 | } |
| 4341 | |
| 4342 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4343 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4344 | if (err) |
| 4345 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4346 | |
| 4347 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4348 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4349 | insn->src_reg); |
| 4350 | return -EACCES; |
| 4351 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4352 | } else { |
| 4353 | if (insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4354 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4355 | return -EINVAL; |
| 4356 | } |
| 4357 | } |
| 4358 | |
| 4359 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4360 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4361 | if (err) |
| 4362 | return err; |
| 4363 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4364 | dst_reg = ®s[insn->dst_reg]; |
| 4365 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4366 | if (BPF_SRC(insn->code) == BPF_K) { |
| 4367 | int pred = is_branch_taken(dst_reg, insn->imm, opcode); |
| 4368 | |
| 4369 | if (pred == 1) { |
| 4370 | /* only follow the goto, ignore fall-through */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4371 | *insn_idx += insn->off; |
| 4372 | return 0; |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 4373 | } else if (pred == 0) { |
| 4374 | /* only follow fall-through branch, since |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4375 | * that's where the program will go |
| 4376 | */ |
| 4377 | return 0; |
| 4378 | } |
| 4379 | } |
| 4380 | |
| 4381 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); |
| 4382 | if (!other_branch) |
| 4383 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4384 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4385 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4386 | /* detect if we are comparing against a constant value so we can adjust |
| 4387 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4388 | * this is only legit if both are scalars (or pointers to the same |
| 4389 | * object, I suppose, but we don't support that right now), because |
| 4390 | * otherwise the different base pointers mean the offsets aren't |
| 4391 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4392 | */ |
| 4393 | if (BPF_SRC(insn->code) == BPF_X) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4394 | if (dst_reg->type == SCALAR_VALUE && |
| 4395 | regs[insn->src_reg].type == SCALAR_VALUE) { |
| 4396 | if (tnum_is_const(regs[insn->src_reg].var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4397 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4398 | dst_reg, regs[insn->src_reg].var_off.value, |
| 4399 | opcode); |
| 4400 | else if (tnum_is_const(dst_reg->var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4401 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4402 | ®s[insn->src_reg], |
| 4403 | dst_reg->var_off.value, opcode); |
| 4404 | else if (opcode == BPF_JEQ || opcode == BPF_JNE) |
| 4405 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4406 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 4407 | &other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4408 | ®s[insn->src_reg], |
| 4409 | ®s[insn->dst_reg], opcode); |
| 4410 | } |
| 4411 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4412 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4413 | dst_reg, insn->imm, opcode); |
| 4414 | } |
| 4415 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4416 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem() */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4417 | if (BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4418 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4419 | reg_type_may_be_null(dst_reg->type)) { |
| 4420 | /* Mark all identical registers in each branch as either |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4421 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 4422 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4423 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 4424 | opcode == BPF_JNE); |
| 4425 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 4426 | opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 4427 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 4428 | this_branch, other_branch) && |
| 4429 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4430 | verbose(env, "R%d pointer comparison prohibited\n", |
| 4431 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4432 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4433 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4434 | if (env->log.level) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4435 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4436 | return 0; |
| 4437 | } |
| 4438 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4439 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
| 4440 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) |
| 4441 | { |
| 4442 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; |
| 4443 | |
| 4444 | return (struct bpf_map *) (unsigned long) imm64; |
| 4445 | } |
| 4446 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4447 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4448 | 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] | 4449 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4450 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4451 | int err; |
| 4452 | |
| 4453 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4454 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4455 | return -EINVAL; |
| 4456 | } |
| 4457 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4458 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4459 | return -EINVAL; |
| 4460 | } |
| 4461 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4462 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4463 | if (err) |
| 4464 | return err; |
| 4465 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 4466 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 4467 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 4468 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4469 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4470 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4471 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 4472 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4473 | |
| 4474 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ |
| 4475 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); |
| 4476 | |
| 4477 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 4478 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); |
| 4479 | return 0; |
| 4480 | } |
| 4481 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 4482 | static bool may_access_skb(enum bpf_prog_type type) |
| 4483 | { |
| 4484 | switch (type) { |
| 4485 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 4486 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 4487 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 4488 | return true; |
| 4489 | default: |
| 4490 | return false; |
| 4491 | } |
| 4492 | } |
| 4493 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4494 | /* verify safety of LD_ABS|LD_IND instructions: |
| 4495 | * - they can only appear in the programs where ctx == skb |
| 4496 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 4497 | * preserve R6-R9, and store return value into R0 |
| 4498 | * |
| 4499 | * Implicit input: |
| 4500 | * ctx == skb == R6 == CTX |
| 4501 | * |
| 4502 | * Explicit input: |
| 4503 | * SRC == any register |
| 4504 | * IMM == 32-bit immediate |
| 4505 | * |
| 4506 | * Output: |
| 4507 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 4508 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4509 | 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] | 4510 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4511 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4512 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4513 | int i, err; |
| 4514 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 4515 | if (!may_access_skb(env->prog->type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4516 | 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] | 4517 | return -EINVAL; |
| 4518 | } |
| 4519 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 4520 | if (!env->ops->gen_ld_abs) { |
| 4521 | verbose(env, "bpf verifier is misconfigured\n"); |
| 4522 | return -EINVAL; |
| 4523 | } |
| 4524 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 4525 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4526 | /* when program has LD_ABS insn JITs and interpreter assume |
| 4527 | * that r1 == ctx == skb which is not the case for callees |
| 4528 | * that can have arbitrary arguments. It's problematic |
| 4529 | * for main prog as well since JITs would need to analyze |
| 4530 | * all functions in order to make proper register save/restore |
| 4531 | * decisions in the main prog. Hence disallow LD_ABS with calls |
| 4532 | */ |
| 4533 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); |
| 4534 | return -EINVAL; |
| 4535 | } |
| 4536 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4537 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 4538 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4539 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4540 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4541 | return -EINVAL; |
| 4542 | } |
| 4543 | |
| 4544 | /* check whether implicit source operand (register R6) is readable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4545 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4546 | if (err) |
| 4547 | return err; |
| 4548 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4549 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 4550 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 4551 | * reference leak. |
| 4552 | */ |
| 4553 | err = check_reference_leak(env); |
| 4554 | if (err) { |
| 4555 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 4556 | return err; |
| 4557 | } |
| 4558 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4559 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4560 | verbose(env, |
| 4561 | "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] | 4562 | return -EINVAL; |
| 4563 | } |
| 4564 | |
| 4565 | if (mode == BPF_IND) { |
| 4566 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4567 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4568 | if (err) |
| 4569 | return err; |
| 4570 | } |
| 4571 | |
| 4572 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4573 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4574 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4575 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 4576 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4577 | |
| 4578 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4579 | * the value fetched from the packet. |
| 4580 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4581 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4582 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4583 | return 0; |
| 4584 | } |
| 4585 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4586 | static int check_return_code(struct bpf_verifier_env *env) |
| 4587 | { |
| 4588 | struct bpf_reg_state *reg; |
| 4589 | struct tnum range = tnum_range(0, 1); |
| 4590 | |
| 4591 | switch (env->prog->type) { |
| 4592 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 4593 | case BPF_PROG_TYPE_CGROUP_SOCK: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 4594 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4595 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 4596 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4597 | break; |
| 4598 | default: |
| 4599 | return 0; |
| 4600 | } |
| 4601 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4602 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4603 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4604 | 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] | 4605 | reg_type_str[reg->type]); |
| 4606 | return -EINVAL; |
| 4607 | } |
| 4608 | |
| 4609 | if (!tnum_in(range, reg->var_off)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4610 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4611 | if (!tnum_is_unknown(reg->var_off)) { |
| 4612 | char tn_buf[48]; |
| 4613 | |
| 4614 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4615 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4616 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4617 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4618 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4619 | verbose(env, " should have been 0 or 1\n"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4620 | return -EINVAL; |
| 4621 | } |
| 4622 | return 0; |
| 4623 | } |
| 4624 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4625 | /* non-recursive DFS pseudo code |
| 4626 | * 1 procedure DFS-iterative(G,v): |
| 4627 | * 2 label v as discovered |
| 4628 | * 3 let S be a stack |
| 4629 | * 4 S.push(v) |
| 4630 | * 5 while S is not empty |
| 4631 | * 6 t <- S.pop() |
| 4632 | * 7 if t is what we're looking for: |
| 4633 | * 8 return t |
| 4634 | * 9 for all edges e in G.adjacentEdges(t) do |
| 4635 | * 10 if edge e is already labelled |
| 4636 | * 11 continue with the next edge |
| 4637 | * 12 w <- G.adjacentVertex(t,e) |
| 4638 | * 13 if vertex w is not discovered and not explored |
| 4639 | * 14 label e as tree-edge |
| 4640 | * 15 label w as discovered |
| 4641 | * 16 S.push(w) |
| 4642 | * 17 continue at 5 |
| 4643 | * 18 else if vertex w is discovered |
| 4644 | * 19 label e as back-edge |
| 4645 | * 20 else |
| 4646 | * 21 // vertex w is explored |
| 4647 | * 22 label e as forward- or cross-edge |
| 4648 | * 23 label t as explored |
| 4649 | * 24 S.pop() |
| 4650 | * |
| 4651 | * convention: |
| 4652 | * 0x10 - discovered |
| 4653 | * 0x11 - discovered and fall-through edge labelled |
| 4654 | * 0x12 - discovered and fall-through and branch edges labelled |
| 4655 | * 0x20 - explored |
| 4656 | */ |
| 4657 | |
| 4658 | enum { |
| 4659 | DISCOVERED = 0x10, |
| 4660 | EXPLORED = 0x20, |
| 4661 | FALLTHROUGH = 1, |
| 4662 | BRANCH = 2, |
| 4663 | }; |
| 4664 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4665 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4666 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4667 | static int *insn_stack; /* stack of insns to process */ |
| 4668 | static int cur_stack; /* current stack index */ |
| 4669 | static int *insn_state; |
| 4670 | |
| 4671 | /* t, w, e - match pseudo-code above: |
| 4672 | * t - index of current instruction |
| 4673 | * w - next instruction |
| 4674 | * e - edge |
| 4675 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4676 | 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] | 4677 | { |
| 4678 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 4679 | return 0; |
| 4680 | |
| 4681 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 4682 | return 0; |
| 4683 | |
| 4684 | if (w < 0 || w >= env->prog->len) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 4685 | verbose_linfo(env, t, "%d: ", t); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4686 | 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] | 4687 | return -EINVAL; |
| 4688 | } |
| 4689 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4690 | if (e == BRANCH) |
| 4691 | /* mark branch target for state pruning */ |
| 4692 | env->explored_states[w] = STATE_LIST_MARK; |
| 4693 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4694 | if (insn_state[w] == 0) { |
| 4695 | /* tree-edge */ |
| 4696 | insn_state[t] = DISCOVERED | e; |
| 4697 | insn_state[w] = DISCOVERED; |
| 4698 | if (cur_stack >= env->prog->len) |
| 4699 | return -E2BIG; |
| 4700 | insn_stack[cur_stack++] = w; |
| 4701 | return 1; |
| 4702 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 4703 | verbose_linfo(env, t, "%d: ", t); |
| 4704 | verbose_linfo(env, w, "%d: ", w); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4705 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4706 | return -EINVAL; |
| 4707 | } else if (insn_state[w] == EXPLORED) { |
| 4708 | /* forward- or cross-edge */ |
| 4709 | insn_state[t] = DISCOVERED | e; |
| 4710 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4711 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4712 | return -EFAULT; |
| 4713 | } |
| 4714 | return 0; |
| 4715 | } |
| 4716 | |
| 4717 | /* non-recursive depth-first-search to detect loops in BPF program |
| 4718 | * loop == back-edge in directed graph |
| 4719 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4720 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4721 | { |
| 4722 | struct bpf_insn *insns = env->prog->insnsi; |
| 4723 | int insn_cnt = env->prog->len; |
| 4724 | int ret = 0; |
| 4725 | int i, t; |
| 4726 | |
| 4727 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 4728 | if (!insn_state) |
| 4729 | return -ENOMEM; |
| 4730 | |
| 4731 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 4732 | if (!insn_stack) { |
| 4733 | kfree(insn_state); |
| 4734 | return -ENOMEM; |
| 4735 | } |
| 4736 | |
| 4737 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 4738 | insn_stack[0] = 0; /* 0 is the first instruction */ |
| 4739 | cur_stack = 1; |
| 4740 | |
| 4741 | peek_stack: |
| 4742 | if (cur_stack == 0) |
| 4743 | goto check_state; |
| 4744 | t = insn_stack[cur_stack - 1]; |
| 4745 | |
| 4746 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { |
| 4747 | u8 opcode = BPF_OP(insns[t].code); |
| 4748 | |
| 4749 | if (opcode == BPF_EXIT) { |
| 4750 | goto mark_explored; |
| 4751 | } else if (opcode == BPF_CALL) { |
| 4752 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4753 | if (ret == 1) |
| 4754 | goto peek_stack; |
| 4755 | else if (ret < 0) |
| 4756 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 4757 | if (t + 1 < insn_cnt) |
| 4758 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 4759 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
| 4760 | env->explored_states[t] = STATE_LIST_MARK; |
| 4761 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); |
| 4762 | if (ret == 1) |
| 4763 | goto peek_stack; |
| 4764 | else if (ret < 0) |
| 4765 | goto err_free; |
| 4766 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4767 | } else if (opcode == BPF_JA) { |
| 4768 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 4769 | ret = -EINVAL; |
| 4770 | goto err_free; |
| 4771 | } |
| 4772 | /* unconditional jump with single edge */ |
| 4773 | ret = push_insn(t, t + insns[t].off + 1, |
| 4774 | FALLTHROUGH, env); |
| 4775 | if (ret == 1) |
| 4776 | goto peek_stack; |
| 4777 | else if (ret < 0) |
| 4778 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4779 | /* tell verifier to check for equivalent states |
| 4780 | * after every call and jump |
| 4781 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 4782 | if (t + 1 < insn_cnt) |
| 4783 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4784 | } else { |
| 4785 | /* conditional jump with two edges */ |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 4786 | env->explored_states[t] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4787 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4788 | if (ret == 1) |
| 4789 | goto peek_stack; |
| 4790 | else if (ret < 0) |
| 4791 | goto err_free; |
| 4792 | |
| 4793 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 4794 | if (ret == 1) |
| 4795 | goto peek_stack; |
| 4796 | else if (ret < 0) |
| 4797 | goto err_free; |
| 4798 | } |
| 4799 | } else { |
| 4800 | /* all other non-branch instructions with single |
| 4801 | * fall-through edge |
| 4802 | */ |
| 4803 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4804 | if (ret == 1) |
| 4805 | goto peek_stack; |
| 4806 | else if (ret < 0) |
| 4807 | goto err_free; |
| 4808 | } |
| 4809 | |
| 4810 | mark_explored: |
| 4811 | insn_state[t] = EXPLORED; |
| 4812 | if (cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4813 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4814 | ret = -EFAULT; |
| 4815 | goto err_free; |
| 4816 | } |
| 4817 | goto peek_stack; |
| 4818 | |
| 4819 | check_state: |
| 4820 | for (i = 0; i < insn_cnt; i++) { |
| 4821 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4822 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4823 | ret = -EINVAL; |
| 4824 | goto err_free; |
| 4825 | } |
| 4826 | } |
| 4827 | ret = 0; /* cfg looks good */ |
| 4828 | |
| 4829 | err_free: |
| 4830 | kfree(insn_state); |
| 4831 | kfree(insn_stack); |
| 4832 | return ret; |
| 4833 | } |
| 4834 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4835 | /* The minimum supported BTF func info size */ |
| 4836 | #define MIN_BPF_FUNCINFO_SIZE 8 |
| 4837 | #define MAX_FUNCINFO_REC_SIZE 252 |
| 4838 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4839 | static int check_btf_func(struct bpf_verifier_env *env, |
| 4840 | const union bpf_attr *attr, |
| 4841 | union bpf_attr __user *uattr) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4842 | { |
| 4843 | u32 i, nfuncs, urec_size, min_size, prev_offset; |
| 4844 | u32 krec_size = sizeof(struct bpf_func_info); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4845 | struct bpf_func_info *krecord; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4846 | const struct btf_type *type; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4847 | struct bpf_prog *prog; |
| 4848 | const struct btf *btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4849 | void __user *urecord; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4850 | int ret = 0; |
| 4851 | |
| 4852 | nfuncs = attr->func_info_cnt; |
| 4853 | if (!nfuncs) |
| 4854 | return 0; |
| 4855 | |
| 4856 | if (nfuncs != env->subprog_cnt) { |
| 4857 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); |
| 4858 | return -EINVAL; |
| 4859 | } |
| 4860 | |
| 4861 | urec_size = attr->func_info_rec_size; |
| 4862 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || |
| 4863 | urec_size > MAX_FUNCINFO_REC_SIZE || |
| 4864 | urec_size % sizeof(u32)) { |
| 4865 | verbose(env, "invalid func info rec size %u\n", urec_size); |
| 4866 | return -EINVAL; |
| 4867 | } |
| 4868 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4869 | prog = env->prog; |
| 4870 | btf = prog->aux->btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4871 | |
| 4872 | urecord = u64_to_user_ptr(attr->func_info); |
| 4873 | min_size = min_t(u32, krec_size, urec_size); |
| 4874 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4875 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4876 | if (!krecord) |
| 4877 | return -ENOMEM; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4878 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4879 | for (i = 0; i < nfuncs; i++) { |
| 4880 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); |
| 4881 | if (ret) { |
| 4882 | if (ret == -E2BIG) { |
| 4883 | verbose(env, "nonzero tailing record in func info"); |
| 4884 | /* set the size kernel expects so loader can zero |
| 4885 | * out the rest of the record. |
| 4886 | */ |
| 4887 | if (put_user(min_size, &uattr->func_info_rec_size)) |
| 4888 | ret = -EFAULT; |
| 4889 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4890 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4891 | } |
| 4892 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4893 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4894 | ret = -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4895 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4896 | } |
| 4897 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4898 | /* check insn_off */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4899 | if (i == 0) { |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4900 | if (krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4901 | verbose(env, |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4902 | "nonzero insn_off %u for the first func info record", |
| 4903 | krecord[i].insn_off); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4904 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4905 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4906 | } |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4907 | } else if (krecord[i].insn_off <= prev_offset) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4908 | verbose(env, |
| 4909 | "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] | 4910 | krecord[i].insn_off, prev_offset); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4911 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4912 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4913 | } |
| 4914 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4915 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4916 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
| 4917 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4918 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4919 | } |
| 4920 | |
| 4921 | /* check type_id */ |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4922 | type = btf_type_by_id(btf, krecord[i].type_id); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4923 | if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) { |
| 4924 | verbose(env, "invalid type id %d in func info", |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4925 | krecord[i].type_id); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4926 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4927 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4928 | } |
| 4929 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4930 | prev_offset = krecord[i].insn_off; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4931 | urecord += urec_size; |
| 4932 | } |
| 4933 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4934 | prog->aux->func_info = krecord; |
| 4935 | prog->aux->func_info_cnt = nfuncs; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4936 | return 0; |
| 4937 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4938 | err_free: |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4939 | kvfree(krecord); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4940 | return ret; |
| 4941 | } |
| 4942 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4943 | static void adjust_btf_func(struct bpf_verifier_env *env) |
| 4944 | { |
| 4945 | int i; |
| 4946 | |
| 4947 | if (!env->prog->aux->func_info) |
| 4948 | return; |
| 4949 | |
| 4950 | for (i = 0; i < env->subprog_cnt; i++) |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4951 | 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] | 4952 | } |
| 4953 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4954 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
| 4955 | sizeof(((struct bpf_line_info *)(0))->line_col)) |
| 4956 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
| 4957 | |
| 4958 | static int check_btf_line(struct bpf_verifier_env *env, |
| 4959 | const union bpf_attr *attr, |
| 4960 | union bpf_attr __user *uattr) |
| 4961 | { |
| 4962 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; |
| 4963 | struct bpf_subprog_info *sub; |
| 4964 | struct bpf_line_info *linfo; |
| 4965 | struct bpf_prog *prog; |
| 4966 | const struct btf *btf; |
| 4967 | void __user *ulinfo; |
| 4968 | int err; |
| 4969 | |
| 4970 | nr_linfo = attr->line_info_cnt; |
| 4971 | if (!nr_linfo) |
| 4972 | return 0; |
| 4973 | |
| 4974 | rec_size = attr->line_info_rec_size; |
| 4975 | if (rec_size < MIN_BPF_LINEINFO_SIZE || |
| 4976 | rec_size > MAX_LINEINFO_REC_SIZE || |
| 4977 | rec_size & (sizeof(u32) - 1)) |
| 4978 | return -EINVAL; |
| 4979 | |
| 4980 | /* Need to zero it in case the userspace may |
| 4981 | * pass in a smaller bpf_line_info object. |
| 4982 | */ |
| 4983 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), |
| 4984 | GFP_KERNEL | __GFP_NOWARN); |
| 4985 | if (!linfo) |
| 4986 | return -ENOMEM; |
| 4987 | |
| 4988 | prog = env->prog; |
| 4989 | btf = prog->aux->btf; |
| 4990 | |
| 4991 | s = 0; |
| 4992 | sub = env->subprog_info; |
| 4993 | ulinfo = u64_to_user_ptr(attr->line_info); |
| 4994 | expected_size = sizeof(struct bpf_line_info); |
| 4995 | ncopy = min_t(u32, expected_size, rec_size); |
| 4996 | for (i = 0; i < nr_linfo; i++) { |
| 4997 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); |
| 4998 | if (err) { |
| 4999 | if (err == -E2BIG) { |
| 5000 | verbose(env, "nonzero tailing record in line_info"); |
| 5001 | if (put_user(expected_size, |
| 5002 | &uattr->line_info_rec_size)) |
| 5003 | err = -EFAULT; |
| 5004 | } |
| 5005 | goto err_free; |
| 5006 | } |
| 5007 | |
| 5008 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { |
| 5009 | err = -EFAULT; |
| 5010 | goto err_free; |
| 5011 | } |
| 5012 | |
| 5013 | /* |
| 5014 | * Check insn_off to ensure |
| 5015 | * 1) strictly increasing AND |
| 5016 | * 2) bounded by prog->len |
| 5017 | * |
| 5018 | * The linfo[0].insn_off == 0 check logically falls into |
| 5019 | * the later "missing bpf_line_info for func..." case |
| 5020 | * because the first linfo[0].insn_off must be the |
| 5021 | * first sub also and the first sub must have |
| 5022 | * subprog_info[0].start == 0. |
| 5023 | */ |
| 5024 | if ((i && linfo[i].insn_off <= prev_offset) || |
| 5025 | linfo[i].insn_off >= prog->len) { |
| 5026 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", |
| 5027 | i, linfo[i].insn_off, prev_offset, |
| 5028 | prog->len); |
| 5029 | err = -EINVAL; |
| 5030 | goto err_free; |
| 5031 | } |
| 5032 | |
Martin KaFai Lau | fdbaa0b | 2018-12-19 13:01:01 -0800 | [diff] [blame] | 5033 | if (!prog->insnsi[linfo[i].insn_off].code) { |
| 5034 | verbose(env, |
| 5035 | "Invalid insn code at line_info[%u].insn_off\n", |
| 5036 | i); |
| 5037 | err = -EINVAL; |
| 5038 | goto err_free; |
| 5039 | } |
| 5040 | |
Martin KaFai Lau | 23127b3 | 2018-12-13 10:41:46 -0800 | [diff] [blame] | 5041 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
| 5042 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 5043 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
| 5044 | err = -EINVAL; |
| 5045 | goto err_free; |
| 5046 | } |
| 5047 | |
| 5048 | if (s != env->subprog_cnt) { |
| 5049 | if (linfo[i].insn_off == sub[s].start) { |
| 5050 | sub[s].linfo_idx = i; |
| 5051 | s++; |
| 5052 | } else if (sub[s].start < linfo[i].insn_off) { |
| 5053 | verbose(env, "missing bpf_line_info for func#%u\n", s); |
| 5054 | err = -EINVAL; |
| 5055 | goto err_free; |
| 5056 | } |
| 5057 | } |
| 5058 | |
| 5059 | prev_offset = linfo[i].insn_off; |
| 5060 | ulinfo += rec_size; |
| 5061 | } |
| 5062 | |
| 5063 | if (s != env->subprog_cnt) { |
| 5064 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", |
| 5065 | env->subprog_cnt - s, s); |
| 5066 | err = -EINVAL; |
| 5067 | goto err_free; |
| 5068 | } |
| 5069 | |
| 5070 | prog->aux->linfo = linfo; |
| 5071 | prog->aux->nr_linfo = nr_linfo; |
| 5072 | |
| 5073 | return 0; |
| 5074 | |
| 5075 | err_free: |
| 5076 | kvfree(linfo); |
| 5077 | return err; |
| 5078 | } |
| 5079 | |
| 5080 | static int check_btf_info(struct bpf_verifier_env *env, |
| 5081 | const union bpf_attr *attr, |
| 5082 | union bpf_attr __user *uattr) |
| 5083 | { |
| 5084 | struct btf *btf; |
| 5085 | int err; |
| 5086 | |
| 5087 | if (!attr->func_info_cnt && !attr->line_info_cnt) |
| 5088 | return 0; |
| 5089 | |
| 5090 | btf = btf_get_by_fd(attr->prog_btf_fd); |
| 5091 | if (IS_ERR(btf)) |
| 5092 | return PTR_ERR(btf); |
| 5093 | env->prog->aux->btf = btf; |
| 5094 | |
| 5095 | err = check_btf_func(env, attr, uattr); |
| 5096 | if (err) |
| 5097 | return err; |
| 5098 | |
| 5099 | err = check_btf_line(env, attr, uattr); |
| 5100 | if (err) |
| 5101 | return err; |
| 5102 | |
| 5103 | return 0; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5104 | } |
| 5105 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5106 | /* check %cur's range satisfies %old's */ |
| 5107 | static bool range_within(struct bpf_reg_state *old, |
| 5108 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5109 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5110 | return old->umin_value <= cur->umin_value && |
| 5111 | old->umax_value >= cur->umax_value && |
| 5112 | old->smin_value <= cur->smin_value && |
| 5113 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5114 | } |
| 5115 | |
| 5116 | /* Maximum number of register states that can exist at once */ |
| 5117 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 5118 | struct idpair { |
| 5119 | u32 old; |
| 5120 | u32 cur; |
| 5121 | }; |
| 5122 | |
| 5123 | /* If in the old state two registers had the same id, then they need to have |
| 5124 | * the same id in the new state as well. But that id could be different from |
| 5125 | * the old state, so we need to track the mapping from old to new ids. |
| 5126 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 5127 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 5128 | * regs with a different old id could still have new id 9, we don't care about |
| 5129 | * that. |
| 5130 | * So we look through our idmap to see if this old id has been seen before. If |
| 5131 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 5132 | */ |
| 5133 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 5134 | { |
| 5135 | unsigned int i; |
| 5136 | |
| 5137 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 5138 | if (!idmap[i].old) { |
| 5139 | /* Reached an empty slot; haven't seen this id before */ |
| 5140 | idmap[i].old = old_id; |
| 5141 | idmap[i].cur = cur_id; |
| 5142 | return true; |
| 5143 | } |
| 5144 | if (idmap[i].old == old_id) |
| 5145 | return idmap[i].cur == cur_id; |
| 5146 | } |
| 5147 | /* We ran out of idmap slots, which should be impossible */ |
| 5148 | WARN_ON_ONCE(1); |
| 5149 | return false; |
| 5150 | } |
| 5151 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 5152 | static void clean_func_state(struct bpf_verifier_env *env, |
| 5153 | struct bpf_func_state *st) |
| 5154 | { |
| 5155 | enum bpf_reg_liveness live; |
| 5156 | int i, j; |
| 5157 | |
| 5158 | for (i = 0; i < BPF_REG_FP; i++) { |
| 5159 | live = st->regs[i].live; |
| 5160 | /* liveness must not touch this register anymore */ |
| 5161 | st->regs[i].live |= REG_LIVE_DONE; |
| 5162 | if (!(live & REG_LIVE_READ)) |
| 5163 | /* since the register is unused, clear its state |
| 5164 | * to make further comparison simpler |
| 5165 | */ |
| 5166 | __mark_reg_not_init(&st->regs[i]); |
| 5167 | } |
| 5168 | |
| 5169 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { |
| 5170 | live = st->stack[i].spilled_ptr.live; |
| 5171 | /* liveness must not touch this stack slot anymore */ |
| 5172 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; |
| 5173 | if (!(live & REG_LIVE_READ)) { |
| 5174 | __mark_reg_not_init(&st->stack[i].spilled_ptr); |
| 5175 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 5176 | st->stack[i].slot_type[j] = STACK_INVALID; |
| 5177 | } |
| 5178 | } |
| 5179 | } |
| 5180 | |
| 5181 | static void clean_verifier_state(struct bpf_verifier_env *env, |
| 5182 | struct bpf_verifier_state *st) |
| 5183 | { |
| 5184 | int i; |
| 5185 | |
| 5186 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) |
| 5187 | /* all regs in this state in all frames were already marked */ |
| 5188 | return; |
| 5189 | |
| 5190 | for (i = 0; i <= st->curframe; i++) |
| 5191 | clean_func_state(env, st->frame[i]); |
| 5192 | } |
| 5193 | |
| 5194 | /* the parentage chains form a tree. |
| 5195 | * the verifier states are added to state lists at given insn and |
| 5196 | * pushed into state stack for future exploration. |
| 5197 | * when the verifier reaches bpf_exit insn some of the verifer states |
| 5198 | * stored in the state lists have their final liveness state already, |
| 5199 | * but a lot of states will get revised from liveness point of view when |
| 5200 | * the verifier explores other branches. |
| 5201 | * Example: |
| 5202 | * 1: r0 = 1 |
| 5203 | * 2: if r1 == 100 goto pc+1 |
| 5204 | * 3: r0 = 2 |
| 5205 | * 4: exit |
| 5206 | * when the verifier reaches exit insn the register r0 in the state list of |
| 5207 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch |
| 5208 | * of insn 2 and goes exploring further. At the insn 4 it will walk the |
| 5209 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. |
| 5210 | * |
| 5211 | * Since the verifier pushes the branch states as it sees them while exploring |
| 5212 | * the program the condition of walking the branch instruction for the second |
| 5213 | * time means that all states below this branch were already explored and |
| 5214 | * their final liveness markes are already propagated. |
| 5215 | * Hence when the verifier completes the search of state list in is_state_visited() |
| 5216 | * we can call this clean_live_states() function to mark all liveness states |
| 5217 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' |
| 5218 | * will not be used. |
| 5219 | * This function also clears the registers and stack for states that !READ |
| 5220 | * to simplify state merging. |
| 5221 | * |
| 5222 | * Important note here that walking the same branch instruction in the callee |
| 5223 | * doesn't meant that the states are DONE. The verifier has to compare |
| 5224 | * the callsites |
| 5225 | */ |
| 5226 | static void clean_live_states(struct bpf_verifier_env *env, int insn, |
| 5227 | struct bpf_verifier_state *cur) |
| 5228 | { |
| 5229 | struct bpf_verifier_state_list *sl; |
| 5230 | int i; |
| 5231 | |
| 5232 | sl = env->explored_states[insn]; |
| 5233 | if (!sl) |
| 5234 | return; |
| 5235 | |
| 5236 | while (sl != STATE_LIST_MARK) { |
| 5237 | if (sl->state.curframe != cur->curframe) |
| 5238 | goto next; |
| 5239 | for (i = 0; i <= cur->curframe; i++) |
| 5240 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) |
| 5241 | goto next; |
| 5242 | clean_verifier_state(env, &sl->state); |
| 5243 | next: |
| 5244 | sl = sl->next; |
| 5245 | } |
| 5246 | } |
| 5247 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5248 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 5249 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 5250 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5251 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5252 | bool equal; |
| 5253 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5254 | if (!(rold->live & REG_LIVE_READ)) |
| 5255 | /* explored state didn't use this */ |
| 5256 | return true; |
| 5257 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5258 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5259 | |
| 5260 | if (rold->type == PTR_TO_STACK) |
| 5261 | /* two stack pointers are equal only if they're pointing to |
| 5262 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 5263 | */ |
| 5264 | return equal && rold->frameno == rcur->frameno; |
| 5265 | |
| 5266 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5267 | return true; |
| 5268 | |
| 5269 | if (rold->type == NOT_INIT) |
| 5270 | /* explored state can't have used this */ |
| 5271 | return true; |
| 5272 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5273 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5274 | switch (rold->type) { |
| 5275 | case SCALAR_VALUE: |
| 5276 | if (rcur->type == SCALAR_VALUE) { |
| 5277 | /* new val must satisfy old val knowledge */ |
| 5278 | return range_within(rold, rcur) && |
| 5279 | tnum_in(rold->var_off, rcur->var_off); |
| 5280 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 5281 | /* We're trying to use a pointer in place of a scalar. |
| 5282 | * Even if the scalar was unbounded, this could lead to |
| 5283 | * pointer leaks because scalars are allowed to leak |
| 5284 | * while pointers are not. We could make this safe in |
| 5285 | * special cases if root is calling us, but it's |
| 5286 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5287 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 5288 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5289 | } |
| 5290 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 5291 | /* If the new min/max/var_off satisfy the old ones and |
| 5292 | * everything else matches, we are OK. |
| 5293 | * We don't care about the 'id' value, because nothing |
| 5294 | * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL) |
| 5295 | */ |
| 5296 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 5297 | range_within(rold, rcur) && |
| 5298 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5299 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 5300 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 5301 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 5302 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 5303 | * checked, doing so could have affected others with the same |
| 5304 | * id, and we can't check for that because we lost the id when |
| 5305 | * we converted to a PTR_TO_MAP_VALUE. |
| 5306 | */ |
| 5307 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 5308 | return false; |
| 5309 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 5310 | return false; |
| 5311 | /* Check our ids match any regs they're supposed to */ |
| 5312 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5313 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5314 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5315 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5316 | return false; |
| 5317 | /* We must have at least as much range as the old ptr |
| 5318 | * did, so that any accesses which were safe before are |
| 5319 | * still safe. This is true even if old range < old off, |
| 5320 | * since someone could have accessed through (ptr - k), or |
| 5321 | * even done ptr -= k in a register, to get a safe access. |
| 5322 | */ |
| 5323 | if (rold->range > rcur->range) |
| 5324 | return false; |
| 5325 | /* If the offsets don't match, we can't trust our alignment; |
| 5326 | * nor can we be sure that we won't fall out of range. |
| 5327 | */ |
| 5328 | if (rold->off != rcur->off) |
| 5329 | return false; |
| 5330 | /* id relations must be preserved */ |
| 5331 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 5332 | return false; |
| 5333 | /* new val must satisfy old val knowledge */ |
| 5334 | return range_within(rold, rcur) && |
| 5335 | tnum_in(rold->var_off, rcur->var_off); |
| 5336 | case PTR_TO_CTX: |
| 5337 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5338 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 5339 | case PTR_TO_FLOW_KEYS: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5340 | case PTR_TO_SOCKET: |
| 5341 | case PTR_TO_SOCKET_OR_NULL: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5342 | /* Only valid matches are exact, which memcmp() above |
| 5343 | * would have accepted |
| 5344 | */ |
| 5345 | default: |
| 5346 | /* Don't know what's going on, just say it's not safe */ |
| 5347 | return false; |
| 5348 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5349 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5350 | /* Shouldn't get here; if we do, say it's not safe */ |
| 5351 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5352 | return false; |
| 5353 | } |
| 5354 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5355 | static bool stacksafe(struct bpf_func_state *old, |
| 5356 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5357 | struct idpair *idmap) |
| 5358 | { |
| 5359 | int i, spi; |
| 5360 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5361 | /* walk slots of the explored stack and ignore any additional |
| 5362 | * slots in the current stack, since explored(safe) state |
| 5363 | * didn't use them |
| 5364 | */ |
| 5365 | for (i = 0; i < old->allocated_stack; i++) { |
| 5366 | spi = i / BPF_REG_SIZE; |
| 5367 | |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 5368 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
| 5369 | i += BPF_REG_SIZE - 1; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 5370 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 5371 | continue; |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 5372 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 5373 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5374 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 5375 | continue; |
Alexei Starovoitov | 19e2dbb | 2018-12-13 11:42:33 -0800 | [diff] [blame] | 5376 | |
| 5377 | /* explored stack has more populated slots than current stack |
| 5378 | * and these slots were used |
| 5379 | */ |
| 5380 | if (i >= cur->allocated_stack) |
| 5381 | return false; |
| 5382 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 5383 | /* if old state was safe with misc data in the stack |
| 5384 | * it will be safe with zero-initialized stack. |
| 5385 | * The opposite is not true |
| 5386 | */ |
| 5387 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 5388 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 5389 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5390 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 5391 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 5392 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 5393 | * this stack slot, but current has has STACK_MISC -> |
| 5394 | * this verifier states are not equivalent, |
| 5395 | * return false to continue verification of this path |
| 5396 | */ |
| 5397 | return false; |
| 5398 | if (i % BPF_REG_SIZE) |
| 5399 | continue; |
| 5400 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 5401 | continue; |
| 5402 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 5403 | &cur->stack[spi].spilled_ptr, |
| 5404 | idmap)) |
| 5405 | /* when explored and current stack slot are both storing |
| 5406 | * spilled registers, check that stored pointers types |
| 5407 | * are the same as well. |
| 5408 | * Ex: explored safe path could have stored |
| 5409 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 5410 | * but current path has stored: |
| 5411 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 5412 | * such verifier states are not equivalent. |
| 5413 | * return false to continue verification of this path |
| 5414 | */ |
| 5415 | return false; |
| 5416 | } |
| 5417 | return true; |
| 5418 | } |
| 5419 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5420 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 5421 | { |
| 5422 | if (old->acquired_refs != cur->acquired_refs) |
| 5423 | return false; |
| 5424 | return !memcmp(old->refs, cur->refs, |
| 5425 | sizeof(*old->refs) * old->acquired_refs); |
| 5426 | } |
| 5427 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5428 | /* compare two verifier states |
| 5429 | * |
| 5430 | * all states stored in state_list are known to be valid, since |
| 5431 | * verifier reached 'bpf_exit' instruction through them |
| 5432 | * |
| 5433 | * this function is called when verifier exploring different branches of |
| 5434 | * execution popped from the state stack. If it sees an old state that has |
| 5435 | * more strict register state and more strict stack state then this execution |
| 5436 | * branch doesn't need to be explored further, since verifier already |
| 5437 | * concluded that more strict state leads to valid finish. |
| 5438 | * |
| 5439 | * Therefore two states are equivalent if register state is more conservative |
| 5440 | * and explored stack state is more conservative than the current one. |
| 5441 | * Example: |
| 5442 | * explored current |
| 5443 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 5444 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 5445 | * |
| 5446 | * In other words if current stack state (one being explored) has more |
| 5447 | * valid slots than old one that already passed validation, it means |
| 5448 | * the verifier can stop exploring and conclude that current state is valid too |
| 5449 | * |
| 5450 | * Similarly with registers. If explored state has register type as invalid |
| 5451 | * whereas register type in current state is meaningful, it means that |
| 5452 | * the current state will reach 'bpf_exit' instruction safely |
| 5453 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5454 | static bool func_states_equal(struct bpf_func_state *old, |
| 5455 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5456 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5457 | struct idpair *idmap; |
| 5458 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5459 | int i; |
| 5460 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5461 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 5462 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 5463 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 5464 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5465 | |
| 5466 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 5467 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5468 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5469 | } |
| 5470 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5471 | if (!stacksafe(old, cur, idmap)) |
| 5472 | goto out_free; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5473 | |
| 5474 | if (!refsafe(old, cur)) |
| 5475 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5476 | ret = true; |
| 5477 | out_free: |
| 5478 | kfree(idmap); |
| 5479 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5480 | } |
| 5481 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5482 | static bool states_equal(struct bpf_verifier_env *env, |
| 5483 | struct bpf_verifier_state *old, |
| 5484 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5485 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5486 | int i; |
| 5487 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5488 | if (old->curframe != cur->curframe) |
| 5489 | return false; |
| 5490 | |
| 5491 | /* for states to be equal callsites have to be the same |
| 5492 | * and all frame states need to be equivalent |
| 5493 | */ |
| 5494 | for (i = 0; i <= old->curframe; i++) { |
| 5495 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 5496 | return false; |
| 5497 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 5498 | return false; |
| 5499 | } |
| 5500 | return true; |
| 5501 | } |
| 5502 | |
| 5503 | /* A write screens off any subsequent reads; but write marks come from the |
| 5504 | * straight-line code between a state and its parent. When we arrive at an |
| 5505 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 5506 | * code, so read marks in the state must propagate to the parent regardless |
| 5507 | * 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] | 5508 | * in mark_reg_read() is for. |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5509 | */ |
| 5510 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 5511 | const struct bpf_verifier_state *vstate, |
| 5512 | struct bpf_verifier_state *vparent) |
| 5513 | { |
| 5514 | int i, frame, err = 0; |
| 5515 | struct bpf_func_state *state, *parent; |
| 5516 | |
| 5517 | if (vparent->curframe != vstate->curframe) { |
| 5518 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 5519 | vparent->curframe, vstate->curframe); |
| 5520 | return -EFAULT; |
| 5521 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5522 | /* Propagate read liveness of registers... */ |
| 5523 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
| 5524 | /* We don't need to worry about FP liveness because it's read-only */ |
| 5525 | for (i = 0; i < BPF_REG_FP; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5526 | if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5527 | continue; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5528 | if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5529 | err = mark_reg_read(env, &vstate->frame[vstate->curframe]->regs[i], |
| 5530 | &vparent->frame[vstate->curframe]->regs[i]); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5531 | if (err) |
| 5532 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5533 | } |
| 5534 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5535 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5536 | /* ... and stack slots */ |
| 5537 | for (frame = 0; frame <= vstate->curframe; frame++) { |
| 5538 | state = vstate->frame[frame]; |
| 5539 | parent = vparent->frame[frame]; |
| 5540 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 5541 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5542 | if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ) |
| 5543 | continue; |
| 5544 | if (state->stack[i].spilled_ptr.live & REG_LIVE_READ) |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5545 | mark_reg_read(env, &state->stack[i].spilled_ptr, |
| 5546 | &parent->stack[i].spilled_ptr); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5547 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5548 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5549 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5550 | } |
| 5551 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5552 | 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] | 5553 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5554 | struct bpf_verifier_state_list *new_sl; |
| 5555 | struct bpf_verifier_state_list *sl; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5556 | struct bpf_verifier_state *cur = env->cur_state, *new; |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 5557 | int i, j, err, states_cnt = 0; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5558 | |
| 5559 | sl = env->explored_states[insn_idx]; |
| 5560 | if (!sl) |
| 5561 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 5562 | * be doing state search here |
| 5563 | */ |
| 5564 | return 0; |
| 5565 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 5566 | clean_live_states(env, insn_idx, cur); |
| 5567 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5568 | while (sl != STATE_LIST_MARK) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5569 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5570 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5571 | * prune the search. |
| 5572 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 5573 | * If we have any write marks in env->cur_state, they |
| 5574 | * will prevent corresponding reads in the continuation |
| 5575 | * from reaching our parent (an explored_state). Our |
| 5576 | * own state will get the read marks recorded, but |
| 5577 | * they'll be immediately forgotten as we're pruning |
| 5578 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5579 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5580 | err = propagate_liveness(env, &sl->state, cur); |
| 5581 | if (err) |
| 5582 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5583 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5584 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5585 | sl = sl->next; |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 5586 | states_cnt++; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5587 | } |
| 5588 | |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 5589 | if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
| 5590 | return 0; |
| 5591 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5592 | /* there were no equivalent states, remember current one. |
| 5593 | * technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5594 | * but it will either reach outer most bpf_exit (which means it's safe) |
| 5595 | * or it will be rejected. Since there are no loops, we won't be |
| 5596 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
| 5597 | * again on the way to bpf_exit |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5598 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5599 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5600 | if (!new_sl) |
| 5601 | return -ENOMEM; |
| 5602 | |
| 5603 | /* add new state to the head of linked list */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5604 | new = &new_sl->state; |
| 5605 | err = copy_verifier_state(new, cur); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 5606 | if (err) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5607 | free_verifier_state(new, false); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 5608 | kfree(new_sl); |
| 5609 | return err; |
| 5610 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5611 | new_sl->next = env->explored_states[insn_idx]; |
| 5612 | env->explored_states[insn_idx] = new_sl; |
Jakub Kicinski | 7640ead | 2018-12-12 16:29:07 -0800 | [diff] [blame] | 5613 | /* connect new state to parentage chain. Current frame needs all |
| 5614 | * registers connected. Only r6 - r9 of the callers are alive (pushed |
| 5615 | * to the stack implicitly by JITs) so in callers' frames connect just |
| 5616 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to |
| 5617 | * the state of the call instruction (with WRITTEN set), and r0 comes |
| 5618 | * from callee with its full parentage chain, anyway. |
| 5619 | */ |
| 5620 | for (j = 0; j <= cur->curframe; j++) |
| 5621 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) |
| 5622 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 5623 | /* clear write marks in current state: the writes we did are not writes |
| 5624 | * our child did, so they don't screen off its reads from us. |
| 5625 | * (There are no read marks in current state, because reads always mark |
| 5626 | * their parent and current state never has children yet. Only |
| 5627 | * explored_states can get read marks.) |
| 5628 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5629 | for (i = 0; i < BPF_REG_FP; i++) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5630 | cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE; |
| 5631 | |
| 5632 | /* all stack frames are accessible from callee, clear them all */ |
| 5633 | for (j = 0; j <= cur->curframe; j++) { |
| 5634 | struct bpf_func_state *frame = cur->frame[j]; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5635 | struct bpf_func_state *newframe = new->frame[j]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5636 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5637 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 5638 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5639 | frame->stack[i].spilled_ptr.parent = |
| 5640 | &newframe->stack[i].spilled_ptr; |
| 5641 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5642 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5643 | return 0; |
| 5644 | } |
| 5645 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5646 | /* Return true if it's OK to have the same insn return a different type. */ |
| 5647 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 5648 | { |
| 5649 | switch (type) { |
| 5650 | case PTR_TO_CTX: |
| 5651 | case PTR_TO_SOCKET: |
| 5652 | case PTR_TO_SOCKET_OR_NULL: |
| 5653 | return false; |
| 5654 | default: |
| 5655 | return true; |
| 5656 | } |
| 5657 | } |
| 5658 | |
| 5659 | /* If an instruction was previously used with particular pointer types, then we |
| 5660 | * need to be careful to avoid cases such as the below, where it may be ok |
| 5661 | * for one branch accessing the pointer, but not ok for the other branch: |
| 5662 | * |
| 5663 | * R1 = sock_ptr |
| 5664 | * goto X; |
| 5665 | * ... |
| 5666 | * R1 = some_other_valid_ptr; |
| 5667 | * goto X; |
| 5668 | * ... |
| 5669 | * R2 = *(u32 *)(R1 + 0); |
| 5670 | */ |
| 5671 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 5672 | { |
| 5673 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 5674 | !reg_type_mismatch_ok(prev)); |
| 5675 | } |
| 5676 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5677 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5678 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5679 | struct bpf_verifier_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5680 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5681 | struct bpf_reg_state *regs; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5682 | int insn_cnt = env->prog->len, i; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5683 | int insn_processed = 0; |
| 5684 | bool do_print_state = false; |
| 5685 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 5686 | env->prev_linfo = NULL; |
| 5687 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5688 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 5689 | if (!state) |
| 5690 | return -ENOMEM; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5691 | state->curframe = 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5692 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 5693 | if (!state->frame[0]) { |
| 5694 | kfree(state); |
| 5695 | return -ENOMEM; |
| 5696 | } |
| 5697 | env->cur_state = state; |
| 5698 | init_func_state(env, state->frame[0], |
| 5699 | BPF_MAIN_FUNC /* callsite */, |
| 5700 | 0 /* frameno */, |
| 5701 | 0 /* subprogno, zero == main subprog */); |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5702 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5703 | for (;;) { |
| 5704 | struct bpf_insn *insn; |
| 5705 | u8 class; |
| 5706 | int err; |
| 5707 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5708 | if (env->insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5709 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5710 | env->insn_idx, insn_cnt); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5711 | return -EFAULT; |
| 5712 | } |
| 5713 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5714 | insn = &insns[env->insn_idx]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5715 | class = BPF_CLASS(insn->code); |
| 5716 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 5717 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5718 | verbose(env, |
| 5719 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5720 | insn_processed); |
| 5721 | return -E2BIG; |
| 5722 | } |
| 5723 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5724 | err = is_state_visited(env, env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5725 | if (err < 0) |
| 5726 | return err; |
| 5727 | if (err == 1) { |
| 5728 | /* found equivalent state, can prune the search */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5729 | if (env->log.level) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5730 | if (do_print_state) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5731 | verbose(env, "\nfrom %d to %d: safe\n", |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5732 | env->prev_insn_idx, env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5733 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5734 | verbose(env, "%d: safe\n", env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5735 | } |
| 5736 | goto process_bpf_exit; |
| 5737 | } |
| 5738 | |
Alexei Starovoitov | c349480 | 2018-12-03 22:46:04 -0800 | [diff] [blame] | 5739 | if (signal_pending(current)) |
| 5740 | return -EAGAIN; |
| 5741 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 5742 | if (need_resched()) |
| 5743 | cond_resched(); |
| 5744 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5745 | if (env->log.level > 1 || (env->log.level && do_print_state)) { |
| 5746 | if (env->log.level > 1) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5747 | verbose(env, "%d:", env->insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 5748 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5749 | verbose(env, "\nfrom %d to %d:", |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5750 | env->prev_insn_idx, env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5751 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5752 | do_print_state = false; |
| 5753 | } |
| 5754 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5755 | if (env->log.level) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5756 | const struct bpf_insn_cbs cbs = { |
| 5757 | .cb_print = verbose, |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 5758 | .private_data = env, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5759 | }; |
| 5760 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5761 | verbose_linfo(env, env->insn_idx, "; "); |
| 5762 | verbose(env, "%d: ", env->insn_idx); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 5763 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5764 | } |
| 5765 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 5766 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5767 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
| 5768 | env->prev_insn_idx); |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 5769 | if (err) |
| 5770 | return err; |
| 5771 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 5772 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5773 | regs = cur_regs(env); |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5774 | env->insn_aux_data[env->insn_idx].seen = true; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5775 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5776 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5777 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5778 | if (err) |
| 5779 | return err; |
| 5780 | |
| 5781 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5782 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5783 | |
| 5784 | /* check for reserved fields is already done */ |
| 5785 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5786 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5787 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5788 | if (err) |
| 5789 | return err; |
| 5790 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5791 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5792 | if (err) |
| 5793 | return err; |
| 5794 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 5795 | src_reg_type = regs[insn->src_reg].type; |
| 5796 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5797 | /* check that memory (src_reg + off) is readable, |
| 5798 | * the state of dst_reg will be updated by this func |
| 5799 | */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5800 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
| 5801 | insn->off, BPF_SIZE(insn->code), |
| 5802 | BPF_READ, insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5803 | if (err) |
| 5804 | return err; |
| 5805 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5806 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5807 | |
| 5808 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5809 | /* saw a valid insn |
| 5810 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5811 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5812 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5813 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5814 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5815 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5816 | /* ABuser program is trying to use the same insn |
| 5817 | * dst_reg = *(u32*) (src_reg + off) |
| 5818 | * with different pointer types: |
| 5819 | * src_reg == ctx in one branch and |
| 5820 | * src_reg == stack|map in some other branch. |
| 5821 | * Reject it. |
| 5822 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5823 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5824 | return -EINVAL; |
| 5825 | } |
| 5826 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5827 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5828 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5829 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5830 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5831 | err = check_xadd(env, env->insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5832 | if (err) |
| 5833 | return err; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5834 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5835 | continue; |
| 5836 | } |
| 5837 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5838 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5839 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5840 | if (err) |
| 5841 | return err; |
| 5842 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5843 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5844 | if (err) |
| 5845 | return err; |
| 5846 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5847 | dst_reg_type = regs[insn->dst_reg].type; |
| 5848 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5849 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5850 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 5851 | insn->off, BPF_SIZE(insn->code), |
| 5852 | BPF_WRITE, insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5853 | if (err) |
| 5854 | return err; |
| 5855 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5856 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5857 | |
| 5858 | if (*prev_dst_type == NOT_INIT) { |
| 5859 | *prev_dst_type = dst_reg_type; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5860 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5861 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5862 | return -EINVAL; |
| 5863 | } |
| 5864 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5865 | } else if (class == BPF_ST) { |
| 5866 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 5867 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5868 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5869 | return -EINVAL; |
| 5870 | } |
| 5871 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5872 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5873 | if (err) |
| 5874 | return err; |
| 5875 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 5876 | if (is_ctx_reg(env, insn->dst_reg)) { |
Joe Stringer | 9d2be44 | 2018-10-02 13:35:31 -0700 | [diff] [blame] | 5877 | 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] | 5878 | insn->dst_reg, |
| 5879 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 5880 | return -EACCES; |
| 5881 | } |
| 5882 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5883 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5884 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 5885 | insn->off, BPF_SIZE(insn->code), |
| 5886 | BPF_WRITE, -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5887 | if (err) |
| 5888 | return err; |
| 5889 | |
| 5890 | } else if (class == BPF_JMP) { |
| 5891 | u8 opcode = BPF_OP(insn->code); |
| 5892 | |
| 5893 | if (opcode == BPF_CALL) { |
| 5894 | if (BPF_SRC(insn->code) != BPF_K || |
| 5895 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5896 | (insn->src_reg != BPF_REG_0 && |
| 5897 | insn->src_reg != BPF_PSEUDO_CALL) || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5898 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5899 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5900 | return -EINVAL; |
| 5901 | } |
| 5902 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5903 | if (insn->src_reg == BPF_PSEUDO_CALL) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5904 | err = check_func_call(env, insn, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5905 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5906 | err = check_helper_call(env, insn->imm, env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5907 | if (err) |
| 5908 | return err; |
| 5909 | |
| 5910 | } else if (opcode == BPF_JA) { |
| 5911 | if (BPF_SRC(insn->code) != BPF_K || |
| 5912 | insn->imm != 0 || |
| 5913 | insn->src_reg != BPF_REG_0 || |
| 5914 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5915 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5916 | return -EINVAL; |
| 5917 | } |
| 5918 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5919 | env->insn_idx += insn->off + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5920 | continue; |
| 5921 | |
| 5922 | } else if (opcode == BPF_EXIT) { |
| 5923 | if (BPF_SRC(insn->code) != BPF_K || |
| 5924 | insn->imm != 0 || |
| 5925 | insn->src_reg != BPF_REG_0 || |
| 5926 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5927 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5928 | return -EINVAL; |
| 5929 | } |
| 5930 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5931 | if (state->curframe) { |
| 5932 | /* exit from nested function */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5933 | env->prev_insn_idx = env->insn_idx; |
| 5934 | err = prepare_func_exit(env, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5935 | if (err) |
| 5936 | return err; |
| 5937 | do_print_state = true; |
| 5938 | continue; |
| 5939 | } |
| 5940 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5941 | err = check_reference_leak(env); |
| 5942 | if (err) |
| 5943 | return err; |
| 5944 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5945 | /* eBPF calling convetion is such that R0 is used |
| 5946 | * to return the value from eBPF program. |
| 5947 | * Make sure that it's readable at this time |
| 5948 | * of bpf_exit, which means that program wrote |
| 5949 | * something into it earlier |
| 5950 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5951 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5952 | if (err) |
| 5953 | return err; |
| 5954 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5955 | if (is_pointer_value(env, BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5956 | verbose(env, "R0 leaks addr as return value\n"); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5957 | return -EACCES; |
| 5958 | } |
| 5959 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5960 | err = check_return_code(env); |
| 5961 | if (err) |
| 5962 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5963 | process_bpf_exit: |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5964 | err = pop_stack(env, &env->prev_insn_idx, |
| 5965 | &env->insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5966 | if (err < 0) { |
| 5967 | if (err != -ENOENT) |
| 5968 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5969 | break; |
| 5970 | } else { |
| 5971 | do_print_state = true; |
| 5972 | continue; |
| 5973 | } |
| 5974 | } else { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5975 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5976 | if (err) |
| 5977 | return err; |
| 5978 | } |
| 5979 | } else if (class == BPF_LD) { |
| 5980 | u8 mode = BPF_MODE(insn->code); |
| 5981 | |
| 5982 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5983 | err = check_ld_abs(env, insn); |
| 5984 | if (err) |
| 5985 | return err; |
| 5986 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5987 | } else if (mode == BPF_IMM) { |
| 5988 | err = check_ld_imm(env, insn); |
| 5989 | if (err) |
| 5990 | return err; |
| 5991 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 5992 | env->insn_idx++; |
| 5993 | env->insn_aux_data[env->insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5994 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5995 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5996 | return -EINVAL; |
| 5997 | } |
| 5998 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5999 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6000 | return -EINVAL; |
| 6001 | } |
| 6002 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 6003 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6004 | } |
| 6005 | |
Daniel Borkmann | 4bd95f4 | 2018-01-20 01:24:36 +0100 | [diff] [blame] | 6006 | verbose(env, "processed %d insns (limit %d), stack depth ", |
| 6007 | insn_processed, BPF_COMPLEXITY_LIMIT_INSNS); |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6008 | for (i = 0; i < env->subprog_cnt; i++) { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 6009 | u32 depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6010 | |
| 6011 | verbose(env, "%d", depth); |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6012 | if (i + 1 < env->subprog_cnt) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6013 | verbose(env, "+"); |
| 6014 | } |
| 6015 | verbose(env, "\n"); |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 6016 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6017 | return 0; |
| 6018 | } |
| 6019 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 6020 | static int check_map_prealloc(struct bpf_map *map) |
| 6021 | { |
| 6022 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 6023 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 6024 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 6025 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 6026 | } |
| 6027 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6028 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 6029 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 6030 | struct bpf_prog *prog) |
| 6031 | |
| 6032 | { |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 6033 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 6034 | * preallocated hash maps, since doing memory allocation |
| 6035 | * in overflow_handler can crash depending on where nmi got |
| 6036 | * triggered. |
| 6037 | */ |
| 6038 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 6039 | if (!check_map_prealloc(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6040 | 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] | 6041 | return -EINVAL; |
| 6042 | } |
| 6043 | if (map->inner_map_meta && |
| 6044 | !check_map_prealloc(map->inner_map_meta)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6045 | 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] | 6046 | return -EINVAL; |
| 6047 | } |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 6048 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 6049 | |
| 6050 | 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] | 6051 | !bpf_offload_prog_map_match(prog, map)) { |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 6052 | verbose(env, "offload device mismatch between prog and map\n"); |
| 6053 | return -EINVAL; |
| 6054 | } |
| 6055 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 6056 | return 0; |
| 6057 | } |
| 6058 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 6059 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 6060 | { |
| 6061 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 6062 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 6063 | } |
| 6064 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6065 | /* look for pseudo eBPF instructions that access map FDs and |
| 6066 | * replace them with actual map pointers |
| 6067 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6068 | 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] | 6069 | { |
| 6070 | struct bpf_insn *insn = env->prog->insnsi; |
| 6071 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 6072 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6073 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 6074 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 6075 | if (err) |
| 6076 | return err; |
| 6077 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6078 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6079 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6080 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6081 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6082 | return -EINVAL; |
| 6083 | } |
| 6084 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6085 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 6086 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 6087 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6088 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6089 | return -EINVAL; |
| 6090 | } |
| 6091 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6092 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
| 6093 | struct bpf_map *map; |
| 6094 | struct fd f; |
| 6095 | |
| 6096 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 6097 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 6098 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6099 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6100 | return -EINVAL; |
| 6101 | } |
| 6102 | |
| 6103 | if (insn->src_reg == 0) |
| 6104 | /* valid generic load 64-bit imm */ |
| 6105 | goto next_insn; |
| 6106 | |
| 6107 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6108 | verbose(env, |
| 6109 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6110 | return -EINVAL; |
| 6111 | } |
| 6112 | |
| 6113 | f = fdget(insn->imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 6114 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6115 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6116 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6117 | insn->imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6118 | return PTR_ERR(map); |
| 6119 | } |
| 6120 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6121 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 6122 | if (err) { |
| 6123 | fdput(f); |
| 6124 | return err; |
| 6125 | } |
| 6126 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6127 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
| 6128 | insn[0].imm = (u32) (unsigned long) map; |
| 6129 | insn[1].imm = ((u64) (unsigned long) map) >> 32; |
| 6130 | |
| 6131 | /* check whether we recorded this map already */ |
| 6132 | for (j = 0; j < env->used_map_cnt; j++) |
| 6133 | if (env->used_maps[j] == map) { |
| 6134 | fdput(f); |
| 6135 | goto next_insn; |
| 6136 | } |
| 6137 | |
| 6138 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 6139 | fdput(f); |
| 6140 | return -E2BIG; |
| 6141 | } |
| 6142 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6143 | /* hold the map. If the program is rejected by verifier, |
| 6144 | * the map will be released by release_maps() or it |
| 6145 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 6146 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6147 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 6148 | map = bpf_map_inc(map, false); |
| 6149 | if (IS_ERR(map)) { |
| 6150 | fdput(f); |
| 6151 | return PTR_ERR(map); |
| 6152 | } |
| 6153 | env->used_maps[env->used_map_cnt++] = map; |
| 6154 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 6155 | if (bpf_map_is_cgroup_storage(map) && |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 6156 | bpf_cgroup_storage_assign(env->prog, map)) { |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 6157 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 6158 | fdput(f); |
| 6159 | return -EBUSY; |
| 6160 | } |
| 6161 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6162 | fdput(f); |
| 6163 | next_insn: |
| 6164 | insn++; |
| 6165 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 6166 | continue; |
| 6167 | } |
| 6168 | |
| 6169 | /* Basic sanity check before we invest more work here. */ |
| 6170 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 6171 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 6172 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6173 | } |
| 6174 | } |
| 6175 | |
| 6176 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 6177 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 6178 | * These pointers will be used later by verifier to validate map access. |
| 6179 | */ |
| 6180 | return 0; |
| 6181 | } |
| 6182 | |
| 6183 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6184 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6185 | { |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 6186 | enum bpf_cgroup_storage_type stype; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6187 | int i; |
| 6188 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 6189 | for_each_cgroup_storage_type(stype) { |
| 6190 | if (!env->prog->aux->cgroup_storage[stype]) |
| 6191 | continue; |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 6192 | bpf_cgroup_storage_release(env->prog, |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 6193 | env->prog->aux->cgroup_storage[stype]); |
| 6194 | } |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 6195 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6196 | for (i = 0; i < env->used_map_cnt; i++) |
| 6197 | bpf_map_put(env->used_maps[i]); |
| 6198 | } |
| 6199 | |
| 6200 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6201 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6202 | { |
| 6203 | struct bpf_insn *insn = env->prog->insnsi; |
| 6204 | int insn_cnt = env->prog->len; |
| 6205 | int i; |
| 6206 | |
| 6207 | for (i = 0; i < insn_cnt; i++, insn++) |
| 6208 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 6209 | insn->src_reg = 0; |
| 6210 | } |
| 6211 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 6212 | /* single env->prog->insni[off] instruction was replaced with the range |
| 6213 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 6214 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 6215 | */ |
| 6216 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, |
| 6217 | u32 off, u32 cnt) |
| 6218 | { |
| 6219 | struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 6220 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 6221 | |
| 6222 | if (cnt == 1) |
| 6223 | return 0; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 6224 | new_data = vzalloc(array_size(prog_len, |
| 6225 | sizeof(struct bpf_insn_aux_data))); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 6226 | if (!new_data) |
| 6227 | return -ENOMEM; |
| 6228 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 6229 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 6230 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 6231 | for (i = off; i < off + cnt - 1; i++) |
| 6232 | new_data[i].seen = true; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 6233 | env->insn_aux_data = new_data; |
| 6234 | vfree(old_data); |
| 6235 | return 0; |
| 6236 | } |
| 6237 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 6238 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 6239 | { |
| 6240 | int i; |
| 6241 | |
| 6242 | if (len == 1) |
| 6243 | return; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 6244 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 6245 | for (i = 0; i <= env->subprog_cnt; i++) { |
Edward Cree | afd5942 | 2018-11-16 12:00:07 +0000 | [diff] [blame] | 6246 | if (env->subprog_info[i].start <= off) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 6247 | continue; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 6248 | env->subprog_info[i].start += len - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 6249 | } |
| 6250 | } |
| 6251 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 6252 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 6253 | const struct bpf_insn *patch, u32 len) |
| 6254 | { |
| 6255 | struct bpf_prog *new_prog; |
| 6256 | |
| 6257 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
| 6258 | if (!new_prog) |
| 6259 | return NULL; |
| 6260 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) |
| 6261 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 6262 | adjust_subprog_starts(env, off, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 6263 | return new_prog; |
| 6264 | } |
| 6265 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 6266 | /* The verifier does more data flow analysis than llvm and will not |
| 6267 | * explore branches that are dead at run time. Malicious programs can |
| 6268 | * have dead code too. Therefore replace all dead at-run-time code |
| 6269 | * with 'ja -1'. |
| 6270 | * |
| 6271 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 6272 | * program and through another bug we would manage to jump there, then |
| 6273 | * we'd execute beyond program memory otherwise. Returning exception |
| 6274 | * code also wouldn't work since we can have subprogs where the dead |
| 6275 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 6276 | */ |
| 6277 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 6278 | { |
| 6279 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 6280 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 6281 | struct bpf_insn *insn = env->prog->insnsi; |
| 6282 | const int insn_cnt = env->prog->len; |
| 6283 | int i; |
| 6284 | |
| 6285 | for (i = 0; i < insn_cnt; i++) { |
| 6286 | if (aux_data[i].seen) |
| 6287 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 6288 | memcpy(insn + i, &trap, sizeof(trap)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 6289 | } |
| 6290 | } |
| 6291 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6292 | /* convert load instructions that access fields of a context type into a |
| 6293 | * sequence of instructions that access fields of the underlying structure: |
| 6294 | * struct __sk_buff -> struct sk_buff |
| 6295 | * struct bpf_sock_ops -> struct sock |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6296 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6297 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6298 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 6299 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6300 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6301 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6302 | struct bpf_insn insn_buf[16], *insn; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 6303 | u32 target_size, size_default, off; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6304 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6305 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6306 | bool is_narrower_load; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6307 | |
Daniel Borkmann | b09928b | 2018-10-24 22:05:49 +0200 | [diff] [blame] | 6308 | if (ops->gen_prologue || env->seen_direct_write) { |
| 6309 | if (!ops->gen_prologue) { |
| 6310 | verbose(env, "bpf verifier is misconfigured\n"); |
| 6311 | return -EINVAL; |
| 6312 | } |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6313 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 6314 | env->prog); |
| 6315 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6316 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6317 | return -EINVAL; |
| 6318 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 6319 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6320 | if (!new_prog) |
| 6321 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 6322 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6323 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6324 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6325 | } |
| 6326 | } |
| 6327 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6328 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6329 | return 0; |
| 6330 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6331 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6332 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6333 | for (i = 0; i < insn_cnt; i++, insn++) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6334 | bpf_convert_ctx_access_t convert_ctx_access; |
| 6335 | |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 6336 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 6337 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 6338 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 6339 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6340 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 6341 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 6342 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 6343 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 6344 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6345 | type = BPF_WRITE; |
| 6346 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6347 | continue; |
| 6348 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 6349 | if (type == BPF_WRITE && |
| 6350 | env->insn_aux_data[i + delta].sanitize_stack_off) { |
| 6351 | struct bpf_insn patch[] = { |
| 6352 | /* Sanitize suspicious stack slot with zero. |
| 6353 | * There are no memory dependencies for this store, |
| 6354 | * since it's only using frame pointer and immediate |
| 6355 | * constant of zero |
| 6356 | */ |
| 6357 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, |
| 6358 | env->insn_aux_data[i + delta].sanitize_stack_off, |
| 6359 | 0), |
| 6360 | /* the original STX instruction will immediately |
| 6361 | * overwrite the same stack slot with appropriate value |
| 6362 | */ |
| 6363 | *insn, |
| 6364 | }; |
| 6365 | |
| 6366 | cnt = ARRAY_SIZE(patch); |
| 6367 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 6368 | if (!new_prog) |
| 6369 | return -ENOMEM; |
| 6370 | |
| 6371 | delta += cnt - 1; |
| 6372 | env->prog = new_prog; |
| 6373 | insn = new_prog->insnsi + i + delta; |
| 6374 | continue; |
| 6375 | } |
| 6376 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6377 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 6378 | case PTR_TO_CTX: |
| 6379 | if (!ops->convert_ctx_access) |
| 6380 | continue; |
| 6381 | convert_ctx_access = ops->convert_ctx_access; |
| 6382 | break; |
| 6383 | case PTR_TO_SOCKET: |
| 6384 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 6385 | break; |
| 6386 | default: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6387 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6388 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6389 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6390 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6391 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6392 | |
| 6393 | /* If the read access is a narrower load of the field, |
| 6394 | * convert to a 4/8-byte load, to minimum program type specific |
| 6395 | * convert_ctx_access changes. If conversion is successful, |
| 6396 | * we will apply proper mask to the result. |
| 6397 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6398 | is_narrower_load = size < ctx_field_size; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 6399 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
| 6400 | off = insn->off; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6401 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6402 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6403 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6404 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6405 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6406 | return -EINVAL; |
| 6407 | } |
| 6408 | |
| 6409 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6410 | if (ctx_field_size == 4) |
| 6411 | size_code = BPF_W; |
| 6412 | else if (ctx_field_size == 8) |
| 6413 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6414 | |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 6415 | insn->off = off & ~(size_default - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6416 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 6417 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6418 | |
| 6419 | target_size = 0; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6420 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 6421 | &target_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6422 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 6423 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6424 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6425 | return -EINVAL; |
| 6426 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6427 | |
| 6428 | if (is_narrower_load && size < target_size) { |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 6429 | u8 shift = (off & (size_default - 1)) * 8; |
| 6430 | |
| 6431 | if (ctx_field_size <= 4) { |
| 6432 | if (shift) |
| 6433 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, |
| 6434 | insn->dst_reg, |
| 6435 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6436 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6437 | (1 << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 6438 | } else { |
| 6439 | if (shift) |
| 6440 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, |
| 6441 | insn->dst_reg, |
| 6442 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6443 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6444 | (1 << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 6445 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6446 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6447 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 6448 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6449 | if (!new_prog) |
| 6450 | return -ENOMEM; |
| 6451 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6452 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6453 | |
| 6454 | /* keep walking new program and skip insns we just inserted */ |
| 6455 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6456 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6457 | } |
| 6458 | |
| 6459 | return 0; |
| 6460 | } |
| 6461 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6462 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 6463 | { |
| 6464 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 6465 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6466 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6467 | void *old_bpf_func; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6468 | int err; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6469 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6470 | if (env->subprog_cnt <= 1) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6471 | return 0; |
| 6472 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6473 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6474 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 6475 | insn->src_reg != BPF_PSEUDO_CALL) |
| 6476 | continue; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 6477 | /* Upon error here we cannot fall back to interpreter but |
| 6478 | * need a hard reject of the program. Thus -EFAULT is |
| 6479 | * propagated in any case. |
| 6480 | */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6481 | subprog = find_subprog(env, i + insn->imm + 1); |
| 6482 | if (subprog < 0) { |
| 6483 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 6484 | i + insn->imm + 1); |
| 6485 | return -EFAULT; |
| 6486 | } |
| 6487 | /* temporarily remember subprog id inside insn instead of |
| 6488 | * aux_data, since next loop will split up all insns into funcs |
| 6489 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6490 | insn->off = subprog; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6491 | /* remember original imm in case JIT fails and fallback |
| 6492 | * to interpreter will be needed |
| 6493 | */ |
| 6494 | env->insn_aux_data[i].call_imm = insn->imm; |
| 6495 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 6496 | insn->imm = 1; |
| 6497 | } |
| 6498 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6499 | err = bpf_prog_alloc_jited_linfo(prog); |
| 6500 | if (err) |
| 6501 | goto out_undo_insn; |
| 6502 | |
| 6503 | err = -ENOMEM; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 6504 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6505 | if (!func) |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 6506 | goto out_undo_insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6507 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6508 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6509 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 6510 | subprog_end = env->subprog_info[i + 1].start; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6511 | |
| 6512 | len = subprog_end - subprog_start; |
| 6513 | func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER); |
| 6514 | if (!func[i]) |
| 6515 | goto out_free; |
| 6516 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 6517 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 6518 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6519 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 6520 | if (bpf_prog_calc_tag(func[i])) |
| 6521 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6522 | func[i]->is_func = 1; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6523 | func[i]->aux->func_idx = i; |
| 6524 | /* the btf and func_info will be freed only at prog->aux */ |
| 6525 | func[i]->aux->btf = prog->aux->btf; |
| 6526 | func[i]->aux->func_info = prog->aux->func_info; |
| 6527 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6528 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 6529 | * Long term would need debug info to populate names |
| 6530 | */ |
| 6531 | func[i]->aux->name[0] = 'F'; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 6532 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6533 | func[i]->jit_requested = 1; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6534 | func[i]->aux->linfo = prog->aux->linfo; |
| 6535 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; |
| 6536 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; |
| 6537 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6538 | func[i] = bpf_int_jit_compile(func[i]); |
| 6539 | if (!func[i]->jited) { |
| 6540 | err = -ENOTSUPP; |
| 6541 | goto out_free; |
| 6542 | } |
| 6543 | cond_resched(); |
| 6544 | } |
| 6545 | /* at this point all bpf functions were successfully JITed |
| 6546 | * now populate all bpf_calls with correct addresses and |
| 6547 | * run last pass of JIT |
| 6548 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6549 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6550 | insn = func[i]->insnsi; |
| 6551 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 6552 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 6553 | insn->src_reg != BPF_PSEUDO_CALL) |
| 6554 | continue; |
| 6555 | subprog = insn->off; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6556 | insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) |
| 6557 | func[subprog]->bpf_func - |
| 6558 | __bpf_call_base; |
| 6559 | } |
Sandipan Das | 2162fed | 2018-05-24 12:26:45 +0530 | [diff] [blame] | 6560 | |
| 6561 | /* we use the aux data to keep a list of the start addresses |
| 6562 | * of the JITed images for each function in the program |
| 6563 | * |
| 6564 | * for some architectures, such as powerpc64, the imm field |
| 6565 | * might not be large enough to hold the offset of the start |
| 6566 | * address of the callee's JITed image from __bpf_call_base |
| 6567 | * |
| 6568 | * in such cases, we can lookup the start address of a callee |
| 6569 | * by using its subprog id, available from the off field of |
| 6570 | * the call instruction, as an index for this list |
| 6571 | */ |
| 6572 | func[i]->aux->func = func; |
| 6573 | func[i]->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6574 | } |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6575 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6576 | old_bpf_func = func[i]->bpf_func; |
| 6577 | tmp = bpf_int_jit_compile(func[i]); |
| 6578 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 6579 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 6580 | err = -ENOTSUPP; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6581 | goto out_free; |
| 6582 | } |
| 6583 | cond_resched(); |
| 6584 | } |
| 6585 | |
| 6586 | /* finally lock prog and jit images for all functions and |
| 6587 | * populate kallsysm |
| 6588 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6589 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6590 | bpf_prog_lock_ro(func[i]); |
| 6591 | bpf_prog_kallsyms_add(func[i]); |
| 6592 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6593 | |
| 6594 | /* Last step: make now unused interpreter insns from main |
| 6595 | * prog consistent for later dump requests, so they can |
| 6596 | * later look the same as if they were interpreted only. |
| 6597 | */ |
| 6598 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6599 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 6600 | insn->src_reg != BPF_PSEUDO_CALL) |
| 6601 | continue; |
| 6602 | insn->off = env->insn_aux_data[i].call_imm; |
| 6603 | subprog = find_subprog(env, i + insn->off + 1); |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 6604 | insn->imm = subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6605 | } |
| 6606 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6607 | prog->jited = 1; |
| 6608 | prog->bpf_func = func[0]->bpf_func; |
| 6609 | prog->aux->func = func; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6610 | prog->aux->func_cnt = env->subprog_cnt; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6611 | bpf_prog_free_unused_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6612 | return 0; |
| 6613 | out_free: |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6614 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6615 | if (func[i]) |
| 6616 | bpf_jit_free(func[i]); |
| 6617 | kfree(func); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 6618 | out_undo_insn: |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6619 | /* cleanup main prog to be interpreted */ |
| 6620 | prog->jit_requested = 0; |
| 6621 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 6622 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 6623 | insn->src_reg != BPF_PSEUDO_CALL) |
| 6624 | continue; |
| 6625 | insn->off = 0; |
| 6626 | insn->imm = env->insn_aux_data[i].call_imm; |
| 6627 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6628 | bpf_prog_free_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6629 | return err; |
| 6630 | } |
| 6631 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6632 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 6633 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6634 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6635 | struct bpf_prog *prog = env->prog; |
| 6636 | struct bpf_insn *insn = prog->insnsi; |
| 6637 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6638 | #endif |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 6639 | int err = 0; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6640 | |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 6641 | if (env->prog->jit_requested && |
| 6642 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6643 | err = jit_subprogs(env); |
| 6644 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6645 | return 0; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 6646 | if (err == -EFAULT) |
| 6647 | return err; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6648 | } |
| 6649 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6650 | for (i = 0; i < prog->len; i++, insn++) { |
| 6651 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 6652 | insn->src_reg != BPF_PSEUDO_CALL) |
| 6653 | continue; |
| 6654 | depth = get_callee_stack_depth(env, insn, i); |
| 6655 | if (depth < 0) |
| 6656 | return depth; |
| 6657 | bpf_patch_call_args(insn, depth); |
| 6658 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6659 | err = 0; |
| 6660 | #endif |
| 6661 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6662 | } |
| 6663 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6664 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6665 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6666 | * |
| 6667 | * this function is called after eBPF program passed verification |
| 6668 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6669 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6670 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6671 | struct bpf_prog *prog = env->prog; |
| 6672 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6673 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6674 | const int insn_cnt = prog->len; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6675 | const struct bpf_map_ops *ops; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6676 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6677 | struct bpf_insn insn_buf[16]; |
| 6678 | struct bpf_prog *new_prog; |
| 6679 | struct bpf_map *map_ptr; |
| 6680 | int i, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6681 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6682 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 6683 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 6684 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 6685 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 6686 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 6687 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
| 6688 | struct bpf_insn mask_and_div[] = { |
| 6689 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 6690 | /* Rx div 0 -> 0 */ |
| 6691 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), |
| 6692 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 6693 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 6694 | *insn, |
| 6695 | }; |
| 6696 | struct bpf_insn mask_and_mod[] = { |
| 6697 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 6698 | /* Rx mod 0 -> Rx */ |
| 6699 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), |
| 6700 | *insn, |
| 6701 | }; |
| 6702 | struct bpf_insn *patchlet; |
| 6703 | |
| 6704 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 6705 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 6706 | patchlet = mask_and_div + (is64 ? 1 : 0); |
| 6707 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); |
| 6708 | } else { |
| 6709 | patchlet = mask_and_mod + (is64 ? 1 : 0); |
| 6710 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); |
| 6711 | } |
| 6712 | |
| 6713 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 6714 | if (!new_prog) |
| 6715 | return -ENOMEM; |
| 6716 | |
| 6717 | delta += cnt - 1; |
| 6718 | env->prog = prog = new_prog; |
| 6719 | insn = new_prog->insnsi + i + delta; |
| 6720 | continue; |
| 6721 | } |
| 6722 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 6723 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 6724 | (BPF_MODE(insn->code) == BPF_ABS || |
| 6725 | BPF_MODE(insn->code) == BPF_IND)) { |
| 6726 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 6727 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 6728 | verbose(env, "bpf verifier is misconfigured\n"); |
| 6729 | return -EINVAL; |
| 6730 | } |
| 6731 | |
| 6732 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 6733 | if (!new_prog) |
| 6734 | return -ENOMEM; |
| 6735 | |
| 6736 | delta += cnt - 1; |
| 6737 | env->prog = prog = new_prog; |
| 6738 | insn = new_prog->insnsi + i + delta; |
| 6739 | continue; |
| 6740 | } |
| 6741 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6742 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 6743 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 6744 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 6745 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6746 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6747 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 6748 | prog->dst_needed = 1; |
| 6749 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 6750 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 6751 | if (insn->imm == BPF_FUNC_override_return) |
| 6752 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6753 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 6754 | /* If we tail call into other programs, we |
| 6755 | * cannot make any assumptions since they can |
| 6756 | * be replaced dynamically during runtime in |
| 6757 | * the program array. |
| 6758 | */ |
| 6759 | prog->cb_access = 1; |
Alexei Starovoitov | 80a58d0 | 2017-05-30 13:31:30 -0700 | [diff] [blame] | 6760 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 6761 | env->prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 6762 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6763 | /* mark bpf_tail_call as different opcode to avoid |
| 6764 | * conditional branch in the interpeter for every normal |
| 6765 | * call and to prevent accidental JITing by JIT compiler |
| 6766 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6767 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6768 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 6769 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 6770 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6771 | aux = &env->insn_aux_data[i + delta]; |
| 6772 | if (!bpf_map_ptr_unpriv(aux)) |
| 6773 | continue; |
| 6774 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 6775 | /* instead of changing every JIT dealing with tail_call |
| 6776 | * emit two extra insns: |
| 6777 | * if (index >= max_entries) goto out; |
| 6778 | * index &= array->index_mask; |
| 6779 | * to avoid out-of-bounds cpu speculation |
| 6780 | */ |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6781 | if (bpf_map_ptr_poisoned(aux)) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 6782 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 6783 | return -EINVAL; |
| 6784 | } |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6785 | |
| 6786 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 6787 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 6788 | map_ptr->max_entries, 2); |
| 6789 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 6790 | container_of(map_ptr, |
| 6791 | struct bpf_array, |
| 6792 | map)->index_mask); |
| 6793 | insn_buf[2] = *insn; |
| 6794 | cnt = 3; |
| 6795 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 6796 | if (!new_prog) |
| 6797 | return -ENOMEM; |
| 6798 | |
| 6799 | delta += cnt - 1; |
| 6800 | env->prog = prog = new_prog; |
| 6801 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6802 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6803 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6804 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 6805 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6806 | * and other inlining handlers are currently limited to 64 bit |
| 6807 | * only. |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 6808 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 6809 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6810 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 6811 | insn->imm == BPF_FUNC_map_update_elem || |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 6812 | insn->imm == BPF_FUNC_map_delete_elem || |
| 6813 | insn->imm == BPF_FUNC_map_push_elem || |
| 6814 | insn->imm == BPF_FUNC_map_pop_elem || |
| 6815 | insn->imm == BPF_FUNC_map_peek_elem)) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6816 | aux = &env->insn_aux_data[i + delta]; |
| 6817 | if (bpf_map_ptr_poisoned(aux)) |
| 6818 | goto patch_call_imm; |
| 6819 | |
| 6820 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6821 | ops = map_ptr->ops; |
| 6822 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 6823 | ops->map_gen_lookup) { |
| 6824 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
| 6825 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 6826 | verbose(env, "bpf verifier is misconfigured\n"); |
| 6827 | return -EINVAL; |
| 6828 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6829 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6830 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 6831 | insn_buf, cnt); |
| 6832 | if (!new_prog) |
| 6833 | return -ENOMEM; |
| 6834 | |
| 6835 | delta += cnt - 1; |
| 6836 | env->prog = prog = new_prog; |
| 6837 | insn = new_prog->insnsi + i + delta; |
| 6838 | continue; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6839 | } |
| 6840 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6841 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 6842 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 6843 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 6844 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 6845 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 6846 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 6847 | u64 flags))NULL)); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 6848 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 6849 | (int (*)(struct bpf_map *map, void *value, |
| 6850 | u64 flags))NULL)); |
| 6851 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 6852 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 6853 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 6854 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 6855 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6856 | switch (insn->imm) { |
| 6857 | case BPF_FUNC_map_lookup_elem: |
| 6858 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - |
| 6859 | __bpf_call_base; |
| 6860 | continue; |
| 6861 | case BPF_FUNC_map_update_elem: |
| 6862 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - |
| 6863 | __bpf_call_base; |
| 6864 | continue; |
| 6865 | case BPF_FUNC_map_delete_elem: |
| 6866 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - |
| 6867 | __bpf_call_base; |
| 6868 | continue; |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 6869 | case BPF_FUNC_map_push_elem: |
| 6870 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - |
| 6871 | __bpf_call_base; |
| 6872 | continue; |
| 6873 | case BPF_FUNC_map_pop_elem: |
| 6874 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - |
| 6875 | __bpf_call_base; |
| 6876 | continue; |
| 6877 | case BPF_FUNC_map_peek_elem: |
| 6878 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - |
| 6879 | __bpf_call_base; |
| 6880 | continue; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6881 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6882 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6883 | goto patch_call_imm; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6884 | } |
| 6885 | |
| 6886 | patch_call_imm: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 6887 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6888 | /* all functions that have prototype and verifier allowed |
| 6889 | * programs to call them, must be real in-kernel functions |
| 6890 | */ |
| 6891 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6892 | verbose(env, |
| 6893 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6894 | func_id_name(insn->imm), insn->imm); |
| 6895 | return -EFAULT; |
| 6896 | } |
| 6897 | insn->imm = fn->func - __bpf_call_base; |
| 6898 | } |
| 6899 | |
| 6900 | return 0; |
| 6901 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6902 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6903 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6904 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6905 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6906 | int i; |
| 6907 | |
| 6908 | if (!env->explored_states) |
| 6909 | return; |
| 6910 | |
| 6911 | for (i = 0; i < env->prog->len; i++) { |
| 6912 | sl = env->explored_states[i]; |
| 6913 | |
| 6914 | if (sl) |
| 6915 | while (sl != STATE_LIST_MARK) { |
| 6916 | sln = sl->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 6917 | free_verifier_state(&sl->state, false); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6918 | kfree(sl); |
| 6919 | sl = sln; |
| 6920 | } |
| 6921 | } |
| 6922 | |
| 6923 | kfree(env->explored_states); |
| 6924 | } |
| 6925 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6926 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
| 6927 | union bpf_attr __user *uattr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 6928 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6929 | struct bpf_verifier_env *env; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 6930 | struct bpf_verifier_log *log; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 6931 | int ret = -EINVAL; |
| 6932 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 6933 | /* no program is valid */ |
| 6934 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 6935 | return -EINVAL; |
| 6936 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6937 | /* '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] | 6938 | * allocate/free it every time bpf_check() is called |
| 6939 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6940 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6941 | if (!env) |
| 6942 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6943 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6944 | |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 6945 | env->insn_aux_data = |
| 6946 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), |
| 6947 | (*prog)->len)); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6948 | ret = -ENOMEM; |
| 6949 | if (!env->insn_aux_data) |
| 6950 | goto err_free_env; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6951 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 6952 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6953 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6954 | /* grab the mutex to protect few globals used by verifier */ |
| 6955 | mutex_lock(&bpf_verifier_lock); |
| 6956 | |
| 6957 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 6958 | /* user requested verbose verifier output |
| 6959 | * and supplied buffer to store the verification trace |
| 6960 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 6961 | log->level = attr->log_level; |
| 6962 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 6963 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6964 | |
| 6965 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 6966 | /* log attributes have to be sane */ |
| 6967 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || |
| 6968 | !log->level || !log->ubuf) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6969 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6970 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 6971 | |
| 6972 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 6973 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 6974 | env->strict_alignment = true; |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 6975 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
| 6976 | env->strict_alignment = false; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6977 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6978 | ret = replace_map_fd_with_map_ptr(env); |
| 6979 | if (ret < 0) |
| 6980 | goto skip_full_check; |
| 6981 | |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 6982 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Quentin Monnet | a40a263 | 2018-11-09 13:03:31 +0000 | [diff] [blame] | 6983 | ret = bpf_prog_offload_verifier_prep(env->prog); |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 6984 | if (ret) |
| 6985 | goto skip_full_check; |
| 6986 | } |
| 6987 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6988 | env->explored_states = kcalloc(env->prog->len, |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6989 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6990 | GFP_USER); |
| 6991 | ret = -ENOMEM; |
| 6992 | if (!env->explored_states) |
| 6993 | goto skip_full_check; |
| 6994 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 6995 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 6996 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 6997 | ret = check_subprogs(env); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6998 | if (ret < 0) |
| 6999 | goto skip_full_check; |
| 7000 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 7001 | ret = check_btf_info(env, attr, uattr); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 7002 | if (ret < 0) |
| 7003 | goto skip_full_check; |
| 7004 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 7005 | ret = check_cfg(env); |
| 7006 | if (ret < 0) |
| 7007 | goto skip_full_check; |
| 7008 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 7009 | ret = do_check(env); |
Craig Gallek | 8c01c4f | 2017-11-02 11:18:01 -0400 | [diff] [blame] | 7010 | if (env->cur_state) { |
| 7011 | free_verifier_state(env->cur_state, true); |
| 7012 | env->cur_state = NULL; |
| 7013 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 7014 | |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 7015 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 7016 | ret = bpf_prog_offload_finalize(env); |
| 7017 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7018 | skip_full_check: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 7019 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 7020 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7021 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7022 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 7023 | ret = check_max_stack_depth(env); |
| 7024 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 7025 | /* instruction rewrites happen after this point */ |
| 7026 | if (ret == 0) |
| 7027 | sanitize_dead_code(env); |
| 7028 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 7029 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7030 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 7031 | ret = convert_ctx_accesses(env); |
| 7032 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 7033 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 7034 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 7035 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 7036 | if (ret == 0) |
| 7037 | ret = fixup_call_args(env); |
| 7038 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 7039 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 7040 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 7041 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 7042 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 7043 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 7044 | } |
| 7045 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7046 | if (ret == 0 && env->used_map_cnt) { |
| 7047 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7048 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 7049 | sizeof(env->used_maps[0]), |
| 7050 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7051 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7052 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7053 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 7054 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7055 | } |
| 7056 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7057 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7058 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7059 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7060 | |
| 7061 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 7062 | * bpf_ld_imm64 instructions |
| 7063 | */ |
| 7064 | convert_pseudo_ld_imm64(env); |
| 7065 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 7066 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 7067 | if (ret == 0) |
| 7068 | adjust_btf_func(env); |
| 7069 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 7070 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7071 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7072 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 7073 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 7074 | */ |
| 7075 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 7076 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7077 | err_unlock: |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 7078 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 7079 | vfree(env->insn_aux_data); |
| 7080 | err_free_env: |
| 7081 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 7082 | return ret; |
| 7083 | } |