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> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 25 | |
Jakub Kicinski | f4ac7e0 | 2017-10-09 10:30:12 -0700 | [diff] [blame] | 26 | #include "disasm.h" |
| 27 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 28 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
| 29 | #define BPF_PROG_TYPE(_id, _name) \ |
| 30 | [_id] = & _name ## _verifier_ops, |
| 31 | #define BPF_MAP_TYPE(_id, _ops) |
| 32 | #include <linux/bpf_types.h> |
| 33 | #undef BPF_PROG_TYPE |
| 34 | #undef BPF_MAP_TYPE |
| 35 | }; |
| 36 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 37 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 38 | * instruction by instruction and updates register/stack state. |
| 39 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 40 | * |
| 41 | * The first pass is depth-first-search to check that the program is a DAG. |
| 42 | * It rejects the following programs: |
| 43 | * - larger than BPF_MAXINSNS insns |
| 44 | * - if loop is present (detected via back-edge) |
| 45 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 46 | * - out of bounds or malformed jumps |
| 47 | * The second pass is all possible path descent from the 1st insn. |
| 48 | * 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] | 49 | * 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] | 50 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 51 | * Number of 'branches to be analyzed' is limited to 1k |
| 52 | * |
| 53 | * On entry to each instruction, each register has a type, and the instruction |
| 54 | * changes the types of the registers depending on instruction semantics. |
| 55 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 56 | * copied to R1. |
| 57 | * |
| 58 | * All registers are 64-bit. |
| 59 | * R0 - return register |
| 60 | * R1-R5 argument passing registers |
| 61 | * R6-R9 callee saved registers |
| 62 | * R10 - frame pointer read-only |
| 63 | * |
| 64 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 65 | * and has type PTR_TO_CTX. |
| 66 | * |
| 67 | * Verifier tracks arithmetic operations on pointers in case: |
| 68 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 69 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 70 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 71 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 72 | * that it wants to construct a pointer to some element within stack. |
| 73 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 74 | * (and -20 constant is saved for further stack bounds checking). |
| 75 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 76 | * |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 77 | * Most of the time the registers have SCALAR_VALUE type, which |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 78 | * 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] | 79 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 80 | * |
| 81 | * When verifier sees load or store instructions the type of base register |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 82 | * 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] | 83 | * types recognized by check_mem_access() function. |
| 84 | * |
| 85 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 86 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 87 | * |
| 88 | * registers used to pass values to function calls are checked against |
| 89 | * function argument constraints. |
| 90 | * |
| 91 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 92 | * It means that the register type passed to this function must be |
| 93 | * PTR_TO_STACK and it will be used inside the function as |
| 94 | * 'pointer to map element key' |
| 95 | * |
| 96 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 97 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 98 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 99 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 100 | * |
| 101 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 102 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 103 | * 2nd argument should be a pointer to stack, which will be used inside |
| 104 | * the helper function as a pointer to map element key. |
| 105 | * |
| 106 | * On the kernel side the helper function looks like: |
| 107 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 108 | * { |
| 109 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 110 | * void *key = (void *) (unsigned long) r2; |
| 111 | * void *value; |
| 112 | * |
| 113 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 114 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 115 | * the stack of eBPF program. |
| 116 | * } |
| 117 | * |
| 118 | * Corresponding eBPF program may look like: |
| 119 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 120 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 121 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 122 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 123 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 124 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 125 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 126 | * |
| 127 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 128 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 129 | * and were initialized prior to this call. |
| 130 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 131 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 132 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 133 | * returns ether pointer to map value or NULL. |
| 134 | * |
| 135 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 136 | * insn, the register holding that pointer in the true branch changes state to |
| 137 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 138 | * branch. See check_cond_jmp_op(). |
| 139 | * |
| 140 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 141 | * are set to NOT_INIT to indicate that they are no longer readable. |
| 142 | */ |
| 143 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 144 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 145 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 146 | /* verifer state is 'st' |
| 147 | * before processing instruction 'insn_idx' |
| 148 | * and after processing instruction 'prev_insn_idx' |
| 149 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 150 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 151 | int insn_idx; |
| 152 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 153 | struct bpf_verifier_stack_elem *next; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Edward Cree | 8e17c1b | 2017-08-07 15:30:30 +0100 | [diff] [blame] | 156 | #define BPF_COMPLEXITY_LIMIT_INSNS 131072 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 157 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
| 158 | |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 159 | #define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA) |
| 160 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 161 | struct bpf_call_arg_meta { |
| 162 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 163 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 164 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 165 | int regno; |
| 166 | int access_size; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 167 | }; |
| 168 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 169 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 170 | |
| 171 | /* log_level controls verbosity level of eBPF verifier. |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 172 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 173 | * so the user can figure out what's wrong with the program |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 174 | */ |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 175 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 176 | const char *fmt, ...) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 177 | { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 178 | struct bpf_verifer_log *log = &env->log; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 179 | unsigned int n; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 180 | va_list args; |
| 181 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 182 | if (!log->level || !log->ubuf || bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 183 | return; |
| 184 | |
| 185 | va_start(args, fmt); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 186 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 187 | va_end(args); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 188 | |
| 189 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 190 | "verifier log line truncated - local buffer too short\n"); |
| 191 | |
| 192 | n = min(log->len_total - log->len_used - 1, n); |
| 193 | log->kbuf[n] = '\0'; |
| 194 | |
| 195 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 196 | log->len_used += n; |
| 197 | else |
| 198 | log->ubuf = NULL; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 199 | } |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 200 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 201 | /* Historically bpf_verifier_log_write was called verbose, but the name was too |
| 202 | * generic for symbol export. The function was renamed, but not the calls in |
| 203 | * the verifier to avoid complicating backports. Hence the alias below. |
| 204 | */ |
| 205 | static __printf(2, 3) void verbose(struct bpf_verifier_env *env, |
| 206 | const char *fmt, ...) |
| 207 | __attribute__((alias("bpf_verifier_log_write"))); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 208 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 209 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 210 | { |
| 211 | return type == PTR_TO_PACKET || |
| 212 | type == PTR_TO_PACKET_META; |
| 213 | } |
| 214 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 215 | /* string representation of 'enum bpf_reg_type' */ |
| 216 | static const char * const reg_type_str[] = { |
| 217 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 218 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 219 | [PTR_TO_CTX] = "ctx", |
| 220 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 221 | [PTR_TO_MAP_VALUE] = "map_value", |
| 222 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 223 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 224 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 225 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 226 | [PTR_TO_PACKET_END] = "pkt_end", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 229 | static void print_liveness(struct bpf_verifier_env *env, |
| 230 | enum bpf_reg_liveness live) |
| 231 | { |
| 232 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN)) |
| 233 | verbose(env, "_"); |
| 234 | if (live & REG_LIVE_READ) |
| 235 | verbose(env, "r"); |
| 236 | if (live & REG_LIVE_WRITTEN) |
| 237 | verbose(env, "w"); |
| 238 | } |
| 239 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 240 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 241 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 242 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 243 | struct bpf_verifier_state *cur = env->cur_state; |
| 244 | |
| 245 | return cur->frame[reg->frameno]; |
| 246 | } |
| 247 | |
| 248 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 249 | const struct bpf_func_state *state) |
| 250 | { |
| 251 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 252 | enum bpf_reg_type t; |
| 253 | int i; |
| 254 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 255 | if (state->frameno) |
| 256 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 257 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 258 | reg = &state->regs[i]; |
| 259 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 260 | if (t == NOT_INIT) |
| 261 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 262 | verbose(env, " R%d", i); |
| 263 | print_liveness(env, reg->live); |
| 264 | verbose(env, "=%s", reg_type_str[t]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 265 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 266 | tnum_is_const(reg->var_off)) { |
| 267 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 268 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 269 | if (t == PTR_TO_STACK) |
| 270 | verbose(env, ",call_%d", func(env, reg)->callsite); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 271 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 272 | verbose(env, "(id=%d", reg->id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 273 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 274 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 275 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 276 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 277 | else if (t == CONST_PTR_TO_MAP || |
| 278 | t == PTR_TO_MAP_VALUE || |
| 279 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 280 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 281 | reg->map_ptr->key_size, |
| 282 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 283 | if (tnum_is_const(reg->var_off)) { |
| 284 | /* Typically an immediate SCALAR_VALUE, but |
| 285 | * could be a pointer whose offset is too big |
| 286 | * for reg->off |
| 287 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 288 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 289 | } else { |
| 290 | if (reg->smin_value != reg->umin_value && |
| 291 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 292 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 293 | (long long)reg->smin_value); |
| 294 | if (reg->smax_value != reg->umax_value && |
| 295 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 296 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 297 | (long long)reg->smax_value); |
| 298 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 299 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 300 | (unsigned long long)reg->umin_value); |
| 301 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 302 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 303 | (unsigned long long)reg->umax_value); |
| 304 | if (!tnum_is_unknown(reg->var_off)) { |
| 305 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 306 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 307 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 308 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 309 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 310 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 311 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 312 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 313 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 314 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 315 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
| 316 | verbose(env, " fp%d", |
| 317 | (-i - 1) * BPF_REG_SIZE); |
| 318 | print_liveness(env, state->stack[i].spilled_ptr.live); |
| 319 | verbose(env, "=%s", |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 320 | reg_type_str[state->stack[i].spilled_ptr.type]); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 321 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 322 | if (state->stack[i].slot_type[0] == STACK_ZERO) |
| 323 | verbose(env, " fp%d=0", (-i - 1) * BPF_REG_SIZE); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 324 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 325 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 328 | static int copy_stack_state(struct bpf_func_state *dst, |
| 329 | const struct bpf_func_state *src) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 330 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 331 | if (!src->stack) |
| 332 | return 0; |
| 333 | if (WARN_ON_ONCE(dst->allocated_stack < src->allocated_stack)) { |
| 334 | /* internal bug, make state invalid to reject the program */ |
| 335 | memset(dst, 0, sizeof(*dst)); |
| 336 | return -EFAULT; |
| 337 | } |
| 338 | memcpy(dst->stack, src->stack, |
| 339 | sizeof(*src->stack) * (src->allocated_stack / BPF_REG_SIZE)); |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 344 | * make it consume minimal amount of memory. check_stack_write() access from |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 345 | * the program calls into realloc_func_state() to grow the stack size. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 346 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 347 | * which this function copies over. It points to previous bpf_verifier_state |
| 348 | * which is never reallocated |
| 349 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 350 | static int realloc_func_state(struct bpf_func_state *state, int size, |
| 351 | bool copy_old) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 352 | { |
| 353 | u32 old_size = state->allocated_stack; |
| 354 | struct bpf_stack_state *new_stack; |
| 355 | int slot = size / BPF_REG_SIZE; |
| 356 | |
| 357 | if (size <= old_size || !size) { |
| 358 | if (copy_old) |
| 359 | return 0; |
| 360 | state->allocated_stack = slot * BPF_REG_SIZE; |
| 361 | if (!size && old_size) { |
| 362 | kfree(state->stack); |
| 363 | state->stack = NULL; |
| 364 | } |
| 365 | return 0; |
| 366 | } |
| 367 | new_stack = kmalloc_array(slot, sizeof(struct bpf_stack_state), |
| 368 | GFP_KERNEL); |
| 369 | if (!new_stack) |
| 370 | return -ENOMEM; |
| 371 | if (copy_old) { |
| 372 | if (state->stack) |
| 373 | memcpy(new_stack, state->stack, |
| 374 | sizeof(*new_stack) * (old_size / BPF_REG_SIZE)); |
| 375 | memset(new_stack + old_size / BPF_REG_SIZE, 0, |
| 376 | sizeof(*new_stack) * (size - old_size) / BPF_REG_SIZE); |
| 377 | } |
| 378 | state->allocated_stack = slot * BPF_REG_SIZE; |
| 379 | kfree(state->stack); |
| 380 | state->stack = new_stack; |
| 381 | return 0; |
| 382 | } |
| 383 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 384 | static void free_func_state(struct bpf_func_state *state) |
| 385 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 386 | if (!state) |
| 387 | return; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 388 | kfree(state->stack); |
| 389 | kfree(state); |
| 390 | } |
| 391 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 392 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 393 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 394 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 395 | int i; |
| 396 | |
| 397 | for (i = 0; i <= state->curframe; i++) { |
| 398 | free_func_state(state->frame[i]); |
| 399 | state->frame[i] = NULL; |
| 400 | } |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 401 | if (free_self) |
| 402 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* copy verifier state from src to dst growing dst stack space |
| 406 | * when necessary to accommodate larger src stack |
| 407 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 408 | static int copy_func_state(struct bpf_func_state *dst, |
| 409 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 410 | { |
| 411 | int err; |
| 412 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 413 | err = realloc_func_state(dst, src->allocated_stack, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 414 | if (err) |
| 415 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 416 | memcpy(dst, src, offsetof(struct bpf_func_state, allocated_stack)); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 417 | return copy_stack_state(dst, src); |
| 418 | } |
| 419 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 420 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 421 | const struct bpf_verifier_state *src) |
| 422 | { |
| 423 | struct bpf_func_state *dst; |
| 424 | int i, err; |
| 425 | |
| 426 | /* if dst has more stack frames then src frame, free them */ |
| 427 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 428 | free_func_state(dst_state->frame[i]); |
| 429 | dst_state->frame[i] = NULL; |
| 430 | } |
| 431 | dst_state->curframe = src->curframe; |
| 432 | dst_state->parent = src->parent; |
| 433 | for (i = 0; i <= src->curframe; i++) { |
| 434 | dst = dst_state->frame[i]; |
| 435 | if (!dst) { |
| 436 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 437 | if (!dst) |
| 438 | return -ENOMEM; |
| 439 | dst_state->frame[i] = dst; |
| 440 | } |
| 441 | err = copy_func_state(dst, src->frame[i]); |
| 442 | if (err) |
| 443 | return err; |
| 444 | } |
| 445 | return 0; |
| 446 | } |
| 447 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 448 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
| 449 | int *insn_idx) |
| 450 | { |
| 451 | struct bpf_verifier_state *cur = env->cur_state; |
| 452 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 453 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 454 | |
| 455 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 456 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 457 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 458 | if (cur) { |
| 459 | err = copy_verifier_state(cur, &head->st); |
| 460 | if (err) |
| 461 | return err; |
| 462 | } |
| 463 | if (insn_idx) |
| 464 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 465 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 466 | *prev_insn_idx = head->prev_insn_idx; |
| 467 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 468 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 469 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 470 | env->head = elem; |
| 471 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 472 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 475 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
| 476 | int insn_idx, int prev_insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 477 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 478 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 479 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 480 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 481 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 482 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 483 | if (!elem) |
| 484 | goto err; |
| 485 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 486 | elem->insn_idx = insn_idx; |
| 487 | elem->prev_insn_idx = prev_insn_idx; |
| 488 | elem->next = env->head; |
| 489 | env->head = elem; |
| 490 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 491 | err = copy_verifier_state(&elem->st, cur); |
| 492 | if (err) |
| 493 | goto err; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 494 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 495 | verbose(env, "BPF program is too complex\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 496 | goto err; |
| 497 | } |
| 498 | return &elem->st; |
| 499 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 500 | free_verifier_state(env->cur_state, true); |
| 501 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 502 | /* pop all elements and return */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 503 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 504 | return NULL; |
| 505 | } |
| 506 | |
| 507 | #define CALLER_SAVED_REGS 6 |
| 508 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 509 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 510 | }; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 511 | #define CALLEE_SAVED_REGS 5 |
| 512 | static const int callee_saved[CALLEE_SAVED_REGS] = { |
| 513 | BPF_REG_6, BPF_REG_7, BPF_REG_8, BPF_REG_9 |
| 514 | }; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 515 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 516 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
| 517 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 518 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 519 | * known to have the value @imm. |
| 520 | */ |
| 521 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 522 | { |
| 523 | reg->id = 0; |
| 524 | reg->var_off = tnum_const(imm); |
| 525 | reg->smin_value = (s64)imm; |
| 526 | reg->smax_value = (s64)imm; |
| 527 | reg->umin_value = imm; |
| 528 | reg->umax_value = imm; |
| 529 | } |
| 530 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 531 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 532 | * used only on registers holding a pointer type. |
| 533 | */ |
| 534 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 535 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 536 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 539 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 540 | { |
| 541 | __mark_reg_known(reg, 0); |
| 542 | reg->off = 0; |
| 543 | reg->type = SCALAR_VALUE; |
| 544 | } |
| 545 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 546 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 547 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 548 | { |
| 549 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 550 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 551 | /* Something bad happened, let's kill all regs */ |
| 552 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 553 | __mark_reg_not_init(regs + regno); |
| 554 | return; |
| 555 | } |
| 556 | __mark_reg_known_zero(regs + regno); |
| 557 | } |
| 558 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 559 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 560 | { |
| 561 | return type_is_pkt_pointer(reg->type); |
| 562 | } |
| 563 | |
| 564 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 565 | { |
| 566 | return reg_is_pkt_pointer(reg) || |
| 567 | reg->type == PTR_TO_PACKET_END; |
| 568 | } |
| 569 | |
| 570 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 571 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 572 | enum bpf_reg_type which) |
| 573 | { |
| 574 | /* The register can already have a range from prior markings. |
| 575 | * This is fine as long as it hasn't been advanced from its |
| 576 | * origin. |
| 577 | */ |
| 578 | return reg->type == which && |
| 579 | reg->id == 0 && |
| 580 | reg->off == 0 && |
| 581 | tnum_equals_const(reg->var_off, 0); |
| 582 | } |
| 583 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 584 | /* Attempts to improve min/max values based on var_off information */ |
| 585 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 586 | { |
| 587 | /* min signed is max(sign bit) | min(other bits) */ |
| 588 | reg->smin_value = max_t(s64, reg->smin_value, |
| 589 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 590 | /* max signed is min(sign bit) | max(other bits) */ |
| 591 | reg->smax_value = min_t(s64, reg->smax_value, |
| 592 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 593 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 594 | reg->umax_value = min(reg->umax_value, |
| 595 | reg->var_off.value | reg->var_off.mask); |
| 596 | } |
| 597 | |
| 598 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
| 599 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 600 | { |
| 601 | /* Learn sign from signed bounds. |
| 602 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 603 | * are the same, so combine. This works even in the negative case, e.g. |
| 604 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 605 | */ |
| 606 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 607 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 608 | reg->umin_value); |
| 609 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 610 | reg->umax_value); |
| 611 | return; |
| 612 | } |
| 613 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 614 | * boundary, so we must be careful. |
| 615 | */ |
| 616 | if ((s64)reg->umax_value >= 0) { |
| 617 | /* Positive. We can't learn anything from the smin, but smax |
| 618 | * is positive, hence safe. |
| 619 | */ |
| 620 | reg->smin_value = reg->umin_value; |
| 621 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 622 | reg->umax_value); |
| 623 | } else if ((s64)reg->umin_value < 0) { |
| 624 | /* Negative. We can't learn anything from the smax, but smin |
| 625 | * is negative, hence safe. |
| 626 | */ |
| 627 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 628 | reg->umin_value); |
| 629 | reg->smax_value = reg->umax_value; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 634 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 635 | { |
| 636 | reg->var_off = tnum_intersect(reg->var_off, |
| 637 | tnum_range(reg->umin_value, |
| 638 | reg->umax_value)); |
| 639 | } |
| 640 | |
| 641 | /* Reset the min/max bounds of a register */ |
| 642 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 643 | { |
| 644 | reg->smin_value = S64_MIN; |
| 645 | reg->smax_value = S64_MAX; |
| 646 | reg->umin_value = 0; |
| 647 | reg->umax_value = U64_MAX; |
| 648 | } |
| 649 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 650 | /* Mark a register as having a completely unknown (scalar) value. */ |
| 651 | static void __mark_reg_unknown(struct bpf_reg_state *reg) |
| 652 | { |
| 653 | reg->type = SCALAR_VALUE; |
| 654 | reg->id = 0; |
| 655 | reg->off = 0; |
| 656 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 657 | reg->frameno = 0; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 658 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 659 | } |
| 660 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 661 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 662 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 663 | { |
| 664 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 665 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 666 | /* Something bad happened, let's kill all regs except FP */ |
| 667 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 668 | __mark_reg_not_init(regs + regno); |
| 669 | return; |
| 670 | } |
| 671 | __mark_reg_unknown(regs + regno); |
| 672 | } |
| 673 | |
| 674 | static void __mark_reg_not_init(struct bpf_reg_state *reg) |
| 675 | { |
| 676 | __mark_reg_unknown(reg); |
| 677 | reg->type = NOT_INIT; |
| 678 | } |
| 679 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 680 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 681 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 682 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 683 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 684 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 685 | /* Something bad happened, let's kill all regs except FP */ |
| 686 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 687 | __mark_reg_not_init(regs + regno); |
| 688 | return; |
| 689 | } |
| 690 | __mark_reg_not_init(regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 691 | } |
| 692 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 693 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 694 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 695 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 696 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 697 | int i; |
| 698 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 699 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 700 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 701 | regs[i].live = REG_LIVE_NONE; |
| 702 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 703 | |
| 704 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 705 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 706 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 707 | regs[BPF_REG_FP].frameno = state->frameno; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 708 | |
| 709 | /* 1st arg to a function */ |
| 710 | regs[BPF_REG_1].type = PTR_TO_CTX; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 711 | mark_reg_known_zero(env, regs, BPF_REG_1); |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 712 | } |
| 713 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 714 | #define BPF_MAIN_FUNC (-1) |
| 715 | static void init_func_state(struct bpf_verifier_env *env, |
| 716 | struct bpf_func_state *state, |
| 717 | int callsite, int frameno, int subprogno) |
| 718 | { |
| 719 | state->callsite = callsite; |
| 720 | state->frameno = frameno; |
| 721 | state->subprogno = subprogno; |
| 722 | init_reg_state(env, state); |
| 723 | } |
| 724 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 725 | enum reg_arg_type { |
| 726 | SRC_OP, /* register is used as source operand */ |
| 727 | DST_OP, /* register is used as destination operand */ |
| 728 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 729 | }; |
| 730 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 731 | static int cmp_subprogs(const void *a, const void *b) |
| 732 | { |
| 733 | return *(int *)a - *(int *)b; |
| 734 | } |
| 735 | |
| 736 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 737 | { |
| 738 | u32 *p; |
| 739 | |
| 740 | p = bsearch(&off, env->subprog_starts, env->subprog_cnt, |
| 741 | sizeof(env->subprog_starts[0]), cmp_subprogs); |
| 742 | if (!p) |
| 743 | return -ENOENT; |
| 744 | return p - env->subprog_starts; |
| 745 | |
| 746 | } |
| 747 | |
| 748 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 749 | { |
| 750 | int insn_cnt = env->prog->len; |
| 751 | int ret; |
| 752 | |
| 753 | if (off >= insn_cnt || off < 0) { |
| 754 | verbose(env, "call to invalid destination\n"); |
| 755 | return -EINVAL; |
| 756 | } |
| 757 | ret = find_subprog(env, off); |
| 758 | if (ret >= 0) |
| 759 | return 0; |
| 760 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
| 761 | verbose(env, "too many subprograms\n"); |
| 762 | return -E2BIG; |
| 763 | } |
| 764 | env->subprog_starts[env->subprog_cnt++] = off; |
| 765 | sort(env->subprog_starts, env->subprog_cnt, |
| 766 | sizeof(env->subprog_starts[0]), cmp_subprogs, NULL); |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | static int check_subprogs(struct bpf_verifier_env *env) |
| 771 | { |
| 772 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
| 773 | struct bpf_insn *insn = env->prog->insnsi; |
| 774 | int insn_cnt = env->prog->len; |
| 775 | |
| 776 | /* determine subprog starts. The end is one before the next starts */ |
| 777 | for (i = 0; i < insn_cnt; i++) { |
| 778 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 779 | continue; |
| 780 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 781 | continue; |
| 782 | if (!env->allow_ptr_leaks) { |
| 783 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); |
| 784 | return -EPERM; |
| 785 | } |
| 786 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Colin Ian King | e90004d5 | 2017-12-18 14:03:12 +0000 | [diff] [blame] | 787 | verbose(env, "function calls in offloaded programs are not supported yet\n"); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 788 | return -EINVAL; |
| 789 | } |
| 790 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 791 | if (ret < 0) |
| 792 | return ret; |
| 793 | } |
| 794 | |
| 795 | if (env->log.level > 1) |
| 796 | for (i = 0; i < env->subprog_cnt; i++) |
| 797 | verbose(env, "func#%d @%d\n", i, env->subprog_starts[i]); |
| 798 | |
| 799 | /* now check that all jumps are within the same subprog */ |
| 800 | subprog_start = 0; |
| 801 | if (env->subprog_cnt == cur_subprog) |
| 802 | subprog_end = insn_cnt; |
| 803 | else |
| 804 | subprog_end = env->subprog_starts[cur_subprog++]; |
| 805 | for (i = 0; i < insn_cnt; i++) { |
| 806 | u8 code = insn[i].code; |
| 807 | |
| 808 | if (BPF_CLASS(code) != BPF_JMP) |
| 809 | goto next; |
| 810 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 811 | goto next; |
| 812 | off = i + insn[i].off + 1; |
| 813 | if (off < subprog_start || off >= subprog_end) { |
| 814 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 815 | return -EINVAL; |
| 816 | } |
| 817 | next: |
| 818 | if (i == subprog_end - 1) { |
| 819 | /* to avoid fall-through from one subprog into another |
| 820 | * the last insn of the subprog should be either exit |
| 821 | * or unconditional jump back |
| 822 | */ |
| 823 | if (code != (BPF_JMP | BPF_EXIT) && |
| 824 | code != (BPF_JMP | BPF_JA)) { |
| 825 | verbose(env, "last insn is not an exit or jmp\n"); |
| 826 | return -EINVAL; |
| 827 | } |
| 828 | subprog_start = subprog_end; |
| 829 | if (env->subprog_cnt == cur_subprog) |
| 830 | subprog_end = insn_cnt; |
| 831 | else |
| 832 | subprog_end = env->subprog_starts[cur_subprog++]; |
| 833 | } |
| 834 | } |
| 835 | return 0; |
| 836 | } |
| 837 | |
Colin Ian King | fa2d41a | 2017-12-18 17:47:07 +0000 | [diff] [blame] | 838 | static |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 839 | struct bpf_verifier_state *skip_callee(struct bpf_verifier_env *env, |
| 840 | const struct bpf_verifier_state *state, |
| 841 | struct bpf_verifier_state *parent, |
| 842 | u32 regno) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 843 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 844 | struct bpf_verifier_state *tmp = NULL; |
| 845 | |
| 846 | /* 'parent' could be a state of caller and |
| 847 | * 'state' could be a state of callee. In such case |
| 848 | * parent->curframe < state->curframe |
| 849 | * and it's ok for r1 - r5 registers |
| 850 | * |
| 851 | * 'parent' could be a callee's state after it bpf_exit-ed. |
| 852 | * In such case parent->curframe > state->curframe |
| 853 | * and it's ok for r0 only |
| 854 | */ |
| 855 | if (parent->curframe == state->curframe || |
| 856 | (parent->curframe < state->curframe && |
| 857 | regno >= BPF_REG_1 && regno <= BPF_REG_5) || |
| 858 | (parent->curframe > state->curframe && |
| 859 | regno == BPF_REG_0)) |
| 860 | return parent; |
| 861 | |
| 862 | if (parent->curframe > state->curframe && |
| 863 | regno >= BPF_REG_6) { |
| 864 | /* for callee saved regs we have to skip the whole chain |
| 865 | * of states that belong to callee and mark as LIVE_READ |
| 866 | * the registers before the call |
| 867 | */ |
| 868 | tmp = parent; |
| 869 | while (tmp && tmp->curframe != state->curframe) { |
| 870 | tmp = tmp->parent; |
| 871 | } |
| 872 | if (!tmp) |
| 873 | goto bug; |
| 874 | parent = tmp; |
| 875 | } else { |
| 876 | goto bug; |
| 877 | } |
| 878 | return parent; |
| 879 | bug: |
| 880 | verbose(env, "verifier bug regno %d tmp %p\n", regno, tmp); |
| 881 | verbose(env, "regno %d parent frame %d current frame %d\n", |
| 882 | regno, parent->curframe, state->curframe); |
Colin Ian King | fa2d41a | 2017-12-18 17:47:07 +0000 | [diff] [blame] | 883 | return NULL; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | static int mark_reg_read(struct bpf_verifier_env *env, |
| 887 | const struct bpf_verifier_state *state, |
| 888 | struct bpf_verifier_state *parent, |
| 889 | u32 regno) |
| 890 | { |
| 891 | bool writes = parent == state->parent; /* Observe write marks */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 892 | |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 893 | if (regno == BPF_REG_FP) |
| 894 | /* 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] | 895 | return 0; |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 896 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 897 | while (parent) { |
| 898 | /* if read wasn't screened by an earlier write ... */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 899 | if (writes && state->frame[state->curframe]->regs[regno].live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 900 | break; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 901 | parent = skip_callee(env, state, parent, regno); |
| 902 | if (!parent) |
| 903 | return -EFAULT; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 904 | /* ... then we depend on parent's value */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 905 | parent->frame[parent->curframe]->regs[regno].live |= REG_LIVE_READ; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 906 | state = parent; |
| 907 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 908 | writes = true; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 909 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 910 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 914 | enum reg_arg_type t) |
| 915 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 916 | struct bpf_verifier_state *vstate = env->cur_state; |
| 917 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 918 | struct bpf_reg_state *regs = state->regs; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 919 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 920 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 921 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 922 | return -EINVAL; |
| 923 | } |
| 924 | |
| 925 | if (t == SRC_OP) { |
| 926 | /* check whether register used as source operand can be read */ |
| 927 | if (regs[regno].type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 928 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 929 | return -EACCES; |
| 930 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 931 | return mark_reg_read(env, vstate, vstate->parent, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 932 | } else { |
| 933 | /* check whether register used as dest operand can be written to */ |
| 934 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 935 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 936 | return -EACCES; |
| 937 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 938 | regs[regno].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 939 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 940 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 941 | } |
| 942 | return 0; |
| 943 | } |
| 944 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 945 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 946 | { |
| 947 | switch (type) { |
| 948 | case PTR_TO_MAP_VALUE: |
| 949 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 950 | case PTR_TO_STACK: |
| 951 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 952 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 953 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 954 | case PTR_TO_PACKET_END: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 955 | case CONST_PTR_TO_MAP: |
| 956 | return true; |
| 957 | default: |
| 958 | return false; |
| 959 | } |
| 960 | } |
| 961 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 962 | /* Does this register contain a constant zero? */ |
| 963 | static bool register_is_null(struct bpf_reg_state *reg) |
| 964 | { |
| 965 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 966 | } |
| 967 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 968 | /* check_stack_read/write functions track spill/fill of registers, |
| 969 | * stack boundary and alignment are checked in check_mem_access() |
| 970 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 971 | static int check_stack_write(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 972 | struct bpf_func_state *state, /* func where register points to */ |
| 973 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 974 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 975 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 976 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 977 | enum bpf_reg_type type; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 978 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 979 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
| 980 | true); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 981 | if (err) |
| 982 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 983 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 984 | * so it's aligned access and [off, off + size) are within stack limits |
| 985 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 986 | if (!env->allow_ptr_leaks && |
| 987 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 988 | size != BPF_REG_SIZE) { |
| 989 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 990 | return -EACCES; |
| 991 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 992 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 993 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 994 | if (value_regno >= 0 && |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 995 | is_spillable_regtype((type = cur->regs[value_regno].type))) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 996 | |
| 997 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 998 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 999 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1000 | return -EACCES; |
| 1001 | } |
| 1002 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1003 | if (state != cur && type == PTR_TO_STACK) { |
| 1004 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 1005 | return -EINVAL; |
| 1006 | } |
| 1007 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1008 | /* save register state */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1009 | state->stack[spi].spilled_ptr = cur->regs[value_regno]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1010 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1011 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1012 | for (i = 0; i < BPF_REG_SIZE; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1013 | state->stack[spi].slot_type[i] = STACK_SPILL; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1014 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1015 | u8 type = STACK_MISC; |
| 1016 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1017 | /* regular write of data into stack */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1018 | state->stack[spi].spilled_ptr = (struct bpf_reg_state) {}; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1019 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1020 | /* only mark the slot as written if all 8 bytes were written |
| 1021 | * otherwise read propagation may incorrectly stop too soon |
| 1022 | * when stack slots are partially written. |
| 1023 | * This heuristic means that read propagation will be |
| 1024 | * conservative, since it will add reg_live_read marks |
| 1025 | * to stack slots all the way to first state when programs |
| 1026 | * writes+reads less than 8 bytes |
| 1027 | */ |
| 1028 | if (size == BPF_REG_SIZE) |
| 1029 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 1030 | |
| 1031 | /* when we zero initialize stack slots mark them as such */ |
| 1032 | if (value_regno >= 0 && |
| 1033 | register_is_null(&cur->regs[value_regno])) |
| 1034 | type = STACK_ZERO; |
| 1035 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1036 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1037 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1038 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1039 | } |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1043 | /* registers of every function are unique and mark_reg_read() propagates |
| 1044 | * the liveness in the following cases: |
| 1045 | * - from callee into caller for R1 - R5 that were used as arguments |
| 1046 | * - from caller into callee for R0 that used as result of the call |
| 1047 | * - from caller to the same caller skipping states of the callee for R6 - R9, |
| 1048 | * since R6 - R9 are callee saved by implicit function prologue and |
| 1049 | * caller's R6 != callee's R6, so when we propagate liveness up to |
| 1050 | * parent states we need to skip callee states for R6 - R9. |
| 1051 | * |
| 1052 | * stack slot marking is different, since stacks of caller and callee are |
| 1053 | * accessible in both (since caller can pass a pointer to caller's stack to |
| 1054 | * callee which can pass it to another function), hence mark_stack_slot_read() |
| 1055 | * has to propagate the stack liveness to all parent states at given frame number. |
| 1056 | * Consider code: |
| 1057 | * f1() { |
| 1058 | * ptr = fp - 8; |
| 1059 | * *ptr = ctx; |
| 1060 | * call f2 { |
| 1061 | * .. = *ptr; |
| 1062 | * } |
| 1063 | * .. = *ptr; |
| 1064 | * } |
| 1065 | * First *ptr is reading from f1's stack and mark_stack_slot_read() has |
| 1066 | * to mark liveness at the f1's frame and not f2's frame. |
| 1067 | * Second *ptr is also reading from f1's stack and mark_stack_slot_read() has |
| 1068 | * to propagate liveness to f2 states at f1's frame level and further into |
| 1069 | * f1 states at f1's frame level until write into that stack slot |
| 1070 | */ |
| 1071 | static void mark_stack_slot_read(struct bpf_verifier_env *env, |
| 1072 | const struct bpf_verifier_state *state, |
| 1073 | struct bpf_verifier_state *parent, |
| 1074 | int slot, int frameno) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1075 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1076 | bool writes = parent == state->parent; /* Observe write marks */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1077 | |
| 1078 | while (parent) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1079 | if (parent->frame[frameno]->allocated_stack <= slot * BPF_REG_SIZE) |
| 1080 | /* since LIVE_WRITTEN mark is only done for full 8-byte |
| 1081 | * write the read marks are conservative and parent |
| 1082 | * state may not even have the stack allocated. In such case |
| 1083 | * end the propagation, since the loop reached beginning |
| 1084 | * of the function |
| 1085 | */ |
| 1086 | break; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1087 | /* if read wasn't screened by an earlier write ... */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1088 | 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] | 1089 | break; |
| 1090 | /* ... then we depend on parent's value */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1091 | parent->frame[frameno]->stack[slot].spilled_ptr.live |= REG_LIVE_READ; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1092 | state = parent; |
| 1093 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1094 | writes = true; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1098 | static int check_stack_read(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1099 | struct bpf_func_state *reg_state /* func where register points to */, |
| 1100 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1101 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1102 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1103 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1104 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
| 1105 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1106 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1107 | if (reg_state->allocated_stack <= slot) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1108 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 1109 | off, size); |
| 1110 | return -EACCES; |
| 1111 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1112 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1113 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1114 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1115 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1116 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1117 | return -EACCES; |
| 1118 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1119 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1120 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1121 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1122 | return -EACCES; |
| 1123 | } |
| 1124 | } |
| 1125 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1126 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1127 | /* restore register state from stack */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1128 | state->regs[value_regno] = reg_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 1129 | /* mark reg as written since spilled pointer state likely |
| 1130 | * has its liveness marks cleared by is_state_visited() |
| 1131 | * which resets stack/reg liveness for state transitions |
| 1132 | */ |
| 1133 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1134 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1135 | mark_stack_slot_read(env, vstate, vstate->parent, spi, |
| 1136 | reg_state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1137 | return 0; |
| 1138 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1139 | int zeros = 0; |
| 1140 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1141 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1142 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 1143 | continue; |
| 1144 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 1145 | zeros++; |
| 1146 | continue; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1147 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1148 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 1149 | off, i, size); |
| 1150 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1151 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1152 | mark_stack_slot_read(env, vstate, vstate->parent, spi, |
| 1153 | reg_state->frameno); |
| 1154 | if (value_regno >= 0) { |
| 1155 | if (zeros == size) { |
| 1156 | /* any size read into register is zero extended, |
| 1157 | * so the whole register == const_zero |
| 1158 | */ |
| 1159 | __mark_reg_const_zero(&state->regs[value_regno]); |
| 1160 | } else { |
| 1161 | /* have read misc data from the stack */ |
| 1162 | mark_reg_unknown(env, state->regs, value_regno); |
| 1163 | } |
| 1164 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 1165 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1166 | return 0; |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1171 | 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] | 1172 | int size, bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1173 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1174 | struct bpf_reg_state *regs = cur_regs(env); |
| 1175 | struct bpf_map *map = regs[regno].map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1176 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1177 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1178 | off + size > map->value_size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1179 | 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] | 1180 | map->value_size, off, size); |
| 1181 | return -EACCES; |
| 1182 | } |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1186 | /* check read/write into a map element with possible variable offset */ |
| 1187 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1188 | int off, int size, bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1189 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1190 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1191 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1192 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 1193 | int err; |
| 1194 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1195 | /* We may have adjusted the register to this map value, so we |
| 1196 | * need to try adding each of min_value and max_value to off |
| 1197 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1198 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1199 | if (env->log.level) |
| 1200 | print_verifier_state(env, state); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1201 | /* The minimum value is only important with signed |
| 1202 | * comparisons where we can't assume the floor of a |
| 1203 | * value is 0. If we are using signed variables for our |
| 1204 | * index'es we need to make sure that whatever we use |
| 1205 | * will have a set floor within our range. |
| 1206 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1207 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1208 | 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] | 1209 | regno); |
| 1210 | return -EACCES; |
| 1211 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1212 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
| 1213 | zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1214 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1215 | verbose(env, "R%d min value is outside of the array range\n", |
| 1216 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1217 | return err; |
| 1218 | } |
| 1219 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1220 | /* If we haven't set a max value then we need to bail since we can't be |
| 1221 | * sure we won't do bad things. |
| 1222 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1223 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1224 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1225 | 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] | 1226 | regno); |
| 1227 | return -EACCES; |
| 1228 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1229 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
| 1230 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1231 | if (err) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1232 | verbose(env, "R%d max value is outside of the array range\n", |
| 1233 | regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1234 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1237 | #define MAX_PACKET_OFF 0xffff |
| 1238 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1239 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1240 | const struct bpf_call_arg_meta *meta, |
| 1241 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1242 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1243 | switch (env->prog->type) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1244 | case BPF_PROG_TYPE_LWT_IN: |
| 1245 | case BPF_PROG_TYPE_LWT_OUT: |
| 1246 | /* dst_input() and dst_output() can't write for now */ |
| 1247 | if (t == BPF_WRITE) |
| 1248 | return false; |
Alexander Alemayhu | 7e57fbb | 2017-02-14 00:02:35 +0100 | [diff] [blame] | 1249 | /* fallthrough */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1250 | case BPF_PROG_TYPE_SCHED_CLS: |
| 1251 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1252 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1253 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 1254 | case BPF_PROG_TYPE_SK_SKB: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1255 | if (meta) |
| 1256 | return meta->pkt_access; |
| 1257 | |
| 1258 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1259 | return true; |
| 1260 | default: |
| 1261 | return false; |
| 1262 | } |
| 1263 | } |
| 1264 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1265 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1266 | int off, int size, bool zero_size_allowed) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1267 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1268 | struct bpf_reg_state *regs = cur_regs(env); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1269 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1270 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1271 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1272 | (u64)off + size > reg->range) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1273 | 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] | 1274 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1275 | return -EACCES; |
| 1276 | } |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1280 | 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] | 1281 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1282 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1283 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1284 | struct bpf_reg_state *reg = ®s[regno]; |
| 1285 | int err; |
| 1286 | |
| 1287 | /* We may have added a variable offset to the packet pointer; but any |
| 1288 | * reg->range we have comes after that. We are only checking the fixed |
| 1289 | * offset. |
| 1290 | */ |
| 1291 | |
| 1292 | /* We don't allow negative numbers, because we aren't tracking enough |
| 1293 | * detail to prove they're safe. |
| 1294 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1295 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1296 | 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] | 1297 | regno); |
| 1298 | return -EACCES; |
| 1299 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1300 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1301 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1302 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1303 | return err; |
| 1304 | } |
| 1305 | return err; |
| 1306 | } |
| 1307 | |
| 1308 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1309 | 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] | 1310 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1311 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1312 | struct bpf_insn_access_aux info = { |
| 1313 | .reg_type = *reg_type, |
| 1314 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1315 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1316 | if (env->ops->is_valid_access && |
| 1317 | env->ops->is_valid_access(off, size, t, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1318 | /* A non zero info.ctx_field_size indicates that this field is a |
| 1319 | * candidate for later verifier transformation to load the whole |
| 1320 | * field and then apply a mask when accessed with a narrower |
| 1321 | * access than actual ctx access size. A zero info.ctx_field_size |
| 1322 | * will only allow for whole field access and rejects any other |
| 1323 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1324 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 1325 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1326 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1327 | 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] | 1328 | /* remember the offset of last byte accessed in ctx */ |
| 1329 | if (env->prog->aux->max_ctx_offset < off + size) |
| 1330 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1331 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 1332 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1333 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1334 | 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] | 1335 | return -EACCES; |
| 1336 | } |
| 1337 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1338 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 1339 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1340 | { |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1341 | if (allow_ptr_leaks) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1342 | return false; |
| 1343 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1344 | return reg->type != SCALAR_VALUE; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1347 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 1348 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1349 | return __is_pointer_value(env->allow_ptr_leaks, cur_regs(env) + regno); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1350 | } |
| 1351 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1352 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 1353 | { |
| 1354 | const struct bpf_reg_state *reg = cur_regs(env) + regno; |
| 1355 | |
| 1356 | return reg->type == PTR_TO_CTX; |
| 1357 | } |
| 1358 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1359 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 1360 | { |
| 1361 | const struct bpf_reg_state *reg = cur_regs(env) + regno; |
| 1362 | |
| 1363 | return type_is_pkt_pointer(reg->type); |
| 1364 | } |
| 1365 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1366 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 1367 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1368 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1369 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1370 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1371 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1372 | |
| 1373 | /* Byte size accesses are always allowed. */ |
| 1374 | if (!strict || size == 1) |
| 1375 | return 0; |
| 1376 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1377 | /* For platforms that do not have a Kconfig enabling |
| 1378 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 1379 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 1380 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 1381 | * to this code only in strict mode where we want to emulate |
| 1382 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 1383 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1384 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1385 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1386 | |
| 1387 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 1388 | if (!tnum_is_aligned(reg_off, size)) { |
| 1389 | char tn_buf[48]; |
| 1390 | |
| 1391 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1392 | verbose(env, |
| 1393 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1394 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1395 | return -EACCES; |
| 1396 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1397 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1398 | return 0; |
| 1399 | } |
| 1400 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1401 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 1402 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1403 | const char *pointer_desc, |
| 1404 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1405 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1406 | struct tnum reg_off; |
| 1407 | |
| 1408 | /* Byte size accesses are always allowed. */ |
| 1409 | if (!strict || size == 1) |
| 1410 | return 0; |
| 1411 | |
| 1412 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 1413 | if (!tnum_is_aligned(reg_off, size)) { |
| 1414 | char tn_buf[48]; |
| 1415 | |
| 1416 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1417 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1418 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1419 | return -EACCES; |
| 1420 | } |
| 1421 | |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1425 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1426 | const struct bpf_reg_state *reg, int off, |
| 1427 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1428 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1429 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1430 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1431 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1432 | switch (reg->type) { |
| 1433 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1434 | case PTR_TO_PACKET_META: |
| 1435 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 1436 | * right in front, treat it the very same way. |
| 1437 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1438 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1439 | case PTR_TO_MAP_VALUE: |
| 1440 | pointer_desc = "value "; |
| 1441 | break; |
| 1442 | case PTR_TO_CTX: |
| 1443 | pointer_desc = "context "; |
| 1444 | break; |
| 1445 | case PTR_TO_STACK: |
| 1446 | pointer_desc = "stack "; |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 1447 | /* The stack spill tracking logic in check_stack_write() |
| 1448 | * and check_stack_read() relies on stack accesses being |
| 1449 | * aligned. |
| 1450 | */ |
| 1451 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1452 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1453 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1454 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1455 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1456 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 1457 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1458 | } |
| 1459 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1460 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 1461 | const struct bpf_func_state *func, |
| 1462 | int off) |
| 1463 | { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1464 | u16 stack = env->subprog_stack_depth[func->subprogno]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1465 | |
| 1466 | if (stack >= -off) |
| 1467 | return 0; |
| 1468 | |
| 1469 | /* update known max for given subprogram */ |
| 1470 | env->subprog_stack_depth[func->subprogno] = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1471 | return 0; |
| 1472 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1473 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1474 | /* starting from main bpf function walk all instructions of the function |
| 1475 | * and recursively walk all callees that given function can call. |
| 1476 | * Ignore jump and exit insns. |
| 1477 | * Since recursion is prevented by check_cfg() this algorithm |
| 1478 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 1479 | */ |
| 1480 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 1481 | { |
| 1482 | int depth = 0, frame = 0, subprog = 0, i = 0, subprog_end; |
| 1483 | struct bpf_insn *insn = env->prog->insnsi; |
| 1484 | int insn_cnt = env->prog->len; |
| 1485 | int ret_insn[MAX_CALL_FRAMES]; |
| 1486 | int ret_prog[MAX_CALL_FRAMES]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1487 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1488 | process_func: |
| 1489 | /* round up to 32-bytes, since this is granularity |
| 1490 | * of interpreter stack size |
| 1491 | */ |
| 1492 | depth += round_up(max_t(u32, env->subprog_stack_depth[subprog], 1), 32); |
| 1493 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1494 | 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] | 1495 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1496 | return -EACCES; |
| 1497 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1498 | continue_func: |
| 1499 | if (env->subprog_cnt == subprog) |
| 1500 | subprog_end = insn_cnt; |
| 1501 | else |
| 1502 | subprog_end = env->subprog_starts[subprog]; |
| 1503 | for (; i < subprog_end; i++) { |
| 1504 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1505 | continue; |
| 1506 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1507 | continue; |
| 1508 | /* remember insn and function to return to */ |
| 1509 | ret_insn[frame] = i + 1; |
| 1510 | ret_prog[frame] = subprog; |
| 1511 | |
| 1512 | /* find the callee */ |
| 1513 | i = i + insn[i].imm + 1; |
| 1514 | subprog = find_subprog(env, i); |
| 1515 | if (subprog < 0) { |
| 1516 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1517 | i); |
| 1518 | return -EFAULT; |
| 1519 | } |
| 1520 | subprog++; |
| 1521 | frame++; |
| 1522 | if (frame >= MAX_CALL_FRAMES) { |
| 1523 | WARN_ONCE(1, "verifier bug. Call stack is too deep\n"); |
| 1524 | return -EFAULT; |
| 1525 | } |
| 1526 | goto process_func; |
| 1527 | } |
| 1528 | /* end of for() loop means the last insn of the 'subprog' |
| 1529 | * was reached. Doesn't matter whether it was JA or EXIT |
| 1530 | */ |
| 1531 | if (frame == 0) |
| 1532 | return 0; |
| 1533 | depth -= round_up(max_t(u32, env->subprog_stack_depth[subprog], 1), 32); |
| 1534 | frame--; |
| 1535 | i = ret_insn[frame]; |
| 1536 | subprog = ret_prog[frame]; |
| 1537 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1538 | } |
| 1539 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1540 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1541 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 1542 | const struct bpf_insn *insn, int idx) |
| 1543 | { |
| 1544 | int start = idx + insn->imm + 1, subprog; |
| 1545 | |
| 1546 | subprog = find_subprog(env, start); |
| 1547 | if (subprog < 0) { |
| 1548 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1549 | start); |
| 1550 | return -EFAULT; |
| 1551 | } |
| 1552 | subprog++; |
| 1553 | return env->subprog_stack_depth[subprog]; |
| 1554 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1555 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1556 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 1557 | /* truncate register to smaller size (in bytes) |
| 1558 | * must be called with size < BPF_REG_SIZE |
| 1559 | */ |
| 1560 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 1561 | { |
| 1562 | u64 mask; |
| 1563 | |
| 1564 | /* clear high bits in bit representation */ |
| 1565 | reg->var_off = tnum_cast(reg->var_off, size); |
| 1566 | |
| 1567 | /* fix arithmetic bounds */ |
| 1568 | mask = ((u64)1 << (size * 8)) - 1; |
| 1569 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 1570 | reg->umin_value &= mask; |
| 1571 | reg->umax_value &= mask; |
| 1572 | } else { |
| 1573 | reg->umin_value = 0; |
| 1574 | reg->umax_value = mask; |
| 1575 | } |
| 1576 | reg->smin_value = reg->umin_value; |
| 1577 | reg->smax_value = reg->umax_value; |
| 1578 | } |
| 1579 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1580 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 1581 | * if t==write, value_regno is a register which value is stored into memory |
| 1582 | * if t==read, value_regno is a register which will receive the value from memory |
| 1583 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 1584 | * if t==read && value_regno==-1, don't care what we read from memory |
| 1585 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1586 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 1587 | int off, int bpf_size, enum bpf_access_type t, |
| 1588 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1589 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1590 | struct bpf_reg_state *regs = cur_regs(env); |
| 1591 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1592 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1593 | int size, err = 0; |
| 1594 | |
| 1595 | size = bpf_size_to_bytes(bpf_size); |
| 1596 | if (size < 0) |
| 1597 | return size; |
| 1598 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1599 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1600 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1601 | if (err) |
| 1602 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1603 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1604 | /* for access checks, reg->off is just part of off */ |
| 1605 | off += reg->off; |
| 1606 | |
| 1607 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1608 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1609 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1610 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1611 | return -EACCES; |
| 1612 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1613 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1614 | err = check_map_access(env, regno, off, size, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1615 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1616 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1617 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1618 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1619 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 1620 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1621 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1622 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1623 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1624 | return -EACCES; |
| 1625 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1626 | /* ctx accesses must be at a fixed offset, so that we can |
| 1627 | * determine what type of data were returned. |
| 1628 | */ |
Jakub Kicinski | 28e33f9 | 2017-10-16 11:16:55 -0700 | [diff] [blame] | 1629 | if (reg->off) { |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 1630 | verbose(env, |
| 1631 | "dereference of modified ctx ptr R%d off=%d+%d, ctx+const is allowed, ctx+const+const is not\n", |
Jakub Kicinski | 28e33f9 | 2017-10-16 11:16:55 -0700 | [diff] [blame] | 1632 | regno, reg->off, off - reg->off); |
| 1633 | return -EACCES; |
| 1634 | } |
| 1635 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1636 | char tn_buf[48]; |
| 1637 | |
| 1638 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1639 | verbose(env, |
| 1640 | "variable ctx access var_off=%s off=%d size=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1641 | tn_buf, off, size); |
| 1642 | return -EACCES; |
| 1643 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1644 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1645 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1646 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1647 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 1648 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1649 | */ |
| 1650 | if (reg_type == SCALAR_VALUE) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1651 | mark_reg_unknown(env, regs, value_regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1652 | else |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1653 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1654 | value_regno); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1655 | regs[value_regno].id = 0; |
| 1656 | regs[value_regno].off = 0; |
| 1657 | regs[value_regno].range = 0; |
| 1658 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1659 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1660 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1661 | } else if (reg->type == PTR_TO_STACK) { |
| 1662 | /* stack accesses must be at a fixed offset, so that we can |
| 1663 | * determine what type of data were returned. |
| 1664 | * See check_stack_read(). |
| 1665 | */ |
| 1666 | if (!tnum_is_const(reg->var_off)) { |
| 1667 | char tn_buf[48]; |
| 1668 | |
| 1669 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1670 | verbose(env, "variable stack access var_off=%s off=%d size=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1671 | tn_buf, off, size); |
| 1672 | return -EACCES; |
| 1673 | } |
| 1674 | off += reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1675 | if (off >= 0 || off < -MAX_BPF_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1676 | verbose(env, "invalid stack off=%d size=%d\n", off, |
| 1677 | size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1678 | return -EACCES; |
| 1679 | } |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1680 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1681 | state = func(env, reg); |
| 1682 | err = update_stack_depth(env, state, off); |
| 1683 | if (err) |
| 1684 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1685 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1686 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1687 | err = check_stack_write(env, state, off, size, |
| 1688 | value_regno); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1689 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1690 | err = check_stack_read(env, state, off, size, |
| 1691 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1692 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1693 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1694 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1695 | return -EACCES; |
| 1696 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1697 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1698 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1699 | verbose(env, "R%d leaks addr into packet\n", |
| 1700 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1701 | return -EACCES; |
| 1702 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1703 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1704 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1705 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1706 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1707 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 1708 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1709 | return -EACCES; |
| 1710 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1711 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1712 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1713 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1714 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 1715 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1716 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1717 | return err; |
| 1718 | } |
| 1719 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1720 | 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] | 1721 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1722 | int err; |
| 1723 | |
| 1724 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 1725 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1726 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1727 | return -EINVAL; |
| 1728 | } |
| 1729 | |
| 1730 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1731 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1732 | if (err) |
| 1733 | return err; |
| 1734 | |
| 1735 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1736 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1737 | if (err) |
| 1738 | return err; |
| 1739 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1740 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1741 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1742 | return -EACCES; |
| 1743 | } |
| 1744 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1745 | if (is_ctx_reg(env, insn->dst_reg) || |
| 1746 | is_pkt_reg(env, insn->dst_reg)) { |
| 1747 | verbose(env, "BPF_XADD stores into R%d %s is not allowed\n", |
| 1748 | insn->dst_reg, is_ctx_reg(env, insn->dst_reg) ? |
| 1749 | "context" : "packet"); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 1750 | return -EACCES; |
| 1751 | } |
| 1752 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1753 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1754 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1755 | BPF_SIZE(insn->code), BPF_READ, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1756 | if (err) |
| 1757 | return err; |
| 1758 | |
| 1759 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1760 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 1761 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | /* when register 'regno' is passed into function that will read 'access_size' |
| 1765 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1766 | * and all elements of stack are initialized. |
| 1767 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 1768 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1769 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1770 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1771 | int access_size, bool zero_size_allowed, |
| 1772 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1773 | { |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1774 | struct bpf_reg_state *reg = cur_regs(env) + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1775 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1776 | int off, i, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1777 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1778 | if (reg->type != PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1779 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1780 | if (zero_size_allowed && access_size == 0 && |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1781 | register_is_null(reg)) |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1782 | return 0; |
| 1783 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1784 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1785 | reg_type_str[reg->type], |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1786 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1787 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1788 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1789 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1790 | /* Only allow fixed-offset stack reads */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1791 | if (!tnum_is_const(reg->var_off)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1792 | char tn_buf[48]; |
| 1793 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1794 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1795 | verbose(env, "invalid variable stack read R%d var_off=%s\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1796 | regno, tn_buf); |
Jann Horn | ea25f91 | 2017-12-18 20:11:57 -0800 | [diff] [blame] | 1797 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1798 | } |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1799 | off = reg->off + reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1800 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1801 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1802 | 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] | 1803 | regno, off, access_size); |
| 1804 | return -EACCES; |
| 1805 | } |
| 1806 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1807 | if (meta && meta->raw_mode) { |
| 1808 | meta->access_size = access_size; |
| 1809 | meta->regno = regno; |
| 1810 | return 0; |
| 1811 | } |
| 1812 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1813 | for (i = 0; i < access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1814 | u8 *stype; |
| 1815 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1816 | slot = -(off + i) - 1; |
| 1817 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1818 | if (state->allocated_stack <= slot) |
| 1819 | goto err; |
| 1820 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 1821 | if (*stype == STACK_MISC) |
| 1822 | goto mark; |
| 1823 | if (*stype == STACK_ZERO) { |
| 1824 | /* helper can write anything into the stack */ |
| 1825 | *stype = STACK_MISC; |
| 1826 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1827 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1828 | err: |
| 1829 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 1830 | off, i, access_size); |
| 1831 | return -EACCES; |
| 1832 | mark: |
| 1833 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 1834 | * the whole slot to be marked as 'read' |
| 1835 | */ |
| 1836 | mark_stack_slot_read(env, env->cur_state, env->cur_state->parent, |
| 1837 | spi, state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1838 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1839 | return update_stack_depth(env, state, off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1840 | } |
| 1841 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1842 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 1843 | int access_size, bool zero_size_allowed, |
| 1844 | struct bpf_call_arg_meta *meta) |
| 1845 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1846 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1847 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1848 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1849 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1850 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1851 | return check_packet_access(env, regno, reg->off, access_size, |
| 1852 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1853 | case PTR_TO_MAP_VALUE: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1854 | return check_map_access(env, regno, reg->off, access_size, |
| 1855 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1856 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1857 | return check_stack_boundary(env, regno, access_size, |
| 1858 | zero_size_allowed, meta); |
| 1859 | } |
| 1860 | } |
| 1861 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 1862 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 1863 | { |
| 1864 | return type == ARG_PTR_TO_MEM || |
| 1865 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 1866 | type == ARG_PTR_TO_UNINIT_MEM; |
| 1867 | } |
| 1868 | |
| 1869 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 1870 | { |
| 1871 | return type == ARG_CONST_SIZE || |
| 1872 | type == ARG_CONST_SIZE_OR_ZERO; |
| 1873 | } |
| 1874 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1875 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1876 | enum bpf_arg_type arg_type, |
| 1877 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1878 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1879 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1880 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1881 | int err = 0; |
| 1882 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1883 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1884 | return 0; |
| 1885 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1886 | err = check_reg_arg(env, regno, SRC_OP); |
| 1887 | if (err) |
| 1888 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1889 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1890 | if (arg_type == ARG_ANYTHING) { |
| 1891 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1892 | verbose(env, "R%d leaks addr into helper function\n", |
| 1893 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1894 | return -EACCES; |
| 1895 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1896 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1897 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1898 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1899 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1900 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1901 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1902 | return -EACCES; |
| 1903 | } |
| 1904 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1905 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1906 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1907 | expected_type = PTR_TO_STACK; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1908 | if (!type_is_pkt_pointer(type) && |
| 1909 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1910 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1911 | } else if (arg_type == ARG_CONST_SIZE || |
| 1912 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1913 | expected_type = SCALAR_VALUE; |
| 1914 | if (type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1915 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1916 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 1917 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1918 | if (type != expected_type) |
| 1919 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1920 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 1921 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1922 | if (type != expected_type) |
| 1923 | goto err_type; |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 1924 | } else if (arg_type_is_mem_ptr(arg_type)) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1925 | expected_type = PTR_TO_STACK; |
| 1926 | /* One exception here. In case function allows for NULL to be |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1927 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1928 | * happens during stack boundary checking. |
| 1929 | */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1930 | if (register_is_null(reg) && |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 1931 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1932 | /* final test in check_stack_boundary() */; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1933 | else if (!type_is_pkt_pointer(type) && |
| 1934 | type != PTR_TO_MAP_VALUE && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1935 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1936 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1937 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1938 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1939 | verbose(env, "unsupported arg_type %d\n", arg_type); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1940 | return -EFAULT; |
| 1941 | } |
| 1942 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1943 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 1944 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1945 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1946 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 1947 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 1948 | * check that [key, key + map->key_size) are within |
| 1949 | * stack limits and initialized |
| 1950 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1951 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1952 | /* in function declaration map_ptr must come before |
| 1953 | * map_key, so that it's verified and known before |
| 1954 | * we have to check map_key here. Otherwise it means |
| 1955 | * that kernel subsystem misconfigured verifier |
| 1956 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1957 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1958 | return -EACCES; |
| 1959 | } |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1960 | if (type_is_pkt_pointer(type)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1961 | err = check_packet_access(env, regno, reg->off, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1962 | meta->map_ptr->key_size, |
| 1963 | false); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1964 | else |
| 1965 | err = check_stack_boundary(env, regno, |
| 1966 | meta->map_ptr->key_size, |
| 1967 | false, NULL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1968 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1969 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 1970 | * check [value, value + map->value_size) validity |
| 1971 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1972 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1973 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1974 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1975 | return -EACCES; |
| 1976 | } |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1977 | if (type_is_pkt_pointer(type)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1978 | err = check_packet_access(env, regno, reg->off, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1979 | meta->map_ptr->value_size, |
| 1980 | false); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1981 | else |
| 1982 | err = check_stack_boundary(env, regno, |
| 1983 | meta->map_ptr->value_size, |
| 1984 | false, NULL); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 1985 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1986 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1987 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1988 | /* The register is SCALAR_VALUE; the access check |
| 1989 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1990 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1991 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1992 | /* For unprivileged variable accesses, disable raw |
| 1993 | * mode so that the program is required to |
| 1994 | * initialize all the memory that the helper could |
| 1995 | * just partially fill up. |
| 1996 | */ |
| 1997 | meta = NULL; |
| 1998 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1999 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2000 | 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] | 2001 | regno); |
| 2002 | return -EACCES; |
| 2003 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2004 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2005 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2006 | err = check_helper_mem_access(env, regno - 1, 0, |
| 2007 | zero_size_allowed, |
| 2008 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2009 | if (err) |
| 2010 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 2011 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2012 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2013 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2014 | 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] | 2015 | regno); |
| 2016 | return -EACCES; |
| 2017 | } |
| 2018 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2019 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2020 | zero_size_allowed, meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2024 | err_type: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2025 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2026 | reg_type_str[type], reg_type_str[expected_type]); |
| 2027 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2030 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 2031 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2032 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2033 | if (!map) |
| 2034 | return 0; |
| 2035 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2036 | /* We need a two way check, first is from map perspective ... */ |
| 2037 | switch (map->map_type) { |
| 2038 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 2039 | if (func_id != BPF_FUNC_tail_call) |
| 2040 | goto error; |
| 2041 | break; |
| 2042 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 2043 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2044 | func_id != BPF_FUNC_perf_event_output && |
| 2045 | func_id != BPF_FUNC_perf_event_read_value) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2046 | goto error; |
| 2047 | break; |
| 2048 | case BPF_MAP_TYPE_STACK_TRACE: |
| 2049 | if (func_id != BPF_FUNC_get_stackid) |
| 2050 | goto error; |
| 2051 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 2052 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 2053 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2054 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2055 | goto error; |
| 2056 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2057 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
| 2058 | * allow to be modified from bpf side. So do not allow lookup elements |
| 2059 | * for now. |
| 2060 | */ |
| 2061 | case BPF_MAP_TYPE_DEVMAP: |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 2062 | if (func_id != BPF_FUNC_redirect_map) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2063 | goto error; |
| 2064 | break; |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2065 | /* Restrict bpf side of cpumap, open when use-cases appear */ |
| 2066 | case BPF_MAP_TYPE_CPUMAP: |
| 2067 | if (func_id != BPF_FUNC_redirect_map) |
| 2068 | goto error; |
| 2069 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2070 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 2071 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2072 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 2073 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 2074 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2075 | case BPF_MAP_TYPE_SOCKMAP: |
| 2076 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 2077 | func_id != BPF_FUNC_sock_map_update && |
| 2078 | func_id != BPF_FUNC_map_delete_elem) |
| 2079 | goto error; |
| 2080 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2081 | default: |
| 2082 | break; |
| 2083 | } |
| 2084 | |
| 2085 | /* ... and second from the function itself. */ |
| 2086 | switch (func_id) { |
| 2087 | case BPF_FUNC_tail_call: |
| 2088 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 2089 | goto error; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2090 | if (env->subprog_cnt) { |
| 2091 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
| 2092 | return -EINVAL; |
| 2093 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2094 | break; |
| 2095 | case BPF_FUNC_perf_event_read: |
| 2096 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2097 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2098 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 2099 | goto error; |
| 2100 | break; |
| 2101 | case BPF_FUNC_get_stackid: |
| 2102 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 2103 | goto error; |
| 2104 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2105 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 2106 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2107 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 2108 | goto error; |
| 2109 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2110 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 2111 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
| 2112 | map->map_type != BPF_MAP_TYPE_CPUMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2113 | goto error; |
| 2114 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2115 | case BPF_FUNC_sk_redirect_map: |
| 2116 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 2117 | goto error; |
| 2118 | break; |
| 2119 | case BPF_FUNC_sock_map_update: |
| 2120 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 2121 | goto error; |
| 2122 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2123 | default: |
| 2124 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2128 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2129 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2130 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2131 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2134 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2135 | { |
| 2136 | int count = 0; |
| 2137 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2138 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2139 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2140 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2141 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2142 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2143 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2144 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2145 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2146 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2147 | count++; |
| 2148 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2149 | /* We only support one arg being in raw mode at the moment, |
| 2150 | * which is sufficient for the helper functions we have |
| 2151 | * right now. |
| 2152 | */ |
| 2153 | return count <= 1; |
| 2154 | } |
| 2155 | |
| 2156 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 2157 | enum bpf_arg_type arg_next) |
| 2158 | { |
| 2159 | return (arg_type_is_mem_ptr(arg_curr) && |
| 2160 | !arg_type_is_mem_size(arg_next)) || |
| 2161 | (!arg_type_is_mem_ptr(arg_curr) && |
| 2162 | arg_type_is_mem_size(arg_next)); |
| 2163 | } |
| 2164 | |
| 2165 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 2166 | { |
| 2167 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 2168 | * bytes from memory 'buf'. Both arg types need |
| 2169 | * to be paired, so make sure there's no buggy |
| 2170 | * helper function specification. |
| 2171 | */ |
| 2172 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 2173 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 2174 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 2175 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 2176 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 2177 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 2178 | return false; |
| 2179 | |
| 2180 | return true; |
| 2181 | } |
| 2182 | |
| 2183 | static int check_func_proto(const struct bpf_func_proto *fn) |
| 2184 | { |
| 2185 | return check_raw_mode_ok(fn) && |
| 2186 | check_arg_pair_ok(fn) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2187 | } |
| 2188 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2189 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 2190 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2191 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2192 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 2193 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2194 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2195 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2196 | int i; |
| 2197 | |
| 2198 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2199 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2200 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2201 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2202 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 2203 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2204 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2205 | reg = &state->stack[i].spilled_ptr; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2206 | if (reg_is_pkt_pointer_any(reg)) |
| 2207 | __mark_reg_unknown(reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2208 | } |
| 2209 | } |
| 2210 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2211 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 2212 | { |
| 2213 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2214 | int i; |
| 2215 | |
| 2216 | for (i = 0; i <= vstate->curframe; i++) |
| 2217 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 2218 | } |
| 2219 | |
| 2220 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 2221 | int *insn_idx) |
| 2222 | { |
| 2223 | struct bpf_verifier_state *state = env->cur_state; |
| 2224 | struct bpf_func_state *caller, *callee; |
| 2225 | int i, subprog, target_insn; |
| 2226 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 2227 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2228 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 2229 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2230 | return -E2BIG; |
| 2231 | } |
| 2232 | |
| 2233 | target_insn = *insn_idx + insn->imm; |
| 2234 | subprog = find_subprog(env, target_insn + 1); |
| 2235 | if (subprog < 0) { |
| 2236 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 2237 | target_insn + 1); |
| 2238 | return -EFAULT; |
| 2239 | } |
| 2240 | |
| 2241 | caller = state->frame[state->curframe]; |
| 2242 | if (state->frame[state->curframe + 1]) { |
| 2243 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 2244 | state->curframe + 1); |
| 2245 | return -EFAULT; |
| 2246 | } |
| 2247 | |
| 2248 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 2249 | if (!callee) |
| 2250 | return -ENOMEM; |
| 2251 | state->frame[state->curframe + 1] = callee; |
| 2252 | |
| 2253 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 2254 | * into its own stack before reading from it. |
| 2255 | * callee can read/write into caller's stack |
| 2256 | */ |
| 2257 | init_func_state(env, callee, |
| 2258 | /* remember the callsite, it will be used by bpf_exit */ |
| 2259 | *insn_idx /* callsite */, |
| 2260 | state->curframe + 1 /* frameno within this callchain */, |
| 2261 | subprog + 1 /* subprog number within this prog */); |
| 2262 | |
| 2263 | /* copy r1 - r5 args that callee can access */ |
| 2264 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 2265 | callee->regs[i] = caller->regs[i]; |
| 2266 | |
| 2267 | /* after the call regsiters r0 - r5 were scratched */ |
| 2268 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 2269 | mark_reg_not_init(env, caller->regs, caller_saved[i]); |
| 2270 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2271 | } |
| 2272 | |
| 2273 | /* only increment it after check_reg_arg() finished */ |
| 2274 | state->curframe++; |
| 2275 | |
| 2276 | /* and go analyze first insn of the callee */ |
| 2277 | *insn_idx = target_insn; |
| 2278 | |
| 2279 | if (env->log.level) { |
| 2280 | verbose(env, "caller:\n"); |
| 2281 | print_verifier_state(env, caller); |
| 2282 | verbose(env, "callee:\n"); |
| 2283 | print_verifier_state(env, callee); |
| 2284 | } |
| 2285 | return 0; |
| 2286 | } |
| 2287 | |
| 2288 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 2289 | { |
| 2290 | struct bpf_verifier_state *state = env->cur_state; |
| 2291 | struct bpf_func_state *caller, *callee; |
| 2292 | struct bpf_reg_state *r0; |
| 2293 | |
| 2294 | callee = state->frame[state->curframe]; |
| 2295 | r0 = &callee->regs[BPF_REG_0]; |
| 2296 | if (r0->type == PTR_TO_STACK) { |
| 2297 | /* technically it's ok to return caller's stack pointer |
| 2298 | * (or caller's caller's pointer) back to the caller, |
| 2299 | * since these pointers are valid. Only current stack |
| 2300 | * pointer will be invalid as soon as function exits, |
| 2301 | * but let's be conservative |
| 2302 | */ |
| 2303 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 2304 | return -EINVAL; |
| 2305 | } |
| 2306 | |
| 2307 | state->curframe--; |
| 2308 | caller = state->frame[state->curframe]; |
| 2309 | /* return to the caller whatever r0 had in the callee */ |
| 2310 | caller->regs[BPF_REG_0] = *r0; |
| 2311 | |
| 2312 | *insn_idx = callee->callsite + 1; |
| 2313 | if (env->log.level) { |
| 2314 | verbose(env, "returning from callee:\n"); |
| 2315 | print_verifier_state(env, callee); |
| 2316 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 2317 | print_verifier_state(env, caller); |
| 2318 | } |
| 2319 | /* clear everything in the callee */ |
| 2320 | free_func_state(callee); |
| 2321 | state->frame[state->curframe + 1] = NULL; |
| 2322 | return 0; |
| 2323 | } |
| 2324 | |
| 2325 | 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] | 2326 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2327 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2328 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2329 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2330 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2331 | int i, err; |
| 2332 | |
| 2333 | /* find function prototype */ |
| 2334 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2335 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 2336 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2337 | return -EINVAL; |
| 2338 | } |
| 2339 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 2340 | if (env->ops->get_func_proto) |
| 2341 | fn = env->ops->get_func_proto(func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2342 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2343 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 2344 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2345 | return -EINVAL; |
| 2346 | } |
| 2347 | |
| 2348 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 2349 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2350 | verbose(env, "cannot call GPL only function from proprietary program\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2351 | return -EINVAL; |
| 2352 | } |
| 2353 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 2354 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 2355 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 2356 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 2357 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 2358 | func_id_name(func_id), func_id); |
| 2359 | return -EINVAL; |
| 2360 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2361 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2362 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2363 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2364 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2365 | err = check_func_proto(fn); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2366 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2367 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2368 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2369 | return err; |
| 2370 | } |
| 2371 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2372 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2373 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2374 | if (err) |
| 2375 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2376 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2377 | if (err) |
| 2378 | return err; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 2379 | if (func_id == BPF_FUNC_tail_call) { |
| 2380 | if (meta.map_ptr == NULL) { |
| 2381 | verbose(env, "verifier bug\n"); |
| 2382 | return -EINVAL; |
| 2383 | } |
| 2384 | env->insn_aux_data[insn_idx].map_ptr = meta.map_ptr; |
| 2385 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2386 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2387 | if (err) |
| 2388 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2389 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2390 | if (err) |
| 2391 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2392 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2393 | if (err) |
| 2394 | return err; |
| 2395 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2396 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 2397 | * is inferred from register state. |
| 2398 | */ |
| 2399 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 2400 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 2401 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2402 | if (err) |
| 2403 | return err; |
| 2404 | } |
| 2405 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2406 | regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2407 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2408 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2409 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2410 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2411 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2412 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2413 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2414 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2415 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2416 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2417 | } else if (fn->ret_type == RET_VOID) { |
| 2418 | regs[BPF_REG_0].type = NOT_INIT; |
| 2419 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) { |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 2420 | struct bpf_insn_aux_data *insn_aux; |
| 2421 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2422 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2423 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2424 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2425 | regs[BPF_REG_0].off = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2426 | /* remember map_ptr, so that check_map_access() |
| 2427 | * can check 'value_size' boundary of memory access |
| 2428 | * to map element returned from bpf_map_lookup_elem() |
| 2429 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2430 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2431 | verbose(env, |
| 2432 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2433 | return -EINVAL; |
| 2434 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2435 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2436 | regs[BPF_REG_0].id = ++env->id_gen; |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 2437 | insn_aux = &env->insn_aux_data[insn_idx]; |
| 2438 | if (!insn_aux->map_ptr) |
| 2439 | insn_aux->map_ptr = meta.map_ptr; |
| 2440 | else if (insn_aux->map_ptr != meta.map_ptr) |
| 2441 | insn_aux->map_ptr = BPF_MAP_PTR_POISON; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2442 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2443 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2444 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2445 | return -EINVAL; |
| 2446 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2447 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2448 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2449 | if (err) |
| 2450 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2451 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2452 | if (changes_data) |
| 2453 | clear_all_pkt_pointers(env); |
| 2454 | return 0; |
| 2455 | } |
| 2456 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2457 | static bool signed_add_overflows(s64 a, s64 b) |
| 2458 | { |
| 2459 | /* Do the add in u64, where overflow is well-defined */ |
| 2460 | s64 res = (s64)((u64)a + (u64)b); |
| 2461 | |
| 2462 | if (b < 0) |
| 2463 | return res > a; |
| 2464 | return res < a; |
| 2465 | } |
| 2466 | |
| 2467 | static bool signed_sub_overflows(s64 a, s64 b) |
| 2468 | { |
| 2469 | /* Do the sub in u64, where overflow is well-defined */ |
| 2470 | s64 res = (s64)((u64)a - (u64)b); |
| 2471 | |
| 2472 | if (b < 0) |
| 2473 | return res < a; |
| 2474 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2477 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 2478 | const struct bpf_reg_state *reg, |
| 2479 | enum bpf_reg_type type) |
| 2480 | { |
| 2481 | bool known = tnum_is_const(reg->var_off); |
| 2482 | s64 val = reg->var_off.value; |
| 2483 | s64 smin = reg->smin_value; |
| 2484 | |
| 2485 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 2486 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 2487 | reg_type_str[type], val); |
| 2488 | return false; |
| 2489 | } |
| 2490 | |
| 2491 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 2492 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 2493 | reg_type_str[type], reg->off); |
| 2494 | return false; |
| 2495 | } |
| 2496 | |
| 2497 | if (smin == S64_MIN) { |
| 2498 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 2499 | reg_type_str[type]); |
| 2500 | return false; |
| 2501 | } |
| 2502 | |
| 2503 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 2504 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 2505 | smin, reg_type_str[type]); |
| 2506 | return false; |
| 2507 | } |
| 2508 | |
| 2509 | return true; |
| 2510 | } |
| 2511 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2512 | /* 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] | 2513 | * Caller should also handle BPF_MOV case separately. |
| 2514 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 2515 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 2516 | */ |
| 2517 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 2518 | struct bpf_insn *insn, |
| 2519 | const struct bpf_reg_state *ptr_reg, |
| 2520 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2521 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2522 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2523 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2524 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2525 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2526 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 2527 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 2528 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 2529 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2530 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2531 | u32 dst = insn->dst_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2532 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2533 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2534 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 2535 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 2536 | smin_val > smax_val || umin_val > umax_val) { |
| 2537 | /* Taint dst register if offset had invalid bounds derived from |
| 2538 | * e.g. dead branches. |
| 2539 | */ |
| 2540 | __mark_reg_unknown(dst_reg); |
| 2541 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2542 | } |
| 2543 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2544 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 2545 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2546 | verbose(env, |
| 2547 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 2548 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2549 | return -EACCES; |
| 2550 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2551 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2552 | if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2553 | verbose(env, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n", |
| 2554 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2555 | return -EACCES; |
| 2556 | } |
| 2557 | if (ptr_reg->type == CONST_PTR_TO_MAP) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2558 | verbose(env, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n", |
| 2559 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2560 | return -EACCES; |
| 2561 | } |
| 2562 | if (ptr_reg->type == PTR_TO_PACKET_END) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2563 | verbose(env, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n", |
| 2564 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2565 | return -EACCES; |
| 2566 | } |
| 2567 | |
| 2568 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 2569 | * 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] | 2570 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2571 | dst_reg->type = ptr_reg->type; |
| 2572 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 2573 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2574 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 2575 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 2576 | return -EINVAL; |
| 2577 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2578 | switch (opcode) { |
| 2579 | case BPF_ADD: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2580 | /* We can take a fixed offset as long as it doesn't overflow |
| 2581 | * the s32 'off' field |
| 2582 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2583 | if (known && (ptr_reg->off + smin_val == |
| 2584 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2585 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2586 | dst_reg->smin_value = smin_ptr; |
| 2587 | dst_reg->smax_value = smax_ptr; |
| 2588 | dst_reg->umin_value = umin_ptr; |
| 2589 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2590 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2591 | dst_reg->off = ptr_reg->off + smin_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2592 | dst_reg->range = ptr_reg->range; |
| 2593 | break; |
| 2594 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2595 | /* A new variable offset is created. Note that off_reg->off |
| 2596 | * == 0, since it's a scalar. |
| 2597 | * dst_reg gets the pointer type and since some positive |
| 2598 | * integer value was added to the pointer, give it a new 'id' |
| 2599 | * if it's a PTR_TO_PACKET. |
| 2600 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 2601 | * added into the variable offset, and we copy the fixed offset |
| 2602 | * from ptr_reg. |
| 2603 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2604 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 2605 | signed_add_overflows(smax_ptr, smax_val)) { |
| 2606 | dst_reg->smin_value = S64_MIN; |
| 2607 | dst_reg->smax_value = S64_MAX; |
| 2608 | } else { |
| 2609 | dst_reg->smin_value = smin_ptr + smin_val; |
| 2610 | dst_reg->smax_value = smax_ptr + smax_val; |
| 2611 | } |
| 2612 | if (umin_ptr + umin_val < umin_ptr || |
| 2613 | umax_ptr + umax_val < umax_ptr) { |
| 2614 | dst_reg->umin_value = 0; |
| 2615 | dst_reg->umax_value = U64_MAX; |
| 2616 | } else { |
| 2617 | dst_reg->umin_value = umin_ptr + umin_val; |
| 2618 | dst_reg->umax_value = umax_ptr + umax_val; |
| 2619 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2620 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 2621 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2622 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2623 | dst_reg->id = ++env->id_gen; |
| 2624 | /* something was added to pkt_ptr, set range to zero */ |
| 2625 | dst_reg->range = 0; |
| 2626 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2627 | break; |
| 2628 | case BPF_SUB: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2629 | if (dst_reg == off_reg) { |
| 2630 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2631 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 2632 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2633 | return -EACCES; |
| 2634 | } |
| 2635 | /* We don't allow subtraction from FP, because (according to |
| 2636 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 2637 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 2638 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2639 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2640 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 2641 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2642 | return -EACCES; |
| 2643 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2644 | if (known && (ptr_reg->off - smin_val == |
| 2645 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2646 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2647 | dst_reg->smin_value = smin_ptr; |
| 2648 | dst_reg->smax_value = smax_ptr; |
| 2649 | dst_reg->umin_value = umin_ptr; |
| 2650 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2651 | dst_reg->var_off = ptr_reg->var_off; |
| 2652 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2653 | dst_reg->off = ptr_reg->off - smin_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2654 | dst_reg->range = ptr_reg->range; |
| 2655 | break; |
| 2656 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2657 | /* A new variable offset is created. If the subtrahend is known |
| 2658 | * nonnegative, then any reg->range we had before is still good. |
| 2659 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2660 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 2661 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 2662 | /* Overflow possible, we know nothing */ |
| 2663 | dst_reg->smin_value = S64_MIN; |
| 2664 | dst_reg->smax_value = S64_MAX; |
| 2665 | } else { |
| 2666 | dst_reg->smin_value = smin_ptr - smax_val; |
| 2667 | dst_reg->smax_value = smax_ptr - smin_val; |
| 2668 | } |
| 2669 | if (umin_ptr < umax_val) { |
| 2670 | /* Overflow possible, we know nothing */ |
| 2671 | dst_reg->umin_value = 0; |
| 2672 | dst_reg->umax_value = U64_MAX; |
| 2673 | } else { |
| 2674 | /* Cannot overflow (as long as bounds are consistent) */ |
| 2675 | dst_reg->umin_value = umin_ptr - umax_val; |
| 2676 | dst_reg->umax_value = umax_ptr - umin_val; |
| 2677 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2678 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 2679 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2680 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2681 | dst_reg->id = ++env->id_gen; |
| 2682 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2683 | if (smin_val < 0) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2684 | dst_reg->range = 0; |
| 2685 | } |
| 2686 | break; |
| 2687 | case BPF_AND: |
| 2688 | case BPF_OR: |
| 2689 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2690 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 2691 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 2692 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2693 | return -EACCES; |
| 2694 | default: |
| 2695 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2696 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 2697 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2698 | return -EACCES; |
| 2699 | } |
| 2700 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2701 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 2702 | return -EINVAL; |
| 2703 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2704 | __update_reg_bounds(dst_reg); |
| 2705 | __reg_deduce_bounds(dst_reg); |
| 2706 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2707 | return 0; |
| 2708 | } |
| 2709 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 2710 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 2711 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 2712 | * need extra checks in the 32-bit case. |
| 2713 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2714 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 2715 | struct bpf_insn *insn, |
| 2716 | struct bpf_reg_state *dst_reg, |
| 2717 | struct bpf_reg_state src_reg) |
| 2718 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2719 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2720 | u8 opcode = BPF_OP(insn->code); |
| 2721 | bool src_known, dst_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2722 | s64 smin_val, smax_val; |
| 2723 | u64 umin_val, umax_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 2724 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2725 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2726 | smin_val = src_reg.smin_value; |
| 2727 | smax_val = src_reg.smax_value; |
| 2728 | umin_val = src_reg.umin_value; |
| 2729 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2730 | src_known = tnum_is_const(src_reg.var_off); |
| 2731 | dst_known = tnum_is_const(dst_reg->var_off); |
| 2732 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 2733 | if ((src_known && (smin_val != smax_val || umin_val != umax_val)) || |
| 2734 | smin_val > smax_val || umin_val > umax_val) { |
| 2735 | /* Taint dst register if offset had invalid bounds derived from |
| 2736 | * e.g. dead branches. |
| 2737 | */ |
| 2738 | __mark_reg_unknown(dst_reg); |
| 2739 | return 0; |
| 2740 | } |
| 2741 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2742 | if (!src_known && |
| 2743 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
| 2744 | __mark_reg_unknown(dst_reg); |
| 2745 | return 0; |
| 2746 | } |
| 2747 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2748 | switch (opcode) { |
| 2749 | case BPF_ADD: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2750 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 2751 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 2752 | dst_reg->smin_value = S64_MIN; |
| 2753 | dst_reg->smax_value = S64_MAX; |
| 2754 | } else { |
| 2755 | dst_reg->smin_value += smin_val; |
| 2756 | dst_reg->smax_value += smax_val; |
| 2757 | } |
| 2758 | if (dst_reg->umin_value + umin_val < umin_val || |
| 2759 | dst_reg->umax_value + umax_val < umax_val) { |
| 2760 | dst_reg->umin_value = 0; |
| 2761 | dst_reg->umax_value = U64_MAX; |
| 2762 | } else { |
| 2763 | dst_reg->umin_value += umin_val; |
| 2764 | dst_reg->umax_value += umax_val; |
| 2765 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2766 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 2767 | break; |
| 2768 | case BPF_SUB: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2769 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 2770 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 2771 | /* Overflow possible, we know nothing */ |
| 2772 | dst_reg->smin_value = S64_MIN; |
| 2773 | dst_reg->smax_value = S64_MAX; |
| 2774 | } else { |
| 2775 | dst_reg->smin_value -= smax_val; |
| 2776 | dst_reg->smax_value -= smin_val; |
| 2777 | } |
| 2778 | if (dst_reg->umin_value < umax_val) { |
| 2779 | /* Overflow possible, we know nothing */ |
| 2780 | dst_reg->umin_value = 0; |
| 2781 | dst_reg->umax_value = U64_MAX; |
| 2782 | } else { |
| 2783 | /* Cannot overflow (as long as bounds are consistent) */ |
| 2784 | dst_reg->umin_value -= umax_val; |
| 2785 | dst_reg->umax_value -= umin_val; |
| 2786 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2787 | 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] | 2788 | break; |
| 2789 | case BPF_MUL: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2790 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 2791 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2792 | /* Ain't nobody got time to multiply that sign */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2793 | __mark_reg_unbounded(dst_reg); |
| 2794 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2795 | break; |
| 2796 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2797 | /* Both values are positive, so we can work with unsigned and |
| 2798 | * copy the result to signed (unless it exceeds S64_MAX). |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2799 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2800 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 2801 | /* Potential overflow, we know nothing */ |
| 2802 | __mark_reg_unbounded(dst_reg); |
| 2803 | /* (except what we can learn from the var_off) */ |
| 2804 | __update_reg_bounds(dst_reg); |
| 2805 | break; |
| 2806 | } |
| 2807 | dst_reg->umin_value *= umin_val; |
| 2808 | dst_reg->umax_value *= umax_val; |
| 2809 | if (dst_reg->umax_value > S64_MAX) { |
| 2810 | /* Overflow possible, we know nothing */ |
| 2811 | dst_reg->smin_value = S64_MIN; |
| 2812 | dst_reg->smax_value = S64_MAX; |
| 2813 | } else { |
| 2814 | dst_reg->smin_value = dst_reg->umin_value; |
| 2815 | dst_reg->smax_value = dst_reg->umax_value; |
| 2816 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2817 | break; |
| 2818 | case BPF_AND: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2819 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2820 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 2821 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2822 | break; |
| 2823 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2824 | /* We get our minimum from the var_off, since that's inherently |
| 2825 | * bitwise. Our maximum is the minimum of the operands' maxima. |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 2826 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2827 | 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] | 2828 | dst_reg->umin_value = dst_reg->var_off.value; |
| 2829 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 2830 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 2831 | /* Lose signed bounds when ANDing negative numbers, |
| 2832 | * ain't nobody got time for that. |
| 2833 | */ |
| 2834 | dst_reg->smin_value = S64_MIN; |
| 2835 | dst_reg->smax_value = S64_MAX; |
| 2836 | } else { |
| 2837 | /* ANDing two positives gives a positive, so safe to |
| 2838 | * cast result into s64. |
| 2839 | */ |
| 2840 | dst_reg->smin_value = dst_reg->umin_value; |
| 2841 | dst_reg->smax_value = dst_reg->umax_value; |
| 2842 | } |
| 2843 | /* We may learn something more from the var_off */ |
| 2844 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2845 | break; |
| 2846 | case BPF_OR: |
| 2847 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2848 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
| 2849 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2850 | break; |
| 2851 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2852 | /* We get our maximum from the var_off, and our minimum is the |
| 2853 | * maximum of the operands' minima |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2854 | */ |
| 2855 | 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] | 2856 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 2857 | dst_reg->umax_value = dst_reg->var_off.value | |
| 2858 | dst_reg->var_off.mask; |
| 2859 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 2860 | /* Lose signed bounds when ORing negative numbers, |
| 2861 | * ain't nobody got time for that. |
| 2862 | */ |
| 2863 | dst_reg->smin_value = S64_MIN; |
| 2864 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2865 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2866 | /* ORing two positives gives a positive, so safe to |
| 2867 | * cast result into s64. |
| 2868 | */ |
| 2869 | dst_reg->smin_value = dst_reg->umin_value; |
| 2870 | dst_reg->smax_value = dst_reg->umax_value; |
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 | /* We may learn something more from the var_off */ |
| 2873 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2874 | break; |
| 2875 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 2876 | if (umax_val >= insn_bitness) { |
| 2877 | /* Shifts greater than 31 or 63 are undefined. |
| 2878 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2879 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2880 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2881 | break; |
| 2882 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2883 | /* We lose all sign bit information (except what we can pick |
| 2884 | * up from var_off) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2885 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2886 | dst_reg->smin_value = S64_MIN; |
| 2887 | dst_reg->smax_value = S64_MAX; |
| 2888 | /* If we might shift our top bit out, then we know nothing */ |
| 2889 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 2890 | dst_reg->umin_value = 0; |
| 2891 | dst_reg->umax_value = U64_MAX; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2892 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2893 | dst_reg->umin_value <<= umin_val; |
| 2894 | dst_reg->umax_value <<= umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2895 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2896 | if (src_known) |
| 2897 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
| 2898 | else |
| 2899 | dst_reg->var_off = tnum_lshift(tnum_unknown, umin_val); |
| 2900 | /* We may learn something more from the var_off */ |
| 2901 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2902 | break; |
| 2903 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 2904 | if (umax_val >= insn_bitness) { |
| 2905 | /* Shifts greater than 31 or 63 are undefined. |
| 2906 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2907 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2908 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2909 | break; |
| 2910 | } |
Edward Cree | 4374f25 | 2017-12-18 20:11:53 -0800 | [diff] [blame] | 2911 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 2912 | * be negative, then either: |
| 2913 | * 1) src_reg might be zero, so the sign bit of the result is |
| 2914 | * unknown, so we lose our signed bounds |
| 2915 | * 2) it's known negative, thus the unsigned bounds capture the |
| 2916 | * signed bounds |
| 2917 | * 3) the signed bounds cross zero, so they tell us nothing |
| 2918 | * about the result |
| 2919 | * If the value in dst_reg is known nonnegative, then again the |
| 2920 | * unsigned bounts capture the signed bounds. |
| 2921 | * Thus, in all cases it suffices to blow away our signed bounds |
| 2922 | * and rely on inferring new ones from the unsigned bounds and |
| 2923 | * var_off of the result. |
| 2924 | */ |
| 2925 | dst_reg->smin_value = S64_MIN; |
| 2926 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2927 | if (src_known) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2928 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, |
| 2929 | umin_val); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2930 | else |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2931 | dst_reg->var_off = tnum_rshift(tnum_unknown, umin_val); |
| 2932 | dst_reg->umin_value >>= umax_val; |
| 2933 | dst_reg->umax_value >>= umin_val; |
| 2934 | /* We may learn something more from the var_off */ |
| 2935 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2936 | break; |
| 2937 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2938 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2939 | break; |
| 2940 | } |
| 2941 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 2942 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 2943 | /* 32-bit ALU ops are (32,32)->32 */ |
| 2944 | coerce_reg_to_size(dst_reg, 4); |
| 2945 | coerce_reg_to_size(&src_reg, 4); |
| 2946 | } |
| 2947 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2948 | __reg_deduce_bounds(dst_reg); |
| 2949 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2950 | return 0; |
| 2951 | } |
| 2952 | |
| 2953 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 2954 | * and var_off. |
| 2955 | */ |
| 2956 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 2957 | struct bpf_insn *insn) |
| 2958 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2959 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2960 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2961 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2962 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 2963 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2964 | |
| 2965 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2966 | src_reg = NULL; |
| 2967 | if (dst_reg->type != SCALAR_VALUE) |
| 2968 | ptr_reg = dst_reg; |
| 2969 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2970 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2971 | if (src_reg->type != SCALAR_VALUE) { |
| 2972 | if (dst_reg->type != SCALAR_VALUE) { |
| 2973 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2974 | * an arbitrary scalar. Disallow all math except |
| 2975 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2976 | */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2977 | if (opcode == BPF_SUB){ |
| 2978 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 2979 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2980 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2981 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 2982 | insn->dst_reg, |
| 2983 | bpf_alu_string[opcode >> 4]); |
| 2984 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2985 | } else { |
| 2986 | /* scalar += pointer |
| 2987 | * This is legal, but we have to reverse our |
| 2988 | * src/dest handling in computing the range |
| 2989 | */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2990 | return adjust_ptr_min_max_vals(env, insn, |
| 2991 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2992 | } |
| 2993 | } else if (ptr_reg) { |
| 2994 | /* pointer += scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2995 | return adjust_ptr_min_max_vals(env, insn, |
| 2996 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2997 | } |
| 2998 | } else { |
| 2999 | /* Pretend the src is a reg with a known value, since we only |
| 3000 | * need to be able to read from this state. |
| 3001 | */ |
| 3002 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3003 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3004 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 3005 | if (ptr_reg) /* pointer += K */ |
| 3006 | return adjust_ptr_min_max_vals(env, insn, |
| 3007 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | /* Got here implies adding two SCALAR_VALUEs */ |
| 3011 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3012 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3013 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3014 | return -EINVAL; |
| 3015 | } |
| 3016 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3017 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3018 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3019 | return -EINVAL; |
| 3020 | } |
| 3021 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3022 | } |
| 3023 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3024 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3025 | 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] | 3026 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3027 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3028 | u8 opcode = BPF_OP(insn->code); |
| 3029 | int err; |
| 3030 | |
| 3031 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 3032 | if (opcode == BPF_NEG) { |
| 3033 | if (BPF_SRC(insn->code) != 0 || |
| 3034 | insn->src_reg != BPF_REG_0 || |
| 3035 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3036 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3037 | return -EINVAL; |
| 3038 | } |
| 3039 | } else { |
| 3040 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 3041 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 3042 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3043 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3044 | return -EINVAL; |
| 3045 | } |
| 3046 | } |
| 3047 | |
| 3048 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3049 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3050 | if (err) |
| 3051 | return err; |
| 3052 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3053 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3054 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3055 | insn->dst_reg); |
| 3056 | return -EACCES; |
| 3057 | } |
| 3058 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3059 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3060 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3061 | if (err) |
| 3062 | return err; |
| 3063 | |
| 3064 | } else if (opcode == BPF_MOV) { |
| 3065 | |
| 3066 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3067 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3068 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3069 | return -EINVAL; |
| 3070 | } |
| 3071 | |
| 3072 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3073 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3074 | if (err) |
| 3075 | return err; |
| 3076 | } else { |
| 3077 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3078 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3079 | return -EINVAL; |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3084 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3085 | if (err) |
| 3086 | return err; |
| 3087 | |
| 3088 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3089 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 3090 | /* case: R1 = R2 |
| 3091 | * copy register state to dest reg |
| 3092 | */ |
| 3093 | regs[insn->dst_reg] = regs[insn->src_reg]; |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 3094 | regs[insn->dst_reg].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3095 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3096 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3097 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3098 | verbose(env, |
| 3099 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3100 | insn->src_reg); |
| 3101 | return -EACCES; |
| 3102 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3103 | mark_reg_unknown(env, regs, insn->dst_reg); |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3104 | coerce_reg_to_size(®s[insn->dst_reg], 4); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3105 | } |
| 3106 | } else { |
| 3107 | /* case: R = imm |
| 3108 | * remember the value we stored into this reg |
| 3109 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3110 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 3111 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 3112 | __mark_reg_known(regs + insn->dst_reg, |
| 3113 | insn->imm); |
| 3114 | } else { |
| 3115 | __mark_reg_known(regs + insn->dst_reg, |
| 3116 | (u32)insn->imm); |
| 3117 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3121 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3122 | return -EINVAL; |
| 3123 | |
| 3124 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 3125 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3126 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3127 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3128 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3129 | return -EINVAL; |
| 3130 | } |
| 3131 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3132 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3133 | if (err) |
| 3134 | return err; |
| 3135 | } else { |
| 3136 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3137 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3138 | return -EINVAL; |
| 3139 | } |
| 3140 | } |
| 3141 | |
| 3142 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3143 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3144 | if (err) |
| 3145 | return err; |
| 3146 | |
| 3147 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 3148 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3149 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3150 | return -EINVAL; |
| 3151 | } |
| 3152 | |
Daniel Borkmann | 7891a87 | 2018-01-10 20:04:37 +0100 | [diff] [blame] | 3153 | if (opcode == BPF_ARSH && BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3154 | verbose(env, "BPF_ARSH not supported for 32 bit ALU\n"); |
| 3155 | return -EINVAL; |
| 3156 | } |
| 3157 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3158 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 3159 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 3160 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 3161 | |
| 3162 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3163 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3164 | return -EINVAL; |
| 3165 | } |
| 3166 | } |
| 3167 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3168 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3169 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3170 | if (err) |
| 3171 | return err; |
| 3172 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3173 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | return 0; |
| 3177 | } |
| 3178 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3179 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3180 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 3181 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3182 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3183 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3184 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3185 | struct bpf_reg_state *regs = state->regs, *reg; |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3186 | u16 new_range; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3187 | int i, j; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3188 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3189 | if (dst_reg->off < 0 || |
| 3190 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3191 | /* This doesn't give us any range */ |
| 3192 | return; |
| 3193 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3194 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 3195 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3196 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 3197 | * than pkt_end, but that's because it's also less than pkt. |
| 3198 | */ |
| 3199 | return; |
| 3200 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3201 | new_range = dst_reg->off; |
| 3202 | if (range_right_open) |
| 3203 | new_range--; |
| 3204 | |
| 3205 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3206 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3207 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3208 | * |
| 3209 | * r2 = r3; |
| 3210 | * r2 += 8; |
| 3211 | * if (r2 > pkt_end) goto <handle exception> |
| 3212 | * <access okay> |
| 3213 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3214 | * r2 = r3; |
| 3215 | * r2 += 8; |
| 3216 | * if (r2 < pkt_end) goto <access okay> |
| 3217 | * <handle exception> |
| 3218 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3219 | * Where: |
| 3220 | * r2 == dst_reg, pkt_end == src_reg |
| 3221 | * r2=pkt(id=n,off=8,r=0) |
| 3222 | * r3=pkt(id=n,off=0,r=0) |
| 3223 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3224 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3225 | * |
| 3226 | * r2 = r3; |
| 3227 | * r2 += 8; |
| 3228 | * if (pkt_end >= r2) goto <access okay> |
| 3229 | * <handle exception> |
| 3230 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3231 | * r2 = r3; |
| 3232 | * r2 += 8; |
| 3233 | * if (pkt_end <= r2) goto <handle exception> |
| 3234 | * <access okay> |
| 3235 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3236 | * Where: |
| 3237 | * pkt_end == dst_reg, r2 == src_reg |
| 3238 | * r2=pkt(id=n,off=8,r=0) |
| 3239 | * r3=pkt(id=n,off=0,r=0) |
| 3240 | * |
| 3241 | * 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] | 3242 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 3243 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 3244 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3245 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3246 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3247 | /* If our ids match, then we must have the same max_value. And we |
| 3248 | * don't care about the other reg's fixed offset, since if it's too big |
| 3249 | * the range won't allow anything. |
| 3250 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 3251 | */ |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3252 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3253 | if (regs[i].type == type && regs[i].id == dst_reg->id) |
Alexei Starovoitov | b197768 | 2017-03-24 15:57:33 -0700 | [diff] [blame] | 3254 | /* keep the maximum range already checked */ |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3255 | regs[i].range = max(regs[i].range, new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3256 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3257 | for (j = 0; j <= vstate->curframe; j++) { |
| 3258 | state = vstate->frame[j]; |
| 3259 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 3260 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 3261 | continue; |
| 3262 | reg = &state->stack[i].spilled_ptr; |
| 3263 | if (reg->type == type && reg->id == dst_reg->id) |
| 3264 | reg->range = max(reg->range, new_range); |
| 3265 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3266 | } |
| 3267 | } |
| 3268 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3269 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 3270 | * variable register that we are working on, and src_reg is a constant or we're |
| 3271 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3272 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3273 | */ |
| 3274 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 3275 | struct bpf_reg_state *false_reg, u64 val, |
| 3276 | u8 opcode) |
| 3277 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3278 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 3279 | * variable offset from the compare (unless src_reg were a pointer into |
| 3280 | * the same object, but we don't bother with that. |
| 3281 | * Since false_reg and true_reg have the same type by construction, we |
| 3282 | * only need to check one of them for pointerness. |
| 3283 | */ |
| 3284 | if (__is_pointer_value(false, false_reg)) |
| 3285 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3286 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3287 | switch (opcode) { |
| 3288 | case BPF_JEQ: |
| 3289 | /* If this is false then we know nothing Jon Snow, but if it is |
| 3290 | * true then we know for sure. |
| 3291 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3292 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3293 | break; |
| 3294 | case BPF_JNE: |
| 3295 | /* If this is true we know nothing Jon Snow, but if it is false |
| 3296 | * we know the value for sure; |
| 3297 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3298 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3299 | break; |
| 3300 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3301 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 3302 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 3303 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3304 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3305 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 3306 | 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] | 3307 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3308 | case BPF_JLT: |
| 3309 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 3310 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 3311 | break; |
| 3312 | case BPF_JSLT: |
| 3313 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
| 3314 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 3315 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3316 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3317 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 3318 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 3319 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3320 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3321 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 3322 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3323 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3324 | case BPF_JLE: |
| 3325 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 3326 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 3327 | break; |
| 3328 | case BPF_JSLE: |
| 3329 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); |
| 3330 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 3331 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3332 | default: |
| 3333 | break; |
| 3334 | } |
| 3335 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3336 | __reg_deduce_bounds(false_reg); |
| 3337 | __reg_deduce_bounds(true_reg); |
| 3338 | /* We might have learned some bits from the bounds. */ |
| 3339 | __reg_bound_offset(false_reg); |
| 3340 | __reg_bound_offset(true_reg); |
| 3341 | /* Intersecting with the old var_off might have improved our bounds |
| 3342 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3343 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3344 | */ |
| 3345 | __update_reg_bounds(false_reg); |
| 3346 | __update_reg_bounds(true_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3347 | } |
| 3348 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3349 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 3350 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3351 | */ |
| 3352 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 3353 | struct bpf_reg_state *false_reg, u64 val, |
| 3354 | u8 opcode) |
| 3355 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3356 | if (__is_pointer_value(false, false_reg)) |
| 3357 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3358 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3359 | switch (opcode) { |
| 3360 | case BPF_JEQ: |
| 3361 | /* If this is false then we know nothing Jon Snow, but if it is |
| 3362 | * true then we know for sure. |
| 3363 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3364 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3365 | break; |
| 3366 | case BPF_JNE: |
| 3367 | /* If this is true we know nothing Jon Snow, but if it is false |
| 3368 | * we know the value for sure; |
| 3369 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3370 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3371 | break; |
| 3372 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3373 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 3374 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 3375 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3376 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3377 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 3378 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3379 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3380 | case BPF_JLT: |
| 3381 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 3382 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 3383 | break; |
| 3384 | case BPF_JSLT: |
| 3385 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); |
| 3386 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 3387 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3388 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3389 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 3390 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 3391 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3392 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3393 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 3394 | 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] | 3395 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3396 | case BPF_JLE: |
| 3397 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 3398 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 3399 | break; |
| 3400 | case BPF_JSLE: |
| 3401 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
| 3402 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 3403 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3404 | default: |
| 3405 | break; |
| 3406 | } |
| 3407 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3408 | __reg_deduce_bounds(false_reg); |
| 3409 | __reg_deduce_bounds(true_reg); |
| 3410 | /* We might have learned some bits from the bounds. */ |
| 3411 | __reg_bound_offset(false_reg); |
| 3412 | __reg_bound_offset(true_reg); |
| 3413 | /* Intersecting with the old var_off might have improved our bounds |
| 3414 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3415 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3416 | */ |
| 3417 | __update_reg_bounds(false_reg); |
| 3418 | __update_reg_bounds(true_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 3422 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 3423 | struct bpf_reg_state *dst_reg) |
| 3424 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3425 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 3426 | dst_reg->umin_value); |
| 3427 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 3428 | dst_reg->umax_value); |
| 3429 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 3430 | dst_reg->smin_value); |
| 3431 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 3432 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3433 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 3434 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3435 | /* We might have learned new bounds from the var_off. */ |
| 3436 | __update_reg_bounds(src_reg); |
| 3437 | __update_reg_bounds(dst_reg); |
| 3438 | /* We might have learned something about the sign bit. */ |
| 3439 | __reg_deduce_bounds(src_reg); |
| 3440 | __reg_deduce_bounds(dst_reg); |
| 3441 | /* We might have learned some bits from the bounds. */ |
| 3442 | __reg_bound_offset(src_reg); |
| 3443 | __reg_bound_offset(dst_reg); |
| 3444 | /* Intersecting with the old var_off might have improved our bounds |
| 3445 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3446 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3447 | */ |
| 3448 | __update_reg_bounds(src_reg); |
| 3449 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 3453 | struct bpf_reg_state *true_dst, |
| 3454 | struct bpf_reg_state *false_src, |
| 3455 | struct bpf_reg_state *false_dst, |
| 3456 | u8 opcode) |
| 3457 | { |
| 3458 | switch (opcode) { |
| 3459 | case BPF_JEQ: |
| 3460 | __reg_combine_min_max(true_src, true_dst); |
| 3461 | break; |
| 3462 | case BPF_JNE: |
| 3463 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3464 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3465 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3466 | } |
| 3467 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3468 | 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] | 3469 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3470 | { |
| 3471 | struct bpf_reg_state *reg = ®s[regno]; |
| 3472 | |
| 3473 | if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3474 | /* Old offset (both fixed and variable parts) should |
| 3475 | * have been known-zero, because we don't allow pointer |
| 3476 | * arithmetic on pointers that might be NULL. |
| 3477 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3478 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 3479 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3480 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3481 | __mark_reg_known_zero(reg); |
| 3482 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3483 | } |
| 3484 | if (is_null) { |
| 3485 | reg->type = SCALAR_VALUE; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3486 | } else if (reg->map_ptr->inner_map_meta) { |
| 3487 | reg->type = CONST_PTR_TO_MAP; |
| 3488 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
| 3489 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3490 | reg->type = PTR_TO_MAP_VALUE; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3491 | } |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 3492 | /* We don't need id from this point onwards anymore, thus we |
| 3493 | * should better reset it, so that state pruning has chances |
| 3494 | * to take effect. |
| 3495 | */ |
| 3496 | reg->id = 0; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3497 | } |
| 3498 | } |
| 3499 | |
| 3500 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 3501 | * be folded together at some point. |
| 3502 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3503 | static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3504 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3505 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3506 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3507 | struct bpf_reg_state *regs = state->regs; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 3508 | u32 id = regs[regno].id; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3509 | int i, j; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3510 | |
| 3511 | for (i = 0; i < MAX_BPF_REG; i++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3512 | mark_map_reg(regs, i, id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3513 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3514 | for (j = 0; j <= vstate->curframe; j++) { |
| 3515 | state = vstate->frame[j]; |
| 3516 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 3517 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 3518 | continue; |
| 3519 | mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null); |
| 3520 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3521 | } |
| 3522 | } |
| 3523 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 3524 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 3525 | struct bpf_reg_state *dst_reg, |
| 3526 | struct bpf_reg_state *src_reg, |
| 3527 | struct bpf_verifier_state *this_branch, |
| 3528 | struct bpf_verifier_state *other_branch) |
| 3529 | { |
| 3530 | if (BPF_SRC(insn->code) != BPF_X) |
| 3531 | return false; |
| 3532 | |
| 3533 | switch (BPF_OP(insn->code)) { |
| 3534 | case BPF_JGT: |
| 3535 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3536 | src_reg->type == PTR_TO_PACKET_END) || |
| 3537 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3538 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3539 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 3540 | find_good_pkt_pointers(this_branch, dst_reg, |
| 3541 | dst_reg->type, false); |
| 3542 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3543 | src_reg->type == PTR_TO_PACKET) || |
| 3544 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3545 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3546 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 3547 | find_good_pkt_pointers(other_branch, src_reg, |
| 3548 | src_reg->type, true); |
| 3549 | } else { |
| 3550 | return false; |
| 3551 | } |
| 3552 | break; |
| 3553 | case BPF_JLT: |
| 3554 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3555 | src_reg->type == PTR_TO_PACKET_END) || |
| 3556 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3557 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3558 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 3559 | find_good_pkt_pointers(other_branch, dst_reg, |
| 3560 | dst_reg->type, true); |
| 3561 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3562 | src_reg->type == PTR_TO_PACKET) || |
| 3563 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3564 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3565 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 3566 | find_good_pkt_pointers(this_branch, src_reg, |
| 3567 | src_reg->type, false); |
| 3568 | } else { |
| 3569 | return false; |
| 3570 | } |
| 3571 | break; |
| 3572 | case BPF_JGE: |
| 3573 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3574 | src_reg->type == PTR_TO_PACKET_END) || |
| 3575 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3576 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3577 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 3578 | find_good_pkt_pointers(this_branch, dst_reg, |
| 3579 | dst_reg->type, true); |
| 3580 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3581 | src_reg->type == PTR_TO_PACKET) || |
| 3582 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3583 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3584 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 3585 | find_good_pkt_pointers(other_branch, src_reg, |
| 3586 | src_reg->type, false); |
| 3587 | } else { |
| 3588 | return false; |
| 3589 | } |
| 3590 | break; |
| 3591 | case BPF_JLE: |
| 3592 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3593 | src_reg->type == PTR_TO_PACKET_END) || |
| 3594 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3595 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3596 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 3597 | find_good_pkt_pointers(other_branch, dst_reg, |
| 3598 | dst_reg->type, false); |
| 3599 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3600 | src_reg->type == PTR_TO_PACKET) || |
| 3601 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3602 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3603 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 3604 | find_good_pkt_pointers(this_branch, src_reg, |
| 3605 | src_reg->type, true); |
| 3606 | } else { |
| 3607 | return false; |
| 3608 | } |
| 3609 | break; |
| 3610 | default: |
| 3611 | return false; |
| 3612 | } |
| 3613 | |
| 3614 | return true; |
| 3615 | } |
| 3616 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3617 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3618 | struct bpf_insn *insn, int *insn_idx) |
| 3619 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3620 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 3621 | struct bpf_verifier_state *other_branch; |
| 3622 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
| 3623 | struct bpf_reg_state *dst_reg, *other_branch_regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3624 | u8 opcode = BPF_OP(insn->code); |
| 3625 | int err; |
| 3626 | |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3627 | if (opcode > BPF_JSLE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3628 | verbose(env, "invalid BPF_JMP opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3629 | return -EINVAL; |
| 3630 | } |
| 3631 | |
| 3632 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3633 | if (insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3634 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3635 | return -EINVAL; |
| 3636 | } |
| 3637 | |
| 3638 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3639 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3640 | if (err) |
| 3641 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3642 | |
| 3643 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3644 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3645 | insn->src_reg); |
| 3646 | return -EACCES; |
| 3647 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3648 | } else { |
| 3649 | if (insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3650 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3651 | return -EINVAL; |
| 3652 | } |
| 3653 | } |
| 3654 | |
| 3655 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3656 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3657 | if (err) |
| 3658 | return err; |
| 3659 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3660 | dst_reg = ®s[insn->dst_reg]; |
| 3661 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3662 | /* detect if R == 0 where R was initialized to zero earlier */ |
| 3663 | if (BPF_SRC(insn->code) == BPF_K && |
| 3664 | (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3665 | dst_reg->type == SCALAR_VALUE && |
Alexei Starovoitov | 3bf1592 | 2017-11-30 21:31:39 -0800 | [diff] [blame] | 3666 | tnum_is_const(dst_reg->var_off)) { |
| 3667 | if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) || |
| 3668 | (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3669 | /* if (imm == imm) goto pc+off; |
| 3670 | * only follow the goto, ignore fall-through |
| 3671 | */ |
| 3672 | *insn_idx += insn->off; |
| 3673 | return 0; |
| 3674 | } else { |
| 3675 | /* if (imm != imm) goto pc+off; |
| 3676 | * only follow fall-through branch, since |
| 3677 | * that's where the program will go |
| 3678 | */ |
| 3679 | return 0; |
| 3680 | } |
| 3681 | } |
| 3682 | |
| 3683 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); |
| 3684 | if (!other_branch) |
| 3685 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3686 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3687 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3688 | /* detect if we are comparing against a constant value so we can adjust |
| 3689 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3690 | * this is only legit if both are scalars (or pointers to the same |
| 3691 | * object, I suppose, but we don't support that right now), because |
| 3692 | * otherwise the different base pointers mean the offsets aren't |
| 3693 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3694 | */ |
| 3695 | if (BPF_SRC(insn->code) == BPF_X) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3696 | if (dst_reg->type == SCALAR_VALUE && |
| 3697 | regs[insn->src_reg].type == SCALAR_VALUE) { |
| 3698 | if (tnum_is_const(regs[insn->src_reg].var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3699 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3700 | dst_reg, regs[insn->src_reg].var_off.value, |
| 3701 | opcode); |
| 3702 | else if (tnum_is_const(dst_reg->var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3703 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3704 | ®s[insn->src_reg], |
| 3705 | dst_reg->var_off.value, opcode); |
| 3706 | else if (opcode == BPF_JEQ || opcode == BPF_JNE) |
| 3707 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3708 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 3709 | &other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3710 | ®s[insn->src_reg], |
| 3711 | ®s[insn->dst_reg], opcode); |
| 3712 | } |
| 3713 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3714 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3715 | dst_reg, insn->imm, opcode); |
| 3716 | } |
| 3717 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3718 | /* 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] | 3719 | if (BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3720 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
| 3721 | dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3722 | /* Mark all identical map registers in each branch as either |
| 3723 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 3724 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3725 | mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE); |
| 3726 | mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 3727 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 3728 | this_branch, other_branch) && |
| 3729 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3730 | verbose(env, "R%d pointer comparison prohibited\n", |
| 3731 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3732 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3733 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3734 | if (env->log.level) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3735 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3736 | return 0; |
| 3737 | } |
| 3738 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3739 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
| 3740 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) |
| 3741 | { |
| 3742 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; |
| 3743 | |
| 3744 | return (struct bpf_map *) (unsigned long) imm64; |
| 3745 | } |
| 3746 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3747 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3748 | 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] | 3749 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3750 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3751 | int err; |
| 3752 | |
| 3753 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3754 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3755 | return -EINVAL; |
| 3756 | } |
| 3757 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3758 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3759 | return -EINVAL; |
| 3760 | } |
| 3761 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3762 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3763 | if (err) |
| 3764 | return err; |
| 3765 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3766 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3767 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 3768 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3769 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3770 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3771 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3772 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3773 | |
| 3774 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ |
| 3775 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); |
| 3776 | |
| 3777 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 3778 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); |
| 3779 | return 0; |
| 3780 | } |
| 3781 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3782 | static bool may_access_skb(enum bpf_prog_type type) |
| 3783 | { |
| 3784 | switch (type) { |
| 3785 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 3786 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 3787 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3788 | return true; |
| 3789 | default: |
| 3790 | return false; |
| 3791 | } |
| 3792 | } |
| 3793 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3794 | /* verify safety of LD_ABS|LD_IND instructions: |
| 3795 | * - they can only appear in the programs where ctx == skb |
| 3796 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 3797 | * preserve R6-R9, and store return value into R0 |
| 3798 | * |
| 3799 | * Implicit input: |
| 3800 | * ctx == skb == R6 == CTX |
| 3801 | * |
| 3802 | * Explicit input: |
| 3803 | * SRC == any register |
| 3804 | * IMM == 32-bit immediate |
| 3805 | * |
| 3806 | * Output: |
| 3807 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 3808 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3809 | 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] | 3810 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3811 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3812 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3813 | int i, err; |
| 3814 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 3815 | if (!may_access_skb(env->prog->type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3816 | 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] | 3817 | return -EINVAL; |
| 3818 | } |
| 3819 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3820 | if (env->subprog_cnt) { |
| 3821 | /* when program has LD_ABS insn JITs and interpreter assume |
| 3822 | * that r1 == ctx == skb which is not the case for callees |
| 3823 | * that can have arbitrary arguments. It's problematic |
| 3824 | * for main prog as well since JITs would need to analyze |
| 3825 | * all functions in order to make proper register save/restore |
| 3826 | * decisions in the main prog. Hence disallow LD_ABS with calls |
| 3827 | */ |
| 3828 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); |
| 3829 | return -EINVAL; |
| 3830 | } |
| 3831 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3832 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 3833 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3834 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3835 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3836 | return -EINVAL; |
| 3837 | } |
| 3838 | |
| 3839 | /* check whether implicit source operand (register R6) is readable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3840 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3841 | if (err) |
| 3842 | return err; |
| 3843 | |
| 3844 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3845 | verbose(env, |
| 3846 | "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] | 3847 | return -EINVAL; |
| 3848 | } |
| 3849 | |
| 3850 | if (mode == BPF_IND) { |
| 3851 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3852 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3853 | if (err) |
| 3854 | return err; |
| 3855 | } |
| 3856 | |
| 3857 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3858 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3859 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3860 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 3861 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3862 | |
| 3863 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3864 | * the value fetched from the packet. |
| 3865 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3866 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3867 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3868 | return 0; |
| 3869 | } |
| 3870 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3871 | static int check_return_code(struct bpf_verifier_env *env) |
| 3872 | { |
| 3873 | struct bpf_reg_state *reg; |
| 3874 | struct tnum range = tnum_range(0, 1); |
| 3875 | |
| 3876 | switch (env->prog->type) { |
| 3877 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 3878 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 3879 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 3880 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3881 | break; |
| 3882 | default: |
| 3883 | return 0; |
| 3884 | } |
| 3885 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3886 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3887 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3888 | 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] | 3889 | reg_type_str[reg->type]); |
| 3890 | return -EINVAL; |
| 3891 | } |
| 3892 | |
| 3893 | if (!tnum_in(range, reg->var_off)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3894 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3895 | if (!tnum_is_unknown(reg->var_off)) { |
| 3896 | char tn_buf[48]; |
| 3897 | |
| 3898 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3899 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3900 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3901 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3902 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3903 | verbose(env, " should have been 0 or 1\n"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3904 | return -EINVAL; |
| 3905 | } |
| 3906 | return 0; |
| 3907 | } |
| 3908 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3909 | /* non-recursive DFS pseudo code |
| 3910 | * 1 procedure DFS-iterative(G,v): |
| 3911 | * 2 label v as discovered |
| 3912 | * 3 let S be a stack |
| 3913 | * 4 S.push(v) |
| 3914 | * 5 while S is not empty |
| 3915 | * 6 t <- S.pop() |
| 3916 | * 7 if t is what we're looking for: |
| 3917 | * 8 return t |
| 3918 | * 9 for all edges e in G.adjacentEdges(t) do |
| 3919 | * 10 if edge e is already labelled |
| 3920 | * 11 continue with the next edge |
| 3921 | * 12 w <- G.adjacentVertex(t,e) |
| 3922 | * 13 if vertex w is not discovered and not explored |
| 3923 | * 14 label e as tree-edge |
| 3924 | * 15 label w as discovered |
| 3925 | * 16 S.push(w) |
| 3926 | * 17 continue at 5 |
| 3927 | * 18 else if vertex w is discovered |
| 3928 | * 19 label e as back-edge |
| 3929 | * 20 else |
| 3930 | * 21 // vertex w is explored |
| 3931 | * 22 label e as forward- or cross-edge |
| 3932 | * 23 label t as explored |
| 3933 | * 24 S.pop() |
| 3934 | * |
| 3935 | * convention: |
| 3936 | * 0x10 - discovered |
| 3937 | * 0x11 - discovered and fall-through edge labelled |
| 3938 | * 0x12 - discovered and fall-through and branch edges labelled |
| 3939 | * 0x20 - explored |
| 3940 | */ |
| 3941 | |
| 3942 | enum { |
| 3943 | DISCOVERED = 0x10, |
| 3944 | EXPLORED = 0x20, |
| 3945 | FALLTHROUGH = 1, |
| 3946 | BRANCH = 2, |
| 3947 | }; |
| 3948 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3949 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3950 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3951 | static int *insn_stack; /* stack of insns to process */ |
| 3952 | static int cur_stack; /* current stack index */ |
| 3953 | static int *insn_state; |
| 3954 | |
| 3955 | /* t, w, e - match pseudo-code above: |
| 3956 | * t - index of current instruction |
| 3957 | * w - next instruction |
| 3958 | * e - edge |
| 3959 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3960 | 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] | 3961 | { |
| 3962 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 3963 | return 0; |
| 3964 | |
| 3965 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 3966 | return 0; |
| 3967 | |
| 3968 | if (w < 0 || w >= env->prog->len) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3969 | 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] | 3970 | return -EINVAL; |
| 3971 | } |
| 3972 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3973 | if (e == BRANCH) |
| 3974 | /* mark branch target for state pruning */ |
| 3975 | env->explored_states[w] = STATE_LIST_MARK; |
| 3976 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3977 | if (insn_state[w] == 0) { |
| 3978 | /* tree-edge */ |
| 3979 | insn_state[t] = DISCOVERED | e; |
| 3980 | insn_state[w] = DISCOVERED; |
| 3981 | if (cur_stack >= env->prog->len) |
| 3982 | return -E2BIG; |
| 3983 | insn_stack[cur_stack++] = w; |
| 3984 | return 1; |
| 3985 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3986 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3987 | return -EINVAL; |
| 3988 | } else if (insn_state[w] == EXPLORED) { |
| 3989 | /* forward- or cross-edge */ |
| 3990 | insn_state[t] = DISCOVERED | e; |
| 3991 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3992 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3993 | return -EFAULT; |
| 3994 | } |
| 3995 | return 0; |
| 3996 | } |
| 3997 | |
| 3998 | /* non-recursive depth-first-search to detect loops in BPF program |
| 3999 | * loop == back-edge in directed graph |
| 4000 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4001 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4002 | { |
| 4003 | struct bpf_insn *insns = env->prog->insnsi; |
| 4004 | int insn_cnt = env->prog->len; |
| 4005 | int ret = 0; |
| 4006 | int i, t; |
| 4007 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 4008 | ret = check_subprogs(env); |
| 4009 | if (ret < 0) |
| 4010 | return ret; |
| 4011 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4012 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 4013 | if (!insn_state) |
| 4014 | return -ENOMEM; |
| 4015 | |
| 4016 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 4017 | if (!insn_stack) { |
| 4018 | kfree(insn_state); |
| 4019 | return -ENOMEM; |
| 4020 | } |
| 4021 | |
| 4022 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 4023 | insn_stack[0] = 0; /* 0 is the first instruction */ |
| 4024 | cur_stack = 1; |
| 4025 | |
| 4026 | peek_stack: |
| 4027 | if (cur_stack == 0) |
| 4028 | goto check_state; |
| 4029 | t = insn_stack[cur_stack - 1]; |
| 4030 | |
| 4031 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { |
| 4032 | u8 opcode = BPF_OP(insns[t].code); |
| 4033 | |
| 4034 | if (opcode == BPF_EXIT) { |
| 4035 | goto mark_explored; |
| 4036 | } else if (opcode == BPF_CALL) { |
| 4037 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4038 | if (ret == 1) |
| 4039 | goto peek_stack; |
| 4040 | else if (ret < 0) |
| 4041 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 4042 | if (t + 1 < insn_cnt) |
| 4043 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 4044 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
| 4045 | env->explored_states[t] = STATE_LIST_MARK; |
| 4046 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); |
| 4047 | if (ret == 1) |
| 4048 | goto peek_stack; |
| 4049 | else if (ret < 0) |
| 4050 | goto err_free; |
| 4051 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4052 | } else if (opcode == BPF_JA) { |
| 4053 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 4054 | ret = -EINVAL; |
| 4055 | goto err_free; |
| 4056 | } |
| 4057 | /* unconditional jump with single edge */ |
| 4058 | ret = push_insn(t, t + insns[t].off + 1, |
| 4059 | FALLTHROUGH, env); |
| 4060 | if (ret == 1) |
| 4061 | goto peek_stack; |
| 4062 | else if (ret < 0) |
| 4063 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4064 | /* tell verifier to check for equivalent states |
| 4065 | * after every call and jump |
| 4066 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 4067 | if (t + 1 < insn_cnt) |
| 4068 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4069 | } else { |
| 4070 | /* conditional jump with two edges */ |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 4071 | env->explored_states[t] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4072 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4073 | if (ret == 1) |
| 4074 | goto peek_stack; |
| 4075 | else if (ret < 0) |
| 4076 | goto err_free; |
| 4077 | |
| 4078 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 4079 | if (ret == 1) |
| 4080 | goto peek_stack; |
| 4081 | else if (ret < 0) |
| 4082 | goto err_free; |
| 4083 | } |
| 4084 | } else { |
| 4085 | /* all other non-branch instructions with single |
| 4086 | * fall-through edge |
| 4087 | */ |
| 4088 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4089 | if (ret == 1) |
| 4090 | goto peek_stack; |
| 4091 | else if (ret < 0) |
| 4092 | goto err_free; |
| 4093 | } |
| 4094 | |
| 4095 | mark_explored: |
| 4096 | insn_state[t] = EXPLORED; |
| 4097 | if (cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4098 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4099 | ret = -EFAULT; |
| 4100 | goto err_free; |
| 4101 | } |
| 4102 | goto peek_stack; |
| 4103 | |
| 4104 | check_state: |
| 4105 | for (i = 0; i < insn_cnt; i++) { |
| 4106 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4107 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4108 | ret = -EINVAL; |
| 4109 | goto err_free; |
| 4110 | } |
| 4111 | } |
| 4112 | ret = 0; /* cfg looks good */ |
| 4113 | |
| 4114 | err_free: |
| 4115 | kfree(insn_state); |
| 4116 | kfree(insn_stack); |
| 4117 | return ret; |
| 4118 | } |
| 4119 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4120 | /* check %cur's range satisfies %old's */ |
| 4121 | static bool range_within(struct bpf_reg_state *old, |
| 4122 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4123 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4124 | return old->umin_value <= cur->umin_value && |
| 4125 | old->umax_value >= cur->umax_value && |
| 4126 | old->smin_value <= cur->smin_value && |
| 4127 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4128 | } |
| 4129 | |
| 4130 | /* Maximum number of register states that can exist at once */ |
| 4131 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 4132 | struct idpair { |
| 4133 | u32 old; |
| 4134 | u32 cur; |
| 4135 | }; |
| 4136 | |
| 4137 | /* If in the old state two registers had the same id, then they need to have |
| 4138 | * the same id in the new state as well. But that id could be different from |
| 4139 | * the old state, so we need to track the mapping from old to new ids. |
| 4140 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 4141 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 4142 | * regs with a different old id could still have new id 9, we don't care about |
| 4143 | * that. |
| 4144 | * So we look through our idmap to see if this old id has been seen before. If |
| 4145 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 4146 | */ |
| 4147 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 4148 | { |
| 4149 | unsigned int i; |
| 4150 | |
| 4151 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 4152 | if (!idmap[i].old) { |
| 4153 | /* Reached an empty slot; haven't seen this id before */ |
| 4154 | idmap[i].old = old_id; |
| 4155 | idmap[i].cur = cur_id; |
| 4156 | return true; |
| 4157 | } |
| 4158 | if (idmap[i].old == old_id) |
| 4159 | return idmap[i].cur == cur_id; |
| 4160 | } |
| 4161 | /* We ran out of idmap slots, which should be impossible */ |
| 4162 | WARN_ON_ONCE(1); |
| 4163 | return false; |
| 4164 | } |
| 4165 | |
| 4166 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4167 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 4168 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4169 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4170 | bool equal; |
| 4171 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4172 | if (!(rold->live & REG_LIVE_READ)) |
| 4173 | /* explored state didn't use this */ |
| 4174 | return true; |
| 4175 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4176 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, frameno)) == 0; |
| 4177 | |
| 4178 | if (rold->type == PTR_TO_STACK) |
| 4179 | /* two stack pointers are equal only if they're pointing to |
| 4180 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 4181 | */ |
| 4182 | return equal && rold->frameno == rcur->frameno; |
| 4183 | |
| 4184 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4185 | return true; |
| 4186 | |
| 4187 | if (rold->type == NOT_INIT) |
| 4188 | /* explored state can't have used this */ |
| 4189 | return true; |
| 4190 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4191 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4192 | switch (rold->type) { |
| 4193 | case SCALAR_VALUE: |
| 4194 | if (rcur->type == SCALAR_VALUE) { |
| 4195 | /* new val must satisfy old val knowledge */ |
| 4196 | return range_within(rold, rcur) && |
| 4197 | tnum_in(rold->var_off, rcur->var_off); |
| 4198 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 4199 | /* We're trying to use a pointer in place of a scalar. |
| 4200 | * Even if the scalar was unbounded, this could lead to |
| 4201 | * pointer leaks because scalars are allowed to leak |
| 4202 | * while pointers are not. We could make this safe in |
| 4203 | * special cases if root is calling us, but it's |
| 4204 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4205 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 4206 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4207 | } |
| 4208 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4209 | /* If the new min/max/var_off satisfy the old ones and |
| 4210 | * everything else matches, we are OK. |
| 4211 | * We don't care about the 'id' value, because nothing |
| 4212 | * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL) |
| 4213 | */ |
| 4214 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 4215 | range_within(rold, rcur) && |
| 4216 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4217 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 4218 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 4219 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 4220 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 4221 | * checked, doing so could have affected others with the same |
| 4222 | * id, and we can't check for that because we lost the id when |
| 4223 | * we converted to a PTR_TO_MAP_VALUE. |
| 4224 | */ |
| 4225 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 4226 | return false; |
| 4227 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 4228 | return false; |
| 4229 | /* Check our ids match any regs they're supposed to */ |
| 4230 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4231 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4232 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4233 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4234 | return false; |
| 4235 | /* We must have at least as much range as the old ptr |
| 4236 | * did, so that any accesses which were safe before are |
| 4237 | * still safe. This is true even if old range < old off, |
| 4238 | * since someone could have accessed through (ptr - k), or |
| 4239 | * even done ptr -= k in a register, to get a safe access. |
| 4240 | */ |
| 4241 | if (rold->range > rcur->range) |
| 4242 | return false; |
| 4243 | /* If the offsets don't match, we can't trust our alignment; |
| 4244 | * nor can we be sure that we won't fall out of range. |
| 4245 | */ |
| 4246 | if (rold->off != rcur->off) |
| 4247 | return false; |
| 4248 | /* id relations must be preserved */ |
| 4249 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 4250 | return false; |
| 4251 | /* new val must satisfy old val knowledge */ |
| 4252 | return range_within(rold, rcur) && |
| 4253 | tnum_in(rold->var_off, rcur->var_off); |
| 4254 | case PTR_TO_CTX: |
| 4255 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4256 | case PTR_TO_PACKET_END: |
| 4257 | /* Only valid matches are exact, which memcmp() above |
| 4258 | * would have accepted |
| 4259 | */ |
| 4260 | default: |
| 4261 | /* Don't know what's going on, just say it's not safe */ |
| 4262 | return false; |
| 4263 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4264 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4265 | /* Shouldn't get here; if we do, say it's not safe */ |
| 4266 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4267 | return false; |
| 4268 | } |
| 4269 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4270 | static bool stacksafe(struct bpf_func_state *old, |
| 4271 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4272 | struct idpair *idmap) |
| 4273 | { |
| 4274 | int i, spi; |
| 4275 | |
| 4276 | /* if explored stack has more populated slots than current stack |
| 4277 | * such stacks are not equivalent |
| 4278 | */ |
| 4279 | if (old->allocated_stack > cur->allocated_stack) |
| 4280 | return false; |
| 4281 | |
| 4282 | /* walk slots of the explored stack and ignore any additional |
| 4283 | * slots in the current stack, since explored(safe) state |
| 4284 | * didn't use them |
| 4285 | */ |
| 4286 | for (i = 0; i < old->allocated_stack; i++) { |
| 4287 | spi = i / BPF_REG_SIZE; |
| 4288 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4289 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) |
| 4290 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 4291 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4292 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4293 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 4294 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4295 | /* if old state was safe with misc data in the stack |
| 4296 | * it will be safe with zero-initialized stack. |
| 4297 | * The opposite is not true |
| 4298 | */ |
| 4299 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 4300 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 4301 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4302 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 4303 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 4304 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 4305 | * this stack slot, but current has has STACK_MISC -> |
| 4306 | * this verifier states are not equivalent, |
| 4307 | * return false to continue verification of this path |
| 4308 | */ |
| 4309 | return false; |
| 4310 | if (i % BPF_REG_SIZE) |
| 4311 | continue; |
| 4312 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 4313 | continue; |
| 4314 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 4315 | &cur->stack[spi].spilled_ptr, |
| 4316 | idmap)) |
| 4317 | /* when explored and current stack slot are both storing |
| 4318 | * spilled registers, check that stored pointers types |
| 4319 | * are the same as well. |
| 4320 | * Ex: explored safe path could have stored |
| 4321 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 4322 | * but current path has stored: |
| 4323 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 4324 | * such verifier states are not equivalent. |
| 4325 | * return false to continue verification of this path |
| 4326 | */ |
| 4327 | return false; |
| 4328 | } |
| 4329 | return true; |
| 4330 | } |
| 4331 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4332 | /* compare two verifier states |
| 4333 | * |
| 4334 | * all states stored in state_list are known to be valid, since |
| 4335 | * verifier reached 'bpf_exit' instruction through them |
| 4336 | * |
| 4337 | * this function is called when verifier exploring different branches of |
| 4338 | * execution popped from the state stack. If it sees an old state that has |
| 4339 | * more strict register state and more strict stack state then this execution |
| 4340 | * branch doesn't need to be explored further, since verifier already |
| 4341 | * concluded that more strict state leads to valid finish. |
| 4342 | * |
| 4343 | * Therefore two states are equivalent if register state is more conservative |
| 4344 | * and explored stack state is more conservative than the current one. |
| 4345 | * Example: |
| 4346 | * explored current |
| 4347 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 4348 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 4349 | * |
| 4350 | * In other words if current stack state (one being explored) has more |
| 4351 | * valid slots than old one that already passed validation, it means |
| 4352 | * the verifier can stop exploring and conclude that current state is valid too |
| 4353 | * |
| 4354 | * Similarly with registers. If explored state has register type as invalid |
| 4355 | * whereas register type in current state is meaningful, it means that |
| 4356 | * the current state will reach 'bpf_exit' instruction safely |
| 4357 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4358 | static bool func_states_equal(struct bpf_func_state *old, |
| 4359 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4360 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4361 | struct idpair *idmap; |
| 4362 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4363 | int i; |
| 4364 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4365 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 4366 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 4367 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4368 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4369 | |
| 4370 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4371 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4372 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4373 | } |
| 4374 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4375 | if (!stacksafe(old, cur, idmap)) |
| 4376 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4377 | ret = true; |
| 4378 | out_free: |
| 4379 | kfree(idmap); |
| 4380 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4381 | } |
| 4382 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4383 | static bool states_equal(struct bpf_verifier_env *env, |
| 4384 | struct bpf_verifier_state *old, |
| 4385 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4386 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4387 | int i; |
| 4388 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4389 | if (old->curframe != cur->curframe) |
| 4390 | return false; |
| 4391 | |
| 4392 | /* for states to be equal callsites have to be the same |
| 4393 | * and all frame states need to be equivalent |
| 4394 | */ |
| 4395 | for (i = 0; i <= old->curframe; i++) { |
| 4396 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 4397 | return false; |
| 4398 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 4399 | return false; |
| 4400 | } |
| 4401 | return true; |
| 4402 | } |
| 4403 | |
| 4404 | /* A write screens off any subsequent reads; but write marks come from the |
| 4405 | * straight-line code between a state and its parent. When we arrive at an |
| 4406 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 4407 | * code, so read marks in the state must propagate to the parent regardless |
| 4408 | * of the state's write marks. That's what 'parent == state->parent' comparison |
| 4409 | * in mark_reg_read() and mark_stack_slot_read() is for. |
| 4410 | */ |
| 4411 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 4412 | const struct bpf_verifier_state *vstate, |
| 4413 | struct bpf_verifier_state *vparent) |
| 4414 | { |
| 4415 | int i, frame, err = 0; |
| 4416 | struct bpf_func_state *state, *parent; |
| 4417 | |
| 4418 | if (vparent->curframe != vstate->curframe) { |
| 4419 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 4420 | vparent->curframe, vstate->curframe); |
| 4421 | return -EFAULT; |
| 4422 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4423 | /* Propagate read liveness of registers... */ |
| 4424 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
| 4425 | /* We don't need to worry about FP liveness because it's read-only */ |
| 4426 | for (i = 0; i < BPF_REG_FP; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4427 | if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4428 | continue; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4429 | if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) { |
| 4430 | err = mark_reg_read(env, vstate, vparent, i); |
| 4431 | if (err) |
| 4432 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4433 | } |
| 4434 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4435 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4436 | /* ... and stack slots */ |
| 4437 | for (frame = 0; frame <= vstate->curframe; frame++) { |
| 4438 | state = vstate->frame[frame]; |
| 4439 | parent = vparent->frame[frame]; |
| 4440 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 4441 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4442 | if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ) |
| 4443 | continue; |
| 4444 | if (state->stack[i].spilled_ptr.live & REG_LIVE_READ) |
| 4445 | mark_stack_slot_read(env, vstate, vparent, i, frame); |
| 4446 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4447 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4448 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4449 | } |
| 4450 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4451 | 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] | 4452 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4453 | struct bpf_verifier_state_list *new_sl; |
| 4454 | struct bpf_verifier_state_list *sl; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4455 | struct bpf_verifier_state *cur = env->cur_state; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4456 | int i, j, err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4457 | |
| 4458 | sl = env->explored_states[insn_idx]; |
| 4459 | if (!sl) |
| 4460 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 4461 | * be doing state search here |
| 4462 | */ |
| 4463 | return 0; |
| 4464 | |
| 4465 | while (sl != STATE_LIST_MARK) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4466 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4467 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4468 | * prune the search. |
| 4469 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 4470 | * If we have any write marks in env->cur_state, they |
| 4471 | * will prevent corresponding reads in the continuation |
| 4472 | * from reaching our parent (an explored_state). Our |
| 4473 | * own state will get the read marks recorded, but |
| 4474 | * they'll be immediately forgotten as we're pruning |
| 4475 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4476 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4477 | err = propagate_liveness(env, &sl->state, cur); |
| 4478 | if (err) |
| 4479 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4480 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4481 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4482 | sl = sl->next; |
| 4483 | } |
| 4484 | |
| 4485 | /* there were no equivalent states, remember current one. |
| 4486 | * technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4487 | * but it will either reach outer most bpf_exit (which means it's safe) |
| 4488 | * or it will be rejected. Since there are no loops, we won't be |
| 4489 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
| 4490 | * again on the way to bpf_exit |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4491 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4492 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4493 | if (!new_sl) |
| 4494 | return -ENOMEM; |
| 4495 | |
| 4496 | /* add new state to the head of linked list */ |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 4497 | err = copy_verifier_state(&new_sl->state, cur); |
| 4498 | if (err) { |
| 4499 | free_verifier_state(&new_sl->state, false); |
| 4500 | kfree(new_sl); |
| 4501 | return err; |
| 4502 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4503 | new_sl->next = env->explored_states[insn_idx]; |
| 4504 | env->explored_states[insn_idx] = new_sl; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4505 | /* connect new state to parentage chain */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4506 | cur->parent = &new_sl->state; |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 4507 | /* clear write marks in current state: the writes we did are not writes |
| 4508 | * our child did, so they don't screen off its reads from us. |
| 4509 | * (There are no read marks in current state, because reads always mark |
| 4510 | * their parent and current state never has children yet. Only |
| 4511 | * explored_states can get read marks.) |
| 4512 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4513 | for (i = 0; i < BPF_REG_FP; i++) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4514 | cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE; |
| 4515 | |
| 4516 | /* all stack frames are accessible from callee, clear them all */ |
| 4517 | for (j = 0; j <= cur->curframe; j++) { |
| 4518 | struct bpf_func_state *frame = cur->frame[j]; |
| 4519 | |
| 4520 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4521 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4522 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4523 | return 0; |
| 4524 | } |
| 4525 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4526 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4527 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4528 | struct bpf_verifier_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4529 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4530 | struct bpf_reg_state *regs; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4531 | int insn_cnt = env->prog->len, i; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4532 | int insn_idx, prev_insn_idx = 0; |
| 4533 | int insn_processed = 0; |
| 4534 | bool do_print_state = false; |
| 4535 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4536 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 4537 | if (!state) |
| 4538 | return -ENOMEM; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4539 | state->curframe = 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4540 | state->parent = NULL; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4541 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 4542 | if (!state->frame[0]) { |
| 4543 | kfree(state); |
| 4544 | return -ENOMEM; |
| 4545 | } |
| 4546 | env->cur_state = state; |
| 4547 | init_func_state(env, state->frame[0], |
| 4548 | BPF_MAIN_FUNC /* callsite */, |
| 4549 | 0 /* frameno */, |
| 4550 | 0 /* subprogno, zero == main subprog */); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4551 | insn_idx = 0; |
| 4552 | for (;;) { |
| 4553 | struct bpf_insn *insn; |
| 4554 | u8 class; |
| 4555 | int err; |
| 4556 | |
| 4557 | if (insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4558 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4559 | insn_idx, insn_cnt); |
| 4560 | return -EFAULT; |
| 4561 | } |
| 4562 | |
| 4563 | insn = &insns[insn_idx]; |
| 4564 | class = BPF_CLASS(insn->code); |
| 4565 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 4566 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4567 | verbose(env, |
| 4568 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4569 | insn_processed); |
| 4570 | return -E2BIG; |
| 4571 | } |
| 4572 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4573 | err = is_state_visited(env, insn_idx); |
| 4574 | if (err < 0) |
| 4575 | return err; |
| 4576 | if (err == 1) { |
| 4577 | /* found equivalent state, can prune the search */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4578 | if (env->log.level) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4579 | if (do_print_state) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4580 | verbose(env, "\nfrom %d to %d: safe\n", |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4581 | prev_insn_idx, insn_idx); |
| 4582 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4583 | verbose(env, "%d: safe\n", insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4584 | } |
| 4585 | goto process_bpf_exit; |
| 4586 | } |
| 4587 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 4588 | if (need_resched()) |
| 4589 | cond_resched(); |
| 4590 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4591 | if (env->log.level > 1 || (env->log.level && do_print_state)) { |
| 4592 | if (env->log.level > 1) |
| 4593 | verbose(env, "%d:", insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 4594 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4595 | verbose(env, "\nfrom %d to %d:", |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 4596 | prev_insn_idx, insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4597 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4598 | do_print_state = false; |
| 4599 | } |
| 4600 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4601 | if (env->log.level) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 4602 | const struct bpf_insn_cbs cbs = { |
| 4603 | .cb_print = verbose, |
| 4604 | }; |
| 4605 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4606 | verbose(env, "%d: ", insn_idx); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 4607 | print_bpf_insn(&cbs, env, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4608 | } |
| 4609 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 4610 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
| 4611 | err = bpf_prog_offload_verify_insn(env, insn_idx, |
| 4612 | prev_insn_idx); |
| 4613 | if (err) |
| 4614 | return err; |
| 4615 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 4616 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4617 | regs = cur_regs(env); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 4618 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4619 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4620 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4621 | if (err) |
| 4622 | return err; |
| 4623 | |
| 4624 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4625 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4626 | |
| 4627 | /* check for reserved fields is already done */ |
| 4628 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4629 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4630 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4631 | if (err) |
| 4632 | return err; |
| 4633 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4634 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4635 | if (err) |
| 4636 | return err; |
| 4637 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 4638 | src_reg_type = regs[insn->src_reg].type; |
| 4639 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4640 | /* check that memory (src_reg + off) is readable, |
| 4641 | * the state of dst_reg will be updated by this func |
| 4642 | */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4643 | err = check_mem_access(env, insn_idx, insn->src_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4644 | BPF_SIZE(insn->code), BPF_READ, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 4645 | insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4646 | if (err) |
| 4647 | return err; |
| 4648 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4649 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 4650 | |
| 4651 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4652 | /* saw a valid insn |
| 4653 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4654 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4655 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4656 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4657 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4658 | } else if (src_reg_type != *prev_src_type && |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4659 | (src_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4660 | *prev_src_type == PTR_TO_CTX)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4661 | /* ABuser program is trying to use the same insn |
| 4662 | * dst_reg = *(u32*) (src_reg + off) |
| 4663 | * with different pointer types: |
| 4664 | * src_reg == ctx in one branch and |
| 4665 | * src_reg == stack|map in some other branch. |
| 4666 | * Reject it. |
| 4667 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4668 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4669 | return -EINVAL; |
| 4670 | } |
| 4671 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4672 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4673 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4674 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4675 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4676 | err = check_xadd(env, insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4677 | if (err) |
| 4678 | return err; |
| 4679 | insn_idx++; |
| 4680 | continue; |
| 4681 | } |
| 4682 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4683 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4684 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4685 | if (err) |
| 4686 | return err; |
| 4687 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4688 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4689 | if (err) |
| 4690 | return err; |
| 4691 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4692 | dst_reg_type = regs[insn->dst_reg].type; |
| 4693 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4694 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4695 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4696 | BPF_SIZE(insn->code), BPF_WRITE, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 4697 | insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4698 | if (err) |
| 4699 | return err; |
| 4700 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4701 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 4702 | |
| 4703 | if (*prev_dst_type == NOT_INIT) { |
| 4704 | *prev_dst_type = dst_reg_type; |
| 4705 | } else if (dst_reg_type != *prev_dst_type && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4706 | (dst_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4707 | *prev_dst_type == PTR_TO_CTX)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4708 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4709 | return -EINVAL; |
| 4710 | } |
| 4711 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4712 | } else if (class == BPF_ST) { |
| 4713 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 4714 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4715 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4716 | return -EINVAL; |
| 4717 | } |
| 4718 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4719 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4720 | if (err) |
| 4721 | return err; |
| 4722 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 4723 | if (is_ctx_reg(env, insn->dst_reg)) { |
| 4724 | verbose(env, "BPF_ST stores into R%d context is not allowed\n", |
| 4725 | insn->dst_reg); |
| 4726 | return -EACCES; |
| 4727 | } |
| 4728 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4729 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4730 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4731 | BPF_SIZE(insn->code), BPF_WRITE, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 4732 | -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4733 | if (err) |
| 4734 | return err; |
| 4735 | |
| 4736 | } else if (class == BPF_JMP) { |
| 4737 | u8 opcode = BPF_OP(insn->code); |
| 4738 | |
| 4739 | if (opcode == BPF_CALL) { |
| 4740 | if (BPF_SRC(insn->code) != BPF_K || |
| 4741 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4742 | (insn->src_reg != BPF_REG_0 && |
| 4743 | insn->src_reg != BPF_PSEUDO_CALL) || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4744 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4745 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4746 | return -EINVAL; |
| 4747 | } |
| 4748 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4749 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 4750 | err = check_func_call(env, insn, &insn_idx); |
| 4751 | else |
| 4752 | err = check_helper_call(env, insn->imm, insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4753 | if (err) |
| 4754 | return err; |
| 4755 | |
| 4756 | } else if (opcode == BPF_JA) { |
| 4757 | if (BPF_SRC(insn->code) != BPF_K || |
| 4758 | insn->imm != 0 || |
| 4759 | insn->src_reg != BPF_REG_0 || |
| 4760 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4761 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4762 | return -EINVAL; |
| 4763 | } |
| 4764 | |
| 4765 | insn_idx += insn->off + 1; |
| 4766 | continue; |
| 4767 | |
| 4768 | } else if (opcode == BPF_EXIT) { |
| 4769 | if (BPF_SRC(insn->code) != BPF_K || |
| 4770 | insn->imm != 0 || |
| 4771 | insn->src_reg != BPF_REG_0 || |
| 4772 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4773 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4774 | return -EINVAL; |
| 4775 | } |
| 4776 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4777 | if (state->curframe) { |
| 4778 | /* exit from nested function */ |
| 4779 | prev_insn_idx = insn_idx; |
| 4780 | err = prepare_func_exit(env, &insn_idx); |
| 4781 | if (err) |
| 4782 | return err; |
| 4783 | do_print_state = true; |
| 4784 | continue; |
| 4785 | } |
| 4786 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4787 | /* eBPF calling convetion is such that R0 is used |
| 4788 | * to return the value from eBPF program. |
| 4789 | * Make sure that it's readable at this time |
| 4790 | * of bpf_exit, which means that program wrote |
| 4791 | * something into it earlier |
| 4792 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4793 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4794 | if (err) |
| 4795 | return err; |
| 4796 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4797 | if (is_pointer_value(env, BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4798 | verbose(env, "R0 leaks addr as return value\n"); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4799 | return -EACCES; |
| 4800 | } |
| 4801 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4802 | err = check_return_code(env); |
| 4803 | if (err) |
| 4804 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4805 | process_bpf_exit: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4806 | err = pop_stack(env, &prev_insn_idx, &insn_idx); |
| 4807 | if (err < 0) { |
| 4808 | if (err != -ENOENT) |
| 4809 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4810 | break; |
| 4811 | } else { |
| 4812 | do_print_state = true; |
| 4813 | continue; |
| 4814 | } |
| 4815 | } else { |
| 4816 | err = check_cond_jmp_op(env, insn, &insn_idx); |
| 4817 | if (err) |
| 4818 | return err; |
| 4819 | } |
| 4820 | } else if (class == BPF_LD) { |
| 4821 | u8 mode = BPF_MODE(insn->code); |
| 4822 | |
| 4823 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4824 | err = check_ld_abs(env, insn); |
| 4825 | if (err) |
| 4826 | return err; |
| 4827 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4828 | } else if (mode == BPF_IMM) { |
| 4829 | err = check_ld_imm(env, insn); |
| 4830 | if (err) |
| 4831 | return err; |
| 4832 | |
| 4833 | insn_idx++; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 4834 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4835 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4836 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4837 | return -EINVAL; |
| 4838 | } |
| 4839 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4840 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4841 | return -EINVAL; |
| 4842 | } |
| 4843 | |
| 4844 | insn_idx++; |
| 4845 | } |
| 4846 | |
Daniel Borkmann | 4bd95f4 | 2018-01-20 01:24:36 +0100 | [diff] [blame] | 4847 | verbose(env, "processed %d insns (limit %d), stack depth ", |
| 4848 | insn_processed, BPF_COMPLEXITY_LIMIT_INSNS); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4849 | for (i = 0; i < env->subprog_cnt + 1; i++) { |
| 4850 | u32 depth = env->subprog_stack_depth[i]; |
| 4851 | |
| 4852 | verbose(env, "%d", depth); |
| 4853 | if (i + 1 < env->subprog_cnt + 1) |
| 4854 | verbose(env, "+"); |
| 4855 | } |
| 4856 | verbose(env, "\n"); |
| 4857 | env->prog->aux->stack_depth = env->subprog_stack_depth[0]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4858 | return 0; |
| 4859 | } |
| 4860 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4861 | static int check_map_prealloc(struct bpf_map *map) |
| 4862 | { |
| 4863 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 4864 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 4865 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4866 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 4867 | } |
| 4868 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4869 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 4870 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4871 | struct bpf_prog *prog) |
| 4872 | |
| 4873 | { |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4874 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 4875 | * preallocated hash maps, since doing memory allocation |
| 4876 | * in overflow_handler can crash depending on where nmi got |
| 4877 | * triggered. |
| 4878 | */ |
| 4879 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 4880 | if (!check_map_prealloc(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4881 | 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] | 4882 | return -EINVAL; |
| 4883 | } |
| 4884 | if (map->inner_map_meta && |
| 4885 | !check_map_prealloc(map->inner_map_meta)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4886 | 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] | 4887 | return -EINVAL; |
| 4888 | } |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4889 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 4890 | |
| 4891 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
| 4892 | !bpf_offload_dev_match(prog, map)) { |
| 4893 | verbose(env, "offload device mismatch between prog and map\n"); |
| 4894 | return -EINVAL; |
| 4895 | } |
| 4896 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4897 | return 0; |
| 4898 | } |
| 4899 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4900 | /* look for pseudo eBPF instructions that access map FDs and |
| 4901 | * replace them with actual map pointers |
| 4902 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4903 | 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] | 4904 | { |
| 4905 | struct bpf_insn *insn = env->prog->insnsi; |
| 4906 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4907 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4908 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 4909 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 4910 | if (err) |
| 4911 | return err; |
| 4912 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4913 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4914 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4915 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4916 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4917 | return -EINVAL; |
| 4918 | } |
| 4919 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4920 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 4921 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 4922 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4923 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4924 | return -EINVAL; |
| 4925 | } |
| 4926 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4927 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
| 4928 | struct bpf_map *map; |
| 4929 | struct fd f; |
| 4930 | |
| 4931 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 4932 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 4933 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4934 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4935 | return -EINVAL; |
| 4936 | } |
| 4937 | |
| 4938 | if (insn->src_reg == 0) |
| 4939 | /* valid generic load 64-bit imm */ |
| 4940 | goto next_insn; |
| 4941 | |
| 4942 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4943 | verbose(env, |
| 4944 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4945 | return -EINVAL; |
| 4946 | } |
| 4947 | |
| 4948 | f = fdget(insn->imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 4949 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4950 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4951 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4952 | insn->imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4953 | return PTR_ERR(map); |
| 4954 | } |
| 4955 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4956 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4957 | if (err) { |
| 4958 | fdput(f); |
| 4959 | return err; |
| 4960 | } |
| 4961 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4962 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
| 4963 | insn[0].imm = (u32) (unsigned long) map; |
| 4964 | insn[1].imm = ((u64) (unsigned long) map) >> 32; |
| 4965 | |
| 4966 | /* check whether we recorded this map already */ |
| 4967 | for (j = 0; j < env->used_map_cnt; j++) |
| 4968 | if (env->used_maps[j] == map) { |
| 4969 | fdput(f); |
| 4970 | goto next_insn; |
| 4971 | } |
| 4972 | |
| 4973 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 4974 | fdput(f); |
| 4975 | return -E2BIG; |
| 4976 | } |
| 4977 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4978 | /* hold the map. If the program is rejected by verifier, |
| 4979 | * the map will be released by release_maps() or it |
| 4980 | * will be used by the valid program until it's unloaded |
| 4981 | * and all maps are released in free_bpf_prog_info() |
| 4982 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 4983 | map = bpf_map_inc(map, false); |
| 4984 | if (IS_ERR(map)) { |
| 4985 | fdput(f); |
| 4986 | return PTR_ERR(map); |
| 4987 | } |
| 4988 | env->used_maps[env->used_map_cnt++] = map; |
| 4989 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4990 | fdput(f); |
| 4991 | next_insn: |
| 4992 | insn++; |
| 4993 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 4994 | continue; |
| 4995 | } |
| 4996 | |
| 4997 | /* Basic sanity check before we invest more work here. */ |
| 4998 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 4999 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 5000 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5001 | } |
| 5002 | } |
| 5003 | |
| 5004 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 5005 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 5006 | * These pointers will be used later by verifier to validate map access. |
| 5007 | */ |
| 5008 | return 0; |
| 5009 | } |
| 5010 | |
| 5011 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5012 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5013 | { |
| 5014 | int i; |
| 5015 | |
| 5016 | for (i = 0; i < env->used_map_cnt; i++) |
| 5017 | bpf_map_put(env->used_maps[i]); |
| 5018 | } |
| 5019 | |
| 5020 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5021 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5022 | { |
| 5023 | struct bpf_insn *insn = env->prog->insnsi; |
| 5024 | int insn_cnt = env->prog->len; |
| 5025 | int i; |
| 5026 | |
| 5027 | for (i = 0; i < insn_cnt; i++, insn++) |
| 5028 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 5029 | insn->src_reg = 0; |
| 5030 | } |
| 5031 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5032 | /* single env->prog->insni[off] instruction was replaced with the range |
| 5033 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 5034 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 5035 | */ |
| 5036 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, |
| 5037 | u32 off, u32 cnt) |
| 5038 | { |
| 5039 | 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] | 5040 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5041 | |
| 5042 | if (cnt == 1) |
| 5043 | return 0; |
| 5044 | new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len); |
| 5045 | if (!new_data) |
| 5046 | return -ENOMEM; |
| 5047 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 5048 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 5049 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5050 | for (i = off; i < off + cnt - 1; i++) |
| 5051 | new_data[i].seen = true; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5052 | env->insn_aux_data = new_data; |
| 5053 | vfree(old_data); |
| 5054 | return 0; |
| 5055 | } |
| 5056 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5057 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 5058 | { |
| 5059 | int i; |
| 5060 | |
| 5061 | if (len == 1) |
| 5062 | return; |
| 5063 | for (i = 0; i < env->subprog_cnt; i++) { |
| 5064 | if (env->subprog_starts[i] < off) |
| 5065 | continue; |
| 5066 | env->subprog_starts[i] += len - 1; |
| 5067 | } |
| 5068 | } |
| 5069 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5070 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 5071 | const struct bpf_insn *patch, u32 len) |
| 5072 | { |
| 5073 | struct bpf_prog *new_prog; |
| 5074 | |
| 5075 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
| 5076 | if (!new_prog) |
| 5077 | return NULL; |
| 5078 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) |
| 5079 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5080 | adjust_subprog_starts(env, off, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5081 | return new_prog; |
| 5082 | } |
| 5083 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 5084 | /* The verifier does more data flow analysis than llvm and will not |
| 5085 | * explore branches that are dead at run time. Malicious programs can |
| 5086 | * have dead code too. Therefore replace all dead at-run-time code |
| 5087 | * with 'ja -1'. |
| 5088 | * |
| 5089 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 5090 | * program and through another bug we would manage to jump there, then |
| 5091 | * we'd execute beyond program memory otherwise. Returning exception |
| 5092 | * code also wouldn't work since we can have subprogs where the dead |
| 5093 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5094 | */ |
| 5095 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 5096 | { |
| 5097 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 5098 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5099 | struct bpf_insn *insn = env->prog->insnsi; |
| 5100 | const int insn_cnt = env->prog->len; |
| 5101 | int i; |
| 5102 | |
| 5103 | for (i = 0; i < insn_cnt; i++) { |
| 5104 | if (aux_data[i].seen) |
| 5105 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 5106 | memcpy(insn + i, &trap, sizeof(trap)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5107 | } |
| 5108 | } |
| 5109 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5110 | /* convert load instructions that access fields of 'struct __sk_buff' |
| 5111 | * into sequence of instructions that access fields of 'struct sk_buff' |
| 5112 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5113 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5114 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5115 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5116 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5117 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5118 | struct bpf_insn insn_buf[16], *insn; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5119 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5120 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5121 | bool is_narrower_load; |
| 5122 | u32 target_size; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5123 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5124 | if (ops->gen_prologue) { |
| 5125 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 5126 | env->prog); |
| 5127 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5128 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5129 | return -EINVAL; |
| 5130 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5131 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5132 | if (!new_prog) |
| 5133 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5134 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5135 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5136 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5137 | } |
| 5138 | } |
| 5139 | |
| 5140 | if (!ops->convert_ctx_access) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5141 | return 0; |
| 5142 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5143 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5144 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5145 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 5146 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 5147 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 5148 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 5149 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5150 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 5151 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 5152 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 5153 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 5154 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5155 | type = BPF_WRITE; |
| 5156 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5157 | continue; |
| 5158 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5159 | if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5160 | continue; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5161 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5162 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5163 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5164 | |
| 5165 | /* If the read access is a narrower load of the field, |
| 5166 | * convert to a 4/8-byte load, to minimum program type specific |
| 5167 | * convert_ctx_access changes. If conversion is successful, |
| 5168 | * we will apply proper mask to the result. |
| 5169 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5170 | is_narrower_load = size < ctx_field_size; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5171 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5172 | u32 off = insn->off; |
| 5173 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5174 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5175 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5176 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5177 | return -EINVAL; |
| 5178 | } |
| 5179 | |
| 5180 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5181 | if (ctx_field_size == 4) |
| 5182 | size_code = BPF_W; |
| 5183 | else if (ctx_field_size == 8) |
| 5184 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5185 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5186 | insn->off = off & ~(ctx_field_size - 1); |
| 5187 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 5188 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5189 | |
| 5190 | target_size = 0; |
| 5191 | cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog, |
| 5192 | &target_size); |
| 5193 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 5194 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5195 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5196 | return -EINVAL; |
| 5197 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5198 | |
| 5199 | if (is_narrower_load && size < target_size) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5200 | if (ctx_field_size <= 4) |
| 5201 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5202 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5203 | else |
| 5204 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5205 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5206 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5207 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5208 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5209 | if (!new_prog) |
| 5210 | return -ENOMEM; |
| 5211 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5212 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5213 | |
| 5214 | /* keep walking new program and skip insns we just inserted */ |
| 5215 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5216 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | return 0; |
| 5220 | } |
| 5221 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5222 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 5223 | { |
| 5224 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 5225 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5226 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5227 | void *old_bpf_func; |
| 5228 | int err = -ENOMEM; |
| 5229 | |
| 5230 | if (env->subprog_cnt == 0) |
| 5231 | return 0; |
| 5232 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5233 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5234 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5235 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5236 | continue; |
| 5237 | subprog = find_subprog(env, i + insn->imm + 1); |
| 5238 | if (subprog < 0) { |
| 5239 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 5240 | i + insn->imm + 1); |
| 5241 | return -EFAULT; |
| 5242 | } |
| 5243 | /* temporarily remember subprog id inside insn instead of |
| 5244 | * aux_data, since next loop will split up all insns into funcs |
| 5245 | */ |
| 5246 | insn->off = subprog + 1; |
| 5247 | /* remember original imm in case JIT fails and fallback |
| 5248 | * to interpreter will be needed |
| 5249 | */ |
| 5250 | env->insn_aux_data[i].call_imm = insn->imm; |
| 5251 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 5252 | insn->imm = 1; |
| 5253 | } |
| 5254 | |
| 5255 | func = kzalloc(sizeof(prog) * (env->subprog_cnt + 1), GFP_KERNEL); |
| 5256 | if (!func) |
| 5257 | return -ENOMEM; |
| 5258 | |
| 5259 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 5260 | subprog_start = subprog_end; |
| 5261 | if (env->subprog_cnt == i) |
| 5262 | subprog_end = prog->len; |
| 5263 | else |
| 5264 | subprog_end = env->subprog_starts[i]; |
| 5265 | |
| 5266 | len = subprog_end - subprog_start; |
| 5267 | func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER); |
| 5268 | if (!func[i]) |
| 5269 | goto out_free; |
| 5270 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 5271 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 5272 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5273 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 5274 | if (bpf_prog_calc_tag(func[i])) |
| 5275 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5276 | func[i]->is_func = 1; |
| 5277 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 5278 | * Long term would need debug info to populate names |
| 5279 | */ |
| 5280 | func[i]->aux->name[0] = 'F'; |
| 5281 | func[i]->aux->stack_depth = env->subprog_stack_depth[i]; |
| 5282 | func[i]->jit_requested = 1; |
| 5283 | func[i] = bpf_int_jit_compile(func[i]); |
| 5284 | if (!func[i]->jited) { |
| 5285 | err = -ENOTSUPP; |
| 5286 | goto out_free; |
| 5287 | } |
| 5288 | cond_resched(); |
| 5289 | } |
| 5290 | /* at this point all bpf functions were successfully JITed |
| 5291 | * now populate all bpf_calls with correct addresses and |
| 5292 | * run last pass of JIT |
| 5293 | */ |
| 5294 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 5295 | insn = func[i]->insnsi; |
| 5296 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 5297 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5298 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5299 | continue; |
| 5300 | subprog = insn->off; |
| 5301 | insn->off = 0; |
| 5302 | insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) |
| 5303 | func[subprog]->bpf_func - |
| 5304 | __bpf_call_base; |
| 5305 | } |
| 5306 | } |
| 5307 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 5308 | old_bpf_func = func[i]->bpf_func; |
| 5309 | tmp = bpf_int_jit_compile(func[i]); |
| 5310 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 5311 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
| 5312 | err = -EFAULT; |
| 5313 | goto out_free; |
| 5314 | } |
| 5315 | cond_resched(); |
| 5316 | } |
| 5317 | |
| 5318 | /* finally lock prog and jit images for all functions and |
| 5319 | * populate kallsysm |
| 5320 | */ |
| 5321 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 5322 | bpf_prog_lock_ro(func[i]); |
| 5323 | bpf_prog_kallsyms_add(func[i]); |
| 5324 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5325 | |
| 5326 | /* Last step: make now unused interpreter insns from main |
| 5327 | * prog consistent for later dump requests, so they can |
| 5328 | * later look the same as if they were interpreted only. |
| 5329 | */ |
| 5330 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 5331 | unsigned long addr; |
| 5332 | |
| 5333 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5334 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5335 | continue; |
| 5336 | insn->off = env->insn_aux_data[i].call_imm; |
| 5337 | subprog = find_subprog(env, i + insn->off + 1); |
| 5338 | addr = (unsigned long)func[subprog + 1]->bpf_func; |
| 5339 | addr &= PAGE_MASK; |
| 5340 | insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) |
| 5341 | addr - __bpf_call_base; |
| 5342 | } |
| 5343 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5344 | prog->jited = 1; |
| 5345 | prog->bpf_func = func[0]->bpf_func; |
| 5346 | prog->aux->func = func; |
| 5347 | prog->aux->func_cnt = env->subprog_cnt + 1; |
| 5348 | return 0; |
| 5349 | out_free: |
| 5350 | for (i = 0; i <= env->subprog_cnt; i++) |
| 5351 | if (func[i]) |
| 5352 | bpf_jit_free(func[i]); |
| 5353 | kfree(func); |
| 5354 | /* cleanup main prog to be interpreted */ |
| 5355 | prog->jit_requested = 0; |
| 5356 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 5357 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5358 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5359 | continue; |
| 5360 | insn->off = 0; |
| 5361 | insn->imm = env->insn_aux_data[i].call_imm; |
| 5362 | } |
| 5363 | return err; |
| 5364 | } |
| 5365 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5366 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 5367 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5368 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5369 | struct bpf_prog *prog = env->prog; |
| 5370 | struct bpf_insn *insn = prog->insnsi; |
| 5371 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5372 | #endif |
| 5373 | int err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5374 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5375 | err = 0; |
| 5376 | if (env->prog->jit_requested) { |
| 5377 | err = jit_subprogs(env); |
| 5378 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5379 | return 0; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5380 | } |
| 5381 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5382 | for (i = 0; i < prog->len; i++, insn++) { |
| 5383 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5384 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5385 | continue; |
| 5386 | depth = get_callee_stack_depth(env, insn, i); |
| 5387 | if (depth < 0) |
| 5388 | return depth; |
| 5389 | bpf_patch_call_args(insn, depth); |
| 5390 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5391 | err = 0; |
| 5392 | #endif |
| 5393 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5394 | } |
| 5395 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5396 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5397 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5398 | * |
| 5399 | * this function is called after eBPF program passed verification |
| 5400 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5401 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5402 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5403 | struct bpf_prog *prog = env->prog; |
| 5404 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5405 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5406 | const int insn_cnt = prog->len; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5407 | struct bpf_insn insn_buf[16]; |
| 5408 | struct bpf_prog *new_prog; |
| 5409 | struct bpf_map *map_ptr; |
| 5410 | int i, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5411 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5412 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 5413 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 5414 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 5415 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 5416 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 5417 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
| 5418 | struct bpf_insn mask_and_div[] = { |
| 5419 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 5420 | /* Rx div 0 -> 0 */ |
| 5421 | BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2), |
| 5422 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 5423 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 5424 | *insn, |
| 5425 | }; |
| 5426 | struct bpf_insn mask_and_mod[] = { |
| 5427 | BPF_MOV32_REG(insn->src_reg, insn->src_reg), |
| 5428 | /* Rx mod 0 -> Rx */ |
| 5429 | BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1), |
| 5430 | *insn, |
| 5431 | }; |
| 5432 | struct bpf_insn *patchlet; |
| 5433 | |
| 5434 | if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 5435 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
| 5436 | patchlet = mask_and_div + (is64 ? 1 : 0); |
| 5437 | cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0); |
| 5438 | } else { |
| 5439 | patchlet = mask_and_mod + (is64 ? 1 : 0); |
| 5440 | cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0); |
| 5441 | } |
| 5442 | |
| 5443 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 5444 | if (!new_prog) |
| 5445 | return -ENOMEM; |
| 5446 | |
| 5447 | delta += cnt - 1; |
| 5448 | env->prog = prog = new_prog; |
| 5449 | insn = new_prog->insnsi + i + delta; |
| 5450 | continue; |
| 5451 | } |
| 5452 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5453 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 5454 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5455 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 5456 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5457 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5458 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 5459 | prog->dst_needed = 1; |
| 5460 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 5461 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 5462 | if (insn->imm == BPF_FUNC_override_return) |
| 5463 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5464 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 5465 | /* If we tail call into other programs, we |
| 5466 | * cannot make any assumptions since they can |
| 5467 | * be replaced dynamically during runtime in |
| 5468 | * the program array. |
| 5469 | */ |
| 5470 | prog->cb_access = 1; |
Alexei Starovoitov | 80a58d0 | 2017-05-30 13:31:30 -0700 | [diff] [blame] | 5471 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 5472 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5473 | /* mark bpf_tail_call as different opcode to avoid |
| 5474 | * conditional branch in the interpeter for every normal |
| 5475 | * call and to prevent accidental JITing by JIT compiler |
| 5476 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5477 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5478 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 5479 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 5480 | |
| 5481 | /* instead of changing every JIT dealing with tail_call |
| 5482 | * emit two extra insns: |
| 5483 | * if (index >= max_entries) goto out; |
| 5484 | * index &= array->index_mask; |
| 5485 | * to avoid out-of-bounds cpu speculation |
| 5486 | */ |
| 5487 | map_ptr = env->insn_aux_data[i + delta].map_ptr; |
| 5488 | if (map_ptr == BPF_MAP_PTR_POISON) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 5489 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 5490 | return -EINVAL; |
| 5491 | } |
| 5492 | if (!map_ptr->unpriv_array) |
| 5493 | continue; |
| 5494 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 5495 | map_ptr->max_entries, 2); |
| 5496 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 5497 | container_of(map_ptr, |
| 5498 | struct bpf_array, |
| 5499 | map)->index_mask); |
| 5500 | insn_buf[2] = *insn; |
| 5501 | cnt = 3; |
| 5502 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 5503 | if (!new_prog) |
| 5504 | return -ENOMEM; |
| 5505 | |
| 5506 | delta += cnt - 1; |
| 5507 | env->prog = prog = new_prog; |
| 5508 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5509 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5510 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5511 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 5512 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
| 5513 | * handlers are currently limited to 64 bit only. |
| 5514 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 5515 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 5516 | insn->imm == BPF_FUNC_map_lookup_elem) { |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5517 | map_ptr = env->insn_aux_data[i + delta].map_ptr; |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 5518 | if (map_ptr == BPF_MAP_PTR_POISON || |
| 5519 | !map_ptr->ops->map_gen_lookup) |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5520 | goto patch_call_imm; |
| 5521 | |
| 5522 | cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf); |
| 5523 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5524 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5525 | return -EINVAL; |
| 5526 | } |
| 5527 | |
| 5528 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, |
| 5529 | cnt); |
| 5530 | if (!new_prog) |
| 5531 | return -ENOMEM; |
| 5532 | |
| 5533 | delta += cnt - 1; |
| 5534 | |
| 5535 | /* keep walking new program and skip insns we just inserted */ |
| 5536 | env->prog = prog = new_prog; |
| 5537 | insn = new_prog->insnsi + i + delta; |
| 5538 | continue; |
| 5539 | } |
| 5540 | |
Daniel Borkmann | 109980b | 2017-09-08 00:14:51 +0200 | [diff] [blame] | 5541 | if (insn->imm == BPF_FUNC_redirect_map) { |
Daniel Borkmann | 7c30013 | 2017-09-20 00:44:21 +0200 | [diff] [blame] | 5542 | /* Note, we cannot use prog directly as imm as subsequent |
| 5543 | * rewrites would still change the prog pointer. The only |
| 5544 | * stable address we can use is aux, which also works with |
| 5545 | * prog clones during blinding. |
| 5546 | */ |
| 5547 | u64 addr = (unsigned long)prog->aux; |
Daniel Borkmann | 109980b | 2017-09-08 00:14:51 +0200 | [diff] [blame] | 5548 | struct bpf_insn r4_ld[] = { |
| 5549 | BPF_LD_IMM64(BPF_REG_4, addr), |
| 5550 | *insn, |
| 5551 | }; |
| 5552 | cnt = ARRAY_SIZE(r4_ld); |
| 5553 | |
| 5554 | new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt); |
| 5555 | if (!new_prog) |
| 5556 | return -ENOMEM; |
| 5557 | |
| 5558 | delta += cnt - 1; |
| 5559 | env->prog = prog = new_prog; |
| 5560 | insn = new_prog->insnsi + i + delta; |
| 5561 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5562 | patch_call_imm: |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5563 | fn = env->ops->get_func_proto(insn->imm); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5564 | /* all functions that have prototype and verifier allowed |
| 5565 | * programs to call them, must be real in-kernel functions |
| 5566 | */ |
| 5567 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5568 | verbose(env, |
| 5569 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5570 | func_id_name(insn->imm), insn->imm); |
| 5571 | return -EFAULT; |
| 5572 | } |
| 5573 | insn->imm = fn->func - __bpf_call_base; |
| 5574 | } |
| 5575 | |
| 5576 | return 0; |
| 5577 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5578 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5579 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5580 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5581 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5582 | int i; |
| 5583 | |
| 5584 | if (!env->explored_states) |
| 5585 | return; |
| 5586 | |
| 5587 | for (i = 0; i < env->prog->len; i++) { |
| 5588 | sl = env->explored_states[i]; |
| 5589 | |
| 5590 | if (sl) |
| 5591 | while (sl != STATE_LIST_MARK) { |
| 5592 | sln = sl->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 5593 | free_verifier_state(&sl->state, false); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5594 | kfree(sl); |
| 5595 | sl = sln; |
| 5596 | } |
| 5597 | } |
| 5598 | |
| 5599 | kfree(env->explored_states); |
| 5600 | } |
| 5601 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5602 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5603 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5604 | struct bpf_verifier_env *env; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5605 | struct bpf_verifer_log *log; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5606 | int ret = -EINVAL; |
| 5607 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 5608 | /* no program is valid */ |
| 5609 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 5610 | return -EINVAL; |
| 5611 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5612 | /* '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] | 5613 | * allocate/free it every time bpf_check() is called |
| 5614 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5615 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5616 | if (!env) |
| 5617 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5618 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5619 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5620 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
| 5621 | (*prog)->len); |
| 5622 | ret = -ENOMEM; |
| 5623 | if (!env->insn_aux_data) |
| 5624 | goto err_free_env; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5625 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5626 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5627 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5628 | /* grab the mutex to protect few globals used by verifier */ |
| 5629 | mutex_lock(&bpf_verifier_lock); |
| 5630 | |
| 5631 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 5632 | /* user requested verbose verifier output |
| 5633 | * and supplied buffer to store the verification trace |
| 5634 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 5635 | log->level = attr->log_level; |
| 5636 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 5637 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5638 | |
| 5639 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 5640 | /* log attributes have to be sane */ |
| 5641 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || |
| 5642 | !log->level || !log->ubuf) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5643 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5644 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 5645 | |
| 5646 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 5647 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 5648 | env->strict_alignment = true; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5649 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 5650 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 5651 | ret = bpf_prog_offload_verifier_prep(env); |
| 5652 | if (ret) |
| 5653 | goto err_unlock; |
| 5654 | } |
| 5655 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5656 | ret = replace_map_fd_with_map_ptr(env); |
| 5657 | if (ret < 0) |
| 5658 | goto skip_full_check; |
| 5659 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5660 | env->explored_states = kcalloc(env->prog->len, |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5661 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5662 | GFP_USER); |
| 5663 | ret = -ENOMEM; |
| 5664 | if (!env->explored_states) |
| 5665 | goto skip_full_check; |
| 5666 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5667 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 5668 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5669 | ret = check_cfg(env); |
| 5670 | if (ret < 0) |
| 5671 | goto skip_full_check; |
| 5672 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5673 | ret = do_check(env); |
Craig Gallek | 8c01c4f | 2017-11-02 11:18:01 -0400 | [diff] [blame] | 5674 | if (env->cur_state) { |
| 5675 | free_verifier_state(env->cur_state, true); |
| 5676 | env->cur_state = NULL; |
| 5677 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5678 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5679 | skip_full_check: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5680 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5681 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5682 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5683 | if (ret == 0) |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5684 | sanitize_dead_code(env); |
| 5685 | |
| 5686 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 5687 | ret = check_max_stack_depth(env); |
| 5688 | |
| 5689 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5690 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 5691 | ret = convert_ctx_accesses(env); |
| 5692 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5693 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5694 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5695 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5696 | if (ret == 0) |
| 5697 | ret = fixup_call_args(env); |
| 5698 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5699 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5700 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5701 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5702 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5703 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5704 | } |
| 5705 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5706 | if (ret == 0 && env->used_map_cnt) { |
| 5707 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5708 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 5709 | sizeof(env->used_maps[0]), |
| 5710 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5711 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5712 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5713 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5714 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5715 | } |
| 5716 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5717 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5718 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5719 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5720 | |
| 5721 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 5722 | * bpf_ld_imm64 instructions |
| 5723 | */ |
| 5724 | convert_pseudo_ld_imm64(env); |
| 5725 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5726 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5727 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5728 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5729 | /* if we didn't copy map pointers into bpf_prog_info, release |
| 5730 | * them now. Otherwise free_bpf_prog_info() will release them. |
| 5731 | */ |
| 5732 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5733 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5734 | err_unlock: |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5735 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5736 | vfree(env->insn_aux_data); |
| 5737 | err_free_env: |
| 5738 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5739 | return ret; |
| 5740 | } |