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 | */ |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/bpf.h> |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 18 | #include <linux/bpf_verifier.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 19 | #include <linux/filter.h> |
| 20 | #include <net/netlink.h> |
| 21 | #include <linux/file.h> |
| 22 | #include <linux/vmalloc.h> |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 23 | #include <linux/stringify.h> |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 24 | #include <linux/bsearch.h> |
| 25 | #include <linux/sort.h> |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 26 | #include <linux/perf_event.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 27 | |
Jakub Kicinski | f4ac7e0 | 2017-10-09 10:30:12 -0700 | [diff] [blame] | 28 | #include "disasm.h" |
| 29 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 30 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
| 31 | #define BPF_PROG_TYPE(_id, _name) \ |
| 32 | [_id] = & _name ## _verifier_ops, |
| 33 | #define BPF_MAP_TYPE(_id, _ops) |
| 34 | #include <linux/bpf_types.h> |
| 35 | #undef BPF_PROG_TYPE |
| 36 | #undef BPF_MAP_TYPE |
| 37 | }; |
| 38 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 39 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 40 | * instruction by instruction and updates register/stack state. |
| 41 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 42 | * |
| 43 | * The first pass is depth-first-search to check that the program is a DAG. |
| 44 | * It rejects the following programs: |
| 45 | * - larger than BPF_MAXINSNS insns |
| 46 | * - if loop is present (detected via back-edge) |
| 47 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 48 | * - out of bounds or malformed jumps |
| 49 | * The second pass is all possible path descent from the 1st insn. |
| 50 | * Since it's analyzing all pathes through the program, the length of the |
Gary Lin | eba38a9 | 2017-03-01 16:25:51 +0800 | [diff] [blame] | 51 | * analysis is limited to 64k insn, which may be hit even if total number of |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 52 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 53 | * Number of 'branches to be analyzed' is limited to 1k |
| 54 | * |
| 55 | * On entry to each instruction, each register has a type, and the instruction |
| 56 | * changes the types of the registers depending on instruction semantics. |
| 57 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 58 | * copied to R1. |
| 59 | * |
| 60 | * All registers are 64-bit. |
| 61 | * R0 - return register |
| 62 | * R1-R5 argument passing registers |
| 63 | * R6-R9 callee saved registers |
| 64 | * R10 - frame pointer read-only |
| 65 | * |
| 66 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 67 | * and has type PTR_TO_CTX. |
| 68 | * |
| 69 | * Verifier tracks arithmetic operations on pointers in case: |
| 70 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 71 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 72 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 73 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 74 | * that it wants to construct a pointer to some element within stack. |
| 75 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 76 | * (and -20 constant is saved for further stack bounds checking). |
| 77 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 78 | * |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 79 | * Most of the time the registers have SCALAR_VALUE type, which |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 80 | * means the register has some value, but it's not a valid pointer. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 81 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 82 | * |
| 83 | * When verifier sees load or store instructions the type of base register |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 84 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
| 85 | * four pointer types recognized by check_mem_access() function. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 86 | * |
| 87 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 88 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 89 | * |
| 90 | * registers used to pass values to function calls are checked against |
| 91 | * function argument constraints. |
| 92 | * |
| 93 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 94 | * It means that the register type passed to this function must be |
| 95 | * PTR_TO_STACK and it will be used inside the function as |
| 96 | * 'pointer to map element key' |
| 97 | * |
| 98 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 99 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 100 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 101 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 102 | * |
| 103 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 104 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 105 | * 2nd argument should be a pointer to stack, which will be used inside |
| 106 | * the helper function as a pointer to map element key. |
| 107 | * |
| 108 | * On the kernel side the helper function looks like: |
| 109 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 110 | * { |
| 111 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 112 | * void *key = (void *) (unsigned long) r2; |
| 113 | * void *value; |
| 114 | * |
| 115 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 116 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 117 | * the stack of eBPF program. |
| 118 | * } |
| 119 | * |
| 120 | * Corresponding eBPF program may look like: |
| 121 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 122 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 123 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 124 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 125 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 126 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 127 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 128 | * |
| 129 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 130 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 131 | * and were initialized prior to this call. |
| 132 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 133 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 134 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 135 | * returns ether pointer to map value or NULL. |
| 136 | * |
| 137 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 138 | * insn, the register holding that pointer in the true branch changes state to |
| 139 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 140 | * branch. See check_cond_jmp_op(). |
| 141 | * |
| 142 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 143 | * are set to NOT_INIT to indicate that they are no longer readable. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 144 | * |
| 145 | * The following reference types represent a potential reference to a kernel |
| 146 | * resource which, after first being allocated, must be checked and freed by |
| 147 | * the BPF program: |
| 148 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET |
| 149 | * |
| 150 | * When the verifier sees a helper call return a reference type, it allocates a |
| 151 | * pointer id for the reference and stores it in the current function state. |
| 152 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into |
| 153 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type |
| 154 | * passes through a NULL-check conditional. For the branch wherein the state is |
| 155 | * changed to CONST_IMM, the verifier releases the reference. |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 156 | * |
| 157 | * For each helper function that allocates a reference, such as |
| 158 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as |
| 159 | * bpf_sk_release(). When a reference type passes into the release function, |
| 160 | * the verifier also releases the reference. If any unchecked or unreleased |
| 161 | * reference remains at the end of the program, the verifier rejects it. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 162 | */ |
| 163 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 164 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 165 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 166 | /* verifer state is 'st' |
| 167 | * before processing instruction 'insn_idx' |
| 168 | * and after processing instruction 'prev_insn_idx' |
| 169 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 170 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 171 | int insn_idx; |
| 172 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 173 | struct bpf_verifier_stack_elem *next; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Edward Cree | 8e17c1b | 2017-08-07 15:30:30 +0100 | [diff] [blame] | 176 | #define BPF_COMPLEXITY_LIMIT_INSNS 131072 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 177 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
| 178 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 179 | #define BPF_MAP_PTR_UNPRIV 1UL |
| 180 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ |
| 181 | POISON_POINTER_DELTA)) |
| 182 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) |
| 183 | |
| 184 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
| 185 | { |
| 186 | return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON; |
| 187 | } |
| 188 | |
| 189 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) |
| 190 | { |
| 191 | return aux->map_state & BPF_MAP_PTR_UNPRIV; |
| 192 | } |
| 193 | |
| 194 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, |
| 195 | const struct bpf_map *map, bool unpriv) |
| 196 | { |
| 197 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); |
| 198 | unpriv |= bpf_map_ptr_unpriv(aux); |
| 199 | aux->map_state = (unsigned long)map | |
| 200 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); |
| 201 | } |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 202 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 203 | struct bpf_call_arg_meta { |
| 204 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 205 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 206 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 207 | int regno; |
| 208 | int access_size; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 209 | s64 msize_smax_value; |
| 210 | u64 msize_umax_value; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 211 | int ptr_id; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 212 | }; |
| 213 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 214 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 215 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 216 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
| 217 | va_list args) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 218 | { |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 219 | unsigned int n; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 220 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 221 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 222 | |
| 223 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 224 | "verifier log line truncated - local buffer too short\n"); |
| 225 | |
| 226 | n = min(log->len_total - log->len_used - 1, n); |
| 227 | log->kbuf[n] = '\0'; |
| 228 | |
| 229 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 230 | log->len_used += n; |
| 231 | else |
| 232 | log->ubuf = NULL; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 233 | } |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 234 | |
| 235 | /* log_level controls verbosity level of eBPF verifier. |
| 236 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 237 | * so the user can figure out what's wrong with the program |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 238 | */ |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 239 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 240 | const char *fmt, ...) |
| 241 | { |
| 242 | va_list args; |
| 243 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 244 | if (!bpf_verifier_log_needed(&env->log)) |
| 245 | return; |
| 246 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 247 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 248 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 249 | va_end(args); |
| 250 | } |
| 251 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 252 | |
| 253 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
| 254 | { |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 255 | struct bpf_verifier_env *env = private_data; |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 256 | va_list args; |
| 257 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 258 | if (!bpf_verifier_log_needed(&env->log)) |
| 259 | return; |
| 260 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 261 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 262 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 263 | va_end(args); |
| 264 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 265 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 266 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 267 | { |
| 268 | return type == PTR_TO_PACKET || |
| 269 | type == PTR_TO_PACKET_META; |
| 270 | } |
| 271 | |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 272 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
| 273 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 274 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
| 275 | type == PTR_TO_SOCKET_OR_NULL; |
| 276 | } |
| 277 | |
| 278 | static bool type_is_refcounted(enum bpf_reg_type type) |
| 279 | { |
| 280 | return type == PTR_TO_SOCKET; |
| 281 | } |
| 282 | |
| 283 | static bool type_is_refcounted_or_null(enum bpf_reg_type type) |
| 284 | { |
| 285 | return type == PTR_TO_SOCKET || type == PTR_TO_SOCKET_OR_NULL; |
| 286 | } |
| 287 | |
| 288 | static bool reg_is_refcounted(const struct bpf_reg_state *reg) |
| 289 | { |
| 290 | return type_is_refcounted(reg->type); |
| 291 | } |
| 292 | |
| 293 | static bool reg_is_refcounted_or_null(const struct bpf_reg_state *reg) |
| 294 | { |
| 295 | return type_is_refcounted_or_null(reg->type); |
| 296 | } |
| 297 | |
| 298 | static bool arg_type_is_refcounted(enum bpf_arg_type type) |
| 299 | { |
| 300 | return type == ARG_PTR_TO_SOCKET; |
| 301 | } |
| 302 | |
| 303 | /* Determine whether the function releases some resources allocated by another |
| 304 | * function call. The first reference type argument will be assumed to be |
| 305 | * released by release_reference(). |
| 306 | */ |
| 307 | static bool is_release_function(enum bpf_func_id func_id) |
| 308 | { |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 309 | return func_id == BPF_FUNC_sk_release; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 312 | /* string representation of 'enum bpf_reg_type' */ |
| 313 | static const char * const reg_type_str[] = { |
| 314 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 315 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 316 | [PTR_TO_CTX] = "ctx", |
| 317 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 318 | [PTR_TO_MAP_VALUE] = "map_value", |
| 319 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 320 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 321 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 322 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 323 | [PTR_TO_PACKET_END] = "pkt_end", |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 324 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 325 | [PTR_TO_SOCKET] = "sock", |
| 326 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 327 | }; |
| 328 | |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 329 | static char slot_type_char[] = { |
| 330 | [STACK_INVALID] = '?', |
| 331 | [STACK_SPILL] = 'r', |
| 332 | [STACK_MISC] = 'm', |
| 333 | [STACK_ZERO] = '0', |
| 334 | }; |
| 335 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 336 | static void print_liveness(struct bpf_verifier_env *env, |
| 337 | enum bpf_reg_liveness live) |
| 338 | { |
| 339 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN)) |
| 340 | verbose(env, "_"); |
| 341 | if (live & REG_LIVE_READ) |
| 342 | verbose(env, "r"); |
| 343 | if (live & REG_LIVE_WRITTEN) |
| 344 | verbose(env, "w"); |
| 345 | } |
| 346 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 347 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 348 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 349 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 350 | struct bpf_verifier_state *cur = env->cur_state; |
| 351 | |
| 352 | return cur->frame[reg->frameno]; |
| 353 | } |
| 354 | |
| 355 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 356 | const struct bpf_func_state *state) |
| 357 | { |
| 358 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 359 | enum bpf_reg_type t; |
| 360 | int i; |
| 361 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 362 | if (state->frameno) |
| 363 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 364 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 365 | reg = &state->regs[i]; |
| 366 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 367 | if (t == NOT_INIT) |
| 368 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 369 | verbose(env, " R%d", i); |
| 370 | print_liveness(env, reg->live); |
| 371 | verbose(env, "=%s", reg_type_str[t]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 372 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 373 | tnum_is_const(reg->var_off)) { |
| 374 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 375 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 376 | if (t == PTR_TO_STACK) |
| 377 | verbose(env, ",call_%d", func(env, reg)->callsite); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 378 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 379 | verbose(env, "(id=%d", reg->id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 380 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 381 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 382 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 383 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 384 | else if (t == CONST_PTR_TO_MAP || |
| 385 | t == PTR_TO_MAP_VALUE || |
| 386 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 387 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 388 | reg->map_ptr->key_size, |
| 389 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 390 | if (tnum_is_const(reg->var_off)) { |
| 391 | /* Typically an immediate SCALAR_VALUE, but |
| 392 | * could be a pointer whose offset is too big |
| 393 | * for reg->off |
| 394 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 395 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 396 | } else { |
| 397 | if (reg->smin_value != reg->umin_value && |
| 398 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 399 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 400 | (long long)reg->smin_value); |
| 401 | if (reg->smax_value != reg->umax_value && |
| 402 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 403 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 404 | (long long)reg->smax_value); |
| 405 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 406 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 407 | (unsigned long long)reg->umin_value); |
| 408 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 409 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 410 | (unsigned long long)reg->umax_value); |
| 411 | if (!tnum_is_unknown(reg->var_off)) { |
| 412 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 413 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 414 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 415 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 416 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 417 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 418 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 419 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 420 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 421 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 422 | char types_buf[BPF_REG_SIZE + 1]; |
| 423 | bool valid = false; |
| 424 | int j; |
| 425 | |
| 426 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 427 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 428 | valid = true; |
| 429 | types_buf[j] = slot_type_char[ |
| 430 | state->stack[i].slot_type[j]]; |
| 431 | } |
| 432 | types_buf[BPF_REG_SIZE] = 0; |
| 433 | if (!valid) |
| 434 | continue; |
| 435 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 436 | print_liveness(env, state->stack[i].spilled_ptr.live); |
| 437 | if (state->stack[i].slot_type[0] == STACK_SPILL) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 438 | verbose(env, "=%s", |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 439 | reg_type_str[state->stack[i].spilled_ptr.type]); |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 440 | else |
| 441 | verbose(env, "=%s", types_buf); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 442 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 443 | if (state->acquired_refs && state->refs[0].id) { |
| 444 | verbose(env, " refs=%d", state->refs[0].id); |
| 445 | for (i = 1; i < state->acquired_refs; i++) |
| 446 | if (state->refs[i].id) |
| 447 | verbose(env, ",%d", state->refs[i].id); |
| 448 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 449 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 452 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 453 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ |
| 454 | const struct bpf_func_state *src) \ |
| 455 | { \ |
| 456 | if (!src->FIELD) \ |
| 457 | return 0; \ |
| 458 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ |
| 459 | /* internal bug, make state invalid to reject the program */ \ |
| 460 | memset(dst, 0, sizeof(*dst)); \ |
| 461 | return -EFAULT; \ |
| 462 | } \ |
| 463 | memcpy(dst->FIELD, src->FIELD, \ |
| 464 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ |
| 465 | return 0; \ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 466 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 467 | /* copy_reference_state() */ |
| 468 | COPY_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 469 | /* copy_stack_state() */ |
| 470 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 471 | #undef COPY_STATE_FN |
| 472 | |
| 473 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 474 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ |
| 475 | bool copy_old) \ |
| 476 | { \ |
| 477 | u32 old_size = state->COUNT; \ |
| 478 | struct bpf_##NAME##_state *new_##FIELD; \ |
| 479 | int slot = size / SIZE; \ |
| 480 | \ |
| 481 | if (size <= old_size || !size) { \ |
| 482 | if (copy_old) \ |
| 483 | return 0; \ |
| 484 | state->COUNT = slot * SIZE; \ |
| 485 | if (!size && old_size) { \ |
| 486 | kfree(state->FIELD); \ |
| 487 | state->FIELD = NULL; \ |
| 488 | } \ |
| 489 | return 0; \ |
| 490 | } \ |
| 491 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ |
| 492 | GFP_KERNEL); \ |
| 493 | if (!new_##FIELD) \ |
| 494 | return -ENOMEM; \ |
| 495 | if (copy_old) { \ |
| 496 | if (state->FIELD) \ |
| 497 | memcpy(new_##FIELD, state->FIELD, \ |
| 498 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ |
| 499 | memset(new_##FIELD + old_size / SIZE, 0, \ |
| 500 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ |
| 501 | } \ |
| 502 | state->COUNT = slot * SIZE; \ |
| 503 | kfree(state->FIELD); \ |
| 504 | state->FIELD = new_##FIELD; \ |
| 505 | return 0; \ |
| 506 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 507 | /* realloc_reference_state() */ |
| 508 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 509 | /* realloc_stack_state() */ |
| 510 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 511 | #undef REALLOC_STATE_FN |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 512 | |
| 513 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 514 | * make it consume minimal amount of memory. check_stack_write() access from |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 515 | * the program calls into realloc_func_state() to grow the stack size. |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 516 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 517 | * which realloc_stack_state() copies over. It points to previous |
| 518 | * bpf_verifier_state which is never reallocated. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 519 | */ |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 520 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
| 521 | int refs_size, bool copy_old) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 522 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 523 | int err = realloc_reference_state(state, refs_size, copy_old); |
| 524 | if (err) |
| 525 | return err; |
| 526 | return realloc_stack_state(state, stack_size, copy_old); |
| 527 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 528 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 529 | /* Acquire a pointer id from the env and update the state->refs to include |
| 530 | * this new pointer reference. |
| 531 | * On success, returns a valid pointer id to associate with the register |
| 532 | * On failure, returns a negative errno. |
| 533 | */ |
| 534 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 535 | { |
| 536 | struct bpf_func_state *state = cur_func(env); |
| 537 | int new_ofs = state->acquired_refs; |
| 538 | int id, err; |
| 539 | |
| 540 | err = realloc_reference_state(state, state->acquired_refs + 1, true); |
| 541 | if (err) |
| 542 | return err; |
| 543 | id = ++env->id_gen; |
| 544 | state->refs[new_ofs].id = id; |
| 545 | state->refs[new_ofs].insn_idx = insn_idx; |
| 546 | |
| 547 | return id; |
| 548 | } |
| 549 | |
| 550 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
| 551 | static int __release_reference_state(struct bpf_func_state *state, int ptr_id) |
| 552 | { |
| 553 | int i, last_idx; |
| 554 | |
| 555 | if (!ptr_id) |
| 556 | return -EFAULT; |
| 557 | |
| 558 | last_idx = state->acquired_refs - 1; |
| 559 | for (i = 0; i < state->acquired_refs; i++) { |
| 560 | if (state->refs[i].id == ptr_id) { |
| 561 | if (last_idx && i != last_idx) |
| 562 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 563 | sizeof(*state->refs)); |
| 564 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 565 | state->acquired_refs--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 566 | return 0; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 567 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 568 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 569 | return -EFAULT; |
| 570 | } |
| 571 | |
| 572 | /* variation on the above for cases where we expect that there must be an |
| 573 | * outstanding reference for the specified ptr_id. |
| 574 | */ |
| 575 | static int release_reference_state(struct bpf_verifier_env *env, int ptr_id) |
| 576 | { |
| 577 | struct bpf_func_state *state = cur_func(env); |
| 578 | int err; |
| 579 | |
| 580 | err = __release_reference_state(state, ptr_id); |
| 581 | if (WARN_ON_ONCE(err != 0)) |
| 582 | verbose(env, "verifier internal error: can't release reference\n"); |
| 583 | return err; |
| 584 | } |
| 585 | |
| 586 | static int transfer_reference_state(struct bpf_func_state *dst, |
| 587 | struct bpf_func_state *src) |
| 588 | { |
| 589 | int err = realloc_reference_state(dst, src->acquired_refs, false); |
| 590 | if (err) |
| 591 | return err; |
| 592 | err = copy_reference_state(dst, src); |
| 593 | if (err) |
| 594 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 595 | return 0; |
| 596 | } |
| 597 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 598 | static void free_func_state(struct bpf_func_state *state) |
| 599 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 600 | if (!state) |
| 601 | return; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 602 | kfree(state->refs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 603 | kfree(state->stack); |
| 604 | kfree(state); |
| 605 | } |
| 606 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 607 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 608 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 609 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 610 | int i; |
| 611 | |
| 612 | for (i = 0; i <= state->curframe; i++) { |
| 613 | free_func_state(state->frame[i]); |
| 614 | state->frame[i] = NULL; |
| 615 | } |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 616 | if (free_self) |
| 617 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* copy verifier state from src to dst growing dst stack space |
| 621 | * when necessary to accommodate larger src stack |
| 622 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 623 | static int copy_func_state(struct bpf_func_state *dst, |
| 624 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 625 | { |
| 626 | int err; |
| 627 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 628 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
| 629 | false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 630 | if (err) |
| 631 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 632 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 633 | err = copy_reference_state(dst, src); |
| 634 | if (err) |
| 635 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 636 | return copy_stack_state(dst, src); |
| 637 | } |
| 638 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 639 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 640 | const struct bpf_verifier_state *src) |
| 641 | { |
| 642 | struct bpf_func_state *dst; |
| 643 | int i, err; |
| 644 | |
| 645 | /* if dst has more stack frames then src frame, free them */ |
| 646 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 647 | free_func_state(dst_state->frame[i]); |
| 648 | dst_state->frame[i] = NULL; |
| 649 | } |
| 650 | dst_state->curframe = src->curframe; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 651 | for (i = 0; i <= src->curframe; i++) { |
| 652 | dst = dst_state->frame[i]; |
| 653 | if (!dst) { |
| 654 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 655 | if (!dst) |
| 656 | return -ENOMEM; |
| 657 | dst_state->frame[i] = dst; |
| 658 | } |
| 659 | err = copy_func_state(dst, src->frame[i]); |
| 660 | if (err) |
| 661 | return err; |
| 662 | } |
| 663 | return 0; |
| 664 | } |
| 665 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 666 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
| 667 | int *insn_idx) |
| 668 | { |
| 669 | struct bpf_verifier_state *cur = env->cur_state; |
| 670 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 671 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 672 | |
| 673 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 674 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 675 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 676 | if (cur) { |
| 677 | err = copy_verifier_state(cur, &head->st); |
| 678 | if (err) |
| 679 | return err; |
| 680 | } |
| 681 | if (insn_idx) |
| 682 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 683 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 684 | *prev_insn_idx = head->prev_insn_idx; |
| 685 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 686 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 687 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 688 | env->head = elem; |
| 689 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 690 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 693 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
| 694 | int insn_idx, int prev_insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 695 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 696 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 697 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 698 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 699 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 700 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 701 | if (!elem) |
| 702 | goto err; |
| 703 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 704 | elem->insn_idx = insn_idx; |
| 705 | elem->prev_insn_idx = prev_insn_idx; |
| 706 | elem->next = env->head; |
| 707 | env->head = elem; |
| 708 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 709 | err = copy_verifier_state(&elem->st, cur); |
| 710 | if (err) |
| 711 | goto err; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 712 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 713 | verbose(env, "BPF program is too complex\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 714 | goto err; |
| 715 | } |
| 716 | return &elem->st; |
| 717 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 718 | free_verifier_state(env->cur_state, true); |
| 719 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 720 | /* pop all elements and return */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 721 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 722 | return NULL; |
| 723 | } |
| 724 | |
| 725 | #define CALLER_SAVED_REGS 6 |
| 726 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 727 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 728 | }; |
| 729 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 730 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
| 731 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 732 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 733 | * known to have the value @imm. |
| 734 | */ |
| 735 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 736 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 737 | /* Clear id, off, and union(map_ptr, range) */ |
| 738 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 739 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 740 | reg->var_off = tnum_const(imm); |
| 741 | reg->smin_value = (s64)imm; |
| 742 | reg->smax_value = (s64)imm; |
| 743 | reg->umin_value = imm; |
| 744 | reg->umax_value = imm; |
| 745 | } |
| 746 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 747 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 748 | * used only on registers holding a pointer type. |
| 749 | */ |
| 750 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 751 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 752 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 753 | } |
| 754 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 755 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 756 | { |
| 757 | __mark_reg_known(reg, 0); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 758 | reg->type = SCALAR_VALUE; |
| 759 | } |
| 760 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 761 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 762 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 763 | { |
| 764 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 765 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 766 | /* Something bad happened, let's kill all regs */ |
| 767 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 768 | __mark_reg_not_init(regs + regno); |
| 769 | return; |
| 770 | } |
| 771 | __mark_reg_known_zero(regs + regno); |
| 772 | } |
| 773 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 774 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 775 | { |
| 776 | return type_is_pkt_pointer(reg->type); |
| 777 | } |
| 778 | |
| 779 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 780 | { |
| 781 | return reg_is_pkt_pointer(reg) || |
| 782 | reg->type == PTR_TO_PACKET_END; |
| 783 | } |
| 784 | |
| 785 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 786 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 787 | enum bpf_reg_type which) |
| 788 | { |
| 789 | /* The register can already have a range from prior markings. |
| 790 | * This is fine as long as it hasn't been advanced from its |
| 791 | * origin. |
| 792 | */ |
| 793 | return reg->type == which && |
| 794 | reg->id == 0 && |
| 795 | reg->off == 0 && |
| 796 | tnum_equals_const(reg->var_off, 0); |
| 797 | } |
| 798 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 799 | /* Attempts to improve min/max values based on var_off information */ |
| 800 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 801 | { |
| 802 | /* min signed is max(sign bit) | min(other bits) */ |
| 803 | reg->smin_value = max_t(s64, reg->smin_value, |
| 804 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 805 | /* max signed is min(sign bit) | max(other bits) */ |
| 806 | reg->smax_value = min_t(s64, reg->smax_value, |
| 807 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 808 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 809 | reg->umax_value = min(reg->umax_value, |
| 810 | reg->var_off.value | reg->var_off.mask); |
| 811 | } |
| 812 | |
| 813 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
| 814 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 815 | { |
| 816 | /* Learn sign from signed bounds. |
| 817 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 818 | * are the same, so combine. This works even in the negative case, e.g. |
| 819 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 820 | */ |
| 821 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 822 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 823 | reg->umin_value); |
| 824 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 825 | reg->umax_value); |
| 826 | return; |
| 827 | } |
| 828 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 829 | * boundary, so we must be careful. |
| 830 | */ |
| 831 | if ((s64)reg->umax_value >= 0) { |
| 832 | /* Positive. We can't learn anything from the smin, but smax |
| 833 | * is positive, hence safe. |
| 834 | */ |
| 835 | reg->smin_value = reg->umin_value; |
| 836 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 837 | reg->umax_value); |
| 838 | } else if ((s64)reg->umin_value < 0) { |
| 839 | /* Negative. We can't learn anything from the smax, but smin |
| 840 | * is negative, hence safe. |
| 841 | */ |
| 842 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 843 | reg->umin_value); |
| 844 | reg->smax_value = reg->umax_value; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 849 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 850 | { |
| 851 | reg->var_off = tnum_intersect(reg->var_off, |
| 852 | tnum_range(reg->umin_value, |
| 853 | reg->umax_value)); |
| 854 | } |
| 855 | |
| 856 | /* Reset the min/max bounds of a register */ |
| 857 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 858 | { |
| 859 | reg->smin_value = S64_MIN; |
| 860 | reg->smax_value = S64_MAX; |
| 861 | reg->umin_value = 0; |
| 862 | reg->umax_value = U64_MAX; |
| 863 | } |
| 864 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 865 | /* Mark a register as having a completely unknown (scalar) value. */ |
| 866 | static void __mark_reg_unknown(struct bpf_reg_state *reg) |
| 867 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 868 | /* |
| 869 | * Clear type, id, off, and union(map_ptr, range) and |
| 870 | * padding between 'type' and union |
| 871 | */ |
| 872 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 873 | reg->type = SCALAR_VALUE; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 874 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 875 | reg->frameno = 0; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 876 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 877 | } |
| 878 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 879 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 880 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 881 | { |
| 882 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 883 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 884 | /* Something bad happened, let's kill all regs except FP */ |
| 885 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 886 | __mark_reg_not_init(regs + regno); |
| 887 | return; |
| 888 | } |
| 889 | __mark_reg_unknown(regs + regno); |
| 890 | } |
| 891 | |
| 892 | static void __mark_reg_not_init(struct bpf_reg_state *reg) |
| 893 | { |
| 894 | __mark_reg_unknown(reg); |
| 895 | reg->type = NOT_INIT; |
| 896 | } |
| 897 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 898 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 899 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 900 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 901 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 902 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 903 | /* Something bad happened, let's kill all regs except FP */ |
| 904 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 905 | __mark_reg_not_init(regs + regno); |
| 906 | return; |
| 907 | } |
| 908 | __mark_reg_not_init(regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 909 | } |
| 910 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 911 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 912 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 913 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 914 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 915 | int i; |
| 916 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 917 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 918 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 919 | regs[i].live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 920 | regs[i].parent = NULL; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 921 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 922 | |
| 923 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 924 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 925 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 926 | regs[BPF_REG_FP].frameno = state->frameno; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 927 | |
| 928 | /* 1st arg to a function */ |
| 929 | regs[BPF_REG_1].type = PTR_TO_CTX; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 930 | mark_reg_known_zero(env, regs, BPF_REG_1); |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 931 | } |
| 932 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 933 | #define BPF_MAIN_FUNC (-1) |
| 934 | static void init_func_state(struct bpf_verifier_env *env, |
| 935 | struct bpf_func_state *state, |
| 936 | int callsite, int frameno, int subprogno) |
| 937 | { |
| 938 | state->callsite = callsite; |
| 939 | state->frameno = frameno; |
| 940 | state->subprogno = subprogno; |
| 941 | init_reg_state(env, state); |
| 942 | } |
| 943 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 944 | enum reg_arg_type { |
| 945 | SRC_OP, /* register is used as source operand */ |
| 946 | DST_OP, /* register is used as destination operand */ |
| 947 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 948 | }; |
| 949 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 950 | static int cmp_subprogs(const void *a, const void *b) |
| 951 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 952 | return ((struct bpf_subprog_info *)a)->start - |
| 953 | ((struct bpf_subprog_info *)b)->start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 957 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 958 | struct bpf_subprog_info *p; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 959 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 960 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 961 | sizeof(env->subprog_info[0]), cmp_subprogs); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 962 | if (!p) |
| 963 | return -ENOENT; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 964 | return p - env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 965 | |
| 966 | } |
| 967 | |
| 968 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 969 | { |
| 970 | int insn_cnt = env->prog->len; |
| 971 | int ret; |
| 972 | |
| 973 | if (off >= insn_cnt || off < 0) { |
| 974 | verbose(env, "call to invalid destination\n"); |
| 975 | return -EINVAL; |
| 976 | } |
| 977 | ret = find_subprog(env, off); |
| 978 | if (ret >= 0) |
| 979 | return 0; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 980 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 981 | verbose(env, "too many subprograms\n"); |
| 982 | return -E2BIG; |
| 983 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 984 | env->subprog_info[env->subprog_cnt++].start = off; |
| 985 | sort(env->subprog_info, env->subprog_cnt, |
| 986 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | static int check_subprogs(struct bpf_verifier_env *env) |
| 991 | { |
| 992 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 993 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 994 | struct bpf_insn *insn = env->prog->insnsi; |
| 995 | int insn_cnt = env->prog->len; |
| 996 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 997 | /* Add entry function. */ |
| 998 | ret = add_subprog(env, 0); |
| 999 | if (ret < 0) |
| 1000 | return ret; |
| 1001 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1002 | /* determine subprog starts. The end is one before the next starts */ |
| 1003 | for (i = 0; i < insn_cnt; i++) { |
| 1004 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1005 | continue; |
| 1006 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1007 | continue; |
| 1008 | if (!env->allow_ptr_leaks) { |
| 1009 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); |
| 1010 | return -EPERM; |
| 1011 | } |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1012 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 1013 | if (ret < 0) |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1017 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 1018 | * logic. 'subprog_cnt' should not be increased. |
| 1019 | */ |
| 1020 | subprog[env->subprog_cnt].start = insn_cnt; |
| 1021 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1022 | if (env->log.level > 1) |
| 1023 | for (i = 0; i < env->subprog_cnt; i++) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1024 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1025 | |
| 1026 | /* now check that all jumps are within the same subprog */ |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1027 | subprog_start = subprog[cur_subprog].start; |
| 1028 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1029 | for (i = 0; i < insn_cnt; i++) { |
| 1030 | u8 code = insn[i].code; |
| 1031 | |
| 1032 | if (BPF_CLASS(code) != BPF_JMP) |
| 1033 | goto next; |
| 1034 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 1035 | goto next; |
| 1036 | off = i + insn[i].off + 1; |
| 1037 | if (off < subprog_start || off >= subprog_end) { |
| 1038 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 1039 | return -EINVAL; |
| 1040 | } |
| 1041 | next: |
| 1042 | if (i == subprog_end - 1) { |
| 1043 | /* to avoid fall-through from one subprog into another |
| 1044 | * the last insn of the subprog should be either exit |
| 1045 | * or unconditional jump back |
| 1046 | */ |
| 1047 | if (code != (BPF_JMP | BPF_EXIT) && |
| 1048 | code != (BPF_JMP | BPF_JA)) { |
| 1049 | verbose(env, "last insn is not an exit or jmp\n"); |
| 1050 | return -EINVAL; |
| 1051 | } |
| 1052 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1053 | cur_subprog++; |
| 1054 | if (cur_subprog < env->subprog_cnt) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1055 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1056 | } |
| 1057 | } |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1061 | /* Parentage chain of this register (or stack slot) should take care of all |
| 1062 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 1063 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1064 | static int mark_reg_read(struct bpf_verifier_env *env, |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1065 | const struct bpf_reg_state *state, |
| 1066 | struct bpf_reg_state *parent) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1067 | { |
| 1068 | bool writes = parent == state->parent; /* Observe write marks */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1069 | |
| 1070 | while (parent) { |
| 1071 | /* if read wasn't screened by an earlier write ... */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1072 | if (writes && state->live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1073 | break; |
| 1074 | /* ... then we depend on parent's value */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1075 | parent->live |= REG_LIVE_READ; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1076 | state = parent; |
| 1077 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1078 | writes = true; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1079 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1080 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1084 | enum reg_arg_type t) |
| 1085 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1086 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1087 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 1088 | struct bpf_reg_state *regs = state->regs; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1089 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1090 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1091 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1092 | return -EINVAL; |
| 1093 | } |
| 1094 | |
| 1095 | if (t == SRC_OP) { |
| 1096 | /* check whether register used as source operand can be read */ |
| 1097 | if (regs[regno].type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1098 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1099 | return -EACCES; |
| 1100 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1101 | /* We don't need to worry about FP liveness because it's read-only */ |
| 1102 | if (regno != BPF_REG_FP) |
| 1103 | return mark_reg_read(env, ®s[regno], |
| 1104 | regs[regno].parent); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1105 | } else { |
| 1106 | /* check whether register used as dest operand can be written to */ |
| 1107 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1108 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1109 | return -EACCES; |
| 1110 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1111 | regs[regno].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1112 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1113 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1114 | } |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1118 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 1119 | { |
| 1120 | switch (type) { |
| 1121 | case PTR_TO_MAP_VALUE: |
| 1122 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 1123 | case PTR_TO_STACK: |
| 1124 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1125 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1126 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1127 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1128 | case PTR_TO_FLOW_KEYS: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1129 | case CONST_PTR_TO_MAP: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1130 | case PTR_TO_SOCKET: |
| 1131 | case PTR_TO_SOCKET_OR_NULL: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1132 | return true; |
| 1133 | default: |
| 1134 | return false; |
| 1135 | } |
| 1136 | } |
| 1137 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1138 | /* Does this register contain a constant zero? */ |
| 1139 | static bool register_is_null(struct bpf_reg_state *reg) |
| 1140 | { |
| 1141 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 1142 | } |
| 1143 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1144 | /* check_stack_read/write functions track spill/fill of registers, |
| 1145 | * stack boundary and alignment are checked in check_mem_access() |
| 1146 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1147 | static int check_stack_write(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1148 | struct bpf_func_state *state, /* func where register points to */ |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1149 | int off, int size, int value_regno, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1150 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1151 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1152 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1153 | enum bpf_reg_type type; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1154 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1155 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 1156 | state->acquired_refs, true); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1157 | if (err) |
| 1158 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1159 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 1160 | * so it's aligned access and [off, off + size) are within stack limits |
| 1161 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1162 | if (!env->allow_ptr_leaks && |
| 1163 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 1164 | size != BPF_REG_SIZE) { |
| 1165 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 1166 | return -EACCES; |
| 1167 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1168 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1169 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1170 | if (value_regno >= 0 && |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1171 | is_spillable_regtype((type = cur->regs[value_regno].type))) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1172 | |
| 1173 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1174 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1175 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1176 | return -EACCES; |
| 1177 | } |
| 1178 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1179 | if (state != cur && type == PTR_TO_STACK) { |
| 1180 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 1181 | return -EINVAL; |
| 1182 | } |
| 1183 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1184 | /* save register state */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1185 | state->stack[spi].spilled_ptr = cur->regs[value_regno]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1186 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1187 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1188 | for (i = 0; i < BPF_REG_SIZE; i++) { |
| 1189 | if (state->stack[spi].slot_type[i] == STACK_MISC && |
| 1190 | !env->allow_ptr_leaks) { |
| 1191 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
| 1192 | int soff = (-spi - 1) * BPF_REG_SIZE; |
| 1193 | |
| 1194 | /* detected reuse of integer stack slot with a pointer |
| 1195 | * which means either llvm is reusing stack slot or |
| 1196 | * an attacker is trying to exploit CVE-2018-3639 |
| 1197 | * (speculative store bypass) |
| 1198 | * Have to sanitize that slot with preemptive |
| 1199 | * store of zero. |
| 1200 | */ |
| 1201 | if (*poff && *poff != soff) { |
| 1202 | /* disallow programs where single insn stores |
| 1203 | * into two different stack slots, since verifier |
| 1204 | * cannot sanitize them |
| 1205 | */ |
| 1206 | verbose(env, |
| 1207 | "insn %d cannot access two stack slots fp%d and fp%d", |
| 1208 | insn_idx, *poff, soff); |
| 1209 | return -EINVAL; |
| 1210 | } |
| 1211 | *poff = soff; |
| 1212 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1213 | state->stack[spi].slot_type[i] = STACK_SPILL; |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1214 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1215 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1216 | u8 type = STACK_MISC; |
| 1217 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1218 | /* regular write of data into stack destroys any spilled ptr */ |
| 1219 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1220 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1221 | /* only mark the slot as written if all 8 bytes were written |
| 1222 | * otherwise read propagation may incorrectly stop too soon |
| 1223 | * when stack slots are partially written. |
| 1224 | * This heuristic means that read propagation will be |
| 1225 | * conservative, since it will add reg_live_read marks |
| 1226 | * to stack slots all the way to first state when programs |
| 1227 | * writes+reads less than 8 bytes |
| 1228 | */ |
| 1229 | if (size == BPF_REG_SIZE) |
| 1230 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 1231 | |
| 1232 | /* when we zero initialize stack slots mark them as such */ |
| 1233 | if (value_regno >= 0 && |
| 1234 | register_is_null(&cur->regs[value_regno])) |
| 1235 | type = STACK_ZERO; |
| 1236 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1237 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1238 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1239 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1240 | } |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1244 | static int check_stack_read(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1245 | struct bpf_func_state *reg_state /* func where register points to */, |
| 1246 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1247 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1248 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1249 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1250 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
| 1251 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1252 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1253 | if (reg_state->allocated_stack <= slot) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1254 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 1255 | off, size); |
| 1256 | return -EACCES; |
| 1257 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1258 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1259 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1260 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1261 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1262 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1263 | return -EACCES; |
| 1264 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1265 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1266 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1267 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1268 | return -EACCES; |
| 1269 | } |
| 1270 | } |
| 1271 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1272 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1273 | /* restore register state from stack */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1274 | state->regs[value_regno] = reg_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 1275 | /* mark reg as written since spilled pointer state likely |
| 1276 | * has its liveness marks cleared by is_state_visited() |
| 1277 | * which resets stack/reg liveness for state transitions |
| 1278 | */ |
| 1279 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1280 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1281 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
| 1282 | reg_state->stack[spi].spilled_ptr.parent); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1283 | return 0; |
| 1284 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1285 | int zeros = 0; |
| 1286 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1287 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1288 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 1289 | continue; |
| 1290 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 1291 | zeros++; |
| 1292 | continue; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1293 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1294 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 1295 | off, i, size); |
| 1296 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1297 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1298 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
| 1299 | reg_state->stack[spi].spilled_ptr.parent); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1300 | if (value_regno >= 0) { |
| 1301 | if (zeros == size) { |
| 1302 | /* any size read into register is zero extended, |
| 1303 | * so the whole register == const_zero |
| 1304 | */ |
| 1305 | __mark_reg_const_zero(&state->regs[value_regno]); |
| 1306 | } else { |
| 1307 | /* have read misc data from the stack */ |
| 1308 | mark_reg_unknown(env, state->regs, value_regno); |
| 1309 | } |
| 1310 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 1311 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1312 | return 0; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1317 | 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] | 1318 | int size, bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1319 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1320 | struct bpf_reg_state *regs = cur_regs(env); |
| 1321 | struct bpf_map *map = regs[regno].map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1322 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1323 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1324 | off + size > map->value_size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1325 | 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] | 1326 | map->value_size, off, size); |
| 1327 | return -EACCES; |
| 1328 | } |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1332 | /* check read/write into a map element with possible variable offset */ |
| 1333 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1334 | int off, int size, bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1335 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1336 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1337 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1338 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 1339 | int err; |
| 1340 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1341 | /* We may have adjusted the register to this map value, so we |
| 1342 | * need to try adding each of min_value and max_value to off |
| 1343 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1344 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1345 | if (env->log.level) |
| 1346 | print_verifier_state(env, state); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1347 | /* The minimum value is only important with signed |
| 1348 | * comparisons where we can't assume the floor of a |
| 1349 | * value is 0. If we are using signed variables for our |
| 1350 | * index'es we need to make sure that whatever we use |
| 1351 | * will have a set floor within our range. |
| 1352 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1353 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1354 | 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] | 1355 | regno); |
| 1356 | return -EACCES; |
| 1357 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1358 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
| 1359 | zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1360 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1361 | verbose(env, "R%d min value is outside of the array range\n", |
| 1362 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1363 | return err; |
| 1364 | } |
| 1365 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1366 | /* If we haven't set a max value then we need to bail since we can't be |
| 1367 | * sure we won't do bad things. |
| 1368 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1369 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1370 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1371 | 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] | 1372 | regno); |
| 1373 | return -EACCES; |
| 1374 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1375 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
| 1376 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1377 | if (err) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1378 | verbose(env, "R%d max value is outside of the array range\n", |
| 1379 | regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1380 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1381 | } |
| 1382 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1383 | #define MAX_PACKET_OFF 0xffff |
| 1384 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1385 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1386 | const struct bpf_call_arg_meta *meta, |
| 1387 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1388 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1389 | switch (env->prog->type) { |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1390 | /* Program types only with direct read access go here! */ |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1391 | case BPF_PROG_TYPE_LWT_IN: |
| 1392 | case BPF_PROG_TYPE_LWT_OUT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 1393 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 1394 | case BPF_PROG_TYPE_SK_REUSEPORT: |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1395 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Daniel Borkmann | d5563d3 | 2018-10-24 22:05:46 +0200 | [diff] [blame] | 1396 | case BPF_PROG_TYPE_CGROUP_SKB: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1397 | if (t == BPF_WRITE) |
| 1398 | return false; |
Alexander Alemayhu | 7e57fbb | 2017-02-14 00:02:35 +0100 | [diff] [blame] | 1399 | /* fallthrough */ |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1400 | |
| 1401 | /* Program types with direct read + write access go here! */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1402 | case BPF_PROG_TYPE_SCHED_CLS: |
| 1403 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1404 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1405 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 1406 | case BPF_PROG_TYPE_SK_SKB: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1407 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1408 | if (meta) |
| 1409 | return meta->pkt_access; |
| 1410 | |
| 1411 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1412 | return true; |
| 1413 | default: |
| 1414 | return false; |
| 1415 | } |
| 1416 | } |
| 1417 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1418 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1419 | int off, int size, bool zero_size_allowed) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1420 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1421 | struct bpf_reg_state *regs = cur_regs(env); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1422 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1423 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1424 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1425 | (u64)off + size > reg->range) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1426 | 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] | 1427 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1428 | return -EACCES; |
| 1429 | } |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1433 | 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] | 1434 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1435 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1436 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1437 | struct bpf_reg_state *reg = ®s[regno]; |
| 1438 | int err; |
| 1439 | |
| 1440 | /* We may have added a variable offset to the packet pointer; but any |
| 1441 | * reg->range we have comes after that. We are only checking the fixed |
| 1442 | * offset. |
| 1443 | */ |
| 1444 | |
| 1445 | /* We don't allow negative numbers, because we aren't tracking enough |
| 1446 | * detail to prove they're safe. |
| 1447 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1448 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1449 | 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] | 1450 | regno); |
| 1451 | return -EACCES; |
| 1452 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1453 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1454 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1455 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1456 | return err; |
| 1457 | } |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 1458 | |
| 1459 | /* __check_packet_access has made sure "off + size - 1" is within u16. |
| 1460 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 1461 | * otherwise find_good_pkt_pointers would have refused to set range info |
| 1462 | * that __check_packet_access would have rejected this pkt access. |
| 1463 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 1464 | */ |
| 1465 | env->prog->aux->max_pkt_offset = |
| 1466 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 1467 | off + reg->umax_value + size - 1); |
| 1468 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1469 | return err; |
| 1470 | } |
| 1471 | |
| 1472 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1473 | 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] | 1474 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1475 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1476 | struct bpf_insn_access_aux info = { |
| 1477 | .reg_type = *reg_type, |
| 1478 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1479 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1480 | if (env->ops->is_valid_access && |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1481 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1482 | /* A non zero info.ctx_field_size indicates that this field is a |
| 1483 | * candidate for later verifier transformation to load the whole |
| 1484 | * field and then apply a mask when accessed with a narrower |
| 1485 | * access than actual ctx access size. A zero info.ctx_field_size |
| 1486 | * will only allow for whole field access and rejects any other |
| 1487 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1488 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 1489 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1490 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1491 | 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] | 1492 | /* remember the offset of last byte accessed in ctx */ |
| 1493 | if (env->prog->aux->max_ctx_offset < off + size) |
| 1494 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1495 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 1496 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1497 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1498 | 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] | 1499 | return -EACCES; |
| 1500 | } |
| 1501 | |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1502 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 1503 | int size) |
| 1504 | { |
| 1505 | if (size < 0 || off < 0 || |
| 1506 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 1507 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 1508 | off, size); |
| 1509 | return -EACCES; |
| 1510 | } |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1514 | static int check_sock_access(struct bpf_verifier_env *env, u32 regno, int off, |
| 1515 | int size, enum bpf_access_type t) |
| 1516 | { |
| 1517 | struct bpf_reg_state *regs = cur_regs(env); |
| 1518 | struct bpf_reg_state *reg = ®s[regno]; |
| 1519 | struct bpf_insn_access_aux info; |
| 1520 | |
| 1521 | if (reg->smin_value < 0) { |
| 1522 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 1523 | regno); |
| 1524 | return -EACCES; |
| 1525 | } |
| 1526 | |
| 1527 | if (!bpf_sock_is_valid_access(off, size, t, &info)) { |
| 1528 | verbose(env, "invalid bpf_sock access off=%d size=%d\n", |
| 1529 | off, size); |
| 1530 | return -EACCES; |
| 1531 | } |
| 1532 | |
| 1533 | return 0; |
| 1534 | } |
| 1535 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1536 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 1537 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1538 | { |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1539 | if (allow_ptr_leaks) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1540 | return false; |
| 1541 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1542 | return reg->type != SCALAR_VALUE; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1545 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
| 1546 | { |
| 1547 | return cur_regs(env) + regno; |
| 1548 | } |
| 1549 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1550 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 1551 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1552 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1553 | } |
| 1554 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1555 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 1556 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1557 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1558 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 1559 | return reg->type == PTR_TO_CTX || |
| 1560 | reg->type == PTR_TO_SOCKET; |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1561 | } |
| 1562 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1563 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 1564 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1565 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1566 | |
| 1567 | return type_is_pkt_pointer(reg->type); |
| 1568 | } |
| 1569 | |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 1570 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 1571 | { |
| 1572 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 1573 | |
| 1574 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 1575 | return reg->type == PTR_TO_FLOW_KEYS; |
| 1576 | } |
| 1577 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1578 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 1579 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1580 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1581 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1582 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1583 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1584 | |
| 1585 | /* Byte size accesses are always allowed. */ |
| 1586 | if (!strict || size == 1) |
| 1587 | return 0; |
| 1588 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1589 | /* For platforms that do not have a Kconfig enabling |
| 1590 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 1591 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 1592 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 1593 | * to this code only in strict mode where we want to emulate |
| 1594 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 1595 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1596 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1597 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1598 | |
| 1599 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 1600 | if (!tnum_is_aligned(reg_off, size)) { |
| 1601 | char tn_buf[48]; |
| 1602 | |
| 1603 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1604 | verbose(env, |
| 1605 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1606 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1607 | return -EACCES; |
| 1608 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1609 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1610 | return 0; |
| 1611 | } |
| 1612 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1613 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 1614 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1615 | const char *pointer_desc, |
| 1616 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1617 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1618 | struct tnum reg_off; |
| 1619 | |
| 1620 | /* Byte size accesses are always allowed. */ |
| 1621 | if (!strict || size == 1) |
| 1622 | return 0; |
| 1623 | |
| 1624 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 1625 | if (!tnum_is_aligned(reg_off, size)) { |
| 1626 | char tn_buf[48]; |
| 1627 | |
| 1628 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1629 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1630 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1631 | return -EACCES; |
| 1632 | } |
| 1633 | |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1637 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1638 | const struct bpf_reg_state *reg, int off, |
| 1639 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1640 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1641 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1642 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1643 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1644 | switch (reg->type) { |
| 1645 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1646 | case PTR_TO_PACKET_META: |
| 1647 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 1648 | * right in front, treat it the very same way. |
| 1649 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1650 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1651 | case PTR_TO_FLOW_KEYS: |
| 1652 | pointer_desc = "flow keys "; |
| 1653 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1654 | case PTR_TO_MAP_VALUE: |
| 1655 | pointer_desc = "value "; |
| 1656 | break; |
| 1657 | case PTR_TO_CTX: |
| 1658 | pointer_desc = "context "; |
| 1659 | break; |
| 1660 | case PTR_TO_STACK: |
| 1661 | pointer_desc = "stack "; |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 1662 | /* The stack spill tracking logic in check_stack_write() |
| 1663 | * and check_stack_read() relies on stack accesses being |
| 1664 | * aligned. |
| 1665 | */ |
| 1666 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1667 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1668 | case PTR_TO_SOCKET: |
| 1669 | pointer_desc = "sock "; |
| 1670 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1671 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1672 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1673 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1674 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 1675 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1676 | } |
| 1677 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1678 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 1679 | const struct bpf_func_state *func, |
| 1680 | int off) |
| 1681 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1682 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1683 | |
| 1684 | if (stack >= -off) |
| 1685 | return 0; |
| 1686 | |
| 1687 | /* update known max for given subprogram */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1688 | env->subprog_info[func->subprogno].stack_depth = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1689 | return 0; |
| 1690 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1691 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1692 | /* starting from main bpf function walk all instructions of the function |
| 1693 | * and recursively walk all callees that given function can call. |
| 1694 | * Ignore jump and exit insns. |
| 1695 | * Since recursion is prevented by check_cfg() this algorithm |
| 1696 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 1697 | */ |
| 1698 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 1699 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1700 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 1701 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1702 | struct bpf_insn *insn = env->prog->insnsi; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1703 | int ret_insn[MAX_CALL_FRAMES]; |
| 1704 | int ret_prog[MAX_CALL_FRAMES]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1705 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1706 | process_func: |
| 1707 | /* round up to 32-bytes, since this is granularity |
| 1708 | * of interpreter stack size |
| 1709 | */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1710 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1711 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1712 | 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] | 1713 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1714 | return -EACCES; |
| 1715 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1716 | continue_func: |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1717 | subprog_end = subprog[idx + 1].start; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1718 | for (; i < subprog_end; i++) { |
| 1719 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1720 | continue; |
| 1721 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1722 | continue; |
| 1723 | /* remember insn and function to return to */ |
| 1724 | ret_insn[frame] = i + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1725 | ret_prog[frame] = idx; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1726 | |
| 1727 | /* find the callee */ |
| 1728 | i = i + insn[i].imm + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1729 | idx = find_subprog(env, i); |
| 1730 | if (idx < 0) { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1731 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1732 | i); |
| 1733 | return -EFAULT; |
| 1734 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1735 | frame++; |
| 1736 | if (frame >= MAX_CALL_FRAMES) { |
| 1737 | WARN_ONCE(1, "verifier bug. Call stack is too deep\n"); |
| 1738 | return -EFAULT; |
| 1739 | } |
| 1740 | goto process_func; |
| 1741 | } |
| 1742 | /* end of for() loop means the last insn of the 'subprog' |
| 1743 | * was reached. Doesn't matter whether it was JA or EXIT |
| 1744 | */ |
| 1745 | if (frame == 0) |
| 1746 | return 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1747 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1748 | frame--; |
| 1749 | i = ret_insn[frame]; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1750 | idx = ret_prog[frame]; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1751 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1752 | } |
| 1753 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1754 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1755 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 1756 | const struct bpf_insn *insn, int idx) |
| 1757 | { |
| 1758 | int start = idx + insn->imm + 1, subprog; |
| 1759 | |
| 1760 | subprog = find_subprog(env, start); |
| 1761 | if (subprog < 0) { |
| 1762 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1763 | start); |
| 1764 | return -EFAULT; |
| 1765 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1766 | return env->subprog_info[subprog].stack_depth; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1767 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1768 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1769 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 1770 | static int check_ctx_reg(struct bpf_verifier_env *env, |
| 1771 | const struct bpf_reg_state *reg, int regno) |
| 1772 | { |
| 1773 | /* Access to ctx or passing it to a helper is only allowed in |
| 1774 | * its original, unmodified form. |
| 1775 | */ |
| 1776 | |
| 1777 | if (reg->off) { |
| 1778 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 1779 | regno, reg->off); |
| 1780 | return -EACCES; |
| 1781 | } |
| 1782 | |
| 1783 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 1784 | char tn_buf[48]; |
| 1785 | |
| 1786 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 1787 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 1788 | return -EACCES; |
| 1789 | } |
| 1790 | |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 1794 | /* truncate register to smaller size (in bytes) |
| 1795 | * must be called with size < BPF_REG_SIZE |
| 1796 | */ |
| 1797 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 1798 | { |
| 1799 | u64 mask; |
| 1800 | |
| 1801 | /* clear high bits in bit representation */ |
| 1802 | reg->var_off = tnum_cast(reg->var_off, size); |
| 1803 | |
| 1804 | /* fix arithmetic bounds */ |
| 1805 | mask = ((u64)1 << (size * 8)) - 1; |
| 1806 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 1807 | reg->umin_value &= mask; |
| 1808 | reg->umax_value &= mask; |
| 1809 | } else { |
| 1810 | reg->umin_value = 0; |
| 1811 | reg->umax_value = mask; |
| 1812 | } |
| 1813 | reg->smin_value = reg->umin_value; |
| 1814 | reg->smax_value = reg->umax_value; |
| 1815 | } |
| 1816 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1817 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 1818 | * if t==write, value_regno is a register which value is stored into memory |
| 1819 | * if t==read, value_regno is a register which will receive the value from memory |
| 1820 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 1821 | * if t==read && value_regno==-1, don't care what we read from memory |
| 1822 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1823 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 1824 | int off, int bpf_size, enum bpf_access_type t, |
| 1825 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1826 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1827 | struct bpf_reg_state *regs = cur_regs(env); |
| 1828 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1829 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1830 | int size, err = 0; |
| 1831 | |
| 1832 | size = bpf_size_to_bytes(bpf_size); |
| 1833 | if (size < 0) |
| 1834 | return size; |
| 1835 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1836 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1837 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1838 | if (err) |
| 1839 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1840 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1841 | /* for access checks, reg->off is just part of off */ |
| 1842 | off += reg->off; |
| 1843 | |
| 1844 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1845 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1846 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1847 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1848 | return -EACCES; |
| 1849 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1850 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1851 | err = check_map_access(env, regno, off, size, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1852 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1853 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1854 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1855 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1856 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 1857 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1858 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1859 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1860 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1861 | return -EACCES; |
| 1862 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1863 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 1864 | err = check_ctx_reg(env, reg, regno); |
| 1865 | if (err < 0) |
| 1866 | return err; |
| 1867 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1868 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1869 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1870 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1871 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 1872 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1873 | */ |
| 1874 | if (reg_type == SCALAR_VALUE) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1875 | mark_reg_unknown(env, regs, value_regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1876 | else |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1877 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1878 | value_regno); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1879 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1880 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1881 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1882 | } else if (reg->type == PTR_TO_STACK) { |
| 1883 | /* stack accesses must be at a fixed offset, so that we can |
| 1884 | * determine what type of data were returned. |
| 1885 | * See check_stack_read(). |
| 1886 | */ |
| 1887 | if (!tnum_is_const(reg->var_off)) { |
| 1888 | char tn_buf[48]; |
| 1889 | |
| 1890 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1891 | verbose(env, "variable stack access var_off=%s off=%d size=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1892 | tn_buf, off, size); |
| 1893 | return -EACCES; |
| 1894 | } |
| 1895 | off += reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1896 | if (off >= 0 || off < -MAX_BPF_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1897 | verbose(env, "invalid stack off=%d size=%d\n", off, |
| 1898 | size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1899 | return -EACCES; |
| 1900 | } |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1901 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1902 | state = func(env, reg); |
| 1903 | err = update_stack_depth(env, state, off); |
| 1904 | if (err) |
| 1905 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1906 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1907 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1908 | err = check_stack_write(env, state, off, size, |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1909 | value_regno, insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1910 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1911 | err = check_stack_read(env, state, off, size, |
| 1912 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1913 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1914 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1915 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1916 | return -EACCES; |
| 1917 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1918 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1919 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1920 | verbose(env, "R%d leaks addr into packet\n", |
| 1921 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1922 | return -EACCES; |
| 1923 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1924 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1925 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1926 | mark_reg_unknown(env, regs, value_regno); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1927 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 1928 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1929 | is_pointer_value(env, value_regno)) { |
| 1930 | verbose(env, "R%d leaks addr into flow keys\n", |
| 1931 | value_regno); |
| 1932 | return -EACCES; |
| 1933 | } |
| 1934 | |
| 1935 | err = check_flow_keys_access(env, off, size); |
| 1936 | if (!err && t == BPF_READ && value_regno >= 0) |
| 1937 | mark_reg_unknown(env, regs, value_regno); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1938 | } else if (reg->type == PTR_TO_SOCKET) { |
| 1939 | if (t == BPF_WRITE) { |
| 1940 | verbose(env, "cannot write into socket\n"); |
| 1941 | return -EACCES; |
| 1942 | } |
| 1943 | err = check_sock_access(env, regno, off, size, t); |
| 1944 | if (!err && value_regno >= 0) |
| 1945 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1946 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1947 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 1948 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1949 | return -EACCES; |
| 1950 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1951 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1952 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1953 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1954 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 1955 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1956 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1957 | return err; |
| 1958 | } |
| 1959 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1960 | 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] | 1961 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1962 | int err; |
| 1963 | |
| 1964 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 1965 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1966 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1967 | return -EINVAL; |
| 1968 | } |
| 1969 | |
| 1970 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1971 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1972 | if (err) |
| 1973 | return err; |
| 1974 | |
| 1975 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1976 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1977 | if (err) |
| 1978 | return err; |
| 1979 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1980 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1981 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1982 | return -EACCES; |
| 1983 | } |
| 1984 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1985 | if (is_ctx_reg(env, insn->dst_reg) || |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 1986 | is_pkt_reg(env, insn->dst_reg) || |
| 1987 | is_flow_key_reg(env, insn->dst_reg)) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1988 | 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] | 1989 | insn->dst_reg, |
| 1990 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1991 | return -EACCES; |
| 1992 | } |
| 1993 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1994 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1995 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1996 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1997 | if (err) |
| 1998 | return err; |
| 1999 | |
| 2000 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2001 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2002 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | /* when register 'regno' is passed into function that will read 'access_size' |
| 2006 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2007 | * and all elements of stack are initialized. |
| 2008 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 2009 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2010 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2011 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2012 | int access_size, bool zero_size_allowed, |
| 2013 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2014 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2015 | struct bpf_reg_state *reg = reg_state(env, regno); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2016 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2017 | int off, i, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2018 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2019 | if (reg->type != PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2020 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2021 | if (zero_size_allowed && access_size == 0 && |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2022 | register_is_null(reg)) |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2023 | return 0; |
| 2024 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2025 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2026 | reg_type_str[reg->type], |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2027 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2028 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2029 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2030 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2031 | /* Only allow fixed-offset stack reads */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2032 | if (!tnum_is_const(reg->var_off)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2033 | char tn_buf[48]; |
| 2034 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2035 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2036 | verbose(env, "invalid variable stack read R%d var_off=%s\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2037 | regno, tn_buf); |
Jann Horn | ea25f91 | 2017-12-18 20:11:57 -0800 | [diff] [blame] | 2038 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2039 | } |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2040 | off = reg->off + reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2041 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2042 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2043 | 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] | 2044 | regno, off, access_size); |
| 2045 | return -EACCES; |
| 2046 | } |
| 2047 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2048 | if (meta && meta->raw_mode) { |
| 2049 | meta->access_size = access_size; |
| 2050 | meta->regno = regno; |
| 2051 | return 0; |
| 2052 | } |
| 2053 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2054 | for (i = 0; i < access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2055 | u8 *stype; |
| 2056 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2057 | slot = -(off + i) - 1; |
| 2058 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2059 | if (state->allocated_stack <= slot) |
| 2060 | goto err; |
| 2061 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 2062 | if (*stype == STACK_MISC) |
| 2063 | goto mark; |
| 2064 | if (*stype == STACK_ZERO) { |
| 2065 | /* helper can write anything into the stack */ |
| 2066 | *stype = STACK_MISC; |
| 2067 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2068 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2069 | err: |
| 2070 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 2071 | off, i, access_size); |
| 2072 | return -EACCES; |
| 2073 | mark: |
| 2074 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 2075 | * the whole slot to be marked as 'read' |
| 2076 | */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2077 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
| 2078 | state->stack[spi].spilled_ptr.parent); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2079 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2080 | return update_stack_depth(env, state, off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2081 | } |
| 2082 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2083 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 2084 | int access_size, bool zero_size_allowed, |
| 2085 | struct bpf_call_arg_meta *meta) |
| 2086 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2087 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2088 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2089 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2090 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2091 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2092 | return check_packet_access(env, regno, reg->off, access_size, |
| 2093 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2094 | case PTR_TO_MAP_VALUE: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2095 | return check_map_access(env, regno, reg->off, access_size, |
| 2096 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2097 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2098 | return check_stack_boundary(env, regno, access_size, |
| 2099 | zero_size_allowed, meta); |
| 2100 | } |
| 2101 | } |
| 2102 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2103 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 2104 | { |
| 2105 | return type == ARG_PTR_TO_MEM || |
| 2106 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 2107 | type == ARG_PTR_TO_UNINIT_MEM; |
| 2108 | } |
| 2109 | |
| 2110 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 2111 | { |
| 2112 | return type == ARG_CONST_SIZE || |
| 2113 | type == ARG_CONST_SIZE_OR_ZERO; |
| 2114 | } |
| 2115 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2116 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2117 | enum bpf_arg_type arg_type, |
| 2118 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2119 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2120 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2121 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2122 | int err = 0; |
| 2123 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2124 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2125 | return 0; |
| 2126 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2127 | err = check_reg_arg(env, regno, SRC_OP); |
| 2128 | if (err) |
| 2129 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2130 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2131 | if (arg_type == ARG_ANYTHING) { |
| 2132 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2133 | verbose(env, "R%d leaks addr into helper function\n", |
| 2134 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2135 | return -EACCES; |
| 2136 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2137 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2138 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2139 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2140 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2141 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2142 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2143 | return -EACCES; |
| 2144 | } |
| 2145 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2146 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2147 | arg_type == ARG_PTR_TO_MAP_VALUE || |
| 2148 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2149 | expected_type = PTR_TO_STACK; |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2150 | if (!type_is_pkt_pointer(type) && type != PTR_TO_MAP_VALUE && |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2151 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2152 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2153 | } else if (arg_type == ARG_CONST_SIZE || |
| 2154 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2155 | expected_type = SCALAR_VALUE; |
| 2156 | if (type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2157 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2158 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 2159 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2160 | if (type != expected_type) |
| 2161 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2162 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 2163 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2164 | if (type != expected_type) |
| 2165 | goto err_type; |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 2166 | err = check_ctx_reg(env, reg, regno); |
| 2167 | if (err < 0) |
| 2168 | return err; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2169 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
| 2170 | expected_type = PTR_TO_SOCKET; |
| 2171 | if (type != expected_type) |
| 2172 | goto err_type; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2173 | if (meta->ptr_id || !reg->id) { |
| 2174 | verbose(env, "verifier internal error: mismatched references meta=%d, reg=%d\n", |
| 2175 | meta->ptr_id, reg->id); |
| 2176 | return -EFAULT; |
| 2177 | } |
| 2178 | meta->ptr_id = reg->id; |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2179 | } else if (arg_type_is_mem_ptr(arg_type)) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2180 | expected_type = PTR_TO_STACK; |
| 2181 | /* One exception here. In case function allows for NULL to be |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2182 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2183 | * happens during stack boundary checking. |
| 2184 | */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2185 | if (register_is_null(reg) && |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 2186 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2187 | /* final test in check_stack_boundary() */; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2188 | else if (!type_is_pkt_pointer(type) && |
| 2189 | type != PTR_TO_MAP_VALUE && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2190 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2191 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2192 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2193 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2194 | verbose(env, "unsupported arg_type %d\n", arg_type); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2195 | return -EFAULT; |
| 2196 | } |
| 2197 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2198 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 2199 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2200 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2201 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 2202 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 2203 | * check that [key, key + map->key_size) are within |
| 2204 | * stack limits and initialized |
| 2205 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2206 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2207 | /* in function declaration map_ptr must come before |
| 2208 | * map_key, so that it's verified and known before |
| 2209 | * we have to check map_key here. Otherwise it means |
| 2210 | * that kernel subsystem misconfigured verifier |
| 2211 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2212 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2213 | return -EACCES; |
| 2214 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2215 | err = check_helper_mem_access(env, regno, |
| 2216 | meta->map_ptr->key_size, false, |
| 2217 | NULL); |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2218 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
| 2219 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2220 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 2221 | * check [value, value + map->value_size) validity |
| 2222 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2223 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2224 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2225 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2226 | return -EACCES; |
| 2227 | } |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2228 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2229 | err = check_helper_mem_access(env, regno, |
| 2230 | meta->map_ptr->value_size, false, |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2231 | meta); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2232 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2233 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2234 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2235 | /* remember the mem_size which may be used later |
| 2236 | * to refine return values. |
| 2237 | */ |
| 2238 | meta->msize_smax_value = reg->smax_value; |
| 2239 | meta->msize_umax_value = reg->umax_value; |
| 2240 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2241 | /* The register is SCALAR_VALUE; the access check |
| 2242 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2243 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2244 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2245 | /* For unprivileged variable accesses, disable raw |
| 2246 | * mode so that the program is required to |
| 2247 | * initialize all the memory that the helper could |
| 2248 | * just partially fill up. |
| 2249 | */ |
| 2250 | meta = NULL; |
| 2251 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2252 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2253 | 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] | 2254 | regno); |
| 2255 | return -EACCES; |
| 2256 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2257 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2258 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2259 | err = check_helper_mem_access(env, regno - 1, 0, |
| 2260 | zero_size_allowed, |
| 2261 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2262 | if (err) |
| 2263 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2264 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2265 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2266 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2267 | 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] | 2268 | regno); |
| 2269 | return -EACCES; |
| 2270 | } |
| 2271 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2272 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2273 | zero_size_allowed, meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2277 | err_type: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2278 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2279 | reg_type_str[type], reg_type_str[expected_type]); |
| 2280 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2281 | } |
| 2282 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2283 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 2284 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2285 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2286 | if (!map) |
| 2287 | return 0; |
| 2288 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2289 | /* We need a two way check, first is from map perspective ... */ |
| 2290 | switch (map->map_type) { |
| 2291 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 2292 | if (func_id != BPF_FUNC_tail_call) |
| 2293 | goto error; |
| 2294 | break; |
| 2295 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 2296 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2297 | func_id != BPF_FUNC_perf_event_output && |
| 2298 | func_id != BPF_FUNC_perf_event_read_value) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2299 | goto error; |
| 2300 | break; |
| 2301 | case BPF_MAP_TYPE_STACK_TRACE: |
| 2302 | if (func_id != BPF_FUNC_get_stackid) |
| 2303 | goto error; |
| 2304 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 2305 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 2306 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2307 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2308 | goto error; |
| 2309 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2310 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 2311 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2312 | if (func_id != BPF_FUNC_get_local_storage) |
| 2313 | goto error; |
| 2314 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2315 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
| 2316 | * allow to be modified from bpf side. So do not allow lookup elements |
| 2317 | * for now. |
| 2318 | */ |
| 2319 | case BPF_MAP_TYPE_DEVMAP: |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 2320 | if (func_id != BPF_FUNC_redirect_map) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2321 | goto error; |
| 2322 | break; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2323 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 2324 | * appear. |
| 2325 | */ |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2326 | case BPF_MAP_TYPE_CPUMAP: |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2327 | case BPF_MAP_TYPE_XSKMAP: |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2328 | if (func_id != BPF_FUNC_redirect_map) |
| 2329 | goto error; |
| 2330 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2331 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 2332 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2333 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 2334 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 2335 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2336 | case BPF_MAP_TYPE_SOCKMAP: |
| 2337 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 2338 | func_id != BPF_FUNC_sock_map_update && |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2339 | func_id != BPF_FUNC_map_delete_elem && |
| 2340 | func_id != BPF_FUNC_msg_redirect_map) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2341 | goto error; |
| 2342 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2343 | case BPF_MAP_TYPE_SOCKHASH: |
| 2344 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 2345 | func_id != BPF_FUNC_sock_hash_update && |
| 2346 | func_id != BPF_FUNC_map_delete_elem && |
| 2347 | func_id != BPF_FUNC_msg_redirect_hash) |
| 2348 | goto error; |
| 2349 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2350 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 2351 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 2352 | goto error; |
| 2353 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 2354 | case BPF_MAP_TYPE_QUEUE: |
| 2355 | case BPF_MAP_TYPE_STACK: |
| 2356 | if (func_id != BPF_FUNC_map_peek_elem && |
| 2357 | func_id != BPF_FUNC_map_pop_elem && |
| 2358 | func_id != BPF_FUNC_map_push_elem) |
| 2359 | goto error; |
| 2360 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2361 | default: |
| 2362 | break; |
| 2363 | } |
| 2364 | |
| 2365 | /* ... and second from the function itself. */ |
| 2366 | switch (func_id) { |
| 2367 | case BPF_FUNC_tail_call: |
| 2368 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 2369 | goto error; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 2370 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2371 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
| 2372 | return -EINVAL; |
| 2373 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2374 | break; |
| 2375 | case BPF_FUNC_perf_event_read: |
| 2376 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2377 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2378 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 2379 | goto error; |
| 2380 | break; |
| 2381 | case BPF_FUNC_get_stackid: |
| 2382 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 2383 | goto error; |
| 2384 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2385 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 2386 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2387 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 2388 | goto error; |
| 2389 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2390 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 2391 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2392 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 2393 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2394 | goto error; |
| 2395 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2396 | case BPF_FUNC_sk_redirect_map: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2397 | case BPF_FUNC_msg_redirect_map: |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2398 | case BPF_FUNC_sock_map_update: |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2399 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 2400 | goto error; |
| 2401 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2402 | case BPF_FUNC_sk_redirect_hash: |
| 2403 | case BPF_FUNC_msg_redirect_hash: |
| 2404 | case BPF_FUNC_sock_hash_update: |
| 2405 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2406 | goto error; |
| 2407 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2408 | case BPF_FUNC_get_local_storage: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 2409 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 2410 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2411 | goto error; |
| 2412 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2413 | case BPF_FUNC_sk_select_reuseport: |
| 2414 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) |
| 2415 | goto error; |
| 2416 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 2417 | case BPF_FUNC_map_peek_elem: |
| 2418 | case BPF_FUNC_map_pop_elem: |
| 2419 | case BPF_FUNC_map_push_elem: |
| 2420 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 2421 | map->map_type != BPF_MAP_TYPE_STACK) |
| 2422 | goto error; |
| 2423 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2424 | default: |
| 2425 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2429 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2430 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2431 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2432 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2435 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2436 | { |
| 2437 | int count = 0; |
| 2438 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2439 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2440 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2441 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2442 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2443 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2444 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2445 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2446 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2447 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2448 | count++; |
| 2449 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2450 | /* We only support one arg being in raw mode at the moment, |
| 2451 | * which is sufficient for the helper functions we have |
| 2452 | * right now. |
| 2453 | */ |
| 2454 | return count <= 1; |
| 2455 | } |
| 2456 | |
| 2457 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 2458 | enum bpf_arg_type arg_next) |
| 2459 | { |
| 2460 | return (arg_type_is_mem_ptr(arg_curr) && |
| 2461 | !arg_type_is_mem_size(arg_next)) || |
| 2462 | (!arg_type_is_mem_ptr(arg_curr) && |
| 2463 | arg_type_is_mem_size(arg_next)); |
| 2464 | } |
| 2465 | |
| 2466 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 2467 | { |
| 2468 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 2469 | * bytes from memory 'buf'. Both arg types need |
| 2470 | * to be paired, so make sure there's no buggy |
| 2471 | * helper function specification. |
| 2472 | */ |
| 2473 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 2474 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 2475 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 2476 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 2477 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 2478 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 2479 | return false; |
| 2480 | |
| 2481 | return true; |
| 2482 | } |
| 2483 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2484 | static bool check_refcount_ok(const struct bpf_func_proto *fn) |
| 2485 | { |
| 2486 | int count = 0; |
| 2487 | |
| 2488 | if (arg_type_is_refcounted(fn->arg1_type)) |
| 2489 | count++; |
| 2490 | if (arg_type_is_refcounted(fn->arg2_type)) |
| 2491 | count++; |
| 2492 | if (arg_type_is_refcounted(fn->arg3_type)) |
| 2493 | count++; |
| 2494 | if (arg_type_is_refcounted(fn->arg4_type)) |
| 2495 | count++; |
| 2496 | if (arg_type_is_refcounted(fn->arg5_type)) |
| 2497 | count++; |
| 2498 | |
| 2499 | /* We only support one arg being unreferenced at the moment, |
| 2500 | * which is sufficient for the helper functions we have right now. |
| 2501 | */ |
| 2502 | return count <= 1; |
| 2503 | } |
| 2504 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2505 | static int check_func_proto(const struct bpf_func_proto *fn) |
| 2506 | { |
| 2507 | return check_raw_mode_ok(fn) && |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2508 | check_arg_pair_ok(fn) && |
| 2509 | check_refcount_ok(fn) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2510 | } |
| 2511 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2512 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 2513 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2514 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2515 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 2516 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2517 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2518 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2519 | int i; |
| 2520 | |
| 2521 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2522 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2523 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2524 | |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 2525 | bpf_for_each_spilled_reg(i, state, reg) { |
| 2526 | if (!reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2527 | continue; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2528 | if (reg_is_pkt_pointer_any(reg)) |
| 2529 | __mark_reg_unknown(reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2530 | } |
| 2531 | } |
| 2532 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2533 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 2534 | { |
| 2535 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2536 | int i; |
| 2537 | |
| 2538 | for (i = 0; i <= vstate->curframe; i++) |
| 2539 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 2540 | } |
| 2541 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2542 | static void release_reg_references(struct bpf_verifier_env *env, |
| 2543 | struct bpf_func_state *state, int id) |
| 2544 | { |
| 2545 | struct bpf_reg_state *regs = state->regs, *reg; |
| 2546 | int i; |
| 2547 | |
| 2548 | for (i = 0; i < MAX_BPF_REG; i++) |
| 2549 | if (regs[i].id == id) |
| 2550 | mark_reg_unknown(env, regs, i); |
| 2551 | |
| 2552 | bpf_for_each_spilled_reg(i, state, reg) { |
| 2553 | if (!reg) |
| 2554 | continue; |
| 2555 | if (reg_is_refcounted(reg) && reg->id == id) |
| 2556 | __mark_reg_unknown(reg); |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | /* The pointer with the specified id has released its reference to kernel |
| 2561 | * resources. Identify all copies of the same pointer and clear the reference. |
| 2562 | */ |
| 2563 | static int release_reference(struct bpf_verifier_env *env, |
| 2564 | struct bpf_call_arg_meta *meta) |
| 2565 | { |
| 2566 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2567 | int i; |
| 2568 | |
| 2569 | for (i = 0; i <= vstate->curframe; i++) |
| 2570 | release_reg_references(env, vstate->frame[i], meta->ptr_id); |
| 2571 | |
| 2572 | return release_reference_state(env, meta->ptr_id); |
| 2573 | } |
| 2574 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2575 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 2576 | int *insn_idx) |
| 2577 | { |
| 2578 | struct bpf_verifier_state *state = env->cur_state; |
| 2579 | struct bpf_func_state *caller, *callee; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2580 | int i, err, subprog, target_insn; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2581 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 2582 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2583 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 2584 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2585 | return -E2BIG; |
| 2586 | } |
| 2587 | |
| 2588 | target_insn = *insn_idx + insn->imm; |
| 2589 | subprog = find_subprog(env, target_insn + 1); |
| 2590 | if (subprog < 0) { |
| 2591 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 2592 | target_insn + 1); |
| 2593 | return -EFAULT; |
| 2594 | } |
| 2595 | |
| 2596 | caller = state->frame[state->curframe]; |
| 2597 | if (state->frame[state->curframe + 1]) { |
| 2598 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 2599 | state->curframe + 1); |
| 2600 | return -EFAULT; |
| 2601 | } |
| 2602 | |
| 2603 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 2604 | if (!callee) |
| 2605 | return -ENOMEM; |
| 2606 | state->frame[state->curframe + 1] = callee; |
| 2607 | |
| 2608 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 2609 | * into its own stack before reading from it. |
| 2610 | * callee can read/write into caller's stack |
| 2611 | */ |
| 2612 | init_func_state(env, callee, |
| 2613 | /* remember the callsite, it will be used by bpf_exit */ |
| 2614 | *insn_idx /* callsite */, |
| 2615 | state->curframe + 1 /* frameno within this callchain */, |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 2616 | subprog /* subprog number within this prog */); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2617 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2618 | /* Transfer references to the callee */ |
| 2619 | err = transfer_reference_state(callee, caller); |
| 2620 | if (err) |
| 2621 | return err; |
| 2622 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2623 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 2624 | * pointers, which connects us up to the liveness chain |
| 2625 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2626 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 2627 | callee->regs[i] = caller->regs[i]; |
| 2628 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2629 | /* after the call registers r0 - r5 were scratched */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2630 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 2631 | mark_reg_not_init(env, caller->regs, caller_saved[i]); |
| 2632 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2633 | } |
| 2634 | |
| 2635 | /* only increment it after check_reg_arg() finished */ |
| 2636 | state->curframe++; |
| 2637 | |
| 2638 | /* and go analyze first insn of the callee */ |
| 2639 | *insn_idx = target_insn; |
| 2640 | |
| 2641 | if (env->log.level) { |
| 2642 | verbose(env, "caller:\n"); |
| 2643 | print_verifier_state(env, caller); |
| 2644 | verbose(env, "callee:\n"); |
| 2645 | print_verifier_state(env, callee); |
| 2646 | } |
| 2647 | return 0; |
| 2648 | } |
| 2649 | |
| 2650 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 2651 | { |
| 2652 | struct bpf_verifier_state *state = env->cur_state; |
| 2653 | struct bpf_func_state *caller, *callee; |
| 2654 | struct bpf_reg_state *r0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2655 | int err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2656 | |
| 2657 | callee = state->frame[state->curframe]; |
| 2658 | r0 = &callee->regs[BPF_REG_0]; |
| 2659 | if (r0->type == PTR_TO_STACK) { |
| 2660 | /* technically it's ok to return caller's stack pointer |
| 2661 | * (or caller's caller's pointer) back to the caller, |
| 2662 | * since these pointers are valid. Only current stack |
| 2663 | * pointer will be invalid as soon as function exits, |
| 2664 | * but let's be conservative |
| 2665 | */ |
| 2666 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 2667 | return -EINVAL; |
| 2668 | } |
| 2669 | |
| 2670 | state->curframe--; |
| 2671 | caller = state->frame[state->curframe]; |
| 2672 | /* return to the caller whatever r0 had in the callee */ |
| 2673 | caller->regs[BPF_REG_0] = *r0; |
| 2674 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2675 | /* Transfer references to the caller */ |
| 2676 | err = transfer_reference_state(caller, callee); |
| 2677 | if (err) |
| 2678 | return err; |
| 2679 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2680 | *insn_idx = callee->callsite + 1; |
| 2681 | if (env->log.level) { |
| 2682 | verbose(env, "returning from callee:\n"); |
| 2683 | print_verifier_state(env, callee); |
| 2684 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 2685 | print_verifier_state(env, caller); |
| 2686 | } |
| 2687 | /* clear everything in the callee */ |
| 2688 | free_func_state(callee); |
| 2689 | state->frame[state->curframe + 1] = NULL; |
| 2690 | return 0; |
| 2691 | } |
| 2692 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2693 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 2694 | int func_id, |
| 2695 | struct bpf_call_arg_meta *meta) |
| 2696 | { |
| 2697 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 2698 | |
| 2699 | if (ret_type != RET_INTEGER || |
| 2700 | (func_id != BPF_FUNC_get_stack && |
| 2701 | func_id != BPF_FUNC_probe_read_str)) |
| 2702 | return; |
| 2703 | |
| 2704 | ret_reg->smax_value = meta->msize_smax_value; |
| 2705 | ret_reg->umax_value = meta->msize_umax_value; |
| 2706 | __reg_deduce_bounds(ret_reg); |
| 2707 | __reg_bound_offset(ret_reg); |
| 2708 | } |
| 2709 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2710 | static int |
| 2711 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 2712 | int func_id, int insn_idx) |
| 2713 | { |
| 2714 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
| 2715 | |
| 2716 | if (func_id != BPF_FUNC_tail_call && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 2717 | func_id != BPF_FUNC_map_lookup_elem && |
| 2718 | func_id != BPF_FUNC_map_update_elem && |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 2719 | func_id != BPF_FUNC_map_delete_elem && |
| 2720 | func_id != BPF_FUNC_map_push_elem && |
| 2721 | func_id != BPF_FUNC_map_pop_elem && |
| 2722 | func_id != BPF_FUNC_map_peek_elem) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2723 | return 0; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 2724 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2725 | if (meta->map_ptr == NULL) { |
| 2726 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 2727 | return -EINVAL; |
| 2728 | } |
| 2729 | |
| 2730 | if (!BPF_MAP_PTR(aux->map_state)) |
| 2731 | bpf_map_ptr_store(aux, meta->map_ptr, |
| 2732 | meta->map_ptr->unpriv_array); |
| 2733 | else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr) |
| 2734 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
| 2735 | meta->map_ptr->unpriv_array); |
| 2736 | return 0; |
| 2737 | } |
| 2738 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2739 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 2740 | { |
| 2741 | struct bpf_func_state *state = cur_func(env); |
| 2742 | int i; |
| 2743 | |
| 2744 | for (i = 0; i < state->acquired_refs; i++) { |
| 2745 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 2746 | state->refs[i].id, state->refs[i].insn_idx); |
| 2747 | } |
| 2748 | return state->acquired_refs ? -EINVAL : 0; |
| 2749 | } |
| 2750 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2751 | 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] | 2752 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2753 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2754 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2755 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2756 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2757 | int i, err; |
| 2758 | |
| 2759 | /* find function prototype */ |
| 2760 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2761 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 2762 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2763 | return -EINVAL; |
| 2764 | } |
| 2765 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 2766 | if (env->ops->get_func_proto) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2767 | fn = env->ops->get_func_proto(func_id, env->prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2768 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2769 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 2770 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2771 | return -EINVAL; |
| 2772 | } |
| 2773 | |
| 2774 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 2775 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Daniel Borkmann | 3fe2867 | 2018-06-02 23:06:33 +0200 | [diff] [blame] | 2776 | 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] | 2777 | return -EINVAL; |
| 2778 | } |
| 2779 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 2780 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 2781 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 2782 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 2783 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 2784 | func_id_name(func_id), func_id); |
| 2785 | return -EINVAL; |
| 2786 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2787 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2788 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2789 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2790 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2791 | err = check_func_proto(fn); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2792 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2793 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2794 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2795 | return err; |
| 2796 | } |
| 2797 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2798 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2799 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2800 | if (err) |
| 2801 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2802 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2803 | if (err) |
| 2804 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2805 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2806 | if (err) |
| 2807 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2808 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2809 | if (err) |
| 2810 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2811 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2812 | if (err) |
| 2813 | return err; |
| 2814 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2815 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 2816 | if (err) |
| 2817 | return err; |
| 2818 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2819 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 2820 | * is inferred from register state. |
| 2821 | */ |
| 2822 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2823 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 2824 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2825 | if (err) |
| 2826 | return err; |
| 2827 | } |
| 2828 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2829 | if (func_id == BPF_FUNC_tail_call) { |
| 2830 | err = check_reference_leak(env); |
| 2831 | if (err) { |
| 2832 | verbose(env, "tail_call would lead to reference leak\n"); |
| 2833 | return err; |
| 2834 | } |
| 2835 | } else if (is_release_function(func_id)) { |
| 2836 | err = release_reference(env, &meta); |
| 2837 | if (err) |
| 2838 | return err; |
| 2839 | } |
| 2840 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2841 | regs = cur_regs(env); |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2842 | |
| 2843 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 2844 | * this is required because get_local_storage() can't return an error. |
| 2845 | */ |
| 2846 | if (func_id == BPF_FUNC_get_local_storage && |
| 2847 | !register_is_null(®s[BPF_REG_2])) { |
| 2848 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 2849 | return -EINVAL; |
| 2850 | } |
| 2851 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2852 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2853 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2854 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2855 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2856 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2857 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2858 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2859 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2860 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2861 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2862 | } else if (fn->ret_type == RET_VOID) { |
| 2863 | regs[BPF_REG_0].type = NOT_INIT; |
Roman Gushchin | 3e6a4b3 | 2018-08-02 14:27:22 -0700 | [diff] [blame] | 2864 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 2865 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2866 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2867 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2868 | /* remember map_ptr, so that check_map_access() |
| 2869 | * can check 'value_size' boundary of memory access |
| 2870 | * to map element returned from bpf_map_lookup_elem() |
| 2871 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2872 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2873 | verbose(env, |
| 2874 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2875 | return -EINVAL; |
| 2876 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2877 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 2878 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 2879 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
| 2880 | } else { |
| 2881 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
| 2882 | regs[BPF_REG_0].id = ++env->id_gen; |
| 2883 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2884 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2885 | int id = acquire_reference_state(env, insn_idx); |
| 2886 | if (id < 0) |
| 2887 | return id; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2888 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 2889 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2890 | regs[BPF_REG_0].id = id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2891 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2892 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2893 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2894 | return -EINVAL; |
| 2895 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2896 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2897 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
| 2898 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2899 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2900 | if (err) |
| 2901 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2902 | |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 2903 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
| 2904 | const char *err_str; |
| 2905 | |
| 2906 | #ifdef CONFIG_PERF_EVENTS |
| 2907 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 2908 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 2909 | #else |
| 2910 | err = -ENOTSUPP; |
| 2911 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 2912 | #endif |
| 2913 | if (err) { |
| 2914 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 2915 | return err; |
| 2916 | } |
| 2917 | |
| 2918 | env->prog->has_callchain_buf = true; |
| 2919 | } |
| 2920 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2921 | if (changes_data) |
| 2922 | clear_all_pkt_pointers(env); |
| 2923 | return 0; |
| 2924 | } |
| 2925 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2926 | static bool signed_add_overflows(s64 a, s64 b) |
| 2927 | { |
| 2928 | /* Do the add in u64, where overflow is well-defined */ |
| 2929 | s64 res = (s64)((u64)a + (u64)b); |
| 2930 | |
| 2931 | if (b < 0) |
| 2932 | return res > a; |
| 2933 | return res < a; |
| 2934 | } |
| 2935 | |
| 2936 | static bool signed_sub_overflows(s64 a, s64 b) |
| 2937 | { |
| 2938 | /* Do the sub in u64, where overflow is well-defined */ |
| 2939 | s64 res = (s64)((u64)a - (u64)b); |
| 2940 | |
| 2941 | if (b < 0) |
| 2942 | return res < a; |
| 2943 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2944 | } |
| 2945 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2946 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 2947 | const struct bpf_reg_state *reg, |
| 2948 | enum bpf_reg_type type) |
| 2949 | { |
| 2950 | bool known = tnum_is_const(reg->var_off); |
| 2951 | s64 val = reg->var_off.value; |
| 2952 | s64 smin = reg->smin_value; |
| 2953 | |
| 2954 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 2955 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 2956 | reg_type_str[type], val); |
| 2957 | return false; |
| 2958 | } |
| 2959 | |
| 2960 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 2961 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 2962 | reg_type_str[type], reg->off); |
| 2963 | return false; |
| 2964 | } |
| 2965 | |
| 2966 | if (smin == S64_MIN) { |
| 2967 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 2968 | reg_type_str[type]); |
| 2969 | return false; |
| 2970 | } |
| 2971 | |
| 2972 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 2973 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 2974 | smin, reg_type_str[type]); |
| 2975 | return false; |
| 2976 | } |
| 2977 | |
| 2978 | return true; |
| 2979 | } |
| 2980 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2981 | /* 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] | 2982 | * Caller should also handle BPF_MOV case separately. |
| 2983 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 2984 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 2985 | */ |
| 2986 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 2987 | struct bpf_insn *insn, |
| 2988 | const struct bpf_reg_state *ptr_reg, |
| 2989 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2990 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2991 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2992 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2993 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2994 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2995 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 2996 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 2997 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 2998 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2999 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3000 | u32 dst = insn->dst_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3001 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3002 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3003 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 3004 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 3005 | smin_val > smax_val || umin_val > umax_val) { |
| 3006 | /* Taint dst register if offset had invalid bounds derived from |
| 3007 | * e.g. dead branches. |
| 3008 | */ |
| 3009 | __mark_reg_unknown(dst_reg); |
| 3010 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3011 | } |
| 3012 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3013 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3014 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3015 | verbose(env, |
| 3016 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 3017 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3018 | return -EACCES; |
| 3019 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3020 | |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3021 | switch (ptr_reg->type) { |
| 3022 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 3023 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
| 3024 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3025 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3026 | case CONST_PTR_TO_MAP: |
| 3027 | case PTR_TO_PACKET_END: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3028 | case PTR_TO_SOCKET: |
| 3029 | case PTR_TO_SOCKET_OR_NULL: |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3030 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
| 3031 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3032 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3033 | default: |
| 3034 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 3038 | * 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] | 3039 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3040 | dst_reg->type = ptr_reg->type; |
| 3041 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 3042 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3043 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 3044 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 3045 | return -EINVAL; |
| 3046 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3047 | switch (opcode) { |
| 3048 | case BPF_ADD: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3049 | /* We can take a fixed offset as long as it doesn't overflow |
| 3050 | * the s32 'off' field |
| 3051 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3052 | if (known && (ptr_reg->off + smin_val == |
| 3053 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3054 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3055 | dst_reg->smin_value = smin_ptr; |
| 3056 | dst_reg->smax_value = smax_ptr; |
| 3057 | dst_reg->umin_value = umin_ptr; |
| 3058 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3059 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3060 | dst_reg->off = ptr_reg->off + smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3061 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3062 | break; |
| 3063 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3064 | /* A new variable offset is created. Note that off_reg->off |
| 3065 | * == 0, since it's a scalar. |
| 3066 | * dst_reg gets the pointer type and since some positive |
| 3067 | * integer value was added to the pointer, give it a new 'id' |
| 3068 | * if it's a PTR_TO_PACKET. |
| 3069 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 3070 | * added into the variable offset, and we copy the fixed offset |
| 3071 | * from ptr_reg. |
| 3072 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3073 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 3074 | signed_add_overflows(smax_ptr, smax_val)) { |
| 3075 | dst_reg->smin_value = S64_MIN; |
| 3076 | dst_reg->smax_value = S64_MAX; |
| 3077 | } else { |
| 3078 | dst_reg->smin_value = smin_ptr + smin_val; |
| 3079 | dst_reg->smax_value = smax_ptr + smax_val; |
| 3080 | } |
| 3081 | if (umin_ptr + umin_val < umin_ptr || |
| 3082 | umax_ptr + umax_val < umax_ptr) { |
| 3083 | dst_reg->umin_value = 0; |
| 3084 | dst_reg->umax_value = U64_MAX; |
| 3085 | } else { |
| 3086 | dst_reg->umin_value = umin_ptr + umin_val; |
| 3087 | dst_reg->umax_value = umax_ptr + umax_val; |
| 3088 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3089 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 3090 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3091 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3092 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3093 | dst_reg->id = ++env->id_gen; |
| 3094 | /* something was added to pkt_ptr, set range to zero */ |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3095 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3096 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3097 | break; |
| 3098 | case BPF_SUB: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3099 | if (dst_reg == off_reg) { |
| 3100 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3101 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 3102 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3103 | return -EACCES; |
| 3104 | } |
| 3105 | /* We don't allow subtraction from FP, because (according to |
| 3106 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 3107 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 3108 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3109 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3110 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 3111 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3112 | return -EACCES; |
| 3113 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3114 | if (known && (ptr_reg->off - smin_val == |
| 3115 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3116 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3117 | dst_reg->smin_value = smin_ptr; |
| 3118 | dst_reg->smax_value = smax_ptr; |
| 3119 | dst_reg->umin_value = umin_ptr; |
| 3120 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3121 | dst_reg->var_off = ptr_reg->var_off; |
| 3122 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3123 | dst_reg->off = ptr_reg->off - smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3124 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3125 | break; |
| 3126 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3127 | /* A new variable offset is created. If the subtrahend is known |
| 3128 | * nonnegative, then any reg->range we had before is still good. |
| 3129 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3130 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 3131 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 3132 | /* Overflow possible, we know nothing */ |
| 3133 | dst_reg->smin_value = S64_MIN; |
| 3134 | dst_reg->smax_value = S64_MAX; |
| 3135 | } else { |
| 3136 | dst_reg->smin_value = smin_ptr - smax_val; |
| 3137 | dst_reg->smax_value = smax_ptr - smin_val; |
| 3138 | } |
| 3139 | if (umin_ptr < umax_val) { |
| 3140 | /* Overflow possible, we know nothing */ |
| 3141 | dst_reg->umin_value = 0; |
| 3142 | dst_reg->umax_value = U64_MAX; |
| 3143 | } else { |
| 3144 | /* Cannot overflow (as long as bounds are consistent) */ |
| 3145 | dst_reg->umin_value = umin_ptr - umax_val; |
| 3146 | dst_reg->umax_value = umax_ptr - umin_val; |
| 3147 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3148 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 3149 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3150 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3151 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3152 | dst_reg->id = ++env->id_gen; |
| 3153 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3154 | if (smin_val < 0) |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3155 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3156 | } |
| 3157 | break; |
| 3158 | case BPF_AND: |
| 3159 | case BPF_OR: |
| 3160 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3161 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 3162 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 3163 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3164 | return -EACCES; |
| 3165 | default: |
| 3166 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3167 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 3168 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3169 | return -EACCES; |
| 3170 | } |
| 3171 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3172 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 3173 | return -EINVAL; |
| 3174 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3175 | __update_reg_bounds(dst_reg); |
| 3176 | __reg_deduce_bounds(dst_reg); |
| 3177 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3178 | return 0; |
| 3179 | } |
| 3180 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3181 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 3182 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 3183 | * need extra checks in the 32-bit case. |
| 3184 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3185 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 3186 | struct bpf_insn *insn, |
| 3187 | struct bpf_reg_state *dst_reg, |
| 3188 | struct bpf_reg_state src_reg) |
| 3189 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3190 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3191 | u8 opcode = BPF_OP(insn->code); |
| 3192 | bool src_known, dst_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3193 | s64 smin_val, smax_val; |
| 3194 | u64 umin_val, umax_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3195 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3196 | |
Jann Horn | b799207 | 2018-10-05 18:17:59 +0200 | [diff] [blame] | 3197 | if (insn_bitness == 32) { |
| 3198 | /* Relevant for 32-bit RSH: Information can propagate towards |
| 3199 | * LSB, so it isn't sufficient to only truncate the output to |
| 3200 | * 32 bits. |
| 3201 | */ |
| 3202 | coerce_reg_to_size(dst_reg, 4); |
| 3203 | coerce_reg_to_size(&src_reg, 4); |
| 3204 | } |
| 3205 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3206 | smin_val = src_reg.smin_value; |
| 3207 | smax_val = src_reg.smax_value; |
| 3208 | umin_val = src_reg.umin_value; |
| 3209 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3210 | src_known = tnum_is_const(src_reg.var_off); |
| 3211 | dst_known = tnum_is_const(dst_reg->var_off); |
| 3212 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 3213 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
| 3214 | smin_val > smax_val || umin_val > umax_val) { |
| 3215 | /* Taint dst register if offset had invalid bounds derived from |
| 3216 | * e.g. dead branches. |
| 3217 | */ |
| 3218 | __mark_reg_unknown(dst_reg); |
| 3219 | return 0; |
| 3220 | } |
| 3221 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3222 | if (!src_known && |
| 3223 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
| 3224 | __mark_reg_unknown(dst_reg); |
| 3225 | return 0; |
| 3226 | } |
| 3227 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3228 | switch (opcode) { |
| 3229 | case BPF_ADD: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3230 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 3231 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 3232 | dst_reg->smin_value = S64_MIN; |
| 3233 | dst_reg->smax_value = S64_MAX; |
| 3234 | } else { |
| 3235 | dst_reg->smin_value += smin_val; |
| 3236 | dst_reg->smax_value += smax_val; |
| 3237 | } |
| 3238 | if (dst_reg->umin_value + umin_val < umin_val || |
| 3239 | dst_reg->umax_value + umax_val < umax_val) { |
| 3240 | dst_reg->umin_value = 0; |
| 3241 | dst_reg->umax_value = U64_MAX; |
| 3242 | } else { |
| 3243 | dst_reg->umin_value += umin_val; |
| 3244 | dst_reg->umax_value += umax_val; |
| 3245 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3246 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 3247 | break; |
| 3248 | case BPF_SUB: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3249 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 3250 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 3251 | /* Overflow possible, we know nothing */ |
| 3252 | dst_reg->smin_value = S64_MIN; |
| 3253 | dst_reg->smax_value = S64_MAX; |
| 3254 | } else { |
| 3255 | dst_reg->smin_value -= smax_val; |
| 3256 | dst_reg->smax_value -= smin_val; |
| 3257 | } |
| 3258 | if (dst_reg->umin_value < umax_val) { |
| 3259 | /* Overflow possible, we know nothing */ |
| 3260 | dst_reg->umin_value = 0; |
| 3261 | dst_reg->umax_value = U64_MAX; |
| 3262 | } else { |
| 3263 | /* Cannot overflow (as long as bounds are consistent) */ |
| 3264 | dst_reg->umin_value -= umax_val; |
| 3265 | dst_reg->umax_value -= umin_val; |
| 3266 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3267 | 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] | 3268 | break; |
| 3269 | case BPF_MUL: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3270 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 3271 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3272 | /* Ain't nobody got time to multiply that sign */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3273 | __mark_reg_unbounded(dst_reg); |
| 3274 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3275 | break; |
| 3276 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3277 | /* Both values are positive, so we can work with unsigned and |
| 3278 | * copy the result to signed (unless it exceeds S64_MAX). |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3279 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3280 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 3281 | /* Potential overflow, we know nothing */ |
| 3282 | __mark_reg_unbounded(dst_reg); |
| 3283 | /* (except what we can learn from the var_off) */ |
| 3284 | __update_reg_bounds(dst_reg); |
| 3285 | break; |
| 3286 | } |
| 3287 | dst_reg->umin_value *= umin_val; |
| 3288 | dst_reg->umax_value *= umax_val; |
| 3289 | if (dst_reg->umax_value > S64_MAX) { |
| 3290 | /* Overflow possible, we know nothing */ |
| 3291 | dst_reg->smin_value = S64_MIN; |
| 3292 | dst_reg->smax_value = S64_MAX; |
| 3293 | } else { |
| 3294 | dst_reg->smin_value = dst_reg->umin_value; |
| 3295 | dst_reg->smax_value = dst_reg->umax_value; |
| 3296 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3297 | break; |
| 3298 | case BPF_AND: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3299 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3300 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 3301 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3302 | break; |
| 3303 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3304 | /* We get our minimum from the var_off, since that's inherently |
| 3305 | * bitwise. Our maximum is the minimum of the operands' maxima. |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 3306 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3307 | 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] | 3308 | dst_reg->umin_value = dst_reg->var_off.value; |
| 3309 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 3310 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 3311 | /* Lose signed bounds when ANDing negative numbers, |
| 3312 | * ain't nobody got time for that. |
| 3313 | */ |
| 3314 | dst_reg->smin_value = S64_MIN; |
| 3315 | dst_reg->smax_value = S64_MAX; |
| 3316 | } else { |
| 3317 | /* ANDing two positives gives a positive, so safe to |
| 3318 | * cast result into s64. |
| 3319 | */ |
| 3320 | dst_reg->smin_value = dst_reg->umin_value; |
| 3321 | dst_reg->smax_value = dst_reg->umax_value; |
| 3322 | } |
| 3323 | /* We may learn something more from the var_off */ |
| 3324 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3325 | break; |
| 3326 | case BPF_OR: |
| 3327 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3328 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
| 3329 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3330 | break; |
| 3331 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3332 | /* We get our maximum from the var_off, and our minimum is the |
| 3333 | * maximum of the operands' minima |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3334 | */ |
| 3335 | 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] | 3336 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 3337 | dst_reg->umax_value = dst_reg->var_off.value | |
| 3338 | dst_reg->var_off.mask; |
| 3339 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 3340 | /* Lose signed bounds when ORing negative numbers, |
| 3341 | * ain't nobody got time for that. |
| 3342 | */ |
| 3343 | dst_reg->smin_value = S64_MIN; |
| 3344 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3345 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3346 | /* ORing two positives gives a positive, so safe to |
| 3347 | * cast result into s64. |
| 3348 | */ |
| 3349 | dst_reg->smin_value = dst_reg->umin_value; |
| 3350 | dst_reg->smax_value = dst_reg->umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3351 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3352 | /* We may learn something more from the var_off */ |
| 3353 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3354 | break; |
| 3355 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3356 | if (umax_val >= insn_bitness) { |
| 3357 | /* Shifts greater than 31 or 63 are undefined. |
| 3358 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3359 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3360 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3361 | break; |
| 3362 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3363 | /* We lose all sign bit information (except what we can pick |
| 3364 | * up from var_off) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3365 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3366 | dst_reg->smin_value = S64_MIN; |
| 3367 | dst_reg->smax_value = S64_MAX; |
| 3368 | /* If we might shift our top bit out, then we know nothing */ |
| 3369 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 3370 | dst_reg->umin_value = 0; |
| 3371 | dst_reg->umax_value = U64_MAX; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3372 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3373 | dst_reg->umin_value <<= umin_val; |
| 3374 | dst_reg->umax_value <<= umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3375 | } |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 3376 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3377 | /* We may learn something more from the var_off */ |
| 3378 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3379 | break; |
| 3380 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3381 | if (umax_val >= insn_bitness) { |
| 3382 | /* Shifts greater than 31 or 63 are undefined. |
| 3383 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3384 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3385 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3386 | break; |
| 3387 | } |
Edward Cree | 4374f25 | 2017-12-18 20:11:53 -0800 | [diff] [blame] | 3388 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 3389 | * be negative, then either: |
| 3390 | * 1) src_reg might be zero, so the sign bit of the result is |
| 3391 | * unknown, so we lose our signed bounds |
| 3392 | * 2) it's known negative, thus the unsigned bounds capture the |
| 3393 | * signed bounds |
| 3394 | * 3) the signed bounds cross zero, so they tell us nothing |
| 3395 | * about the result |
| 3396 | * If the value in dst_reg is known nonnegative, then again the |
| 3397 | * unsigned bounts capture the signed bounds. |
| 3398 | * Thus, in all cases it suffices to blow away our signed bounds |
| 3399 | * and rely on inferring new ones from the unsigned bounds and |
| 3400 | * var_off of the result. |
| 3401 | */ |
| 3402 | dst_reg->smin_value = S64_MIN; |
| 3403 | dst_reg->smax_value = S64_MAX; |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 3404 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3405 | dst_reg->umin_value >>= umax_val; |
| 3406 | dst_reg->umax_value >>= umin_val; |
| 3407 | /* We may learn something more from the var_off */ |
| 3408 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3409 | break; |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 3410 | case BPF_ARSH: |
| 3411 | if (umax_val >= insn_bitness) { |
| 3412 | /* Shifts greater than 31 or 63 are undefined. |
| 3413 | * This includes shifts by a negative number. |
| 3414 | */ |
| 3415 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 3416 | break; |
| 3417 | } |
| 3418 | |
| 3419 | /* Upon reaching here, src_known is true and |
| 3420 | * umax_val is equal to umin_val. |
| 3421 | */ |
| 3422 | dst_reg->smin_value >>= umin_val; |
| 3423 | dst_reg->smax_value >>= umin_val; |
| 3424 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val); |
| 3425 | |
| 3426 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 3427 | * dst_reg var_off to refine the result. |
| 3428 | */ |
| 3429 | dst_reg->umin_value = 0; |
| 3430 | dst_reg->umax_value = U64_MAX; |
| 3431 | __update_reg_bounds(dst_reg); |
| 3432 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3433 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3434 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3435 | break; |
| 3436 | } |
| 3437 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3438 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3439 | /* 32-bit ALU ops are (32,32)->32 */ |
| 3440 | coerce_reg_to_size(dst_reg, 4); |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3441 | } |
| 3442 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3443 | __reg_deduce_bounds(dst_reg); |
| 3444 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3445 | return 0; |
| 3446 | } |
| 3447 | |
| 3448 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 3449 | * and var_off. |
| 3450 | */ |
| 3451 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 3452 | struct bpf_insn *insn) |
| 3453 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3454 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3455 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 3456 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3457 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 3458 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3459 | |
| 3460 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3461 | src_reg = NULL; |
| 3462 | if (dst_reg->type != SCALAR_VALUE) |
| 3463 | ptr_reg = dst_reg; |
| 3464 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3465 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3466 | if (src_reg->type != SCALAR_VALUE) { |
| 3467 | if (dst_reg->type != SCALAR_VALUE) { |
| 3468 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3469 | * an arbitrary scalar. Disallow all math except |
| 3470 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3471 | */ |
Alexei Starovoitov | dd06682 | 2018-09-12 14:06:10 -0700 | [diff] [blame] | 3472 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3473 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 3474 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3475 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3476 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 3477 | insn->dst_reg, |
| 3478 | bpf_alu_string[opcode >> 4]); |
| 3479 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3480 | } else { |
| 3481 | /* scalar += pointer |
| 3482 | * This is legal, but we have to reverse our |
| 3483 | * src/dest handling in computing the range |
| 3484 | */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3485 | return adjust_ptr_min_max_vals(env, insn, |
| 3486 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3487 | } |
| 3488 | } else if (ptr_reg) { |
| 3489 | /* pointer += scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3490 | return adjust_ptr_min_max_vals(env, insn, |
| 3491 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3492 | } |
| 3493 | } else { |
| 3494 | /* Pretend the src is a reg with a known value, since we only |
| 3495 | * need to be able to read from this state. |
| 3496 | */ |
| 3497 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3498 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3499 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3500 | if (ptr_reg) /* pointer += K */ |
| 3501 | return adjust_ptr_min_max_vals(env, insn, |
| 3502 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3503 | } |
| 3504 | |
| 3505 | /* Got here implies adding two SCALAR_VALUEs */ |
| 3506 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3507 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3508 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3509 | return -EINVAL; |
| 3510 | } |
| 3511 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3512 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3513 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3514 | return -EINVAL; |
| 3515 | } |
| 3516 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3517 | } |
| 3518 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3519 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3520 | 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] | 3521 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3522 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3523 | u8 opcode = BPF_OP(insn->code); |
| 3524 | int err; |
| 3525 | |
| 3526 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 3527 | if (opcode == BPF_NEG) { |
| 3528 | if (BPF_SRC(insn->code) != 0 || |
| 3529 | insn->src_reg != BPF_REG_0 || |
| 3530 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3531 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3532 | return -EINVAL; |
| 3533 | } |
| 3534 | } else { |
| 3535 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 3536 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 3537 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3538 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3539 | return -EINVAL; |
| 3540 | } |
| 3541 | } |
| 3542 | |
| 3543 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3544 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3545 | if (err) |
| 3546 | return err; |
| 3547 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3548 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3549 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3550 | insn->dst_reg); |
| 3551 | return -EACCES; |
| 3552 | } |
| 3553 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3554 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3555 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3556 | if (err) |
| 3557 | return err; |
| 3558 | |
| 3559 | } else if (opcode == BPF_MOV) { |
| 3560 | |
| 3561 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3562 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3563 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3564 | return -EINVAL; |
| 3565 | } |
| 3566 | |
| 3567 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3568 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3569 | if (err) |
| 3570 | return err; |
| 3571 | } else { |
| 3572 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3573 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3574 | return -EINVAL; |
| 3575 | } |
| 3576 | } |
| 3577 | |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 3578 | /* check dest operand, mark as required later */ |
| 3579 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3580 | if (err) |
| 3581 | return err; |
| 3582 | |
| 3583 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3584 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 3585 | /* case: R1 = R2 |
| 3586 | * copy register state to dest reg |
| 3587 | */ |
| 3588 | regs[insn->dst_reg] = regs[insn->src_reg]; |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 3589 | regs[insn->dst_reg].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3590 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3591 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3592 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3593 | verbose(env, |
| 3594 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3595 | insn->src_reg); |
| 3596 | return -EACCES; |
| 3597 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3598 | mark_reg_unknown(env, regs, insn->dst_reg); |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3599 | coerce_reg_to_size(®s[insn->dst_reg], 4); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3600 | } |
| 3601 | } else { |
| 3602 | /* case: R = imm |
| 3603 | * remember the value we stored into this reg |
| 3604 | */ |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 3605 | /* clear any state __mark_reg_known doesn't set */ |
| 3606 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3607 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 3608 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 3609 | __mark_reg_known(regs + insn->dst_reg, |
| 3610 | insn->imm); |
| 3611 | } else { |
| 3612 | __mark_reg_known(regs + insn->dst_reg, |
| 3613 | (u32)insn->imm); |
| 3614 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3615 | } |
| 3616 | |
| 3617 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3618 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3619 | return -EINVAL; |
| 3620 | |
| 3621 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 3622 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3623 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3624 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3625 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3626 | return -EINVAL; |
| 3627 | } |
| 3628 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3629 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3630 | if (err) |
| 3631 | return err; |
| 3632 | } else { |
| 3633 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3634 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3635 | return -EINVAL; |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3640 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3641 | if (err) |
| 3642 | return err; |
| 3643 | |
| 3644 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 3645 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3646 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3647 | return -EINVAL; |
| 3648 | } |
| 3649 | |
Daniel Borkmann | 7891a87 | 2018-01-10 20:04:37 +0100 | [diff] [blame] | 3650 | if (opcode == BPF_ARSH && BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3651 | verbose(env, "BPF_ARSH not supported for 32 bit ALU\n"); |
| 3652 | return -EINVAL; |
| 3653 | } |
| 3654 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3655 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 3656 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 3657 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 3658 | |
| 3659 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3660 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3661 | return -EINVAL; |
| 3662 | } |
| 3663 | } |
| 3664 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3665 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3666 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3667 | if (err) |
| 3668 | return err; |
| 3669 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3670 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | return 0; |
| 3674 | } |
| 3675 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3676 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3677 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 3678 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3679 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3680 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3681 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3682 | struct bpf_reg_state *regs = state->regs, *reg; |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3683 | u16 new_range; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3684 | int i, j; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3685 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3686 | if (dst_reg->off < 0 || |
| 3687 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3688 | /* This doesn't give us any range */ |
| 3689 | return; |
| 3690 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3691 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 3692 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3693 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 3694 | * than pkt_end, but that's because it's also less than pkt. |
| 3695 | */ |
| 3696 | return; |
| 3697 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3698 | new_range = dst_reg->off; |
| 3699 | if (range_right_open) |
| 3700 | new_range--; |
| 3701 | |
| 3702 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3703 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3704 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3705 | * |
| 3706 | * r2 = r3; |
| 3707 | * r2 += 8; |
| 3708 | * if (r2 > pkt_end) goto <handle exception> |
| 3709 | * <access okay> |
| 3710 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3711 | * r2 = r3; |
| 3712 | * r2 += 8; |
| 3713 | * if (r2 < pkt_end) goto <access okay> |
| 3714 | * <handle exception> |
| 3715 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3716 | * Where: |
| 3717 | * r2 == dst_reg, pkt_end == src_reg |
| 3718 | * r2=pkt(id=n,off=8,r=0) |
| 3719 | * r3=pkt(id=n,off=0,r=0) |
| 3720 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3721 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3722 | * |
| 3723 | * r2 = r3; |
| 3724 | * r2 += 8; |
| 3725 | * if (pkt_end >= r2) goto <access okay> |
| 3726 | * <handle exception> |
| 3727 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3728 | * r2 = r3; |
| 3729 | * r2 += 8; |
| 3730 | * if (pkt_end <= r2) goto <handle exception> |
| 3731 | * <access okay> |
| 3732 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3733 | * Where: |
| 3734 | * pkt_end == dst_reg, r2 == src_reg |
| 3735 | * r2=pkt(id=n,off=8,r=0) |
| 3736 | * r3=pkt(id=n,off=0,r=0) |
| 3737 | * |
| 3738 | * 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] | 3739 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 3740 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 3741 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3742 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3743 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3744 | /* If our ids match, then we must have the same max_value. And we |
| 3745 | * don't care about the other reg's fixed offset, since if it's too big |
| 3746 | * the range won't allow anything. |
| 3747 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 3748 | */ |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3749 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3750 | if (regs[i].type == type && regs[i].id == dst_reg->id) |
Alexei Starovoitov | b197768 | 2017-03-24 15:57:33 -0700 | [diff] [blame] | 3751 | /* keep the maximum range already checked */ |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3752 | regs[i].range = max(regs[i].range, new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3753 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3754 | for (j = 0; j <= vstate->curframe; j++) { |
| 3755 | state = vstate->frame[j]; |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 3756 | bpf_for_each_spilled_reg(i, state, reg) { |
| 3757 | if (!reg) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3758 | continue; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3759 | if (reg->type == type && reg->id == dst_reg->id) |
| 3760 | reg->range = max(reg->range, new_range); |
| 3761 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3762 | } |
| 3763 | } |
| 3764 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3765 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 3766 | * variable register that we are working on, and src_reg is a constant or we're |
| 3767 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3768 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3769 | */ |
| 3770 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 3771 | struct bpf_reg_state *false_reg, u64 val, |
| 3772 | u8 opcode) |
| 3773 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3774 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 3775 | * variable offset from the compare (unless src_reg were a pointer into |
| 3776 | * the same object, but we don't bother with that. |
| 3777 | * Since false_reg and true_reg have the same type by construction, we |
| 3778 | * only need to check one of them for pointerness. |
| 3779 | */ |
| 3780 | if (__is_pointer_value(false, false_reg)) |
| 3781 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3782 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3783 | switch (opcode) { |
| 3784 | case BPF_JEQ: |
| 3785 | /* If this is false then we know nothing Jon Snow, but if it is |
| 3786 | * true then we know for sure. |
| 3787 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3788 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3789 | break; |
| 3790 | case BPF_JNE: |
| 3791 | /* If this is true we know nothing Jon Snow, but if it is false |
| 3792 | * we know the value for sure; |
| 3793 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3794 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3795 | break; |
| 3796 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3797 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 3798 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 3799 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3800 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3801 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 3802 | 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] | 3803 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3804 | case BPF_JLT: |
| 3805 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 3806 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 3807 | break; |
| 3808 | case BPF_JSLT: |
| 3809 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
| 3810 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 3811 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3812 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3813 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 3814 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 3815 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3816 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3817 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 3818 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3819 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3820 | case BPF_JLE: |
| 3821 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 3822 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 3823 | break; |
| 3824 | case BPF_JSLE: |
| 3825 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); |
| 3826 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 3827 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3828 | default: |
| 3829 | break; |
| 3830 | } |
| 3831 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3832 | __reg_deduce_bounds(false_reg); |
| 3833 | __reg_deduce_bounds(true_reg); |
| 3834 | /* We might have learned some bits from the bounds. */ |
| 3835 | __reg_bound_offset(false_reg); |
| 3836 | __reg_bound_offset(true_reg); |
| 3837 | /* Intersecting with the old var_off might have improved our bounds |
| 3838 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3839 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3840 | */ |
| 3841 | __update_reg_bounds(false_reg); |
| 3842 | __update_reg_bounds(true_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3843 | } |
| 3844 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3845 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 3846 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3847 | */ |
| 3848 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 3849 | struct bpf_reg_state *false_reg, u64 val, |
| 3850 | u8 opcode) |
| 3851 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3852 | if (__is_pointer_value(false, false_reg)) |
| 3853 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3854 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3855 | switch (opcode) { |
| 3856 | case BPF_JEQ: |
| 3857 | /* If this is false then we know nothing Jon Snow, but if it is |
| 3858 | * true then we know for sure. |
| 3859 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3860 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3861 | break; |
| 3862 | case BPF_JNE: |
| 3863 | /* If this is true we know nothing Jon Snow, but if it is false |
| 3864 | * we know the value for sure; |
| 3865 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3866 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3867 | break; |
| 3868 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3869 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 3870 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 3871 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3872 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3873 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 3874 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3875 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3876 | case BPF_JLT: |
| 3877 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 3878 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 3879 | break; |
| 3880 | case BPF_JSLT: |
| 3881 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); |
| 3882 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 3883 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3884 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3885 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 3886 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 3887 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3888 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3889 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 3890 | 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] | 3891 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3892 | case BPF_JLE: |
| 3893 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 3894 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 3895 | break; |
| 3896 | case BPF_JSLE: |
| 3897 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
| 3898 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 3899 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3900 | default: |
| 3901 | break; |
| 3902 | } |
| 3903 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3904 | __reg_deduce_bounds(false_reg); |
| 3905 | __reg_deduce_bounds(true_reg); |
| 3906 | /* We might have learned some bits from the bounds. */ |
| 3907 | __reg_bound_offset(false_reg); |
| 3908 | __reg_bound_offset(true_reg); |
| 3909 | /* Intersecting with the old var_off might have improved our bounds |
| 3910 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3911 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3912 | */ |
| 3913 | __update_reg_bounds(false_reg); |
| 3914 | __update_reg_bounds(true_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3915 | } |
| 3916 | |
| 3917 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 3918 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 3919 | struct bpf_reg_state *dst_reg) |
| 3920 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3921 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 3922 | dst_reg->umin_value); |
| 3923 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 3924 | dst_reg->umax_value); |
| 3925 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 3926 | dst_reg->smin_value); |
| 3927 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 3928 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3929 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 3930 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3931 | /* We might have learned new bounds from the var_off. */ |
| 3932 | __update_reg_bounds(src_reg); |
| 3933 | __update_reg_bounds(dst_reg); |
| 3934 | /* We might have learned something about the sign bit. */ |
| 3935 | __reg_deduce_bounds(src_reg); |
| 3936 | __reg_deduce_bounds(dst_reg); |
| 3937 | /* We might have learned some bits from the bounds. */ |
| 3938 | __reg_bound_offset(src_reg); |
| 3939 | __reg_bound_offset(dst_reg); |
| 3940 | /* Intersecting with the old var_off might have improved our bounds |
| 3941 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3942 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3943 | */ |
| 3944 | __update_reg_bounds(src_reg); |
| 3945 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 3949 | struct bpf_reg_state *true_dst, |
| 3950 | struct bpf_reg_state *false_src, |
| 3951 | struct bpf_reg_state *false_dst, |
| 3952 | u8 opcode) |
| 3953 | { |
| 3954 | switch (opcode) { |
| 3955 | case BPF_JEQ: |
| 3956 | __reg_combine_min_max(true_src, true_dst); |
| 3957 | break; |
| 3958 | case BPF_JNE: |
| 3959 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3960 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3961 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3962 | } |
| 3963 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3964 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 3965 | struct bpf_reg_state *reg, u32 id, |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 3966 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3967 | { |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 3968 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3969 | /* Old offset (both fixed and variable parts) should |
| 3970 | * have been known-zero, because we don't allow pointer |
| 3971 | * arithmetic on pointers that might be NULL. |
| 3972 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3973 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 3974 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3975 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3976 | __mark_reg_known_zero(reg); |
| 3977 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3978 | } |
| 3979 | if (is_null) { |
| 3980 | reg->type = SCALAR_VALUE; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 3981 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
| 3982 | if (reg->map_ptr->inner_map_meta) { |
| 3983 | reg->type = CONST_PTR_TO_MAP; |
| 3984 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
| 3985 | } else { |
| 3986 | reg->type = PTR_TO_MAP_VALUE; |
| 3987 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3988 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
| 3989 | reg->type = PTR_TO_SOCKET; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3990 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3991 | if (is_null || !reg_is_refcounted(reg)) { |
| 3992 | /* We don't need id from this point onwards anymore, |
| 3993 | * thus we should better reset it, so that state |
| 3994 | * pruning has chances to take effect. |
| 3995 | */ |
| 3996 | reg->id = 0; |
| 3997 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3998 | } |
| 3999 | } |
| 4000 | |
| 4001 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 4002 | * be folded together at some point. |
| 4003 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4004 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 4005 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4006 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4007 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 4008 | struct bpf_reg_state *reg, *regs = state->regs; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 4009 | u32 id = regs[regno].id; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4010 | int i, j; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4011 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4012 | if (reg_is_refcounted_or_null(®s[regno]) && is_null) |
| 4013 | __release_reference_state(state, id); |
| 4014 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4015 | for (i = 0; i < MAX_BPF_REG; i++) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4016 | mark_ptr_or_null_reg(state, ®s[i], id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4017 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4018 | for (j = 0; j <= vstate->curframe; j++) { |
| 4019 | state = vstate->frame[j]; |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 4020 | bpf_for_each_spilled_reg(i, state, reg) { |
| 4021 | if (!reg) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4022 | continue; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4023 | mark_ptr_or_null_reg(state, reg, id, is_null); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4024 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4025 | } |
| 4026 | } |
| 4027 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 4028 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 4029 | struct bpf_reg_state *dst_reg, |
| 4030 | struct bpf_reg_state *src_reg, |
| 4031 | struct bpf_verifier_state *this_branch, |
| 4032 | struct bpf_verifier_state *other_branch) |
| 4033 | { |
| 4034 | if (BPF_SRC(insn->code) != BPF_X) |
| 4035 | return false; |
| 4036 | |
| 4037 | switch (BPF_OP(insn->code)) { |
| 4038 | case BPF_JGT: |
| 4039 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4040 | src_reg->type == PTR_TO_PACKET_END) || |
| 4041 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4042 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4043 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 4044 | find_good_pkt_pointers(this_branch, dst_reg, |
| 4045 | dst_reg->type, false); |
| 4046 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4047 | src_reg->type == PTR_TO_PACKET) || |
| 4048 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4049 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4050 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 4051 | find_good_pkt_pointers(other_branch, src_reg, |
| 4052 | src_reg->type, true); |
| 4053 | } else { |
| 4054 | return false; |
| 4055 | } |
| 4056 | break; |
| 4057 | case BPF_JLT: |
| 4058 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4059 | src_reg->type == PTR_TO_PACKET_END) || |
| 4060 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4061 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4062 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 4063 | find_good_pkt_pointers(other_branch, dst_reg, |
| 4064 | dst_reg->type, true); |
| 4065 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4066 | src_reg->type == PTR_TO_PACKET) || |
| 4067 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4068 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4069 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 4070 | find_good_pkt_pointers(this_branch, src_reg, |
| 4071 | src_reg->type, false); |
| 4072 | } else { |
| 4073 | return false; |
| 4074 | } |
| 4075 | break; |
| 4076 | case BPF_JGE: |
| 4077 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4078 | src_reg->type == PTR_TO_PACKET_END) || |
| 4079 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4080 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4081 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 4082 | find_good_pkt_pointers(this_branch, dst_reg, |
| 4083 | dst_reg->type, true); |
| 4084 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4085 | src_reg->type == PTR_TO_PACKET) || |
| 4086 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4087 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4088 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 4089 | find_good_pkt_pointers(other_branch, src_reg, |
| 4090 | src_reg->type, false); |
| 4091 | } else { |
| 4092 | return false; |
| 4093 | } |
| 4094 | break; |
| 4095 | case BPF_JLE: |
| 4096 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4097 | src_reg->type == PTR_TO_PACKET_END) || |
| 4098 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4099 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4100 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 4101 | find_good_pkt_pointers(other_branch, dst_reg, |
| 4102 | dst_reg->type, false); |
| 4103 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4104 | src_reg->type == PTR_TO_PACKET) || |
| 4105 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4106 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4107 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 4108 | find_good_pkt_pointers(this_branch, src_reg, |
| 4109 | src_reg->type, true); |
| 4110 | } else { |
| 4111 | return false; |
| 4112 | } |
| 4113 | break; |
| 4114 | default: |
| 4115 | return false; |
| 4116 | } |
| 4117 | |
| 4118 | return true; |
| 4119 | } |
| 4120 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4121 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4122 | struct bpf_insn *insn, int *insn_idx) |
| 4123 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4124 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 4125 | struct bpf_verifier_state *other_branch; |
| 4126 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
| 4127 | struct bpf_reg_state *dst_reg, *other_branch_regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4128 | u8 opcode = BPF_OP(insn->code); |
| 4129 | int err; |
| 4130 | |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4131 | if (opcode > BPF_JSLE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4132 | verbose(env, "invalid BPF_JMP opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4133 | return -EINVAL; |
| 4134 | } |
| 4135 | |
| 4136 | if (BPF_SRC(insn->code) == BPF_X) { |
| 4137 | if (insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4138 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4139 | return -EINVAL; |
| 4140 | } |
| 4141 | |
| 4142 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4143 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4144 | if (err) |
| 4145 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4146 | |
| 4147 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4148 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4149 | insn->src_reg); |
| 4150 | return -EACCES; |
| 4151 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4152 | } else { |
| 4153 | if (insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4154 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4155 | return -EINVAL; |
| 4156 | } |
| 4157 | } |
| 4158 | |
| 4159 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4160 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4161 | if (err) |
| 4162 | return err; |
| 4163 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4164 | dst_reg = ®s[insn->dst_reg]; |
| 4165 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4166 | /* detect if R == 0 where R was initialized to zero earlier */ |
| 4167 | if (BPF_SRC(insn->code) == BPF_K && |
| 4168 | (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4169 | dst_reg->type == SCALAR_VALUE && |
Alexei Starovoitov | 3bf1592 | 2017-11-30 21:31:39 -0800 | [diff] [blame] | 4170 | tnum_is_const(dst_reg->var_off)) { |
| 4171 | if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) || |
| 4172 | (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4173 | /* if (imm == imm) goto pc+off; |
| 4174 | * only follow the goto, ignore fall-through |
| 4175 | */ |
| 4176 | *insn_idx += insn->off; |
| 4177 | return 0; |
| 4178 | } else { |
| 4179 | /* if (imm != imm) goto pc+off; |
| 4180 | * only follow fall-through branch, since |
| 4181 | * that's where the program will go |
| 4182 | */ |
| 4183 | return 0; |
| 4184 | } |
| 4185 | } |
| 4186 | |
| 4187 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); |
| 4188 | if (!other_branch) |
| 4189 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4190 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4191 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4192 | /* detect if we are comparing against a constant value so we can adjust |
| 4193 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4194 | * this is only legit if both are scalars (or pointers to the same |
| 4195 | * object, I suppose, but we don't support that right now), because |
| 4196 | * otherwise the different base pointers mean the offsets aren't |
| 4197 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4198 | */ |
| 4199 | if (BPF_SRC(insn->code) == BPF_X) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4200 | if (dst_reg->type == SCALAR_VALUE && |
| 4201 | regs[insn->src_reg].type == SCALAR_VALUE) { |
| 4202 | if (tnum_is_const(regs[insn->src_reg].var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4203 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4204 | dst_reg, regs[insn->src_reg].var_off.value, |
| 4205 | opcode); |
| 4206 | else if (tnum_is_const(dst_reg->var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4207 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4208 | ®s[insn->src_reg], |
| 4209 | dst_reg->var_off.value, opcode); |
| 4210 | else if (opcode == BPF_JEQ || opcode == BPF_JNE) |
| 4211 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4212 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 4213 | &other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4214 | ®s[insn->src_reg], |
| 4215 | ®s[insn->dst_reg], opcode); |
| 4216 | } |
| 4217 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4218 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4219 | dst_reg, insn->imm, opcode); |
| 4220 | } |
| 4221 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4222 | /* 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] | 4223 | if (BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4224 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4225 | reg_type_may_be_null(dst_reg->type)) { |
| 4226 | /* Mark all identical registers in each branch as either |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4227 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 4228 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4229 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 4230 | opcode == BPF_JNE); |
| 4231 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 4232 | opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 4233 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 4234 | this_branch, other_branch) && |
| 4235 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4236 | verbose(env, "R%d pointer comparison prohibited\n", |
| 4237 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4238 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4239 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4240 | if (env->log.level) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4241 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4242 | return 0; |
| 4243 | } |
| 4244 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4245 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
| 4246 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) |
| 4247 | { |
| 4248 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; |
| 4249 | |
| 4250 | return (struct bpf_map *) (unsigned long) imm64; |
| 4251 | } |
| 4252 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4253 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4254 | 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] | 4255 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4256 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4257 | int err; |
| 4258 | |
| 4259 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4260 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4261 | return -EINVAL; |
| 4262 | } |
| 4263 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4264 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4265 | return -EINVAL; |
| 4266 | } |
| 4267 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4268 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4269 | if (err) |
| 4270 | return err; |
| 4271 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 4272 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 4273 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 4274 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4275 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4276 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4277 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 4278 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4279 | |
| 4280 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ |
| 4281 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); |
| 4282 | |
| 4283 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 4284 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); |
| 4285 | return 0; |
| 4286 | } |
| 4287 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 4288 | static bool may_access_skb(enum bpf_prog_type type) |
| 4289 | { |
| 4290 | switch (type) { |
| 4291 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 4292 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 4293 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 4294 | return true; |
| 4295 | default: |
| 4296 | return false; |
| 4297 | } |
| 4298 | } |
| 4299 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4300 | /* verify safety of LD_ABS|LD_IND instructions: |
| 4301 | * - they can only appear in the programs where ctx == skb |
| 4302 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 4303 | * preserve R6-R9, and store return value into R0 |
| 4304 | * |
| 4305 | * Implicit input: |
| 4306 | * ctx == skb == R6 == CTX |
| 4307 | * |
| 4308 | * Explicit input: |
| 4309 | * SRC == any register |
| 4310 | * IMM == 32-bit immediate |
| 4311 | * |
| 4312 | * Output: |
| 4313 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 4314 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4315 | 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] | 4316 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4317 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4318 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4319 | int i, err; |
| 4320 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 4321 | if (!may_access_skb(env->prog->type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4322 | 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] | 4323 | return -EINVAL; |
| 4324 | } |
| 4325 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 4326 | if (!env->ops->gen_ld_abs) { |
| 4327 | verbose(env, "bpf verifier is misconfigured\n"); |
| 4328 | return -EINVAL; |
| 4329 | } |
| 4330 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 4331 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4332 | /* when program has LD_ABS insn JITs and interpreter assume |
| 4333 | * that r1 == ctx == skb which is not the case for callees |
| 4334 | * that can have arbitrary arguments. It's problematic |
| 4335 | * for main prog as well since JITs would need to analyze |
| 4336 | * all functions in order to make proper register save/restore |
| 4337 | * decisions in the main prog. Hence disallow LD_ABS with calls |
| 4338 | */ |
| 4339 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); |
| 4340 | return -EINVAL; |
| 4341 | } |
| 4342 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4343 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 4344 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4345 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4346 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4347 | return -EINVAL; |
| 4348 | } |
| 4349 | |
| 4350 | /* check whether implicit source operand (register R6) is readable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4351 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4352 | if (err) |
| 4353 | return err; |
| 4354 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4355 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 4356 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 4357 | * reference leak. |
| 4358 | */ |
| 4359 | err = check_reference_leak(env); |
| 4360 | if (err) { |
| 4361 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 4362 | return err; |
| 4363 | } |
| 4364 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4365 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4366 | verbose(env, |
| 4367 | "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] | 4368 | return -EINVAL; |
| 4369 | } |
| 4370 | |
| 4371 | if (mode == BPF_IND) { |
| 4372 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4373 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4374 | if (err) |
| 4375 | return err; |
| 4376 | } |
| 4377 | |
| 4378 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4379 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4380 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4381 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 4382 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4383 | |
| 4384 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4385 | * the value fetched from the packet. |
| 4386 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4387 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4388 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4389 | return 0; |
| 4390 | } |
| 4391 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4392 | static int check_return_code(struct bpf_verifier_env *env) |
| 4393 | { |
| 4394 | struct bpf_reg_state *reg; |
| 4395 | struct tnum range = tnum_range(0, 1); |
| 4396 | |
| 4397 | switch (env->prog->type) { |
| 4398 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 4399 | case BPF_PROG_TYPE_CGROUP_SOCK: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 4400 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4401 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 4402 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4403 | break; |
| 4404 | default: |
| 4405 | return 0; |
| 4406 | } |
| 4407 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4408 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4409 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4410 | 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] | 4411 | reg_type_str[reg->type]); |
| 4412 | return -EINVAL; |
| 4413 | } |
| 4414 | |
| 4415 | if (!tnum_in(range, reg->var_off)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4416 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4417 | if (!tnum_is_unknown(reg->var_off)) { |
| 4418 | char tn_buf[48]; |
| 4419 | |
| 4420 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4421 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4422 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4423 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4424 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4425 | verbose(env, " should have been 0 or 1\n"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4426 | return -EINVAL; |
| 4427 | } |
| 4428 | return 0; |
| 4429 | } |
| 4430 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4431 | /* non-recursive DFS pseudo code |
| 4432 | * 1 procedure DFS-iterative(G,v): |
| 4433 | * 2 label v as discovered |
| 4434 | * 3 let S be a stack |
| 4435 | * 4 S.push(v) |
| 4436 | * 5 while S is not empty |
| 4437 | * 6 t <- S.pop() |
| 4438 | * 7 if t is what we're looking for: |
| 4439 | * 8 return t |
| 4440 | * 9 for all edges e in G.adjacentEdges(t) do |
| 4441 | * 10 if edge e is already labelled |
| 4442 | * 11 continue with the next edge |
| 4443 | * 12 w <- G.adjacentVertex(t,e) |
| 4444 | * 13 if vertex w is not discovered and not explored |
| 4445 | * 14 label e as tree-edge |
| 4446 | * 15 label w as discovered |
| 4447 | * 16 S.push(w) |
| 4448 | * 17 continue at 5 |
| 4449 | * 18 else if vertex w is discovered |
| 4450 | * 19 label e as back-edge |
| 4451 | * 20 else |
| 4452 | * 21 // vertex w is explored |
| 4453 | * 22 label e as forward- or cross-edge |
| 4454 | * 23 label t as explored |
| 4455 | * 24 S.pop() |
| 4456 | * |
| 4457 | * convention: |
| 4458 | * 0x10 - discovered |
| 4459 | * 0x11 - discovered and fall-through edge labelled |
| 4460 | * 0x12 - discovered and fall-through and branch edges labelled |
| 4461 | * 0x20 - explored |
| 4462 | */ |
| 4463 | |
| 4464 | enum { |
| 4465 | DISCOVERED = 0x10, |
| 4466 | EXPLORED = 0x20, |
| 4467 | FALLTHROUGH = 1, |
| 4468 | BRANCH = 2, |
| 4469 | }; |
| 4470 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4471 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4472 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4473 | static int *insn_stack; /* stack of insns to process */ |
| 4474 | static int cur_stack; /* current stack index */ |
| 4475 | static int *insn_state; |
| 4476 | |
| 4477 | /* t, w, e - match pseudo-code above: |
| 4478 | * t - index of current instruction |
| 4479 | * w - next instruction |
| 4480 | * e - edge |
| 4481 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4482 | 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] | 4483 | { |
| 4484 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 4485 | return 0; |
| 4486 | |
| 4487 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 4488 | return 0; |
| 4489 | |
| 4490 | if (w < 0 || w >= env->prog->len) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4491 | 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] | 4492 | return -EINVAL; |
| 4493 | } |
| 4494 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4495 | if (e == BRANCH) |
| 4496 | /* mark branch target for state pruning */ |
| 4497 | env->explored_states[w] = STATE_LIST_MARK; |
| 4498 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4499 | if (insn_state[w] == 0) { |
| 4500 | /* tree-edge */ |
| 4501 | insn_state[t] = DISCOVERED | e; |
| 4502 | insn_state[w] = DISCOVERED; |
| 4503 | if (cur_stack >= env->prog->len) |
| 4504 | return -E2BIG; |
| 4505 | insn_stack[cur_stack++] = w; |
| 4506 | return 1; |
| 4507 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4508 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4509 | return -EINVAL; |
| 4510 | } else if (insn_state[w] == EXPLORED) { |
| 4511 | /* forward- or cross-edge */ |
| 4512 | insn_state[t] = DISCOVERED | e; |
| 4513 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4514 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4515 | return -EFAULT; |
| 4516 | } |
| 4517 | return 0; |
| 4518 | } |
| 4519 | |
| 4520 | /* non-recursive depth-first-search to detect loops in BPF program |
| 4521 | * loop == back-edge in directed graph |
| 4522 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4523 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4524 | { |
| 4525 | struct bpf_insn *insns = env->prog->insnsi; |
| 4526 | int insn_cnt = env->prog->len; |
| 4527 | int ret = 0; |
| 4528 | int i, t; |
| 4529 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 4530 | ret = check_subprogs(env); |
| 4531 | if (ret < 0) |
| 4532 | return ret; |
| 4533 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4534 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 4535 | if (!insn_state) |
| 4536 | return -ENOMEM; |
| 4537 | |
| 4538 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 4539 | if (!insn_stack) { |
| 4540 | kfree(insn_state); |
| 4541 | return -ENOMEM; |
| 4542 | } |
| 4543 | |
| 4544 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 4545 | insn_stack[0] = 0; /* 0 is the first instruction */ |
| 4546 | cur_stack = 1; |
| 4547 | |
| 4548 | peek_stack: |
| 4549 | if (cur_stack == 0) |
| 4550 | goto check_state; |
| 4551 | t = insn_stack[cur_stack - 1]; |
| 4552 | |
| 4553 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { |
| 4554 | u8 opcode = BPF_OP(insns[t].code); |
| 4555 | |
| 4556 | if (opcode == BPF_EXIT) { |
| 4557 | goto mark_explored; |
| 4558 | } else if (opcode == BPF_CALL) { |
| 4559 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4560 | if (ret == 1) |
| 4561 | goto peek_stack; |
| 4562 | else if (ret < 0) |
| 4563 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 4564 | if (t + 1 < insn_cnt) |
| 4565 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 4566 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
| 4567 | env->explored_states[t] = STATE_LIST_MARK; |
| 4568 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); |
| 4569 | if (ret == 1) |
| 4570 | goto peek_stack; |
| 4571 | else if (ret < 0) |
| 4572 | goto err_free; |
| 4573 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4574 | } else if (opcode == BPF_JA) { |
| 4575 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 4576 | ret = -EINVAL; |
| 4577 | goto err_free; |
| 4578 | } |
| 4579 | /* unconditional jump with single edge */ |
| 4580 | ret = push_insn(t, t + insns[t].off + 1, |
| 4581 | FALLTHROUGH, env); |
| 4582 | if (ret == 1) |
| 4583 | goto peek_stack; |
| 4584 | else if (ret < 0) |
| 4585 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4586 | /* tell verifier to check for equivalent states |
| 4587 | * after every call and jump |
| 4588 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 4589 | if (t + 1 < insn_cnt) |
| 4590 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4591 | } else { |
| 4592 | /* conditional jump with two edges */ |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 4593 | env->explored_states[t] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4594 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4595 | if (ret == 1) |
| 4596 | goto peek_stack; |
| 4597 | else if (ret < 0) |
| 4598 | goto err_free; |
| 4599 | |
| 4600 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 4601 | if (ret == 1) |
| 4602 | goto peek_stack; |
| 4603 | else if (ret < 0) |
| 4604 | goto err_free; |
| 4605 | } |
| 4606 | } else { |
| 4607 | /* all other non-branch instructions with single |
| 4608 | * fall-through edge |
| 4609 | */ |
| 4610 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4611 | if (ret == 1) |
| 4612 | goto peek_stack; |
| 4613 | else if (ret < 0) |
| 4614 | goto err_free; |
| 4615 | } |
| 4616 | |
| 4617 | mark_explored: |
| 4618 | insn_state[t] = EXPLORED; |
| 4619 | if (cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4620 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4621 | ret = -EFAULT; |
| 4622 | goto err_free; |
| 4623 | } |
| 4624 | goto peek_stack; |
| 4625 | |
| 4626 | check_state: |
| 4627 | for (i = 0; i < insn_cnt; i++) { |
| 4628 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4629 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4630 | ret = -EINVAL; |
| 4631 | goto err_free; |
| 4632 | } |
| 4633 | } |
| 4634 | ret = 0; /* cfg looks good */ |
| 4635 | |
| 4636 | err_free: |
| 4637 | kfree(insn_state); |
| 4638 | kfree(insn_stack); |
| 4639 | return ret; |
| 4640 | } |
| 4641 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4642 | /* check %cur's range satisfies %old's */ |
| 4643 | static bool range_within(struct bpf_reg_state *old, |
| 4644 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4645 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4646 | return old->umin_value <= cur->umin_value && |
| 4647 | old->umax_value >= cur->umax_value && |
| 4648 | old->smin_value <= cur->smin_value && |
| 4649 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | /* Maximum number of register states that can exist at once */ |
| 4653 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 4654 | struct idpair { |
| 4655 | u32 old; |
| 4656 | u32 cur; |
| 4657 | }; |
| 4658 | |
| 4659 | /* If in the old state two registers had the same id, then they need to have |
| 4660 | * the same id in the new state as well. But that id could be different from |
| 4661 | * the old state, so we need to track the mapping from old to new ids. |
| 4662 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 4663 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 4664 | * regs with a different old id could still have new id 9, we don't care about |
| 4665 | * that. |
| 4666 | * So we look through our idmap to see if this old id has been seen before. If |
| 4667 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 4668 | */ |
| 4669 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 4670 | { |
| 4671 | unsigned int i; |
| 4672 | |
| 4673 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 4674 | if (!idmap[i].old) { |
| 4675 | /* Reached an empty slot; haven't seen this id before */ |
| 4676 | idmap[i].old = old_id; |
| 4677 | idmap[i].cur = cur_id; |
| 4678 | return true; |
| 4679 | } |
| 4680 | if (idmap[i].old == old_id) |
| 4681 | return idmap[i].cur == cur_id; |
| 4682 | } |
| 4683 | /* We ran out of idmap slots, which should be impossible */ |
| 4684 | WARN_ON_ONCE(1); |
| 4685 | return false; |
| 4686 | } |
| 4687 | |
| 4688 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4689 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 4690 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4691 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4692 | bool equal; |
| 4693 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4694 | if (!(rold->live & REG_LIVE_READ)) |
| 4695 | /* explored state didn't use this */ |
| 4696 | return true; |
| 4697 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 4698 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4699 | |
| 4700 | if (rold->type == PTR_TO_STACK) |
| 4701 | /* two stack pointers are equal only if they're pointing to |
| 4702 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 4703 | */ |
| 4704 | return equal && rold->frameno == rcur->frameno; |
| 4705 | |
| 4706 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4707 | return true; |
| 4708 | |
| 4709 | if (rold->type == NOT_INIT) |
| 4710 | /* explored state can't have used this */ |
| 4711 | return true; |
| 4712 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4713 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4714 | switch (rold->type) { |
| 4715 | case SCALAR_VALUE: |
| 4716 | if (rcur->type == SCALAR_VALUE) { |
| 4717 | /* new val must satisfy old val knowledge */ |
| 4718 | return range_within(rold, rcur) && |
| 4719 | tnum_in(rold->var_off, rcur->var_off); |
| 4720 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 4721 | /* We're trying to use a pointer in place of a scalar. |
| 4722 | * Even if the scalar was unbounded, this could lead to |
| 4723 | * pointer leaks because scalars are allowed to leak |
| 4724 | * while pointers are not. We could make this safe in |
| 4725 | * special cases if root is calling us, but it's |
| 4726 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4727 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 4728 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4729 | } |
| 4730 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4731 | /* If the new min/max/var_off satisfy the old ones and |
| 4732 | * everything else matches, we are OK. |
| 4733 | * We don't care about the 'id' value, because nothing |
| 4734 | * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL) |
| 4735 | */ |
| 4736 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 4737 | range_within(rold, rcur) && |
| 4738 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4739 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 4740 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 4741 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 4742 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 4743 | * checked, doing so could have affected others with the same |
| 4744 | * id, and we can't check for that because we lost the id when |
| 4745 | * we converted to a PTR_TO_MAP_VALUE. |
| 4746 | */ |
| 4747 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 4748 | return false; |
| 4749 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 4750 | return false; |
| 4751 | /* Check our ids match any regs they're supposed to */ |
| 4752 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4753 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4754 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4755 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4756 | return false; |
| 4757 | /* We must have at least as much range as the old ptr |
| 4758 | * did, so that any accesses which were safe before are |
| 4759 | * still safe. This is true even if old range < old off, |
| 4760 | * since someone could have accessed through (ptr - k), or |
| 4761 | * even done ptr -= k in a register, to get a safe access. |
| 4762 | */ |
| 4763 | if (rold->range > rcur->range) |
| 4764 | return false; |
| 4765 | /* If the offsets don't match, we can't trust our alignment; |
| 4766 | * nor can we be sure that we won't fall out of range. |
| 4767 | */ |
| 4768 | if (rold->off != rcur->off) |
| 4769 | return false; |
| 4770 | /* id relations must be preserved */ |
| 4771 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 4772 | return false; |
| 4773 | /* new val must satisfy old val knowledge */ |
| 4774 | return range_within(rold, rcur) && |
| 4775 | tnum_in(rold->var_off, rcur->var_off); |
| 4776 | case PTR_TO_CTX: |
| 4777 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4778 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 4779 | case PTR_TO_FLOW_KEYS: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 4780 | case PTR_TO_SOCKET: |
| 4781 | case PTR_TO_SOCKET_OR_NULL: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4782 | /* Only valid matches are exact, which memcmp() above |
| 4783 | * would have accepted |
| 4784 | */ |
| 4785 | default: |
| 4786 | /* Don't know what's going on, just say it's not safe */ |
| 4787 | return false; |
| 4788 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4789 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4790 | /* Shouldn't get here; if we do, say it's not safe */ |
| 4791 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4792 | return false; |
| 4793 | } |
| 4794 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4795 | static bool stacksafe(struct bpf_func_state *old, |
| 4796 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4797 | struct idpair *idmap) |
| 4798 | { |
| 4799 | int i, spi; |
| 4800 | |
| 4801 | /* if explored stack has more populated slots than current stack |
| 4802 | * such stacks are not equivalent |
| 4803 | */ |
| 4804 | if (old->allocated_stack > cur->allocated_stack) |
| 4805 | return false; |
| 4806 | |
| 4807 | /* walk slots of the explored stack and ignore any additional |
| 4808 | * slots in the current stack, since explored(safe) state |
| 4809 | * didn't use them |
| 4810 | */ |
| 4811 | for (i = 0; i < old->allocated_stack; i++) { |
| 4812 | spi = i / BPF_REG_SIZE; |
| 4813 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4814 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) |
| 4815 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 4816 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4817 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4818 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 4819 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4820 | /* if old state was safe with misc data in the stack |
| 4821 | * it will be safe with zero-initialized stack. |
| 4822 | * The opposite is not true |
| 4823 | */ |
| 4824 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 4825 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 4826 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4827 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 4828 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 4829 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 4830 | * this stack slot, but current has has STACK_MISC -> |
| 4831 | * this verifier states are not equivalent, |
| 4832 | * return false to continue verification of this path |
| 4833 | */ |
| 4834 | return false; |
| 4835 | if (i % BPF_REG_SIZE) |
| 4836 | continue; |
| 4837 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 4838 | continue; |
| 4839 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 4840 | &cur->stack[spi].spilled_ptr, |
| 4841 | idmap)) |
| 4842 | /* when explored and current stack slot are both storing |
| 4843 | * spilled registers, check that stored pointers types |
| 4844 | * are the same as well. |
| 4845 | * Ex: explored safe path could have stored |
| 4846 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 4847 | * but current path has stored: |
| 4848 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 4849 | * such verifier states are not equivalent. |
| 4850 | * return false to continue verification of this path |
| 4851 | */ |
| 4852 | return false; |
| 4853 | } |
| 4854 | return true; |
| 4855 | } |
| 4856 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4857 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 4858 | { |
| 4859 | if (old->acquired_refs != cur->acquired_refs) |
| 4860 | return false; |
| 4861 | return !memcmp(old->refs, cur->refs, |
| 4862 | sizeof(*old->refs) * old->acquired_refs); |
| 4863 | } |
| 4864 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4865 | /* compare two verifier states |
| 4866 | * |
| 4867 | * all states stored in state_list are known to be valid, since |
| 4868 | * verifier reached 'bpf_exit' instruction through them |
| 4869 | * |
| 4870 | * this function is called when verifier exploring different branches of |
| 4871 | * execution popped from the state stack. If it sees an old state that has |
| 4872 | * more strict register state and more strict stack state then this execution |
| 4873 | * branch doesn't need to be explored further, since verifier already |
| 4874 | * concluded that more strict state leads to valid finish. |
| 4875 | * |
| 4876 | * Therefore two states are equivalent if register state is more conservative |
| 4877 | * and explored stack state is more conservative than the current one. |
| 4878 | * Example: |
| 4879 | * explored current |
| 4880 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 4881 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 4882 | * |
| 4883 | * In other words if current stack state (one being explored) has more |
| 4884 | * valid slots than old one that already passed validation, it means |
| 4885 | * the verifier can stop exploring and conclude that current state is valid too |
| 4886 | * |
| 4887 | * Similarly with registers. If explored state has register type as invalid |
| 4888 | * whereas register type in current state is meaningful, it means that |
| 4889 | * the current state will reach 'bpf_exit' instruction safely |
| 4890 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4891 | static bool func_states_equal(struct bpf_func_state *old, |
| 4892 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4893 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4894 | struct idpair *idmap; |
| 4895 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4896 | int i; |
| 4897 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4898 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 4899 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 4900 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4901 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4902 | |
| 4903 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4904 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4905 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4906 | } |
| 4907 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4908 | if (!stacksafe(old, cur, idmap)) |
| 4909 | goto out_free; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4910 | |
| 4911 | if (!refsafe(old, cur)) |
| 4912 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4913 | ret = true; |
| 4914 | out_free: |
| 4915 | kfree(idmap); |
| 4916 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4917 | } |
| 4918 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4919 | static bool states_equal(struct bpf_verifier_env *env, |
| 4920 | struct bpf_verifier_state *old, |
| 4921 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4922 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4923 | int i; |
| 4924 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4925 | if (old->curframe != cur->curframe) |
| 4926 | return false; |
| 4927 | |
| 4928 | /* for states to be equal callsites have to be the same |
| 4929 | * and all frame states need to be equivalent |
| 4930 | */ |
| 4931 | for (i = 0; i <= old->curframe; i++) { |
| 4932 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 4933 | return false; |
| 4934 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 4935 | return false; |
| 4936 | } |
| 4937 | return true; |
| 4938 | } |
| 4939 | |
| 4940 | /* A write screens off any subsequent reads; but write marks come from the |
| 4941 | * straight-line code between a state and its parent. When we arrive at an |
| 4942 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 4943 | * code, so read marks in the state must propagate to the parent regardless |
| 4944 | * 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] | 4945 | * in mark_reg_read() is for. |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4946 | */ |
| 4947 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 4948 | const struct bpf_verifier_state *vstate, |
| 4949 | struct bpf_verifier_state *vparent) |
| 4950 | { |
| 4951 | int i, frame, err = 0; |
| 4952 | struct bpf_func_state *state, *parent; |
| 4953 | |
| 4954 | if (vparent->curframe != vstate->curframe) { |
| 4955 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 4956 | vparent->curframe, vstate->curframe); |
| 4957 | return -EFAULT; |
| 4958 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4959 | /* Propagate read liveness of registers... */ |
| 4960 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
| 4961 | /* We don't need to worry about FP liveness because it's read-only */ |
| 4962 | for (i = 0; i < BPF_REG_FP; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4963 | if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4964 | continue; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4965 | if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 4966 | err = mark_reg_read(env, &vstate->frame[vstate->curframe]->regs[i], |
| 4967 | &vparent->frame[vstate->curframe]->regs[i]); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4968 | if (err) |
| 4969 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4970 | } |
| 4971 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4972 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4973 | /* ... and stack slots */ |
| 4974 | for (frame = 0; frame <= vstate->curframe; frame++) { |
| 4975 | state = vstate->frame[frame]; |
| 4976 | parent = vparent->frame[frame]; |
| 4977 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 4978 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4979 | if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ) |
| 4980 | continue; |
| 4981 | if (state->stack[i].spilled_ptr.live & REG_LIVE_READ) |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 4982 | mark_reg_read(env, &state->stack[i].spilled_ptr, |
| 4983 | &parent->stack[i].spilled_ptr); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4984 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4985 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4986 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4987 | } |
| 4988 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4989 | 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] | 4990 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4991 | struct bpf_verifier_state_list *new_sl; |
| 4992 | struct bpf_verifier_state_list *sl; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 4993 | struct bpf_verifier_state *cur = env->cur_state, *new; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4994 | int i, j, err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4995 | |
| 4996 | sl = env->explored_states[insn_idx]; |
| 4997 | if (!sl) |
| 4998 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 4999 | * be doing state search here |
| 5000 | */ |
| 5001 | return 0; |
| 5002 | |
| 5003 | while (sl != STATE_LIST_MARK) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5004 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5005 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5006 | * prune the search. |
| 5007 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 5008 | * If we have any write marks in env->cur_state, they |
| 5009 | * will prevent corresponding reads in the continuation |
| 5010 | * from reaching our parent (an explored_state). Our |
| 5011 | * own state will get the read marks recorded, but |
| 5012 | * they'll be immediately forgotten as we're pruning |
| 5013 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5014 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5015 | err = propagate_liveness(env, &sl->state, cur); |
| 5016 | if (err) |
| 5017 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5018 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5019 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5020 | sl = sl->next; |
| 5021 | } |
| 5022 | |
| 5023 | /* there were no equivalent states, remember current one. |
| 5024 | * technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5025 | * but it will either reach outer most bpf_exit (which means it's safe) |
| 5026 | * or it will be rejected. Since there are no loops, we won't be |
| 5027 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
| 5028 | * again on the way to bpf_exit |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5029 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5030 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5031 | if (!new_sl) |
| 5032 | return -ENOMEM; |
| 5033 | |
| 5034 | /* add new state to the head of linked list */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5035 | new = &new_sl->state; |
| 5036 | err = copy_verifier_state(new, cur); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 5037 | if (err) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5038 | free_verifier_state(new, false); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 5039 | kfree(new_sl); |
| 5040 | return err; |
| 5041 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5042 | new_sl->next = env->explored_states[insn_idx]; |
| 5043 | env->explored_states[insn_idx] = new_sl; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5044 | /* connect new state to parentage chain */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5045 | for (i = 0; i < BPF_REG_FP; i++) |
| 5046 | cur_regs(env)[i].parent = &new->frame[new->curframe]->regs[i]; |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 5047 | /* clear write marks in current state: the writes we did are not writes |
| 5048 | * our child did, so they don't screen off its reads from us. |
| 5049 | * (There are no read marks in current state, because reads always mark |
| 5050 | * their parent and current state never has children yet. Only |
| 5051 | * explored_states can get read marks.) |
| 5052 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5053 | for (i = 0; i < BPF_REG_FP; i++) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5054 | cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE; |
| 5055 | |
| 5056 | /* all stack frames are accessible from callee, clear them all */ |
| 5057 | for (j = 0; j <= cur->curframe; j++) { |
| 5058 | struct bpf_func_state *frame = cur->frame[j]; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5059 | struct bpf_func_state *newframe = new->frame[j]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5060 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5061 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 5062 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5063 | frame->stack[i].spilled_ptr.parent = |
| 5064 | &newframe->stack[i].spilled_ptr; |
| 5065 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5066 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5067 | return 0; |
| 5068 | } |
| 5069 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5070 | /* Return true if it's OK to have the same insn return a different type. */ |
| 5071 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 5072 | { |
| 5073 | switch (type) { |
| 5074 | case PTR_TO_CTX: |
| 5075 | case PTR_TO_SOCKET: |
| 5076 | case PTR_TO_SOCKET_OR_NULL: |
| 5077 | return false; |
| 5078 | default: |
| 5079 | return true; |
| 5080 | } |
| 5081 | } |
| 5082 | |
| 5083 | /* If an instruction was previously used with particular pointer types, then we |
| 5084 | * need to be careful to avoid cases such as the below, where it may be ok |
| 5085 | * for one branch accessing the pointer, but not ok for the other branch: |
| 5086 | * |
| 5087 | * R1 = sock_ptr |
| 5088 | * goto X; |
| 5089 | * ... |
| 5090 | * R1 = some_other_valid_ptr; |
| 5091 | * goto X; |
| 5092 | * ... |
| 5093 | * R2 = *(u32 *)(R1 + 0); |
| 5094 | */ |
| 5095 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 5096 | { |
| 5097 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 5098 | !reg_type_mismatch_ok(prev)); |
| 5099 | } |
| 5100 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5101 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5102 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5103 | struct bpf_verifier_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5104 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5105 | struct bpf_reg_state *regs; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5106 | int insn_cnt = env->prog->len, i; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5107 | int insn_idx, prev_insn_idx = 0; |
| 5108 | int insn_processed = 0; |
| 5109 | bool do_print_state = false; |
| 5110 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5111 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 5112 | if (!state) |
| 5113 | return -ENOMEM; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5114 | state->curframe = 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5115 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 5116 | if (!state->frame[0]) { |
| 5117 | kfree(state); |
| 5118 | return -ENOMEM; |
| 5119 | } |
| 5120 | env->cur_state = state; |
| 5121 | init_func_state(env, state->frame[0], |
| 5122 | BPF_MAIN_FUNC /* callsite */, |
| 5123 | 0 /* frameno */, |
| 5124 | 0 /* subprogno, zero == main subprog */); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5125 | insn_idx = 0; |
| 5126 | for (;;) { |
| 5127 | struct bpf_insn *insn; |
| 5128 | u8 class; |
| 5129 | int err; |
| 5130 | |
| 5131 | if (insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5132 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5133 | insn_idx, insn_cnt); |
| 5134 | return -EFAULT; |
| 5135 | } |
| 5136 | |
| 5137 | insn = &insns[insn_idx]; |
| 5138 | class = BPF_CLASS(insn->code); |
| 5139 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 5140 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5141 | verbose(env, |
| 5142 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5143 | insn_processed); |
| 5144 | return -E2BIG; |
| 5145 | } |
| 5146 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5147 | err = is_state_visited(env, insn_idx); |
| 5148 | if (err < 0) |
| 5149 | return err; |
| 5150 | if (err == 1) { |
| 5151 | /* found equivalent state, can prune the search */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5152 | if (env->log.level) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5153 | if (do_print_state) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5154 | verbose(env, "\nfrom %d to %d: safe\n", |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5155 | prev_insn_idx, insn_idx); |
| 5156 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5157 | verbose(env, "%d: safe\n", insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5158 | } |
| 5159 | goto process_bpf_exit; |
| 5160 | } |
| 5161 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 5162 | if (need_resched()) |
| 5163 | cond_resched(); |
| 5164 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5165 | if (env->log.level > 1 || (env->log.level && do_print_state)) { |
| 5166 | if (env->log.level > 1) |
| 5167 | verbose(env, "%d:", insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 5168 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5169 | verbose(env, "\nfrom %d to %d:", |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 5170 | prev_insn_idx, insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5171 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5172 | do_print_state = false; |
| 5173 | } |
| 5174 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5175 | if (env->log.level) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5176 | const struct bpf_insn_cbs cbs = { |
| 5177 | .cb_print = verbose, |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 5178 | .private_data = env, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5179 | }; |
| 5180 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5181 | verbose(env, "%d: ", insn_idx); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 5182 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5183 | } |
| 5184 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 5185 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
| 5186 | err = bpf_prog_offload_verify_insn(env, insn_idx, |
| 5187 | prev_insn_idx); |
| 5188 | if (err) |
| 5189 | return err; |
| 5190 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 5191 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5192 | regs = cur_regs(env); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5193 | env->insn_aux_data[insn_idx].seen = true; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5194 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5195 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5196 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5197 | if (err) |
| 5198 | return err; |
| 5199 | |
| 5200 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5201 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5202 | |
| 5203 | /* check for reserved fields is already done */ |
| 5204 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5205 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5206 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5207 | if (err) |
| 5208 | return err; |
| 5209 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5210 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5211 | if (err) |
| 5212 | return err; |
| 5213 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 5214 | src_reg_type = regs[insn->src_reg].type; |
| 5215 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5216 | /* check that memory (src_reg + off) is readable, |
| 5217 | * the state of dst_reg will be updated by this func |
| 5218 | */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5219 | err = check_mem_access(env, insn_idx, insn->src_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5220 | BPF_SIZE(insn->code), BPF_READ, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 5221 | insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5222 | if (err) |
| 5223 | return err; |
| 5224 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5225 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 5226 | |
| 5227 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5228 | /* saw a valid insn |
| 5229 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5230 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5231 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5232 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5233 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5234 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5235 | /* ABuser program is trying to use the same insn |
| 5236 | * dst_reg = *(u32*) (src_reg + off) |
| 5237 | * with different pointer types: |
| 5238 | * src_reg == ctx in one branch and |
| 5239 | * src_reg == stack|map in some other branch. |
| 5240 | * Reject it. |
| 5241 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5242 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5243 | return -EINVAL; |
| 5244 | } |
| 5245 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5246 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5247 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5248 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5249 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5250 | err = check_xadd(env, insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5251 | if (err) |
| 5252 | return err; |
| 5253 | insn_idx++; |
| 5254 | continue; |
| 5255 | } |
| 5256 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5257 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5258 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5259 | if (err) |
| 5260 | return err; |
| 5261 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5262 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5263 | if (err) |
| 5264 | return err; |
| 5265 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5266 | dst_reg_type = regs[insn->dst_reg].type; |
| 5267 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5268 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5269 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5270 | BPF_SIZE(insn->code), BPF_WRITE, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 5271 | insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5272 | if (err) |
| 5273 | return err; |
| 5274 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5275 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 5276 | |
| 5277 | if (*prev_dst_type == NOT_INIT) { |
| 5278 | *prev_dst_type = dst_reg_type; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5279 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5280 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5281 | return -EINVAL; |
| 5282 | } |
| 5283 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5284 | } else if (class == BPF_ST) { |
| 5285 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 5286 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5287 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5288 | return -EINVAL; |
| 5289 | } |
| 5290 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5291 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5292 | if (err) |
| 5293 | return err; |
| 5294 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 5295 | if (is_ctx_reg(env, insn->dst_reg)) { |
Joe Stringer | 9d2be44 | 2018-10-02 13:35:31 -0700 | [diff] [blame] | 5296 | 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] | 5297 | insn->dst_reg, |
| 5298 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 5299 | return -EACCES; |
| 5300 | } |
| 5301 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5302 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5303 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5304 | BPF_SIZE(insn->code), BPF_WRITE, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 5305 | -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5306 | if (err) |
| 5307 | return err; |
| 5308 | |
| 5309 | } else if (class == BPF_JMP) { |
| 5310 | u8 opcode = BPF_OP(insn->code); |
| 5311 | |
| 5312 | if (opcode == BPF_CALL) { |
| 5313 | if (BPF_SRC(insn->code) != BPF_K || |
| 5314 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5315 | (insn->src_reg != BPF_REG_0 && |
| 5316 | insn->src_reg != BPF_PSEUDO_CALL) || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5317 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5318 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5319 | return -EINVAL; |
| 5320 | } |
| 5321 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5322 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 5323 | err = check_func_call(env, insn, &insn_idx); |
| 5324 | else |
| 5325 | err = check_helper_call(env, insn->imm, insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5326 | if (err) |
| 5327 | return err; |
| 5328 | |
| 5329 | } else if (opcode == BPF_JA) { |
| 5330 | if (BPF_SRC(insn->code) != BPF_K || |
| 5331 | insn->imm != 0 || |
| 5332 | insn->src_reg != BPF_REG_0 || |
| 5333 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5334 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5335 | return -EINVAL; |
| 5336 | } |
| 5337 | |
| 5338 | insn_idx += insn->off + 1; |
| 5339 | continue; |
| 5340 | |
| 5341 | } else if (opcode == BPF_EXIT) { |
| 5342 | if (BPF_SRC(insn->code) != BPF_K || |
| 5343 | insn->imm != 0 || |
| 5344 | insn->src_reg != BPF_REG_0 || |
| 5345 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5346 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5347 | return -EINVAL; |
| 5348 | } |
| 5349 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5350 | if (state->curframe) { |
| 5351 | /* exit from nested function */ |
| 5352 | prev_insn_idx = insn_idx; |
| 5353 | err = prepare_func_exit(env, &insn_idx); |
| 5354 | if (err) |
| 5355 | return err; |
| 5356 | do_print_state = true; |
| 5357 | continue; |
| 5358 | } |
| 5359 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5360 | err = check_reference_leak(env); |
| 5361 | if (err) |
| 5362 | return err; |
| 5363 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5364 | /* eBPF calling convetion is such that R0 is used |
| 5365 | * to return the value from eBPF program. |
| 5366 | * Make sure that it's readable at this time |
| 5367 | * of bpf_exit, which means that program wrote |
| 5368 | * something into it earlier |
| 5369 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5370 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5371 | if (err) |
| 5372 | return err; |
| 5373 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5374 | if (is_pointer_value(env, BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5375 | verbose(env, "R0 leaks addr as return value\n"); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5376 | return -EACCES; |
| 5377 | } |
| 5378 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5379 | err = check_return_code(env); |
| 5380 | if (err) |
| 5381 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5382 | process_bpf_exit: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5383 | err = pop_stack(env, &prev_insn_idx, &insn_idx); |
| 5384 | if (err < 0) { |
| 5385 | if (err != -ENOENT) |
| 5386 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5387 | break; |
| 5388 | } else { |
| 5389 | do_print_state = true; |
| 5390 | continue; |
| 5391 | } |
| 5392 | } else { |
| 5393 | err = check_cond_jmp_op(env, insn, &insn_idx); |
| 5394 | if (err) |
| 5395 | return err; |
| 5396 | } |
| 5397 | } else if (class == BPF_LD) { |
| 5398 | u8 mode = BPF_MODE(insn->code); |
| 5399 | |
| 5400 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5401 | err = check_ld_abs(env, insn); |
| 5402 | if (err) |
| 5403 | return err; |
| 5404 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5405 | } else if (mode == BPF_IMM) { |
| 5406 | err = check_ld_imm(env, insn); |
| 5407 | if (err) |
| 5408 | return err; |
| 5409 | |
| 5410 | insn_idx++; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5411 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5412 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5413 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5414 | return -EINVAL; |
| 5415 | } |
| 5416 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5417 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5418 | return -EINVAL; |
| 5419 | } |
| 5420 | |
| 5421 | insn_idx++; |
| 5422 | } |
| 5423 | |
Daniel Borkmann | 4bd95f4 | 2018-01-20 01:24:36 +0100 | [diff] [blame] | 5424 | verbose(env, "processed %d insns (limit %d), stack depth ", |
| 5425 | insn_processed, BPF_COMPLEXITY_LIMIT_INSNS); |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5426 | for (i = 0; i < env->subprog_cnt; i++) { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 5427 | u32 depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5428 | |
| 5429 | verbose(env, "%d", depth); |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5430 | if (i + 1 < env->subprog_cnt) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5431 | verbose(env, "+"); |
| 5432 | } |
| 5433 | verbose(env, "\n"); |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 5434 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5435 | return 0; |
| 5436 | } |
| 5437 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5438 | static int check_map_prealloc(struct bpf_map *map) |
| 5439 | { |
| 5440 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 5441 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 5442 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5443 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 5444 | } |
| 5445 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5446 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 5447 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5448 | struct bpf_prog *prog) |
| 5449 | |
| 5450 | { |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5451 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 5452 | * preallocated hash maps, since doing memory allocation |
| 5453 | * in overflow_handler can crash depending on where nmi got |
| 5454 | * triggered. |
| 5455 | */ |
| 5456 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 5457 | if (!check_map_prealloc(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5458 | 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] | 5459 | return -EINVAL; |
| 5460 | } |
| 5461 | if (map->inner_map_meta && |
| 5462 | !check_map_prealloc(map->inner_map_meta)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5463 | 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] | 5464 | return -EINVAL; |
| 5465 | } |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5466 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 5467 | |
| 5468 | 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] | 5469 | !bpf_offload_prog_map_match(prog, map)) { |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 5470 | verbose(env, "offload device mismatch between prog and map\n"); |
| 5471 | return -EINVAL; |
| 5472 | } |
| 5473 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5474 | return 0; |
| 5475 | } |
| 5476 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 5477 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 5478 | { |
| 5479 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 5480 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 5481 | } |
| 5482 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5483 | /* look for pseudo eBPF instructions that access map FDs and |
| 5484 | * replace them with actual map pointers |
| 5485 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5486 | 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] | 5487 | { |
| 5488 | struct bpf_insn *insn = env->prog->insnsi; |
| 5489 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5490 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5491 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 5492 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 5493 | if (err) |
| 5494 | return err; |
| 5495 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5496 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5497 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5498 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5499 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5500 | return -EINVAL; |
| 5501 | } |
| 5502 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5503 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 5504 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 5505 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5506 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5507 | return -EINVAL; |
| 5508 | } |
| 5509 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5510 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
| 5511 | struct bpf_map *map; |
| 5512 | struct fd f; |
| 5513 | |
| 5514 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 5515 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 5516 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5517 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5518 | return -EINVAL; |
| 5519 | } |
| 5520 | |
| 5521 | if (insn->src_reg == 0) |
| 5522 | /* valid generic load 64-bit imm */ |
| 5523 | goto next_insn; |
| 5524 | |
| 5525 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5526 | verbose(env, |
| 5527 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5528 | return -EINVAL; |
| 5529 | } |
| 5530 | |
| 5531 | f = fdget(insn->imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 5532 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5533 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5534 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5535 | insn->imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5536 | return PTR_ERR(map); |
| 5537 | } |
| 5538 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5539 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5540 | if (err) { |
| 5541 | fdput(f); |
| 5542 | return err; |
| 5543 | } |
| 5544 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5545 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
| 5546 | insn[0].imm = (u32) (unsigned long) map; |
| 5547 | insn[1].imm = ((u64) (unsigned long) map) >> 32; |
| 5548 | |
| 5549 | /* check whether we recorded this map already */ |
| 5550 | for (j = 0; j < env->used_map_cnt; j++) |
| 5551 | if (env->used_maps[j] == map) { |
| 5552 | fdput(f); |
| 5553 | goto next_insn; |
| 5554 | } |
| 5555 | |
| 5556 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 5557 | fdput(f); |
| 5558 | return -E2BIG; |
| 5559 | } |
| 5560 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5561 | /* hold the map. If the program is rejected by verifier, |
| 5562 | * the map will be released by release_maps() or it |
| 5563 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 5564 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5565 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 5566 | map = bpf_map_inc(map, false); |
| 5567 | if (IS_ERR(map)) { |
| 5568 | fdput(f); |
| 5569 | return PTR_ERR(map); |
| 5570 | } |
| 5571 | env->used_maps[env->used_map_cnt++] = map; |
| 5572 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 5573 | if (bpf_map_is_cgroup_storage(map) && |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 5574 | bpf_cgroup_storage_assign(env->prog, map)) { |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 5575 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 5576 | fdput(f); |
| 5577 | return -EBUSY; |
| 5578 | } |
| 5579 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5580 | fdput(f); |
| 5581 | next_insn: |
| 5582 | insn++; |
| 5583 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 5584 | continue; |
| 5585 | } |
| 5586 | |
| 5587 | /* Basic sanity check before we invest more work here. */ |
| 5588 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 5589 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 5590 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5591 | } |
| 5592 | } |
| 5593 | |
| 5594 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 5595 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 5596 | * These pointers will be used later by verifier to validate map access. |
| 5597 | */ |
| 5598 | return 0; |
| 5599 | } |
| 5600 | |
| 5601 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5602 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5603 | { |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 5604 | enum bpf_cgroup_storage_type stype; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5605 | int i; |
| 5606 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 5607 | for_each_cgroup_storage_type(stype) { |
| 5608 | if (!env->prog->aux->cgroup_storage[stype]) |
| 5609 | continue; |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 5610 | bpf_cgroup_storage_release(env->prog, |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 5611 | env->prog->aux->cgroup_storage[stype]); |
| 5612 | } |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 5613 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5614 | for (i = 0; i < env->used_map_cnt; i++) |
| 5615 | bpf_map_put(env->used_maps[i]); |
| 5616 | } |
| 5617 | |
| 5618 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5619 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5620 | { |
| 5621 | struct bpf_insn *insn = env->prog->insnsi; |
| 5622 | int insn_cnt = env->prog->len; |
| 5623 | int i; |
| 5624 | |
| 5625 | for (i = 0; i < insn_cnt; i++, insn++) |
| 5626 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 5627 | insn->src_reg = 0; |
| 5628 | } |
| 5629 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5630 | /* single env->prog->insni[off] instruction was replaced with the range |
| 5631 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 5632 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 5633 | */ |
| 5634 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, |
| 5635 | u32 off, u32 cnt) |
| 5636 | { |
| 5637 | 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] | 5638 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5639 | |
| 5640 | if (cnt == 1) |
| 5641 | return 0; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 5642 | new_data = vzalloc(array_size(prog_len, |
| 5643 | sizeof(struct bpf_insn_aux_data))); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5644 | if (!new_data) |
| 5645 | return -ENOMEM; |
| 5646 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 5647 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 5648 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5649 | for (i = off; i < off + cnt - 1; i++) |
| 5650 | new_data[i].seen = true; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5651 | env->insn_aux_data = new_data; |
| 5652 | vfree(old_data); |
| 5653 | return 0; |
| 5654 | } |
| 5655 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5656 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 5657 | { |
| 5658 | int i; |
| 5659 | |
| 5660 | if (len == 1) |
| 5661 | return; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 5662 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 5663 | for (i = 0; i <= env->subprog_cnt; i++) { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 5664 | if (env->subprog_info[i].start < off) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5665 | continue; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 5666 | env->subprog_info[i].start += len - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5667 | } |
| 5668 | } |
| 5669 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5670 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 5671 | const struct bpf_insn *patch, u32 len) |
| 5672 | { |
| 5673 | struct bpf_prog *new_prog; |
| 5674 | |
| 5675 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
| 5676 | if (!new_prog) |
| 5677 | return NULL; |
| 5678 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) |
| 5679 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5680 | adjust_subprog_starts(env, off, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5681 | return new_prog; |
| 5682 | } |
| 5683 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 5684 | /* The verifier does more data flow analysis than llvm and will not |
| 5685 | * explore branches that are dead at run time. Malicious programs can |
| 5686 | * have dead code too. Therefore replace all dead at-run-time code |
| 5687 | * with 'ja -1'. |
| 5688 | * |
| 5689 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 5690 | * program and through another bug we would manage to jump there, then |
| 5691 | * we'd execute beyond program memory otherwise. Returning exception |
| 5692 | * code also wouldn't work since we can have subprogs where the dead |
| 5693 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5694 | */ |
| 5695 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 5696 | { |
| 5697 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 5698 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5699 | struct bpf_insn *insn = env->prog->insnsi; |
| 5700 | const int insn_cnt = env->prog->len; |
| 5701 | int i; |
| 5702 | |
| 5703 | for (i = 0; i < insn_cnt; i++) { |
| 5704 | if (aux_data[i].seen) |
| 5705 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 5706 | memcpy(insn + i, &trap, sizeof(trap)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5707 | } |
| 5708 | } |
| 5709 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5710 | /* convert load instructions that access fields of a context type into a |
| 5711 | * sequence of instructions that access fields of the underlying structure: |
| 5712 | * struct __sk_buff -> struct sk_buff |
| 5713 | * struct bpf_sock_ops -> struct sock |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5714 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5715 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5716 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5717 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5718 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5719 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5720 | struct bpf_insn insn_buf[16], *insn; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5721 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5722 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5723 | bool is_narrower_load; |
| 5724 | u32 target_size; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5725 | |
Daniel Borkmann | b09928b | 2018-10-24 22:05:49 +0200 | [diff] [blame] | 5726 | if (ops->gen_prologue || env->seen_direct_write) { |
| 5727 | if (!ops->gen_prologue) { |
| 5728 | verbose(env, "bpf verifier is misconfigured\n"); |
| 5729 | return -EINVAL; |
| 5730 | } |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5731 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 5732 | env->prog); |
| 5733 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5734 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5735 | return -EINVAL; |
| 5736 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5737 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5738 | if (!new_prog) |
| 5739 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5740 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5741 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5742 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5743 | } |
| 5744 | } |
| 5745 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5746 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5747 | return 0; |
| 5748 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5749 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5750 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5751 | for (i = 0; i < insn_cnt; i++, insn++) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5752 | bpf_convert_ctx_access_t convert_ctx_access; |
| 5753 | |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 5754 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 5755 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 5756 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 5757 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5758 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 5759 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 5760 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 5761 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 5762 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5763 | type = BPF_WRITE; |
| 5764 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5765 | continue; |
| 5766 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 5767 | if (type == BPF_WRITE && |
| 5768 | env->insn_aux_data[i + delta].sanitize_stack_off) { |
| 5769 | struct bpf_insn patch[] = { |
| 5770 | /* Sanitize suspicious stack slot with zero. |
| 5771 | * There are no memory dependencies for this store, |
| 5772 | * since it's only using frame pointer and immediate |
| 5773 | * constant of zero |
| 5774 | */ |
| 5775 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, |
| 5776 | env->insn_aux_data[i + delta].sanitize_stack_off, |
| 5777 | 0), |
| 5778 | /* the original STX instruction will immediately |
| 5779 | * overwrite the same stack slot with appropriate value |
| 5780 | */ |
| 5781 | *insn, |
| 5782 | }; |
| 5783 | |
| 5784 | cnt = ARRAY_SIZE(patch); |
| 5785 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 5786 | if (!new_prog) |
| 5787 | return -ENOMEM; |
| 5788 | |
| 5789 | delta += cnt - 1; |
| 5790 | env->prog = new_prog; |
| 5791 | insn = new_prog->insnsi + i + delta; |
| 5792 | continue; |
| 5793 | } |
| 5794 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5795 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 5796 | case PTR_TO_CTX: |
| 5797 | if (!ops->convert_ctx_access) |
| 5798 | continue; |
| 5799 | convert_ctx_access = ops->convert_ctx_access; |
| 5800 | break; |
| 5801 | case PTR_TO_SOCKET: |
| 5802 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 5803 | break; |
| 5804 | default: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5805 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5806 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5807 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5808 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5809 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5810 | |
| 5811 | /* If the read access is a narrower load of the field, |
| 5812 | * convert to a 4/8-byte load, to minimum program type specific |
| 5813 | * convert_ctx_access changes. If conversion is successful, |
| 5814 | * we will apply proper mask to the result. |
| 5815 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5816 | is_narrower_load = size < ctx_field_size; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5817 | if (is_narrower_load) { |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 5818 | u32 size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5819 | u32 off = insn->off; |
| 5820 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5821 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5822 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5823 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5824 | return -EINVAL; |
| 5825 | } |
| 5826 | |
| 5827 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5828 | if (ctx_field_size == 4) |
| 5829 | size_code = BPF_W; |
| 5830 | else if (ctx_field_size == 8) |
| 5831 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5832 | |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 5833 | insn->off = off & ~(size_default - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5834 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 5835 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5836 | |
| 5837 | target_size = 0; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5838 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 5839 | &target_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5840 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 5841 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5842 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5843 | return -EINVAL; |
| 5844 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5845 | |
| 5846 | if (is_narrower_load && size < target_size) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5847 | if (ctx_field_size <= 4) |
| 5848 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5849 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5850 | else |
| 5851 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5852 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5853 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5854 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5855 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5856 | if (!new_prog) |
| 5857 | return -ENOMEM; |
| 5858 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5859 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5860 | |
| 5861 | /* keep walking new program and skip insns we just inserted */ |
| 5862 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5863 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5864 | } |
| 5865 | |
| 5866 | return 0; |
| 5867 | } |
| 5868 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5869 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 5870 | { |
| 5871 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 5872 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5873 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5874 | void *old_bpf_func; |
| 5875 | int err = -ENOMEM; |
| 5876 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5877 | if (env->subprog_cnt <= 1) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5878 | return 0; |
| 5879 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5880 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5881 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5882 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5883 | continue; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 5884 | /* Upon error here we cannot fall back to interpreter but |
| 5885 | * need a hard reject of the program. Thus -EFAULT is |
| 5886 | * propagated in any case. |
| 5887 | */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5888 | subprog = find_subprog(env, i + insn->imm + 1); |
| 5889 | if (subprog < 0) { |
| 5890 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 5891 | i + insn->imm + 1); |
| 5892 | return -EFAULT; |
| 5893 | } |
| 5894 | /* temporarily remember subprog id inside insn instead of |
| 5895 | * aux_data, since next loop will split up all insns into funcs |
| 5896 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5897 | insn->off = subprog; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5898 | /* remember original imm in case JIT fails and fallback |
| 5899 | * to interpreter will be needed |
| 5900 | */ |
| 5901 | env->insn_aux_data[i].call_imm = insn->imm; |
| 5902 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 5903 | insn->imm = 1; |
| 5904 | } |
| 5905 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 5906 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5907 | if (!func) |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 5908 | goto out_undo_insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5909 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5910 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5911 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 5912 | subprog_end = env->subprog_info[i + 1].start; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5913 | |
| 5914 | len = subprog_end - subprog_start; |
| 5915 | func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER); |
| 5916 | if (!func[i]) |
| 5917 | goto out_free; |
| 5918 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 5919 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 5920 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5921 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 5922 | if (bpf_prog_calc_tag(func[i])) |
| 5923 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5924 | func[i]->is_func = 1; |
| 5925 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 5926 | * Long term would need debug info to populate names |
| 5927 | */ |
| 5928 | func[i]->aux->name[0] = 'F'; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 5929 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5930 | func[i]->jit_requested = 1; |
| 5931 | func[i] = bpf_int_jit_compile(func[i]); |
| 5932 | if (!func[i]->jited) { |
| 5933 | err = -ENOTSUPP; |
| 5934 | goto out_free; |
| 5935 | } |
| 5936 | cond_resched(); |
| 5937 | } |
| 5938 | /* at this point all bpf functions were successfully JITed |
| 5939 | * now populate all bpf_calls with correct addresses and |
| 5940 | * run last pass of JIT |
| 5941 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5942 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5943 | insn = func[i]->insnsi; |
| 5944 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 5945 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5946 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5947 | continue; |
| 5948 | subprog = insn->off; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5949 | insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) |
| 5950 | func[subprog]->bpf_func - |
| 5951 | __bpf_call_base; |
| 5952 | } |
Sandipan Das | 2162fed | 2018-05-24 12:26:45 +0530 | [diff] [blame] | 5953 | |
| 5954 | /* we use the aux data to keep a list of the start addresses |
| 5955 | * of the JITed images for each function in the program |
| 5956 | * |
| 5957 | * for some architectures, such as powerpc64, the imm field |
| 5958 | * might not be large enough to hold the offset of the start |
| 5959 | * address of the callee's JITed image from __bpf_call_base |
| 5960 | * |
| 5961 | * in such cases, we can lookup the start address of a callee |
| 5962 | * by using its subprog id, available from the off field of |
| 5963 | * the call instruction, as an index for this list |
| 5964 | */ |
| 5965 | func[i]->aux->func = func; |
| 5966 | func[i]->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5967 | } |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5968 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5969 | old_bpf_func = func[i]->bpf_func; |
| 5970 | tmp = bpf_int_jit_compile(func[i]); |
| 5971 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 5972 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 5973 | err = -ENOTSUPP; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5974 | goto out_free; |
| 5975 | } |
| 5976 | cond_resched(); |
| 5977 | } |
| 5978 | |
| 5979 | /* finally lock prog and jit images for all functions and |
| 5980 | * populate kallsysm |
| 5981 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5982 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5983 | bpf_prog_lock_ro(func[i]); |
| 5984 | bpf_prog_kallsyms_add(func[i]); |
| 5985 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5986 | |
| 5987 | /* Last step: make now unused interpreter insns from main |
| 5988 | * prog consistent for later dump requests, so they can |
| 5989 | * later look the same as if they were interpreted only. |
| 5990 | */ |
| 5991 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5992 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5993 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5994 | continue; |
| 5995 | insn->off = env->insn_aux_data[i].call_imm; |
| 5996 | subprog = find_subprog(env, i + insn->off + 1); |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 5997 | insn->imm = subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5998 | } |
| 5999 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6000 | prog->jited = 1; |
| 6001 | prog->bpf_func = func[0]->bpf_func; |
| 6002 | prog->aux->func = func; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6003 | prog->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6004 | return 0; |
| 6005 | out_free: |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6006 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6007 | if (func[i]) |
| 6008 | bpf_jit_free(func[i]); |
| 6009 | kfree(func); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 6010 | out_undo_insn: |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6011 | /* cleanup main prog to be interpreted */ |
| 6012 | prog->jit_requested = 0; |
| 6013 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 6014 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 6015 | insn->src_reg != BPF_PSEUDO_CALL) |
| 6016 | continue; |
| 6017 | insn->off = 0; |
| 6018 | insn->imm = env->insn_aux_data[i].call_imm; |
| 6019 | } |
| 6020 | return err; |
| 6021 | } |
| 6022 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6023 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 6024 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6025 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6026 | struct bpf_prog *prog = env->prog; |
| 6027 | struct bpf_insn *insn = prog->insnsi; |
| 6028 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6029 | #endif |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 6030 | int err = 0; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6031 | |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 6032 | if (env->prog->jit_requested && |
| 6033 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6034 | err = jit_subprogs(env); |
| 6035 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6036 | return 0; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 6037 | if (err == -EFAULT) |
| 6038 | return err; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6039 | } |
| 6040 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6041 | for (i = 0; i < prog->len; i++, insn++) { |
| 6042 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 6043 | insn->src_reg != BPF_PSEUDO_CALL) |
| 6044 | continue; |
| 6045 | depth = get_callee_stack_depth(env, insn, i); |
| 6046 | if (depth < 0) |
| 6047 | return depth; |
| 6048 | bpf_patch_call_args(insn, depth); |
| 6049 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6050 | err = 0; |
| 6051 | #endif |
| 6052 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6053 | } |
| 6054 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6055 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6056 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6057 | * |
| 6058 | * this function is called after eBPF program passed verification |
| 6059 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6060 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6061 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6062 | struct bpf_prog *prog = env->prog; |
| 6063 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6064 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6065 | const int insn_cnt = prog->len; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6066 | const struct bpf_map_ops *ops; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6067 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6068 | struct bpf_insn insn_buf[16]; |
| 6069 | struct bpf_prog *new_prog; |
| 6070 | struct bpf_map *map_ptr; |
| 6071 | int i, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6072 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6073 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 6074 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 6075 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 6076 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 6077 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 6078 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
| 6079 | struct bpf_insn mask_and_div[] = { |
| 6080 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 6081 | /* Rx div 0 -> 0 */ |
| 6082 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), |
| 6083 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 6084 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 6085 | *insn, |
| 6086 | }; |
| 6087 | struct bpf_insn mask_and_mod[] = { |
| 6088 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 6089 | /* Rx mod 0 -> Rx */ |
| 6090 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), |
| 6091 | *insn, |
| 6092 | }; |
| 6093 | struct bpf_insn *patchlet; |
| 6094 | |
| 6095 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 6096 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 6097 | patchlet = mask_and_div + (is64 ? 1 : 0); |
| 6098 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); |
| 6099 | } else { |
| 6100 | patchlet = mask_and_mod + (is64 ? 1 : 0); |
| 6101 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); |
| 6102 | } |
| 6103 | |
| 6104 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 6105 | if (!new_prog) |
| 6106 | return -ENOMEM; |
| 6107 | |
| 6108 | delta += cnt - 1; |
| 6109 | env->prog = prog = new_prog; |
| 6110 | insn = new_prog->insnsi + i + delta; |
| 6111 | continue; |
| 6112 | } |
| 6113 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 6114 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 6115 | (BPF_MODE(insn->code) == BPF_ABS || |
| 6116 | BPF_MODE(insn->code) == BPF_IND)) { |
| 6117 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 6118 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 6119 | verbose(env, "bpf verifier is misconfigured\n"); |
| 6120 | return -EINVAL; |
| 6121 | } |
| 6122 | |
| 6123 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 6124 | if (!new_prog) |
| 6125 | return -ENOMEM; |
| 6126 | |
| 6127 | delta += cnt - 1; |
| 6128 | env->prog = prog = new_prog; |
| 6129 | insn = new_prog->insnsi + i + delta; |
| 6130 | continue; |
| 6131 | } |
| 6132 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6133 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 6134 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 6135 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 6136 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6137 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6138 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 6139 | prog->dst_needed = 1; |
| 6140 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 6141 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 6142 | if (insn->imm == BPF_FUNC_override_return) |
| 6143 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6144 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 6145 | /* If we tail call into other programs, we |
| 6146 | * cannot make any assumptions since they can |
| 6147 | * be replaced dynamically during runtime in |
| 6148 | * the program array. |
| 6149 | */ |
| 6150 | prog->cb_access = 1; |
Alexei Starovoitov | 80a58d0 | 2017-05-30 13:31:30 -0700 | [diff] [blame] | 6151 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 6152 | env->prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 6153 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6154 | /* mark bpf_tail_call as different opcode to avoid |
| 6155 | * conditional branch in the interpeter for every normal |
| 6156 | * call and to prevent accidental JITing by JIT compiler |
| 6157 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6158 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6159 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 6160 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 6161 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6162 | aux = &env->insn_aux_data[i + delta]; |
| 6163 | if (!bpf_map_ptr_unpriv(aux)) |
| 6164 | continue; |
| 6165 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 6166 | /* instead of changing every JIT dealing with tail_call |
| 6167 | * emit two extra insns: |
| 6168 | * if (index >= max_entries) goto out; |
| 6169 | * index &= array->index_mask; |
| 6170 | * to avoid out-of-bounds cpu speculation |
| 6171 | */ |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6172 | if (bpf_map_ptr_poisoned(aux)) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 6173 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 6174 | return -EINVAL; |
| 6175 | } |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6176 | |
| 6177 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 6178 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 6179 | map_ptr->max_entries, 2); |
| 6180 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 6181 | container_of(map_ptr, |
| 6182 | struct bpf_array, |
| 6183 | map)->index_mask); |
| 6184 | insn_buf[2] = *insn; |
| 6185 | cnt = 3; |
| 6186 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 6187 | if (!new_prog) |
| 6188 | return -ENOMEM; |
| 6189 | |
| 6190 | delta += cnt - 1; |
| 6191 | env->prog = prog = new_prog; |
| 6192 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6193 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6194 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6195 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 6196 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6197 | * and other inlining handlers are currently limited to 64 bit |
| 6198 | * only. |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 6199 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 6200 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6201 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 6202 | insn->imm == BPF_FUNC_map_update_elem || |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 6203 | insn->imm == BPF_FUNC_map_delete_elem || |
| 6204 | insn->imm == BPF_FUNC_map_push_elem || |
| 6205 | insn->imm == BPF_FUNC_map_pop_elem || |
| 6206 | insn->imm == BPF_FUNC_map_peek_elem)) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6207 | aux = &env->insn_aux_data[i + delta]; |
| 6208 | if (bpf_map_ptr_poisoned(aux)) |
| 6209 | goto patch_call_imm; |
| 6210 | |
| 6211 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6212 | ops = map_ptr->ops; |
| 6213 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 6214 | ops->map_gen_lookup) { |
| 6215 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
| 6216 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 6217 | verbose(env, "bpf verifier is misconfigured\n"); |
| 6218 | return -EINVAL; |
| 6219 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6220 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6221 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 6222 | insn_buf, cnt); |
| 6223 | if (!new_prog) |
| 6224 | return -ENOMEM; |
| 6225 | |
| 6226 | delta += cnt - 1; |
| 6227 | env->prog = prog = new_prog; |
| 6228 | insn = new_prog->insnsi + i + delta; |
| 6229 | continue; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6230 | } |
| 6231 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6232 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 6233 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 6234 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 6235 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 6236 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 6237 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 6238 | u64 flags))NULL)); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 6239 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 6240 | (int (*)(struct bpf_map *map, void *value, |
| 6241 | u64 flags))NULL)); |
| 6242 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 6243 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 6244 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 6245 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 6246 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6247 | switch (insn->imm) { |
| 6248 | case BPF_FUNC_map_lookup_elem: |
| 6249 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - |
| 6250 | __bpf_call_base; |
| 6251 | continue; |
| 6252 | case BPF_FUNC_map_update_elem: |
| 6253 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - |
| 6254 | __bpf_call_base; |
| 6255 | continue; |
| 6256 | case BPF_FUNC_map_delete_elem: |
| 6257 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - |
| 6258 | __bpf_call_base; |
| 6259 | continue; |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 6260 | case BPF_FUNC_map_push_elem: |
| 6261 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - |
| 6262 | __bpf_call_base; |
| 6263 | continue; |
| 6264 | case BPF_FUNC_map_pop_elem: |
| 6265 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - |
| 6266 | __bpf_call_base; |
| 6267 | continue; |
| 6268 | case BPF_FUNC_map_peek_elem: |
| 6269 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - |
| 6270 | __bpf_call_base; |
| 6271 | continue; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6272 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6273 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6274 | goto patch_call_imm; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6275 | } |
| 6276 | |
| 6277 | patch_call_imm: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 6278 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6279 | /* all functions that have prototype and verifier allowed |
| 6280 | * programs to call them, must be real in-kernel functions |
| 6281 | */ |
| 6282 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6283 | verbose(env, |
| 6284 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6285 | func_id_name(insn->imm), insn->imm); |
| 6286 | return -EFAULT; |
| 6287 | } |
| 6288 | insn->imm = fn->func - __bpf_call_base; |
| 6289 | } |
| 6290 | |
| 6291 | return 0; |
| 6292 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6293 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6294 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6295 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6296 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6297 | int i; |
| 6298 | |
| 6299 | if (!env->explored_states) |
| 6300 | return; |
| 6301 | |
| 6302 | for (i = 0; i < env->prog->len; i++) { |
| 6303 | sl = env->explored_states[i]; |
| 6304 | |
| 6305 | if (sl) |
| 6306 | while (sl != STATE_LIST_MARK) { |
| 6307 | sln = sl->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 6308 | free_verifier_state(&sl->state, false); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6309 | kfree(sl); |
| 6310 | sl = sln; |
| 6311 | } |
| 6312 | } |
| 6313 | |
| 6314 | kfree(env->explored_states); |
| 6315 | } |
| 6316 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6317 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 6318 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6319 | struct bpf_verifier_env *env; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 6320 | struct bpf_verifier_log *log; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 6321 | int ret = -EINVAL; |
| 6322 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 6323 | /* no program is valid */ |
| 6324 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 6325 | return -EINVAL; |
| 6326 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6327 | /* '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] | 6328 | * allocate/free it every time bpf_check() is called |
| 6329 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6330 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6331 | if (!env) |
| 6332 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6333 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6334 | |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 6335 | env->insn_aux_data = |
| 6336 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), |
| 6337 | (*prog)->len)); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6338 | ret = -ENOMEM; |
| 6339 | if (!env->insn_aux_data) |
| 6340 | goto err_free_env; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6341 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 6342 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6343 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6344 | /* grab the mutex to protect few globals used by verifier */ |
| 6345 | mutex_lock(&bpf_verifier_lock); |
| 6346 | |
| 6347 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 6348 | /* user requested verbose verifier output |
| 6349 | * and supplied buffer to store the verification trace |
| 6350 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 6351 | log->level = attr->log_level; |
| 6352 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 6353 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6354 | |
| 6355 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 6356 | /* log attributes have to be sane */ |
| 6357 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || |
| 6358 | !log->level || !log->ubuf) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6359 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6360 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 6361 | |
| 6362 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 6363 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 6364 | env->strict_alignment = true; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6365 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6366 | ret = replace_map_fd_with_map_ptr(env); |
| 6367 | if (ret < 0) |
| 6368 | goto skip_full_check; |
| 6369 | |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 6370 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Quentin Monnet | a40a263 | 2018-11-09 13:03:31 +0000 | [diff] [blame^] | 6371 | ret = bpf_prog_offload_verifier_prep(env->prog); |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 6372 | if (ret) |
| 6373 | goto skip_full_check; |
| 6374 | } |
| 6375 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6376 | env->explored_states = kcalloc(env->prog->len, |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6377 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6378 | GFP_USER); |
| 6379 | ret = -ENOMEM; |
| 6380 | if (!env->explored_states) |
| 6381 | goto skip_full_check; |
| 6382 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 6383 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 6384 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6385 | ret = check_cfg(env); |
| 6386 | if (ret < 0) |
| 6387 | goto skip_full_check; |
| 6388 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6389 | ret = do_check(env); |
Craig Gallek | 8c01c4f | 2017-11-02 11:18:01 -0400 | [diff] [blame] | 6390 | if (env->cur_state) { |
| 6391 | free_verifier_state(env->cur_state, true); |
| 6392 | env->cur_state = NULL; |
| 6393 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6394 | |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 6395 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 6396 | ret = bpf_prog_offload_finalize(env); |
| 6397 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6398 | skip_full_check: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6399 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6400 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6401 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6402 | if (ret == 0) |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 6403 | sanitize_dead_code(env); |
| 6404 | |
| 6405 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 6406 | ret = check_max_stack_depth(env); |
| 6407 | |
| 6408 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6409 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 6410 | ret = convert_ctx_accesses(env); |
| 6411 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6412 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6413 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6414 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6415 | if (ret == 0) |
| 6416 | ret = fixup_call_args(env); |
| 6417 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 6418 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6419 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 6420 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6421 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 6422 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6423 | } |
| 6424 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6425 | if (ret == 0 && env->used_map_cnt) { |
| 6426 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6427 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 6428 | sizeof(env->used_maps[0]), |
| 6429 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6430 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6431 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6432 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 6433 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6434 | } |
| 6435 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6436 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6437 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6438 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6439 | |
| 6440 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 6441 | * bpf_ld_imm64 instructions |
| 6442 | */ |
| 6443 | convert_pseudo_ld_imm64(env); |
| 6444 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6445 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 6446 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6447 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6448 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 6449 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6450 | */ |
| 6451 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6452 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6453 | err_unlock: |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6454 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6455 | vfree(env->insn_aux_data); |
| 6456 | err_free_env: |
| 6457 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 6458 | return ret; |
| 6459 | } |