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 |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | */ |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/bpf.h> |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 17 | #include <linux/bpf_verifier.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 18 | #include <linux/filter.h> |
| 19 | #include <net/netlink.h> |
| 20 | #include <linux/file.h> |
| 21 | #include <linux/vmalloc.h> |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 22 | #include <linux/stringify.h> |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 23 | #include <linux/bsearch.h> |
| 24 | #include <linux/sort.h> |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 25 | #include <linux/perf_event.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 26 | |
Jakub Kicinski | f4ac7e0 | 2017-10-09 10:30:12 -0700 | [diff] [blame] | 27 | #include "disasm.h" |
| 28 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 29 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
| 30 | #define BPF_PROG_TYPE(_id, _name) \ |
| 31 | [_id] = & _name ## _verifier_ops, |
| 32 | #define BPF_MAP_TYPE(_id, _ops) |
| 33 | #include <linux/bpf_types.h> |
| 34 | #undef BPF_PROG_TYPE |
| 35 | #undef BPF_MAP_TYPE |
| 36 | }; |
| 37 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 38 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 39 | * instruction by instruction and updates register/stack state. |
| 40 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 41 | * |
| 42 | * The first pass is depth-first-search to check that the program is a DAG. |
| 43 | * It rejects the following programs: |
| 44 | * - larger than BPF_MAXINSNS insns |
| 45 | * - if loop is present (detected via back-edge) |
| 46 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 47 | * - out of bounds or malformed jumps |
| 48 | * The second pass is all possible path descent from the 1st insn. |
| 49 | * 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] | 50 | * 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] | 51 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 52 | * Number of 'branches to be analyzed' is limited to 1k |
| 53 | * |
| 54 | * On entry to each instruction, each register has a type, and the instruction |
| 55 | * changes the types of the registers depending on instruction semantics. |
| 56 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 57 | * copied to R1. |
| 58 | * |
| 59 | * All registers are 64-bit. |
| 60 | * R0 - return register |
| 61 | * R1-R5 argument passing registers |
| 62 | * R6-R9 callee saved registers |
| 63 | * R10 - frame pointer read-only |
| 64 | * |
| 65 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 66 | * and has type PTR_TO_CTX. |
| 67 | * |
| 68 | * Verifier tracks arithmetic operations on pointers in case: |
| 69 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 70 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 71 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 72 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 73 | * that it wants to construct a pointer to some element within stack. |
| 74 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 75 | * (and -20 constant is saved for further stack bounds checking). |
| 76 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 77 | * |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 78 | * Most of the time the registers have SCALAR_VALUE type, which |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 79 | * 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] | 80 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 81 | * |
| 82 | * When verifier sees load or store instructions the type of base register |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 83 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK. These are three pointer |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 84 | * types recognized by check_mem_access() function. |
| 85 | * |
| 86 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 87 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 88 | * |
| 89 | * registers used to pass values to function calls are checked against |
| 90 | * function argument constraints. |
| 91 | * |
| 92 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 93 | * It means that the register type passed to this function must be |
| 94 | * PTR_TO_STACK and it will be used inside the function as |
| 95 | * 'pointer to map element key' |
| 96 | * |
| 97 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 98 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 99 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 100 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 101 | * |
| 102 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 103 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 104 | * 2nd argument should be a pointer to stack, which will be used inside |
| 105 | * the helper function as a pointer to map element key. |
| 106 | * |
| 107 | * On the kernel side the helper function looks like: |
| 108 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 109 | * { |
| 110 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 111 | * void *key = (void *) (unsigned long) r2; |
| 112 | * void *value; |
| 113 | * |
| 114 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 115 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 116 | * the stack of eBPF program. |
| 117 | * } |
| 118 | * |
| 119 | * Corresponding eBPF program may look like: |
| 120 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 121 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 122 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 123 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 124 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 125 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 126 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 127 | * |
| 128 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 129 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 130 | * and were initialized prior to this call. |
| 131 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 132 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 133 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 134 | * returns ether pointer to map value or NULL. |
| 135 | * |
| 136 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 137 | * insn, the register holding that pointer in the true branch changes state to |
| 138 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 139 | * branch. See check_cond_jmp_op(). |
| 140 | * |
| 141 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 142 | * are set to NOT_INIT to indicate that they are no longer readable. |
| 143 | */ |
| 144 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 145 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 146 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 147 | /* verifer state is 'st' |
| 148 | * before processing instruction 'insn_idx' |
| 149 | * and after processing instruction 'prev_insn_idx' |
| 150 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 151 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 152 | int insn_idx; |
| 153 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 154 | struct bpf_verifier_stack_elem *next; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
Edward Cree | 8e17c1b | 2017-08-07 15:30:30 +0100 | [diff] [blame] | 157 | #define BPF_COMPLEXITY_LIMIT_INSNS 131072 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 158 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
| 159 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 160 | #define BPF_MAP_PTR_UNPRIV 1UL |
| 161 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ |
| 162 | POISON_POINTER_DELTA)) |
| 163 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) |
| 164 | |
| 165 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
| 166 | { |
| 167 | return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON; |
| 168 | } |
| 169 | |
| 170 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) |
| 171 | { |
| 172 | return aux->map_state & BPF_MAP_PTR_UNPRIV; |
| 173 | } |
| 174 | |
| 175 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, |
| 176 | const struct bpf_map *map, bool unpriv) |
| 177 | { |
| 178 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); |
| 179 | unpriv |= bpf_map_ptr_unpriv(aux); |
| 180 | aux->map_state = (unsigned long)map | |
| 181 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); |
| 182 | } |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 183 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 184 | struct bpf_call_arg_meta { |
| 185 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 186 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 187 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 188 | int regno; |
| 189 | int access_size; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 190 | s64 msize_smax_value; |
| 191 | u64 msize_umax_value; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 192 | }; |
| 193 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 194 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 195 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 196 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
| 197 | va_list args) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 198 | { |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 199 | unsigned int n; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 200 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 201 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 202 | |
| 203 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 204 | "verifier log line truncated - local buffer too short\n"); |
| 205 | |
| 206 | n = min(log->len_total - log->len_used - 1, n); |
| 207 | log->kbuf[n] = '\0'; |
| 208 | |
| 209 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 210 | log->len_used += n; |
| 211 | else |
| 212 | log->ubuf = NULL; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 213 | } |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 214 | |
| 215 | /* log_level controls verbosity level of eBPF verifier. |
| 216 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 217 | * so the user can figure out what's wrong with the program |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 218 | */ |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 219 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 220 | const char *fmt, ...) |
| 221 | { |
| 222 | va_list args; |
| 223 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 224 | if (!bpf_verifier_log_needed(&env->log)) |
| 225 | return; |
| 226 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 227 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 228 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 229 | va_end(args); |
| 230 | } |
| 231 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 232 | |
| 233 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
| 234 | { |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 235 | struct bpf_verifier_env *env = private_data; |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 236 | va_list args; |
| 237 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 238 | if (!bpf_verifier_log_needed(&env->log)) |
| 239 | return; |
| 240 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 241 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 242 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 243 | va_end(args); |
| 244 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 245 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 246 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 247 | { |
| 248 | return type == PTR_TO_PACKET || |
| 249 | type == PTR_TO_PACKET_META; |
| 250 | } |
| 251 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 252 | /* string representation of 'enum bpf_reg_type' */ |
| 253 | static const char * const reg_type_str[] = { |
| 254 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 255 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 256 | [PTR_TO_CTX] = "ctx", |
| 257 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 258 | [PTR_TO_MAP_VALUE] = "map_value", |
| 259 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 260 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 261 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 262 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 263 | [PTR_TO_PACKET_END] = "pkt_end", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 266 | static void print_liveness(struct bpf_verifier_env *env, |
| 267 | enum bpf_reg_liveness live) |
| 268 | { |
| 269 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN)) |
| 270 | verbose(env, "_"); |
| 271 | if (live & REG_LIVE_READ) |
| 272 | verbose(env, "r"); |
| 273 | if (live & REG_LIVE_WRITTEN) |
| 274 | verbose(env, "w"); |
| 275 | } |
| 276 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 277 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 278 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 279 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 280 | struct bpf_verifier_state *cur = env->cur_state; |
| 281 | |
| 282 | return cur->frame[reg->frameno]; |
| 283 | } |
| 284 | |
| 285 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 286 | const struct bpf_func_state *state) |
| 287 | { |
| 288 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 289 | enum bpf_reg_type t; |
| 290 | int i; |
| 291 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 292 | if (state->frameno) |
| 293 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 294 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 295 | reg = &state->regs[i]; |
| 296 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 297 | if (t == NOT_INIT) |
| 298 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 299 | verbose(env, " R%d", i); |
| 300 | print_liveness(env, reg->live); |
| 301 | verbose(env, "=%s", reg_type_str[t]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 302 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 303 | tnum_is_const(reg->var_off)) { |
| 304 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 305 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 306 | if (t == PTR_TO_STACK) |
| 307 | verbose(env, ",call_%d", func(env, reg)->callsite); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 308 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 309 | verbose(env, "(id=%d", reg->id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 310 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 311 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 312 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 313 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 314 | else if (t == CONST_PTR_TO_MAP || |
| 315 | t == PTR_TO_MAP_VALUE || |
| 316 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 317 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 318 | reg->map_ptr->key_size, |
| 319 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 320 | if (tnum_is_const(reg->var_off)) { |
| 321 | /* Typically an immediate SCALAR_VALUE, but |
| 322 | * could be a pointer whose offset is too big |
| 323 | * for reg->off |
| 324 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 325 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 326 | } else { |
| 327 | if (reg->smin_value != reg->umin_value && |
| 328 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 329 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 330 | (long long)reg->smin_value); |
| 331 | if (reg->smax_value != reg->umax_value && |
| 332 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 333 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 334 | (long long)reg->smax_value); |
| 335 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 336 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 337 | (unsigned long long)reg->umin_value); |
| 338 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 339 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 340 | (unsigned long long)reg->umax_value); |
| 341 | if (!tnum_is_unknown(reg->var_off)) { |
| 342 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 343 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 344 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 345 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 346 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 347 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 348 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 349 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 350 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 351 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 352 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
| 353 | verbose(env, " fp%d", |
| 354 | (-i - 1) * BPF_REG_SIZE); |
| 355 | print_liveness(env, state->stack[i].spilled_ptr.live); |
| 356 | verbose(env, "=%s", |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 357 | reg_type_str[state->stack[i].spilled_ptr.type]); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 358 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 359 | if (state->stack[i].slot_type[0] == STACK_ZERO) |
| 360 | verbose(env, " fp%d=0", (-i - 1) * BPF_REG_SIZE); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 361 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 362 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 365 | static int copy_stack_state(struct bpf_func_state *dst, |
| 366 | const struct bpf_func_state *src) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 367 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 368 | if (!src->stack) |
| 369 | return 0; |
| 370 | if (WARN_ON_ONCE(dst->allocated_stack < src->allocated_stack)) { |
| 371 | /* internal bug, make state invalid to reject the program */ |
| 372 | memset(dst, 0, sizeof(*dst)); |
| 373 | return -EFAULT; |
| 374 | } |
| 375 | memcpy(dst->stack, src->stack, |
| 376 | sizeof(*src->stack) * (src->allocated_stack / BPF_REG_SIZE)); |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 381 | * make it consume minimal amount of memory. check_stack_write() access from |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 382 | * the program calls into realloc_func_state() to grow the stack size. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 383 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 384 | * which this function copies over. It points to previous bpf_verifier_state |
| 385 | * which is never reallocated |
| 386 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 387 | static int realloc_func_state(struct bpf_func_state *state, int size, |
| 388 | bool copy_old) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 389 | { |
| 390 | u32 old_size = state->allocated_stack; |
| 391 | struct bpf_stack_state *new_stack; |
| 392 | int slot = size / BPF_REG_SIZE; |
| 393 | |
| 394 | if (size <= old_size || !size) { |
| 395 | if (copy_old) |
| 396 | return 0; |
| 397 | state->allocated_stack = slot * BPF_REG_SIZE; |
| 398 | if (!size && old_size) { |
| 399 | kfree(state->stack); |
| 400 | state->stack = NULL; |
| 401 | } |
| 402 | return 0; |
| 403 | } |
| 404 | new_stack = kmalloc_array(slot, sizeof(struct bpf_stack_state), |
| 405 | GFP_KERNEL); |
| 406 | if (!new_stack) |
| 407 | return -ENOMEM; |
| 408 | if (copy_old) { |
| 409 | if (state->stack) |
| 410 | memcpy(new_stack, state->stack, |
| 411 | sizeof(*new_stack) * (old_size / BPF_REG_SIZE)); |
| 412 | memset(new_stack + old_size / BPF_REG_SIZE, 0, |
| 413 | sizeof(*new_stack) * (size - old_size) / BPF_REG_SIZE); |
| 414 | } |
| 415 | state->allocated_stack = slot * BPF_REG_SIZE; |
| 416 | kfree(state->stack); |
| 417 | state->stack = new_stack; |
| 418 | return 0; |
| 419 | } |
| 420 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 421 | static void free_func_state(struct bpf_func_state *state) |
| 422 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 423 | if (!state) |
| 424 | return; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 425 | kfree(state->stack); |
| 426 | kfree(state); |
| 427 | } |
| 428 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 429 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 430 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 431 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 432 | int i; |
| 433 | |
| 434 | for (i = 0; i <= state->curframe; i++) { |
| 435 | free_func_state(state->frame[i]); |
| 436 | state->frame[i] = NULL; |
| 437 | } |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 438 | if (free_self) |
| 439 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* copy verifier state from src to dst growing dst stack space |
| 443 | * when necessary to accommodate larger src stack |
| 444 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 445 | static int copy_func_state(struct bpf_func_state *dst, |
| 446 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 447 | { |
| 448 | int err; |
| 449 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 450 | err = realloc_func_state(dst, src->allocated_stack, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 451 | if (err) |
| 452 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 453 | memcpy(dst, src, offsetof(struct bpf_func_state, allocated_stack)); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 454 | return copy_stack_state(dst, src); |
| 455 | } |
| 456 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 457 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 458 | const struct bpf_verifier_state *src) |
| 459 | { |
| 460 | struct bpf_func_state *dst; |
| 461 | int i, err; |
| 462 | |
| 463 | /* if dst has more stack frames then src frame, free them */ |
| 464 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 465 | free_func_state(dst_state->frame[i]); |
| 466 | dst_state->frame[i] = NULL; |
| 467 | } |
| 468 | dst_state->curframe = src->curframe; |
| 469 | dst_state->parent = src->parent; |
| 470 | for (i = 0; i <= src->curframe; i++) { |
| 471 | dst = dst_state->frame[i]; |
| 472 | if (!dst) { |
| 473 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 474 | if (!dst) |
| 475 | return -ENOMEM; |
| 476 | dst_state->frame[i] = dst; |
| 477 | } |
| 478 | err = copy_func_state(dst, src->frame[i]); |
| 479 | if (err) |
| 480 | return err; |
| 481 | } |
| 482 | return 0; |
| 483 | } |
| 484 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 485 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
| 486 | int *insn_idx) |
| 487 | { |
| 488 | struct bpf_verifier_state *cur = env->cur_state; |
| 489 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 490 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 491 | |
| 492 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 493 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 494 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 495 | if (cur) { |
| 496 | err = copy_verifier_state(cur, &head->st); |
| 497 | if (err) |
| 498 | return err; |
| 499 | } |
| 500 | if (insn_idx) |
| 501 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 502 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 503 | *prev_insn_idx = head->prev_insn_idx; |
| 504 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 505 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 506 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 507 | env->head = elem; |
| 508 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 509 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 512 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
| 513 | int insn_idx, int prev_insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 514 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 515 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 516 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 517 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 518 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 519 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 520 | if (!elem) |
| 521 | goto err; |
| 522 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 523 | elem->insn_idx = insn_idx; |
| 524 | elem->prev_insn_idx = prev_insn_idx; |
| 525 | elem->next = env->head; |
| 526 | env->head = elem; |
| 527 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 528 | err = copy_verifier_state(&elem->st, cur); |
| 529 | if (err) |
| 530 | goto err; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 531 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 532 | verbose(env, "BPF program is too complex\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 533 | goto err; |
| 534 | } |
| 535 | return &elem->st; |
| 536 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 537 | free_verifier_state(env->cur_state, true); |
| 538 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 539 | /* pop all elements and return */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 540 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 541 | return NULL; |
| 542 | } |
| 543 | |
| 544 | #define CALLER_SAVED_REGS 6 |
| 545 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 546 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 547 | }; |
| 548 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 549 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
| 550 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 551 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 552 | * known to have the value @imm. |
| 553 | */ |
| 554 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 555 | { |
| 556 | reg->id = 0; |
| 557 | reg->var_off = tnum_const(imm); |
| 558 | reg->smin_value = (s64)imm; |
| 559 | reg->smax_value = (s64)imm; |
| 560 | reg->umin_value = imm; |
| 561 | reg->umax_value = imm; |
| 562 | } |
| 563 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 564 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 565 | * used only on registers holding a pointer type. |
| 566 | */ |
| 567 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 568 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 569 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 572 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 573 | { |
| 574 | __mark_reg_known(reg, 0); |
| 575 | reg->off = 0; |
| 576 | reg->type = SCALAR_VALUE; |
| 577 | } |
| 578 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 579 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 580 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 581 | { |
| 582 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 583 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 584 | /* Something bad happened, let's kill all regs */ |
| 585 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 586 | __mark_reg_not_init(regs + regno); |
| 587 | return; |
| 588 | } |
| 589 | __mark_reg_known_zero(regs + regno); |
| 590 | } |
| 591 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 592 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 593 | { |
| 594 | return type_is_pkt_pointer(reg->type); |
| 595 | } |
| 596 | |
| 597 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 598 | { |
| 599 | return reg_is_pkt_pointer(reg) || |
| 600 | reg->type == PTR_TO_PACKET_END; |
| 601 | } |
| 602 | |
| 603 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 604 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 605 | enum bpf_reg_type which) |
| 606 | { |
| 607 | /* The register can already have a range from prior markings. |
| 608 | * This is fine as long as it hasn't been advanced from its |
| 609 | * origin. |
| 610 | */ |
| 611 | return reg->type == which && |
| 612 | reg->id == 0 && |
| 613 | reg->off == 0 && |
| 614 | tnum_equals_const(reg->var_off, 0); |
| 615 | } |
| 616 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 617 | /* Attempts to improve min/max values based on var_off information */ |
| 618 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 619 | { |
| 620 | /* min signed is max(sign bit) | min(other bits) */ |
| 621 | reg->smin_value = max_t(s64, reg->smin_value, |
| 622 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 623 | /* max signed is min(sign bit) | max(other bits) */ |
| 624 | reg->smax_value = min_t(s64, reg->smax_value, |
| 625 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 626 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 627 | reg->umax_value = min(reg->umax_value, |
| 628 | reg->var_off.value | reg->var_off.mask); |
| 629 | } |
| 630 | |
| 631 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
| 632 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 633 | { |
| 634 | /* Learn sign from signed bounds. |
| 635 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 636 | * are the same, so combine. This works even in the negative case, e.g. |
| 637 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 638 | */ |
| 639 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 640 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 641 | reg->umin_value); |
| 642 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 643 | reg->umax_value); |
| 644 | return; |
| 645 | } |
| 646 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 647 | * boundary, so we must be careful. |
| 648 | */ |
| 649 | if ((s64)reg->umax_value >= 0) { |
| 650 | /* Positive. We can't learn anything from the smin, but smax |
| 651 | * is positive, hence safe. |
| 652 | */ |
| 653 | reg->smin_value = reg->umin_value; |
| 654 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 655 | reg->umax_value); |
| 656 | } else if ((s64)reg->umin_value < 0) { |
| 657 | /* Negative. We can't learn anything from the smax, but smin |
| 658 | * is negative, hence safe. |
| 659 | */ |
| 660 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 661 | reg->umin_value); |
| 662 | reg->smax_value = reg->umax_value; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 667 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 668 | { |
| 669 | reg->var_off = tnum_intersect(reg->var_off, |
| 670 | tnum_range(reg->umin_value, |
| 671 | reg->umax_value)); |
| 672 | } |
| 673 | |
| 674 | /* Reset the min/max bounds of a register */ |
| 675 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 676 | { |
| 677 | reg->smin_value = S64_MIN; |
| 678 | reg->smax_value = S64_MAX; |
| 679 | reg->umin_value = 0; |
| 680 | reg->umax_value = U64_MAX; |
| 681 | } |
| 682 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 683 | /* Mark a register as having a completely unknown (scalar) value. */ |
| 684 | static void __mark_reg_unknown(struct bpf_reg_state *reg) |
| 685 | { |
| 686 | reg->type = SCALAR_VALUE; |
| 687 | reg->id = 0; |
| 688 | reg->off = 0; |
| 689 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 690 | reg->frameno = 0; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 691 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 692 | } |
| 693 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 694 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 695 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 696 | { |
| 697 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 698 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 699 | /* Something bad happened, let's kill all regs except FP */ |
| 700 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 701 | __mark_reg_not_init(regs + regno); |
| 702 | return; |
| 703 | } |
| 704 | __mark_reg_unknown(regs + regno); |
| 705 | } |
| 706 | |
| 707 | static void __mark_reg_not_init(struct bpf_reg_state *reg) |
| 708 | { |
| 709 | __mark_reg_unknown(reg); |
| 710 | reg->type = NOT_INIT; |
| 711 | } |
| 712 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 713 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 714 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 715 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 716 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 717 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 718 | /* Something bad happened, let's kill all regs except FP */ |
| 719 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 720 | __mark_reg_not_init(regs + regno); |
| 721 | return; |
| 722 | } |
| 723 | __mark_reg_not_init(regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 724 | } |
| 725 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 726 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 727 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 728 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 729 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 730 | int i; |
| 731 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 732 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 733 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 734 | regs[i].live = REG_LIVE_NONE; |
| 735 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 736 | |
| 737 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 738 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 739 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 740 | regs[BPF_REG_FP].frameno = state->frameno; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 741 | |
| 742 | /* 1st arg to a function */ |
| 743 | regs[BPF_REG_1].type = PTR_TO_CTX; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 744 | mark_reg_known_zero(env, regs, BPF_REG_1); |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 745 | } |
| 746 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 747 | #define BPF_MAIN_FUNC (-1) |
| 748 | static void init_func_state(struct bpf_verifier_env *env, |
| 749 | struct bpf_func_state *state, |
| 750 | int callsite, int frameno, int subprogno) |
| 751 | { |
| 752 | state->callsite = callsite; |
| 753 | state->frameno = frameno; |
| 754 | state->subprogno = subprogno; |
| 755 | init_reg_state(env, state); |
| 756 | } |
| 757 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 758 | enum reg_arg_type { |
| 759 | SRC_OP, /* register is used as source operand */ |
| 760 | DST_OP, /* register is used as destination operand */ |
| 761 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 762 | }; |
| 763 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 764 | static int cmp_subprogs(const void *a, const void *b) |
| 765 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 766 | return ((struct bpf_subprog_info *)a)->start - |
| 767 | ((struct bpf_subprog_info *)b)->start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 771 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 772 | struct bpf_subprog_info *p; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 773 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 774 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 775 | sizeof(env->subprog_info[0]), cmp_subprogs); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 776 | if (!p) |
| 777 | return -ENOENT; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 778 | return p - env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 779 | |
| 780 | } |
| 781 | |
| 782 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 783 | { |
| 784 | int insn_cnt = env->prog->len; |
| 785 | int ret; |
| 786 | |
| 787 | if (off >= insn_cnt || off < 0) { |
| 788 | verbose(env, "call to invalid destination\n"); |
| 789 | return -EINVAL; |
| 790 | } |
| 791 | ret = find_subprog(env, off); |
| 792 | if (ret >= 0) |
| 793 | return 0; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 794 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 795 | verbose(env, "too many subprograms\n"); |
| 796 | return -E2BIG; |
| 797 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 798 | env->subprog_info[env->subprog_cnt++].start = off; |
| 799 | sort(env->subprog_info, env->subprog_cnt, |
| 800 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | static int check_subprogs(struct bpf_verifier_env *env) |
| 805 | { |
| 806 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 807 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 808 | struct bpf_insn *insn = env->prog->insnsi; |
| 809 | int insn_cnt = env->prog->len; |
| 810 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 811 | /* Add entry function. */ |
| 812 | ret = add_subprog(env, 0); |
| 813 | if (ret < 0) |
| 814 | return ret; |
| 815 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 816 | /* determine subprog starts. The end is one before the next starts */ |
| 817 | for (i = 0; i < insn_cnt; i++) { |
| 818 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 819 | continue; |
| 820 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 821 | continue; |
| 822 | if (!env->allow_ptr_leaks) { |
| 823 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); |
| 824 | return -EPERM; |
| 825 | } |
| 826 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Colin Ian King | e90004d5 | 2017-12-18 14:03:12 +0000 | [diff] [blame] | 827 | verbose(env, "function calls in offloaded programs are not supported yet\n"); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 828 | return -EINVAL; |
| 829 | } |
| 830 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 831 | if (ret < 0) |
| 832 | return ret; |
| 833 | } |
| 834 | |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 835 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 836 | * logic. 'subprog_cnt' should not be increased. |
| 837 | */ |
| 838 | subprog[env->subprog_cnt].start = insn_cnt; |
| 839 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 840 | if (env->log.level > 1) |
| 841 | for (i = 0; i < env->subprog_cnt; i++) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 842 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 843 | |
| 844 | /* now check that all jumps are within the same subprog */ |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 845 | subprog_start = subprog[cur_subprog].start; |
| 846 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 847 | for (i = 0; i < insn_cnt; i++) { |
| 848 | u8 code = insn[i].code; |
| 849 | |
| 850 | if (BPF_CLASS(code) != BPF_JMP) |
| 851 | goto next; |
| 852 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 853 | goto next; |
| 854 | off = i + insn[i].off + 1; |
| 855 | if (off < subprog_start || off >= subprog_end) { |
| 856 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 857 | return -EINVAL; |
| 858 | } |
| 859 | next: |
| 860 | if (i == subprog_end - 1) { |
| 861 | /* to avoid fall-through from one subprog into another |
| 862 | * the last insn of the subprog should be either exit |
| 863 | * or unconditional jump back |
| 864 | */ |
| 865 | if (code != (BPF_JMP | BPF_EXIT) && |
| 866 | code != (BPF_JMP | BPF_JA)) { |
| 867 | verbose(env, "last insn is not an exit or jmp\n"); |
| 868 | return -EINVAL; |
| 869 | } |
| 870 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 871 | cur_subprog++; |
| 872 | if (cur_subprog < env->subprog_cnt) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 873 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | return 0; |
| 877 | } |
| 878 | |
Colin Ian King | fa2d41a | 2017-12-18 17:47:07 +0000 | [diff] [blame] | 879 | static |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 880 | struct bpf_verifier_state *skip_callee(struct bpf_verifier_env *env, |
| 881 | const struct bpf_verifier_state *state, |
| 882 | struct bpf_verifier_state *parent, |
| 883 | u32 regno) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 884 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 885 | struct bpf_verifier_state *tmp = NULL; |
| 886 | |
| 887 | /* 'parent' could be a state of caller and |
| 888 | * 'state' could be a state of callee. In such case |
| 889 | * parent->curframe < state->curframe |
| 890 | * and it's ok for r1 - r5 registers |
| 891 | * |
| 892 | * 'parent' could be a callee's state after it bpf_exit-ed. |
| 893 | * In such case parent->curframe > state->curframe |
| 894 | * and it's ok for r0 only |
| 895 | */ |
| 896 | if (parent->curframe == state->curframe || |
| 897 | (parent->curframe < state->curframe && |
| 898 | regno >= BPF_REG_1 && regno <= BPF_REG_5) || |
| 899 | (parent->curframe > state->curframe && |
| 900 | regno == BPF_REG_0)) |
| 901 | return parent; |
| 902 | |
| 903 | if (parent->curframe > state->curframe && |
| 904 | regno >= BPF_REG_6) { |
| 905 | /* for callee saved regs we have to skip the whole chain |
| 906 | * of states that belong to callee and mark as LIVE_READ |
| 907 | * the registers before the call |
| 908 | */ |
| 909 | tmp = parent; |
| 910 | while (tmp && tmp->curframe != state->curframe) { |
| 911 | tmp = tmp->parent; |
| 912 | } |
| 913 | if (!tmp) |
| 914 | goto bug; |
| 915 | parent = tmp; |
| 916 | } else { |
| 917 | goto bug; |
| 918 | } |
| 919 | return parent; |
| 920 | bug: |
| 921 | verbose(env, "verifier bug regno %d tmp %p\n", regno, tmp); |
| 922 | verbose(env, "regno %d parent frame %d current frame %d\n", |
| 923 | regno, parent->curframe, state->curframe); |
Colin Ian King | fa2d41a | 2017-12-18 17:47:07 +0000 | [diff] [blame] | 924 | return NULL; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | static int mark_reg_read(struct bpf_verifier_env *env, |
| 928 | const struct bpf_verifier_state *state, |
| 929 | struct bpf_verifier_state *parent, |
| 930 | u32 regno) |
| 931 | { |
| 932 | bool writes = parent == state->parent; /* Observe write marks */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 933 | |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 934 | if (regno == BPF_REG_FP) |
| 935 | /* We don't need to worry about FP liveness because it's read-only */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 936 | return 0; |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 937 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 938 | while (parent) { |
| 939 | /* if read wasn't screened by an earlier write ... */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 940 | if (writes && state->frame[state->curframe]->regs[regno].live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 941 | break; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 942 | parent = skip_callee(env, state, parent, regno); |
| 943 | if (!parent) |
| 944 | return -EFAULT; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 945 | /* ... then we depend on parent's value */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 946 | parent->frame[parent->curframe]->regs[regno].live |= REG_LIVE_READ; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 947 | state = parent; |
| 948 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 949 | writes = true; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 950 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 951 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 955 | enum reg_arg_type t) |
| 956 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 957 | struct bpf_verifier_state *vstate = env->cur_state; |
| 958 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 959 | struct bpf_reg_state *regs = state->regs; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 960 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 961 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 962 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 963 | return -EINVAL; |
| 964 | } |
| 965 | |
| 966 | if (t == SRC_OP) { |
| 967 | /* check whether register used as source operand can be read */ |
| 968 | if (regs[regno].type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 969 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 970 | return -EACCES; |
| 971 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 972 | return mark_reg_read(env, vstate, vstate->parent, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 973 | } else { |
| 974 | /* check whether register used as dest operand can be written to */ |
| 975 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 976 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 977 | return -EACCES; |
| 978 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 979 | regs[regno].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 980 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 981 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 982 | } |
| 983 | return 0; |
| 984 | } |
| 985 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 986 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 987 | { |
| 988 | switch (type) { |
| 989 | case PTR_TO_MAP_VALUE: |
| 990 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 991 | case PTR_TO_STACK: |
| 992 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 993 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 994 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 995 | case PTR_TO_PACKET_END: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 996 | case CONST_PTR_TO_MAP: |
| 997 | return true; |
| 998 | default: |
| 999 | return false; |
| 1000 | } |
| 1001 | } |
| 1002 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1003 | /* Does this register contain a constant zero? */ |
| 1004 | static bool register_is_null(struct bpf_reg_state *reg) |
| 1005 | { |
| 1006 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 1007 | } |
| 1008 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1009 | /* check_stack_read/write functions track spill/fill of registers, |
| 1010 | * stack boundary and alignment are checked in check_mem_access() |
| 1011 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1012 | static int check_stack_write(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1013 | struct bpf_func_state *state, /* func where register points to */ |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1014 | int off, int size, int value_regno, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1015 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1016 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1017 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1018 | enum bpf_reg_type type; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1019 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1020 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
| 1021 | true); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1022 | if (err) |
| 1023 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1024 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 1025 | * so it's aligned access and [off, off + size) are within stack limits |
| 1026 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1027 | if (!env->allow_ptr_leaks && |
| 1028 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 1029 | size != BPF_REG_SIZE) { |
| 1030 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 1031 | return -EACCES; |
| 1032 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1033 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1034 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1035 | if (value_regno >= 0 && |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1036 | is_spillable_regtype((type = cur->regs[value_regno].type))) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1037 | |
| 1038 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1039 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1040 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1041 | return -EACCES; |
| 1042 | } |
| 1043 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1044 | if (state != cur && type == PTR_TO_STACK) { |
| 1045 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 1046 | return -EINVAL; |
| 1047 | } |
| 1048 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1049 | /* save register state */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1050 | state->stack[spi].spilled_ptr = cur->regs[value_regno]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1051 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1052 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1053 | for (i = 0; i < BPF_REG_SIZE; i++) { |
| 1054 | if (state->stack[spi].slot_type[i] == STACK_MISC && |
| 1055 | !env->allow_ptr_leaks) { |
| 1056 | int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; |
| 1057 | int soff = (-spi - 1) * BPF_REG_SIZE; |
| 1058 | |
| 1059 | /* detected reuse of integer stack slot with a pointer |
| 1060 | * which means either llvm is reusing stack slot or |
| 1061 | * an attacker is trying to exploit CVE-2018-3639 |
| 1062 | * (speculative store bypass) |
| 1063 | * Have to sanitize that slot with preemptive |
| 1064 | * store of zero. |
| 1065 | */ |
| 1066 | if (*poff && *poff != soff) { |
| 1067 | /* disallow programs where single insn stores |
| 1068 | * into two different stack slots, since verifier |
| 1069 | * cannot sanitize them |
| 1070 | */ |
| 1071 | verbose(env, |
| 1072 | "insn %d cannot access two stack slots fp%d and fp%d", |
| 1073 | insn_idx, *poff, soff); |
| 1074 | return -EINVAL; |
| 1075 | } |
| 1076 | *poff = soff; |
| 1077 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1078 | state->stack[spi].slot_type[i] = STACK_SPILL; |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1079 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1080 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1081 | u8 type = STACK_MISC; |
| 1082 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1083 | /* regular write of data into stack */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1084 | state->stack[spi].spilled_ptr = (struct bpf_reg_state) {}; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1085 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1086 | /* only mark the slot as written if all 8 bytes were written |
| 1087 | * otherwise read propagation may incorrectly stop too soon |
| 1088 | * when stack slots are partially written. |
| 1089 | * This heuristic means that read propagation will be |
| 1090 | * conservative, since it will add reg_live_read marks |
| 1091 | * to stack slots all the way to first state when programs |
| 1092 | * writes+reads less than 8 bytes |
| 1093 | */ |
| 1094 | if (size == BPF_REG_SIZE) |
| 1095 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 1096 | |
| 1097 | /* when we zero initialize stack slots mark them as such */ |
| 1098 | if (value_regno >= 0 && |
| 1099 | register_is_null(&cur->regs[value_regno])) |
| 1100 | type = STACK_ZERO; |
| 1101 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1102 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1103 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1104 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1105 | } |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1109 | /* registers of every function are unique and mark_reg_read() propagates |
| 1110 | * the liveness in the following cases: |
| 1111 | * - from callee into caller for R1 - R5 that were used as arguments |
| 1112 | * - from caller into callee for R0 that used as result of the call |
| 1113 | * - from caller to the same caller skipping states of the callee for R6 - R9, |
| 1114 | * since R6 - R9 are callee saved by implicit function prologue and |
| 1115 | * caller's R6 != callee's R6, so when we propagate liveness up to |
| 1116 | * parent states we need to skip callee states for R6 - R9. |
| 1117 | * |
| 1118 | * stack slot marking is different, since stacks of caller and callee are |
| 1119 | * accessible in both (since caller can pass a pointer to caller's stack to |
| 1120 | * callee which can pass it to another function), hence mark_stack_slot_read() |
| 1121 | * has to propagate the stack liveness to all parent states at given frame number. |
| 1122 | * Consider code: |
| 1123 | * f1() { |
| 1124 | * ptr = fp - 8; |
| 1125 | * *ptr = ctx; |
| 1126 | * call f2 { |
| 1127 | * .. = *ptr; |
| 1128 | * } |
| 1129 | * .. = *ptr; |
| 1130 | * } |
| 1131 | * First *ptr is reading from f1's stack and mark_stack_slot_read() has |
| 1132 | * to mark liveness at the f1's frame and not f2's frame. |
| 1133 | * Second *ptr is also reading from f1's stack and mark_stack_slot_read() has |
| 1134 | * to propagate liveness to f2 states at f1's frame level and further into |
| 1135 | * f1 states at f1's frame level until write into that stack slot |
| 1136 | */ |
| 1137 | static void mark_stack_slot_read(struct bpf_verifier_env *env, |
| 1138 | const struct bpf_verifier_state *state, |
| 1139 | struct bpf_verifier_state *parent, |
| 1140 | int slot, int frameno) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1141 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1142 | bool writes = parent == state->parent; /* Observe write marks */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1143 | |
| 1144 | while (parent) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1145 | if (parent->frame[frameno]->allocated_stack <= slot * BPF_REG_SIZE) |
| 1146 | /* since LIVE_WRITTEN mark is only done for full 8-byte |
| 1147 | * write the read marks are conservative and parent |
| 1148 | * state may not even have the stack allocated. In such case |
| 1149 | * end the propagation, since the loop reached beginning |
| 1150 | * of the function |
| 1151 | */ |
| 1152 | break; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1153 | /* if read wasn't screened by an earlier write ... */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1154 | if (writes && state->frame[frameno]->stack[slot].spilled_ptr.live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1155 | break; |
| 1156 | /* ... then we depend on parent's value */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1157 | parent->frame[frameno]->stack[slot].spilled_ptr.live |= REG_LIVE_READ; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1158 | state = parent; |
| 1159 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1160 | writes = true; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1164 | static int check_stack_read(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1165 | struct bpf_func_state *reg_state /* func where register points to */, |
| 1166 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1167 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1168 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1169 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1170 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
| 1171 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1172 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1173 | if (reg_state->allocated_stack <= slot) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1174 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 1175 | off, size); |
| 1176 | return -EACCES; |
| 1177 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1178 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1179 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1180 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1181 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1182 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1183 | return -EACCES; |
| 1184 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1185 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1186 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1187 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1188 | return -EACCES; |
| 1189 | } |
| 1190 | } |
| 1191 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1192 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1193 | /* restore register state from stack */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1194 | state->regs[value_regno] = reg_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 1195 | /* mark reg as written since spilled pointer state likely |
| 1196 | * has its liveness marks cleared by is_state_visited() |
| 1197 | * which resets stack/reg liveness for state transitions |
| 1198 | */ |
| 1199 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1200 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1201 | mark_stack_slot_read(env, vstate, vstate->parent, spi, |
| 1202 | reg_state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1203 | return 0; |
| 1204 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1205 | int zeros = 0; |
| 1206 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1207 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1208 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 1209 | continue; |
| 1210 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 1211 | zeros++; |
| 1212 | continue; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1213 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1214 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 1215 | off, i, size); |
| 1216 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1217 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1218 | mark_stack_slot_read(env, vstate, vstate->parent, spi, |
| 1219 | reg_state->frameno); |
| 1220 | if (value_regno >= 0) { |
| 1221 | if (zeros == size) { |
| 1222 | /* any size read into register is zero extended, |
| 1223 | * so the whole register == const_zero |
| 1224 | */ |
| 1225 | __mark_reg_const_zero(&state->regs[value_regno]); |
| 1226 | } else { |
| 1227 | /* have read misc data from the stack */ |
| 1228 | mark_reg_unknown(env, state->regs, value_regno); |
| 1229 | } |
| 1230 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 1231 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1232 | return 0; |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1237 | 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] | 1238 | int size, bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1239 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1240 | struct bpf_reg_state *regs = cur_regs(env); |
| 1241 | struct bpf_map *map = regs[regno].map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1242 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1243 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1244 | off + size > map->value_size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1245 | 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] | 1246 | map->value_size, off, size); |
| 1247 | return -EACCES; |
| 1248 | } |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1252 | /* check read/write into a map element with possible variable offset */ |
| 1253 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1254 | int off, int size, bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1255 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1256 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1257 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1258 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 1259 | int err; |
| 1260 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1261 | /* We may have adjusted the register to this map value, so we |
| 1262 | * need to try adding each of min_value and max_value to off |
| 1263 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1264 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1265 | if (env->log.level) |
| 1266 | print_verifier_state(env, state); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1267 | /* The minimum value is only important with signed |
| 1268 | * comparisons where we can't assume the floor of a |
| 1269 | * value is 0. If we are using signed variables for our |
| 1270 | * index'es we need to make sure that whatever we use |
| 1271 | * will have a set floor within our range. |
| 1272 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1273 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1274 | 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] | 1275 | regno); |
| 1276 | return -EACCES; |
| 1277 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1278 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
| 1279 | zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1280 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1281 | verbose(env, "R%d min value is outside of the array range\n", |
| 1282 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1283 | return err; |
| 1284 | } |
| 1285 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1286 | /* If we haven't set a max value then we need to bail since we can't be |
| 1287 | * sure we won't do bad things. |
| 1288 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1289 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1290 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1291 | 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] | 1292 | regno); |
| 1293 | return -EACCES; |
| 1294 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1295 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
| 1296 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1297 | if (err) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1298 | verbose(env, "R%d max value is outside of the array range\n", |
| 1299 | regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1300 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1303 | #define MAX_PACKET_OFF 0xffff |
| 1304 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1305 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1306 | const struct bpf_call_arg_meta *meta, |
| 1307 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1308 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1309 | switch (env->prog->type) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1310 | case BPF_PROG_TYPE_LWT_IN: |
| 1311 | case BPF_PROG_TYPE_LWT_OUT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 1312 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1313 | /* dst_input() and dst_output() can't write for now */ |
| 1314 | if (t == BPF_WRITE) |
| 1315 | return false; |
Alexander Alemayhu | 7e57fbb | 2017-02-14 00:02:35 +0100 | [diff] [blame] | 1316 | /* fallthrough */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1317 | case BPF_PROG_TYPE_SCHED_CLS: |
| 1318 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1319 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1320 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 1321 | case BPF_PROG_TYPE_SK_SKB: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1322 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1323 | if (meta) |
| 1324 | return meta->pkt_access; |
| 1325 | |
| 1326 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1327 | return true; |
| 1328 | default: |
| 1329 | return false; |
| 1330 | } |
| 1331 | } |
| 1332 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1333 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1334 | int off, int size, bool zero_size_allowed) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1335 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1336 | struct bpf_reg_state *regs = cur_regs(env); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1337 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1338 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1339 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1340 | (u64)off + size > reg->range) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1341 | 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] | 1342 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1343 | return -EACCES; |
| 1344 | } |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1348 | 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] | 1349 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1350 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1351 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1352 | struct bpf_reg_state *reg = ®s[regno]; |
| 1353 | int err; |
| 1354 | |
| 1355 | /* We may have added a variable offset to the packet pointer; but any |
| 1356 | * reg->range we have comes after that. We are only checking the fixed |
| 1357 | * offset. |
| 1358 | */ |
| 1359 | |
| 1360 | /* We don't allow negative numbers, because we aren't tracking enough |
| 1361 | * detail to prove they're safe. |
| 1362 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1363 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1364 | 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] | 1365 | regno); |
| 1366 | return -EACCES; |
| 1367 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1368 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1369 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1370 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1371 | return err; |
| 1372 | } |
| 1373 | return err; |
| 1374 | } |
| 1375 | |
| 1376 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1377 | 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] | 1378 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1379 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1380 | struct bpf_insn_access_aux info = { |
| 1381 | .reg_type = *reg_type, |
| 1382 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1383 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1384 | if (env->ops->is_valid_access && |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1385 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1386 | /* A non zero info.ctx_field_size indicates that this field is a |
| 1387 | * candidate for later verifier transformation to load the whole |
| 1388 | * field and then apply a mask when accessed with a narrower |
| 1389 | * access than actual ctx access size. A zero info.ctx_field_size |
| 1390 | * will only allow for whole field access and rejects any other |
| 1391 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1392 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 1393 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1394 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1395 | 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] | 1396 | /* remember the offset of last byte accessed in ctx */ |
| 1397 | if (env->prog->aux->max_ctx_offset < off + size) |
| 1398 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1399 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 1400 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1401 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1402 | 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] | 1403 | return -EACCES; |
| 1404 | } |
| 1405 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1406 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 1407 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1408 | { |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1409 | if (allow_ptr_leaks) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1410 | return false; |
| 1411 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1412 | return reg->type != SCALAR_VALUE; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1415 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 1416 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1417 | return __is_pointer_value(env->allow_ptr_leaks, cur_regs(env) + regno); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1418 | } |
| 1419 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1420 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 1421 | { |
| 1422 | const struct bpf_reg_state *reg = cur_regs(env) + regno; |
| 1423 | |
| 1424 | return reg->type == PTR_TO_CTX; |
| 1425 | } |
| 1426 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1427 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 1428 | { |
| 1429 | const struct bpf_reg_state *reg = cur_regs(env) + regno; |
| 1430 | |
| 1431 | return type_is_pkt_pointer(reg->type); |
| 1432 | } |
| 1433 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1434 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 1435 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1436 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1437 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1438 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1439 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1440 | |
| 1441 | /* Byte size accesses are always allowed. */ |
| 1442 | if (!strict || size == 1) |
| 1443 | return 0; |
| 1444 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1445 | /* For platforms that do not have a Kconfig enabling |
| 1446 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 1447 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 1448 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 1449 | * to this code only in strict mode where we want to emulate |
| 1450 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 1451 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1452 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1453 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1454 | |
| 1455 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 1456 | if (!tnum_is_aligned(reg_off, size)) { |
| 1457 | char tn_buf[48]; |
| 1458 | |
| 1459 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1460 | verbose(env, |
| 1461 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1462 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1463 | return -EACCES; |
| 1464 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1465 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1466 | return 0; |
| 1467 | } |
| 1468 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1469 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 1470 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1471 | const char *pointer_desc, |
| 1472 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1473 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1474 | struct tnum reg_off; |
| 1475 | |
| 1476 | /* Byte size accesses are always allowed. */ |
| 1477 | if (!strict || size == 1) |
| 1478 | return 0; |
| 1479 | |
| 1480 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 1481 | if (!tnum_is_aligned(reg_off, size)) { |
| 1482 | char tn_buf[48]; |
| 1483 | |
| 1484 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1485 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1486 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1487 | return -EACCES; |
| 1488 | } |
| 1489 | |
| 1490 | return 0; |
| 1491 | } |
| 1492 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1493 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1494 | const struct bpf_reg_state *reg, int off, |
| 1495 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1496 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1497 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1498 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1499 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1500 | switch (reg->type) { |
| 1501 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1502 | case PTR_TO_PACKET_META: |
| 1503 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 1504 | * right in front, treat it the very same way. |
| 1505 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1506 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1507 | case PTR_TO_MAP_VALUE: |
| 1508 | pointer_desc = "value "; |
| 1509 | break; |
| 1510 | case PTR_TO_CTX: |
| 1511 | pointer_desc = "context "; |
| 1512 | break; |
| 1513 | case PTR_TO_STACK: |
| 1514 | pointer_desc = "stack "; |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 1515 | /* The stack spill tracking logic in check_stack_write() |
| 1516 | * and check_stack_read() relies on stack accesses being |
| 1517 | * aligned. |
| 1518 | */ |
| 1519 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1520 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1521 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1522 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1523 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1524 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 1525 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1526 | } |
| 1527 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1528 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 1529 | const struct bpf_func_state *func, |
| 1530 | int off) |
| 1531 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1532 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1533 | |
| 1534 | if (stack >= -off) |
| 1535 | return 0; |
| 1536 | |
| 1537 | /* update known max for given subprogram */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1538 | env->subprog_info[func->subprogno].stack_depth = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1539 | return 0; |
| 1540 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1541 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1542 | /* starting from main bpf function walk all instructions of the function |
| 1543 | * and recursively walk all callees that given function can call. |
| 1544 | * Ignore jump and exit insns. |
| 1545 | * Since recursion is prevented by check_cfg() this algorithm |
| 1546 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 1547 | */ |
| 1548 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 1549 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1550 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 1551 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1552 | struct bpf_insn *insn = env->prog->insnsi; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1553 | int ret_insn[MAX_CALL_FRAMES]; |
| 1554 | int ret_prog[MAX_CALL_FRAMES]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1555 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1556 | process_func: |
| 1557 | /* round up to 32-bytes, since this is granularity |
| 1558 | * of interpreter stack size |
| 1559 | */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1560 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1561 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1562 | 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] | 1563 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1564 | return -EACCES; |
| 1565 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1566 | continue_func: |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1567 | subprog_end = subprog[idx + 1].start; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1568 | for (; i < subprog_end; i++) { |
| 1569 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1570 | continue; |
| 1571 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1572 | continue; |
| 1573 | /* remember insn and function to return to */ |
| 1574 | ret_insn[frame] = i + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1575 | ret_prog[frame] = idx; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1576 | |
| 1577 | /* find the callee */ |
| 1578 | i = i + insn[i].imm + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1579 | idx = find_subprog(env, i); |
| 1580 | if (idx < 0) { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1581 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1582 | i); |
| 1583 | return -EFAULT; |
| 1584 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1585 | frame++; |
| 1586 | if (frame >= MAX_CALL_FRAMES) { |
| 1587 | WARN_ONCE(1, "verifier bug. Call stack is too deep\n"); |
| 1588 | return -EFAULT; |
| 1589 | } |
| 1590 | goto process_func; |
| 1591 | } |
| 1592 | /* end of for() loop means the last insn of the 'subprog' |
| 1593 | * was reached. Doesn't matter whether it was JA or EXIT |
| 1594 | */ |
| 1595 | if (frame == 0) |
| 1596 | return 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1597 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1598 | frame--; |
| 1599 | i = ret_insn[frame]; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1600 | idx = ret_prog[frame]; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1601 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1602 | } |
| 1603 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1604 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1605 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 1606 | const struct bpf_insn *insn, int idx) |
| 1607 | { |
| 1608 | int start = idx + insn->imm + 1, subprog; |
| 1609 | |
| 1610 | subprog = find_subprog(env, start); |
| 1611 | if (subprog < 0) { |
| 1612 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1613 | start); |
| 1614 | return -EFAULT; |
| 1615 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1616 | return env->subprog_info[subprog].stack_depth; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1617 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1618 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1619 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 1620 | static int check_ctx_reg(struct bpf_verifier_env *env, |
| 1621 | const struct bpf_reg_state *reg, int regno) |
| 1622 | { |
| 1623 | /* Access to ctx or passing it to a helper is only allowed in |
| 1624 | * its original, unmodified form. |
| 1625 | */ |
| 1626 | |
| 1627 | if (reg->off) { |
| 1628 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 1629 | regno, reg->off); |
| 1630 | return -EACCES; |
| 1631 | } |
| 1632 | |
| 1633 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 1634 | char tn_buf[48]; |
| 1635 | |
| 1636 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 1637 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 1638 | return -EACCES; |
| 1639 | } |
| 1640 | |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 1644 | /* truncate register to smaller size (in bytes) |
| 1645 | * must be called with size < BPF_REG_SIZE |
| 1646 | */ |
| 1647 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 1648 | { |
| 1649 | u64 mask; |
| 1650 | |
| 1651 | /* clear high bits in bit representation */ |
| 1652 | reg->var_off = tnum_cast(reg->var_off, size); |
| 1653 | |
| 1654 | /* fix arithmetic bounds */ |
| 1655 | mask = ((u64)1 << (size * 8)) - 1; |
| 1656 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 1657 | reg->umin_value &= mask; |
| 1658 | reg->umax_value &= mask; |
| 1659 | } else { |
| 1660 | reg->umin_value = 0; |
| 1661 | reg->umax_value = mask; |
| 1662 | } |
| 1663 | reg->smin_value = reg->umin_value; |
| 1664 | reg->smax_value = reg->umax_value; |
| 1665 | } |
| 1666 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1667 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 1668 | * if t==write, value_regno is a register which value is stored into memory |
| 1669 | * if t==read, value_regno is a register which will receive the value from memory |
| 1670 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 1671 | * if t==read && value_regno==-1, don't care what we read from memory |
| 1672 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1673 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 1674 | int off, int bpf_size, enum bpf_access_type t, |
| 1675 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1676 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1677 | struct bpf_reg_state *regs = cur_regs(env); |
| 1678 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1679 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1680 | int size, err = 0; |
| 1681 | |
| 1682 | size = bpf_size_to_bytes(bpf_size); |
| 1683 | if (size < 0) |
| 1684 | return size; |
| 1685 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1686 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1687 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1688 | if (err) |
| 1689 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1690 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1691 | /* for access checks, reg->off is just part of off */ |
| 1692 | off += reg->off; |
| 1693 | |
| 1694 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1695 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1696 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1697 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1698 | return -EACCES; |
| 1699 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1700 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1701 | err = check_map_access(env, regno, off, size, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1702 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1703 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1704 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1705 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1706 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 1707 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1708 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1709 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1710 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1711 | return -EACCES; |
| 1712 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1713 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 1714 | err = check_ctx_reg(env, reg, regno); |
| 1715 | if (err < 0) |
| 1716 | return err; |
| 1717 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1718 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1719 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1720 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1721 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 1722 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1723 | */ |
| 1724 | if (reg_type == SCALAR_VALUE) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1725 | mark_reg_unknown(env, regs, value_regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1726 | else |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1727 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1728 | value_regno); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1729 | regs[value_regno].id = 0; |
| 1730 | regs[value_regno].off = 0; |
| 1731 | regs[value_regno].range = 0; |
| 1732 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1733 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1734 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1735 | } else if (reg->type == PTR_TO_STACK) { |
| 1736 | /* stack accesses must be at a fixed offset, so that we can |
| 1737 | * determine what type of data were returned. |
| 1738 | * See check_stack_read(). |
| 1739 | */ |
| 1740 | if (!tnum_is_const(reg->var_off)) { |
| 1741 | char tn_buf[48]; |
| 1742 | |
| 1743 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1744 | verbose(env, "variable stack access var_off=%s off=%d size=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1745 | tn_buf, off, size); |
| 1746 | return -EACCES; |
| 1747 | } |
| 1748 | off += reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1749 | if (off >= 0 || off < -MAX_BPF_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1750 | verbose(env, "invalid stack off=%d size=%d\n", off, |
| 1751 | size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1752 | return -EACCES; |
| 1753 | } |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1754 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1755 | state = func(env, reg); |
| 1756 | err = update_stack_depth(env, state, off); |
| 1757 | if (err) |
| 1758 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1759 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1760 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1761 | err = check_stack_write(env, state, off, size, |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 1762 | value_regno, insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1763 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1764 | err = check_stack_read(env, state, off, size, |
| 1765 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1766 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1767 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1768 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1769 | return -EACCES; |
| 1770 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1771 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1772 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1773 | verbose(env, "R%d leaks addr into packet\n", |
| 1774 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1775 | return -EACCES; |
| 1776 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1777 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1778 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1779 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1780 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1781 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 1782 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1783 | return -EACCES; |
| 1784 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1785 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1786 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1787 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1788 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 1789 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1790 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1791 | return err; |
| 1792 | } |
| 1793 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1794 | 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] | 1795 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1796 | int err; |
| 1797 | |
| 1798 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 1799 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1800 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1801 | return -EINVAL; |
| 1802 | } |
| 1803 | |
| 1804 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1805 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1806 | if (err) |
| 1807 | return err; |
| 1808 | |
| 1809 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1810 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1811 | if (err) |
| 1812 | return err; |
| 1813 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1814 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1815 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1816 | return -EACCES; |
| 1817 | } |
| 1818 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1819 | if (is_ctx_reg(env, insn->dst_reg) || |
| 1820 | is_pkt_reg(env, insn->dst_reg)) { |
| 1821 | verbose(env, "BPF_XADD stores into R%d %s is not allowed\n", |
| 1822 | insn->dst_reg, is_ctx_reg(env, insn->dst_reg) ? |
| 1823 | "context" : "packet"); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1824 | return -EACCES; |
| 1825 | } |
| 1826 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1827 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1828 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1829 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1830 | if (err) |
| 1831 | return err; |
| 1832 | |
| 1833 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1834 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1835 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | /* when register 'regno' is passed into function that will read 'access_size' |
| 1839 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1840 | * and all elements of stack are initialized. |
| 1841 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 1842 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1843 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1844 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1845 | int access_size, bool zero_size_allowed, |
| 1846 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1847 | { |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1848 | struct bpf_reg_state *reg = cur_regs(env) + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1849 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1850 | int off, i, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1851 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1852 | if (reg->type != PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1853 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1854 | if (zero_size_allowed && access_size == 0 && |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1855 | register_is_null(reg)) |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1856 | return 0; |
| 1857 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1858 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1859 | reg_type_str[reg->type], |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1860 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1861 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1862 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1863 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1864 | /* Only allow fixed-offset stack reads */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1865 | if (!tnum_is_const(reg->var_off)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1866 | char tn_buf[48]; |
| 1867 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1868 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1869 | verbose(env, "invalid variable stack read R%d var_off=%s\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1870 | regno, tn_buf); |
Jann Horn | ea25f91 | 2017-12-18 20:11:57 -0800 | [diff] [blame] | 1871 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1872 | } |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1873 | off = reg->off + reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1874 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1875 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1876 | 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] | 1877 | regno, off, access_size); |
| 1878 | return -EACCES; |
| 1879 | } |
| 1880 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1881 | if (meta && meta->raw_mode) { |
| 1882 | meta->access_size = access_size; |
| 1883 | meta->regno = regno; |
| 1884 | return 0; |
| 1885 | } |
| 1886 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1887 | for (i = 0; i < access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1888 | u8 *stype; |
| 1889 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1890 | slot = -(off + i) - 1; |
| 1891 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1892 | if (state->allocated_stack <= slot) |
| 1893 | goto err; |
| 1894 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 1895 | if (*stype == STACK_MISC) |
| 1896 | goto mark; |
| 1897 | if (*stype == STACK_ZERO) { |
| 1898 | /* helper can write anything into the stack */ |
| 1899 | *stype = STACK_MISC; |
| 1900 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1901 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1902 | err: |
| 1903 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 1904 | off, i, access_size); |
| 1905 | return -EACCES; |
| 1906 | mark: |
| 1907 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 1908 | * the whole slot to be marked as 'read' |
| 1909 | */ |
| 1910 | mark_stack_slot_read(env, env->cur_state, env->cur_state->parent, |
| 1911 | spi, state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1912 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1913 | return update_stack_depth(env, state, off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1914 | } |
| 1915 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1916 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 1917 | int access_size, bool zero_size_allowed, |
| 1918 | struct bpf_call_arg_meta *meta) |
| 1919 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1920 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1921 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1922 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1923 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1924 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1925 | return check_packet_access(env, regno, reg->off, access_size, |
| 1926 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1927 | case PTR_TO_MAP_VALUE: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1928 | return check_map_access(env, regno, reg->off, access_size, |
| 1929 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1930 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1931 | return check_stack_boundary(env, regno, access_size, |
| 1932 | zero_size_allowed, meta); |
| 1933 | } |
| 1934 | } |
| 1935 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 1936 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 1937 | { |
| 1938 | return type == ARG_PTR_TO_MEM || |
| 1939 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 1940 | type == ARG_PTR_TO_UNINIT_MEM; |
| 1941 | } |
| 1942 | |
| 1943 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 1944 | { |
| 1945 | return type == ARG_CONST_SIZE || |
| 1946 | type == ARG_CONST_SIZE_OR_ZERO; |
| 1947 | } |
| 1948 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1949 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1950 | enum bpf_arg_type arg_type, |
| 1951 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1952 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1953 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1954 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1955 | int err = 0; |
| 1956 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1957 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1958 | return 0; |
| 1959 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1960 | err = check_reg_arg(env, regno, SRC_OP); |
| 1961 | if (err) |
| 1962 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1963 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1964 | if (arg_type == ARG_ANYTHING) { |
| 1965 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1966 | verbose(env, "R%d leaks addr into helper function\n", |
| 1967 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1968 | return -EACCES; |
| 1969 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1970 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1971 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1972 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1973 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1974 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1975 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1976 | return -EACCES; |
| 1977 | } |
| 1978 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1979 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1980 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1981 | expected_type = PTR_TO_STACK; |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 1982 | if (!type_is_pkt_pointer(type) && type != PTR_TO_MAP_VALUE && |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1983 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1984 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1985 | } else if (arg_type == ARG_CONST_SIZE || |
| 1986 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1987 | expected_type = SCALAR_VALUE; |
| 1988 | if (type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1989 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1990 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 1991 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1992 | if (type != expected_type) |
| 1993 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1994 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 1995 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1996 | if (type != expected_type) |
| 1997 | goto err_type; |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 1998 | err = check_ctx_reg(env, reg, regno); |
| 1999 | if (err < 0) |
| 2000 | return err; |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2001 | } else if (arg_type_is_mem_ptr(arg_type)) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2002 | expected_type = PTR_TO_STACK; |
| 2003 | /* One exception here. In case function allows for NULL to be |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2004 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 2005 | * happens during stack boundary checking. |
| 2006 | */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 2007 | if (register_is_null(reg) && |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 2008 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2009 | /* final test in check_stack_boundary() */; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2010 | else if (!type_is_pkt_pointer(type) && |
| 2011 | type != PTR_TO_MAP_VALUE && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2012 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2013 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2014 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2015 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2016 | verbose(env, "unsupported arg_type %d\n", arg_type); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2017 | return -EFAULT; |
| 2018 | } |
| 2019 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2020 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 2021 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2022 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2023 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 2024 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 2025 | * check that [key, key + map->key_size) are within |
| 2026 | * stack limits and initialized |
| 2027 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2028 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2029 | /* in function declaration map_ptr must come before |
| 2030 | * map_key, so that it's verified and known before |
| 2031 | * we have to check map_key here. Otherwise it means |
| 2032 | * that kernel subsystem misconfigured verifier |
| 2033 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2034 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2035 | return -EACCES; |
| 2036 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2037 | err = check_helper_mem_access(env, regno, |
| 2038 | meta->map_ptr->key_size, false, |
| 2039 | NULL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2040 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 2041 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 2042 | * check [value, value + map->value_size) validity |
| 2043 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2044 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2045 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2046 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2047 | return -EACCES; |
| 2048 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 2049 | err = check_helper_mem_access(env, regno, |
| 2050 | meta->map_ptr->value_size, false, |
| 2051 | NULL); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2052 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2053 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2054 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2055 | /* remember the mem_size which may be used later |
| 2056 | * to refine return values. |
| 2057 | */ |
| 2058 | meta->msize_smax_value = reg->smax_value; |
| 2059 | meta->msize_umax_value = reg->umax_value; |
| 2060 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2061 | /* The register is SCALAR_VALUE; the access check |
| 2062 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2063 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2064 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2065 | /* For unprivileged variable accesses, disable raw |
| 2066 | * mode so that the program is required to |
| 2067 | * initialize all the memory that the helper could |
| 2068 | * just partially fill up. |
| 2069 | */ |
| 2070 | meta = NULL; |
| 2071 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2072 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2073 | 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] | 2074 | regno); |
| 2075 | return -EACCES; |
| 2076 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2077 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2078 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2079 | err = check_helper_mem_access(env, regno - 1, 0, |
| 2080 | zero_size_allowed, |
| 2081 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2082 | if (err) |
| 2083 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2084 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2085 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2086 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2087 | 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] | 2088 | regno); |
| 2089 | return -EACCES; |
| 2090 | } |
| 2091 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2092 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2093 | zero_size_allowed, meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2097 | err_type: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2098 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2099 | reg_type_str[type], reg_type_str[expected_type]); |
| 2100 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2101 | } |
| 2102 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2103 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 2104 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2105 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2106 | if (!map) |
| 2107 | return 0; |
| 2108 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2109 | /* We need a two way check, first is from map perspective ... */ |
| 2110 | switch (map->map_type) { |
| 2111 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 2112 | if (func_id != BPF_FUNC_tail_call) |
| 2113 | goto error; |
| 2114 | break; |
| 2115 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 2116 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2117 | func_id != BPF_FUNC_perf_event_output && |
| 2118 | func_id != BPF_FUNC_perf_event_read_value) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2119 | goto error; |
| 2120 | break; |
| 2121 | case BPF_MAP_TYPE_STACK_TRACE: |
| 2122 | if (func_id != BPF_FUNC_get_stackid) |
| 2123 | goto error; |
| 2124 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 2125 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 2126 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2127 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2128 | goto error; |
| 2129 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2130 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
| 2131 | * allow to be modified from bpf side. So do not allow lookup elements |
| 2132 | * for now. |
| 2133 | */ |
| 2134 | case BPF_MAP_TYPE_DEVMAP: |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 2135 | if (func_id != BPF_FUNC_redirect_map) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2136 | goto error; |
| 2137 | break; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2138 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 2139 | * appear. |
| 2140 | */ |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2141 | case BPF_MAP_TYPE_CPUMAP: |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2142 | case BPF_MAP_TYPE_XSKMAP: |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2143 | if (func_id != BPF_FUNC_redirect_map) |
| 2144 | goto error; |
| 2145 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2146 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 2147 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2148 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 2149 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 2150 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2151 | case BPF_MAP_TYPE_SOCKMAP: |
| 2152 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 2153 | func_id != BPF_FUNC_sock_map_update && |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2154 | func_id != BPF_FUNC_map_delete_elem && |
| 2155 | func_id != BPF_FUNC_msg_redirect_map) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2156 | goto error; |
| 2157 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2158 | case BPF_MAP_TYPE_SOCKHASH: |
| 2159 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 2160 | func_id != BPF_FUNC_sock_hash_update && |
| 2161 | func_id != BPF_FUNC_map_delete_elem && |
| 2162 | func_id != BPF_FUNC_msg_redirect_hash) |
| 2163 | goto error; |
| 2164 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2165 | default: |
| 2166 | break; |
| 2167 | } |
| 2168 | |
| 2169 | /* ... and second from the function itself. */ |
| 2170 | switch (func_id) { |
| 2171 | case BPF_FUNC_tail_call: |
| 2172 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 2173 | goto error; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 2174 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2175 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
| 2176 | return -EINVAL; |
| 2177 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2178 | break; |
| 2179 | case BPF_FUNC_perf_event_read: |
| 2180 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2181 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2182 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 2183 | goto error; |
| 2184 | break; |
| 2185 | case BPF_FUNC_get_stackid: |
| 2186 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 2187 | goto error; |
| 2188 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2189 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 2190 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2191 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 2192 | goto error; |
| 2193 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2194 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 2195 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 2196 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 2197 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2198 | goto error; |
| 2199 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2200 | case BPF_FUNC_sk_redirect_map: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2201 | case BPF_FUNC_msg_redirect_map: |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2202 | case BPF_FUNC_sock_map_update: |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2203 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 2204 | goto error; |
| 2205 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 2206 | case BPF_FUNC_sk_redirect_hash: |
| 2207 | case BPF_FUNC_msg_redirect_hash: |
| 2208 | case BPF_FUNC_sock_hash_update: |
| 2209 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2210 | goto error; |
| 2211 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2212 | default: |
| 2213 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2217 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2218 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2219 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2220 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2223 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2224 | { |
| 2225 | int count = 0; |
| 2226 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2227 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2228 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2229 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2230 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2231 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2232 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2233 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2234 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2235 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2236 | count++; |
| 2237 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2238 | /* We only support one arg being in raw mode at the moment, |
| 2239 | * which is sufficient for the helper functions we have |
| 2240 | * right now. |
| 2241 | */ |
| 2242 | return count <= 1; |
| 2243 | } |
| 2244 | |
| 2245 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 2246 | enum bpf_arg_type arg_next) |
| 2247 | { |
| 2248 | return (arg_type_is_mem_ptr(arg_curr) && |
| 2249 | !arg_type_is_mem_size(arg_next)) || |
| 2250 | (!arg_type_is_mem_ptr(arg_curr) && |
| 2251 | arg_type_is_mem_size(arg_next)); |
| 2252 | } |
| 2253 | |
| 2254 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 2255 | { |
| 2256 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 2257 | * bytes from memory 'buf'. Both arg types need |
| 2258 | * to be paired, so make sure there's no buggy |
| 2259 | * helper function specification. |
| 2260 | */ |
| 2261 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 2262 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 2263 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 2264 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 2265 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 2266 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 2267 | return false; |
| 2268 | |
| 2269 | return true; |
| 2270 | } |
| 2271 | |
| 2272 | static int check_func_proto(const struct bpf_func_proto *fn) |
| 2273 | { |
| 2274 | return check_raw_mode_ok(fn) && |
| 2275 | check_arg_pair_ok(fn) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2276 | } |
| 2277 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2278 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 2279 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2280 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2281 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 2282 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2283 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2284 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2285 | int i; |
| 2286 | |
| 2287 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2288 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2289 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2290 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2291 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 2292 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2293 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2294 | reg = &state->stack[i].spilled_ptr; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2295 | if (reg_is_pkt_pointer_any(reg)) |
| 2296 | __mark_reg_unknown(reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2297 | } |
| 2298 | } |
| 2299 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2300 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 2301 | { |
| 2302 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2303 | int i; |
| 2304 | |
| 2305 | for (i = 0; i <= vstate->curframe; i++) |
| 2306 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 2307 | } |
| 2308 | |
| 2309 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 2310 | int *insn_idx) |
| 2311 | { |
| 2312 | struct bpf_verifier_state *state = env->cur_state; |
| 2313 | struct bpf_func_state *caller, *callee; |
| 2314 | int i, subprog, target_insn; |
| 2315 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 2316 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2317 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 2318 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2319 | return -E2BIG; |
| 2320 | } |
| 2321 | |
| 2322 | target_insn = *insn_idx + insn->imm; |
| 2323 | subprog = find_subprog(env, target_insn + 1); |
| 2324 | if (subprog < 0) { |
| 2325 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 2326 | target_insn + 1); |
| 2327 | return -EFAULT; |
| 2328 | } |
| 2329 | |
| 2330 | caller = state->frame[state->curframe]; |
| 2331 | if (state->frame[state->curframe + 1]) { |
| 2332 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 2333 | state->curframe + 1); |
| 2334 | return -EFAULT; |
| 2335 | } |
| 2336 | |
| 2337 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 2338 | if (!callee) |
| 2339 | return -ENOMEM; |
| 2340 | state->frame[state->curframe + 1] = callee; |
| 2341 | |
| 2342 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 2343 | * into its own stack before reading from it. |
| 2344 | * callee can read/write into caller's stack |
| 2345 | */ |
| 2346 | init_func_state(env, callee, |
| 2347 | /* remember the callsite, it will be used by bpf_exit */ |
| 2348 | *insn_idx /* callsite */, |
| 2349 | state->curframe + 1 /* frameno within this callchain */, |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 2350 | subprog /* subprog number within this prog */); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2351 | |
| 2352 | /* copy r1 - r5 args that callee can access */ |
| 2353 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 2354 | callee->regs[i] = caller->regs[i]; |
| 2355 | |
| 2356 | /* after the call regsiters r0 - r5 were scratched */ |
| 2357 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 2358 | mark_reg_not_init(env, caller->regs, caller_saved[i]); |
| 2359 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2360 | } |
| 2361 | |
| 2362 | /* only increment it after check_reg_arg() finished */ |
| 2363 | state->curframe++; |
| 2364 | |
| 2365 | /* and go analyze first insn of the callee */ |
| 2366 | *insn_idx = target_insn; |
| 2367 | |
| 2368 | if (env->log.level) { |
| 2369 | verbose(env, "caller:\n"); |
| 2370 | print_verifier_state(env, caller); |
| 2371 | verbose(env, "callee:\n"); |
| 2372 | print_verifier_state(env, callee); |
| 2373 | } |
| 2374 | return 0; |
| 2375 | } |
| 2376 | |
| 2377 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 2378 | { |
| 2379 | struct bpf_verifier_state *state = env->cur_state; |
| 2380 | struct bpf_func_state *caller, *callee; |
| 2381 | struct bpf_reg_state *r0; |
| 2382 | |
| 2383 | callee = state->frame[state->curframe]; |
| 2384 | r0 = &callee->regs[BPF_REG_0]; |
| 2385 | if (r0->type == PTR_TO_STACK) { |
| 2386 | /* technically it's ok to return caller's stack pointer |
| 2387 | * (or caller's caller's pointer) back to the caller, |
| 2388 | * since these pointers are valid. Only current stack |
| 2389 | * pointer will be invalid as soon as function exits, |
| 2390 | * but let's be conservative |
| 2391 | */ |
| 2392 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 2393 | return -EINVAL; |
| 2394 | } |
| 2395 | |
| 2396 | state->curframe--; |
| 2397 | caller = state->frame[state->curframe]; |
| 2398 | /* return to the caller whatever r0 had in the callee */ |
| 2399 | caller->regs[BPF_REG_0] = *r0; |
| 2400 | |
| 2401 | *insn_idx = callee->callsite + 1; |
| 2402 | if (env->log.level) { |
| 2403 | verbose(env, "returning from callee:\n"); |
| 2404 | print_verifier_state(env, callee); |
| 2405 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 2406 | print_verifier_state(env, caller); |
| 2407 | } |
| 2408 | /* clear everything in the callee */ |
| 2409 | free_func_state(callee); |
| 2410 | state->frame[state->curframe + 1] = NULL; |
| 2411 | return 0; |
| 2412 | } |
| 2413 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2414 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 2415 | int func_id, |
| 2416 | struct bpf_call_arg_meta *meta) |
| 2417 | { |
| 2418 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 2419 | |
| 2420 | if (ret_type != RET_INTEGER || |
| 2421 | (func_id != BPF_FUNC_get_stack && |
| 2422 | func_id != BPF_FUNC_probe_read_str)) |
| 2423 | return; |
| 2424 | |
| 2425 | ret_reg->smax_value = meta->msize_smax_value; |
| 2426 | ret_reg->umax_value = meta->msize_umax_value; |
| 2427 | __reg_deduce_bounds(ret_reg); |
| 2428 | __reg_bound_offset(ret_reg); |
| 2429 | } |
| 2430 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2431 | static int |
| 2432 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 2433 | int func_id, int insn_idx) |
| 2434 | { |
| 2435 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
| 2436 | |
| 2437 | if (func_id != BPF_FUNC_tail_call && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 2438 | func_id != BPF_FUNC_map_lookup_elem && |
| 2439 | func_id != BPF_FUNC_map_update_elem && |
| 2440 | func_id != BPF_FUNC_map_delete_elem) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2441 | return 0; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 2442 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2443 | if (meta->map_ptr == NULL) { |
| 2444 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 2445 | return -EINVAL; |
| 2446 | } |
| 2447 | |
| 2448 | if (!BPF_MAP_PTR(aux->map_state)) |
| 2449 | bpf_map_ptr_store(aux, meta->map_ptr, |
| 2450 | meta->map_ptr->unpriv_array); |
| 2451 | else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr) |
| 2452 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
| 2453 | meta->map_ptr->unpriv_array); |
| 2454 | return 0; |
| 2455 | } |
| 2456 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2457 | 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] | 2458 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2459 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2460 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2461 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2462 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2463 | int i, err; |
| 2464 | |
| 2465 | /* find function prototype */ |
| 2466 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2467 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 2468 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2469 | return -EINVAL; |
| 2470 | } |
| 2471 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 2472 | if (env->ops->get_func_proto) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 2473 | fn = env->ops->get_func_proto(func_id, env->prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2474 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2475 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 2476 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2477 | return -EINVAL; |
| 2478 | } |
| 2479 | |
| 2480 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 2481 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Daniel Borkmann | 3fe2867 | 2018-06-02 23:06:33 +0200 | [diff] [blame] | 2482 | 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] | 2483 | return -EINVAL; |
| 2484 | } |
| 2485 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 2486 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 2487 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 2488 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 2489 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 2490 | func_id_name(func_id), func_id); |
| 2491 | return -EINVAL; |
| 2492 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2493 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2494 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2495 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2496 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2497 | err = check_func_proto(fn); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2498 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2499 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2500 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2501 | return err; |
| 2502 | } |
| 2503 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2504 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2505 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2506 | if (err) |
| 2507 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2508 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2509 | if (err) |
| 2510 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2511 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2512 | if (err) |
| 2513 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2514 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2515 | if (err) |
| 2516 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2517 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2518 | if (err) |
| 2519 | return err; |
| 2520 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 2521 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 2522 | if (err) |
| 2523 | return err; |
| 2524 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2525 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 2526 | * is inferred from register state. |
| 2527 | */ |
| 2528 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2529 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 2530 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2531 | if (err) |
| 2532 | return err; |
| 2533 | } |
| 2534 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2535 | regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2536 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2537 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2538 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2539 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2540 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2541 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2542 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2543 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2544 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2545 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2546 | } else if (fn->ret_type == RET_VOID) { |
| 2547 | regs[BPF_REG_0].type = NOT_INIT; |
Roman Gushchin | 3e6a4b3 | 2018-08-02 14:27:22 -0700 | [diff] [blame] | 2548 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 2549 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 2550 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) |
| 2551 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
| 2552 | else |
| 2553 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2554 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2555 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2556 | regs[BPF_REG_0].off = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2557 | /* remember map_ptr, so that check_map_access() |
| 2558 | * can check 'value_size' boundary of memory access |
| 2559 | * to map element returned from bpf_map_lookup_elem() |
| 2560 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2561 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2562 | verbose(env, |
| 2563 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2564 | return -EINVAL; |
| 2565 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2566 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2567 | regs[BPF_REG_0].id = ++env->id_gen; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2568 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2569 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2570 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2571 | return -EINVAL; |
| 2572 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2573 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 2574 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
| 2575 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2576 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2577 | if (err) |
| 2578 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2579 | |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 2580 | if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) { |
| 2581 | const char *err_str; |
| 2582 | |
| 2583 | #ifdef CONFIG_PERF_EVENTS |
| 2584 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 2585 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 2586 | #else |
| 2587 | err = -ENOTSUPP; |
| 2588 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 2589 | #endif |
| 2590 | if (err) { |
| 2591 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 2592 | return err; |
| 2593 | } |
| 2594 | |
| 2595 | env->prog->has_callchain_buf = true; |
| 2596 | } |
| 2597 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2598 | if (changes_data) |
| 2599 | clear_all_pkt_pointers(env); |
| 2600 | return 0; |
| 2601 | } |
| 2602 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2603 | static bool signed_add_overflows(s64 a, s64 b) |
| 2604 | { |
| 2605 | /* Do the add in u64, where overflow is well-defined */ |
| 2606 | s64 res = (s64)((u64)a + (u64)b); |
| 2607 | |
| 2608 | if (b < 0) |
| 2609 | return res > a; |
| 2610 | return res < a; |
| 2611 | } |
| 2612 | |
| 2613 | static bool signed_sub_overflows(s64 a, s64 b) |
| 2614 | { |
| 2615 | /* Do the sub in u64, where overflow is well-defined */ |
| 2616 | s64 res = (s64)((u64)a - (u64)b); |
| 2617 | |
| 2618 | if (b < 0) |
| 2619 | return res < a; |
| 2620 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2621 | } |
| 2622 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2623 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 2624 | const struct bpf_reg_state *reg, |
| 2625 | enum bpf_reg_type type) |
| 2626 | { |
| 2627 | bool known = tnum_is_const(reg->var_off); |
| 2628 | s64 val = reg->var_off.value; |
| 2629 | s64 smin = reg->smin_value; |
| 2630 | |
| 2631 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 2632 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 2633 | reg_type_str[type], val); |
| 2634 | return false; |
| 2635 | } |
| 2636 | |
| 2637 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 2638 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 2639 | reg_type_str[type], reg->off); |
| 2640 | return false; |
| 2641 | } |
| 2642 | |
| 2643 | if (smin == S64_MIN) { |
| 2644 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 2645 | reg_type_str[type]); |
| 2646 | return false; |
| 2647 | } |
| 2648 | |
| 2649 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 2650 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 2651 | smin, reg_type_str[type]); |
| 2652 | return false; |
| 2653 | } |
| 2654 | |
| 2655 | return true; |
| 2656 | } |
| 2657 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2658 | /* 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] | 2659 | * Caller should also handle BPF_MOV case separately. |
| 2660 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 2661 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 2662 | */ |
| 2663 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 2664 | struct bpf_insn *insn, |
| 2665 | const struct bpf_reg_state *ptr_reg, |
| 2666 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2667 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2668 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2669 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2670 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2671 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2672 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 2673 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 2674 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 2675 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2676 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2677 | u32 dst = insn->dst_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2678 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2679 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2680 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 2681 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 2682 | smin_val > smax_val || umin_val > umax_val) { |
| 2683 | /* Taint dst register if offset had invalid bounds derived from |
| 2684 | * e.g. dead branches. |
| 2685 | */ |
| 2686 | __mark_reg_unknown(dst_reg); |
| 2687 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2688 | } |
| 2689 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2690 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 2691 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2692 | verbose(env, |
| 2693 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 2694 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2695 | return -EACCES; |
| 2696 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2697 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2698 | if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2699 | verbose(env, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n", |
| 2700 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2701 | return -EACCES; |
| 2702 | } |
| 2703 | if (ptr_reg->type == CONST_PTR_TO_MAP) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2704 | verbose(env, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n", |
| 2705 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2706 | return -EACCES; |
| 2707 | } |
| 2708 | if (ptr_reg->type == PTR_TO_PACKET_END) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2709 | verbose(env, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n", |
| 2710 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2711 | return -EACCES; |
| 2712 | } |
| 2713 | |
| 2714 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 2715 | * 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] | 2716 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2717 | dst_reg->type = ptr_reg->type; |
| 2718 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 2719 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2720 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 2721 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 2722 | return -EINVAL; |
| 2723 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2724 | switch (opcode) { |
| 2725 | case BPF_ADD: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2726 | /* We can take a fixed offset as long as it doesn't overflow |
| 2727 | * the s32 'off' field |
| 2728 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2729 | if (known && (ptr_reg->off + smin_val == |
| 2730 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2731 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2732 | dst_reg->smin_value = smin_ptr; |
| 2733 | dst_reg->smax_value = smax_ptr; |
| 2734 | dst_reg->umin_value = umin_ptr; |
| 2735 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2736 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2737 | dst_reg->off = ptr_reg->off + smin_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2738 | dst_reg->range = ptr_reg->range; |
| 2739 | break; |
| 2740 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2741 | /* A new variable offset is created. Note that off_reg->off |
| 2742 | * == 0, since it's a scalar. |
| 2743 | * dst_reg gets the pointer type and since some positive |
| 2744 | * integer value was added to the pointer, give it a new 'id' |
| 2745 | * if it's a PTR_TO_PACKET. |
| 2746 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 2747 | * added into the variable offset, and we copy the fixed offset |
| 2748 | * from ptr_reg. |
| 2749 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2750 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 2751 | signed_add_overflows(smax_ptr, smax_val)) { |
| 2752 | dst_reg->smin_value = S64_MIN; |
| 2753 | dst_reg->smax_value = S64_MAX; |
| 2754 | } else { |
| 2755 | dst_reg->smin_value = smin_ptr + smin_val; |
| 2756 | dst_reg->smax_value = smax_ptr + smax_val; |
| 2757 | } |
| 2758 | if (umin_ptr + umin_val < umin_ptr || |
| 2759 | umax_ptr + umax_val < umax_ptr) { |
| 2760 | dst_reg->umin_value = 0; |
| 2761 | dst_reg->umax_value = U64_MAX; |
| 2762 | } else { |
| 2763 | dst_reg->umin_value = umin_ptr + umin_val; |
| 2764 | dst_reg->umax_value = umax_ptr + umax_val; |
| 2765 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2766 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 2767 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2768 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2769 | dst_reg->id = ++env->id_gen; |
| 2770 | /* something was added to pkt_ptr, set range to zero */ |
| 2771 | dst_reg->range = 0; |
| 2772 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2773 | break; |
| 2774 | case BPF_SUB: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2775 | if (dst_reg == off_reg) { |
| 2776 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2777 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 2778 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2779 | return -EACCES; |
| 2780 | } |
| 2781 | /* We don't allow subtraction from FP, because (according to |
| 2782 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 2783 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 2784 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2785 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2786 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 2787 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2788 | return -EACCES; |
| 2789 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2790 | if (known && (ptr_reg->off - smin_val == |
| 2791 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2792 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2793 | dst_reg->smin_value = smin_ptr; |
| 2794 | dst_reg->smax_value = smax_ptr; |
| 2795 | dst_reg->umin_value = umin_ptr; |
| 2796 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2797 | dst_reg->var_off = ptr_reg->var_off; |
| 2798 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2799 | dst_reg->off = ptr_reg->off - smin_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2800 | dst_reg->range = ptr_reg->range; |
| 2801 | break; |
| 2802 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2803 | /* A new variable offset is created. If the subtrahend is known |
| 2804 | * nonnegative, then any reg->range we had before is still good. |
| 2805 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2806 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 2807 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 2808 | /* Overflow possible, we know nothing */ |
| 2809 | dst_reg->smin_value = S64_MIN; |
| 2810 | dst_reg->smax_value = S64_MAX; |
| 2811 | } else { |
| 2812 | dst_reg->smin_value = smin_ptr - smax_val; |
| 2813 | dst_reg->smax_value = smax_ptr - smin_val; |
| 2814 | } |
| 2815 | if (umin_ptr < umax_val) { |
| 2816 | /* Overflow possible, we know nothing */ |
| 2817 | dst_reg->umin_value = 0; |
| 2818 | dst_reg->umax_value = U64_MAX; |
| 2819 | } else { |
| 2820 | /* Cannot overflow (as long as bounds are consistent) */ |
| 2821 | dst_reg->umin_value = umin_ptr - umax_val; |
| 2822 | dst_reg->umax_value = umax_ptr - umin_val; |
| 2823 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2824 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 2825 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2826 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2827 | dst_reg->id = ++env->id_gen; |
| 2828 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2829 | if (smin_val < 0) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2830 | dst_reg->range = 0; |
| 2831 | } |
| 2832 | break; |
| 2833 | case BPF_AND: |
| 2834 | case BPF_OR: |
| 2835 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2836 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 2837 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 2838 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2839 | return -EACCES; |
| 2840 | default: |
| 2841 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2842 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 2843 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2844 | return -EACCES; |
| 2845 | } |
| 2846 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2847 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 2848 | return -EINVAL; |
| 2849 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2850 | __update_reg_bounds(dst_reg); |
| 2851 | __reg_deduce_bounds(dst_reg); |
| 2852 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2853 | return 0; |
| 2854 | } |
| 2855 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 2856 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 2857 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 2858 | * need extra checks in the 32-bit case. |
| 2859 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2860 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 2861 | struct bpf_insn *insn, |
| 2862 | struct bpf_reg_state *dst_reg, |
| 2863 | struct bpf_reg_state src_reg) |
| 2864 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2865 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2866 | u8 opcode = BPF_OP(insn->code); |
| 2867 | bool src_known, dst_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2868 | s64 smin_val, smax_val; |
| 2869 | u64 umin_val, umax_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 2870 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2871 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2872 | smin_val = src_reg.smin_value; |
| 2873 | smax_val = src_reg.smax_value; |
| 2874 | umin_val = src_reg.umin_value; |
| 2875 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2876 | src_known = tnum_is_const(src_reg.var_off); |
| 2877 | dst_known = tnum_is_const(dst_reg->var_off); |
| 2878 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 2879 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
| 2880 | smin_val > smax_val || umin_val > umax_val) { |
| 2881 | /* Taint dst register if offset had invalid bounds derived from |
| 2882 | * e.g. dead branches. |
| 2883 | */ |
| 2884 | __mark_reg_unknown(dst_reg); |
| 2885 | return 0; |
| 2886 | } |
| 2887 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2888 | if (!src_known && |
| 2889 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
| 2890 | __mark_reg_unknown(dst_reg); |
| 2891 | return 0; |
| 2892 | } |
| 2893 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2894 | switch (opcode) { |
| 2895 | case BPF_ADD: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2896 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 2897 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 2898 | dst_reg->smin_value = S64_MIN; |
| 2899 | dst_reg->smax_value = S64_MAX; |
| 2900 | } else { |
| 2901 | dst_reg->smin_value += smin_val; |
| 2902 | dst_reg->smax_value += smax_val; |
| 2903 | } |
| 2904 | if (dst_reg->umin_value + umin_val < umin_val || |
| 2905 | dst_reg->umax_value + umax_val < umax_val) { |
| 2906 | dst_reg->umin_value = 0; |
| 2907 | dst_reg->umax_value = U64_MAX; |
| 2908 | } else { |
| 2909 | dst_reg->umin_value += umin_val; |
| 2910 | dst_reg->umax_value += umax_val; |
| 2911 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2912 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 2913 | break; |
| 2914 | case BPF_SUB: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2915 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 2916 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 2917 | /* Overflow possible, we know nothing */ |
| 2918 | dst_reg->smin_value = S64_MIN; |
| 2919 | dst_reg->smax_value = S64_MAX; |
| 2920 | } else { |
| 2921 | dst_reg->smin_value -= smax_val; |
| 2922 | dst_reg->smax_value -= smin_val; |
| 2923 | } |
| 2924 | if (dst_reg->umin_value < umax_val) { |
| 2925 | /* Overflow possible, we know nothing */ |
| 2926 | dst_reg->umin_value = 0; |
| 2927 | dst_reg->umax_value = U64_MAX; |
| 2928 | } else { |
| 2929 | /* Cannot overflow (as long as bounds are consistent) */ |
| 2930 | dst_reg->umin_value -= umax_val; |
| 2931 | dst_reg->umax_value -= umin_val; |
| 2932 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2933 | 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] | 2934 | break; |
| 2935 | case BPF_MUL: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2936 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 2937 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2938 | /* Ain't nobody got time to multiply that sign */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2939 | __mark_reg_unbounded(dst_reg); |
| 2940 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2941 | break; |
| 2942 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2943 | /* Both values are positive, so we can work with unsigned and |
| 2944 | * copy the result to signed (unless it exceeds S64_MAX). |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2945 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2946 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 2947 | /* Potential overflow, we know nothing */ |
| 2948 | __mark_reg_unbounded(dst_reg); |
| 2949 | /* (except what we can learn from the var_off) */ |
| 2950 | __update_reg_bounds(dst_reg); |
| 2951 | break; |
| 2952 | } |
| 2953 | dst_reg->umin_value *= umin_val; |
| 2954 | dst_reg->umax_value *= umax_val; |
| 2955 | if (dst_reg->umax_value > S64_MAX) { |
| 2956 | /* Overflow possible, we know nothing */ |
| 2957 | dst_reg->smin_value = S64_MIN; |
| 2958 | dst_reg->smax_value = S64_MAX; |
| 2959 | } else { |
| 2960 | dst_reg->smin_value = dst_reg->umin_value; |
| 2961 | dst_reg->smax_value = dst_reg->umax_value; |
| 2962 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2963 | break; |
| 2964 | case BPF_AND: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2965 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2966 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 2967 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2968 | break; |
| 2969 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2970 | /* We get our minimum from the var_off, since that's inherently |
| 2971 | * bitwise. Our maximum is the minimum of the operands' maxima. |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 2972 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2973 | 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] | 2974 | dst_reg->umin_value = dst_reg->var_off.value; |
| 2975 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 2976 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 2977 | /* Lose signed bounds when ANDing negative numbers, |
| 2978 | * ain't nobody got time for that. |
| 2979 | */ |
| 2980 | dst_reg->smin_value = S64_MIN; |
| 2981 | dst_reg->smax_value = S64_MAX; |
| 2982 | } else { |
| 2983 | /* ANDing two positives gives a positive, so safe to |
| 2984 | * cast result into s64. |
| 2985 | */ |
| 2986 | dst_reg->smin_value = dst_reg->umin_value; |
| 2987 | dst_reg->smax_value = dst_reg->umax_value; |
| 2988 | } |
| 2989 | /* We may learn something more from the var_off */ |
| 2990 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2991 | break; |
| 2992 | case BPF_OR: |
| 2993 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2994 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
| 2995 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2996 | break; |
| 2997 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2998 | /* We get our maximum from the var_off, and our minimum is the |
| 2999 | * maximum of the operands' minima |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3000 | */ |
| 3001 | 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] | 3002 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 3003 | dst_reg->umax_value = dst_reg->var_off.value | |
| 3004 | dst_reg->var_off.mask; |
| 3005 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 3006 | /* Lose signed bounds when ORing negative numbers, |
| 3007 | * ain't nobody got time for that. |
| 3008 | */ |
| 3009 | dst_reg->smin_value = S64_MIN; |
| 3010 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3011 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3012 | /* ORing two positives gives a positive, so safe to |
| 3013 | * cast result into s64. |
| 3014 | */ |
| 3015 | dst_reg->smin_value = dst_reg->umin_value; |
| 3016 | dst_reg->smax_value = dst_reg->umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3017 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3018 | /* We may learn something more from the var_off */ |
| 3019 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3020 | break; |
| 3021 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3022 | if (umax_val >= insn_bitness) { |
| 3023 | /* Shifts greater than 31 or 63 are undefined. |
| 3024 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3025 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3026 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3027 | break; |
| 3028 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3029 | /* We lose all sign bit information (except what we can pick |
| 3030 | * up from var_off) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3031 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3032 | dst_reg->smin_value = S64_MIN; |
| 3033 | dst_reg->smax_value = S64_MAX; |
| 3034 | /* If we might shift our top bit out, then we know nothing */ |
| 3035 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 3036 | dst_reg->umin_value = 0; |
| 3037 | dst_reg->umax_value = U64_MAX; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3038 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3039 | dst_reg->umin_value <<= umin_val; |
| 3040 | dst_reg->umax_value <<= umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3041 | } |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 3042 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3043 | /* We may learn something more from the var_off */ |
| 3044 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3045 | break; |
| 3046 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3047 | if (umax_val >= insn_bitness) { |
| 3048 | /* Shifts greater than 31 or 63 are undefined. |
| 3049 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3050 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3051 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3052 | break; |
| 3053 | } |
Edward Cree | 4374f25 | 2017-12-18 20:11:53 -0800 | [diff] [blame] | 3054 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 3055 | * be negative, then either: |
| 3056 | * 1) src_reg might be zero, so the sign bit of the result is |
| 3057 | * unknown, so we lose our signed bounds |
| 3058 | * 2) it's known negative, thus the unsigned bounds capture the |
| 3059 | * signed bounds |
| 3060 | * 3) the signed bounds cross zero, so they tell us nothing |
| 3061 | * about the result |
| 3062 | * If the value in dst_reg is known nonnegative, then again the |
| 3063 | * unsigned bounts capture the signed bounds. |
| 3064 | * Thus, in all cases it suffices to blow away our signed bounds |
| 3065 | * and rely on inferring new ones from the unsigned bounds and |
| 3066 | * var_off of the result. |
| 3067 | */ |
| 3068 | dst_reg->smin_value = S64_MIN; |
| 3069 | dst_reg->smax_value = S64_MAX; |
Yonghong Song | afbe1a5 | 2018-04-28 22:28:10 -0700 | [diff] [blame] | 3070 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3071 | dst_reg->umin_value >>= umax_val; |
| 3072 | dst_reg->umax_value >>= umin_val; |
| 3073 | /* We may learn something more from the var_off */ |
| 3074 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3075 | break; |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 3076 | case BPF_ARSH: |
| 3077 | if (umax_val >= insn_bitness) { |
| 3078 | /* Shifts greater than 31 or 63 are undefined. |
| 3079 | * This includes shifts by a negative number. |
| 3080 | */ |
| 3081 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 3082 | break; |
| 3083 | } |
| 3084 | |
| 3085 | /* Upon reaching here, src_known is true and |
| 3086 | * umax_val is equal to umin_val. |
| 3087 | */ |
| 3088 | dst_reg->smin_value >>= umin_val; |
| 3089 | dst_reg->smax_value >>= umin_val; |
| 3090 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val); |
| 3091 | |
| 3092 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 3093 | * dst_reg var_off to refine the result. |
| 3094 | */ |
| 3095 | dst_reg->umin_value = 0; |
| 3096 | dst_reg->umax_value = U64_MAX; |
| 3097 | __update_reg_bounds(dst_reg); |
| 3098 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3099 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3100 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3101 | break; |
| 3102 | } |
| 3103 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 3104 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3105 | /* 32-bit ALU ops are (32,32)->32 */ |
| 3106 | coerce_reg_to_size(dst_reg, 4); |
| 3107 | coerce_reg_to_size(&src_reg, 4); |
| 3108 | } |
| 3109 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3110 | __reg_deduce_bounds(dst_reg); |
| 3111 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3112 | return 0; |
| 3113 | } |
| 3114 | |
| 3115 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 3116 | * and var_off. |
| 3117 | */ |
| 3118 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 3119 | struct bpf_insn *insn) |
| 3120 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3121 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3122 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 3123 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3124 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 3125 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3126 | |
| 3127 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3128 | src_reg = NULL; |
| 3129 | if (dst_reg->type != SCALAR_VALUE) |
| 3130 | ptr_reg = dst_reg; |
| 3131 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3132 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3133 | if (src_reg->type != SCALAR_VALUE) { |
| 3134 | if (dst_reg->type != SCALAR_VALUE) { |
| 3135 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3136 | * an arbitrary scalar. Disallow all math except |
| 3137 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3138 | */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3139 | if (opcode == BPF_SUB){ |
| 3140 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 3141 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3142 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3143 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 3144 | insn->dst_reg, |
| 3145 | bpf_alu_string[opcode >> 4]); |
| 3146 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3147 | } else { |
| 3148 | /* scalar += pointer |
| 3149 | * This is legal, but we have to reverse our |
| 3150 | * src/dest handling in computing the range |
| 3151 | */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3152 | return adjust_ptr_min_max_vals(env, insn, |
| 3153 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3154 | } |
| 3155 | } else if (ptr_reg) { |
| 3156 | /* pointer += scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3157 | return adjust_ptr_min_max_vals(env, insn, |
| 3158 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3159 | } |
| 3160 | } else { |
| 3161 | /* Pretend the src is a reg with a known value, since we only |
| 3162 | * need to be able to read from this state. |
| 3163 | */ |
| 3164 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3165 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3166 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3167 | if (ptr_reg) /* pointer += K */ |
| 3168 | return adjust_ptr_min_max_vals(env, insn, |
| 3169 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | /* Got here implies adding two SCALAR_VALUEs */ |
| 3173 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3174 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3175 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3176 | return -EINVAL; |
| 3177 | } |
| 3178 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3179 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3180 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3181 | return -EINVAL; |
| 3182 | } |
| 3183 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3184 | } |
| 3185 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3186 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3187 | 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] | 3188 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3189 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3190 | u8 opcode = BPF_OP(insn->code); |
| 3191 | int err; |
| 3192 | |
| 3193 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 3194 | if (opcode == BPF_NEG) { |
| 3195 | if (BPF_SRC(insn->code) != 0 || |
| 3196 | insn->src_reg != BPF_REG_0 || |
| 3197 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3198 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3199 | return -EINVAL; |
| 3200 | } |
| 3201 | } else { |
| 3202 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 3203 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 3204 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3205 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3206 | return -EINVAL; |
| 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3211 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3212 | if (err) |
| 3213 | return err; |
| 3214 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3215 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3216 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3217 | insn->dst_reg); |
| 3218 | return -EACCES; |
| 3219 | } |
| 3220 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3221 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3222 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3223 | if (err) |
| 3224 | return err; |
| 3225 | |
| 3226 | } else if (opcode == BPF_MOV) { |
| 3227 | |
| 3228 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3229 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3230 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3231 | return -EINVAL; |
| 3232 | } |
| 3233 | |
| 3234 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3235 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3236 | if (err) |
| 3237 | return err; |
| 3238 | } else { |
| 3239 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3240 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3241 | return -EINVAL; |
| 3242 | } |
| 3243 | } |
| 3244 | |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 3245 | /* check dest operand, mark as required later */ |
| 3246 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3247 | if (err) |
| 3248 | return err; |
| 3249 | |
| 3250 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3251 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 3252 | /* case: R1 = R2 |
| 3253 | * copy register state to dest reg |
| 3254 | */ |
| 3255 | regs[insn->dst_reg] = regs[insn->src_reg]; |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 3256 | regs[insn->dst_reg].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3257 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3258 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3259 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3260 | verbose(env, |
| 3261 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3262 | insn->src_reg); |
| 3263 | return -EACCES; |
| 3264 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3265 | mark_reg_unknown(env, regs, insn->dst_reg); |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3266 | coerce_reg_to_size(®s[insn->dst_reg], 4); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3267 | } |
| 3268 | } else { |
| 3269 | /* case: R = imm |
| 3270 | * remember the value we stored into this reg |
| 3271 | */ |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 3272 | /* clear any state __mark_reg_known doesn't set */ |
| 3273 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3274 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 3275 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 3276 | __mark_reg_known(regs + insn->dst_reg, |
| 3277 | insn->imm); |
| 3278 | } else { |
| 3279 | __mark_reg_known(regs + insn->dst_reg, |
| 3280 | (u32)insn->imm); |
| 3281 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3282 | } |
| 3283 | |
| 3284 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3285 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3286 | return -EINVAL; |
| 3287 | |
| 3288 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 3289 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3290 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3291 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3292 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3293 | return -EINVAL; |
| 3294 | } |
| 3295 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3296 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3297 | if (err) |
| 3298 | return err; |
| 3299 | } else { |
| 3300 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3301 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3302 | return -EINVAL; |
| 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3307 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3308 | if (err) |
| 3309 | return err; |
| 3310 | |
| 3311 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 3312 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3313 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3314 | return -EINVAL; |
| 3315 | } |
| 3316 | |
Daniel Borkmann | 7891a87 | 2018-01-10 20:04:37 +0100 | [diff] [blame] | 3317 | if (opcode == BPF_ARSH && BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3318 | verbose(env, "BPF_ARSH not supported for 32 bit ALU\n"); |
| 3319 | return -EINVAL; |
| 3320 | } |
| 3321 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3322 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 3323 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 3324 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 3325 | |
| 3326 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3327 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3328 | return -EINVAL; |
| 3329 | } |
| 3330 | } |
| 3331 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3332 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3333 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3334 | if (err) |
| 3335 | return err; |
| 3336 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3337 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | return 0; |
| 3341 | } |
| 3342 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3343 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3344 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 3345 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3346 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3347 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3348 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3349 | struct bpf_reg_state *regs = state->regs, *reg; |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3350 | u16 new_range; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3351 | int i, j; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3352 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3353 | if (dst_reg->off < 0 || |
| 3354 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3355 | /* This doesn't give us any range */ |
| 3356 | return; |
| 3357 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3358 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 3359 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3360 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 3361 | * than pkt_end, but that's because it's also less than pkt. |
| 3362 | */ |
| 3363 | return; |
| 3364 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3365 | new_range = dst_reg->off; |
| 3366 | if (range_right_open) |
| 3367 | new_range--; |
| 3368 | |
| 3369 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3370 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3371 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3372 | * |
| 3373 | * r2 = r3; |
| 3374 | * r2 += 8; |
| 3375 | * if (r2 > pkt_end) goto <handle exception> |
| 3376 | * <access okay> |
| 3377 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3378 | * r2 = r3; |
| 3379 | * r2 += 8; |
| 3380 | * if (r2 < pkt_end) goto <access okay> |
| 3381 | * <handle exception> |
| 3382 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3383 | * Where: |
| 3384 | * r2 == dst_reg, pkt_end == src_reg |
| 3385 | * r2=pkt(id=n,off=8,r=0) |
| 3386 | * r3=pkt(id=n,off=0,r=0) |
| 3387 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3388 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3389 | * |
| 3390 | * r2 = r3; |
| 3391 | * r2 += 8; |
| 3392 | * if (pkt_end >= r2) goto <access okay> |
| 3393 | * <handle exception> |
| 3394 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3395 | * r2 = r3; |
| 3396 | * r2 += 8; |
| 3397 | * if (pkt_end <= r2) goto <handle exception> |
| 3398 | * <access okay> |
| 3399 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3400 | * Where: |
| 3401 | * pkt_end == dst_reg, r2 == src_reg |
| 3402 | * r2=pkt(id=n,off=8,r=0) |
| 3403 | * r3=pkt(id=n,off=0,r=0) |
| 3404 | * |
| 3405 | * 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] | 3406 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 3407 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 3408 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3409 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3410 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3411 | /* If our ids match, then we must have the same max_value. And we |
| 3412 | * don't care about the other reg's fixed offset, since if it's too big |
| 3413 | * the range won't allow anything. |
| 3414 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 3415 | */ |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3416 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3417 | if (regs[i].type == type && regs[i].id == dst_reg->id) |
Alexei Starovoitov | b197768 | 2017-03-24 15:57:33 -0700 | [diff] [blame] | 3418 | /* keep the maximum range already checked */ |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3419 | regs[i].range = max(regs[i].range, new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3420 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3421 | for (j = 0; j <= vstate->curframe; j++) { |
| 3422 | state = vstate->frame[j]; |
| 3423 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 3424 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 3425 | continue; |
| 3426 | reg = &state->stack[i].spilled_ptr; |
| 3427 | if (reg->type == type && reg->id == dst_reg->id) |
| 3428 | reg->range = max(reg->range, new_range); |
| 3429 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3430 | } |
| 3431 | } |
| 3432 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3433 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 3434 | * variable register that we are working on, and src_reg is a constant or we're |
| 3435 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3436 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3437 | */ |
| 3438 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 3439 | struct bpf_reg_state *false_reg, u64 val, |
| 3440 | u8 opcode) |
| 3441 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3442 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 3443 | * variable offset from the compare (unless src_reg were a pointer into |
| 3444 | * the same object, but we don't bother with that. |
| 3445 | * Since false_reg and true_reg have the same type by construction, we |
| 3446 | * only need to check one of them for pointerness. |
| 3447 | */ |
| 3448 | if (__is_pointer_value(false, false_reg)) |
| 3449 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3450 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3451 | switch (opcode) { |
| 3452 | case BPF_JEQ: |
| 3453 | /* If this is false then we know nothing Jon Snow, but if it is |
| 3454 | * true then we know for sure. |
| 3455 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3456 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3457 | break; |
| 3458 | case BPF_JNE: |
| 3459 | /* If this is true we know nothing Jon Snow, but if it is false |
| 3460 | * we know the value for sure; |
| 3461 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3462 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3463 | break; |
| 3464 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3465 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 3466 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 3467 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3468 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3469 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 3470 | 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] | 3471 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3472 | case BPF_JLT: |
| 3473 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 3474 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 3475 | break; |
| 3476 | case BPF_JSLT: |
| 3477 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
| 3478 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 3479 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3480 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3481 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 3482 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 3483 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3484 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3485 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 3486 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3487 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3488 | case BPF_JLE: |
| 3489 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 3490 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 3491 | break; |
| 3492 | case BPF_JSLE: |
| 3493 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); |
| 3494 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 3495 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3496 | default: |
| 3497 | break; |
| 3498 | } |
| 3499 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3500 | __reg_deduce_bounds(false_reg); |
| 3501 | __reg_deduce_bounds(true_reg); |
| 3502 | /* We might have learned some bits from the bounds. */ |
| 3503 | __reg_bound_offset(false_reg); |
| 3504 | __reg_bound_offset(true_reg); |
| 3505 | /* Intersecting with the old var_off might have improved our bounds |
| 3506 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3507 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3508 | */ |
| 3509 | __update_reg_bounds(false_reg); |
| 3510 | __update_reg_bounds(true_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3511 | } |
| 3512 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3513 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 3514 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3515 | */ |
| 3516 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 3517 | struct bpf_reg_state *false_reg, u64 val, |
| 3518 | u8 opcode) |
| 3519 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3520 | if (__is_pointer_value(false, false_reg)) |
| 3521 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3522 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3523 | switch (opcode) { |
| 3524 | case BPF_JEQ: |
| 3525 | /* If this is false then we know nothing Jon Snow, but if it is |
| 3526 | * true then we know for sure. |
| 3527 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3528 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3529 | break; |
| 3530 | case BPF_JNE: |
| 3531 | /* If this is true we know nothing Jon Snow, but if it is false |
| 3532 | * we know the value for sure; |
| 3533 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3534 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3535 | break; |
| 3536 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3537 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 3538 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 3539 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3540 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3541 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 3542 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3543 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3544 | case BPF_JLT: |
| 3545 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 3546 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 3547 | break; |
| 3548 | case BPF_JSLT: |
| 3549 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); |
| 3550 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 3551 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3552 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3553 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 3554 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 3555 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3556 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3557 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 3558 | 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] | 3559 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3560 | case BPF_JLE: |
| 3561 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 3562 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 3563 | break; |
| 3564 | case BPF_JSLE: |
| 3565 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
| 3566 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 3567 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3568 | default: |
| 3569 | break; |
| 3570 | } |
| 3571 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3572 | __reg_deduce_bounds(false_reg); |
| 3573 | __reg_deduce_bounds(true_reg); |
| 3574 | /* We might have learned some bits from the bounds. */ |
| 3575 | __reg_bound_offset(false_reg); |
| 3576 | __reg_bound_offset(true_reg); |
| 3577 | /* Intersecting with the old var_off might have improved our bounds |
| 3578 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3579 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3580 | */ |
| 3581 | __update_reg_bounds(false_reg); |
| 3582 | __update_reg_bounds(true_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 3586 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 3587 | struct bpf_reg_state *dst_reg) |
| 3588 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3589 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 3590 | dst_reg->umin_value); |
| 3591 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 3592 | dst_reg->umax_value); |
| 3593 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 3594 | dst_reg->smin_value); |
| 3595 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 3596 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3597 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 3598 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3599 | /* We might have learned new bounds from the var_off. */ |
| 3600 | __update_reg_bounds(src_reg); |
| 3601 | __update_reg_bounds(dst_reg); |
| 3602 | /* We might have learned something about the sign bit. */ |
| 3603 | __reg_deduce_bounds(src_reg); |
| 3604 | __reg_deduce_bounds(dst_reg); |
| 3605 | /* We might have learned some bits from the bounds. */ |
| 3606 | __reg_bound_offset(src_reg); |
| 3607 | __reg_bound_offset(dst_reg); |
| 3608 | /* Intersecting with the old var_off might have improved our bounds |
| 3609 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3610 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3611 | */ |
| 3612 | __update_reg_bounds(src_reg); |
| 3613 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 3617 | struct bpf_reg_state *true_dst, |
| 3618 | struct bpf_reg_state *false_src, |
| 3619 | struct bpf_reg_state *false_dst, |
| 3620 | u8 opcode) |
| 3621 | { |
| 3622 | switch (opcode) { |
| 3623 | case BPF_JEQ: |
| 3624 | __reg_combine_min_max(true_src, true_dst); |
| 3625 | break; |
| 3626 | case BPF_JNE: |
| 3627 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3628 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3629 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3630 | } |
| 3631 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3632 | static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3633 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3634 | { |
| 3635 | struct bpf_reg_state *reg = ®s[regno]; |
| 3636 | |
| 3637 | if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3638 | /* Old offset (both fixed and variable parts) should |
| 3639 | * have been known-zero, because we don't allow pointer |
| 3640 | * arithmetic on pointers that might be NULL. |
| 3641 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3642 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 3643 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3644 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3645 | __mark_reg_known_zero(reg); |
| 3646 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3647 | } |
| 3648 | if (is_null) { |
| 3649 | reg->type = SCALAR_VALUE; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3650 | } else if (reg->map_ptr->inner_map_meta) { |
| 3651 | reg->type = CONST_PTR_TO_MAP; |
| 3652 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
| 3653 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3654 | reg->type = PTR_TO_MAP_VALUE; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3655 | } |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 3656 | /* We don't need id from this point onwards anymore, thus we |
| 3657 | * should better reset it, so that state pruning has chances |
| 3658 | * to take effect. |
| 3659 | */ |
| 3660 | reg->id = 0; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3661 | } |
| 3662 | } |
| 3663 | |
| 3664 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 3665 | * be folded together at some point. |
| 3666 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3667 | static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3668 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3669 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3670 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3671 | struct bpf_reg_state *regs = state->regs; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 3672 | u32 id = regs[regno].id; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3673 | int i, j; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3674 | |
| 3675 | for (i = 0; i < MAX_BPF_REG; i++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3676 | mark_map_reg(regs, i, id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3677 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3678 | for (j = 0; j <= vstate->curframe; j++) { |
| 3679 | state = vstate->frame[j]; |
| 3680 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 3681 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 3682 | continue; |
| 3683 | mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null); |
| 3684 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3685 | } |
| 3686 | } |
| 3687 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 3688 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 3689 | struct bpf_reg_state *dst_reg, |
| 3690 | struct bpf_reg_state *src_reg, |
| 3691 | struct bpf_verifier_state *this_branch, |
| 3692 | struct bpf_verifier_state *other_branch) |
| 3693 | { |
| 3694 | if (BPF_SRC(insn->code) != BPF_X) |
| 3695 | return false; |
| 3696 | |
| 3697 | switch (BPF_OP(insn->code)) { |
| 3698 | case BPF_JGT: |
| 3699 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3700 | src_reg->type == PTR_TO_PACKET_END) || |
| 3701 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3702 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3703 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 3704 | find_good_pkt_pointers(this_branch, dst_reg, |
| 3705 | dst_reg->type, false); |
| 3706 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3707 | src_reg->type == PTR_TO_PACKET) || |
| 3708 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3709 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3710 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 3711 | find_good_pkt_pointers(other_branch, src_reg, |
| 3712 | src_reg->type, true); |
| 3713 | } else { |
| 3714 | return false; |
| 3715 | } |
| 3716 | break; |
| 3717 | case BPF_JLT: |
| 3718 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3719 | src_reg->type == PTR_TO_PACKET_END) || |
| 3720 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3721 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3722 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 3723 | find_good_pkt_pointers(other_branch, dst_reg, |
| 3724 | dst_reg->type, true); |
| 3725 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3726 | src_reg->type == PTR_TO_PACKET) || |
| 3727 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3728 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3729 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 3730 | find_good_pkt_pointers(this_branch, src_reg, |
| 3731 | src_reg->type, false); |
| 3732 | } else { |
| 3733 | return false; |
| 3734 | } |
| 3735 | break; |
| 3736 | case BPF_JGE: |
| 3737 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3738 | src_reg->type == PTR_TO_PACKET_END) || |
| 3739 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3740 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3741 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 3742 | find_good_pkt_pointers(this_branch, dst_reg, |
| 3743 | dst_reg->type, true); |
| 3744 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3745 | src_reg->type == PTR_TO_PACKET) || |
| 3746 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3747 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3748 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 3749 | find_good_pkt_pointers(other_branch, src_reg, |
| 3750 | src_reg->type, false); |
| 3751 | } else { |
| 3752 | return false; |
| 3753 | } |
| 3754 | break; |
| 3755 | case BPF_JLE: |
| 3756 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3757 | src_reg->type == PTR_TO_PACKET_END) || |
| 3758 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3759 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3760 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 3761 | find_good_pkt_pointers(other_branch, dst_reg, |
| 3762 | dst_reg->type, false); |
| 3763 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3764 | src_reg->type == PTR_TO_PACKET) || |
| 3765 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3766 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3767 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 3768 | find_good_pkt_pointers(this_branch, src_reg, |
| 3769 | src_reg->type, true); |
| 3770 | } else { |
| 3771 | return false; |
| 3772 | } |
| 3773 | break; |
| 3774 | default: |
| 3775 | return false; |
| 3776 | } |
| 3777 | |
| 3778 | return true; |
| 3779 | } |
| 3780 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3781 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3782 | struct bpf_insn *insn, int *insn_idx) |
| 3783 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3784 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 3785 | struct bpf_verifier_state *other_branch; |
| 3786 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
| 3787 | struct bpf_reg_state *dst_reg, *other_branch_regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3788 | u8 opcode = BPF_OP(insn->code); |
| 3789 | int err; |
| 3790 | |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3791 | if (opcode > BPF_JSLE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3792 | verbose(env, "invalid BPF_JMP opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3793 | return -EINVAL; |
| 3794 | } |
| 3795 | |
| 3796 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3797 | if (insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3798 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3799 | return -EINVAL; |
| 3800 | } |
| 3801 | |
| 3802 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3803 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3804 | if (err) |
| 3805 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3806 | |
| 3807 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3808 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3809 | insn->src_reg); |
| 3810 | return -EACCES; |
| 3811 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3812 | } else { |
| 3813 | if (insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3814 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3815 | return -EINVAL; |
| 3816 | } |
| 3817 | } |
| 3818 | |
| 3819 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3820 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3821 | if (err) |
| 3822 | return err; |
| 3823 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3824 | dst_reg = ®s[insn->dst_reg]; |
| 3825 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3826 | /* detect if R == 0 where R was initialized to zero earlier */ |
| 3827 | if (BPF_SRC(insn->code) == BPF_K && |
| 3828 | (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3829 | dst_reg->type == SCALAR_VALUE && |
Alexei Starovoitov | 3bf1592 | 2017-11-30 21:31:39 -0800 | [diff] [blame] | 3830 | tnum_is_const(dst_reg->var_off)) { |
| 3831 | if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) || |
| 3832 | (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3833 | /* if (imm == imm) goto pc+off; |
| 3834 | * only follow the goto, ignore fall-through |
| 3835 | */ |
| 3836 | *insn_idx += insn->off; |
| 3837 | return 0; |
| 3838 | } else { |
| 3839 | /* if (imm != imm) goto pc+off; |
| 3840 | * only follow fall-through branch, since |
| 3841 | * that's where the program will go |
| 3842 | */ |
| 3843 | return 0; |
| 3844 | } |
| 3845 | } |
| 3846 | |
| 3847 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); |
| 3848 | if (!other_branch) |
| 3849 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3850 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3851 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3852 | /* detect if we are comparing against a constant value so we can adjust |
| 3853 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3854 | * this is only legit if both are scalars (or pointers to the same |
| 3855 | * object, I suppose, but we don't support that right now), because |
| 3856 | * otherwise the different base pointers mean the offsets aren't |
| 3857 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3858 | */ |
| 3859 | if (BPF_SRC(insn->code) == BPF_X) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3860 | if (dst_reg->type == SCALAR_VALUE && |
| 3861 | regs[insn->src_reg].type == SCALAR_VALUE) { |
| 3862 | if (tnum_is_const(regs[insn->src_reg].var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3863 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3864 | dst_reg, regs[insn->src_reg].var_off.value, |
| 3865 | opcode); |
| 3866 | else if (tnum_is_const(dst_reg->var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3867 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3868 | ®s[insn->src_reg], |
| 3869 | dst_reg->var_off.value, opcode); |
| 3870 | else if (opcode == BPF_JEQ || opcode == BPF_JNE) |
| 3871 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3872 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 3873 | &other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3874 | ®s[insn->src_reg], |
| 3875 | ®s[insn->dst_reg], opcode); |
| 3876 | } |
| 3877 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3878 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3879 | dst_reg, insn->imm, opcode); |
| 3880 | } |
| 3881 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3882 | /* 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] | 3883 | if (BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3884 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
| 3885 | dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3886 | /* Mark all identical map registers in each branch as either |
| 3887 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 3888 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3889 | mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE); |
| 3890 | mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 3891 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 3892 | this_branch, other_branch) && |
| 3893 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3894 | verbose(env, "R%d pointer comparison prohibited\n", |
| 3895 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3896 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3897 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3898 | if (env->log.level) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3899 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3900 | return 0; |
| 3901 | } |
| 3902 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3903 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
| 3904 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) |
| 3905 | { |
| 3906 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; |
| 3907 | |
| 3908 | return (struct bpf_map *) (unsigned long) imm64; |
| 3909 | } |
| 3910 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3911 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3912 | 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] | 3913 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3914 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3915 | int err; |
| 3916 | |
| 3917 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3918 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3919 | return -EINVAL; |
| 3920 | } |
| 3921 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3922 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3923 | return -EINVAL; |
| 3924 | } |
| 3925 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3926 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3927 | if (err) |
| 3928 | return err; |
| 3929 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3930 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3931 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 3932 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3933 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3934 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3935 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3936 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3937 | |
| 3938 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ |
| 3939 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); |
| 3940 | |
| 3941 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 3942 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); |
| 3943 | return 0; |
| 3944 | } |
| 3945 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3946 | static bool may_access_skb(enum bpf_prog_type type) |
| 3947 | { |
| 3948 | switch (type) { |
| 3949 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 3950 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 3951 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3952 | return true; |
| 3953 | default: |
| 3954 | return false; |
| 3955 | } |
| 3956 | } |
| 3957 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3958 | /* verify safety of LD_ABS|LD_IND instructions: |
| 3959 | * - they can only appear in the programs where ctx == skb |
| 3960 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 3961 | * preserve R6-R9, and store return value into R0 |
| 3962 | * |
| 3963 | * Implicit input: |
| 3964 | * ctx == skb == R6 == CTX |
| 3965 | * |
| 3966 | * Explicit input: |
| 3967 | * SRC == any register |
| 3968 | * IMM == 32-bit immediate |
| 3969 | * |
| 3970 | * Output: |
| 3971 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 3972 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3973 | 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] | 3974 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3975 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3976 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3977 | int i, err; |
| 3978 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 3979 | if (!may_access_skb(env->prog->type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3980 | 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] | 3981 | return -EINVAL; |
| 3982 | } |
| 3983 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 3984 | if (!env->ops->gen_ld_abs) { |
| 3985 | verbose(env, "bpf verifier is misconfigured\n"); |
| 3986 | return -EINVAL; |
| 3987 | } |
| 3988 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 3989 | if (env->subprog_cnt > 1) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3990 | /* when program has LD_ABS insn JITs and interpreter assume |
| 3991 | * that r1 == ctx == skb which is not the case for callees |
| 3992 | * that can have arbitrary arguments. It's problematic |
| 3993 | * for main prog as well since JITs would need to analyze |
| 3994 | * all functions in order to make proper register save/restore |
| 3995 | * decisions in the main prog. Hence disallow LD_ABS with calls |
| 3996 | */ |
| 3997 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); |
| 3998 | return -EINVAL; |
| 3999 | } |
| 4000 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4001 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 4002 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4003 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4004 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4005 | return -EINVAL; |
| 4006 | } |
| 4007 | |
| 4008 | /* check whether implicit source operand (register R6) is readable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4009 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4010 | if (err) |
| 4011 | return err; |
| 4012 | |
| 4013 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4014 | verbose(env, |
| 4015 | "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] | 4016 | return -EINVAL; |
| 4017 | } |
| 4018 | |
| 4019 | if (mode == BPF_IND) { |
| 4020 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4021 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4022 | if (err) |
| 4023 | return err; |
| 4024 | } |
| 4025 | |
| 4026 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4027 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4028 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4029 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 4030 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4031 | |
| 4032 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4033 | * the value fetched from the packet. |
| 4034 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4035 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4036 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4037 | return 0; |
| 4038 | } |
| 4039 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4040 | static int check_return_code(struct bpf_verifier_env *env) |
| 4041 | { |
| 4042 | struct bpf_reg_state *reg; |
| 4043 | struct tnum range = tnum_range(0, 1); |
| 4044 | |
| 4045 | switch (env->prog->type) { |
| 4046 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 4047 | case BPF_PROG_TYPE_CGROUP_SOCK: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 4048 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4049 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 4050 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4051 | break; |
| 4052 | default: |
| 4053 | return 0; |
| 4054 | } |
| 4055 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4056 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4057 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4058 | 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] | 4059 | reg_type_str[reg->type]); |
| 4060 | return -EINVAL; |
| 4061 | } |
| 4062 | |
| 4063 | if (!tnum_in(range, reg->var_off)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4064 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4065 | if (!tnum_is_unknown(reg->var_off)) { |
| 4066 | char tn_buf[48]; |
| 4067 | |
| 4068 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4069 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4070 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4071 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4072 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4073 | verbose(env, " should have been 0 or 1\n"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4074 | return -EINVAL; |
| 4075 | } |
| 4076 | return 0; |
| 4077 | } |
| 4078 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4079 | /* non-recursive DFS pseudo code |
| 4080 | * 1 procedure DFS-iterative(G,v): |
| 4081 | * 2 label v as discovered |
| 4082 | * 3 let S be a stack |
| 4083 | * 4 S.push(v) |
| 4084 | * 5 while S is not empty |
| 4085 | * 6 t <- S.pop() |
| 4086 | * 7 if t is what we're looking for: |
| 4087 | * 8 return t |
| 4088 | * 9 for all edges e in G.adjacentEdges(t) do |
| 4089 | * 10 if edge e is already labelled |
| 4090 | * 11 continue with the next edge |
| 4091 | * 12 w <- G.adjacentVertex(t,e) |
| 4092 | * 13 if vertex w is not discovered and not explored |
| 4093 | * 14 label e as tree-edge |
| 4094 | * 15 label w as discovered |
| 4095 | * 16 S.push(w) |
| 4096 | * 17 continue at 5 |
| 4097 | * 18 else if vertex w is discovered |
| 4098 | * 19 label e as back-edge |
| 4099 | * 20 else |
| 4100 | * 21 // vertex w is explored |
| 4101 | * 22 label e as forward- or cross-edge |
| 4102 | * 23 label t as explored |
| 4103 | * 24 S.pop() |
| 4104 | * |
| 4105 | * convention: |
| 4106 | * 0x10 - discovered |
| 4107 | * 0x11 - discovered and fall-through edge labelled |
| 4108 | * 0x12 - discovered and fall-through and branch edges labelled |
| 4109 | * 0x20 - explored |
| 4110 | */ |
| 4111 | |
| 4112 | enum { |
| 4113 | DISCOVERED = 0x10, |
| 4114 | EXPLORED = 0x20, |
| 4115 | FALLTHROUGH = 1, |
| 4116 | BRANCH = 2, |
| 4117 | }; |
| 4118 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4119 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4120 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4121 | static int *insn_stack; /* stack of insns to process */ |
| 4122 | static int cur_stack; /* current stack index */ |
| 4123 | static int *insn_state; |
| 4124 | |
| 4125 | /* t, w, e - match pseudo-code above: |
| 4126 | * t - index of current instruction |
| 4127 | * w - next instruction |
| 4128 | * e - edge |
| 4129 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4130 | 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] | 4131 | { |
| 4132 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 4133 | return 0; |
| 4134 | |
| 4135 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 4136 | return 0; |
| 4137 | |
| 4138 | if (w < 0 || w >= env->prog->len) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4139 | 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] | 4140 | return -EINVAL; |
| 4141 | } |
| 4142 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4143 | if (e == BRANCH) |
| 4144 | /* mark branch target for state pruning */ |
| 4145 | env->explored_states[w] = STATE_LIST_MARK; |
| 4146 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4147 | if (insn_state[w] == 0) { |
| 4148 | /* tree-edge */ |
| 4149 | insn_state[t] = DISCOVERED | e; |
| 4150 | insn_state[w] = DISCOVERED; |
| 4151 | if (cur_stack >= env->prog->len) |
| 4152 | return -E2BIG; |
| 4153 | insn_stack[cur_stack++] = w; |
| 4154 | return 1; |
| 4155 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4156 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4157 | return -EINVAL; |
| 4158 | } else if (insn_state[w] == EXPLORED) { |
| 4159 | /* forward- or cross-edge */ |
| 4160 | insn_state[t] = DISCOVERED | e; |
| 4161 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4162 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4163 | return -EFAULT; |
| 4164 | } |
| 4165 | return 0; |
| 4166 | } |
| 4167 | |
| 4168 | /* non-recursive depth-first-search to detect loops in BPF program |
| 4169 | * loop == back-edge in directed graph |
| 4170 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4171 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4172 | { |
| 4173 | struct bpf_insn *insns = env->prog->insnsi; |
| 4174 | int insn_cnt = env->prog->len; |
| 4175 | int ret = 0; |
| 4176 | int i, t; |
| 4177 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 4178 | ret = check_subprogs(env); |
| 4179 | if (ret < 0) |
| 4180 | return ret; |
| 4181 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4182 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 4183 | if (!insn_state) |
| 4184 | return -ENOMEM; |
| 4185 | |
| 4186 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 4187 | if (!insn_stack) { |
| 4188 | kfree(insn_state); |
| 4189 | return -ENOMEM; |
| 4190 | } |
| 4191 | |
| 4192 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 4193 | insn_stack[0] = 0; /* 0 is the first instruction */ |
| 4194 | cur_stack = 1; |
| 4195 | |
| 4196 | peek_stack: |
| 4197 | if (cur_stack == 0) |
| 4198 | goto check_state; |
| 4199 | t = insn_stack[cur_stack - 1]; |
| 4200 | |
| 4201 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { |
| 4202 | u8 opcode = BPF_OP(insns[t].code); |
| 4203 | |
| 4204 | if (opcode == BPF_EXIT) { |
| 4205 | goto mark_explored; |
| 4206 | } else if (opcode == BPF_CALL) { |
| 4207 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4208 | if (ret == 1) |
| 4209 | goto peek_stack; |
| 4210 | else if (ret < 0) |
| 4211 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 4212 | if (t + 1 < insn_cnt) |
| 4213 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 4214 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
| 4215 | env->explored_states[t] = STATE_LIST_MARK; |
| 4216 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); |
| 4217 | if (ret == 1) |
| 4218 | goto peek_stack; |
| 4219 | else if (ret < 0) |
| 4220 | goto err_free; |
| 4221 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4222 | } else if (opcode == BPF_JA) { |
| 4223 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 4224 | ret = -EINVAL; |
| 4225 | goto err_free; |
| 4226 | } |
| 4227 | /* unconditional jump with single edge */ |
| 4228 | ret = push_insn(t, t + insns[t].off + 1, |
| 4229 | FALLTHROUGH, env); |
| 4230 | if (ret == 1) |
| 4231 | goto peek_stack; |
| 4232 | else if (ret < 0) |
| 4233 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4234 | /* tell verifier to check for equivalent states |
| 4235 | * after every call and jump |
| 4236 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 4237 | if (t + 1 < insn_cnt) |
| 4238 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4239 | } else { |
| 4240 | /* conditional jump with two edges */ |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 4241 | env->explored_states[t] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4242 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4243 | if (ret == 1) |
| 4244 | goto peek_stack; |
| 4245 | else if (ret < 0) |
| 4246 | goto err_free; |
| 4247 | |
| 4248 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 4249 | if (ret == 1) |
| 4250 | goto peek_stack; |
| 4251 | else if (ret < 0) |
| 4252 | goto err_free; |
| 4253 | } |
| 4254 | } else { |
| 4255 | /* all other non-branch instructions with single |
| 4256 | * fall-through edge |
| 4257 | */ |
| 4258 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4259 | if (ret == 1) |
| 4260 | goto peek_stack; |
| 4261 | else if (ret < 0) |
| 4262 | goto err_free; |
| 4263 | } |
| 4264 | |
| 4265 | mark_explored: |
| 4266 | insn_state[t] = EXPLORED; |
| 4267 | if (cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4268 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4269 | ret = -EFAULT; |
| 4270 | goto err_free; |
| 4271 | } |
| 4272 | goto peek_stack; |
| 4273 | |
| 4274 | check_state: |
| 4275 | for (i = 0; i < insn_cnt; i++) { |
| 4276 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4277 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4278 | ret = -EINVAL; |
| 4279 | goto err_free; |
| 4280 | } |
| 4281 | } |
| 4282 | ret = 0; /* cfg looks good */ |
| 4283 | |
| 4284 | err_free: |
| 4285 | kfree(insn_state); |
| 4286 | kfree(insn_stack); |
| 4287 | return ret; |
| 4288 | } |
| 4289 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4290 | /* check %cur's range satisfies %old's */ |
| 4291 | static bool range_within(struct bpf_reg_state *old, |
| 4292 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4293 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4294 | return old->umin_value <= cur->umin_value && |
| 4295 | old->umax_value >= cur->umax_value && |
| 4296 | old->smin_value <= cur->smin_value && |
| 4297 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4298 | } |
| 4299 | |
| 4300 | /* Maximum number of register states that can exist at once */ |
| 4301 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 4302 | struct idpair { |
| 4303 | u32 old; |
| 4304 | u32 cur; |
| 4305 | }; |
| 4306 | |
| 4307 | /* If in the old state two registers had the same id, then they need to have |
| 4308 | * the same id in the new state as well. But that id could be different from |
| 4309 | * the old state, so we need to track the mapping from old to new ids. |
| 4310 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 4311 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 4312 | * regs with a different old id could still have new id 9, we don't care about |
| 4313 | * that. |
| 4314 | * So we look through our idmap to see if this old id has been seen before. If |
| 4315 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 4316 | */ |
| 4317 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 4318 | { |
| 4319 | unsigned int i; |
| 4320 | |
| 4321 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 4322 | if (!idmap[i].old) { |
| 4323 | /* Reached an empty slot; haven't seen this id before */ |
| 4324 | idmap[i].old = old_id; |
| 4325 | idmap[i].cur = cur_id; |
| 4326 | return true; |
| 4327 | } |
| 4328 | if (idmap[i].old == old_id) |
| 4329 | return idmap[i].cur == cur_id; |
| 4330 | } |
| 4331 | /* We ran out of idmap slots, which should be impossible */ |
| 4332 | WARN_ON_ONCE(1); |
| 4333 | return false; |
| 4334 | } |
| 4335 | |
| 4336 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4337 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 4338 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4339 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4340 | bool equal; |
| 4341 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4342 | if (!(rold->live & REG_LIVE_READ)) |
| 4343 | /* explored state didn't use this */ |
| 4344 | return true; |
| 4345 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4346 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, frameno)) == 0; |
| 4347 | |
| 4348 | if (rold->type == PTR_TO_STACK) |
| 4349 | /* two stack pointers are equal only if they're pointing to |
| 4350 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 4351 | */ |
| 4352 | return equal && rold->frameno == rcur->frameno; |
| 4353 | |
| 4354 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4355 | return true; |
| 4356 | |
| 4357 | if (rold->type == NOT_INIT) |
| 4358 | /* explored state can't have used this */ |
| 4359 | return true; |
| 4360 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4361 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4362 | switch (rold->type) { |
| 4363 | case SCALAR_VALUE: |
| 4364 | if (rcur->type == SCALAR_VALUE) { |
| 4365 | /* new val must satisfy old val knowledge */ |
| 4366 | return range_within(rold, rcur) && |
| 4367 | tnum_in(rold->var_off, rcur->var_off); |
| 4368 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 4369 | /* We're trying to use a pointer in place of a scalar. |
| 4370 | * Even if the scalar was unbounded, this could lead to |
| 4371 | * pointer leaks because scalars are allowed to leak |
| 4372 | * while pointers are not. We could make this safe in |
| 4373 | * special cases if root is calling us, but it's |
| 4374 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4375 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 4376 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4377 | } |
| 4378 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4379 | /* If the new min/max/var_off satisfy the old ones and |
| 4380 | * everything else matches, we are OK. |
| 4381 | * We don't care about the 'id' value, because nothing |
| 4382 | * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL) |
| 4383 | */ |
| 4384 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 4385 | range_within(rold, rcur) && |
| 4386 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4387 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 4388 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 4389 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 4390 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 4391 | * checked, doing so could have affected others with the same |
| 4392 | * id, and we can't check for that because we lost the id when |
| 4393 | * we converted to a PTR_TO_MAP_VALUE. |
| 4394 | */ |
| 4395 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 4396 | return false; |
| 4397 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 4398 | return false; |
| 4399 | /* Check our ids match any regs they're supposed to */ |
| 4400 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4401 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4402 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4403 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4404 | return false; |
| 4405 | /* We must have at least as much range as the old ptr |
| 4406 | * did, so that any accesses which were safe before are |
| 4407 | * still safe. This is true even if old range < old off, |
| 4408 | * since someone could have accessed through (ptr - k), or |
| 4409 | * even done ptr -= k in a register, to get a safe access. |
| 4410 | */ |
| 4411 | if (rold->range > rcur->range) |
| 4412 | return false; |
| 4413 | /* If the offsets don't match, we can't trust our alignment; |
| 4414 | * nor can we be sure that we won't fall out of range. |
| 4415 | */ |
| 4416 | if (rold->off != rcur->off) |
| 4417 | return false; |
| 4418 | /* id relations must be preserved */ |
| 4419 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 4420 | return false; |
| 4421 | /* new val must satisfy old val knowledge */ |
| 4422 | return range_within(rold, rcur) && |
| 4423 | tnum_in(rold->var_off, rcur->var_off); |
| 4424 | case PTR_TO_CTX: |
| 4425 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4426 | case PTR_TO_PACKET_END: |
| 4427 | /* Only valid matches are exact, which memcmp() above |
| 4428 | * would have accepted |
| 4429 | */ |
| 4430 | default: |
| 4431 | /* Don't know what's going on, just say it's not safe */ |
| 4432 | return false; |
| 4433 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4434 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4435 | /* Shouldn't get here; if we do, say it's not safe */ |
| 4436 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4437 | return false; |
| 4438 | } |
| 4439 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4440 | static bool stacksafe(struct bpf_func_state *old, |
| 4441 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4442 | struct idpair *idmap) |
| 4443 | { |
| 4444 | int i, spi; |
| 4445 | |
| 4446 | /* if explored stack has more populated slots than current stack |
| 4447 | * such stacks are not equivalent |
| 4448 | */ |
| 4449 | if (old->allocated_stack > cur->allocated_stack) |
| 4450 | return false; |
| 4451 | |
| 4452 | /* walk slots of the explored stack and ignore any additional |
| 4453 | * slots in the current stack, since explored(safe) state |
| 4454 | * didn't use them |
| 4455 | */ |
| 4456 | for (i = 0; i < old->allocated_stack; i++) { |
| 4457 | spi = i / BPF_REG_SIZE; |
| 4458 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4459 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) |
| 4460 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 4461 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4462 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4463 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 4464 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4465 | /* if old state was safe with misc data in the stack |
| 4466 | * it will be safe with zero-initialized stack. |
| 4467 | * The opposite is not true |
| 4468 | */ |
| 4469 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 4470 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 4471 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4472 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 4473 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 4474 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 4475 | * this stack slot, but current has has STACK_MISC -> |
| 4476 | * this verifier states are not equivalent, |
| 4477 | * return false to continue verification of this path |
| 4478 | */ |
| 4479 | return false; |
| 4480 | if (i % BPF_REG_SIZE) |
| 4481 | continue; |
| 4482 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 4483 | continue; |
| 4484 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 4485 | &cur->stack[spi].spilled_ptr, |
| 4486 | idmap)) |
| 4487 | /* when explored and current stack slot are both storing |
| 4488 | * spilled registers, check that stored pointers types |
| 4489 | * are the same as well. |
| 4490 | * Ex: explored safe path could have stored |
| 4491 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 4492 | * but current path has stored: |
| 4493 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 4494 | * such verifier states are not equivalent. |
| 4495 | * return false to continue verification of this path |
| 4496 | */ |
| 4497 | return false; |
| 4498 | } |
| 4499 | return true; |
| 4500 | } |
| 4501 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4502 | /* compare two verifier states |
| 4503 | * |
| 4504 | * all states stored in state_list are known to be valid, since |
| 4505 | * verifier reached 'bpf_exit' instruction through them |
| 4506 | * |
| 4507 | * this function is called when verifier exploring different branches of |
| 4508 | * execution popped from the state stack. If it sees an old state that has |
| 4509 | * more strict register state and more strict stack state then this execution |
| 4510 | * branch doesn't need to be explored further, since verifier already |
| 4511 | * concluded that more strict state leads to valid finish. |
| 4512 | * |
| 4513 | * Therefore two states are equivalent if register state is more conservative |
| 4514 | * and explored stack state is more conservative than the current one. |
| 4515 | * Example: |
| 4516 | * explored current |
| 4517 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 4518 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 4519 | * |
| 4520 | * In other words if current stack state (one being explored) has more |
| 4521 | * valid slots than old one that already passed validation, it means |
| 4522 | * the verifier can stop exploring and conclude that current state is valid too |
| 4523 | * |
| 4524 | * Similarly with registers. If explored state has register type as invalid |
| 4525 | * whereas register type in current state is meaningful, it means that |
| 4526 | * the current state will reach 'bpf_exit' instruction safely |
| 4527 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4528 | static bool func_states_equal(struct bpf_func_state *old, |
| 4529 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4530 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4531 | struct idpair *idmap; |
| 4532 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4533 | int i; |
| 4534 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4535 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 4536 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 4537 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4538 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4539 | |
| 4540 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4541 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4542 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4543 | } |
| 4544 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4545 | if (!stacksafe(old, cur, idmap)) |
| 4546 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4547 | ret = true; |
| 4548 | out_free: |
| 4549 | kfree(idmap); |
| 4550 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4551 | } |
| 4552 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4553 | static bool states_equal(struct bpf_verifier_env *env, |
| 4554 | struct bpf_verifier_state *old, |
| 4555 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4556 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4557 | int i; |
| 4558 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4559 | if (old->curframe != cur->curframe) |
| 4560 | return false; |
| 4561 | |
| 4562 | /* for states to be equal callsites have to be the same |
| 4563 | * and all frame states need to be equivalent |
| 4564 | */ |
| 4565 | for (i = 0; i <= old->curframe; i++) { |
| 4566 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 4567 | return false; |
| 4568 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 4569 | return false; |
| 4570 | } |
| 4571 | return true; |
| 4572 | } |
| 4573 | |
| 4574 | /* A write screens off any subsequent reads; but write marks come from the |
| 4575 | * straight-line code between a state and its parent. When we arrive at an |
| 4576 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 4577 | * code, so read marks in the state must propagate to the parent regardless |
| 4578 | * of the state's write marks. That's what 'parent == state->parent' comparison |
| 4579 | * in mark_reg_read() and mark_stack_slot_read() is for. |
| 4580 | */ |
| 4581 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 4582 | const struct bpf_verifier_state *vstate, |
| 4583 | struct bpf_verifier_state *vparent) |
| 4584 | { |
| 4585 | int i, frame, err = 0; |
| 4586 | struct bpf_func_state *state, *parent; |
| 4587 | |
| 4588 | if (vparent->curframe != vstate->curframe) { |
| 4589 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 4590 | vparent->curframe, vstate->curframe); |
| 4591 | return -EFAULT; |
| 4592 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4593 | /* Propagate read liveness of registers... */ |
| 4594 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
| 4595 | /* We don't need to worry about FP liveness because it's read-only */ |
| 4596 | for (i = 0; i < BPF_REG_FP; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4597 | if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4598 | continue; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4599 | if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) { |
| 4600 | err = mark_reg_read(env, vstate, vparent, i); |
| 4601 | if (err) |
| 4602 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4603 | } |
| 4604 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4605 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4606 | /* ... and stack slots */ |
| 4607 | for (frame = 0; frame <= vstate->curframe; frame++) { |
| 4608 | state = vstate->frame[frame]; |
| 4609 | parent = vparent->frame[frame]; |
| 4610 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 4611 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4612 | if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ) |
| 4613 | continue; |
| 4614 | if (state->stack[i].spilled_ptr.live & REG_LIVE_READ) |
| 4615 | mark_stack_slot_read(env, vstate, vparent, i, frame); |
| 4616 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4617 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4618 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4619 | } |
| 4620 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4621 | 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] | 4622 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4623 | struct bpf_verifier_state_list *new_sl; |
| 4624 | struct bpf_verifier_state_list *sl; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4625 | struct bpf_verifier_state *cur = env->cur_state; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4626 | int i, j, err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4627 | |
| 4628 | sl = env->explored_states[insn_idx]; |
| 4629 | if (!sl) |
| 4630 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 4631 | * be doing state search here |
| 4632 | */ |
| 4633 | return 0; |
| 4634 | |
| 4635 | while (sl != STATE_LIST_MARK) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4636 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4637 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4638 | * prune the search. |
| 4639 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 4640 | * If we have any write marks in env->cur_state, they |
| 4641 | * will prevent corresponding reads in the continuation |
| 4642 | * from reaching our parent (an explored_state). Our |
| 4643 | * own state will get the read marks recorded, but |
| 4644 | * they'll be immediately forgotten as we're pruning |
| 4645 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4646 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4647 | err = propagate_liveness(env, &sl->state, cur); |
| 4648 | if (err) |
| 4649 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4650 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4651 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4652 | sl = sl->next; |
| 4653 | } |
| 4654 | |
| 4655 | /* there were no equivalent states, remember current one. |
| 4656 | * technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4657 | * but it will either reach outer most bpf_exit (which means it's safe) |
| 4658 | * or it will be rejected. Since there are no loops, we won't be |
| 4659 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
| 4660 | * again on the way to bpf_exit |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4661 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4662 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4663 | if (!new_sl) |
| 4664 | return -ENOMEM; |
| 4665 | |
| 4666 | /* add new state to the head of linked list */ |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 4667 | err = copy_verifier_state(&new_sl->state, cur); |
| 4668 | if (err) { |
| 4669 | free_verifier_state(&new_sl->state, false); |
| 4670 | kfree(new_sl); |
| 4671 | return err; |
| 4672 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4673 | new_sl->next = env->explored_states[insn_idx]; |
| 4674 | env->explored_states[insn_idx] = new_sl; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4675 | /* connect new state to parentage chain */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4676 | cur->parent = &new_sl->state; |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 4677 | /* clear write marks in current state: the writes we did are not writes |
| 4678 | * our child did, so they don't screen off its reads from us. |
| 4679 | * (There are no read marks in current state, because reads always mark |
| 4680 | * their parent and current state never has children yet. Only |
| 4681 | * explored_states can get read marks.) |
| 4682 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4683 | for (i = 0; i < BPF_REG_FP; i++) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4684 | cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE; |
| 4685 | |
| 4686 | /* all stack frames are accessible from callee, clear them all */ |
| 4687 | for (j = 0; j <= cur->curframe; j++) { |
| 4688 | struct bpf_func_state *frame = cur->frame[j]; |
| 4689 | |
| 4690 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4691 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4692 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4693 | return 0; |
| 4694 | } |
| 4695 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4696 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4697 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4698 | struct bpf_verifier_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4699 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4700 | struct bpf_reg_state *regs; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4701 | int insn_cnt = env->prog->len, i; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4702 | int insn_idx, prev_insn_idx = 0; |
| 4703 | int insn_processed = 0; |
| 4704 | bool do_print_state = false; |
| 4705 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4706 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 4707 | if (!state) |
| 4708 | return -ENOMEM; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4709 | state->curframe = 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4710 | state->parent = NULL; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4711 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 4712 | if (!state->frame[0]) { |
| 4713 | kfree(state); |
| 4714 | return -ENOMEM; |
| 4715 | } |
| 4716 | env->cur_state = state; |
| 4717 | init_func_state(env, state->frame[0], |
| 4718 | BPF_MAIN_FUNC /* callsite */, |
| 4719 | 0 /* frameno */, |
| 4720 | 0 /* subprogno, zero == main subprog */); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4721 | insn_idx = 0; |
| 4722 | for (;;) { |
| 4723 | struct bpf_insn *insn; |
| 4724 | u8 class; |
| 4725 | int err; |
| 4726 | |
| 4727 | if (insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4728 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4729 | insn_idx, insn_cnt); |
| 4730 | return -EFAULT; |
| 4731 | } |
| 4732 | |
| 4733 | insn = &insns[insn_idx]; |
| 4734 | class = BPF_CLASS(insn->code); |
| 4735 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 4736 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4737 | verbose(env, |
| 4738 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4739 | insn_processed); |
| 4740 | return -E2BIG; |
| 4741 | } |
| 4742 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4743 | err = is_state_visited(env, insn_idx); |
| 4744 | if (err < 0) |
| 4745 | return err; |
| 4746 | if (err == 1) { |
| 4747 | /* found equivalent state, can prune the search */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4748 | if (env->log.level) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4749 | if (do_print_state) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4750 | verbose(env, "\nfrom %d to %d: safe\n", |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4751 | prev_insn_idx, insn_idx); |
| 4752 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4753 | verbose(env, "%d: safe\n", insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4754 | } |
| 4755 | goto process_bpf_exit; |
| 4756 | } |
| 4757 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 4758 | if (need_resched()) |
| 4759 | cond_resched(); |
| 4760 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4761 | if (env->log.level > 1 || (env->log.level && do_print_state)) { |
| 4762 | if (env->log.level > 1) |
| 4763 | verbose(env, "%d:", insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 4764 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4765 | verbose(env, "\nfrom %d to %d:", |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 4766 | prev_insn_idx, insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4767 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4768 | do_print_state = false; |
| 4769 | } |
| 4770 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4771 | if (env->log.level) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 4772 | const struct bpf_insn_cbs cbs = { |
| 4773 | .cb_print = verbose, |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 4774 | .private_data = env, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 4775 | }; |
| 4776 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4777 | verbose(env, "%d: ", insn_idx); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 4778 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4779 | } |
| 4780 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 4781 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
| 4782 | err = bpf_prog_offload_verify_insn(env, insn_idx, |
| 4783 | prev_insn_idx); |
| 4784 | if (err) |
| 4785 | return err; |
| 4786 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 4787 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4788 | regs = cur_regs(env); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 4789 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4790 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4791 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4792 | if (err) |
| 4793 | return err; |
| 4794 | |
| 4795 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4796 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4797 | |
| 4798 | /* check for reserved fields is already done */ |
| 4799 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4800 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4801 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4802 | if (err) |
| 4803 | return err; |
| 4804 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4805 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4806 | if (err) |
| 4807 | return err; |
| 4808 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 4809 | src_reg_type = regs[insn->src_reg].type; |
| 4810 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4811 | /* check that memory (src_reg + off) is readable, |
| 4812 | * the state of dst_reg will be updated by this func |
| 4813 | */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4814 | err = check_mem_access(env, insn_idx, insn->src_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4815 | BPF_SIZE(insn->code), BPF_READ, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 4816 | insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4817 | if (err) |
| 4818 | return err; |
| 4819 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4820 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 4821 | |
| 4822 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4823 | /* saw a valid insn |
| 4824 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4825 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4826 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4827 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4828 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4829 | } else if (src_reg_type != *prev_src_type && |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4830 | (src_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4831 | *prev_src_type == PTR_TO_CTX)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4832 | /* ABuser program is trying to use the same insn |
| 4833 | * dst_reg = *(u32*) (src_reg + off) |
| 4834 | * with different pointer types: |
| 4835 | * src_reg == ctx in one branch and |
| 4836 | * src_reg == stack|map in some other branch. |
| 4837 | * Reject it. |
| 4838 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4839 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4840 | return -EINVAL; |
| 4841 | } |
| 4842 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4843 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4844 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4845 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4846 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4847 | err = check_xadd(env, insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4848 | if (err) |
| 4849 | return err; |
| 4850 | insn_idx++; |
| 4851 | continue; |
| 4852 | } |
| 4853 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4854 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4855 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4856 | if (err) |
| 4857 | return err; |
| 4858 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4859 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4860 | if (err) |
| 4861 | return err; |
| 4862 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4863 | dst_reg_type = regs[insn->dst_reg].type; |
| 4864 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4865 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4866 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4867 | BPF_SIZE(insn->code), BPF_WRITE, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 4868 | insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4869 | if (err) |
| 4870 | return err; |
| 4871 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4872 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 4873 | |
| 4874 | if (*prev_dst_type == NOT_INIT) { |
| 4875 | *prev_dst_type = dst_reg_type; |
| 4876 | } else if (dst_reg_type != *prev_dst_type && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4877 | (dst_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4878 | *prev_dst_type == PTR_TO_CTX)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4879 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4880 | return -EINVAL; |
| 4881 | } |
| 4882 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4883 | } else if (class == BPF_ST) { |
| 4884 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 4885 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4886 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4887 | return -EINVAL; |
| 4888 | } |
| 4889 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4890 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4891 | if (err) |
| 4892 | return err; |
| 4893 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 4894 | if (is_ctx_reg(env, insn->dst_reg)) { |
| 4895 | verbose(env, "BPF_ST stores into R%d context is not allowed\n", |
| 4896 | insn->dst_reg); |
| 4897 | return -EACCES; |
| 4898 | } |
| 4899 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4900 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4901 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4902 | BPF_SIZE(insn->code), BPF_WRITE, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 4903 | -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4904 | if (err) |
| 4905 | return err; |
| 4906 | |
| 4907 | } else if (class == BPF_JMP) { |
| 4908 | u8 opcode = BPF_OP(insn->code); |
| 4909 | |
| 4910 | if (opcode == BPF_CALL) { |
| 4911 | if (BPF_SRC(insn->code) != BPF_K || |
| 4912 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4913 | (insn->src_reg != BPF_REG_0 && |
| 4914 | insn->src_reg != BPF_PSEUDO_CALL) || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4915 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4916 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4917 | return -EINVAL; |
| 4918 | } |
| 4919 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4920 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 4921 | err = check_func_call(env, insn, &insn_idx); |
| 4922 | else |
| 4923 | err = check_helper_call(env, insn->imm, insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4924 | if (err) |
| 4925 | return err; |
| 4926 | |
| 4927 | } else if (opcode == BPF_JA) { |
| 4928 | if (BPF_SRC(insn->code) != BPF_K || |
| 4929 | insn->imm != 0 || |
| 4930 | insn->src_reg != BPF_REG_0 || |
| 4931 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4932 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4933 | return -EINVAL; |
| 4934 | } |
| 4935 | |
| 4936 | insn_idx += insn->off + 1; |
| 4937 | continue; |
| 4938 | |
| 4939 | } else if (opcode == BPF_EXIT) { |
| 4940 | if (BPF_SRC(insn->code) != BPF_K || |
| 4941 | insn->imm != 0 || |
| 4942 | insn->src_reg != BPF_REG_0 || |
| 4943 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4944 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4945 | return -EINVAL; |
| 4946 | } |
| 4947 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4948 | if (state->curframe) { |
| 4949 | /* exit from nested function */ |
| 4950 | prev_insn_idx = insn_idx; |
| 4951 | err = prepare_func_exit(env, &insn_idx); |
| 4952 | if (err) |
| 4953 | return err; |
| 4954 | do_print_state = true; |
| 4955 | continue; |
| 4956 | } |
| 4957 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4958 | /* eBPF calling convetion is such that R0 is used |
| 4959 | * to return the value from eBPF program. |
| 4960 | * Make sure that it's readable at this time |
| 4961 | * of bpf_exit, which means that program wrote |
| 4962 | * something into it earlier |
| 4963 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4964 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4965 | if (err) |
| 4966 | return err; |
| 4967 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4968 | if (is_pointer_value(env, BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4969 | verbose(env, "R0 leaks addr as return value\n"); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4970 | return -EACCES; |
| 4971 | } |
| 4972 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4973 | err = check_return_code(env); |
| 4974 | if (err) |
| 4975 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4976 | process_bpf_exit: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4977 | err = pop_stack(env, &prev_insn_idx, &insn_idx); |
| 4978 | if (err < 0) { |
| 4979 | if (err != -ENOENT) |
| 4980 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4981 | break; |
| 4982 | } else { |
| 4983 | do_print_state = true; |
| 4984 | continue; |
| 4985 | } |
| 4986 | } else { |
| 4987 | err = check_cond_jmp_op(env, insn, &insn_idx); |
| 4988 | if (err) |
| 4989 | return err; |
| 4990 | } |
| 4991 | } else if (class == BPF_LD) { |
| 4992 | u8 mode = BPF_MODE(insn->code); |
| 4993 | |
| 4994 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4995 | err = check_ld_abs(env, insn); |
| 4996 | if (err) |
| 4997 | return err; |
| 4998 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4999 | } else if (mode == BPF_IMM) { |
| 5000 | err = check_ld_imm(env, insn); |
| 5001 | if (err) |
| 5002 | return err; |
| 5003 | |
| 5004 | insn_idx++; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5005 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5006 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5007 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5008 | return -EINVAL; |
| 5009 | } |
| 5010 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5011 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5012 | return -EINVAL; |
| 5013 | } |
| 5014 | |
| 5015 | insn_idx++; |
| 5016 | } |
| 5017 | |
Daniel Borkmann | 4bd95f4 | 2018-01-20 01:24:36 +0100 | [diff] [blame] | 5018 | verbose(env, "processed %d insns (limit %d), stack depth ", |
| 5019 | insn_processed, BPF_COMPLEXITY_LIMIT_INSNS); |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5020 | for (i = 0; i < env->subprog_cnt; i++) { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 5021 | u32 depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5022 | |
| 5023 | verbose(env, "%d", depth); |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5024 | if (i + 1 < env->subprog_cnt) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5025 | verbose(env, "+"); |
| 5026 | } |
| 5027 | verbose(env, "\n"); |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 5028 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5029 | return 0; |
| 5030 | } |
| 5031 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5032 | static int check_map_prealloc(struct bpf_map *map) |
| 5033 | { |
| 5034 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 5035 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 5036 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5037 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 5038 | } |
| 5039 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5040 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 5041 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5042 | struct bpf_prog *prog) |
| 5043 | |
| 5044 | { |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5045 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 5046 | * preallocated hash maps, since doing memory allocation |
| 5047 | * in overflow_handler can crash depending on where nmi got |
| 5048 | * triggered. |
| 5049 | */ |
| 5050 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 5051 | if (!check_map_prealloc(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5052 | 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] | 5053 | return -EINVAL; |
| 5054 | } |
| 5055 | if (map->inner_map_meta && |
| 5056 | !check_map_prealloc(map->inner_map_meta)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5057 | 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] | 5058 | return -EINVAL; |
| 5059 | } |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5060 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 5061 | |
| 5062 | 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] | 5063 | !bpf_offload_prog_map_match(prog, map)) { |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 5064 | verbose(env, "offload device mismatch between prog and map\n"); |
| 5065 | return -EINVAL; |
| 5066 | } |
| 5067 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5068 | return 0; |
| 5069 | } |
| 5070 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5071 | /* look for pseudo eBPF instructions that access map FDs and |
| 5072 | * replace them with actual map pointers |
| 5073 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5074 | 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] | 5075 | { |
| 5076 | struct bpf_insn *insn = env->prog->insnsi; |
| 5077 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5078 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5079 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 5080 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 5081 | if (err) |
| 5082 | return err; |
| 5083 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5084 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5085 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5086 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5087 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5088 | return -EINVAL; |
| 5089 | } |
| 5090 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5091 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 5092 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 5093 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5094 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5095 | return -EINVAL; |
| 5096 | } |
| 5097 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5098 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
| 5099 | struct bpf_map *map; |
| 5100 | struct fd f; |
| 5101 | |
| 5102 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 5103 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 5104 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5105 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5106 | return -EINVAL; |
| 5107 | } |
| 5108 | |
| 5109 | if (insn->src_reg == 0) |
| 5110 | /* valid generic load 64-bit imm */ |
| 5111 | goto next_insn; |
| 5112 | |
| 5113 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5114 | verbose(env, |
| 5115 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5116 | return -EINVAL; |
| 5117 | } |
| 5118 | |
| 5119 | f = fdget(insn->imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 5120 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5121 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5122 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5123 | insn->imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5124 | return PTR_ERR(map); |
| 5125 | } |
| 5126 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5127 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 5128 | if (err) { |
| 5129 | fdput(f); |
| 5130 | return err; |
| 5131 | } |
| 5132 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5133 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
| 5134 | insn[0].imm = (u32) (unsigned long) map; |
| 5135 | insn[1].imm = ((u64) (unsigned long) map) >> 32; |
| 5136 | |
| 5137 | /* check whether we recorded this map already */ |
| 5138 | for (j = 0; j < env->used_map_cnt; j++) |
| 5139 | if (env->used_maps[j] == map) { |
| 5140 | fdput(f); |
| 5141 | goto next_insn; |
| 5142 | } |
| 5143 | |
| 5144 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 5145 | fdput(f); |
| 5146 | return -E2BIG; |
| 5147 | } |
| 5148 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5149 | /* hold the map. If the program is rejected by verifier, |
| 5150 | * the map will be released by release_maps() or it |
| 5151 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 5152 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5153 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 5154 | map = bpf_map_inc(map, false); |
| 5155 | if (IS_ERR(map)) { |
| 5156 | fdput(f); |
| 5157 | return PTR_ERR(map); |
| 5158 | } |
| 5159 | env->used_maps[env->used_map_cnt++] = map; |
| 5160 | |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 5161 | if (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE && |
| 5162 | bpf_cgroup_storage_assign(env->prog, map)) { |
| 5163 | verbose(env, |
| 5164 | "only one cgroup storage is allowed\n"); |
| 5165 | fdput(f); |
| 5166 | return -EBUSY; |
| 5167 | } |
| 5168 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5169 | fdput(f); |
| 5170 | next_insn: |
| 5171 | insn++; |
| 5172 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 5173 | continue; |
| 5174 | } |
| 5175 | |
| 5176 | /* Basic sanity check before we invest more work here. */ |
| 5177 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 5178 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 5179 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5180 | } |
| 5181 | } |
| 5182 | |
| 5183 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 5184 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 5185 | * These pointers will be used later by verifier to validate map access. |
| 5186 | */ |
| 5187 | return 0; |
| 5188 | } |
| 5189 | |
| 5190 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5191 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5192 | { |
| 5193 | int i; |
| 5194 | |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 5195 | if (env->prog->aux->cgroup_storage) |
| 5196 | bpf_cgroup_storage_release(env->prog, |
| 5197 | env->prog->aux->cgroup_storage); |
| 5198 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5199 | for (i = 0; i < env->used_map_cnt; i++) |
| 5200 | bpf_map_put(env->used_maps[i]); |
| 5201 | } |
| 5202 | |
| 5203 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5204 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5205 | { |
| 5206 | struct bpf_insn *insn = env->prog->insnsi; |
| 5207 | int insn_cnt = env->prog->len; |
| 5208 | int i; |
| 5209 | |
| 5210 | for (i = 0; i < insn_cnt; i++, insn++) |
| 5211 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 5212 | insn->src_reg = 0; |
| 5213 | } |
| 5214 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5215 | /* single env->prog->insni[off] instruction was replaced with the range |
| 5216 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 5217 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 5218 | */ |
| 5219 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, |
| 5220 | u32 off, u32 cnt) |
| 5221 | { |
| 5222 | 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] | 5223 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5224 | |
| 5225 | if (cnt == 1) |
| 5226 | return 0; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 5227 | new_data = vzalloc(array_size(prog_len, |
| 5228 | sizeof(struct bpf_insn_aux_data))); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5229 | if (!new_data) |
| 5230 | return -ENOMEM; |
| 5231 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 5232 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 5233 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5234 | for (i = off; i < off + cnt - 1; i++) |
| 5235 | new_data[i].seen = true; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5236 | env->insn_aux_data = new_data; |
| 5237 | vfree(old_data); |
| 5238 | return 0; |
| 5239 | } |
| 5240 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5241 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 5242 | { |
| 5243 | int i; |
| 5244 | |
| 5245 | if (len == 1) |
| 5246 | return; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 5247 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 5248 | for (i = 0; i <= env->subprog_cnt; i++) { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 5249 | if (env->subprog_info[i].start < off) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5250 | continue; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 5251 | env->subprog_info[i].start += len - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5252 | } |
| 5253 | } |
| 5254 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5255 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 5256 | const struct bpf_insn *patch, u32 len) |
| 5257 | { |
| 5258 | struct bpf_prog *new_prog; |
| 5259 | |
| 5260 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
| 5261 | if (!new_prog) |
| 5262 | return NULL; |
| 5263 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) |
| 5264 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5265 | adjust_subprog_starts(env, off, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5266 | return new_prog; |
| 5267 | } |
| 5268 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 5269 | /* The verifier does more data flow analysis than llvm and will not |
| 5270 | * explore branches that are dead at run time. Malicious programs can |
| 5271 | * have dead code too. Therefore replace all dead at-run-time code |
| 5272 | * with 'ja -1'. |
| 5273 | * |
| 5274 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 5275 | * program and through another bug we would manage to jump there, then |
| 5276 | * we'd execute beyond program memory otherwise. Returning exception |
| 5277 | * code also wouldn't work since we can have subprogs where the dead |
| 5278 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5279 | */ |
| 5280 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 5281 | { |
| 5282 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 5283 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5284 | struct bpf_insn *insn = env->prog->insnsi; |
| 5285 | const int insn_cnt = env->prog->len; |
| 5286 | int i; |
| 5287 | |
| 5288 | for (i = 0; i < insn_cnt; i++) { |
| 5289 | if (aux_data[i].seen) |
| 5290 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 5291 | memcpy(insn + i, &trap, sizeof(trap)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5292 | } |
| 5293 | } |
| 5294 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5295 | /* convert load instructions that access fields of 'struct __sk_buff' |
| 5296 | * into sequence of instructions that access fields of 'struct sk_buff' |
| 5297 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5298 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5299 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5300 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5301 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5302 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5303 | struct bpf_insn insn_buf[16], *insn; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5304 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5305 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5306 | bool is_narrower_load; |
| 5307 | u32 target_size; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5308 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5309 | if (ops->gen_prologue) { |
| 5310 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 5311 | env->prog); |
| 5312 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5313 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5314 | return -EINVAL; |
| 5315 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5316 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5317 | if (!new_prog) |
| 5318 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5319 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5320 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5321 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5322 | } |
| 5323 | } |
| 5324 | |
Jakub Kicinski | 0d83003 | 2018-05-08 19:37:06 -0700 | [diff] [blame] | 5325 | if (!ops->convert_ctx_access || bpf_prog_is_dev_bound(env->prog->aux)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5326 | return 0; |
| 5327 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5328 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5329 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5330 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 5331 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 5332 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 5333 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 5334 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5335 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 5336 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 5337 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 5338 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 5339 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5340 | type = BPF_WRITE; |
| 5341 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5342 | continue; |
| 5343 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 5344 | if (type == BPF_WRITE && |
| 5345 | env->insn_aux_data[i + delta].sanitize_stack_off) { |
| 5346 | struct bpf_insn patch[] = { |
| 5347 | /* Sanitize suspicious stack slot with zero. |
| 5348 | * There are no memory dependencies for this store, |
| 5349 | * since it's only using frame pointer and immediate |
| 5350 | * constant of zero |
| 5351 | */ |
| 5352 | BPF_ST_MEM(BPF_DW, BPF_REG_FP, |
| 5353 | env->insn_aux_data[i + delta].sanitize_stack_off, |
| 5354 | 0), |
| 5355 | /* the original STX instruction will immediately |
| 5356 | * overwrite the same stack slot with appropriate value |
| 5357 | */ |
| 5358 | *insn, |
| 5359 | }; |
| 5360 | |
| 5361 | cnt = ARRAY_SIZE(patch); |
| 5362 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 5363 | if (!new_prog) |
| 5364 | return -ENOMEM; |
| 5365 | |
| 5366 | delta += cnt - 1; |
| 5367 | env->prog = new_prog; |
| 5368 | insn = new_prog->insnsi + i + delta; |
| 5369 | continue; |
| 5370 | } |
| 5371 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5372 | if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5373 | continue; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5374 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5375 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5376 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5377 | |
| 5378 | /* If the read access is a narrower load of the field, |
| 5379 | * convert to a 4/8-byte load, to minimum program type specific |
| 5380 | * convert_ctx_access changes. If conversion is successful, |
| 5381 | * we will apply proper mask to the result. |
| 5382 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5383 | is_narrower_load = size < ctx_field_size; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5384 | if (is_narrower_load) { |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 5385 | u32 size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5386 | u32 off = insn->off; |
| 5387 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5388 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5389 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5390 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5391 | return -EINVAL; |
| 5392 | } |
| 5393 | |
| 5394 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5395 | if (ctx_field_size == 4) |
| 5396 | size_code = BPF_W; |
| 5397 | else if (ctx_field_size == 8) |
| 5398 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5399 | |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 5400 | insn->off = off & ~(size_default - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5401 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 5402 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5403 | |
| 5404 | target_size = 0; |
| 5405 | cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog, |
| 5406 | &target_size); |
| 5407 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 5408 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5409 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5410 | return -EINVAL; |
| 5411 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5412 | |
| 5413 | if (is_narrower_load && size < target_size) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5414 | if (ctx_field_size <= 4) |
| 5415 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5416 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5417 | else |
| 5418 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5419 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5420 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5421 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5422 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5423 | if (!new_prog) |
| 5424 | return -ENOMEM; |
| 5425 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5426 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5427 | |
| 5428 | /* keep walking new program and skip insns we just inserted */ |
| 5429 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5430 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5431 | } |
| 5432 | |
| 5433 | return 0; |
| 5434 | } |
| 5435 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5436 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 5437 | { |
| 5438 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 5439 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5440 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5441 | void *old_bpf_func; |
| 5442 | int err = -ENOMEM; |
| 5443 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5444 | if (env->subprog_cnt <= 1) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5445 | return 0; |
| 5446 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5447 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5448 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5449 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5450 | continue; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 5451 | /* Upon error here we cannot fall back to interpreter but |
| 5452 | * need a hard reject of the program. Thus -EFAULT is |
| 5453 | * propagated in any case. |
| 5454 | */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5455 | subprog = find_subprog(env, i + insn->imm + 1); |
| 5456 | if (subprog < 0) { |
| 5457 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 5458 | i + insn->imm + 1); |
| 5459 | return -EFAULT; |
| 5460 | } |
| 5461 | /* temporarily remember subprog id inside insn instead of |
| 5462 | * aux_data, since next loop will split up all insns into funcs |
| 5463 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5464 | insn->off = subprog; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5465 | /* remember original imm in case JIT fails and fallback |
| 5466 | * to interpreter will be needed |
| 5467 | */ |
| 5468 | env->insn_aux_data[i].call_imm = insn->imm; |
| 5469 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 5470 | insn->imm = 1; |
| 5471 | } |
| 5472 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 5473 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5474 | if (!func) |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 5475 | goto out_undo_insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5476 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5477 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5478 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 5479 | subprog_end = env->subprog_info[i + 1].start; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5480 | |
| 5481 | len = subprog_end - subprog_start; |
| 5482 | func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER); |
| 5483 | if (!func[i]) |
| 5484 | goto out_free; |
| 5485 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 5486 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 5487 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5488 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 5489 | if (bpf_prog_calc_tag(func[i])) |
| 5490 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5491 | func[i]->is_func = 1; |
| 5492 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 5493 | * Long term would need debug info to populate names |
| 5494 | */ |
| 5495 | func[i]->aux->name[0] = 'F'; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 5496 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5497 | func[i]->jit_requested = 1; |
| 5498 | func[i] = bpf_int_jit_compile(func[i]); |
| 5499 | if (!func[i]->jited) { |
| 5500 | err = -ENOTSUPP; |
| 5501 | goto out_free; |
| 5502 | } |
| 5503 | cond_resched(); |
| 5504 | } |
| 5505 | /* at this point all bpf functions were successfully JITed |
| 5506 | * now populate all bpf_calls with correct addresses and |
| 5507 | * run last pass of JIT |
| 5508 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5509 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5510 | insn = func[i]->insnsi; |
| 5511 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 5512 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5513 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5514 | continue; |
| 5515 | subprog = insn->off; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5516 | insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) |
| 5517 | func[subprog]->bpf_func - |
| 5518 | __bpf_call_base; |
| 5519 | } |
Sandipan Das | 2162fed | 2018-05-24 12:26:45 +0530 | [diff] [blame] | 5520 | |
| 5521 | /* we use the aux data to keep a list of the start addresses |
| 5522 | * of the JITed images for each function in the program |
| 5523 | * |
| 5524 | * for some architectures, such as powerpc64, the imm field |
| 5525 | * might not be large enough to hold the offset of the start |
| 5526 | * address of the callee's JITed image from __bpf_call_base |
| 5527 | * |
| 5528 | * in such cases, we can lookup the start address of a callee |
| 5529 | * by using its subprog id, available from the off field of |
| 5530 | * the call instruction, as an index for this list |
| 5531 | */ |
| 5532 | func[i]->aux->func = func; |
| 5533 | func[i]->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5534 | } |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5535 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5536 | old_bpf_func = func[i]->bpf_func; |
| 5537 | tmp = bpf_int_jit_compile(func[i]); |
| 5538 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 5539 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 5540 | err = -ENOTSUPP; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5541 | goto out_free; |
| 5542 | } |
| 5543 | cond_resched(); |
| 5544 | } |
| 5545 | |
| 5546 | /* finally lock prog and jit images for all functions and |
| 5547 | * populate kallsysm |
| 5548 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5549 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5550 | bpf_prog_lock_ro(func[i]); |
| 5551 | bpf_prog_kallsyms_add(func[i]); |
| 5552 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5553 | |
| 5554 | /* Last step: make now unused interpreter insns from main |
| 5555 | * prog consistent for later dump requests, so they can |
| 5556 | * later look the same as if they were interpreted only. |
| 5557 | */ |
| 5558 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5559 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5560 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5561 | continue; |
| 5562 | insn->off = env->insn_aux_data[i].call_imm; |
| 5563 | subprog = find_subprog(env, i + insn->off + 1); |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 5564 | insn->imm = subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5565 | } |
| 5566 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5567 | prog->jited = 1; |
| 5568 | prog->bpf_func = func[0]->bpf_func; |
| 5569 | prog->aux->func = func; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5570 | prog->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5571 | return 0; |
| 5572 | out_free: |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 5573 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5574 | if (func[i]) |
| 5575 | bpf_jit_free(func[i]); |
| 5576 | kfree(func); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 5577 | out_undo_insn: |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5578 | /* cleanup main prog to be interpreted */ |
| 5579 | prog->jit_requested = 0; |
| 5580 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 5581 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5582 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5583 | continue; |
| 5584 | insn->off = 0; |
| 5585 | insn->imm = env->insn_aux_data[i].call_imm; |
| 5586 | } |
| 5587 | return err; |
| 5588 | } |
| 5589 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5590 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 5591 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5592 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5593 | struct bpf_prog *prog = env->prog; |
| 5594 | struct bpf_insn *insn = prog->insnsi; |
| 5595 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5596 | #endif |
| 5597 | int err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5598 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5599 | err = 0; |
| 5600 | if (env->prog->jit_requested) { |
| 5601 | err = jit_subprogs(env); |
| 5602 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5603 | return 0; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 5604 | if (err == -EFAULT) |
| 5605 | return err; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5606 | } |
| 5607 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5608 | for (i = 0; i < prog->len; i++, insn++) { |
| 5609 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5610 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5611 | continue; |
| 5612 | depth = get_callee_stack_depth(env, insn, i); |
| 5613 | if (depth < 0) |
| 5614 | return depth; |
| 5615 | bpf_patch_call_args(insn, depth); |
| 5616 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5617 | err = 0; |
| 5618 | #endif |
| 5619 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5620 | } |
| 5621 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5622 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5623 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5624 | * |
| 5625 | * this function is called after eBPF program passed verification |
| 5626 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5627 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5628 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5629 | struct bpf_prog *prog = env->prog; |
| 5630 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5631 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5632 | const int insn_cnt = prog->len; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 5633 | const struct bpf_map_ops *ops; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 5634 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5635 | struct bpf_insn insn_buf[16]; |
| 5636 | struct bpf_prog *new_prog; |
| 5637 | struct bpf_map *map_ptr; |
| 5638 | int i, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5639 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5640 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 5641 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 5642 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 5643 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 5644 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 5645 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
| 5646 | struct bpf_insn mask_and_div[] = { |
| 5647 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 5648 | /* Rx div 0 -> 0 */ |
| 5649 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), |
| 5650 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 5651 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 5652 | *insn, |
| 5653 | }; |
| 5654 | struct bpf_insn mask_and_mod[] = { |
| 5655 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 5656 | /* Rx mod 0 -> Rx */ |
| 5657 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), |
| 5658 | *insn, |
| 5659 | }; |
| 5660 | struct bpf_insn *patchlet; |
| 5661 | |
| 5662 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 5663 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 5664 | patchlet = mask_and_div + (is64 ? 1 : 0); |
| 5665 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); |
| 5666 | } else { |
| 5667 | patchlet = mask_and_mod + (is64 ? 1 : 0); |
| 5668 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); |
| 5669 | } |
| 5670 | |
| 5671 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 5672 | if (!new_prog) |
| 5673 | return -ENOMEM; |
| 5674 | |
| 5675 | delta += cnt - 1; |
| 5676 | env->prog = prog = new_prog; |
| 5677 | insn = new_prog->insnsi + i + delta; |
| 5678 | continue; |
| 5679 | } |
| 5680 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 5681 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 5682 | (BPF_MODE(insn->code) == BPF_ABS || |
| 5683 | BPF_MODE(insn->code) == BPF_IND)) { |
| 5684 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 5685 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 5686 | verbose(env, "bpf verifier is misconfigured\n"); |
| 5687 | return -EINVAL; |
| 5688 | } |
| 5689 | |
| 5690 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 5691 | if (!new_prog) |
| 5692 | return -ENOMEM; |
| 5693 | |
| 5694 | delta += cnt - 1; |
| 5695 | env->prog = prog = new_prog; |
| 5696 | insn = new_prog->insnsi + i + delta; |
| 5697 | continue; |
| 5698 | } |
| 5699 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5700 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 5701 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5702 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 5703 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5704 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5705 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 5706 | prog->dst_needed = 1; |
| 5707 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 5708 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 5709 | if (insn->imm == BPF_FUNC_override_return) |
| 5710 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5711 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 5712 | /* If we tail call into other programs, we |
| 5713 | * cannot make any assumptions since they can |
| 5714 | * be replaced dynamically during runtime in |
| 5715 | * the program array. |
| 5716 | */ |
| 5717 | prog->cb_access = 1; |
Alexei Starovoitov | 80a58d0 | 2017-05-30 13:31:30 -0700 | [diff] [blame] | 5718 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 5719 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5720 | /* mark bpf_tail_call as different opcode to avoid |
| 5721 | * conditional branch in the interpeter for every normal |
| 5722 | * call and to prevent accidental JITing by JIT compiler |
| 5723 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5724 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5725 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 5726 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 5727 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 5728 | aux = &env->insn_aux_data[i + delta]; |
| 5729 | if (!bpf_map_ptr_unpriv(aux)) |
| 5730 | continue; |
| 5731 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 5732 | /* instead of changing every JIT dealing with tail_call |
| 5733 | * emit two extra insns: |
| 5734 | * if (index >= max_entries) goto out; |
| 5735 | * index &= array->index_mask; |
| 5736 | * to avoid out-of-bounds cpu speculation |
| 5737 | */ |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 5738 | if (bpf_map_ptr_poisoned(aux)) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 5739 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 5740 | return -EINVAL; |
| 5741 | } |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 5742 | |
| 5743 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 5744 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 5745 | map_ptr->max_entries, 2); |
| 5746 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 5747 | container_of(map_ptr, |
| 5748 | struct bpf_array, |
| 5749 | map)->index_mask); |
| 5750 | insn_buf[2] = *insn; |
| 5751 | cnt = 3; |
| 5752 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 5753 | if (!new_prog) |
| 5754 | return -ENOMEM; |
| 5755 | |
| 5756 | delta += cnt - 1; |
| 5757 | env->prog = prog = new_prog; |
| 5758 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5759 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5760 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5761 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 5762 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 5763 | * and other inlining handlers are currently limited to 64 bit |
| 5764 | * only. |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 5765 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 5766 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 5767 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 5768 | insn->imm == BPF_FUNC_map_update_elem || |
| 5769 | insn->imm == BPF_FUNC_map_delete_elem)) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 5770 | aux = &env->insn_aux_data[i + delta]; |
| 5771 | if (bpf_map_ptr_poisoned(aux)) |
| 5772 | goto patch_call_imm; |
| 5773 | |
| 5774 | map_ptr = BPF_MAP_PTR(aux->map_state); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 5775 | ops = map_ptr->ops; |
| 5776 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 5777 | ops->map_gen_lookup) { |
| 5778 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
| 5779 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 5780 | verbose(env, "bpf verifier is misconfigured\n"); |
| 5781 | return -EINVAL; |
| 5782 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5783 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 5784 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 5785 | insn_buf, cnt); |
| 5786 | if (!new_prog) |
| 5787 | return -ENOMEM; |
| 5788 | |
| 5789 | delta += cnt - 1; |
| 5790 | env->prog = prog = new_prog; |
| 5791 | insn = new_prog->insnsi + i + delta; |
| 5792 | continue; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5793 | } |
| 5794 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 5795 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 5796 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 5797 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 5798 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 5799 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 5800 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 5801 | u64 flags))NULL)); |
| 5802 | switch (insn->imm) { |
| 5803 | case BPF_FUNC_map_lookup_elem: |
| 5804 | insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) - |
| 5805 | __bpf_call_base; |
| 5806 | continue; |
| 5807 | case BPF_FUNC_map_update_elem: |
| 5808 | insn->imm = BPF_CAST_CALL(ops->map_update_elem) - |
| 5809 | __bpf_call_base; |
| 5810 | continue; |
| 5811 | case BPF_FUNC_map_delete_elem: |
| 5812 | insn->imm = BPF_CAST_CALL(ops->map_delete_elem) - |
| 5813 | __bpf_call_base; |
| 5814 | continue; |
| 5815 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5816 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 5817 | goto patch_call_imm; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5818 | } |
| 5819 | |
Daniel Borkmann | 109980b | 2017-09-08 00:14:51 +0200 | [diff] [blame] | 5820 | if (insn->imm == BPF_FUNC_redirect_map) { |
Daniel Borkmann | 7c30013 | 2017-09-20 00:44:21 +0200 | [diff] [blame] | 5821 | /* Note, we cannot use prog directly as imm as subsequent |
| 5822 | * rewrites would still change the prog pointer. The only |
| 5823 | * stable address we can use is aux, which also works with |
| 5824 | * prog clones during blinding. |
| 5825 | */ |
| 5826 | u64 addr = (unsigned long)prog->aux; |
Daniel Borkmann | 109980b | 2017-09-08 00:14:51 +0200 | [diff] [blame] | 5827 | struct bpf_insn r4_ld[] = { |
| 5828 | BPF_LD_IMM64(BPF_REG_4, addr), |
| 5829 | *insn, |
| 5830 | }; |
| 5831 | cnt = ARRAY_SIZE(r4_ld); |
| 5832 | |
| 5833 | new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt); |
| 5834 | if (!new_prog) |
| 5835 | return -ENOMEM; |
| 5836 | |
| 5837 | delta += cnt - 1; |
| 5838 | env->prog = prog = new_prog; |
| 5839 | insn = new_prog->insnsi + i + delta; |
| 5840 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5841 | patch_call_imm: |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 5842 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5843 | /* all functions that have prototype and verifier allowed |
| 5844 | * programs to call them, must be real in-kernel functions |
| 5845 | */ |
| 5846 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5847 | verbose(env, |
| 5848 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5849 | func_id_name(insn->imm), insn->imm); |
| 5850 | return -EFAULT; |
| 5851 | } |
| 5852 | insn->imm = fn->func - __bpf_call_base; |
| 5853 | } |
| 5854 | |
| 5855 | return 0; |
| 5856 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5857 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5858 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5859 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5860 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5861 | int i; |
| 5862 | |
| 5863 | if (!env->explored_states) |
| 5864 | return; |
| 5865 | |
| 5866 | for (i = 0; i < env->prog->len; i++) { |
| 5867 | sl = env->explored_states[i]; |
| 5868 | |
| 5869 | if (sl) |
| 5870 | while (sl != STATE_LIST_MARK) { |
| 5871 | sln = sl->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 5872 | free_verifier_state(&sl->state, false); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5873 | kfree(sl); |
| 5874 | sl = sln; |
| 5875 | } |
| 5876 | } |
| 5877 | |
| 5878 | kfree(env->explored_states); |
| 5879 | } |
| 5880 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5881 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5882 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5883 | struct bpf_verifier_env *env; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 5884 | struct bpf_verifier_log *log; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5885 | int ret = -EINVAL; |
| 5886 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 5887 | /* no program is valid */ |
| 5888 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 5889 | return -EINVAL; |
| 5890 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5891 | /* '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] | 5892 | * allocate/free it every time bpf_check() is called |
| 5893 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5894 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5895 | if (!env) |
| 5896 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5897 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5898 | |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 5899 | env->insn_aux_data = |
| 5900 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), |
| 5901 | (*prog)->len)); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5902 | ret = -ENOMEM; |
| 5903 | if (!env->insn_aux_data) |
| 5904 | goto err_free_env; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5905 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5906 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5907 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5908 | /* grab the mutex to protect few globals used by verifier */ |
| 5909 | mutex_lock(&bpf_verifier_lock); |
| 5910 | |
| 5911 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 5912 | /* user requested verbose verifier output |
| 5913 | * and supplied buffer to store the verification trace |
| 5914 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 5915 | log->level = attr->log_level; |
| 5916 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 5917 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5918 | |
| 5919 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 5920 | /* log attributes have to be sane */ |
| 5921 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || |
| 5922 | !log->level || !log->ubuf) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5923 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5924 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 5925 | |
| 5926 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 5927 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 5928 | env->strict_alignment = true; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5929 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5930 | ret = replace_map_fd_with_map_ptr(env); |
| 5931 | if (ret < 0) |
| 5932 | goto skip_full_check; |
| 5933 | |
Jakub Kicinski | f4e3ec0 | 2018-05-03 18:37:11 -0700 | [diff] [blame] | 5934 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
| 5935 | ret = bpf_prog_offload_verifier_prep(env); |
| 5936 | if (ret) |
| 5937 | goto skip_full_check; |
| 5938 | } |
| 5939 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5940 | env->explored_states = kcalloc(env->prog->len, |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5941 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5942 | GFP_USER); |
| 5943 | ret = -ENOMEM; |
| 5944 | if (!env->explored_states) |
| 5945 | goto skip_full_check; |
| 5946 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5947 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 5948 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5949 | ret = check_cfg(env); |
| 5950 | if (ret < 0) |
| 5951 | goto skip_full_check; |
| 5952 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5953 | ret = do_check(env); |
Craig Gallek | 8c01c4f | 2017-11-02 11:18:01 -0400 | [diff] [blame] | 5954 | if (env->cur_state) { |
| 5955 | free_verifier_state(env->cur_state, true); |
| 5956 | env->cur_state = NULL; |
| 5957 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5958 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5959 | skip_full_check: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5960 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5961 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5962 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5963 | if (ret == 0) |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5964 | sanitize_dead_code(env); |
| 5965 | |
| 5966 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 5967 | ret = check_max_stack_depth(env); |
| 5968 | |
| 5969 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5970 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 5971 | ret = convert_ctx_accesses(env); |
| 5972 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5973 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5974 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5975 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5976 | if (ret == 0) |
| 5977 | ret = fixup_call_args(env); |
| 5978 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5979 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5980 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5981 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5982 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5983 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5984 | } |
| 5985 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5986 | if (ret == 0 && env->used_map_cnt) { |
| 5987 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5988 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 5989 | sizeof(env->used_maps[0]), |
| 5990 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5991 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5992 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5993 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5994 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5995 | } |
| 5996 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5997 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5998 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5999 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6000 | |
| 6001 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 6002 | * bpf_ld_imm64 instructions |
| 6003 | */ |
| 6004 | convert_pseudo_ld_imm64(env); |
| 6005 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6006 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 6007 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6008 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6009 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 6010 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 6011 | */ |
| 6012 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 6013 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6014 | err_unlock: |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 6015 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 6016 | vfree(env->insn_aux_data); |
| 6017 | err_free_env: |
| 6018 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 6019 | return ret; |
| 6020 | } |