Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016 Facebook |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3 | * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of version 2 of the GNU General Public |
| 7 | * License as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 14 | #include <uapi/linux/btf.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/bpf.h> |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 19 | #include <linux/btf.h> |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 20 | #include <linux/bpf_verifier.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 21 | #include <linux/filter.h> |
| 22 | #include <net/netlink.h> |
| 23 | #include <linux/file.h> |
| 24 | #include <linux/vmalloc.h> |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 25 | #include <linux/stringify.h> |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 26 | #include <linux/bsearch.h> |
| 27 | #include <linux/sort.h> |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 28 | #include <linux/perf_event.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 29 | |
Jakub Kicinski | f4ac7e0 | 2017-10-09 10:30:12 -0700 | [diff] [blame] | 30 | #include "disasm.h" |
| 31 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 32 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
| 33 | #define BPF_PROG_TYPE(_id, _name) \ |
| 34 | [_id] = & _name ## _verifier_ops, |
| 35 | #define BPF_MAP_TYPE(_id, _ops) |
| 36 | #include <linux/bpf_types.h> |
| 37 | #undef BPF_PROG_TYPE |
| 38 | #undef BPF_MAP_TYPE |
| 39 | }; |
| 40 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 41 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 42 | * instruction by instruction and updates register/stack state. |
| 43 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 44 | * |
| 45 | * The first pass is depth-first-search to check that the program is a DAG. |
| 46 | * It rejects the following programs: |
| 47 | * - larger than BPF_MAXINSNS insns |
| 48 | * - if loop is present (detected via back-edge) |
| 49 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 50 | * - out of bounds or malformed jumps |
| 51 | * The second pass is all possible path descent from the 1st insn. |
| 52 | * 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] | 53 | * 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] | 54 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 55 | * Number of 'branches to be analyzed' is limited to 1k |
| 56 | * |
| 57 | * On entry to each instruction, each register has a type, and the instruction |
| 58 | * changes the types of the registers depending on instruction semantics. |
| 59 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 60 | * copied to R1. |
| 61 | * |
| 62 | * All registers are 64-bit. |
| 63 | * R0 - return register |
| 64 | * R1-R5 argument passing registers |
| 65 | * R6-R9 callee saved registers |
| 66 | * R10 - frame pointer read-only |
| 67 | * |
| 68 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 69 | * and has type PTR_TO_CTX. |
| 70 | * |
| 71 | * Verifier tracks arithmetic operations on pointers in case: |
| 72 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 73 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 74 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 75 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 76 | * that it wants to construct a pointer to some element within stack. |
| 77 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 78 | * (and -20 constant is saved for further stack bounds checking). |
| 79 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 80 | * |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 81 | * Most of the time the registers have SCALAR_VALUE type, which |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 82 | * 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] | 83 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 84 | * |
| 85 | * When verifier sees load or store instructions the type of base register |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 86 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
| 87 | * four pointer types recognized by check_mem_access() function. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 88 | * |
| 89 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 90 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 91 | * |
| 92 | * registers used to pass values to function calls are checked against |
| 93 | * function argument constraints. |
| 94 | * |
| 95 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 96 | * It means that the register type passed to this function must be |
| 97 | * PTR_TO_STACK and it will be used inside the function as |
| 98 | * 'pointer to map element key' |
| 99 | * |
| 100 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 101 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 102 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 103 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 104 | * |
| 105 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 106 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 107 | * 2nd argument should be a pointer to stack, which will be used inside |
| 108 | * the helper function as a pointer to map element key. |
| 109 | * |
| 110 | * On the kernel side the helper function looks like: |
| 111 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 112 | * { |
| 113 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 114 | * void *key = (void *) (unsigned long) r2; |
| 115 | * void *value; |
| 116 | * |
| 117 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 118 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 119 | * the stack of eBPF program. |
| 120 | * } |
| 121 | * |
| 122 | * Corresponding eBPF program may look like: |
| 123 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 124 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 125 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 126 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 127 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 128 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 129 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 130 | * |
| 131 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 132 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 133 | * and were initialized prior to this call. |
| 134 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 135 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 136 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 137 | * returns ether pointer to map value or NULL. |
| 138 | * |
| 139 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 140 | * insn, the register holding that pointer in the true branch changes state to |
| 141 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 142 | * branch. See check_cond_jmp_op(). |
| 143 | * |
| 144 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 145 | * 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] | 146 | * |
| 147 | * The following reference types represent a potential reference to a kernel |
| 148 | * resource which, after first being allocated, must be checked and freed by |
| 149 | * the BPF program: |
| 150 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET |
| 151 | * |
| 152 | * When the verifier sees a helper call return a reference type, it allocates a |
| 153 | * pointer id for the reference and stores it in the current function state. |
| 154 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into |
| 155 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type |
| 156 | * passes through a NULL-check conditional. For the branch wherein the state is |
| 157 | * changed to CONST_IMM, the verifier releases the reference. |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 158 | * |
| 159 | * For each helper function that allocates a reference, such as |
| 160 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as |
| 161 | * bpf_sk_release(). When a reference type passes into the release function, |
| 162 | * the verifier also releases the reference. If any unchecked or unreleased |
| 163 | * reference remains at the end of the program, the verifier rejects it. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 164 | */ |
| 165 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 166 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 167 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 168 | /* verifer state is 'st' |
| 169 | * before processing instruction 'insn_idx' |
| 170 | * and after processing instruction 'prev_insn_idx' |
| 171 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 172 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 173 | int insn_idx; |
| 174 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 175 | struct bpf_verifier_stack_elem *next; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Edward Cree | 8e17c1b | 2017-08-07 15:30:30 +0100 | [diff] [blame] | 178 | #define BPF_COMPLEXITY_LIMIT_INSNS 131072 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 179 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
| 180 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 181 | #define BPF_MAP_PTR_UNPRIV 1UL |
| 182 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ |
| 183 | POISON_POINTER_DELTA)) |
| 184 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) |
| 185 | |
| 186 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
| 187 | { |
| 188 | return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON; |
| 189 | } |
| 190 | |
| 191 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) |
| 192 | { |
| 193 | return aux->map_state & BPF_MAP_PTR_UNPRIV; |
| 194 | } |
| 195 | |
| 196 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, |
| 197 | const struct bpf_map *map, bool unpriv) |
| 198 | { |
| 199 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); |
| 200 | unpriv |= bpf_map_ptr_unpriv(aux); |
| 201 | aux->map_state = (unsigned long)map | |
| 202 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); |
| 203 | } |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 204 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 205 | struct bpf_call_arg_meta { |
| 206 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 207 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 208 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 209 | int regno; |
| 210 | int access_size; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 211 | s64 msize_smax_value; |
| 212 | u64 msize_umax_value; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 213 | int ptr_id; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 214 | }; |
| 215 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 216 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 217 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 218 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
| 219 | va_list args) |
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 | unsigned int n; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 222 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 223 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 224 | |
| 225 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 226 | "verifier log line truncated - local buffer too short\n"); |
| 227 | |
| 228 | n = min(log->len_total - log->len_used - 1, n); |
| 229 | log->kbuf[n] = '\0'; |
| 230 | |
| 231 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 232 | log->len_used += n; |
| 233 | else |
| 234 | log->ubuf = NULL; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 235 | } |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 236 | |
| 237 | /* log_level controls verbosity level of eBPF verifier. |
| 238 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 239 | * so the user can figure out what's wrong with the program |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 240 | */ |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 241 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 242 | const char *fmt, ...) |
| 243 | { |
| 244 | va_list args; |
| 245 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 246 | if (!bpf_verifier_log_needed(&env->log)) |
| 247 | return; |
| 248 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 249 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 250 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 251 | va_end(args); |
| 252 | } |
| 253 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 254 | |
| 255 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
| 256 | { |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 257 | struct bpf_verifier_env *env = private_data; |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 258 | va_list args; |
| 259 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 260 | if (!bpf_verifier_log_needed(&env->log)) |
| 261 | return; |
| 262 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 263 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 264 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 265 | va_end(args); |
| 266 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 267 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 268 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 269 | { |
| 270 | return type == PTR_TO_PACKET || |
| 271 | type == PTR_TO_PACKET_META; |
| 272 | } |
| 273 | |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 274 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
| 275 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 276 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
| 277 | type == PTR_TO_SOCKET_OR_NULL; |
| 278 | } |
| 279 | |
| 280 | static bool type_is_refcounted(enum bpf_reg_type type) |
| 281 | { |
| 282 | return type == PTR_TO_SOCKET; |
| 283 | } |
| 284 | |
| 285 | static bool type_is_refcounted_or_null(enum bpf_reg_type type) |
| 286 | { |
| 287 | return type == PTR_TO_SOCKET || type == PTR_TO_SOCKET_OR_NULL; |
| 288 | } |
| 289 | |
| 290 | static bool reg_is_refcounted(const struct bpf_reg_state *reg) |
| 291 | { |
| 292 | return type_is_refcounted(reg->type); |
| 293 | } |
| 294 | |
| 295 | static bool reg_is_refcounted_or_null(const struct bpf_reg_state *reg) |
| 296 | { |
| 297 | return type_is_refcounted_or_null(reg->type); |
| 298 | } |
| 299 | |
| 300 | static bool arg_type_is_refcounted(enum bpf_arg_type type) |
| 301 | { |
| 302 | return type == ARG_PTR_TO_SOCKET; |
| 303 | } |
| 304 | |
| 305 | /* Determine whether the function releases some resources allocated by another |
| 306 | * function call. The first reference type argument will be assumed to be |
| 307 | * released by release_reference(). |
| 308 | */ |
| 309 | static bool is_release_function(enum bpf_func_id func_id) |
| 310 | { |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 311 | return func_id == BPF_FUNC_sk_release; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 314 | /* string representation of 'enum bpf_reg_type' */ |
| 315 | static const char * const reg_type_str[] = { |
| 316 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 317 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 318 | [PTR_TO_CTX] = "ctx", |
| 319 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 320 | [PTR_TO_MAP_VALUE] = "map_value", |
| 321 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 322 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 323 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 324 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 325 | [PTR_TO_PACKET_END] = "pkt_end", |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 326 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 327 | [PTR_TO_SOCKET] = "sock", |
| 328 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 329 | }; |
| 330 | |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 331 | static char slot_type_char[] = { |
| 332 | [STACK_INVALID] = '?', |
| 333 | [STACK_SPILL] = 'r', |
| 334 | [STACK_MISC] = 'm', |
| 335 | [STACK_ZERO] = '0', |
| 336 | }; |
| 337 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 338 | static void print_liveness(struct bpf_verifier_env *env, |
| 339 | enum bpf_reg_liveness live) |
| 340 | { |
| 341 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN)) |
| 342 | verbose(env, "_"); |
| 343 | if (live & REG_LIVE_READ) |
| 344 | verbose(env, "r"); |
| 345 | if (live & REG_LIVE_WRITTEN) |
| 346 | verbose(env, "w"); |
| 347 | } |
| 348 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 349 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 350 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 351 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 352 | struct bpf_verifier_state *cur = env->cur_state; |
| 353 | |
| 354 | return cur->frame[reg->frameno]; |
| 355 | } |
| 356 | |
| 357 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 358 | const struct bpf_func_state *state) |
| 359 | { |
| 360 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 361 | enum bpf_reg_type t; |
| 362 | int i; |
| 363 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 364 | if (state->frameno) |
| 365 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 366 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 367 | reg = &state->regs[i]; |
| 368 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 369 | if (t == NOT_INIT) |
| 370 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 371 | verbose(env, " R%d", i); |
| 372 | print_liveness(env, reg->live); |
| 373 | verbose(env, "=%s", reg_type_str[t]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 374 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 375 | tnum_is_const(reg->var_off)) { |
| 376 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 377 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 378 | if (t == PTR_TO_STACK) |
| 379 | verbose(env, ",call_%d", func(env, reg)->callsite); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 380 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 381 | verbose(env, "(id=%d", reg->id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 382 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 383 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 384 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 385 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 386 | else if (t == CONST_PTR_TO_MAP || |
| 387 | t == PTR_TO_MAP_VALUE || |
| 388 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 389 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 390 | reg->map_ptr->key_size, |
| 391 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 392 | if (tnum_is_const(reg->var_off)) { |
| 393 | /* Typically an immediate SCALAR_VALUE, but |
| 394 | * could be a pointer whose offset is too big |
| 395 | * for reg->off |
| 396 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 397 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 398 | } else { |
| 399 | if (reg->smin_value != reg->umin_value && |
| 400 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 401 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 402 | (long long)reg->smin_value); |
| 403 | if (reg->smax_value != reg->umax_value && |
| 404 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 405 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 406 | (long long)reg->smax_value); |
| 407 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 408 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 409 | (unsigned long long)reg->umin_value); |
| 410 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 411 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 412 | (unsigned long long)reg->umax_value); |
| 413 | if (!tnum_is_unknown(reg->var_off)) { |
| 414 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 415 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 416 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 417 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 418 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 419 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 420 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 421 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 422 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 423 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 424 | char types_buf[BPF_REG_SIZE + 1]; |
| 425 | bool valid = false; |
| 426 | int j; |
| 427 | |
| 428 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 429 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 430 | valid = true; |
| 431 | types_buf[j] = slot_type_char[ |
| 432 | state->stack[i].slot_type[j]]; |
| 433 | } |
| 434 | types_buf[BPF_REG_SIZE] = 0; |
| 435 | if (!valid) |
| 436 | continue; |
| 437 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 438 | print_liveness(env, state->stack[i].spilled_ptr.live); |
| 439 | if (state->stack[i].slot_type[0] == STACK_SPILL) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 440 | verbose(env, "=%s", |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 441 | reg_type_str[state->stack[i].spilled_ptr.type]); |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 442 | else |
| 443 | verbose(env, "=%s", types_buf); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 444 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 445 | if (state->acquired_refs && state->refs[0].id) { |
| 446 | verbose(env, " refs=%d", state->refs[0].id); |
| 447 | for (i = 1; i < state->acquired_refs; i++) |
| 448 | if (state->refs[i].id) |
| 449 | verbose(env, ",%d", state->refs[i].id); |
| 450 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 451 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 454 | #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 455 | static int copy_##NAME##_state(struct bpf_func_state *dst, \ |
| 456 | const struct bpf_func_state *src) \ |
| 457 | { \ |
| 458 | if (!src->FIELD) \ |
| 459 | return 0; \ |
| 460 | if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \ |
| 461 | /* internal bug, make state invalid to reject the program */ \ |
| 462 | memset(dst, 0, sizeof(*dst)); \ |
| 463 | return -EFAULT; \ |
| 464 | } \ |
| 465 | memcpy(dst->FIELD, src->FIELD, \ |
| 466 | sizeof(*src->FIELD) * (src->COUNT / SIZE)); \ |
| 467 | return 0; \ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 468 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 469 | /* copy_reference_state() */ |
| 470 | COPY_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 471 | /* copy_stack_state() */ |
| 472 | COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 473 | #undef COPY_STATE_FN |
| 474 | |
| 475 | #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \ |
| 476 | static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \ |
| 477 | bool copy_old) \ |
| 478 | { \ |
| 479 | u32 old_size = state->COUNT; \ |
| 480 | struct bpf_##NAME##_state *new_##FIELD; \ |
| 481 | int slot = size / SIZE; \ |
| 482 | \ |
| 483 | if (size <= old_size || !size) { \ |
| 484 | if (copy_old) \ |
| 485 | return 0; \ |
| 486 | state->COUNT = slot * SIZE; \ |
| 487 | if (!size && old_size) { \ |
| 488 | kfree(state->FIELD); \ |
| 489 | state->FIELD = NULL; \ |
| 490 | } \ |
| 491 | return 0; \ |
| 492 | } \ |
| 493 | new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \ |
| 494 | GFP_KERNEL); \ |
| 495 | if (!new_##FIELD) \ |
| 496 | return -ENOMEM; \ |
| 497 | if (copy_old) { \ |
| 498 | if (state->FIELD) \ |
| 499 | memcpy(new_##FIELD, state->FIELD, \ |
| 500 | sizeof(*new_##FIELD) * (old_size / SIZE)); \ |
| 501 | memset(new_##FIELD + old_size / SIZE, 0, \ |
| 502 | sizeof(*new_##FIELD) * (size - old_size) / SIZE); \ |
| 503 | } \ |
| 504 | state->COUNT = slot * SIZE; \ |
| 505 | kfree(state->FIELD); \ |
| 506 | state->FIELD = new_##FIELD; \ |
| 507 | return 0; \ |
| 508 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 509 | /* realloc_reference_state() */ |
| 510 | REALLOC_STATE_FN(reference, acquired_refs, refs, 1) |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 511 | /* realloc_stack_state() */ |
| 512 | REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE) |
| 513 | #undef REALLOC_STATE_FN |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 514 | |
| 515 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 516 | * make it consume minimal amount of memory. check_stack_write() access from |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 517 | * the program calls into realloc_func_state() to grow the stack size. |
Joe Stringer | 84dbf35 | 2018-10-02 13:35:34 -0700 | [diff] [blame] | 518 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 519 | * which realloc_stack_state() copies over. It points to previous |
| 520 | * bpf_verifier_state which is never reallocated. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 521 | */ |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 522 | static int realloc_func_state(struct bpf_func_state *state, int stack_size, |
| 523 | int refs_size, bool copy_old) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 524 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 525 | int err = realloc_reference_state(state, refs_size, copy_old); |
| 526 | if (err) |
| 527 | return err; |
| 528 | return realloc_stack_state(state, stack_size, copy_old); |
| 529 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 530 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 531 | /* Acquire a pointer id from the env and update the state->refs to include |
| 532 | * this new pointer reference. |
| 533 | * On success, returns a valid pointer id to associate with the register |
| 534 | * On failure, returns a negative errno. |
| 535 | */ |
| 536 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 537 | { |
| 538 | struct bpf_func_state *state = cur_func(env); |
| 539 | int new_ofs = state->acquired_refs; |
| 540 | int id, err; |
| 541 | |
| 542 | err = realloc_reference_state(state, state->acquired_refs + 1, true); |
| 543 | if (err) |
| 544 | return err; |
| 545 | id = ++env->id_gen; |
| 546 | state->refs[new_ofs].id = id; |
| 547 | state->refs[new_ofs].insn_idx = insn_idx; |
| 548 | |
| 549 | return id; |
| 550 | } |
| 551 | |
| 552 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
| 553 | static int __release_reference_state(struct bpf_func_state *state, int ptr_id) |
| 554 | { |
| 555 | int i, last_idx; |
| 556 | |
| 557 | if (!ptr_id) |
| 558 | return -EFAULT; |
| 559 | |
| 560 | last_idx = state->acquired_refs - 1; |
| 561 | for (i = 0; i < state->acquired_refs; i++) { |
| 562 | if (state->refs[i].id == ptr_id) { |
| 563 | if (last_idx && i != last_idx) |
| 564 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 565 | sizeof(*state->refs)); |
| 566 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 567 | state->acquired_refs--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 568 | return 0; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 569 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 570 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 571 | return -EFAULT; |
| 572 | } |
| 573 | |
| 574 | /* variation on the above for cases where we expect that there must be an |
| 575 | * outstanding reference for the specified ptr_id. |
| 576 | */ |
| 577 | static int release_reference_state(struct bpf_verifier_env *env, int ptr_id) |
| 578 | { |
| 579 | struct bpf_func_state *state = cur_func(env); |
| 580 | int err; |
| 581 | |
| 582 | err = __release_reference_state(state, ptr_id); |
| 583 | if (WARN_ON_ONCE(err != 0)) |
| 584 | verbose(env, "verifier internal error: can't release reference\n"); |
| 585 | return err; |
| 586 | } |
| 587 | |
| 588 | static int transfer_reference_state(struct bpf_func_state *dst, |
| 589 | struct bpf_func_state *src) |
| 590 | { |
| 591 | int err = realloc_reference_state(dst, src->acquired_refs, false); |
| 592 | if (err) |
| 593 | return err; |
| 594 | err = copy_reference_state(dst, src); |
| 595 | if (err) |
| 596 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 600 | static void free_func_state(struct bpf_func_state *state) |
| 601 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 602 | if (!state) |
| 603 | return; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 604 | kfree(state->refs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 605 | kfree(state->stack); |
| 606 | kfree(state); |
| 607 | } |
| 608 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 609 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 610 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 611 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 612 | int i; |
| 613 | |
| 614 | for (i = 0; i <= state->curframe; i++) { |
| 615 | free_func_state(state->frame[i]); |
| 616 | state->frame[i] = NULL; |
| 617 | } |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 618 | if (free_self) |
| 619 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* copy verifier state from src to dst growing dst stack space |
| 623 | * when necessary to accommodate larger src stack |
| 624 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 625 | static int copy_func_state(struct bpf_func_state *dst, |
| 626 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 627 | { |
| 628 | int err; |
| 629 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 630 | err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs, |
| 631 | false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 632 | if (err) |
| 633 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 634 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 635 | err = copy_reference_state(dst, src); |
| 636 | if (err) |
| 637 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 638 | return copy_stack_state(dst, src); |
| 639 | } |
| 640 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 641 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 642 | const struct bpf_verifier_state *src) |
| 643 | { |
| 644 | struct bpf_func_state *dst; |
| 645 | int i, err; |
| 646 | |
| 647 | /* if dst has more stack frames then src frame, free them */ |
| 648 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 649 | free_func_state(dst_state->frame[i]); |
| 650 | dst_state->frame[i] = NULL; |
| 651 | } |
| 652 | dst_state->curframe = src->curframe; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 653 | for (i = 0; i <= src->curframe; i++) { |
| 654 | dst = dst_state->frame[i]; |
| 655 | if (!dst) { |
| 656 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 657 | if (!dst) |
| 658 | return -ENOMEM; |
| 659 | dst_state->frame[i] = dst; |
| 660 | } |
| 661 | err = copy_func_state(dst, src->frame[i]); |
| 662 | if (err) |
| 663 | return err; |
| 664 | } |
| 665 | return 0; |
| 666 | } |
| 667 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 668 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
| 669 | int *insn_idx) |
| 670 | { |
| 671 | struct bpf_verifier_state *cur = env->cur_state; |
| 672 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 673 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 674 | |
| 675 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 676 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 677 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 678 | if (cur) { |
| 679 | err = copy_verifier_state(cur, &head->st); |
| 680 | if (err) |
| 681 | return err; |
| 682 | } |
| 683 | if (insn_idx) |
| 684 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 685 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 686 | *prev_insn_idx = head->prev_insn_idx; |
| 687 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 688 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 689 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 690 | env->head = elem; |
| 691 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 692 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 695 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
| 696 | int insn_idx, int prev_insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 697 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 698 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 699 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 700 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 701 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 702 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 703 | if (!elem) |
| 704 | goto err; |
| 705 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 706 | elem->insn_idx = insn_idx; |
| 707 | elem->prev_insn_idx = prev_insn_idx; |
| 708 | elem->next = env->head; |
| 709 | env->head = elem; |
| 710 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 711 | err = copy_verifier_state(&elem->st, cur); |
| 712 | if (err) |
| 713 | goto err; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 714 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 715 | verbose(env, "BPF program is too complex\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 716 | goto err; |
| 717 | } |
| 718 | return &elem->st; |
| 719 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 720 | free_verifier_state(env->cur_state, true); |
| 721 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 722 | /* pop all elements and return */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 723 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 724 | return NULL; |
| 725 | } |
| 726 | |
| 727 | #define CALLER_SAVED_REGS 6 |
| 728 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 729 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 730 | }; |
| 731 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 732 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
| 733 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 734 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 735 | * known to have the value @imm. |
| 736 | */ |
| 737 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 738 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 739 | /* Clear id, off, and union(map_ptr, range) */ |
| 740 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 741 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 742 | reg->var_off = tnum_const(imm); |
| 743 | reg->smin_value = (s64)imm; |
| 744 | reg->smax_value = (s64)imm; |
| 745 | reg->umin_value = imm; |
| 746 | reg->umax_value = imm; |
| 747 | } |
| 748 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 749 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 750 | * used only on registers holding a pointer type. |
| 751 | */ |
| 752 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 753 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 754 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 755 | } |
| 756 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 757 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 758 | { |
| 759 | __mark_reg_known(reg, 0); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 760 | reg->type = SCALAR_VALUE; |
| 761 | } |
| 762 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 763 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 764 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 765 | { |
| 766 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 767 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 768 | /* Something bad happened, let's kill all regs */ |
| 769 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 770 | __mark_reg_not_init(regs + regno); |
| 771 | return; |
| 772 | } |
| 773 | __mark_reg_known_zero(regs + regno); |
| 774 | } |
| 775 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 776 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 777 | { |
| 778 | return type_is_pkt_pointer(reg->type); |
| 779 | } |
| 780 | |
| 781 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 782 | { |
| 783 | return reg_is_pkt_pointer(reg) || |
| 784 | reg->type == PTR_TO_PACKET_END; |
| 785 | } |
| 786 | |
| 787 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 788 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 789 | enum bpf_reg_type which) |
| 790 | { |
| 791 | /* The register can already have a range from prior markings. |
| 792 | * This is fine as long as it hasn't been advanced from its |
| 793 | * origin. |
| 794 | */ |
| 795 | return reg->type == which && |
| 796 | reg->id == 0 && |
| 797 | reg->off == 0 && |
| 798 | tnum_equals_const(reg->var_off, 0); |
| 799 | } |
| 800 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 801 | /* Attempts to improve min/max values based on var_off information */ |
| 802 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 803 | { |
| 804 | /* min signed is max(sign bit) | min(other bits) */ |
| 805 | reg->smin_value = max_t(s64, reg->smin_value, |
| 806 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 807 | /* max signed is min(sign bit) | max(other bits) */ |
| 808 | reg->smax_value = min_t(s64, reg->smax_value, |
| 809 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 810 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 811 | reg->umax_value = min(reg->umax_value, |
| 812 | reg->var_off.value | reg->var_off.mask); |
| 813 | } |
| 814 | |
| 815 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
| 816 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 817 | { |
| 818 | /* Learn sign from signed bounds. |
| 819 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 820 | * are the same, so combine. This works even in the negative case, e.g. |
| 821 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 822 | */ |
| 823 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 824 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 825 | reg->umin_value); |
| 826 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 827 | reg->umax_value); |
| 828 | return; |
| 829 | } |
| 830 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 831 | * boundary, so we must be careful. |
| 832 | */ |
| 833 | if ((s64)reg->umax_value >= 0) { |
| 834 | /* Positive. We can't learn anything from the smin, but smax |
| 835 | * is positive, hence safe. |
| 836 | */ |
| 837 | reg->smin_value = reg->umin_value; |
| 838 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 839 | reg->umax_value); |
| 840 | } else if ((s64)reg->umin_value < 0) { |
| 841 | /* Negative. We can't learn anything from the smax, but smin |
| 842 | * is negative, hence safe. |
| 843 | */ |
| 844 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 845 | reg->umin_value); |
| 846 | reg->smax_value = reg->umax_value; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 851 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 852 | { |
| 853 | reg->var_off = tnum_intersect(reg->var_off, |
| 854 | tnum_range(reg->umin_value, |
| 855 | reg->umax_value)); |
| 856 | } |
| 857 | |
| 858 | /* Reset the min/max bounds of a register */ |
| 859 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 860 | { |
| 861 | reg->smin_value = S64_MIN; |
| 862 | reg->smax_value = S64_MAX; |
| 863 | reg->umin_value = 0; |
| 864 | reg->umax_value = U64_MAX; |
| 865 | } |
| 866 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 867 | /* Mark a register as having a completely unknown (scalar) value. */ |
| 868 | static void __mark_reg_unknown(struct bpf_reg_state *reg) |
| 869 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 870 | /* |
| 871 | * Clear type, id, off, and union(map_ptr, range) and |
| 872 | * padding between 'type' and union |
| 873 | */ |
| 874 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 875 | reg->type = SCALAR_VALUE; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 876 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 877 | reg->frameno = 0; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 878 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 879 | } |
| 880 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 881 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 882 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 883 | { |
| 884 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 885 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 886 | /* Something bad happened, let's kill all regs except FP */ |
| 887 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 888 | __mark_reg_not_init(regs + regno); |
| 889 | return; |
| 890 | } |
| 891 | __mark_reg_unknown(regs + regno); |
| 892 | } |
| 893 | |
| 894 | static void __mark_reg_not_init(struct bpf_reg_state *reg) |
| 895 | { |
| 896 | __mark_reg_unknown(reg); |
| 897 | reg->type = NOT_INIT; |
| 898 | } |
| 899 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 900 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 901 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 902 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 903 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 904 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 905 | /* Something bad happened, let's kill all regs except FP */ |
| 906 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 907 | __mark_reg_not_init(regs + regno); |
| 908 | return; |
| 909 | } |
| 910 | __mark_reg_not_init(regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 911 | } |
| 912 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 913 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 914 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 915 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 916 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 917 | int i; |
| 918 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 919 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 920 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 921 | regs[i].live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 922 | regs[i].parent = NULL; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 923 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 924 | |
| 925 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 926 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 927 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 928 | regs[BPF_REG_FP].frameno = state->frameno; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 929 | |
| 930 | /* 1st arg to a function */ |
| 931 | regs[BPF_REG_1].type = PTR_TO_CTX; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 932 | mark_reg_known_zero(env, regs, BPF_REG_1); |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 933 | } |
| 934 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 935 | #define BPF_MAIN_FUNC (-1) |
| 936 | static void init_func_state(struct bpf_verifier_env *env, |
| 937 | struct bpf_func_state *state, |
| 938 | int callsite, int frameno, int subprogno) |
| 939 | { |
| 940 | state->callsite = callsite; |
| 941 | state->frameno = frameno; |
| 942 | state->subprogno = subprogno; |
| 943 | init_reg_state(env, state); |
| 944 | } |
| 945 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 946 | enum reg_arg_type { |
| 947 | SRC_OP, /* register is used as source operand */ |
| 948 | DST_OP, /* register is used as destination operand */ |
| 949 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 950 | }; |
| 951 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 952 | static int cmp_subprogs(const void *a, const void *b) |
| 953 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 954 | return ((struct bpf_subprog_info *)a)->start - |
| 955 | ((struct bpf_subprog_info *)b)->start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 959 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 960 | struct bpf_subprog_info *p; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 961 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 962 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 963 | sizeof(env->subprog_info[0]), cmp_subprogs); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 964 | if (!p) |
| 965 | return -ENOENT; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 966 | return p - env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 967 | |
| 968 | } |
| 969 | |
| 970 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 971 | { |
| 972 | int insn_cnt = env->prog->len; |
| 973 | int ret; |
| 974 | |
| 975 | if (off >= insn_cnt || off < 0) { |
| 976 | verbose(env, "call to invalid destination\n"); |
| 977 | return -EINVAL; |
| 978 | } |
| 979 | ret = find_subprog(env, off); |
| 980 | if (ret >= 0) |
| 981 | return 0; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 982 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 983 | verbose(env, "too many subprograms\n"); |
| 984 | return -E2BIG; |
| 985 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 986 | env->subprog_info[env->subprog_cnt++].start = off; |
| 987 | sort(env->subprog_info, env->subprog_cnt, |
| 988 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | static int check_subprogs(struct bpf_verifier_env *env) |
| 993 | { |
| 994 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 995 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 996 | struct bpf_insn *insn = env->prog->insnsi; |
| 997 | int insn_cnt = env->prog->len; |
| 998 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 999 | /* Add entry function. */ |
| 1000 | ret = add_subprog(env, 0); |
| 1001 | if (ret < 0) |
| 1002 | return ret; |
| 1003 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1004 | /* determine subprog starts. The end is one before the next starts */ |
| 1005 | for (i = 0; i < insn_cnt; i++) { |
| 1006 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1007 | continue; |
| 1008 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1009 | continue; |
| 1010 | if (!env->allow_ptr_leaks) { |
| 1011 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); |
| 1012 | return -EPERM; |
| 1013 | } |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1014 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 1015 | if (ret < 0) |
| 1016 | return ret; |
| 1017 | } |
| 1018 | |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1019 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 1020 | * logic. 'subprog_cnt' should not be increased. |
| 1021 | */ |
| 1022 | subprog[env->subprog_cnt].start = insn_cnt; |
| 1023 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1024 | if (env->log.level > 1) |
| 1025 | for (i = 0; i < env->subprog_cnt; i++) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1026 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1027 | |
| 1028 | /* now check that all jumps are within the same subprog */ |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1029 | subprog_start = subprog[cur_subprog].start; |
| 1030 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1031 | for (i = 0; i < insn_cnt; i++) { |
| 1032 | u8 code = insn[i].code; |
| 1033 | |
| 1034 | if (BPF_CLASS(code) != BPF_JMP) |
| 1035 | goto next; |
| 1036 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 1037 | goto next; |
| 1038 | off = i + insn[i].off + 1; |
| 1039 | if (off < subprog_start || off >= subprog_end) { |
| 1040 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 1041 | return -EINVAL; |
| 1042 | } |
| 1043 | next: |
| 1044 | if (i == subprog_end - 1) { |
| 1045 | /* to avoid fall-through from one subprog into another |
| 1046 | * the last insn of the subprog should be either exit |
| 1047 | * or unconditional jump back |
| 1048 | */ |
| 1049 | if (code != (BPF_JMP | BPF_EXIT) && |
| 1050 | code != (BPF_JMP | BPF_JA)) { |
| 1051 | verbose(env, "last insn is not an exit or jmp\n"); |
| 1052 | return -EINVAL; |
| 1053 | } |
| 1054 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1055 | cur_subprog++; |
| 1056 | if (cur_subprog < env->subprog_cnt) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1057 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1063 | /* Parentage chain of this register (or stack slot) should take care of all |
| 1064 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 1065 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1066 | static int mark_reg_read(struct bpf_verifier_env *env, |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1067 | const struct bpf_reg_state *state, |
| 1068 | struct bpf_reg_state *parent) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1069 | { |
| 1070 | bool writes = parent == state->parent; /* Observe write marks */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1071 | |
| 1072 | while (parent) { |
| 1073 | /* if read wasn't screened by an earlier write ... */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1074 | if (writes && state->live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1075 | break; |
| 1076 | /* ... then we depend on parent's value */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1077 | parent->live |= REG_LIVE_READ; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1078 | state = parent; |
| 1079 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1080 | writes = true; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1081 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1082 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1086 | enum reg_arg_type t) |
| 1087 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1088 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1089 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 1090 | struct bpf_reg_state *regs = state->regs; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1091 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1092 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1093 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1094 | return -EINVAL; |
| 1095 | } |
| 1096 | |
| 1097 | if (t == SRC_OP) { |
| 1098 | /* check whether register used as source operand can be read */ |
| 1099 | if (regs[regno].type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1100 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1101 | return -EACCES; |
| 1102 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1103 | /* We don't need to worry about FP liveness because it's read-only */ |
| 1104 | if (regno != BPF_REG_FP) |
| 1105 | return mark_reg_read(env, ®s[regno], |
| 1106 | regs[regno].parent); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1107 | } else { |
| 1108 | /* check whether register used as dest operand can be written to */ |
| 1109 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1110 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1111 | return -EACCES; |
| 1112 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1113 | regs[regno].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1114 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1115 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1116 | } |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1120 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 1121 | { |
| 1122 | switch (type) { |
| 1123 | case PTR_TO_MAP_VALUE: |
| 1124 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 1125 | case PTR_TO_STACK: |
| 1126 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1127 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1128 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1129 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1130 | case PTR_TO_FLOW_KEYS: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1131 | case CONST_PTR_TO_MAP: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1132 | case PTR_TO_SOCKET: |
| 1133 | case PTR_TO_SOCKET_OR_NULL: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1134 | return true; |
| 1135 | default: |
| 1136 | return false; |
| 1137 | } |
| 1138 | } |
| 1139 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1140 | /* Does this register contain a constant zero? */ |
| 1141 | static bool register_is_null(struct bpf_reg_state *reg) |
| 1142 | { |
| 1143 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 1144 | } |
| 1145 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1146 | /* check_stack_read/write functions track spill/fill of registers, |
| 1147 | * stack boundary and alignment are checked in check_mem_access() |
| 1148 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1149 | static int check_stack_write(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1150 | struct bpf_func_state *state, /* func where register points to */ |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1151 | int off, int size, int value_regno, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1152 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1153 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1154 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1155 | enum bpf_reg_type type; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1156 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1157 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 1158 | state->acquired_refs, true); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1159 | if (err) |
| 1160 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1161 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 1162 | * so it's aligned access and [off, off + size) are within stack limits |
| 1163 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1164 | if (!env->allow_ptr_leaks && |
| 1165 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 1166 | size != BPF_REG_SIZE) { |
| 1167 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 1168 | return -EACCES; |
| 1169 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1170 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1171 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1172 | if (value_regno >= 0 && |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1173 | is_spillable_regtype((type = cur->regs[value_regno].type))) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1174 | |
| 1175 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1176 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1177 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1178 | return -EACCES; |
| 1179 | } |
| 1180 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1181 | if (state != cur && type == PTR_TO_STACK) { |
| 1182 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 1183 | return -EINVAL; |
| 1184 | } |
| 1185 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1186 | /* save register state */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1187 | state->stack[spi].spilled_ptr = cur->regs[value_regno]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1188 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1189 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1190 | for (i = 0; i < BPF_REG_SIZE; i++) { |
| 1191 | if (state->stack[spi].slot_type[i] == STACK_MISC && |
| 1192 | !env->allow_ptr_leaks) { |
| 1193 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
| 1194 | int soff = (-spi - 1) * BPF_REG_SIZE; |
| 1195 | |
| 1196 | /* detected reuse of integer stack slot with a pointer |
| 1197 | * which means either llvm is reusing stack slot or |
| 1198 | * an attacker is trying to exploit CVE-2018-3639 |
| 1199 | * (speculative store bypass) |
| 1200 | * Have to sanitize that slot with preemptive |
| 1201 | * store of zero. |
| 1202 | */ |
| 1203 | if (*poff && *poff != soff) { |
| 1204 | /* disallow programs where single insn stores |
| 1205 | * into two different stack slots, since verifier |
| 1206 | * cannot sanitize them |
| 1207 | */ |
| 1208 | verbose(env, |
| 1209 | "insn %d cannot access two stack slots fp%d and fp%d", |
| 1210 | insn_idx, *poff, soff); |
| 1211 | return -EINVAL; |
| 1212 | } |
| 1213 | *poff = soff; |
| 1214 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1215 | state->stack[spi].slot_type[i] = STACK_SPILL; |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1216 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1217 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1218 | u8 type = STACK_MISC; |
| 1219 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1220 | /* regular write of data into stack destroys any spilled ptr */ |
| 1221 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1222 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1223 | /* only mark the slot as written if all 8 bytes were written |
| 1224 | * otherwise read propagation may incorrectly stop too soon |
| 1225 | * when stack slots are partially written. |
| 1226 | * This heuristic means that read propagation will be |
| 1227 | * conservative, since it will add reg_live_read marks |
| 1228 | * to stack slots all the way to first state when programs |
| 1229 | * writes+reads less than 8 bytes |
| 1230 | */ |
| 1231 | if (size == BPF_REG_SIZE) |
| 1232 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 1233 | |
| 1234 | /* when we zero initialize stack slots mark them as such */ |
| 1235 | if (value_regno >= 0 && |
| 1236 | register_is_null(&cur->regs[value_regno])) |
| 1237 | type = STACK_ZERO; |
| 1238 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1239 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1240 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1241 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1242 | } |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1246 | static int check_stack_read(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1247 | struct bpf_func_state *reg_state /* func where register points to */, |
| 1248 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1249 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1250 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1251 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1252 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
| 1253 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1254 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1255 | if (reg_state->allocated_stack <= slot) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1256 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 1257 | off, size); |
| 1258 | return -EACCES; |
| 1259 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1260 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1261 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1262 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1263 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1264 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1265 | return -EACCES; |
| 1266 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1267 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1268 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1269 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1270 | return -EACCES; |
| 1271 | } |
| 1272 | } |
| 1273 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1274 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1275 | /* restore register state from stack */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1276 | state->regs[value_regno] = reg_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 1277 | /* mark reg as written since spilled pointer state likely |
| 1278 | * has its liveness marks cleared by is_state_visited() |
| 1279 | * which resets stack/reg liveness for state transitions |
| 1280 | */ |
| 1281 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1282 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1283 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
| 1284 | reg_state->stack[spi].spilled_ptr.parent); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1285 | return 0; |
| 1286 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1287 | int zeros = 0; |
| 1288 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1289 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1290 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 1291 | continue; |
| 1292 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 1293 | zeros++; |
| 1294 | continue; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1295 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1296 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 1297 | off, i, size); |
| 1298 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1299 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1300 | mark_reg_read(env, ®_state->stack[spi].spilled_ptr, |
| 1301 | reg_state->stack[spi].spilled_ptr.parent); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1302 | if (value_regno >= 0) { |
| 1303 | if (zeros == size) { |
| 1304 | /* any size read into register is zero extended, |
| 1305 | * so the whole register == const_zero |
| 1306 | */ |
| 1307 | __mark_reg_const_zero(&state->regs[value_regno]); |
| 1308 | } else { |
| 1309 | /* have read misc data from the stack */ |
| 1310 | mark_reg_unknown(env, state->regs, value_regno); |
| 1311 | } |
| 1312 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 1313 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1314 | return 0; |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1319 | 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] | 1320 | int size, bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1321 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1322 | struct bpf_reg_state *regs = cur_regs(env); |
| 1323 | struct bpf_map *map = regs[regno].map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1324 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1325 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1326 | off + size > map->value_size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1327 | 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] | 1328 | map->value_size, off, size); |
| 1329 | return -EACCES; |
| 1330 | } |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1334 | /* check read/write into a map element with possible variable offset */ |
| 1335 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1336 | int off, int size, bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1337 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1338 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1339 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1340 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 1341 | int err; |
| 1342 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1343 | /* We may have adjusted the register to this map value, so we |
| 1344 | * need to try adding each of min_value and max_value to off |
| 1345 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1346 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1347 | if (env->log.level) |
| 1348 | print_verifier_state(env, state); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1349 | /* The minimum value is only important with signed |
| 1350 | * comparisons where we can't assume the floor of a |
| 1351 | * value is 0. If we are using signed variables for our |
| 1352 | * index'es we need to make sure that whatever we use |
| 1353 | * will have a set floor within our range. |
| 1354 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1355 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1356 | 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] | 1357 | regno); |
| 1358 | return -EACCES; |
| 1359 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1360 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
| 1361 | zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1362 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1363 | verbose(env, "R%d min value is outside of the array range\n", |
| 1364 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1365 | return err; |
| 1366 | } |
| 1367 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1368 | /* If we haven't set a max value then we need to bail since we can't be |
| 1369 | * sure we won't do bad things. |
| 1370 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1371 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1372 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1373 | 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] | 1374 | regno); |
| 1375 | return -EACCES; |
| 1376 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1377 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
| 1378 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1379 | if (err) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1380 | verbose(env, "R%d max value is outside of the array range\n", |
| 1381 | regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1382 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1383 | } |
| 1384 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1385 | #define MAX_PACKET_OFF 0xffff |
| 1386 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1387 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1388 | const struct bpf_call_arg_meta *meta, |
| 1389 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1390 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1391 | switch (env->prog->type) { |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1392 | /* Program types only with direct read access go here! */ |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1393 | case BPF_PROG_TYPE_LWT_IN: |
| 1394 | case BPF_PROG_TYPE_LWT_OUT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 1395 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 1396 | case BPF_PROG_TYPE_SK_REUSEPORT: |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1397 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Daniel Borkmann | d5563d3 | 2018-10-24 22:05:46 +0200 | [diff] [blame] | 1398 | case BPF_PROG_TYPE_CGROUP_SKB: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1399 | if (t == BPF_WRITE) |
| 1400 | return false; |
Alexander Alemayhu | 7e57fbb | 2017-02-14 00:02:35 +0100 | [diff] [blame] | 1401 | /* fallthrough */ |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 1402 | |
| 1403 | /* Program types with direct read + write access go here! */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1404 | case BPF_PROG_TYPE_SCHED_CLS: |
| 1405 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1406 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1407 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 1408 | case BPF_PROG_TYPE_SK_SKB: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1409 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1410 | if (meta) |
| 1411 | return meta->pkt_access; |
| 1412 | |
| 1413 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1414 | return true; |
| 1415 | default: |
| 1416 | return false; |
| 1417 | } |
| 1418 | } |
| 1419 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1420 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1421 | int off, int size, bool zero_size_allowed) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1422 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1423 | struct bpf_reg_state *regs = cur_regs(env); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1424 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1425 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1426 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1427 | (u64)off + size > reg->range) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1428 | 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] | 1429 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1430 | return -EACCES; |
| 1431 | } |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1435 | 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] | 1436 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1437 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1438 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1439 | struct bpf_reg_state *reg = ®s[regno]; |
| 1440 | int err; |
| 1441 | |
| 1442 | /* We may have added a variable offset to the packet pointer; but any |
| 1443 | * reg->range we have comes after that. We are only checking the fixed |
| 1444 | * offset. |
| 1445 | */ |
| 1446 | |
| 1447 | /* We don't allow negative numbers, because we aren't tracking enough |
| 1448 | * detail to prove they're safe. |
| 1449 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1450 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1451 | 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] | 1452 | regno); |
| 1453 | return -EACCES; |
| 1454 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1455 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1456 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1457 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1458 | return err; |
| 1459 | } |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 1460 | |
| 1461 | /* __check_packet_access has made sure "off + size - 1" is within u16. |
| 1462 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 1463 | * otherwise find_good_pkt_pointers would have refused to set range info |
| 1464 | * that __check_packet_access would have rejected this pkt access. |
| 1465 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 1466 | */ |
| 1467 | env->prog->aux->max_pkt_offset = |
| 1468 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 1469 | off + reg->umax_value + size - 1); |
| 1470 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1471 | return err; |
| 1472 | } |
| 1473 | |
| 1474 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1475 | 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] | 1476 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1477 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1478 | struct bpf_insn_access_aux info = { |
| 1479 | .reg_type = *reg_type, |
| 1480 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1481 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1482 | if (env->ops->is_valid_access && |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1483 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1484 | /* A non zero info.ctx_field_size indicates that this field is a |
| 1485 | * candidate for later verifier transformation to load the whole |
| 1486 | * field and then apply a mask when accessed with a narrower |
| 1487 | * access than actual ctx access size. A zero info.ctx_field_size |
| 1488 | * will only allow for whole field access and rejects any other |
| 1489 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1490 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 1491 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1492 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1493 | 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] | 1494 | /* remember the offset of last byte accessed in ctx */ |
| 1495 | if (env->prog->aux->max_ctx_offset < off + size) |
| 1496 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1497 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 1498 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1499 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1500 | 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] | 1501 | return -EACCES; |
| 1502 | } |
| 1503 | |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1504 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 1505 | int size) |
| 1506 | { |
| 1507 | if (size < 0 || off < 0 || |
| 1508 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 1509 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 1510 | off, size); |
| 1511 | return -EACCES; |
| 1512 | } |
| 1513 | return 0; |
| 1514 | } |
| 1515 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1516 | static int check_sock_access(struct bpf_verifier_env *env, u32 regno, int off, |
| 1517 | int size, enum bpf_access_type t) |
| 1518 | { |
| 1519 | struct bpf_reg_state *regs = cur_regs(env); |
| 1520 | struct bpf_reg_state *reg = ®s[regno]; |
| 1521 | struct bpf_insn_access_aux info; |
| 1522 | |
| 1523 | if (reg->smin_value < 0) { |
| 1524 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 1525 | regno); |
| 1526 | return -EACCES; |
| 1527 | } |
| 1528 | |
| 1529 | if (!bpf_sock_is_valid_access(off, size, t, &info)) { |
| 1530 | verbose(env, "invalid bpf_sock access off=%d size=%d\n", |
| 1531 | off, size); |
| 1532 | return -EACCES; |
| 1533 | } |
| 1534 | |
| 1535 | return 0; |
| 1536 | } |
| 1537 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1538 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 1539 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1540 | { |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1541 | if (allow_ptr_leaks) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1542 | return false; |
| 1543 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1544 | return reg->type != SCALAR_VALUE; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1547 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
| 1548 | { |
| 1549 | return cur_regs(env) + regno; |
| 1550 | } |
| 1551 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1552 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 1553 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1554 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1555 | } |
| 1556 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1557 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 1558 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1559 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1560 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 1561 | return reg->type == PTR_TO_CTX || |
| 1562 | reg->type == PTR_TO_SOCKET; |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1563 | } |
| 1564 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1565 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 1566 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 1567 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1568 | |
| 1569 | return type_is_pkt_pointer(reg->type); |
| 1570 | } |
| 1571 | |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 1572 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 1573 | { |
| 1574 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 1575 | |
| 1576 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 1577 | return reg->type == PTR_TO_FLOW_KEYS; |
| 1578 | } |
| 1579 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1580 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 1581 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1582 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1583 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1584 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1585 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1586 | |
| 1587 | /* Byte size accesses are always allowed. */ |
| 1588 | if (!strict || size == 1) |
| 1589 | return 0; |
| 1590 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1591 | /* For platforms that do not have a Kconfig enabling |
| 1592 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 1593 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 1594 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 1595 | * to this code only in strict mode where we want to emulate |
| 1596 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 1597 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1598 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1599 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1600 | |
| 1601 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 1602 | if (!tnum_is_aligned(reg_off, size)) { |
| 1603 | char tn_buf[48]; |
| 1604 | |
| 1605 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1606 | verbose(env, |
| 1607 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1608 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1609 | return -EACCES; |
| 1610 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1611 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1612 | return 0; |
| 1613 | } |
| 1614 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1615 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 1616 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1617 | const char *pointer_desc, |
| 1618 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1619 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1620 | struct tnum reg_off; |
| 1621 | |
| 1622 | /* Byte size accesses are always allowed. */ |
| 1623 | if (!strict || size == 1) |
| 1624 | return 0; |
| 1625 | |
| 1626 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 1627 | if (!tnum_is_aligned(reg_off, size)) { |
| 1628 | char tn_buf[48]; |
| 1629 | |
| 1630 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1631 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1632 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1633 | return -EACCES; |
| 1634 | } |
| 1635 | |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1639 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1640 | const struct bpf_reg_state *reg, int off, |
| 1641 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1642 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1643 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1644 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1645 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1646 | switch (reg->type) { |
| 1647 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1648 | case PTR_TO_PACKET_META: |
| 1649 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 1650 | * right in front, treat it the very same way. |
| 1651 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1652 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1653 | case PTR_TO_FLOW_KEYS: |
| 1654 | pointer_desc = "flow keys "; |
| 1655 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1656 | case PTR_TO_MAP_VALUE: |
| 1657 | pointer_desc = "value "; |
| 1658 | break; |
| 1659 | case PTR_TO_CTX: |
| 1660 | pointer_desc = "context "; |
| 1661 | break; |
| 1662 | case PTR_TO_STACK: |
| 1663 | pointer_desc = "stack "; |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 1664 | /* The stack spill tracking logic in check_stack_write() |
| 1665 | * and check_stack_read() relies on stack accesses being |
| 1666 | * aligned. |
| 1667 | */ |
| 1668 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1669 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1670 | case PTR_TO_SOCKET: |
| 1671 | pointer_desc = "sock "; |
| 1672 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1673 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1674 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1675 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1676 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 1677 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1678 | } |
| 1679 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1680 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 1681 | const struct bpf_func_state *func, |
| 1682 | int off) |
| 1683 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1684 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1685 | |
| 1686 | if (stack >= -off) |
| 1687 | return 0; |
| 1688 | |
| 1689 | /* update known max for given subprogram */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1690 | env->subprog_info[func->subprogno].stack_depth = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1691 | return 0; |
| 1692 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1693 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1694 | /* starting from main bpf function walk all instructions of the function |
| 1695 | * and recursively walk all callees that given function can call. |
| 1696 | * Ignore jump and exit insns. |
| 1697 | * Since recursion is prevented by check_cfg() this algorithm |
| 1698 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 1699 | */ |
| 1700 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 1701 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1702 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 1703 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1704 | struct bpf_insn *insn = env->prog->insnsi; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1705 | int ret_insn[MAX_CALL_FRAMES]; |
| 1706 | int ret_prog[MAX_CALL_FRAMES]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1707 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1708 | process_func: |
| 1709 | /* round up to 32-bytes, since this is granularity |
| 1710 | * of interpreter stack size |
| 1711 | */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1712 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1713 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1714 | 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] | 1715 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1716 | return -EACCES; |
| 1717 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1718 | continue_func: |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1719 | subprog_end = subprog[idx + 1].start; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1720 | for (; i < subprog_end; i++) { |
| 1721 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1722 | continue; |
| 1723 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1724 | continue; |
| 1725 | /* remember insn and function to return to */ |
| 1726 | ret_insn[frame] = i + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1727 | ret_prog[frame] = idx; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1728 | |
| 1729 | /* find the callee */ |
| 1730 | i = i + insn[i].imm + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1731 | idx = find_subprog(env, i); |
| 1732 | if (idx < 0) { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1733 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1734 | i); |
| 1735 | return -EFAULT; |
| 1736 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1737 | frame++; |
| 1738 | if (frame >= MAX_CALL_FRAMES) { |
| 1739 | WARN_ONCE(1, "verifier bug. Call stack is too deep\n"); |
| 1740 | return -EFAULT; |
| 1741 | } |
| 1742 | goto process_func; |
| 1743 | } |
| 1744 | /* end of for() loop means the last insn of the 'subprog' |
| 1745 | * was reached. Doesn't matter whether it was JA or EXIT |
| 1746 | */ |
| 1747 | if (frame == 0) |
| 1748 | return 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1749 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1750 | frame--; |
| 1751 | i = ret_insn[frame]; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1752 | idx = ret_prog[frame]; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1753 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1754 | } |
| 1755 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1756 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1757 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 1758 | const struct bpf_insn *insn, int idx) |
| 1759 | { |
| 1760 | int start = idx + insn->imm + 1, subprog; |
| 1761 | |
| 1762 | subprog = find_subprog(env, start); |
| 1763 | if (subprog < 0) { |
| 1764 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1765 | start); |
| 1766 | return -EFAULT; |
| 1767 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1768 | return env->subprog_info[subprog].stack_depth; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1769 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1770 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1771 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 1772 | static int check_ctx_reg(struct bpf_verifier_env *env, |
| 1773 | const struct bpf_reg_state *reg, int regno) |
| 1774 | { |
| 1775 | /* Access to ctx or passing it to a helper is only allowed in |
| 1776 | * its original, unmodified form. |
| 1777 | */ |
| 1778 | |
| 1779 | if (reg->off) { |
| 1780 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 1781 | regno, reg->off); |
| 1782 | return -EACCES; |
| 1783 | } |
| 1784 | |
| 1785 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 1786 | char tn_buf[48]; |
| 1787 | |
| 1788 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 1789 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 1790 | return -EACCES; |
| 1791 | } |
| 1792 | |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 1796 | /* truncate register to smaller size (in bytes) |
| 1797 | * must be called with size < BPF_REG_SIZE |
| 1798 | */ |
| 1799 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 1800 | { |
| 1801 | u64 mask; |
| 1802 | |
| 1803 | /* clear high bits in bit representation */ |
| 1804 | reg->var_off = tnum_cast(reg->var_off, size); |
| 1805 | |
| 1806 | /* fix arithmetic bounds */ |
| 1807 | mask = ((u64)1 << (size * 8)) - 1; |
| 1808 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 1809 | reg->umin_value &= mask; |
| 1810 | reg->umax_value &= mask; |
| 1811 | } else { |
| 1812 | reg->umin_value = 0; |
| 1813 | reg->umax_value = mask; |
| 1814 | } |
| 1815 | reg->smin_value = reg->umin_value; |
| 1816 | reg->smax_value = reg->umax_value; |
| 1817 | } |
| 1818 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1819 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 1820 | * if t==write, value_regno is a register which value is stored into memory |
| 1821 | * if t==read, value_regno is a register which will receive the value from memory |
| 1822 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 1823 | * if t==read && value_regno==-1, don't care what we read from memory |
| 1824 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1825 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 1826 | int off, int bpf_size, enum bpf_access_type t, |
| 1827 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1828 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1829 | struct bpf_reg_state *regs = cur_regs(env); |
| 1830 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1831 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1832 | int size, err = 0; |
| 1833 | |
| 1834 | size = bpf_size_to_bytes(bpf_size); |
| 1835 | if (size < 0) |
| 1836 | return size; |
| 1837 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1838 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1839 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1840 | if (err) |
| 1841 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1842 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1843 | /* for access checks, reg->off is just part of off */ |
| 1844 | off += reg->off; |
| 1845 | |
| 1846 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1847 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1848 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1849 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1850 | return -EACCES; |
| 1851 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1852 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1853 | err = check_map_access(env, regno, off, size, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1854 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1855 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1856 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1857 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1858 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 1859 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1860 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1861 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1862 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1863 | return -EACCES; |
| 1864 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1865 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 1866 | err = check_ctx_reg(env, reg, regno); |
| 1867 | if (err < 0) |
| 1868 | return err; |
| 1869 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1870 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1871 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1872 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1873 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 1874 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1875 | */ |
| 1876 | if (reg_type == SCALAR_VALUE) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1877 | mark_reg_unknown(env, regs, value_regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1878 | else |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1879 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1880 | value_regno); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1881 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1882 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1883 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1884 | } else if (reg->type == PTR_TO_STACK) { |
| 1885 | /* stack accesses must be at a fixed offset, so that we can |
| 1886 | * determine what type of data were returned. |
| 1887 | * See check_stack_read(). |
| 1888 | */ |
| 1889 | if (!tnum_is_const(reg->var_off)) { |
| 1890 | char tn_buf[48]; |
| 1891 | |
| 1892 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1893 | verbose(env, "variable stack access var_off=%s off=%d size=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1894 | tn_buf, off, size); |
| 1895 | return -EACCES; |
| 1896 | } |
| 1897 | off += reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1898 | if (off >= 0 || off < -MAX_BPF_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1899 | verbose(env, "invalid stack off=%d size=%d\n", off, |
| 1900 | size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1901 | return -EACCES; |
| 1902 | } |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1903 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1904 | state = func(env, reg); |
| 1905 | err = update_stack_depth(env, state, off); |
| 1906 | if (err) |
| 1907 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1908 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1909 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1910 | err = check_stack_write(env, state, off, size, |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1911 | value_regno, insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1912 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1913 | err = check_stack_read(env, state, off, size, |
| 1914 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1915 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1916 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1917 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1918 | return -EACCES; |
| 1919 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1920 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1921 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1922 | verbose(env, "R%d leaks addr into packet\n", |
| 1923 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1924 | return -EACCES; |
| 1925 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1926 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1927 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1928 | mark_reg_unknown(env, regs, value_regno); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1929 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 1930 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1931 | is_pointer_value(env, value_regno)) { |
| 1932 | verbose(env, "R%d leaks addr into flow keys\n", |
| 1933 | value_regno); |
| 1934 | return -EACCES; |
| 1935 | } |
| 1936 | |
| 1937 | err = check_flow_keys_access(env, off, size); |
| 1938 | if (!err && t == BPF_READ && value_regno >= 0) |
| 1939 | mark_reg_unknown(env, regs, value_regno); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 1940 | } else if (reg->type == PTR_TO_SOCKET) { |
| 1941 | if (t == BPF_WRITE) { |
| 1942 | verbose(env, "cannot write into socket\n"); |
| 1943 | return -EACCES; |
| 1944 | } |
| 1945 | err = check_sock_access(env, regno, off, size, t); |
| 1946 | if (!err && value_regno >= 0) |
| 1947 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1948 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1949 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 1950 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1951 | return -EACCES; |
| 1952 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1953 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1954 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1955 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1956 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 1957 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1958 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1959 | return err; |
| 1960 | } |
| 1961 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1962 | 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] | 1963 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1964 | int err; |
| 1965 | |
| 1966 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 1967 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1968 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1969 | return -EINVAL; |
| 1970 | } |
| 1971 | |
| 1972 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1973 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1974 | if (err) |
| 1975 | return err; |
| 1976 | |
| 1977 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1978 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1979 | if (err) |
| 1980 | return err; |
| 1981 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1982 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1983 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1984 | return -EACCES; |
| 1985 | } |
| 1986 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1987 | if (is_ctx_reg(env, insn->dst_reg) || |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 1988 | is_pkt_reg(env, insn->dst_reg) || |
| 1989 | is_flow_key_reg(env, insn->dst_reg)) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1990 | 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] | 1991 | insn->dst_reg, |
| 1992 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1993 | return -EACCES; |
| 1994 | } |
| 1995 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1996 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1997 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1998 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1999 | if (err) |
| 2000 | return err; |
| 2001 | |
| 2002 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2003 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2004 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | /* when register 'regno' is passed into function that will read 'access_size' |
| 2008 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2009 | * and all elements of stack are initialized. |
| 2010 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 2011 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2012 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2013 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2014 | int access_size, bool zero_size_allowed, |
| 2015 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2016 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 2017 | struct bpf_reg_state *reg = reg_state(env, regno); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2018 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2019 | int off, i, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2020 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2021 | if (reg->type != PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2022 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2023 | if (zero_size_allowed && access_size == 0 && |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2024 | register_is_null(reg)) |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2025 | return 0; |
| 2026 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2027 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2028 | reg_type_str[reg->type], |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2029 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2030 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2031 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2032 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2033 | /* Only allow fixed-offset stack reads */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2034 | if (!tnum_is_const(reg->var_off)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2035 | char tn_buf[48]; |
| 2036 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2037 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2038 | verbose(env, "invalid variable stack read R%d var_off=%s\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2039 | regno, tn_buf); |
Jann Horn | ea25f91 | 2017-12-18 20:11:57 -0800 | [diff] [blame] | 2040 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2041 | } |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2042 | off = reg->off + reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2043 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2044 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2045 | 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] | 2046 | regno, off, access_size); |
| 2047 | return -EACCES; |
| 2048 | } |
| 2049 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2050 | if (meta && meta->raw_mode) { |
| 2051 | meta->access_size = access_size; |
| 2052 | meta->regno = regno; |
| 2053 | return 0; |
| 2054 | } |
| 2055 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2056 | for (i = 0; i < access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2057 | u8 *stype; |
| 2058 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2059 | slot = -(off + i) - 1; |
| 2060 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2061 | if (state->allocated_stack <= slot) |
| 2062 | goto err; |
| 2063 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 2064 | if (*stype == STACK_MISC) |
| 2065 | goto mark; |
| 2066 | if (*stype == STACK_ZERO) { |
| 2067 | /* helper can write anything into the stack */ |
| 2068 | *stype = STACK_MISC; |
| 2069 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2070 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2071 | err: |
| 2072 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 2073 | off, i, access_size); |
| 2074 | return -EACCES; |
| 2075 | mark: |
| 2076 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 2077 | * the whole slot to be marked as 'read' |
| 2078 | */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2079 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
| 2080 | state->stack[spi].spilled_ptr.parent); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2081 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2082 | return update_stack_depth(env, state, off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2085 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 2086 | int access_size, bool zero_size_allowed, |
| 2087 | struct bpf_call_arg_meta *meta) |
| 2088 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2089 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2090 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2091 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2092 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2093 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2094 | return check_packet_access(env, regno, reg->off, access_size, |
| 2095 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2096 | case PTR_TO_MAP_VALUE: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 2097 | return check_map_access(env, regno, reg->off, access_size, |
| 2098 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2099 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2100 | return check_stack_boundary(env, regno, access_size, |
| 2101 | zero_size_allowed, meta); |
| 2102 | } |
| 2103 | } |
| 2104 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2105 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 2106 | { |
| 2107 | return type == ARG_PTR_TO_MEM || |
| 2108 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 2109 | type == ARG_PTR_TO_UNINIT_MEM; |
| 2110 | } |
| 2111 | |
| 2112 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 2113 | { |
| 2114 | return type == ARG_CONST_SIZE || |
| 2115 | type == ARG_CONST_SIZE_OR_ZERO; |
| 2116 | } |
| 2117 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2118 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2119 | enum bpf_arg_type arg_type, |
| 2120 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2121 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2122 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2123 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2124 | int err = 0; |
| 2125 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2126 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2127 | return 0; |
| 2128 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2129 | err = check_reg_arg(env, regno, SRC_OP); |
| 2130 | if (err) |
| 2131 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2132 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2133 | if (arg_type == ARG_ANYTHING) { |
| 2134 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2135 | verbose(env, "R%d leaks addr into helper function\n", |
| 2136 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2137 | return -EACCES; |
| 2138 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2139 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2140 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 2141 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2142 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 2143 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2144 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2145 | return -EACCES; |
| 2146 | } |
| 2147 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2148 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2149 | arg_type == ARG_PTR_TO_MAP_VALUE || |
| 2150 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2151 | expected_type = PTR_TO_STACK; |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2152 | if (!type_is_pkt_pointer(type) && type != PTR_TO_MAP_VALUE && |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2153 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2154 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2155 | } else if (arg_type == ARG_CONST_SIZE || |
| 2156 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2157 | expected_type = SCALAR_VALUE; |
| 2158 | if (type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2159 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2160 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 2161 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2162 | if (type != expected_type) |
| 2163 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 2164 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 2165 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2166 | if (type != expected_type) |
| 2167 | goto err_type; |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 2168 | err = check_ctx_reg(env, reg, regno); |
| 2169 | if (err < 0) |
| 2170 | return err; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2171 | } else if (arg_type == ARG_PTR_TO_SOCKET) { |
| 2172 | expected_type = PTR_TO_SOCKET; |
| 2173 | if (type != expected_type) |
| 2174 | goto err_type; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2175 | if (meta->ptr_id || !reg->id) { |
| 2176 | verbose(env, "verifier internal error: mismatched references meta=%d, reg=%d\n", |
| 2177 | meta->ptr_id, reg->id); |
| 2178 | return -EFAULT; |
| 2179 | } |
| 2180 | meta->ptr_id = reg->id; |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2181 | } else if (arg_type_is_mem_ptr(arg_type)) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2182 | expected_type = PTR_TO_STACK; |
| 2183 | /* One exception here. In case function allows for NULL to be |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2184 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2185 | * happens during stack boundary checking. |
| 2186 | */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2187 | if (register_is_null(reg) && |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 2188 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2189 | /* final test in check_stack_boundary() */; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2190 | else if (!type_is_pkt_pointer(type) && |
| 2191 | type != PTR_TO_MAP_VALUE && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2192 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2193 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2194 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2195 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2196 | verbose(env, "unsupported arg_type %d\n", arg_type); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2197 | return -EFAULT; |
| 2198 | } |
| 2199 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2200 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 2201 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2202 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2203 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 2204 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 2205 | * check that [key, key + map->key_size) are within |
| 2206 | * stack limits and initialized |
| 2207 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2208 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2209 | /* in function declaration map_ptr must come before |
| 2210 | * map_key, so that it's verified and known before |
| 2211 | * we have to check map_key here. Otherwise it means |
| 2212 | * that kernel subsystem misconfigured verifier |
| 2213 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2214 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2215 | return -EACCES; |
| 2216 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2217 | err = check_helper_mem_access(env, regno, |
| 2218 | meta->map_ptr->key_size, false, |
| 2219 | NULL); |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2220 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
| 2221 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2222 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 2223 | * check [value, value + map->value_size) validity |
| 2224 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2225 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2226 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2227 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2228 | return -EACCES; |
| 2229 | } |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2230 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2231 | err = check_helper_mem_access(env, regno, |
| 2232 | meta->map_ptr->value_size, false, |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 2233 | meta); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2234 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2235 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2236 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2237 | /* remember the mem_size which may be used later |
| 2238 | * to refine return values. |
| 2239 | */ |
| 2240 | meta->msize_smax_value = reg->smax_value; |
| 2241 | meta->msize_umax_value = reg->umax_value; |
| 2242 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2243 | /* The register is SCALAR_VALUE; the access check |
| 2244 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2245 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2246 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2247 | /* For unprivileged variable accesses, disable raw |
| 2248 | * mode so that the program is required to |
| 2249 | * initialize all the memory that the helper could |
| 2250 | * just partially fill up. |
| 2251 | */ |
| 2252 | meta = NULL; |
| 2253 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2254 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2255 | 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] | 2256 | regno); |
| 2257 | return -EACCES; |
| 2258 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2259 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2260 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2261 | err = check_helper_mem_access(env, regno - 1, 0, |
| 2262 | zero_size_allowed, |
| 2263 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2264 | if (err) |
| 2265 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2266 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2267 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2268 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2269 | 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] | 2270 | regno); |
| 2271 | return -EACCES; |
| 2272 | } |
| 2273 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2274 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2275 | zero_size_allowed, meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2279 | err_type: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2280 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2281 | reg_type_str[type], reg_type_str[expected_type]); |
| 2282 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2283 | } |
| 2284 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2285 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 2286 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2287 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2288 | if (!map) |
| 2289 | return 0; |
| 2290 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2291 | /* We need a two way check, first is from map perspective ... */ |
| 2292 | switch (map->map_type) { |
| 2293 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 2294 | if (func_id != BPF_FUNC_tail_call) |
| 2295 | goto error; |
| 2296 | break; |
| 2297 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 2298 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2299 | func_id != BPF_FUNC_perf_event_output && |
| 2300 | func_id != BPF_FUNC_perf_event_read_value) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2301 | goto error; |
| 2302 | break; |
| 2303 | case BPF_MAP_TYPE_STACK_TRACE: |
| 2304 | if (func_id != BPF_FUNC_get_stackid) |
| 2305 | goto error; |
| 2306 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 2307 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 2308 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2309 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2310 | goto error; |
| 2311 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2312 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 2313 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2314 | if (func_id != BPF_FUNC_get_local_storage) |
| 2315 | goto error; |
| 2316 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2317 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
| 2318 | * allow to be modified from bpf side. So do not allow lookup elements |
| 2319 | * for now. |
| 2320 | */ |
| 2321 | case BPF_MAP_TYPE_DEVMAP: |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 2322 | if (func_id != BPF_FUNC_redirect_map) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2323 | goto error; |
| 2324 | break; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2325 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 2326 | * appear. |
| 2327 | */ |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2328 | case BPF_MAP_TYPE_CPUMAP: |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2329 | case BPF_MAP_TYPE_XSKMAP: |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2330 | if (func_id != BPF_FUNC_redirect_map) |
| 2331 | goto error; |
| 2332 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2333 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 2334 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2335 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 2336 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 2337 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2338 | case BPF_MAP_TYPE_SOCKMAP: |
| 2339 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 2340 | func_id != BPF_FUNC_sock_map_update && |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2341 | func_id != BPF_FUNC_map_delete_elem && |
| 2342 | func_id != BPF_FUNC_msg_redirect_map) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2343 | goto error; |
| 2344 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2345 | case BPF_MAP_TYPE_SOCKHASH: |
| 2346 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 2347 | func_id != BPF_FUNC_sock_hash_update && |
| 2348 | func_id != BPF_FUNC_map_delete_elem && |
| 2349 | func_id != BPF_FUNC_msg_redirect_hash) |
| 2350 | goto error; |
| 2351 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2352 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 2353 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 2354 | goto error; |
| 2355 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 2356 | case BPF_MAP_TYPE_QUEUE: |
| 2357 | case BPF_MAP_TYPE_STACK: |
| 2358 | if (func_id != BPF_FUNC_map_peek_elem && |
| 2359 | func_id != BPF_FUNC_map_pop_elem && |
| 2360 | func_id != BPF_FUNC_map_push_elem) |
| 2361 | goto error; |
| 2362 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2363 | default: |
| 2364 | break; |
| 2365 | } |
| 2366 | |
| 2367 | /* ... and second from the function itself. */ |
| 2368 | switch (func_id) { |
| 2369 | case BPF_FUNC_tail_call: |
| 2370 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 2371 | goto error; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 2372 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2373 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
| 2374 | return -EINVAL; |
| 2375 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2376 | break; |
| 2377 | case BPF_FUNC_perf_event_read: |
| 2378 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2379 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2380 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 2381 | goto error; |
| 2382 | break; |
| 2383 | case BPF_FUNC_get_stackid: |
| 2384 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 2385 | goto error; |
| 2386 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2387 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 2388 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2389 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 2390 | goto error; |
| 2391 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2392 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 2393 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2394 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 2395 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2396 | goto error; |
| 2397 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2398 | case BPF_FUNC_sk_redirect_map: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2399 | case BPF_FUNC_msg_redirect_map: |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2400 | case BPF_FUNC_sock_map_update: |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2401 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 2402 | goto error; |
| 2403 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2404 | case BPF_FUNC_sk_redirect_hash: |
| 2405 | case BPF_FUNC_msg_redirect_hash: |
| 2406 | case BPF_FUNC_sock_hash_update: |
| 2407 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2408 | goto error; |
| 2409 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2410 | case BPF_FUNC_get_local_storage: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 2411 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 2412 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2413 | goto error; |
| 2414 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 2415 | case BPF_FUNC_sk_select_reuseport: |
| 2416 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) |
| 2417 | goto error; |
| 2418 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 2419 | case BPF_FUNC_map_peek_elem: |
| 2420 | case BPF_FUNC_map_pop_elem: |
| 2421 | case BPF_FUNC_map_push_elem: |
| 2422 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 2423 | map->map_type != BPF_MAP_TYPE_STACK) |
| 2424 | goto error; |
| 2425 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2426 | default: |
| 2427 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2431 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2432 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2433 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2434 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2437 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2438 | { |
| 2439 | int count = 0; |
| 2440 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2441 | if (fn->arg1_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->arg2_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->arg3_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->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2448 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2449 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2450 | count++; |
| 2451 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2452 | /* We only support one arg being in raw mode at the moment, |
| 2453 | * which is sufficient for the helper functions we have |
| 2454 | * right now. |
| 2455 | */ |
| 2456 | return count <= 1; |
| 2457 | } |
| 2458 | |
| 2459 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 2460 | enum bpf_arg_type arg_next) |
| 2461 | { |
| 2462 | return (arg_type_is_mem_ptr(arg_curr) && |
| 2463 | !arg_type_is_mem_size(arg_next)) || |
| 2464 | (!arg_type_is_mem_ptr(arg_curr) && |
| 2465 | arg_type_is_mem_size(arg_next)); |
| 2466 | } |
| 2467 | |
| 2468 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 2469 | { |
| 2470 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 2471 | * bytes from memory 'buf'. Both arg types need |
| 2472 | * to be paired, so make sure there's no buggy |
| 2473 | * helper function specification. |
| 2474 | */ |
| 2475 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 2476 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 2477 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 2478 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 2479 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 2480 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 2481 | return false; |
| 2482 | |
| 2483 | return true; |
| 2484 | } |
| 2485 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2486 | static bool check_refcount_ok(const struct bpf_func_proto *fn) |
| 2487 | { |
| 2488 | int count = 0; |
| 2489 | |
| 2490 | if (arg_type_is_refcounted(fn->arg1_type)) |
| 2491 | count++; |
| 2492 | if (arg_type_is_refcounted(fn->arg2_type)) |
| 2493 | count++; |
| 2494 | if (arg_type_is_refcounted(fn->arg3_type)) |
| 2495 | count++; |
| 2496 | if (arg_type_is_refcounted(fn->arg4_type)) |
| 2497 | count++; |
| 2498 | if (arg_type_is_refcounted(fn->arg5_type)) |
| 2499 | count++; |
| 2500 | |
| 2501 | /* We only support one arg being unreferenced at the moment, |
| 2502 | * which is sufficient for the helper functions we have right now. |
| 2503 | */ |
| 2504 | return count <= 1; |
| 2505 | } |
| 2506 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2507 | static int check_func_proto(const struct bpf_func_proto *fn) |
| 2508 | { |
| 2509 | return check_raw_mode_ok(fn) && |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2510 | check_arg_pair_ok(fn) && |
| 2511 | check_refcount_ok(fn) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2512 | } |
| 2513 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2514 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 2515 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2516 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2517 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 2518 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2519 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2520 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2521 | int i; |
| 2522 | |
| 2523 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2524 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2525 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2526 | |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 2527 | bpf_for_each_spilled_reg(i, state, reg) { |
| 2528 | if (!reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2529 | continue; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2530 | if (reg_is_pkt_pointer_any(reg)) |
| 2531 | __mark_reg_unknown(reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2532 | } |
| 2533 | } |
| 2534 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2535 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 2536 | { |
| 2537 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2538 | int i; |
| 2539 | |
| 2540 | for (i = 0; i <= vstate->curframe; i++) |
| 2541 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 2542 | } |
| 2543 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2544 | static void release_reg_references(struct bpf_verifier_env *env, |
| 2545 | struct bpf_func_state *state, int id) |
| 2546 | { |
| 2547 | struct bpf_reg_state *regs = state->regs, *reg; |
| 2548 | int i; |
| 2549 | |
| 2550 | for (i = 0; i < MAX_BPF_REG; i++) |
| 2551 | if (regs[i].id == id) |
| 2552 | mark_reg_unknown(env, regs, i); |
| 2553 | |
| 2554 | bpf_for_each_spilled_reg(i, state, reg) { |
| 2555 | if (!reg) |
| 2556 | continue; |
| 2557 | if (reg_is_refcounted(reg) && reg->id == id) |
| 2558 | __mark_reg_unknown(reg); |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | /* The pointer with the specified id has released its reference to kernel |
| 2563 | * resources. Identify all copies of the same pointer and clear the reference. |
| 2564 | */ |
| 2565 | static int release_reference(struct bpf_verifier_env *env, |
| 2566 | struct bpf_call_arg_meta *meta) |
| 2567 | { |
| 2568 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2569 | int i; |
| 2570 | |
| 2571 | for (i = 0; i <= vstate->curframe; i++) |
| 2572 | release_reg_references(env, vstate->frame[i], meta->ptr_id); |
| 2573 | |
| 2574 | return release_reference_state(env, meta->ptr_id); |
| 2575 | } |
| 2576 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2577 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 2578 | int *insn_idx) |
| 2579 | { |
| 2580 | struct bpf_verifier_state *state = env->cur_state; |
| 2581 | struct bpf_func_state *caller, *callee; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2582 | int i, err, subprog, target_insn; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2583 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 2584 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2585 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 2586 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2587 | return -E2BIG; |
| 2588 | } |
| 2589 | |
| 2590 | target_insn = *insn_idx + insn->imm; |
| 2591 | subprog = find_subprog(env, target_insn + 1); |
| 2592 | if (subprog < 0) { |
| 2593 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 2594 | target_insn + 1); |
| 2595 | return -EFAULT; |
| 2596 | } |
| 2597 | |
| 2598 | caller = state->frame[state->curframe]; |
| 2599 | if (state->frame[state->curframe + 1]) { |
| 2600 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 2601 | state->curframe + 1); |
| 2602 | return -EFAULT; |
| 2603 | } |
| 2604 | |
| 2605 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 2606 | if (!callee) |
| 2607 | return -ENOMEM; |
| 2608 | state->frame[state->curframe + 1] = callee; |
| 2609 | |
| 2610 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 2611 | * into its own stack before reading from it. |
| 2612 | * callee can read/write into caller's stack |
| 2613 | */ |
| 2614 | init_func_state(env, callee, |
| 2615 | /* remember the callsite, it will be used by bpf_exit */ |
| 2616 | *insn_idx /* callsite */, |
| 2617 | state->curframe + 1 /* frameno within this callchain */, |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 2618 | subprog /* subprog number within this prog */); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2619 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2620 | /* Transfer references to the callee */ |
| 2621 | err = transfer_reference_state(callee, caller); |
| 2622 | if (err) |
| 2623 | return err; |
| 2624 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2625 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 2626 | * pointers, which connects us up to the liveness chain |
| 2627 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2628 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 2629 | callee->regs[i] = caller->regs[i]; |
| 2630 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2631 | /* after the call registers r0 - r5 were scratched */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2632 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 2633 | mark_reg_not_init(env, caller->regs, caller_saved[i]); |
| 2634 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2635 | } |
| 2636 | |
| 2637 | /* only increment it after check_reg_arg() finished */ |
| 2638 | state->curframe++; |
| 2639 | |
| 2640 | /* and go analyze first insn of the callee */ |
| 2641 | *insn_idx = target_insn; |
| 2642 | |
| 2643 | if (env->log.level) { |
| 2644 | verbose(env, "caller:\n"); |
| 2645 | print_verifier_state(env, caller); |
| 2646 | verbose(env, "callee:\n"); |
| 2647 | print_verifier_state(env, callee); |
| 2648 | } |
| 2649 | return 0; |
| 2650 | } |
| 2651 | |
| 2652 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 2653 | { |
| 2654 | struct bpf_verifier_state *state = env->cur_state; |
| 2655 | struct bpf_func_state *caller, *callee; |
| 2656 | struct bpf_reg_state *r0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2657 | int err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2658 | |
| 2659 | callee = state->frame[state->curframe]; |
| 2660 | r0 = &callee->regs[BPF_REG_0]; |
| 2661 | if (r0->type == PTR_TO_STACK) { |
| 2662 | /* technically it's ok to return caller's stack pointer |
| 2663 | * (or caller's caller's pointer) back to the caller, |
| 2664 | * since these pointers are valid. Only current stack |
| 2665 | * pointer will be invalid as soon as function exits, |
| 2666 | * but let's be conservative |
| 2667 | */ |
| 2668 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 2669 | return -EINVAL; |
| 2670 | } |
| 2671 | |
| 2672 | state->curframe--; |
| 2673 | caller = state->frame[state->curframe]; |
| 2674 | /* return to the caller whatever r0 had in the callee */ |
| 2675 | caller->regs[BPF_REG_0] = *r0; |
| 2676 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2677 | /* Transfer references to the caller */ |
| 2678 | err = transfer_reference_state(caller, callee); |
| 2679 | if (err) |
| 2680 | return err; |
| 2681 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2682 | *insn_idx = callee->callsite + 1; |
| 2683 | if (env->log.level) { |
| 2684 | verbose(env, "returning from callee:\n"); |
| 2685 | print_verifier_state(env, callee); |
| 2686 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 2687 | print_verifier_state(env, caller); |
| 2688 | } |
| 2689 | /* clear everything in the callee */ |
| 2690 | free_func_state(callee); |
| 2691 | state->frame[state->curframe + 1] = NULL; |
| 2692 | return 0; |
| 2693 | } |
| 2694 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2695 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 2696 | int func_id, |
| 2697 | struct bpf_call_arg_meta *meta) |
| 2698 | { |
| 2699 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 2700 | |
| 2701 | if (ret_type != RET_INTEGER || |
| 2702 | (func_id != BPF_FUNC_get_stack && |
| 2703 | func_id != BPF_FUNC_probe_read_str)) |
| 2704 | return; |
| 2705 | |
| 2706 | ret_reg->smax_value = meta->msize_smax_value; |
| 2707 | ret_reg->umax_value = meta->msize_umax_value; |
| 2708 | __reg_deduce_bounds(ret_reg); |
| 2709 | __reg_bound_offset(ret_reg); |
| 2710 | } |
| 2711 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2712 | static int |
| 2713 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 2714 | int func_id, int insn_idx) |
| 2715 | { |
| 2716 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
| 2717 | |
| 2718 | if (func_id != BPF_FUNC_tail_call && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 2719 | func_id != BPF_FUNC_map_lookup_elem && |
| 2720 | func_id != BPF_FUNC_map_update_elem && |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 2721 | func_id != BPF_FUNC_map_delete_elem && |
| 2722 | func_id != BPF_FUNC_map_push_elem && |
| 2723 | func_id != BPF_FUNC_map_pop_elem && |
| 2724 | func_id != BPF_FUNC_map_peek_elem) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2725 | return 0; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 2726 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2727 | if (meta->map_ptr == NULL) { |
| 2728 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 2729 | return -EINVAL; |
| 2730 | } |
| 2731 | |
| 2732 | if (!BPF_MAP_PTR(aux->map_state)) |
| 2733 | bpf_map_ptr_store(aux, meta->map_ptr, |
| 2734 | meta->map_ptr->unpriv_array); |
| 2735 | else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr) |
| 2736 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
| 2737 | meta->map_ptr->unpriv_array); |
| 2738 | return 0; |
| 2739 | } |
| 2740 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2741 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 2742 | { |
| 2743 | struct bpf_func_state *state = cur_func(env); |
| 2744 | int i; |
| 2745 | |
| 2746 | for (i = 0; i < state->acquired_refs; i++) { |
| 2747 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 2748 | state->refs[i].id, state->refs[i].insn_idx); |
| 2749 | } |
| 2750 | return state->acquired_refs ? -EINVAL : 0; |
| 2751 | } |
| 2752 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2753 | 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] | 2754 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2755 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2756 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2757 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2758 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2759 | int i, err; |
| 2760 | |
| 2761 | /* find function prototype */ |
| 2762 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2763 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 2764 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2765 | return -EINVAL; |
| 2766 | } |
| 2767 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 2768 | if (env->ops->get_func_proto) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2769 | fn = env->ops->get_func_proto(func_id, env->prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2770 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2771 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 2772 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2773 | return -EINVAL; |
| 2774 | } |
| 2775 | |
| 2776 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 2777 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Daniel Borkmann | 3fe2867 | 2018-06-02 23:06:33 +0200 | [diff] [blame] | 2778 | 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] | 2779 | return -EINVAL; |
| 2780 | } |
| 2781 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 2782 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 2783 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 2784 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 2785 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 2786 | func_id_name(func_id), func_id); |
| 2787 | return -EINVAL; |
| 2788 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2789 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2790 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2791 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2792 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2793 | err = check_func_proto(fn); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2794 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2795 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2796 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2797 | return err; |
| 2798 | } |
| 2799 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2800 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2801 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2802 | if (err) |
| 2803 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2804 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2805 | if (err) |
| 2806 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2807 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2808 | if (err) |
| 2809 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2810 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2811 | if (err) |
| 2812 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2813 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2814 | if (err) |
| 2815 | return err; |
| 2816 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2817 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 2818 | if (err) |
| 2819 | return err; |
| 2820 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2821 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 2822 | * is inferred from register state. |
| 2823 | */ |
| 2824 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2825 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 2826 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2827 | if (err) |
| 2828 | return err; |
| 2829 | } |
| 2830 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2831 | if (func_id == BPF_FUNC_tail_call) { |
| 2832 | err = check_reference_leak(env); |
| 2833 | if (err) { |
| 2834 | verbose(env, "tail_call would lead to reference leak\n"); |
| 2835 | return err; |
| 2836 | } |
| 2837 | } else if (is_release_function(func_id)) { |
| 2838 | err = release_reference(env, &meta); |
| 2839 | if (err) |
| 2840 | return err; |
| 2841 | } |
| 2842 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2843 | regs = cur_regs(env); |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 2844 | |
| 2845 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 2846 | * this is required because get_local_storage() can't return an error. |
| 2847 | */ |
| 2848 | if (func_id == BPF_FUNC_get_local_storage && |
| 2849 | !register_is_null(®s[BPF_REG_2])) { |
| 2850 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 2851 | return -EINVAL; |
| 2852 | } |
| 2853 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2854 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2855 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2856 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2857 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2858 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2859 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2860 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2861 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2862 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2863 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2864 | } else if (fn->ret_type == RET_VOID) { |
| 2865 | regs[BPF_REG_0].type = NOT_INIT; |
Roman Gushchin | 3e6a4b3 | 2018-08-02 14:27:22 -0700 | [diff] [blame] | 2866 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 2867 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2868 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2869 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2870 | /* remember map_ptr, so that check_map_access() |
| 2871 | * can check 'value_size' boundary of memory access |
| 2872 | * to map element returned from bpf_map_lookup_elem() |
| 2873 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2874 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2875 | verbose(env, |
| 2876 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2877 | return -EINVAL; |
| 2878 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2879 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 2880 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 2881 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
| 2882 | } else { |
| 2883 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
| 2884 | regs[BPF_REG_0].id = ++env->id_gen; |
| 2885 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2886 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2887 | int id = acquire_reference_state(env, insn_idx); |
| 2888 | if (id < 0) |
| 2889 | return id; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2890 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 2891 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 2892 | regs[BPF_REG_0].id = id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2893 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2894 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2895 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2896 | return -EINVAL; |
| 2897 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2898 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2899 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
| 2900 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2901 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2902 | if (err) |
| 2903 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2904 | |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 2905 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
| 2906 | const char *err_str; |
| 2907 | |
| 2908 | #ifdef CONFIG_PERF_EVENTS |
| 2909 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 2910 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 2911 | #else |
| 2912 | err = -ENOTSUPP; |
| 2913 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 2914 | #endif |
| 2915 | if (err) { |
| 2916 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 2917 | return err; |
| 2918 | } |
| 2919 | |
| 2920 | env->prog->has_callchain_buf = true; |
| 2921 | } |
| 2922 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2923 | if (changes_data) |
| 2924 | clear_all_pkt_pointers(env); |
| 2925 | return 0; |
| 2926 | } |
| 2927 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2928 | static bool signed_add_overflows(s64 a, s64 b) |
| 2929 | { |
| 2930 | /* Do the add in u64, where overflow is well-defined */ |
| 2931 | s64 res = (s64)((u64)a + (u64)b); |
| 2932 | |
| 2933 | if (b < 0) |
| 2934 | return res > a; |
| 2935 | return res < a; |
| 2936 | } |
| 2937 | |
| 2938 | static bool signed_sub_overflows(s64 a, s64 b) |
| 2939 | { |
| 2940 | /* Do the sub in u64, where overflow is well-defined */ |
| 2941 | s64 res = (s64)((u64)a - (u64)b); |
| 2942 | |
| 2943 | if (b < 0) |
| 2944 | return res < a; |
| 2945 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2946 | } |
| 2947 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2948 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 2949 | const struct bpf_reg_state *reg, |
| 2950 | enum bpf_reg_type type) |
| 2951 | { |
| 2952 | bool known = tnum_is_const(reg->var_off); |
| 2953 | s64 val = reg->var_off.value; |
| 2954 | s64 smin = reg->smin_value; |
| 2955 | |
| 2956 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 2957 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 2958 | reg_type_str[type], val); |
| 2959 | return false; |
| 2960 | } |
| 2961 | |
| 2962 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 2963 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 2964 | reg_type_str[type], reg->off); |
| 2965 | return false; |
| 2966 | } |
| 2967 | |
| 2968 | if (smin == S64_MIN) { |
| 2969 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 2970 | reg_type_str[type]); |
| 2971 | return false; |
| 2972 | } |
| 2973 | |
| 2974 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 2975 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 2976 | smin, reg_type_str[type]); |
| 2977 | return false; |
| 2978 | } |
| 2979 | |
| 2980 | return true; |
| 2981 | } |
| 2982 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2983 | /* 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] | 2984 | * Caller should also handle BPF_MOV case separately. |
| 2985 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 2986 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 2987 | */ |
| 2988 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 2989 | struct bpf_insn *insn, |
| 2990 | const struct bpf_reg_state *ptr_reg, |
| 2991 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2992 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2993 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2994 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2995 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2996 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2997 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 2998 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 2999 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 3000 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3001 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3002 | u32 dst = insn->dst_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3003 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3004 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3005 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 3006 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 3007 | smin_val > smax_val || umin_val > umax_val) { |
| 3008 | /* Taint dst register if offset had invalid bounds derived from |
| 3009 | * e.g. dead branches. |
| 3010 | */ |
| 3011 | __mark_reg_unknown(dst_reg); |
| 3012 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3013 | } |
| 3014 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3015 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3016 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3017 | verbose(env, |
| 3018 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 3019 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3020 | return -EACCES; |
| 3021 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3022 | |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3023 | switch (ptr_reg->type) { |
| 3024 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 3025 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
| 3026 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3027 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3028 | case CONST_PTR_TO_MAP: |
| 3029 | case PTR_TO_PACKET_END: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3030 | case PTR_TO_SOCKET: |
| 3031 | case PTR_TO_SOCKET_OR_NULL: |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3032 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
| 3033 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3034 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 3035 | default: |
| 3036 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3037 | } |
| 3038 | |
| 3039 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 3040 | * 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] | 3041 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3042 | dst_reg->type = ptr_reg->type; |
| 3043 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 3044 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3045 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 3046 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 3047 | return -EINVAL; |
| 3048 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3049 | switch (opcode) { |
| 3050 | case BPF_ADD: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3051 | /* We can take a fixed offset as long as it doesn't overflow |
| 3052 | * the s32 'off' field |
| 3053 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3054 | if (known && (ptr_reg->off + smin_val == |
| 3055 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3056 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3057 | dst_reg->smin_value = smin_ptr; |
| 3058 | dst_reg->smax_value = smax_ptr; |
| 3059 | dst_reg->umin_value = umin_ptr; |
| 3060 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3061 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3062 | dst_reg->off = ptr_reg->off + smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3063 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3064 | break; |
| 3065 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3066 | /* A new variable offset is created. Note that off_reg->off |
| 3067 | * == 0, since it's a scalar. |
| 3068 | * dst_reg gets the pointer type and since some positive |
| 3069 | * integer value was added to the pointer, give it a new 'id' |
| 3070 | * if it's a PTR_TO_PACKET. |
| 3071 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 3072 | * added into the variable offset, and we copy the fixed offset |
| 3073 | * from ptr_reg. |
| 3074 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3075 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 3076 | signed_add_overflows(smax_ptr, smax_val)) { |
| 3077 | dst_reg->smin_value = S64_MIN; |
| 3078 | dst_reg->smax_value = S64_MAX; |
| 3079 | } else { |
| 3080 | dst_reg->smin_value = smin_ptr + smin_val; |
| 3081 | dst_reg->smax_value = smax_ptr + smax_val; |
| 3082 | } |
| 3083 | if (umin_ptr + umin_val < umin_ptr || |
| 3084 | umax_ptr + umax_val < umax_ptr) { |
| 3085 | dst_reg->umin_value = 0; |
| 3086 | dst_reg->umax_value = U64_MAX; |
| 3087 | } else { |
| 3088 | dst_reg->umin_value = umin_ptr + umin_val; |
| 3089 | dst_reg->umax_value = umax_ptr + umax_val; |
| 3090 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3091 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 3092 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3093 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3094 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3095 | dst_reg->id = ++env->id_gen; |
| 3096 | /* something was added to pkt_ptr, set range to zero */ |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3097 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3098 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3099 | break; |
| 3100 | case BPF_SUB: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3101 | if (dst_reg == off_reg) { |
| 3102 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3103 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 3104 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3105 | return -EACCES; |
| 3106 | } |
| 3107 | /* We don't allow subtraction from FP, because (according to |
| 3108 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 3109 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 3110 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3111 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3112 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 3113 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3114 | return -EACCES; |
| 3115 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3116 | if (known && (ptr_reg->off - smin_val == |
| 3117 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3118 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3119 | dst_reg->smin_value = smin_ptr; |
| 3120 | dst_reg->smax_value = smax_ptr; |
| 3121 | dst_reg->umin_value = umin_ptr; |
| 3122 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3123 | dst_reg->var_off = ptr_reg->var_off; |
| 3124 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3125 | dst_reg->off = ptr_reg->off - smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3126 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3127 | break; |
| 3128 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3129 | /* A new variable offset is created. If the subtrahend is known |
| 3130 | * nonnegative, then any reg->range we had before is still good. |
| 3131 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3132 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 3133 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 3134 | /* Overflow possible, we know nothing */ |
| 3135 | dst_reg->smin_value = S64_MIN; |
| 3136 | dst_reg->smax_value = S64_MAX; |
| 3137 | } else { |
| 3138 | dst_reg->smin_value = smin_ptr - smax_val; |
| 3139 | dst_reg->smax_value = smax_ptr - smin_val; |
| 3140 | } |
| 3141 | if (umin_ptr < umax_val) { |
| 3142 | /* Overflow possible, we know nothing */ |
| 3143 | dst_reg->umin_value = 0; |
| 3144 | dst_reg->umax_value = U64_MAX; |
| 3145 | } else { |
| 3146 | /* Cannot overflow (as long as bounds are consistent) */ |
| 3147 | dst_reg->umin_value = umin_ptr - umax_val; |
| 3148 | dst_reg->umax_value = umax_ptr - umin_val; |
| 3149 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3150 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 3151 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3152 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3153 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3154 | dst_reg->id = ++env->id_gen; |
| 3155 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3156 | if (smin_val < 0) |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 3157 | dst_reg->raw = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3158 | } |
| 3159 | break; |
| 3160 | case BPF_AND: |
| 3161 | case BPF_OR: |
| 3162 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3163 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 3164 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 3165 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3166 | return -EACCES; |
| 3167 | default: |
| 3168 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3169 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 3170 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3171 | return -EACCES; |
| 3172 | } |
| 3173 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3174 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 3175 | return -EINVAL; |
| 3176 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3177 | __update_reg_bounds(dst_reg); |
| 3178 | __reg_deduce_bounds(dst_reg); |
| 3179 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3180 | return 0; |
| 3181 | } |
| 3182 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3183 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 3184 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 3185 | * need extra checks in the 32-bit case. |
| 3186 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3187 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 3188 | struct bpf_insn *insn, |
| 3189 | struct bpf_reg_state *dst_reg, |
| 3190 | struct bpf_reg_state src_reg) |
| 3191 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3192 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3193 | u8 opcode = BPF_OP(insn->code); |
| 3194 | bool src_known, dst_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3195 | s64 smin_val, smax_val; |
| 3196 | u64 umin_val, umax_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3197 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3198 | |
Jann Horn | b799207 | 2018-10-05 18:17:59 +0200 | [diff] [blame] | 3199 | if (insn_bitness == 32) { |
| 3200 | /* Relevant for 32-bit RSH: Information can propagate towards |
| 3201 | * LSB, so it isn't sufficient to only truncate the output to |
| 3202 | * 32 bits. |
| 3203 | */ |
| 3204 | coerce_reg_to_size(dst_reg, 4); |
| 3205 | coerce_reg_to_size(&src_reg, 4); |
| 3206 | } |
| 3207 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3208 | smin_val = src_reg.smin_value; |
| 3209 | smax_val = src_reg.smax_value; |
| 3210 | umin_val = src_reg.umin_value; |
| 3211 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3212 | src_known = tnum_is_const(src_reg.var_off); |
| 3213 | dst_known = tnum_is_const(dst_reg->var_off); |
| 3214 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 3215 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
| 3216 | smin_val > smax_val || umin_val > umax_val) { |
| 3217 | /* Taint dst register if offset had invalid bounds derived from |
| 3218 | * e.g. dead branches. |
| 3219 | */ |
| 3220 | __mark_reg_unknown(dst_reg); |
| 3221 | return 0; |
| 3222 | } |
| 3223 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 3224 | if (!src_known && |
| 3225 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
| 3226 | __mark_reg_unknown(dst_reg); |
| 3227 | return 0; |
| 3228 | } |
| 3229 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3230 | switch (opcode) { |
| 3231 | case BPF_ADD: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3232 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 3233 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 3234 | dst_reg->smin_value = S64_MIN; |
| 3235 | dst_reg->smax_value = S64_MAX; |
| 3236 | } else { |
| 3237 | dst_reg->smin_value += smin_val; |
| 3238 | dst_reg->smax_value += smax_val; |
| 3239 | } |
| 3240 | if (dst_reg->umin_value + umin_val < umin_val || |
| 3241 | dst_reg->umax_value + umax_val < umax_val) { |
| 3242 | dst_reg->umin_value = 0; |
| 3243 | dst_reg->umax_value = U64_MAX; |
| 3244 | } else { |
| 3245 | dst_reg->umin_value += umin_val; |
| 3246 | dst_reg->umax_value += umax_val; |
| 3247 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3248 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 3249 | break; |
| 3250 | case BPF_SUB: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3251 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 3252 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 3253 | /* Overflow possible, we know nothing */ |
| 3254 | dst_reg->smin_value = S64_MIN; |
| 3255 | dst_reg->smax_value = S64_MAX; |
| 3256 | } else { |
| 3257 | dst_reg->smin_value -= smax_val; |
| 3258 | dst_reg->smax_value -= smin_val; |
| 3259 | } |
| 3260 | if (dst_reg->umin_value < umax_val) { |
| 3261 | /* Overflow possible, we know nothing */ |
| 3262 | dst_reg->umin_value = 0; |
| 3263 | dst_reg->umax_value = U64_MAX; |
| 3264 | } else { |
| 3265 | /* Cannot overflow (as long as bounds are consistent) */ |
| 3266 | dst_reg->umin_value -= umax_val; |
| 3267 | dst_reg->umax_value -= umin_val; |
| 3268 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3269 | 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] | 3270 | break; |
| 3271 | case BPF_MUL: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3272 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 3273 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3274 | /* Ain't nobody got time to multiply that sign */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3275 | __mark_reg_unbounded(dst_reg); |
| 3276 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3277 | break; |
| 3278 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3279 | /* Both values are positive, so we can work with unsigned and |
| 3280 | * copy the result to signed (unless it exceeds S64_MAX). |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3281 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3282 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 3283 | /* Potential overflow, we know nothing */ |
| 3284 | __mark_reg_unbounded(dst_reg); |
| 3285 | /* (except what we can learn from the var_off) */ |
| 3286 | __update_reg_bounds(dst_reg); |
| 3287 | break; |
| 3288 | } |
| 3289 | dst_reg->umin_value *= umin_val; |
| 3290 | dst_reg->umax_value *= umax_val; |
| 3291 | if (dst_reg->umax_value > S64_MAX) { |
| 3292 | /* Overflow possible, we know nothing */ |
| 3293 | dst_reg->smin_value = S64_MIN; |
| 3294 | dst_reg->smax_value = S64_MAX; |
| 3295 | } else { |
| 3296 | dst_reg->smin_value = dst_reg->umin_value; |
| 3297 | dst_reg->smax_value = dst_reg->umax_value; |
| 3298 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3299 | break; |
| 3300 | case BPF_AND: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3301 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3302 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 3303 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3304 | break; |
| 3305 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3306 | /* We get our minimum from the var_off, since that's inherently |
| 3307 | * bitwise. Our maximum is the minimum of the operands' maxima. |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 3308 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3309 | 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] | 3310 | dst_reg->umin_value = dst_reg->var_off.value; |
| 3311 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 3312 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 3313 | /* Lose signed bounds when ANDing negative numbers, |
| 3314 | * ain't nobody got time for that. |
| 3315 | */ |
| 3316 | dst_reg->smin_value = S64_MIN; |
| 3317 | dst_reg->smax_value = S64_MAX; |
| 3318 | } else { |
| 3319 | /* ANDing two positives gives a positive, so safe to |
| 3320 | * cast result into s64. |
| 3321 | */ |
| 3322 | dst_reg->smin_value = dst_reg->umin_value; |
| 3323 | dst_reg->smax_value = dst_reg->umax_value; |
| 3324 | } |
| 3325 | /* We may learn something more from the var_off */ |
| 3326 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3327 | break; |
| 3328 | case BPF_OR: |
| 3329 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3330 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
| 3331 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3332 | break; |
| 3333 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3334 | /* We get our maximum from the var_off, and our minimum is the |
| 3335 | * maximum of the operands' minima |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3336 | */ |
| 3337 | 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] | 3338 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 3339 | dst_reg->umax_value = dst_reg->var_off.value | |
| 3340 | dst_reg->var_off.mask; |
| 3341 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 3342 | /* Lose signed bounds when ORing negative numbers, |
| 3343 | * ain't nobody got time for that. |
| 3344 | */ |
| 3345 | dst_reg->smin_value = S64_MIN; |
| 3346 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3347 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3348 | /* ORing two positives gives a positive, so safe to |
| 3349 | * cast result into s64. |
| 3350 | */ |
| 3351 | dst_reg->smin_value = dst_reg->umin_value; |
| 3352 | dst_reg->smax_value = dst_reg->umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3353 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3354 | /* We may learn something more from the var_off */ |
| 3355 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3356 | break; |
| 3357 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3358 | if (umax_val >= insn_bitness) { |
| 3359 | /* Shifts greater than 31 or 63 are undefined. |
| 3360 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3361 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3362 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3363 | break; |
| 3364 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3365 | /* We lose all sign bit information (except what we can pick |
| 3366 | * up from var_off) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3367 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3368 | dst_reg->smin_value = S64_MIN; |
| 3369 | dst_reg->smax_value = S64_MAX; |
| 3370 | /* If we might shift our top bit out, then we know nothing */ |
| 3371 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 3372 | dst_reg->umin_value = 0; |
| 3373 | dst_reg->umax_value = U64_MAX; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3374 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3375 | dst_reg->umin_value <<= umin_val; |
| 3376 | dst_reg->umax_value <<= umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3377 | } |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 3378 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3379 | /* We may learn something more from the var_off */ |
| 3380 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3381 | break; |
| 3382 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3383 | if (umax_val >= insn_bitness) { |
| 3384 | /* Shifts greater than 31 or 63 are undefined. |
| 3385 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3386 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3387 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3388 | break; |
| 3389 | } |
Edward Cree | 4374f25 | 2017-12-18 20:11:53 -0800 | [diff] [blame] | 3390 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 3391 | * be negative, then either: |
| 3392 | * 1) src_reg might be zero, so the sign bit of the result is |
| 3393 | * unknown, so we lose our signed bounds |
| 3394 | * 2) it's known negative, thus the unsigned bounds capture the |
| 3395 | * signed bounds |
| 3396 | * 3) the signed bounds cross zero, so they tell us nothing |
| 3397 | * about the result |
| 3398 | * If the value in dst_reg is known nonnegative, then again the |
| 3399 | * unsigned bounts capture the signed bounds. |
| 3400 | * Thus, in all cases it suffices to blow away our signed bounds |
| 3401 | * and rely on inferring new ones from the unsigned bounds and |
| 3402 | * var_off of the result. |
| 3403 | */ |
| 3404 | dst_reg->smin_value = S64_MIN; |
| 3405 | dst_reg->smax_value = S64_MAX; |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 3406 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3407 | dst_reg->umin_value >>= umax_val; |
| 3408 | dst_reg->umax_value >>= umin_val; |
| 3409 | /* We may learn something more from the var_off */ |
| 3410 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3411 | break; |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 3412 | case BPF_ARSH: |
| 3413 | if (umax_val >= insn_bitness) { |
| 3414 | /* Shifts greater than 31 or 63 are undefined. |
| 3415 | * This includes shifts by a negative number. |
| 3416 | */ |
| 3417 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 3418 | break; |
| 3419 | } |
| 3420 | |
| 3421 | /* Upon reaching here, src_known is true and |
| 3422 | * umax_val is equal to umin_val. |
| 3423 | */ |
| 3424 | dst_reg->smin_value >>= umin_val; |
| 3425 | dst_reg->smax_value >>= umin_val; |
| 3426 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val); |
| 3427 | |
| 3428 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 3429 | * dst_reg var_off to refine the result. |
| 3430 | */ |
| 3431 | dst_reg->umin_value = 0; |
| 3432 | dst_reg->umax_value = U64_MAX; |
| 3433 | __update_reg_bounds(dst_reg); |
| 3434 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3435 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3436 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3437 | break; |
| 3438 | } |
| 3439 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3440 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3441 | /* 32-bit ALU ops are (32,32)->32 */ |
| 3442 | coerce_reg_to_size(dst_reg, 4); |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3443 | } |
| 3444 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3445 | __reg_deduce_bounds(dst_reg); |
| 3446 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3447 | return 0; |
| 3448 | } |
| 3449 | |
| 3450 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 3451 | * and var_off. |
| 3452 | */ |
| 3453 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 3454 | struct bpf_insn *insn) |
| 3455 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3456 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3457 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 3458 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3459 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 3460 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3461 | |
| 3462 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3463 | src_reg = NULL; |
| 3464 | if (dst_reg->type != SCALAR_VALUE) |
| 3465 | ptr_reg = dst_reg; |
| 3466 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3467 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3468 | if (src_reg->type != SCALAR_VALUE) { |
| 3469 | if (dst_reg->type != SCALAR_VALUE) { |
| 3470 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3471 | * an arbitrary scalar. Disallow all math except |
| 3472 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3473 | */ |
Alexei Starovoitov | dd06682 | 2018-09-12 14:06:10 -0700 | [diff] [blame] | 3474 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3475 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 3476 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3477 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3478 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 3479 | insn->dst_reg, |
| 3480 | bpf_alu_string[opcode >> 4]); |
| 3481 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3482 | } else { |
| 3483 | /* scalar += pointer |
| 3484 | * This is legal, but we have to reverse our |
| 3485 | * src/dest handling in computing the range |
| 3486 | */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3487 | return adjust_ptr_min_max_vals(env, insn, |
| 3488 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3489 | } |
| 3490 | } else if (ptr_reg) { |
| 3491 | /* pointer += scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3492 | return adjust_ptr_min_max_vals(env, insn, |
| 3493 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3494 | } |
| 3495 | } else { |
| 3496 | /* Pretend the src is a reg with a known value, since we only |
| 3497 | * need to be able to read from this state. |
| 3498 | */ |
| 3499 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3500 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3501 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3502 | if (ptr_reg) /* pointer += K */ |
| 3503 | return adjust_ptr_min_max_vals(env, insn, |
| 3504 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | /* Got here implies adding two SCALAR_VALUEs */ |
| 3508 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3509 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3510 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3511 | return -EINVAL; |
| 3512 | } |
| 3513 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3514 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3515 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3516 | return -EINVAL; |
| 3517 | } |
| 3518 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3519 | } |
| 3520 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3521 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3522 | 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] | 3523 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3524 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3525 | u8 opcode = BPF_OP(insn->code); |
| 3526 | int err; |
| 3527 | |
| 3528 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 3529 | if (opcode == BPF_NEG) { |
| 3530 | if (BPF_SRC(insn->code) != 0 || |
| 3531 | insn->src_reg != BPF_REG_0 || |
| 3532 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3533 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3534 | return -EINVAL; |
| 3535 | } |
| 3536 | } else { |
| 3537 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 3538 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 3539 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3540 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3541 | return -EINVAL; |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3546 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3547 | if (err) |
| 3548 | return err; |
| 3549 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3550 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3551 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3552 | insn->dst_reg); |
| 3553 | return -EACCES; |
| 3554 | } |
| 3555 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3556 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3557 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3558 | if (err) |
| 3559 | return err; |
| 3560 | |
| 3561 | } else if (opcode == BPF_MOV) { |
| 3562 | |
| 3563 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3564 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3565 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3566 | return -EINVAL; |
| 3567 | } |
| 3568 | |
| 3569 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3570 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3571 | if (err) |
| 3572 | return err; |
| 3573 | } else { |
| 3574 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3575 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3576 | return -EINVAL; |
| 3577 | } |
| 3578 | } |
| 3579 | |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 3580 | /* check dest operand, mark as required later */ |
| 3581 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3582 | if (err) |
| 3583 | return err; |
| 3584 | |
| 3585 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame^] | 3586 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
| 3587 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; |
| 3588 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3589 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 3590 | /* case: R1 = R2 |
| 3591 | * copy register state to dest reg |
| 3592 | */ |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame^] | 3593 | *dst_reg = *src_reg; |
| 3594 | dst_reg->live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3595 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3596 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3597 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3598 | verbose(env, |
| 3599 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3600 | insn->src_reg); |
| 3601 | return -EACCES; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame^] | 3602 | } else if (src_reg->type == SCALAR_VALUE) { |
| 3603 | *dst_reg = *src_reg; |
| 3604 | dst_reg->live |= REG_LIVE_WRITTEN; |
| 3605 | } else { |
| 3606 | mark_reg_unknown(env, regs, |
| 3607 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3608 | } |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame^] | 3609 | coerce_reg_to_size(dst_reg, 4); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3610 | } |
| 3611 | } else { |
| 3612 | /* case: R = imm |
| 3613 | * remember the value we stored into this reg |
| 3614 | */ |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 3615 | /* clear any state __mark_reg_known doesn't set */ |
| 3616 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3617 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 3618 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 3619 | __mark_reg_known(regs + insn->dst_reg, |
| 3620 | insn->imm); |
| 3621 | } else { |
| 3622 | __mark_reg_known(regs + insn->dst_reg, |
| 3623 | (u32)insn->imm); |
| 3624 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3625 | } |
| 3626 | |
| 3627 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3628 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3629 | return -EINVAL; |
| 3630 | |
| 3631 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 3632 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3633 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3634 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3635 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3636 | return -EINVAL; |
| 3637 | } |
| 3638 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3639 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3640 | if (err) |
| 3641 | return err; |
| 3642 | } else { |
| 3643 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3644 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3645 | return -EINVAL; |
| 3646 | } |
| 3647 | } |
| 3648 | |
| 3649 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3650 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3651 | if (err) |
| 3652 | return err; |
| 3653 | |
| 3654 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 3655 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3656 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3657 | return -EINVAL; |
| 3658 | } |
| 3659 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3660 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 3661 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 3662 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 3663 | |
| 3664 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3665 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3666 | return -EINVAL; |
| 3667 | } |
| 3668 | } |
| 3669 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3670 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3671 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3672 | if (err) |
| 3673 | return err; |
| 3674 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3675 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | return 0; |
| 3679 | } |
| 3680 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3681 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3682 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 3683 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3684 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3685 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3686 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3687 | struct bpf_reg_state *regs = state->regs, *reg; |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3688 | u16 new_range; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3689 | int i, j; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3690 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3691 | if (dst_reg->off < 0 || |
| 3692 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3693 | /* This doesn't give us any range */ |
| 3694 | return; |
| 3695 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3696 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 3697 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3698 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 3699 | * than pkt_end, but that's because it's also less than pkt. |
| 3700 | */ |
| 3701 | return; |
| 3702 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3703 | new_range = dst_reg->off; |
| 3704 | if (range_right_open) |
| 3705 | new_range--; |
| 3706 | |
| 3707 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3708 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3709 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3710 | * |
| 3711 | * r2 = r3; |
| 3712 | * r2 += 8; |
| 3713 | * if (r2 > pkt_end) goto <handle exception> |
| 3714 | * <access okay> |
| 3715 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3716 | * r2 = r3; |
| 3717 | * r2 += 8; |
| 3718 | * if (r2 < pkt_end) goto <access okay> |
| 3719 | * <handle exception> |
| 3720 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3721 | * Where: |
| 3722 | * r2 == dst_reg, pkt_end == src_reg |
| 3723 | * r2=pkt(id=n,off=8,r=0) |
| 3724 | * r3=pkt(id=n,off=0,r=0) |
| 3725 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3726 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3727 | * |
| 3728 | * r2 = r3; |
| 3729 | * r2 += 8; |
| 3730 | * if (pkt_end >= r2) goto <access okay> |
| 3731 | * <handle exception> |
| 3732 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3733 | * r2 = r3; |
| 3734 | * r2 += 8; |
| 3735 | * if (pkt_end <= r2) goto <handle exception> |
| 3736 | * <access okay> |
| 3737 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3738 | * Where: |
| 3739 | * pkt_end == dst_reg, r2 == src_reg |
| 3740 | * r2=pkt(id=n,off=8,r=0) |
| 3741 | * r3=pkt(id=n,off=0,r=0) |
| 3742 | * |
| 3743 | * 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] | 3744 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 3745 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 3746 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3747 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3748 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3749 | /* If our ids match, then we must have the same max_value. And we |
| 3750 | * don't care about the other reg's fixed offset, since if it's too big |
| 3751 | * the range won't allow anything. |
| 3752 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 3753 | */ |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3754 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3755 | if (regs[i].type == type && regs[i].id == dst_reg->id) |
Alexei Starovoitov | b197768 | 2017-03-24 15:57:33 -0700 | [diff] [blame] | 3756 | /* keep the maximum range already checked */ |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3757 | regs[i].range = max(regs[i].range, new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3758 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3759 | for (j = 0; j <= vstate->curframe; j++) { |
| 3760 | state = vstate->frame[j]; |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 3761 | bpf_for_each_spilled_reg(i, state, reg) { |
| 3762 | if (!reg) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3763 | continue; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3764 | if (reg->type == type && reg->id == dst_reg->id) |
| 3765 | reg->range = max(reg->range, new_range); |
| 3766 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3767 | } |
| 3768 | } |
| 3769 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3770 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 3771 | * variable register that we are working on, and src_reg is a constant or we're |
| 3772 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3773 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3774 | */ |
| 3775 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 3776 | struct bpf_reg_state *false_reg, u64 val, |
| 3777 | u8 opcode) |
| 3778 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3779 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 3780 | * variable offset from the compare (unless src_reg were a pointer into |
| 3781 | * the same object, but we don't bother with that. |
| 3782 | * Since false_reg and true_reg have the same type by construction, we |
| 3783 | * only need to check one of them for pointerness. |
| 3784 | */ |
| 3785 | if (__is_pointer_value(false, false_reg)) |
| 3786 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3787 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3788 | switch (opcode) { |
| 3789 | case BPF_JEQ: |
| 3790 | /* If this is false then we know nothing Jon Snow, but if it is |
| 3791 | * true then we know for sure. |
| 3792 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3793 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3794 | break; |
| 3795 | case BPF_JNE: |
| 3796 | /* If this is true we know nothing Jon Snow, but if it is false |
| 3797 | * we know the value for sure; |
| 3798 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3799 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3800 | break; |
| 3801 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3802 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 3803 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 3804 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3805 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3806 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 3807 | 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] | 3808 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3809 | case BPF_JLT: |
| 3810 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 3811 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 3812 | break; |
| 3813 | case BPF_JSLT: |
| 3814 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
| 3815 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 3816 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3817 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3818 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 3819 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 3820 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3821 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3822 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 3823 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3824 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3825 | case BPF_JLE: |
| 3826 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 3827 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 3828 | break; |
| 3829 | case BPF_JSLE: |
| 3830 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); |
| 3831 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 3832 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3833 | default: |
| 3834 | break; |
| 3835 | } |
| 3836 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3837 | __reg_deduce_bounds(false_reg); |
| 3838 | __reg_deduce_bounds(true_reg); |
| 3839 | /* We might have learned some bits from the bounds. */ |
| 3840 | __reg_bound_offset(false_reg); |
| 3841 | __reg_bound_offset(true_reg); |
| 3842 | /* Intersecting with the old var_off might have improved our bounds |
| 3843 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3844 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3845 | */ |
| 3846 | __update_reg_bounds(false_reg); |
| 3847 | __update_reg_bounds(true_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3848 | } |
| 3849 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3850 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 3851 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3852 | */ |
| 3853 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 3854 | struct bpf_reg_state *false_reg, u64 val, |
| 3855 | u8 opcode) |
| 3856 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3857 | if (__is_pointer_value(false, false_reg)) |
| 3858 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3859 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3860 | switch (opcode) { |
| 3861 | case BPF_JEQ: |
| 3862 | /* If this is false then we know nothing Jon Snow, but if it is |
| 3863 | * true then we know for sure. |
| 3864 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3865 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3866 | break; |
| 3867 | case BPF_JNE: |
| 3868 | /* If this is true we know nothing Jon Snow, but if it is false |
| 3869 | * we know the value for sure; |
| 3870 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3871 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3872 | break; |
| 3873 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3874 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 3875 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 3876 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3877 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3878 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 3879 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3880 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3881 | case BPF_JLT: |
| 3882 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 3883 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 3884 | break; |
| 3885 | case BPF_JSLT: |
| 3886 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); |
| 3887 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 3888 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3889 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3890 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 3891 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 3892 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3893 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3894 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 3895 | 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] | 3896 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3897 | case BPF_JLE: |
| 3898 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 3899 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 3900 | break; |
| 3901 | case BPF_JSLE: |
| 3902 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
| 3903 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 3904 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3905 | default: |
| 3906 | break; |
| 3907 | } |
| 3908 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3909 | __reg_deduce_bounds(false_reg); |
| 3910 | __reg_deduce_bounds(true_reg); |
| 3911 | /* We might have learned some bits from the bounds. */ |
| 3912 | __reg_bound_offset(false_reg); |
| 3913 | __reg_bound_offset(true_reg); |
| 3914 | /* Intersecting with the old var_off might have improved our bounds |
| 3915 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3916 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3917 | */ |
| 3918 | __update_reg_bounds(false_reg); |
| 3919 | __update_reg_bounds(true_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 3923 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 3924 | struct bpf_reg_state *dst_reg) |
| 3925 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3926 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 3927 | dst_reg->umin_value); |
| 3928 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 3929 | dst_reg->umax_value); |
| 3930 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 3931 | dst_reg->smin_value); |
| 3932 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 3933 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3934 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 3935 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3936 | /* We might have learned new bounds from the var_off. */ |
| 3937 | __update_reg_bounds(src_reg); |
| 3938 | __update_reg_bounds(dst_reg); |
| 3939 | /* We might have learned something about the sign bit. */ |
| 3940 | __reg_deduce_bounds(src_reg); |
| 3941 | __reg_deduce_bounds(dst_reg); |
| 3942 | /* We might have learned some bits from the bounds. */ |
| 3943 | __reg_bound_offset(src_reg); |
| 3944 | __reg_bound_offset(dst_reg); |
| 3945 | /* Intersecting with the old var_off might have improved our bounds |
| 3946 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3947 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3948 | */ |
| 3949 | __update_reg_bounds(src_reg); |
| 3950 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3951 | } |
| 3952 | |
| 3953 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 3954 | struct bpf_reg_state *true_dst, |
| 3955 | struct bpf_reg_state *false_src, |
| 3956 | struct bpf_reg_state *false_dst, |
| 3957 | u8 opcode) |
| 3958 | { |
| 3959 | switch (opcode) { |
| 3960 | case BPF_JEQ: |
| 3961 | __reg_combine_min_max(true_src, true_dst); |
| 3962 | break; |
| 3963 | case BPF_JNE: |
| 3964 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3965 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3966 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3967 | } |
| 3968 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3969 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 3970 | struct bpf_reg_state *reg, u32 id, |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 3971 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3972 | { |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 3973 | if (reg_type_may_be_null(reg->type) && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3974 | /* Old offset (both fixed and variable parts) should |
| 3975 | * have been known-zero, because we don't allow pointer |
| 3976 | * arithmetic on pointers that might be NULL. |
| 3977 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3978 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 3979 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3980 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3981 | __mark_reg_known_zero(reg); |
| 3982 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3983 | } |
| 3984 | if (is_null) { |
| 3985 | reg->type = SCALAR_VALUE; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 3986 | } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
| 3987 | if (reg->map_ptr->inner_map_meta) { |
| 3988 | reg->type = CONST_PTR_TO_MAP; |
| 3989 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
| 3990 | } else { |
| 3991 | reg->type = PTR_TO_MAP_VALUE; |
| 3992 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3993 | } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
| 3994 | reg->type = PTR_TO_SOCKET; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3995 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 3996 | if (is_null || !reg_is_refcounted(reg)) { |
| 3997 | /* We don't need id from this point onwards anymore, |
| 3998 | * thus we should better reset it, so that state |
| 3999 | * pruning has chances to take effect. |
| 4000 | */ |
| 4001 | reg->id = 0; |
| 4002 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4003 | } |
| 4004 | } |
| 4005 | |
| 4006 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 4007 | * be folded together at some point. |
| 4008 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4009 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 4010 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4011 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4012 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 4013 | struct bpf_reg_state *reg, *regs = state->regs; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 4014 | u32 id = regs[regno].id; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4015 | int i, j; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4016 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4017 | if (reg_is_refcounted_or_null(®s[regno]) && is_null) |
| 4018 | __release_reference_state(state, id); |
| 4019 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4020 | for (i = 0; i < MAX_BPF_REG; i++) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4021 | mark_ptr_or_null_reg(state, ®s[i], id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4022 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4023 | for (j = 0; j <= vstate->curframe; j++) { |
| 4024 | state = vstate->frame[j]; |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 4025 | bpf_for_each_spilled_reg(i, state, reg) { |
| 4026 | if (!reg) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4027 | continue; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4028 | mark_ptr_or_null_reg(state, reg, id, is_null); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4029 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4030 | } |
| 4031 | } |
| 4032 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 4033 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 4034 | struct bpf_reg_state *dst_reg, |
| 4035 | struct bpf_reg_state *src_reg, |
| 4036 | struct bpf_verifier_state *this_branch, |
| 4037 | struct bpf_verifier_state *other_branch) |
| 4038 | { |
| 4039 | if (BPF_SRC(insn->code) != BPF_X) |
| 4040 | return false; |
| 4041 | |
| 4042 | switch (BPF_OP(insn->code)) { |
| 4043 | case BPF_JGT: |
| 4044 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4045 | src_reg->type == PTR_TO_PACKET_END) || |
| 4046 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4047 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4048 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 4049 | find_good_pkt_pointers(this_branch, dst_reg, |
| 4050 | dst_reg->type, false); |
| 4051 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4052 | src_reg->type == PTR_TO_PACKET) || |
| 4053 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4054 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4055 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 4056 | find_good_pkt_pointers(other_branch, src_reg, |
| 4057 | src_reg->type, true); |
| 4058 | } else { |
| 4059 | return false; |
| 4060 | } |
| 4061 | break; |
| 4062 | case BPF_JLT: |
| 4063 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4064 | src_reg->type == PTR_TO_PACKET_END) || |
| 4065 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4066 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4067 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 4068 | find_good_pkt_pointers(other_branch, dst_reg, |
| 4069 | dst_reg->type, true); |
| 4070 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4071 | src_reg->type == PTR_TO_PACKET) || |
| 4072 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4073 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4074 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 4075 | find_good_pkt_pointers(this_branch, src_reg, |
| 4076 | src_reg->type, false); |
| 4077 | } else { |
| 4078 | return false; |
| 4079 | } |
| 4080 | break; |
| 4081 | case BPF_JGE: |
| 4082 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4083 | src_reg->type == PTR_TO_PACKET_END) || |
| 4084 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4085 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4086 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 4087 | find_good_pkt_pointers(this_branch, dst_reg, |
| 4088 | dst_reg->type, true); |
| 4089 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4090 | src_reg->type == PTR_TO_PACKET) || |
| 4091 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4092 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4093 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 4094 | find_good_pkt_pointers(other_branch, src_reg, |
| 4095 | src_reg->type, false); |
| 4096 | } else { |
| 4097 | return false; |
| 4098 | } |
| 4099 | break; |
| 4100 | case BPF_JLE: |
| 4101 | if ((dst_reg->type == PTR_TO_PACKET && |
| 4102 | src_reg->type == PTR_TO_PACKET_END) || |
| 4103 | (dst_reg->type == PTR_TO_PACKET_META && |
| 4104 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 4105 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 4106 | find_good_pkt_pointers(other_branch, dst_reg, |
| 4107 | dst_reg->type, false); |
| 4108 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 4109 | src_reg->type == PTR_TO_PACKET) || |
| 4110 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 4111 | src_reg->type == PTR_TO_PACKET_META)) { |
| 4112 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 4113 | find_good_pkt_pointers(this_branch, src_reg, |
| 4114 | src_reg->type, true); |
| 4115 | } else { |
| 4116 | return false; |
| 4117 | } |
| 4118 | break; |
| 4119 | default: |
| 4120 | return false; |
| 4121 | } |
| 4122 | |
| 4123 | return true; |
| 4124 | } |
| 4125 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4126 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4127 | struct bpf_insn *insn, int *insn_idx) |
| 4128 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4129 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 4130 | struct bpf_verifier_state *other_branch; |
| 4131 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
| 4132 | struct bpf_reg_state *dst_reg, *other_branch_regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4133 | u8 opcode = BPF_OP(insn->code); |
| 4134 | int err; |
| 4135 | |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 4136 | if (opcode > BPF_JSLE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4137 | verbose(env, "invalid BPF_JMP opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4138 | return -EINVAL; |
| 4139 | } |
| 4140 | |
| 4141 | if (BPF_SRC(insn->code) == BPF_X) { |
| 4142 | if (insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4143 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4144 | return -EINVAL; |
| 4145 | } |
| 4146 | |
| 4147 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4148 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4149 | if (err) |
| 4150 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4151 | |
| 4152 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4153 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4154 | insn->src_reg); |
| 4155 | return -EACCES; |
| 4156 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4157 | } else { |
| 4158 | if (insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4159 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4160 | return -EINVAL; |
| 4161 | } |
| 4162 | } |
| 4163 | |
| 4164 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4165 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4166 | if (err) |
| 4167 | return err; |
| 4168 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4169 | dst_reg = ®s[insn->dst_reg]; |
| 4170 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4171 | /* detect if R == 0 where R was initialized to zero earlier */ |
| 4172 | if (BPF_SRC(insn->code) == BPF_K && |
| 4173 | (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4174 | dst_reg->type == SCALAR_VALUE && |
Alexei Starovoitov | 3bf1592 | 2017-11-30 21:31:39 -0800 | [diff] [blame] | 4175 | tnum_is_const(dst_reg->var_off)) { |
| 4176 | if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) || |
| 4177 | (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4178 | /* if (imm == imm) goto pc+off; |
| 4179 | * only follow the goto, ignore fall-through |
| 4180 | */ |
| 4181 | *insn_idx += insn->off; |
| 4182 | return 0; |
| 4183 | } else { |
| 4184 | /* if (imm != imm) goto pc+off; |
| 4185 | * only follow fall-through branch, since |
| 4186 | * that's where the program will go |
| 4187 | */ |
| 4188 | return 0; |
| 4189 | } |
| 4190 | } |
| 4191 | |
| 4192 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); |
| 4193 | if (!other_branch) |
| 4194 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4195 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4196 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4197 | /* detect if we are comparing against a constant value so we can adjust |
| 4198 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4199 | * this is only legit if both are scalars (or pointers to the same |
| 4200 | * object, I suppose, but we don't support that right now), because |
| 4201 | * otherwise the different base pointers mean the offsets aren't |
| 4202 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4203 | */ |
| 4204 | if (BPF_SRC(insn->code) == BPF_X) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4205 | if (dst_reg->type == SCALAR_VALUE && |
| 4206 | regs[insn->src_reg].type == SCALAR_VALUE) { |
| 4207 | if (tnum_is_const(regs[insn->src_reg].var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4208 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4209 | dst_reg, regs[insn->src_reg].var_off.value, |
| 4210 | opcode); |
| 4211 | else if (tnum_is_const(dst_reg->var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4212 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4213 | ®s[insn->src_reg], |
| 4214 | dst_reg->var_off.value, opcode); |
| 4215 | else if (opcode == BPF_JEQ || opcode == BPF_JNE) |
| 4216 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4217 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 4218 | &other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4219 | ®s[insn->src_reg], |
| 4220 | ®s[insn->dst_reg], opcode); |
| 4221 | } |
| 4222 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4223 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 4224 | dst_reg, insn->imm, opcode); |
| 4225 | } |
| 4226 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4227 | /* 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] | 4228 | if (BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4229 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4230 | reg_type_may_be_null(dst_reg->type)) { |
| 4231 | /* Mark all identical registers in each branch as either |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 4232 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 4233 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 4234 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 4235 | opcode == BPF_JNE); |
| 4236 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 4237 | opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 4238 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 4239 | this_branch, other_branch) && |
| 4240 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4241 | verbose(env, "R%d pointer comparison prohibited\n", |
| 4242 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4243 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4244 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4245 | if (env->log.level) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4246 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4247 | return 0; |
| 4248 | } |
| 4249 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4250 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
| 4251 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) |
| 4252 | { |
| 4253 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; |
| 4254 | |
| 4255 | return (struct bpf_map *) (unsigned long) imm64; |
| 4256 | } |
| 4257 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4258 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4259 | 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] | 4260 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4261 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4262 | int err; |
| 4263 | |
| 4264 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4265 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4266 | return -EINVAL; |
| 4267 | } |
| 4268 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4269 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4270 | return -EINVAL; |
| 4271 | } |
| 4272 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4273 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4274 | if (err) |
| 4275 | return err; |
| 4276 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 4277 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 4278 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 4279 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4280 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4281 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4282 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 4283 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4284 | |
| 4285 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ |
| 4286 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); |
| 4287 | |
| 4288 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 4289 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); |
| 4290 | return 0; |
| 4291 | } |
| 4292 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 4293 | static bool may_access_skb(enum bpf_prog_type type) |
| 4294 | { |
| 4295 | switch (type) { |
| 4296 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 4297 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 4298 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 4299 | return true; |
| 4300 | default: |
| 4301 | return false; |
| 4302 | } |
| 4303 | } |
| 4304 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4305 | /* verify safety of LD_ABS|LD_IND instructions: |
| 4306 | * - they can only appear in the programs where ctx == skb |
| 4307 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 4308 | * preserve R6-R9, and store return value into R0 |
| 4309 | * |
| 4310 | * Implicit input: |
| 4311 | * ctx == skb == R6 == CTX |
| 4312 | * |
| 4313 | * Explicit input: |
| 4314 | * SRC == any register |
| 4315 | * IMM == 32-bit immediate |
| 4316 | * |
| 4317 | * Output: |
| 4318 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 4319 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4320 | 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] | 4321 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4322 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4323 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4324 | int i, err; |
| 4325 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 4326 | if (!may_access_skb(env->prog->type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4327 | 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] | 4328 | return -EINVAL; |
| 4329 | } |
| 4330 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 4331 | if (!env->ops->gen_ld_abs) { |
| 4332 | verbose(env, "bpf verifier is misconfigured\n"); |
| 4333 | return -EINVAL; |
| 4334 | } |
| 4335 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 4336 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4337 | /* when program has LD_ABS insn JITs and interpreter assume |
| 4338 | * that r1 == ctx == skb which is not the case for callees |
| 4339 | * that can have arbitrary arguments. It's problematic |
| 4340 | * for main prog as well since JITs would need to analyze |
| 4341 | * all functions in order to make proper register save/restore |
| 4342 | * decisions in the main prog. Hence disallow LD_ABS with calls |
| 4343 | */ |
| 4344 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); |
| 4345 | return -EINVAL; |
| 4346 | } |
| 4347 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4348 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 4349 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4350 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4351 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4352 | return -EINVAL; |
| 4353 | } |
| 4354 | |
| 4355 | /* check whether implicit source operand (register R6) is readable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4356 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4357 | if (err) |
| 4358 | return err; |
| 4359 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4360 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 4361 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 4362 | * reference leak. |
| 4363 | */ |
| 4364 | err = check_reference_leak(env); |
| 4365 | if (err) { |
| 4366 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 4367 | return err; |
| 4368 | } |
| 4369 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4370 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4371 | verbose(env, |
| 4372 | "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] | 4373 | return -EINVAL; |
| 4374 | } |
| 4375 | |
| 4376 | if (mode == BPF_IND) { |
| 4377 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4378 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4379 | if (err) |
| 4380 | return err; |
| 4381 | } |
| 4382 | |
| 4383 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4384 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4385 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4386 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 4387 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4388 | |
| 4389 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4390 | * the value fetched from the packet. |
| 4391 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4392 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4393 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4394 | return 0; |
| 4395 | } |
| 4396 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4397 | static int check_return_code(struct bpf_verifier_env *env) |
| 4398 | { |
| 4399 | struct bpf_reg_state *reg; |
| 4400 | struct tnum range = tnum_range(0, 1); |
| 4401 | |
| 4402 | switch (env->prog->type) { |
| 4403 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 4404 | case BPF_PROG_TYPE_CGROUP_SOCK: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 4405 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4406 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 4407 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4408 | break; |
| 4409 | default: |
| 4410 | return 0; |
| 4411 | } |
| 4412 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4413 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4414 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4415 | 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] | 4416 | reg_type_str[reg->type]); |
| 4417 | return -EINVAL; |
| 4418 | } |
| 4419 | |
| 4420 | if (!tnum_in(range, reg->var_off)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4421 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4422 | if (!tnum_is_unknown(reg->var_off)) { |
| 4423 | char tn_buf[48]; |
| 4424 | |
| 4425 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4426 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4427 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4428 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4429 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4430 | verbose(env, " should have been 0 or 1\n"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4431 | return -EINVAL; |
| 4432 | } |
| 4433 | return 0; |
| 4434 | } |
| 4435 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4436 | /* non-recursive DFS pseudo code |
| 4437 | * 1 procedure DFS-iterative(G,v): |
| 4438 | * 2 label v as discovered |
| 4439 | * 3 let S be a stack |
| 4440 | * 4 S.push(v) |
| 4441 | * 5 while S is not empty |
| 4442 | * 6 t <- S.pop() |
| 4443 | * 7 if t is what we're looking for: |
| 4444 | * 8 return t |
| 4445 | * 9 for all edges e in G.adjacentEdges(t) do |
| 4446 | * 10 if edge e is already labelled |
| 4447 | * 11 continue with the next edge |
| 4448 | * 12 w <- G.adjacentVertex(t,e) |
| 4449 | * 13 if vertex w is not discovered and not explored |
| 4450 | * 14 label e as tree-edge |
| 4451 | * 15 label w as discovered |
| 4452 | * 16 S.push(w) |
| 4453 | * 17 continue at 5 |
| 4454 | * 18 else if vertex w is discovered |
| 4455 | * 19 label e as back-edge |
| 4456 | * 20 else |
| 4457 | * 21 // vertex w is explored |
| 4458 | * 22 label e as forward- or cross-edge |
| 4459 | * 23 label t as explored |
| 4460 | * 24 S.pop() |
| 4461 | * |
| 4462 | * convention: |
| 4463 | * 0x10 - discovered |
| 4464 | * 0x11 - discovered and fall-through edge labelled |
| 4465 | * 0x12 - discovered and fall-through and branch edges labelled |
| 4466 | * 0x20 - explored |
| 4467 | */ |
| 4468 | |
| 4469 | enum { |
| 4470 | DISCOVERED = 0x10, |
| 4471 | EXPLORED = 0x20, |
| 4472 | FALLTHROUGH = 1, |
| 4473 | BRANCH = 2, |
| 4474 | }; |
| 4475 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4476 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4477 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4478 | static int *insn_stack; /* stack of insns to process */ |
| 4479 | static int cur_stack; /* current stack index */ |
| 4480 | static int *insn_state; |
| 4481 | |
| 4482 | /* t, w, e - match pseudo-code above: |
| 4483 | * t - index of current instruction |
| 4484 | * w - next instruction |
| 4485 | * e - edge |
| 4486 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4487 | 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] | 4488 | { |
| 4489 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 4490 | return 0; |
| 4491 | |
| 4492 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 4493 | return 0; |
| 4494 | |
| 4495 | if (w < 0 || w >= env->prog->len) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4496 | 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] | 4497 | return -EINVAL; |
| 4498 | } |
| 4499 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4500 | if (e == BRANCH) |
| 4501 | /* mark branch target for state pruning */ |
| 4502 | env->explored_states[w] = STATE_LIST_MARK; |
| 4503 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4504 | if (insn_state[w] == 0) { |
| 4505 | /* tree-edge */ |
| 4506 | insn_state[t] = DISCOVERED | e; |
| 4507 | insn_state[w] = DISCOVERED; |
| 4508 | if (cur_stack >= env->prog->len) |
| 4509 | return -E2BIG; |
| 4510 | insn_stack[cur_stack++] = w; |
| 4511 | return 1; |
| 4512 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4513 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4514 | return -EINVAL; |
| 4515 | } else if (insn_state[w] == EXPLORED) { |
| 4516 | /* forward- or cross-edge */ |
| 4517 | insn_state[t] = DISCOVERED | e; |
| 4518 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4519 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4520 | return -EFAULT; |
| 4521 | } |
| 4522 | return 0; |
| 4523 | } |
| 4524 | |
| 4525 | /* non-recursive depth-first-search to detect loops in BPF program |
| 4526 | * loop == back-edge in directed graph |
| 4527 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4528 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4529 | { |
| 4530 | struct bpf_insn *insns = env->prog->insnsi; |
| 4531 | int insn_cnt = env->prog->len; |
| 4532 | int ret = 0; |
| 4533 | int i, t; |
| 4534 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 4535 | ret = check_subprogs(env); |
| 4536 | if (ret < 0) |
| 4537 | return ret; |
| 4538 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4539 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 4540 | if (!insn_state) |
| 4541 | return -ENOMEM; |
| 4542 | |
| 4543 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 4544 | if (!insn_stack) { |
| 4545 | kfree(insn_state); |
| 4546 | return -ENOMEM; |
| 4547 | } |
| 4548 | |
| 4549 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 4550 | insn_stack[0] = 0; /* 0 is the first instruction */ |
| 4551 | cur_stack = 1; |
| 4552 | |
| 4553 | peek_stack: |
| 4554 | if (cur_stack == 0) |
| 4555 | goto check_state; |
| 4556 | t = insn_stack[cur_stack - 1]; |
| 4557 | |
| 4558 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { |
| 4559 | u8 opcode = BPF_OP(insns[t].code); |
| 4560 | |
| 4561 | if (opcode == BPF_EXIT) { |
| 4562 | goto mark_explored; |
| 4563 | } else if (opcode == BPF_CALL) { |
| 4564 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4565 | if (ret == 1) |
| 4566 | goto peek_stack; |
| 4567 | else if (ret < 0) |
| 4568 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 4569 | if (t + 1 < insn_cnt) |
| 4570 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 4571 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
| 4572 | env->explored_states[t] = STATE_LIST_MARK; |
| 4573 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); |
| 4574 | if (ret == 1) |
| 4575 | goto peek_stack; |
| 4576 | else if (ret < 0) |
| 4577 | goto err_free; |
| 4578 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4579 | } else if (opcode == BPF_JA) { |
| 4580 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 4581 | ret = -EINVAL; |
| 4582 | goto err_free; |
| 4583 | } |
| 4584 | /* unconditional jump with single edge */ |
| 4585 | ret = push_insn(t, t + insns[t].off + 1, |
| 4586 | FALLTHROUGH, env); |
| 4587 | if (ret == 1) |
| 4588 | goto peek_stack; |
| 4589 | else if (ret < 0) |
| 4590 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4591 | /* tell verifier to check for equivalent states |
| 4592 | * after every call and jump |
| 4593 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 4594 | if (t + 1 < insn_cnt) |
| 4595 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4596 | } else { |
| 4597 | /* conditional jump with two edges */ |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 4598 | env->explored_states[t] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4599 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4600 | if (ret == 1) |
| 4601 | goto peek_stack; |
| 4602 | else if (ret < 0) |
| 4603 | goto err_free; |
| 4604 | |
| 4605 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 4606 | if (ret == 1) |
| 4607 | goto peek_stack; |
| 4608 | else if (ret < 0) |
| 4609 | goto err_free; |
| 4610 | } |
| 4611 | } else { |
| 4612 | /* all other non-branch instructions with single |
| 4613 | * fall-through edge |
| 4614 | */ |
| 4615 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4616 | if (ret == 1) |
| 4617 | goto peek_stack; |
| 4618 | else if (ret < 0) |
| 4619 | goto err_free; |
| 4620 | } |
| 4621 | |
| 4622 | mark_explored: |
| 4623 | insn_state[t] = EXPLORED; |
| 4624 | if (cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4625 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4626 | ret = -EFAULT; |
| 4627 | goto err_free; |
| 4628 | } |
| 4629 | goto peek_stack; |
| 4630 | |
| 4631 | check_state: |
| 4632 | for (i = 0; i < insn_cnt; i++) { |
| 4633 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4634 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4635 | ret = -EINVAL; |
| 4636 | goto err_free; |
| 4637 | } |
| 4638 | } |
| 4639 | ret = 0; /* cfg looks good */ |
| 4640 | |
| 4641 | err_free: |
| 4642 | kfree(insn_state); |
| 4643 | kfree(insn_stack); |
| 4644 | return ret; |
| 4645 | } |
| 4646 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4647 | /* The minimum supported BTF func info size */ |
| 4648 | #define MIN_BPF_FUNCINFO_SIZE 8 |
| 4649 | #define MAX_FUNCINFO_REC_SIZE 252 |
| 4650 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4651 | static int check_btf_func(struct bpf_verifier_env *env, |
| 4652 | const union bpf_attr *attr, |
| 4653 | union bpf_attr __user *uattr) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4654 | { |
| 4655 | u32 i, nfuncs, urec_size, min_size, prev_offset; |
| 4656 | u32 krec_size = sizeof(struct bpf_func_info); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4657 | struct bpf_func_info *krecord; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4658 | const struct btf_type *type; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4659 | struct bpf_prog *prog; |
| 4660 | const struct btf *btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4661 | void __user *urecord; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4662 | int ret = 0; |
| 4663 | |
| 4664 | nfuncs = attr->func_info_cnt; |
| 4665 | if (!nfuncs) |
| 4666 | return 0; |
| 4667 | |
| 4668 | if (nfuncs != env->subprog_cnt) { |
| 4669 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); |
| 4670 | return -EINVAL; |
| 4671 | } |
| 4672 | |
| 4673 | urec_size = attr->func_info_rec_size; |
| 4674 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || |
| 4675 | urec_size > MAX_FUNCINFO_REC_SIZE || |
| 4676 | urec_size % sizeof(u32)) { |
| 4677 | verbose(env, "invalid func info rec size %u\n", urec_size); |
| 4678 | return -EINVAL; |
| 4679 | } |
| 4680 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4681 | prog = env->prog; |
| 4682 | btf = prog->aux->btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4683 | |
| 4684 | urecord = u64_to_user_ptr(attr->func_info); |
| 4685 | min_size = min_t(u32, krec_size, urec_size); |
| 4686 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4687 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4688 | if (!krecord) |
| 4689 | return -ENOMEM; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4690 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4691 | for (i = 0; i < nfuncs; i++) { |
| 4692 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); |
| 4693 | if (ret) { |
| 4694 | if (ret == -E2BIG) { |
| 4695 | verbose(env, "nonzero tailing record in func info"); |
| 4696 | /* set the size kernel expects so loader can zero |
| 4697 | * out the rest of the record. |
| 4698 | */ |
| 4699 | if (put_user(min_size, &uattr->func_info_rec_size)) |
| 4700 | ret = -EFAULT; |
| 4701 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4702 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4703 | } |
| 4704 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4705 | if (copy_from_user(&krecord[i], urecord, min_size)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4706 | ret = -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4707 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4708 | } |
| 4709 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4710 | /* check insn_off */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4711 | if (i == 0) { |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4712 | if (krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4713 | verbose(env, |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4714 | "nonzero insn_off %u for the first func info record", |
| 4715 | krecord[i].insn_off); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4716 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4717 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4718 | } |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4719 | } else if (krecord[i].insn_off <= prev_offset) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4720 | verbose(env, |
| 4721 | "same or smaller insn offset (%u) than previous func info record (%u)", |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4722 | krecord[i].insn_off, prev_offset); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4723 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4724 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4725 | } |
| 4726 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4727 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4728 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
| 4729 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4730 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4731 | } |
| 4732 | |
| 4733 | /* check type_id */ |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4734 | type = btf_type_by_id(btf, krecord[i].type_id); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4735 | if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) { |
| 4736 | verbose(env, "invalid type id %d in func info", |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4737 | krecord[i].type_id); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4738 | ret = -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4739 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4740 | } |
| 4741 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4742 | prev_offset = krecord[i].insn_off; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4743 | urecord += urec_size; |
| 4744 | } |
| 4745 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4746 | prog->aux->func_info = krecord; |
| 4747 | prog->aux->func_info_cnt = nfuncs; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4748 | return 0; |
| 4749 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4750 | err_free: |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4751 | kvfree(krecord); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 4752 | return ret; |
| 4753 | } |
| 4754 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4755 | static void adjust_btf_func(struct bpf_verifier_env *env) |
| 4756 | { |
| 4757 | int i; |
| 4758 | |
| 4759 | if (!env->prog->aux->func_info) |
| 4760 | return; |
| 4761 | |
| 4762 | for (i = 0; i < env->subprog_cnt; i++) |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 4763 | env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 4764 | } |
| 4765 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 4766 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
| 4767 | sizeof(((struct bpf_line_info *)(0))->line_col)) |
| 4768 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
| 4769 | |
| 4770 | static int check_btf_line(struct bpf_verifier_env *env, |
| 4771 | const union bpf_attr *attr, |
| 4772 | union bpf_attr __user *uattr) |
| 4773 | { |
| 4774 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; |
| 4775 | struct bpf_subprog_info *sub; |
| 4776 | struct bpf_line_info *linfo; |
| 4777 | struct bpf_prog *prog; |
| 4778 | const struct btf *btf; |
| 4779 | void __user *ulinfo; |
| 4780 | int err; |
| 4781 | |
| 4782 | nr_linfo = attr->line_info_cnt; |
| 4783 | if (!nr_linfo) |
| 4784 | return 0; |
| 4785 | |
| 4786 | rec_size = attr->line_info_rec_size; |
| 4787 | if (rec_size < MIN_BPF_LINEINFO_SIZE || |
| 4788 | rec_size > MAX_LINEINFO_REC_SIZE || |
| 4789 | rec_size & (sizeof(u32) - 1)) |
| 4790 | return -EINVAL; |
| 4791 | |
| 4792 | /* Need to zero it in case the userspace may |
| 4793 | * pass in a smaller bpf_line_info object. |
| 4794 | */ |
| 4795 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), |
| 4796 | GFP_KERNEL | __GFP_NOWARN); |
| 4797 | if (!linfo) |
| 4798 | return -ENOMEM; |
| 4799 | |
| 4800 | prog = env->prog; |
| 4801 | btf = prog->aux->btf; |
| 4802 | |
| 4803 | s = 0; |
| 4804 | sub = env->subprog_info; |
| 4805 | ulinfo = u64_to_user_ptr(attr->line_info); |
| 4806 | expected_size = sizeof(struct bpf_line_info); |
| 4807 | ncopy = min_t(u32, expected_size, rec_size); |
| 4808 | for (i = 0; i < nr_linfo; i++) { |
| 4809 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); |
| 4810 | if (err) { |
| 4811 | if (err == -E2BIG) { |
| 4812 | verbose(env, "nonzero tailing record in line_info"); |
| 4813 | if (put_user(expected_size, |
| 4814 | &uattr->line_info_rec_size)) |
| 4815 | err = -EFAULT; |
| 4816 | } |
| 4817 | goto err_free; |
| 4818 | } |
| 4819 | |
| 4820 | if (copy_from_user(&linfo[i], ulinfo, ncopy)) { |
| 4821 | err = -EFAULT; |
| 4822 | goto err_free; |
| 4823 | } |
| 4824 | |
| 4825 | /* |
| 4826 | * Check insn_off to ensure |
| 4827 | * 1) strictly increasing AND |
| 4828 | * 2) bounded by prog->len |
| 4829 | * |
| 4830 | * The linfo[0].insn_off == 0 check logically falls into |
| 4831 | * the later "missing bpf_line_info for func..." case |
| 4832 | * because the first linfo[0].insn_off must be the |
| 4833 | * first sub also and the first sub must have |
| 4834 | * subprog_info[0].start == 0. |
| 4835 | */ |
| 4836 | if ((i && linfo[i].insn_off <= prev_offset) || |
| 4837 | linfo[i].insn_off >= prog->len) { |
| 4838 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", |
| 4839 | i, linfo[i].insn_off, prev_offset, |
| 4840 | prog->len); |
| 4841 | err = -EINVAL; |
| 4842 | goto err_free; |
| 4843 | } |
| 4844 | |
| 4845 | if (!btf_name_offset_valid(btf, linfo[i].line_off) || |
| 4846 | !btf_name_offset_valid(btf, linfo[i].file_name_off)) { |
| 4847 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
| 4848 | err = -EINVAL; |
| 4849 | goto err_free; |
| 4850 | } |
| 4851 | |
| 4852 | if (s != env->subprog_cnt) { |
| 4853 | if (linfo[i].insn_off == sub[s].start) { |
| 4854 | sub[s].linfo_idx = i; |
| 4855 | s++; |
| 4856 | } else if (sub[s].start < linfo[i].insn_off) { |
| 4857 | verbose(env, "missing bpf_line_info for func#%u\n", s); |
| 4858 | err = -EINVAL; |
| 4859 | goto err_free; |
| 4860 | } |
| 4861 | } |
| 4862 | |
| 4863 | prev_offset = linfo[i].insn_off; |
| 4864 | ulinfo += rec_size; |
| 4865 | } |
| 4866 | |
| 4867 | if (s != env->subprog_cnt) { |
| 4868 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", |
| 4869 | env->subprog_cnt - s, s); |
| 4870 | err = -EINVAL; |
| 4871 | goto err_free; |
| 4872 | } |
| 4873 | |
| 4874 | prog->aux->linfo = linfo; |
| 4875 | prog->aux->nr_linfo = nr_linfo; |
| 4876 | |
| 4877 | return 0; |
| 4878 | |
| 4879 | err_free: |
| 4880 | kvfree(linfo); |
| 4881 | return err; |
| 4882 | } |
| 4883 | |
| 4884 | static int check_btf_info(struct bpf_verifier_env *env, |
| 4885 | const union bpf_attr *attr, |
| 4886 | union bpf_attr __user *uattr) |
| 4887 | { |
| 4888 | struct btf *btf; |
| 4889 | int err; |
| 4890 | |
| 4891 | if (!attr->func_info_cnt && !attr->line_info_cnt) |
| 4892 | return 0; |
| 4893 | |
| 4894 | btf = btf_get_by_fd(attr->prog_btf_fd); |
| 4895 | if (IS_ERR(btf)) |
| 4896 | return PTR_ERR(btf); |
| 4897 | env->prog->aux->btf = btf; |
| 4898 | |
| 4899 | err = check_btf_func(env, attr, uattr); |
| 4900 | if (err) |
| 4901 | return err; |
| 4902 | |
| 4903 | err = check_btf_line(env, attr, uattr); |
| 4904 | if (err) |
| 4905 | return err; |
| 4906 | |
| 4907 | return 0; |
| 4908 | } |
| 4909 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4910 | /* check %cur's range satisfies %old's */ |
| 4911 | static bool range_within(struct bpf_reg_state *old, |
| 4912 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4913 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4914 | return old->umin_value <= cur->umin_value && |
| 4915 | old->umax_value >= cur->umax_value && |
| 4916 | old->smin_value <= cur->smin_value && |
| 4917 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4918 | } |
| 4919 | |
| 4920 | /* Maximum number of register states that can exist at once */ |
| 4921 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 4922 | struct idpair { |
| 4923 | u32 old; |
| 4924 | u32 cur; |
| 4925 | }; |
| 4926 | |
| 4927 | /* If in the old state two registers had the same id, then they need to have |
| 4928 | * the same id in the new state as well. But that id could be different from |
| 4929 | * the old state, so we need to track the mapping from old to new ids. |
| 4930 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 4931 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 4932 | * regs with a different old id could still have new id 9, we don't care about |
| 4933 | * that. |
| 4934 | * So we look through our idmap to see if this old id has been seen before. If |
| 4935 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 4936 | */ |
| 4937 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 4938 | { |
| 4939 | unsigned int i; |
| 4940 | |
| 4941 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 4942 | if (!idmap[i].old) { |
| 4943 | /* Reached an empty slot; haven't seen this id before */ |
| 4944 | idmap[i].old = old_id; |
| 4945 | idmap[i].cur = cur_id; |
| 4946 | return true; |
| 4947 | } |
| 4948 | if (idmap[i].old == old_id) |
| 4949 | return idmap[i].cur == cur_id; |
| 4950 | } |
| 4951 | /* We ran out of idmap slots, which should be impossible */ |
| 4952 | WARN_ON_ONCE(1); |
| 4953 | return false; |
| 4954 | } |
| 4955 | |
| 4956 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4957 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 4958 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4959 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4960 | bool equal; |
| 4961 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4962 | if (!(rold->live & REG_LIVE_READ)) |
| 4963 | /* explored state didn't use this */ |
| 4964 | return true; |
| 4965 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 4966 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4967 | |
| 4968 | if (rold->type == PTR_TO_STACK) |
| 4969 | /* two stack pointers are equal only if they're pointing to |
| 4970 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 4971 | */ |
| 4972 | return equal && rold->frameno == rcur->frameno; |
| 4973 | |
| 4974 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4975 | return true; |
| 4976 | |
| 4977 | if (rold->type == NOT_INIT) |
| 4978 | /* explored state can't have used this */ |
| 4979 | return true; |
| 4980 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4981 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4982 | switch (rold->type) { |
| 4983 | case SCALAR_VALUE: |
| 4984 | if (rcur->type == SCALAR_VALUE) { |
| 4985 | /* new val must satisfy old val knowledge */ |
| 4986 | return range_within(rold, rcur) && |
| 4987 | tnum_in(rold->var_off, rcur->var_off); |
| 4988 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 4989 | /* We're trying to use a pointer in place of a scalar. |
| 4990 | * Even if the scalar was unbounded, this could lead to |
| 4991 | * pointer leaks because scalars are allowed to leak |
| 4992 | * while pointers are not. We could make this safe in |
| 4993 | * special cases if root is calling us, but it's |
| 4994 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4995 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 4996 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4997 | } |
| 4998 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4999 | /* If the new min/max/var_off satisfy the old ones and |
| 5000 | * everything else matches, we are OK. |
| 5001 | * We don't care about the 'id' value, because nothing |
| 5002 | * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL) |
| 5003 | */ |
| 5004 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 5005 | range_within(rold, rcur) && |
| 5006 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5007 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 5008 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 5009 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 5010 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 5011 | * checked, doing so could have affected others with the same |
| 5012 | * id, and we can't check for that because we lost the id when |
| 5013 | * we converted to a PTR_TO_MAP_VALUE. |
| 5014 | */ |
| 5015 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 5016 | return false; |
| 5017 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 5018 | return false; |
| 5019 | /* Check our ids match any regs they're supposed to */ |
| 5020 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5021 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5022 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5023 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5024 | return false; |
| 5025 | /* We must have at least as much range as the old ptr |
| 5026 | * did, so that any accesses which were safe before are |
| 5027 | * still safe. This is true even if old range < old off, |
| 5028 | * since someone could have accessed through (ptr - k), or |
| 5029 | * even done ptr -= k in a register, to get a safe access. |
| 5030 | */ |
| 5031 | if (rold->range > rcur->range) |
| 5032 | return false; |
| 5033 | /* If the offsets don't match, we can't trust our alignment; |
| 5034 | * nor can we be sure that we won't fall out of range. |
| 5035 | */ |
| 5036 | if (rold->off != rcur->off) |
| 5037 | return false; |
| 5038 | /* id relations must be preserved */ |
| 5039 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 5040 | return false; |
| 5041 | /* new val must satisfy old val knowledge */ |
| 5042 | return range_within(rold, rcur) && |
| 5043 | tnum_in(rold->var_off, rcur->var_off); |
| 5044 | case PTR_TO_CTX: |
| 5045 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5046 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 5047 | case PTR_TO_FLOW_KEYS: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5048 | case PTR_TO_SOCKET: |
| 5049 | case PTR_TO_SOCKET_OR_NULL: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5050 | /* Only valid matches are exact, which memcmp() above |
| 5051 | * would have accepted |
| 5052 | */ |
| 5053 | default: |
| 5054 | /* Don't know what's going on, just say it's not safe */ |
| 5055 | return false; |
| 5056 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5057 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5058 | /* Shouldn't get here; if we do, say it's not safe */ |
| 5059 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5060 | return false; |
| 5061 | } |
| 5062 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5063 | static bool stacksafe(struct bpf_func_state *old, |
| 5064 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5065 | struct idpair *idmap) |
| 5066 | { |
| 5067 | int i, spi; |
| 5068 | |
| 5069 | /* if explored stack has more populated slots than current stack |
| 5070 | * such stacks are not equivalent |
| 5071 | */ |
| 5072 | if (old->allocated_stack > cur->allocated_stack) |
| 5073 | return false; |
| 5074 | |
| 5075 | /* walk slots of the explored stack and ignore any additional |
| 5076 | * slots in the current stack, since explored(safe) state |
| 5077 | * didn't use them |
| 5078 | */ |
| 5079 | for (i = 0; i < old->allocated_stack; i++) { |
| 5080 | spi = i / BPF_REG_SIZE; |
| 5081 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 5082 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) |
| 5083 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 5084 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 5085 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5086 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 5087 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 5088 | /* if old state was safe with misc data in the stack |
| 5089 | * it will be safe with zero-initialized stack. |
| 5090 | * The opposite is not true |
| 5091 | */ |
| 5092 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 5093 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 5094 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5095 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 5096 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 5097 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 5098 | * this stack slot, but current has has STACK_MISC -> |
| 5099 | * this verifier states are not equivalent, |
| 5100 | * return false to continue verification of this path |
| 5101 | */ |
| 5102 | return false; |
| 5103 | if (i % BPF_REG_SIZE) |
| 5104 | continue; |
| 5105 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 5106 | continue; |
| 5107 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 5108 | &cur->stack[spi].spilled_ptr, |
| 5109 | idmap)) |
| 5110 | /* when explored and current stack slot are both storing |
| 5111 | * spilled registers, check that stored pointers types |
| 5112 | * are the same as well. |
| 5113 | * Ex: explored safe path could have stored |
| 5114 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 5115 | * but current path has stored: |
| 5116 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 5117 | * such verifier states are not equivalent. |
| 5118 | * return false to continue verification of this path |
| 5119 | */ |
| 5120 | return false; |
| 5121 | } |
| 5122 | return true; |
| 5123 | } |
| 5124 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5125 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 5126 | { |
| 5127 | if (old->acquired_refs != cur->acquired_refs) |
| 5128 | return false; |
| 5129 | return !memcmp(old->refs, cur->refs, |
| 5130 | sizeof(*old->refs) * old->acquired_refs); |
| 5131 | } |
| 5132 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5133 | /* compare two verifier states |
| 5134 | * |
| 5135 | * all states stored in state_list are known to be valid, since |
| 5136 | * verifier reached 'bpf_exit' instruction through them |
| 5137 | * |
| 5138 | * this function is called when verifier exploring different branches of |
| 5139 | * execution popped from the state stack. If it sees an old state that has |
| 5140 | * more strict register state and more strict stack state then this execution |
| 5141 | * branch doesn't need to be explored further, since verifier already |
| 5142 | * concluded that more strict state leads to valid finish. |
| 5143 | * |
| 5144 | * Therefore two states are equivalent if register state is more conservative |
| 5145 | * and explored stack state is more conservative than the current one. |
| 5146 | * Example: |
| 5147 | * explored current |
| 5148 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 5149 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 5150 | * |
| 5151 | * In other words if current stack state (one being explored) has more |
| 5152 | * valid slots than old one that already passed validation, it means |
| 5153 | * the verifier can stop exploring and conclude that current state is valid too |
| 5154 | * |
| 5155 | * Similarly with registers. If explored state has register type as invalid |
| 5156 | * whereas register type in current state is meaningful, it means that |
| 5157 | * the current state will reach 'bpf_exit' instruction safely |
| 5158 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5159 | static bool func_states_equal(struct bpf_func_state *old, |
| 5160 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5161 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5162 | struct idpair *idmap; |
| 5163 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5164 | int i; |
| 5165 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5166 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 5167 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 5168 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 5169 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5170 | |
| 5171 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 5172 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5173 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5174 | } |
| 5175 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5176 | if (!stacksafe(old, cur, idmap)) |
| 5177 | goto out_free; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5178 | |
| 5179 | if (!refsafe(old, cur)) |
| 5180 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5181 | ret = true; |
| 5182 | out_free: |
| 5183 | kfree(idmap); |
| 5184 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5185 | } |
| 5186 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5187 | static bool states_equal(struct bpf_verifier_env *env, |
| 5188 | struct bpf_verifier_state *old, |
| 5189 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5190 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5191 | int i; |
| 5192 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5193 | if (old->curframe != cur->curframe) |
| 5194 | return false; |
| 5195 | |
| 5196 | /* for states to be equal callsites have to be the same |
| 5197 | * and all frame states need to be equivalent |
| 5198 | */ |
| 5199 | for (i = 0; i <= old->curframe; i++) { |
| 5200 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 5201 | return false; |
| 5202 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 5203 | return false; |
| 5204 | } |
| 5205 | return true; |
| 5206 | } |
| 5207 | |
| 5208 | /* A write screens off any subsequent reads; but write marks come from the |
| 5209 | * straight-line code between a state and its parent. When we arrive at an |
| 5210 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 5211 | * code, so read marks in the state must propagate to the parent regardless |
| 5212 | * 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] | 5213 | * in mark_reg_read() is for. |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5214 | */ |
| 5215 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 5216 | const struct bpf_verifier_state *vstate, |
| 5217 | struct bpf_verifier_state *vparent) |
| 5218 | { |
| 5219 | int i, frame, err = 0; |
| 5220 | struct bpf_func_state *state, *parent; |
| 5221 | |
| 5222 | if (vparent->curframe != vstate->curframe) { |
| 5223 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 5224 | vparent->curframe, vstate->curframe); |
| 5225 | return -EFAULT; |
| 5226 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5227 | /* Propagate read liveness of registers... */ |
| 5228 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
| 5229 | /* We don't need to worry about FP liveness because it's read-only */ |
| 5230 | for (i = 0; i < BPF_REG_FP; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5231 | if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5232 | continue; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5233 | if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5234 | err = mark_reg_read(env, &vstate->frame[vstate->curframe]->regs[i], |
| 5235 | &vparent->frame[vstate->curframe]->regs[i]); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5236 | if (err) |
| 5237 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5238 | } |
| 5239 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5240 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5241 | /* ... and stack slots */ |
| 5242 | for (frame = 0; frame <= vstate->curframe; frame++) { |
| 5243 | state = vstate->frame[frame]; |
| 5244 | parent = vparent->frame[frame]; |
| 5245 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 5246 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5247 | if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ) |
| 5248 | continue; |
| 5249 | if (state->stack[i].spilled_ptr.live & REG_LIVE_READ) |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5250 | mark_reg_read(env, &state->stack[i].spilled_ptr, |
| 5251 | &parent->stack[i].spilled_ptr); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5252 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5253 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5254 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5255 | } |
| 5256 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5257 | 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] | 5258 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5259 | struct bpf_verifier_state_list *new_sl; |
| 5260 | struct bpf_verifier_state_list *sl; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5261 | struct bpf_verifier_state *cur = env->cur_state, *new; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5262 | int i, j, err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5263 | |
| 5264 | sl = env->explored_states[insn_idx]; |
| 5265 | if (!sl) |
| 5266 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 5267 | * be doing state search here |
| 5268 | */ |
| 5269 | return 0; |
| 5270 | |
| 5271 | while (sl != STATE_LIST_MARK) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5272 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5273 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5274 | * prune the search. |
| 5275 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 5276 | * If we have any write marks in env->cur_state, they |
| 5277 | * will prevent corresponding reads in the continuation |
| 5278 | * from reaching our parent (an explored_state). Our |
| 5279 | * own state will get the read marks recorded, but |
| 5280 | * they'll be immediately forgotten as we're pruning |
| 5281 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5282 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5283 | err = propagate_liveness(env, &sl->state, cur); |
| 5284 | if (err) |
| 5285 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5286 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5287 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5288 | sl = sl->next; |
| 5289 | } |
| 5290 | |
| 5291 | /* there were no equivalent states, remember current one. |
| 5292 | * technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5293 | * but it will either reach outer most bpf_exit (which means it's safe) |
| 5294 | * or it will be rejected. Since there are no loops, we won't be |
| 5295 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
| 5296 | * again on the way to bpf_exit |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5297 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5298 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5299 | if (!new_sl) |
| 5300 | return -ENOMEM; |
| 5301 | |
| 5302 | /* add new state to the head of linked list */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5303 | new = &new_sl->state; |
| 5304 | err = copy_verifier_state(new, cur); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 5305 | if (err) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5306 | free_verifier_state(new, false); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 5307 | kfree(new_sl); |
| 5308 | return err; |
| 5309 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5310 | new_sl->next = env->explored_states[insn_idx]; |
| 5311 | env->explored_states[insn_idx] = new_sl; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5312 | /* connect new state to parentage chain */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5313 | for (i = 0; i < BPF_REG_FP; i++) |
| 5314 | cur_regs(env)[i].parent = &new->frame[new->curframe]->regs[i]; |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 5315 | /* clear write marks in current state: the writes we did are not writes |
| 5316 | * our child did, so they don't screen off its reads from us. |
| 5317 | * (There are no read marks in current state, because reads always mark |
| 5318 | * their parent and current state never has children yet. Only |
| 5319 | * explored_states can get read marks.) |
| 5320 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5321 | for (i = 0; i < BPF_REG_FP; i++) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5322 | cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE; |
| 5323 | |
| 5324 | /* all stack frames are accessible from callee, clear them all */ |
| 5325 | for (j = 0; j <= cur->curframe; j++) { |
| 5326 | struct bpf_func_state *frame = cur->frame[j]; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5327 | struct bpf_func_state *newframe = new->frame[j]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5328 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5329 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 5330 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 5331 | frame->stack[i].spilled_ptr.parent = |
| 5332 | &newframe->stack[i].spilled_ptr; |
| 5333 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5334 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5335 | return 0; |
| 5336 | } |
| 5337 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5338 | /* Return true if it's OK to have the same insn return a different type. */ |
| 5339 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 5340 | { |
| 5341 | switch (type) { |
| 5342 | case PTR_TO_CTX: |
| 5343 | case PTR_TO_SOCKET: |
| 5344 | case PTR_TO_SOCKET_OR_NULL: |
| 5345 | return false; |
| 5346 | default: |
| 5347 | return true; |
| 5348 | } |
| 5349 | } |
| 5350 | |
| 5351 | /* If an instruction was previously used with particular pointer types, then we |
| 5352 | * need to be careful to avoid cases such as the below, where it may be ok |
| 5353 | * for one branch accessing the pointer, but not ok for the other branch: |
| 5354 | * |
| 5355 | * R1 = sock_ptr |
| 5356 | * goto X; |
| 5357 | * ... |
| 5358 | * R1 = some_other_valid_ptr; |
| 5359 | * goto X; |
| 5360 | * ... |
| 5361 | * R2 = *(u32 *)(R1 + 0); |
| 5362 | */ |
| 5363 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 5364 | { |
| 5365 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 5366 | !reg_type_mismatch_ok(prev)); |
| 5367 | } |
| 5368 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5369 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5370 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5371 | struct bpf_verifier_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5372 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5373 | struct bpf_reg_state *regs; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5374 | int insn_cnt = env->prog->len, i; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5375 | int insn_idx, prev_insn_idx = 0; |
| 5376 | int insn_processed = 0; |
| 5377 | bool do_print_state = false; |
| 5378 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5379 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 5380 | if (!state) |
| 5381 | return -ENOMEM; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5382 | state->curframe = 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5383 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 5384 | if (!state->frame[0]) { |
| 5385 | kfree(state); |
| 5386 | return -ENOMEM; |
| 5387 | } |
| 5388 | env->cur_state = state; |
| 5389 | init_func_state(env, state->frame[0], |
| 5390 | BPF_MAIN_FUNC /* callsite */, |
| 5391 | 0 /* frameno */, |
| 5392 | 0 /* subprogno, zero == main subprog */); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5393 | insn_idx = 0; |
| 5394 | for (;;) { |
| 5395 | struct bpf_insn *insn; |
| 5396 | u8 class; |
| 5397 | int err; |
| 5398 | |
| 5399 | if (insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5400 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5401 | insn_idx, insn_cnt); |
| 5402 | return -EFAULT; |
| 5403 | } |
| 5404 | |
| 5405 | insn = &insns[insn_idx]; |
| 5406 | class = BPF_CLASS(insn->code); |
| 5407 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 5408 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5409 | verbose(env, |
| 5410 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5411 | insn_processed); |
| 5412 | return -E2BIG; |
| 5413 | } |
| 5414 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5415 | err = is_state_visited(env, insn_idx); |
| 5416 | if (err < 0) |
| 5417 | return err; |
| 5418 | if (err == 1) { |
| 5419 | /* found equivalent state, can prune the search */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5420 | if (env->log.level) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5421 | if (do_print_state) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5422 | verbose(env, "\nfrom %d to %d: safe\n", |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5423 | prev_insn_idx, insn_idx); |
| 5424 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5425 | verbose(env, "%d: safe\n", insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5426 | } |
| 5427 | goto process_bpf_exit; |
| 5428 | } |
| 5429 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 5430 | if (need_resched()) |
| 5431 | cond_resched(); |
| 5432 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5433 | if (env->log.level > 1 || (env->log.level && do_print_state)) { |
| 5434 | if (env->log.level > 1) |
| 5435 | verbose(env, "%d:", insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 5436 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5437 | verbose(env, "\nfrom %d to %d:", |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 5438 | prev_insn_idx, insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5439 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5440 | do_print_state = false; |
| 5441 | } |
| 5442 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5443 | if (env->log.level) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5444 | const struct bpf_insn_cbs cbs = { |
| 5445 | .cb_print = verbose, |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 5446 | .private_data = env, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5447 | }; |
| 5448 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5449 | verbose(env, "%d: ", insn_idx); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 5450 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5451 | } |
| 5452 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 5453 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
| 5454 | err = bpf_prog_offload_verify_insn(env, insn_idx, |
| 5455 | prev_insn_idx); |
| 5456 | if (err) |
| 5457 | return err; |
| 5458 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 5459 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5460 | regs = cur_regs(env); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5461 | env->insn_aux_data[insn_idx].seen = true; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5462 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5463 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5464 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5465 | if (err) |
| 5466 | return err; |
| 5467 | |
| 5468 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5469 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5470 | |
| 5471 | /* check for reserved fields is already done */ |
| 5472 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5473 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5474 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5475 | if (err) |
| 5476 | return err; |
| 5477 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5478 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5479 | if (err) |
| 5480 | return err; |
| 5481 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 5482 | src_reg_type = regs[insn->src_reg].type; |
| 5483 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5484 | /* check that memory (src_reg + off) is readable, |
| 5485 | * the state of dst_reg will be updated by this func |
| 5486 | */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5487 | err = check_mem_access(env, insn_idx, insn->src_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5488 | BPF_SIZE(insn->code), BPF_READ, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 5489 | insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5490 | if (err) |
| 5491 | return err; |
| 5492 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5493 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 5494 | |
| 5495 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5496 | /* saw a valid insn |
| 5497 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5498 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5499 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5500 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5501 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5502 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5503 | /* ABuser program is trying to use the same insn |
| 5504 | * dst_reg = *(u32*) (src_reg + off) |
| 5505 | * with different pointer types: |
| 5506 | * src_reg == ctx in one branch and |
| 5507 | * src_reg == stack|map in some other branch. |
| 5508 | * Reject it. |
| 5509 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5510 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5511 | return -EINVAL; |
| 5512 | } |
| 5513 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5514 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5515 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5516 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5517 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5518 | err = check_xadd(env, insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5519 | if (err) |
| 5520 | return err; |
| 5521 | insn_idx++; |
| 5522 | continue; |
| 5523 | } |
| 5524 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5525 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5526 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5527 | if (err) |
| 5528 | return err; |
| 5529 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5530 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5531 | if (err) |
| 5532 | return err; |
| 5533 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5534 | dst_reg_type = regs[insn->dst_reg].type; |
| 5535 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5536 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5537 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5538 | BPF_SIZE(insn->code), BPF_WRITE, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 5539 | insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5540 | if (err) |
| 5541 | return err; |
| 5542 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5543 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 5544 | |
| 5545 | if (*prev_dst_type == NOT_INIT) { |
| 5546 | *prev_dst_type = dst_reg_type; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5547 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5548 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5549 | return -EINVAL; |
| 5550 | } |
| 5551 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5552 | } else if (class == BPF_ST) { |
| 5553 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 5554 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5555 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5556 | return -EINVAL; |
| 5557 | } |
| 5558 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5559 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5560 | if (err) |
| 5561 | return err; |
| 5562 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 5563 | if (is_ctx_reg(env, insn->dst_reg)) { |
Joe Stringer | 9d2be44 | 2018-10-02 13:35:31 -0700 | [diff] [blame] | 5564 | 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] | 5565 | insn->dst_reg, |
| 5566 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 5567 | return -EACCES; |
| 5568 | } |
| 5569 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5570 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5571 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5572 | BPF_SIZE(insn->code), BPF_WRITE, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 5573 | -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5574 | if (err) |
| 5575 | return err; |
| 5576 | |
| 5577 | } else if (class == BPF_JMP) { |
| 5578 | u8 opcode = BPF_OP(insn->code); |
| 5579 | |
| 5580 | if (opcode == BPF_CALL) { |
| 5581 | if (BPF_SRC(insn->code) != BPF_K || |
| 5582 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5583 | (insn->src_reg != BPF_REG_0 && |
| 5584 | insn->src_reg != BPF_PSEUDO_CALL) || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5585 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5586 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5587 | return -EINVAL; |
| 5588 | } |
| 5589 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5590 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 5591 | err = check_func_call(env, insn, &insn_idx); |
| 5592 | else |
| 5593 | err = check_helper_call(env, insn->imm, insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5594 | if (err) |
| 5595 | return err; |
| 5596 | |
| 5597 | } else if (opcode == BPF_JA) { |
| 5598 | if (BPF_SRC(insn->code) != BPF_K || |
| 5599 | insn->imm != 0 || |
| 5600 | insn->src_reg != BPF_REG_0 || |
| 5601 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5602 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5603 | return -EINVAL; |
| 5604 | } |
| 5605 | |
| 5606 | insn_idx += insn->off + 1; |
| 5607 | continue; |
| 5608 | |
| 5609 | } else if (opcode == BPF_EXIT) { |
| 5610 | if (BPF_SRC(insn->code) != BPF_K || |
| 5611 | insn->imm != 0 || |
| 5612 | insn->src_reg != BPF_REG_0 || |
| 5613 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5614 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5615 | return -EINVAL; |
| 5616 | } |
| 5617 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5618 | if (state->curframe) { |
| 5619 | /* exit from nested function */ |
| 5620 | prev_insn_idx = insn_idx; |
| 5621 | err = prepare_func_exit(env, &insn_idx); |
| 5622 | if (err) |
| 5623 | return err; |
| 5624 | do_print_state = true; |
| 5625 | continue; |
| 5626 | } |
| 5627 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5628 | err = check_reference_leak(env); |
| 5629 | if (err) |
| 5630 | return err; |
| 5631 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5632 | /* eBPF calling convetion is such that R0 is used |
| 5633 | * to return the value from eBPF program. |
| 5634 | * Make sure that it's readable at this time |
| 5635 | * of bpf_exit, which means that program wrote |
| 5636 | * something into it earlier |
| 5637 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5638 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5639 | if (err) |
| 5640 | return err; |
| 5641 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5642 | if (is_pointer_value(env, BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5643 | verbose(env, "R0 leaks addr as return value\n"); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5644 | return -EACCES; |
| 5645 | } |
| 5646 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 5647 | err = check_return_code(env); |
| 5648 | if (err) |
| 5649 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5650 | process_bpf_exit: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5651 | err = pop_stack(env, &prev_insn_idx, &insn_idx); |
| 5652 | if (err < 0) { |
| 5653 | if (err != -ENOENT) |
| 5654 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5655 | break; |
| 5656 | } else { |
| 5657 | do_print_state = true; |
| 5658 | continue; |
| 5659 | } |
| 5660 | } else { |
| 5661 | err = check_cond_jmp_op(env, insn, &insn_idx); |
| 5662 | if (err) |
| 5663 | return err; |
| 5664 | } |
| 5665 | } else if (class == BPF_LD) { |
| 5666 | u8 mode = BPF_MODE(insn->code); |
| 5667 | |
| 5668 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 5669 | err = check_ld_abs(env, insn); |
| 5670 | if (err) |
| 5671 | return err; |
| 5672 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5673 | } else if (mode == BPF_IMM) { |
| 5674 | err = check_ld_imm(env, insn); |
| 5675 | if (err) |
| 5676 | return err; |
| 5677 | |
| 5678 | insn_idx++; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5679 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5680 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5681 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5682 | return -EINVAL; |
| 5683 | } |
| 5684 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5685 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5686 | return -EINVAL; |
| 5687 | } |
| 5688 | |
| 5689 | insn_idx++; |
| 5690 | } |
| 5691 | |
Daniel Borkmann | 4bd95f4 | 2018-01-20 01:24:36 +0100 | [diff] [blame] | 5692 | verbose(env, "processed %d insns (limit %d), stack depth ", |
| 5693 | insn_processed, BPF_COMPLEXITY_LIMIT_INSNS); |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5694 | for (i = 0; i < env->subprog_cnt; i++) { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 5695 | u32 depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5696 | |
| 5697 | verbose(env, "%d", depth); |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5698 | if (i + 1 < env->subprog_cnt) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5699 | verbose(env, "+"); |
| 5700 | } |
| 5701 | verbose(env, "\n"); |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 5702 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5703 | return 0; |
| 5704 | } |
| 5705 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5706 | static int check_map_prealloc(struct bpf_map *map) |
| 5707 | { |
| 5708 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 5709 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 5710 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5711 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 5712 | } |
| 5713 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5714 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 5715 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5716 | struct bpf_prog *prog) |
| 5717 | |
| 5718 | { |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5719 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 5720 | * preallocated hash maps, since doing memory allocation |
| 5721 | * in overflow_handler can crash depending on where nmi got |
| 5722 | * triggered. |
| 5723 | */ |
| 5724 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 5725 | if (!check_map_prealloc(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5726 | 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] | 5727 | return -EINVAL; |
| 5728 | } |
| 5729 | if (map->inner_map_meta && |
| 5730 | !check_map_prealloc(map->inner_map_meta)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5731 | 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] | 5732 | return -EINVAL; |
| 5733 | } |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5734 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 5735 | |
| 5736 | 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] | 5737 | !bpf_offload_prog_map_match(prog, map)) { |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 5738 | verbose(env, "offload device mismatch between prog and map\n"); |
| 5739 | return -EINVAL; |
| 5740 | } |
| 5741 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5742 | return 0; |
| 5743 | } |
| 5744 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 5745 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 5746 | { |
| 5747 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 5748 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 5749 | } |
| 5750 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5751 | /* look for pseudo eBPF instructions that access map FDs and |
| 5752 | * replace them with actual map pointers |
| 5753 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5754 | 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] | 5755 | { |
| 5756 | struct bpf_insn *insn = env->prog->insnsi; |
| 5757 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5758 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5759 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 5760 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 5761 | if (err) |
| 5762 | return err; |
| 5763 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5764 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5765 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5766 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5767 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5768 | return -EINVAL; |
| 5769 | } |
| 5770 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5771 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 5772 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 5773 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5774 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5775 | return -EINVAL; |
| 5776 | } |
| 5777 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5778 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
| 5779 | struct bpf_map *map; |
| 5780 | struct fd f; |
| 5781 | |
| 5782 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 5783 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 5784 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5785 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5786 | return -EINVAL; |
| 5787 | } |
| 5788 | |
| 5789 | if (insn->src_reg == 0) |
| 5790 | /* valid generic load 64-bit imm */ |
| 5791 | goto next_insn; |
| 5792 | |
| 5793 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5794 | verbose(env, |
| 5795 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5796 | return -EINVAL; |
| 5797 | } |
| 5798 | |
| 5799 | f = fdget(insn->imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 5800 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5801 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5802 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5803 | insn->imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5804 | return PTR_ERR(map); |
| 5805 | } |
| 5806 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5807 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5808 | if (err) { |
| 5809 | fdput(f); |
| 5810 | return err; |
| 5811 | } |
| 5812 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5813 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
| 5814 | insn[0].imm = (u32) (unsigned long) map; |
| 5815 | insn[1].imm = ((u64) (unsigned long) map) >> 32; |
| 5816 | |
| 5817 | /* check whether we recorded this map already */ |
| 5818 | for (j = 0; j < env->used_map_cnt; j++) |
| 5819 | if (env->used_maps[j] == map) { |
| 5820 | fdput(f); |
| 5821 | goto next_insn; |
| 5822 | } |
| 5823 | |
| 5824 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 5825 | fdput(f); |
| 5826 | return -E2BIG; |
| 5827 | } |
| 5828 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5829 | /* hold the map. If the program is rejected by verifier, |
| 5830 | * the map will be released by release_maps() or it |
| 5831 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 5832 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5833 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 5834 | map = bpf_map_inc(map, false); |
| 5835 | if (IS_ERR(map)) { |
| 5836 | fdput(f); |
| 5837 | return PTR_ERR(map); |
| 5838 | } |
| 5839 | env->used_maps[env->used_map_cnt++] = map; |
| 5840 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 5841 | if (bpf_map_is_cgroup_storage(map) && |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 5842 | bpf_cgroup_storage_assign(env->prog, map)) { |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 5843 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 5844 | fdput(f); |
| 5845 | return -EBUSY; |
| 5846 | } |
| 5847 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5848 | fdput(f); |
| 5849 | next_insn: |
| 5850 | insn++; |
| 5851 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 5852 | continue; |
| 5853 | } |
| 5854 | |
| 5855 | /* Basic sanity check before we invest more work here. */ |
| 5856 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 5857 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 5858 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5859 | } |
| 5860 | } |
| 5861 | |
| 5862 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 5863 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 5864 | * These pointers will be used later by verifier to validate map access. |
| 5865 | */ |
| 5866 | return 0; |
| 5867 | } |
| 5868 | |
| 5869 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5870 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5871 | { |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 5872 | enum bpf_cgroup_storage_type stype; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5873 | int i; |
| 5874 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 5875 | for_each_cgroup_storage_type(stype) { |
| 5876 | if (!env->prog->aux->cgroup_storage[stype]) |
| 5877 | continue; |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 5878 | bpf_cgroup_storage_release(env->prog, |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 5879 | env->prog->aux->cgroup_storage[stype]); |
| 5880 | } |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 5881 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5882 | for (i = 0; i < env->used_map_cnt; i++) |
| 5883 | bpf_map_put(env->used_maps[i]); |
| 5884 | } |
| 5885 | |
| 5886 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5887 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5888 | { |
| 5889 | struct bpf_insn *insn = env->prog->insnsi; |
| 5890 | int insn_cnt = env->prog->len; |
| 5891 | int i; |
| 5892 | |
| 5893 | for (i = 0; i < insn_cnt; i++, insn++) |
| 5894 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 5895 | insn->src_reg = 0; |
| 5896 | } |
| 5897 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5898 | /* single env->prog->insni[off] instruction was replaced with the range |
| 5899 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 5900 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 5901 | */ |
| 5902 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, |
| 5903 | u32 off, u32 cnt) |
| 5904 | { |
| 5905 | 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] | 5906 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5907 | |
| 5908 | if (cnt == 1) |
| 5909 | return 0; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 5910 | new_data = vzalloc(array_size(prog_len, |
| 5911 | sizeof(struct bpf_insn_aux_data))); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5912 | if (!new_data) |
| 5913 | return -ENOMEM; |
| 5914 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 5915 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 5916 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5917 | for (i = off; i < off + cnt - 1; i++) |
| 5918 | new_data[i].seen = true; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5919 | env->insn_aux_data = new_data; |
| 5920 | vfree(old_data); |
| 5921 | return 0; |
| 5922 | } |
| 5923 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5924 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 5925 | { |
| 5926 | int i; |
| 5927 | |
| 5928 | if (len == 1) |
| 5929 | return; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 5930 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 5931 | for (i = 0; i <= env->subprog_cnt; i++) { |
Edward Cree | afd5942 | 2018-11-16 12:00:07 +0000 | [diff] [blame] | 5932 | if (env->subprog_info[i].start <= off) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5933 | continue; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 5934 | env->subprog_info[i].start += len - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5935 | } |
| 5936 | } |
| 5937 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5938 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 5939 | const struct bpf_insn *patch, u32 len) |
| 5940 | { |
| 5941 | struct bpf_prog *new_prog; |
| 5942 | |
| 5943 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
| 5944 | if (!new_prog) |
| 5945 | return NULL; |
| 5946 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) |
| 5947 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5948 | adjust_subprog_starts(env, off, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5949 | return new_prog; |
| 5950 | } |
| 5951 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 5952 | /* The verifier does more data flow analysis than llvm and will not |
| 5953 | * explore branches that are dead at run time. Malicious programs can |
| 5954 | * have dead code too. Therefore replace all dead at-run-time code |
| 5955 | * with 'ja -1'. |
| 5956 | * |
| 5957 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 5958 | * program and through another bug we would manage to jump there, then |
| 5959 | * we'd execute beyond program memory otherwise. Returning exception |
| 5960 | * code also wouldn't work since we can have subprogs where the dead |
| 5961 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5962 | */ |
| 5963 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 5964 | { |
| 5965 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 5966 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5967 | struct bpf_insn *insn = env->prog->insnsi; |
| 5968 | const int insn_cnt = env->prog->len; |
| 5969 | int i; |
| 5970 | |
| 5971 | for (i = 0; i < insn_cnt; i++) { |
| 5972 | if (aux_data[i].seen) |
| 5973 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 5974 | memcpy(insn + i, &trap, sizeof(trap)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5975 | } |
| 5976 | } |
| 5977 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 5978 | /* convert load instructions that access fields of a context type into a |
| 5979 | * sequence of instructions that access fields of the underlying structure: |
| 5980 | * struct __sk_buff -> struct sk_buff |
| 5981 | * struct bpf_sock_ops -> struct sock |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5982 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5983 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5984 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5985 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5986 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5987 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5988 | struct bpf_insn insn_buf[16], *insn; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 5989 | u32 target_size, size_default, off; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5990 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5991 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5992 | bool is_narrower_load; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5993 | |
Daniel Borkmann | b09928b | 2018-10-24 22:05:49 +0200 | [diff] [blame] | 5994 | if (ops->gen_prologue || env->seen_direct_write) { |
| 5995 | if (!ops->gen_prologue) { |
| 5996 | verbose(env, "bpf verifier is misconfigured\n"); |
| 5997 | return -EINVAL; |
| 5998 | } |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5999 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 6000 | env->prog); |
| 6001 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6002 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6003 | return -EINVAL; |
| 6004 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 6005 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6006 | if (!new_prog) |
| 6007 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 6008 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6009 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6010 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6011 | } |
| 6012 | } |
| 6013 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6014 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6015 | return 0; |
| 6016 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6017 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6018 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6019 | for (i = 0; i < insn_cnt; i++, insn++) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6020 | bpf_convert_ctx_access_t convert_ctx_access; |
| 6021 | |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 6022 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 6023 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 6024 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 6025 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6026 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 6027 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 6028 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 6029 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 6030 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 6031 | type = BPF_WRITE; |
| 6032 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6033 | continue; |
| 6034 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 6035 | if (type == BPF_WRITE && |
| 6036 | env->insn_aux_data[i + delta].sanitize_stack_off) { |
| 6037 | struct bpf_insn patch[] = { |
| 6038 | /* Sanitize suspicious stack slot with zero. |
| 6039 | * There are no memory dependencies for this store, |
| 6040 | * since it's only using frame pointer and immediate |
| 6041 | * constant of zero |
| 6042 | */ |
| 6043 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, |
| 6044 | env->insn_aux_data[i + delta].sanitize_stack_off, |
| 6045 | 0), |
| 6046 | /* the original STX instruction will immediately |
| 6047 | * overwrite the same stack slot with appropriate value |
| 6048 | */ |
| 6049 | *insn, |
| 6050 | }; |
| 6051 | |
| 6052 | cnt = ARRAY_SIZE(patch); |
| 6053 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 6054 | if (!new_prog) |
| 6055 | return -ENOMEM; |
| 6056 | |
| 6057 | delta += cnt - 1; |
| 6058 | env->prog = new_prog; |
| 6059 | insn = new_prog->insnsi + i + delta; |
| 6060 | continue; |
| 6061 | } |
| 6062 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6063 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 6064 | case PTR_TO_CTX: |
| 6065 | if (!ops->convert_ctx_access) |
| 6066 | continue; |
| 6067 | convert_ctx_access = ops->convert_ctx_access; |
| 6068 | break; |
| 6069 | case PTR_TO_SOCKET: |
| 6070 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 6071 | break; |
| 6072 | default: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6073 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6074 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6075 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6076 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6077 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6078 | |
| 6079 | /* If the read access is a narrower load of the field, |
| 6080 | * convert to a 4/8-byte load, to minimum program type specific |
| 6081 | * convert_ctx_access changes. If conversion is successful, |
| 6082 | * we will apply proper mask to the result. |
| 6083 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6084 | is_narrower_load = size < ctx_field_size; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 6085 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
| 6086 | off = insn->off; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6087 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6088 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6089 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6090 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6091 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6092 | return -EINVAL; |
| 6093 | } |
| 6094 | |
| 6095 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6096 | if (ctx_field_size == 4) |
| 6097 | size_code = BPF_W; |
| 6098 | else if (ctx_field_size == 8) |
| 6099 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6100 | |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 6101 | insn->off = off & ~(size_default - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6102 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 6103 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6104 | |
| 6105 | target_size = 0; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6106 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 6107 | &target_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6108 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 6109 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6110 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6111 | return -EINVAL; |
| 6112 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6113 | |
| 6114 | if (is_narrower_load && size < target_size) { |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 6115 | u8 shift = (off & (size_default - 1)) * 8; |
| 6116 | |
| 6117 | if (ctx_field_size <= 4) { |
| 6118 | if (shift) |
| 6119 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, |
| 6120 | insn->dst_reg, |
| 6121 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6122 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6123 | (1 << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 6124 | } else { |
| 6125 | if (shift) |
| 6126 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, |
| 6127 | insn->dst_reg, |
| 6128 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6129 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 6130 | (1 << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 6131 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 6132 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6133 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 6134 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6135 | if (!new_prog) |
| 6136 | return -ENOMEM; |
| 6137 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6138 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6139 | |
| 6140 | /* keep walking new program and skip insns we just inserted */ |
| 6141 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6142 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6143 | } |
| 6144 | |
| 6145 | return 0; |
| 6146 | } |
| 6147 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6148 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 6149 | { |
| 6150 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 6151 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6152 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6153 | void *old_bpf_func; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6154 | int err; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6155 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6156 | if (env->subprog_cnt <= 1) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6157 | return 0; |
| 6158 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6159 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6160 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 6161 | insn->src_reg != BPF_PSEUDO_CALL) |
| 6162 | continue; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 6163 | /* Upon error here we cannot fall back to interpreter but |
| 6164 | * need a hard reject of the program. Thus -EFAULT is |
| 6165 | * propagated in any case. |
| 6166 | */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6167 | subprog = find_subprog(env, i + insn->imm + 1); |
| 6168 | if (subprog < 0) { |
| 6169 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 6170 | i + insn->imm + 1); |
| 6171 | return -EFAULT; |
| 6172 | } |
| 6173 | /* temporarily remember subprog id inside insn instead of |
| 6174 | * aux_data, since next loop will split up all insns into funcs |
| 6175 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6176 | insn->off = subprog; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6177 | /* remember original imm in case JIT fails and fallback |
| 6178 | * to interpreter will be needed |
| 6179 | */ |
| 6180 | env->insn_aux_data[i].call_imm = insn->imm; |
| 6181 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 6182 | insn->imm = 1; |
| 6183 | } |
| 6184 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6185 | err = bpf_prog_alloc_jited_linfo(prog); |
| 6186 | if (err) |
| 6187 | goto out_undo_insn; |
| 6188 | |
| 6189 | err = -ENOMEM; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 6190 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6191 | if (!func) |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 6192 | goto out_undo_insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6193 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6194 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6195 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 6196 | subprog_end = env->subprog_info[i + 1].start; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6197 | |
| 6198 | len = subprog_end - subprog_start; |
| 6199 | func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER); |
| 6200 | if (!func[i]) |
| 6201 | goto out_free; |
| 6202 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 6203 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 6204 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6205 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 6206 | if (bpf_prog_calc_tag(func[i])) |
| 6207 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6208 | func[i]->is_func = 1; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6209 | func[i]->aux->func_idx = i; |
| 6210 | /* the btf and func_info will be freed only at prog->aux */ |
| 6211 | func[i]->aux->btf = prog->aux->btf; |
| 6212 | func[i]->aux->func_info = prog->aux->func_info; |
| 6213 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6214 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 6215 | * Long term would need debug info to populate names |
| 6216 | */ |
| 6217 | func[i]->aux->name[0] = 'F'; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 6218 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6219 | func[i]->jit_requested = 1; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6220 | func[i]->aux->linfo = prog->aux->linfo; |
| 6221 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; |
| 6222 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; |
| 6223 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6224 | func[i] = bpf_int_jit_compile(func[i]); |
| 6225 | if (!func[i]->jited) { |
| 6226 | err = -ENOTSUPP; |
| 6227 | goto out_free; |
| 6228 | } |
| 6229 | cond_resched(); |
| 6230 | } |
| 6231 | /* at this point all bpf functions were successfully JITed |
| 6232 | * now populate all bpf_calls with correct addresses and |
| 6233 | * run last pass of JIT |
| 6234 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6235 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6236 | insn = func[i]->insnsi; |
| 6237 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 6238 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 6239 | insn->src_reg != BPF_PSEUDO_CALL) |
| 6240 | continue; |
| 6241 | subprog = insn->off; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6242 | insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) |
| 6243 | func[subprog]->bpf_func - |
| 6244 | __bpf_call_base; |
| 6245 | } |
Sandipan Das | 2162fed | 2018-05-24 12:26:45 +0530 | [diff] [blame] | 6246 | |
| 6247 | /* we use the aux data to keep a list of the start addresses |
| 6248 | * of the JITed images for each function in the program |
| 6249 | * |
| 6250 | * for some architectures, such as powerpc64, the imm field |
| 6251 | * might not be large enough to hold the offset of the start |
| 6252 | * address of the callee's JITed image from __bpf_call_base |
| 6253 | * |
| 6254 | * in such cases, we can lookup the start address of a callee |
| 6255 | * by using its subprog id, available from the off field of |
| 6256 | * the call instruction, as an index for this list |
| 6257 | */ |
| 6258 | func[i]->aux->func = func; |
| 6259 | func[i]->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6260 | } |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6261 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6262 | old_bpf_func = func[i]->bpf_func; |
| 6263 | tmp = bpf_int_jit_compile(func[i]); |
| 6264 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 6265 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 6266 | err = -ENOTSUPP; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6267 | goto out_free; |
| 6268 | } |
| 6269 | cond_resched(); |
| 6270 | } |
| 6271 | |
| 6272 | /* finally lock prog and jit images for all functions and |
| 6273 | * populate kallsysm |
| 6274 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6275 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6276 | bpf_prog_lock_ro(func[i]); |
| 6277 | bpf_prog_kallsyms_add(func[i]); |
| 6278 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6279 | |
| 6280 | /* Last step: make now unused interpreter insns from main |
| 6281 | * prog consistent for later dump requests, so they can |
| 6282 | * later look the same as if they were interpreted only. |
| 6283 | */ |
| 6284 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6285 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 6286 | insn->src_reg != BPF_PSEUDO_CALL) |
| 6287 | continue; |
| 6288 | insn->off = env->insn_aux_data[i].call_imm; |
| 6289 | subprog = find_subprog(env, i + insn->off + 1); |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 6290 | insn->imm = subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 6291 | } |
| 6292 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6293 | prog->jited = 1; |
| 6294 | prog->bpf_func = func[0]->bpf_func; |
| 6295 | prog->aux->func = func; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6296 | prog->aux->func_cnt = env->subprog_cnt; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6297 | bpf_prog_free_unused_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6298 | return 0; |
| 6299 | out_free: |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6300 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6301 | if (func[i]) |
| 6302 | bpf_jit_free(func[i]); |
| 6303 | kfree(func); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 6304 | out_undo_insn: |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6305 | /* cleanup main prog to be interpreted */ |
| 6306 | prog->jit_requested = 0; |
| 6307 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 6308 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 6309 | insn->src_reg != BPF_PSEUDO_CALL) |
| 6310 | continue; |
| 6311 | insn->off = 0; |
| 6312 | insn->imm = env->insn_aux_data[i].call_imm; |
| 6313 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6314 | bpf_prog_free_jited_linfo(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6315 | return err; |
| 6316 | } |
| 6317 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6318 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 6319 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6320 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6321 | struct bpf_prog *prog = env->prog; |
| 6322 | struct bpf_insn *insn = prog->insnsi; |
| 6323 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6324 | #endif |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 6325 | int err = 0; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6326 | |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 6327 | if (env->prog->jit_requested && |
| 6328 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6329 | err = jit_subprogs(env); |
| 6330 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 6331 | return 0; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 6332 | if (err == -EFAULT) |
| 6333 | return err; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6334 | } |
| 6335 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6336 | for (i = 0; i < prog->len; i++, insn++) { |
| 6337 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 6338 | insn->src_reg != BPF_PSEUDO_CALL) |
| 6339 | continue; |
| 6340 | depth = get_callee_stack_depth(env, insn, i); |
| 6341 | if (depth < 0) |
| 6342 | return depth; |
| 6343 | bpf_patch_call_args(insn, depth); |
| 6344 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 6345 | err = 0; |
| 6346 | #endif |
| 6347 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6348 | } |
| 6349 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6350 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6351 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6352 | * |
| 6353 | * this function is called after eBPF program passed verification |
| 6354 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6355 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6356 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6357 | struct bpf_prog *prog = env->prog; |
| 6358 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6359 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6360 | const int insn_cnt = prog->len; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6361 | const struct bpf_map_ops *ops; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6362 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6363 | struct bpf_insn insn_buf[16]; |
| 6364 | struct bpf_prog *new_prog; |
| 6365 | struct bpf_map *map_ptr; |
| 6366 | int i, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6367 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6368 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 6369 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 6370 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 6371 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 6372 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 6373 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
| 6374 | struct bpf_insn mask_and_div[] = { |
| 6375 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 6376 | /* Rx div 0 -> 0 */ |
| 6377 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), |
| 6378 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 6379 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 6380 | *insn, |
| 6381 | }; |
| 6382 | struct bpf_insn mask_and_mod[] = { |
| 6383 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 6384 | /* Rx mod 0 -> Rx */ |
| 6385 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), |
| 6386 | *insn, |
| 6387 | }; |
| 6388 | struct bpf_insn *patchlet; |
| 6389 | |
| 6390 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 6391 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 6392 | patchlet = mask_and_div + (is64 ? 1 : 0); |
| 6393 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); |
| 6394 | } else { |
| 6395 | patchlet = mask_and_mod + (is64 ? 1 : 0); |
| 6396 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); |
| 6397 | } |
| 6398 | |
| 6399 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 6400 | if (!new_prog) |
| 6401 | return -ENOMEM; |
| 6402 | |
| 6403 | delta += cnt - 1; |
| 6404 | env->prog = prog = new_prog; |
| 6405 | insn = new_prog->insnsi + i + delta; |
| 6406 | continue; |
| 6407 | } |
| 6408 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 6409 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 6410 | (BPF_MODE(insn->code) == BPF_ABS || |
| 6411 | BPF_MODE(insn->code) == BPF_IND)) { |
| 6412 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 6413 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 6414 | verbose(env, "bpf verifier is misconfigured\n"); |
| 6415 | return -EINVAL; |
| 6416 | } |
| 6417 | |
| 6418 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 6419 | if (!new_prog) |
| 6420 | return -ENOMEM; |
| 6421 | |
| 6422 | delta += cnt - 1; |
| 6423 | env->prog = prog = new_prog; |
| 6424 | insn = new_prog->insnsi + i + delta; |
| 6425 | continue; |
| 6426 | } |
| 6427 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6428 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 6429 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 6430 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 6431 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6432 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6433 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 6434 | prog->dst_needed = 1; |
| 6435 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 6436 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 6437 | if (insn->imm == BPF_FUNC_override_return) |
| 6438 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6439 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 6440 | /* If we tail call into other programs, we |
| 6441 | * cannot make any assumptions since they can |
| 6442 | * be replaced dynamically during runtime in |
| 6443 | * the program array. |
| 6444 | */ |
| 6445 | prog->cb_access = 1; |
Alexei Starovoitov | 80a58d0 | 2017-05-30 13:31:30 -0700 | [diff] [blame] | 6446 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 6447 | env->prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 6448 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6449 | /* mark bpf_tail_call as different opcode to avoid |
| 6450 | * conditional branch in the interpeter for every normal |
| 6451 | * call and to prevent accidental JITing by JIT compiler |
| 6452 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6453 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6454 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 6455 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 6456 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6457 | aux = &env->insn_aux_data[i + delta]; |
| 6458 | if (!bpf_map_ptr_unpriv(aux)) |
| 6459 | continue; |
| 6460 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 6461 | /* instead of changing every JIT dealing with tail_call |
| 6462 | * emit two extra insns: |
| 6463 | * if (index >= max_entries) goto out; |
| 6464 | * index &= array->index_mask; |
| 6465 | * to avoid out-of-bounds cpu speculation |
| 6466 | */ |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6467 | if (bpf_map_ptr_poisoned(aux)) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 6468 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 6469 | return -EINVAL; |
| 6470 | } |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6471 | |
| 6472 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 6473 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 6474 | map_ptr->max_entries, 2); |
| 6475 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 6476 | container_of(map_ptr, |
| 6477 | struct bpf_array, |
| 6478 | map)->index_mask); |
| 6479 | insn_buf[2] = *insn; |
| 6480 | cnt = 3; |
| 6481 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 6482 | if (!new_prog) |
| 6483 | return -ENOMEM; |
| 6484 | |
| 6485 | delta += cnt - 1; |
| 6486 | env->prog = prog = new_prog; |
| 6487 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6488 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6489 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6490 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 6491 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6492 | * and other inlining handlers are currently limited to 64 bit |
| 6493 | * only. |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 6494 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 6495 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6496 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 6497 | insn->imm == BPF_FUNC_map_update_elem || |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 6498 | insn->imm == BPF_FUNC_map_delete_elem || |
| 6499 | insn->imm == BPF_FUNC_map_push_elem || |
| 6500 | insn->imm == BPF_FUNC_map_pop_elem || |
| 6501 | insn->imm == BPF_FUNC_map_peek_elem)) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6502 | aux = &env->insn_aux_data[i + delta]; |
| 6503 | if (bpf_map_ptr_poisoned(aux)) |
| 6504 | goto patch_call_imm; |
| 6505 | |
| 6506 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6507 | ops = map_ptr->ops; |
| 6508 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 6509 | ops->map_gen_lookup) { |
| 6510 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
| 6511 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 6512 | verbose(env, "bpf verifier is misconfigured\n"); |
| 6513 | return -EINVAL; |
| 6514 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6515 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6516 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 6517 | insn_buf, cnt); |
| 6518 | if (!new_prog) |
| 6519 | return -ENOMEM; |
| 6520 | |
| 6521 | delta += cnt - 1; |
| 6522 | env->prog = prog = new_prog; |
| 6523 | insn = new_prog->insnsi + i + delta; |
| 6524 | continue; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6525 | } |
| 6526 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6527 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 6528 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 6529 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 6530 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 6531 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 6532 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 6533 | u64 flags))NULL)); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 6534 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 6535 | (int (*)(struct bpf_map *map, void *value, |
| 6536 | u64 flags))NULL)); |
| 6537 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 6538 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 6539 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 6540 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 6541 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6542 | switch (insn->imm) { |
| 6543 | case BPF_FUNC_map_lookup_elem: |
| 6544 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - |
| 6545 | __bpf_call_base; |
| 6546 | continue; |
| 6547 | case BPF_FUNC_map_update_elem: |
| 6548 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - |
| 6549 | __bpf_call_base; |
| 6550 | continue; |
| 6551 | case BPF_FUNC_map_delete_elem: |
| 6552 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - |
| 6553 | __bpf_call_base; |
| 6554 | continue; |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 6555 | case BPF_FUNC_map_push_elem: |
| 6556 | insn->imm = BPF_CAST_CALL(ops->map_push_elem) - |
| 6557 | __bpf_call_base; |
| 6558 | continue; |
| 6559 | case BPF_FUNC_map_pop_elem: |
| 6560 | insn->imm = BPF_CAST_CALL(ops->map_pop_elem) - |
| 6561 | __bpf_call_base; |
| 6562 | continue; |
| 6563 | case BPF_FUNC_map_peek_elem: |
| 6564 | insn->imm = BPF_CAST_CALL(ops->map_peek_elem) - |
| 6565 | __bpf_call_base; |
| 6566 | continue; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6567 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6568 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6569 | goto patch_call_imm; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 6570 | } |
| 6571 | |
| 6572 | patch_call_imm: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 6573 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6574 | /* all functions that have prototype and verifier allowed |
| 6575 | * programs to call them, must be real in-kernel functions |
| 6576 | */ |
| 6577 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6578 | verbose(env, |
| 6579 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6580 | func_id_name(insn->imm), insn->imm); |
| 6581 | return -EFAULT; |
| 6582 | } |
| 6583 | insn->imm = fn->func - __bpf_call_base; |
| 6584 | } |
| 6585 | |
| 6586 | return 0; |
| 6587 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6588 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6589 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6590 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6591 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6592 | int i; |
| 6593 | |
| 6594 | if (!env->explored_states) |
| 6595 | return; |
| 6596 | |
| 6597 | for (i = 0; i < env->prog->len; i++) { |
| 6598 | sl = env->explored_states[i]; |
| 6599 | |
| 6600 | if (sl) |
| 6601 | while (sl != STATE_LIST_MARK) { |
| 6602 | sln = sl->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 6603 | free_verifier_state(&sl->state, false); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6604 | kfree(sl); |
| 6605 | sl = sln; |
| 6606 | } |
| 6607 | } |
| 6608 | |
| 6609 | kfree(env->explored_states); |
| 6610 | } |
| 6611 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6612 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, |
| 6613 | union bpf_attr __user *uattr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 6614 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6615 | struct bpf_verifier_env *env; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 6616 | struct bpf_verifier_log *log; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 6617 | int ret = -EINVAL; |
| 6618 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 6619 | /* no program is valid */ |
| 6620 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 6621 | return -EINVAL; |
| 6622 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6623 | /* '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] | 6624 | * allocate/free it every time bpf_check() is called |
| 6625 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6626 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6627 | if (!env) |
| 6628 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6629 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6630 | |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 6631 | env->insn_aux_data = |
| 6632 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), |
| 6633 | (*prog)->len)); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6634 | ret = -ENOMEM; |
| 6635 | if (!env->insn_aux_data) |
| 6636 | goto err_free_env; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6637 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 6638 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6639 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6640 | /* grab the mutex to protect few globals used by verifier */ |
| 6641 | mutex_lock(&bpf_verifier_lock); |
| 6642 | |
| 6643 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 6644 | /* user requested verbose verifier output |
| 6645 | * and supplied buffer to store the verification trace |
| 6646 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 6647 | log->level = attr->log_level; |
| 6648 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 6649 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6650 | |
| 6651 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 6652 | /* log attributes have to be sane */ |
| 6653 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || |
| 6654 | !log->level || !log->ubuf) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6655 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6656 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 6657 | |
| 6658 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 6659 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 6660 | env->strict_alignment = true; |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 6661 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
| 6662 | env->strict_alignment = false; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6663 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6664 | ret = replace_map_fd_with_map_ptr(env); |
| 6665 | if (ret < 0) |
| 6666 | goto skip_full_check; |
| 6667 | |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 6668 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Quentin Monnet | a40a263 | 2018-11-09 13:03:31 +0000 | [diff] [blame] | 6669 | ret = bpf_prog_offload_verifier_prep(env->prog); |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 6670 | if (ret) |
| 6671 | goto skip_full_check; |
| 6672 | } |
| 6673 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6674 | env->explored_states = kcalloc(env->prog->len, |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 6675 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6676 | GFP_USER); |
| 6677 | ret = -ENOMEM; |
| 6678 | if (!env->explored_states) |
| 6679 | goto skip_full_check; |
| 6680 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 6681 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 6682 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 6683 | ret = check_cfg(env); |
| 6684 | if (ret < 0) |
| 6685 | goto skip_full_check; |
| 6686 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 6687 | ret = check_btf_info(env, attr, uattr); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6688 | if (ret < 0) |
| 6689 | goto skip_full_check; |
| 6690 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6691 | ret = do_check(env); |
Craig Gallek | 8c01c4f | 2017-11-02 11:18:01 -0400 | [diff] [blame] | 6692 | if (env->cur_state) { |
| 6693 | free_verifier_state(env->cur_state, true); |
| 6694 | env->cur_state = NULL; |
| 6695 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6696 | |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 6697 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 6698 | ret = bpf_prog_offload_finalize(env); |
| 6699 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6700 | skip_full_check: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6701 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 6702 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6703 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6704 | if (ret == 0) |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 6705 | sanitize_dead_code(env); |
| 6706 | |
| 6707 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 6708 | ret = check_max_stack_depth(env); |
| 6709 | |
| 6710 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6711 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 6712 | ret = convert_ctx_accesses(env); |
| 6713 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6714 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 6715 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 6716 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 6717 | if (ret == 0) |
| 6718 | ret = fixup_call_args(env); |
| 6719 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 6720 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6721 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 6722 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6723 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 6724 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6725 | } |
| 6726 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6727 | if (ret == 0 && env->used_map_cnt) { |
| 6728 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6729 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 6730 | sizeof(env->used_maps[0]), |
| 6731 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6732 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6733 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6734 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 6735 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6736 | } |
| 6737 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6738 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6739 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6740 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6741 | |
| 6742 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 6743 | * bpf_ld_imm64 instructions |
| 6744 | */ |
| 6745 | convert_pseudo_ld_imm64(env); |
| 6746 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6747 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 6748 | if (ret == 0) |
| 6749 | adjust_btf_func(env); |
| 6750 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 6751 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6752 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6753 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 6754 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6755 | */ |
| 6756 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6757 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6758 | err_unlock: |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6759 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6760 | vfree(env->insn_aux_data); |
| 6761 | err_free_env: |
| 6762 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 6763 | return ret; |
| 6764 | } |